diff --git a/packages/editor/src/client/services/use-codemirror-editor/utils/fold-drawio.ts b/packages/editor/src/client/services/use-codemirror-editor/utils/fold-drawio.ts index 168218e341c..e9c73878308 100644 --- a/packages/editor/src/client/services/use-codemirror-editor/utils/fold-drawio.ts +++ b/packages/editor/src/client/services/use-codemirror-editor/utils/fold-drawio.ts @@ -3,42 +3,61 @@ import { useEffect } from 'react'; import { foldEffect } from '@codemirror/language'; import type { EditorView } from '@codemirror/view'; + export type FoldDrawio = void; const findAllDrawioSection = (view?: EditorView) => { if (view == null) { return; } - const lineBeginPartOfDrawioRE = /^```(\s.*)drawio$/; - const lineNumbers: number[] = []; - // repeat the process in each line from the top to the bottom in the editor - for (let i = 1, e = view.state.doc.lines; i <= e; i++) { - // get each line text - const lineTxt = view.state.doc.line(i).text; - const match = lineBeginPartOfDrawioRE.exec(lineTxt); - if (match) { - lineNumbers.push(i); + + try { + const lineBeginPartOfDrawioRE = /^```(\s.*)drawio$/; + const lineNumbers: number[] = []; + // repeat the process in each line from the top to the bottom in the editor + for (let i = 1, e = view.state.doc.lines; i <= e; i++) { + // get each line text + const lineTxt = view.state.doc.line(i).text; + const match = lineBeginPartOfDrawioRE.exec(lineTxt); + if (match) { + lineNumbers.push(i); + } + } + return lineNumbers; + } + catch (err) { + if (err instanceof Error) { + // eslint-disable-next-line no-console + console.warn(err.toString()); } } - return lineNumbers; }; const foldDrawioSection = (lineNumbers?: number[], view?: EditorView) => { if (view == null || lineNumbers == null) { return; } - lineNumbers.forEach((lineNumber) => { - // get the end of the lines containing '''drawio - const from = view.state.doc.line(lineNumber).to; - // get the end of the lines containing ''' - const to = view.state.doc.line(lineNumber + 2).to; - view?.dispatch({ - effects: foldEffect.of({ - from, - to, - }), + + try { + lineNumbers.forEach((lineNumber) => { + // get the end of the lines containing '''drawio + const from = view.state.doc.line(lineNumber).to; + // get the end of the lines containing ''' + const to = view.state.doc.line(lineNumber + 2).to; + view?.dispatch({ + effects: foldEffect.of({ + from, + to, + }), + }); }); - }); + } + catch (err) { + if (err instanceof Error) { + // eslint-disable-next-line no-console + console.warn(err.toString()); + } + } }; export const useFoldDrawio = (view?: EditorView): FoldDrawio => {