Skip to content

Commit 5e897d3

Browse files
author
罗冉
committed
Optimization of render performance
1 parent 77b1b17 commit 5e897d3

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

src/muya/lib/contentState/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ class ContentState {
529529
getActiveBlocks () {
530530
const result = []
531531
let block = this.getBlock(this.cursor.start.key)
532-
if (block) result.push(block)
532+
if (block) {
533+
result.push(block)
534+
}
533535
while (block && block.parent) {
534536
block = this.getBlock(block.parent)
535537
result.push(block)

src/muya/lib/parser/render/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class StateRender {
1818
this.tokenCache = new Map()
1919
this.labels = new Map()
2020
this.urlMap = new Map()
21+
this.renderingTable = null
22+
this.renderingRowContainer = null
2123
this.container = null
2224
}
2325

src/muya/lib/parser/render/renderBlock/renderContainerBlock.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ export default function renderContainerBlock (parent, block, activeBlocks, match
3333
lang,
3434
column
3535
} = block
36+
37+
if (type === 'table') {
38+
this.renderingTable = block
39+
} else if (/thead|tbody/.test(type)) {
40+
this.renderingRowContainer = block
41+
}
42+
3643
const children = block.children.map(child => this.renderBlock(block, child, activeBlocks, matches, useCache))
3744
const data = {
3845
attrs: {},
@@ -51,8 +58,20 @@ export default function renderContainerBlock (parent, block, activeBlocks, match
5158
Object.assign(data.attrs, { spellcheck: 'false' })
5259
}
5360

54-
if (/th|td/.test(type)) {
61+
if (/^(th|td)$/.test(type)) {
5562
const { cells } = this.muya.contentState.selectedTableCells || {}
63+
if (align) {
64+
Object.assign(data.attrs, {
65+
style: `text-align:${align}`
66+
})
67+
}
68+
69+
if (typeof column === 'number') {
70+
Object.assign(data.dataset, {
71+
column
72+
})
73+
}
74+
5675
if (cells && cells.length) {
5776
const cell = cells.find(c => c.key === key)
5877
if (cell) {
@@ -71,45 +90,29 @@ export default function renderContainerBlock (parent, block, activeBlocks, match
7190
selector += '.ag-cell-border-left'
7291
}
7392
}
74-
}
75-
}
93+
} else {
94+
// Judge whether to render the table drag bar.
95+
const { renderingTable, renderingRowContainer } = this
7696

77-
// Judge whether to render the table drag bar.
78-
const { cells } = this.muya.contentState.selectedTableCells || {}
79-
if (/th|td/.test(type) && (!cells || cells && cells.length === 0)) {
80-
const table = this.muya.contentState.closest(block, 'table')
81-
const findTable = activeBlocks.find(b => b.key === table.key)
82-
if (findTable) {
83-
const { row: tableRow, column: tableColumn } = findTable
84-
const isLastRow = () => {
85-
const rowContainer = this.muya.contentState.closest(block, /tbody|thead/)
86-
if (rowContainer.type === 'thead') {
87-
return tableRow === 0
88-
} else {
89-
return !parent.nextSibling
97+
const findTable = renderingTable ? activeBlocks.find(b => b.key === renderingTable.key) : null
98+
if (findTable && renderingRowContainer) {
99+
const { row: tableRow, column: tableColumn } = findTable
100+
const isLastRow = () => {
101+
if (renderingRowContainer.type === 'thead') {
102+
return tableRow === 0
103+
} else {
104+
return !parent.nextSibling
105+
}
106+
}
107+
if (block.parent === activeBlocks[1].parent && !block.preSibling && tableRow > 0) {
108+
children.unshift(renderLeftBar())
90109
}
91-
}
92-
if (block.parent === activeBlocks[1].parent && !block.preSibling && tableRow > 0) {
93-
children.unshift(renderLeftBar())
94-
}
95110

96-
if (column === activeBlocks[1].column && isLastRow() && tableColumn > 0) {
97-
children.push(renderBottomBar())
111+
if (column === activeBlocks[1].column && isLastRow() && tableColumn > 0) {
112+
children.push(renderBottomBar())
113+
}
98114
}
99115
}
100-
}
101-
102-
if (/th|td/.test(type)) {
103-
if (align) {
104-
Object.assign(data.attrs, {
105-
style: `text-align:${align}`
106-
})
107-
}
108-
if (typeof column === 'number') {
109-
Object.assign(data.dataset, {
110-
column
111-
})
112-
}
113116
} else if (/^h/.test(type)) {
114117
if (/^h\d$/.test(type)) {
115118
// TODO: This should be the best place to create and update the TOC.

0 commit comments

Comments
 (0)