Skip to content

Commit

Permalink
perf: html paste
Browse files Browse the repository at this point in the history
  • Loading branch information
1943time committed Oct 4, 2023
1 parent 0a3d2c7 commit d03fb77
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/renderer/src/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>()
export const saveDoc$ = new Subject<any[] | null>()
const preventDefault = (e: React.CompositionEvent) => e.preventDefault()
Expand Down Expand Up @@ -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
Expand Down
38 changes: 21 additions & 17 deletions src/renderer/src/editor/plugins/htmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d03fb77

Please sign in to comment.