Skip to content

Commit

Permalink
fix: improve performance for isActive method, see #1930
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Sep 22, 2021
1 parent 6fa8862 commit fcca1e6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/helpers/getMarkAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/helpers/getMarksBetween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/helpers/getNodeAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/helpers/isMarkActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
}))]
})))
}
})

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/helpers/isNodeActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}]
})
}
})

Expand Down

0 comments on commit fcca1e6

Please sign in to comment.