Skip to content

Commit

Permalink
[#64933] ensure folding arrows are added in larger documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin committed Sep 30, 2024
1 parent ccd0fe2 commit 03a839a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/components/CodeMirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ const CodeEditor = styled.div`
rotate: -90deg;
}
}
.cm-foldPlaceholder {
margin-left: 4px;
}
`;

const setEditorText = (editor, text) => {
Expand Down
11 changes: 9 additions & 2 deletions src/hooks/useText.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { StateEffect } from "@codemirror/state";
import markdownMermaid from "./markdownMermaid";
import { EditorView, ViewUpdate } from "@codemirror/view";
import { addFoldUI } from "./markdownFoldButtons";
import { foldedRanges } from "@codemirror/language";
import { foldedRanges, ensureSyntaxTree } from "@codemirror/language";

const countOccurences = (str, pattern) => (str?.match(pattern) || []).length;
const getUnfoldedMarkdown = (src, /** @type {EditorView} */ view) => {
Expand Down Expand Up @@ -183,8 +183,8 @@ export const useText = ({ initialText, transforms, customRoles, preview, backsla
.map(({ md, startLine, endLine }, id) => {
const hash = new IMurMurHash(md, 42).result();

// Clear source mappings for chunk we are rerendering
if (!lookup[hash]) {
// Clear source mappings for chunk we are rerendering
let unfoldedStart = startLine;
let unfoldedEnd = endLine;
for (const range of foldedLines ?? []) {
Expand All @@ -198,6 +198,13 @@ export const useText = ({ initialText, transforms, customRoles, preview, backsla
for (let l = unfoldedStart; l <= unfoldedEnd; l++) {
lineMap.current.delete(l);
}

const endLineTo = window.myst_editor.main_editor.state.doc.line(unfoldedEnd).to;
// This might cause longer startup times for larger documents
// Calling this causes the Markdown parser in CodeMirror to parse at least up to the range
// being rendered. Thanks to this, we can query the editor about lines being foldable.
ensureSyntaxTree(window.myst_editor.main_editor.state, endLineTo, 1000);
window.myst_editor.main_editor.dispatch({});
}

const html =
Expand Down

0 comments on commit 03a839a

Please sign in to comment.