File size: 4,243 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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)
  )
}

/**
 * Check if a list of decorator ranges are equal to another.
 *
 * PERF: this requires the two lists to also have the ranges inside them in the
 * same order, but this is an okay constraint for us since decorations are
 * kept in order, and the odd case where they aren't is okay to re-render for.
 */

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
}

/**
 * Check if a list of decorator ranges are equal to another.
 *
 * PERF: this requires the two lists to also have the ranges inside them in the
 * same order, but this is an okay constraint for us since decorations are
 * kept in order, and the odd case where they aren't is okay to re-render for.
 */

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]

    // compare only offsets because paths doesn't matter for text
    if (
      range.anchor.offset !== other.anchor.offset ||
      range.focus.offset !== other.focus.offset ||
      !isDecorationFlagsEqual(range, other)
    ) {
      return false
    }
  }

  return true
}

/**
 * Split and group decorations by each child of a node.
 *
 * @returns An array with length equal to that of `node.children`. Each index
 * corresponds to a child of `node`, and the value is an array of decorations
 * for that child.
 */

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
}