From 2e133e21e0aeb8a77b6da87593b64aabb36166a1 Mon Sep 17 00:00:00 2001 From: jocs Date: Wed, 7 Nov 2018 20:27:53 +0800 Subject: [PATCH] fix: #546 --- .github/CHANGELOG.md | 1 + src/muya/lib/contentState/pasteCtrl.js | 12 ++++++++++++ src/muya/lib/parser/parse.js | 4 ++-- .../render/renderBlock/renderLeafBlock.js | 17 ++++++++++++----- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 3508a1a2..0cfd6edb 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -13,6 +13,7 @@ - fix: #534 - fix: #535 Application menu is not updated when switching windows - fix #216 and #311 key binding issues on Linux and Windows +- fix #546 paste issue in table ### 0.13.50 diff --git a/src/muya/lib/contentState/pasteCtrl.js b/src/muya/lib/contentState/pasteCtrl.js index ca582733..d30ab4e6 100644 --- a/src/muya/lib/contentState/pasteCtrl.js +++ b/src/muya/lib/contentState/pasteCtrl.js @@ -131,6 +131,18 @@ const pasteCtrl = ContentState => { return this.partialRender() } + if (/th|td/.test(startBlock.type)) { + const pendingText = text.trim().replace(/\n/g, '
') + startBlock.text += pendingText + const { key } = startBlock + const offset = start.offset + pendingText.length + this.cursor = { + start: { key, offset }, + end: { key, offset } + } + return this.partialRender() + } + // handle copyAsHtml if (copyType === 'copyAsHtml') { switch (type) { diff --git a/src/muya/lib/parser/parse.js b/src/muya/lib/parser/parse.js index 122111c5..cf0c3980 100644 --- a/src/muya/lib/parser/parse.js +++ b/src/muya/lib/parser/parse.js @@ -508,8 +508,8 @@ const tokenizerFac = (src, beginRules, inlineRules, pos = 0, top) => { return tokens } -export const tokenizer = (src, highlights = []) => { - const tokens = tokenizerFac(src, beginRules, inlineRules, 0, true) +export const tokenizer = (src, highlights = [], hasBeginRules = true) => { + const tokens = tokenizerFac(src, hasBeginRules ? beginRules : null, inlineRules, 0, true) const postTokenizer = tokens => { for (const token of tokens) { for (const light of highlights) { diff --git a/src/muya/lib/parser/render/renderBlock/renderLeafBlock.js b/src/muya/lib/parser/render/renderBlock/renderLeafBlock.js index ecebb44f..e8a3207e 100644 --- a/src/muya/lib/parser/render/renderBlock/renderLeafBlock.js +++ b/src/muya/lib/parser/render/renderBlock/renderLeafBlock.js @@ -1,7 +1,7 @@ import katex from 'katex' import mermaid from 'mermaid' import prism, { loadedCache } from '../../../prism/' -import { CLASS_OR_ID, DEVICE_MEMORY, isInElectron, PREVIEW_DOMPURIFY_CONFIG } from '../../../config' +import { CLASS_OR_ID, DEVICE_MEMORY, isInElectron, PREVIEW_DOMPURIFY_CONFIG, HAS_TEXT_BLOCK_REG } from '../../../config' import { tokenizer } from '../../parse' import { snakeToCamel, sanitize, escapeHtml, getLongUniqueId } from '../../../utils' import { h, htmlToVNode } from '../snabbdom' @@ -70,12 +70,19 @@ export default function renderLeafBlock (block, cursor, activeBlocks, matches, u } let children = '' if (text) { - let tokens = null + let tokens = [] if (highlights.length === 0 && this.tokenCache.has(text)) { tokens = this.tokenCache.get(text) - } else { - tokens = tokenizer(text, highlights) - if (highlights.length === 0 && useCache && DEVICE_MEMORY >= 4) this.tokenCache.set(text, tokens) + } else if ( + HAS_TEXT_BLOCK_REG.test(type) && + functionType !== 'codeLine' && + functionType !== 'languageInput' + ) { + const hasBeginRules = /^(h\d|span|hr)/.test(type) + tokens = tokenizer(text, highlights, hasBeginRules) + if (highlights.length === 0 && useCache && DEVICE_MEMORY >= 4) { + this.tokenCache.set(text, tokens) + } } children = tokens.reduce((acc, token) => [...acc, ...this[snakeToCamel(token.type)](h, cursor, block, token)], []) }