Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature #3274: Select multiple lines in the code block and press tab … #3275

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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