@tiptap/extension-node-range 2.24.2 → 3.0.0-beta.10
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.md +21 -0
- package/dist/index.cjs +315 -297
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -0
- package/dist/index.d.ts +47 -8
- package/dist/index.js +286 -290
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
- package/src/helpers/NodeRangeBookmark.ts +2 -4
- package/src/helpers/NodeRangeSelection.ts +9 -25
- package/src/helpers/getNodeRangeDecorations.ts +1 -1
- package/src/helpers/getSelectionRanges.ts +11 -15
- package/src/node-range.ts +28 -25
- package/dist/helpers/NodeRangeBookmark.d.ts +0 -11
- package/dist/helpers/NodeRangeBookmark.d.ts.map +0 -1
- package/dist/helpers/NodeRangeSelection.d.ts +0 -24
- package/dist/helpers/NodeRangeSelection.d.ts.map +0 -1
- package/dist/helpers/getNodeRangeDecorations.d.ts +0 -4
- package/dist/helpers/getNodeRangeDecorations.d.ts.map +0 -1
- package/dist/helpers/getSelectionRanges.d.ts +0 -4
- package/dist/helpers/getSelectionRanges.d.ts.map +0 -1
- package/dist/helpers/isNodeRangeSelection.d.ts +0 -3
- package/dist/helpers/isNodeRangeSelection.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.umd.js +0 -320
- package/dist/index.umd.js.map +0 -1
- package/dist/node-range.d.ts +0 -7
- package/dist/node-range.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,310 +1,306 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { NodeRange as NodeRange$1 } from '@tiptap/pm/model';
|
|
1
|
+
// src/node-range.ts
|
|
2
|
+
import { Extension } from "@tiptap/core";
|
|
3
|
+
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
5
4
|
|
|
5
|
+
// src/helpers/getNodeRangeDecorations.ts
|
|
6
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view";
|
|
6
7
|
function getNodeRangeDecorations(ranges) {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
if (!ranges.length) {
|
|
9
|
+
return DecorationSet.empty;
|
|
10
|
+
}
|
|
11
|
+
const decorations = [];
|
|
12
|
+
const doc = ranges[0].$from.node(0);
|
|
13
|
+
ranges.forEach((range) => {
|
|
14
|
+
const pos = range.$from.pos;
|
|
15
|
+
const node = range.$from.nodeAfter;
|
|
16
|
+
if (!node) {
|
|
17
|
+
return;
|
|
9
18
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
decorations.push(Decoration.node(pos, pos + node.nodeSize, {
|
|
19
|
-
class: 'ProseMirror-selectednoderange',
|
|
20
|
-
}));
|
|
21
|
-
});
|
|
22
|
-
return DecorationSet.create(doc, decorations);
|
|
19
|
+
decorations.push(
|
|
20
|
+
Decoration.node(pos, pos + node.nodeSize, {
|
|
21
|
+
class: "ProseMirror-selectednoderange"
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
return DecorationSet.create(doc, decorations);
|
|
23
26
|
}
|
|
24
27
|
|
|
28
|
+
// src/helpers/getSelectionRanges.ts
|
|
29
|
+
import { NodeRange } from "@tiptap/pm/model";
|
|
30
|
+
import { SelectionRange } from "@tiptap/pm/state";
|
|
25
31
|
function getSelectionRanges($from, $to, depth) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (from < nodeRange.start || from >= nodeRange.end) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const selectionRange = new SelectionRange(doc.resolve(from), doc.resolve(to));
|
|
45
|
-
ranges.push(selectionRange);
|
|
46
|
-
});
|
|
47
|
-
return ranges;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
class NodeRangeBookmark {
|
|
51
|
-
constructor(anchor, head) {
|
|
52
|
-
this.anchor = anchor;
|
|
53
|
-
this.head = head;
|
|
54
|
-
}
|
|
55
|
-
map(mapping) {
|
|
56
|
-
return new NodeRangeBookmark(mapping.map(this.anchor), mapping.map(this.head));
|
|
57
|
-
}
|
|
58
|
-
resolve(doc) {
|
|
59
|
-
const $anchor = doc.resolve(this.anchor);
|
|
60
|
-
const $head = doc.resolve(this.head);
|
|
61
|
-
return new NodeRangeSelection($anchor, $head);
|
|
32
|
+
const ranges = [];
|
|
33
|
+
const doc = $from.node(0);
|
|
34
|
+
if (typeof depth === "number" && depth >= 0) {
|
|
35
|
+
} else if ($from.sameParent($to)) {
|
|
36
|
+
depth = Math.max(0, $from.sharedDepth($to.pos) - 1);
|
|
37
|
+
} else {
|
|
38
|
+
depth = $from.sharedDepth($to.pos);
|
|
39
|
+
}
|
|
40
|
+
const nodeRange = new NodeRange($from, $to, depth);
|
|
41
|
+
const offset = nodeRange.depth === 0 ? 0 : doc.resolve(nodeRange.start).posAtIndex(0);
|
|
42
|
+
nodeRange.parent.forEach((node, pos) => {
|
|
43
|
+
const from = offset + pos;
|
|
44
|
+
const to = from + node.nodeSize;
|
|
45
|
+
if (from < nodeRange.start || from >= nodeRange.end) {
|
|
46
|
+
return;
|
|
62
47
|
}
|
|
48
|
+
const selectionRange = new SelectionRange(doc.resolve(from), doc.resolve(to));
|
|
49
|
+
ranges.push(selectionRange);
|
|
50
|
+
});
|
|
51
|
+
return ranges;
|
|
63
52
|
}
|
|
64
53
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
extendForwards() {
|
|
133
|
-
const { doc } = this.$from;
|
|
134
|
-
if (this.isBackwards && this.ranges.length > 1) {
|
|
135
|
-
const ranges = this.ranges.slice(1);
|
|
136
|
-
const $from = ranges[0].$from;
|
|
137
|
-
const $to = ranges[ranges.length - 1].$to;
|
|
138
|
-
return new NodeRangeSelection($to, $from, this.depth);
|
|
139
|
-
}
|
|
140
|
-
const lastRange = this.ranges[this.ranges.length - 1];
|
|
141
|
-
const $to = doc.resolve(Math.min(doc.content.size, lastRange.$to.pos + 1));
|
|
142
|
-
return new NodeRangeSelection(this.$anchor, $to, this.depth);
|
|
143
|
-
}
|
|
144
|
-
static fromJSON(doc, json) {
|
|
145
|
-
return new NodeRangeSelection(doc.resolve(json.anchor), doc.resolve(json.head));
|
|
146
|
-
}
|
|
147
|
-
static create(doc, anchor, head, depth, bias = 1) {
|
|
148
|
-
return new this(doc.resolve(anchor), doc.resolve(head), depth, bias);
|
|
54
|
+
// src/helpers/NodeRangeSelection.ts
|
|
55
|
+
import { Selection } from "@tiptap/pm/state";
|
|
56
|
+
|
|
57
|
+
// src/helpers/NodeRangeBookmark.ts
|
|
58
|
+
var NodeRangeBookmark = class _NodeRangeBookmark {
|
|
59
|
+
constructor(anchor, head) {
|
|
60
|
+
this.anchor = anchor;
|
|
61
|
+
this.head = head;
|
|
62
|
+
}
|
|
63
|
+
map(mapping) {
|
|
64
|
+
return new _NodeRangeBookmark(mapping.map(this.anchor), mapping.map(this.head));
|
|
65
|
+
}
|
|
66
|
+
resolve(doc) {
|
|
67
|
+
const $anchor = doc.resolve(this.anchor);
|
|
68
|
+
const $head = doc.resolve(this.head);
|
|
69
|
+
return new NodeRangeSelection($anchor, $head);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/helpers/NodeRangeSelection.ts
|
|
74
|
+
var NodeRangeSelection = class _NodeRangeSelection extends Selection {
|
|
75
|
+
constructor($anchor, $head, depth, bias = 1) {
|
|
76
|
+
const { doc } = $anchor;
|
|
77
|
+
const isCursor = $anchor === $head;
|
|
78
|
+
const isCursorAtEnd = $anchor.pos === doc.content.size && $head.pos === doc.content.size;
|
|
79
|
+
const $correctedHead = isCursor && !isCursorAtEnd ? doc.resolve($head.pos + (bias > 0 ? 1 : -1)) : $head;
|
|
80
|
+
const $correctedAnchor = isCursor && isCursorAtEnd ? doc.resolve($anchor.pos - (bias > 0 ? 1 : -1)) : $anchor;
|
|
81
|
+
const ranges = getSelectionRanges($correctedAnchor.min($correctedHead), $correctedAnchor.max($correctedHead), depth);
|
|
82
|
+
const $rangeFrom = $correctedHead.pos >= $anchor.pos ? ranges[0].$from : ranges[ranges.length - 1].$to;
|
|
83
|
+
const $rangeTo = $correctedHead.pos >= $anchor.pos ? ranges[ranges.length - 1].$to : ranges[0].$from;
|
|
84
|
+
super($rangeFrom, $rangeTo, ranges);
|
|
85
|
+
this.depth = depth;
|
|
86
|
+
}
|
|
87
|
+
// we can safely ignore this TypeScript error: https://github.com/Microsoft/TypeScript/issues/338
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
get $to() {
|
|
90
|
+
return this.ranges[this.ranges.length - 1].$to;
|
|
91
|
+
}
|
|
92
|
+
eq(other) {
|
|
93
|
+
return other instanceof _NodeRangeSelection && other.$from.pos === this.$from.pos && other.$to.pos === this.$to.pos;
|
|
94
|
+
}
|
|
95
|
+
map(doc, mapping) {
|
|
96
|
+
const $anchor = doc.resolve(mapping.map(this.anchor));
|
|
97
|
+
const $head = doc.resolve(mapping.map(this.head));
|
|
98
|
+
return new _NodeRangeSelection($anchor, $head);
|
|
99
|
+
}
|
|
100
|
+
toJSON() {
|
|
101
|
+
return {
|
|
102
|
+
type: "nodeRange",
|
|
103
|
+
anchor: this.anchor,
|
|
104
|
+
head: this.head
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
get isForwards() {
|
|
108
|
+
return this.head >= this.anchor;
|
|
109
|
+
}
|
|
110
|
+
get isBackwards() {
|
|
111
|
+
return !this.isForwards;
|
|
112
|
+
}
|
|
113
|
+
extendBackwards() {
|
|
114
|
+
const { doc } = this.$from;
|
|
115
|
+
if (this.isForwards && this.ranges.length > 1) {
|
|
116
|
+
const ranges = this.ranges.slice(0, -1);
|
|
117
|
+
const $from2 = ranges[0].$from;
|
|
118
|
+
const $to = ranges[ranges.length - 1].$to;
|
|
119
|
+
return new _NodeRangeSelection($from2, $to, this.depth);
|
|
149
120
|
}
|
|
150
|
-
|
|
151
|
-
|
|
121
|
+
const firstRange = this.ranges[0];
|
|
122
|
+
const $from = doc.resolve(Math.max(0, firstRange.$from.pos - 1));
|
|
123
|
+
return new _NodeRangeSelection(this.$anchor, $from, this.depth);
|
|
124
|
+
}
|
|
125
|
+
extendForwards() {
|
|
126
|
+
const { doc } = this.$from;
|
|
127
|
+
if (this.isBackwards && this.ranges.length > 1) {
|
|
128
|
+
const ranges = this.ranges.slice(1);
|
|
129
|
+
const $from = ranges[0].$from;
|
|
130
|
+
const $to2 = ranges[ranges.length - 1].$to;
|
|
131
|
+
return new _NodeRangeSelection($to2, $from, this.depth);
|
|
152
132
|
}
|
|
153
|
-
|
|
133
|
+
const lastRange = this.ranges[this.ranges.length - 1];
|
|
134
|
+
const $to = doc.resolve(Math.min(doc.content.size, lastRange.$to.pos + 1));
|
|
135
|
+
return new _NodeRangeSelection(this.$anchor, $to, this.depth);
|
|
136
|
+
}
|
|
137
|
+
static fromJSON(doc, json) {
|
|
138
|
+
return new _NodeRangeSelection(doc.resolve(json.anchor), doc.resolve(json.head));
|
|
139
|
+
}
|
|
140
|
+
static create(doc, anchor, head, depth, bias = 1) {
|
|
141
|
+
return new this(doc.resolve(anchor), doc.resolve(head), depth, bias);
|
|
142
|
+
}
|
|
143
|
+
getBookmark() {
|
|
144
|
+
return new NodeRangeBookmark(this.anchor, this.head);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
154
147
|
NodeRangeSelection.prototype.visible = false;
|
|
155
148
|
|
|
149
|
+
// src/helpers/isNodeRangeSelection.ts
|
|
156
150
|
function isNodeRangeSelection(value) {
|
|
157
|
-
|
|
151
|
+
return value instanceof NodeRangeSelection;
|
|
158
152
|
}
|
|
159
153
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const nodeRangeSelection = selection.extendBackwards();
|
|
183
|
-
tr.setSelection(nodeRangeSelection);
|
|
184
|
-
view.dispatch(tr);
|
|
185
|
-
return true;
|
|
186
|
-
},
|
|
187
|
-
// extend NodeRangeSelection downwards
|
|
188
|
-
'Shift-ArrowDown': ({ editor }) => {
|
|
189
|
-
const { depth } = this.options;
|
|
190
|
-
const { view, state } = editor;
|
|
191
|
-
const { doc, selection, tr } = state;
|
|
192
|
-
const { anchor, head } = selection;
|
|
193
|
-
if (!isNodeRangeSelection(selection)) {
|
|
194
|
-
const nodeRangeSelection = NodeRangeSelection.create(doc, anchor, head, depth);
|
|
195
|
-
tr.setSelection(nodeRangeSelection);
|
|
196
|
-
view.dispatch(tr);
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
|
-
const nodeRangeSelection = selection.extendForwards();
|
|
200
|
-
tr.setSelection(nodeRangeSelection);
|
|
201
|
-
view.dispatch(tr);
|
|
202
|
-
return true;
|
|
203
|
-
},
|
|
204
|
-
// add `NodeRangeSelection` to all nodes
|
|
205
|
-
'Mod-a': ({ editor }) => {
|
|
206
|
-
const { depth } = this.options;
|
|
207
|
-
const { view, state } = editor;
|
|
208
|
-
const { doc, tr } = state;
|
|
209
|
-
const nodeRangeSelection = NodeRangeSelection.create(doc, 0, doc.content.size, depth);
|
|
210
|
-
tr.setSelection(nodeRangeSelection);
|
|
211
|
-
view.dispatch(tr);
|
|
212
|
-
return true;
|
|
213
|
-
},
|
|
214
|
-
};
|
|
215
|
-
},
|
|
216
|
-
onSelectionUpdate() {
|
|
217
|
-
const { selection } = this.editor.state;
|
|
218
|
-
if (isNodeRangeSelection(selection)) {
|
|
219
|
-
this.editor.view.dom.classList.add('ProseMirror-noderangeselection');
|
|
154
|
+
// src/node-range.ts
|
|
155
|
+
var NodeRange2 = Extension.create({
|
|
156
|
+
name: "nodeRange",
|
|
157
|
+
addOptions() {
|
|
158
|
+
return {
|
|
159
|
+
depth: void 0,
|
|
160
|
+
key: "Mod"
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
addKeyboardShortcuts() {
|
|
164
|
+
return {
|
|
165
|
+
// extend NodeRangeSelection upwards
|
|
166
|
+
"Shift-ArrowUp": ({ editor }) => {
|
|
167
|
+
const { depth } = this.options;
|
|
168
|
+
const { view, state } = editor;
|
|
169
|
+
const { doc, selection, tr } = state;
|
|
170
|
+
const { anchor, head } = selection;
|
|
171
|
+
if (!isNodeRangeSelection(selection)) {
|
|
172
|
+
const nodeRangeSelection2 = NodeRangeSelection.create(doc, anchor, head, depth, -1);
|
|
173
|
+
tr.setSelection(nodeRangeSelection2);
|
|
174
|
+
view.dispatch(tr);
|
|
175
|
+
return true;
|
|
220
176
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
177
|
+
const nodeRangeSelection = selection.extendBackwards();
|
|
178
|
+
tr.setSelection(nodeRangeSelection);
|
|
179
|
+
view.dispatch(tr);
|
|
180
|
+
return true;
|
|
181
|
+
},
|
|
182
|
+
// extend NodeRangeSelection downwards
|
|
183
|
+
"Shift-ArrowDown": ({ editor }) => {
|
|
184
|
+
const { depth } = this.options;
|
|
185
|
+
const { view, state } = editor;
|
|
186
|
+
const { doc, selection, tr } = state;
|
|
187
|
+
const { anchor, head } = selection;
|
|
188
|
+
if (!isNodeRangeSelection(selection)) {
|
|
189
|
+
const nodeRangeSelection2 = NodeRangeSelection.create(doc, anchor, head, depth);
|
|
190
|
+
tr.setSelection(nodeRangeSelection2);
|
|
191
|
+
view.dispatch(tr);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
const nodeRangeSelection = selection.extendForwards();
|
|
195
|
+
tr.setSelection(nodeRangeSelection);
|
|
196
|
+
view.dispatch(tr);
|
|
197
|
+
return true;
|
|
198
|
+
},
|
|
199
|
+
// add `NodeRangeSelection` to all nodes
|
|
200
|
+
"Mod-a": ({ editor }) => {
|
|
201
|
+
const { depth } = this.options;
|
|
202
|
+
const { view, state } = editor;
|
|
203
|
+
const { doc, tr } = state;
|
|
204
|
+
const nodeRangeSelection = NodeRangeSelection.create(doc, 0, doc.content.size, depth);
|
|
205
|
+
tr.setSelection(nodeRangeSelection);
|
|
206
|
+
view.dispatch(tr);
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
onSelectionUpdate() {
|
|
212
|
+
const { selection } = this.editor.state;
|
|
213
|
+
if (isNodeRangeSelection(selection)) {
|
|
214
|
+
this.editor.view.dom.classList.add("ProseMirror-noderangeselection");
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
addProseMirrorPlugins() {
|
|
218
|
+
let hideTextSelection = false;
|
|
219
|
+
let activeMouseSelection = false;
|
|
220
|
+
return [
|
|
221
|
+
new Plugin({
|
|
222
|
+
key: new PluginKey("nodeRange"),
|
|
223
|
+
props: {
|
|
224
|
+
attributes: () => {
|
|
225
|
+
if (hideTextSelection) {
|
|
226
|
+
return {
|
|
227
|
+
class: "ProseMirror-noderangeselection"
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return { class: "" };
|
|
231
|
+
},
|
|
232
|
+
handleDOMEvents: {
|
|
233
|
+
mousedown: (view, event) => {
|
|
234
|
+
const { key } = this.options;
|
|
235
|
+
const isMac = /Mac/.test(navigator.platform);
|
|
236
|
+
const isShift = !!event.shiftKey;
|
|
237
|
+
const isControl = !!event.ctrlKey;
|
|
238
|
+
const isAlt = !!event.altKey;
|
|
239
|
+
const isMeta = !!event.metaKey;
|
|
240
|
+
const isMod = isMac ? isMeta : isControl;
|
|
241
|
+
if (key === null || key === void 0 || key === "Shift" && isShift || key === "Control" && isControl || key === "Alt" && isAlt || key === "Meta" && isMeta || key === "Mod" && isMod) {
|
|
242
|
+
activeMouseSelection = true;
|
|
243
|
+
}
|
|
244
|
+
if (!activeMouseSelection) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
document.addEventListener(
|
|
248
|
+
"mouseup",
|
|
249
|
+
() => {
|
|
250
|
+
activeMouseSelection = false;
|
|
251
|
+
const { state } = view;
|
|
252
|
+
const { doc, selection, tr } = state;
|
|
253
|
+
const { $anchor, $head } = selection;
|
|
254
|
+
if ($anchor.sameParent($head)) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const nodeRangeSelection = NodeRangeSelection.create(doc, $anchor.pos, $head.pos, this.options.depth);
|
|
258
|
+
tr.setSelection(nodeRangeSelection);
|
|
259
|
+
view.dispatch(tr);
|
|
303
260
|
},
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
261
|
+
{ once: true }
|
|
262
|
+
);
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
// when selecting some text we want to render some decorations
|
|
267
|
+
// to preview a `NodeRangeSelection`
|
|
268
|
+
decorations: (state) => {
|
|
269
|
+
const { selection } = state;
|
|
270
|
+
const isNodeRange = isNodeRangeSelection(selection);
|
|
271
|
+
hideTextSelection = false;
|
|
272
|
+
if (!activeMouseSelection) {
|
|
273
|
+
if (!isNodeRange) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
hideTextSelection = true;
|
|
277
|
+
return getNodeRangeDecorations(selection.ranges);
|
|
278
|
+
}
|
|
279
|
+
const { $from, $to } = selection;
|
|
280
|
+
if (!isNodeRange && $from.sameParent($to)) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
const nodeRanges = getSelectionRanges($from, $to, this.options.depth);
|
|
284
|
+
if (!nodeRanges.length) {
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
hideTextSelection = true;
|
|
288
|
+
return getNodeRangeDecorations(nodeRanges);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
})
|
|
292
|
+
];
|
|
293
|
+
}
|
|
307
294
|
});
|
|
308
295
|
|
|
309
|
-
|
|
310
|
-
|
|
296
|
+
// src/index.ts
|
|
297
|
+
var index_default = NodeRange2;
|
|
298
|
+
export {
|
|
299
|
+
NodeRange2 as NodeRange,
|
|
300
|
+
NodeRangeSelection,
|
|
301
|
+
index_default as default,
|
|
302
|
+
getNodeRangeDecorations,
|
|
303
|
+
getSelectionRanges,
|
|
304
|
+
isNodeRangeSelection
|
|
305
|
+
};
|
|
306
|
+
//# sourceMappingURL=index.js.map
|