@jvs-milkdown/components 1.0.0

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.
Files changed (192) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/lib/__internal__/components/icon.d.ts +24 -0
  4. package/lib/__internal__/components/icon.d.ts.map +1 -0
  5. package/lib/__internal__/components/image-input.d.ts +17 -0
  6. package/lib/__internal__/components/image-input.d.ts.map +1 -0
  7. package/lib/__internal__/keep-alive.d.ts +2 -0
  8. package/lib/__internal__/keep-alive.d.ts.map +1 -0
  9. package/lib/__internal__/meta.d.ts +3 -0
  10. package/lib/__internal__/meta.d.ts.map +1 -0
  11. package/lib/__tests__/setup.d.ts +2 -0
  12. package/lib/__tests__/setup.d.ts.map +1 -0
  13. package/lib/code-block/config.d.ts +23 -0
  14. package/lib/code-block/config.d.ts.map +1 -0
  15. package/lib/code-block/index.d.ts +5 -0
  16. package/lib/code-block/index.d.ts.map +1 -0
  17. package/lib/code-block/index.js +4160 -0
  18. package/lib/code-block/index.js.map +1 -0
  19. package/lib/code-block/view/components/code-block.d.ts +16 -0
  20. package/lib/code-block/view/components/code-block.d.ts.map +1 -0
  21. package/lib/code-block/view/components/copy-button.d.ts +9 -0
  22. package/lib/code-block/view/components/copy-button.d.ts.map +1 -0
  23. package/lib/code-block/view/components/language-picker.d.ts +5 -0
  24. package/lib/code-block/view/components/language-picker.d.ts.map +1 -0
  25. package/lib/code-block/view/components/preview-panel.d.ts +9 -0
  26. package/lib/code-block/view/components/preview-panel.d.ts.map +1 -0
  27. package/lib/code-block/view/index.d.ts +3 -0
  28. package/lib/code-block/view/index.d.ts.map +1 -0
  29. package/lib/code-block/view/loader.d.ts +13 -0
  30. package/lib/code-block/view/loader.d.ts.map +1 -0
  31. package/lib/code-block/view/node-view.d.ts +40 -0
  32. package/lib/code-block/view/node-view.d.ts.map +1 -0
  33. package/lib/image-block/config.d.ts +16 -0
  34. package/lib/image-block/config.d.ts.map +1 -0
  35. package/lib/image-block/index.d.ts +7 -0
  36. package/lib/image-block/index.d.ts.map +1 -0
  37. package/lib/image-block/index.js +660 -0
  38. package/lib/image-block/index.js.map +1 -0
  39. package/lib/image-block/remark-plugin.d.ts +2 -0
  40. package/lib/image-block/remark-plugin.d.ts.map +1 -0
  41. package/lib/image-block/schema.d.ts +3 -0
  42. package/lib/image-block/schema.d.ts.map +1 -0
  43. package/lib/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.d.ts +2 -0
  44. package/lib/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.d.ts.map +1 -0
  45. package/lib/image-block/view/components/image-block.d.ts +18 -0
  46. package/lib/image-block/view/components/image-block.d.ts.map +1 -0
  47. package/lib/image-block/view/components/image-viewer.d.ts +3 -0
  48. package/lib/image-block/view/components/image-viewer.d.ts.map +1 -0
  49. package/lib/image-block/view/index.d.ts +3 -0
  50. package/lib/image-block/view/index.d.ts.map +1 -0
  51. package/lib/image-inline/components/image-inline.d.ts +18 -0
  52. package/lib/image-inline/components/image-inline.d.ts.map +1 -0
  53. package/lib/image-inline/config.d.ts +11 -0
  54. package/lib/image-inline/config.d.ts.map +1 -0
  55. package/lib/image-inline/index.d.ts +5 -0
  56. package/lib/image-inline/index.d.ts.map +1 -0
  57. package/lib/image-inline/index.js +377 -0
  58. package/lib/image-inline/index.js.map +1 -0
  59. package/lib/image-inline/view.d.ts +3 -0
  60. package/lib/image-inline/view.d.ts.map +1 -0
  61. package/lib/index.d.ts +2 -0
  62. package/lib/index.d.ts.map +1 -0
  63. package/lib/index.js +35 -0
  64. package/lib/index.js.map +1 -0
  65. package/lib/link-tooltip/command.d.ts +2 -0
  66. package/lib/link-tooltip/command.d.ts.map +1 -0
  67. package/lib/link-tooltip/configure.d.ts +3 -0
  68. package/lib/link-tooltip/configure.d.ts.map +1 -0
  69. package/lib/link-tooltip/edit/component.d.ts +11 -0
  70. package/lib/link-tooltip/edit/component.d.ts.map +1 -0
  71. package/lib/link-tooltip/edit/edit-configure.d.ts +3 -0
  72. package/lib/link-tooltip/edit/edit-configure.d.ts.map +1 -0
  73. package/lib/link-tooltip/edit/edit-view.d.ts +15 -0
  74. package/lib/link-tooltip/edit/edit-view.d.ts.map +1 -0
  75. package/lib/link-tooltip/index.d.ts +7 -0
  76. package/lib/link-tooltip/index.d.ts.map +1 -0
  77. package/lib/link-tooltip/index.js +2526 -0
  78. package/lib/link-tooltip/index.js.map +1 -0
  79. package/lib/link-tooltip/preview/component.d.ts +11 -0
  80. package/lib/link-tooltip/preview/component.d.ts.map +1 -0
  81. package/lib/link-tooltip/preview/preview-configure.d.ts +3 -0
  82. package/lib/link-tooltip/preview/preview-configure.d.ts.map +1 -0
  83. package/lib/link-tooltip/preview/preview-view.d.ts +14 -0
  84. package/lib/link-tooltip/preview/preview-view.d.ts.map +1 -0
  85. package/lib/link-tooltip/slices.d.ts +34 -0
  86. package/lib/link-tooltip/slices.d.ts.map +1 -0
  87. package/lib/link-tooltip/tooltips.d.ts +3 -0
  88. package/lib/link-tooltip/tooltips.d.ts.map +1 -0
  89. package/lib/link-tooltip/utils.d.ts +14 -0
  90. package/lib/link-tooltip/utils.d.ts.map +1 -0
  91. package/lib/list-item-block/component.d.ts +19 -0
  92. package/lib/list-item-block/component.d.ts.map +1 -0
  93. package/lib/list-item-block/config.d.ts +13 -0
  94. package/lib/list-item-block/config.d.ts.map +1 -0
  95. package/lib/list-item-block/index.d.ts +6 -0
  96. package/lib/list-item-block/index.d.ts.map +1 -0
  97. package/lib/list-item-block/index.js +2149 -0
  98. package/lib/list-item-block/index.js.map +1 -0
  99. package/lib/list-item-block/view.d.ts +3 -0
  100. package/lib/list-item-block/view.d.ts.map +1 -0
  101. package/lib/table-block/config.d.ts +8 -0
  102. package/lib/table-block/config.d.ts.map +1 -0
  103. package/lib/table-block/dnd/calc-drag-over.d.ts +3 -0
  104. package/lib/table-block/dnd/calc-drag-over.d.ts.map +1 -0
  105. package/lib/table-block/dnd/create-drag-handler.d.ts +5 -0
  106. package/lib/table-block/dnd/create-drag-handler.d.ts.map +1 -0
  107. package/lib/table-block/dnd/drag-over-handler.d.ts +3 -0
  108. package/lib/table-block/dnd/drag-over-handler.d.ts.map +1 -0
  109. package/lib/table-block/dnd/prepare-dnd-context.d.ts +3 -0
  110. package/lib/table-block/dnd/prepare-dnd-context.d.ts.map +1 -0
  111. package/lib/table-block/dnd/preview.d.ts +3 -0
  112. package/lib/table-block/dnd/preview.d.ts.map +1 -0
  113. package/lib/table-block/index.d.ts +5 -0
  114. package/lib/table-block/index.d.ts.map +1 -0
  115. package/lib/table-block/index.js +3961 -0
  116. package/lib/table-block/index.js.map +1 -0
  117. package/lib/table-block/view/component.d.ts +16 -0
  118. package/lib/table-block/view/component.d.ts.map +1 -0
  119. package/lib/table-block/view/drag.d.ts +7 -0
  120. package/lib/table-block/view/drag.d.ts.map +1 -0
  121. package/lib/table-block/view/index.d.ts +2 -0
  122. package/lib/table-block/view/index.d.ts.map +1 -0
  123. package/lib/table-block/view/operation.d.ts +11 -0
  124. package/lib/table-block/view/operation.d.ts.map +1 -0
  125. package/lib/table-block/view/pointer.d.ts +7 -0
  126. package/lib/table-block/view/pointer.d.ts.map +1 -0
  127. package/lib/table-block/view/types.d.ts +32 -0
  128. package/lib/table-block/view/types.d.ts.map +1 -0
  129. package/lib/table-block/view/utils.d.ts +21 -0
  130. package/lib/table-block/view/utils.d.ts.map +1 -0
  131. package/lib/table-block/view/view.d.ts +22 -0
  132. package/lib/table-block/view/view.d.ts.map +1 -0
  133. package/lib/tsconfig.tsbuildinfo +1 -0
  134. package/package.json +110 -0
  135. package/src/__internal__/components/icon.tsx +38 -0
  136. package/src/__internal__/components/image-input.tsx +182 -0
  137. package/src/__internal__/keep-alive.ts +3 -0
  138. package/src/__internal__/meta.ts +15 -0
  139. package/src/__tests__/setup.ts +6 -0
  140. package/src/code-block/config.ts +54 -0
  141. package/src/code-block/index.ts +12 -0
  142. package/src/code-block/view/components/code-block.tsx +170 -0
  143. package/src/code-block/view/components/copy-button.tsx +96 -0
  144. package/src/code-block/view/components/language-picker.tsx +239 -0
  145. package/src/code-block/view/components/preview-panel.tsx +79 -0
  146. package/src/code-block/view/index.ts +24 -0
  147. package/src/code-block/view/loader.ts +40 -0
  148. package/src/code-block/view/node-view.ts +310 -0
  149. package/src/image-block/config.ts +37 -0
  150. package/src/image-block/index.ts +18 -0
  151. package/src/image-block/remark-plugin.ts +51 -0
  152. package/src/image-block/schema.ts +71 -0
  153. package/src/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.tsx +42 -0
  154. package/src/image-block/view/components/image-block.tsx +80 -0
  155. package/src/image-block/view/components/image-viewer.tsx +186 -0
  156. package/src/image-block/view/index.ts +111 -0
  157. package/src/image-inline/components/image-inline.tsx +85 -0
  158. package/src/image-inline/config.ts +30 -0
  159. package/src/image-inline/index.ts +12 -0
  160. package/src/image-inline/view.ts +109 -0
  161. package/src/index.ts +1 -0
  162. package/src/link-tooltip/command.ts +19 -0
  163. package/src/link-tooltip/configure.ts +9 -0
  164. package/src/link-tooltip/edit/component.tsx +82 -0
  165. package/src/link-tooltip/edit/edit-configure.ts +29 -0
  166. package/src/link-tooltip/edit/edit-view.ts +165 -0
  167. package/src/link-tooltip/index.ts +19 -0
  168. package/src/link-tooltip/preview/component.tsx +87 -0
  169. package/src/link-tooltip/preview/preview-configure.ts +65 -0
  170. package/src/link-tooltip/preview/preview-view.ts +101 -0
  171. package/src/link-tooltip/slices.ts +69 -0
  172. package/src/link-tooltip/tooltips.ts +22 -0
  173. package/src/link-tooltip/utils.ts +56 -0
  174. package/src/list-item-block/component.tsx +133 -0
  175. package/src/list-item-block/config.ts +39 -0
  176. package/src/list-item-block/index.ts +13 -0
  177. package/src/list-item-block/view.ts +130 -0
  178. package/src/table-block/config.ts +53 -0
  179. package/src/table-block/dnd/calc-drag-over.ts +46 -0
  180. package/src/table-block/dnd/create-drag-handler.ts +99 -0
  181. package/src/table-block/dnd/drag-over-handler.ts +113 -0
  182. package/src/table-block/dnd/prepare-dnd-context.ts +46 -0
  183. package/src/table-block/dnd/preview.ts +58 -0
  184. package/src/table-block/index.ts +9 -0
  185. package/src/table-block/view/component.tsx +219 -0
  186. package/src/table-block/view/drag.ts +121 -0
  187. package/src/table-block/view/index.ts +1 -0
  188. package/src/table-block/view/operation.ts +148 -0
  189. package/src/table-block/view/pointer.ts +165 -0
  190. package/src/table-block/view/types.ts +35 -0
  191. package/src/table-block/view/utils.ts +192 -0
  192. package/src/table-block/view/view.ts +165 -0
