|
|
@@ -14,6 +14,8 @@ class MarkdownTableUtil {
|
|
|
// https://regex101.com/r/1UuWBJ/3
|
|
|
this.emptyLineOfTableRE = /^([^\r\n|]*)\|((\s*\|)+)$/;
|
|
|
|
|
|
+ this.curPos = this.curPos.bind(this);
|
|
|
+ this.getBot = this.getBot.bind(this);
|
|
|
this.getEot = this.getEot.bind(this);
|
|
|
this.getStrFromBot = this.getStrFromBot.bind(this);
|
|
|
this.getStrToEot = this.getStrToEot.bind(this);
|
|
|
@@ -22,26 +24,30 @@ class MarkdownTableUtil {
|
|
|
this.replaceMarkdownTableWithReformed = this.replaceFocusedMarkdownTableWithEditor; // alias
|
|
|
}
|
|
|
|
|
|
+ curPos(editor) {
|
|
|
+ return editor.state.selection.main.head;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* return the postion of the BOT(beginning of table)
|
|
|
* (If the cursor is not in a table, return its position)
|
|
|
*/
|
|
|
getBot(editor) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
if (!this.isInTable(editor)) {
|
|
|
- return { line: curPos.line, ch: curPos.ch };
|
|
|
+ return this.curPos(editor);
|
|
|
}
|
|
|
|
|
|
- const firstLine = editor.getDoc().firstLine();
|
|
|
- let line = curPos.line - 1;
|
|
|
+ const doc = editor.state.doc;
|
|
|
+ const firstLine = 1;
|
|
|
+ let line = doc.lineAt(this.curPos(editor)).number - 1;
|
|
|
for (; line >= firstLine; line--) {
|
|
|
- const strLine = editor.getDoc().getLine(line);
|
|
|
+ const strLine = doc.line(line).text;
|
|
|
if (!this.linePartOfTableRE.test(strLine)) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
const botLine = Math.max(firstLine, line + 1);
|
|
|
- return { line: botLine, ch: 0 };
|
|
|
+ return doc.line(botLine).from;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -49,38 +55,35 @@ class MarkdownTableUtil {
|
|
|
* (If the cursor is not in a table, return its position)
|
|
|
*/
|
|
|
getEot(editor) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
if (!this.isInTable(editor)) {
|
|
|
- return { line: curPos.line, ch: curPos.ch };
|
|
|
+ return this.curPos(editor);
|
|
|
}
|
|
|
|
|
|
- const lastLine = editor.getDoc().lastLine();
|
|
|
- let line = curPos.line + 1;
|
|
|
+ const doc = editor.state.doc;
|
|
|
+ const lastLine = doc.lines;
|
|
|
+ let line = doc.lineAt(this.curPos(editor)).number + 1;
|
|
|
for (; line <= lastLine; line++) {
|
|
|
- const strLine = editor.getDoc().getLine(line);
|
|
|
+ const strLine = doc.line(line).text;
|
|
|
if (!this.linePartOfTableRE.test(strLine)) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
const eotLine = Math.min(line - 1, lastLine);
|
|
|
- const lineLength = editor.getDoc().getLine(eotLine).length;
|
|
|
- return { line: eotLine, ch: lineLength };
|
|
|
+ return doc.line(eotLine).to;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* return strings from BOT(beginning of table) to the cursor position
|
|
|
*/
|
|
|
getStrFromBot(editor) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
- return editor.getDoc().getRange(this.getBot(editor), curPos);
|
|
|
+ return editor.state.sliceDoc(this.getBot(editor), this.curPos(editor));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* return strings from the cursor position to EOT(end of table)
|
|
|
*/
|
|
|
getStrToEot(editor) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
- return editor.getDoc().getRange(curPos, this.getEot(editor));
|
|
|
+ return editor.state.sliceDoc(this.curPos(editor), this.getEot(editor));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -92,7 +95,7 @@ class MarkdownTableUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- const strFromBotToEot = editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
|
|
|
+ const strFromBotToEot = editor.state.sliceDoc(this.getBot(editor), this.getEot(editor));
|
|
|
return MarkdownTable.fromMarkdownString(strFromBotToEot);
|
|
|
}
|
|
|
|
|
|
@@ -105,16 +108,15 @@ class MarkdownTableUtil {
|
|
|
* return boolean value whether the cursor position is end of line
|
|
|
*/
|
|
|
isEndOfLine(editor) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
- return (curPos.ch === editor.getDoc().getLine(curPos.line).length);
|
|
|
+ return this.curPos(editor) === editor.state.doc.lineAt(this.curPos(editor)).to;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* return boolean value whether the cursor position is in a table
|
|
|
*/
|
|
|
isInTable(editor) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
- return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
|
|
|
+ const lineText = editor.state.doc.lineAt(this.curPos(editor)).text;
|
|
|
+ return this.linePartOfTableRE.test(lineText);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -144,7 +146,7 @@ class MarkdownTableUtil {
|
|
|
mdtableList.forEach((mdtable) => {
|
|
|
newTable = newTable.concat(mdtable.table);
|
|
|
});
|
|
|
- return (new MarkdownTable(newTable, options));
|
|
|
+ return new MarkdownTable(newTable, options);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -153,9 +155,20 @@ class MarkdownTableUtil {
|
|
|
* @param {MarkdownTable} table
|
|
|
*/
|
|
|
replaceFocusedMarkdownTableWithEditor(editor, table) {
|
|
|
- const curPos = editor.getCursor();
|
|
|
- editor.getDoc().replaceRange(table.toString(), this.getBot(editor), this.getEot(editor));
|
|
|
- editor.getDoc().setCursor(curPos.line + 1, 2);
|
|
|
+ const botPos = this.getBot(editor);
|
|
|
+ const eotPos = this.getEot(editor);
|
|
|
+
|
|
|
+ editor.dispatch({
|
|
|
+ changes: {
|
|
|
+ from: botPos,
|
|
|
+ to: eotPos,
|
|
|
+ insert: table.toString(),
|
|
|
+ },
|
|
|
+ });
|
|
|
+ editor.dispatch({
|
|
|
+ selection: { anchor: editor.state.doc.lineAt(eotPos).to },
|
|
|
+ });
|
|
|
+ editor.focus();
|
|
|
}
|
|
|
|
|
|
/**
|