Commit ab02f37
committed
feat(mdviewer): line-scoped reflow with per-source-line cursor sync
Turndown emits each block as one physical line regardless of width, so
typing or pasting a long paragraph in the md viewer wrote one very long
line to CM on every save. Adds a line-scoped reflow that wraps only the
lines the user actually edited, behind a new opt-out preference
mdViewerWrapEditedLines (default true) using EditorOptionHandlers
.getMaxLineLength() as the width source.
New module markdown-line-wrap.js (with 26 unit specs):
- Prefix/suffix line-range diff so unchanged lines stay byte-identical.
- Indent-aware continuation: bullets (- * +), ordered lists (N. / N)),
and blockquotes drive the continuation indent on wrapped lines.
- Inline atoms (image-only links like [ ](href), plain
image links, inline links, inline code, inline HTML tags) are kept
whole by tokenization. Single oversize tokens are left intact.
- Conservative skip rules: fenced code, tables, ATX headings, setext
underlines, link reference definitions, hr, HTML blocks, frontmatter.
- Balanced packer: binary-searches for the smallest width that still
produces the greedy line count, so the two physical lines come out
e.g. 95+90 instead of 118+67 for a 186-char paragraph. Critical for
the cursor-sync precision in the rendered viewer (see below).
- Fast-path early exit when no changed line exceeds printWidth, so
typical typing keystrokes never run the regex-heavy state scan.
Per-source-line cursor sync in the markdown viewer (bridge.js + editor.js):
- The custom paragraph renderer in bridge.js now wraps each source line
of a multi-line paragraph in <span data-source-line="N">. Without
these the whole <p> shared a single data-source-line and cursor sync
always resolved to the block's first line, so moving the caret
through a wrapped paragraph never updated the CM highlight.
- editor.js _updateSourceLineAttrs keeps those spans in sync as the
user edits. _refreshParagraphSourceSpans no-ops when the layout is
unchanged, updates attributes in place when only the start line
shifted, and rebuilds inner HTML (preserving caret by character
offset) when the wrap line count actually changed.
- Fixes a pre-existing _updateSourceLineAttrs filter bug: it was
skipping any element with the cursor-sync-highlight class, but that
class is added to real content blocks (<p>, <h1>) for highlighting
purposes. Skipping them threw mdLineIdx off-by-one for every block
after the highlighted one. Now we only skip the standalone overlay
variants (cursor-sync-br-line, cursor-sync-code-line).
Tests:
- 26 unit specs in unit:markdown-line-wrap cover bullets, nested lists,
ordered lists, blockquotes, fences, tables, headings, link refs,
inline atoms, frontmatter, and the badge-row regression.
- The existing 58-spec livepreview:Markdown Editor 1 suite still passes.1 parent 0da2c61 commit ab02f37
6 files changed
Lines changed: 844 additions & 3 deletions
File tree
- src-mdviewer/src
- components
- src/extensionsIntegrated/Phoenix-live-preview
- test
- spec
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
191 | 215 | | |
192 | 216 | | |
193 | 217 | | |
194 | | - | |
| 218 | + | |
195 | 219 | | |
196 | 220 | | |
197 | 221 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
1799 | 1800 | | |
1800 | 1801 | | |
1801 | 1802 | | |
1802 | | - | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
1803 | 1809 | | |
1804 | 1810 | | |
1805 | 1811 | | |
1806 | 1812 | | |
1807 | | - | |
| 1813 | + | |
| 1814 | + | |
1808 | 1815 | | |
1809 | 1816 | | |
1810 | 1817 | | |
| |||
1840 | 1847 | | |
1841 | 1848 | | |
1842 | 1849 | | |
| 1850 | + | |
1843 | 1851 | | |
1844 | 1852 | | |
1845 | 1853 | | |
1846 | 1854 | | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
1847 | 1905 | | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
1848 | 1957 | | |
1849 | 1958 | | |
1850 | 1959 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
35 | 44 | | |
36 | 45 | | |
37 | 46 | | |
| |||
620 | 629 | | |
621 | 630 | | |
622 | 631 | | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
623 | 647 | | |
624 | 648 | | |
625 | 649 | | |
| |||
0 commit comments