From 7473ceefde72a0136d35b4c6b734063ffd28abab Mon Sep 17 00:00:00 2001 From: morecup Date: Mon, 23 May 2022 18:13:30 +0800 Subject: [PATCH] feature #3274: Select multiple lines in the code block and press tab to indent multiple lines or tab + shift to cancel indenting multiple lines --- src/muya/lib/contentState/tabCtrl.js | 55 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/muya/lib/contentState/tabCtrl.js b/src/muya/lib/contentState/tabCtrl.js index 4a2e6790..70b78910 100644 --- a/src/muya/lib/contentState/tabCtrl.js +++ b/src/muya/lib/contentState/tabCtrl.js @@ -187,7 +187,7 @@ const tabCtrl = ContentState => { return this.partialRender() } - ContentState.prototype.insertTab = function () { + ContentState.prototype.insertTab = function (event) { const tabSize = this.tabSize const tabCharacter = String.fromCharCode(160).repeat(tabSize) const { start, end } = this.cursor @@ -203,6 +203,55 @@ const tabCtrl = ContentState => { end: { key, offset } } return this.partialRender() + } else if (start.key === end.key && + start.offset !== end.offset && + startBlock.type === 'span' && + startBlock.functionType === 'codeContent') { + // 在代码块内 + let nowLen = 0 + let oldText = startBlock.text + let lines = oldText.split('\n') + let dealLine + let startTabSize = null + if (event.shiftKey) { + dealLine = (line) => { + let i = 0 + for (; i < line.length && i < tabSize; i++) { + if (!(line.charAt(i) === String.fromCharCode(160) || line.charAt(i) === String.fromCharCode(32))) { + break + } + } + if (!startTabSize) startTabSize = -1 * i + return line.substr(i) + } + } else { + startTabSize = tabSize + dealLine = (line) => { + return tabCharacter + line + } + } + let isDealLine = false + for (let nowLineNum = 0; nowLineNum < lines.length; nowLineNum++) { + nowLen += lines[nowLineNum].length + if (start.offset <= nowLen && !isDealLine) { + isDealLine = true + } + if (isDealLine) lines[nowLineNum] = dealLine(lines[nowLineNum]) + if (end.offset <= nowLen) { + break + } + nowLen += 1 + } + startBlock.text = lines.join('\n') + let sk = start.key + let so = start.offset + startTabSize + let ek = end.key + let eo = startBlock.text.length - (oldText.length - end.offset) + this.cursor = { + start: { key: sk, offset: so }, + end: { key: ek, offset: eo } + } + return this.partialRender() } } @@ -313,8 +362,8 @@ const tabCtrl = ContentState => { const unindentType = this.isUnindentableListItem(startBlock) if (unindentType) { this.unindentListItem(startBlock, unindentType) + return } - return } // Handle `tab` to jump to the end of format when the cursor is at the end of format content. @@ -422,7 +471,7 @@ const tabCtrl = ContentState => { if (this.isIndentableListItem()) { return this.indentListItem() } - return this.insertTab() + return this.insertTab(event) } }