prosemirror-view 1.23.13 → 1.24.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
- import browser from "./browser"
1
+ import * as browser from "./browser"
2
2
  import {domIndex, isEquivalentPosition} from "./dom"
3
3
  import {hasFocusAndSelection, selectionToDOM} from "./selection"
4
+ import {EditorView} from "./index"
4
5
 
5
6
  const observeOptions = {
6
7
  childList: true,
@@ -14,27 +15,38 @@ const observeOptions = {
14
15
  const useCharData = browser.ie && browser.ie_version <= 11
15
16
 
16
17
  class SelectionState {
17
- constructor() {
18
- this.anchorNode = this.anchorOffset = this.focusNode = this.focusOffset = null
19
- }
18
+ anchorNode: Node | null = null
19
+ anchorOffset: number = 0
20
+ focusNode: Node | null = null
21
+ focusOffset: number = 0
20
22
 
21
- set(sel) {
23
+ set(sel: Selection) {
22
24
  this.anchorNode = sel.anchorNode; this.anchorOffset = sel.anchorOffset
23
25
  this.focusNode = sel.focusNode; this.focusOffset = sel.focusOffset
24
26
  }
25
27
 
26
- eq(sel) {
28
+ clear() {
29
+ this.anchorNode = this.focusNode = null
30
+ }
31
+
32
+ eq(sel: Selection) {
27
33
  return sel.anchorNode == this.anchorNode && sel.anchorOffset == this.anchorOffset &&
28
34
  sel.focusNode == this.focusNode && sel.focusOffset == this.focusOffset
29
35
  }
30
36
  }
31
37
 
32
38
  export class DOMObserver {
33
- constructor(view, handleDOMChange) {
34
- this.view = view
35
- this.handleDOMChange = handleDOMChange
36
- this.queue = []
37
- this.flushingSoon = -1
39
+ queue: MutationRecord[] = []
40
+ flushingSoon = -1
41
+ observer: MutationObserver | null = null
42
+ currentSelection = new SelectionState
43
+ onCharData: ((e: Event) => void) | null = null
44
+ suppressingSelectionUpdates = false
45
+
46
+ constructor(
47
+ readonly view: EditorView,
48
+ readonly handleDOMChange: (from: number, to: number, typeOver: boolean, added: Node[]) => void
49
+ ) {
38
50
  this.observer = window.MutationObserver &&
39
51
  new window.MutationObserver(mutations => {
40
52
  for (let i = 0; i < mutations.length; i++) this.queue.push(mutations[i])
@@ -44,20 +56,18 @@ export class DOMObserver {
44
56
  // ProseMirror to miss the change (see #930)
45
57
  if (browser.ie && browser.ie_version <= 11 && mutations.some(
46
58
  m => m.type == "childList" && m.removedNodes.length ||
47
- m.type == "characterData" && m.oldValue.length > m.target.nodeValue.length))
59
+ m.type == "characterData" && m.oldValue!.length > m.target.nodeValue!.length))
48
60
  this.flushSoon()
49
61
  else
50
62
  this.flush()
51
63
  })
52
- this.currentSelection = new SelectionState
53
64
  if (useCharData) {
54
65
  this.onCharData = e => {
55
- this.queue.push({target: e.target, type: "characterData", oldValue: e.prevValue})
66
+ this.queue.push({target: e.target as Node, type: "characterData", oldValue: (e as any).prevValue} as MutationRecord)
56
67
  this.flushSoon()
57
68
  }
58
69
  }
59
70
  this.onSelectionChange = this.onSelectionChange.bind(this)
60
- this.suppressingSelectionUpdates = false
61
71
  }
62
72
 
63
73
  flushSoon() {
@@ -76,7 +86,7 @@ export class DOMObserver {
76
86
  start() {
77
87
  if (this.observer)
78
88
  this.observer.observe(this.view.dom, observeOptions)
79
- if (useCharData)
89
+ if (this.onCharData)
80
90
  this.view.dom.addEventListener("DOMCharacterDataModified", this.onCharData)
81
91
  this.connectSelection()
82
92
  }
@@ -90,7 +100,7 @@ export class DOMObserver {
90
100
  }
91
101
  this.observer.disconnect()
92
102
  }
93
- if (useCharData) this.view.dom.removeEventListener("DOMCharacterDataModified", this.onCharData)
103
+ if (this.onCharData) this.view.dom.removeEventListener("DOMCharacterDataModified", this.onCharData)
94
104
  this.disconnectSelection()
95
105
  }
96
106
 
@@ -114,23 +124,26 @@ export class DOMObserver {
114
124
  // us a selection change event before the DOM changes are
115
125
  // reported.
116
126
  if (browser.ie && browser.ie_version <= 11 && !this.view.state.selection.empty) {
117
- let sel = this.view.root.getSelection()
127
+ let sel = this.view.domSelection()
118
128
  // Selection.isCollapsed isn't reliable on IE
119
- if (sel.focusNode && isEquivalentPosition(sel.focusNode, sel.focusOffset, sel.anchorNode, sel.anchorOffset))
129
+ if (sel.focusNode && isEquivalentPosition(sel.focusNode, sel.focusOffset, sel.anchorNode!, sel.anchorOffset))
120
130
  return this.flushSoon()
121
131
  }
122
132
  this.flush()
123
133
  }
124
134
 
125
135
  setCurSelection() {
126
- this.currentSelection.set(this.view.root.getSelection())
136
+ this.currentSelection.set(this.view.domSelection())
127
137
  }
128
138
 
129
- ignoreSelectionChange(sel) {
139
+ ignoreSelectionChange(sel: Selection) {
130
140
  if (sel.rangeCount == 0) return true
131
141
  let container = sel.getRangeAt(0).commonAncestorContainer
132
142
  let desc = this.view.docView.nearestDesc(container)
133
- if (desc && desc.ignoreMutation({type: "selection", target: container.nodeType == 3 ? container.parentNode : container})) {
143
+ if (desc && desc.ignoreMutation({
144
+ type: "selection",
145
+ target: container.nodeType == 3 ? container.parentNode : container
146
+ } as any)) {
134
147
  this.setCurSelection()
135
148
  return true
136
149
  }
@@ -144,10 +157,10 @@ export class DOMObserver {
144
157
  this.queue.length = 0
145
158
  }
146
159
 
147
- let sel = this.view.root.getSelection()
160
+ let sel = this.view.domSelection()
148
161
  let newSel = !this.suppressingSelectionUpdates && !this.currentSelection.eq(sel) && hasFocusAndSelection(this.view) && !this.ignoreSelectionChange(sel)
149
162
 
150
- let from = -1, to = -1, typeOver = false, added = []
163
+ let from = -1, to = -1, typeOver = false, added: Node[] = []
151
164
  if (this.view.editable) {
152
165
  for (let i = 0; i < mutations.length; i++) {
153
166
  let result = this.registerMutation(mutations[i], added)
@@ -162,7 +175,7 @@ export class DOMObserver {
162
175
  if (browser.gecko && added.length > 1) {
163
176
  let brs = added.filter(n => n.nodeName == "BR")
164
177
  if (brs.length == 2) {
165
- let [a, b] = brs
178
+ let a = brs[0] as HTMLElement, b = brs[1] as HTMLElement
166
179
  if (a.parentNode && a.parentNode.parentNode == b.parentNode) b.remove()
167
180
  else a.remove()
168
181
  }
@@ -180,14 +193,14 @@ export class DOMObserver {
180
193
  }
181
194
  }
182
195
 
183
- registerMutation(mut, added) {
196
+ registerMutation(mut: MutationRecord, added: Node[]) {
184
197
  // Ignore mutations inside nodes that were already noted as inserted
185
198
  if (added.indexOf(mut.target) > -1) return null
186
199
  let desc = this.view.docView.nearestDesc(mut.target)
187
200
  if (mut.type == "attributes" &&
188
201
  (desc == this.view.docView || mut.attributeName == "contenteditable" ||
189
202
  // Firefox sometimes fires spurious events for null/empty styles
190
- (mut.attributeName == "style" && !mut.oldValue && !mut.target.getAttribute("style"))))
203
+ (mut.attributeName == "style" && !mut.oldValue && !(mut.target as HTMLElement).getAttribute("style"))))
191
204
  return null
192
205
  if (!desc || desc.ignoreMutation(mut)) return null
193
206
 
@@ -230,7 +243,7 @@ export class DOMObserver {
230
243
 
231
244
  let cssChecked = false
232
245
 
233
- function checkCSS(view) {
246
+ function checkCSS(view: EditorView) {
234
247
  if (cssChecked) return
235
248
  cssChecked = true
236
249
  if (getComputedStyle(view.dom).whiteSpace == "normal")