@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,4160 @@
1
+ import { $ctx, $view } from '@jvs-milkdown/utils';
2
+ import { codeBlockSchema } from '@jvs-milkdown/preset-commonmark';
3
+ import { Compartment, EditorState } from '@codemirror/state';
4
+ import { EditorView, drawSelection, keymap } from '@codemirror/view';
5
+ import { h, Fragment as Fragment$1, defineComponent, ref, watch, computed, onMounted, onUnmounted, watchEffect, createApp } from 'vue';
6
+ import clsx from 'clsx';
7
+ import DOMPurify from 'dompurify';
8
+ import { computePosition } from '@floating-ui/dom';
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
12
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
13
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
14
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
15
+ var __spreadValues = (a, b) => {
16
+ for (var prop in b || (b = {}))
17
+ if (__hasOwnProp.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ if (__getOwnPropSymbols)
20
+ for (var prop of __getOwnPropSymbols(b)) {
21
+ if (__propIsEnum.call(b, prop))
22
+ __defNormalProp(a, prop, b[prop]);
23
+ }
24
+ return a;
25
+ };
26
+ function withMeta(plugin, meta) {
27
+ Object.assign(plugin, {
28
+ meta: __spreadValues({
29
+ package: "@jvs-milkdown/components"
30
+ }, meta)
31
+ });
32
+ return plugin;
33
+ }
34
+
35
+ const defaultConfig = {
36
+ extensions: [],
37
+ languages: [],
38
+ expandIcon: "\u2B07",
39
+ searchIcon: "\u{1F50D}",
40
+ clearSearchIcon: "\u232B",
41
+ searchPlaceholder: "Search language",
42
+ noResultText: "No result",
43
+ copyText: "Copy",
44
+ copyIcon: "\u{1F4CB}",
45
+ onCopy: () => {
46
+ },
47
+ renderLanguage: (language) => language,
48
+ renderPreview: () => null,
49
+ previewToggleButton: (previewOnlyMode) => previewOnlyMode ? "Edit" : "Hide",
50
+ previewLabel: "Preview",
51
+ previewLoading: "Loading..."
52
+ };
53
+ const codeBlockConfig = $ctx(defaultConfig, "codeBlockConfigCtx");
54
+ withMeta(codeBlockConfig, {
55
+ displayName: "Config<code-block>",
56
+ group: "CodeBlock"
57
+ });
58
+
59
+ class LanguageLoader {
60
+ constructor(languages) {
61
+ this.languages = languages;
62
+ this.map = {};
63
+ languages.forEach((language) => {
64
+ language.alias.forEach((alias) => {
65
+ this.map[alias] = language;
66
+ });
67
+ });
68
+ }
69
+ getAll() {
70
+ return this.languages.map((language) => {
71
+ return {
72
+ name: language.name,
73
+ alias: language.alias
74
+ };
75
+ });
76
+ }
77
+ load(languageName) {
78
+ const languageMap = this.map;
79
+ const language = languageMap[languageName.toLowerCase()];
80
+ if (!language) return Promise.resolve(void 0);
81
+ if (language.support) return Promise.resolve(language.support);
82
+ return language.load();
83
+ }
84
+ }
85
+
86
+ function findDiffStart(a, b, pos) {
87
+ for (let i = 0;; i++) {
88
+ if (i == a.childCount || i == b.childCount)
89
+ return a.childCount == b.childCount ? null : pos;
90
+ let childA = a.child(i), childB = b.child(i);
91
+ if (childA == childB) {
92
+ pos += childA.nodeSize;
93
+ continue;
94
+ }
95
+ if (!childA.sameMarkup(childB))
96
+ return pos;
97
+ if (childA.isText && childA.text != childB.text) {
98
+ for (let j = 0; childA.text[j] == childB.text[j]; j++)
99
+ pos++;
100
+ return pos;
101
+ }
102
+ if (childA.content.size || childB.content.size) {
103
+ let inner = findDiffStart(childA.content, childB.content, pos + 1);
104
+ if (inner != null)
105
+ return inner;
106
+ }
107
+ pos += childA.nodeSize;
108
+ }
109
+ }
110
+ function findDiffEnd(a, b, posA, posB) {
111
+ for (let iA = a.childCount, iB = b.childCount;;) {
112
+ if (iA == 0 || iB == 0)
113
+ return iA == iB ? null : { a: posA, b: posB };
114
+ let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
115
+ if (childA == childB) {
116
+ posA -= size;
117
+ posB -= size;
118
+ continue;
119
+ }
120
+ if (!childA.sameMarkup(childB))
121
+ return { a: posA, b: posB };
122
+ if (childA.isText && childA.text != childB.text) {
123
+ let same = 0, minSize = Math.min(childA.text.length, childB.text.length);
124
+ while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
125
+ same++;
126
+ posA--;
127
+ posB--;
128
+ }
129
+ return { a: posA, b: posB };
130
+ }
131
+ if (childA.content.size || childB.content.size) {
132
+ let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
133
+ if (inner)
134
+ return inner;
135
+ }
136
+ posA -= size;
137
+ posB -= size;
138
+ }
139
+ }
140
+
141
+ /**
142
+ A fragment represents a node's collection of child nodes.
143
+
144
+ Like nodes, fragments are persistent data structures, and you
145
+ should not mutate them or their content. Rather, you create new
146
+ instances whenever needed. The API tries to make this easy.
147
+ */
148
+ class Fragment {
149
+ /**
150
+ @internal
151
+ */
152
+ constructor(
153
+ /**
154
+ The child nodes in this fragment.
155
+ */
156
+ content, size) {
157
+ this.content = content;
158
+ this.size = size || 0;
159
+ if (size == null)
160
+ for (let i = 0; i < content.length; i++)
161
+ this.size += content[i].nodeSize;
162
+ }
163
+ /**
164
+ Invoke a callback for all descendant nodes between the given two
165
+ positions (relative to start of this fragment). Doesn't descend
166
+ into a node when the callback returns `false`.
167
+ */
168
+ nodesBetween(from, to, f, nodeStart = 0, parent) {
169
+ for (let i = 0, pos = 0; pos < to; i++) {
170
+ let child = this.content[i], end = pos + child.nodeSize;
171
+ if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
172
+ let start = pos + 1;
173
+ child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
174
+ }
175
+ pos = end;
176
+ }
177
+ }
178
+ /**
179
+ Call the given callback for every descendant node. `pos` will be
180
+ relative to the start of the fragment. The callback may return
181
+ `false` to prevent traversal of a given node's children.
182
+ */
183
+ descendants(f) {
184
+ this.nodesBetween(0, this.size, f);
185
+ }
186
+ /**
187
+ Extract the text between `from` and `to`. See the same method on
188
+ [`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
189
+ */
190
+ textBetween(from, to, blockSeparator, leafText) {
191
+ let text = "", first = true;
192
+ this.nodesBetween(from, to, (node, pos) => {
193
+ let nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos)
194
+ : !node.isLeaf ? ""
195
+ : leafText ? (typeof leafText === "function" ? leafText(node) : leafText)
196
+ : node.type.spec.leafText ? node.type.spec.leafText(node)
197
+ : "";
198
+ if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) {
199
+ if (first)
200
+ first = false;
201
+ else
202
+ text += blockSeparator;
203
+ }
204
+ text += nodeText;
205
+ }, 0);
206
+ return text;
207
+ }
208
+ /**
209
+ Create a new fragment containing the combined content of this
210
+ fragment and the other.
211
+ */
212
+ append(other) {
213
+ if (!other.size)
214
+ return this;
215
+ if (!this.size)
216
+ return other;
217
+ let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
218
+ if (last.isText && last.sameMarkup(first)) {
219
+ content[content.length - 1] = last.withText(last.text + first.text);
220
+ i = 1;
221
+ }
222
+ for (; i < other.content.length; i++)
223
+ content.push(other.content[i]);
224
+ return new Fragment(content, this.size + other.size);
225
+ }
226
+ /**
227
+ Cut out the sub-fragment between the two given positions.
228
+ */
229
+ cut(from, to = this.size) {
230
+ if (from == 0 && to == this.size)
231
+ return this;
232
+ let result = [], size = 0;
233
+ if (to > from)
234
+ for (let i = 0, pos = 0; pos < to; i++) {
235
+ let child = this.content[i], end = pos + child.nodeSize;
236
+ if (end > from) {
237
+ if (pos < from || end > to) {
238
+ if (child.isText)
239
+ child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
240
+ else
241
+ child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
242
+ }
243
+ result.push(child);
244
+ size += child.nodeSize;
245
+ }
246
+ pos = end;
247
+ }
248
+ return new Fragment(result, size);
249
+ }
250
+ /**
251
+ @internal
252
+ */
253
+ cutByIndex(from, to) {
254
+ if (from == to)
255
+ return Fragment.empty;
256
+ if (from == 0 && to == this.content.length)
257
+ return this;
258
+ return new Fragment(this.content.slice(from, to));
259
+ }
260
+ /**
261
+ Create a new fragment in which the node at the given index is
262
+ replaced by the given node.
263
+ */
264
+ replaceChild(index, node) {
265
+ let current = this.content[index];
266
+ if (current == node)
267
+ return this;
268
+ let copy = this.content.slice();
269
+ let size = this.size + node.nodeSize - current.nodeSize;
270
+ copy[index] = node;
271
+ return new Fragment(copy, size);
272
+ }
273
+ /**
274
+ Create a new fragment by prepending the given node to this
275
+ fragment.
276
+ */
277
+ addToStart(node) {
278
+ return new Fragment([node].concat(this.content), this.size + node.nodeSize);
279
+ }
280
+ /**
281
+ Create a new fragment by appending the given node to this
282
+ fragment.
283
+ */
284
+ addToEnd(node) {
285
+ return new Fragment(this.content.concat(node), this.size + node.nodeSize);
286
+ }
287
+ /**
288
+ Compare this fragment to another one.
289
+ */
290
+ eq(other) {
291
+ if (this.content.length != other.content.length)
292
+ return false;
293
+ for (let i = 0; i < this.content.length; i++)
294
+ if (!this.content[i].eq(other.content[i]))
295
+ return false;
296
+ return true;
297
+ }
298
+ /**
299
+ The first child of the fragment, or `null` if it is empty.
300
+ */
301
+ get firstChild() { return this.content.length ? this.content[0] : null; }
302
+ /**
303
+ The last child of the fragment, or `null` if it is empty.
304
+ */
305
+ get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; }
306
+ /**
307
+ The number of child nodes in this fragment.
308
+ */
309
+ get childCount() { return this.content.length; }
310
+ /**
311
+ Get the child node at the given index. Raise an error when the
312
+ index is out of range.
313
+ */
314
+ child(index) {
315
+ let found = this.content[index];
316
+ if (!found)
317
+ throw new RangeError("Index " + index + " out of range for " + this);
318
+ return found;
319
+ }
320
+ /**
321
+ Get the child node at the given index, if it exists.
322
+ */
323
+ maybeChild(index) {
324
+ return this.content[index] || null;
325
+ }
326
+ /**
327
+ Call `f` for every child node, passing the node, its offset
328
+ into this parent node, and its index.
329
+ */
330
+ forEach(f) {
331
+ for (let i = 0, p = 0; i < this.content.length; i++) {
332
+ let child = this.content[i];
333
+ f(child, p, i);
334
+ p += child.nodeSize;
335
+ }
336
+ }
337
+ /**
338
+ Find the first position at which this fragment and another
339
+ fragment differ, or `null` if they are the same.
340
+ */
341
+ findDiffStart(other, pos = 0) {
342
+ return findDiffStart(this, other, pos);
343
+ }
344
+ /**
345
+ Find the first position, searching from the end, at which this
346
+ fragment and the given fragment differ, or `null` if they are
347
+ the same. Since this position will not be the same in both
348
+ nodes, an object with two separate positions is returned.
349
+ */
350
+ findDiffEnd(other, pos = this.size, otherPos = other.size) {
351
+ return findDiffEnd(this, other, pos, otherPos);
352
+ }
353
+ /**
354
+ Find the index and inner offset corresponding to a given relative
355
+ position in this fragment. The result object will be reused
356
+ (overwritten) the next time the function is called. @internal
357
+ */
358
+ findIndex(pos) {
359
+ if (pos == 0)
360
+ return retIndex(0, pos);
361
+ if (pos == this.size)
362
+ return retIndex(this.content.length, pos);
363
+ if (pos > this.size || pos < 0)
364
+ throw new RangeError(`Position ${pos} outside of fragment (${this})`);
365
+ for (let i = 0, curPos = 0;; i++) {
366
+ let cur = this.child(i), end = curPos + cur.nodeSize;
367
+ if (end >= pos) {
368
+ if (end == pos)
369
+ return retIndex(i + 1, end);
370
+ return retIndex(i, curPos);
371
+ }
372
+ curPos = end;
373
+ }
374
+ }
375
+ /**
376
+ Return a debugging string that describes this fragment.
377
+ */
378
+ toString() { return "<" + this.toStringInner() + ">"; }
379
+ /**
380
+ @internal
381
+ */
382
+ toStringInner() { return this.content.join(", "); }
383
+ /**
384
+ Create a JSON-serializeable representation of this fragment.
385
+ */
386
+ toJSON() {
387
+ return this.content.length ? this.content.map(n => n.toJSON()) : null;
388
+ }
389
+ /**
390
+ Deserialize a fragment from its JSON representation.
391
+ */
392
+ static fromJSON(schema, value) {
393
+ if (!value)
394
+ return Fragment.empty;
395
+ if (!Array.isArray(value))
396
+ throw new RangeError("Invalid input for Fragment.fromJSON");
397
+ return new Fragment(value.map(schema.nodeFromJSON));
398
+ }
399
+ /**
400
+ Build a fragment from an array of nodes. Ensures that adjacent
401
+ text nodes with the same marks are joined together.
402
+ */
403
+ static fromArray(array) {
404
+ if (!array.length)
405
+ return Fragment.empty;
406
+ let joined, size = 0;
407
+ for (let i = 0; i < array.length; i++) {
408
+ let node = array[i];
409
+ size += node.nodeSize;
410
+ if (i && node.isText && array[i - 1].sameMarkup(node)) {
411
+ if (!joined)
412
+ joined = array.slice(0, i);
413
+ joined[joined.length - 1] = node
414
+ .withText(joined[joined.length - 1].text + node.text);
415
+ }
416
+ else if (joined) {
417
+ joined.push(node);
418
+ }
419
+ }
420
+ return new Fragment(joined || array, size);
421
+ }
422
+ /**
423
+ Create a fragment from something that can be interpreted as a
424
+ set of nodes. For `null`, it returns the empty fragment. For a
425
+ fragment, the fragment itself. For a node or array of nodes, a
426
+ fragment containing those nodes.
427
+ */
428
+ static from(nodes) {
429
+ if (!nodes)
430
+ return Fragment.empty;
431
+ if (nodes instanceof Fragment)
432
+ return nodes;
433
+ if (Array.isArray(nodes))
434
+ return this.fromArray(nodes);
435
+ if (nodes.attrs)
436
+ return new Fragment([nodes], nodes.nodeSize);
437
+ throw new RangeError("Can not convert " + nodes + " to a Fragment" +
438
+ (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
439
+ }
440
+ }
441
+ /**
442
+ An empty fragment. Intended to be reused whenever a node doesn't
443
+ contain anything (rather than allocating a new empty fragment for
444
+ each leaf node).
445
+ */
446
+ Fragment.empty = new Fragment([], 0);
447
+ const found = { index: 0, offset: 0 };
448
+ function retIndex(index, offset) {
449
+ found.index = index;
450
+ found.offset = offset;
451
+ return found;
452
+ }
453
+
454
+ /**
455
+ Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
456
+ given an invalid replacement.
457
+ */
458
+ class ReplaceError extends Error {
459
+ }
460
+ /*
461
+ ReplaceError = function(this: any, message: string) {
462
+ let err = Error.call(this, message)
463
+ ;(err as any).__proto__ = ReplaceError.prototype
464
+ return err
465
+ } as any
466
+
467
+ ReplaceError.prototype = Object.create(Error.prototype)
468
+ ReplaceError.prototype.constructor = ReplaceError
469
+ ReplaceError.prototype.name = "ReplaceError"
470
+ */
471
+ /**
472
+ A slice represents a piece cut out of a larger document. It
473
+ stores not only a fragment, but also the depth up to which nodes on
474
+ both side are ‘open’ (cut through).
475
+ */
476
+ class Slice {
477
+ /**
478
+ Create a slice. When specifying a non-zero open depth, you must
479
+ make sure that there are nodes of at least that depth at the
480
+ appropriate side of the fragment—i.e. if the fragment is an
481
+ empty paragraph node, `openStart` and `openEnd` can't be greater
482
+ than 1.
483
+
484
+ It is not necessary for the content of open nodes to conform to
485
+ the schema's content constraints, though it should be a valid
486
+ start/end/middle for such a node, depending on which sides are
487
+ open.
488
+ */
489
+ constructor(
490
+ /**
491
+ The slice's content.
492
+ */
493
+ content,
494
+ /**
495
+ The open depth at the start of the fragment.
496
+ */
497
+ openStart,
498
+ /**
499
+ The open depth at the end.
500
+ */
501
+ openEnd) {
502
+ this.content = content;
503
+ this.openStart = openStart;
504
+ this.openEnd = openEnd;
505
+ }
506
+ /**
507
+ The size this slice would add when inserted into a document.
508
+ */
509
+ get size() {
510
+ return this.content.size - this.openStart - this.openEnd;
511
+ }
512
+ /**
513
+ @internal
514
+ */
515
+ insertAt(pos, fragment) {
516
+ let content = insertInto(this.content, pos + this.openStart, fragment);
517
+ return content && new Slice(content, this.openStart, this.openEnd);
518
+ }
519
+ /**
520
+ @internal
521
+ */
522
+ removeBetween(from, to) {
523
+ return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
524
+ }
525
+ /**
526
+ Tests whether this slice is equal to another slice.
527
+ */
528
+ eq(other) {
529
+ return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
530
+ }
531
+ /**
532
+ @internal
533
+ */
534
+ toString() {
535
+ return this.content + "(" + this.openStart + "," + this.openEnd + ")";
536
+ }
537
+ /**
538
+ Convert a slice to a JSON-serializable representation.
539
+ */
540
+ toJSON() {
541
+ if (!this.content.size)
542
+ return null;
543
+ let json = { content: this.content.toJSON() };
544
+ if (this.openStart > 0)
545
+ json.openStart = this.openStart;
546
+ if (this.openEnd > 0)
547
+ json.openEnd = this.openEnd;
548
+ return json;
549
+ }
550
+ /**
551
+ Deserialize a slice from its JSON representation.
552
+ */
553
+ static fromJSON(schema, json) {
554
+ if (!json)
555
+ return Slice.empty;
556
+ let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
557
+ if (typeof openStart != "number" || typeof openEnd != "number")
558
+ throw new RangeError("Invalid input for Slice.fromJSON");
559
+ return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
560
+ }
561
+ /**
562
+ Create a slice from a fragment by taking the maximum possible
563
+ open value on both side of the fragment.
564
+ */
565
+ static maxOpen(fragment, openIsolating = true) {
566
+ let openStart = 0, openEnd = 0;
567
+ for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
568
+ openStart++;
569
+ for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
570
+ openEnd++;
571
+ return new Slice(fragment, openStart, openEnd);
572
+ }
573
+ }
574
+ /**
575
+ The empty slice.
576
+ */
577
+ Slice.empty = new Slice(Fragment.empty, 0, 0);
578
+ function removeRange(content, from, to) {
579
+ let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
580
+ let { index: indexTo, offset: offsetTo } = content.findIndex(to);
581
+ if (offset == from || child.isText) {
582
+ if (offsetTo != to && !content.child(indexTo).isText)
583
+ throw new RangeError("Removing non-flat range");
584
+ return content.cut(0, from).append(content.cut(to));
585
+ }
586
+ if (index != indexTo)
587
+ throw new RangeError("Removing non-flat range");
588
+ return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
589
+ }
590
+ function insertInto(content, dist, insert, parent) {
591
+ let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
592
+ if (offset == dist || child.isText) {
593
+ if (parent && !parent.canReplace(index, index, insert))
594
+ return null;
595
+ return content.cut(0, dist).append(insert).append(content.cut(dist));
596
+ }
597
+ let inner = insertInto(child.content, dist - offset - 1, insert, child);
598
+ return inner && content.replaceChild(index, child.copy(inner));
599
+ }
600
+
601
+ // Recovery values encode a range index and an offset. They are
602
+ // represented as numbers, because tons of them will be created when
603
+ // mapping, for example, a large number of decorations. The number's
604
+ // lower 16 bits provide the index, the remaining bits the offset.
605
+ //
606
+ // Note: We intentionally don't use bit shift operators to en- and
607
+ // decode these, since those clip to 32 bits, which we might in rare
608
+ // cases want to overflow. A 64-bit float can represent 48-bit
609
+ // integers precisely.
610
+ const lower16 = 0xffff;
611
+ const factor16 = Math.pow(2, 16);
612
+ function makeRecover(index, offset) { return index + offset * factor16; }
613
+ function recoverIndex(value) { return value & lower16; }
614
+ function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
615
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
616
+ /**
617
+ An object representing a mapped position with extra
618
+ information.
619
+ */
620
+ class MapResult {
621
+ /**
622
+ @internal
623
+ */
624
+ constructor(
625
+ /**
626
+ The mapped version of the position.
627
+ */
628
+ pos,
629
+ /**
630
+ @internal
631
+ */
632
+ delInfo,
633
+ /**
634
+ @internal
635
+ */
636
+ recover) {
637
+ this.pos = pos;
638
+ this.delInfo = delInfo;
639
+ this.recover = recover;
640
+ }
641
+ /**
642
+ Tells you whether the position was deleted, that is, whether the
643
+ step removed the token on the side queried (via the `assoc`)
644
+ argument from the document.
645
+ */
646
+ get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
647
+ /**
648
+ Tells you whether the token before the mapped position was deleted.
649
+ */
650
+ get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
651
+ /**
652
+ True when the token after the mapped position was deleted.
653
+ */
654
+ get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
655
+ /**
656
+ Tells whether any of the steps mapped through deletes across the
657
+ position (including both the token before and after the
658
+ position).
659
+ */
660
+ get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
661
+ }
662
+ /**
663
+ A map describing the deletions and insertions made by a step, which
664
+ can be used to find the correspondence between positions in the
665
+ pre-step version of a document and the same position in the
666
+ post-step version.
667
+ */
668
+ class StepMap {
669
+ /**
670
+ Create a position map. The modifications to the document are
671
+ represented as an array of numbers, in which each group of three
672
+ represents a modified chunk as `[start, oldSize, newSize]`.
673
+ */
674
+ constructor(
675
+ /**
676
+ @internal
677
+ */
678
+ ranges,
679
+ /**
680
+ @internal
681
+ */
682
+ inverted = false) {
683
+ this.ranges = ranges;
684
+ this.inverted = inverted;
685
+ if (!ranges.length && StepMap.empty)
686
+ return StepMap.empty;
687
+ }
688
+ /**
689
+ @internal
690
+ */
691
+ recover(value) {
692
+ let diff = 0, index = recoverIndex(value);
693
+ if (!this.inverted)
694
+ for (let i = 0; i < index; i++)
695
+ diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
696
+ return this.ranges[index * 3] + diff + recoverOffset(value);
697
+ }
698
+ mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
699
+ map(pos, assoc = 1) { return this._map(pos, assoc, true); }
700
+ /**
701
+ @internal
702
+ */
703
+ _map(pos, assoc, simple) {
704
+ let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
705
+ for (let i = 0; i < this.ranges.length; i += 3) {
706
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
707
+ if (start > pos)
708
+ break;
709
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
710
+ if (pos <= end) {
711
+ let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
712
+ let result = start + diff + (side < 0 ? 0 : newSize);
713
+ if (simple)
714
+ return result;
715
+ let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
716
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
717
+ if (assoc < 0 ? pos != start : pos != end)
718
+ del |= DEL_SIDE;
719
+ return new MapResult(result, del, recover);
720
+ }
721
+ diff += newSize - oldSize;
722
+ }
723
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
724
+ }
725
+ /**
726
+ @internal
727
+ */
728
+ touches(pos, recover) {
729
+ let diff = 0, index = recoverIndex(recover);
730
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
731
+ for (let i = 0; i < this.ranges.length; i += 3) {
732
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
733
+ if (start > pos)
734
+ break;
735
+ let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
736
+ if (pos <= end && i == index * 3)
737
+ return true;
738
+ diff += this.ranges[i + newIndex] - oldSize;
739
+ }
740
+ return false;
741
+ }
742
+ /**
743
+ Calls the given function on each of the changed ranges included in
744
+ this map.
745
+ */
746
+ forEach(f) {
747
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
748
+ for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
749
+ let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
750
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
751
+ f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
752
+ diff += newSize - oldSize;
753
+ }
754
+ }
755
+ /**
756
+ Create an inverted version of this map. The result can be used to
757
+ map positions in the post-step document to the pre-step document.
758
+ */
759
+ invert() {
760
+ return new StepMap(this.ranges, !this.inverted);
761
+ }
762
+ /**
763
+ @internal
764
+ */
765
+ toString() {
766
+ return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
767
+ }
768
+ /**
769
+ Create a map that moves all positions by offset `n` (which may be
770
+ negative). This can be useful when applying steps meant for a
771
+ sub-document to a larger document, or vice-versa.
772
+ */
773
+ static offset(n) {
774
+ return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
775
+ }
776
+ }
777
+ /**
778
+ A StepMap that contains no changed ranges.
779
+ */
780
+ StepMap.empty = new StepMap([]);
781
+ /**
782
+ A mapping represents a pipeline of zero or more [step
783
+ maps](https://prosemirror.net/docs/ref/#transform.StepMap). It has special provisions for losslessly
784
+ handling mapping positions through a series of steps in which some
785
+ steps are inverted versions of earlier steps. (This comes up when
786
+ ‘[rebasing](https://prosemirror.net/docs/guide/#transform.rebasing)’ steps for
787
+ collaboration or history management.)
788
+ */
789
+ class Mapping {
790
+ /**
791
+ Create a new mapping with the given position maps.
792
+ */
793
+ constructor(maps,
794
+ /**
795
+ @internal
796
+ */
797
+ mirror,
798
+ /**
799
+ The starting position in the `maps` array, used when `map` or
800
+ `mapResult` is called.
801
+ */
802
+ from = 0,
803
+ /**
804
+ The end position in the `maps` array.
805
+ */
806
+ to = maps ? maps.length : 0) {
807
+ this.mirror = mirror;
808
+ this.from = from;
809
+ this.to = to;
810
+ this._maps = maps || [];
811
+ this.ownData = !(maps || mirror);
812
+ }
813
+ /**
814
+ The step maps in this mapping.
815
+ */
816
+ get maps() { return this._maps; }
817
+ /**
818
+ Create a mapping that maps only through a part of this one.
819
+ */
820
+ slice(from = 0, to = this.maps.length) {
821
+ return new Mapping(this._maps, this.mirror, from, to);
822
+ }
823
+ /**
824
+ Add a step map to the end of this mapping. If `mirrors` is
825
+ given, it should be the index of the step map that is the mirror
826
+ image of this one.
827
+ */
828
+ appendMap(map, mirrors) {
829
+ if (!this.ownData) {
830
+ this._maps = this._maps.slice();
831
+ this.mirror = this.mirror && this.mirror.slice();
832
+ this.ownData = true;
833
+ }
834
+ this.to = this._maps.push(map);
835
+ if (mirrors != null)
836
+ this.setMirror(this._maps.length - 1, mirrors);
837
+ }
838
+ /**
839
+ Add all the step maps in a given mapping to this one (preserving
840
+ mirroring information).
841
+ */
842
+ appendMapping(mapping) {
843
+ for (let i = 0, startSize = this._maps.length; i < mapping._maps.length; i++) {
844
+ let mirr = mapping.getMirror(i);
845
+ this.appendMap(mapping._maps[i], mirr != null && mirr < i ? startSize + mirr : undefined);
846
+ }
847
+ }
848
+ /**
849
+ Finds the offset of the step map that mirrors the map at the
850
+ given offset, in this mapping (as per the second argument to
851
+ `appendMap`).
852
+ */
853
+ getMirror(n) {
854
+ if (this.mirror)
855
+ for (let i = 0; i < this.mirror.length; i++)
856
+ if (this.mirror[i] == n)
857
+ return this.mirror[i + (i % 2 ? -1 : 1)];
858
+ }
859
+ /**
860
+ @internal
861
+ */
862
+ setMirror(n, m) {
863
+ if (!this.mirror)
864
+ this.mirror = [];
865
+ this.mirror.push(n, m);
866
+ }
867
+ /**
868
+ Append the inverse of the given mapping to this one.
869
+ */
870
+ appendMappingInverted(mapping) {
871
+ for (let i = mapping.maps.length - 1, totalSize = this._maps.length + mapping._maps.length; i >= 0; i--) {
872
+ let mirr = mapping.getMirror(i);
873
+ this.appendMap(mapping._maps[i].invert(), mirr != null && mirr > i ? totalSize - mirr - 1 : undefined);
874
+ }
875
+ }
876
+ /**
877
+ Create an inverted version of this mapping.
878
+ */
879
+ invert() {
880
+ let inverse = new Mapping;
881
+ inverse.appendMappingInverted(this);
882
+ return inverse;
883
+ }
884
+ /**
885
+ Map a position through this mapping.
886
+ */
887
+ map(pos, assoc = 1) {
888
+ if (this.mirror)
889
+ return this._map(pos, assoc, true);
890
+ for (let i = this.from; i < this.to; i++)
891
+ pos = this._maps[i].map(pos, assoc);
892
+ return pos;
893
+ }
894
+ /**
895
+ Map a position through this mapping, returning a mapping
896
+ result.
897
+ */
898
+ mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
899
+ /**
900
+ @internal
901
+ */
902
+ _map(pos, assoc, simple) {
903
+ let delInfo = 0;
904
+ for (let i = this.from; i < this.to; i++) {
905
+ let map = this._maps[i], result = map.mapResult(pos, assoc);
906
+ if (result.recover != null) {
907
+ let corr = this.getMirror(i);
908
+ if (corr != null && corr > i && corr < this.to) {
909
+ i = corr;
910
+ pos = this._maps[corr].recover(result.recover);
911
+ continue;
912
+ }
913
+ }
914
+ delInfo |= result.delInfo;
915
+ pos = result.pos;
916
+ }
917
+ return simple ? pos : new MapResult(pos, delInfo, null);
918
+ }
919
+ }
920
+
921
+ const stepsByID = Object.create(null);
922
+ /**
923
+ A step object represents an atomic change. It generally applies
924
+ only to the document it was created for, since the positions
925
+ stored in it will only make sense for that document.
926
+
927
+ New steps are defined by creating classes that extend `Step`,
928
+ overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
929
+ methods, and registering your class with a unique
930
+ JSON-serialization identifier using
931
+ [`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
932
+ */
933
+ class Step {
934
+ /**
935
+ Get the step map that represents the changes made by this step,
936
+ and which can be used to transform between positions in the old
937
+ and the new document.
938
+ */
939
+ getMap() { return StepMap.empty; }
940
+ /**
941
+ Try to merge this step with another one, to be applied directly
942
+ after it. Returns the merged step when possible, null if the
943
+ steps can't be merged.
944
+ */
945
+ merge(other) { return null; }
946
+ /**
947
+ Deserialize a step from its JSON representation. Will call
948
+ through to the step class' own implementation of this method.
949
+ */
950
+ static fromJSON(schema, json) {
951
+ if (!json || !json.stepType)
952
+ throw new RangeError("Invalid input for Step.fromJSON");
953
+ let type = stepsByID[json.stepType];
954
+ if (!type)
955
+ throw new RangeError(`No step type ${json.stepType} defined`);
956
+ return type.fromJSON(schema, json);
957
+ }
958
+ /**
959
+ To be able to serialize steps to JSON, each step needs a string
960
+ ID to attach to its JSON representation. Use this method to
961
+ register an ID for your step classes. Try to pick something
962
+ that's unlikely to clash with steps from other modules.
963
+ */
964
+ static jsonID(id, stepClass) {
965
+ if (id in stepsByID)
966
+ throw new RangeError("Duplicate use of step JSON ID " + id);
967
+ stepsByID[id] = stepClass;
968
+ stepClass.prototype.jsonID = id;
969
+ return stepClass;
970
+ }
971
+ }
972
+ /**
973
+ The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
974
+ new document or a failure value.
975
+ */
976
+ class StepResult {
977
+ /**
978
+ @internal
979
+ */
980
+ constructor(
981
+ /**
982
+ The transformed document, if successful.
983
+ */
984
+ doc,
985
+ /**
986
+ The failure message, if unsuccessful.
987
+ */
988
+ failed) {
989
+ this.doc = doc;
990
+ this.failed = failed;
991
+ }
992
+ /**
993
+ Create a successful step result.
994
+ */
995
+ static ok(doc) { return new StepResult(doc, null); }
996
+ /**
997
+ Create a failed step result.
998
+ */
999
+ static fail(message) { return new StepResult(null, message); }
1000
+ /**
1001
+ Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
1002
+ arguments. Create a successful result if it succeeds, and a
1003
+ failed one if it throws a `ReplaceError`.
1004
+ */
1005
+ static fromReplace(doc, from, to, slice) {
1006
+ try {
1007
+ return StepResult.ok(doc.replace(from, to, slice));
1008
+ }
1009
+ catch (e) {
1010
+ if (e instanceof ReplaceError)
1011
+ return StepResult.fail(e.message);
1012
+ throw e;
1013
+ }
1014
+ }
1015
+ }
1016
+
1017
+ function mapFragment(fragment, f, parent) {
1018
+ let mapped = [];
1019
+ for (let i = 0; i < fragment.childCount; i++) {
1020
+ let child = fragment.child(i);
1021
+ if (child.content.size)
1022
+ child = child.copy(mapFragment(child.content, f, child));
1023
+ if (child.isInline)
1024
+ child = f(child, parent, i);
1025
+ mapped.push(child);
1026
+ }
1027
+ return Fragment.fromArray(mapped);
1028
+ }
1029
+ /**
1030
+ Add a mark to all inline content between two positions.
1031
+ */
1032
+ class AddMarkStep extends Step {
1033
+ /**
1034
+ Create a mark step.
1035
+ */
1036
+ constructor(
1037
+ /**
1038
+ The start of the marked range.
1039
+ */
1040
+ from,
1041
+ /**
1042
+ The end of the marked range.
1043
+ */
1044
+ to,
1045
+ /**
1046
+ The mark to add.
1047
+ */
1048
+ mark) {
1049
+ super();
1050
+ this.from = from;
1051
+ this.to = to;
1052
+ this.mark = mark;
1053
+ }
1054
+ apply(doc) {
1055
+ let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
1056
+ let parent = $from.node($from.sharedDepth(this.to));
1057
+ let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
1058
+ if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type))
1059
+ return node;
1060
+ return node.mark(this.mark.addToSet(node.marks));
1061
+ }, parent), oldSlice.openStart, oldSlice.openEnd);
1062
+ return StepResult.fromReplace(doc, this.from, this.to, slice);
1063
+ }
1064
+ invert() {
1065
+ return new RemoveMarkStep(this.from, this.to, this.mark);
1066
+ }
1067
+ map(mapping) {
1068
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1069
+ if (from.deleted && to.deleted || from.pos >= to.pos)
1070
+ return null;
1071
+ return new AddMarkStep(from.pos, to.pos, this.mark);
1072
+ }
1073
+ merge(other) {
1074
+ if (other instanceof AddMarkStep &&
1075
+ other.mark.eq(this.mark) &&
1076
+ this.from <= other.to && this.to >= other.from)
1077
+ return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
1078
+ return null;
1079
+ }
1080
+ toJSON() {
1081
+ return { stepType: "addMark", mark: this.mark.toJSON(),
1082
+ from: this.from, to: this.to };
1083
+ }
1084
+ /**
1085
+ @internal
1086
+ */
1087
+ static fromJSON(schema, json) {
1088
+ if (typeof json.from != "number" || typeof json.to != "number")
1089
+ throw new RangeError("Invalid input for AddMarkStep.fromJSON");
1090
+ return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
1091
+ }
1092
+ }
1093
+ Step.jsonID("addMark", AddMarkStep);
1094
+ /**
1095
+ Remove a mark from all inline content between two positions.
1096
+ */
1097
+ class RemoveMarkStep extends Step {
1098
+ /**
1099
+ Create a mark-removing step.
1100
+ */
1101
+ constructor(
1102
+ /**
1103
+ The start of the unmarked range.
1104
+ */
1105
+ from,
1106
+ /**
1107
+ The end of the unmarked range.
1108
+ */
1109
+ to,
1110
+ /**
1111
+ The mark to remove.
1112
+ */
1113
+ mark) {
1114
+ super();
1115
+ this.from = from;
1116
+ this.to = to;
1117
+ this.mark = mark;
1118
+ }
1119
+ apply(doc) {
1120
+ let oldSlice = doc.slice(this.from, this.to);
1121
+ let slice = new Slice(mapFragment(oldSlice.content, node => {
1122
+ return node.mark(this.mark.removeFromSet(node.marks));
1123
+ }, doc), oldSlice.openStart, oldSlice.openEnd);
1124
+ return StepResult.fromReplace(doc, this.from, this.to, slice);
1125
+ }
1126
+ invert() {
1127
+ return new AddMarkStep(this.from, this.to, this.mark);
1128
+ }
1129
+ map(mapping) {
1130
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1131
+ if (from.deleted && to.deleted || from.pos >= to.pos)
1132
+ return null;
1133
+ return new RemoveMarkStep(from.pos, to.pos, this.mark);
1134
+ }
1135
+ merge(other) {
1136
+ if (other instanceof RemoveMarkStep &&
1137
+ other.mark.eq(this.mark) &&
1138
+ this.from <= other.to && this.to >= other.from)
1139
+ return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
1140
+ return null;
1141
+ }
1142
+ toJSON() {
1143
+ return { stepType: "removeMark", mark: this.mark.toJSON(),
1144
+ from: this.from, to: this.to };
1145
+ }
1146
+ /**
1147
+ @internal
1148
+ */
1149
+ static fromJSON(schema, json) {
1150
+ if (typeof json.from != "number" || typeof json.to != "number")
1151
+ throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
1152
+ return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
1153
+ }
1154
+ }
1155
+ Step.jsonID("removeMark", RemoveMarkStep);
1156
+ /**
1157
+ Add a mark to a specific node.
1158
+ */
1159
+ class AddNodeMarkStep extends Step {
1160
+ /**
1161
+ Create a node mark step.
1162
+ */
1163
+ constructor(
1164
+ /**
1165
+ The position of the target node.
1166
+ */
1167
+ pos,
1168
+ /**
1169
+ The mark to add.
1170
+ */
1171
+ mark) {
1172
+ super();
1173
+ this.pos = pos;
1174
+ this.mark = mark;
1175
+ }
1176
+ apply(doc) {
1177
+ let node = doc.nodeAt(this.pos);
1178
+ if (!node)
1179
+ return StepResult.fail("No node at mark step's position");
1180
+ let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
1181
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1182
+ }
1183
+ invert(doc) {
1184
+ let node = doc.nodeAt(this.pos);
1185
+ if (node) {
1186
+ let newSet = this.mark.addToSet(node.marks);
1187
+ if (newSet.length == node.marks.length) {
1188
+ for (let i = 0; i < node.marks.length; i++)
1189
+ if (!node.marks[i].isInSet(newSet))
1190
+ return new AddNodeMarkStep(this.pos, node.marks[i]);
1191
+ return new AddNodeMarkStep(this.pos, this.mark);
1192
+ }
1193
+ }
1194
+ return new RemoveNodeMarkStep(this.pos, this.mark);
1195
+ }
1196
+ map(mapping) {
1197
+ let pos = mapping.mapResult(this.pos, 1);
1198
+ return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
1199
+ }
1200
+ toJSON() {
1201
+ return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1202
+ }
1203
+ /**
1204
+ @internal
1205
+ */
1206
+ static fromJSON(schema, json) {
1207
+ if (typeof json.pos != "number")
1208
+ throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
1209
+ return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1210
+ }
1211
+ }
1212
+ Step.jsonID("addNodeMark", AddNodeMarkStep);
1213
+ /**
1214
+ Remove a mark from a specific node.
1215
+ */
1216
+ class RemoveNodeMarkStep extends Step {
1217
+ /**
1218
+ Create a mark-removing step.
1219
+ */
1220
+ constructor(
1221
+ /**
1222
+ The position of the target node.
1223
+ */
1224
+ pos,
1225
+ /**
1226
+ The mark to remove.
1227
+ */
1228
+ mark) {
1229
+ super();
1230
+ this.pos = pos;
1231
+ this.mark = mark;
1232
+ }
1233
+ apply(doc) {
1234
+ let node = doc.nodeAt(this.pos);
1235
+ if (!node)
1236
+ return StepResult.fail("No node at mark step's position");
1237
+ let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
1238
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1239
+ }
1240
+ invert(doc) {
1241
+ let node = doc.nodeAt(this.pos);
1242
+ if (!node || !this.mark.isInSet(node.marks))
1243
+ return this;
1244
+ return new AddNodeMarkStep(this.pos, this.mark);
1245
+ }
1246
+ map(mapping) {
1247
+ let pos = mapping.mapResult(this.pos, 1);
1248
+ return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
1249
+ }
1250
+ toJSON() {
1251
+ return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1252
+ }
1253
+ /**
1254
+ @internal
1255
+ */
1256
+ static fromJSON(schema, json) {
1257
+ if (typeof json.pos != "number")
1258
+ throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
1259
+ return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1260
+ }
1261
+ }
1262
+ Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
1263
+
1264
+ /**
1265
+ Replace a part of the document with a slice of new content.
1266
+ */
1267
+ class ReplaceStep extends Step {
1268
+ /**
1269
+ The given `slice` should fit the 'gap' between `from` and
1270
+ `to`—the depths must line up, and the surrounding nodes must be
1271
+ able to be joined with the open sides of the slice. When
1272
+ `structure` is true, the step will fail if the content between
1273
+ from and to is not just a sequence of closing and then opening
1274
+ tokens (this is to guard against rebased replace steps
1275
+ overwriting something they weren't supposed to).
1276
+ */
1277
+ constructor(
1278
+ /**
1279
+ The start position of the replaced range.
1280
+ */
1281
+ from,
1282
+ /**
1283
+ The end position of the replaced range.
1284
+ */
1285
+ to,
1286
+ /**
1287
+ The slice to insert.
1288
+ */
1289
+ slice,
1290
+ /**
1291
+ @internal
1292
+ */
1293
+ structure = false) {
1294
+ super();
1295
+ this.from = from;
1296
+ this.to = to;
1297
+ this.slice = slice;
1298
+ this.structure = structure;
1299
+ }
1300
+ apply(doc) {
1301
+ if (this.structure && contentBetween(doc, this.from, this.to))
1302
+ return StepResult.fail("Structure replace would overwrite content");
1303
+ return StepResult.fromReplace(doc, this.from, this.to, this.slice);
1304
+ }
1305
+ getMap() {
1306
+ return new StepMap([this.from, this.to - this.from, this.slice.size]);
1307
+ }
1308
+ invert(doc) {
1309
+ return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
1310
+ }
1311
+ map(mapping) {
1312
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1313
+ if (from.deletedAcross && to.deletedAcross)
1314
+ return null;
1315
+ return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
1316
+ }
1317
+ merge(other) {
1318
+ if (!(other instanceof ReplaceStep) || other.structure || this.structure)
1319
+ return null;
1320
+ if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
1321
+ let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1322
+ : new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
1323
+ return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
1324
+ }
1325
+ else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
1326
+ let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1327
+ : new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
1328
+ return new ReplaceStep(other.from, this.to, slice, this.structure);
1329
+ }
1330
+ else {
1331
+ return null;
1332
+ }
1333
+ }
1334
+ toJSON() {
1335
+ let json = { stepType: "replace", from: this.from, to: this.to };
1336
+ if (this.slice.size)
1337
+ json.slice = this.slice.toJSON();
1338
+ if (this.structure)
1339
+ json.structure = true;
1340
+ return json;
1341
+ }
1342
+ /**
1343
+ @internal
1344
+ */
1345
+ static fromJSON(schema, json) {
1346
+ if (typeof json.from != "number" || typeof json.to != "number")
1347
+ throw new RangeError("Invalid input for ReplaceStep.fromJSON");
1348
+ return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
1349
+ }
1350
+ }
1351
+ Step.jsonID("replace", ReplaceStep);
1352
+ /**
1353
+ Replace a part of the document with a slice of content, but
1354
+ preserve a range of the replaced content by moving it into the
1355
+ slice.
1356
+ */
1357
+ class ReplaceAroundStep extends Step {
1358
+ /**
1359
+ Create a replace-around step with the given range and gap.
1360
+ `insert` should be the point in the slice into which the content
1361
+ of the gap should be moved. `structure` has the same meaning as
1362
+ it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
1363
+ */
1364
+ constructor(
1365
+ /**
1366
+ The start position of the replaced range.
1367
+ */
1368
+ from,
1369
+ /**
1370
+ The end position of the replaced range.
1371
+ */
1372
+ to,
1373
+ /**
1374
+ The start of preserved range.
1375
+ */
1376
+ gapFrom,
1377
+ /**
1378
+ The end of preserved range.
1379
+ */
1380
+ gapTo,
1381
+ /**
1382
+ The slice to insert.
1383
+ */
1384
+ slice,
1385
+ /**
1386
+ The position in the slice where the preserved range should be
1387
+ inserted.
1388
+ */
1389
+ insert,
1390
+ /**
1391
+ @internal
1392
+ */
1393
+ structure = false) {
1394
+ super();
1395
+ this.from = from;
1396
+ this.to = to;
1397
+ this.gapFrom = gapFrom;
1398
+ this.gapTo = gapTo;
1399
+ this.slice = slice;
1400
+ this.insert = insert;
1401
+ this.structure = structure;
1402
+ }
1403
+ apply(doc) {
1404
+ if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
1405
+ contentBetween(doc, this.gapTo, this.to)))
1406
+ return StepResult.fail("Structure gap-replace would overwrite content");
1407
+ let gap = doc.slice(this.gapFrom, this.gapTo);
1408
+ if (gap.openStart || gap.openEnd)
1409
+ return StepResult.fail("Gap is not a flat range");
1410
+ let inserted = this.slice.insertAt(this.insert, gap.content);
1411
+ if (!inserted)
1412
+ return StepResult.fail("Content does not fit in gap");
1413
+ return StepResult.fromReplace(doc, this.from, this.to, inserted);
1414
+ }
1415
+ getMap() {
1416
+ return new StepMap([this.from, this.gapFrom - this.from, this.insert,
1417
+ this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
1418
+ }
1419
+ invert(doc) {
1420
+ let gap = this.gapTo - this.gapFrom;
1421
+ 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);
1422
+ }
1423
+ map(mapping) {
1424
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1425
+ let gapFrom = this.from == this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1);
1426
+ let gapTo = this.to == this.gapTo ? to.pos : mapping.map(this.gapTo, 1);
1427
+ if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
1428
+ return null;
1429
+ return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
1430
+ }
1431
+ toJSON() {
1432
+ let json = { stepType: "replaceAround", from: this.from, to: this.to,
1433
+ gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert };
1434
+ if (this.slice.size)
1435
+ json.slice = this.slice.toJSON();
1436
+ if (this.structure)
1437
+ json.structure = true;
1438
+ return json;
1439
+ }
1440
+ /**
1441
+ @internal
1442
+ */
1443
+ static fromJSON(schema, json) {
1444
+ if (typeof json.from != "number" || typeof json.to != "number" ||
1445
+ typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
1446
+ throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
1447
+ return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
1448
+ }
1449
+ }
1450
+ Step.jsonID("replaceAround", ReplaceAroundStep);
1451
+ function contentBetween(doc, from, to) {
1452
+ let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
1453
+ while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
1454
+ depth--;
1455
+ dist--;
1456
+ }
1457
+ if (dist > 0) {
1458
+ let next = $from.node(depth).maybeChild($from.indexAfter(depth));
1459
+ while (dist > 0) {
1460
+ if (!next || next.isLeaf)
1461
+ return true;
1462
+ next = next.firstChild;
1463
+ dist--;
1464
+ }
1465
+ }
1466
+ return false;
1467
+ }
1468
+
1469
+ function canCut(node, start, end) {
1470
+ return (start == 0 || node.canReplace(start, node.childCount)) &&
1471
+ (end == node.childCount || node.canReplace(0, end));
1472
+ }
1473
+ /**
1474
+ Try to find a target depth to which the content in the given range
1475
+ can be lifted. Will not go across
1476
+ [isolating](https://prosemirror.net/docs/ref/#model.NodeSpec.isolating) parent nodes.
1477
+ */
1478
+ function liftTarget(range) {
1479
+ let parent = range.parent;
1480
+ let content = parent.content.cutByIndex(range.startIndex, range.endIndex);
1481
+ for (let depth = range.depth, contentBefore = 0, contentAfter = 0;; --depth) {
1482
+ let node = range.$from.node(depth);
1483
+ let index = range.$from.index(depth) + contentBefore, endIndex = range.$to.indexAfter(depth) - contentAfter;
1484
+ if (depth < range.depth && node.canReplace(index, endIndex, content))
1485
+ return depth;
1486
+ if (depth == 0 || node.type.spec.isolating || !canCut(node, index, endIndex))
1487
+ break;
1488
+ if (index)
1489
+ contentBefore = 1;
1490
+ if (endIndex < node.childCount)
1491
+ contentAfter = 1;
1492
+ }
1493
+ return null;
1494
+ }
1495
+ /**
1496
+ Check whether splitting at the given position is allowed.
1497
+ */
1498
+ function canSplit(doc, pos, depth = 1, typesAfter) {
1499
+ let $pos = doc.resolve(pos), base = $pos.depth - depth;
1500
+ let innerType = (typesAfter && typesAfter[typesAfter.length - 1]) || $pos.parent;
1501
+ if (base < 0 || $pos.parent.type.spec.isolating ||
1502
+ !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) ||
1503
+ !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount)))
1504
+ return false;
1505
+ for (let d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) {
1506
+ let node = $pos.node(d), index = $pos.index(d);
1507
+ if (node.type.spec.isolating)
1508
+ return false;
1509
+ let rest = node.content.cutByIndex(index, node.childCount);
1510
+ let overrideChild = typesAfter && typesAfter[i + 1];
1511
+ if (overrideChild)
1512
+ rest = rest.replaceChild(0, overrideChild.type.create(overrideChild.attrs));
1513
+ let after = (typesAfter && typesAfter[i]) || node;
1514
+ if (!node.canReplace(index + 1, node.childCount) || !after.type.validContent(rest))
1515
+ return false;
1516
+ }
1517
+ let index = $pos.indexAfter(base);
1518
+ let baseType = typesAfter && typesAfter[0];
1519
+ return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type);
1520
+ }
1521
+ /**
1522
+ Test whether the blocks before and after a given position can be
1523
+ joined.
1524
+ */
1525
+ function canJoin(doc, pos) {
1526
+ let $pos = doc.resolve(pos), index = $pos.index();
1527
+ return joinable($pos.nodeBefore, $pos.nodeAfter) &&
1528
+ $pos.parent.canReplace(index, index + 1);
1529
+ }
1530
+ function canAppendWithSubstitutedLinebreaks(a, b) {
1531
+ if (!b.content.size)
1532
+ a.type.compatibleContent(b.type);
1533
+ let match = a.contentMatchAt(a.childCount);
1534
+ let { linebreakReplacement } = a.type.schema;
1535
+ for (let i = 0; i < b.childCount; i++) {
1536
+ let child = b.child(i);
1537
+ let type = child.type == linebreakReplacement ? a.type.schema.nodes.text : child.type;
1538
+ match = match.matchType(type);
1539
+ if (!match)
1540
+ return false;
1541
+ if (!a.type.allowsMarks(child.marks))
1542
+ return false;
1543
+ }
1544
+ return match.validEnd;
1545
+ }
1546
+ function joinable(a, b) {
1547
+ return !!(a && b && !a.isLeaf && canAppendWithSubstitutedLinebreaks(a, b));
1548
+ }
1549
+
1550
+ /**
1551
+ ‘Fit’ a slice into a given position in the document, producing a
1552
+ [step](https://prosemirror.net/docs/ref/#transform.Step) that inserts it. Will return null if
1553
+ there's no meaningful way to insert the slice here, or inserting it
1554
+ would be a no-op (an empty slice over an empty range).
1555
+ */
1556
+ function replaceStep(doc, from, to = from, slice = Slice.empty) {
1557
+ if (from == to && !slice.size)
1558
+ return null;
1559
+ let $from = doc.resolve(from), $to = doc.resolve(to);
1560
+ // Optimization -- avoid work if it's obvious that it's not needed.
1561
+ if (fitsTrivially($from, $to, slice))
1562
+ return new ReplaceStep(from, to, slice);
1563
+ return new Fitter($from, $to, slice).fit();
1564
+ }
1565
+ function fitsTrivially($from, $to, slice) {
1566
+ return !slice.openStart && !slice.openEnd && $from.start() == $to.start() &&
1567
+ $from.parent.canReplace($from.index(), $to.index(), slice.content);
1568
+ }
1569
+ // Algorithm for 'placing' the elements of a slice into a gap:
1570
+ //
1571
+ // We consider the content of each node that is open to the left to be
1572
+ // independently placeable. I.e. in <p("foo"), p("bar")>, when the
1573
+ // paragraph on the left is open, "foo" can be placed (somewhere on
1574
+ // the left side of the replacement gap) independently from p("bar").
1575
+ //
1576
+ // This class tracks the state of the placement progress in the
1577
+ // following properties:
1578
+ //
1579
+ // - `frontier` holds a stack of `{type, match}` objects that
1580
+ // represent the open side of the replacement. It starts at
1581
+ // `$from`, then moves forward as content is placed, and is finally
1582
+ // reconciled with `$to`.
1583
+ //
1584
+ // - `unplaced` is a slice that represents the content that hasn't
1585
+ // been placed yet.
1586
+ //
1587
+ // - `placed` is a fragment of placed content. Its open-start value
1588
+ // is implicit in `$from`, and its open-end value in `frontier`.
1589
+ class Fitter {
1590
+ constructor($from, $to, unplaced) {
1591
+ this.$from = $from;
1592
+ this.$to = $to;
1593
+ this.unplaced = unplaced;
1594
+ this.frontier = [];
1595
+ this.placed = Fragment.empty;
1596
+ for (let i = 0; i <= $from.depth; i++) {
1597
+ let node = $from.node(i);
1598
+ this.frontier.push({
1599
+ type: node.type,
1600
+ match: node.contentMatchAt($from.indexAfter(i))
1601
+ });
1602
+ }
1603
+ for (let i = $from.depth; i > 0; i--)
1604
+ this.placed = Fragment.from($from.node(i).copy(this.placed));
1605
+ }
1606
+ get depth() { return this.frontier.length - 1; }
1607
+ fit() {
1608
+ // As long as there's unplaced content, try to place some of it.
1609
+ // If that fails, either increase the open score of the unplaced
1610
+ // slice, or drop nodes from it, and then try again.
1611
+ while (this.unplaced.size) {
1612
+ let fit = this.findFittable();
1613
+ if (fit)
1614
+ this.placeNodes(fit);
1615
+ else
1616
+ this.openMore() || this.dropNode();
1617
+ }
1618
+ // When there's inline content directly after the frontier _and_
1619
+ // directly after `this.$to`, we must generate a `ReplaceAround`
1620
+ // step that pulls that content into the node after the frontier.
1621
+ // That means the fitting must be done to the end of the textblock
1622
+ // node after `this.$to`, not `this.$to` itself.
1623
+ let moveInline = this.mustMoveInline(), placedSize = this.placed.size - this.depth - this.$from.depth;
1624
+ let $from = this.$from, $to = this.close(moveInline < 0 ? this.$to : $from.doc.resolve(moveInline));
1625
+ if (!$to)
1626
+ return null;
1627
+ // If closing to `$to` succeeded, create a step
1628
+ let content = this.placed, openStart = $from.depth, openEnd = $to.depth;
1629
+ while (openStart && openEnd && content.childCount == 1) { // Normalize by dropping open parent nodes
1630
+ content = content.firstChild.content;
1631
+ openStart--;
1632
+ openEnd--;
1633
+ }
1634
+ let slice = new Slice(content, openStart, openEnd);
1635
+ if (moveInline > -1)
1636
+ return new ReplaceAroundStep($from.pos, moveInline, this.$to.pos, this.$to.end(), slice, placedSize);
1637
+ if (slice.size || $from.pos != this.$to.pos) // Don't generate no-op steps
1638
+ return new ReplaceStep($from.pos, $to.pos, slice);
1639
+ return null;
1640
+ }
1641
+ // Find a position on the start spine of `this.unplaced` that has
1642
+ // content that can be moved somewhere on the frontier. Returns two
1643
+ // depths, one for the slice and one for the frontier.
1644
+ findFittable() {
1645
+ let startDepth = this.unplaced.openStart;
1646
+ for (let cur = this.unplaced.content, d = 0, openEnd = this.unplaced.openEnd; d < startDepth; d++) {
1647
+ let node = cur.firstChild;
1648
+ if (cur.childCount > 1)
1649
+ openEnd = 0;
1650
+ if (node.type.spec.isolating && openEnd <= d) {
1651
+ startDepth = d;
1652
+ break;
1653
+ }
1654
+ cur = node.content;
1655
+ }
1656
+ // Only try wrapping nodes (pass 2) after finding a place without
1657
+ // wrapping failed.
1658
+ for (let pass = 1; pass <= 2; pass++) {
1659
+ for (let sliceDepth = pass == 1 ? startDepth : this.unplaced.openStart; sliceDepth >= 0; sliceDepth--) {
1660
+ let fragment, parent = null;
1661
+ if (sliceDepth) {
1662
+ parent = contentAt(this.unplaced.content, sliceDepth - 1).firstChild;
1663
+ fragment = parent.content;
1664
+ }
1665
+ else {
1666
+ fragment = this.unplaced.content;
1667
+ }
1668
+ let first = fragment.firstChild;
1669
+ for (let frontierDepth = this.depth; frontierDepth >= 0; frontierDepth--) {
1670
+ let { type, match } = this.frontier[frontierDepth], wrap, inject = null;
1671
+ // In pass 1, if the next node matches, or there is no next
1672
+ // node but the parents look compatible, we've found a
1673
+ // place.
1674
+ if (pass == 1 && (first ? match.matchType(first.type) || (inject = match.fillBefore(Fragment.from(first), false))
1675
+ : parent && type.compatibleContent(parent.type)))
1676
+ return { sliceDepth, frontierDepth, parent, inject };
1677
+ // In pass 2, look for a set of wrapping nodes that make
1678
+ // `first` fit here.
1679
+ else if (pass == 2 && first && (wrap = match.findWrapping(first.type)))
1680
+ return { sliceDepth, frontierDepth, parent, wrap };
1681
+ // Don't continue looking further up if the parent node
1682
+ // would fit here.
1683
+ if (parent && match.matchType(parent.type))
1684
+ break;
1685
+ }
1686
+ }
1687
+ }
1688
+ }
1689
+ openMore() {
1690
+ let { content, openStart, openEnd } = this.unplaced;
1691
+ let inner = contentAt(content, openStart);
1692
+ if (!inner.childCount || inner.firstChild.isLeaf)
1693
+ return false;
1694
+ this.unplaced = new Slice(content, openStart + 1, Math.max(openEnd, inner.size + openStart >= content.size - openEnd ? openStart + 1 : 0));
1695
+ return true;
1696
+ }
1697
+ dropNode() {
1698
+ let { content, openStart, openEnd } = this.unplaced;
1699
+ let inner = contentAt(content, openStart);
1700
+ if (inner.childCount <= 1 && openStart > 0) {
1701
+ let openAtEnd = content.size - openStart <= openStart + inner.size;
1702
+ this.unplaced = new Slice(dropFromFragment(content, openStart - 1, 1), openStart - 1, openAtEnd ? openStart - 1 : openEnd);
1703
+ }
1704
+ else {
1705
+ this.unplaced = new Slice(dropFromFragment(content, openStart, 1), openStart, openEnd);
1706
+ }
1707
+ }
1708
+ // Move content from the unplaced slice at `sliceDepth` to the
1709
+ // frontier node at `frontierDepth`. Close that frontier node when
1710
+ // applicable.
1711
+ placeNodes({ sliceDepth, frontierDepth, parent, inject, wrap }) {
1712
+ while (this.depth > frontierDepth)
1713
+ this.closeFrontierNode();
1714
+ if (wrap)
1715
+ for (let i = 0; i < wrap.length; i++)
1716
+ this.openFrontierNode(wrap[i]);
1717
+ let slice = this.unplaced, fragment = parent ? parent.content : slice.content;
1718
+ let openStart = slice.openStart - sliceDepth;
1719
+ let taken = 0, add = [];
1720
+ let { match, type } = this.frontier[frontierDepth];
1721
+ if (inject) {
1722
+ for (let i = 0; i < inject.childCount; i++)
1723
+ add.push(inject.child(i));
1724
+ match = match.matchFragment(inject);
1725
+ }
1726
+ // Computes the amount of (end) open nodes at the end of the
1727
+ // fragment. When 0, the parent is open, but no more. When
1728
+ // negative, nothing is open.
1729
+ let openEndCount = (fragment.size + sliceDepth) - (slice.content.size - slice.openEnd);
1730
+ // Scan over the fragment, fitting as many child nodes as
1731
+ // possible.
1732
+ while (taken < fragment.childCount) {
1733
+ let next = fragment.child(taken), matches = match.matchType(next.type);
1734
+ if (!matches)
1735
+ break;
1736
+ taken++;
1737
+ if (taken > 1 || openStart == 0 || next.content.size) { // Drop empty open nodes
1738
+ match = matches;
1739
+ add.push(closeNodeStart(next.mark(type.allowedMarks(next.marks)), taken == 1 ? openStart : 0, taken == fragment.childCount ? openEndCount : -1));
1740
+ }
1741
+ }
1742
+ let toEnd = taken == fragment.childCount;
1743
+ if (!toEnd)
1744
+ openEndCount = -1;
1745
+ this.placed = addToFragment(this.placed, frontierDepth, Fragment.from(add));
1746
+ this.frontier[frontierDepth].match = match;
1747
+ // If the parent types match, and the entire node was moved, and
1748
+ // it's not open, close this frontier node right away.
1749
+ if (toEnd && openEndCount < 0 && parent && parent.type == this.frontier[this.depth].type && this.frontier.length > 1)
1750
+ this.closeFrontierNode();
1751
+ // Add new frontier nodes for any open nodes at the end.
1752
+ for (let i = 0, cur = fragment; i < openEndCount; i++) {
1753
+ let node = cur.lastChild;
1754
+ this.frontier.push({ type: node.type, match: node.contentMatchAt(node.childCount) });
1755
+ cur = node.content;
1756
+ }
1757
+ // Update `this.unplaced`. Drop the entire node from which we
1758
+ // placed it we got to its end, otherwise just drop the placed
1759
+ // nodes.
1760
+ this.unplaced = !toEnd ? new Slice(dropFromFragment(slice.content, sliceDepth, taken), slice.openStart, slice.openEnd)
1761
+ : sliceDepth == 0 ? Slice.empty
1762
+ : new Slice(dropFromFragment(slice.content, sliceDepth - 1, 1), sliceDepth - 1, openEndCount < 0 ? slice.openEnd : sliceDepth - 1);
1763
+ }
1764
+ mustMoveInline() {
1765
+ if (!this.$to.parent.isTextblock)
1766
+ return -1;
1767
+ let top = this.frontier[this.depth], level;
1768
+ if (!top.type.isTextblock || !contentAfterFits(this.$to, this.$to.depth, top.type, top.match, false) ||
1769
+ (this.$to.depth == this.depth && (level = this.findCloseLevel(this.$to)) && level.depth == this.depth))
1770
+ return -1;
1771
+ let { depth } = this.$to, after = this.$to.after(depth);
1772
+ while (depth > 1 && after == this.$to.end(--depth))
1773
+ ++after;
1774
+ return after;
1775
+ }
1776
+ findCloseLevel($to) {
1777
+ scan: for (let i = Math.min(this.depth, $to.depth); i >= 0; i--) {
1778
+ let { match, type } = this.frontier[i];
1779
+ let dropInner = i < $to.depth && $to.end(i + 1) == $to.pos + ($to.depth - (i + 1));
1780
+ let fit = contentAfterFits($to, i, type, match, dropInner);
1781
+ if (!fit)
1782
+ continue;
1783
+ for (let d = i - 1; d >= 0; d--) {
1784
+ let { match, type } = this.frontier[d];
1785
+ let matches = contentAfterFits($to, d, type, match, true);
1786
+ if (!matches || matches.childCount)
1787
+ continue scan;
1788
+ }
1789
+ return { depth: i, fit, move: dropInner ? $to.doc.resolve($to.after(i + 1)) : $to };
1790
+ }
1791
+ }
1792
+ close($to) {
1793
+ let close = this.findCloseLevel($to);
1794
+ if (!close)
1795
+ return null;
1796
+ while (this.depth > close.depth)
1797
+ this.closeFrontierNode();
1798
+ if (close.fit.childCount)
1799
+ this.placed = addToFragment(this.placed, close.depth, close.fit);
1800
+ $to = close.move;
1801
+ for (let d = close.depth + 1; d <= $to.depth; d++) {
1802
+ let node = $to.node(d), add = node.type.contentMatch.fillBefore(node.content, true, $to.index(d));
1803
+ this.openFrontierNode(node.type, node.attrs, add);
1804
+ }
1805
+ return $to;
1806
+ }
1807
+ openFrontierNode(type, attrs = null, content) {
1808
+ let top = this.frontier[this.depth];
1809
+ top.match = top.match.matchType(type);
1810
+ this.placed = addToFragment(this.placed, this.depth, Fragment.from(type.create(attrs, content)));
1811
+ this.frontier.push({ type, match: type.contentMatch });
1812
+ }
1813
+ closeFrontierNode() {
1814
+ let open = this.frontier.pop();
1815
+ let add = open.match.fillBefore(Fragment.empty, true);
1816
+ if (add.childCount)
1817
+ this.placed = addToFragment(this.placed, this.frontier.length, add);
1818
+ }
1819
+ }
1820
+ function dropFromFragment(fragment, depth, count) {
1821
+ if (depth == 0)
1822
+ return fragment.cutByIndex(count, fragment.childCount);
1823
+ return fragment.replaceChild(0, fragment.firstChild.copy(dropFromFragment(fragment.firstChild.content, depth - 1, count)));
1824
+ }
1825
+ function addToFragment(fragment, depth, content) {
1826
+ if (depth == 0)
1827
+ return fragment.append(content);
1828
+ return fragment.replaceChild(fragment.childCount - 1, fragment.lastChild.copy(addToFragment(fragment.lastChild.content, depth - 1, content)));
1829
+ }
1830
+ function contentAt(fragment, depth) {
1831
+ for (let i = 0; i < depth; i++)
1832
+ fragment = fragment.firstChild.content;
1833
+ return fragment;
1834
+ }
1835
+ function closeNodeStart(node, openStart, openEnd) {
1836
+ if (openStart <= 0)
1837
+ return node;
1838
+ let frag = node.content;
1839
+ if (openStart > 1)
1840
+ frag = frag.replaceChild(0, closeNodeStart(frag.firstChild, openStart - 1, frag.childCount == 1 ? openEnd - 1 : 0));
1841
+ if (openStart > 0) {
1842
+ frag = node.type.contentMatch.fillBefore(frag).append(frag);
1843
+ if (openEnd <= 0)
1844
+ frag = frag.append(node.type.contentMatch.matchFragment(frag).fillBefore(Fragment.empty, true));
1845
+ }
1846
+ return node.copy(frag);
1847
+ }
1848
+ function contentAfterFits($to, depth, type, match, open) {
1849
+ let node = $to.node(depth), index = open ? $to.indexAfter(depth) : $to.index(depth);
1850
+ if (index == node.childCount && !type.compatibleContent(node.type))
1851
+ return null;
1852
+ let fit = match.fillBefore(node.content, true, index);
1853
+ return fit && !invalidMarks(type, node.content, index) ? fit : null;
1854
+ }
1855
+ function invalidMarks(type, fragment, start) {
1856
+ for (let i = start; i < fragment.childCount; i++)
1857
+ if (!type.allowsMarks(fragment.child(i).marks))
1858
+ return true;
1859
+ return false;
1860
+ }
1861
+
1862
+ /**
1863
+ Update an attribute in a specific node.
1864
+ */
1865
+ class AttrStep extends Step {
1866
+ /**
1867
+ Construct an attribute step.
1868
+ */
1869
+ constructor(
1870
+ /**
1871
+ The position of the target node.
1872
+ */
1873
+ pos,
1874
+ /**
1875
+ The attribute to set.
1876
+ */
1877
+ attr,
1878
+ // The attribute's new value.
1879
+ value) {
1880
+ super();
1881
+ this.pos = pos;
1882
+ this.attr = attr;
1883
+ this.value = value;
1884
+ }
1885
+ apply(doc) {
1886
+ let node = doc.nodeAt(this.pos);
1887
+ if (!node)
1888
+ return StepResult.fail("No node at attribute step's position");
1889
+ let attrs = Object.create(null);
1890
+ for (let name in node.attrs)
1891
+ attrs[name] = node.attrs[name];
1892
+ attrs[this.attr] = this.value;
1893
+ let updated = node.type.create(attrs, null, node.marks);
1894
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1895
+ }
1896
+ getMap() {
1897
+ return StepMap.empty;
1898
+ }
1899
+ invert(doc) {
1900
+ return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1901
+ }
1902
+ map(mapping) {
1903
+ let pos = mapping.mapResult(this.pos, 1);
1904
+ return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1905
+ }
1906
+ toJSON() {
1907
+ return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
1908
+ }
1909
+ static fromJSON(schema, json) {
1910
+ if (typeof json.pos != "number" || typeof json.attr != "string")
1911
+ throw new RangeError("Invalid input for AttrStep.fromJSON");
1912
+ return new AttrStep(json.pos, json.attr, json.value);
1913
+ }
1914
+ }
1915
+ Step.jsonID("attr", AttrStep);
1916
+ /**
1917
+ Update an attribute in the doc node.
1918
+ */
1919
+ class DocAttrStep extends Step {
1920
+ /**
1921
+ Construct an attribute step.
1922
+ */
1923
+ constructor(
1924
+ /**
1925
+ The attribute to set.
1926
+ */
1927
+ attr,
1928
+ // The attribute's new value.
1929
+ value) {
1930
+ super();
1931
+ this.attr = attr;
1932
+ this.value = value;
1933
+ }
1934
+ apply(doc) {
1935
+ let attrs = Object.create(null);
1936
+ for (let name in doc.attrs)
1937
+ attrs[name] = doc.attrs[name];
1938
+ attrs[this.attr] = this.value;
1939
+ let updated = doc.type.create(attrs, doc.content, doc.marks);
1940
+ return StepResult.ok(updated);
1941
+ }
1942
+ getMap() {
1943
+ return StepMap.empty;
1944
+ }
1945
+ invert(doc) {
1946
+ return new DocAttrStep(this.attr, doc.attrs[this.attr]);
1947
+ }
1948
+ map(mapping) {
1949
+ return this;
1950
+ }
1951
+ toJSON() {
1952
+ return { stepType: "docAttr", attr: this.attr, value: this.value };
1953
+ }
1954
+ static fromJSON(schema, json) {
1955
+ if (typeof json.attr != "string")
1956
+ throw new RangeError("Invalid input for DocAttrStep.fromJSON");
1957
+ return new DocAttrStep(json.attr, json.value);
1958
+ }
1959
+ }
1960
+ Step.jsonID("docAttr", DocAttrStep);
1961
+
1962
+ /**
1963
+ @internal
1964
+ */
1965
+ let TransformError = class extends Error {
1966
+ };
1967
+ TransformError = function TransformError(message) {
1968
+ let err = Error.call(this, message);
1969
+ err.__proto__ = TransformError.prototype;
1970
+ return err;
1971
+ };
1972
+ TransformError.prototype = Object.create(Error.prototype);
1973
+ TransformError.prototype.constructor = TransformError;
1974
+ TransformError.prototype.name = "TransformError";
1975
+
1976
+ const classesById = Object.create(null);
1977
+ /**
1978
+ Superclass for editor selections. Every selection type should
1979
+ extend this. Should not be instantiated directly.
1980
+ */
1981
+ class Selection {
1982
+ /**
1983
+ Initialize a selection with the head and anchor and ranges. If no
1984
+ ranges are given, constructs a single range across `$anchor` and
1985
+ `$head`.
1986
+ */
1987
+ constructor(
1988
+ /**
1989
+ The resolved anchor of the selection (the side that stays in
1990
+ place when the selection is modified).
1991
+ */
1992
+ $anchor,
1993
+ /**
1994
+ The resolved head of the selection (the side that moves when
1995
+ the selection is modified).
1996
+ */
1997
+ $head, ranges) {
1998
+ this.$anchor = $anchor;
1999
+ this.$head = $head;
2000
+ this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
2001
+ }
2002
+ /**
2003
+ The selection's anchor, as an unresolved position.
2004
+ */
2005
+ get anchor() { return this.$anchor.pos; }
2006
+ /**
2007
+ The selection's head.
2008
+ */
2009
+ get head() { return this.$head.pos; }
2010
+ /**
2011
+ The lower bound of the selection's main range.
2012
+ */
2013
+ get from() { return this.$from.pos; }
2014
+ /**
2015
+ The upper bound of the selection's main range.
2016
+ */
2017
+ get to() { return this.$to.pos; }
2018
+ /**
2019
+ The resolved lower bound of the selection's main range.
2020
+ */
2021
+ get $from() {
2022
+ return this.ranges[0].$from;
2023
+ }
2024
+ /**
2025
+ The resolved upper bound of the selection's main range.
2026
+ */
2027
+ get $to() {
2028
+ return this.ranges[0].$to;
2029
+ }
2030
+ /**
2031
+ Indicates whether the selection contains any content.
2032
+ */
2033
+ get empty() {
2034
+ let ranges = this.ranges;
2035
+ for (let i = 0; i < ranges.length; i++)
2036
+ if (ranges[i].$from.pos != ranges[i].$to.pos)
2037
+ return false;
2038
+ return true;
2039
+ }
2040
+ /**
2041
+ Get the content of this selection as a slice.
2042
+ */
2043
+ content() {
2044
+ return this.$from.doc.slice(this.from, this.to, true);
2045
+ }
2046
+ /**
2047
+ Replace the selection with a slice or, if no slice is given,
2048
+ delete the selection. Will append to the given transaction.
2049
+ */
2050
+ replace(tr, content = Slice.empty) {
2051
+ // Put the new selection at the position after the inserted
2052
+ // content. When that ended in an inline node, search backwards,
2053
+ // to get the position after that node. If not, search forward.
2054
+ let lastNode = content.content.lastChild, lastParent = null;
2055
+ for (let i = 0; i < content.openEnd; i++) {
2056
+ lastParent = lastNode;
2057
+ lastNode = lastNode.lastChild;
2058
+ }
2059
+ let mapFrom = tr.steps.length, ranges = this.ranges;
2060
+ for (let i = 0; i < ranges.length; i++) {
2061
+ let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
2062
+ tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
2063
+ if (i == 0)
2064
+ selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
2065
+ }
2066
+ }
2067
+ /**
2068
+ Replace the selection with the given node, appending the changes
2069
+ to the given transaction.
2070
+ */
2071
+ replaceWith(tr, node) {
2072
+ let mapFrom = tr.steps.length, ranges = this.ranges;
2073
+ for (let i = 0; i < ranges.length; i++) {
2074
+ let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
2075
+ let from = mapping.map($from.pos), to = mapping.map($to.pos);
2076
+ if (i) {
2077
+ tr.deleteRange(from, to);
2078
+ }
2079
+ else {
2080
+ tr.replaceRangeWith(from, to, node);
2081
+ selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
2082
+ }
2083
+ }
2084
+ }
2085
+ /**
2086
+ Find a valid cursor or leaf node selection starting at the given
2087
+ position and searching back if `dir` is negative, and forward if
2088
+ positive. When `textOnly` is true, only consider cursor
2089
+ selections. Will return null when no valid selection position is
2090
+ found.
2091
+ */
2092
+ static findFrom($pos, dir, textOnly = false) {
2093
+ let inner = $pos.parent.inlineContent ? new TextSelection($pos)
2094
+ : findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
2095
+ if (inner)
2096
+ return inner;
2097
+ for (let depth = $pos.depth - 1; depth >= 0; depth--) {
2098
+ let found = dir < 0
2099
+ ? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly)
2100
+ : findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
2101
+ if (found)
2102
+ return found;
2103
+ }
2104
+ return null;
2105
+ }
2106
+ /**
2107
+ Find a valid cursor or leaf node selection near the given
2108
+ position. Searches forward first by default, but if `bias` is
2109
+ negative, it will search backwards first.
2110
+ */
2111
+ static near($pos, bias = 1) {
2112
+ return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
2113
+ }
2114
+ /**
2115
+ Find the cursor or leaf node selection closest to the start of
2116
+ the given document. Will return an
2117
+ [`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
2118
+ exists.
2119
+ */
2120
+ static atStart(doc) {
2121
+ return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
2122
+ }
2123
+ /**
2124
+ Find the cursor or leaf node selection closest to the end of the
2125
+ given document.
2126
+ */
2127
+ static atEnd(doc) {
2128
+ return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
2129
+ }
2130
+ /**
2131
+ Deserialize the JSON representation of a selection. Must be
2132
+ implemented for custom classes (as a static class method).
2133
+ */
2134
+ static fromJSON(doc, json) {
2135
+ if (!json || !json.type)
2136
+ throw new RangeError("Invalid input for Selection.fromJSON");
2137
+ let cls = classesById[json.type];
2138
+ if (!cls)
2139
+ throw new RangeError(`No selection type ${json.type} defined`);
2140
+ return cls.fromJSON(doc, json);
2141
+ }
2142
+ /**
2143
+ To be able to deserialize selections from JSON, custom selection
2144
+ classes must register themselves with an ID string, so that they
2145
+ can be disambiguated. Try to pick something that's unlikely to
2146
+ clash with classes from other modules.
2147
+ */
2148
+ static jsonID(id, selectionClass) {
2149
+ if (id in classesById)
2150
+ throw new RangeError("Duplicate use of selection JSON ID " + id);
2151
+ classesById[id] = selectionClass;
2152
+ selectionClass.prototype.jsonID = id;
2153
+ return selectionClass;
2154
+ }
2155
+ /**
2156
+ Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
2157
+ which is a value that can be mapped without having access to a
2158
+ current document, and later resolved to a real selection for a
2159
+ given document again. (This is used mostly by the history to
2160
+ track and restore old selections.) The default implementation of
2161
+ this method just converts the selection to a text selection and
2162
+ returns the bookmark for that.
2163
+ */
2164
+ getBookmark() {
2165
+ return TextSelection.between(this.$anchor, this.$head).getBookmark();
2166
+ }
2167
+ }
2168
+ Selection.prototype.visible = true;
2169
+ /**
2170
+ Represents a selected range in a document.
2171
+ */
2172
+ class SelectionRange {
2173
+ /**
2174
+ Create a range.
2175
+ */
2176
+ constructor(
2177
+ /**
2178
+ The lower bound of the range.
2179
+ */
2180
+ $from,
2181
+ /**
2182
+ The upper bound of the range.
2183
+ */
2184
+ $to) {
2185
+ this.$from = $from;
2186
+ this.$to = $to;
2187
+ }
2188
+ }
2189
+ let warnedAboutTextSelection = false;
2190
+ function checkTextSelection($pos) {
2191
+ if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
2192
+ warnedAboutTextSelection = true;
2193
+ console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
2194
+ }
2195
+ }
2196
+ /**
2197
+ A text selection represents a classical editor selection, with a
2198
+ head (the moving side) and anchor (immobile side), both of which
2199
+ point into textblock nodes. It can be empty (a regular cursor
2200
+ position).
2201
+ */
2202
+ class TextSelection extends Selection {
2203
+ /**
2204
+ Construct a text selection between the given points.
2205
+ */
2206
+ constructor($anchor, $head = $anchor) {
2207
+ checkTextSelection($anchor);
2208
+ checkTextSelection($head);
2209
+ super($anchor, $head);
2210
+ }
2211
+ /**
2212
+ Returns a resolved position if this is a cursor selection (an
2213
+ empty text selection), and null otherwise.
2214
+ */
2215
+ get $cursor() { return this.$anchor.pos == this.$head.pos ? this.$head : null; }
2216
+ map(doc, mapping) {
2217
+ let $head = doc.resolve(mapping.map(this.head));
2218
+ if (!$head.parent.inlineContent)
2219
+ return Selection.near($head);
2220
+ let $anchor = doc.resolve(mapping.map(this.anchor));
2221
+ return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
2222
+ }
2223
+ replace(tr, content = Slice.empty) {
2224
+ super.replace(tr, content);
2225
+ if (content == Slice.empty) {
2226
+ let marks = this.$from.marksAcross(this.$to);
2227
+ if (marks)
2228
+ tr.ensureMarks(marks);
2229
+ }
2230
+ }
2231
+ eq(other) {
2232
+ return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
2233
+ }
2234
+ getBookmark() {
2235
+ return new TextBookmark(this.anchor, this.head);
2236
+ }
2237
+ toJSON() {
2238
+ return { type: "text", anchor: this.anchor, head: this.head };
2239
+ }
2240
+ /**
2241
+ @internal
2242
+ */
2243
+ static fromJSON(doc, json) {
2244
+ if (typeof json.anchor != "number" || typeof json.head != "number")
2245
+ throw new RangeError("Invalid input for TextSelection.fromJSON");
2246
+ return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
2247
+ }
2248
+ /**
2249
+ Create a text selection from non-resolved positions.
2250
+ */
2251
+ static create(doc, anchor, head = anchor) {
2252
+ let $anchor = doc.resolve(anchor);
2253
+ return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
2254
+ }
2255
+ /**
2256
+ Return a text selection that spans the given positions or, if
2257
+ they aren't text positions, find a text selection near them.
2258
+ `bias` determines whether the method searches forward (default)
2259
+ or backwards (negative number) first. Will fall back to calling
2260
+ [`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
2261
+ doesn't contain a valid text position.
2262
+ */
2263
+ static between($anchor, $head, bias) {
2264
+ let dPos = $anchor.pos - $head.pos;
2265
+ if (!bias || dPos)
2266
+ bias = dPos >= 0 ? 1 : -1;
2267
+ if (!$head.parent.inlineContent) {
2268
+ let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
2269
+ if (found)
2270
+ $head = found.$head;
2271
+ else
2272
+ return Selection.near($head, bias);
2273
+ }
2274
+ if (!$anchor.parent.inlineContent) {
2275
+ if (dPos == 0) {
2276
+ $anchor = $head;
2277
+ }
2278
+ else {
2279
+ $anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
2280
+ if (($anchor.pos < $head.pos) != (dPos < 0))
2281
+ $anchor = $head;
2282
+ }
2283
+ }
2284
+ return new TextSelection($anchor, $head);
2285
+ }
2286
+ }
2287
+ Selection.jsonID("text", TextSelection);
2288
+ class TextBookmark {
2289
+ constructor(anchor, head) {
2290
+ this.anchor = anchor;
2291
+ this.head = head;
2292
+ }
2293
+ map(mapping) {
2294
+ return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
2295
+ }
2296
+ resolve(doc) {
2297
+ return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
2298
+ }
2299
+ }
2300
+ /**
2301
+ A node selection is a selection that points at a single node. All
2302
+ nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
2303
+ target of a node selection. In such a selection, `from` and `to`
2304
+ point directly before and after the selected node, `anchor` equals
2305
+ `from`, and `head` equals `to`..
2306
+ */
2307
+ class NodeSelection extends Selection {
2308
+ /**
2309
+ Create a node selection. Does not verify the validity of its
2310
+ argument.
2311
+ */
2312
+ constructor($pos) {
2313
+ let node = $pos.nodeAfter;
2314
+ let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
2315
+ super($pos, $end);
2316
+ this.node = node;
2317
+ }
2318
+ map(doc, mapping) {
2319
+ let { deleted, pos } = mapping.mapResult(this.anchor);
2320
+ let $pos = doc.resolve(pos);
2321
+ if (deleted)
2322
+ return Selection.near($pos);
2323
+ return new NodeSelection($pos);
2324
+ }
2325
+ content() {
2326
+ return new Slice(Fragment.from(this.node), 0, 0);
2327
+ }
2328
+ eq(other) {
2329
+ return other instanceof NodeSelection && other.anchor == this.anchor;
2330
+ }
2331
+ toJSON() {
2332
+ return { type: "node", anchor: this.anchor };
2333
+ }
2334
+ getBookmark() { return new NodeBookmark(this.anchor); }
2335
+ /**
2336
+ @internal
2337
+ */
2338
+ static fromJSON(doc, json) {
2339
+ if (typeof json.anchor != "number")
2340
+ throw new RangeError("Invalid input for NodeSelection.fromJSON");
2341
+ return new NodeSelection(doc.resolve(json.anchor));
2342
+ }
2343
+ /**
2344
+ Create a node selection from non-resolved positions.
2345
+ */
2346
+ static create(doc, from) {
2347
+ return new NodeSelection(doc.resolve(from));
2348
+ }
2349
+ /**
2350
+ Determines whether the given node may be selected as a node
2351
+ selection.
2352
+ */
2353
+ static isSelectable(node) {
2354
+ return !node.isText && node.type.spec.selectable !== false;
2355
+ }
2356
+ }
2357
+ NodeSelection.prototype.visible = false;
2358
+ Selection.jsonID("node", NodeSelection);
2359
+ class NodeBookmark {
2360
+ constructor(anchor) {
2361
+ this.anchor = anchor;
2362
+ }
2363
+ map(mapping) {
2364
+ let { deleted, pos } = mapping.mapResult(this.anchor);
2365
+ return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
2366
+ }
2367
+ resolve(doc) {
2368
+ let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
2369
+ if (node && NodeSelection.isSelectable(node))
2370
+ return new NodeSelection($pos);
2371
+ return Selection.near($pos);
2372
+ }
2373
+ }
2374
+ /**
2375
+ A selection type that represents selecting the whole document
2376
+ (which can not necessarily be expressed with a text selection, when
2377
+ there are for example leaf block nodes at the start or end of the
2378
+ document).
2379
+ */
2380
+ class AllSelection extends Selection {
2381
+ /**
2382
+ Create an all-selection over the given document.
2383
+ */
2384
+ constructor(doc) {
2385
+ super(doc.resolve(0), doc.resolve(doc.content.size));
2386
+ }
2387
+ replace(tr, content = Slice.empty) {
2388
+ if (content == Slice.empty) {
2389
+ tr.delete(0, tr.doc.content.size);
2390
+ let sel = Selection.atStart(tr.doc);
2391
+ if (!sel.eq(tr.selection))
2392
+ tr.setSelection(sel);
2393
+ }
2394
+ else {
2395
+ super.replace(tr, content);
2396
+ }
2397
+ }
2398
+ toJSON() { return { type: "all" }; }
2399
+ /**
2400
+ @internal
2401
+ */
2402
+ static fromJSON(doc) { return new AllSelection(doc); }
2403
+ map(doc) { return new AllSelection(doc); }
2404
+ eq(other) { return other instanceof AllSelection; }
2405
+ getBookmark() { return AllBookmark; }
2406
+ }
2407
+ Selection.jsonID("all", AllSelection);
2408
+ const AllBookmark = {
2409
+ map() { return this; },
2410
+ resolve(doc) { return new AllSelection(doc); }
2411
+ };
2412
+ // FIXME we'll need some awareness of text direction when scanning for selections
2413
+ // Try to find a selection inside the given node. `pos` points at the
2414
+ // position where the search starts. When `text` is true, only return
2415
+ // text selections.
2416
+ function findSelectionIn(doc, node, pos, index, dir, text = false) {
2417
+ if (node.inlineContent)
2418
+ return TextSelection.create(doc, pos);
2419
+ for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
2420
+ let child = node.child(i);
2421
+ if (!child.isAtom) {
2422
+ let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
2423
+ if (inner)
2424
+ return inner;
2425
+ }
2426
+ else if (!text && NodeSelection.isSelectable(child)) {
2427
+ return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
2428
+ }
2429
+ pos += child.nodeSize * dir;
2430
+ }
2431
+ return null;
2432
+ }
2433
+ function selectionToInsertionEnd(tr, startLen, bias) {
2434
+ let last = tr.steps.length - 1;
2435
+ if (last < startLen)
2436
+ return;
2437
+ let step = tr.steps[last];
2438
+ if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep))
2439
+ return;
2440
+ let map = tr.mapping.maps[last], end;
2441
+ map.forEach((_from, _to, _newFrom, newTo) => { if (end == null)
2442
+ end = newTo; });
2443
+ tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
2444
+ }
2445
+
2446
+ function bind(f, self) {
2447
+ return !self || !f ? f : f.bind(self);
2448
+ }
2449
+ class FieldDesc {
2450
+ constructor(name, desc, self) {
2451
+ this.name = name;
2452
+ this.init = bind(desc.init, self);
2453
+ this.apply = bind(desc.apply, self);
2454
+ }
2455
+ }
2456
+ [
2457
+ new FieldDesc("doc", {
2458
+ init(config) { return config.doc || config.schema.topNodeType.createAndFill(); },
2459
+ apply(tr) { return tr.doc; }
2460
+ }),
2461
+ new FieldDesc("selection", {
2462
+ init(config, instance) { return config.selection || Selection.atStart(instance.doc); },
2463
+ apply(tr) { return tr.selection; }
2464
+ }),
2465
+ new FieldDesc("storedMarks", {
2466
+ init(config) { return config.storedMarks || null; },
2467
+ apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null; }
2468
+ }),
2469
+ new FieldDesc("scrollToSelection", {
2470
+ init() { return 0; },
2471
+ apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev; }
2472
+ })
2473
+ ];
2474
+ const keys = Object.create(null);
2475
+ function createKey(name) {
2476
+ if (name in keys)
2477
+ return name + "$" + ++keys[name];
2478
+ keys[name] = 0;
2479
+ return name + "$";
2480
+ }
2481
+ /**
2482
+ A key is used to [tag](https://prosemirror.net/docs/ref/#state.PluginSpec.key) plugins in a way
2483
+ that makes it possible to find them, given an editor state.
2484
+ Assigning a key does mean only one plugin of that type can be
2485
+ active in a state.
2486
+ */
2487
+ class PluginKey {
2488
+ /**
2489
+ Create a plugin key.
2490
+ */
2491
+ constructor(name = "key") { this.key = createKey(name); }
2492
+ /**
2493
+ Get the active plugin with this key, if any, from an editor
2494
+ state.
2495
+ */
2496
+ get(state) { return state.config.pluginsByKey[this.key]; }
2497
+ /**
2498
+ Get the plugin's state from an editor state.
2499
+ */
2500
+ getState(state) { return state[this.key]; }
2501
+ }
2502
+
2503
+ /**
2504
+ Delete the selection, if there is one.
2505
+ */
2506
+ const deleteSelection = (state, dispatch) => {
2507
+ if (state.selection.empty)
2508
+ return false;
2509
+ if (dispatch)
2510
+ dispatch(state.tr.deleteSelection().scrollIntoView());
2511
+ return true;
2512
+ };
2513
+ function atBlockStart(state, view) {
2514
+ let { $cursor } = state.selection;
2515
+ if (!$cursor || (view ? !view.endOfTextblock("backward", state)
2516
+ : $cursor.parentOffset > 0))
2517
+ return null;
2518
+ return $cursor;
2519
+ }
2520
+ /**
2521
+ If the selection is empty and at the start of a textblock, try to
2522
+ reduce the distance between that block and the one before it—if
2523
+ there's a block directly before it that can be joined, join them.
2524
+ If not, try to move the selected block closer to the next one in
2525
+ the document structure by lifting it out of its parent or moving it
2526
+ into a parent of the previous block. Will use the view for accurate
2527
+ (bidi-aware) start-of-textblock detection if given.
2528
+ */
2529
+ const joinBackward = (state, dispatch, view) => {
2530
+ let $cursor = atBlockStart(state, view);
2531
+ if (!$cursor)
2532
+ return false;
2533
+ let $cut = findCutBefore($cursor);
2534
+ // If there is no node before this, try to lift
2535
+ if (!$cut) {
2536
+ let range = $cursor.blockRange(), target = range && liftTarget(range);
2537
+ if (target == null)
2538
+ return false;
2539
+ if (dispatch)
2540
+ dispatch(state.tr.lift(range, target).scrollIntoView());
2541
+ return true;
2542
+ }
2543
+ let before = $cut.nodeBefore;
2544
+ // Apply the joining algorithm
2545
+ if (deleteBarrier(state, $cut, dispatch, -1))
2546
+ return true;
2547
+ // If the node below has no content and the node above is
2548
+ // selectable, delete the node below and select the one above.
2549
+ if ($cursor.parent.content.size == 0 &&
2550
+ (textblockAt(before, "end") || NodeSelection.isSelectable(before))) {
2551
+ for (let depth = $cursor.depth;; depth--) {
2552
+ let delStep = replaceStep(state.doc, $cursor.before(depth), $cursor.after(depth), Slice.empty);
2553
+ if (delStep && delStep.slice.size < delStep.to - delStep.from) {
2554
+ if (dispatch) {
2555
+ let tr = state.tr.step(delStep);
2556
+ tr.setSelection(textblockAt(before, "end")
2557
+ ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos, -1)), -1)
2558
+ : NodeSelection.create(tr.doc, $cut.pos - before.nodeSize));
2559
+ dispatch(tr.scrollIntoView());
2560
+ }
2561
+ return true;
2562
+ }
2563
+ if (depth == 1 || $cursor.node(depth - 1).childCount > 1)
2564
+ break;
2565
+ }
2566
+ }
2567
+ // If the node before is an atom, delete it
2568
+ if (before.isAtom && $cut.depth == $cursor.depth - 1) {
2569
+ if (dispatch)
2570
+ dispatch(state.tr.delete($cut.pos - before.nodeSize, $cut.pos).scrollIntoView());
2571
+ return true;
2572
+ }
2573
+ return false;
2574
+ };
2575
+ function textblockAt(node, side, only = false) {
2576
+ for (let scan = node; scan; scan = (side == "start" ? scan.firstChild : scan.lastChild)) {
2577
+ if (scan.isTextblock)
2578
+ return true;
2579
+ if (only && scan.childCount != 1)
2580
+ return false;
2581
+ }
2582
+ return false;
2583
+ }
2584
+ /**
2585
+ When the selection is empty and at the start of a textblock, select
2586
+ the node before that textblock, if possible. This is intended to be
2587
+ bound to keys like backspace, after
2588
+ [`joinBackward`](https://prosemirror.net/docs/ref/#commands.joinBackward) or other deleting
2589
+ commands, as a fall-back behavior when the schema doesn't allow
2590
+ deletion at the selected point.
2591
+ */
2592
+ const selectNodeBackward = (state, dispatch, view) => {
2593
+ let { $head, empty } = state.selection, $cut = $head;
2594
+ if (!empty)
2595
+ return false;
2596
+ if ($head.parent.isTextblock) {
2597
+ if (view ? !view.endOfTextblock("backward", state) : $head.parentOffset > 0)
2598
+ return false;
2599
+ $cut = findCutBefore($head);
2600
+ }
2601
+ let node = $cut && $cut.nodeBefore;
2602
+ if (!node || !NodeSelection.isSelectable(node))
2603
+ return false;
2604
+ if (dispatch)
2605
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos - node.nodeSize)).scrollIntoView());
2606
+ return true;
2607
+ };
2608
+ function findCutBefore($pos) {
2609
+ if (!$pos.parent.type.spec.isolating)
2610
+ for (let i = $pos.depth - 1; i >= 0; i--) {
2611
+ if ($pos.index(i) > 0)
2612
+ return $pos.doc.resolve($pos.before(i + 1));
2613
+ if ($pos.node(i).type.spec.isolating)
2614
+ break;
2615
+ }
2616
+ return null;
2617
+ }
2618
+ function atBlockEnd(state, view) {
2619
+ let { $cursor } = state.selection;
2620
+ if (!$cursor || (view ? !view.endOfTextblock("forward", state)
2621
+ : $cursor.parentOffset < $cursor.parent.content.size))
2622
+ return null;
2623
+ return $cursor;
2624
+ }
2625
+ /**
2626
+ If the selection is empty and the cursor is at the end of a
2627
+ textblock, try to reduce or remove the boundary between that block
2628
+ and the one after it, either by joining them or by moving the other
2629
+ block closer to this one in the tree structure. Will use the view
2630
+ for accurate start-of-textblock detection if given.
2631
+ */
2632
+ const joinForward = (state, dispatch, view) => {
2633
+ let $cursor = atBlockEnd(state, view);
2634
+ if (!$cursor)
2635
+ return false;
2636
+ let $cut = findCutAfter($cursor);
2637
+ // If there is no node after this, there's nothing to do
2638
+ if (!$cut)
2639
+ return false;
2640
+ let after = $cut.nodeAfter;
2641
+ // Try the joining algorithm
2642
+ if (deleteBarrier(state, $cut, dispatch, 1))
2643
+ return true;
2644
+ // If the node above has no content and the node below is
2645
+ // selectable, delete the node above and select the one below.
2646
+ if ($cursor.parent.content.size == 0 &&
2647
+ (textblockAt(after, "start") || NodeSelection.isSelectable(after))) {
2648
+ let delStep = replaceStep(state.doc, $cursor.before(), $cursor.after(), Slice.empty);
2649
+ if (delStep && delStep.slice.size < delStep.to - delStep.from) {
2650
+ if (dispatch) {
2651
+ let tr = state.tr.step(delStep);
2652
+ tr.setSelection(textblockAt(after, "start") ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos)), 1)
2653
+ : NodeSelection.create(tr.doc, tr.mapping.map($cut.pos)));
2654
+ dispatch(tr.scrollIntoView());
2655
+ }
2656
+ return true;
2657
+ }
2658
+ }
2659
+ // If the next node is an atom, delete it
2660
+ if (after.isAtom && $cut.depth == $cursor.depth - 1) {
2661
+ if (dispatch)
2662
+ dispatch(state.tr.delete($cut.pos, $cut.pos + after.nodeSize).scrollIntoView());
2663
+ return true;
2664
+ }
2665
+ return false;
2666
+ };
2667
+ /**
2668
+ When the selection is empty and at the end of a textblock, select
2669
+ the node coming after that textblock, if possible. This is intended
2670
+ to be bound to keys like delete, after
2671
+ [`joinForward`](https://prosemirror.net/docs/ref/#commands.joinForward) and similar deleting
2672
+ commands, to provide a fall-back behavior when the schema doesn't
2673
+ allow deletion at the selected point.
2674
+ */
2675
+ const selectNodeForward = (state, dispatch, view) => {
2676
+ let { $head, empty } = state.selection, $cut = $head;
2677
+ if (!empty)
2678
+ return false;
2679
+ if ($head.parent.isTextblock) {
2680
+ if (view ? !view.endOfTextblock("forward", state) : $head.parentOffset < $head.parent.content.size)
2681
+ return false;
2682
+ $cut = findCutAfter($head);
2683
+ }
2684
+ let node = $cut && $cut.nodeAfter;
2685
+ if (!node || !NodeSelection.isSelectable(node))
2686
+ return false;
2687
+ if (dispatch)
2688
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos)).scrollIntoView());
2689
+ return true;
2690
+ };
2691
+ function findCutAfter($pos) {
2692
+ if (!$pos.parent.type.spec.isolating)
2693
+ for (let i = $pos.depth - 1; i >= 0; i--) {
2694
+ let parent = $pos.node(i);
2695
+ if ($pos.index(i) + 1 < parent.childCount)
2696
+ return $pos.doc.resolve($pos.after(i + 1));
2697
+ if (parent.type.spec.isolating)
2698
+ break;
2699
+ }
2700
+ return null;
2701
+ }
2702
+ /**
2703
+ If the selection is in a node whose type has a truthy
2704
+ [`code`](https://prosemirror.net/docs/ref/#model.NodeSpec.code) property in its spec, replace the
2705
+ selection with a newline character.
2706
+ */
2707
+ const newlineInCode = (state, dispatch) => {
2708
+ let { $head, $anchor } = state.selection;
2709
+ if (!$head.parent.type.spec.code || !$head.sameParent($anchor))
2710
+ return false;
2711
+ if (dispatch)
2712
+ dispatch(state.tr.insertText("\n").scrollIntoView());
2713
+ return true;
2714
+ };
2715
+ function defaultBlockAt(match) {
2716
+ for (let i = 0; i < match.edgeCount; i++) {
2717
+ let { type } = match.edge(i);
2718
+ if (type.isTextblock && !type.hasRequiredAttrs())
2719
+ return type;
2720
+ }
2721
+ return null;
2722
+ }
2723
+ /**
2724
+ When the selection is in a node with a truthy
2725
+ [`code`](https://prosemirror.net/docs/ref/#model.NodeSpec.code) property in its spec, create a
2726
+ default block after the code block, and move the cursor there.
2727
+ */
2728
+ const exitCode = (state, dispatch) => {
2729
+ let { $head, $anchor } = state.selection;
2730
+ if (!$head.parent.type.spec.code || !$head.sameParent($anchor))
2731
+ return false;
2732
+ let above = $head.node(-1), after = $head.indexAfter(-1), type = defaultBlockAt(above.contentMatchAt(after));
2733
+ if (!type || !above.canReplaceWith(after, after, type))
2734
+ return false;
2735
+ if (dispatch) {
2736
+ let pos = $head.after(), tr = state.tr.replaceWith(pos, pos, type.createAndFill());
2737
+ tr.setSelection(Selection.near(tr.doc.resolve(pos), 1));
2738
+ dispatch(tr.scrollIntoView());
2739
+ }
2740
+ return true;
2741
+ };
2742
+ /**
2743
+ If a block node is selected, create an empty paragraph before (if
2744
+ it is its parent's first child) or after it.
2745
+ */
2746
+ const createParagraphNear = (state, dispatch) => {
2747
+ let sel = state.selection, { $from, $to } = sel;
2748
+ if (sel instanceof AllSelection || $from.parent.inlineContent || $to.parent.inlineContent)
2749
+ return false;
2750
+ let type = defaultBlockAt($to.parent.contentMatchAt($to.indexAfter()));
2751
+ if (!type || !type.isTextblock)
2752
+ return false;
2753
+ if (dispatch) {
2754
+ let side = (!$from.parentOffset && $to.index() < $to.parent.childCount ? $from : $to).pos;
2755
+ let tr = state.tr.insert(side, type.createAndFill());
2756
+ tr.setSelection(TextSelection.create(tr.doc, side + 1));
2757
+ dispatch(tr.scrollIntoView());
2758
+ }
2759
+ return true;
2760
+ };
2761
+ /**
2762
+ If the cursor is in an empty textblock that can be lifted, lift the
2763
+ block.
2764
+ */
2765
+ const liftEmptyBlock = (state, dispatch) => {
2766
+ let { $cursor } = state.selection;
2767
+ if (!$cursor || $cursor.parent.content.size)
2768
+ return false;
2769
+ if ($cursor.depth > 1 && $cursor.after() != $cursor.end(-1)) {
2770
+ let before = $cursor.before();
2771
+ if (canSplit(state.doc, before)) {
2772
+ if (dispatch)
2773
+ dispatch(state.tr.split(before).scrollIntoView());
2774
+ return true;
2775
+ }
2776
+ }
2777
+ let range = $cursor.blockRange(), target = range && liftTarget(range);
2778
+ if (target == null)
2779
+ return false;
2780
+ if (dispatch)
2781
+ dispatch(state.tr.lift(range, target).scrollIntoView());
2782
+ return true;
2783
+ };
2784
+ /**
2785
+ Create a variant of [`splitBlock`](https://prosemirror.net/docs/ref/#commands.splitBlock) that uses
2786
+ a custom function to determine the type of the newly split off block.
2787
+ */
2788
+ function splitBlockAs(splitNode) {
2789
+ return (state, dispatch) => {
2790
+ let { $from, $to } = state.selection;
2791
+ if (state.selection instanceof NodeSelection && state.selection.node.isBlock) {
2792
+ if (!$from.parentOffset || !canSplit(state.doc, $from.pos))
2793
+ return false;
2794
+ if (dispatch)
2795
+ dispatch(state.tr.split($from.pos).scrollIntoView());
2796
+ return true;
2797
+ }
2798
+ if (!$from.depth)
2799
+ return false;
2800
+ let types = [];
2801
+ let splitDepth, deflt, atEnd = false, atStart = false;
2802
+ for (let d = $from.depth;; d--) {
2803
+ let node = $from.node(d);
2804
+ if (node.isBlock) {
2805
+ atEnd = $from.end(d) == $from.pos + ($from.depth - d);
2806
+ atStart = $from.start(d) == $from.pos - ($from.depth - d);
2807
+ deflt = defaultBlockAt($from.node(d - 1).contentMatchAt($from.indexAfter(d - 1)));
2808
+ types.unshift((atEnd && deflt ? { type: deflt } : null));
2809
+ splitDepth = d;
2810
+ break;
2811
+ }
2812
+ else {
2813
+ if (d == 1)
2814
+ return false;
2815
+ types.unshift(null);
2816
+ }
2817
+ }
2818
+ let tr = state.tr;
2819
+ if (state.selection instanceof TextSelection || state.selection instanceof AllSelection)
2820
+ tr.deleteSelection();
2821
+ let splitPos = tr.mapping.map($from.pos);
2822
+ let can = canSplit(tr.doc, splitPos, types.length, types);
2823
+ if (!can) {
2824
+ types[0] = deflt ? { type: deflt } : null;
2825
+ can = canSplit(tr.doc, splitPos, types.length, types);
2826
+ }
2827
+ if (!can)
2828
+ return false;
2829
+ tr.split(splitPos, types.length, types);
2830
+ if (!atEnd && atStart && $from.node(splitDepth).type != deflt) {
2831
+ let first = tr.mapping.map($from.before(splitDepth)), $first = tr.doc.resolve(first);
2832
+ if (deflt && $from.node(splitDepth - 1).canReplaceWith($first.index(), $first.index() + 1, deflt))
2833
+ tr.setNodeMarkup(tr.mapping.map($from.before(splitDepth)), deflt);
2834
+ }
2835
+ if (dispatch)
2836
+ dispatch(tr.scrollIntoView());
2837
+ return true;
2838
+ };
2839
+ }
2840
+ /**
2841
+ Split the parent block of the selection. If the selection is a text
2842
+ selection, also delete its content.
2843
+ */
2844
+ const splitBlock = splitBlockAs();
2845
+ function joinMaybeClear(state, $pos, dispatch) {
2846
+ let before = $pos.nodeBefore, after = $pos.nodeAfter, index = $pos.index();
2847
+ if (!before || !after || !before.type.compatibleContent(after.type))
2848
+ return false;
2849
+ if (!before.content.size && $pos.parent.canReplace(index - 1, index)) {
2850
+ if (dispatch)
2851
+ dispatch(state.tr.delete($pos.pos - before.nodeSize, $pos.pos).scrollIntoView());
2852
+ return true;
2853
+ }
2854
+ if (!$pos.parent.canReplace(index, index + 1) || !(after.isTextblock || canJoin(state.doc, $pos.pos)))
2855
+ return false;
2856
+ if (dispatch)
2857
+ dispatch(state.tr.join($pos.pos).scrollIntoView());
2858
+ return true;
2859
+ }
2860
+ function deleteBarrier(state, $cut, dispatch, dir) {
2861
+ let before = $cut.nodeBefore, after = $cut.nodeAfter, conn, match;
2862
+ let isolated = before.type.spec.isolating || after.type.spec.isolating;
2863
+ if (!isolated && joinMaybeClear(state, $cut, dispatch))
2864
+ return true;
2865
+ let canDelAfter = !isolated && $cut.parent.canReplace($cut.index(), $cut.index() + 1);
2866
+ if (canDelAfter &&
2867
+ (conn = (match = before.contentMatchAt(before.childCount)).findWrapping(after.type)) &&
2868
+ match.matchType(conn[0] || after.type).validEnd) {
2869
+ if (dispatch) {
2870
+ let end = $cut.pos + after.nodeSize, wrap = Fragment.empty;
2871
+ for (let i = conn.length - 1; i >= 0; i--)
2872
+ wrap = Fragment.from(conn[i].create(null, wrap));
2873
+ wrap = Fragment.from(before.copy(wrap));
2874
+ let tr = state.tr.step(new ReplaceAroundStep($cut.pos - 1, end, $cut.pos, end, new Slice(wrap, 1, 0), conn.length, true));
2875
+ let $joinAt = tr.doc.resolve(end + 2 * conn.length);
2876
+ if ($joinAt.nodeAfter && $joinAt.nodeAfter.type == before.type &&
2877
+ canJoin(tr.doc, $joinAt.pos))
2878
+ tr.join($joinAt.pos);
2879
+ dispatch(tr.scrollIntoView());
2880
+ }
2881
+ return true;
2882
+ }
2883
+ let selAfter = after.type.spec.isolating || (dir > 0 && isolated) ? null : Selection.findFrom($cut, 1);
2884
+ let range = selAfter && selAfter.$from.blockRange(selAfter.$to), target = range && liftTarget(range);
2885
+ if (target != null && target >= $cut.depth) {
2886
+ if (dispatch)
2887
+ dispatch(state.tr.lift(range, target).scrollIntoView());
2888
+ return true;
2889
+ }
2890
+ if (canDelAfter && textblockAt(after, "start", true) && textblockAt(before, "end")) {
2891
+ let at = before, wrap = [];
2892
+ for (;;) {
2893
+ wrap.push(at);
2894
+ if (at.isTextblock)
2895
+ break;
2896
+ at = at.lastChild;
2897
+ }
2898
+ let afterText = after, afterDepth = 1;
2899
+ for (; !afterText.isTextblock; afterText = afterText.firstChild)
2900
+ afterDepth++;
2901
+ if (at.canReplace(at.childCount, at.childCount, afterText.content)) {
2902
+ if (dispatch) {
2903
+ let end = Fragment.empty;
2904
+ for (let i = wrap.length - 1; i >= 0; i--)
2905
+ end = Fragment.from(wrap[i].copy(end));
2906
+ let tr = state.tr.step(new ReplaceAroundStep($cut.pos - wrap.length, $cut.pos + after.nodeSize, $cut.pos + afterDepth, $cut.pos + after.nodeSize - afterDepth, new Slice(end, wrap.length, 0), 0, true));
2907
+ dispatch(tr.scrollIntoView());
2908
+ }
2909
+ return true;
2910
+ }
2911
+ }
2912
+ return false;
2913
+ }
2914
+ /**
2915
+ Combine a number of command functions into a single function (which
2916
+ calls them one by one until one returns true).
2917
+ */
2918
+ function chainCommands(...commands) {
2919
+ return function (state, dispatch, view) {
2920
+ for (let i = 0; i < commands.length; i++)
2921
+ if (commands[i](state, dispatch, view))
2922
+ return true;
2923
+ return false;
2924
+ };
2925
+ }
2926
+ chainCommands(deleteSelection, joinBackward, selectNodeBackward);
2927
+ chainCommands(deleteSelection, joinForward, selectNodeForward);
2928
+ /**
2929
+ A basic keymap containing bindings not specific to any schema.
2930
+ Binds the following keys (when multiple commands are listed, they
2931
+ are chained with [`chainCommands`](https://prosemirror.net/docs/ref/#commands.chainCommands)):
2932
+
2933
+ * **Enter** to `newlineInCode`, `createParagraphNear`, `liftEmptyBlock`, `splitBlock`
2934
+ * **Mod-Enter** to `exitCode`
2935
+ * **Backspace** and **Mod-Backspace** to `deleteSelection`, `joinBackward`, `selectNodeBackward`
2936
+ * **Delete** and **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
2937
+ * **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
2938
+ * **Mod-a** to `selectAll`
2939
+ */
2940
+ ({
2941
+ "Enter": chainCommands(newlineInCode, createParagraphNear, liftEmptyBlock, splitBlock)});
2942
+ typeof navigator != "undefined" ? /Mac|iP(hone|[oa]d)/.test(navigator.platform)
2943
+ // @ts-ignore
2944
+ : typeof os != "undefined" && os.platform ? os.platform() == "darwin" : false;
2945
+
2946
+ var GOOD_LEAF_SIZE = 200;
2947
+
2948
+ // :: class<T> A rope sequence is a persistent sequence data structure
2949
+ // that supports appending, prepending, and slicing without doing a
2950
+ // full copy. It is represented as a mostly-balanced tree.
2951
+ var RopeSequence = function RopeSequence () {};
2952
+
2953
+ RopeSequence.prototype.append = function append (other) {
2954
+ if (!other.length) { return this }
2955
+ other = RopeSequence.from(other);
2956
+
2957
+ return (!this.length && other) ||
2958
+ (other.length < GOOD_LEAF_SIZE && this.leafAppend(other)) ||
2959
+ (this.length < GOOD_LEAF_SIZE && other.leafPrepend(this)) ||
2960
+ this.appendInner(other)
2961
+ };
2962
+
2963
+ // :: (union<[T], RopeSequence<T>>) → RopeSequence<T>
2964
+ // Prepend an array or other rope to this one, returning a new rope.
2965
+ RopeSequence.prototype.prepend = function prepend (other) {
2966
+ if (!other.length) { return this }
2967
+ return RopeSequence.from(other).append(this)
2968
+ };
2969
+
2970
+ RopeSequence.prototype.appendInner = function appendInner (other) {
2971
+ return new Append(this, other)
2972
+ };
2973
+
2974
+ // :: (?number, ?number) → RopeSequence<T>
2975
+ // Create a rope repesenting a sub-sequence of this rope.
2976
+ RopeSequence.prototype.slice = function slice (from, to) {
2977
+ if ( from === void 0 ) from = 0;
2978
+ if ( to === void 0 ) to = this.length;
2979
+
2980
+ if (from >= to) { return RopeSequence.empty }
2981
+ return this.sliceInner(Math.max(0, from), Math.min(this.length, to))
2982
+ };
2983
+
2984
+ // :: (number) → T
2985
+ // Retrieve the element at the given position from this rope.
2986
+ RopeSequence.prototype.get = function get (i) {
2987
+ if (i < 0 || i >= this.length) { return undefined }
2988
+ return this.getInner(i)
2989
+ };
2990
+
2991
+ // :: ((element: T, index: number) → ?bool, ?number, ?number)
2992
+ // Call the given function for each element between the given
2993
+ // indices. This tends to be more efficient than looping over the
2994
+ // indices and calling `get`, because it doesn't have to descend the
2995
+ // tree for every element.
2996
+ RopeSequence.prototype.forEach = function forEach (f, from, to) {
2997
+ if ( from === void 0 ) from = 0;
2998
+ if ( to === void 0 ) to = this.length;
2999
+
3000
+ if (from <= to)
3001
+ { this.forEachInner(f, from, to, 0); }
3002
+ else
3003
+ { this.forEachInvertedInner(f, from, to, 0); }
3004
+ };
3005
+
3006
+ // :: ((element: T, index: number) → U, ?number, ?number) → [U]
3007
+ // Map the given functions over the elements of the rope, producing
3008
+ // a flat array.
3009
+ RopeSequence.prototype.map = function map (f, from, to) {
3010
+ if ( from === void 0 ) from = 0;
3011
+ if ( to === void 0 ) to = this.length;
3012
+
3013
+ var result = [];
3014
+ this.forEach(function (elt, i) { return result.push(f(elt, i)); }, from, to);
3015
+ return result
3016
+ };
3017
+
3018
+ // :: (?union<[T], RopeSequence<T>>) → RopeSequence<T>
3019
+ // Create a rope representing the given array, or return the rope
3020
+ // itself if a rope was given.
3021
+ RopeSequence.from = function from (values) {
3022
+ if (values instanceof RopeSequence) { return values }
3023
+ return values && values.length ? new Leaf(values) : RopeSequence.empty
3024
+ };
3025
+
3026
+ var Leaf = /*@__PURE__*/(function (RopeSequence) {
3027
+ function Leaf(values) {
3028
+ RopeSequence.call(this);
3029
+ this.values = values;
3030
+ }
3031
+
3032
+ if ( RopeSequence ) Leaf.__proto__ = RopeSequence;
3033
+ Leaf.prototype = Object.create( RopeSequence && RopeSequence.prototype );
3034
+ Leaf.prototype.constructor = Leaf;
3035
+
3036
+ var prototypeAccessors = { length: { configurable: true },depth: { configurable: true } };
3037
+
3038
+ Leaf.prototype.flatten = function flatten () {
3039
+ return this.values
3040
+ };
3041
+
3042
+ Leaf.prototype.sliceInner = function sliceInner (from, to) {
3043
+ if (from == 0 && to == this.length) { return this }
3044
+ return new Leaf(this.values.slice(from, to))
3045
+ };
3046
+
3047
+ Leaf.prototype.getInner = function getInner (i) {
3048
+ return this.values[i]
3049
+ };
3050
+
3051
+ Leaf.prototype.forEachInner = function forEachInner (f, from, to, start) {
3052
+ for (var i = from; i < to; i++)
3053
+ { if (f(this.values[i], start + i) === false) { return false } }
3054
+ };
3055
+
3056
+ Leaf.prototype.forEachInvertedInner = function forEachInvertedInner (f, from, to, start) {
3057
+ for (var i = from - 1; i >= to; i--)
3058
+ { if (f(this.values[i], start + i) === false) { return false } }
3059
+ };
3060
+
3061
+ Leaf.prototype.leafAppend = function leafAppend (other) {
3062
+ if (this.length + other.length <= GOOD_LEAF_SIZE)
3063
+ { return new Leaf(this.values.concat(other.flatten())) }
3064
+ };
3065
+
3066
+ Leaf.prototype.leafPrepend = function leafPrepend (other) {
3067
+ if (this.length + other.length <= GOOD_LEAF_SIZE)
3068
+ { return new Leaf(other.flatten().concat(this.values)) }
3069
+ };
3070
+
3071
+ prototypeAccessors.length.get = function () { return this.values.length };
3072
+
3073
+ prototypeAccessors.depth.get = function () { return 0 };
3074
+
3075
+ Object.defineProperties( Leaf.prototype, prototypeAccessors );
3076
+
3077
+ return Leaf;
3078
+ }(RopeSequence));
3079
+
3080
+ // :: RopeSequence
3081
+ // The empty rope sequence.
3082
+ RopeSequence.empty = new Leaf([]);
3083
+
3084
+ var Append = /*@__PURE__*/(function (RopeSequence) {
3085
+ function Append(left, right) {
3086
+ RopeSequence.call(this);
3087
+ this.left = left;
3088
+ this.right = right;
3089
+ this.length = left.length + right.length;
3090
+ this.depth = Math.max(left.depth, right.depth) + 1;
3091
+ }
3092
+
3093
+ if ( RopeSequence ) Append.__proto__ = RopeSequence;
3094
+ Append.prototype = Object.create( RopeSequence && RopeSequence.prototype );
3095
+ Append.prototype.constructor = Append;
3096
+
3097
+ Append.prototype.flatten = function flatten () {
3098
+ return this.left.flatten().concat(this.right.flatten())
3099
+ };
3100
+
3101
+ Append.prototype.getInner = function getInner (i) {
3102
+ return i < this.left.length ? this.left.get(i) : this.right.get(i - this.left.length)
3103
+ };
3104
+
3105
+ Append.prototype.forEachInner = function forEachInner (f, from, to, start) {
3106
+ var leftLen = this.left.length;
3107
+ if (from < leftLen &&
3108
+ this.left.forEachInner(f, from, Math.min(to, leftLen), start) === false)
3109
+ { return false }
3110
+ if (to > leftLen &&
3111
+ this.right.forEachInner(f, Math.max(from - leftLen, 0), Math.min(this.length, to) - leftLen, start + leftLen) === false)
3112
+ { return false }
3113
+ };
3114
+
3115
+ Append.prototype.forEachInvertedInner = function forEachInvertedInner (f, from, to, start) {
3116
+ var leftLen = this.left.length;
3117
+ if (from > leftLen &&
3118
+ this.right.forEachInvertedInner(f, from - leftLen, Math.max(to, leftLen) - leftLen, start + leftLen) === false)
3119
+ { return false }
3120
+ if (to < leftLen &&
3121
+ this.left.forEachInvertedInner(f, Math.min(from, leftLen), to, start) === false)
3122
+ { return false }
3123
+ };
3124
+
3125
+ Append.prototype.sliceInner = function sliceInner (from, to) {
3126
+ if (from == 0 && to == this.length) { return this }
3127
+ var leftLen = this.left.length;
3128
+ if (to <= leftLen) { return this.left.slice(from, to) }
3129
+ if (from >= leftLen) { return this.right.slice(from - leftLen, to - leftLen) }
3130
+ return this.left.slice(from, leftLen).append(this.right.slice(0, to - leftLen))
3131
+ };
3132
+
3133
+ Append.prototype.leafAppend = function leafAppend (other) {
3134
+ var inner = this.right.leafAppend(other);
3135
+ if (inner) { return new Append(this.left, inner) }
3136
+ };
3137
+
3138
+ Append.prototype.leafPrepend = function leafPrepend (other) {
3139
+ var inner = this.left.leafPrepend(other);
3140
+ if (inner) { return new Append(inner, this.right) }
3141
+ };
3142
+
3143
+ Append.prototype.appendInner = function appendInner (other) {
3144
+ if (this.left.depth >= Math.max(this.right.depth, other.depth) + 1)
3145
+ { return new Append(this.left, new Append(this.right, other)) }
3146
+ return new Append(this, other)
3147
+ };
3148
+
3149
+ return Append;
3150
+ }(RopeSequence));
3151
+
3152
+ // ProseMirror's history isn't simply a way to roll back to a previous
3153
+ // state, because ProseMirror supports applying changes without adding
3154
+ // them to the history (for example during collaboration).
3155
+ //
3156
+ // To this end, each 'Branch' (one for the undo history and one for
3157
+ // the redo history) keeps an array of 'Items', which can optionally
3158
+ // hold a step (an actual undoable change), and always hold a position
3159
+ // map (which is needed to move changes below them to apply to the
3160
+ // current document).
3161
+ //
3162
+ // An item that has both a step and a selection bookmark is the start
3163
+ // of an 'event' — a group of changes that will be undone or redone at
3164
+ // once. (It stores only the bookmark, since that way we don't have to
3165
+ // provide a document until the selection is actually applied, which
3166
+ // is useful when compressing.)
3167
+ // Used to schedule history compression
3168
+ const max_empty_items = 500;
3169
+ class Branch {
3170
+ constructor(items, eventCount) {
3171
+ this.items = items;
3172
+ this.eventCount = eventCount;
3173
+ }
3174
+ // Pop the latest event off the branch's history and apply it
3175
+ // to a document transform.
3176
+ popEvent(state, preserveItems) {
3177
+ if (this.eventCount == 0)
3178
+ return null;
3179
+ let end = this.items.length;
3180
+ for (;; end--) {
3181
+ let next = this.items.get(end - 1);
3182
+ if (next.selection) {
3183
+ --end;
3184
+ break;
3185
+ }
3186
+ }
3187
+ let remap, mapFrom;
3188
+ if (preserveItems) {
3189
+ remap = this.remapping(end, this.items.length);
3190
+ mapFrom = remap.maps.length;
3191
+ }
3192
+ let transform = state.tr;
3193
+ let selection, remaining;
3194
+ let addAfter = [], addBefore = [];
3195
+ this.items.forEach((item, i) => {
3196
+ if (!item.step) {
3197
+ if (!remap) {
3198
+ remap = this.remapping(end, i + 1);
3199
+ mapFrom = remap.maps.length;
3200
+ }
3201
+ mapFrom--;
3202
+ addBefore.push(item);
3203
+ return;
3204
+ }
3205
+ if (remap) {
3206
+ addBefore.push(new Item(item.map));
3207
+ let step = item.step.map(remap.slice(mapFrom)), map;
3208
+ if (step && transform.maybeStep(step).doc) {
3209
+ map = transform.mapping.maps[transform.mapping.maps.length - 1];
3210
+ addAfter.push(new Item(map, undefined, undefined, addAfter.length + addBefore.length));
3211
+ }
3212
+ mapFrom--;
3213
+ if (map)
3214
+ remap.appendMap(map, mapFrom);
3215
+ }
3216
+ else {
3217
+ transform.maybeStep(item.step);
3218
+ }
3219
+ if (item.selection) {
3220
+ selection = remap ? item.selection.map(remap.slice(mapFrom)) : item.selection;
3221
+ remaining = new Branch(this.items.slice(0, end).append(addBefore.reverse().concat(addAfter)), this.eventCount - 1);
3222
+ return false;
3223
+ }
3224
+ }, this.items.length, 0);
3225
+ return { remaining: remaining, transform, selection: selection };
3226
+ }
3227
+ // Create a new branch with the given transform added.
3228
+ addTransform(transform, selection, histOptions, preserveItems) {
3229
+ let newItems = [], eventCount = this.eventCount;
3230
+ let oldItems = this.items, lastItem = !preserveItems && oldItems.length ? oldItems.get(oldItems.length - 1) : null;
3231
+ for (let i = 0; i < transform.steps.length; i++) {
3232
+ let step = transform.steps[i].invert(transform.docs[i]);
3233
+ let item = new Item(transform.mapping.maps[i], step, selection), merged;
3234
+ if (merged = lastItem && lastItem.merge(item)) {
3235
+ item = merged;
3236
+ if (i)
3237
+ newItems.pop();
3238
+ else
3239
+ oldItems = oldItems.slice(0, oldItems.length - 1);
3240
+ }
3241
+ newItems.push(item);
3242
+ if (selection) {
3243
+ eventCount++;
3244
+ selection = undefined;
3245
+ }
3246
+ if (!preserveItems)
3247
+ lastItem = item;
3248
+ }
3249
+ let overflow = eventCount - histOptions.depth;
3250
+ if (overflow > DEPTH_OVERFLOW) {
3251
+ oldItems = cutOffEvents(oldItems, overflow);
3252
+ eventCount -= overflow;
3253
+ }
3254
+ return new Branch(oldItems.append(newItems), eventCount);
3255
+ }
3256
+ remapping(from, to) {
3257
+ let maps = new Mapping;
3258
+ this.items.forEach((item, i) => {
3259
+ let mirrorPos = item.mirrorOffset != null && i - item.mirrorOffset >= from
3260
+ ? maps.maps.length - item.mirrorOffset : undefined;
3261
+ maps.appendMap(item.map, mirrorPos);
3262
+ }, from, to);
3263
+ return maps;
3264
+ }
3265
+ addMaps(array) {
3266
+ if (this.eventCount == 0)
3267
+ return this;
3268
+ return new Branch(this.items.append(array.map(map => new Item(map))), this.eventCount);
3269
+ }
3270
+ // When the collab module receives remote changes, the history has
3271
+ // to know about those, so that it can adjust the steps that were
3272
+ // rebased on top of the remote changes, and include the position
3273
+ // maps for the remote changes in its array of items.
3274
+ rebased(rebasedTransform, rebasedCount) {
3275
+ if (!this.eventCount)
3276
+ return this;
3277
+ let rebasedItems = [], start = Math.max(0, this.items.length - rebasedCount);
3278
+ let mapping = rebasedTransform.mapping;
3279
+ let newUntil = rebasedTransform.steps.length;
3280
+ let eventCount = this.eventCount;
3281
+ this.items.forEach(item => { if (item.selection)
3282
+ eventCount--; }, start);
3283
+ let iRebased = rebasedCount;
3284
+ this.items.forEach(item => {
3285
+ let pos = mapping.getMirror(--iRebased);
3286
+ if (pos == null)
3287
+ return;
3288
+ newUntil = Math.min(newUntil, pos);
3289
+ let map = mapping.maps[pos];
3290
+ if (item.step) {
3291
+ let step = rebasedTransform.steps[pos].invert(rebasedTransform.docs[pos]);
3292
+ let selection = item.selection && item.selection.map(mapping.slice(iRebased + 1, pos));
3293
+ if (selection)
3294
+ eventCount++;
3295
+ rebasedItems.push(new Item(map, step, selection));
3296
+ }
3297
+ else {
3298
+ rebasedItems.push(new Item(map));
3299
+ }
3300
+ }, start);
3301
+ let newMaps = [];
3302
+ for (let i = rebasedCount; i < newUntil; i++)
3303
+ newMaps.push(new Item(mapping.maps[i]));
3304
+ let items = this.items.slice(0, start).append(newMaps).append(rebasedItems);
3305
+ let branch = new Branch(items, eventCount);
3306
+ if (branch.emptyItemCount() > max_empty_items)
3307
+ branch = branch.compress(this.items.length - rebasedItems.length);
3308
+ return branch;
3309
+ }
3310
+ emptyItemCount() {
3311
+ let count = 0;
3312
+ this.items.forEach(item => { if (!item.step)
3313
+ count++; });
3314
+ return count;
3315
+ }
3316
+ // Compressing a branch means rewriting it to push the air (map-only
3317
+ // items) out. During collaboration, these naturally accumulate
3318
+ // because each remote change adds one. The `upto` argument is used
3319
+ // to ensure that only the items below a given level are compressed,
3320
+ // because `rebased` relies on a clean, untouched set of items in
3321
+ // order to associate old items with rebased steps.
3322
+ compress(upto = this.items.length) {
3323
+ let remap = this.remapping(0, upto), mapFrom = remap.maps.length;
3324
+ let items = [], events = 0;
3325
+ this.items.forEach((item, i) => {
3326
+ if (i >= upto) {
3327
+ items.push(item);
3328
+ if (item.selection)
3329
+ events++;
3330
+ }
3331
+ else if (item.step) {
3332
+ let step = item.step.map(remap.slice(mapFrom)), map = step && step.getMap();
3333
+ mapFrom--;
3334
+ if (map)
3335
+ remap.appendMap(map, mapFrom);
3336
+ if (step) {
3337
+ let selection = item.selection && item.selection.map(remap.slice(mapFrom));
3338
+ if (selection)
3339
+ events++;
3340
+ let newItem = new Item(map.invert(), step, selection), merged, last = items.length - 1;
3341
+ if (merged = items.length && items[last].merge(newItem))
3342
+ items[last] = merged;
3343
+ else
3344
+ items.push(newItem);
3345
+ }
3346
+ }
3347
+ else if (item.map) {
3348
+ mapFrom--;
3349
+ }
3350
+ }, this.items.length, 0);
3351
+ return new Branch(RopeSequence.from(items.reverse()), events);
3352
+ }
3353
+ }
3354
+ Branch.empty = new Branch(RopeSequence.empty, 0);
3355
+ function cutOffEvents(items, n) {
3356
+ let cutPoint;
3357
+ items.forEach((item, i) => {
3358
+ if (item.selection && (n-- == 0)) {
3359
+ cutPoint = i;
3360
+ return false;
3361
+ }
3362
+ });
3363
+ return items.slice(cutPoint);
3364
+ }
3365
+ class Item {
3366
+ constructor(
3367
+ // The (forward) step map for this item.
3368
+ map,
3369
+ // The inverted step
3370
+ step,
3371
+ // If this is non-null, this item is the start of a group, and
3372
+ // this selection is the starting selection for the group (the one
3373
+ // that was active before the first step was applied)
3374
+ selection,
3375
+ // If this item is the inverse of a previous mapping on the stack,
3376
+ // this points at the inverse's offset
3377
+ mirrorOffset) {
3378
+ this.map = map;
3379
+ this.step = step;
3380
+ this.selection = selection;
3381
+ this.mirrorOffset = mirrorOffset;
3382
+ }
3383
+ merge(other) {
3384
+ if (this.step && other.step && !other.selection) {
3385
+ let step = other.step.merge(this.step);
3386
+ if (step)
3387
+ return new Item(step.getMap().invert(), step, this.selection);
3388
+ }
3389
+ }
3390
+ }
3391
+ // The value of the state field that tracks undo/redo history for that
3392
+ // state. Will be stored in the plugin state when the history plugin
3393
+ // is active.
3394
+ class HistoryState {
3395
+ constructor(done, undone, prevRanges, prevTime, prevComposition) {
3396
+ this.done = done;
3397
+ this.undone = undone;
3398
+ this.prevRanges = prevRanges;
3399
+ this.prevTime = prevTime;
3400
+ this.prevComposition = prevComposition;
3401
+ }
3402
+ }
3403
+ const DEPTH_OVERFLOW = 20;
3404
+ // Apply the latest event from one branch to the document and shift the event
3405
+ // onto the other branch.
3406
+ function histTransaction(history, state, redo) {
3407
+ let preserveItems = mustPreserveItems(state);
3408
+ let histOptions = historyKey.get(state).spec.config;
3409
+ let pop = (redo ? history.undone : history.done).popEvent(state, preserveItems);
3410
+ if (!pop)
3411
+ return null;
3412
+ let selection = pop.selection.resolve(pop.transform.doc);
3413
+ let added = (redo ? history.done : history.undone).addTransform(pop.transform, state.selection.getBookmark(), histOptions, preserveItems);
3414
+ let newHist = new HistoryState(redo ? added : pop.remaining, redo ? pop.remaining : added, null, 0, -1);
3415
+ return pop.transform.setSelection(selection).setMeta(historyKey, { redo, historyState: newHist });
3416
+ }
3417
+ let cachedPreserveItems = false, cachedPreserveItemsPlugins = null;
3418
+ // Check whether any plugin in the given state has a
3419
+ // `historyPreserveItems` property in its spec, in which case we must
3420
+ // preserve steps exactly as they came in, so that they can be
3421
+ // rebased.
3422
+ function mustPreserveItems(state) {
3423
+ let plugins = state.plugins;
3424
+ if (cachedPreserveItemsPlugins != plugins) {
3425
+ cachedPreserveItems = false;
3426
+ cachedPreserveItemsPlugins = plugins;
3427
+ for (let i = 0; i < plugins.length; i++)
3428
+ if (plugins[i].spec.historyPreserveItems) {
3429
+ cachedPreserveItems = true;
3430
+ break;
3431
+ }
3432
+ }
3433
+ return cachedPreserveItems;
3434
+ }
3435
+ const historyKey = new PluginKey("history");
3436
+ new PluginKey("closeHistory");
3437
+ function buildCommand(redo, scroll) {
3438
+ return (state, dispatch) => {
3439
+ let hist = historyKey.getState(state);
3440
+ if (!hist || (redo ? hist.undone : hist.done).eventCount == 0)
3441
+ return false;
3442
+ if (dispatch) {
3443
+ let tr = histTransaction(hist, state, redo);
3444
+ if (tr)
3445
+ dispatch(scroll ? tr.scrollIntoView() : tr);
3446
+ }
3447
+ return true;
3448
+ };
3449
+ }
3450
+ /**
3451
+ A command function that undoes the last change, if any.
3452
+ */
3453
+ const undo = buildCommand(false, true);
3454
+ /**
3455
+ A command function that redoes the last undone change, if any.
3456
+ */
3457
+ const redo = buildCommand(true, true);
3458
+
3459
+ function keepAlive(..._args) {
3460
+ }
3461
+
3462
+ keepAlive(h);
3463
+ function Icon({ icon, class: className, onClick }) {
3464
+ return /* @__PURE__ */ h(
3465
+ "span",
3466
+ {
3467
+ class: clsx("milkdown-icon", className),
3468
+ onPointerdown: onClick,
3469
+ innerHTML: icon ? DOMPurify.sanitize(icon.trim()) : void 0
3470
+ }
3471
+ );
3472
+ }
3473
+ Icon.props = {
3474
+ icon: {
3475
+ type: String,
3476
+ required: false
3477
+ },
3478
+ class: {
3479
+ type: String,
3480
+ required: false
3481
+ },
3482
+ onClick: {
3483
+ type: Function,
3484
+ required: false
3485
+ }
3486
+ };
3487
+
3488
+ var __async = (__this, __arguments, generator) => {
3489
+ return new Promise((resolve, reject) => {
3490
+ var fulfilled = (value) => {
3491
+ try {
3492
+ step(generator.next(value));
3493
+ } catch (e) {
3494
+ reject(e);
3495
+ }
3496
+ };
3497
+ var rejected = (value) => {
3498
+ try {
3499
+ step(generator.throw(value));
3500
+ } catch (e) {
3501
+ reject(e);
3502
+ }
3503
+ };
3504
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
3505
+ step((generator = generator.apply(__this, __arguments)).next());
3506
+ });
3507
+ };
3508
+ keepAlive(h, Fragment$1);
3509
+ function copyToClipboard(text) {
3510
+ return __async(this, null, function* () {
3511
+ try {
3512
+ return navigator.clipboard.writeText(text);
3513
+ } catch (e) {
3514
+ const element = document.createElement("textarea");
3515
+ const previouslyFocusedElement = document.activeElement;
3516
+ element.value = text;
3517
+ element.setAttribute("readonly", "");
3518
+ element.style.contain = "strict";
3519
+ element.style.position = "absolute";
3520
+ element.style.left = "-9999px";
3521
+ element.style.fontSize = "12pt";
3522
+ const selection = document.getSelection();
3523
+ const originalRange = selection ? selection.rangeCount > 0 && selection.getRangeAt(0) : null;
3524
+ document.body.appendChild(element);
3525
+ element.select();
3526
+ element.selectionStart = 0;
3527
+ element.selectionEnd = text.length;
3528
+ document.execCommand("copy");
3529
+ document.body.removeChild(element);
3530
+ if (originalRange) {
3531
+ selection.removeAllRanges();
3532
+ selection.addRange(originalRange);
3533
+ }
3534
+ if (previouslyFocusedElement) {
3535
+ previouslyFocusedElement.focus();
3536
+ }
3537
+ }
3538
+ });
3539
+ }
3540
+ const CopyButton = defineComponent({
3541
+ props: {
3542
+ copyText: {
3543
+ type: String,
3544
+ required: true
3545
+ },
3546
+ copyIcon: {
3547
+ type: String,
3548
+ required: true
3549
+ },
3550
+ onCopy: {
3551
+ type: Function,
3552
+ required: true
3553
+ },
3554
+ text: {
3555
+ type: String,
3556
+ required: true
3557
+ }
3558
+ },
3559
+ setup(props) {
3560
+ const onCopyCode = () => {
3561
+ copyToClipboard(props.text).then(() => props.onCopy(props.text)).catch(console.error);
3562
+ };
3563
+ return () => {
3564
+ return /* @__PURE__ */ h(Fragment$1, null, /* @__PURE__ */ h("button", { type: "button", class: "copy-button", onClick: onCopyCode }, /* @__PURE__ */ h(Icon, { icon: props.copyIcon }), props.copyText));
3565
+ };
3566
+ }
3567
+ });
3568
+
3569
+ keepAlive(h, Fragment$1);
3570
+ const LanguagePicker = defineComponent({
3571
+ props: {
3572
+ language: {
3573
+ type: Object,
3574
+ required: true
3575
+ },
3576
+ getReadOnly: {
3577
+ type: Function,
3578
+ required: true
3579
+ },
3580
+ config: {
3581
+ type: Object,
3582
+ required: true
3583
+ },
3584
+ getAllLanguages: {
3585
+ type: Function,
3586
+ required: true
3587
+ },
3588
+ setLanguage: {
3589
+ type: Function,
3590
+ required: true
3591
+ }
3592
+ },
3593
+ setup({ language, config, setLanguage, getAllLanguages, getReadOnly }) {
3594
+ const triggerRef = ref();
3595
+ const showPicker = ref(false);
3596
+ const searchRef = ref();
3597
+ const pickerRef = ref();
3598
+ const filter = ref("");
3599
+ watch([showPicker, triggerRef, pickerRef], () => {
3600
+ filter.value = "";
3601
+ const picker = triggerRef.value;
3602
+ const languageList = pickerRef.value;
3603
+ if (!picker || !languageList) return;
3604
+ computePosition(picker, languageList, {
3605
+ placement: "bottom-start"
3606
+ }).then(({ x, y }) => {
3607
+ Object.assign(languageList.style, {
3608
+ left: `${x}px`,
3609
+ top: `${y}px`
3610
+ });
3611
+ }).catch(console.error);
3612
+ });
3613
+ const onTogglePicker = (e) => {
3614
+ e.preventDefault();
3615
+ e.stopPropagation();
3616
+ if (getReadOnly()) return;
3617
+ const next = !showPicker.value;
3618
+ showPicker.value = next;
3619
+ if (next) {
3620
+ setTimeout(() => {
3621
+ var _a;
3622
+ return (_a = searchRef.value) == null ? void 0 : _a.focus();
3623
+ }, 0);
3624
+ }
3625
+ };
3626
+ const changeFilter = (e) => {
3627
+ const target = e.target;
3628
+ filter.value = target.value;
3629
+ };
3630
+ const onSearchKeydown = (e) => {
3631
+ if (e.key === "Escape") filter.value = "";
3632
+ };
3633
+ const languages = computed(() => {
3634
+ var _a;
3635
+ if (!showPicker.value) return [];
3636
+ const all = (_a = getAllLanguages()) != null ? _a : [];
3637
+ const selected = all.find(
3638
+ (languageInfo) => languageInfo.name.toLowerCase() === language.value.toLowerCase()
3639
+ );
3640
+ const filtered = all.filter((languageInfo) => {
3641
+ const currentValue = filter.value.toLowerCase();
3642
+ return (languageInfo.name.toLowerCase().includes(currentValue) || languageInfo.alias.some(
3643
+ (alias) => alias.toLowerCase().includes(currentValue)
3644
+ )) && languageInfo !== selected;
3645
+ });
3646
+ if (filtered.length === 0) return [];
3647
+ if (!selected) return filtered;
3648
+ return [selected, ...filtered];
3649
+ });
3650
+ const clickHandler = (e) => {
3651
+ const target = e.target;
3652
+ if (triggerRef.value && triggerRef.value.contains(target)) return;
3653
+ const picker = pickerRef.value;
3654
+ const trigger = triggerRef.value;
3655
+ if (!trigger || !picker) return;
3656
+ if (trigger.dataset.expanded !== "true") return;
3657
+ if (!picker.contains(target)) showPicker.value = false;
3658
+ };
3659
+ onMounted(() => {
3660
+ window.addEventListener("click", clickHandler);
3661
+ });
3662
+ onUnmounted(() => {
3663
+ window.removeEventListener("click", clickHandler);
3664
+ });
3665
+ return () => {
3666
+ return /* @__PURE__ */ h(Fragment$1, null, /* @__PURE__ */ h(
3667
+ "button",
3668
+ {
3669
+ type: "button",
3670
+ ref: triggerRef,
3671
+ class: "language-button",
3672
+ onClick: onTogglePicker,
3673
+ "data-expanded": String(showPicker.value)
3674
+ },
3675
+ language.value || "Text",
3676
+ /* @__PURE__ */ h("div", { class: "expand-icon" }, /* @__PURE__ */ h(Icon, { icon: config.expandIcon }))
3677
+ ), /* @__PURE__ */ h("div", { ref: pickerRef, class: "language-picker" }, showPicker.value ? /* @__PURE__ */ h("div", { class: "list-wrapper" }, /* @__PURE__ */ h("div", { class: "search-box" }, /* @__PURE__ */ h("div", { class: "search-icon" }, /* @__PURE__ */ h(Icon, { icon: config.searchIcon })), /* @__PURE__ */ h(
3678
+ "input",
3679
+ {
3680
+ ref: searchRef,
3681
+ class: "search-input",
3682
+ placeholder: config.searchPlaceholder,
3683
+ value: filter.value,
3684
+ onInput: changeFilter,
3685
+ onKeydown: onSearchKeydown
3686
+ }
3687
+ ), /* @__PURE__ */ h(
3688
+ "div",
3689
+ {
3690
+ class: clsx(
3691
+ "clear-icon",
3692
+ filter.value.length === 0 && "hidden"
3693
+ ),
3694
+ onMousedown: (e) => {
3695
+ e.preventDefault();
3696
+ filter.value = "";
3697
+ }
3698
+ },
3699
+ /* @__PURE__ */ h(Icon, { icon: config.clearSearchIcon })
3700
+ )), /* @__PURE__ */ h(
3701
+ "ul",
3702
+ {
3703
+ class: "language-list",
3704
+ role: "listbox",
3705
+ onKeydown: (e) => {
3706
+ if (e.key === "Enter") {
3707
+ const active = document.activeElement;
3708
+ if (active instanceof HTMLElement && active.dataset.language)
3709
+ setLanguage(active.dataset.language);
3710
+ }
3711
+ }
3712
+ },
3713
+ !languages.value.length ? /* @__PURE__ */ h("li", { class: "language-list-item no-result" }, config.noResultText) : languages.value.map((languageInfo) => /* @__PURE__ */ h(
3714
+ "li",
3715
+ {
3716
+ role: "listitem",
3717
+ tabindex: "0",
3718
+ class: "language-list-item",
3719
+ "aria-selected": languageInfo.name.toLowerCase() === language.value.toLowerCase(),
3720
+ "data-language": languageInfo.name,
3721
+ onClick: () => {
3722
+ setLanguage(languageInfo.name);
3723
+ showPicker.value = false;
3724
+ }
3725
+ },
3726
+ config.renderLanguage(
3727
+ languageInfo.name,
3728
+ languageInfo.name.toLowerCase() === language.value.toLowerCase()
3729
+ )
3730
+ ))
3731
+ )) : null));
3732
+ };
3733
+ }
3734
+ });
3735
+
3736
+ keepAlive(h, Fragment$1);
3737
+ const PreviewPanel = defineComponent({
3738
+ props: {
3739
+ text: {
3740
+ type: Object,
3741
+ required: true
3742
+ },
3743
+ language: {
3744
+ type: Object,
3745
+ required: true
3746
+ },
3747
+ config: {
3748
+ type: Object,
3749
+ required: true
3750
+ },
3751
+ previewOnlyMode: {
3752
+ type: Object,
3753
+ required: true
3754
+ },
3755
+ preview: {
3756
+ type: Object,
3757
+ required: true
3758
+ }
3759
+ },
3760
+ setup(props) {
3761
+ const { previewOnlyMode, config, preview } = props;
3762
+ const previewRef = ref();
3763
+ watchEffect(() => {
3764
+ const previewContainer = previewRef.value;
3765
+ if (!previewContainer) return;
3766
+ while (previewContainer.firstChild) {
3767
+ previewContainer.removeChild(previewContainer.firstChild);
3768
+ }
3769
+ const previewContent = preview.value;
3770
+ if (typeof previewContent === "string" || previewContent instanceof Element) {
3771
+ previewContainer.innerHTML = DOMPurify.sanitize(previewContent);
3772
+ }
3773
+ });
3774
+ return () => {
3775
+ if (!preview.value) return null;
3776
+ return /* @__PURE__ */ h("div", { class: "preview-panel" }, !previewOnlyMode.value && /* @__PURE__ */ h(Fragment$1, null, /* @__PURE__ */ h("div", { class: "preview-divider" }), /* @__PURE__ */ h("div", { class: "preview-label" }, config.previewLabel)), /* @__PURE__ */ h("div", { ref: previewRef, class: "preview" }));
3777
+ };
3778
+ }
3779
+ });
3780
+
3781
+ keepAlive(h, Fragment$1);
3782
+ const CodeBlock = defineComponent({
3783
+ props: {
3784
+ text: {
3785
+ type: Object,
3786
+ required: true
3787
+ },
3788
+ selected: {
3789
+ type: Object,
3790
+ required: true
3791
+ },
3792
+ getReadOnly: {
3793
+ type: Function,
3794
+ required: true
3795
+ },
3796
+ codemirror: {
3797
+ type: Object,
3798
+ required: true
3799
+ },
3800
+ language: {
3801
+ type: Object,
3802
+ required: true
3803
+ },
3804
+ getAllLanguages: {
3805
+ type: Function,
3806
+ required: true
3807
+ },
3808
+ setLanguage: {
3809
+ type: Function,
3810
+ required: true
3811
+ },
3812
+ config: {
3813
+ type: Object,
3814
+ required: true
3815
+ }
3816
+ },
3817
+ setup(props) {
3818
+ var _a;
3819
+ const previewOnlyByDefault = (_a = props.config.previewOnlyByDefault) != null ? _a : props.getReadOnly();
3820
+ const previewOnlyMode = ref(previewOnlyByDefault);
3821
+ const codemirrorHostRef = ref();
3822
+ const preview = ref(null);
3823
+ onMounted(() => {
3824
+ var _a2;
3825
+ while ((_a2 = codemirrorHostRef.value) == null ? void 0 : _a2.firstChild) {
3826
+ codemirrorHostRef.value.removeChild(codemirrorHostRef.value.firstChild);
3827
+ }
3828
+ if (codemirrorHostRef.value) {
3829
+ codemirrorHostRef.value.appendChild(props.codemirror.dom);
3830
+ }
3831
+ });
3832
+ watch(
3833
+ () => [props.text.value, props.language.value],
3834
+ () => {
3835
+ const result = props.config.renderPreview(
3836
+ props.language.value,
3837
+ props.text.value,
3838
+ (value) => preview.value = value
3839
+ );
3840
+ if (result) {
3841
+ preview.value = result;
3842
+ }
3843
+ const isAsyncPreview = result === void 0;
3844
+ if (isAsyncPreview && !preview.value) {
3845
+ preview.value = DOMPurify.sanitize(props.config.previewLoading);
3846
+ }
3847
+ if (result === null) {
3848
+ preview.value = null;
3849
+ }
3850
+ },
3851
+ { immediate: true }
3852
+ );
3853
+ const empty = () => {
3854
+ };
3855
+ return () => {
3856
+ var _a2;
3857
+ return /* @__PURE__ */ h(Fragment$1, null, /* @__PURE__ */ h("div", { class: "tools" }, /* @__PURE__ */ h(
3858
+ LanguagePicker,
3859
+ {
3860
+ language: props.language,
3861
+ config: props.config,
3862
+ setLanguage: props.setLanguage,
3863
+ getAllLanguages: props.getAllLanguages,
3864
+ getReadOnly: props.getReadOnly
3865
+ }
3866
+ ), /* @__PURE__ */ h("div", { class: "tools-button-group" }, /* @__PURE__ */ h(
3867
+ CopyButton,
3868
+ {
3869
+ copyIcon: props.config.copyIcon,
3870
+ copyText: props.config.copyText,
3871
+ onCopy: (_a2 = props.config.onCopy) != null ? _a2 : empty,
3872
+ text: props.text.value
3873
+ }
3874
+ ), preview.value ? /* @__PURE__ */ h(
3875
+ "button",
3876
+ {
3877
+ class: "preview-toggle-button",
3878
+ onClick: () => previewOnlyMode.value = !previewOnlyMode.value
3879
+ },
3880
+ /* @__PURE__ */ h(
3881
+ Icon,
3882
+ {
3883
+ icon: props.config.previewToggleButton(
3884
+ previewOnlyMode.value
3885
+ )
3886
+ }
3887
+ )
3888
+ ) : null)), /* @__PURE__ */ h(
3889
+ "div",
3890
+ {
3891
+ ref: codemirrorHostRef,
3892
+ class: clsx(
3893
+ "codemirror-host",
3894
+ preview.value && previewOnlyMode.value && "hidden"
3895
+ )
3896
+ }
3897
+ ), /* @__PURE__ */ h(
3898
+ PreviewPanel,
3899
+ {
3900
+ text: props.text,
3901
+ language: props.language,
3902
+ config: props.config,
3903
+ previewOnlyMode,
3904
+ preview
3905
+ }
3906
+ ));
3907
+ };
3908
+ }
3909
+ });
3910
+
3911
+ class CodeMirrorBlock {
3912
+ constructor(node, view, getPos, loader, config) {
3913
+ this.node = node;
3914
+ this.view = view;
3915
+ this.getPos = getPos;
3916
+ this.loader = loader;
3917
+ this.config = config;
3918
+ this.selected = ref(false);
3919
+ this.language = ref("");
3920
+ this.text = ref("");
3921
+ this.updating = false;
3922
+ this.languageName = "";
3923
+ this.forwardUpdate = (update) => {
3924
+ var _a;
3925
+ if (this.updating || !this.cm.hasFocus) return;
3926
+ let offset = ((_a = this.getPos()) != null ? _a : 0) + 1;
3927
+ const { main } = update.state.selection;
3928
+ const selFrom = offset + main.from;
3929
+ const selTo = offset + main.to;
3930
+ const pmSel = this.view.state.selection;
3931
+ if (update.docChanged || pmSel.from !== selFrom || pmSel.to !== selTo) {
3932
+ const tr = this.view.state.tr;
3933
+ update.changes.iterChanges((fromA, toA, fromB, toB, text) => {
3934
+ if (text.length)
3935
+ tr.replaceWith(
3936
+ offset + fromA,
3937
+ offset + toA,
3938
+ this.view.state.schema.text(text.toString())
3939
+ );
3940
+ else tr.delete(offset + fromA, offset + toA);
3941
+ offset += toB - fromB - (toA - fromA);
3942
+ });
3943
+ tr.setSelection(TextSelection.create(tr.doc, selFrom, selTo));
3944
+ this.view.dispatch(tr);
3945
+ }
3946
+ };
3947
+ this.createApp = () => {
3948
+ return createApp(CodeBlock, {
3949
+ text: this.text,
3950
+ selected: this.selected,
3951
+ codemirror: this.cm,
3952
+ language: this.language,
3953
+ getAllLanguages: this.getAllLanguages,
3954
+ getReadOnly: () => !this.view.editable,
3955
+ setLanguage: this.setLanguage,
3956
+ config: this.config
3957
+ });
3958
+ };
3959
+ this.codeMirrorKeymap = () => {
3960
+ const view = this.view;
3961
+ return [
3962
+ { key: "ArrowUp", run: () => this.maybeEscape("line", -1) },
3963
+ { key: "ArrowLeft", run: () => this.maybeEscape("char", -1) },
3964
+ { key: "ArrowDown", run: () => this.maybeEscape("line", 1) },
3965
+ { key: "ArrowRight", run: () => this.maybeEscape("char", 1) },
3966
+ {
3967
+ key: "Mod-Enter",
3968
+ run: () => {
3969
+ if (!exitCode(view.state, view.dispatch)) return false;
3970
+ view.focus();
3971
+ return true;
3972
+ }
3973
+ },
3974
+ { key: "Mod-z", run: () => undo(view.state, view.dispatch) },
3975
+ { key: "Shift-Mod-z", run: () => redo(view.state, view.dispatch) },
3976
+ { key: "Mod-y", run: () => redo(view.state, view.dispatch) },
3977
+ {
3978
+ key: "Backspace",
3979
+ run: () => {
3980
+ var _a;
3981
+ const ranges = this.cm.state.selection.ranges;
3982
+ if (ranges.length > 1) return false;
3983
+ const selection = ranges[0];
3984
+ if (selection && (!selection.empty || selection.anchor > 0))
3985
+ return false;
3986
+ if (this.cm.state.doc.lines >= 2) return false;
3987
+ const state = this.view.state;
3988
+ const pos = (_a = this.getPos()) != null ? _a : 0;
3989
+ const tr = state.tr.replaceWith(
3990
+ pos,
3991
+ pos + this.node.nodeSize,
3992
+ state.schema.nodes.paragraph.createChecked({}, this.node.content)
3993
+ );
3994
+ tr.setSelection(TextSelection.near(tr.doc.resolve(pos)));
3995
+ this.view.dispatch(tr);
3996
+ this.view.focus();
3997
+ return true;
3998
+ }
3999
+ }
4000
+ ];
4001
+ };
4002
+ this.maybeEscape = (unit, dir) => {
4003
+ var _a;
4004
+ const { state } = this.cm;
4005
+ let main = state.selection.main;
4006
+ if (!main.empty) return false;
4007
+ if (unit === "line") main = state.doc.lineAt(main.head);
4008
+ if (dir < 0 ? main.from > 0 : main.to < state.doc.length) return false;
4009
+ const targetPos = ((_a = this.getPos()) != null ? _a : 0) + (dir < 0 ? 0 : this.node.nodeSize);
4010
+ const selection = TextSelection.near(
4011
+ this.view.state.doc.resolve(targetPos),
4012
+ dir
4013
+ );
4014
+ const tr = this.view.state.tr.setSelection(selection).scrollIntoView();
4015
+ this.view.dispatch(tr);
4016
+ this.view.focus();
4017
+ return true;
4018
+ };
4019
+ this.setLanguage = (language) => {
4020
+ var _a;
4021
+ this.view.dispatch(
4022
+ this.view.state.tr.setNodeAttribute(
4023
+ (_a = this.getPos()) != null ? _a : 0,
4024
+ "language",
4025
+ language
4026
+ )
4027
+ );
4028
+ };
4029
+ this.getAllLanguages = () => {
4030
+ return this.loader.getAll();
4031
+ };
4032
+ this.languageConf = new Compartment();
4033
+ this.readOnlyConf = new Compartment();
4034
+ this.cm = new EditorView({
4035
+ doc: this.node.textContent,
4036
+ root: this.view.root,
4037
+ extensions: [
4038
+ this.readOnlyConf.of(EditorState.readOnly.of(!this.view.editable)),
4039
+ drawSelection(),
4040
+ keymap.of(this.codeMirrorKeymap()),
4041
+ this.languageConf.of([]),
4042
+ EditorState.changeFilter.of(() => this.view.editable),
4043
+ ...config.extensions,
4044
+ EditorView.updateListener.of(this.forwardUpdate)
4045
+ ]
4046
+ });
4047
+ this.app = this.createApp();
4048
+ this.dom = this.createDom(this.app);
4049
+ this.disposeSelectedWatcher = watchEffect(() => {
4050
+ const isSelected = this.selected.value;
4051
+ if (isSelected) {
4052
+ this.dom.classList.add("selected");
4053
+ } else {
4054
+ this.dom.classList.remove("selected");
4055
+ }
4056
+ });
4057
+ this.updateLanguage();
4058
+ }
4059
+ createDom(app) {
4060
+ const dom = document.createElement("div");
4061
+ dom.className = "milkdown-code-block";
4062
+ this.text.value = this.node.textContent;
4063
+ app.mount(dom);
4064
+ return dom;
4065
+ }
4066
+ updateLanguage() {
4067
+ const languageName = this.node.attrs.language;
4068
+ if (languageName === this.languageName) return;
4069
+ this.language.value = languageName;
4070
+ const language = this.loader.load(languageName != null ? languageName : "");
4071
+ language.then((lang) => {
4072
+ if (lang) {
4073
+ this.cm.dispatch({
4074
+ effects: this.languageConf.reconfigure(lang)
4075
+ });
4076
+ this.languageName = languageName;
4077
+ }
4078
+ }).catch(console.error);
4079
+ }
4080
+ setSelection(anchor, head) {
4081
+ if (!this.cm.dom.isConnected) return;
4082
+ this.cm.focus();
4083
+ this.updating = true;
4084
+ this.cm.dispatch({ selection: { anchor, head } });
4085
+ this.updating = false;
4086
+ }
4087
+ update(node) {
4088
+ if (node.type !== this.node.type) return false;
4089
+ if (this.updating) return true;
4090
+ this.node = node;
4091
+ this.text.value = node.textContent;
4092
+ this.updateLanguage();
4093
+ if (this.view.editable === this.cm.state.readOnly) {
4094
+ this.cm.dispatch({
4095
+ effects: this.readOnlyConf.reconfigure(
4096
+ EditorState.readOnly.of(!this.view.editable)
4097
+ )
4098
+ });
4099
+ }
4100
+ const change = computeChange(this.cm.state.doc.toString(), node.textContent);
4101
+ if (change) {
4102
+ this.updating = true;
4103
+ this.cm.dispatch({
4104
+ changes: { from: change.from, to: change.to, insert: change.text },
4105
+ scrollIntoView: true
4106
+ });
4107
+ this.updating = false;
4108
+ }
4109
+ return true;
4110
+ }
4111
+ selectNode() {
4112
+ this.selected.value = true;
4113
+ this.cm.focus();
4114
+ }
4115
+ deselectNode() {
4116
+ this.selected.value = false;
4117
+ }
4118
+ stopEvent() {
4119
+ return true;
4120
+ }
4121
+ destroy() {
4122
+ this.app.unmount();
4123
+ this.cm.destroy();
4124
+ this.disposeSelectedWatcher();
4125
+ }
4126
+ }
4127
+ function computeChange(oldVal, newVal) {
4128
+ if (oldVal === newVal) return null;
4129
+ let start = 0;
4130
+ let oldEnd = oldVal.length;
4131
+ let newEnd = newVal.length;
4132
+ while (start < oldEnd && oldVal.charCodeAt(start) === newVal.charCodeAt(start))
4133
+ ++start;
4134
+ while (oldEnd > start && newEnd > start && oldVal.charCodeAt(oldEnd - 1) === newVal.charCodeAt(newEnd - 1)) {
4135
+ oldEnd--;
4136
+ newEnd--;
4137
+ }
4138
+ return { from: start, to: oldEnd, text: newVal.slice(start, newEnd) };
4139
+ }
4140
+
4141
+ const codeBlockView = $view(
4142
+ codeBlockSchema.node,
4143
+ (ctx) => {
4144
+ const config = ctx.get(codeBlockConfig.key);
4145
+ const languageLoader = new LanguageLoader(config.languages);
4146
+ return (node, view, getPos) => new CodeMirrorBlock(node, view, getPos, languageLoader, config);
4147
+ }
4148
+ );
4149
+ withMeta(codeBlockView, {
4150
+ displayName: "NodeView<code-block>",
4151
+ group: "CodeBlock"
4152
+ });
4153
+
4154
+ const codeBlockComponent = [
4155
+ codeBlockView,
4156
+ codeBlockConfig
4157
+ ];
4158
+
4159
+ export { codeBlockComponent, codeBlockConfig, codeBlockView, defaultConfig };
4160
+ //# sourceMappingURL=index.js.map