|
2 | 2 |
|
3 | 3 | <script lang="ts"> |
4 | 4 | import type { Editor } from 'codemirror'; |
| 5 | + import type { Root, Element } from 'hast'; |
5 | 6 | import type { BytemdPlugin, EditorProps, ViewerProps } from './types'; |
6 | 7 | import { onMount, createEventDispatcher, onDestroy, tick } from 'svelte'; |
7 | | - import { debounce } from 'lodash-es'; |
| 8 | + import { debounce, throttle } from 'lodash-es'; |
| 9 | + import cx from 'classnames'; |
8 | 10 | import Toolbar from './toolbar.svelte'; |
9 | 11 | import Viewer from './viewer.svelte'; |
10 | | - import { createUtils } from './editor'; |
11 | | - import { scrollSync } from './plugins'; |
| 12 | + import Toc from './toc.svelte'; |
| 13 | + import { createUtils, findStartIndex } from './editor'; |
12 | 14 | import Status from './status.svelte'; |
| 15 | + import Help from './help.svelte'; |
| 16 | + import { icons } from './icons'; |
13 | 17 |
|
14 | 18 | export let value: EditorProps['value'] = ''; |
15 | 19 | export let plugins: NonNullable<EditorProps['plugins']> = []; |
|
19 | 23 | export let placeholder: EditorProps['placeholder']; |
20 | 24 | export let editorConfig: EditorProps['editorConfig']; |
21 | 25 |
|
22 | | - let scrollSyncInstance = scrollSync(); |
23 | | -
|
24 | | - $: fullPlugins = (() => { |
25 | | - const ps = [...plugins]; |
26 | | - if (mode === 'split' && scrollSyncEnabled) { |
27 | | - ps.push(scrollSyncInstance); |
28 | | - } |
29 | | - return ps; |
30 | | - })(); |
31 | | -
|
32 | 26 | let el: HTMLElement; |
33 | 27 | let previewEl: HTMLElement; |
34 | | - let viewerProps: ViewerProps = { |
35 | | - value, |
36 | | - plugins, |
37 | | - sanitize, |
38 | | - }; |
39 | 28 | let textarea: HTMLTextAreaElement; |
| 29 | +
|
| 30 | + let viewerProps: ViewerProps = { value, plugins, sanitize }; |
40 | 31 | let editor: Editor; |
41 | 32 | let activeTab = 0; |
42 | 33 | let fullscreen = false; |
43 | | - let scrollSyncEnabled = true; |
| 34 | + let sidebar: false | 'help' | 'toc' = 'toc'; // false; |
| 35 | +
|
| 36 | + $: styles = (() => { |
| 37 | + let edit: string; |
| 38 | + let preview: string; |
| 39 | +
|
| 40 | + if (mode === 'tab') { |
| 41 | + if (activeTab === 0) { |
| 42 | + edit = `width:calc(100% - ${sidebar ? 280 : 0}px)`; |
| 43 | + preview = 'display:none'; |
| 44 | + } else { |
| 45 | + edit = 'display:none'; |
| 46 | + preview = `width:calc(100% - ${sidebar ? 280 : 0}px)`; |
| 47 | + } |
| 48 | + } else { |
| 49 | + if (sidebar) { |
| 50 | + edit = `width:calc(50% - ${sidebar ? 140 : 0}px)`; |
| 51 | + preview = `width:calc(50% - ${sidebar ? 140 : 0}px)`; |
| 52 | + } else { |
| 53 | + edit = 'width:50%'; |
| 54 | + preview = 'width:50%'; |
| 55 | + } |
| 56 | + } |
| 57 | +
|
| 58 | + return { edit, preview }; |
| 59 | + })(); |
44 | 60 |
|
45 | 61 | $: context = { editor, $el: el, utils: createUtils(editor) }; |
46 | 62 |
|
47 | 63 | let cbs: ReturnType<NonNullable<BytemdPlugin['editorEffect']>>[] = []; |
48 | 64 | const dispatch = createEventDispatcher(); |
49 | 65 |
|
50 | | - // @ts-ignore |
51 | | - function setActiveTab(e) { |
52 | | - activeTab = e.detail.value; |
53 | | - if (editor && activeTab === 0) { |
54 | | - tick().then(() => { |
55 | | - editor.focus(); |
56 | | - }); |
57 | | - } |
58 | | - } |
59 | | -
|
60 | 66 | function on() { |
61 | | - // console.log('on', fullPlugins); |
62 | | - cbs = fullPlugins.map((p) => p.editorEffect?.(context)); |
| 67 | + // console.log('on', plugins); |
| 68 | + cbs = plugins.map((p) => p.editorEffect?.(context)); |
63 | 69 | } |
64 | 70 | function off() { |
65 | | - // console.log('off', fullPlugins); |
| 71 | + // console.log('off', plugins); |
66 | 72 | cbs.forEach((cb) => cb && cb()); |
67 | 73 | } |
68 | 74 |
|
69 | 75 | const updateViewerValue = debounce(() => { |
70 | | - viewerProps = { |
71 | | - value, |
72 | | - plugins: fullPlugins, |
73 | | - sanitize, |
74 | | - }; |
| 76 | + viewerProps = { value, plugins, sanitize }; |
75 | 77 | }, previewDebounce); |
76 | 78 |
|
77 | 79 | $: if (editor && value !== editor.getValue()) { |
78 | 80 | editor.setValue(value); |
79 | 81 | } |
80 | 82 | $: if (value != null) updateViewerValue(); |
81 | 83 |
|
82 | | - $: if (editor && el && fullPlugins) { |
| 84 | + $: if (editor && el && plugins && hast) { |
83 | 85 | off(); |
84 | 86 | tick().then(() => { |
85 | 87 | on(); |
86 | 88 | }); |
87 | 89 | } |
88 | 90 |
|
| 91 | + // Scroll sync vars |
| 92 | + let scrollSyncEnabled = true; |
| 93 | + let editCalled = false; |
| 94 | + let previewCalled = false; |
| 95 | + let editPs: number[]; |
| 96 | + let previewPs: number[]; |
| 97 | + let hast: Root = { type: 'root', children: [] }; |
| 98 | + let currentBlockIndex = 0; |
| 99 | +
|
89 | 100 | onMount(async () => { |
90 | 101 | const [codemirror] = await Promise.all([ |
91 | 102 | import('codemirror'), |
|
104 | 115 | }); |
105 | 116 |
|
106 | 117 | // https://github.com/codemirror/CodeMirror/issues/2428#issuecomment-39315423 |
107 | | - editor.addKeyMap({ |
108 | | - 'Shift-Tab': 'indentLess', |
109 | | - }); |
| 118 | + editor.addKeyMap({ 'Shift-Tab': 'indentLess' }); |
110 | 119 | editor.setValue(value); |
111 | 120 | editor.on('change', (doc, change) => { |
112 | 121 | dispatch('change', { value: editor.getValue() }); |
113 | 122 | }); |
114 | 123 |
|
| 124 | + const updateBlockPositions = throttle(() => { |
| 125 | + editPs = []; |
| 126 | + previewPs = []; |
| 127 | +
|
| 128 | + const scrollInfo = editor.getScrollInfo(); |
| 129 | + const body = previewEl.querySelector<HTMLElement>('.markdown-body')!; |
| 130 | +
|
| 131 | + const leftNodes = hast.children.filter( |
| 132 | + (v) => v.type === 'element' |
| 133 | + ) as Element[]; |
| 134 | + const rightNodes = [...body.childNodes].filter( |
| 135 | + (v): v is HTMLElement => v instanceof HTMLElement |
| 136 | + ); |
| 137 | +
|
| 138 | + for (let i = 0; i < leftNodes.length; i++) { |
| 139 | + const leftNode = leftNodes[i]; |
| 140 | + const rightNode = rightNodes[i]; |
| 141 | +
|
| 142 | + // if there is no position info, move to the next node |
| 143 | + if (!leftNode.position) { |
| 144 | + continue; |
| 145 | + } |
| 146 | +
|
| 147 | + const left = |
| 148 | + editor.heightAtLine(leftNode.position.start.line - 1, 'local') / |
| 149 | + (scrollInfo.height - scrollInfo.clientHeight); |
| 150 | + const right = |
| 151 | + (rightNode.offsetTop - body.offsetTop) / |
| 152 | + (previewEl.scrollHeight - previewEl.clientHeight); |
| 153 | +
|
| 154 | + if (left >= 1 || right >= 1) { |
| 155 | + break; |
| 156 | + } |
| 157 | +
|
| 158 | + editPs.push(left); |
| 159 | + previewPs.push(right); |
| 160 | + } |
| 161 | +
|
| 162 | + editPs.push(1); |
| 163 | + previewPs.push(1); |
| 164 | + // console.log(editPs, previewPs); |
| 165 | + }, 1000); |
| 166 | + const editorScrollHandler = () => { |
| 167 | + if (!scrollSyncEnabled) return; |
| 168 | +
|
| 169 | + if (previewCalled) { |
| 170 | + previewCalled = false; |
| 171 | + return; |
| 172 | + } |
| 173 | +
|
| 174 | + updateBlockPositions(); |
| 175 | +
|
| 176 | + const info = editor.getScrollInfo(); |
| 177 | + const leftRatio = info.top / (info.height - info.clientHeight); |
| 178 | +
|
| 179 | + const startIndex = findStartIndex(leftRatio, editPs); |
| 180 | +
|
| 181 | + const rightRatio = |
| 182 | + ((leftRatio - editPs[startIndex]) * |
| 183 | + (previewPs[startIndex + 1] - previewPs[startIndex])) / |
| 184 | + (editPs[startIndex + 1] - editPs[startIndex]) + |
| 185 | + previewPs[startIndex]; |
| 186 | + // const rightRatio = rightPs[startIndex]; // for testing |
| 187 | +
|
| 188 | + previewEl.scrollTo( |
| 189 | + 0, |
| 190 | + rightRatio * (previewEl.scrollHeight - previewEl.clientHeight) |
| 191 | + ); |
| 192 | + editCalled = true; |
| 193 | + }; |
| 194 | + const previewScrollHandler = () => { |
| 195 | + // find the current block in the view |
| 196 | + updateBlockPositions(); |
| 197 | + currentBlockIndex = findStartIndex( |
| 198 | + previewEl.scrollTop / (previewEl.scrollHeight - previewEl.offsetHeight), |
| 199 | + previewPs |
| 200 | + ); |
| 201 | +
|
| 202 | + if (!scrollSyncEnabled) return; |
| 203 | +
|
| 204 | + if (editCalled) { |
| 205 | + editCalled = false; |
| 206 | + return; |
| 207 | + } |
| 208 | +
|
| 209 | + const rightRatio = |
| 210 | + previewEl.scrollTop / (previewEl.scrollHeight - previewEl.clientHeight); |
| 211 | +
|
| 212 | + const startIndex = findStartIndex(rightRatio, previewPs); |
| 213 | +
|
| 214 | + const leftRatio = |
| 215 | + ((rightRatio - previewPs[startIndex]) * |
| 216 | + (editPs[startIndex + 1] - editPs[startIndex])) / |
| 217 | + (previewPs[startIndex + 1] - previewPs[startIndex]) + |
| 218 | + editPs[startIndex]; |
| 219 | +
|
| 220 | + const info = editor.getScrollInfo(); |
| 221 | + editor.scrollTo(0, leftRatio * (info.height - info.clientHeight)); |
| 222 | + previewCalled = true; |
| 223 | + }; |
| 224 | +
|
| 225 | + editor.on('scroll', editorScrollHandler); |
| 226 | + previewEl.addEventListener('scroll', previewScrollHandler, { |
| 227 | + passive: true, |
| 228 | + }); |
| 229 | +
|
115 | 230 | // No need to call `on` because cm instance would change once after init |
116 | 231 | }); |
117 | 232 | onDestroy(off); |
118 | 233 | </script> |
119 | 234 |
|
120 | 235 | <div |
121 | | - class={`bytemd bytemd-mode-${mode}${fullscreen ? ' bytemd-fullscreen' : ''}`} |
| 236 | + class={cx('bytemd', `bytemd-mode-${mode}`, { |
| 237 | + 'bytemd-fullscreen': fullscreen, |
| 238 | + // 'bytemd-sidebar-open': sidebar, |
| 239 | + })} |
122 | 240 | bind:this={el} |
123 | 241 | > |
124 | 242 | <Toolbar |
125 | 243 | {context} |
126 | 244 | {mode} |
127 | 245 | {activeTab} |
| 246 | + {sidebar} |
128 | 247 | {plugins} |
129 | 248 | {fullscreen} |
130 | | - on:tab={setActiveTab} |
131 | | - on:fullscreen={() => { |
132 | | - fullscreen = !fullscreen; |
| 249 | + on:tab={(e) => { |
| 250 | + activeTab = e.detail; |
| 251 | + if (activeTab === 0 && editor) { |
| 252 | + tick().then(() => { |
| 253 | + editor.focus(); |
| 254 | + }); |
| 255 | + } |
| 256 | + }} |
| 257 | + on:click={(e) => { |
| 258 | + switch (e.detail) { |
| 259 | + case 'fullscreen': |
| 260 | + fullscreen = !fullscreen; |
| 261 | + break; |
| 262 | + case 'help': |
| 263 | + if (sidebar === 'help') { |
| 264 | + sidebar = false; |
| 265 | + } else { |
| 266 | + sidebar = 'help'; |
| 267 | + } |
| 268 | + break; |
| 269 | + case 'toc': |
| 270 | + if (sidebar === 'toc') { |
| 271 | + sidebar = false; |
| 272 | + } else { |
| 273 | + sidebar = 'toc'; |
| 274 | + } |
| 275 | + break; |
| 276 | + } |
133 | 277 | }} |
134 | 278 | /> |
135 | 279 | <div class="bytemd-body"> |
136 | | - <div |
137 | | - class="bytemd-editor" |
138 | | - style={mode === 'tab' && activeTab === 1 ? 'display:none' : undefined} |
139 | | - > |
| 280 | + <div class="bytemd-editor" style={styles.edit}> |
140 | 281 | <textarea bind:this={textarea} style="display:none" /> |
141 | 282 | </div> |
142 | | - <div |
143 | | - bind:this={previewEl} |
144 | | - class="bytemd-preview" |
145 | | - style={mode === 'tab' && activeTab === 0 ? 'display:none' : undefined} |
146 | | - > |
| 283 | + <div bind:this={previewEl} class="bytemd-preview" style={styles.preview}> |
147 | 284 | <Viewer |
148 | 285 | value={viewerProps.value} |
149 | 286 | plugins={viewerProps.plugins} |
150 | 287 | sanitize={viewerProps.sanitize} |
| 288 | + on:hast={(e) => { |
| 289 | + hast = e.detail; |
| 290 | + }} |
151 | 291 | /> |
152 | 292 | </div> |
| 293 | + <div class="bytemd-sidebar" style={sidebar ? undefined : 'display:none'}> |
| 294 | + <div |
| 295 | + class="bytemd-sidebar-close" |
| 296 | + on:click={() => { |
| 297 | + sidebar = false; |
| 298 | + }} |
| 299 | + > |
| 300 | + {@html icons.close} |
| 301 | + </div> |
| 302 | + {#if sidebar === 'help'} |
| 303 | + <Help {plugins} /> |
| 304 | + {:else if sidebar === 'toc'} |
| 305 | + <Toc |
| 306 | + {hast} |
| 307 | + {currentBlockIndex} |
| 308 | + on:click={(e) => { |
| 309 | + const headings = previewEl.querySelectorAll('h1,h2,h3,h4,h5,h6'); |
| 310 | + headings[e.detail].scrollIntoView(); |
| 311 | + }} |
| 312 | + /> |
| 313 | + {/if} |
| 314 | + </div> |
153 | 315 | </div> |
154 | 316 | <Status |
155 | 317 | scrollVisible={mode === 'split'} |
|
0 commit comments