|
|
@@ -6,6 +6,18 @@ class MarkdownDrawioUtil {
|
|
|
constructor() {
|
|
|
this.lineBeginPartOfDrawioRE = /^```(\s.*)drawio$/;
|
|
|
this.lineEndPartOfDrawioRE = /^```$/;
|
|
|
+ this.curPos = this.curPos.bind(this);
|
|
|
+ this.doc = this.doc.bind(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ // get cursor position from editor
|
|
|
+ curPos(editor) {
|
|
|
+ return editor.state.selection.main.head;
|
|
|
+ }
|
|
|
+
|
|
|
+ // get doc from editor
|
|
|
+ doc(editor) {
|
|
|
+ return editor.state.doc;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -13,18 +25,17 @@ class MarkdownDrawioUtil {
|
|
|
* (If the BOD is not found after the cursor or the EOD is found before the BOD, return null)
|
|
|
*/
|
|
|
getBod(editor) {
|
|
|
- const curPos = editor.state.selection.main.head;
|
|
|
- const doc = editor.state.doc;
|
|
|
const firstLine = 1;
|
|
|
|
|
|
- if (this.lineBeginPartOfDrawioRE.test(doc.lineAt(curPos)).text) {
|
|
|
- return doc.lineAt(curPos).from;
|
|
|
+ if (this.lineBeginPartOfDrawioRE.test(this.doc(editor).lineAt(this.curPos(editor))).text) {
|
|
|
+ // get the beginning of the line where the cursor is located
|
|
|
+ return this.doc(editor).lineAt(this.curPos(editor)).from;
|
|
|
}
|
|
|
|
|
|
- let line = doc.lineAt(curPos(editor)).number - 1;
|
|
|
+ let line = this.doc(editor).lineAt(this.curPos(editor)).number - 1;
|
|
|
let isFound = false;
|
|
|
for (; line >= firstLine; line--) {
|
|
|
- const strLine = doc.line(line).text;
|
|
|
+ const strLine = this.doc(editor).line(line).text;
|
|
|
if (this.lineBeginPartOfDrawioRE.test(strLine)) {
|
|
|
isFound = true;
|
|
|
break;
|
|
|
@@ -41,7 +52,7 @@ class MarkdownDrawioUtil {
|
|
|
}
|
|
|
|
|
|
const botLine = Math.max(firstLine, line);
|
|
|
- return doc.line(botLine).from;
|
|
|
+ return this.doc(editor).line(botLine).from;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -49,18 +60,17 @@ class MarkdownDrawioUtil {
|
|
|
* (If the EOD is not found after the cursor or the BOD is found before the EOD, return null)
|
|
|
*/
|
|
|
getEod(editor) {
|
|
|
- const curPos = editor.state.selection.main.head;
|
|
|
- const doc = editor.state.doc;
|
|
|
- const lastLine = doc.lines;
|
|
|
+ const lastLine = this.doc(editor).lines;
|
|
|
|
|
|
- if (this.lineEndPartOfDrawioRE.test(doc.lineAt(curPos)).text) {
|
|
|
- return doc.lineAt(curPos).to;
|
|
|
+ if (this.lineEndPartOfDrawioRE.test(this.doc(editor).lineAt(this.curPos(editor))).text) {
|
|
|
+ // get the end of the line where the cursor is located
|
|
|
+ return this.doc(editor).lineAt(this.curPos(editor)).to;
|
|
|
}
|
|
|
|
|
|
- let line = doc.lineAt(curPos(editor)).number + 1;
|
|
|
+ let line = this.doc(editor).lineAt(this.curPos(editor)).number + 1;
|
|
|
let isFound = false;
|
|
|
for (; line <= lastLine; line++) {
|
|
|
- const strLine = doc.line(line).text;
|
|
|
+ const strLine = this.doc(editor).line(line).text;
|
|
|
if (this.lineEndPartOfDrawioRE.test(strLine)) {
|
|
|
isFound = true;
|
|
|
break;
|
|
|
@@ -77,7 +87,7 @@ class MarkdownDrawioUtil {
|
|
|
}
|
|
|
|
|
|
const eodLine = Math.min(line, lastLine);
|
|
|
- return doc.line(eodLine).to;
|
|
|
+ return this.doc(editor).line(eodLine).to;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -97,15 +107,14 @@ class MarkdownDrawioUtil {
|
|
|
* (If the cursor is not in a drawio block, return null)
|
|
|
*/
|
|
|
getMarkdownDrawioMxfile(editor) {
|
|
|
- const doc = editor.state.doc;
|
|
|
if (this.isInDrawioBlock(editor)) {
|
|
|
const bod = this.getBod(editor);
|
|
|
const eod = this.getEod(editor);
|
|
|
|
|
|
// skip block begin sesion("``` drawio")
|
|
|
- doc.lineAt(bod).number++;
|
|
|
+ this.doc(editor).lineAt(bod).number++;
|
|
|
// skip block end sesion("```")
|
|
|
- doc.lineAt(eod).number--;
|
|
|
+ this.doc.lineAt(eod).number--;
|
|
|
|
|
|
return editor.state.sliceDoc(bod, eod);
|
|
|
}
|
|
|
@@ -113,8 +122,6 @@ class MarkdownDrawioUtil {
|
|
|
}
|
|
|
|
|
|
replaceFocusedDrawioWithEditor(editor, drawioData) {
|
|
|
- const curPos = editor.state.selection.main.head;
|
|
|
- const doc = editor.state.doc;
|
|
|
const drawioBlock = ['``` drawio', drawioData.toString(), '```'].join('\n');
|
|
|
let beginPos;
|
|
|
let endPos;
|
|
|
@@ -124,8 +131,8 @@ class MarkdownDrawioUtil {
|
|
|
endPos = this.getEod(editor);
|
|
|
}
|
|
|
else {
|
|
|
- beginPos = doc.lineAt(curPos);
|
|
|
- endPos = doc.lineAt(curPos);
|
|
|
+ beginPos = this.doc(editor).lineAt(this.curPos(editor));
|
|
|
+ endPos = this.doc(editor).lineAt(this.curPos(editor));
|
|
|
}
|
|
|
|
|
|
editor.dispatch({
|
|
|
@@ -168,10 +175,9 @@ class MarkdownDrawioUtil {
|
|
|
*/
|
|
|
findAllDrawioSection(editor) {
|
|
|
const lineNumbers = [];
|
|
|
- const doc = editor.state.doc;
|
|
|
// refs: https://github.com/codemirror/CodeMirror/blob/5.64.0/addon/fold/foldcode.js#L106-L111
|
|
|
- for (let i = 1, e = doc.lines; i <= e; i++) {
|
|
|
- const line = doc.line(i + 1).text;
|
|
|
+ for (let i = 1, e = this.doc(editor).lines; i <= e; i++) {
|
|
|
+ const line = this.doc(editor).line(i + 1).text;
|
|
|
const match = this.lineBeginPartOfDrawioRE.exec(line);
|
|
|
if (match) {
|
|
|
lineNumbers.push(i);
|