@@ -0,0 +1,2149 @@
1
+ import { $ctx, $view } from '@jvs-milkdown/utils';
2
+ import { listItemSchema } from '@jvs-milkdown/preset-commonmark';
3
+ import { h, defineComponent, computed, ref, watchEffect, createApp } from 'vue';
4
+ import clsx from 'clsx';
5
+ import DOMPurify from 'dompurify';
6
+
7
+ var __defProp = Object.defineProperty;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ function withMeta(plugin, meta) {
24
+ Object.assign(plugin, {
25
+ meta: __spreadValues({
26
+ package: "@jvs-milkdown/components"
27
+ }, meta)
28
+ });
29
+ return plugin;
30
+ }
31
+
32
+ const defaultListItemBlockConfig = {
33
+ renderLabel: ({ label, listType, checked }) => {
34
+ const content = checked == null ? listType === "bullet" ? "\u29BF" : label : checked ? "\u2611" : "\u25A1";
35
+ return content;
36
+ }
37
+ };
38
+ const listItemBlockConfig = $ctx(
39
+ defaultListItemBlockConfig,
40
+ "listItemBlockConfigCtx"
41
+ );
42
+ withMeta(listItemBlockConfig, {
43
+ displayName: "Config<list-item-block>",
44
+ group: "ListItemBlock"
45
+ });
46
+
47
+ function findDiffStart(a, b, pos) {
48
+ for (let i = 0;; i++) {
49
+ if (i == a.childCount || i == b.childCount)
50
+ return a.childCount == b.childCount ? null : pos;
51
+ let childA = a.child(i), childB = b.child(i);
52
+ if (childA == childB) {
53
+ pos += childA.nodeSize;
54
+ continue;
55
+ }
56
+ if (!childA.sameMarkup(childB))
57
+ return pos;
58
+ if (childA.isText && childA.text != childB.text) {
59
+ for (let j = 0; childA.text[j] == childB.text[j]; j++)
60
+ pos++;
61
+ return pos;
62
+ }
63
+ if (childA.content.size || childB.content.size) {
64
+ let inner = findDiffStart(childA.content, childB.content, pos + 1);
65
+ if (inner != null)
66
+ return inner;
67
+ }
68
+ pos += childA.nodeSize;
69
+ }
70
+ }
71
+ function findDiffEnd(a, b, posA, posB) {
72
+ for (let iA = a.childCount, iB = b.childCount;;) {
73
+ if (iA == 0 || iB == 0)
74
+ return iA == iB ? null : { a: posA, b: posB };
75
+ let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
76
+ if (childA == childB) {
77
+ posA -= size;
78
+ posB -= size;
79
+ continue;
80
+ }
81
+ if (!childA.sameMarkup(childB))
82
+ return { a: posA, b: posB };
83
+ if (childA.isText && childA.text != childB.text) {
84
+ let same = 0, minSize = Math.min(childA.text.length, childB.text.length);
85
+ while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
86
+ same++;
87
+ posA--;
88
+ posB--;
89
+ }
90
+ return { a: posA, b: posB };
91
+ }
92
+ if (childA.content.size || childB.content.size) {
93
+ let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
94
+ if (inner)
95
+ return inner;
96
+ }
97
+ posA -= size;
98
+ posB -= size;
99
+ }
100
+ }
101
+
102
+ /**
103
+ A fragment represents a node's collection of child nodes.
104
+
105
+ Like nodes, fragments are persistent data structures, and you
106
+ should not mutate them or their content. Rather, you create new
107
+ instances whenever needed. The API tries to make this easy.
108
+ */
109
+ class Fragment {
110
+ /**
111
+ @internal
112
+ */
113
+ constructor(
114
+ /**
115
+ The child nodes in this fragment.
116
+ */
117
+ content, size) {
118
+ this.content = content;
119
+ this.size = size || 0;
120
+ if (size == null)
121
+ for (let i = 0; i < content.length; i++)
122
+ this.size += content[i].nodeSize;
123
+ }
124
+ /**
125
+ Invoke a callback for all descendant nodes between the given two
126
+ positions (relative to start of this fragment). Doesn't descend
127
+ into a node when the callback returns `false`.
128
+ */
129
+ nodesBetween(from, to, f, nodeStart = 0, parent) {
130
+ for (let i = 0, pos = 0; pos < to; i++) {
131
+ let child = this.content[i], end = pos + child.nodeSize;
132
+ if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
133
+ let start = pos + 1;
134
+ child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
135
+ }
136
+ pos = end;
137
+ }
138
+ }
139
+ /**
140
+ Call the given callback for every descendant node. `pos` will be
141
+ relative to the start of the fragment. The callback may return
142
+ `false` to prevent traversal of a given node's children.
143
+ */
144
+ descendants(f) {
145
+ this.nodesBetween(0, this.size, f);
146
+ }
147
+ /**
148
+ Extract the text between `from` and `to`. See the same method on
149
+ [`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
150
+ */
151
+ textBetween(from, to, blockSeparator, leafText) {
152
+ let text = "", first = true;
153
+ this.nodesBetween(from, to, (node, pos) => {
154
+ let nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos)
155
+ : !node.isLeaf ? ""
156
+ : leafText ? (typeof leafText === "function" ? leafText(node) : leafText)
157
+ : node.type.spec.leafText ? node.type.spec.leafText(node)
158
+ : "";
159
+ if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) {
160
+ if (first)
161
+ first = false;
162
+ else
163
+ text += blockSeparator;
164
+ }
165
+ text += nodeText;
166
+ }, 0);
167
+ return text;
168
+ }
169
+ /**
170
+ Create a new fragment containing the combined content of this
171
+ fragment and the other.
172
+ */
173
+ append(other) {
174
+ if (!other.size)
175
+ return this;
176
+ if (!this.size)
177
+ return other;
178
+ let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
179
+ if (last.isText && last.sameMarkup(first)) {
180
+ content[content.length - 1] = last.withText(last.text + first.text);
181
+ i = 1;
182
+ }
183
+ for (; i < other.content.length; i++)
184
+ content.push(other.content[i]);
185
+ return new Fragment(content, this.size + other.size);
186
+ }
187
+ /**
188
+ Cut out the sub-fragment between the two given positions.
189
+ */
190
+ cut(from, to = this.size) {
191
+ if (from == 0 && to == this.size)
192
+ return this;
193
+ let result = [], size = 0;
194
+ if (to > from)
195
+ for (let i = 0, pos = 0; pos < to; i++) {
196
+ let child = this.content[i], end = pos + child.nodeSize;
197
+ if (end > from) {
198
+ if (pos < from || end > to) {
199
+ if (child.isText)
200
+ child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
201
+ else
202
+ child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
203
+ }
204
+ result.push(child);
205
+ size += child.nodeSize;
206
+ }
207
+ pos = end;
208
+ }
209
+ return new Fragment(result, size);
210
+ }
211
+ /**
212
+ @internal
213
+ */
214
+ cutByIndex(from, to) {
215
+ if (from == to)
216
+ return Fragment.empty;
217
+ if (from == 0 && to == this.content.length)
218
+ return this;
219
+ return new Fragment(this.content.slice(from, to));
220
+ }
221
+ /**
222
+ Create a new fragment in which the node at the given index is
223
+ replaced by the given node.
224
+ */
225
+ replaceChild(index, node) {
226
+ let current = this.content[index];
227
+ if (current == node)
228
+ return this;
229
+ let copy = this.content.slice();
230
+ let size = this.size + node.nodeSize - current.nodeSize;
231
+ copy[index] = node;
232
+ return new Fragment(copy, size);
233
+ }
234
+ /**
235
+ Create a new fragment by prepending the given node to this
236
+ fragment.
237
+ */
238
+ addToStart(node) {
239
+ return new Fragment([node].concat(this.content), this.size + node.nodeSize);
240
+ }
241
+ /**
242
+ Create a new fragment by appending the given node to this
243
+ fragment.
244
+ */
245
+ addToEnd(node) {
246
+ return new Fragment(this.content.concat(node), this.size + node.nodeSize);
247
+ }
248
+ /**
249
+ Compare this fragment to another one.
250
+ */
251
+ eq(other) {
252
+ if (this.content.length != other.content.length)
253
+ return false;
254
+ for (let i = 0; i < this.content.length; i++)
255
+ if (!this.content[i].eq(other.content[i]))
256
+ return false;
257
+ return true;
258
+ }
259
+ /**
260
+ The first child of the fragment, or `null` if it is empty.
261
+ */
262
+ get firstChild() { return this.content.length ? this.content[0] : null; }
263
+ /**
264
+ The last child of the fragment, or `null` if it is empty.
265
+ */
266
+ get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; }
267
+ /**
268
+ The number of child nodes in this fragment.
269
+ */
270
+ get childCount() { return this.content.length; }
271
+ /**
272
+ Get the child node at the given index. Raise an error when the
273
+ index is out of range.
274
+ */
275
+ child(index) {
276
+ let found = this.content[index];
277
+ if (!found)
278
+ throw new RangeError("Index " + index + " out of range for " + this);
279
+ return found;
280
+ }
281
+ /**
282
+ Get the child node at the given index, if it exists.
283
+ */
284
+ maybeChild(index) {
285
+ return this.content[index] || null;
286
+ }
287
+ /**
288
+ Call `f` for every child node, passing the node, its offset
289
+ into this parent node, and its index.
290
+ */
291
+ forEach(f) {
292
+ for (let i = 0, p = 0; i < this.content.length; i++) {
293
+ let child = this.content[i];
294
+ f(child, p, i);
295
+ p += child.nodeSize;
296
+ }
297
+ }
298
+ /**
299
+ Find the first position at which this fragment and another
300
+ fragment differ, or `null` if they are the same.
301
+ */
302
+ findDiffStart(other, pos = 0) {
303
+ return findDiffStart(this, other, pos);
304
+ }
305
+ /**
306
+ Find the first position, searching from the end, at which this
307
+ fragment and the given fragment differ, or `null` if they are
308
+ the same. Since this position will not be the same in both
309
+ nodes, an object with two separate positions is returned.
310
+ */
311
+ findDiffEnd(other, pos = this.size, otherPos = other.size) {
312
+ return findDiffEnd(this, other, pos, otherPos);
313
+ }
314
+ /**
315
+ Find the index and inner offset corresponding to a given relative
316
+ position in this fragment. The result object will be reused
317
+ (overwritten) the next time the function is called. @internal
318
+ */
319
+ findIndex(pos) {
320
+ if (pos == 0)
321
+ return retIndex(0, pos);
322
+ if (pos == this.size)
323
+ return retIndex(this.content.length, pos);
324
+ if (pos > this.size || pos < 0)
325
+ throw new RangeError(`Position ${pos} outside of fragment (${this})`);
326
+ for (let i = 0, curPos = 0;; i++) {
327
+ let cur = this.child(i), end = curPos + cur.nodeSize;
328
+ if (end >= pos) {
329
+ if (end == pos)
330
+ return retIndex(i + 1, end);
331
+ return retIndex(i, curPos);
332
+ }
333
+ curPos = end;
334
+ }
335
+ }
336
+ /**
337
+ Return a debugging string that describes this fragment.
338
+ */
339
+ toString() { return "<" + this.toStringInner() + ">"; }
340
+ /**
341
+ @internal
342
+ */
343
+ toStringInner() { return this.content.join(", "); }
344
+ /**
345
+ Create a JSON-serializeable representation of this fragment.
346
+ */
347
+ toJSON() {
348
+ return this.content.length ? this.content.map(n => n.toJSON()) : null;
349
+ }
350
+ /**
351
+ Deserialize a fragment from its JSON representation.
352
+ */
353
+ static fromJSON(schema, value) {
354
+ if (!value)
355
+ return Fragment.empty;
356
+ if (!Array.isArray(value))
357
+ throw new RangeError("Invalid input for Fragment.fromJSON");
358
+ return new Fragment(value.map(schema.nodeFromJSON));
359
+ }
360
+ /**
361
+ Build a fragment from an array of nodes. Ensures that adjacent
362
+ text nodes with the same marks are joined together.
363
+ */
364
+ static fromArray(array) {
365
+ if (!array.length)
366
+ return Fragment.empty;
367
+ let joined, size = 0;
368
+ for (let i = 0; i < array.length; i++) {
369
+ let node = array[i];
370
+ size += node.nodeSize;
371
+ if (i && node.isText && array[i - 1].sameMarkup(node)) {
372
+ if (!joined)
373
+ joined = array.slice(0, i);
374
+ joined[joined.length - 1] = node
375
+ .withText(joined[joined.length - 1].text + node.text);
376
+ }
377
+ else if (joined) {
378
+ joined.push(node);
379
+ }
380
+ }
381
+ return new Fragment(joined || array, size);
382
+ }
383
+ /**
384
+ Create a fragment from something that can be interpreted as a
385
+ set of nodes. For `null`, it returns the empty fragment. For a
386
+ fragment, the fragment itself. For a node or array of nodes, a
387
+ fragment containing those nodes.
388
+ */
389
+ static from(nodes) {
390
+ if (!nodes)
391
+ return Fragment.empty;
392
+ if (nodes instanceof Fragment)
393
+ return nodes;
394
+ if (Array.isArray(nodes))
395
+ return this.fromArray(nodes);
396
+ if (nodes.attrs)
397
+ return new Fragment([nodes], nodes.nodeSize);
398
+ throw new RangeError("Can not convert " + nodes + " to a Fragment" +
399
+ (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
400
+ }
401
+ }
402
+ /**
403
+ An empty fragment. Intended to be reused whenever a node doesn't
404
+ contain anything (rather than allocating a new empty fragment for
405
+ each leaf node).
406
+ */
407
+ Fragment.empty = new Fragment([], 0);
408
+ const found = { index: 0, offset: 0 };
409
+ function retIndex(index, offset) {
410
+ found.index = index;
411
+ found.offset = offset;
412
+ return found;
413
+ }
414
+
415
+ /**
416
+ Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
417
+ given an invalid replacement.
418
+ */
419
+ class ReplaceError extends Error {
420
+ }
421
+ /*
422
+ ReplaceError = function(this: any, message: string) {
423
+ let err = Error.call(this, message)
424
+ ;(err as any).__proto__ = ReplaceError.prototype
425
+ return err
426
+ } as any
427
+
428
+ ReplaceError.prototype = Object.create(Error.prototype)
429
+ ReplaceError.prototype.constructor = ReplaceError
430
+ ReplaceError.prototype.name = "ReplaceError"
431
+ */
432
+ /**
433
+ A slice represents a piece cut out of a larger document. It
434
+ stores not only a fragment, but also the depth up to which nodes on
435
+ both side are ‘open’ (cut through).
436
+ */
437
+ class Slice {
438
+ /**
439
+ Create a slice. When specifying a non-zero open depth, you must
440
+ make sure that there are nodes of at least that depth at the
441
+ appropriate side of the fragment—i.e. if the fragment is an
442
+ empty paragraph node, `openStart` and `openEnd` can't be greater
443
+ than 1.
444
+
445
+ It is not necessary for the content of open nodes to conform to
446
+ the schema's content constraints, though it should be a valid
447
+ start/end/middle for such a node, depending on which sides are
448
+ open.
449
+ */
450
+ constructor(
451
+ /**
452
+ The slice's content.
453
+ */
454
+ content,
455
+ /**
456
+ The open depth at the start of the fragment.
457
+ */
458
+ openStart,
459
+ /**
460
+ The open depth at the end.
461
+ */
462
+ openEnd) {
463
+ this.content = content;
464
+ this.openStart = openStart;
465
+ this.openEnd = openEnd;
466
+ }
467
+ /**
468
+ The size this slice would add when inserted into a document.
469
+ */
470
+ get size() {
471
+ return this.content.size - this.openStart - this.openEnd;
472
+ }
473
+ /**
474
+ @internal
475
+ */
476
+ insertAt(pos, fragment) {
477
+ let content = insertInto(this.content, pos + this.openStart, fragment);
478
+ return content && new Slice(content, this.openStart, this.openEnd);
479
+ }
480
+ /**
481
+ @internal
482
+ */
483
+ removeBetween(from, to) {
484
+ return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
485
+ }
486
+ /**
487
+ Tests whether this slice is equal to another slice.
488
+ */
489
+ eq(other) {
490
+ return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
491
+ }
492
+ /**
493
+ @internal
494
+ */
495
+ toString() {
496
+ return this.content + "(" + this.openStart + "," + this.openEnd + ")";
497
+ }
498
+ /**
499
+ Convert a slice to a JSON-serializable representation.
500
+ */
501
+ toJSON() {
502
+ if (!this.content.size)
503
+ return null;
504
+ let json = { content: this.content.toJSON() };
505
+ if (this.openStart > 0)
506
+ json.openStart = this.openStart;
507
+ if (this.openEnd > 0)
508
+ json.openEnd = this.openEnd;
509
+ return json;
510
+ }
511
+ /**
512
+ Deserialize a slice from its JSON representation.
513
+ */
514
+ static fromJSON(schema, json) {
515
+ if (!json)
516
+ return Slice.empty;
517
+ let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
518
+ if (typeof openStart != "number" || typeof openEnd != "number")
519
+ throw new RangeError("Invalid input for Slice.fromJSON");
520
+ return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
521
+ }
522
+ /**
523
+ Create a slice from a fragment by taking the maximum possible
524
+ open value on both side of the fragment.
525
+ */
526
+ static maxOpen(fragment, openIsolating = true) {
527
+ let openStart = 0, openEnd = 0;
528
+ for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
529
+ openStart++;
530
+ for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
531
+ openEnd++;
532
+ return new Slice(fragment, openStart, openEnd);
533
+ }
534
+ }
535
+ /**
536
+ The empty slice.
537
+ */
538
+ Slice.empty = new Slice(Fragment.empty, 0, 0);
539
+ function removeRange(content, from, to) {
540
+ let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
541
+ let { index: indexTo, offset: offsetTo } = content.findIndex(to);
542
+ if (offset == from || child.isText) {
543
+ if (offsetTo != to && !content.child(indexTo).isText)
544
+ throw new RangeError("Removing non-flat range");
545
+ return content.cut(0, from).append(content.cut(to));
546
+ }
547
+ if (index != indexTo)
548
+ throw new RangeError("Removing non-flat range");
549
+ return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
550
+ }
551
+ function insertInto(content, dist, insert, parent) {
552
+ let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
553
+ if (offset == dist || child.isText) {
554
+ if (parent && !parent.canReplace(index, index, insert))
555
+ return null;
556
+ return content.cut(0, dist).append(insert).append(content.cut(dist));
557
+ }
558
+ let inner = insertInto(child.content, dist - offset - 1, insert, child);
559
+ return inner && content.replaceChild(index, child.copy(inner));
560
+ }
561
+
562
+ // Recovery values encode a range index and an offset. They are
563
+ // represented as numbers, because tons of them will be created when
564
+ // mapping, for example, a large number of decorations. The number's
565
+ // lower 16 bits provide the index, the remaining bits the offset.
566
+ //
567
+ // Note: We intentionally don't use bit shift operators to en- and
568
+ // decode these, since those clip to 32 bits, which we might in rare
569
+ // cases want to overflow. A 64-bit float can represent 48-bit
570
+ // integers precisely.
571
+ const lower16 = 0xffff;
572
+ const factor16 = Math.pow(2, 16);
573
+ function makeRecover(index, offset) { return index + offset * factor16; }
574
+ function recoverIndex(value) { return value & lower16; }
575
+ function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
576
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
577
+ /**
578
+ An object representing a mapped position with extra
579
+ information.
580
+ */
581
+ class MapResult {
582
+ /**
583
+ @internal
584
+ */
585
+ constructor(
586
+ /**
587
+ The mapped version of the position.
588
+ */
589
+ pos,
590
+ /**
591
+ @internal
592
+ */
593
+ delInfo,
594
+ /**
595
+ @internal
596
+ */
597
+ recover) {
598
+ this.pos = pos;
599
+ this.delInfo = delInfo;
600
+ this.recover = recover;
601
+ }
602
+ /**
603
+ Tells you whether the position was deleted, that is, whether the
604
+ step removed the token on the side queried (via the `assoc`)
605
+ argument from the document.
606
+ */
607
+ get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
608
+ /**
609
+ Tells you whether the token before the mapped position was deleted.
610
+ */
611
+ get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
612
+ /**
613
+ True when the token after the mapped position was deleted.
614
+ */
615
+ get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
616
+ /**
617
+ Tells whether any of the steps mapped through deletes across the
618
+ position (including both the token before and after the
619
+ position).
620
+ */
621
+ get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
622
+ }
623
+ /**
624
+ A map describing the deletions and insertions made by a step, which
625
+ can be used to find the correspondence between positions in the
626
+ pre-step version of a document and the same position in the
627
+ post-step version.
628
+ */
629
+ class StepMap {
630
+ /**
631
+ Create a position map. The modifications to the document are
632
+ represented as an array of numbers, in which each group of three
633
+ represents a modified chunk as `[start, oldSize, newSize]`.
634
+ */
635
+ constructor(
636
+ /**
637
+ @internal
638
+ */
639
+ ranges,
640
+ /**
641
+ @internal
642
+ */
643
+ inverted = false) {
644
+ this.ranges = ranges;
645
+ this.inverted = inverted;
646
+ if (!ranges.length && StepMap.empty)
647
+ return StepMap.empty;
648
+ }
649
+ /**
650
+ @internal
651
+ */
652
+ recover(value) {
653
+ let diff = 0, index = recoverIndex(value);
654
+ if (!this.inverted)
655
+ for (let i = 0; i < index; i++)
656
+ diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
657
+ return this.ranges[index * 3] + diff + recoverOffset(value);
658
+ }
659
+ mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
660
+ map(pos, assoc = 1) { return this._map(pos, assoc, true); }
661
+ /**
662
+ @internal
663
+ */
664
+ _map(pos, assoc, simple) {
665
+ let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
666
+ for (let i = 0; i < this.ranges.length; i += 3) {
667
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
668
+ if (start > pos)
669
+ break;
670
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
671
+ if (pos <= end) {
672
+ let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
673
+ let result = start + diff + (side < 0 ? 0 : newSize);
674
+ if (simple)
675
+ return result;
676
+ let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
677
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
678
+ if (assoc < 0 ? pos != start : pos != end)
679
+ del |= DEL_SIDE;
680
+ return new MapResult(result, del, recover);
681
+ }
682
+ diff += newSize - oldSize;
683
+ }
684
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
685
+ }
686
+ /**
687
+ @internal
688
+ */
689
+ touches(pos, recover) {
690
+ let diff = 0, index = recoverIndex(recover);
691
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
692
+ for (let i = 0; i < this.ranges.length; i += 3) {
693
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
694
+ if (start > pos)
695
+ break;
696
+ let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
697
+ if (pos <= end && i == index * 3)
698
+ return true;
699
+ diff += this.ranges[i + newIndex] - oldSize;
700
+ }
701
+ return false;
702
+ }
703
+ /**
704
+ Calls the given function on each of the changed ranges included in
705
+ this map.
706
+ */
707
+ forEach(f) {
708
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
709
+ for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
710
+ let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
711
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
712
+ f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
713
+ diff += newSize - oldSize;
714
+ }
715
+ }
716
+ /**
717
+ Create an inverted version of this map. The result can be used to
718
+ map positions in the post-step document to the pre-step document.
719
+ */
720
+ invert() {
721
+ return new StepMap(this.ranges, !this.inverted);
722
+ }
723
+ /**
724
+ @internal
725
+ */
726
+ toString() {
727
+ return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
728
+ }
729
+ /**
730
+ Create a map that moves all positions by offset `n` (which may be
731
+ negative). This can be useful when applying steps meant for a
732
+ sub-document to a larger document, or vice-versa.
733
+ */
734
+ static offset(n) {
735
+ return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
736
+ }
737
+ }
738
+ /**
739
+ A StepMap that contains no changed ranges.
740
+ */
741
+ StepMap.empty = new StepMap([]);
742
+
743
+ const stepsByID = Object.create(null);
744
+ /**
745
+ A step object represents an atomic change. It generally applies
746
+ only to the document it was created for, since the positions
747
+ stored in it will only make sense for that document.
748
+
749
+ New steps are defined by creating classes that extend `Step`,
750
+ overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
751
+ methods, and registering your class with a unique
752
+ JSON-serialization identifier using
753
+ [`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
754
+ */
755
+ class Step {
756
+ /**
757
+ Get the step map that represents the changes made by this step,
758
+ and which can be used to transform between positions in the old
759
+ and the new document.
760
+ */
761
+ getMap() { return StepMap.empty; }
762
+ /**
763
+ Try to merge this step with another one, to be applied directly
764
+ after it. Returns the merged step when possible, null if the
765
+ steps can't be merged.
766
+ */
767
+ merge(other) { return null; }
768
+ /**
769
+ Deserialize a step from its JSON representation. Will call
770
+ through to the step class' own implementation of this method.
771
+ */
772
+ static fromJSON(schema, json) {
773
+ if (!json || !json.stepType)
774
+ throw new RangeError("Invalid input for Step.fromJSON");
775
+ let type = stepsByID[json.stepType];
776
+ if (!type)
777
+ throw new RangeError(`No step type ${json.stepType} defined`);
778
+ return type.fromJSON(schema, json);
779
+ }
780
+ /**
781
+ To be able to serialize steps to JSON, each step needs a string
782
+ ID to attach to its JSON representation. Use this method to
783
+ register an ID for your step classes. Try to pick something
784
+ that's unlikely to clash with steps from other modules.
785
+ */
786
+ static jsonID(id, stepClass) {
787
+ if (id in stepsByID)
788
+ throw new RangeError("Duplicate use of step JSON ID " + id);
789
+ stepsByID[id] = stepClass;
790
+ stepClass.prototype.jsonID = id;
791
+ return stepClass;
792
+ }
793
+ }
794
+ /**
795
+ The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
796
+ new document or a failure value.
797
+ */
798
+ class StepResult {
799
+ /**
800
+ @internal
801
+ */
802
+ constructor(
803
+ /**
804
+ The transformed document, if successful.
805
+ */
806
+ doc,
807
+ /**
808
+ The failure message, if unsuccessful.
809
+ */
810
+ failed) {
811
+ this.doc = doc;
812
+ this.failed = failed;
813
+ }
814
+ /**
815
+ Create a successful step result.
816
+ */
817
+ static ok(doc) { return new StepResult(doc, null); }
818
+ /**
819
+ Create a failed step result.
820
+ */
821
+ static fail(message) { return new StepResult(null, message); }
822
+ /**
823
+ Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
824
+ arguments. Create a successful result if it succeeds, and a
825
+ failed one if it throws a `ReplaceError`.
826
+ */
827
+ static fromReplace(doc, from, to, slice) {
828
+ try {
829
+ return StepResult.ok(doc.replace(from, to, slice));
830
+ }
831
+ catch (e) {
832
+ if (e instanceof ReplaceError)
833
+ return StepResult.fail(e.message);
834
+ throw e;
835
+ }
836
+ }
837
+ }
838
+
839
+ function mapFragment(fragment, f, parent) {
840
+ let mapped = [];
841
+ for (let i = 0; i < fragment.childCount; i++) {
842
+ let child = fragment.child(i);
843
+ if (child.content.size)
844
+ child = child.copy(mapFragment(child.content, f, child));
845
+ if (child.isInline)
846
+ child = f(child, parent, i);
847
+ mapped.push(child);
848
+ }
849
+ return Fragment.fromArray(mapped);
850
+ }
851
+ /**
852
+ Add a mark to all inline content between two positions.
853
+ */
854
+ class AddMarkStep extends Step {
855
+ /**
856
+ Create a mark step.
857
+ */
858
+ constructor(
859
+ /**
860
+ The start of the marked range.
861
+ */
862
+ from,
863
+ /**
864
+ The end of the marked range.
865
+ */
866
+ to,
867
+ /**
868
+ The mark to add.
869
+ */
870
+ mark) {
871
+ super();
872
+ this.from = from;
873
+ this.to = to;
874
+ this.mark = mark;
875
+ }
876
+ apply(doc) {
877
+ let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
878
+ let parent = $from.node($from.sharedDepth(this.to));
879
+ let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
880
+ if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type))
881
+ return node;
882
+ return node.mark(this.mark.addToSet(node.marks));
883
+ }, parent), oldSlice.openStart, oldSlice.openEnd);
884
+ return StepResult.fromReplace(doc, this.from, this.to, slice);
885
+ }
886
+ invert() {
887
+ return new RemoveMarkStep(this.from, this.to, this.mark);
888
+ }
889
+ map(mapping) {
890
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
891
+ if (from.deleted && to.deleted || from.pos >= to.pos)
892
+ return null;
893
+ return new AddMarkStep(from.pos, to.pos, this.mark);
894
+ }
895
+ merge(other) {
896
+ if (other instanceof AddMarkStep &&
897
+ other.mark.eq(this.mark) &&
898
+ this.from <= other.to && this.to >= other.from)
899
+ return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
900
+ return null;
901
+ }
902
+ toJSON() {
903
+ return { stepType: "addMark", mark: this.mark.toJSON(),
904
+ from: this.from, to: this.to };
905
+ }
906
+ /**
907
+ @internal
908
+ */
909
+ static fromJSON(schema, json) {
910
+ if (typeof json.from != "number" || typeof json.to != "number")
911
+ throw new RangeError("Invalid input for AddMarkStep.fromJSON");
912
+ return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
913
+ }
914
+ }
915
+ Step.jsonID("addMark", AddMarkStep);
916
+ /**
917
+ Remove a mark from all inline content between two positions.
918
+ */
919
+ class RemoveMarkStep extends Step {
920
+ /**
921
+ Create a mark-removing step.
922
+ */
923
+ constructor(
924
+ /**
925
+ The start of the unmarked range.
926
+ */
927
+ from,
928
+ /**
929
+ The end of the unmarked range.
930
+ */
931
+ to,
932
+ /**
933
+ The mark to remove.
934
+ */
935
+ mark) {
936
+ super();
937
+ this.from = from;
938
+ this.to = to;
939
+ this.mark = mark;
940
+ }
941
+ apply(doc) {
942
+ let oldSlice = doc.slice(this.from, this.to);
943
+ let slice = new Slice(mapFragment(oldSlice.content, node => {
944
+ return node.mark(this.mark.removeFromSet(node.marks));
945
+ }, doc), oldSlice.openStart, oldSlice.openEnd);
946
+ return StepResult.fromReplace(doc, this.from, this.to, slice);
947
+ }
948
+ invert() {
949
+ return new AddMarkStep(this.from, this.to, this.mark);
950
+ }
951
+ map(mapping) {
952
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
953
+ if (from.deleted && to.deleted || from.pos >= to.pos)
954
+ return null;
955
+ return new RemoveMarkStep(from.pos, to.pos, this.mark);
956
+ }
957
+ merge(other) {
958
+ if (other instanceof RemoveMarkStep &&
959
+ other.mark.eq(this.mark) &&
960
+ this.from <= other.to && this.to >= other.from)
961
+ return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
962
+ return null;
963
+ }
964
+ toJSON() {
965
+ return { stepType: "removeMark", mark: this.mark.toJSON(),
966
+ from: this.from, to: this.to };
967
+ }
968
+ /**
969
+ @internal
970
+ */
971
+ static fromJSON(schema, json) {
972
+ if (typeof json.from != "number" || typeof json.to != "number")
973
+ throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
974
+ return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
975
+ }
976
+ }
977
+ Step.jsonID("removeMark", RemoveMarkStep);
978
+ /**
979
+ Add a mark to a specific node.
980
+ */
981
+ class AddNodeMarkStep extends Step {
982
+ /**
983
+ Create a node mark step.
984
+ */
985
+ constructor(
986
+ /**
987
+ The position of the target node.
988
+ */
989
+ pos,
990
+ /**
991
+ The mark to add.
992
+ */
993
+ mark) {
994
+ super();
995
+ this.pos = pos;
996
+ this.mark = mark;
997
+ }
998
+ apply(doc) {
999
+ let node = doc.nodeAt(this.pos);
1000
+ if (!node)
1001
+ return StepResult.fail("No node at mark step's position");
1002
+ let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
1003
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1004
+ }
1005
+ invert(doc) {
1006
+ let node = doc.nodeAt(this.pos);
1007
+ if (node) {
1008
+ let newSet = this.mark.addToSet(node.marks);
1009
+ if (newSet.length == node.marks.length) {
1010
+ for (let i = 0; i < node.marks.length; i++)
1011
+ if (!node.marks[i].isInSet(newSet))
1012
+ return new AddNodeMarkStep(this.pos, node.marks[i]);
1013
+ return new AddNodeMarkStep(this.pos, this.mark);
1014
+ }
1015
+ }
1016
+ return new RemoveNodeMarkStep(this.pos, this.mark);
1017
+ }
1018
+ map(mapping) {
1019
+ let pos = mapping.mapResult(this.pos, 1);
1020
+ return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
1021
+ }
1022
+ toJSON() {
1023
+ return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1024
+ }
1025
+ /**
1026
+ @internal
1027
+ */
1028
+ static fromJSON(schema, json) {
1029
+ if (typeof json.pos != "number")
1030
+ throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
1031
+ return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1032
+ }
1033
+ }
1034
+ Step.jsonID("addNodeMark", AddNodeMarkStep);
1035
+ /**
1036
+ Remove a mark from a specific node.
1037
+ */
1038
+ class RemoveNodeMarkStep extends Step {
1039
+ /**
1040
+ Create a mark-removing step.
1041
+ */
1042
+ constructor(
1043
+ /**
1044
+ The position of the target node.
1045
+ */
1046
+ pos,
1047
+ /**
1048
+ The mark to remove.
1049
+ */
1050
+ mark) {
1051
+ super();
1052
+ this.pos = pos;
1053
+ this.mark = mark;
1054
+ }
1055
+ apply(doc) {
1056
+ let node = doc.nodeAt(this.pos);
1057
+ if (!node)
1058
+ return StepResult.fail("No node at mark step's position");
1059
+ let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
1060
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1061
+ }
1062
+ invert(doc) {
1063
+ let node = doc.nodeAt(this.pos);
1064
+ if (!node || !this.mark.isInSet(node.marks))
1065
+ return this;
1066
+ return new AddNodeMarkStep(this.pos, this.mark);
1067
+ }
1068
+ map(mapping) {
1069
+ let pos = mapping.mapResult(this.pos, 1);
1070
+ return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
1071
+ }
1072
+ toJSON() {
1073
+ return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1074
+ }
1075
+ /**
1076
+ @internal
1077
+ */
1078
+ static fromJSON(schema, json) {
1079
+ if (typeof json.pos != "number")
1080
+ throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
1081
+ return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1082
+ }
1083
+ }
1084
+ Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
1085
+
1086
+ /**
1087
+ Replace a part of the document with a slice of new content.
1088
+ */
1089
+ class ReplaceStep extends Step {
1090
+ /**
1091
+ The given `slice` should fit the 'gap' between `from` and
1092
+ `to`—the depths must line up, and the surrounding nodes must be
1093
+ able to be joined with the open sides of the slice. When
1094
+ `structure` is true, the step will fail if the content between
1095
+ from and to is not just a sequence of closing and then opening
1096
+ tokens (this is to guard against rebased replace steps
1097
+ overwriting something they weren't supposed to).
1098
+ */
1099
+ constructor(
1100
+ /**
1101
+ The start position of the replaced range.
1102
+ */
1103
+ from,
1104
+ /**
1105
+ The end position of the replaced range.
1106
+ */
1107
+ to,
1108
+ /**
1109
+ The slice to insert.
1110
+ */
1111
+ slice,
1112
+ /**
1113
+ @internal
1114
+ */
1115
+ structure = false) {
1116
+ super();
1117
+ this.from = from;
1118
+ this.to = to;
1119
+ this.slice = slice;
1120
+ this.structure = structure;
1121
+ }
1122
+ apply(doc) {
1123
+ if (this.structure && contentBetween(doc, this.from, this.to))
1124
+ return StepResult.fail("Structure replace would overwrite content");
1125
+ return StepResult.fromReplace(doc, this.from, this.to, this.slice);
1126
+ }
1127
+ getMap() {
1128
+ return new StepMap([this.from, this.to - this.from, this.slice.size]);
1129
+ }
1130
+ invert(doc) {
1131
+ return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
1132
+ }
1133
+ map(mapping) {
1134
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1135
+ if (from.deletedAcross && to.deletedAcross)
1136
+ return null;
1137
+ return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
1138
+ }
1139
+ merge(other) {
1140
+ if (!(other instanceof ReplaceStep) || other.structure || this.structure)
1141
+ return null;
1142
+ if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
1143
+ let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1144
+ : new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
1145
+ return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
1146
+ }
1147
+ else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
1148
+ let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1149
+ : new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
1150
+ return new ReplaceStep(other.from, this.to, slice, this.structure);
1151
+ }
1152
+ else {
1153
+ return null;
1154
+ }
1155
+ }
1156
+ toJSON() {
1157
+ let json = { stepType: "replace", from: this.from, to: this.to };
1158
+ if (this.slice.size)
1159
+ json.slice = this.slice.toJSON();
1160
+ if (this.structure)
1161
+ json.structure = true;
1162
+ return json;
1163
+ }
1164
+ /**
1165
+ @internal
1166
+ */
1167
+ static fromJSON(schema, json) {
1168
+ if (typeof json.from != "number" || typeof json.to != "number")
1169
+ throw new RangeError("Invalid input for ReplaceStep.fromJSON");
1170
+ return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
1171
+ }
1172
+ }
1173
+ Step.jsonID("replace", ReplaceStep);
1174
+ /**
1175
+ Replace a part of the document with a slice of content, but
1176
+ preserve a range of the replaced content by moving it into the
1177
+ slice.
1178
+ */
1179
+ class ReplaceAroundStep extends Step {
1180
+ /**
1181
+ Create a replace-around step with the given range and gap.
1182
+ `insert` should be the point in the slice into which the content
1183
+ of the gap should be moved. `structure` has the same meaning as
1184
+ it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
1185
+ */
1186
+ constructor(
1187
+ /**
1188
+ The start position of the replaced range.
1189
+ */
1190
+ from,
1191
+ /**
1192
+ The end position of the replaced range.
1193
+ */
1194
+ to,
1195
+ /**
1196
+ The start of preserved range.
1197
+ */
1198
+ gapFrom,
1199
+ /**
1200
+ The end of preserved range.
1201
+ */
1202
+ gapTo,
1203
+ /**
1204
+ The slice to insert.
1205
+ */
1206
+ slice,
1207
+ /**
1208
+ The position in the slice where the preserved range should be
1209
+ inserted.
1210
+ */
1211
+ insert,
1212
+ /**
1213
+ @internal
1214
+ */
1215
+ structure = false) {
1216
+ super();
1217
+ this.from = from;
1218
+ this.to = to;
1219
+ this.gapFrom = gapFrom;
1220
+ this.gapTo = gapTo;
1221
+ this.slice = slice;
1222
+ this.insert = insert;
1223
+ this.structure = structure;
1224
+ }
1225
+ apply(doc) {
1226
+ if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
1227
+ contentBetween(doc, this.gapTo, this.to)))
1228
+ return StepResult.fail("Structure gap-replace would overwrite content");
1229
+ let gap = doc.slice(this.gapFrom, this.gapTo);
1230
+ if (gap.openStart || gap.openEnd)
1231
+ return StepResult.fail("Gap is not a flat range");
1232
+ let inserted = this.slice.insertAt(this.insert, gap.content);
1233
+ if (!inserted)
1234
+ return StepResult.fail("Content does not fit in gap");
1235
+ return StepResult.fromReplace(doc, this.from, this.to, inserted);
1236
+ }
1237
+ getMap() {
1238
+ return new StepMap([this.from, this.gapFrom - this.from, this.insert,
1239
+ this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
1240
+ }
1241
+ invert(doc) {
1242
+ let gap = this.gapTo - this.gapFrom;
1243
+ return new ReplaceAroundStep(this.from, this.from + this.slice.size + gap, this.from + this.insert, this.from + this.insert + gap, doc.slice(this.from, this.to).removeBetween(this.gapFrom - this.from, this.gapTo - this.from), this.gapFrom - this.from, this.structure);
1244
+ }
1245
+ map(mapping) {
1246
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1247
+ let gapFrom = this.from == this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1);
1248
+ let gapTo = this.to == this.gapTo ? to.pos : mapping.map(this.gapTo, 1);
1249
+ if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
1250
+ return null;
1251
+ return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
1252
+ }
1253
+ toJSON() {
1254
+ let json = { stepType: "replaceAround", from: this.from, to: this.to,
1255
+ gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert };
1256
+ if (this.slice.size)
1257
+ json.slice = this.slice.toJSON();
1258
+ if (this.structure)
1259
+ json.structure = true;
1260
+ return json;
1261
+ }
1262
+ /**
1263
+ @internal
1264
+ */
1265
+ static fromJSON(schema, json) {
1266
+ if (typeof json.from != "number" || typeof json.to != "number" ||
1267
+ typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
1268
+ throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
1269
+ return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
1270
+ }
1271
+ }
1272
+ Step.jsonID("replaceAround", ReplaceAroundStep);
1273
+ function contentBetween(doc, from, to) {
1274
+ let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
1275
+ while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
1276
+ depth--;
1277
+ dist--;
1278
+ }
1279
+ if (dist > 0) {
1280
+ let next = $from.node(depth).maybeChild($from.indexAfter(depth));
1281
+ while (dist > 0) {
1282
+ if (!next || next.isLeaf)
1283
+ return true;
1284
+ next = next.firstChild;
1285
+ dist--;
1286
+ }
1287
+ }
1288
+ return false;
1289
+ }
1290
+
1291
+ /**
1292
+ Update an attribute in a specific node.
1293
+ */
1294
+ class AttrStep extends Step {
1295
+ /**
1296
+ Construct an attribute step.
1297
+ */
1298
+ constructor(
1299
+ /**
1300
+ The position of the target node.
1301
+ */
1302
+ pos,
1303
+ /**
1304
+ The attribute to set.
1305
+ */
1306
+ attr,
1307
+ // The attribute's new value.
1308
+ value) {
1309
+ super();
1310
+ this.pos = pos;
1311
+ this.attr = attr;
1312
+ this.value = value;
1313
+ }
1314
+ apply(doc) {
1315
+ let node = doc.nodeAt(this.pos);
1316
+ if (!node)
1317
+ return StepResult.fail("No node at attribute step's position");
1318
+ let attrs = Object.create(null);
1319
+ for (let name in node.attrs)
1320
+ attrs[name] = node.attrs[name];
1321
+ attrs[this.attr] = this.value;
1322
+ let updated = node.type.create(attrs, null, node.marks);
1323
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1324
+ }
1325
+ getMap() {
1326
+ return StepMap.empty;
1327
+ }
1328
+ invert(doc) {
1329
+ return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1330
+ }
1331
+ map(mapping) {
1332
+ let pos = mapping.mapResult(this.pos, 1);
1333
+ return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1334
+ }
1335
+ toJSON() {
1336
+ return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
1337
+ }
1338
+ static fromJSON(schema, json) {
1339
+ if (typeof json.pos != "number" || typeof json.attr != "string")
1340
+ throw new RangeError("Invalid input for AttrStep.fromJSON");
1341
+ return new AttrStep(json.pos, json.attr, json.value);
1342
+ }
1343
+ }
1344
+ Step.jsonID("attr", AttrStep);
1345
+ /**
1346
+ Update an attribute in the doc node.
1347
+ */
1348
+ class DocAttrStep extends Step {
1349
+ /**
1350
+ Construct an attribute step.
1351
+ */
1352
+ constructor(
1353
+ /**
1354
+ The attribute to set.
1355
+ */
1356
+ attr,
1357
+ // The attribute's new value.
1358
+ value) {
1359
+ super();
1360
+ this.attr = attr;
1361
+ this.value = value;
1362
+ }
1363
+ apply(doc) {
1364
+ let attrs = Object.create(null);
1365
+ for (let name in doc.attrs)
1366
+ attrs[name] = doc.attrs[name];
1367
+ attrs[this.attr] = this.value;
1368
+ let updated = doc.type.create(attrs, doc.content, doc.marks);
1369
+ return StepResult.ok(updated);
1370
+ }
1371
+ getMap() {
1372
+ return StepMap.empty;
1373
+ }
1374
+ invert(doc) {
1375
+ return new DocAttrStep(this.attr, doc.attrs[this.attr]);
1376
+ }
1377
+ map(mapping) {
1378
+ return this;
1379
+ }
1380
+ toJSON() {
1381
+ return { stepType: "docAttr", attr: this.attr, value: this.value };
1382
+ }
1383
+ static fromJSON(schema, json) {
1384
+ if (typeof json.attr != "string")
1385
+ throw new RangeError("Invalid input for DocAttrStep.fromJSON");
1386
+ return new DocAttrStep(json.attr, json.value);
1387
+ }
1388
+ }
1389
+ Step.jsonID("docAttr", DocAttrStep);
1390
+
1391
+ /**
1392
+ @internal
1393
+ */
1394
+ let TransformError = class extends Error {
1395
+ };
1396
+ TransformError = function TransformError(message) {
1397
+ let err = Error.call(this, message);
1398
+ err.__proto__ = TransformError.prototype;
1399
+ return err;
1400
+ };
1401
+ TransformError.prototype = Object.create(Error.prototype);
1402
+ TransformError.prototype.constructor = TransformError;
1403
+ TransformError.prototype.name = "TransformError";
1404
+
1405
+ const classesById = Object.create(null);
1406
+ /**
1407
+ Superclass for editor selections. Every selection type should
1408
+ extend this. Should not be instantiated directly.
1409
+ */
1410
+ class Selection {
1411
+ /**
1412
+ Initialize a selection with the head and anchor and ranges. If no
1413
+ ranges are given, constructs a single range across `$anchor` and
1414
+ `$head`.
1415
+ */
1416
+ constructor(
1417
+ /**
1418
+ The resolved anchor of the selection (the side that stays in
1419
+ place when the selection is modified).
1420
+ */
1421
+ $anchor,
1422
+ /**
1423
+ The resolved head of the selection (the side that moves when
1424
+ the selection is modified).
1425
+ */
1426
+ $head, ranges) {
1427
+ this.$anchor = $anchor;
1428
+ this.$head = $head;
1429
+ this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
1430
+ }
1431
+ /**
1432
+ The selection's anchor, as an unresolved position.
1433
+ */
1434
+ get anchor() { return this.$anchor.pos; }
1435
+ /**
1436
+ The selection's head.
1437
+ */
1438
+ get head() { return this.$head.pos; }
1439
+ /**
1440
+ The lower bound of the selection's main range.
1441
+ */
1442
+ get from() { return this.$from.pos; }
1443
+ /**
1444
+ The upper bound of the selection's main range.
1445
+ */
1446
+ get to() { return this.$to.pos; }
1447
+ /**
1448
+ The resolved lower bound of the selection's main range.
1449
+ */
1450
+ get $from() {
1451
+ return this.ranges[0].$from;
1452
+ }
1453
+ /**
1454
+ The resolved upper bound of the selection's main range.
1455
+ */
1456
+ get $to() {
1457
+ return this.ranges[0].$to;
1458
+ }
1459
+ /**
1460
+ Indicates whether the selection contains any content.
1461
+ */
1462
+ get empty() {
1463
+ let ranges = this.ranges;
1464
+ for (let i = 0; i < ranges.length; i++)
1465
+ if (ranges[i].$from.pos != ranges[i].$to.pos)
1466
+ return false;
1467
+ return true;
1468
+ }
1469
+ /**
1470
+ Get the content of this selection as a slice.
1471
+ */
1472
+ content() {
1473
+ return this.$from.doc.slice(this.from, this.to, true);
1474
+ }
1475
+ /**
1476
+ Replace the selection with a slice or, if no slice is given,
1477
+ delete the selection. Will append to the given transaction.
1478
+ */
1479
+ replace(tr, content = Slice.empty) {
1480
+ // Put the new selection at the position after the inserted
1481
+ // content. When that ended in an inline node, search backwards,
1482
+ // to get the position after that node. If not, search forward.
1483
+ let lastNode = content.content.lastChild, lastParent = null;
1484
+ for (let i = 0; i < content.openEnd; i++) {
1485
+ lastParent = lastNode;
1486
+ lastNode = lastNode.lastChild;
1487
+ }
1488
+ let mapFrom = tr.steps.length, ranges = this.ranges;
1489
+ for (let i = 0; i < ranges.length; i++) {
1490
+ let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
1491
+ tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
1492
+ if (i == 0)
1493
+ selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
1494
+ }
1495
+ }
1496
+ /**
1497
+ Replace the selection with the given node, appending the changes
1498
+ to the given transaction.
1499
+ */
1500
+ replaceWith(tr, node) {
1501
+ let mapFrom = tr.steps.length, ranges = this.ranges;
1502
+ for (let i = 0; i < ranges.length; i++) {
1503
+ let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
1504
+ let from = mapping.map($from.pos), to = mapping.map($to.pos);
1505
+ if (i) {
1506
+ tr.deleteRange(from, to);
1507
+ }
1508
+ else {
1509
+ tr.replaceRangeWith(from, to, node);
1510
+ selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
1511
+ }
1512
+ }
1513
+ }
1514
+ /**
1515
+ Find a valid cursor or leaf node selection starting at the given
1516
+ position and searching back if `dir` is negative, and forward if
1517
+ positive. When `textOnly` is true, only consider cursor
1518
+ selections. Will return null when no valid selection position is
1519
+ found.
1520
+ */
1521
+ static findFrom($pos, dir, textOnly = false) {
1522
+ let inner = $pos.parent.inlineContent ? new TextSelection($pos)
1523
+ : findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
1524
+ if (inner)
1525
+ return inner;
1526
+ for (let depth = $pos.depth - 1; depth >= 0; depth--) {
1527
+ let found = dir < 0
1528
+ ? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly)
1529
+ : findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
1530
+ if (found)
1531
+ return found;
1532
+ }
1533
+ return null;
1534
+ }
1535
+ /**
1536
+ Find a valid cursor or leaf node selection near the given
1537
+ position. Searches forward first by default, but if `bias` is
1538
+ negative, it will search backwards first.
1539
+ */
1540
+ static near($pos, bias = 1) {
1541
+ return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
1542
+ }
1543
+ /**
1544
+ Find the cursor or leaf node selection closest to the start of
1545
+ the given document. Will return an
1546
+ [`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
1547
+ exists.
1548
+ */
1549
+ static atStart(doc) {
1550
+ return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
1551
+ }
1552
+ /**
1553
+ Find the cursor or leaf node selection closest to the end of the
1554
+ given document.
1555
+ */
1556
+ static atEnd(doc) {
1557
+ return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
1558
+ }
1559
+ /**
1560
+ Deserialize the JSON representation of a selection. Must be
1561
+ implemented for custom classes (as a static class method).
1562
+ */
1563
+ static fromJSON(doc, json) {
1564
+ if (!json || !json.type)
1565
+ throw new RangeError("Invalid input for Selection.fromJSON");
1566
+ let cls = classesById[json.type];
1567
+ if (!cls)
1568
+ throw new RangeError(`No selection type ${json.type} defined`);
1569
+ return cls.fromJSON(doc, json);
1570
+ }
1571
+ /**
1572
+ To be able to deserialize selections from JSON, custom selection
1573
+ classes must register themselves with an ID string, so that they
1574
+ can be disambiguated. Try to pick something that's unlikely to
1575
+ clash with classes from other modules.
1576
+ */
1577
+ static jsonID(id, selectionClass) {
1578
+ if (id in classesById)
1579
+ throw new RangeError("Duplicate use of selection JSON ID " + id);
1580
+ classesById[id] = selectionClass;
1581
+ selectionClass.prototype.jsonID = id;
1582
+ return selectionClass;
1583
+ }
1584
+ /**
1585
+ Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
1586
+ which is a value that can be mapped without having access to a
1587
+ current document, and later resolved to a real selection for a
1588
+ given document again. (This is used mostly by the history to
1589
+ track and restore old selections.) The default implementation of
1590
+ this method just converts the selection to a text selection and
1591
+ returns the bookmark for that.
1592
+ */
1593
+ getBookmark() {
1594
+ return TextSelection.between(this.$anchor, this.$head).getBookmark();
1595
+ }
1596
+ }
1597
+ Selection.prototype.visible = true;
1598
+ /**
1599
+ Represents a selected range in a document.
1600
+ */
1601
+ class SelectionRange {
1602
+ /**
1603
+ Create a range.
1604
+ */
1605
+ constructor(
1606
+ /**
1607
+ The lower bound of the range.
1608
+ */
1609
+ $from,
1610
+ /**
1611
+ The upper bound of the range.
1612
+ */
1613
+ $to) {
1614
+ this.$from = $from;
1615
+ this.$to = $to;
1616
+ }
1617
+ }
1618
+ let warnedAboutTextSelection = false;
1619
+ function checkTextSelection($pos) {
1620
+ if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
1621
+ warnedAboutTextSelection = true;
1622
+ console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
1623
+ }
1624
+ }
1625
+ /**
1626
+ A text selection represents a classical editor selection, with a
1627
+ head (the moving side) and anchor (immobile side), both of which
1628
+ point into textblock nodes. It can be empty (a regular cursor
1629
+ position).
1630
+ */
1631
+ class TextSelection extends Selection {
1632
+ /**
1633
+ Construct a text selection between the given points.
1634
+ */
1635
+ constructor($anchor, $head = $anchor) {
1636
+ checkTextSelection($anchor);
1637
+ checkTextSelection($head);
1638
+ super($anchor, $head);
1639
+ }
1640
+ /**
1641
+ Returns a resolved position if this is a cursor selection (an
1642
+ empty text selection), and null otherwise.
1643
+ */
1644
+ get $cursor() { return this.$anchor.pos == this.$head.pos ? this.$head : null; }
1645
+ map(doc, mapping) {
1646
+ let $head = doc.resolve(mapping.map(this.head));
1647
+ if (!$head.parent.inlineContent)
1648
+ return Selection.near($head);
1649
+ let $anchor = doc.resolve(mapping.map(this.anchor));
1650
+ return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
1651
+ }
1652
+ replace(tr, content = Slice.empty) {
1653
+ super.replace(tr, content);
1654
+ if (content == Slice.empty) {
1655
+ let marks = this.$from.marksAcross(this.$to);
1656
+ if (marks)
1657
+ tr.ensureMarks(marks);
1658
+ }
1659
+ }
1660
+ eq(other) {
1661
+ return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
1662
+ }
1663
+ getBookmark() {
1664
+ return new TextBookmark(this.anchor, this.head);
1665
+ }
1666
+ toJSON() {
1667
+ return { type: "text", anchor: this.anchor, head: this.head };
1668
+ }
1669
+ /**
1670
+ @internal
1671
+ */
1672
+ static fromJSON(doc, json) {
1673
+ if (typeof json.anchor != "number" || typeof json.head != "number")
1674
+ throw new RangeError("Invalid input for TextSelection.fromJSON");
1675
+ return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
1676
+ }
1677
+ /**
1678
+ Create a text selection from non-resolved positions.
1679
+ */
1680
+ static create(doc, anchor, head = anchor) {
1681
+ let $anchor = doc.resolve(anchor);
1682
+ return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
1683
+ }
1684
+ /**
1685
+ Return a text selection that spans the given positions or, if
1686
+ they aren't text positions, find a text selection near them.
1687
+ `bias` determines whether the method searches forward (default)
1688
+ or backwards (negative number) first. Will fall back to calling
1689
+ [`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
1690
+ doesn't contain a valid text position.
1691
+ */
1692
+ static between($anchor, $head, bias) {
1693
+ let dPos = $anchor.pos - $head.pos;
1694
+ if (!bias || dPos)
1695
+ bias = dPos >= 0 ? 1 : -1;
1696
+ if (!$head.parent.inlineContent) {
1697
+ let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
1698
+ if (found)
1699
+ $head = found.$head;
1700
+ else
1701
+ return Selection.near($head, bias);
1702
+ }
1703
+ if (!$anchor.parent.inlineContent) {
1704
+ if (dPos == 0) {
1705
+ $anchor = $head;
1706
+ }
1707
+ else {
1708
+ $anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
1709
+ if (($anchor.pos < $head.pos) != (dPos < 0))
1710
+ $anchor = $head;
1711
+ }
1712
+ }
1713
+ return new TextSelection($anchor, $head);
1714
+ }
1715
+ }
1716
+ Selection.jsonID("text", TextSelection);
1717
+ class TextBookmark {
1718
+ constructor(anchor, head) {
1719
+ this.anchor = anchor;
1720
+ this.head = head;
1721
+ }
1722
+ map(mapping) {
1723
+ return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
1724
+ }
1725
+ resolve(doc) {
1726
+ return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
1727
+ }
1728
+ }
1729
+ /**
1730
+ A node selection is a selection that points at a single node. All
1731
+ nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
1732
+ target of a node selection. In such a selection, `from` and `to`
1733
+ point directly before and after the selected node, `anchor` equals
1734
+ `from`, and `head` equals `to`..
1735
+ */
1736
+ class NodeSelection extends Selection {
1737
+ /**
1738
+ Create a node selection. Does not verify the validity of its
1739
+ argument.
1740
+ */
1741
+ constructor($pos) {
1742
+ let node = $pos.nodeAfter;
1743
+ let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
1744
+ super($pos, $end);
1745
+ this.node = node;
1746
+ }
1747
+ map(doc, mapping) {
1748
+ let { deleted, pos } = mapping.mapResult(this.anchor);
1749
+ let $pos = doc.resolve(pos);
1750
+ if (deleted)
1751
+ return Selection.near($pos);
1752
+ return new NodeSelection($pos);
1753
+ }
1754
+ content() {
1755
+ return new Slice(Fragment.from(this.node), 0, 0);
1756
+ }
1757
+ eq(other) {
1758
+ return other instanceof NodeSelection && other.anchor == this.anchor;
1759
+ }
1760
+ toJSON() {
1761
+ return { type: "node", anchor: this.anchor };
1762
+ }
1763
+ getBookmark() { return new NodeBookmark(this.anchor); }
1764
+ /**
1765
+ @internal
1766
+ */
1767
+ static fromJSON(doc, json) {
1768
+ if (typeof json.anchor != "number")
1769
+ throw new RangeError("Invalid input for NodeSelection.fromJSON");
1770
+ return new NodeSelection(doc.resolve(json.anchor));
1771
+ }
1772
+ /**
1773
+ Create a node selection from non-resolved positions.
1774
+ */
1775
+ static create(doc, from) {
1776
+ return new NodeSelection(doc.resolve(from));
1777
+ }
1778
+ /**
1779
+ Determines whether the given node may be selected as a node
1780
+ selection.
1781
+ */
1782
+ static isSelectable(node) {
1783
+ return !node.isText && node.type.spec.selectable !== false;
1784
+ }
1785
+ }
1786
+ NodeSelection.prototype.visible = false;
1787
+ Selection.jsonID("node", NodeSelection);
1788
+ class NodeBookmark {
1789
+ constructor(anchor) {
1790
+ this.anchor = anchor;
1791
+ }
1792
+ map(mapping) {
1793
+ let { deleted, pos } = mapping.mapResult(this.anchor);
1794
+ return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
1795
+ }
1796
+ resolve(doc) {
1797
+ let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
1798
+ if (node && NodeSelection.isSelectable(node))
1799
+ return new NodeSelection($pos);
1800
+ return Selection.near($pos);
1801
+ }
1802
+ }
1803
+ /**
1804
+ A selection type that represents selecting the whole document
1805
+ (which can not necessarily be expressed with a text selection, when
1806
+ there are for example leaf block nodes at the start or end of the
1807
+ document).
1808
+ */
1809
+ class AllSelection extends Selection {
1810
+ /**
1811
+ Create an all-selection over the given document.
1812
+ */
1813
+ constructor(doc) {
1814
+ super(doc.resolve(0), doc.resolve(doc.content.size));
1815
+ }
1816
+ replace(tr, content = Slice.empty) {
1817
+ if (content == Slice.empty) {
1818
+ tr.delete(0, tr.doc.content.size);
1819
+ let sel = Selection.atStart(tr.doc);
1820
+ if (!sel.eq(tr.selection))
1821
+ tr.setSelection(sel);
1822
+ }
1823
+ else {
1824
+ super.replace(tr, content);
1825
+ }
1826
+ }
1827
+ toJSON() { return { type: "all" }; }
1828
+ /**
1829
+ @internal
1830
+ */
1831
+ static fromJSON(doc) { return new AllSelection(doc); }
1832
+ map(doc) { return new AllSelection(doc); }
1833
+ eq(other) { return other instanceof AllSelection; }
1834
+ getBookmark() { return AllBookmark; }
1835
+ }
1836
+ Selection.jsonID("all", AllSelection);
1837
+ const AllBookmark = {
1838
+ map() { return this; },
1839
+ resolve(doc) { return new AllSelection(doc); }
1840
+ };
1841
+ // FIXME we'll need some awareness of text direction when scanning for selections
1842
+ // Try to find a selection inside the given node. `pos` points at the
1843
+ // position where the search starts. When `text` is true, only return
1844
+ // text selections.
1845
+ function findSelectionIn(doc, node, pos, index, dir, text = false) {
1846
+ if (node.inlineContent)
1847
+ return TextSelection.create(doc, pos);
1848
+ for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
1849
+ let child = node.child(i);
1850
+ if (!child.isAtom) {
1851
+ let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
1852
+ if (inner)
1853
+ return inner;
1854
+ }
1855
+ else if (!text && NodeSelection.isSelectable(child)) {
1856
+ return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
1857
+ }
1858
+ pos += child.nodeSize * dir;
1859
+ }
1860
+ return null;
1861
+ }
1862
+ function selectionToInsertionEnd(tr, startLen, bias) {
1863
+ let last = tr.steps.length - 1;
1864
+ if (last < startLen)
1865
+ return;
1866
+ let step = tr.steps[last];
1867
+ if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep))
1868
+ return;
1869
+ let map = tr.mapping.maps[last], end;
1870
+ map.forEach((_from, _to, _newFrom, newTo) => { if (end == null)
1871
+ end = newTo; });
1872
+ tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
1873
+ }
1874
+
1875
+ function bind(f, self) {
1876
+ return !self || !f ? f : f.bind(self);
1877
+ }
1878
+ class FieldDesc {
1879
+ constructor(name, desc, self) {
1880
+ this.name = name;
1881
+ this.init = bind(desc.init, self);
1882
+ this.apply = bind(desc.apply, self);
1883
+ }
1884
+ }
1885
+ [
1886
+ new FieldDesc("doc", {
1887
+ init(config) { return config.doc || config.schema.topNodeType.createAndFill(); },
1888
+ apply(tr) { return tr.doc; }
1889
+ }),
1890
+ new FieldDesc("selection", {
1891
+ init(config, instance) { return config.selection || Selection.atStart(instance.doc); },
1892
+ apply(tr) { return tr.selection; }
1893
+ }),
1894
+ new FieldDesc("storedMarks", {
1895
+ init(config) { return config.storedMarks || null; },
1896
+ apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null; }
1897
+ }),
1898
+ new FieldDesc("scrollToSelection", {
1899
+ init() { return 0; },
1900
+ apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev; }
1901
+ })
1902
+ ];
1903
+
1904
+ function keepAlive(..._args) {
1905
+ }
1906
+
1907
+ keepAlive(h);
1908
+ function Icon({ icon, class: className, onClick }) {
1909
+ return /* @__PURE__ */ h(
1910
+ "span",
1911
+ {
1912
+ class: clsx("milkdown-icon", className),
1913
+ onPointerdown: onClick,
1914
+ innerHTML: icon ? DOMPurify.sanitize(icon.trim()) : void 0
1915
+ }
1916
+ );
1917
+ }
1918
+ Icon.props = {
1919
+ icon: {
1920
+ type: String,
1921
+ required: false
1922
+ },
1923
+ class: {
1924
+ type: String,
1925
+ required: false
1926
+ },
1927
+ onClick: {
1928
+ type: Function,
1929
+ required: false
1930
+ }
1931
+ };
1932
+
1933
+ keepAlive(h);
1934
+ const ListItem = defineComponent({
1935
+ props: {
1936
+ label: {
1937
+ type: Object,
1938
+ required: true
1939
+ },
1940
+ checked: {
1941
+ type: Object,
1942
+ required: true
1943
+ },
1944
+ listType: {
1945
+ type: Object,
1946
+ required: true
1947
+ },
1948
+ config: {
1949
+ type: Object,
1950
+ required: true
1951
+ },
1952
+ readonly: {
1953
+ type: Object,
1954
+ required: true
1955
+ },
1956
+ selected: {
1957
+ type: Object,
1958
+ required: true
1959
+ },
1960
+ setAttr: {
1961
+ type: Function,
1962
+ required: true
1963
+ },
1964
+ onMount: {
1965
+ type: Function,
1966
+ required: true
1967
+ }
1968
+ },
1969
+ setup({
1970
+ label,
1971
+ checked,
1972
+ listType,
1973
+ config,
1974
+ readonly,
1975
+ setAttr,
1976
+ onMount,
1977
+ selected
1978
+ }) {
1979
+ const contentWrapperRef = (div) => {
1980
+ if (div == null) return;
1981
+ if (div instanceof Element) {
1982
+ onMount(div);
1983
+ }
1984
+ };
1985
+ const onClickLabel = (e) => {
1986
+ e.stopPropagation();
1987
+ e.preventDefault();
1988
+ if (checked.value == null) return;
1989
+ setAttr("checked", !checked.value);
1990
+ };
1991
+ const icon = computed(() => {
1992
+ return config.renderLabel({
1993
+ label: label.value,
1994
+ listType: listType.value,
1995
+ checked: checked.value,
1996
+ readonly: readonly.value
1997
+ });
1998
+ });
1999
+ const labelClass = computed(() => {
2000
+ if (checked.value == null) {
2001
+ if (listType.value === "bullet") return "bullet";
2002
+ return "ordered";
2003
+ }
2004
+ if (checked.value) return "checked";
2005
+ return "unchecked";
2006
+ });
2007
+ return () => {
2008
+ return /* @__PURE__ */ h(
2009
+ "li",
2010
+ {
2011
+ class: clsx(
2012
+ "list-item",
2013
+ selected.value && "ProseMirror-selectednode"
2014
+ )
2015
+ },
2016
+ /* @__PURE__ */ h(
2017
+ "div",
2018
+ {
2019
+ class: "label-wrapper",
2020
+ onPointerdown: onClickLabel,
2021
+ contenteditable: false
2022
+ },
2023
+ /* @__PURE__ */ h(
2024
+ Icon,
2025
+ {
2026
+ class: clsx(
2027
+ "label",
2028
+ readonly.value && "readonly",
2029
+ labelClass.value
2030
+ ),
2031
+ icon: icon.value
2032
+ }
2033
+ )
2034
+ ),
2035
+ /* @__PURE__ */ h("div", { class: "children", ref: contentWrapperRef })
2036
+ );
2037
+ };
2038
+ }
2039
+ });
2040
+
2041
+ const listItemBlockView = $view(
2042
+ listItemSchema.node,
2043
+ (ctx) => {
2044
+ return (initialNode, view, getPos) => {
2045
+ const dom = document.createElement("div");
2046
+ dom.className = "milkdown-list-item-block";
2047
+ const contentDOM = document.createElement("div");
2048
+ contentDOM.setAttribute("data-content-dom", "true");
2049
+ contentDOM.classList.add("content-dom");
2050
+ const label = ref(initialNode.attrs.label);
2051
+ const checked = ref(initialNode.attrs.checked);
2052
+ const listType = ref(initialNode.attrs.listType);
2053
+ const readonly = ref(!view.editable);
2054
+ const config = ctx.get(listItemBlockConfig.key);
2055
+ const selected = ref(false);
2056
+ const setAttr = (attr, value) => {
2057
+ if (!view.editable) return;
2058
+ const pos = getPos();
2059
+ if (pos == null) return;
2060
+ if (!view.hasFocus()) view.focus();
2061
+ view.dispatch(view.state.tr.setNodeAttribute(pos, attr, value));
2062
+ };
2063
+ const disposeSelectedWatcher = watchEffect(() => {
2064
+ const isSelected = selected.value;
2065
+ if (isSelected) {
2066
+ dom.classList.add("selected");
2067
+ } else {
2068
+ dom.classList.remove("selected");
2069
+ }
2070
+ });
2071
+ let raf = 0;
2072
+ const onMount = (div) => {
2073
+ const { anchor, head } = view.state.selection;
2074
+ div.appendChild(contentDOM);
2075
+ const anchorPos = view.state.doc.resolve(anchor);
2076
+ const headPos = view.state.doc.resolve(head);
2077
+ raf = requestAnimationFrame(() => {
2078
+ cancelAnimationFrame(raf);
2079
+ if (!anchorPos.doc.eq(view.state.doc)) return;
2080
+ const selection = new TextSelection(anchorPos, headPos);
2081
+ view.dispatch(view.state.tr.setSelection(selection));
2082
+ });
2083
+ };
2084
+ const app = createApp(ListItem, {
2085
+ label,
2086
+ checked,
2087
+ listType,
2088
+ readonly,
2089
+ config,
2090
+ selected,
2091
+ setAttr,
2092
+ onMount
2093
+ });
2094
+ app.mount(dom);
2095
+ const bindAttrs = (node2) => {
2096
+ listType.value = node2.attrs.listType;
2097
+ label.value = node2.attrs.label;
2098
+ checked.value = node2.attrs.checked;
2099
+ readonly.value = !view.editable;
2100
+ };
2101
+ bindAttrs(initialNode);
2102
+ let node = initialNode;
2103
+ return {
2104
+ dom,
2105
+ contentDOM,
2106
+ update: (updatedNode) => {
2107
+ if (updatedNode.type !== initialNode.type) return false;
2108
+ if (updatedNode.sameMarkup(node) && updatedNode.content.eq(node.content))
2109
+ return true;
2110
+ node = updatedNode;
2111
+ bindAttrs(updatedNode);
2112
+ return true;
2113
+ },
2114
+ ignoreMutation: (mutation) => {
2115
+ if (!dom || !contentDOM) return true;
2116
+ if (mutation.type === "selection") return false;
2117
+ if (contentDOM === mutation.target && mutation.type === "attributes")
2118
+ return true;
2119
+ if (contentDOM.contains(mutation.target)) return false;
2120
+ return true;
2121
+ },
2122
+ selectNode: () => {
2123
+ selected.value = true;
2124
+ },
2125
+ deselectNode: () => {
2126
+ selected.value = false;
2127
+ },
2128
+ destroy: () => {
2129
+ disposeSelectedWatcher();
2130
+ app.unmount();
2131
+ dom.remove();
2132
+ contentDOM.remove();
2133
+ }
2134
+ };
2135
+ };
2136
+ }
2137
+ );
2138
+ withMeta(listItemBlockView, {
2139
+ displayName: "NodeView<list-item-block>",
2140
+ group: "ListItemBlock"
2141
+ });
2142
+
2143
+ const listItemBlockComponent = [
2144
+ listItemBlockConfig,
2145
+ listItemBlockView
2146
+ ];
2147
+
2148
+ export { ListItem, defaultListItemBlockConfig, listItemBlockComponent, listItemBlockConfig, listItemBlockView };
2149
+ //# sourceMappingURL=index.js.map