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

Codeblock indent rnw pr #3298

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ thumbs.db
.idea
.env
.eslintcache
marktext.code-workspace
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.15.1
33 changes: 23 additions & 10 deletions src/muya/lib/contentState/backspaceCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,29 +258,42 @@ const backspaceCtrl = ContentState => {
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 (
startBlock.functionType === 'codeContent' &&
startBlock.key === endBlock.key &&
this.cursor.start.offset === this.cursor.end.offset &&
(/\n.$/.test(startBlock.text) || startBlock.text === '\n') &&
startBlock.text.length === this.cursor.start.offset
!(this.cursor.start.offset === 0 && this.cursor.end.offset === 0)
) {
event.preventDefault()
event.stopPropagation()

startBlock.text = /\n.$/.test(startBlock.text) ? startBlock.text.replace(/.$/, '') : ''
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 = {
start: { key, offset },
end: { key, offset }
}

return this.singleRender(startBlock)
}

// If select multiple paragraph or multiple characters in one paragraph, just let
// inputCtrl to handle this case.
if (start.key !== end.key || start.offset !== end.offset) {
Expand Down
6 changes: 5 additions & 1 deletion src/muya/lib/contentState/enterCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const checkAutoIndent = (text, offset) => {
const pairStr = text.substring(offset - 1, offset + 1)
return /^(\{\}|\[\]|\(\)|><)$/.test(pairStr)
}
const getCodeblockIndentSpace = text => {
const match = /\n([ \t]*).*$/.exec(text)
return match ? match[1] : ''
}
const getIndentSpace = text => {
const match = /^(\s*)\S/.exec(text)
return match ? match[1] : ''
Expand Down Expand Up @@ -271,7 +275,7 @@ const enterCtrl = ContentState => {
) {
const { text, key } = block
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) +
'\n' +
(autoIndent ? indent + ' '.repeat(this.tabSize) + '\n' : '') +
Expand Down
2 changes: 1 addition & 1 deletion src/muya/lib/contentState/tabCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const tabCtrl = ContentState => {

ContentState.prototype.insertTab = function () {
const tabSize = this.tabSize
const tabCharacter = String.fromCharCode(160).repeat(tabSize)
const tabCharacter = String.fromCharCode(32).repeat(tabSize)
const { start, end } = this.cursor
const startBlock = this.getBlock(start.key)
const endBlock = this.getBlock(end.key)
Expand Down