Skip to content

Commit 8b27fd9

Browse files
committed
refactor: move scroll sync to bytemd
1 parent cea2217 commit 8b27fd9

File tree

5 files changed

+16
-78
lines changed

5 files changed

+16
-78
lines changed

packages/bytemd/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ The 2,5,7 steps are designed for user customization via ByteMD plugin API.
172172
| [@bytemd/plugin-math-ssr](./packages/plugin-math-ssr) | [![npm](https://img.shields.io/npm/v/@bytemd/plugin-math-ssr.svg)](https://npm.im/@bytemd/plugin-math-ssr) [![gzip size](https://img.badgesize.io/https://unpkg.com/@bytemd/plugin-math-ssr/dist/index.min.js?compression=gzip)](https://unpkg.com/@bytemd/plugin-math-ssr) | Support math equation (SSR compatible) |
173173
| [@bytemd/plugin-medium-zoom](./packages/plugin-medium-zoom) | [![npm](https://img.shields.io/npm/v/@bytemd/plugin-medium-zoom.svg)](https://npm.im/@bytemd/plugin-medium-zoom) [![gzip size](https://img.badgesize.io/https://unpkg.com/@bytemd/plugin-medium-zoom/dist/index.min.js?compression=gzip)](https://unpkg.com/@bytemd/plugin-medium-zoom) | Zoom images like Medium |
174174
| [@bytemd/plugin-mermaid](./packages/plugin-mermaid) | [![npm](https://img.shields.io/npm/v/@bytemd/plugin-mermaid.svg)](https://npm.im/@bytemd/plugin-mermaid) [![gzip size](https://img.badgesize.io/https://unpkg.com/@bytemd/plugin-mermaid/dist/index.min.js?compression=gzip)](https://unpkg.com/@bytemd/plugin-mermaid) | Support Mermaid diagram and flowchart |
175-
| [@bytemd/plugin-scroll-sync](./packages/plugin-scroll-sync) | [![npm](https://img.shields.io/npm/v/@bytemd/plugin-scroll-sync.svg)](https://npm.im/@bytemd/plugin-scroll-sync) [![gzip size](https://img.badgesize.io/https://unpkg.com/@bytemd/plugin-scroll-sync/dist/index.min.js?compression=gzip)](https://unpkg.com/@bytemd/plugin-scroll-sync) | Sync scroll position of edit and preview area |
176175
| [@bytemd/plugin-vega](./packages/plugin-vega) | [![npm](https://img.shields.io/npm/v/@bytemd/plugin-vega.svg)](https://npm.im/@bytemd/plugin-vega) [![gzip size](https://img.badgesize.io/https://unpkg.com/@bytemd/plugin-vega/dist/index.min.js?compression=gzip)](https://unpkg.com/@bytemd/plugin-vega) | Support vega charts |
177176

178177
## Write a plugin

packages/bytemd/src/editor.svelte

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
export let toolbar: EditorProps['toolbar'];
1515
1616
let el: HTMLElement;
17+
let previewEl: HTMLElement;
1718
let viewerProps: ViewerProps = {
1819
value,
1920
plugins,
@@ -87,6 +88,20 @@
8788
editor.on('change', (doc, change) => {
8889
dispatch('change', { value: editor.getValue() });
8990
});
91+
92+
// Scroll sync
93+
editor.on('scroll', () => {
94+
requestAnimationFrame(() => {
95+
const editorInfo = editor.getScrollInfo();
96+
const ratio =
97+
editorInfo.top / (editorInfo.height - editorInfo.clientHeight);
98+
previewEl.scrollTo(
99+
0,
100+
ratio * (previewEl.scrollHeight - previewEl.clientHeight)
101+
);
102+
});
103+
});
104+
90105
// No need to call `on` because cm instance would change once after init
91106
});
92107
onDestroy(off);
@@ -109,6 +124,7 @@
109124
<textarea bind:this={textarea} style="display:none" />
110125
</div>
111126
<div
127+
bind:this={previewEl}
112128
class="bytemd-preview"
113129
style={mode === 'tab' && activeTab === 0 ? 'display:none' : undefined}>
114130
<Viewer {...viewerProps} />

packages/plugin-scroll-sync/README.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/plugin-scroll-sync/package.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/plugin-scroll-sync/src/index.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)