fix: improve performance for isActive method, see #1930

This commit is contained in:
Philipp Kühn
2021-09-22 19:43:55 +02:00
parent 6fa886273f
commit fcca1e6f4d
5 changed files with 14 additions and 14 deletions

View File

@@ -5,13 +5,13 @@ import getMarkType from './getMarkType'
export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType): Record<string, any> { export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType): Record<string, any> {
const type = getMarkType(typeOrName, state.schema) const type = getMarkType(typeOrName, state.schema)
const { from, to, empty } = state.selection const { from, to, empty } = state.selection
let marks: Mark[] = [] const marks: Mark[] = []
if (empty) { if (empty) {
marks = state.selection.$head.marks() marks.push(...state.selection.$head.marks())
} else { } else {
state.doc.nodesBetween(from, to, node => { state.doc.nodesBetween(from, to, node => {
marks = [...marks, ...node.marks] marks.push(...node.marks)
}) })
} }

View File

@@ -2,14 +2,14 @@ import { EditorState } from 'prosemirror-state'
import { MarkRange } from '../types' import { MarkRange } from '../types'
export default function getMarksBetween(from: number, to: number, state: EditorState): MarkRange[] { export default function getMarksBetween(from: number, to: number, state: EditorState): MarkRange[] {
let marks: MarkRange[] = [] const marks: MarkRange[] = []
state.doc.nodesBetween(from, to, (node, pos) => { state.doc.nodesBetween(from, to, (node, pos) => {
marks = [...marks, ...node.marks.map(mark => ({ marks.push(...node.marks.map(mark => ({
from: pos, from: pos,
to: pos + node.nodeSize, to: pos + node.nodeSize,
mark, mark,
}))] })))
}) })
return marks return marks

View File

@@ -5,10 +5,10 @@ import getNodeType from './getNodeType'
export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType): Record<string, any> { export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType): Record<string, any> {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const { from, to } = state.selection const { from, to } = state.selection
let nodes: Node[] = [] const nodes: Node[] = []
state.doc.nodesBetween(from, to, node => { state.doc.nodesBetween(from, to, node => {
nodes = [...nodes, node] nodes.push(node)
}) })
const node = nodes const node = nodes

View File

@@ -27,7 +27,7 @@ export default function isMarkActive(
} }
let selectionRange = 0 let selectionRange = 0
let markRanges: MarkRange[] = [] const markRanges: MarkRange[] = []
state.doc.nodesBetween(from, to, (node, pos) => { state.doc.nodesBetween(from, to, (node, pos) => {
if (node.isText || node.marks.length) { if (node.isText || node.marks.length) {
@@ -37,11 +37,11 @@ export default function isMarkActive(
selectionRange += range selectionRange += range
markRanges = [...markRanges, ...node.marks.map(mark => ({ markRanges.push(...node.marks.map(mark => ({
mark, mark,
from: relativeFrom, from: relativeFrom,
to: relativeTo, to: relativeTo,
}))] })))
} }
}) })

View File

@@ -14,18 +14,18 @@ export default function isNodeActive(
? getNodeType(typeOrName, state.schema) ? getNodeType(typeOrName, state.schema)
: null : null
let nodeRanges: NodeRange[] = [] const nodeRanges: NodeRange[] = []
state.doc.nodesBetween(from, to, (node, pos) => { state.doc.nodesBetween(from, to, (node, pos) => {
if (!node.isText) { if (!node.isText) {
const relativeFrom = Math.max(from, pos) const relativeFrom = Math.max(from, pos)
const relativeTo = Math.min(to, pos + node.nodeSize) const relativeTo = Math.min(to, pos + node.nodeSize)
nodeRanges = [...nodeRanges, { nodeRanges.push({
node, node,
from: relativeFrom, from: relativeFrom,
to: relativeTo, to: relativeTo,
}] })
} }
}) })