Skip to content

Commit

Permalink
feature marktext#3274: Select multiple lines in the code block and pr…
Browse files Browse the repository at this point in the history
…ess tab to indent multiple lines or tab + shift to cancel indenting multiple lines
  • Loading branch information
morecup committed May 23, 2022
1 parent 2bb405a commit 7473cee
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions src/muya/lib/contentState/tabCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -422,7 +471,7 @@ const tabCtrl = ContentState => {
if (this.isIndentableListItem()) {
return this.indentListItem()
}
return this.insertTab()
return this.insertTab(event)
}
}

Expand Down

0 comments on commit 7473cee

Please sign in to comment.