feature #3274: Select multiple lines in the code block and press tab to indent multiple lines or tab + shift to cancel indenting multiple lines

This commit is contained in:
morecup 2022-05-23 18:13:30 +08:00
parent 2bb405a03e
commit 7473ceefde

View File

@ -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,9 +362,9 @@ const tabCtrl = ContentState => {
const unindentType = this.isUnindentableListItem(startBlock)
if (unindentType) {
this.unindentListItem(startBlock, unindentType)
}
return
}
}
// Handle `tab` to jump to the end of format when the cursor is at the end of format content.
if (
@ -422,7 +471,7 @@ const tabCtrl = ContentState => {
if (this.isIndentableListItem()) {
return this.indentListItem()
}
return this.insertTab()
return this.insertTab(event)
}
}