@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.
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/lib/__internal__/components/icon.d.ts +24 -0
- package/lib/__internal__/components/icon.d.ts.map +1 -0
- package/lib/__internal__/components/image-input.d.ts +17 -0
- package/lib/__internal__/components/image-input.d.ts.map +1 -0
- package/lib/__internal__/keep-alive.d.ts +2 -0
- package/lib/__internal__/keep-alive.d.ts.map +1 -0
- package/lib/__internal__/meta.d.ts +3 -0
- package/lib/__internal__/meta.d.ts.map +1 -0
- package/lib/__tests__/setup.d.ts +2 -0
- package/lib/__tests__/setup.d.ts.map +1 -0
- package/lib/code-block/config.d.ts +23 -0
- package/lib/code-block/config.d.ts.map +1 -0
- package/lib/code-block/index.d.ts +5 -0
- package/lib/code-block/index.d.ts.map +1 -0
- package/lib/code-block/index.js +4160 -0
- package/lib/code-block/index.js.map +1 -0
- package/lib/code-block/view/components/code-block.d.ts +16 -0
- package/lib/code-block/view/components/code-block.d.ts.map +1 -0
- package/lib/code-block/view/components/copy-button.d.ts +9 -0
- package/lib/code-block/view/components/copy-button.d.ts.map +1 -0
- package/lib/code-block/view/components/language-picker.d.ts +5 -0
- package/lib/code-block/view/components/language-picker.d.ts.map +1 -0
- package/lib/code-block/view/components/preview-panel.d.ts +9 -0
- package/lib/code-block/view/components/preview-panel.d.ts.map +1 -0
- package/lib/code-block/view/index.d.ts +3 -0
- package/lib/code-block/view/index.d.ts.map +1 -0
- package/lib/code-block/view/loader.d.ts +13 -0
- package/lib/code-block/view/loader.d.ts.map +1 -0
- package/lib/code-block/view/node-view.d.ts +40 -0
- package/lib/code-block/view/node-view.d.ts.map +1 -0
- package/lib/image-block/config.d.ts +16 -0
- package/lib/image-block/config.d.ts.map +1 -0
- package/lib/image-block/index.d.ts +7 -0
- package/lib/image-block/index.d.ts.map +1 -0
- package/lib/image-block/index.js +660 -0
- package/lib/image-block/index.js.map +1 -0
- package/lib/image-block/remark-plugin.d.ts +2 -0
- package/lib/image-block/remark-plugin.d.ts.map +1 -0
- package/lib/image-block/schema.d.ts +3 -0
- package/lib/image-block/schema.d.ts.map +1 -0
- package/lib/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.d.ts +2 -0
- package/lib/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.d.ts.map +1 -0
- package/lib/image-block/view/components/image-block.d.ts +18 -0
- package/lib/image-block/view/components/image-block.d.ts.map +1 -0
- package/lib/image-block/view/components/image-viewer.d.ts +3 -0
- package/lib/image-block/view/components/image-viewer.d.ts.map +1 -0
- package/lib/image-block/view/index.d.ts +3 -0
- package/lib/image-block/view/index.d.ts.map +1 -0
- package/lib/image-inline/components/image-inline.d.ts +18 -0
- package/lib/image-inline/components/image-inline.d.ts.map +1 -0
- package/lib/image-inline/config.d.ts +11 -0
- package/lib/image-inline/config.d.ts.map +1 -0
- package/lib/image-inline/index.d.ts +5 -0
- package/lib/image-inline/index.d.ts.map +1 -0
- package/lib/image-inline/index.js +377 -0
- package/lib/image-inline/index.js.map +1 -0
- package/lib/image-inline/view.d.ts +3 -0
- package/lib/image-inline/view.d.ts.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +35 -0
- package/lib/index.js.map +1 -0
- package/lib/link-tooltip/command.d.ts +2 -0
- package/lib/link-tooltip/command.d.ts.map +1 -0
- package/lib/link-tooltip/configure.d.ts +3 -0
- package/lib/link-tooltip/configure.d.ts.map +1 -0
- package/lib/link-tooltip/edit/component.d.ts +11 -0
- package/lib/link-tooltip/edit/component.d.ts.map +1 -0
- package/lib/link-tooltip/edit/edit-configure.d.ts +3 -0
- package/lib/link-tooltip/edit/edit-configure.d.ts.map +1 -0
- package/lib/link-tooltip/edit/edit-view.d.ts +15 -0
- package/lib/link-tooltip/edit/edit-view.d.ts.map +1 -0
- package/lib/link-tooltip/index.d.ts +7 -0
- package/lib/link-tooltip/index.d.ts.map +1 -0
- package/lib/link-tooltip/index.js +2526 -0
- package/lib/link-tooltip/index.js.map +1 -0
- package/lib/link-tooltip/preview/component.d.ts +11 -0
- package/lib/link-tooltip/preview/component.d.ts.map +1 -0
- package/lib/link-tooltip/preview/preview-configure.d.ts +3 -0
- package/lib/link-tooltip/preview/preview-configure.d.ts.map +1 -0
- package/lib/link-tooltip/preview/preview-view.d.ts +14 -0
- package/lib/link-tooltip/preview/preview-view.d.ts.map +1 -0
- package/lib/link-tooltip/slices.d.ts +34 -0
- package/lib/link-tooltip/slices.d.ts.map +1 -0
- package/lib/link-tooltip/tooltips.d.ts +3 -0
- package/lib/link-tooltip/tooltips.d.ts.map +1 -0
- package/lib/link-tooltip/utils.d.ts +14 -0
- package/lib/link-tooltip/utils.d.ts.map +1 -0
- package/lib/list-item-block/component.d.ts +19 -0
- package/lib/list-item-block/component.d.ts.map +1 -0
- package/lib/list-item-block/config.d.ts +13 -0
- package/lib/list-item-block/config.d.ts.map +1 -0
- package/lib/list-item-block/index.d.ts +6 -0
- package/lib/list-item-block/index.d.ts.map +1 -0
- package/lib/list-item-block/index.js +2149 -0
- package/lib/list-item-block/index.js.map +1 -0
- package/lib/list-item-block/view.d.ts +3 -0
- package/lib/list-item-block/view.d.ts.map +1 -0
- package/lib/table-block/config.d.ts +8 -0
- package/lib/table-block/config.d.ts.map +1 -0
- package/lib/table-block/dnd/calc-drag-over.d.ts +3 -0
- package/lib/table-block/dnd/calc-drag-over.d.ts.map +1 -0
- package/lib/table-block/dnd/create-drag-handler.d.ts +5 -0
- package/lib/table-block/dnd/create-drag-handler.d.ts.map +1 -0
- package/lib/table-block/dnd/drag-over-handler.d.ts +3 -0
- package/lib/table-block/dnd/drag-over-handler.d.ts.map +1 -0
- package/lib/table-block/dnd/prepare-dnd-context.d.ts +3 -0
- package/lib/table-block/dnd/prepare-dnd-context.d.ts.map +1 -0
- package/lib/table-block/dnd/preview.d.ts +3 -0
- package/lib/table-block/dnd/preview.d.ts.map +1 -0
- package/lib/table-block/index.d.ts +5 -0
- package/lib/table-block/index.d.ts.map +1 -0
- package/lib/table-block/index.js +3961 -0
- package/lib/table-block/index.js.map +1 -0
- package/lib/table-block/view/component.d.ts +16 -0
- package/lib/table-block/view/component.d.ts.map +1 -0
- package/lib/table-block/view/drag.d.ts +7 -0
- package/lib/table-block/view/drag.d.ts.map +1 -0
- package/lib/table-block/view/index.d.ts +2 -0
- package/lib/table-block/view/index.d.ts.map +1 -0
- package/lib/table-block/view/operation.d.ts +11 -0
- package/lib/table-block/view/operation.d.ts.map +1 -0
- package/lib/table-block/view/pointer.d.ts +7 -0
- package/lib/table-block/view/pointer.d.ts.map +1 -0
- package/lib/table-block/view/types.d.ts +32 -0
- package/lib/table-block/view/types.d.ts.map +1 -0
- package/lib/table-block/view/utils.d.ts +21 -0
- package/lib/table-block/view/utils.d.ts.map +1 -0
- package/lib/table-block/view/view.d.ts +22 -0
- package/lib/table-block/view/view.d.ts.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +110 -0
- package/src/__internal__/components/icon.tsx +38 -0
- package/src/__internal__/components/image-input.tsx +182 -0
- package/src/__internal__/keep-alive.ts +3 -0
- package/src/__internal__/meta.ts +15 -0
- package/src/__tests__/setup.ts +6 -0
- package/src/code-block/config.ts +54 -0
- package/src/code-block/index.ts +12 -0
- package/src/code-block/view/components/code-block.tsx +170 -0
- package/src/code-block/view/components/copy-button.tsx +96 -0
- package/src/code-block/view/components/language-picker.tsx +239 -0
- package/src/code-block/view/components/preview-panel.tsx +79 -0
- package/src/code-block/view/index.ts +24 -0
- package/src/code-block/view/loader.ts +40 -0
- package/src/code-block/view/node-view.ts +310 -0
- package/src/image-block/config.ts +37 -0
- package/src/image-block/index.ts +18 -0
- package/src/image-block/remark-plugin.ts +51 -0
- package/src/image-block/schema.ts +71 -0
- package/src/image-block/view/components/__tests__/image-viewer.onImageLoadError.spec.tsx +42 -0
- package/src/image-block/view/components/image-block.tsx +80 -0
- package/src/image-block/view/components/image-viewer.tsx +186 -0
- package/src/image-block/view/index.ts +111 -0
- package/src/image-inline/components/image-inline.tsx +85 -0
- package/src/image-inline/config.ts +30 -0
- package/src/image-inline/index.ts +12 -0
- package/src/image-inline/view.ts +109 -0
- package/src/index.ts +1 -0
- package/src/link-tooltip/command.ts +19 -0
- package/src/link-tooltip/configure.ts +9 -0
- package/src/link-tooltip/edit/component.tsx +82 -0
- package/src/link-tooltip/edit/edit-configure.ts +29 -0
- package/src/link-tooltip/edit/edit-view.ts +165 -0
- package/src/link-tooltip/index.ts +19 -0
- package/src/link-tooltip/preview/component.tsx +87 -0
- package/src/link-tooltip/preview/preview-configure.ts +65 -0
- package/src/link-tooltip/preview/preview-view.ts +101 -0
- package/src/link-tooltip/slices.ts +69 -0
- package/src/link-tooltip/tooltips.ts +22 -0
- package/src/link-tooltip/utils.ts +56 -0
- package/src/list-item-block/component.tsx +133 -0
- package/src/list-item-block/config.ts +39 -0
- package/src/list-item-block/index.ts +13 -0
- package/src/list-item-block/view.ts +130 -0
- package/src/table-block/config.ts +53 -0
- package/src/table-block/dnd/calc-drag-over.ts +46 -0
- package/src/table-block/dnd/create-drag-handler.ts +99 -0
- package/src/table-block/dnd/drag-over-handler.ts +113 -0
- package/src/table-block/dnd/prepare-dnd-context.ts +46 -0
- package/src/table-block/dnd/preview.ts +58 -0
- package/src/table-block/index.ts +9 -0
- package/src/table-block/view/component.tsx +219 -0
- package/src/table-block/view/drag.ts +121 -0
- package/src/table-block/view/index.ts +1 -0
- package/src/table-block/view/operation.ts +148 -0
- package/src/table-block/view/pointer.ts +165 -0
- package/src/table-block/view/types.ts +35 -0
- package/src/table-block/view/utils.ts +192 -0
- package/src/table-block/view/view.ts +165 -0
|
@@ -0,0 +1,3961 @@
|
|
|
1
|
+
import { $ctx, $view } from '@jvs-milkdown/utils';
|
|
2
|
+
import { selectColCommand, moveColCommand, selectRowCommand, moveRowCommand, setAlignCommand, deleteSelectedCellsCommand, addColAfterCommand, addColBeforeCommand, addRowAfterCommand, addRowBeforeCommand, tableSchema } from '@jvs-milkdown/preset-gfm';
|
|
3
|
+
import { findParent } from '@jvs-milkdown/prose';
|
|
4
|
+
import { h, onMounted, onUnmounted, defineComponent, ref, shallowRef, createApp } from 'vue';
|
|
5
|
+
import clsx from 'clsx';
|
|
6
|
+
import DOMPurify from 'dompurify';
|
|
7
|
+
import { editorViewCtx, commandsCtx } from '@jvs-milkdown/core';
|
|
8
|
+
import { computePosition, offset } from '@floating-ui/dom';
|
|
9
|
+
import { throttle } from 'lodash-es';
|
|
10
|
+
|
|
11
|
+
var __defProp$1 = Object.defineProperty;
|
|
12
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
13
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
15
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
16
|
+
var __spreadValues$1 = (a, b) => {
|
|
17
|
+
for (var prop in b || (b = {}))
|
|
18
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
19
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
20
|
+
if (__getOwnPropSymbols$1)
|
|
21
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
22
|
+
if (__propIsEnum$1.call(b, prop))
|
|
23
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
24
|
+
}
|
|
25
|
+
return a;
|
|
26
|
+
};
|
|
27
|
+
function withMeta(plugin, meta) {
|
|
28
|
+
Object.assign(plugin, {
|
|
29
|
+
meta: __spreadValues$1({
|
|
30
|
+
package: "@jvs-milkdown/components"
|
|
31
|
+
}, meta)
|
|
32
|
+
});
|
|
33
|
+
return plugin;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
var __defProp = Object.defineProperty;
|
|
37
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
38
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
39
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
40
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
41
|
+
var __spreadValues = (a, b) => {
|
|
42
|
+
for (var prop in b || (b = {}))
|
|
43
|
+
if (__hasOwnProp.call(b, prop))
|
|
44
|
+
__defNormalProp(a, prop, b[prop]);
|
|
45
|
+
if (__getOwnPropSymbols)
|
|
46
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
47
|
+
if (__propIsEnum.call(b, prop))
|
|
48
|
+
__defNormalProp(a, prop, b[prop]);
|
|
49
|
+
}
|
|
50
|
+
return a;
|
|
51
|
+
};
|
|
52
|
+
const defaultTableBlockConfig = {
|
|
53
|
+
renderButton: (renderType) => {
|
|
54
|
+
switch (renderType) {
|
|
55
|
+
case "add_row":
|
|
56
|
+
return "+";
|
|
57
|
+
case "add_col":
|
|
58
|
+
return "+";
|
|
59
|
+
case "delete_row":
|
|
60
|
+
return "-";
|
|
61
|
+
case "delete_col":
|
|
62
|
+
return "-";
|
|
63
|
+
case "align_col_left":
|
|
64
|
+
return "left";
|
|
65
|
+
case "align_col_center":
|
|
66
|
+
return "center";
|
|
67
|
+
case "align_col_right":
|
|
68
|
+
return "right";
|
|
69
|
+
case "col_drag_handle":
|
|
70
|
+
return "=";
|
|
71
|
+
case "row_drag_handle":
|
|
72
|
+
return "=";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const tableBlockConfig = $ctx(
|
|
77
|
+
__spreadValues({}, defaultTableBlockConfig),
|
|
78
|
+
"tableBlockConfigCtx"
|
|
79
|
+
);
|
|
80
|
+
withMeta(tableBlockConfig, {
|
|
81
|
+
displayName: "Config<table-block>",
|
|
82
|
+
group: "TableBlock"
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
function findDiffStart(a, b, pos) {
|
|
86
|
+
for (let i = 0;; i++) {
|
|
87
|
+
if (i == a.childCount || i == b.childCount)
|
|
88
|
+
return a.childCount == b.childCount ? null : pos;
|
|
89
|
+
let childA = a.child(i), childB = b.child(i);
|
|
90
|
+
if (childA == childB) {
|
|
91
|
+
pos += childA.nodeSize;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (!childA.sameMarkup(childB))
|
|
95
|
+
return pos;
|
|
96
|
+
if (childA.isText && childA.text != childB.text) {
|
|
97
|
+
for (let j = 0; childA.text[j] == childB.text[j]; j++)
|
|
98
|
+
pos++;
|
|
99
|
+
return pos;
|
|
100
|
+
}
|
|
101
|
+
if (childA.content.size || childB.content.size) {
|
|
102
|
+
let inner = findDiffStart(childA.content, childB.content, pos + 1);
|
|
103
|
+
if (inner != null)
|
|
104
|
+
return inner;
|
|
105
|
+
}
|
|
106
|
+
pos += childA.nodeSize;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function findDiffEnd(a, b, posA, posB) {
|
|
110
|
+
for (let iA = a.childCount, iB = b.childCount;;) {
|
|
111
|
+
if (iA == 0 || iB == 0)
|
|
112
|
+
return iA == iB ? null : { a: posA, b: posB };
|
|
113
|
+
let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
|
|
114
|
+
if (childA == childB) {
|
|
115
|
+
posA -= size;
|
|
116
|
+
posB -= size;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!childA.sameMarkup(childB))
|
|
120
|
+
return { a: posA, b: posB };
|
|
121
|
+
if (childA.isText && childA.text != childB.text) {
|
|
122
|
+
let same = 0, minSize = Math.min(childA.text.length, childB.text.length);
|
|
123
|
+
while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
|
|
124
|
+
same++;
|
|
125
|
+
posA--;
|
|
126
|
+
posB--;
|
|
127
|
+
}
|
|
128
|
+
return { a: posA, b: posB };
|
|
129
|
+
}
|
|
130
|
+
if (childA.content.size || childB.content.size) {
|
|
131
|
+
let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
|
|
132
|
+
if (inner)
|
|
133
|
+
return inner;
|
|
134
|
+
}
|
|
135
|
+
posA -= size;
|
|
136
|
+
posB -= size;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
A fragment represents a node's collection of child nodes.
|
|
142
|
+
|
|
143
|
+
Like nodes, fragments are persistent data structures, and you
|
|
144
|
+
should not mutate them or their content. Rather, you create new
|
|
145
|
+
instances whenever needed. The API tries to make this easy.
|
|
146
|
+
*/
|
|
147
|
+
class Fragment {
|
|
148
|
+
/**
|
|
149
|
+
@internal
|
|
150
|
+
*/
|
|
151
|
+
constructor(
|
|
152
|
+
/**
|
|
153
|
+
The child nodes in this fragment.
|
|
154
|
+
*/
|
|
155
|
+
content, size) {
|
|
156
|
+
this.content = content;
|
|
157
|
+
this.size = size || 0;
|
|
158
|
+
if (size == null)
|
|
159
|
+
for (let i = 0; i < content.length; i++)
|
|
160
|
+
this.size += content[i].nodeSize;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
Invoke a callback for all descendant nodes between the given two
|
|
164
|
+
positions (relative to start of this fragment). Doesn't descend
|
|
165
|
+
into a node when the callback returns `false`.
|
|
166
|
+
*/
|
|
167
|
+
nodesBetween(from, to, f, nodeStart = 0, parent) {
|
|
168
|
+
for (let i = 0, pos = 0; pos < to; i++) {
|
|
169
|
+
let child = this.content[i], end = pos + child.nodeSize;
|
|
170
|
+
if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
|
|
171
|
+
let start = pos + 1;
|
|
172
|
+
child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
|
|
173
|
+
}
|
|
174
|
+
pos = end;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
Call the given callback for every descendant node. `pos` will be
|
|
179
|
+
relative to the start of the fragment. The callback may return
|
|
180
|
+
`false` to prevent traversal of a given node's children.
|
|
181
|
+
*/
|
|
182
|
+
descendants(f) {
|
|
183
|
+
this.nodesBetween(0, this.size, f);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
Extract the text between `from` and `to`. See the same method on
|
|
187
|
+
[`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
|
|
188
|
+
*/
|
|
189
|
+
textBetween(from, to, blockSeparator, leafText) {
|
|
190
|
+
let text = "", first = true;
|
|
191
|
+
this.nodesBetween(from, to, (node, pos) => {
|
|
192
|
+
let nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos)
|
|
193
|
+
: !node.isLeaf ? ""
|
|
194
|
+
: leafText ? (typeof leafText === "function" ? leafText(node) : leafText)
|
|
195
|
+
: node.type.spec.leafText ? node.type.spec.leafText(node)
|
|
196
|
+
: "";
|
|
197
|
+
if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) {
|
|
198
|
+
if (first)
|
|
199
|
+
first = false;
|
|
200
|
+
else
|
|
201
|
+
text += blockSeparator;
|
|
202
|
+
}
|
|
203
|
+
text += nodeText;
|
|
204
|
+
}, 0);
|
|
205
|
+
return text;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
Create a new fragment containing the combined content of this
|
|
209
|
+
fragment and the other.
|
|
210
|
+
*/
|
|
211
|
+
append(other) {
|
|
212
|
+
if (!other.size)
|
|
213
|
+
return this;
|
|
214
|
+
if (!this.size)
|
|
215
|
+
return other;
|
|
216
|
+
let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
|
|
217
|
+
if (last.isText && last.sameMarkup(first)) {
|
|
218
|
+
content[content.length - 1] = last.withText(last.text + first.text);
|
|
219
|
+
i = 1;
|
|
220
|
+
}
|
|
221
|
+
for (; i < other.content.length; i++)
|
|
222
|
+
content.push(other.content[i]);
|
|
223
|
+
return new Fragment(content, this.size + other.size);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
Cut out the sub-fragment between the two given positions.
|
|
227
|
+
*/
|
|
228
|
+
cut(from, to = this.size) {
|
|
229
|
+
if (from == 0 && to == this.size)
|
|
230
|
+
return this;
|
|
231
|
+
let result = [], size = 0;
|
|
232
|
+
if (to > from)
|
|
233
|
+
for (let i = 0, pos = 0; pos < to; i++) {
|
|
234
|
+
let child = this.content[i], end = pos + child.nodeSize;
|
|
235
|
+
if (end > from) {
|
|
236
|
+
if (pos < from || end > to) {
|
|
237
|
+
if (child.isText)
|
|
238
|
+
child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
|
|
239
|
+
else
|
|
240
|
+
child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
|
|
241
|
+
}
|
|
242
|
+
result.push(child);
|
|
243
|
+
size += child.nodeSize;
|
|
244
|
+
}
|
|
245
|
+
pos = end;
|
|
246
|
+
}
|
|
247
|
+
return new Fragment(result, size);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
@internal
|
|
251
|
+
*/
|
|
252
|
+
cutByIndex(from, to) {
|
|
253
|
+
if (from == to)
|
|
254
|
+
return Fragment.empty;
|
|
255
|
+
if (from == 0 && to == this.content.length)
|
|
256
|
+
return this;
|
|
257
|
+
return new Fragment(this.content.slice(from, to));
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
Create a new fragment in which the node at the given index is
|
|
261
|
+
replaced by the given node.
|
|
262
|
+
*/
|
|
263
|
+
replaceChild(index, node) {
|
|
264
|
+
let current = this.content[index];
|
|
265
|
+
if (current == node)
|
|
266
|
+
return this;
|
|
267
|
+
let copy = this.content.slice();
|
|
268
|
+
let size = this.size + node.nodeSize - current.nodeSize;
|
|
269
|
+
copy[index] = node;
|
|
270
|
+
return new Fragment(copy, size);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
Create a new fragment by prepending the given node to this
|
|
274
|
+
fragment.
|
|
275
|
+
*/
|
|
276
|
+
addToStart(node) {
|
|
277
|
+
return new Fragment([node].concat(this.content), this.size + node.nodeSize);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
Create a new fragment by appending the given node to this
|
|
281
|
+
fragment.
|
|
282
|
+
*/
|
|
283
|
+
addToEnd(node) {
|
|
284
|
+
return new Fragment(this.content.concat(node), this.size + node.nodeSize);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
Compare this fragment to another one.
|
|
288
|
+
*/
|
|
289
|
+
eq(other) {
|
|
290
|
+
if (this.content.length != other.content.length)
|
|
291
|
+
return false;
|
|
292
|
+
for (let i = 0; i < this.content.length; i++)
|
|
293
|
+
if (!this.content[i].eq(other.content[i]))
|
|
294
|
+
return false;
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
The first child of the fragment, or `null` if it is empty.
|
|
299
|
+
*/
|
|
300
|
+
get firstChild() { return this.content.length ? this.content[0] : null; }
|
|
301
|
+
/**
|
|
302
|
+
The last child of the fragment, or `null` if it is empty.
|
|
303
|
+
*/
|
|
304
|
+
get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; }
|
|
305
|
+
/**
|
|
306
|
+
The number of child nodes in this fragment.
|
|
307
|
+
*/
|
|
308
|
+
get childCount() { return this.content.length; }
|
|
309
|
+
/**
|
|
310
|
+
Get the child node at the given index. Raise an error when the
|
|
311
|
+
index is out of range.
|
|
312
|
+
*/
|
|
313
|
+
child(index) {
|
|
314
|
+
let found = this.content[index];
|
|
315
|
+
if (!found)
|
|
316
|
+
throw new RangeError("Index " + index + " out of range for " + this);
|
|
317
|
+
return found;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
Get the child node at the given index, if it exists.
|
|
321
|
+
*/
|
|
322
|
+
maybeChild(index) {
|
|
323
|
+
return this.content[index] || null;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
Call `f` for every child node, passing the node, its offset
|
|
327
|
+
into this parent node, and its index.
|
|
328
|
+
*/
|
|
329
|
+
forEach(f) {
|
|
330
|
+
for (let i = 0, p = 0; i < this.content.length; i++) {
|
|
331
|
+
let child = this.content[i];
|
|
332
|
+
f(child, p, i);
|
|
333
|
+
p += child.nodeSize;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
Find the first position at which this fragment and another
|
|
338
|
+
fragment differ, or `null` if they are the same.
|
|
339
|
+
*/
|
|
340
|
+
findDiffStart(other, pos = 0) {
|
|
341
|
+
return findDiffStart(this, other, pos);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
Find the first position, searching from the end, at which this
|
|
345
|
+
fragment and the given fragment differ, or `null` if they are
|
|
346
|
+
the same. Since this position will not be the same in both
|
|
347
|
+
nodes, an object with two separate positions is returned.
|
|
348
|
+
*/
|
|
349
|
+
findDiffEnd(other, pos = this.size, otherPos = other.size) {
|
|
350
|
+
return findDiffEnd(this, other, pos, otherPos);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
Find the index and inner offset corresponding to a given relative
|
|
354
|
+
position in this fragment. The result object will be reused
|
|
355
|
+
(overwritten) the next time the function is called. @internal
|
|
356
|
+
*/
|
|
357
|
+
findIndex(pos) {
|
|
358
|
+
if (pos == 0)
|
|
359
|
+
return retIndex(0, pos);
|
|
360
|
+
if (pos == this.size)
|
|
361
|
+
return retIndex(this.content.length, pos);
|
|
362
|
+
if (pos > this.size || pos < 0)
|
|
363
|
+
throw new RangeError(`Position ${pos} outside of fragment (${this})`);
|
|
364
|
+
for (let i = 0, curPos = 0;; i++) {
|
|
365
|
+
let cur = this.child(i), end = curPos + cur.nodeSize;
|
|
366
|
+
if (end >= pos) {
|
|
367
|
+
if (end == pos)
|
|
368
|
+
return retIndex(i + 1, end);
|
|
369
|
+
return retIndex(i, curPos);
|
|
370
|
+
}
|
|
371
|
+
curPos = end;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
Return a debugging string that describes this fragment.
|
|
376
|
+
*/
|
|
377
|
+
toString() { return "<" + this.toStringInner() + ">"; }
|
|
378
|
+
/**
|
|
379
|
+
@internal
|
|
380
|
+
*/
|
|
381
|
+
toStringInner() { return this.content.join(", "); }
|
|
382
|
+
/**
|
|
383
|
+
Create a JSON-serializeable representation of this fragment.
|
|
384
|
+
*/
|
|
385
|
+
toJSON() {
|
|
386
|
+
return this.content.length ? this.content.map(n => n.toJSON()) : null;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
Deserialize a fragment from its JSON representation.
|
|
390
|
+
*/
|
|
391
|
+
static fromJSON(schema, value) {
|
|
392
|
+
if (!value)
|
|
393
|
+
return Fragment.empty;
|
|
394
|
+
if (!Array.isArray(value))
|
|
395
|
+
throw new RangeError("Invalid input for Fragment.fromJSON");
|
|
396
|
+
return new Fragment(value.map(schema.nodeFromJSON));
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
Build a fragment from an array of nodes. Ensures that adjacent
|
|
400
|
+
text nodes with the same marks are joined together.
|
|
401
|
+
*/
|
|
402
|
+
static fromArray(array) {
|
|
403
|
+
if (!array.length)
|
|
404
|
+
return Fragment.empty;
|
|
405
|
+
let joined, size = 0;
|
|
406
|
+
for (let i = 0; i < array.length; i++) {
|
|
407
|
+
let node = array[i];
|
|
408
|
+
size += node.nodeSize;
|
|
409
|
+
if (i && node.isText && array[i - 1].sameMarkup(node)) {
|
|
410
|
+
if (!joined)
|
|
411
|
+
joined = array.slice(0, i);
|
|
412
|
+
joined[joined.length - 1] = node
|
|
413
|
+
.withText(joined[joined.length - 1].text + node.text);
|
|
414
|
+
}
|
|
415
|
+
else if (joined) {
|
|
416
|
+
joined.push(node);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return new Fragment(joined || array, size);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
Create a fragment from something that can be interpreted as a
|
|
423
|
+
set of nodes. For `null`, it returns the empty fragment. For a
|
|
424
|
+
fragment, the fragment itself. For a node or array of nodes, a
|
|
425
|
+
fragment containing those nodes.
|
|
426
|
+
*/
|
|
427
|
+
static from(nodes) {
|
|
428
|
+
if (!nodes)
|
|
429
|
+
return Fragment.empty;
|
|
430
|
+
if (nodes instanceof Fragment)
|
|
431
|
+
return nodes;
|
|
432
|
+
if (Array.isArray(nodes))
|
|
433
|
+
return this.fromArray(nodes);
|
|
434
|
+
if (nodes.attrs)
|
|
435
|
+
return new Fragment([nodes], nodes.nodeSize);
|
|
436
|
+
throw new RangeError("Can not convert " + nodes + " to a Fragment" +
|
|
437
|
+
(nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
An empty fragment. Intended to be reused whenever a node doesn't
|
|
442
|
+
contain anything (rather than allocating a new empty fragment for
|
|
443
|
+
each leaf node).
|
|
444
|
+
*/
|
|
445
|
+
Fragment.empty = new Fragment([], 0);
|
|
446
|
+
const found = { index: 0, offset: 0 };
|
|
447
|
+
function retIndex(index, offset) {
|
|
448
|
+
found.index = index;
|
|
449
|
+
found.offset = offset;
|
|
450
|
+
return found;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
|
|
455
|
+
given an invalid replacement.
|
|
456
|
+
*/
|
|
457
|
+
class ReplaceError extends Error {
|
|
458
|
+
}
|
|
459
|
+
/*
|
|
460
|
+
ReplaceError = function(this: any, message: string) {
|
|
461
|
+
let err = Error.call(this, message)
|
|
462
|
+
;(err as any).__proto__ = ReplaceError.prototype
|
|
463
|
+
return err
|
|
464
|
+
} as any
|
|
465
|
+
|
|
466
|
+
ReplaceError.prototype = Object.create(Error.prototype)
|
|
467
|
+
ReplaceError.prototype.constructor = ReplaceError
|
|
468
|
+
ReplaceError.prototype.name = "ReplaceError"
|
|
469
|
+
*/
|
|
470
|
+
/**
|
|
471
|
+
A slice represents a piece cut out of a larger document. It
|
|
472
|
+
stores not only a fragment, but also the depth up to which nodes on
|
|
473
|
+
both side are ‘open’ (cut through).
|
|
474
|
+
*/
|
|
475
|
+
class Slice {
|
|
476
|
+
/**
|
|
477
|
+
Create a slice. When specifying a non-zero open depth, you must
|
|
478
|
+
make sure that there are nodes of at least that depth at the
|
|
479
|
+
appropriate side of the fragment—i.e. if the fragment is an
|
|
480
|
+
empty paragraph node, `openStart` and `openEnd` can't be greater
|
|
481
|
+
than 1.
|
|
482
|
+
|
|
483
|
+
It is not necessary for the content of open nodes to conform to
|
|
484
|
+
the schema's content constraints, though it should be a valid
|
|
485
|
+
start/end/middle for such a node, depending on which sides are
|
|
486
|
+
open.
|
|
487
|
+
*/
|
|
488
|
+
constructor(
|
|
489
|
+
/**
|
|
490
|
+
The slice's content.
|
|
491
|
+
*/
|
|
492
|
+
content,
|
|
493
|
+
/**
|
|
494
|
+
The open depth at the start of the fragment.
|
|
495
|
+
*/
|
|
496
|
+
openStart,
|
|
497
|
+
/**
|
|
498
|
+
The open depth at the end.
|
|
499
|
+
*/
|
|
500
|
+
openEnd) {
|
|
501
|
+
this.content = content;
|
|
502
|
+
this.openStart = openStart;
|
|
503
|
+
this.openEnd = openEnd;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
The size this slice would add when inserted into a document.
|
|
507
|
+
*/
|
|
508
|
+
get size() {
|
|
509
|
+
return this.content.size - this.openStart - this.openEnd;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
@internal
|
|
513
|
+
*/
|
|
514
|
+
insertAt(pos, fragment) {
|
|
515
|
+
let content = insertInto(this.content, pos + this.openStart, fragment);
|
|
516
|
+
return content && new Slice(content, this.openStart, this.openEnd);
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
@internal
|
|
520
|
+
*/
|
|
521
|
+
removeBetween(from, to) {
|
|
522
|
+
return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
Tests whether this slice is equal to another slice.
|
|
526
|
+
*/
|
|
527
|
+
eq(other) {
|
|
528
|
+
return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
@internal
|
|
532
|
+
*/
|
|
533
|
+
toString() {
|
|
534
|
+
return this.content + "(" + this.openStart + "," + this.openEnd + ")";
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
Convert a slice to a JSON-serializable representation.
|
|
538
|
+
*/
|
|
539
|
+
toJSON() {
|
|
540
|
+
if (!this.content.size)
|
|
541
|
+
return null;
|
|
542
|
+
let json = { content: this.content.toJSON() };
|
|
543
|
+
if (this.openStart > 0)
|
|
544
|
+
json.openStart = this.openStart;
|
|
545
|
+
if (this.openEnd > 0)
|
|
546
|
+
json.openEnd = this.openEnd;
|
|
547
|
+
return json;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
Deserialize a slice from its JSON representation.
|
|
551
|
+
*/
|
|
552
|
+
static fromJSON(schema, json) {
|
|
553
|
+
if (!json)
|
|
554
|
+
return Slice.empty;
|
|
555
|
+
let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
|
|
556
|
+
if (typeof openStart != "number" || typeof openEnd != "number")
|
|
557
|
+
throw new RangeError("Invalid input for Slice.fromJSON");
|
|
558
|
+
return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
Create a slice from a fragment by taking the maximum possible
|
|
562
|
+
open value on both side of the fragment.
|
|
563
|
+
*/
|
|
564
|
+
static maxOpen(fragment, openIsolating = true) {
|
|
565
|
+
let openStart = 0, openEnd = 0;
|
|
566
|
+
for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
|
|
567
|
+
openStart++;
|
|
568
|
+
for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
|
|
569
|
+
openEnd++;
|
|
570
|
+
return new Slice(fragment, openStart, openEnd);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
The empty slice.
|
|
575
|
+
*/
|
|
576
|
+
Slice.empty = new Slice(Fragment.empty, 0, 0);
|
|
577
|
+
function removeRange(content, from, to) {
|
|
578
|
+
let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
|
|
579
|
+
let { index: indexTo, offset: offsetTo } = content.findIndex(to);
|
|
580
|
+
if (offset == from || child.isText) {
|
|
581
|
+
if (offsetTo != to && !content.child(indexTo).isText)
|
|
582
|
+
throw new RangeError("Removing non-flat range");
|
|
583
|
+
return content.cut(0, from).append(content.cut(to));
|
|
584
|
+
}
|
|
585
|
+
if (index != indexTo)
|
|
586
|
+
throw new RangeError("Removing non-flat range");
|
|
587
|
+
return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
|
|
588
|
+
}
|
|
589
|
+
function insertInto(content, dist, insert, parent) {
|
|
590
|
+
let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
|
|
591
|
+
if (offset == dist || child.isText) {
|
|
592
|
+
if (parent && !parent.canReplace(index, index, insert))
|
|
593
|
+
return null;
|
|
594
|
+
return content.cut(0, dist).append(insert).append(content.cut(dist));
|
|
595
|
+
}
|
|
596
|
+
let inner = insertInto(child.content, dist - offset - 1, insert, child);
|
|
597
|
+
return inner && content.replaceChild(index, child.copy(inner));
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Recovery values encode a range index and an offset. They are
|
|
601
|
+
// represented as numbers, because tons of them will be created when
|
|
602
|
+
// mapping, for example, a large number of decorations. The number's
|
|
603
|
+
// lower 16 bits provide the index, the remaining bits the offset.
|
|
604
|
+
//
|
|
605
|
+
// Note: We intentionally don't use bit shift operators to en- and
|
|
606
|
+
// decode these, since those clip to 32 bits, which we might in rare
|
|
607
|
+
// cases want to overflow. A 64-bit float can represent 48-bit
|
|
608
|
+
// integers precisely.
|
|
609
|
+
const lower16 = 0xffff;
|
|
610
|
+
const factor16 = Math.pow(2, 16);
|
|
611
|
+
function makeRecover(index, offset) { return index + offset * factor16; }
|
|
612
|
+
function recoverIndex(value) { return value & lower16; }
|
|
613
|
+
function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
|
|
614
|
+
const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
|
|
615
|
+
/**
|
|
616
|
+
An object representing a mapped position with extra
|
|
617
|
+
information.
|
|
618
|
+
*/
|
|
619
|
+
class MapResult {
|
|
620
|
+
/**
|
|
621
|
+
@internal
|
|
622
|
+
*/
|
|
623
|
+
constructor(
|
|
624
|
+
/**
|
|
625
|
+
The mapped version of the position.
|
|
626
|
+
*/
|
|
627
|
+
pos,
|
|
628
|
+
/**
|
|
629
|
+
@internal
|
|
630
|
+
*/
|
|
631
|
+
delInfo,
|
|
632
|
+
/**
|
|
633
|
+
@internal
|
|
634
|
+
*/
|
|
635
|
+
recover) {
|
|
636
|
+
this.pos = pos;
|
|
637
|
+
this.delInfo = delInfo;
|
|
638
|
+
this.recover = recover;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
Tells you whether the position was deleted, that is, whether the
|
|
642
|
+
step removed the token on the side queried (via the `assoc`)
|
|
643
|
+
argument from the document.
|
|
644
|
+
*/
|
|
645
|
+
get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
|
|
646
|
+
/**
|
|
647
|
+
Tells you whether the token before the mapped position was deleted.
|
|
648
|
+
*/
|
|
649
|
+
get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
|
|
650
|
+
/**
|
|
651
|
+
True when the token after the mapped position was deleted.
|
|
652
|
+
*/
|
|
653
|
+
get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
|
|
654
|
+
/**
|
|
655
|
+
Tells whether any of the steps mapped through deletes across the
|
|
656
|
+
position (including both the token before and after the
|
|
657
|
+
position).
|
|
658
|
+
*/
|
|
659
|
+
get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
A map describing the deletions and insertions made by a step, which
|
|
663
|
+
can be used to find the correspondence between positions in the
|
|
664
|
+
pre-step version of a document and the same position in the
|
|
665
|
+
post-step version.
|
|
666
|
+
*/
|
|
667
|
+
class StepMap {
|
|
668
|
+
/**
|
|
669
|
+
Create a position map. The modifications to the document are
|
|
670
|
+
represented as an array of numbers, in which each group of three
|
|
671
|
+
represents a modified chunk as `[start, oldSize, newSize]`.
|
|
672
|
+
*/
|
|
673
|
+
constructor(
|
|
674
|
+
/**
|
|
675
|
+
@internal
|
|
676
|
+
*/
|
|
677
|
+
ranges,
|
|
678
|
+
/**
|
|
679
|
+
@internal
|
|
680
|
+
*/
|
|
681
|
+
inverted = false) {
|
|
682
|
+
this.ranges = ranges;
|
|
683
|
+
this.inverted = inverted;
|
|
684
|
+
if (!ranges.length && StepMap.empty)
|
|
685
|
+
return StepMap.empty;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
@internal
|
|
689
|
+
*/
|
|
690
|
+
recover(value) {
|
|
691
|
+
let diff = 0, index = recoverIndex(value);
|
|
692
|
+
if (!this.inverted)
|
|
693
|
+
for (let i = 0; i < index; i++)
|
|
694
|
+
diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
|
|
695
|
+
return this.ranges[index * 3] + diff + recoverOffset(value);
|
|
696
|
+
}
|
|
697
|
+
mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
|
|
698
|
+
map(pos, assoc = 1) { return this._map(pos, assoc, true); }
|
|
699
|
+
/**
|
|
700
|
+
@internal
|
|
701
|
+
*/
|
|
702
|
+
_map(pos, assoc, simple) {
|
|
703
|
+
let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
704
|
+
for (let i = 0; i < this.ranges.length; i += 3) {
|
|
705
|
+
let start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
706
|
+
if (start > pos)
|
|
707
|
+
break;
|
|
708
|
+
let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
|
|
709
|
+
if (pos <= end) {
|
|
710
|
+
let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
|
|
711
|
+
let result = start + diff + (side < 0 ? 0 : newSize);
|
|
712
|
+
if (simple)
|
|
713
|
+
return result;
|
|
714
|
+
let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
|
|
715
|
+
let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
|
|
716
|
+
if (assoc < 0 ? pos != start : pos != end)
|
|
717
|
+
del |= DEL_SIDE;
|
|
718
|
+
return new MapResult(result, del, recover);
|
|
719
|
+
}
|
|
720
|
+
diff += newSize - oldSize;
|
|
721
|
+
}
|
|
722
|
+
return simple ? pos + diff : new MapResult(pos + diff, 0, null);
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
@internal
|
|
726
|
+
*/
|
|
727
|
+
touches(pos, recover) {
|
|
728
|
+
let diff = 0, index = recoverIndex(recover);
|
|
729
|
+
let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
730
|
+
for (let i = 0; i < this.ranges.length; i += 3) {
|
|
731
|
+
let start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
732
|
+
if (start > pos)
|
|
733
|
+
break;
|
|
734
|
+
let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
|
|
735
|
+
if (pos <= end && i == index * 3)
|
|
736
|
+
return true;
|
|
737
|
+
diff += this.ranges[i + newIndex] - oldSize;
|
|
738
|
+
}
|
|
739
|
+
return false;
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
Calls the given function on each of the changed ranges included in
|
|
743
|
+
this map.
|
|
744
|
+
*/
|
|
745
|
+
forEach(f) {
|
|
746
|
+
let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
747
|
+
for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
|
|
748
|
+
let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
|
|
749
|
+
let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
|
|
750
|
+
f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
|
|
751
|
+
diff += newSize - oldSize;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
Create an inverted version of this map. The result can be used to
|
|
756
|
+
map positions in the post-step document to the pre-step document.
|
|
757
|
+
*/
|
|
758
|
+
invert() {
|
|
759
|
+
return new StepMap(this.ranges, !this.inverted);
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
@internal
|
|
763
|
+
*/
|
|
764
|
+
toString() {
|
|
765
|
+
return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
Create a map that moves all positions by offset `n` (which may be
|
|
769
|
+
negative). This can be useful when applying steps meant for a
|
|
770
|
+
sub-document to a larger document, or vice-versa.
|
|
771
|
+
*/
|
|
772
|
+
static offset(n) {
|
|
773
|
+
return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
A StepMap that contains no changed ranges.
|
|
778
|
+
*/
|
|
779
|
+
StepMap.empty = new StepMap([]);
|
|
780
|
+
|
|
781
|
+
const stepsByID = Object.create(null);
|
|
782
|
+
/**
|
|
783
|
+
A step object represents an atomic change. It generally applies
|
|
784
|
+
only to the document it was created for, since the positions
|
|
785
|
+
stored in it will only make sense for that document.
|
|
786
|
+
|
|
787
|
+
New steps are defined by creating classes that extend `Step`,
|
|
788
|
+
overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
|
|
789
|
+
methods, and registering your class with a unique
|
|
790
|
+
JSON-serialization identifier using
|
|
791
|
+
[`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
|
|
792
|
+
*/
|
|
793
|
+
class Step {
|
|
794
|
+
/**
|
|
795
|
+
Get the step map that represents the changes made by this step,
|
|
796
|
+
and which can be used to transform between positions in the old
|
|
797
|
+
and the new document.
|
|
798
|
+
*/
|
|
799
|
+
getMap() { return StepMap.empty; }
|
|
800
|
+
/**
|
|
801
|
+
Try to merge this step with another one, to be applied directly
|
|
802
|
+
after it. Returns the merged step when possible, null if the
|
|
803
|
+
steps can't be merged.
|
|
804
|
+
*/
|
|
805
|
+
merge(other) { return null; }
|
|
806
|
+
/**
|
|
807
|
+
Deserialize a step from its JSON representation. Will call
|
|
808
|
+
through to the step class' own implementation of this method.
|
|
809
|
+
*/
|
|
810
|
+
static fromJSON(schema, json) {
|
|
811
|
+
if (!json || !json.stepType)
|
|
812
|
+
throw new RangeError("Invalid input for Step.fromJSON");
|
|
813
|
+
let type = stepsByID[json.stepType];
|
|
814
|
+
if (!type)
|
|
815
|
+
throw new RangeError(`No step type ${json.stepType} defined`);
|
|
816
|
+
return type.fromJSON(schema, json);
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
To be able to serialize steps to JSON, each step needs a string
|
|
820
|
+
ID to attach to its JSON representation. Use this method to
|
|
821
|
+
register an ID for your step classes. Try to pick something
|
|
822
|
+
that's unlikely to clash with steps from other modules.
|
|
823
|
+
*/
|
|
824
|
+
static jsonID(id, stepClass) {
|
|
825
|
+
if (id in stepsByID)
|
|
826
|
+
throw new RangeError("Duplicate use of step JSON ID " + id);
|
|
827
|
+
stepsByID[id] = stepClass;
|
|
828
|
+
stepClass.prototype.jsonID = id;
|
|
829
|
+
return stepClass;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
|
|
834
|
+
new document or a failure value.
|
|
835
|
+
*/
|
|
836
|
+
class StepResult {
|
|
837
|
+
/**
|
|
838
|
+
@internal
|
|
839
|
+
*/
|
|
840
|
+
constructor(
|
|
841
|
+
/**
|
|
842
|
+
The transformed document, if successful.
|
|
843
|
+
*/
|
|
844
|
+
doc,
|
|
845
|
+
/**
|
|
846
|
+
The failure message, if unsuccessful.
|
|
847
|
+
*/
|
|
848
|
+
failed) {
|
|
849
|
+
this.doc = doc;
|
|
850
|
+
this.failed = failed;
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
Create a successful step result.
|
|
854
|
+
*/
|
|
855
|
+
static ok(doc) { return new StepResult(doc, null); }
|
|
856
|
+
/**
|
|
857
|
+
Create a failed step result.
|
|
858
|
+
*/
|
|
859
|
+
static fail(message) { return new StepResult(null, message); }
|
|
860
|
+
/**
|
|
861
|
+
Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
|
|
862
|
+
arguments. Create a successful result if it succeeds, and a
|
|
863
|
+
failed one if it throws a `ReplaceError`.
|
|
864
|
+
*/
|
|
865
|
+
static fromReplace(doc, from, to, slice) {
|
|
866
|
+
try {
|
|
867
|
+
return StepResult.ok(doc.replace(from, to, slice));
|
|
868
|
+
}
|
|
869
|
+
catch (e) {
|
|
870
|
+
if (e instanceof ReplaceError)
|
|
871
|
+
return StepResult.fail(e.message);
|
|
872
|
+
throw e;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function mapFragment(fragment, f, parent) {
|
|
878
|
+
let mapped = [];
|
|
879
|
+
for (let i = 0; i < fragment.childCount; i++) {
|
|
880
|
+
let child = fragment.child(i);
|
|
881
|
+
if (child.content.size)
|
|
882
|
+
child = child.copy(mapFragment(child.content, f, child));
|
|
883
|
+
if (child.isInline)
|
|
884
|
+
child = f(child, parent, i);
|
|
885
|
+
mapped.push(child);
|
|
886
|
+
}
|
|
887
|
+
return Fragment.fromArray(mapped);
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
Add a mark to all inline content between two positions.
|
|
891
|
+
*/
|
|
892
|
+
class AddMarkStep extends Step {
|
|
893
|
+
/**
|
|
894
|
+
Create a mark step.
|
|
895
|
+
*/
|
|
896
|
+
constructor(
|
|
897
|
+
/**
|
|
898
|
+
The start of the marked range.
|
|
899
|
+
*/
|
|
900
|
+
from,
|
|
901
|
+
/**
|
|
902
|
+
The end of the marked range.
|
|
903
|
+
*/
|
|
904
|
+
to,
|
|
905
|
+
/**
|
|
906
|
+
The mark to add.
|
|
907
|
+
*/
|
|
908
|
+
mark) {
|
|
909
|
+
super();
|
|
910
|
+
this.from = from;
|
|
911
|
+
this.to = to;
|
|
912
|
+
this.mark = mark;
|
|
913
|
+
}
|
|
914
|
+
apply(doc) {
|
|
915
|
+
let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
|
|
916
|
+
let parent = $from.node($from.sharedDepth(this.to));
|
|
917
|
+
let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
|
|
918
|
+
if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type))
|
|
919
|
+
return node;
|
|
920
|
+
return node.mark(this.mark.addToSet(node.marks));
|
|
921
|
+
}, parent), oldSlice.openStart, oldSlice.openEnd);
|
|
922
|
+
return StepResult.fromReplace(doc, this.from, this.to, slice);
|
|
923
|
+
}
|
|
924
|
+
invert() {
|
|
925
|
+
return new RemoveMarkStep(this.from, this.to, this.mark);
|
|
926
|
+
}
|
|
927
|
+
map(mapping) {
|
|
928
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
929
|
+
if (from.deleted && to.deleted || from.pos >= to.pos)
|
|
930
|
+
return null;
|
|
931
|
+
return new AddMarkStep(from.pos, to.pos, this.mark);
|
|
932
|
+
}
|
|
933
|
+
merge(other) {
|
|
934
|
+
if (other instanceof AddMarkStep &&
|
|
935
|
+
other.mark.eq(this.mark) &&
|
|
936
|
+
this.from <= other.to && this.to >= other.from)
|
|
937
|
+
return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
|
|
938
|
+
return null;
|
|
939
|
+
}
|
|
940
|
+
toJSON() {
|
|
941
|
+
return { stepType: "addMark", mark: this.mark.toJSON(),
|
|
942
|
+
from: this.from, to: this.to };
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
@internal
|
|
946
|
+
*/
|
|
947
|
+
static fromJSON(schema, json) {
|
|
948
|
+
if (typeof json.from != "number" || typeof json.to != "number")
|
|
949
|
+
throw new RangeError("Invalid input for AddMarkStep.fromJSON");
|
|
950
|
+
return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
Step.jsonID("addMark", AddMarkStep);
|
|
954
|
+
/**
|
|
955
|
+
Remove a mark from all inline content between two positions.
|
|
956
|
+
*/
|
|
957
|
+
class RemoveMarkStep extends Step {
|
|
958
|
+
/**
|
|
959
|
+
Create a mark-removing step.
|
|
960
|
+
*/
|
|
961
|
+
constructor(
|
|
962
|
+
/**
|
|
963
|
+
The start of the unmarked range.
|
|
964
|
+
*/
|
|
965
|
+
from,
|
|
966
|
+
/**
|
|
967
|
+
The end of the unmarked range.
|
|
968
|
+
*/
|
|
969
|
+
to,
|
|
970
|
+
/**
|
|
971
|
+
The mark to remove.
|
|
972
|
+
*/
|
|
973
|
+
mark) {
|
|
974
|
+
super();
|
|
975
|
+
this.from = from;
|
|
976
|
+
this.to = to;
|
|
977
|
+
this.mark = mark;
|
|
978
|
+
}
|
|
979
|
+
apply(doc) {
|
|
980
|
+
let oldSlice = doc.slice(this.from, this.to);
|
|
981
|
+
let slice = new Slice(mapFragment(oldSlice.content, node => {
|
|
982
|
+
return node.mark(this.mark.removeFromSet(node.marks));
|
|
983
|
+
}, doc), oldSlice.openStart, oldSlice.openEnd);
|
|
984
|
+
return StepResult.fromReplace(doc, this.from, this.to, slice);
|
|
985
|
+
}
|
|
986
|
+
invert() {
|
|
987
|
+
return new AddMarkStep(this.from, this.to, this.mark);
|
|
988
|
+
}
|
|
989
|
+
map(mapping) {
|
|
990
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
991
|
+
if (from.deleted && to.deleted || from.pos >= to.pos)
|
|
992
|
+
return null;
|
|
993
|
+
return new RemoveMarkStep(from.pos, to.pos, this.mark);
|
|
994
|
+
}
|
|
995
|
+
merge(other) {
|
|
996
|
+
if (other instanceof RemoveMarkStep &&
|
|
997
|
+
other.mark.eq(this.mark) &&
|
|
998
|
+
this.from <= other.to && this.to >= other.from)
|
|
999
|
+
return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
|
|
1000
|
+
return null;
|
|
1001
|
+
}
|
|
1002
|
+
toJSON() {
|
|
1003
|
+
return { stepType: "removeMark", mark: this.mark.toJSON(),
|
|
1004
|
+
from: this.from, to: this.to };
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
@internal
|
|
1008
|
+
*/
|
|
1009
|
+
static fromJSON(schema, json) {
|
|
1010
|
+
if (typeof json.from != "number" || typeof json.to != "number")
|
|
1011
|
+
throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
|
|
1012
|
+
return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
Step.jsonID("removeMark", RemoveMarkStep);
|
|
1016
|
+
/**
|
|
1017
|
+
Add a mark to a specific node.
|
|
1018
|
+
*/
|
|
1019
|
+
class AddNodeMarkStep extends Step {
|
|
1020
|
+
/**
|
|
1021
|
+
Create a node mark step.
|
|
1022
|
+
*/
|
|
1023
|
+
constructor(
|
|
1024
|
+
/**
|
|
1025
|
+
The position of the target node.
|
|
1026
|
+
*/
|
|
1027
|
+
pos,
|
|
1028
|
+
/**
|
|
1029
|
+
The mark to add.
|
|
1030
|
+
*/
|
|
1031
|
+
mark) {
|
|
1032
|
+
super();
|
|
1033
|
+
this.pos = pos;
|
|
1034
|
+
this.mark = mark;
|
|
1035
|
+
}
|
|
1036
|
+
apply(doc) {
|
|
1037
|
+
let node = doc.nodeAt(this.pos);
|
|
1038
|
+
if (!node)
|
|
1039
|
+
return StepResult.fail("No node at mark step's position");
|
|
1040
|
+
let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
|
|
1041
|
+
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
1042
|
+
}
|
|
1043
|
+
invert(doc) {
|
|
1044
|
+
let node = doc.nodeAt(this.pos);
|
|
1045
|
+
if (node) {
|
|
1046
|
+
let newSet = this.mark.addToSet(node.marks);
|
|
1047
|
+
if (newSet.length == node.marks.length) {
|
|
1048
|
+
for (let i = 0; i < node.marks.length; i++)
|
|
1049
|
+
if (!node.marks[i].isInSet(newSet))
|
|
1050
|
+
return new AddNodeMarkStep(this.pos, node.marks[i]);
|
|
1051
|
+
return new AddNodeMarkStep(this.pos, this.mark);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
return new RemoveNodeMarkStep(this.pos, this.mark);
|
|
1055
|
+
}
|
|
1056
|
+
map(mapping) {
|
|
1057
|
+
let pos = mapping.mapResult(this.pos, 1);
|
|
1058
|
+
return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
|
|
1059
|
+
}
|
|
1060
|
+
toJSON() {
|
|
1061
|
+
return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
@internal
|
|
1065
|
+
*/
|
|
1066
|
+
static fromJSON(schema, json) {
|
|
1067
|
+
if (typeof json.pos != "number")
|
|
1068
|
+
throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
|
|
1069
|
+
return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
Step.jsonID("addNodeMark", AddNodeMarkStep);
|
|
1073
|
+
/**
|
|
1074
|
+
Remove a mark from a specific node.
|
|
1075
|
+
*/
|
|
1076
|
+
class RemoveNodeMarkStep extends Step {
|
|
1077
|
+
/**
|
|
1078
|
+
Create a mark-removing step.
|
|
1079
|
+
*/
|
|
1080
|
+
constructor(
|
|
1081
|
+
/**
|
|
1082
|
+
The position of the target node.
|
|
1083
|
+
*/
|
|
1084
|
+
pos,
|
|
1085
|
+
/**
|
|
1086
|
+
The mark to remove.
|
|
1087
|
+
*/
|
|
1088
|
+
mark) {
|
|
1089
|
+
super();
|
|
1090
|
+
this.pos = pos;
|
|
1091
|
+
this.mark = mark;
|
|
1092
|
+
}
|
|
1093
|
+
apply(doc) {
|
|
1094
|
+
let node = doc.nodeAt(this.pos);
|
|
1095
|
+
if (!node)
|
|
1096
|
+
return StepResult.fail("No node at mark step's position");
|
|
1097
|
+
let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
|
|
1098
|
+
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
1099
|
+
}
|
|
1100
|
+
invert(doc) {
|
|
1101
|
+
let node = doc.nodeAt(this.pos);
|
|
1102
|
+
if (!node || !this.mark.isInSet(node.marks))
|
|
1103
|
+
return this;
|
|
1104
|
+
return new AddNodeMarkStep(this.pos, this.mark);
|
|
1105
|
+
}
|
|
1106
|
+
map(mapping) {
|
|
1107
|
+
let pos = mapping.mapResult(this.pos, 1);
|
|
1108
|
+
return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
|
|
1109
|
+
}
|
|
1110
|
+
toJSON() {
|
|
1111
|
+
return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
@internal
|
|
1115
|
+
*/
|
|
1116
|
+
static fromJSON(schema, json) {
|
|
1117
|
+
if (typeof json.pos != "number")
|
|
1118
|
+
throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
|
|
1119
|
+
return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
Replace a part of the document with a slice of new content.
|
|
1126
|
+
*/
|
|
1127
|
+
class ReplaceStep extends Step {
|
|
1128
|
+
/**
|
|
1129
|
+
The given `slice` should fit the 'gap' between `from` and
|
|
1130
|
+
`to`—the depths must line up, and the surrounding nodes must be
|
|
1131
|
+
able to be joined with the open sides of the slice. When
|
|
1132
|
+
`structure` is true, the step will fail if the content between
|
|
1133
|
+
from and to is not just a sequence of closing and then opening
|
|
1134
|
+
tokens (this is to guard against rebased replace steps
|
|
1135
|
+
overwriting something they weren't supposed to).
|
|
1136
|
+
*/
|
|
1137
|
+
constructor(
|
|
1138
|
+
/**
|
|
1139
|
+
The start position of the replaced range.
|
|
1140
|
+
*/
|
|
1141
|
+
from,
|
|
1142
|
+
/**
|
|
1143
|
+
The end position of the replaced range.
|
|
1144
|
+
*/
|
|
1145
|
+
to,
|
|
1146
|
+
/**
|
|
1147
|
+
The slice to insert.
|
|
1148
|
+
*/
|
|
1149
|
+
slice,
|
|
1150
|
+
/**
|
|
1151
|
+
@internal
|
|
1152
|
+
*/
|
|
1153
|
+
structure = false) {
|
|
1154
|
+
super();
|
|
1155
|
+
this.from = from;
|
|
1156
|
+
this.to = to;
|
|
1157
|
+
this.slice = slice;
|
|
1158
|
+
this.structure = structure;
|
|
1159
|
+
}
|
|
1160
|
+
apply(doc) {
|
|
1161
|
+
if (this.structure && contentBetween(doc, this.from, this.to))
|
|
1162
|
+
return StepResult.fail("Structure replace would overwrite content");
|
|
1163
|
+
return StepResult.fromReplace(doc, this.from, this.to, this.slice);
|
|
1164
|
+
}
|
|
1165
|
+
getMap() {
|
|
1166
|
+
return new StepMap([this.from, this.to - this.from, this.slice.size]);
|
|
1167
|
+
}
|
|
1168
|
+
invert(doc) {
|
|
1169
|
+
return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
|
|
1170
|
+
}
|
|
1171
|
+
map(mapping) {
|
|
1172
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1173
|
+
if (from.deletedAcross && to.deletedAcross)
|
|
1174
|
+
return null;
|
|
1175
|
+
return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
|
|
1176
|
+
}
|
|
1177
|
+
merge(other) {
|
|
1178
|
+
if (!(other instanceof ReplaceStep) || other.structure || this.structure)
|
|
1179
|
+
return null;
|
|
1180
|
+
if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
|
|
1181
|
+
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
|
|
1182
|
+
: new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
|
|
1183
|
+
return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
|
|
1184
|
+
}
|
|
1185
|
+
else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
|
|
1186
|
+
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
|
|
1187
|
+
: new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
|
|
1188
|
+
return new ReplaceStep(other.from, this.to, slice, this.structure);
|
|
1189
|
+
}
|
|
1190
|
+
else {
|
|
1191
|
+
return null;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
toJSON() {
|
|
1195
|
+
let json = { stepType: "replace", from: this.from, to: this.to };
|
|
1196
|
+
if (this.slice.size)
|
|
1197
|
+
json.slice = this.slice.toJSON();
|
|
1198
|
+
if (this.structure)
|
|
1199
|
+
json.structure = true;
|
|
1200
|
+
return json;
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
@internal
|
|
1204
|
+
*/
|
|
1205
|
+
static fromJSON(schema, json) {
|
|
1206
|
+
if (typeof json.from != "number" || typeof json.to != "number")
|
|
1207
|
+
throw new RangeError("Invalid input for ReplaceStep.fromJSON");
|
|
1208
|
+
return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
Step.jsonID("replace", ReplaceStep);
|
|
1212
|
+
/**
|
|
1213
|
+
Replace a part of the document with a slice of content, but
|
|
1214
|
+
preserve a range of the replaced content by moving it into the
|
|
1215
|
+
slice.
|
|
1216
|
+
*/
|
|
1217
|
+
class ReplaceAroundStep extends Step {
|
|
1218
|
+
/**
|
|
1219
|
+
Create a replace-around step with the given range and gap.
|
|
1220
|
+
`insert` should be the point in the slice into which the content
|
|
1221
|
+
of the gap should be moved. `structure` has the same meaning as
|
|
1222
|
+
it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
|
|
1223
|
+
*/
|
|
1224
|
+
constructor(
|
|
1225
|
+
/**
|
|
1226
|
+
The start position of the replaced range.
|
|
1227
|
+
*/
|
|
1228
|
+
from,
|
|
1229
|
+
/**
|
|
1230
|
+
The end position of the replaced range.
|
|
1231
|
+
*/
|
|
1232
|
+
to,
|
|
1233
|
+
/**
|
|
1234
|
+
The start of preserved range.
|
|
1235
|
+
*/
|
|
1236
|
+
gapFrom,
|
|
1237
|
+
/**
|
|
1238
|
+
The end of preserved range.
|
|
1239
|
+
*/
|
|
1240
|
+
gapTo,
|
|
1241
|
+
/**
|
|
1242
|
+
The slice to insert.
|
|
1243
|
+
*/
|
|
1244
|
+
slice,
|
|
1245
|
+
/**
|
|
1246
|
+
The position in the slice where the preserved range should be
|
|
1247
|
+
inserted.
|
|
1248
|
+
*/
|
|
1249
|
+
insert,
|
|
1250
|
+
/**
|
|
1251
|
+
@internal
|
|
1252
|
+
*/
|
|
1253
|
+
structure = false) {
|
|
1254
|
+
super();
|
|
1255
|
+
this.from = from;
|
|
1256
|
+
this.to = to;
|
|
1257
|
+
this.gapFrom = gapFrom;
|
|
1258
|
+
this.gapTo = gapTo;
|
|
1259
|
+
this.slice = slice;
|
|
1260
|
+
this.insert = insert;
|
|
1261
|
+
this.structure = structure;
|
|
1262
|
+
}
|
|
1263
|
+
apply(doc) {
|
|
1264
|
+
if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
|
|
1265
|
+
contentBetween(doc, this.gapTo, this.to)))
|
|
1266
|
+
return StepResult.fail("Structure gap-replace would overwrite content");
|
|
1267
|
+
let gap = doc.slice(this.gapFrom, this.gapTo);
|
|
1268
|
+
if (gap.openStart || gap.openEnd)
|
|
1269
|
+
return StepResult.fail("Gap is not a flat range");
|
|
1270
|
+
let inserted = this.slice.insertAt(this.insert, gap.content);
|
|
1271
|
+
if (!inserted)
|
|
1272
|
+
return StepResult.fail("Content does not fit in gap");
|
|
1273
|
+
return StepResult.fromReplace(doc, this.from, this.to, inserted);
|
|
1274
|
+
}
|
|
1275
|
+
getMap() {
|
|
1276
|
+
return new StepMap([this.from, this.gapFrom - this.from, this.insert,
|
|
1277
|
+
this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
|
|
1278
|
+
}
|
|
1279
|
+
invert(doc) {
|
|
1280
|
+
let gap = this.gapTo - this.gapFrom;
|
|
1281
|
+
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);
|
|
1282
|
+
}
|
|
1283
|
+
map(mapping) {
|
|
1284
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1285
|
+
let gapFrom = this.from == this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1);
|
|
1286
|
+
let gapTo = this.to == this.gapTo ? to.pos : mapping.map(this.gapTo, 1);
|
|
1287
|
+
if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
|
|
1288
|
+
return null;
|
|
1289
|
+
return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
|
|
1290
|
+
}
|
|
1291
|
+
toJSON() {
|
|
1292
|
+
let json = { stepType: "replaceAround", from: this.from, to: this.to,
|
|
1293
|
+
gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert };
|
|
1294
|
+
if (this.slice.size)
|
|
1295
|
+
json.slice = this.slice.toJSON();
|
|
1296
|
+
if (this.structure)
|
|
1297
|
+
json.structure = true;
|
|
1298
|
+
return json;
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
@internal
|
|
1302
|
+
*/
|
|
1303
|
+
static fromJSON(schema, json) {
|
|
1304
|
+
if (typeof json.from != "number" || typeof json.to != "number" ||
|
|
1305
|
+
typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
|
|
1306
|
+
throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
|
|
1307
|
+
return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
Step.jsonID("replaceAround", ReplaceAroundStep);
|
|
1311
|
+
function contentBetween(doc, from, to) {
|
|
1312
|
+
let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
|
|
1313
|
+
while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
|
|
1314
|
+
depth--;
|
|
1315
|
+
dist--;
|
|
1316
|
+
}
|
|
1317
|
+
if (dist > 0) {
|
|
1318
|
+
let next = $from.node(depth).maybeChild($from.indexAfter(depth));
|
|
1319
|
+
while (dist > 0) {
|
|
1320
|
+
if (!next || next.isLeaf)
|
|
1321
|
+
return true;
|
|
1322
|
+
next = next.firstChild;
|
|
1323
|
+
dist--;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
return false;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
/**
|
|
1330
|
+
Update an attribute in a specific node.
|
|
1331
|
+
*/
|
|
1332
|
+
class AttrStep extends Step {
|
|
1333
|
+
/**
|
|
1334
|
+
Construct an attribute step.
|
|
1335
|
+
*/
|
|
1336
|
+
constructor(
|
|
1337
|
+
/**
|
|
1338
|
+
The position of the target node.
|
|
1339
|
+
*/
|
|
1340
|
+
pos,
|
|
1341
|
+
/**
|
|
1342
|
+
The attribute to set.
|
|
1343
|
+
*/
|
|
1344
|
+
attr,
|
|
1345
|
+
// The attribute's new value.
|
|
1346
|
+
value) {
|
|
1347
|
+
super();
|
|
1348
|
+
this.pos = pos;
|
|
1349
|
+
this.attr = attr;
|
|
1350
|
+
this.value = value;
|
|
1351
|
+
}
|
|
1352
|
+
apply(doc) {
|
|
1353
|
+
let node = doc.nodeAt(this.pos);
|
|
1354
|
+
if (!node)
|
|
1355
|
+
return StepResult.fail("No node at attribute step's position");
|
|
1356
|
+
let attrs = Object.create(null);
|
|
1357
|
+
for (let name in node.attrs)
|
|
1358
|
+
attrs[name] = node.attrs[name];
|
|
1359
|
+
attrs[this.attr] = this.value;
|
|
1360
|
+
let updated = node.type.create(attrs, null, node.marks);
|
|
1361
|
+
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
1362
|
+
}
|
|
1363
|
+
getMap() {
|
|
1364
|
+
return StepMap.empty;
|
|
1365
|
+
}
|
|
1366
|
+
invert(doc) {
|
|
1367
|
+
return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
|
|
1368
|
+
}
|
|
1369
|
+
map(mapping) {
|
|
1370
|
+
let pos = mapping.mapResult(this.pos, 1);
|
|
1371
|
+
return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
|
|
1372
|
+
}
|
|
1373
|
+
toJSON() {
|
|
1374
|
+
return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
|
|
1375
|
+
}
|
|
1376
|
+
static fromJSON(schema, json) {
|
|
1377
|
+
if (typeof json.pos != "number" || typeof json.attr != "string")
|
|
1378
|
+
throw new RangeError("Invalid input for AttrStep.fromJSON");
|
|
1379
|
+
return new AttrStep(json.pos, json.attr, json.value);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
Step.jsonID("attr", AttrStep);
|
|
1383
|
+
/**
|
|
1384
|
+
Update an attribute in the doc node.
|
|
1385
|
+
*/
|
|
1386
|
+
class DocAttrStep extends Step {
|
|
1387
|
+
/**
|
|
1388
|
+
Construct an attribute step.
|
|
1389
|
+
*/
|
|
1390
|
+
constructor(
|
|
1391
|
+
/**
|
|
1392
|
+
The attribute to set.
|
|
1393
|
+
*/
|
|
1394
|
+
attr,
|
|
1395
|
+
// The attribute's new value.
|
|
1396
|
+
value) {
|
|
1397
|
+
super();
|
|
1398
|
+
this.attr = attr;
|
|
1399
|
+
this.value = value;
|
|
1400
|
+
}
|
|
1401
|
+
apply(doc) {
|
|
1402
|
+
let attrs = Object.create(null);
|
|
1403
|
+
for (let name in doc.attrs)
|
|
1404
|
+
attrs[name] = doc.attrs[name];
|
|
1405
|
+
attrs[this.attr] = this.value;
|
|
1406
|
+
let updated = doc.type.create(attrs, doc.content, doc.marks);
|
|
1407
|
+
return StepResult.ok(updated);
|
|
1408
|
+
}
|
|
1409
|
+
getMap() {
|
|
1410
|
+
return StepMap.empty;
|
|
1411
|
+
}
|
|
1412
|
+
invert(doc) {
|
|
1413
|
+
return new DocAttrStep(this.attr, doc.attrs[this.attr]);
|
|
1414
|
+
}
|
|
1415
|
+
map(mapping) {
|
|
1416
|
+
return this;
|
|
1417
|
+
}
|
|
1418
|
+
toJSON() {
|
|
1419
|
+
return { stepType: "docAttr", attr: this.attr, value: this.value };
|
|
1420
|
+
}
|
|
1421
|
+
static fromJSON(schema, json) {
|
|
1422
|
+
if (typeof json.attr != "string")
|
|
1423
|
+
throw new RangeError("Invalid input for DocAttrStep.fromJSON");
|
|
1424
|
+
return new DocAttrStep(json.attr, json.value);
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
Step.jsonID("docAttr", DocAttrStep);
|
|
1428
|
+
|
|
1429
|
+
/**
|
|
1430
|
+
@internal
|
|
1431
|
+
*/
|
|
1432
|
+
let TransformError = class extends Error {
|
|
1433
|
+
};
|
|
1434
|
+
TransformError = function TransformError(message) {
|
|
1435
|
+
let err = Error.call(this, message);
|
|
1436
|
+
err.__proto__ = TransformError.prototype;
|
|
1437
|
+
return err;
|
|
1438
|
+
};
|
|
1439
|
+
TransformError.prototype = Object.create(Error.prototype);
|
|
1440
|
+
TransformError.prototype.constructor = TransformError;
|
|
1441
|
+
TransformError.prototype.name = "TransformError";
|
|
1442
|
+
|
|
1443
|
+
const classesById = Object.create(null);
|
|
1444
|
+
/**
|
|
1445
|
+
Superclass for editor selections. Every selection type should
|
|
1446
|
+
extend this. Should not be instantiated directly.
|
|
1447
|
+
*/
|
|
1448
|
+
class Selection {
|
|
1449
|
+
/**
|
|
1450
|
+
Initialize a selection with the head and anchor and ranges. If no
|
|
1451
|
+
ranges are given, constructs a single range across `$anchor` and
|
|
1452
|
+
`$head`.
|
|
1453
|
+
*/
|
|
1454
|
+
constructor(
|
|
1455
|
+
/**
|
|
1456
|
+
The resolved anchor of the selection (the side that stays in
|
|
1457
|
+
place when the selection is modified).
|
|
1458
|
+
*/
|
|
1459
|
+
$anchor,
|
|
1460
|
+
/**
|
|
1461
|
+
The resolved head of the selection (the side that moves when
|
|
1462
|
+
the selection is modified).
|
|
1463
|
+
*/
|
|
1464
|
+
$head, ranges) {
|
|
1465
|
+
this.$anchor = $anchor;
|
|
1466
|
+
this.$head = $head;
|
|
1467
|
+
this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
|
|
1468
|
+
}
|
|
1469
|
+
/**
|
|
1470
|
+
The selection's anchor, as an unresolved position.
|
|
1471
|
+
*/
|
|
1472
|
+
get anchor() { return this.$anchor.pos; }
|
|
1473
|
+
/**
|
|
1474
|
+
The selection's head.
|
|
1475
|
+
*/
|
|
1476
|
+
get head() { return this.$head.pos; }
|
|
1477
|
+
/**
|
|
1478
|
+
The lower bound of the selection's main range.
|
|
1479
|
+
*/
|
|
1480
|
+
get from() { return this.$from.pos; }
|
|
1481
|
+
/**
|
|
1482
|
+
The upper bound of the selection's main range.
|
|
1483
|
+
*/
|
|
1484
|
+
get to() { return this.$to.pos; }
|
|
1485
|
+
/**
|
|
1486
|
+
The resolved lower bound of the selection's main range.
|
|
1487
|
+
*/
|
|
1488
|
+
get $from() {
|
|
1489
|
+
return this.ranges[0].$from;
|
|
1490
|
+
}
|
|
1491
|
+
/**
|
|
1492
|
+
The resolved upper bound of the selection's main range.
|
|
1493
|
+
*/
|
|
1494
|
+
get $to() {
|
|
1495
|
+
return this.ranges[0].$to;
|
|
1496
|
+
}
|
|
1497
|
+
/**
|
|
1498
|
+
Indicates whether the selection contains any content.
|
|
1499
|
+
*/
|
|
1500
|
+
get empty() {
|
|
1501
|
+
let ranges = this.ranges;
|
|
1502
|
+
for (let i = 0; i < ranges.length; i++)
|
|
1503
|
+
if (ranges[i].$from.pos != ranges[i].$to.pos)
|
|
1504
|
+
return false;
|
|
1505
|
+
return true;
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
Get the content of this selection as a slice.
|
|
1509
|
+
*/
|
|
1510
|
+
content() {
|
|
1511
|
+
return this.$from.doc.slice(this.from, this.to, true);
|
|
1512
|
+
}
|
|
1513
|
+
/**
|
|
1514
|
+
Replace the selection with a slice or, if no slice is given,
|
|
1515
|
+
delete the selection. Will append to the given transaction.
|
|
1516
|
+
*/
|
|
1517
|
+
replace(tr, content = Slice.empty) {
|
|
1518
|
+
// Put the new selection at the position after the inserted
|
|
1519
|
+
// content. When that ended in an inline node, search backwards,
|
|
1520
|
+
// to get the position after that node. If not, search forward.
|
|
1521
|
+
let lastNode = content.content.lastChild, lastParent = null;
|
|
1522
|
+
for (let i = 0; i < content.openEnd; i++) {
|
|
1523
|
+
lastParent = lastNode;
|
|
1524
|
+
lastNode = lastNode.lastChild;
|
|
1525
|
+
}
|
|
1526
|
+
let mapFrom = tr.steps.length, ranges = this.ranges;
|
|
1527
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
1528
|
+
let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
1529
|
+
tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
|
|
1530
|
+
if (i == 0)
|
|
1531
|
+
selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
/**
|
|
1535
|
+
Replace the selection with the given node, appending the changes
|
|
1536
|
+
to the given transaction.
|
|
1537
|
+
*/
|
|
1538
|
+
replaceWith(tr, node) {
|
|
1539
|
+
let mapFrom = tr.steps.length, ranges = this.ranges;
|
|
1540
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
1541
|
+
let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
1542
|
+
let from = mapping.map($from.pos), to = mapping.map($to.pos);
|
|
1543
|
+
if (i) {
|
|
1544
|
+
tr.deleteRange(from, to);
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
tr.replaceRangeWith(from, to, node);
|
|
1548
|
+
selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
/**
|
|
1553
|
+
Find a valid cursor or leaf node selection starting at the given
|
|
1554
|
+
position and searching back if `dir` is negative, and forward if
|
|
1555
|
+
positive. When `textOnly` is true, only consider cursor
|
|
1556
|
+
selections. Will return null when no valid selection position is
|
|
1557
|
+
found.
|
|
1558
|
+
*/
|
|
1559
|
+
static findFrom($pos, dir, textOnly = false) {
|
|
1560
|
+
let inner = $pos.parent.inlineContent ? new TextSelection($pos)
|
|
1561
|
+
: findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
|
|
1562
|
+
if (inner)
|
|
1563
|
+
return inner;
|
|
1564
|
+
for (let depth = $pos.depth - 1; depth >= 0; depth--) {
|
|
1565
|
+
let found = dir < 0
|
|
1566
|
+
? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly)
|
|
1567
|
+
: findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
|
|
1568
|
+
if (found)
|
|
1569
|
+
return found;
|
|
1570
|
+
}
|
|
1571
|
+
return null;
|
|
1572
|
+
}
|
|
1573
|
+
/**
|
|
1574
|
+
Find a valid cursor or leaf node selection near the given
|
|
1575
|
+
position. Searches forward first by default, but if `bias` is
|
|
1576
|
+
negative, it will search backwards first.
|
|
1577
|
+
*/
|
|
1578
|
+
static near($pos, bias = 1) {
|
|
1579
|
+
return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
Find the cursor or leaf node selection closest to the start of
|
|
1583
|
+
the given document. Will return an
|
|
1584
|
+
[`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
|
|
1585
|
+
exists.
|
|
1586
|
+
*/
|
|
1587
|
+
static atStart(doc) {
|
|
1588
|
+
return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
|
|
1589
|
+
}
|
|
1590
|
+
/**
|
|
1591
|
+
Find the cursor or leaf node selection closest to the end of the
|
|
1592
|
+
given document.
|
|
1593
|
+
*/
|
|
1594
|
+
static atEnd(doc) {
|
|
1595
|
+
return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
Deserialize the JSON representation of a selection. Must be
|
|
1599
|
+
implemented for custom classes (as a static class method).
|
|
1600
|
+
*/
|
|
1601
|
+
static fromJSON(doc, json) {
|
|
1602
|
+
if (!json || !json.type)
|
|
1603
|
+
throw new RangeError("Invalid input for Selection.fromJSON");
|
|
1604
|
+
let cls = classesById[json.type];
|
|
1605
|
+
if (!cls)
|
|
1606
|
+
throw new RangeError(`No selection type ${json.type} defined`);
|
|
1607
|
+
return cls.fromJSON(doc, json);
|
|
1608
|
+
}
|
|
1609
|
+
/**
|
|
1610
|
+
To be able to deserialize selections from JSON, custom selection
|
|
1611
|
+
classes must register themselves with an ID string, so that they
|
|
1612
|
+
can be disambiguated. Try to pick something that's unlikely to
|
|
1613
|
+
clash with classes from other modules.
|
|
1614
|
+
*/
|
|
1615
|
+
static jsonID(id, selectionClass) {
|
|
1616
|
+
if (id in classesById)
|
|
1617
|
+
throw new RangeError("Duplicate use of selection JSON ID " + id);
|
|
1618
|
+
classesById[id] = selectionClass;
|
|
1619
|
+
selectionClass.prototype.jsonID = id;
|
|
1620
|
+
return selectionClass;
|
|
1621
|
+
}
|
|
1622
|
+
/**
|
|
1623
|
+
Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
|
|
1624
|
+
which is a value that can be mapped without having access to a
|
|
1625
|
+
current document, and later resolved to a real selection for a
|
|
1626
|
+
given document again. (This is used mostly by the history to
|
|
1627
|
+
track and restore old selections.) The default implementation of
|
|
1628
|
+
this method just converts the selection to a text selection and
|
|
1629
|
+
returns the bookmark for that.
|
|
1630
|
+
*/
|
|
1631
|
+
getBookmark() {
|
|
1632
|
+
return TextSelection.between(this.$anchor, this.$head).getBookmark();
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
Selection.prototype.visible = true;
|
|
1636
|
+
/**
|
|
1637
|
+
Represents a selected range in a document.
|
|
1638
|
+
*/
|
|
1639
|
+
class SelectionRange {
|
|
1640
|
+
/**
|
|
1641
|
+
Create a range.
|
|
1642
|
+
*/
|
|
1643
|
+
constructor(
|
|
1644
|
+
/**
|
|
1645
|
+
The lower bound of the range.
|
|
1646
|
+
*/
|
|
1647
|
+
$from,
|
|
1648
|
+
/**
|
|
1649
|
+
The upper bound of the range.
|
|
1650
|
+
*/
|
|
1651
|
+
$to) {
|
|
1652
|
+
this.$from = $from;
|
|
1653
|
+
this.$to = $to;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
let warnedAboutTextSelection = false;
|
|
1657
|
+
function checkTextSelection($pos) {
|
|
1658
|
+
if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
|
|
1659
|
+
warnedAboutTextSelection = true;
|
|
1660
|
+
console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
A text selection represents a classical editor selection, with a
|
|
1665
|
+
head (the moving side) and anchor (immobile side), both of which
|
|
1666
|
+
point into textblock nodes. It can be empty (a regular cursor
|
|
1667
|
+
position).
|
|
1668
|
+
*/
|
|
1669
|
+
class TextSelection extends Selection {
|
|
1670
|
+
/**
|
|
1671
|
+
Construct a text selection between the given points.
|
|
1672
|
+
*/
|
|
1673
|
+
constructor($anchor, $head = $anchor) {
|
|
1674
|
+
checkTextSelection($anchor);
|
|
1675
|
+
checkTextSelection($head);
|
|
1676
|
+
super($anchor, $head);
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
Returns a resolved position if this is a cursor selection (an
|
|
1680
|
+
empty text selection), and null otherwise.
|
|
1681
|
+
*/
|
|
1682
|
+
get $cursor() { return this.$anchor.pos == this.$head.pos ? this.$head : null; }
|
|
1683
|
+
map(doc, mapping) {
|
|
1684
|
+
let $head = doc.resolve(mapping.map(this.head));
|
|
1685
|
+
if (!$head.parent.inlineContent)
|
|
1686
|
+
return Selection.near($head);
|
|
1687
|
+
let $anchor = doc.resolve(mapping.map(this.anchor));
|
|
1688
|
+
return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
|
|
1689
|
+
}
|
|
1690
|
+
replace(tr, content = Slice.empty) {
|
|
1691
|
+
super.replace(tr, content);
|
|
1692
|
+
if (content == Slice.empty) {
|
|
1693
|
+
let marks = this.$from.marksAcross(this.$to);
|
|
1694
|
+
if (marks)
|
|
1695
|
+
tr.ensureMarks(marks);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
eq(other) {
|
|
1699
|
+
return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
|
|
1700
|
+
}
|
|
1701
|
+
getBookmark() {
|
|
1702
|
+
return new TextBookmark(this.anchor, this.head);
|
|
1703
|
+
}
|
|
1704
|
+
toJSON() {
|
|
1705
|
+
return { type: "text", anchor: this.anchor, head: this.head };
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
@internal
|
|
1709
|
+
*/
|
|
1710
|
+
static fromJSON(doc, json) {
|
|
1711
|
+
if (typeof json.anchor != "number" || typeof json.head != "number")
|
|
1712
|
+
throw new RangeError("Invalid input for TextSelection.fromJSON");
|
|
1713
|
+
return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
Create a text selection from non-resolved positions.
|
|
1717
|
+
*/
|
|
1718
|
+
static create(doc, anchor, head = anchor) {
|
|
1719
|
+
let $anchor = doc.resolve(anchor);
|
|
1720
|
+
return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
Return a text selection that spans the given positions or, if
|
|
1724
|
+
they aren't text positions, find a text selection near them.
|
|
1725
|
+
`bias` determines whether the method searches forward (default)
|
|
1726
|
+
or backwards (negative number) first. Will fall back to calling
|
|
1727
|
+
[`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
|
|
1728
|
+
doesn't contain a valid text position.
|
|
1729
|
+
*/
|
|
1730
|
+
static between($anchor, $head, bias) {
|
|
1731
|
+
let dPos = $anchor.pos - $head.pos;
|
|
1732
|
+
if (!bias || dPos)
|
|
1733
|
+
bias = dPos >= 0 ? 1 : -1;
|
|
1734
|
+
if (!$head.parent.inlineContent) {
|
|
1735
|
+
let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
|
|
1736
|
+
if (found)
|
|
1737
|
+
$head = found.$head;
|
|
1738
|
+
else
|
|
1739
|
+
return Selection.near($head, bias);
|
|
1740
|
+
}
|
|
1741
|
+
if (!$anchor.parent.inlineContent) {
|
|
1742
|
+
if (dPos == 0) {
|
|
1743
|
+
$anchor = $head;
|
|
1744
|
+
}
|
|
1745
|
+
else {
|
|
1746
|
+
$anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
|
|
1747
|
+
if (($anchor.pos < $head.pos) != (dPos < 0))
|
|
1748
|
+
$anchor = $head;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
return new TextSelection($anchor, $head);
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
Selection.jsonID("text", TextSelection);
|
|
1755
|
+
class TextBookmark {
|
|
1756
|
+
constructor(anchor, head) {
|
|
1757
|
+
this.anchor = anchor;
|
|
1758
|
+
this.head = head;
|
|
1759
|
+
}
|
|
1760
|
+
map(mapping) {
|
|
1761
|
+
return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
|
|
1762
|
+
}
|
|
1763
|
+
resolve(doc) {
|
|
1764
|
+
return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
/**
|
|
1768
|
+
A node selection is a selection that points at a single node. All
|
|
1769
|
+
nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
|
|
1770
|
+
target of a node selection. In such a selection, `from` and `to`
|
|
1771
|
+
point directly before and after the selected node, `anchor` equals
|
|
1772
|
+
`from`, and `head` equals `to`..
|
|
1773
|
+
*/
|
|
1774
|
+
class NodeSelection extends Selection {
|
|
1775
|
+
/**
|
|
1776
|
+
Create a node selection. Does not verify the validity of its
|
|
1777
|
+
argument.
|
|
1778
|
+
*/
|
|
1779
|
+
constructor($pos) {
|
|
1780
|
+
let node = $pos.nodeAfter;
|
|
1781
|
+
let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
|
|
1782
|
+
super($pos, $end);
|
|
1783
|
+
this.node = node;
|
|
1784
|
+
}
|
|
1785
|
+
map(doc, mapping) {
|
|
1786
|
+
let { deleted, pos } = mapping.mapResult(this.anchor);
|
|
1787
|
+
let $pos = doc.resolve(pos);
|
|
1788
|
+
if (deleted)
|
|
1789
|
+
return Selection.near($pos);
|
|
1790
|
+
return new NodeSelection($pos);
|
|
1791
|
+
}
|
|
1792
|
+
content() {
|
|
1793
|
+
return new Slice(Fragment.from(this.node), 0, 0);
|
|
1794
|
+
}
|
|
1795
|
+
eq(other) {
|
|
1796
|
+
return other instanceof NodeSelection && other.anchor == this.anchor;
|
|
1797
|
+
}
|
|
1798
|
+
toJSON() {
|
|
1799
|
+
return { type: "node", anchor: this.anchor };
|
|
1800
|
+
}
|
|
1801
|
+
getBookmark() { return new NodeBookmark(this.anchor); }
|
|
1802
|
+
/**
|
|
1803
|
+
@internal
|
|
1804
|
+
*/
|
|
1805
|
+
static fromJSON(doc, json) {
|
|
1806
|
+
if (typeof json.anchor != "number")
|
|
1807
|
+
throw new RangeError("Invalid input for NodeSelection.fromJSON");
|
|
1808
|
+
return new NodeSelection(doc.resolve(json.anchor));
|
|
1809
|
+
}
|
|
1810
|
+
/**
|
|
1811
|
+
Create a node selection from non-resolved positions.
|
|
1812
|
+
*/
|
|
1813
|
+
static create(doc, from) {
|
|
1814
|
+
return new NodeSelection(doc.resolve(from));
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
Determines whether the given node may be selected as a node
|
|
1818
|
+
selection.
|
|
1819
|
+
*/
|
|
1820
|
+
static isSelectable(node) {
|
|
1821
|
+
return !node.isText && node.type.spec.selectable !== false;
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
NodeSelection.prototype.visible = false;
|
|
1825
|
+
Selection.jsonID("node", NodeSelection);
|
|
1826
|
+
class NodeBookmark {
|
|
1827
|
+
constructor(anchor) {
|
|
1828
|
+
this.anchor = anchor;
|
|
1829
|
+
}
|
|
1830
|
+
map(mapping) {
|
|
1831
|
+
let { deleted, pos } = mapping.mapResult(this.anchor);
|
|
1832
|
+
return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
|
|
1833
|
+
}
|
|
1834
|
+
resolve(doc) {
|
|
1835
|
+
let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
|
|
1836
|
+
if (node && NodeSelection.isSelectable(node))
|
|
1837
|
+
return new NodeSelection($pos);
|
|
1838
|
+
return Selection.near($pos);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
/**
|
|
1842
|
+
A selection type that represents selecting the whole document
|
|
1843
|
+
(which can not necessarily be expressed with a text selection, when
|
|
1844
|
+
there are for example leaf block nodes at the start or end of the
|
|
1845
|
+
document).
|
|
1846
|
+
*/
|
|
1847
|
+
class AllSelection extends Selection {
|
|
1848
|
+
/**
|
|
1849
|
+
Create an all-selection over the given document.
|
|
1850
|
+
*/
|
|
1851
|
+
constructor(doc) {
|
|
1852
|
+
super(doc.resolve(0), doc.resolve(doc.content.size));
|
|
1853
|
+
}
|
|
1854
|
+
replace(tr, content = Slice.empty) {
|
|
1855
|
+
if (content == Slice.empty) {
|
|
1856
|
+
tr.delete(0, tr.doc.content.size);
|
|
1857
|
+
let sel = Selection.atStart(tr.doc);
|
|
1858
|
+
if (!sel.eq(tr.selection))
|
|
1859
|
+
tr.setSelection(sel);
|
|
1860
|
+
}
|
|
1861
|
+
else {
|
|
1862
|
+
super.replace(tr, content);
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
toJSON() { return { type: "all" }; }
|
|
1866
|
+
/**
|
|
1867
|
+
@internal
|
|
1868
|
+
*/
|
|
1869
|
+
static fromJSON(doc) { return new AllSelection(doc); }
|
|
1870
|
+
map(doc) { return new AllSelection(doc); }
|
|
1871
|
+
eq(other) { return other instanceof AllSelection; }
|
|
1872
|
+
getBookmark() { return AllBookmark; }
|
|
1873
|
+
}
|
|
1874
|
+
Selection.jsonID("all", AllSelection);
|
|
1875
|
+
const AllBookmark = {
|
|
1876
|
+
map() { return this; },
|
|
1877
|
+
resolve(doc) { return new AllSelection(doc); }
|
|
1878
|
+
};
|
|
1879
|
+
// FIXME we'll need some awareness of text direction when scanning for selections
|
|
1880
|
+
// Try to find a selection inside the given node. `pos` points at the
|
|
1881
|
+
// position where the search starts. When `text` is true, only return
|
|
1882
|
+
// text selections.
|
|
1883
|
+
function findSelectionIn(doc, node, pos, index, dir, text = false) {
|
|
1884
|
+
if (node.inlineContent)
|
|
1885
|
+
return TextSelection.create(doc, pos);
|
|
1886
|
+
for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
|
|
1887
|
+
let child = node.child(i);
|
|
1888
|
+
if (!child.isAtom) {
|
|
1889
|
+
let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
|
|
1890
|
+
if (inner)
|
|
1891
|
+
return inner;
|
|
1892
|
+
}
|
|
1893
|
+
else if (!text && NodeSelection.isSelectable(child)) {
|
|
1894
|
+
return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
|
|
1895
|
+
}
|
|
1896
|
+
pos += child.nodeSize * dir;
|
|
1897
|
+
}
|
|
1898
|
+
return null;
|
|
1899
|
+
}
|
|
1900
|
+
function selectionToInsertionEnd(tr, startLen, bias) {
|
|
1901
|
+
let last = tr.steps.length - 1;
|
|
1902
|
+
if (last < startLen)
|
|
1903
|
+
return;
|
|
1904
|
+
let step = tr.steps[last];
|
|
1905
|
+
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep))
|
|
1906
|
+
return;
|
|
1907
|
+
let map = tr.mapping.maps[last], end;
|
|
1908
|
+
map.forEach((_from, _to, _newFrom, newTo) => { if (end == null)
|
|
1909
|
+
end = newTo; });
|
|
1910
|
+
tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
|
|
1911
|
+
}
|
|
1912
|
+
|
|
1913
|
+
function bind(f, self) {
|
|
1914
|
+
return !self || !f ? f : f.bind(self);
|
|
1915
|
+
}
|
|
1916
|
+
class FieldDesc {
|
|
1917
|
+
constructor(name, desc, self) {
|
|
1918
|
+
this.name = name;
|
|
1919
|
+
this.init = bind(desc.init, self);
|
|
1920
|
+
this.apply = bind(desc.apply, self);
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
[
|
|
1924
|
+
new FieldDesc("doc", {
|
|
1925
|
+
init(config) { return config.doc || config.schema.topNodeType.createAndFill(); },
|
|
1926
|
+
apply(tr) { return tr.doc; }
|
|
1927
|
+
}),
|
|
1928
|
+
new FieldDesc("selection", {
|
|
1929
|
+
init(config, instance) { return config.selection || Selection.atStart(instance.doc); },
|
|
1930
|
+
apply(tr) { return tr.selection; }
|
|
1931
|
+
}),
|
|
1932
|
+
new FieldDesc("storedMarks", {
|
|
1933
|
+
init(config) { return config.storedMarks || null; },
|
|
1934
|
+
apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null; }
|
|
1935
|
+
}),
|
|
1936
|
+
new FieldDesc("scrollToSelection", {
|
|
1937
|
+
init() { return 0; },
|
|
1938
|
+
apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev; }
|
|
1939
|
+
})
|
|
1940
|
+
];
|
|
1941
|
+
const keys = Object.create(null);
|
|
1942
|
+
function createKey(name) {
|
|
1943
|
+
if (name in keys)
|
|
1944
|
+
return name + "$" + ++keys[name];
|
|
1945
|
+
keys[name] = 0;
|
|
1946
|
+
return name + "$";
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1949
|
+
A key is used to [tag](https://prosemirror.net/docs/ref/#state.PluginSpec.key) plugins in a way
|
|
1950
|
+
that makes it possible to find them, given an editor state.
|
|
1951
|
+
Assigning a key does mean only one plugin of that type can be
|
|
1952
|
+
active in a state.
|
|
1953
|
+
*/
|
|
1954
|
+
class PluginKey {
|
|
1955
|
+
/**
|
|
1956
|
+
Create a plugin key.
|
|
1957
|
+
*/
|
|
1958
|
+
constructor(name = "key") { this.key = createKey(name); }
|
|
1959
|
+
/**
|
|
1960
|
+
Get the active plugin with this key, if any, from an editor
|
|
1961
|
+
state.
|
|
1962
|
+
*/
|
|
1963
|
+
get(state) { return state.config.pluginsByKey[this.key]; }
|
|
1964
|
+
/**
|
|
1965
|
+
Get the plugin's state from an editor state.
|
|
1966
|
+
*/
|
|
1967
|
+
getState(state) { return state[this.key]; }
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
var base = {
|
|
1971
|
+
8: "Backspace",
|
|
1972
|
+
9: "Tab",
|
|
1973
|
+
10: "Enter",
|
|
1974
|
+
12: "NumLock",
|
|
1975
|
+
13: "Enter",
|
|
1976
|
+
16: "Shift",
|
|
1977
|
+
17: "Control",
|
|
1978
|
+
18: "Alt",
|
|
1979
|
+
20: "CapsLock",
|
|
1980
|
+
27: "Escape",
|
|
1981
|
+
32: " ",
|
|
1982
|
+
33: "PageUp",
|
|
1983
|
+
34: "PageDown",
|
|
1984
|
+
35: "End",
|
|
1985
|
+
36: "Home",
|
|
1986
|
+
37: "ArrowLeft",
|
|
1987
|
+
38: "ArrowUp",
|
|
1988
|
+
39: "ArrowRight",
|
|
1989
|
+
40: "ArrowDown",
|
|
1990
|
+
44: "PrintScreen",
|
|
1991
|
+
45: "Insert",
|
|
1992
|
+
46: "Delete",
|
|
1993
|
+
59: ";",
|
|
1994
|
+
61: "=",
|
|
1995
|
+
91: "Meta",
|
|
1996
|
+
92: "Meta",
|
|
1997
|
+
106: "*",
|
|
1998
|
+
107: "+",
|
|
1999
|
+
108: ",",
|
|
2000
|
+
109: "-",
|
|
2001
|
+
110: ".",
|
|
2002
|
+
111: "/",
|
|
2003
|
+
144: "NumLock",
|
|
2004
|
+
145: "ScrollLock",
|
|
2005
|
+
160: "Shift",
|
|
2006
|
+
161: "Shift",
|
|
2007
|
+
162: "Control",
|
|
2008
|
+
163: "Control",
|
|
2009
|
+
164: "Alt",
|
|
2010
|
+
165: "Alt",
|
|
2011
|
+
173: "-",
|
|
2012
|
+
186: ";",
|
|
2013
|
+
187: "=",
|
|
2014
|
+
188: ",",
|
|
2015
|
+
189: "-",
|
|
2016
|
+
190: ".",
|
|
2017
|
+
191: "/",
|
|
2018
|
+
192: "`",
|
|
2019
|
+
219: "[",
|
|
2020
|
+
220: "\\",
|
|
2021
|
+
221: "]",
|
|
2022
|
+
222: "'"
|
|
2023
|
+
};
|
|
2024
|
+
|
|
2025
|
+
var shift = {
|
|
2026
|
+
48: ")",
|
|
2027
|
+
49: "!",
|
|
2028
|
+
50: "@",
|
|
2029
|
+
51: "#",
|
|
2030
|
+
52: "$",
|
|
2031
|
+
53: "%",
|
|
2032
|
+
54: "^",
|
|
2033
|
+
55: "&",
|
|
2034
|
+
56: "*",
|
|
2035
|
+
57: "(",
|
|
2036
|
+
59: ":",
|
|
2037
|
+
61: "+",
|
|
2038
|
+
173: "_",
|
|
2039
|
+
186: ":",
|
|
2040
|
+
187: "+",
|
|
2041
|
+
188: "<",
|
|
2042
|
+
189: "_",
|
|
2043
|
+
190: ">",
|
|
2044
|
+
191: "?",
|
|
2045
|
+
192: "~",
|
|
2046
|
+
219: "{",
|
|
2047
|
+
220: "|",
|
|
2048
|
+
221: "}",
|
|
2049
|
+
222: "\""
|
|
2050
|
+
};
|
|
2051
|
+
|
|
2052
|
+
var mac$1 = typeof navigator != "undefined" && /Mac/.test(navigator.platform);
|
|
2053
|
+
var ie = typeof navigator != "undefined" && /MSIE \d|Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(navigator.userAgent);
|
|
2054
|
+
|
|
2055
|
+
// Fill in the digit keys
|
|
2056
|
+
for (var i = 0; i < 10; i++) base[48 + i] = base[96 + i] = String(i);
|
|
2057
|
+
|
|
2058
|
+
// The function keys
|
|
2059
|
+
for (var i = 1; i <= 24; i++) base[i + 111] = "F" + i;
|
|
2060
|
+
|
|
2061
|
+
// And the alphabetic keys
|
|
2062
|
+
for (var i = 65; i <= 90; i++) {
|
|
2063
|
+
base[i] = String.fromCharCode(i + 32);
|
|
2064
|
+
shift[i] = String.fromCharCode(i);
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
// For each code that doesn't have a shift-equivalent, copy the base name
|
|
2068
|
+
for (var code in base) if (!shift.hasOwnProperty(code)) shift[code] = base[code];
|
|
2069
|
+
|
|
2070
|
+
function keyName(event) {
|
|
2071
|
+
// On macOS, keys held with Shift and Cmd don't reflect the effect of Shift in `.key`.
|
|
2072
|
+
// On IE, shift effect is never included in `.key`.
|
|
2073
|
+
var ignoreKey = mac$1 && event.metaKey && event.shiftKey && !event.ctrlKey && !event.altKey ||
|
|
2074
|
+
ie && event.shiftKey && event.key && event.key.length == 1 ||
|
|
2075
|
+
event.key == "Unidentified";
|
|
2076
|
+
var name = (!ignoreKey && event.key) ||
|
|
2077
|
+
(event.shiftKey ? shift : base)[event.keyCode] ||
|
|
2078
|
+
event.key || "Unidentified";
|
|
2079
|
+
// Edge sometimes produces wrong names (Issue #3)
|
|
2080
|
+
if (name == "Esc") name = "Escape";
|
|
2081
|
+
if (name == "Del") name = "Delete";
|
|
2082
|
+
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
|
|
2083
|
+
if (name == "Left") name = "ArrowLeft";
|
|
2084
|
+
if (name == "Up") name = "ArrowUp";
|
|
2085
|
+
if (name == "Right") name = "ArrowRight";
|
|
2086
|
+
if (name == "Down") name = "ArrowDown";
|
|
2087
|
+
return name
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
const mac = typeof navigator != "undefined" && /Mac|iP(hone|[oa]d)/.test(navigator.platform);
|
|
2091
|
+
const windows = typeof navigator != "undefined" && /Win/.test(navigator.platform);
|
|
2092
|
+
function normalizeKeyName(name) {
|
|
2093
|
+
let parts = name.split(/-(?!$)/), result = parts[parts.length - 1];
|
|
2094
|
+
if (result == "Space")
|
|
2095
|
+
result = " ";
|
|
2096
|
+
let alt, ctrl, shift, meta;
|
|
2097
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
2098
|
+
let mod = parts[i];
|
|
2099
|
+
if (/^(cmd|meta|m)$/i.test(mod))
|
|
2100
|
+
meta = true;
|
|
2101
|
+
else if (/^a(lt)?$/i.test(mod))
|
|
2102
|
+
alt = true;
|
|
2103
|
+
else if (/^(c|ctrl|control)$/i.test(mod))
|
|
2104
|
+
ctrl = true;
|
|
2105
|
+
else if (/^s(hift)?$/i.test(mod))
|
|
2106
|
+
shift = true;
|
|
2107
|
+
else if (/^mod$/i.test(mod)) {
|
|
2108
|
+
if (mac)
|
|
2109
|
+
meta = true;
|
|
2110
|
+
else
|
|
2111
|
+
ctrl = true;
|
|
2112
|
+
}
|
|
2113
|
+
else
|
|
2114
|
+
throw new Error("Unrecognized modifier name: " + mod);
|
|
2115
|
+
}
|
|
2116
|
+
if (alt)
|
|
2117
|
+
result = "Alt-" + result;
|
|
2118
|
+
if (ctrl)
|
|
2119
|
+
result = "Ctrl-" + result;
|
|
2120
|
+
if (meta)
|
|
2121
|
+
result = "Meta-" + result;
|
|
2122
|
+
if (shift)
|
|
2123
|
+
result = "Shift-" + result;
|
|
2124
|
+
return result;
|
|
2125
|
+
}
|
|
2126
|
+
function normalize(map) {
|
|
2127
|
+
let copy = Object.create(null);
|
|
2128
|
+
for (let prop in map)
|
|
2129
|
+
copy[normalizeKeyName(prop)] = map[prop];
|
|
2130
|
+
return copy;
|
|
2131
|
+
}
|
|
2132
|
+
function modifiers(name, event, shift = true) {
|
|
2133
|
+
if (event.altKey)
|
|
2134
|
+
name = "Alt-" + name;
|
|
2135
|
+
if (event.ctrlKey)
|
|
2136
|
+
name = "Ctrl-" + name;
|
|
2137
|
+
if (event.metaKey)
|
|
2138
|
+
name = "Meta-" + name;
|
|
2139
|
+
if (shift && event.shiftKey)
|
|
2140
|
+
name = "Shift-" + name;
|
|
2141
|
+
return name;
|
|
2142
|
+
}
|
|
2143
|
+
/**
|
|
2144
|
+
Given a set of bindings (using the same format as
|
|
2145
|
+
[`keymap`](https://prosemirror.net/docs/ref/#keymap.keymap)), return a [keydown
|
|
2146
|
+
handler](https://prosemirror.net/docs/ref/#view.EditorProps.handleKeyDown) that handles them.
|
|
2147
|
+
*/
|
|
2148
|
+
function keydownHandler(bindings) {
|
|
2149
|
+
let map = normalize(bindings);
|
|
2150
|
+
return function (view, event) {
|
|
2151
|
+
let name = keyName(event), baseName, direct = map[modifiers(name, event)];
|
|
2152
|
+
if (direct && direct(view.state, view.dispatch, view))
|
|
2153
|
+
return true;
|
|
2154
|
+
// A character key
|
|
2155
|
+
if (name.length == 1 && name != " ") {
|
|
2156
|
+
if (event.shiftKey) {
|
|
2157
|
+
// In case the name was already modified by shift, try looking
|
|
2158
|
+
// it up without its shift modifier
|
|
2159
|
+
let noShift = map[modifiers(name, event, false)];
|
|
2160
|
+
if (noShift && noShift(view.state, view.dispatch, view))
|
|
2161
|
+
return true;
|
|
2162
|
+
}
|
|
2163
|
+
if ((event.altKey || event.metaKey || event.ctrlKey) &&
|
|
2164
|
+
// Ctrl-Alt may be used for AltGr on Windows
|
|
2165
|
+
!(windows && event.ctrlKey && event.altKey) &&
|
|
2166
|
+
(baseName = base[event.keyCode]) && baseName != name) {
|
|
2167
|
+
// Try falling back to the keyCode when there's a modifier
|
|
2168
|
+
// active or the character produced isn't ASCII, and our table
|
|
2169
|
+
// produces a different name from the the keyCode. See #668,
|
|
2170
|
+
// #1060, #1529.
|
|
2171
|
+
let fromCode = map[modifiers(baseName, event)];
|
|
2172
|
+
if (fromCode && fromCode(view.state, view.dispatch, view))
|
|
2173
|
+
return true;
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
return false;
|
|
2177
|
+
};
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
//#region src/tablemap.ts
|
|
2181
|
+
let readFromCache;
|
|
2182
|
+
let addToCache;
|
|
2183
|
+
if (typeof WeakMap != "undefined") {
|
|
2184
|
+
let cache = /* @__PURE__ */ new WeakMap();
|
|
2185
|
+
readFromCache = (key) => cache.get(key);
|
|
2186
|
+
addToCache = (key, value) => {
|
|
2187
|
+
cache.set(key, value);
|
|
2188
|
+
return value;
|
|
2189
|
+
};
|
|
2190
|
+
} else {
|
|
2191
|
+
const cache = [];
|
|
2192
|
+
const cacheSize = 10;
|
|
2193
|
+
let cachePos = 0;
|
|
2194
|
+
readFromCache = (key) => {
|
|
2195
|
+
for (let i = 0; i < cache.length; i += 2) if (cache[i] == key) return cache[i + 1];
|
|
2196
|
+
};
|
|
2197
|
+
addToCache = (key, value) => {
|
|
2198
|
+
if (cachePos == cacheSize) cachePos = 0;
|
|
2199
|
+
cache[cachePos++] = key;
|
|
2200
|
+
return cache[cachePos++] = value;
|
|
2201
|
+
};
|
|
2202
|
+
}
|
|
2203
|
+
/**
|
|
2204
|
+
* A table map describes the structure of a given table. To avoid
|
|
2205
|
+
* recomputing them all the time, they are cached per table node. To
|
|
2206
|
+
* be able to do that, positions saved in the map are relative to the
|
|
2207
|
+
* start of the table, rather than the start of the document.
|
|
2208
|
+
*
|
|
2209
|
+
* @public
|
|
2210
|
+
*/
|
|
2211
|
+
var TableMap = class {
|
|
2212
|
+
constructor(width, height, map, problems) {
|
|
2213
|
+
this.width = width;
|
|
2214
|
+
this.height = height;
|
|
2215
|
+
this.map = map;
|
|
2216
|
+
this.problems = problems;
|
|
2217
|
+
}
|
|
2218
|
+
findCell(pos) {
|
|
2219
|
+
for (let i = 0; i < this.map.length; i++) {
|
|
2220
|
+
const curPos = this.map[i];
|
|
2221
|
+
if (curPos != pos) continue;
|
|
2222
|
+
const left = i % this.width;
|
|
2223
|
+
const top = i / this.width | 0;
|
|
2224
|
+
let right = left + 1;
|
|
2225
|
+
let bottom = top + 1;
|
|
2226
|
+
for (let j = 1; right < this.width && this.map[i + j] == curPos; j++) right++;
|
|
2227
|
+
for (let j = 1; bottom < this.height && this.map[i + this.width * j] == curPos; j++) bottom++;
|
|
2228
|
+
return {
|
|
2229
|
+
left,
|
|
2230
|
+
top,
|
|
2231
|
+
right,
|
|
2232
|
+
bottom
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
throw new RangeError(`No cell with offset ${pos} found`);
|
|
2236
|
+
}
|
|
2237
|
+
colCount(pos) {
|
|
2238
|
+
for (let i = 0; i < this.map.length; i++) if (this.map[i] == pos) return i % this.width;
|
|
2239
|
+
throw new RangeError(`No cell with offset ${pos} found`);
|
|
2240
|
+
}
|
|
2241
|
+
nextCell(pos, axis, dir) {
|
|
2242
|
+
const { left, right, top, bottom } = this.findCell(pos);
|
|
2243
|
+
if (axis == "horiz") {
|
|
2244
|
+
if (dir < 0 ? left == 0 : right == this.width) return null;
|
|
2245
|
+
return this.map[top * this.width + (dir < 0 ? left - 1 : right)];
|
|
2246
|
+
} else {
|
|
2247
|
+
if (dir < 0 ? top == 0 : bottom == this.height) return null;
|
|
2248
|
+
return this.map[left + this.width * (dir < 0 ? top - 1 : bottom)];
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
rectBetween(a, b) {
|
|
2252
|
+
const { left: leftA, right: rightA, top: topA, bottom: bottomA } = this.findCell(a);
|
|
2253
|
+
const { left: leftB, right: rightB, top: topB, bottom: bottomB } = this.findCell(b);
|
|
2254
|
+
return {
|
|
2255
|
+
left: Math.min(leftA, leftB),
|
|
2256
|
+
top: Math.min(topA, topB),
|
|
2257
|
+
right: Math.max(rightA, rightB),
|
|
2258
|
+
bottom: Math.max(bottomA, bottomB)
|
|
2259
|
+
};
|
|
2260
|
+
}
|
|
2261
|
+
cellsInRect(rect) {
|
|
2262
|
+
const result = [];
|
|
2263
|
+
const seen = {};
|
|
2264
|
+
for (let row = rect.top; row < rect.bottom; row++) for (let col = rect.left; col < rect.right; col++) {
|
|
2265
|
+
const index = row * this.width + col;
|
|
2266
|
+
const pos = this.map[index];
|
|
2267
|
+
if (seen[pos]) continue;
|
|
2268
|
+
seen[pos] = true;
|
|
2269
|
+
if (col == rect.left && col && this.map[index - 1] == pos || row == rect.top && row && this.map[index - this.width] == pos) continue;
|
|
2270
|
+
result.push(pos);
|
|
2271
|
+
}
|
|
2272
|
+
return result;
|
|
2273
|
+
}
|
|
2274
|
+
positionAt(row, col, table) {
|
|
2275
|
+
for (let i = 0, rowStart = 0;; i++) {
|
|
2276
|
+
const rowEnd = rowStart + table.child(i).nodeSize;
|
|
2277
|
+
if (i == row) {
|
|
2278
|
+
let index = col + row * this.width;
|
|
2279
|
+
const rowEndIndex = (row + 1) * this.width;
|
|
2280
|
+
while (index < rowEndIndex && this.map[index] < rowStart) index++;
|
|
2281
|
+
return index == rowEndIndex ? rowEnd - 1 : this.map[index];
|
|
2282
|
+
}
|
|
2283
|
+
rowStart = rowEnd;
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
static get(table) {
|
|
2287
|
+
return readFromCache(table) || addToCache(table, computeMap(table));
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2290
|
+
function computeMap(table) {
|
|
2291
|
+
if (table.type.spec.tableRole != "table") throw new RangeError("Not a table node: " + table.type.name);
|
|
2292
|
+
const width = findWidth(table), height = table.childCount;
|
|
2293
|
+
const map = [];
|
|
2294
|
+
let mapPos = 0;
|
|
2295
|
+
let problems = null;
|
|
2296
|
+
const colWidths = [];
|
|
2297
|
+
for (let i = 0, e = width * height; i < e; i++) map[i] = 0;
|
|
2298
|
+
for (let row = 0, pos = 0; row < height; row++) {
|
|
2299
|
+
const rowNode = table.child(row);
|
|
2300
|
+
pos++;
|
|
2301
|
+
for (let i = 0;; i++) {
|
|
2302
|
+
while (mapPos < map.length && map[mapPos] != 0) mapPos++;
|
|
2303
|
+
if (i == rowNode.childCount) break;
|
|
2304
|
+
const cellNode = rowNode.child(i);
|
|
2305
|
+
const { colspan, rowspan, colwidth } = cellNode.attrs;
|
|
2306
|
+
for (let h = 0; h < rowspan; h++) {
|
|
2307
|
+
if (h + row >= height) {
|
|
2308
|
+
(problems || (problems = [])).push({
|
|
2309
|
+
type: "overlong_rowspan",
|
|
2310
|
+
pos,
|
|
2311
|
+
n: rowspan - h
|
|
2312
|
+
});
|
|
2313
|
+
break;
|
|
2314
|
+
}
|
|
2315
|
+
const start = mapPos + h * width;
|
|
2316
|
+
for (let w = 0; w < colspan; w++) {
|
|
2317
|
+
if (map[start + w] == 0) map[start + w] = pos;
|
|
2318
|
+
else (problems || (problems = [])).push({
|
|
2319
|
+
type: "collision",
|
|
2320
|
+
row,
|
|
2321
|
+
pos,
|
|
2322
|
+
n: colspan - w
|
|
2323
|
+
});
|
|
2324
|
+
const colW = colwidth && colwidth[w];
|
|
2325
|
+
if (colW) {
|
|
2326
|
+
const widthIndex = (start + w) % width * 2, prev = colWidths[widthIndex];
|
|
2327
|
+
if (prev == null || prev != colW && colWidths[widthIndex + 1] == 1) {
|
|
2328
|
+
colWidths[widthIndex] = colW;
|
|
2329
|
+
colWidths[widthIndex + 1] = 1;
|
|
2330
|
+
} else if (prev == colW) colWidths[widthIndex + 1]++;
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
mapPos += colspan;
|
|
2335
|
+
pos += cellNode.nodeSize;
|
|
2336
|
+
}
|
|
2337
|
+
const expectedPos = (row + 1) * width;
|
|
2338
|
+
let missing = 0;
|
|
2339
|
+
while (mapPos < expectedPos) if (map[mapPos++] == 0) missing++;
|
|
2340
|
+
if (missing) (problems || (problems = [])).push({
|
|
2341
|
+
type: "missing",
|
|
2342
|
+
row,
|
|
2343
|
+
n: missing
|
|
2344
|
+
});
|
|
2345
|
+
pos++;
|
|
2346
|
+
}
|
|
2347
|
+
if (width === 0 || height === 0) (problems || (problems = [])).push({ type: "zero_sized" });
|
|
2348
|
+
const tableMap = new TableMap(width, height, map, problems);
|
|
2349
|
+
let badWidths = false;
|
|
2350
|
+
for (let i = 0; !badWidths && i < colWidths.length; i += 2) if (colWidths[i] != null && colWidths[i + 1] < height) badWidths = true;
|
|
2351
|
+
if (badWidths) findBadColWidths(tableMap, colWidths, table);
|
|
2352
|
+
return tableMap;
|
|
2353
|
+
}
|
|
2354
|
+
function findWidth(table) {
|
|
2355
|
+
let width = -1;
|
|
2356
|
+
let hasRowSpan = false;
|
|
2357
|
+
for (let row = 0; row < table.childCount; row++) {
|
|
2358
|
+
const rowNode = table.child(row);
|
|
2359
|
+
let rowWidth = 0;
|
|
2360
|
+
if (hasRowSpan) for (let j = 0; j < row; j++) {
|
|
2361
|
+
const prevRow = table.child(j);
|
|
2362
|
+
for (let i = 0; i < prevRow.childCount; i++) {
|
|
2363
|
+
const cell = prevRow.child(i);
|
|
2364
|
+
if (j + cell.attrs.rowspan > row) rowWidth += cell.attrs.colspan;
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
for (let i = 0; i < rowNode.childCount; i++) {
|
|
2368
|
+
const cell = rowNode.child(i);
|
|
2369
|
+
rowWidth += cell.attrs.colspan;
|
|
2370
|
+
if (cell.attrs.rowspan > 1) hasRowSpan = true;
|
|
2371
|
+
}
|
|
2372
|
+
if (width == -1) width = rowWidth;
|
|
2373
|
+
else if (width != rowWidth) width = Math.max(width, rowWidth);
|
|
2374
|
+
}
|
|
2375
|
+
return width;
|
|
2376
|
+
}
|
|
2377
|
+
function findBadColWidths(map, colWidths, table) {
|
|
2378
|
+
if (!map.problems) map.problems = [];
|
|
2379
|
+
const seen = {};
|
|
2380
|
+
for (let i = 0; i < map.map.length; i++) {
|
|
2381
|
+
const pos = map.map[i];
|
|
2382
|
+
if (seen[pos]) continue;
|
|
2383
|
+
seen[pos] = true;
|
|
2384
|
+
const node = table.nodeAt(pos);
|
|
2385
|
+
if (!node) throw new RangeError(`No cell with offset ${pos} found`);
|
|
2386
|
+
let updated = null;
|
|
2387
|
+
const attrs = node.attrs;
|
|
2388
|
+
for (let j = 0; j < attrs.colspan; j++) {
|
|
2389
|
+
const colWidth = colWidths[(i + j) % map.width * 2];
|
|
2390
|
+
if (colWidth != null && (!attrs.colwidth || attrs.colwidth[j] != colWidth)) (updated || (updated = freshColWidth(attrs)))[j] = colWidth;
|
|
2391
|
+
}
|
|
2392
|
+
if (updated) map.problems.unshift({
|
|
2393
|
+
type: "colwidth mismatch",
|
|
2394
|
+
pos,
|
|
2395
|
+
colwidth: updated
|
|
2396
|
+
});
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
function freshColWidth(attrs) {
|
|
2400
|
+
if (attrs.colwidth) return attrs.colwidth.slice();
|
|
2401
|
+
const result = [];
|
|
2402
|
+
for (let i = 0; i < attrs.colspan; i++) result.push(0);
|
|
2403
|
+
return result;
|
|
2404
|
+
}
|
|
2405
|
+
/**
|
|
2406
|
+
* @public
|
|
2407
|
+
*/
|
|
2408
|
+
function tableNodeTypes(schema) {
|
|
2409
|
+
let result = schema.cached.tableNodeTypes;
|
|
2410
|
+
if (!result) {
|
|
2411
|
+
result = schema.cached.tableNodeTypes = {};
|
|
2412
|
+
for (const name in schema.nodes) {
|
|
2413
|
+
const type = schema.nodes[name], role = type.spec.tableRole;
|
|
2414
|
+
if (role) result[role] = type;
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
return result;
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
//#endregion
|
|
2421
|
+
//#region src/util.ts
|
|
2422
|
+
/**
|
|
2423
|
+
* @public
|
|
2424
|
+
*/
|
|
2425
|
+
new PluginKey("selectingCells");
|
|
2426
|
+
/**
|
|
2427
|
+
* @public
|
|
2428
|
+
*/
|
|
2429
|
+
function cellAround($pos) {
|
|
2430
|
+
for (let d = $pos.depth - 1; d > 0; d--) if ($pos.node(d).type.spec.tableRole == "row") return $pos.node(0).resolve($pos.before(d + 1));
|
|
2431
|
+
return null;
|
|
2432
|
+
}
|
|
2433
|
+
/**
|
|
2434
|
+
* @public
|
|
2435
|
+
*/
|
|
2436
|
+
function isInTable(state) {
|
|
2437
|
+
const $head = state.selection.$head;
|
|
2438
|
+
for (let d = $head.depth; d > 0; d--) if ($head.node(d).type.spec.tableRole == "row") return true;
|
|
2439
|
+
return false;
|
|
2440
|
+
}
|
|
2441
|
+
/**
|
|
2442
|
+
* @internal
|
|
2443
|
+
*/
|
|
2444
|
+
function selectionCell(state) {
|
|
2445
|
+
const sel = state.selection;
|
|
2446
|
+
if ("$anchorCell" in sel && sel.$anchorCell) return sel.$anchorCell.pos > sel.$headCell.pos ? sel.$anchorCell : sel.$headCell;
|
|
2447
|
+
else if ("node" in sel && sel.node && sel.node.type.spec.tableRole == "cell") return sel.$anchor;
|
|
2448
|
+
const $cell = cellAround(sel.$head) || cellNear(sel.$head);
|
|
2449
|
+
if ($cell) return $cell;
|
|
2450
|
+
throw new RangeError(`No cell found around position ${sel.head}`);
|
|
2451
|
+
}
|
|
2452
|
+
/**
|
|
2453
|
+
* @public
|
|
2454
|
+
*/
|
|
2455
|
+
function cellNear($pos) {
|
|
2456
|
+
for (let after = $pos.nodeAfter, pos = $pos.pos; after; after = after.firstChild, pos++) {
|
|
2457
|
+
const role = after.type.spec.tableRole;
|
|
2458
|
+
if (role == "cell" || role == "header_cell") return $pos.doc.resolve(pos);
|
|
2459
|
+
}
|
|
2460
|
+
for (let before = $pos.nodeBefore, pos = $pos.pos; before; before = before.lastChild, pos--) {
|
|
2461
|
+
const role = before.type.spec.tableRole;
|
|
2462
|
+
if (role == "cell" || role == "header_cell") return $pos.doc.resolve(pos - before.nodeSize);
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
/**
|
|
2466
|
+
* @public
|
|
2467
|
+
*/
|
|
2468
|
+
function pointsAtCell($pos) {
|
|
2469
|
+
return $pos.parent.type.spec.tableRole == "row" && !!$pos.nodeAfter;
|
|
2470
|
+
}
|
|
2471
|
+
/**
|
|
2472
|
+
* @internal
|
|
2473
|
+
*/
|
|
2474
|
+
function inSameTable($cellA, $cellB) {
|
|
2475
|
+
return $cellA.depth == $cellB.depth && $cellA.pos >= $cellB.start(-1) && $cellA.pos <= $cellB.end(-1);
|
|
2476
|
+
}
|
|
2477
|
+
/**
|
|
2478
|
+
* @public
|
|
2479
|
+
*/
|
|
2480
|
+
function nextCell($pos, axis, dir) {
|
|
2481
|
+
const table = $pos.node(-1);
|
|
2482
|
+
const map = TableMap.get(table);
|
|
2483
|
+
const tableStart = $pos.start(-1);
|
|
2484
|
+
const moved = map.nextCell($pos.pos - tableStart, axis, dir);
|
|
2485
|
+
return moved == null ? null : $pos.node(0).resolve(tableStart + moved);
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
* @public
|
|
2489
|
+
*/
|
|
2490
|
+
function removeColSpan(attrs, pos, n = 1) {
|
|
2491
|
+
const result = {
|
|
2492
|
+
...attrs,
|
|
2493
|
+
colspan: attrs.colspan - n
|
|
2494
|
+
};
|
|
2495
|
+
if (result.colwidth) {
|
|
2496
|
+
result.colwidth = result.colwidth.slice();
|
|
2497
|
+
result.colwidth.splice(pos, n);
|
|
2498
|
+
if (!result.colwidth.some((w) => w > 0)) result.colwidth = null;
|
|
2499
|
+
}
|
|
2500
|
+
return result;
|
|
2501
|
+
}
|
|
2502
|
+
|
|
2503
|
+
//#endregion
|
|
2504
|
+
//#region src/cellselection.ts
|
|
2505
|
+
/**
|
|
2506
|
+
* A [`Selection`](http://prosemirror.net/docs/ref/#state.Selection)
|
|
2507
|
+
* subclass that represents a cell selection spanning part of a table.
|
|
2508
|
+
* With the plugin enabled, these will be created when the user
|
|
2509
|
+
* selects across cells, and will be drawn by giving selected cells a
|
|
2510
|
+
* `selectedCell` CSS class.
|
|
2511
|
+
*
|
|
2512
|
+
* @public
|
|
2513
|
+
*/
|
|
2514
|
+
var CellSelection = class CellSelection extends Selection {
|
|
2515
|
+
constructor($anchorCell, $headCell = $anchorCell) {
|
|
2516
|
+
const table = $anchorCell.node(-1);
|
|
2517
|
+
const map = TableMap.get(table);
|
|
2518
|
+
const tableStart = $anchorCell.start(-1);
|
|
2519
|
+
const rect = map.rectBetween($anchorCell.pos - tableStart, $headCell.pos - tableStart);
|
|
2520
|
+
const doc = $anchorCell.node(0);
|
|
2521
|
+
const cells = map.cellsInRect(rect).filter((p) => p != $headCell.pos - tableStart);
|
|
2522
|
+
cells.unshift($headCell.pos - tableStart);
|
|
2523
|
+
const ranges = cells.map((pos) => {
|
|
2524
|
+
const cell = table.nodeAt(pos);
|
|
2525
|
+
if (!cell) throw new RangeError(`No cell with offset ${pos} found`);
|
|
2526
|
+
const from = tableStart + pos + 1;
|
|
2527
|
+
return new SelectionRange(doc.resolve(from), doc.resolve(from + cell.content.size));
|
|
2528
|
+
});
|
|
2529
|
+
super(ranges[0].$from, ranges[0].$to, ranges);
|
|
2530
|
+
this.$anchorCell = $anchorCell;
|
|
2531
|
+
this.$headCell = $headCell;
|
|
2532
|
+
}
|
|
2533
|
+
map(doc, mapping) {
|
|
2534
|
+
const $anchorCell = doc.resolve(mapping.map(this.$anchorCell.pos));
|
|
2535
|
+
const $headCell = doc.resolve(mapping.map(this.$headCell.pos));
|
|
2536
|
+
if (pointsAtCell($anchorCell) && pointsAtCell($headCell) && inSameTable($anchorCell, $headCell)) {
|
|
2537
|
+
const tableChanged = this.$anchorCell.node(-1) != $anchorCell.node(-1);
|
|
2538
|
+
if (tableChanged && this.isRowSelection()) return CellSelection.rowSelection($anchorCell, $headCell);
|
|
2539
|
+
else if (tableChanged && this.isColSelection()) return CellSelection.colSelection($anchorCell, $headCell);
|
|
2540
|
+
else return new CellSelection($anchorCell, $headCell);
|
|
2541
|
+
}
|
|
2542
|
+
return TextSelection.between($anchorCell, $headCell);
|
|
2543
|
+
}
|
|
2544
|
+
content() {
|
|
2545
|
+
const table = this.$anchorCell.node(-1);
|
|
2546
|
+
const map = TableMap.get(table);
|
|
2547
|
+
const tableStart = this.$anchorCell.start(-1);
|
|
2548
|
+
const rect = map.rectBetween(this.$anchorCell.pos - tableStart, this.$headCell.pos - tableStart);
|
|
2549
|
+
const seen = {};
|
|
2550
|
+
const rows = [];
|
|
2551
|
+
for (let row = rect.top; row < rect.bottom; row++) {
|
|
2552
|
+
const rowContent = [];
|
|
2553
|
+
for (let index = row * map.width + rect.left, col = rect.left; col < rect.right; col++, index++) {
|
|
2554
|
+
const pos = map.map[index];
|
|
2555
|
+
if (seen[pos]) continue;
|
|
2556
|
+
seen[pos] = true;
|
|
2557
|
+
const cellRect = map.findCell(pos);
|
|
2558
|
+
let cell = table.nodeAt(pos);
|
|
2559
|
+
if (!cell) throw new RangeError(`No cell with offset ${pos} found`);
|
|
2560
|
+
const extraLeft = rect.left - cellRect.left;
|
|
2561
|
+
const extraRight = cellRect.right - rect.right;
|
|
2562
|
+
if (extraLeft > 0 || extraRight > 0) {
|
|
2563
|
+
let attrs = cell.attrs;
|
|
2564
|
+
if (extraLeft > 0) attrs = removeColSpan(attrs, 0, extraLeft);
|
|
2565
|
+
if (extraRight > 0) attrs = removeColSpan(attrs, attrs.colspan - extraRight, extraRight);
|
|
2566
|
+
if (cellRect.left < rect.left) {
|
|
2567
|
+
cell = cell.type.createAndFill(attrs);
|
|
2568
|
+
if (!cell) throw new RangeError(`Could not create cell with attrs ${JSON.stringify(attrs)}`);
|
|
2569
|
+
} else cell = cell.type.create(attrs, cell.content);
|
|
2570
|
+
}
|
|
2571
|
+
if (cellRect.top < rect.top || cellRect.bottom > rect.bottom) {
|
|
2572
|
+
const attrs = {
|
|
2573
|
+
...cell.attrs,
|
|
2574
|
+
rowspan: Math.min(cellRect.bottom, rect.bottom) - Math.max(cellRect.top, rect.top)
|
|
2575
|
+
};
|
|
2576
|
+
if (cellRect.top < rect.top) cell = cell.type.createAndFill(attrs);
|
|
2577
|
+
else cell = cell.type.create(attrs, cell.content);
|
|
2578
|
+
}
|
|
2579
|
+
rowContent.push(cell);
|
|
2580
|
+
}
|
|
2581
|
+
rows.push(table.child(row).copy(Fragment.from(rowContent)));
|
|
2582
|
+
}
|
|
2583
|
+
const fragment = this.isColSelection() && this.isRowSelection() ? table : rows;
|
|
2584
|
+
return new Slice(Fragment.from(fragment), 1, 1);
|
|
2585
|
+
}
|
|
2586
|
+
replace(tr, content = Slice.empty) {
|
|
2587
|
+
const mapFrom = tr.steps.length, ranges = this.ranges;
|
|
2588
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
2589
|
+
const { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
2590
|
+
tr.replace(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
|
|
2591
|
+
}
|
|
2592
|
+
const sel = Selection.findFrom(tr.doc.resolve(tr.mapping.slice(mapFrom).map(this.to)), -1);
|
|
2593
|
+
if (sel) tr.setSelection(sel);
|
|
2594
|
+
}
|
|
2595
|
+
replaceWith(tr, node) {
|
|
2596
|
+
this.replace(tr, new Slice(Fragment.from(node), 0, 0));
|
|
2597
|
+
}
|
|
2598
|
+
forEachCell(f) {
|
|
2599
|
+
const table = this.$anchorCell.node(-1);
|
|
2600
|
+
const map = TableMap.get(table);
|
|
2601
|
+
const tableStart = this.$anchorCell.start(-1);
|
|
2602
|
+
const cells = map.cellsInRect(map.rectBetween(this.$anchorCell.pos - tableStart, this.$headCell.pos - tableStart));
|
|
2603
|
+
for (let i = 0; i < cells.length; i++) f(table.nodeAt(cells[i]), tableStart + cells[i]);
|
|
2604
|
+
}
|
|
2605
|
+
isColSelection() {
|
|
2606
|
+
const anchorTop = this.$anchorCell.index(-1);
|
|
2607
|
+
const headTop = this.$headCell.index(-1);
|
|
2608
|
+
if (Math.min(anchorTop, headTop) > 0) return false;
|
|
2609
|
+
const anchorBottom = anchorTop + this.$anchorCell.nodeAfter.attrs.rowspan;
|
|
2610
|
+
const headBottom = headTop + this.$headCell.nodeAfter.attrs.rowspan;
|
|
2611
|
+
return Math.max(anchorBottom, headBottom) == this.$headCell.node(-1).childCount;
|
|
2612
|
+
}
|
|
2613
|
+
static colSelection($anchorCell, $headCell = $anchorCell) {
|
|
2614
|
+
const table = $anchorCell.node(-1);
|
|
2615
|
+
const map = TableMap.get(table);
|
|
2616
|
+
const tableStart = $anchorCell.start(-1);
|
|
2617
|
+
const anchorRect = map.findCell($anchorCell.pos - tableStart);
|
|
2618
|
+
const headRect = map.findCell($headCell.pos - tableStart);
|
|
2619
|
+
const doc = $anchorCell.node(0);
|
|
2620
|
+
if (anchorRect.top <= headRect.top) {
|
|
2621
|
+
if (anchorRect.top > 0) $anchorCell = doc.resolve(tableStart + map.map[anchorRect.left]);
|
|
2622
|
+
if (headRect.bottom < map.height) $headCell = doc.resolve(tableStart + map.map[map.width * (map.height - 1) + headRect.right - 1]);
|
|
2623
|
+
} else {
|
|
2624
|
+
if (headRect.top > 0) $headCell = doc.resolve(tableStart + map.map[headRect.left]);
|
|
2625
|
+
if (anchorRect.bottom < map.height) $anchorCell = doc.resolve(tableStart + map.map[map.width * (map.height - 1) + anchorRect.right - 1]);
|
|
2626
|
+
}
|
|
2627
|
+
return new CellSelection($anchorCell, $headCell);
|
|
2628
|
+
}
|
|
2629
|
+
isRowSelection() {
|
|
2630
|
+
const table = this.$anchorCell.node(-1);
|
|
2631
|
+
const map = TableMap.get(table);
|
|
2632
|
+
const tableStart = this.$anchorCell.start(-1);
|
|
2633
|
+
const anchorLeft = map.colCount(this.$anchorCell.pos - tableStart);
|
|
2634
|
+
const headLeft = map.colCount(this.$headCell.pos - tableStart);
|
|
2635
|
+
if (Math.min(anchorLeft, headLeft) > 0) return false;
|
|
2636
|
+
const anchorRight = anchorLeft + this.$anchorCell.nodeAfter.attrs.colspan;
|
|
2637
|
+
const headRight = headLeft + this.$headCell.nodeAfter.attrs.colspan;
|
|
2638
|
+
return Math.max(anchorRight, headRight) == map.width;
|
|
2639
|
+
}
|
|
2640
|
+
eq(other) {
|
|
2641
|
+
return other instanceof CellSelection && other.$anchorCell.pos == this.$anchorCell.pos && other.$headCell.pos == this.$headCell.pos;
|
|
2642
|
+
}
|
|
2643
|
+
static rowSelection($anchorCell, $headCell = $anchorCell) {
|
|
2644
|
+
const table = $anchorCell.node(-1);
|
|
2645
|
+
const map = TableMap.get(table);
|
|
2646
|
+
const tableStart = $anchorCell.start(-1);
|
|
2647
|
+
const anchorRect = map.findCell($anchorCell.pos - tableStart);
|
|
2648
|
+
const headRect = map.findCell($headCell.pos - tableStart);
|
|
2649
|
+
const doc = $anchorCell.node(0);
|
|
2650
|
+
if (anchorRect.left <= headRect.left) {
|
|
2651
|
+
if (anchorRect.left > 0) $anchorCell = doc.resolve(tableStart + map.map[anchorRect.top * map.width]);
|
|
2652
|
+
if (headRect.right < map.width) $headCell = doc.resolve(tableStart + map.map[map.width * (headRect.top + 1) - 1]);
|
|
2653
|
+
} else {
|
|
2654
|
+
if (headRect.left > 0) $headCell = doc.resolve(tableStart + map.map[headRect.top * map.width]);
|
|
2655
|
+
if (anchorRect.right < map.width) $anchorCell = doc.resolve(tableStart + map.map[map.width * (anchorRect.top + 1) - 1]);
|
|
2656
|
+
}
|
|
2657
|
+
return new CellSelection($anchorCell, $headCell);
|
|
2658
|
+
}
|
|
2659
|
+
toJSON() {
|
|
2660
|
+
return {
|
|
2661
|
+
type: "cell",
|
|
2662
|
+
anchor: this.$anchorCell.pos,
|
|
2663
|
+
head: this.$headCell.pos
|
|
2664
|
+
};
|
|
2665
|
+
}
|
|
2666
|
+
static fromJSON(doc, json) {
|
|
2667
|
+
return new CellSelection(doc.resolve(json.anchor), doc.resolve(json.head));
|
|
2668
|
+
}
|
|
2669
|
+
static create(doc, anchorCell, headCell = anchorCell) {
|
|
2670
|
+
return new CellSelection(doc.resolve(anchorCell), doc.resolve(headCell));
|
|
2671
|
+
}
|
|
2672
|
+
getBookmark() {
|
|
2673
|
+
return new CellBookmark(this.$anchorCell.pos, this.$headCell.pos);
|
|
2674
|
+
}
|
|
2675
|
+
};
|
|
2676
|
+
CellSelection.prototype.visible = false;
|
|
2677
|
+
Selection.jsonID("cell", CellSelection);
|
|
2678
|
+
/**
|
|
2679
|
+
* @public
|
|
2680
|
+
*/
|
|
2681
|
+
var CellBookmark = class CellBookmark {
|
|
2682
|
+
constructor(anchor, head) {
|
|
2683
|
+
this.anchor = anchor;
|
|
2684
|
+
this.head = head;
|
|
2685
|
+
}
|
|
2686
|
+
map(mapping) {
|
|
2687
|
+
return new CellBookmark(mapping.map(this.anchor), mapping.map(this.head));
|
|
2688
|
+
}
|
|
2689
|
+
resolve(doc) {
|
|
2690
|
+
const $anchorCell = doc.resolve(this.anchor), $headCell = doc.resolve(this.head);
|
|
2691
|
+
if ($anchorCell.parent.type.spec.tableRole == "row" && $headCell.parent.type.spec.tableRole == "row" && $anchorCell.index() < $anchorCell.parent.childCount && $headCell.index() < $headCell.parent.childCount && inSameTable($anchorCell, $headCell)) return new CellSelection($anchorCell, $headCell);
|
|
2692
|
+
else return Selection.near($headCell, 1);
|
|
2693
|
+
}
|
|
2694
|
+
};
|
|
2695
|
+
|
|
2696
|
+
//#endregion
|
|
2697
|
+
//#region src/fixtables.ts
|
|
2698
|
+
/**
|
|
2699
|
+
* @public
|
|
2700
|
+
*/
|
|
2701
|
+
new PluginKey("fix-tables");
|
|
2702
|
+
/**
|
|
2703
|
+
* Find the closest table node for a given position.
|
|
2704
|
+
*
|
|
2705
|
+
* @public
|
|
2706
|
+
*/
|
|
2707
|
+
function findTable($pos) {
|
|
2708
|
+
return findParentNode((node) => node.type.spec.tableRole === "table", $pos);
|
|
2709
|
+
}
|
|
2710
|
+
/**
|
|
2711
|
+
* Find the closest parent node that satisfies the predicate.
|
|
2712
|
+
*
|
|
2713
|
+
* @internal
|
|
2714
|
+
*/
|
|
2715
|
+
function findParentNode(predicate, $pos) {
|
|
2716
|
+
for (let depth = $pos.depth; depth >= 0; depth -= 1) {
|
|
2717
|
+
const node = $pos.node(depth);
|
|
2718
|
+
if (predicate(node)) return {
|
|
2719
|
+
node,
|
|
2720
|
+
pos: depth === 0 ? 0 : $pos.before(depth),
|
|
2721
|
+
start: $pos.start(depth),
|
|
2722
|
+
depth
|
|
2723
|
+
};
|
|
2724
|
+
}
|
|
2725
|
+
return null;
|
|
2726
|
+
}
|
|
2727
|
+
|
|
2728
|
+
//#endregion
|
|
2729
|
+
//#region src/commands.ts
|
|
2730
|
+
/**
|
|
2731
|
+
* Helper to get the selected rectangle in a table, if any. Adds table
|
|
2732
|
+
* map, table node, and table start offset to the object for
|
|
2733
|
+
* convenience.
|
|
2734
|
+
*
|
|
2735
|
+
* @public
|
|
2736
|
+
*/
|
|
2737
|
+
function selectedRect(state) {
|
|
2738
|
+
const sel = state.selection;
|
|
2739
|
+
const $pos = selectionCell(state);
|
|
2740
|
+
const table = $pos.node(-1);
|
|
2741
|
+
const tableStart = $pos.start(-1);
|
|
2742
|
+
const map = TableMap.get(table);
|
|
2743
|
+
return {
|
|
2744
|
+
...sel instanceof CellSelection ? map.rectBetween(sel.$anchorCell.pos - tableStart, sel.$headCell.pos - tableStart) : map.findCell($pos.pos - tableStart),
|
|
2745
|
+
tableStart,
|
|
2746
|
+
map,
|
|
2747
|
+
table
|
|
2748
|
+
};
|
|
2749
|
+
}
|
|
2750
|
+
function deprecated_toggleHeader(type) {
|
|
2751
|
+
return function(state, dispatch) {
|
|
2752
|
+
if (!isInTable(state)) return false;
|
|
2753
|
+
if (dispatch) {
|
|
2754
|
+
const types = tableNodeTypes(state.schema);
|
|
2755
|
+
const rect = selectedRect(state), tr = state.tr;
|
|
2756
|
+
const cells = rect.map.cellsInRect(type == "column" ? {
|
|
2757
|
+
left: rect.left,
|
|
2758
|
+
top: 0,
|
|
2759
|
+
right: rect.right,
|
|
2760
|
+
bottom: rect.map.height
|
|
2761
|
+
} : type == "row" ? {
|
|
2762
|
+
left: 0,
|
|
2763
|
+
top: rect.top,
|
|
2764
|
+
right: rect.map.width,
|
|
2765
|
+
bottom: rect.bottom
|
|
2766
|
+
} : rect);
|
|
2767
|
+
const nodes = cells.map((pos) => rect.table.nodeAt(pos));
|
|
2768
|
+
for (let i = 0; i < cells.length; i++) if (nodes[i].type == types.header_cell) tr.setNodeMarkup(rect.tableStart + cells[i], types.cell, nodes[i].attrs);
|
|
2769
|
+
if (tr.steps.length === 0) for (let i = 0; i < cells.length; i++) tr.setNodeMarkup(rect.tableStart + cells[i], types.header_cell, nodes[i].attrs);
|
|
2770
|
+
dispatch(tr);
|
|
2771
|
+
}
|
|
2772
|
+
return true;
|
|
2773
|
+
};
|
|
2774
|
+
}
|
|
2775
|
+
function isHeaderEnabledByType(type, rect, types) {
|
|
2776
|
+
const cellPositions = rect.map.cellsInRect({
|
|
2777
|
+
left: 0,
|
|
2778
|
+
top: 0,
|
|
2779
|
+
right: type == "row" ? rect.map.width : 1,
|
|
2780
|
+
bottom: type == "column" ? rect.map.height : 1
|
|
2781
|
+
});
|
|
2782
|
+
for (let i = 0; i < cellPositions.length; i++) {
|
|
2783
|
+
const cell = rect.table.nodeAt(cellPositions[i]);
|
|
2784
|
+
if (cell && cell.type !== types.header_cell) return false;
|
|
2785
|
+
}
|
|
2786
|
+
return true;
|
|
2787
|
+
}
|
|
2788
|
+
/**
|
|
2789
|
+
* Toggles between row/column header and normal cells (Only applies to first row/column).
|
|
2790
|
+
* For deprecated behavior pass `useDeprecatedLogic` in options with true.
|
|
2791
|
+
*
|
|
2792
|
+
* @public
|
|
2793
|
+
*/
|
|
2794
|
+
function toggleHeader(type, options) {
|
|
2795
|
+
options = options || { useDeprecatedLogic: false };
|
|
2796
|
+
if (options.useDeprecatedLogic) return deprecated_toggleHeader(type);
|
|
2797
|
+
return function(state, dispatch) {
|
|
2798
|
+
if (!isInTable(state)) return false;
|
|
2799
|
+
if (dispatch) {
|
|
2800
|
+
const types = tableNodeTypes(state.schema);
|
|
2801
|
+
const rect = selectedRect(state), tr = state.tr;
|
|
2802
|
+
const isHeaderRowEnabled = isHeaderEnabledByType("row", rect, types);
|
|
2803
|
+
const isHeaderColumnEnabled = isHeaderEnabledByType("column", rect, types);
|
|
2804
|
+
const selectionStartsAt = (type === "column" ? isHeaderRowEnabled : type === "row" ? isHeaderColumnEnabled : false) ? 1 : 0;
|
|
2805
|
+
const cellsRect = type == "column" ? {
|
|
2806
|
+
left: 0,
|
|
2807
|
+
top: selectionStartsAt,
|
|
2808
|
+
right: 1,
|
|
2809
|
+
bottom: rect.map.height
|
|
2810
|
+
} : type == "row" ? {
|
|
2811
|
+
left: selectionStartsAt,
|
|
2812
|
+
top: 0,
|
|
2813
|
+
right: rect.map.width,
|
|
2814
|
+
bottom: 1
|
|
2815
|
+
} : rect;
|
|
2816
|
+
const newType = type == "column" ? isHeaderColumnEnabled ? types.cell : types.header_cell : type == "row" ? isHeaderRowEnabled ? types.cell : types.header_cell : types.cell;
|
|
2817
|
+
rect.map.cellsInRect(cellsRect).forEach((relativeCellPos) => {
|
|
2818
|
+
const cellPos = relativeCellPos + rect.tableStart;
|
|
2819
|
+
const cell = tr.doc.nodeAt(cellPos);
|
|
2820
|
+
if (cell) tr.setNodeMarkup(cellPos, newType, cell.attrs);
|
|
2821
|
+
});
|
|
2822
|
+
dispatch(tr);
|
|
2823
|
+
}
|
|
2824
|
+
return true;
|
|
2825
|
+
};
|
|
2826
|
+
}
|
|
2827
|
+
/**
|
|
2828
|
+
* Toggles whether the selected row contains header cells.
|
|
2829
|
+
*
|
|
2830
|
+
* @public
|
|
2831
|
+
*/
|
|
2832
|
+
toggleHeader("row", { useDeprecatedLogic: true });
|
|
2833
|
+
/**
|
|
2834
|
+
* Toggles whether the selected column contains header cells.
|
|
2835
|
+
*
|
|
2836
|
+
* @public
|
|
2837
|
+
*/
|
|
2838
|
+
toggleHeader("column", { useDeprecatedLogic: true });
|
|
2839
|
+
/**
|
|
2840
|
+
* Toggles whether the selected cells are header cells.
|
|
2841
|
+
*
|
|
2842
|
+
* @public
|
|
2843
|
+
*/
|
|
2844
|
+
toggleHeader("cell", { useDeprecatedLogic: true });
|
|
2845
|
+
/**
|
|
2846
|
+
* Deletes the content of the selected cells, if they are not empty.
|
|
2847
|
+
*
|
|
2848
|
+
* @public
|
|
2849
|
+
*/
|
|
2850
|
+
function deleteCellSelection(state, dispatch) {
|
|
2851
|
+
const sel = state.selection;
|
|
2852
|
+
if (!(sel instanceof CellSelection)) return false;
|
|
2853
|
+
if (dispatch) {
|
|
2854
|
+
const tr = state.tr;
|
|
2855
|
+
const baseContent = tableNodeTypes(state.schema).cell.createAndFill().content;
|
|
2856
|
+
sel.forEachCell((cell, pos) => {
|
|
2857
|
+
if (!cell.content.eq(baseContent)) tr.replace(tr.mapping.map(pos + 1), tr.mapping.map(pos + cell.nodeSize - 1), new Slice(baseContent, 0, 0));
|
|
2858
|
+
});
|
|
2859
|
+
if (tr.docChanged) dispatch(tr);
|
|
2860
|
+
}
|
|
2861
|
+
return true;
|
|
2862
|
+
}
|
|
2863
|
+
|
|
2864
|
+
//#endregion
|
|
2865
|
+
//#region src/input.ts
|
|
2866
|
+
keydownHandler({
|
|
2867
|
+
ArrowLeft: arrow("horiz", -1),
|
|
2868
|
+
ArrowRight: arrow("horiz", 1),
|
|
2869
|
+
ArrowUp: arrow("vert", -1),
|
|
2870
|
+
ArrowDown: arrow("vert", 1),
|
|
2871
|
+
"Shift-ArrowLeft": shiftArrow("horiz", -1),
|
|
2872
|
+
"Shift-ArrowRight": shiftArrow("horiz", 1),
|
|
2873
|
+
"Shift-ArrowUp": shiftArrow("vert", -1),
|
|
2874
|
+
"Shift-ArrowDown": shiftArrow("vert", 1),
|
|
2875
|
+
Backspace: deleteCellSelection,
|
|
2876
|
+
"Mod-Backspace": deleteCellSelection,
|
|
2877
|
+
Delete: deleteCellSelection,
|
|
2878
|
+
"Mod-Delete": deleteCellSelection
|
|
2879
|
+
});
|
|
2880
|
+
function maybeSetSelection(state, dispatch, selection) {
|
|
2881
|
+
if (selection.eq(state.selection)) return false;
|
|
2882
|
+
if (dispatch) dispatch(state.tr.setSelection(selection).scrollIntoView());
|
|
2883
|
+
return true;
|
|
2884
|
+
}
|
|
2885
|
+
/**
|
|
2886
|
+
* @internal
|
|
2887
|
+
*/
|
|
2888
|
+
function arrow(axis, dir) {
|
|
2889
|
+
return (state, dispatch, view) => {
|
|
2890
|
+
if (!view) return false;
|
|
2891
|
+
const sel = state.selection;
|
|
2892
|
+
if (sel instanceof CellSelection) return maybeSetSelection(state, dispatch, Selection.near(sel.$headCell, dir));
|
|
2893
|
+
if (axis != "horiz" && !sel.empty) return false;
|
|
2894
|
+
const end = atEndOfCell(view, axis, dir);
|
|
2895
|
+
if (end == null) return false;
|
|
2896
|
+
if (axis == "horiz") return maybeSetSelection(state, dispatch, Selection.near(state.doc.resolve(sel.head + dir), dir));
|
|
2897
|
+
else {
|
|
2898
|
+
const $cell = state.doc.resolve(end);
|
|
2899
|
+
const $next = nextCell($cell, axis, dir);
|
|
2900
|
+
let newSel;
|
|
2901
|
+
if ($next) newSel = Selection.near($next, 1);
|
|
2902
|
+
else if (dir < 0) newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1);
|
|
2903
|
+
else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1);
|
|
2904
|
+
return maybeSetSelection(state, dispatch, newSel);
|
|
2905
|
+
}
|
|
2906
|
+
};
|
|
2907
|
+
}
|
|
2908
|
+
function shiftArrow(axis, dir) {
|
|
2909
|
+
return (state, dispatch, view) => {
|
|
2910
|
+
if (!view) return false;
|
|
2911
|
+
const sel = state.selection;
|
|
2912
|
+
let cellSel;
|
|
2913
|
+
if (sel instanceof CellSelection) cellSel = sel;
|
|
2914
|
+
else {
|
|
2915
|
+
const end = atEndOfCell(view, axis, dir);
|
|
2916
|
+
if (end == null) return false;
|
|
2917
|
+
cellSel = new CellSelection(state.doc.resolve(end));
|
|
2918
|
+
}
|
|
2919
|
+
const $head = nextCell(cellSel.$headCell, axis, dir);
|
|
2920
|
+
if (!$head) return false;
|
|
2921
|
+
return maybeSetSelection(state, dispatch, new CellSelection(cellSel.$anchorCell, $head));
|
|
2922
|
+
};
|
|
2923
|
+
}
|
|
2924
|
+
function atEndOfCell(view, axis, dir) {
|
|
2925
|
+
if (!(view.state.selection instanceof TextSelection)) return null;
|
|
2926
|
+
const { $head } = view.state.selection;
|
|
2927
|
+
for (let d = $head.depth - 1; d >= 0; d--) {
|
|
2928
|
+
const parent = $head.node(d);
|
|
2929
|
+
if ((dir < 0 ? $head.index(d) : $head.indexAfter(d)) != (dir < 0 ? 0 : parent.childCount)) return null;
|
|
2930
|
+
if (parent.type.spec.tableRole == "cell" || parent.type.spec.tableRole == "header_cell") {
|
|
2931
|
+
const cellPos = $head.before(d);
|
|
2932
|
+
const dirStr = axis == "vert" ? dir > 0 ? "down" : "up" : dir > 0 ? "right" : "left";
|
|
2933
|
+
return view.endOfTextblock(dirStr) ? cellPos : null;
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
return null;
|
|
2937
|
+
}
|
|
2938
|
+
|
|
2939
|
+
//#endregion
|
|
2940
|
+
//#region src/columnresizing.ts
|
|
2941
|
+
/**
|
|
2942
|
+
* @public
|
|
2943
|
+
*/
|
|
2944
|
+
new PluginKey("tableColumnResizing");
|
|
2945
|
+
|
|
2946
|
+
function keepAlive(..._args) {
|
|
2947
|
+
}
|
|
2948
|
+
|
|
2949
|
+
keepAlive(h);
|
|
2950
|
+
function Icon({ icon, class: className, onClick }) {
|
|
2951
|
+
return /* @__PURE__ */ h(
|
|
2952
|
+
"span",
|
|
2953
|
+
{
|
|
2954
|
+
class: clsx("milkdown-icon", className),
|
|
2955
|
+
onPointerdown: onClick,
|
|
2956
|
+
innerHTML: icon ? DOMPurify.sanitize(icon.trim()) : void 0
|
|
2957
|
+
}
|
|
2958
|
+
);
|
|
2959
|
+
}
|
|
2960
|
+
Icon.props = {
|
|
2961
|
+
icon: {
|
|
2962
|
+
type: String,
|
|
2963
|
+
required: false
|
|
2964
|
+
},
|
|
2965
|
+
class: {
|
|
2966
|
+
type: String,
|
|
2967
|
+
required: false
|
|
2968
|
+
},
|
|
2969
|
+
onClick: {
|
|
2970
|
+
type: Function,
|
|
2971
|
+
required: false
|
|
2972
|
+
}
|
|
2973
|
+
};
|
|
2974
|
+
|
|
2975
|
+
function prepareDndContext(refs) {
|
|
2976
|
+
const {
|
|
2977
|
+
dragPreviewRef,
|
|
2978
|
+
tableWrapperRef,
|
|
2979
|
+
contentWrapperRef,
|
|
2980
|
+
yLineHandleRef,
|
|
2981
|
+
xLineHandleRef,
|
|
2982
|
+
colHandleRef,
|
|
2983
|
+
rowHandleRef
|
|
2984
|
+
} = refs;
|
|
2985
|
+
const preview = dragPreviewRef.value;
|
|
2986
|
+
if (!preview) return;
|
|
2987
|
+
const wrapper = tableWrapperRef.value;
|
|
2988
|
+
if (!wrapper) return;
|
|
2989
|
+
const content = contentWrapperRef.value;
|
|
2990
|
+
if (!content) return;
|
|
2991
|
+
const contentRoot = content.querySelector("tbody");
|
|
2992
|
+
if (!contentRoot) return;
|
|
2993
|
+
const previewRoot = preview.querySelector("tbody");
|
|
2994
|
+
if (!previewRoot) return;
|
|
2995
|
+
const yHandle = yLineHandleRef.value;
|
|
2996
|
+
if (!yHandle) return;
|
|
2997
|
+
const xHandle = xLineHandleRef.value;
|
|
2998
|
+
if (!xHandle) return;
|
|
2999
|
+
const colHandle = colHandleRef.value;
|
|
3000
|
+
if (!colHandle) return;
|
|
3001
|
+
const rowHandle = rowHandleRef.value;
|
|
3002
|
+
if (!rowHandle) return;
|
|
3003
|
+
const context = {
|
|
3004
|
+
preview,
|
|
3005
|
+
wrapper,
|
|
3006
|
+
content,
|
|
3007
|
+
contentRoot,
|
|
3008
|
+
previewRoot,
|
|
3009
|
+
yHandle,
|
|
3010
|
+
xHandle,
|
|
3011
|
+
colHandle,
|
|
3012
|
+
rowHandle
|
|
3013
|
+
};
|
|
3014
|
+
return context;
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3017
|
+
function clearPreview(previewRoot) {
|
|
3018
|
+
while (previewRoot.firstChild) previewRoot.removeChild(previewRoot.firstChild);
|
|
3019
|
+
}
|
|
3020
|
+
function renderPreview(axis, preview, previewRoot, tableContent, index) {
|
|
3021
|
+
const { width: tableWidth, height: tableHeight } = tableContent.querySelector("tbody").getBoundingClientRect();
|
|
3022
|
+
if (axis === "y") {
|
|
3023
|
+
const rows = tableContent.querySelectorAll("tr");
|
|
3024
|
+
const row = rows[index];
|
|
3025
|
+
if (!row) return;
|
|
3026
|
+
previewRoot.appendChild(row.cloneNode(true));
|
|
3027
|
+
const height = row.getBoundingClientRect().height;
|
|
3028
|
+
Object.assign(preview.style, {
|
|
3029
|
+
width: `${tableWidth}px`,
|
|
3030
|
+
height: `${height}px`
|
|
3031
|
+
});
|
|
3032
|
+
preview.dataset.show = "true";
|
|
3033
|
+
return;
|
|
3034
|
+
}
|
|
3035
|
+
if (axis === "x") {
|
|
3036
|
+
const rows = tableContent.querySelectorAll("tr");
|
|
3037
|
+
let width;
|
|
3038
|
+
Array.from(rows).forEach((row) => {
|
|
3039
|
+
const col = row.children[index];
|
|
3040
|
+
if (!col) return;
|
|
3041
|
+
if (width === void 0) width = col.getBoundingClientRect().width;
|
|
3042
|
+
const tr = col.parentElement.cloneNode(false);
|
|
3043
|
+
const clone = col.cloneNode(true);
|
|
3044
|
+
tr.appendChild(clone);
|
|
3045
|
+
previewRoot.appendChild(tr);
|
|
3046
|
+
});
|
|
3047
|
+
Object.assign(preview.style, {
|
|
3048
|
+
width: `${width}px`,
|
|
3049
|
+
height: `${tableHeight}px`
|
|
3050
|
+
});
|
|
3051
|
+
preview.dataset.show = "true";
|
|
3052
|
+
return;
|
|
3053
|
+
}
|
|
3054
|
+
}
|
|
3055
|
+
|
|
3056
|
+
function createDragRowHandler(refs, ctx) {
|
|
3057
|
+
return (event) => {
|
|
3058
|
+
handleDrag(refs, event, ctx, (context) => {
|
|
3059
|
+
updateDragInfo("y", event, context, refs);
|
|
3060
|
+
const { preview, content, previewRoot } = context;
|
|
3061
|
+
clearPreview(previewRoot);
|
|
3062
|
+
const { hoverIndex } = refs;
|
|
3063
|
+
const [rowIndex] = hoverIndex.value;
|
|
3064
|
+
renderPreview("y", preview, previewRoot, content, rowIndex);
|
|
3065
|
+
});
|
|
3066
|
+
};
|
|
3067
|
+
}
|
|
3068
|
+
function createDragColHandler(refs, ctx) {
|
|
3069
|
+
return (event) => {
|
|
3070
|
+
handleDrag(refs, event, ctx, (context) => {
|
|
3071
|
+
updateDragInfo("x", event, context, refs);
|
|
3072
|
+
const { preview, content, previewRoot } = context;
|
|
3073
|
+
const { hoverIndex } = refs;
|
|
3074
|
+
const [_, colIndex] = hoverIndex.value;
|
|
3075
|
+
clearPreview(previewRoot);
|
|
3076
|
+
renderPreview("x", preview, previewRoot, content, colIndex);
|
|
3077
|
+
});
|
|
3078
|
+
};
|
|
3079
|
+
}
|
|
3080
|
+
function updateDragInfo(axis, event, context, refs) {
|
|
3081
|
+
const { xHandle, yHandle, colHandle, rowHandle, preview } = context;
|
|
3082
|
+
xHandle.dataset.displayType = axis === "y" ? "indicator" : "none";
|
|
3083
|
+
yHandle.dataset.displayType = axis === "x" ? "indicator" : "none";
|
|
3084
|
+
if (axis === "y") {
|
|
3085
|
+
colHandle.dataset.show = "false";
|
|
3086
|
+
hideButtonGroup(rowHandle);
|
|
3087
|
+
} else {
|
|
3088
|
+
rowHandle.dataset.show = "false";
|
|
3089
|
+
hideButtonGroup(colHandle);
|
|
3090
|
+
}
|
|
3091
|
+
const { hoverIndex, dragInfo } = refs;
|
|
3092
|
+
const [rowIndex, colIndex] = hoverIndex.value;
|
|
3093
|
+
dragInfo.value = {
|
|
3094
|
+
startCoords: [event.clientX, event.clientY],
|
|
3095
|
+
startIndex: axis === "y" ? rowIndex : colIndex,
|
|
3096
|
+
endIndex: axis === "y" ? rowIndex : colIndex,
|
|
3097
|
+
type: axis === "y" ? "row" : "col"
|
|
3098
|
+
};
|
|
3099
|
+
preview.dataset.direction = axis === "y" ? "vertical" : "horizontal";
|
|
3100
|
+
}
|
|
3101
|
+
function handleDrag(refs, event, ctx, fn) {
|
|
3102
|
+
const view = ctx == null ? void 0 : ctx.get(editorViewCtx);
|
|
3103
|
+
if (!(view == null ? void 0 : view.editable)) return;
|
|
3104
|
+
event.stopPropagation();
|
|
3105
|
+
if (event.dataTransfer) event.dataTransfer.effectAllowed = "move";
|
|
3106
|
+
const context = prepareDndContext(refs);
|
|
3107
|
+
if (!context) return;
|
|
3108
|
+
requestAnimationFrame(() => {
|
|
3109
|
+
fn(context);
|
|
3110
|
+
});
|
|
3111
|
+
}
|
|
3112
|
+
function hideButtonGroup(handle) {
|
|
3113
|
+
var _a;
|
|
3114
|
+
(_a = handle.querySelector(".button-group")) == null ? void 0 : _a.setAttribute("data-show", "false");
|
|
3115
|
+
}
|
|
3116
|
+
|
|
3117
|
+
function findNodeIndex(parent, child) {
|
|
3118
|
+
for (let i = 0; i < parent.childCount; i++) {
|
|
3119
|
+
if (parent.child(i) === child) return i;
|
|
3120
|
+
}
|
|
3121
|
+
return -1;
|
|
3122
|
+
}
|
|
3123
|
+
function findPointerIndex(event, view) {
|
|
3124
|
+
var _a, _b, _c;
|
|
3125
|
+
if (!view) return;
|
|
3126
|
+
try {
|
|
3127
|
+
const posAtCoords = view.posAtCoords({
|
|
3128
|
+
left: event.clientX,
|
|
3129
|
+
top: event.clientY
|
|
3130
|
+
});
|
|
3131
|
+
if (!posAtCoords) return;
|
|
3132
|
+
const pos = posAtCoords == null ? void 0 : posAtCoords.inside;
|
|
3133
|
+
if (pos == null || pos < 0) return;
|
|
3134
|
+
const $pos = view.state.doc.resolve(pos);
|
|
3135
|
+
const node = view.state.doc.nodeAt(pos);
|
|
3136
|
+
if (!node) return;
|
|
3137
|
+
const cellType = ["table_cell", "table_header"];
|
|
3138
|
+
const rowType = ["table_row", "table_header_row"];
|
|
3139
|
+
const cell = cellType.includes(node.type.name) ? node : (_a = findParent((node2) => cellType.includes(node2.type.name))($pos)) == null ? void 0 : _a.node;
|
|
3140
|
+
const row = (_b = findParent((node2) => rowType.includes(node2.type.name))(
|
|
3141
|
+
$pos
|
|
3142
|
+
)) == null ? void 0 : _b.node;
|
|
3143
|
+
const table = (_c = findParent((node2) => node2.type.name === "table")($pos)) == null ? void 0 : _c.node;
|
|
3144
|
+
if (!cell || !row || !table) return;
|
|
3145
|
+
const columnIndex = findNodeIndex(row, cell);
|
|
3146
|
+
const rowIndex = findNodeIndex(table, row);
|
|
3147
|
+
return [rowIndex, columnIndex];
|
|
3148
|
+
} catch (e) {
|
|
3149
|
+
return void 0;
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
function getRelatedDOM(contentWrapperRef, [rowIndex, columnIndex]) {
|
|
3153
|
+
const content = contentWrapperRef.value;
|
|
3154
|
+
if (!content) return;
|
|
3155
|
+
const rows = content.querySelectorAll("tr");
|
|
3156
|
+
const row = rows[rowIndex];
|
|
3157
|
+
if (!row) return;
|
|
3158
|
+
const firstRow = rows[0];
|
|
3159
|
+
if (!firstRow) return;
|
|
3160
|
+
const headerCol = firstRow.children[columnIndex];
|
|
3161
|
+
if (!headerCol) return;
|
|
3162
|
+
const col = row.children[columnIndex];
|
|
3163
|
+
if (!col) return;
|
|
3164
|
+
return {
|
|
3165
|
+
row,
|
|
3166
|
+
col,
|
|
3167
|
+
headerCol
|
|
3168
|
+
};
|
|
3169
|
+
}
|
|
3170
|
+
function recoveryStateBetweenUpdate(refs, view, node) {
|
|
3171
|
+
if (!node) return;
|
|
3172
|
+
if (!view) return;
|
|
3173
|
+
const { selection } = view.state;
|
|
3174
|
+
if (!(selection instanceof CellSelection)) return;
|
|
3175
|
+
const { $from } = selection;
|
|
3176
|
+
const table = findTable($from);
|
|
3177
|
+
if (!table || table.node !== node) return;
|
|
3178
|
+
if (selection.isColSelection()) {
|
|
3179
|
+
const { $head } = selection;
|
|
3180
|
+
const colIndex = $head.index($head.depth - 1);
|
|
3181
|
+
computeColHandlePositionByIndex({
|
|
3182
|
+
refs,
|
|
3183
|
+
index: [0, colIndex],
|
|
3184
|
+
before: (handleDOM) => {
|
|
3185
|
+
var _a;
|
|
3186
|
+
(_a = handleDOM.querySelector(".button-group")) == null ? void 0 : _a.setAttribute("data-show", "true");
|
|
3187
|
+
}
|
|
3188
|
+
});
|
|
3189
|
+
return;
|
|
3190
|
+
}
|
|
3191
|
+
if (selection.isRowSelection()) {
|
|
3192
|
+
const { $head } = selection;
|
|
3193
|
+
const rowNode = findParent(
|
|
3194
|
+
(node2) => node2.type.name === "table_row" || node2.type.name === "table_header_row"
|
|
3195
|
+
)($head);
|
|
3196
|
+
if (!rowNode) return;
|
|
3197
|
+
const rowIndex = findNodeIndex(table.node, rowNode.node);
|
|
3198
|
+
computeRowHandlePositionByIndex({
|
|
3199
|
+
refs,
|
|
3200
|
+
index: [rowIndex, 0],
|
|
3201
|
+
before: (handleDOM) => {
|
|
3202
|
+
var _a;
|
|
3203
|
+
if (rowIndex > 0)
|
|
3204
|
+
(_a = handleDOM.querySelector(".button-group")) == null ? void 0 : _a.setAttribute("data-show", "true");
|
|
3205
|
+
}
|
|
3206
|
+
});
|
|
3207
|
+
}
|
|
3208
|
+
}
|
|
3209
|
+
function computeColHandlePositionByIndex({
|
|
3210
|
+
refs,
|
|
3211
|
+
index,
|
|
3212
|
+
before,
|
|
3213
|
+
after
|
|
3214
|
+
}) {
|
|
3215
|
+
const { contentWrapperRef, colHandleRef, hoverIndex } = refs;
|
|
3216
|
+
const colHandle = colHandleRef.value;
|
|
3217
|
+
if (!colHandle) return;
|
|
3218
|
+
hoverIndex.value = index;
|
|
3219
|
+
const dom = getRelatedDOM(contentWrapperRef, index);
|
|
3220
|
+
if (!dom) return;
|
|
3221
|
+
const { headerCol: col } = dom;
|
|
3222
|
+
colHandle.dataset.show = "true";
|
|
3223
|
+
if (before) before(colHandle);
|
|
3224
|
+
computePosition(col, colHandle, { placement: "top" }).then(({ x, y }) => {
|
|
3225
|
+
Object.assign(colHandle.style, {
|
|
3226
|
+
left: `${x}px`,
|
|
3227
|
+
top: `${y}px`
|
|
3228
|
+
});
|
|
3229
|
+
if (after) after(colHandle);
|
|
3230
|
+
}).catch(console.error);
|
|
3231
|
+
}
|
|
3232
|
+
function computeRowHandlePositionByIndex({
|
|
3233
|
+
refs,
|
|
3234
|
+
index,
|
|
3235
|
+
before,
|
|
3236
|
+
after
|
|
3237
|
+
}) {
|
|
3238
|
+
const { contentWrapperRef, rowHandleRef, hoverIndex } = refs;
|
|
3239
|
+
const rowHandle = rowHandleRef.value;
|
|
3240
|
+
if (!rowHandle) return;
|
|
3241
|
+
hoverIndex.value = index;
|
|
3242
|
+
const dom = getRelatedDOM(contentWrapperRef, index);
|
|
3243
|
+
if (!dom) return;
|
|
3244
|
+
const { row } = dom;
|
|
3245
|
+
rowHandle.dataset.show = "true";
|
|
3246
|
+
if (before) before(rowHandle);
|
|
3247
|
+
computePosition(row, rowHandle, { placement: "left" }).then(({ x, y }) => {
|
|
3248
|
+
Object.assign(rowHandle.style, {
|
|
3249
|
+
left: `${x}px`,
|
|
3250
|
+
top: `${y}px`
|
|
3251
|
+
});
|
|
3252
|
+
if (after) after(rowHandle);
|
|
3253
|
+
}).catch(console.error);
|
|
3254
|
+
}
|
|
3255
|
+
|
|
3256
|
+
function findDragOverElement(elements, pointer, axis) {
|
|
3257
|
+
const startProp = axis === "x" ? "left" : "top";
|
|
3258
|
+
const endProp = axis === "x" ? "right" : "bottom";
|
|
3259
|
+
const lastIndex = elements.length - 1;
|
|
3260
|
+
const index = elements.findIndex((el, index2) => {
|
|
3261
|
+
const rect = el.getBoundingClientRect();
|
|
3262
|
+
const boundaryStart = rect[startProp];
|
|
3263
|
+
const boundaryEnd = rect[endProp];
|
|
3264
|
+
if (boundaryStart <= pointer && pointer <= boundaryEnd) return true;
|
|
3265
|
+
if (index2 === lastIndex && pointer > boundaryEnd) return true;
|
|
3266
|
+
if (index2 === 0 && pointer < boundaryStart) return true;
|
|
3267
|
+
return false;
|
|
3268
|
+
});
|
|
3269
|
+
const element = elements[index];
|
|
3270
|
+
return element ? [element, index] : void 0;
|
|
3271
|
+
}
|
|
3272
|
+
function getDragOverColumn(table, pointerX) {
|
|
3273
|
+
const firstRow = table.querySelector("tr");
|
|
3274
|
+
if (!firstRow) return;
|
|
3275
|
+
const cells = Array.from(firstRow.children);
|
|
3276
|
+
return findDragOverElement(cells, pointerX, "x");
|
|
3277
|
+
}
|
|
3278
|
+
function getDragOverRow(table, pointerY) {
|
|
3279
|
+
const rows = Array.from(table.querySelectorAll("tr"));
|
|
3280
|
+
return findDragOverElement(rows, pointerY, "y");
|
|
3281
|
+
}
|
|
3282
|
+
|
|
3283
|
+
function createDragOverHandler(refs) {
|
|
3284
|
+
return throttle((e) => {
|
|
3285
|
+
const context = prepareDndContext(refs);
|
|
3286
|
+
if (!context) return;
|
|
3287
|
+
const { preview, content, contentRoot, xHandle, yHandle } = context;
|
|
3288
|
+
const { dragInfo, hoverIndex } = refs;
|
|
3289
|
+
if (preview.dataset.show === "false") return;
|
|
3290
|
+
const dom = getRelatedDOM(refs.contentWrapperRef, hoverIndex.value);
|
|
3291
|
+
if (!dom) return;
|
|
3292
|
+
const firstRow = contentRoot.querySelector("tr");
|
|
3293
|
+
if (!firstRow) return;
|
|
3294
|
+
const info = dragInfo.value;
|
|
3295
|
+
if (!info) return;
|
|
3296
|
+
if (!contentRoot.offsetParent) return;
|
|
3297
|
+
const wrapperOffsetTop = contentRoot.offsetParent.offsetTop;
|
|
3298
|
+
const wrapperOffsetLeft = contentRoot.offsetParent.offsetLeft;
|
|
3299
|
+
if (info.type === "col") {
|
|
3300
|
+
const width = dom.col.getBoundingClientRect().width;
|
|
3301
|
+
const { left, width: fullWidth } = contentRoot.getBoundingClientRect();
|
|
3302
|
+
const leftGap = wrapperOffsetLeft - left;
|
|
3303
|
+
const previewLeft = e.clientX + leftGap - width / 2;
|
|
3304
|
+
const [startX] = info.startCoords;
|
|
3305
|
+
const direction = startX < e.clientX ? "right" : "left";
|
|
3306
|
+
preview.style.top = `${wrapperOffsetTop}px`;
|
|
3307
|
+
const previewLeftOffset = previewLeft < left + leftGap - 20 ? left + leftGap - 20 : previewLeft > left + fullWidth + leftGap - width + 20 ? left + fullWidth + leftGap - width + 20 : previewLeft;
|
|
3308
|
+
preview.style.left = `${previewLeftOffset}px`;
|
|
3309
|
+
const dragOverColumn = getDragOverColumn(contentRoot, e.clientX);
|
|
3310
|
+
if (dragOverColumn) {
|
|
3311
|
+
const [col, index] = dragOverColumn;
|
|
3312
|
+
const yHandleWidth = yHandle.getBoundingClientRect().width;
|
|
3313
|
+
const contentBoundary = content.getBoundingClientRect();
|
|
3314
|
+
info.endIndex = index;
|
|
3315
|
+
computePosition(col, yHandle, {
|
|
3316
|
+
placement: direction === "left" ? "left" : "right",
|
|
3317
|
+
middleware: [offset(direction === "left" ? -1 * yHandleWidth : 0)]
|
|
3318
|
+
}).then(({ x }) => {
|
|
3319
|
+
yHandle.dataset.show = "true";
|
|
3320
|
+
Object.assign(yHandle.style, {
|
|
3321
|
+
height: `${contentBoundary.height}px`,
|
|
3322
|
+
left: `${x}px`,
|
|
3323
|
+
top: `${wrapperOffsetTop}px`
|
|
3324
|
+
});
|
|
3325
|
+
}).catch(console.error);
|
|
3326
|
+
}
|
|
3327
|
+
} else if (info.type === "row") {
|
|
3328
|
+
const height = dom.row.getBoundingClientRect().height;
|
|
3329
|
+
const { top, height: fullHeight } = contentRoot.getBoundingClientRect();
|
|
3330
|
+
const topGap = wrapperOffsetTop - top;
|
|
3331
|
+
const previewTop = e.clientY + topGap - height / 2;
|
|
3332
|
+
const [_, startY] = info.startCoords;
|
|
3333
|
+
const direction = startY < e.clientY ? "down" : "up";
|
|
3334
|
+
const previewTopOffset = previewTop < top + topGap - 20 ? top + topGap - 20 : previewTop > top + fullHeight + topGap - height + 20 ? top + fullHeight + topGap - height + 20 : previewTop;
|
|
3335
|
+
preview.style.top = `${previewTopOffset}px`;
|
|
3336
|
+
preview.style.left = `${wrapperOffsetLeft}px`;
|
|
3337
|
+
const dragOverRow = getDragOverRow(contentRoot, e.clientY);
|
|
3338
|
+
if (dragOverRow) {
|
|
3339
|
+
const [row, index] = dragOverRow;
|
|
3340
|
+
const xHandleHeight = xHandle.getBoundingClientRect().height;
|
|
3341
|
+
const contentBoundary = content.getBoundingClientRect();
|
|
3342
|
+
info.endIndex = index;
|
|
3343
|
+
computePosition(row, xHandle, {
|
|
3344
|
+
placement: direction === "up" ? "top" : "bottom",
|
|
3345
|
+
middleware: [offset(direction === "up" ? -1 * xHandleHeight : 0)]
|
|
3346
|
+
}).then(({ y }) => {
|
|
3347
|
+
xHandle.dataset.show = "true";
|
|
3348
|
+
Object.assign(xHandle.style, {
|
|
3349
|
+
width: `${contentBoundary.width}px`,
|
|
3350
|
+
top: `${y}px`
|
|
3351
|
+
});
|
|
3352
|
+
}).catch(console.error);
|
|
3353
|
+
}
|
|
3354
|
+
}
|
|
3355
|
+
}, 20);
|
|
3356
|
+
}
|
|
3357
|
+
|
|
3358
|
+
function useDragHandlers(refs, ctx, getPos) {
|
|
3359
|
+
const { dragPreviewRef, yLineHandleRef, xLineHandleRef, dragInfo } = refs;
|
|
3360
|
+
const dragRow = createDragRowHandler(refs, ctx);
|
|
3361
|
+
const dragCol = createDragColHandler(refs, ctx);
|
|
3362
|
+
const onDragEnd = () => {
|
|
3363
|
+
const preview = dragPreviewRef.value;
|
|
3364
|
+
if (!preview) return;
|
|
3365
|
+
if (preview.dataset.show === "false") return;
|
|
3366
|
+
const previewRoot = preview == null ? void 0 : preview.querySelector("tbody");
|
|
3367
|
+
while (previewRoot == null ? void 0 : previewRoot.firstChild)
|
|
3368
|
+
previewRoot == null ? void 0 : previewRoot.removeChild(previewRoot.firstChild);
|
|
3369
|
+
if (preview) preview.dataset.show = "false";
|
|
3370
|
+
};
|
|
3371
|
+
const onDrop = () => {
|
|
3372
|
+
var _a;
|
|
3373
|
+
const preview = dragPreviewRef.value;
|
|
3374
|
+
if (!preview) return;
|
|
3375
|
+
const yHandle = yLineHandleRef.value;
|
|
3376
|
+
if (!yHandle) return;
|
|
3377
|
+
const xHandle = xLineHandleRef.value;
|
|
3378
|
+
if (!xHandle) return;
|
|
3379
|
+
const info = dragInfo.value;
|
|
3380
|
+
if (!info) return;
|
|
3381
|
+
if (!ctx) return;
|
|
3382
|
+
if (preview.dataset.show === "false") return;
|
|
3383
|
+
const colHandle = refs.colHandleRef.value;
|
|
3384
|
+
if (!colHandle) return;
|
|
3385
|
+
const rowHandle = refs.rowHandleRef.value;
|
|
3386
|
+
if (!rowHandle) return;
|
|
3387
|
+
yHandle.dataset.show = "false";
|
|
3388
|
+
xHandle.dataset.show = "false";
|
|
3389
|
+
if (info.startIndex === info.endIndex) return;
|
|
3390
|
+
const commands = ctx.get(commandsCtx);
|
|
3391
|
+
const payload = {
|
|
3392
|
+
from: info.startIndex,
|
|
3393
|
+
to: info.endIndex,
|
|
3394
|
+
pos: ((_a = getPos == null ? void 0 : getPos()) != null ? _a : 0) + 1
|
|
3395
|
+
};
|
|
3396
|
+
if (info.type === "col") {
|
|
3397
|
+
commands.call(selectColCommand.key, {
|
|
3398
|
+
pos: payload.pos,
|
|
3399
|
+
index: info.startIndex
|
|
3400
|
+
});
|
|
3401
|
+
commands.call(moveColCommand.key, payload);
|
|
3402
|
+
const index = [0, info.endIndex];
|
|
3403
|
+
computeColHandlePositionByIndex({
|
|
3404
|
+
refs,
|
|
3405
|
+
index
|
|
3406
|
+
});
|
|
3407
|
+
} else {
|
|
3408
|
+
commands.call(selectRowCommand.key, {
|
|
3409
|
+
pos: payload.pos,
|
|
3410
|
+
index: info.startIndex
|
|
3411
|
+
});
|
|
3412
|
+
commands.call(moveRowCommand.key, payload);
|
|
3413
|
+
const index = [info.endIndex, 0];
|
|
3414
|
+
computeRowHandlePositionByIndex({
|
|
3415
|
+
refs,
|
|
3416
|
+
index
|
|
3417
|
+
});
|
|
3418
|
+
}
|
|
3419
|
+
requestAnimationFrame(() => {
|
|
3420
|
+
ctx.get(editorViewCtx).focus();
|
|
3421
|
+
});
|
|
3422
|
+
};
|
|
3423
|
+
const onDragOver = createDragOverHandler(refs);
|
|
3424
|
+
onMounted(() => {
|
|
3425
|
+
window.addEventListener("dragover", onDragOver);
|
|
3426
|
+
window.addEventListener("dragend", onDragEnd);
|
|
3427
|
+
window.addEventListener("drop", onDrop);
|
|
3428
|
+
});
|
|
3429
|
+
onUnmounted(() => {
|
|
3430
|
+
window.removeEventListener("dragover", onDragOver);
|
|
3431
|
+
window.removeEventListener("dragend", onDragEnd);
|
|
3432
|
+
window.removeEventListener("drop", onDrop);
|
|
3433
|
+
});
|
|
3434
|
+
return {
|
|
3435
|
+
dragRow,
|
|
3436
|
+
dragCol
|
|
3437
|
+
};
|
|
3438
|
+
}
|
|
3439
|
+
|
|
3440
|
+
function useOperation(refs, ctx, getPos) {
|
|
3441
|
+
const {
|
|
3442
|
+
xLineHandleRef,
|
|
3443
|
+
contentWrapperRef,
|
|
3444
|
+
colHandleRef,
|
|
3445
|
+
rowHandleRef,
|
|
3446
|
+
hoverIndex,
|
|
3447
|
+
lineHoverIndex
|
|
3448
|
+
} = refs;
|
|
3449
|
+
const onAddRow = () => {
|
|
3450
|
+
var _a, _b, _c;
|
|
3451
|
+
if (!ctx) return;
|
|
3452
|
+
const xHandle = xLineHandleRef.value;
|
|
3453
|
+
if (!xHandle) return;
|
|
3454
|
+
const [rowIndex] = lineHoverIndex.value;
|
|
3455
|
+
if (rowIndex < 0) return;
|
|
3456
|
+
if (!ctx.get(editorViewCtx).editable) return;
|
|
3457
|
+
const rows = Array.from(
|
|
3458
|
+
(_b = (_a = contentWrapperRef.value) == null ? void 0 : _a.querySelectorAll("tr")) != null ? _b : []
|
|
3459
|
+
);
|
|
3460
|
+
const commands = ctx.get(commandsCtx);
|
|
3461
|
+
const pos = ((_c = getPos == null ? void 0 : getPos()) != null ? _c : 0) + 1;
|
|
3462
|
+
if (rows.length === rowIndex) {
|
|
3463
|
+
commands.call(selectRowCommand.key, { pos, index: rowIndex - 1 });
|
|
3464
|
+
commands.call(addRowAfterCommand.key);
|
|
3465
|
+
} else {
|
|
3466
|
+
commands.call(selectRowCommand.key, { pos, index: rowIndex });
|
|
3467
|
+
commands.call(addRowBeforeCommand.key);
|
|
3468
|
+
}
|
|
3469
|
+
commands.call(selectRowCommand.key, { pos, index: rowIndex });
|
|
3470
|
+
xHandle.dataset.show = "false";
|
|
3471
|
+
};
|
|
3472
|
+
const onAddCol = () => {
|
|
3473
|
+
var _a, _b, _c, _d;
|
|
3474
|
+
if (!ctx) return;
|
|
3475
|
+
const xHandle = xLineHandleRef.value;
|
|
3476
|
+
if (!xHandle) return;
|
|
3477
|
+
const [_, colIndex] = lineHoverIndex.value;
|
|
3478
|
+
if (colIndex < 0) return;
|
|
3479
|
+
if (!ctx.get(editorViewCtx).editable) return;
|
|
3480
|
+
const cols = Array.from(
|
|
3481
|
+
(_c = (_b = (_a = contentWrapperRef.value) == null ? void 0 : _a.querySelector("tr")) == null ? void 0 : _b.children) != null ? _c : []
|
|
3482
|
+
);
|
|
3483
|
+
const commands = ctx.get(commandsCtx);
|
|
3484
|
+
const pos = ((_d = getPos == null ? void 0 : getPos()) != null ? _d : 0) + 1;
|
|
3485
|
+
if (cols.length === colIndex) {
|
|
3486
|
+
commands.call(selectColCommand.key, { pos, index: colIndex - 1 });
|
|
3487
|
+
commands.call(addColAfterCommand.key);
|
|
3488
|
+
} else {
|
|
3489
|
+
commands.call(selectColCommand.key, { pos, index: colIndex });
|
|
3490
|
+
commands.call(addColBeforeCommand.key);
|
|
3491
|
+
}
|
|
3492
|
+
commands.call(selectColCommand.key, { pos, index: colIndex });
|
|
3493
|
+
};
|
|
3494
|
+
const selectCol = () => {
|
|
3495
|
+
var _a, _b;
|
|
3496
|
+
if (!ctx) return;
|
|
3497
|
+
const [_, colIndex] = hoverIndex.value;
|
|
3498
|
+
const commands = ctx.get(commandsCtx);
|
|
3499
|
+
const pos = ((_a = getPos == null ? void 0 : getPos()) != null ? _a : 0) + 1;
|
|
3500
|
+
commands.call(selectColCommand.key, { pos, index: colIndex });
|
|
3501
|
+
const buttonGroup = (_b = colHandleRef.value) == null ? void 0 : _b.querySelector(".button-group");
|
|
3502
|
+
if (buttonGroup)
|
|
3503
|
+
buttonGroup.dataset.show = buttonGroup.dataset.show === "true" ? "false" : "true";
|
|
3504
|
+
};
|
|
3505
|
+
const selectRow = () => {
|
|
3506
|
+
var _a, _b;
|
|
3507
|
+
if (!ctx) return;
|
|
3508
|
+
const [rowIndex, _] = hoverIndex.value;
|
|
3509
|
+
const commands = ctx.get(commandsCtx);
|
|
3510
|
+
const pos = ((_a = getPos == null ? void 0 : getPos()) != null ? _a : 0) + 1;
|
|
3511
|
+
commands.call(selectRowCommand.key, { pos, index: rowIndex });
|
|
3512
|
+
const buttonGroup = (_b = rowHandleRef.value) == null ? void 0 : _b.querySelector(".button-group");
|
|
3513
|
+
if (buttonGroup && rowIndex > 0)
|
|
3514
|
+
buttonGroup.dataset.show = buttonGroup.dataset.show === "true" ? "false" : "true";
|
|
3515
|
+
};
|
|
3516
|
+
const deleteSelected = (e) => {
|
|
3517
|
+
if (!ctx) return;
|
|
3518
|
+
if (!ctx.get(editorViewCtx).editable) return;
|
|
3519
|
+
e.preventDefault();
|
|
3520
|
+
e.stopPropagation();
|
|
3521
|
+
const commands = ctx.get(commandsCtx);
|
|
3522
|
+
commands.call(deleteSelectedCellsCommand.key);
|
|
3523
|
+
requestAnimationFrame(() => {
|
|
3524
|
+
ctx.get(editorViewCtx).focus();
|
|
3525
|
+
});
|
|
3526
|
+
};
|
|
3527
|
+
const onAlign = (direction) => (e) => {
|
|
3528
|
+
if (!ctx) return;
|
|
3529
|
+
if (!ctx.get(editorViewCtx).editable) return;
|
|
3530
|
+
e.preventDefault();
|
|
3531
|
+
e.stopPropagation();
|
|
3532
|
+
const commands = ctx.get(commandsCtx);
|
|
3533
|
+
commands.call(setAlignCommand.key, direction);
|
|
3534
|
+
requestAnimationFrame(() => {
|
|
3535
|
+
ctx.get(editorViewCtx).focus();
|
|
3536
|
+
});
|
|
3537
|
+
};
|
|
3538
|
+
return {
|
|
3539
|
+
onAddRow,
|
|
3540
|
+
onAddCol,
|
|
3541
|
+
selectCol,
|
|
3542
|
+
selectRow,
|
|
3543
|
+
deleteSelected,
|
|
3544
|
+
onAlign
|
|
3545
|
+
};
|
|
3546
|
+
}
|
|
3547
|
+
|
|
3548
|
+
function createPointerMoveHandler(refs, view) {
|
|
3549
|
+
return throttle((e) => {
|
|
3550
|
+
if (!(view == null ? void 0 : view.editable)) return;
|
|
3551
|
+
const {
|
|
3552
|
+
contentWrapperRef,
|
|
3553
|
+
yLineHandleRef,
|
|
3554
|
+
xLineHandleRef,
|
|
3555
|
+
colHandleRef,
|
|
3556
|
+
rowHandleRef,
|
|
3557
|
+
hoverIndex,
|
|
3558
|
+
lineHoverIndex
|
|
3559
|
+
} = refs;
|
|
3560
|
+
const yHandle = yLineHandleRef.value;
|
|
3561
|
+
if (!yHandle) return;
|
|
3562
|
+
const xHandle = xLineHandleRef.value;
|
|
3563
|
+
if (!xHandle) return;
|
|
3564
|
+
const content = contentWrapperRef.value;
|
|
3565
|
+
if (!content) return;
|
|
3566
|
+
const rowHandle = rowHandleRef.value;
|
|
3567
|
+
if (!rowHandle) return;
|
|
3568
|
+
const colHandle = colHandleRef.value;
|
|
3569
|
+
if (!colHandle) return;
|
|
3570
|
+
const index = findPointerIndex(e, view);
|
|
3571
|
+
if (!index) return;
|
|
3572
|
+
const dom = getRelatedDOM(contentWrapperRef, index);
|
|
3573
|
+
if (!dom) return;
|
|
3574
|
+
const [rowIndex, colIndex] = index;
|
|
3575
|
+
const boundary = dom.col.getBoundingClientRect();
|
|
3576
|
+
const closeToBoundaryLeft = Math.abs(e.clientX - boundary.left) < 8;
|
|
3577
|
+
const closeToBoundaryRight = Math.abs(boundary.right - e.clientX) < 8;
|
|
3578
|
+
const closeToBoundaryTop = Math.abs(e.clientY - boundary.top) < 8;
|
|
3579
|
+
const closeToBoundaryBottom = Math.abs(boundary.bottom - e.clientY) < 8;
|
|
3580
|
+
const closeToBoundary = closeToBoundaryLeft || closeToBoundaryRight || closeToBoundaryTop || closeToBoundaryBottom;
|
|
3581
|
+
const rowButtonGroup = rowHandle.querySelector(".button-group");
|
|
3582
|
+
const colButtonGroup = colHandle.querySelector(".button-group");
|
|
3583
|
+
if (rowButtonGroup) rowButtonGroup.dataset.show = "false";
|
|
3584
|
+
if (colButtonGroup) colButtonGroup.dataset.show = "false";
|
|
3585
|
+
if (closeToBoundary) {
|
|
3586
|
+
const contentBoundary = content.getBoundingClientRect();
|
|
3587
|
+
rowHandle.dataset.show = "false";
|
|
3588
|
+
colHandle.dataset.show = "false";
|
|
3589
|
+
xHandle.dataset.displayType = "tool";
|
|
3590
|
+
yHandle.dataset.displayType = "tool";
|
|
3591
|
+
const yHandleWidth = yHandle.getBoundingClientRect().width;
|
|
3592
|
+
const xHandleHeight = xHandle.getBoundingClientRect().height;
|
|
3593
|
+
if (closeToBoundaryLeft || closeToBoundaryRight) {
|
|
3594
|
+
lineHoverIndex.value[1] = closeToBoundaryLeft ? colIndex : colIndex + 1;
|
|
3595
|
+
computePosition(dom.col, yHandle, {
|
|
3596
|
+
placement: closeToBoundaryLeft ? "left" : "right",
|
|
3597
|
+
middleware: [offset(closeToBoundaryLeft ? -1 * yHandleWidth : 0)]
|
|
3598
|
+
}).then(({ x }) => {
|
|
3599
|
+
yHandle.dataset.show = "true";
|
|
3600
|
+
Object.assign(yHandle.style, {
|
|
3601
|
+
height: `${contentBoundary.height}px`,
|
|
3602
|
+
left: `${x}px`
|
|
3603
|
+
});
|
|
3604
|
+
}).catch(console.error);
|
|
3605
|
+
} else {
|
|
3606
|
+
yHandle.dataset.show = "false";
|
|
3607
|
+
}
|
|
3608
|
+
if (index[0] !== 0 && (closeToBoundaryTop || closeToBoundaryBottom)) {
|
|
3609
|
+
lineHoverIndex.value[0] = closeToBoundaryTop ? rowIndex : rowIndex + 1;
|
|
3610
|
+
computePosition(dom.row, xHandle, {
|
|
3611
|
+
placement: closeToBoundaryTop ? "top" : "bottom",
|
|
3612
|
+
middleware: [offset(closeToBoundaryTop ? -1 * xHandleHeight : 0)]
|
|
3613
|
+
}).then(({ y }) => {
|
|
3614
|
+
xHandle.dataset.show = "true";
|
|
3615
|
+
Object.assign(xHandle.style, {
|
|
3616
|
+
width: `${contentBoundary.width}px`,
|
|
3617
|
+
top: `${y}px`
|
|
3618
|
+
});
|
|
3619
|
+
}).catch(console.error);
|
|
3620
|
+
} else {
|
|
3621
|
+
xHandle.dataset.show = "false";
|
|
3622
|
+
}
|
|
3623
|
+
return;
|
|
3624
|
+
}
|
|
3625
|
+
lineHoverIndex.value = [-1, -1];
|
|
3626
|
+
yHandle.dataset.show = "false";
|
|
3627
|
+
xHandle.dataset.show = "false";
|
|
3628
|
+
rowHandle.dataset.show = "true";
|
|
3629
|
+
colHandle.dataset.show = "true";
|
|
3630
|
+
computeRowHandlePositionByIndex({
|
|
3631
|
+
refs,
|
|
3632
|
+
index
|
|
3633
|
+
});
|
|
3634
|
+
computeColHandlePositionByIndex({
|
|
3635
|
+
refs,
|
|
3636
|
+
index
|
|
3637
|
+
});
|
|
3638
|
+
hoverIndex.value = index;
|
|
3639
|
+
}, 20);
|
|
3640
|
+
}
|
|
3641
|
+
function createPointerLeaveHandler(refs) {
|
|
3642
|
+
return () => {
|
|
3643
|
+
const { rowHandleRef, colHandleRef, yLineHandleRef, xLineHandleRef } = refs;
|
|
3644
|
+
setTimeout(() => {
|
|
3645
|
+
const rowHandle = rowHandleRef.value;
|
|
3646
|
+
if (!rowHandle) return;
|
|
3647
|
+
const colHandle = colHandleRef.value;
|
|
3648
|
+
if (!colHandle) return;
|
|
3649
|
+
const yHandle = yLineHandleRef.value;
|
|
3650
|
+
if (!yHandle) return;
|
|
3651
|
+
const xHandle = xLineHandleRef.value;
|
|
3652
|
+
if (!xHandle) return;
|
|
3653
|
+
rowHandle.dataset.show = "false";
|
|
3654
|
+
colHandle.dataset.show = "false";
|
|
3655
|
+
yHandle.dataset.show = "false";
|
|
3656
|
+
xHandle.dataset.show = "false";
|
|
3657
|
+
}, 200);
|
|
3658
|
+
};
|
|
3659
|
+
}
|
|
3660
|
+
function usePointerHandlers(refs, view) {
|
|
3661
|
+
const pointerMove = createPointerMoveHandler(refs, view);
|
|
3662
|
+
const pointerLeave = createPointerLeaveHandler(refs);
|
|
3663
|
+
return {
|
|
3664
|
+
pointerMove,
|
|
3665
|
+
pointerLeave
|
|
3666
|
+
};
|
|
3667
|
+
}
|
|
3668
|
+
|
|
3669
|
+
keepAlive(h);
|
|
3670
|
+
const TableBlock = defineComponent({
|
|
3671
|
+
props: {
|
|
3672
|
+
view: {
|
|
3673
|
+
type: Object,
|
|
3674
|
+
required: true
|
|
3675
|
+
},
|
|
3676
|
+
ctx: {
|
|
3677
|
+
type: Object,
|
|
3678
|
+
required: true
|
|
3679
|
+
},
|
|
3680
|
+
getPos: {
|
|
3681
|
+
type: Function,
|
|
3682
|
+
required: true
|
|
3683
|
+
},
|
|
3684
|
+
config: {
|
|
3685
|
+
type: Object,
|
|
3686
|
+
required: true
|
|
3687
|
+
},
|
|
3688
|
+
onMount: {
|
|
3689
|
+
type: Function,
|
|
3690
|
+
required: true
|
|
3691
|
+
},
|
|
3692
|
+
node: {
|
|
3693
|
+
type: Object,
|
|
3694
|
+
required: true
|
|
3695
|
+
}
|
|
3696
|
+
},
|
|
3697
|
+
setup({ view, node, ctx, getPos, config, onMount }) {
|
|
3698
|
+
const contentWrapperRef = ref();
|
|
3699
|
+
const contentWrapperFunctionRef = (div) => {
|
|
3700
|
+
if (div == null) return;
|
|
3701
|
+
if (div instanceof HTMLElement) {
|
|
3702
|
+
contentWrapperRef.value = div;
|
|
3703
|
+
onMount(div);
|
|
3704
|
+
} else {
|
|
3705
|
+
contentWrapperRef.value = void 0;
|
|
3706
|
+
}
|
|
3707
|
+
};
|
|
3708
|
+
const colHandleRef = ref();
|
|
3709
|
+
const rowHandleRef = ref();
|
|
3710
|
+
const xLineHandleRef = ref();
|
|
3711
|
+
const yLineHandleRef = ref();
|
|
3712
|
+
const tableWrapperRef = ref();
|
|
3713
|
+
const dragPreviewRef = ref();
|
|
3714
|
+
const hoverIndex = ref([0, 0]);
|
|
3715
|
+
const lineHoverIndex = ref([-1, -1]);
|
|
3716
|
+
const dragInfo = ref();
|
|
3717
|
+
const refs = {
|
|
3718
|
+
dragPreviewRef,
|
|
3719
|
+
tableWrapperRef,
|
|
3720
|
+
contentWrapperRef,
|
|
3721
|
+
yLineHandleRef,
|
|
3722
|
+
xLineHandleRef,
|
|
3723
|
+
colHandleRef,
|
|
3724
|
+
rowHandleRef,
|
|
3725
|
+
hoverIndex,
|
|
3726
|
+
lineHoverIndex,
|
|
3727
|
+
dragInfo
|
|
3728
|
+
};
|
|
3729
|
+
const { pointerLeave, pointerMove } = usePointerHandlers(refs, view);
|
|
3730
|
+
const { dragRow, dragCol } = useDragHandlers(refs, ctx, getPos);
|
|
3731
|
+
const {
|
|
3732
|
+
onAddRow,
|
|
3733
|
+
onAddCol,
|
|
3734
|
+
selectCol,
|
|
3735
|
+
selectRow,
|
|
3736
|
+
deleteSelected,
|
|
3737
|
+
onAlign
|
|
3738
|
+
} = useOperation(refs, ctx, getPos);
|
|
3739
|
+
onMounted(() => {
|
|
3740
|
+
requestAnimationFrame(() => {
|
|
3741
|
+
if (view.editable) recoveryStateBetweenUpdate(refs, view, node.value);
|
|
3742
|
+
});
|
|
3743
|
+
});
|
|
3744
|
+
return () => {
|
|
3745
|
+
return /* @__PURE__ */ h(
|
|
3746
|
+
"div",
|
|
3747
|
+
{
|
|
3748
|
+
onDragstart: (e) => e.preventDefault(),
|
|
3749
|
+
onDragover: (e) => e.preventDefault(),
|
|
3750
|
+
onDragleave: (e) => e.preventDefault(),
|
|
3751
|
+
onPointermove: pointerMove,
|
|
3752
|
+
onPointerleave: pointerLeave
|
|
3753
|
+
},
|
|
3754
|
+
/* @__PURE__ */ h(
|
|
3755
|
+
"div",
|
|
3756
|
+
{
|
|
3757
|
+
"data-show": "false",
|
|
3758
|
+
contenteditable: "false",
|
|
3759
|
+
draggable: "true",
|
|
3760
|
+
"data-role": "col-drag-handle",
|
|
3761
|
+
class: "handle cell-handle",
|
|
3762
|
+
onDragstart: dragCol,
|
|
3763
|
+
onClick: selectCol,
|
|
3764
|
+
onPointerdown: (e) => e.stopPropagation(),
|
|
3765
|
+
onPointermove: (e) => e.stopPropagation(),
|
|
3766
|
+
ref: colHandleRef
|
|
3767
|
+
},
|
|
3768
|
+
/* @__PURE__ */ h(Icon, { icon: config.renderButton("col_drag_handle") }),
|
|
3769
|
+
/* @__PURE__ */ h(
|
|
3770
|
+
"div",
|
|
3771
|
+
{
|
|
3772
|
+
"data-show": "false",
|
|
3773
|
+
class: "button-group",
|
|
3774
|
+
onPointermove: (e) => e.stopPropagation()
|
|
3775
|
+
},
|
|
3776
|
+
/* @__PURE__ */ h("button", { type: "button", onPointerdown: onAlign("left") }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_left") })),
|
|
3777
|
+
/* @__PURE__ */ h("button", { type: "button", onPointerdown: onAlign("center") }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_center") })),
|
|
3778
|
+
/* @__PURE__ */ h("button", { type: "button", onPointerdown: onAlign("right") }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_right") })),
|
|
3779
|
+
/* @__PURE__ */ h("button", { type: "button", onPointerdown: deleteSelected }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_col") }))
|
|
3780
|
+
)
|
|
3781
|
+
),
|
|
3782
|
+
/* @__PURE__ */ h(
|
|
3783
|
+
"div",
|
|
3784
|
+
{
|
|
3785
|
+
"data-show": "false",
|
|
3786
|
+
contenteditable: "false",
|
|
3787
|
+
draggable: "true",
|
|
3788
|
+
"data-role": "row-drag-handle",
|
|
3789
|
+
class: "handle cell-handle",
|
|
3790
|
+
onDragstart: dragRow,
|
|
3791
|
+
onClick: selectRow,
|
|
3792
|
+
onPointerdown: (e) => e.stopPropagation(),
|
|
3793
|
+
onPointermove: (e) => e.stopPropagation(),
|
|
3794
|
+
ref: rowHandleRef
|
|
3795
|
+
},
|
|
3796
|
+
/* @__PURE__ */ h(Icon, { icon: config.renderButton("row_drag_handle") }),
|
|
3797
|
+
/* @__PURE__ */ h(
|
|
3798
|
+
"div",
|
|
3799
|
+
{
|
|
3800
|
+
"data-show": "false",
|
|
3801
|
+
class: "button-group",
|
|
3802
|
+
onPointermove: (e) => e.stopPropagation()
|
|
3803
|
+
},
|
|
3804
|
+
/* @__PURE__ */ h("button", { type: "button", onPointerdown: deleteSelected }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_row") }))
|
|
3805
|
+
)
|
|
3806
|
+
),
|
|
3807
|
+
/* @__PURE__ */ h("div", { class: "table-wrapper", ref: tableWrapperRef }, /* @__PURE__ */ h(
|
|
3808
|
+
"div",
|
|
3809
|
+
{
|
|
3810
|
+
"data-show": "false",
|
|
3811
|
+
class: "drag-preview",
|
|
3812
|
+
"data-direction": "vertical",
|
|
3813
|
+
ref: dragPreviewRef
|
|
3814
|
+
},
|
|
3815
|
+
/* @__PURE__ */ h("table", null, /* @__PURE__ */ h("tbody", null))
|
|
3816
|
+
), /* @__PURE__ */ h(
|
|
3817
|
+
"div",
|
|
3818
|
+
{
|
|
3819
|
+
"data-show": "false",
|
|
3820
|
+
contenteditable: "false",
|
|
3821
|
+
"data-display-type": "tool",
|
|
3822
|
+
"data-role": "x-line-drag-handle",
|
|
3823
|
+
class: "handle line-handle",
|
|
3824
|
+
ref: xLineHandleRef
|
|
3825
|
+
},
|
|
3826
|
+
/* @__PURE__ */ h("button", { type: "button", onClick: onAddRow, class: "add-button" }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("add_row") }))
|
|
3827
|
+
), /* @__PURE__ */ h(
|
|
3828
|
+
"div",
|
|
3829
|
+
{
|
|
3830
|
+
"data-show": "false",
|
|
3831
|
+
contenteditable: "false",
|
|
3832
|
+
"data-display-type": "tool",
|
|
3833
|
+
"data-role": "y-line-drag-handle",
|
|
3834
|
+
class: "handle line-handle",
|
|
3835
|
+
ref: yLineHandleRef
|
|
3836
|
+
},
|
|
3837
|
+
/* @__PURE__ */ h("button", { type: "button", onClick: onAddCol, class: "add-button" }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("add_col") }))
|
|
3838
|
+
), /* @__PURE__ */ h("table", { ref: contentWrapperFunctionRef, class: "children" }))
|
|
3839
|
+
);
|
|
3840
|
+
};
|
|
3841
|
+
}
|
|
3842
|
+
});
|
|
3843
|
+
|
|
3844
|
+
var __typeError = (msg) => {
|
|
3845
|
+
throw TypeError(msg);
|
|
3846
|
+
};
|
|
3847
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
3848
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
3849
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
3850
|
+
var _TableNodeView_instances, handleClick_fn;
|
|
3851
|
+
class TableNodeView {
|
|
3852
|
+
constructor(ctx, node, view, getPos) {
|
|
3853
|
+
this.ctx = ctx;
|
|
3854
|
+
this.node = node;
|
|
3855
|
+
this.view = view;
|
|
3856
|
+
this.getPos = getPos;
|
|
3857
|
+
__privateAdd(this, _TableNodeView_instances);
|
|
3858
|
+
const dom = document.createElement("div");
|
|
3859
|
+
dom.className = "milkdown-table-block";
|
|
3860
|
+
const contentDOM = document.createElement("tbody");
|
|
3861
|
+
this.contentDOM = contentDOM;
|
|
3862
|
+
contentDOM.setAttribute("data-content-dom", "true");
|
|
3863
|
+
contentDOM.classList.add("content-dom");
|
|
3864
|
+
this.nodeRef = shallowRef(node);
|
|
3865
|
+
const app = createApp(TableBlock, {
|
|
3866
|
+
view,
|
|
3867
|
+
ctx,
|
|
3868
|
+
getPos,
|
|
3869
|
+
config: ctx.get(tableBlockConfig.key),
|
|
3870
|
+
onMount: (div) => {
|
|
3871
|
+
div.appendChild(contentDOM);
|
|
3872
|
+
},
|
|
3873
|
+
node: this.nodeRef
|
|
3874
|
+
});
|
|
3875
|
+
app.mount(dom);
|
|
3876
|
+
this.app = app;
|
|
3877
|
+
this.dom = dom;
|
|
3878
|
+
}
|
|
3879
|
+
update(node) {
|
|
3880
|
+
if (node.type !== this.node.type) return false;
|
|
3881
|
+
if (node.sameMarkup(this.node) && node.content.eq(this.node.content))
|
|
3882
|
+
return false;
|
|
3883
|
+
this.node = node;
|
|
3884
|
+
this.nodeRef.value = node;
|
|
3885
|
+
return true;
|
|
3886
|
+
}
|
|
3887
|
+
stopEvent(e) {
|
|
3888
|
+
if (e.type === "drop" || e.type.startsWith("drag")) return true;
|
|
3889
|
+
if (e.type === "mousedown" || e.type === "pointerdown") {
|
|
3890
|
+
if (e.target instanceof Element && e.target.closest("button")) return true;
|
|
3891
|
+
const target = e.target;
|
|
3892
|
+
if (target instanceof HTMLElement && (target.closest("th") || target.closest("td"))) {
|
|
3893
|
+
const event = e;
|
|
3894
|
+
return __privateMethod(this, _TableNodeView_instances, handleClick_fn).call(this, event);
|
|
3895
|
+
}
|
|
3896
|
+
}
|
|
3897
|
+
return false;
|
|
3898
|
+
}
|
|
3899
|
+
ignoreMutation(mutation) {
|
|
3900
|
+
if (!this.dom || !this.contentDOM) return true;
|
|
3901
|
+
if (mutation.type === "selection") return false;
|
|
3902
|
+
if (this.contentDOM === mutation.target && mutation.type === "attributes")
|
|
3903
|
+
return true;
|
|
3904
|
+
if (this.contentDOM.contains(mutation.target)) return false;
|
|
3905
|
+
return true;
|
|
3906
|
+
}
|
|
3907
|
+
destroy() {
|
|
3908
|
+
this.app.unmount();
|
|
3909
|
+
this.dom.remove();
|
|
3910
|
+
this.contentDOM.remove();
|
|
3911
|
+
}
|
|
3912
|
+
}
|
|
3913
|
+
_TableNodeView_instances = new WeakSet();
|
|
3914
|
+
handleClick_fn = function(event) {
|
|
3915
|
+
const view = this.view;
|
|
3916
|
+
if (!view.editable) return false;
|
|
3917
|
+
const { state, dispatch } = view;
|
|
3918
|
+
const pos = view.posAtCoords({ left: event.clientX, top: event.clientY });
|
|
3919
|
+
if (!pos) return false;
|
|
3920
|
+
const $pos = state.doc.resolve(pos.inside);
|
|
3921
|
+
const node = findParent(
|
|
3922
|
+
(node2) => node2.type.name === "table_cell" || node2.type.name === "table_header"
|
|
3923
|
+
)($pos);
|
|
3924
|
+
if (!node) return false;
|
|
3925
|
+
if (state.selection instanceof TextSelection) {
|
|
3926
|
+
const currentNode = findParent(
|
|
3927
|
+
(node2) => node2.type.name === "table_cell" || node2.type.name === "table_header"
|
|
3928
|
+
)(state.selection.$from);
|
|
3929
|
+
if ((currentNode == null ? void 0 : currentNode.node) === node.node) return false;
|
|
3930
|
+
}
|
|
3931
|
+
const { from } = node;
|
|
3932
|
+
const selection = NodeSelection.create(state.doc, from + 1);
|
|
3933
|
+
if (state.selection.eq(selection)) return false;
|
|
3934
|
+
if (state.selection instanceof CellSelection) {
|
|
3935
|
+
setTimeout(() => {
|
|
3936
|
+
dispatch(state.tr.setSelection(selection).scrollIntoView());
|
|
3937
|
+
}, 20);
|
|
3938
|
+
} else {
|
|
3939
|
+
requestAnimationFrame(() => {
|
|
3940
|
+
dispatch(state.tr.setSelection(selection).scrollIntoView());
|
|
3941
|
+
});
|
|
3942
|
+
}
|
|
3943
|
+
return true;
|
|
3944
|
+
};
|
|
3945
|
+
const tableBlockView = $view(
|
|
3946
|
+
tableSchema.node,
|
|
3947
|
+
(ctx) => {
|
|
3948
|
+
return (initialNode, view, getPos) => {
|
|
3949
|
+
return new TableNodeView(ctx, initialNode, view, getPos);
|
|
3950
|
+
};
|
|
3951
|
+
}
|
|
3952
|
+
);
|
|
3953
|
+
withMeta(tableBlockView, {
|
|
3954
|
+
displayName: "NodeView<table-block>",
|
|
3955
|
+
group: "TableBlock"
|
|
3956
|
+
});
|
|
3957
|
+
|
|
3958
|
+
const tableBlock = [tableBlockConfig, tableBlockView];
|
|
3959
|
+
|
|
3960
|
+
export { TableNodeView, tableBlock, tableBlockConfig, tableBlockView };
|
|
3961
|
+
//# sourceMappingURL=index.js.map
|