From 701fc0bc80b83dd5be4e9324d5578b15c8957c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=BE=E4=BA=95=E8=AA=A0=E6=B3=B0?= Date: Sun, 4 Feb 2018 10:03:50 +0900 Subject: [PATCH] I fixed the behavior of the editor. On enter key pushed, if already the line contains a token in markdown list, no completion is needed, I think. By this change, node_modules/codemirror/addon/edit/continueList.js could be trashed, but I can't because of the gitignore. --- browser/components/CodeEditor.js | 2 +- lib/main.html | 2 +- .../boostNewLineIndentContinueMarkdownList.js | 47 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 node_modules/boost/boostNewLineIndentContinueMarkdownList.js diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 217a14b1..00dc1f6d 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -92,7 +92,7 @@ export default class CodeEditor extends React.Component { 'Cmd-T': function (cm) { // Do nothing }, - Enter: 'newlineAndIndentContinueMarkdownList', + Enter: 'boostNewLineAndIndentContinueMarkdownList', 'Ctrl-C': (cm) => { if (cm.getOption('keyMap').substr(0, 3) === 'vim') { document.execCommand('copy') diff --git a/lib/main.html b/lib/main.html index 11c1c62e..53520957 100644 --- a/lib/main.html +++ b/lib/main.html @@ -78,7 +78,7 @@ - + diff --git a/node_modules/boost/boostNewLineIndentContinueMarkdownList.js b/node_modules/boost/boostNewLineIndentContinueMarkdownList.js new file mode 100644 index 00000000..4a2055b2 --- /dev/null +++ b/node_modules/boost/boostNewLineIndentContinueMarkdownList.js @@ -0,0 +1,47 @@ +(function(mod) { + if (typeof exports == "object" && typeof module == "object") // CommonJS + mod(require("../codemirror/lib/codemirror")); + else if (typeof define == "function" && define.amd) // AMD + define(["../codemirror/lib/codemirror"], mod); + else // Plain browser env + mod(CodeMirror); +})(function(CodeMirror) { + "use strict"; + + var listRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/, + emptyListRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/, + unorderedListRE = /[*+-]\s/; + + CodeMirror.commands.boostNewLineAndIndentContinueMarkdownList = function(cm) { + console.log('success'); + if (cm.getOption("disableInput")) return CodeMirror.Pass; + var ranges = cm.listSelections(), replacements = []; + for (var i = 0; i < ranges.length; i++) { + var pos = ranges[i].head; + var eolState = cm.getStateAfter(pos.line); + var inList = eolState.list !== false; + var inQuote = eolState.quote !== 0; + var line = cm.getLine(pos.line), match = listRE.exec(line); + if (!ranges[i].empty() || (!inList && !inQuote) || !match || pos.ch < match[2].length - 1) { + cm.execCommand("newlineAndIndent"); + return; + } + if (emptyListRE.test(line)) { + if (!/>\s*$/.test(line)) cm.replaceRange("", { + line: pos.line, ch: 0 + }, { + line: pos.line, ch: pos.ch + 1 + }); + replacements[i] = "\n"; + } else { + var indent = match[1], after = match[5]; + var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0 + ? match[2].replace("x", " ") + : (parseInt(match[3], 10) + 1) + match[4]; + replacements[i] = "\n" + indent + bullet + after; + } + } + + cm.replaceSelections(replacements); + }; +});