mirror of
https://github.com/marktext/marktext.git
synced 2025-05-03 04:20:36 +08:00
codeblock intent handling
This commit is contained in:
parent
45f22fc95e
commit
efa2bf2b34
1
.gitignore
vendored
1
.gitignore
vendored
@ -16,4 +16,3 @@ thumbs.db
|
|||||||
.env
|
.env
|
||||||
.eslintcache
|
.eslintcache
|
||||||
marktext.code-workspace
|
marktext.code-workspace
|
||||||
**/*.NEW
|
|
||||||
|
@ -258,29 +258,42 @@ const backspaceCtrl = ContentState => {
|
|||||||
return this.singleRender(startBlock)
|
return this.singleRender(startBlock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix: https://github.com/marktext/marktext/issues/2013
|
|
||||||
// Also fix the codeblock crashed when the code content is '\n' and press backspace.
|
|
||||||
if (
|
if (
|
||||||
startBlock.functionType === 'codeContent' &&
|
startBlock.functionType === 'codeContent' &&
|
||||||
startBlock.key === endBlock.key &&
|
startBlock.key === endBlock.key &&
|
||||||
this.cursor.start.offset === this.cursor.end.offset &&
|
!(this.cursor.start.offset === 0 && this.cursor.end.offset === 0)
|
||||||
(/\n.$/.test(startBlock.text) || startBlock.text === '\n') &&
|
|
||||||
startBlock.text.length === this.cursor.start.offset
|
|
||||||
) {
|
) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
|
|
||||||
startBlock.text = /\n.$/.test(startBlock.text) ? startBlock.text.replace(/.$/, '') : ''
|
|
||||||
const { key } = startBlock
|
const { key } = startBlock
|
||||||
const offset = startBlock.text.length
|
let offset
|
||||||
|
const startOffset = this.cursor.start.offset
|
||||||
|
const endOffset = this.cursor.end.offset
|
||||||
|
// Fix: https://github.com/marktext/marktext/issues/2013
|
||||||
|
// Also fix the codeblock crashed when the code content is '\n' and press backspace.
|
||||||
|
if (
|
||||||
|
startOffset === endOffset &&
|
||||||
|
(/\n.$/.test(startBlock.text) || startBlock.text === '\n') &&
|
||||||
|
startBlock.text.length === startOffset
|
||||||
|
) {
|
||||||
|
startBlock.text = /\n.$/.test(startBlock.text) ? startBlock.text.slice(0, -1) : ''
|
||||||
|
offset = startBlock.text.length
|
||||||
|
} else {
|
||||||
|
// backspace at tabwidth within a codeblock if no text highlighted
|
||||||
|
// and cursor is after a tabWidth of whitespace
|
||||||
|
const regexUnindent = new RegExp(`\n.*(${String.fromCharCode(32).repeat(this.tabSize)})$`)
|
||||||
|
const shouldUnindent = regexUnindent.test(startBlock.text.substring(0, startOffset))
|
||||||
|
const backspaceSize = (shouldUnindent) ? this.tabSize : 1
|
||||||
|
offset = (startOffset === endOffset) ? startOffset - backspaceSize : startOffset
|
||||||
|
startBlock.text = startBlock.text.substring(0, offset) +
|
||||||
|
startBlock.text.substring(endOffset)
|
||||||
|
}
|
||||||
this.cursor = {
|
this.cursor = {
|
||||||
start: { key, offset },
|
start: { key, offset },
|
||||||
end: { key, offset }
|
end: { key, offset }
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.singleRender(startBlock)
|
return this.singleRender(startBlock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If select multiple paragraph or multiple characters in one paragraph, just let
|
// If select multiple paragraph or multiple characters in one paragraph, just let
|
||||||
// inputCtrl to handle this case.
|
// inputCtrl to handle this case.
|
||||||
if (start.key !== end.key || start.offset !== end.offset) {
|
if (start.key !== end.key || start.offset !== end.offset) {
|
||||||
|
@ -9,6 +9,10 @@ const checkAutoIndent = (text, offset) => {
|
|||||||
const pairStr = text.substring(offset - 1, offset + 1)
|
const pairStr = text.substring(offset - 1, offset + 1)
|
||||||
return /^(\{\}|\[\]|\(\)|><)$/.test(pairStr)
|
return /^(\{\}|\[\]|\(\)|><)$/.test(pairStr)
|
||||||
}
|
}
|
||||||
|
const getCodeblockIndentSpace = text => {
|
||||||
|
const match = /\n([ \t]*).*$/.exec(text)
|
||||||
|
return match ? match[1] : ''
|
||||||
|
}
|
||||||
const getIndentSpace = text => {
|
const getIndentSpace = text => {
|
||||||
const match = /^(\s*)\S/.exec(text)
|
const match = /^(\s*)\S/.exec(text)
|
||||||
return match ? match[1] : ''
|
return match ? match[1] : ''
|
||||||
@ -271,7 +275,7 @@ const enterCtrl = ContentState => {
|
|||||||
) {
|
) {
|
||||||
const { text, key } = block
|
const { text, key } = block
|
||||||
const autoIndent = checkAutoIndent(text, start.offset)
|
const autoIndent = checkAutoIndent(text, start.offset)
|
||||||
const indent = getIndentSpace(text)
|
const indent = getCodeblockIndentSpace(text.substring(1, start.offset))
|
||||||
block.text = text.substring(0, start.offset) +
|
block.text = text.substring(0, start.offset) +
|
||||||
'\n' +
|
'\n' +
|
||||||
(autoIndent ? indent + ' '.repeat(this.tabSize) + '\n' : '') +
|
(autoIndent ? indent + ' '.repeat(this.tabSize) + '\n' : '') +
|
||||||
|
@ -189,7 +189,7 @@ const tabCtrl = ContentState => {
|
|||||||
|
|
||||||
ContentState.prototype.insertTab = function () {
|
ContentState.prototype.insertTab = function () {
|
||||||
const tabSize = this.tabSize
|
const tabSize = this.tabSize
|
||||||
const tabCharacter = String.fromCharCode(160).repeat(tabSize)
|
const tabCharacter = String.fromCharCode(32).repeat(tabSize)
|
||||||
const { start, end } = this.cursor
|
const { start, end } = this.cursor
|
||||||
const startBlock = this.getBlock(start.key)
|
const startBlock = this.getBlock(start.key)
|
||||||
const endBlock = this.getBlock(end.key)
|
const endBlock = this.getBlock(end.key)
|
||||||
|
Loading…
Reference in New Issue
Block a user