Skip to content

Commit

Permalink
Merge pull request #8977 from weseek/fix/error-handling-in-fold-drawio
Browse files Browse the repository at this point in the history
fix: Handle error when folding drawio blocks
  • Loading branch information
yuki-takei authored Jul 19, 2024
2 parents 413b647 + 1ab43b1 commit d2bc8d0
Showing 1 changed file with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit d2bc8d0

Please sign in to comment.