|
@@ -1,14 +1,66 @@
|
|
|
|
|
+import MarkdownTable from '../../models/MarkdownTable';
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Utility for markdown link
|
|
* Utility for markdown link
|
|
|
*/
|
|
*/
|
|
|
class MarkdownLinkUtil {
|
|
class MarkdownLinkUtil {
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
- // TODO Regular expression for link
|
|
|
|
|
- this.linePartOfLink = /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/;
|
|
|
|
|
|
|
+ // TODO Regular expression for link /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/
|
|
|
|
|
+ this.linePartOfLink = /^\[/;
|
|
|
this.isInTable = this.isInTable.bind(this);
|
|
this.isInTable = this.isInTable.bind(this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ getBot(editor) {
|
|
|
|
|
+ const curPos = editor.getCursor();
|
|
|
|
|
+ if (!this.isInTable(editor)) {
|
|
|
|
|
+ return { line: curPos.line, ch: curPos.ch };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const firstLine = editor.getDoc().firstLine();
|
|
|
|
|
+ let line = curPos.line - 1;
|
|
|
|
|
+ for (; line >= firstLine; line--) {
|
|
|
|
|
+ const strLine = editor.getDoc().getLine(line);
|
|
|
|
|
+ if (!this.linePartOfTableRE.test(strLine)) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ const botLine = Math.max(firstLine, line + 1);
|
|
|
|
|
+ return { line: botLine, ch: 0 };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * return the postion of the EOT(end of table)
|
|
|
|
|
+ * (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 };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const lastLine = editor.getDoc().lastLine();
|
|
|
|
|
+ let line = curPos.line + 1;
|
|
|
|
|
+ for (; line <= lastLine; line++) {
|
|
|
|
|
+ const strLine = editor.getDoc().getLine(line);
|
|
|
|
|
+ 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 };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ getMarkdownLink(editor) {
|
|
|
|
|
+ if (!this.isInTable(editor)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const strFromBotToEot = editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
|
|
|
|
|
+ return MarkdownTable.fromMarkdownString(strFromBotToEot);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
getSelectedTextInEditor(editor) {
|
|
getSelectedTextInEditor(editor) {
|
|
|
return editor.getDoc().getSelection();
|
|
return editor.getDoc().getSelection();
|
|
|
}
|
|
}
|
|
@@ -19,7 +71,7 @@ class MarkdownLinkUtil {
|
|
|
|
|
|
|
|
isInTable(editor) {
|
|
isInTable(editor) {
|
|
|
const curPos = editor.getCursor();
|
|
const curPos = editor.getCursor();
|
|
|
- return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
|
|
|
|
|
|
|
+ return this.linePartOfLink.test(editor.getDoc().getLine(curPos.line));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|