|
|
import { Ancestor, DecoratedRange, Editor, Range } from 'slate' |
|
|
import { PLACEHOLDER_SYMBOL } from './weak-maps' |
|
|
import { DOMEditor } from '../plugin/dom-editor' |
|
|
|
|
|
export const shallowCompare = ( |
|
|
obj1: { [key: string]: unknown }, |
|
|
obj2: { [key: string]: unknown } |
|
|
) => |
|
|
Object.keys(obj1).length === Object.keys(obj2).length && |
|
|
Object.keys(obj1).every( |
|
|
key => obj2.hasOwnProperty(key) && obj1[key] === obj2[key] |
|
|
) |
|
|
|
|
|
const isDecorationFlagsEqual = (range: Range, other: Range) => { |
|
|
const { anchor: rangeAnchor, focus: rangeFocus, ...rangeOwnProps } = range |
|
|
const { anchor: otherAnchor, focus: otherFocus, ...otherOwnProps } = other |
|
|
|
|
|
return ( |
|
|
range[PLACEHOLDER_SYMBOL] === other[PLACEHOLDER_SYMBOL] && |
|
|
shallowCompare(rangeOwnProps, otherOwnProps) |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isElementDecorationsEqual = ( |
|
|
list: Range[] | null, |
|
|
another: Range[] | null |
|
|
): boolean => { |
|
|
if (list === another) { |
|
|
return true |
|
|
} |
|
|
|
|
|
if (!list || !another) { |
|
|
return false |
|
|
} |
|
|
|
|
|
if (list.length !== another.length) { |
|
|
return false |
|
|
} |
|
|
|
|
|
for (let i = 0; i < list.length; i++) { |
|
|
const range = list[i] |
|
|
const other = another[i] |
|
|
|
|
|
if (!Range.equals(range, other) || !isDecorationFlagsEqual(range, other)) { |
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
return true |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isTextDecorationsEqual = ( |
|
|
list: Range[] | null, |
|
|
another: Range[] | null |
|
|
): boolean => { |
|
|
if (list === another) { |
|
|
return true |
|
|
} |
|
|
|
|
|
if (!list || !another) { |
|
|
return false |
|
|
} |
|
|
|
|
|
if (list.length !== another.length) { |
|
|
return false |
|
|
} |
|
|
|
|
|
for (let i = 0; i < list.length; i++) { |
|
|
const range = list[i] |
|
|
const other = another[i] |
|
|
|
|
|
|
|
|
if ( |
|
|
range.anchor.offset !== other.anchor.offset || |
|
|
range.focus.offset !== other.focus.offset || |
|
|
!isDecorationFlagsEqual(range, other) |
|
|
) { |
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
return true |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const splitDecorationsByChild = ( |
|
|
editor: Editor, |
|
|
node: Ancestor, |
|
|
decorations: DecoratedRange[] |
|
|
): DecoratedRange[][] => { |
|
|
const decorationsByChild = Array.from( |
|
|
node.children, |
|
|
(): DecoratedRange[] => [] |
|
|
) |
|
|
|
|
|
if (decorations.length === 0) { |
|
|
return decorationsByChild |
|
|
} |
|
|
|
|
|
const path = DOMEditor.findPath(editor, node) |
|
|
const level = path.length |
|
|
const ancestorRange = Editor.range(editor, path) |
|
|
|
|
|
const cachedChildRanges = new Array<Range | undefined>(node.children.length) |
|
|
|
|
|
const getChildRange = (index: number) => { |
|
|
const cachedRange = cachedChildRanges[index] |
|
|
if (cachedRange) return cachedRange |
|
|
const childRange = Editor.range(editor, [...path, index]) |
|
|
cachedChildRanges[index] = childRange |
|
|
return childRange |
|
|
} |
|
|
|
|
|
for (const decoration of decorations) { |
|
|
const decorationRange = Range.intersection(ancestorRange, decoration) |
|
|
if (!decorationRange) continue |
|
|
|
|
|
const [startPoint, endPoint] = Range.edges(decorationRange) |
|
|
const startIndex = startPoint.path[level] |
|
|
const endIndex = endPoint.path[level] |
|
|
|
|
|
for (let i = startIndex; i <= endIndex; i++) { |
|
|
const ds = decorationsByChild[i] |
|
|
if (!ds) continue |
|
|
|
|
|
const childRange = getChildRange(i) |
|
|
const childDecorationRange = Range.intersection(childRange, decoration) |
|
|
if (!childDecorationRange) continue |
|
|
|
|
|
ds.push({ |
|
|
...decoration, |
|
|
...childDecorationRange, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
return decorationsByChild |
|
|
} |
|
|
|