mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-28 07:40:13 +08:00
fix: improve performance for isActive method, see #1930
This commit is contained in:
parent
6fa886273f
commit
fcca1e6f4d
@ -5,13 +5,13 @@ import getMarkType from './getMarkType'
|
||||
export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType): Record<string, any> {
|
||||
const type = getMarkType(typeOrName, state.schema)
|
||||
const { from, to, empty } = state.selection
|
||||
let marks: Mark[] = []
|
||||
const marks: Mark[] = []
|
||||
|
||||
if (empty) {
|
||||
marks = state.selection.$head.marks()
|
||||
marks.push(...state.selection.$head.marks())
|
||||
} else {
|
||||
state.doc.nodesBetween(from, to, node => {
|
||||
marks = [...marks, ...node.marks]
|
||||
marks.push(...node.marks)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -2,14 +2,14 @@ import { EditorState } from 'prosemirror-state'
|
||||
import { MarkRange } from '../types'
|
||||
|
||||
export default function getMarksBetween(from: number, to: number, state: EditorState): MarkRange[] {
|
||||
let marks: MarkRange[] = []
|
||||
const marks: MarkRange[] = []
|
||||
|
||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||
marks = [...marks, ...node.marks.map(mark => ({
|
||||
marks.push(...node.marks.map(mark => ({
|
||||
from: pos,
|
||||
to: pos + node.nodeSize,
|
||||
mark,
|
||||
}))]
|
||||
})))
|
||||
})
|
||||
|
||||
return marks
|
||||
|
@ -5,10 +5,10 @@ import getNodeType from './getNodeType'
|
||||
export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType): Record<string, any> {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const { from, to } = state.selection
|
||||
let nodes: Node[] = []
|
||||
const nodes: Node[] = []
|
||||
|
||||
state.doc.nodesBetween(from, to, node => {
|
||||
nodes = [...nodes, node]
|
||||
nodes.push(node)
|
||||
})
|
||||
|
||||
const node = nodes
|
||||
|
@ -27,7 +27,7 @@ export default function isMarkActive(
|
||||
}
|
||||
|
||||
let selectionRange = 0
|
||||
let markRanges: MarkRange[] = []
|
||||
const markRanges: MarkRange[] = []
|
||||
|
||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||
if (node.isText || node.marks.length) {
|
||||
@ -37,11 +37,11 @@ export default function isMarkActive(
|
||||
|
||||
selectionRange += range
|
||||
|
||||
markRanges = [...markRanges, ...node.marks.map(mark => ({
|
||||
markRanges.push(...node.marks.map(mark => ({
|
||||
mark,
|
||||
from: relativeFrom,
|
||||
to: relativeTo,
|
||||
}))]
|
||||
})))
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -14,18 +14,18 @@ export default function isNodeActive(
|
||||
? getNodeType(typeOrName, state.schema)
|
||||
: null
|
||||
|
||||
let nodeRanges: NodeRange[] = []
|
||||
const nodeRanges: NodeRange[] = []
|
||||
|
||||
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||
if (!node.isText) {
|
||||
const relativeFrom = Math.max(from, pos)
|
||||
const relativeTo = Math.min(to, pos + node.nodeSize)
|
||||
|
||||
nodeRanges = [...nodeRanges, {
|
||||
nodeRanges.push({
|
||||
node,
|
||||
from: relativeFrom,
|
||||
to: relativeTo,
|
||||
}]
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user