@tiptap/vue-3 3.20.2 → 3.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,876 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/menus/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- BubbleMenu: () => BubbleMenu,
24
- FloatingMenu: () => FloatingMenu
25
- });
26
- module.exports = __toCommonJS(index_exports);
27
-
28
- // ../extension-bubble-menu/src/bubble-menu-plugin.ts
29
- var import_dom = require("@floating-ui/dom");
30
- var import_core = require("@tiptap/core");
31
- var import_state = require("@tiptap/pm/state");
32
- var import_tables = require("@tiptap/pm/tables");
33
- function combineDOMRects(rect1, rect2) {
34
- const top = Math.min(rect1.top, rect2.top);
35
- const bottom = Math.max(rect1.bottom, rect2.bottom);
36
- const left = Math.min(rect1.left, rect2.left);
37
- const right = Math.max(rect1.right, rect2.right);
38
- const width = right - left;
39
- const height = bottom - top;
40
- const x = left;
41
- const y = top;
42
- return new DOMRect(x, y, width, height);
43
- }
44
- var BubbleMenuView = class {
45
- constructor({
46
- editor,
47
- element,
48
- view,
49
- pluginKey = "bubbleMenu",
50
- updateDelay = 250,
51
- resizeDelay = 60,
52
- shouldShow,
53
- appendTo,
54
- getReferencedVirtualElement,
55
- options
56
- }) {
57
- this.preventHide = false;
58
- this.isVisible = false;
59
- this.scrollTarget = window;
60
- this.floatingUIOptions = {
61
- strategy: "absolute",
62
- placement: "top",
63
- offset: 8,
64
- flip: {},
65
- shift: {},
66
- arrow: false,
67
- size: false,
68
- autoPlacement: false,
69
- hide: false,
70
- inline: false,
71
- onShow: void 0,
72
- onHide: void 0,
73
- onUpdate: void 0,
74
- onDestroy: void 0
75
- };
76
- this.shouldShow = ({ view, state, from, to }) => {
77
- const { doc, selection } = state;
78
- const { empty } = selection;
79
- const isEmptyTextBlock = !doc.textBetween(from, to).length && (0, import_core.isTextSelection)(state.selection);
80
- const isChildOfMenu = this.element.contains(document.activeElement);
81
- const hasEditorFocus = view.hasFocus() || isChildOfMenu;
82
- if (!hasEditorFocus || empty || isEmptyTextBlock || !this.editor.isEditable) {
83
- return false;
84
- }
85
- return true;
86
- };
87
- this.mousedownHandler = () => {
88
- this.preventHide = true;
89
- };
90
- this.dragstartHandler = () => {
91
- this.hide();
92
- };
93
- /**
94
- * Handles the window resize event to update the position of the bubble menu.
95
- * It uses a debounce mechanism to prevent excessive updates.
96
- * The delay is defined by the `resizeDelay` property.
97
- */
98
- this.resizeHandler = () => {
99
- if (this.resizeDebounceTimer) {
100
- clearTimeout(this.resizeDebounceTimer);
101
- }
102
- this.resizeDebounceTimer = window.setTimeout(() => {
103
- this.updatePosition();
104
- }, this.resizeDelay);
105
- };
106
- this.focusHandler = () => {
107
- setTimeout(() => this.update(this.editor.view));
108
- };
109
- this.blurHandler = ({ event }) => {
110
- var _a;
111
- if (this.editor.isDestroyed) {
112
- this.destroy();
113
- return;
114
- }
115
- if (this.preventHide) {
116
- this.preventHide = false;
117
- return;
118
- }
119
- if ((event == null ? void 0 : event.relatedTarget) && ((_a = this.element.parentNode) == null ? void 0 : _a.contains(event.relatedTarget))) {
120
- return;
121
- }
122
- if ((event == null ? void 0 : event.relatedTarget) === this.editor.view.dom) {
123
- return;
124
- }
125
- this.hide();
126
- };
127
- this.handleDebouncedUpdate = (view, oldState) => {
128
- const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
129
- const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
130
- if (!selectionChanged && !docChanged) {
131
- return;
132
- }
133
- if (this.updateDebounceTimer) {
134
- clearTimeout(this.updateDebounceTimer);
135
- }
136
- this.updateDebounceTimer = window.setTimeout(() => {
137
- this.updateHandler(view, selectionChanged, docChanged, oldState);
138
- }, this.updateDelay);
139
- };
140
- this.updateHandler = (view, selectionChanged, docChanged, oldState) => {
141
- const { composing } = view;
142
- const isSame = !selectionChanged && !docChanged;
143
- if (composing || isSame) {
144
- return;
145
- }
146
- const shouldShow = this.getShouldShow(oldState);
147
- if (!shouldShow) {
148
- this.hide();
149
- return;
150
- }
151
- this.updatePosition();
152
- this.show();
153
- };
154
- /**
155
- * Handles the transaction event to update the position of the bubble menu.
156
- * This allows external code to trigger a position update via:
157
- * `editor.view.dispatch(editor.state.tr.setMeta(pluginKey, 'updatePosition'))`
158
- * The `pluginKey` defaults to `bubbleMenu`
159
- */
160
- this.transactionHandler = ({ transaction: tr }) => {
161
- const meta = tr.getMeta(this.pluginKey);
162
- if (meta === "updatePosition") {
163
- this.updatePosition();
164
- } else if (meta && typeof meta === "object" && meta.type === "updateOptions") {
165
- this.updateOptions(meta.options);
166
- }
167
- };
168
- var _a;
169
- this.editor = editor;
170
- this.element = element;
171
- this.view = view;
172
- this.pluginKey = pluginKey;
173
- this.updateDelay = updateDelay;
174
- this.resizeDelay = resizeDelay;
175
- this.appendTo = appendTo;
176
- this.scrollTarget = (_a = options == null ? void 0 : options.scrollTarget) != null ? _a : window;
177
- this.getReferencedVirtualElement = getReferencedVirtualElement;
178
- this.floatingUIOptions = {
179
- ...this.floatingUIOptions,
180
- ...options
181
- };
182
- this.element.tabIndex = 0;
183
- if (shouldShow) {
184
- this.shouldShow = shouldShow;
185
- }
186
- this.element.addEventListener("mousedown", this.mousedownHandler, { capture: true });
187
- this.view.dom.addEventListener("dragstart", this.dragstartHandler);
188
- this.editor.on("focus", this.focusHandler);
189
- this.editor.on("blur", this.blurHandler);
190
- this.editor.on("transaction", this.transactionHandler);
191
- window.addEventListener("resize", this.resizeHandler);
192
- this.scrollTarget.addEventListener("scroll", this.resizeHandler);
193
- this.update(view, view.state);
194
- if (this.getShouldShow()) {
195
- this.show();
196
- this.updatePosition();
197
- }
198
- }
199
- get middlewares() {
200
- const middlewares = [];
201
- if (this.floatingUIOptions.flip) {
202
- middlewares.push((0, import_dom.flip)(typeof this.floatingUIOptions.flip !== "boolean" ? this.floatingUIOptions.flip : void 0));
203
- }
204
- if (this.floatingUIOptions.shift) {
205
- middlewares.push(
206
- (0, import_dom.shift)(typeof this.floatingUIOptions.shift !== "boolean" ? this.floatingUIOptions.shift : void 0)
207
- );
208
- }
209
- if (this.floatingUIOptions.offset) {
210
- middlewares.push(
211
- (0, import_dom.offset)(typeof this.floatingUIOptions.offset !== "boolean" ? this.floatingUIOptions.offset : void 0)
212
- );
213
- }
214
- if (this.floatingUIOptions.arrow) {
215
- middlewares.push((0, import_dom.arrow)(this.floatingUIOptions.arrow));
216
- }
217
- if (this.floatingUIOptions.size) {
218
- middlewares.push((0, import_dom.size)(typeof this.floatingUIOptions.size !== "boolean" ? this.floatingUIOptions.size : void 0));
219
- }
220
- if (this.floatingUIOptions.autoPlacement) {
221
- middlewares.push(
222
- (0, import_dom.autoPlacement)(
223
- typeof this.floatingUIOptions.autoPlacement !== "boolean" ? this.floatingUIOptions.autoPlacement : void 0
224
- )
225
- );
226
- }
227
- if (this.floatingUIOptions.hide) {
228
- middlewares.push((0, import_dom.hide)(typeof this.floatingUIOptions.hide !== "boolean" ? this.floatingUIOptions.hide : void 0));
229
- }
230
- if (this.floatingUIOptions.inline) {
231
- middlewares.push(
232
- (0, import_dom.inline)(typeof this.floatingUIOptions.inline !== "boolean" ? this.floatingUIOptions.inline : void 0)
233
- );
234
- }
235
- return middlewares;
236
- }
237
- get virtualElement() {
238
- var _a, _b, _c;
239
- const { selection } = this.editor.state;
240
- const referencedVirtualElement = (_a = this.getReferencedVirtualElement) == null ? void 0 : _a.call(this);
241
- if (referencedVirtualElement) {
242
- return referencedVirtualElement;
243
- }
244
- if (!((_c = (_b = this.view) == null ? void 0 : _b.dom) == null ? void 0 : _c.parentNode)) {
245
- return;
246
- }
247
- const domRect = (0, import_core.posToDOMRect)(this.view, selection.from, selection.to);
248
- let virtualElement = {
249
- getBoundingClientRect: () => domRect,
250
- getClientRects: () => [domRect]
251
- };
252
- if (selection instanceof import_state.NodeSelection) {
253
- let node = this.view.nodeDOM(selection.from);
254
- const nodeViewWrapper = node.dataset.nodeViewWrapper ? node : node.querySelector("[data-node-view-wrapper]");
255
- if (nodeViewWrapper) {
256
- node = nodeViewWrapper;
257
- }
258
- if (node) {
259
- virtualElement = {
260
- getBoundingClientRect: () => node.getBoundingClientRect(),
261
- getClientRects: () => [node.getBoundingClientRect()]
262
- };
263
- }
264
- }
265
- if (selection instanceof import_tables.CellSelection) {
266
- const { $anchorCell, $headCell } = selection;
267
- const from = $anchorCell ? $anchorCell.pos : $headCell.pos;
268
- const to = $headCell ? $headCell.pos : $anchorCell.pos;
269
- const fromDOM = this.view.nodeDOM(from);
270
- const toDOM = this.view.nodeDOM(to);
271
- if (!fromDOM || !toDOM) {
272
- return;
273
- }
274
- const clientRect = fromDOM === toDOM ? fromDOM.getBoundingClientRect() : combineDOMRects(
275
- fromDOM.getBoundingClientRect(),
276
- toDOM.getBoundingClientRect()
277
- );
278
- virtualElement = {
279
- getBoundingClientRect: () => clientRect,
280
- getClientRects: () => [clientRect]
281
- };
282
- }
283
- return virtualElement;
284
- }
285
- updatePosition() {
286
- const virtualElement = this.virtualElement;
287
- if (!virtualElement) {
288
- return;
289
- }
290
- (0, import_dom.computePosition)(virtualElement, this.element, {
291
- placement: this.floatingUIOptions.placement,
292
- strategy: this.floatingUIOptions.strategy,
293
- middleware: this.middlewares
294
- }).then(({ x, y, strategy, middlewareData }) => {
295
- var _a, _b;
296
- if (((_a = middlewareData.hide) == null ? void 0 : _a.referenceHidden) || ((_b = middlewareData.hide) == null ? void 0 : _b.escaped)) {
297
- this.element.style.visibility = "hidden";
298
- return;
299
- }
300
- this.element.style.visibility = "visible";
301
- this.element.style.width = "max-content";
302
- this.element.style.position = strategy;
303
- this.element.style.left = `${x}px`;
304
- this.element.style.top = `${y}px`;
305
- if (this.isVisible && this.floatingUIOptions.onUpdate) {
306
- this.floatingUIOptions.onUpdate();
307
- }
308
- });
309
- }
310
- update(view, oldState) {
311
- const { state } = view;
312
- const hasValidSelection = state.selection.from !== state.selection.to;
313
- if (this.updateDelay > 0 && hasValidSelection) {
314
- this.handleDebouncedUpdate(view, oldState);
315
- return;
316
- }
317
- const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
318
- const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
319
- this.updateHandler(view, selectionChanged, docChanged, oldState);
320
- }
321
- getShouldShow(oldState) {
322
- var _a;
323
- const { state } = this.view;
324
- const { selection } = state;
325
- const { ranges } = selection;
326
- const from = Math.min(...ranges.map((range) => range.$from.pos));
327
- const to = Math.max(...ranges.map((range) => range.$to.pos));
328
- const shouldShow = (_a = this.shouldShow) == null ? void 0 : _a.call(this, {
329
- editor: this.editor,
330
- element: this.element,
331
- view: this.view,
332
- state,
333
- oldState,
334
- from,
335
- to
336
- });
337
- return shouldShow || false;
338
- }
339
- show() {
340
- var _a;
341
- if (this.isVisible) {
342
- return;
343
- }
344
- this.element.style.visibility = "visible";
345
- this.element.style.opacity = "1";
346
- const appendToElement = typeof this.appendTo === "function" ? this.appendTo() : this.appendTo;
347
- (_a = appendToElement != null ? appendToElement : this.view.dom.parentElement) == null ? void 0 : _a.appendChild(this.element);
348
- if (this.floatingUIOptions.onShow) {
349
- this.floatingUIOptions.onShow();
350
- }
351
- this.isVisible = true;
352
- }
353
- hide() {
354
- if (!this.isVisible) {
355
- return;
356
- }
357
- this.element.style.visibility = "hidden";
358
- this.element.style.opacity = "0";
359
- this.element.remove();
360
- if (this.floatingUIOptions.onHide) {
361
- this.floatingUIOptions.onHide();
362
- }
363
- this.isVisible = false;
364
- }
365
- updateOptions(newProps) {
366
- var _a;
367
- if (newProps.updateDelay !== void 0) {
368
- this.updateDelay = newProps.updateDelay;
369
- }
370
- if (newProps.resizeDelay !== void 0) {
371
- this.resizeDelay = newProps.resizeDelay;
372
- }
373
- if (newProps.appendTo !== void 0) {
374
- this.appendTo = newProps.appendTo;
375
- }
376
- if (newProps.getReferencedVirtualElement !== void 0) {
377
- this.getReferencedVirtualElement = newProps.getReferencedVirtualElement;
378
- }
379
- if (newProps.shouldShow !== void 0) {
380
- if (newProps.shouldShow) {
381
- this.shouldShow = newProps.shouldShow;
382
- }
383
- }
384
- if (newProps.options !== void 0) {
385
- const newScrollTarget = (_a = newProps.options.scrollTarget) != null ? _a : window;
386
- if (newScrollTarget !== this.scrollTarget) {
387
- this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
388
- this.scrollTarget = newScrollTarget;
389
- this.scrollTarget.addEventListener("scroll", this.resizeHandler);
390
- }
391
- this.floatingUIOptions = {
392
- ...this.floatingUIOptions,
393
- ...newProps.options
394
- };
395
- }
396
- }
397
- destroy() {
398
- this.hide();
399
- this.element.removeEventListener("mousedown", this.mousedownHandler, { capture: true });
400
- this.view.dom.removeEventListener("dragstart", this.dragstartHandler);
401
- window.removeEventListener("resize", this.resizeHandler);
402
- this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
403
- this.editor.off("focus", this.focusHandler);
404
- this.editor.off("blur", this.blurHandler);
405
- this.editor.off("transaction", this.transactionHandler);
406
- if (this.floatingUIOptions.onDestroy) {
407
- this.floatingUIOptions.onDestroy();
408
- }
409
- }
410
- };
411
- var BubbleMenuPlugin = (options) => {
412
- return new import_state.Plugin({
413
- key: typeof options.pluginKey === "string" ? new import_state.PluginKey(options.pluginKey) : options.pluginKey,
414
- view: (view) => new BubbleMenuView({ view, ...options })
415
- });
416
- };
417
-
418
- // src/menus/BubbleMenu.ts
419
- var import_vue = require("vue");
420
- var BubbleMenu = (0, import_vue.defineComponent)({
421
- name: "BubbleMenu",
422
- inheritAttrs: false,
423
- props: {
424
- pluginKey: {
425
- type: [String, Object],
426
- default: "bubbleMenu"
427
- },
428
- editor: {
429
- type: Object,
430
- required: true
431
- },
432
- updateDelay: {
433
- type: Number,
434
- default: void 0
435
- },
436
- resizeDelay: {
437
- type: Number,
438
- default: void 0
439
- },
440
- options: {
441
- type: Object,
442
- default: () => ({})
443
- },
444
- appendTo: {
445
- type: [Object, Function],
446
- default: void 0
447
- },
448
- shouldShow: {
449
- type: Function,
450
- default: null
451
- },
452
- getReferencedVirtualElement: {
453
- type: Function,
454
- default: void 0
455
- }
456
- },
457
- setup(props, { slots, attrs }) {
458
- const root = (0, import_vue.ref)(null);
459
- (0, import_vue.onMounted)(() => {
460
- const {
461
- editor,
462
- options,
463
- pluginKey,
464
- resizeDelay,
465
- appendTo,
466
- shouldShow,
467
- getReferencedVirtualElement,
468
- updateDelay
469
- } = props;
470
- const el = root.value;
471
- if (!el) {
472
- return;
473
- }
474
- el.style.visibility = "hidden";
475
- el.style.position = "absolute";
476
- el.remove();
477
- (0, import_vue.nextTick)(() => {
478
- editor.registerPlugin(
479
- BubbleMenuPlugin({
480
- editor,
481
- element: el,
482
- options,
483
- pluginKey,
484
- resizeDelay,
485
- appendTo,
486
- shouldShow,
487
- getReferencedVirtualElement,
488
- updateDelay
489
- })
490
- );
491
- });
492
- });
493
- (0, import_vue.onBeforeUnmount)(() => {
494
- const { pluginKey, editor } = props;
495
- editor.unregisterPlugin(pluginKey);
496
- });
497
- return () => {
498
- var _a;
499
- return (0, import_vue.h)("div", { ref: root, ...attrs }, (_a = slots.default) == null ? void 0 : _a.call(slots));
500
- };
501
- }
502
- });
503
-
504
- // ../extension-floating-menu/src/floating-menu-plugin.ts
505
- var import_dom2 = require("@floating-ui/dom");
506
- var import_core2 = require("@tiptap/core");
507
- var import_state2 = require("@tiptap/pm/state");
508
- var FloatingMenuView = class {
509
- constructor({
510
- editor,
511
- element,
512
- view,
513
- pluginKey = "floatingMenu",
514
- updateDelay = 250,
515
- resizeDelay = 60,
516
- options,
517
- appendTo,
518
- shouldShow
519
- }) {
520
- this.preventHide = false;
521
- this.isVisible = false;
522
- this.scrollTarget = window;
523
- this.shouldShow = ({ view, state }) => {
524
- const { selection } = state;
525
- const { $anchor, empty } = selection;
526
- const isRootDepth = $anchor.depth === 1;
527
- const isEmptyTextBlock = $anchor.parent.isTextblock && !$anchor.parent.type.spec.code && !$anchor.parent.textContent && $anchor.parent.childCount === 0 && !this.getTextContent($anchor.parent);
528
- if (!view.hasFocus() || !empty || !isRootDepth || !isEmptyTextBlock || !this.editor.isEditable) {
529
- return false;
530
- }
531
- return true;
532
- };
533
- this.floatingUIOptions = {
534
- strategy: "absolute",
535
- placement: "right",
536
- offset: 8,
537
- flip: {},
538
- shift: {},
539
- arrow: false,
540
- size: false,
541
- autoPlacement: false,
542
- hide: false,
543
- inline: false
544
- };
545
- this.updateHandler = (view, selectionChanged, docChanged, oldState) => {
546
- const { composing } = view;
547
- const isSame = !selectionChanged && !docChanged;
548
- if (composing || isSame) {
549
- return;
550
- }
551
- const shouldShow = this.getShouldShow(oldState);
552
- if (!shouldShow) {
553
- this.hide();
554
- return;
555
- }
556
- this.updatePosition();
557
- this.show();
558
- };
559
- this.mousedownHandler = () => {
560
- this.preventHide = true;
561
- };
562
- this.focusHandler = () => {
563
- setTimeout(() => this.update(this.editor.view));
564
- };
565
- this.blurHandler = ({ event }) => {
566
- var _a;
567
- if (this.preventHide) {
568
- this.preventHide = false;
569
- return;
570
- }
571
- if ((event == null ? void 0 : event.relatedTarget) && ((_a = this.element.parentNode) == null ? void 0 : _a.contains(event.relatedTarget))) {
572
- return;
573
- }
574
- if ((event == null ? void 0 : event.relatedTarget) === this.editor.view.dom) {
575
- return;
576
- }
577
- this.hide();
578
- };
579
- /**
580
- * Handles the transaction event to update the position of the floating menu.
581
- * This allows external code to trigger a position update via:
582
- * `editor.view.dispatch(editor.state.tr.setMeta(pluginKey, 'updatePosition'))`
583
- * The `pluginKey` defaults to `floatingMenu`
584
- */
585
- this.transactionHandler = ({ transaction: tr }) => {
586
- const meta = tr.getMeta(this.pluginKey);
587
- if (meta === "updatePosition") {
588
- this.updatePosition();
589
- } else if (meta && typeof meta === "object" && meta.type === "updateOptions") {
590
- this.updateOptions(meta.options);
591
- }
592
- };
593
- /**
594
- * Handles the window resize event to update the position of the floating menu.
595
- * It uses a debounce mechanism to prevent excessive updates.
596
- * The delay is defined by the `resizeDelay` property.
597
- */
598
- this.resizeHandler = () => {
599
- if (this.resizeDebounceTimer) {
600
- clearTimeout(this.resizeDebounceTimer);
601
- }
602
- this.resizeDebounceTimer = window.setTimeout(() => {
603
- this.updatePosition();
604
- }, this.resizeDelay);
605
- };
606
- var _a;
607
- this.editor = editor;
608
- this.element = element;
609
- this.view = view;
610
- this.pluginKey = pluginKey;
611
- this.updateDelay = updateDelay;
612
- this.resizeDelay = resizeDelay;
613
- this.appendTo = appendTo;
614
- this.scrollTarget = (_a = options == null ? void 0 : options.scrollTarget) != null ? _a : window;
615
- this.floatingUIOptions = {
616
- ...this.floatingUIOptions,
617
- ...options
618
- };
619
- this.element.tabIndex = 0;
620
- if (shouldShow) {
621
- this.shouldShow = shouldShow;
622
- }
623
- this.element.addEventListener("mousedown", this.mousedownHandler, { capture: true });
624
- this.editor.on("focus", this.focusHandler);
625
- this.editor.on("blur", this.blurHandler);
626
- this.editor.on("transaction", this.transactionHandler);
627
- window.addEventListener("resize", this.resizeHandler);
628
- this.scrollTarget.addEventListener("scroll", this.resizeHandler);
629
- this.update(view, view.state);
630
- if (this.getShouldShow()) {
631
- this.show();
632
- this.updatePosition();
633
- }
634
- }
635
- getTextContent(node) {
636
- return (0, import_core2.getText)(node, { textSerializers: (0, import_core2.getTextSerializersFromSchema)(this.editor.schema) });
637
- }
638
- get middlewares() {
639
- const middlewares = [];
640
- if (this.floatingUIOptions.flip) {
641
- middlewares.push((0, import_dom2.flip)(typeof this.floatingUIOptions.flip !== "boolean" ? this.floatingUIOptions.flip : void 0));
642
- }
643
- if (this.floatingUIOptions.shift) {
644
- middlewares.push(
645
- (0, import_dom2.shift)(typeof this.floatingUIOptions.shift !== "boolean" ? this.floatingUIOptions.shift : void 0)
646
- );
647
- }
648
- if (this.floatingUIOptions.offset) {
649
- middlewares.push(
650
- (0, import_dom2.offset)(typeof this.floatingUIOptions.offset !== "boolean" ? this.floatingUIOptions.offset : void 0)
651
- );
652
- }
653
- if (this.floatingUIOptions.arrow) {
654
- middlewares.push((0, import_dom2.arrow)(this.floatingUIOptions.arrow));
655
- }
656
- if (this.floatingUIOptions.size) {
657
- middlewares.push((0, import_dom2.size)(typeof this.floatingUIOptions.size !== "boolean" ? this.floatingUIOptions.size : void 0));
658
- }
659
- if (this.floatingUIOptions.autoPlacement) {
660
- middlewares.push(
661
- (0, import_dom2.autoPlacement)(
662
- typeof this.floatingUIOptions.autoPlacement !== "boolean" ? this.floatingUIOptions.autoPlacement : void 0
663
- )
664
- );
665
- }
666
- if (this.floatingUIOptions.hide) {
667
- middlewares.push((0, import_dom2.hide)(typeof this.floatingUIOptions.hide !== "boolean" ? this.floatingUIOptions.hide : void 0));
668
- }
669
- if (this.floatingUIOptions.inline) {
670
- middlewares.push(
671
- (0, import_dom2.inline)(typeof this.floatingUIOptions.inline !== "boolean" ? this.floatingUIOptions.inline : void 0)
672
- );
673
- }
674
- return middlewares;
675
- }
676
- getShouldShow(oldState) {
677
- var _a;
678
- const { state } = this.view;
679
- const { selection } = state;
680
- const { ranges } = selection;
681
- const from = Math.min(...ranges.map((range) => range.$from.pos));
682
- const to = Math.max(...ranges.map((range) => range.$to.pos));
683
- const shouldShow = (_a = this.shouldShow) == null ? void 0 : _a.call(this, {
684
- editor: this.editor,
685
- view: this.view,
686
- state,
687
- oldState,
688
- from,
689
- to
690
- });
691
- return shouldShow;
692
- }
693
- updateOptions(newProps) {
694
- var _a;
695
- if (newProps.updateDelay !== void 0) {
696
- this.updateDelay = newProps.updateDelay;
697
- }
698
- if (newProps.resizeDelay !== void 0) {
699
- this.resizeDelay = newProps.resizeDelay;
700
- }
701
- if (newProps.appendTo !== void 0) {
702
- this.appendTo = newProps.appendTo;
703
- }
704
- if (newProps.shouldShow !== void 0) {
705
- if (newProps.shouldShow) {
706
- this.shouldShow = newProps.shouldShow;
707
- }
708
- }
709
- if (newProps.options !== void 0) {
710
- const newScrollTarget = (_a = newProps.options.scrollTarget) != null ? _a : window;
711
- if (newScrollTarget !== this.scrollTarget) {
712
- this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
713
- this.scrollTarget = newScrollTarget;
714
- this.scrollTarget.addEventListener("scroll", this.resizeHandler);
715
- }
716
- this.floatingUIOptions = {
717
- ...this.floatingUIOptions,
718
- ...newProps.options
719
- };
720
- }
721
- }
722
- updatePosition() {
723
- const { selection } = this.editor.state;
724
- const domRect = (0, import_core2.posToDOMRect)(this.view, selection.from, selection.to);
725
- const virtualElement = {
726
- getBoundingClientRect: () => domRect,
727
- getClientRects: () => [domRect]
728
- };
729
- (0, import_dom2.computePosition)(virtualElement, this.element, {
730
- placement: this.floatingUIOptions.placement,
731
- strategy: this.floatingUIOptions.strategy,
732
- middleware: this.middlewares
733
- }).then(({ x, y, strategy, middlewareData }) => {
734
- var _a, _b;
735
- if (((_a = middlewareData.hide) == null ? void 0 : _a.referenceHidden) || ((_b = middlewareData.hide) == null ? void 0 : _b.escaped)) {
736
- this.element.style.visibility = "hidden";
737
- return;
738
- }
739
- this.element.style.visibility = "visible";
740
- this.element.style.width = "max-content";
741
- this.element.style.position = strategy;
742
- this.element.style.left = `${x}px`;
743
- this.element.style.top = `${y}px`;
744
- if (this.isVisible && this.floatingUIOptions.onUpdate) {
745
- this.floatingUIOptions.onUpdate();
746
- }
747
- });
748
- }
749
- update(view, oldState) {
750
- const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
751
- const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
752
- this.updateHandler(view, selectionChanged, docChanged, oldState);
753
- }
754
- show() {
755
- var _a;
756
- if (this.isVisible) {
757
- return;
758
- }
759
- this.element.style.visibility = "visible";
760
- this.element.style.opacity = "1";
761
- const appendToElement = typeof this.appendTo === "function" ? this.appendTo() : this.appendTo;
762
- (_a = appendToElement != null ? appendToElement : this.view.dom.parentElement) == null ? void 0 : _a.appendChild(this.element);
763
- if (this.floatingUIOptions.onShow) {
764
- this.floatingUIOptions.onShow();
765
- }
766
- this.isVisible = true;
767
- }
768
- hide() {
769
- if (!this.isVisible) {
770
- return;
771
- }
772
- this.element.style.visibility = "hidden";
773
- this.element.style.opacity = "0";
774
- this.element.remove();
775
- if (this.floatingUIOptions.onHide) {
776
- this.floatingUIOptions.onHide();
777
- }
778
- this.isVisible = false;
779
- }
780
- destroy() {
781
- this.hide();
782
- this.element.removeEventListener("mousedown", this.mousedownHandler, { capture: true });
783
- window.removeEventListener("resize", this.resizeHandler);
784
- this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
785
- this.editor.off("focus", this.focusHandler);
786
- this.editor.off("blur", this.blurHandler);
787
- this.editor.off("transaction", this.transactionHandler);
788
- if (this.floatingUIOptions.onDestroy) {
789
- this.floatingUIOptions.onDestroy();
790
- }
791
- }
792
- };
793
- var FloatingMenuPlugin = (options) => {
794
- return new import_state2.Plugin({
795
- key: typeof options.pluginKey === "string" ? new import_state2.PluginKey(options.pluginKey) : options.pluginKey,
796
- view: (view) => new FloatingMenuView({ view, ...options })
797
- });
798
- };
799
-
800
- // src/menus/FloatingMenu.ts
801
- var import_vue2 = require("vue");
802
- var FloatingMenu = (0, import_vue2.defineComponent)({
803
- name: "FloatingMenu",
804
- inheritAttrs: false,
805
- props: {
806
- pluginKey: {
807
- // TODO: TypeScript breaks :(
808
- // type: [String, Object as PropType<Exclude<FloatingMenuPluginProps['pluginKey'], string>>],
809
- type: null,
810
- default: "floatingMenu"
811
- },
812
- editor: {
813
- type: Object,
814
- required: true
815
- },
816
- updateDelay: {
817
- type: Number,
818
- default: void 0
819
- },
820
- resizeDelay: {
821
- type: Number,
822
- default: void 0
823
- },
824
- options: {
825
- type: Object,
826
- default: () => ({})
827
- },
828
- appendTo: {
829
- type: [Object, Function],
830
- default: void 0
831
- },
832
- shouldShow: {
833
- type: Function,
834
- default: null
835
- }
836
- },
837
- setup(props, { slots, attrs }) {
838
- const root = (0, import_vue2.ref)(null);
839
- (0, import_vue2.onMounted)(() => {
840
- const { pluginKey, editor, updateDelay, resizeDelay, options, appendTo, shouldShow } = props;
841
- const el = root.value;
842
- if (!el) {
843
- return;
844
- }
845
- el.style.visibility = "hidden";
846
- el.style.position = "absolute";
847
- el.remove();
848
- editor.registerPlugin(
849
- FloatingMenuPlugin({
850
- pluginKey,
851
- editor,
852
- element: el,
853
- updateDelay,
854
- resizeDelay,
855
- options,
856
- appendTo,
857
- shouldShow
858
- })
859
- );
860
- });
861
- (0, import_vue2.onBeforeUnmount)(() => {
862
- const { pluginKey, editor } = props;
863
- editor.unregisterPlugin(pluginKey);
864
- });
865
- return () => {
866
- var _a;
867
- return (0, import_vue2.h)("div", { ref: root, ...attrs }, (_a = slots.default) == null ? void 0 : _a.call(slots));
868
- };
869
- }
870
- });
871
- // Annotate the CommonJS export names for ESM import in node:
872
- 0 && (module.exports = {
873
- BubbleMenu,
874
- FloatingMenu
875
- });
876
- //# sourceMappingURL=index.cjs.map