Skip to content

Commit

Permalink
[#63062] make scroll sync optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin authored and MaciejWas committed Sep 13, 2024
1 parent c789d61 commit 7af505d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Here are the props you can pass to the MystEditor component:
- `dictionaryPath` *(default: "/dictionaries")* - Path to a folder with dictionaries. For an example see `public/dictionaries/`.
- `backslashLineBreak` *(default: true)* - treat `\` as a line break even when it is at the end of a paragraph/block
- `additionalStyles` *(`CSSStylesheet` | `CSSStylesheet[]`)* - extra CSS stylesheets to customize the component
- `syncScroll` *(default: false)* - synchronize scrolling of the editor and preview in `Dual Pane` view mode.
- > NOTE: This only works if you set a height limit on the MyST parent element.
## License

Expand Down
3 changes: 2 additions & 1 deletion src/MystEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const MystWrapper = styled.div`
box-sizing: border-box;
height: calc(100% - 60px);
width: 100%;
height: calc(100vh - 60px);
position: relative;
background-color: white;
${(props) => props.fullscreen && "box-sizing:border-box; height: calc(100vh - 60px);"}
Expand Down Expand Up @@ -126,6 +125,7 @@ const MystEditor = ({
getAvatar = (login) => `https://secure.gravatar.com/avatar/${login}?s=30&d=identicon`,
backslashLineBreak = true,
parent,
syncScroll = false,
}) => {
const [mode, setMode] = useState(initialMode);
const [fullscreen, setFullscreen] = useState(false);
Expand Down Expand Up @@ -196,6 +196,7 @@ const MystEditor = ({
root: parent,
highlights: transforms,
preview,
syncScroll,
collaboration: {
opts: collaboration,
setUsers,
Expand Down
8 changes: 5 additions & 3 deletions src/components/CodeMirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const setEditorText = (editor, text) => {
});
};

const CodeMirror = ({ text, id, root, mode, spellcheckOpts, highlights, collaboration, preview }) => {
const CodeMirror = ({ text, id, root, mode, spellcheckOpts, highlights, collaboration, preview, syncScroll }) => {
const editorRef = useRef(null);
const editorMountpoint = useRef(null);
const focusScroll = useRef(null);
Expand Down Expand Up @@ -219,8 +219,8 @@ const CodeMirror = ({ text, id, root, mode, spellcheckOpts, highlights, collabor
.addUpdateListener((update) => update.docChanged && text.set(view.state.doc.toString(), update))
.useFixFoldingScroll(focusScroll)
.useMoveCursorAfterFold()
.useSyncPreviewWithCursor({ lineMap: text.lineMap, preview })
.useCursorIndicator({ lineMap: text.lineMap, preview })
.if(syncScroll, (b) => b.useSyncPreviewWithCursor({ lineMap: text.lineMap, preview }))
.create(),
});

Expand All @@ -231,7 +231,9 @@ const CodeMirror = ({ text, id, root, mode, spellcheckOpts, highlights, collabor
editorRef.current = view;
window.myst_editor.main_editor = view;

syncEditorWithPreviewScroll(preview.current, text.lineMap, view);
if (syncScroll) {
syncEditorWithPreviewScroll(preview.current, text.lineMap, view);
}

collaboration.ycomments?.registerCodeMirror(view);
collaboration.provider?.watchCollabolators(collaboration.setUsers);
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ export class ExtensionBuilder {
return this;
}

/** @param {(b: ExtensionBuilder) => ExtensionBuilder} extender */
if(condition, extender) {
if (condition) {
return extender(this);
} else {
return this;
}
}

useMoveCursorAfterFold() {
this.base.push(
EditorState.transactionFilter.of((tr) => {
Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
dict: "en_US",
dictionaryPath: "/myst-editor/dictionaries",
},
syncScroll: true,
},
document.getElementById("myst"),
);
Expand Down

0 comments on commit 7af505d

Please sign in to comment.