From d03fb77472c731b9e87b9d181acd9a1da5d4ce74 Mon Sep 17 00:00:00 2001 From: mdwriter-09idI Date: Wed, 4 Oct 2023 14:42:05 +0800 Subject: [PATCH] perf: html paste --- src/renderer/src/editor/Editor.tsx | 4 +- src/renderer/src/editor/plugins/htmlParser.ts | 38 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/renderer/src/editor/Editor.tsx b/src/renderer/src/editor/Editor.tsx index 47235626..d60af430 100644 --- a/src/renderer/src/editor/Editor.tsx +++ b/src/renderer/src/editor/Editor.tsx @@ -20,8 +20,6 @@ import {configStore} from '../store/config' import {countWords} from 'alfaaz' import {debounceTime, Subject} from 'rxjs' import {saveRecord} from '../store/db' -import {transformSchema} from './output/html/transform' -import {renderToString} from 'react-dom/server' const countThrottle$ = new Subject() export const saveDoc$ = new Subject() const preventDefault = (e: React.CompositionEvent) => e.preventDefault() @@ -68,7 +66,7 @@ export const MEditor = observer(({note}: { const count = useCallback((nodes: any[]) => { if (!configStore.config.showCharactersCount) return const root = Editor.node(editor, []) - const res = toMarkdown(nodes, '', [root[0]]) + const res = toMarkdown(nodes, '', root[0].children || []) const texts = Editor.nodes(editor, { at: [], match: n => n.text diff --git a/src/renderer/src/editor/plugins/htmlParser.ts b/src/renderer/src/editor/plugins/htmlParser.ts index d8b05a7b..be36d578 100644 --- a/src/renderer/src/editor/plugins/htmlParser.ts +++ b/src/renderer/src/editor/plugins/htmlParser.ts @@ -132,33 +132,37 @@ const getTextsNode = (nodes: any[]) => { return text } -const processFragment = (fragment: any[]) => { +const processFragment = (fragment: any[], parentType = '') => { let trans:any[] = [] - let list:any = null + let container: null | any = null for (let f of fragment) { if (f.text) { f.text = f.text.replace(/^\n+|\n+$/g, '') if (!f.text) continue } - if (['media', 'link'].includes(f.type)) { - f = {type: 'paragraph', children: [f]} - } - if (f.type === 'list-item') { - if (!list) { - list = {type: 'list', children: [f]} + if ((['media', 'link'].includes(f.type) || f.text) && !['paragraph', 'table-cell', 'head'].includes(parentType)) { + if (!container) { + f = {type: 'paragraph', children: [f]} + container = f + trans.push(container) } else { - list.children.push(f) + container.children.push(f) } - } else { - if (list) { - trans.push(list) - list = null + continue + } + if (f.type === 'list-item' && parentType !== 'list') { + if (!container) { + container = {type: 'list', children: [f]} + trans.push(container) + } else { + container.children.push(f) } - trans.push(f) + continue } - } - if (list) { - trans.push(list) + if (f.children && f.type) { + f.children = processFragment(f.children, f.type) + } + trans.push(f) } return trans }