|
@@ -1,38 +1,65 @@
|
|
|
|
|
+import { BasicInterceptor } from 'crowi-pluginkit';
|
|
|
import * as codemirror from 'codemirror';
|
|
import * as codemirror from 'codemirror';
|
|
|
|
|
|
|
|
-class MarkdownListHelper {
|
|
|
|
|
|
|
+import mlu from '../../util/interceptor/MarkdownListUtil';
|
|
|
|
|
+
|
|
|
|
|
+export default class MarkdownListHelper extends BasicInterceptor {
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
|
|
+ super();
|
|
|
|
|
+
|
|
|
// https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
|
|
// https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
|
|
|
// https://regex101.com/r/7BN2fR/5
|
|
// https://regex101.com/r/7BN2fR/5
|
|
|
this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
|
|
this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
|
|
|
this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
|
|
this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
|
|
|
|
|
|
|
|
- this.newlineAndIndentContinueMarkdownList = this.newlineAndIndentContinueMarkdownList.bind(this);
|
|
|
|
|
this.pasteText = this.pasteText.bind(this);
|
|
this.pasteText = this.pasteText.bind(this);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- this.getBol = this.getBol.bind(this);
|
|
|
|
|
- this.getEol = this.getEol.bind(this);
|
|
|
|
|
- this.getStrFromBol = this.getStrFromBol.bind(this);
|
|
|
|
|
- this.getStrToEol = this.getStrToEol.bind(this);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @inheritdoc
|
|
|
|
|
+ */
|
|
|
|
|
+ isInterceptWhen(contextName) {
|
|
|
|
|
+ return (
|
|
|
|
|
+ contextName === 'preHandleEnter'
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * wrap codemirror.commands.newlineAndIndentContinueMarkdownList
|
|
|
|
|
- * @param {any} editor An editor instance of CodeMirror
|
|
|
|
|
|
|
+ * return boolean value whether processable parallel
|
|
|
*/
|
|
*/
|
|
|
- newlineAndIndentContinueMarkdownList(editor) {
|
|
|
|
|
- // get strings from current position to EOL(end of line) before break the line
|
|
|
|
|
- const strToEol = this.getStrToEol(editor);
|
|
|
|
|
|
|
+ isProcessableParallel() {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @inheritdoc
|
|
|
|
|
+ */
|
|
|
|
|
+ process(contextName, ...args) {
|
|
|
|
|
+ console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is started');
|
|
|
|
|
+ const orgContext = args[0];
|
|
|
|
|
+ const editor = orgContext.editor;
|
|
|
|
|
+
|
|
|
|
|
+ console.log('AbortContinueMarkdownListInterceptor.process');
|
|
|
|
|
|
|
|
|
|
+ // get strings from current position to EOL(end of line) before break the line
|
|
|
|
|
+ const strToEol = mlu.getStrToEol(editor);
|
|
|
if (this.indentAndMarkRE.test(strToEol)) {
|
|
if (this.indentAndMarkRE.test(strToEol)) {
|
|
|
|
|
+ const context = Object.assign(args[0]); // clone
|
|
|
|
|
+
|
|
|
|
|
+ console.log('AbortContinueMarkdownListInterceptor.newlineAndIndentContinueMarkdownList: abort auto indent');
|
|
|
codemirror.commands.newlineAndIndent(editor);
|
|
codemirror.commands.newlineAndIndent(editor);
|
|
|
// replace the line with strToEol (abort auto indent)
|
|
// replace the line with strToEol (abort auto indent)
|
|
|
- editor.getDoc().replaceRange(strToEol, this.getBol(editor), this.getEol(editor));
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- codemirror.commands.newlineAndIndentContinueMarkdownList(editor);
|
|
|
|
|
|
|
+ editor.getDoc().replaceRange(strToEol, mlu.getBol(editor), mlu.getEol(editor));
|
|
|
|
|
+
|
|
|
|
|
+ // report to manager that handling was done
|
|
|
|
|
+ context.handlers.push(this.className);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is finished');
|
|
|
|
|
+
|
|
|
|
|
+ // resolve
|
|
|
|
|
+ return Promise.resolve(orgContext);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -43,7 +70,7 @@ class MarkdownListHelper {
|
|
|
*/
|
|
*/
|
|
|
pasteText(editor, event, text) {
|
|
pasteText(editor, event, text) {
|
|
|
// get strings from BOL(beginning of line) to current position
|
|
// get strings from BOL(beginning of line) to current position
|
|
|
- const strFromBol = this.getStrFromBol(editor);
|
|
|
|
|
|
|
+ const strFromBol = mlu.getStrFromBol(editor);
|
|
|
|
|
|
|
|
const matched = strFromBol.match(this.indentAndMarkRE);
|
|
const matched = strFromBol.match(this.indentAndMarkRE);
|
|
|
// when match indentAndMarkOnlyRE
|
|
// when match indentAndMarkOnlyRE
|
|
@@ -54,7 +81,7 @@ class MarkdownListHelper {
|
|
|
// replace
|
|
// replace
|
|
|
if (adjusted != null) {
|
|
if (adjusted != null) {
|
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
|
- editor.getDoc().replaceRange(adjusted, this.getBol(editor), editor.getCursor());
|
|
|
|
|
|
|
+ editor.getDoc().replaceRange(adjusted, mlu.getBol(editor), editor.getCursor());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -126,42 +153,4 @@ class MarkdownListHelper {
|
|
|
|
|
|
|
|
return isListful;
|
|
return isListful;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * return the postion of the BOL(beginning of line)
|
|
|
|
|
- */
|
|
|
|
|
- getBol(editor) {
|
|
|
|
|
- const curPos = editor.getCursor();
|
|
|
|
|
- return { line: curPos.line, ch: 0 };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * return the postion of the EOL(end of line)
|
|
|
|
|
- */
|
|
|
|
|
- getEol(editor) {
|
|
|
|
|
- const curPos = editor.getCursor();
|
|
|
|
|
- const lineLength = editor.getDoc().getLine(curPos.line).length;
|
|
|
|
|
- return { line: curPos.line, ch: lineLength };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * return strings from BOL(beginning of line) to current position
|
|
|
|
|
- */
|
|
|
|
|
- getStrFromBol(editor) {
|
|
|
|
|
- const curPos = editor.getCursor();
|
|
|
|
|
- return editor.getDoc().getRange(this.getBol(editor), curPos);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * return strings from current position to EOL(end of line)
|
|
|
|
|
- */
|
|
|
|
|
- getStrToEol(editor) {
|
|
|
|
|
- const curPos = editor.getCursor();
|
|
|
|
|
- return editor.getDoc().getRange(curPos, this.getEol(editor));
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-// singleton pattern
|
|
|
|
|
-const instance = new MarkdownListHelper();
|
|
|
|
|
-Object.freeze(instance);
|
|
|
|
|
-export default instance;
|
|
|