diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 8708b170..5545d787 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -113,6 +113,8 @@ foo
bar
zar - Fixed list parse error [more info](https://github.com/marktext/marktext/issues/831#issuecomment-477719256) - Fixed source code mode tab switching - Fixed source code mode to preview switching +- Mark Text didn't remove highlight when I delete the markdown symbol like * or `. (#893) +- After delete ``` at the beginning to paragraph by backspace, then type other text foo, the color will be strange, if you type 1. bar. error happened. (#892) ### 0.13.65 diff --git a/src/muya/lib/contentState/backspaceCtrl.js b/src/muya/lib/contentState/backspaceCtrl.js index 61aceaf8..1f0e4a55 100644 --- a/src/muya/lib/contentState/backspaceCtrl.js +++ b/src/muya/lib/contentState/backspaceCtrl.js @@ -1,5 +1,6 @@ import selection from '../selection' import { findNearestParagraph, findOutMostParagraph } from '../selection/dom' +import { tokenizer, generator } from '../parser/parse' const backspaceCtrl = ContentState => { ContentState.prototype.checkBackspaceCase = function () { @@ -101,11 +102,51 @@ const backspaceCtrl = ContentState => { ContentState.prototype.backspaceHandler = function (event) { const { start, end } = selection.getCursorRange() + if (!start || !end) { return } const startBlock = this.getBlock(start.key) const endBlock = this.getBlock(end.key) + // fix: #897 + const { text } = startBlock + const tokens = tokenizer(text) + let needRender = false + let preToken = null + for (const token of tokens) { + // handle delete the second $ in inline_math. + if ( + token.range.end === start.offset && + token.type === 'inline_math' + ) { + needRender = true + token.raw = token.raw.substr(0, token.raw.length - 1) + break + } + // handle pre token is a html tag, need preventdefault. + if ( + token.range.start + 1 === start.offset && + preToken && + preToken.type === 'html_tag' && + preToken.tag === 'ruby' + ) { + needRender = true + token.raw = token.raw.substr(1) + break + } + preToken = token + } + if (needRender) { + startBlock.text = generator(tokens) + event.preventDefault() + start.offset-- + end.offset-- + this.cursor = { + start, + end + } + return this.partialRender() + } // fix: #67 problem 1 if (startBlock.icon) return event.preventDefault() // fix: unexpect remove all editor html. #67 problem 4 diff --git a/src/muya/lib/contentState/inputCtrl.js b/src/muya/lib/contentState/inputCtrl.js index 57f70a50..4ffed4f7 100644 --- a/src/muya/lib/contentState/inputCtrl.js +++ b/src/muya/lib/contentState/inputCtrl.js @@ -41,6 +41,42 @@ const inputCtrl = ContentState => { return tokens.filter(t => t.type === 'inline_math').some(t => offset >= t.range.start && offset <= t.range.end) } + ContentState.prototype.checkNotSameToken = function (oldText, text) { + const oldTokens = tokenizer(oldText) + const tokens = tokenizer(text) + + const oldCache = {} + const cache = {} + + for (const { type } of oldTokens) { + if (oldCache[type]) { + oldCache[type]++ + } else { + oldCache[type] = 1 + } + } + + for (const { type } of tokens) { + if (cache[type]) { + cache[type]++ + } else { + cache[type] = 1 + } + } + + if (Object.keys(oldCache).length !== Object.keys(cache).length) { + return true + } + + for (const key of Object.keys(oldCache)) { + if (!cache[key] || oldCache[key] !== cache[key]) { + return true + } + } + + return false + } + ContentState.prototype.inputHandler = function (event) { const { start, end } = selection.getCursorRange() if (!start || !end) { @@ -135,6 +171,10 @@ const inputCtrl = ContentState => { } } } + + if (this.checkNotSameToken(block.text, text)) { + needRender = true + } block.text = text if (beginRules['reference_definition'].test(text)) { needRenderAll = true