|
9 | 9 | import Viewer from './viewer.svelte'; |
10 | 10 | import { createUtils } from './editor'; |
11 | 11 | import { scrollSync } from './plugins'; |
| 12 | + import Status from './status.svelte'; |
12 | 13 |
|
13 | 14 | export let value: EditorProps['value'] = ''; |
14 | 15 | export let plugins: NonNullable<EditorProps['plugins']> = []; |
|
18 | 19 | export let placeholder: EditorProps['placeholder']; |
19 | 20 | export let editorConfig: EditorProps['editorConfig']; |
20 | 21 |
|
21 | | - let builtinPlugins: BytemdPlugin[] = []; |
| 22 | + let scrollSyncInstance = scrollSync(); |
22 | 23 |
|
23 | | - $: fullPlugins = [...builtinPlugins, ...plugins]; |
| 24 | + $: fullPlugins = (() => { |
| 25 | + const ps = [...plugins]; |
| 26 | + if (mode === 'split' && scrollSyncEnabled) { |
| 27 | + ps.push(scrollSyncInstance); |
| 28 | + } |
| 29 | + return ps; |
| 30 | + })(); |
24 | 31 |
|
25 | 32 | let el: HTMLElement; |
| 33 | + let previewEl: HTMLElement; |
26 | 34 | let viewerProps: ViewerProps = { |
27 | 35 | value, |
28 | 36 | plugins, |
|
32 | 40 | let editor: Editor; |
33 | 41 | let activeTab = 0; |
34 | 42 | let fullscreen = false; |
| 43 | + let scrollSyncEnabled = true; |
35 | 44 |
|
36 | 45 | $: context = { editor, $el: el, utils: createUtils(editor) }; |
37 | 46 |
|
|
78 | 87 | } |
79 | 88 |
|
80 | 89 | onMount(async () => { |
81 | | - builtinPlugins = [scrollSync()]; |
82 | | -
|
83 | 90 | const [codemirror] = await Promise.all([ |
84 | 91 | import('codemirror'), |
85 | 92 | // @ts-ignore |
|
133 | 140 | <textarea bind:this={textarea} style="display:none" /> |
134 | 141 | </div> |
135 | 142 | <div |
| 143 | + bind:this={previewEl} |
136 | 144 | class="bytemd-preview" |
137 | 145 | style={mode === 'tab' && activeTab === 0 ? 'display:none' : undefined} |
138 | 146 | > |
|
143 | 151 | /> |
144 | 152 | </div> |
145 | 153 | </div> |
| 154 | + <Status |
| 155 | + scrollVisible={mode === 'split'} |
| 156 | + {value} |
| 157 | + {scrollSyncEnabled} |
| 158 | + on:sync={(e) => { |
| 159 | + scrollSyncEnabled = e.detail; |
| 160 | + }} |
| 161 | + on:top={() => { |
| 162 | + previewEl.scrollTo({ top: 0, behavior: 'smooth' }); |
| 163 | + }} |
| 164 | + /> |
146 | 165 | </div> |
0 commit comments