gdpr audit implemented, email log, vollmachten, pdf delete cancel data privacy and vollmachten, removed message no id card in engergy car, and other contracts that are not telecom contracts, added insert counter for engery
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025, Tiptap GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
# @tiptap/extension-bubble-menu
|
||||
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-bubble-menu)
|
||||
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-bubble-menu)
|
||||
[](https://github.com/sponsors/ueberdosis)
|
||||
|
||||
## Introduction
|
||||
|
||||
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
|
||||
|
||||
## Official Documentation
|
||||
|
||||
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
||||
|
||||
## License
|
||||
|
||||
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
||||
+454
@@ -0,0 +1,454 @@
|
||||
"use strict";
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
|
||||
// src/index.ts
|
||||
var index_exports = {};
|
||||
__export(index_exports, {
|
||||
BubbleMenu: () => BubbleMenu,
|
||||
BubbleMenuPlugin: () => BubbleMenuPlugin,
|
||||
BubbleMenuView: () => BubbleMenuView,
|
||||
default: () => index_default
|
||||
});
|
||||
module.exports = __toCommonJS(index_exports);
|
||||
|
||||
// src/bubble-menu.ts
|
||||
var import_core2 = require("@tiptap/core");
|
||||
|
||||
// src/bubble-menu-plugin.ts
|
||||
var import_dom = require("@floating-ui/dom");
|
||||
var import_core = require("@tiptap/core");
|
||||
var import_state = require("@tiptap/pm/state");
|
||||
var import_tables = require("@tiptap/pm/tables");
|
||||
function combineDOMRects(rect1, rect2) {
|
||||
const top = Math.min(rect1.top, rect2.top);
|
||||
const bottom = Math.max(rect1.bottom, rect2.bottom);
|
||||
const left = Math.min(rect1.left, rect2.left);
|
||||
const right = Math.max(rect1.right, rect2.right);
|
||||
const width = right - left;
|
||||
const height = bottom - top;
|
||||
const x = left;
|
||||
const y = top;
|
||||
return new DOMRect(x, y, width, height);
|
||||
}
|
||||
var BubbleMenuView = class {
|
||||
constructor({
|
||||
editor,
|
||||
element,
|
||||
view,
|
||||
updateDelay = 250,
|
||||
resizeDelay = 60,
|
||||
shouldShow,
|
||||
appendTo,
|
||||
getReferencedVirtualElement,
|
||||
options
|
||||
}) {
|
||||
this.preventHide = false;
|
||||
this.isVisible = false;
|
||||
this.scrollTarget = window;
|
||||
this.floatingUIOptions = {
|
||||
strategy: "absolute",
|
||||
placement: "top",
|
||||
offset: 8,
|
||||
flip: {},
|
||||
shift: {},
|
||||
arrow: false,
|
||||
size: false,
|
||||
autoPlacement: false,
|
||||
hide: false,
|
||||
inline: false,
|
||||
onShow: void 0,
|
||||
onHide: void 0,
|
||||
onUpdate: void 0,
|
||||
onDestroy: void 0
|
||||
};
|
||||
this.shouldShow = ({ view, state, from, to }) => {
|
||||
const { doc, selection } = state;
|
||||
const { empty } = selection;
|
||||
const isEmptyTextBlock = !doc.textBetween(from, to).length && (0, import_core.isTextSelection)(state.selection);
|
||||
const isChildOfMenu = this.element.contains(document.activeElement);
|
||||
const hasEditorFocus = view.hasFocus() || isChildOfMenu;
|
||||
if (!hasEditorFocus || empty || isEmptyTextBlock || !this.editor.isEditable) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
this.mousedownHandler = () => {
|
||||
this.preventHide = true;
|
||||
};
|
||||
this.dragstartHandler = () => {
|
||||
this.hide();
|
||||
};
|
||||
/**
|
||||
* Handles the window resize event to update the position of the bubble menu.
|
||||
* It uses a debounce mechanism to prevent excessive updates.
|
||||
* The delay is defined by the `resizeDelay` property.
|
||||
*/
|
||||
this.resizeHandler = () => {
|
||||
if (this.resizeDebounceTimer) {
|
||||
clearTimeout(this.resizeDebounceTimer);
|
||||
}
|
||||
this.resizeDebounceTimer = window.setTimeout(() => {
|
||||
this.updatePosition();
|
||||
}, this.resizeDelay);
|
||||
};
|
||||
this.focusHandler = () => {
|
||||
setTimeout(() => this.update(this.editor.view));
|
||||
};
|
||||
this.blurHandler = ({ event }) => {
|
||||
var _a;
|
||||
if (this.editor.isDestroyed) {
|
||||
this.destroy();
|
||||
return;
|
||||
}
|
||||
if (this.preventHide) {
|
||||
this.preventHide = false;
|
||||
return;
|
||||
}
|
||||
if ((event == null ? void 0 : event.relatedTarget) && ((_a = this.element.parentNode) == null ? void 0 : _a.contains(event.relatedTarget))) {
|
||||
return;
|
||||
}
|
||||
if ((event == null ? void 0 : event.relatedTarget) === this.editor.view.dom) {
|
||||
return;
|
||||
}
|
||||
this.hide();
|
||||
};
|
||||
this.handleDebouncedUpdate = (view, oldState) => {
|
||||
const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
|
||||
const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
|
||||
if (!selectionChanged && !docChanged) {
|
||||
return;
|
||||
}
|
||||
if (this.updateDebounceTimer) {
|
||||
clearTimeout(this.updateDebounceTimer);
|
||||
}
|
||||
this.updateDebounceTimer = window.setTimeout(() => {
|
||||
this.updateHandler(view, selectionChanged, docChanged, oldState);
|
||||
}, this.updateDelay);
|
||||
};
|
||||
this.updateHandler = (view, selectionChanged, docChanged, oldState) => {
|
||||
const { composing } = view;
|
||||
const isSame = !selectionChanged && !docChanged;
|
||||
if (composing || isSame) {
|
||||
return;
|
||||
}
|
||||
const shouldShow = this.getShouldShow(oldState);
|
||||
if (!shouldShow) {
|
||||
this.hide();
|
||||
return;
|
||||
}
|
||||
this.updatePosition();
|
||||
this.show();
|
||||
};
|
||||
this.transactionHandler = ({ transaction: tr }) => {
|
||||
const meta = tr.getMeta("bubbleMenu");
|
||||
if (meta === "updatePosition") {
|
||||
this.updatePosition();
|
||||
} else if (meta && typeof meta === "object" && meta.type === "updateOptions") {
|
||||
this.updateOptions(meta.options);
|
||||
}
|
||||
};
|
||||
var _a;
|
||||
this.editor = editor;
|
||||
this.element = element;
|
||||
this.view = view;
|
||||
this.updateDelay = updateDelay;
|
||||
this.resizeDelay = resizeDelay;
|
||||
this.appendTo = appendTo;
|
||||
this.scrollTarget = (_a = options == null ? void 0 : options.scrollTarget) != null ? _a : window;
|
||||
this.getReferencedVirtualElement = getReferencedVirtualElement;
|
||||
this.floatingUIOptions = {
|
||||
...this.floatingUIOptions,
|
||||
...options
|
||||
};
|
||||
this.element.tabIndex = 0;
|
||||
if (shouldShow) {
|
||||
this.shouldShow = shouldShow;
|
||||
}
|
||||
this.element.addEventListener("mousedown", this.mousedownHandler, { capture: true });
|
||||
this.view.dom.addEventListener("dragstart", this.dragstartHandler);
|
||||
this.editor.on("focus", this.focusHandler);
|
||||
this.editor.on("blur", this.blurHandler);
|
||||
this.editor.on("transaction", this.transactionHandler);
|
||||
window.addEventListener("resize", this.resizeHandler);
|
||||
this.scrollTarget.addEventListener("scroll", this.resizeHandler);
|
||||
this.update(view, view.state);
|
||||
if (this.getShouldShow()) {
|
||||
this.show();
|
||||
this.updatePosition();
|
||||
}
|
||||
}
|
||||
get middlewares() {
|
||||
const middlewares = [];
|
||||
if (this.floatingUIOptions.flip) {
|
||||
middlewares.push((0, import_dom.flip)(typeof this.floatingUIOptions.flip !== "boolean" ? this.floatingUIOptions.flip : void 0));
|
||||
}
|
||||
if (this.floatingUIOptions.shift) {
|
||||
middlewares.push(
|
||||
(0, import_dom.shift)(typeof this.floatingUIOptions.shift !== "boolean" ? this.floatingUIOptions.shift : void 0)
|
||||
);
|
||||
}
|
||||
if (this.floatingUIOptions.offset) {
|
||||
middlewares.push(
|
||||
(0, import_dom.offset)(typeof this.floatingUIOptions.offset !== "boolean" ? this.floatingUIOptions.offset : void 0)
|
||||
);
|
||||
}
|
||||
if (this.floatingUIOptions.arrow) {
|
||||
middlewares.push((0, import_dom.arrow)(this.floatingUIOptions.arrow));
|
||||
}
|
||||
if (this.floatingUIOptions.size) {
|
||||
middlewares.push((0, import_dom.size)(typeof this.floatingUIOptions.size !== "boolean" ? this.floatingUIOptions.size : void 0));
|
||||
}
|
||||
if (this.floatingUIOptions.autoPlacement) {
|
||||
middlewares.push(
|
||||
(0, import_dom.autoPlacement)(
|
||||
typeof this.floatingUIOptions.autoPlacement !== "boolean" ? this.floatingUIOptions.autoPlacement : void 0
|
||||
)
|
||||
);
|
||||
}
|
||||
if (this.floatingUIOptions.hide) {
|
||||
middlewares.push((0, import_dom.hide)(typeof this.floatingUIOptions.hide !== "boolean" ? this.floatingUIOptions.hide : void 0));
|
||||
}
|
||||
if (this.floatingUIOptions.inline) {
|
||||
middlewares.push(
|
||||
(0, import_dom.inline)(typeof this.floatingUIOptions.inline !== "boolean" ? this.floatingUIOptions.inline : void 0)
|
||||
);
|
||||
}
|
||||
return middlewares;
|
||||
}
|
||||
get virtualElement() {
|
||||
var _a, _b, _c;
|
||||
const { selection } = this.editor.state;
|
||||
const referencedVirtualElement = (_a = this.getReferencedVirtualElement) == null ? void 0 : _a.call(this);
|
||||
if (referencedVirtualElement) {
|
||||
return referencedVirtualElement;
|
||||
}
|
||||
if (!((_c = (_b = this.view) == null ? void 0 : _b.dom) == null ? void 0 : _c.parentNode)) {
|
||||
return;
|
||||
}
|
||||
const domRect = (0, import_core.posToDOMRect)(this.view, selection.from, selection.to);
|
||||
let virtualElement = {
|
||||
getBoundingClientRect: () => domRect,
|
||||
getClientRects: () => [domRect]
|
||||
};
|
||||
if (selection instanceof import_state.NodeSelection) {
|
||||
let node = this.view.nodeDOM(selection.from);
|
||||
const nodeViewWrapper = node.dataset.nodeViewWrapper ? node : node.querySelector("[data-node-view-wrapper]");
|
||||
if (nodeViewWrapper) {
|
||||
node = nodeViewWrapper;
|
||||
}
|
||||
if (node) {
|
||||
virtualElement = {
|
||||
getBoundingClientRect: () => node.getBoundingClientRect(),
|
||||
getClientRects: () => [node.getBoundingClientRect()]
|
||||
};
|
||||
}
|
||||
}
|
||||
if (selection instanceof import_tables.CellSelection) {
|
||||
const { $anchorCell, $headCell } = selection;
|
||||
const from = $anchorCell ? $anchorCell.pos : $headCell.pos;
|
||||
const to = $headCell ? $headCell.pos : $anchorCell.pos;
|
||||
const fromDOM = this.view.nodeDOM(from);
|
||||
const toDOM = this.view.nodeDOM(to);
|
||||
if (!fromDOM || !toDOM) {
|
||||
return;
|
||||
}
|
||||
const clientRect = fromDOM === toDOM ? fromDOM.getBoundingClientRect() : combineDOMRects(
|
||||
fromDOM.getBoundingClientRect(),
|
||||
toDOM.getBoundingClientRect()
|
||||
);
|
||||
virtualElement = {
|
||||
getBoundingClientRect: () => clientRect,
|
||||
getClientRects: () => [clientRect]
|
||||
};
|
||||
}
|
||||
return virtualElement;
|
||||
}
|
||||
updatePosition() {
|
||||
const virtualElement = this.virtualElement;
|
||||
if (!virtualElement) {
|
||||
return;
|
||||
}
|
||||
(0, import_dom.computePosition)(virtualElement, this.element, {
|
||||
placement: this.floatingUIOptions.placement,
|
||||
strategy: this.floatingUIOptions.strategy,
|
||||
middleware: this.middlewares
|
||||
}).then(({ x, y, strategy, middlewareData }) => {
|
||||
var _a, _b;
|
||||
if (((_a = middlewareData.hide) == null ? void 0 : _a.referenceHidden) || ((_b = middlewareData.hide) == null ? void 0 : _b.escaped)) {
|
||||
this.element.style.visibility = "hidden";
|
||||
return;
|
||||
}
|
||||
this.element.style.visibility = "visible";
|
||||
this.element.style.width = "max-content";
|
||||
this.element.style.position = strategy;
|
||||
this.element.style.left = `${x}px`;
|
||||
this.element.style.top = `${y}px`;
|
||||
if (this.isVisible && this.floatingUIOptions.onUpdate) {
|
||||
this.floatingUIOptions.onUpdate();
|
||||
}
|
||||
});
|
||||
}
|
||||
update(view, oldState) {
|
||||
const { state } = view;
|
||||
const hasValidSelection = state.selection.from !== state.selection.to;
|
||||
if (this.updateDelay > 0 && hasValidSelection) {
|
||||
this.handleDebouncedUpdate(view, oldState);
|
||||
return;
|
||||
}
|
||||
const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
|
||||
const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
|
||||
this.updateHandler(view, selectionChanged, docChanged, oldState);
|
||||
}
|
||||
getShouldShow(oldState) {
|
||||
var _a;
|
||||
const { state } = this.view;
|
||||
const { selection } = state;
|
||||
const { ranges } = selection;
|
||||
const from = Math.min(...ranges.map((range) => range.$from.pos));
|
||||
const to = Math.max(...ranges.map((range) => range.$to.pos));
|
||||
const shouldShow = (_a = this.shouldShow) == null ? void 0 : _a.call(this, {
|
||||
editor: this.editor,
|
||||
element: this.element,
|
||||
view: this.view,
|
||||
state,
|
||||
oldState,
|
||||
from,
|
||||
to
|
||||
});
|
||||
return shouldShow || false;
|
||||
}
|
||||
show() {
|
||||
var _a;
|
||||
if (this.isVisible) {
|
||||
return;
|
||||
}
|
||||
this.element.style.visibility = "visible";
|
||||
this.element.style.opacity = "1";
|
||||
const appendToElement = typeof this.appendTo === "function" ? this.appendTo() : this.appendTo;
|
||||
(_a = appendToElement != null ? appendToElement : this.view.dom.parentElement) == null ? void 0 : _a.appendChild(this.element);
|
||||
if (this.floatingUIOptions.onShow) {
|
||||
this.floatingUIOptions.onShow();
|
||||
}
|
||||
this.isVisible = true;
|
||||
}
|
||||
hide() {
|
||||
if (!this.isVisible) {
|
||||
return;
|
||||
}
|
||||
this.element.style.visibility = "hidden";
|
||||
this.element.style.opacity = "0";
|
||||
this.element.remove();
|
||||
if (this.floatingUIOptions.onHide) {
|
||||
this.floatingUIOptions.onHide();
|
||||
}
|
||||
this.isVisible = false;
|
||||
}
|
||||
updateOptions(newProps) {
|
||||
var _a;
|
||||
if (newProps.updateDelay !== void 0) {
|
||||
this.updateDelay = newProps.updateDelay;
|
||||
}
|
||||
if (newProps.resizeDelay !== void 0) {
|
||||
this.resizeDelay = newProps.resizeDelay;
|
||||
}
|
||||
if (newProps.appendTo !== void 0) {
|
||||
this.appendTo = newProps.appendTo;
|
||||
}
|
||||
if (newProps.getReferencedVirtualElement !== void 0) {
|
||||
this.getReferencedVirtualElement = newProps.getReferencedVirtualElement;
|
||||
}
|
||||
if (newProps.shouldShow !== void 0) {
|
||||
if (newProps.shouldShow) {
|
||||
this.shouldShow = newProps.shouldShow;
|
||||
}
|
||||
}
|
||||
if (newProps.options !== void 0) {
|
||||
const newScrollTarget = (_a = newProps.options.scrollTarget) != null ? _a : window;
|
||||
if (newScrollTarget !== this.scrollTarget) {
|
||||
this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
|
||||
this.scrollTarget = newScrollTarget;
|
||||
this.scrollTarget.addEventListener("scroll", this.resizeHandler);
|
||||
}
|
||||
this.floatingUIOptions = {
|
||||
...this.floatingUIOptions,
|
||||
...newProps.options
|
||||
};
|
||||
}
|
||||
}
|
||||
destroy() {
|
||||
this.hide();
|
||||
this.element.removeEventListener("mousedown", this.mousedownHandler, { capture: true });
|
||||
this.view.dom.removeEventListener("dragstart", this.dragstartHandler);
|
||||
window.removeEventListener("resize", this.resizeHandler);
|
||||
this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
|
||||
this.editor.off("focus", this.focusHandler);
|
||||
this.editor.off("blur", this.blurHandler);
|
||||
this.editor.off("transaction", this.transactionHandler);
|
||||
if (this.floatingUIOptions.onDestroy) {
|
||||
this.floatingUIOptions.onDestroy();
|
||||
}
|
||||
}
|
||||
};
|
||||
var BubbleMenuPlugin = (options) => {
|
||||
return new import_state.Plugin({
|
||||
key: typeof options.pluginKey === "string" ? new import_state.PluginKey(options.pluginKey) : options.pluginKey,
|
||||
view: (view) => new BubbleMenuView({ view, ...options })
|
||||
});
|
||||
};
|
||||
|
||||
// src/bubble-menu.ts
|
||||
var BubbleMenu = import_core2.Extension.create({
|
||||
name: "bubbleMenu",
|
||||
addOptions() {
|
||||
return {
|
||||
element: null,
|
||||
pluginKey: "bubbleMenu",
|
||||
updateDelay: void 0,
|
||||
appendTo: void 0,
|
||||
shouldShow: null
|
||||
};
|
||||
},
|
||||
addProseMirrorPlugins() {
|
||||
if (!this.options.element) {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
BubbleMenuPlugin({
|
||||
pluginKey: this.options.pluginKey,
|
||||
editor: this.editor,
|
||||
element: this.options.element,
|
||||
updateDelay: this.options.updateDelay,
|
||||
options: this.options.options,
|
||||
appendTo: this.options.appendTo,
|
||||
getReferencedVirtualElement: this.options.getReferencedVirtualElement,
|
||||
shouldShow: this.options.shouldShow
|
||||
})
|
||||
];
|
||||
}
|
||||
});
|
||||
|
||||
// src/index.ts
|
||||
var index_default = BubbleMenu;
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
BubbleMenu,
|
||||
BubbleMenuPlugin,
|
||||
BubbleMenuView
|
||||
});
|
||||
//# sourceMappingURL=index.cjs.map
|
||||
+1
File diff suppressed because one or more lines are too long
+162
@@ -0,0 +1,162 @@
|
||||
import { Editor, Extension } from '@tiptap/core';
|
||||
import * as _floating_ui_dom from '@floating-ui/dom';
|
||||
import { VirtualElement, offset, flip, shift, arrow, size, autoPlacement, hide, inline } from '@floating-ui/dom';
|
||||
import { PluginKey, EditorState, PluginView, Transaction, Plugin } from '@tiptap/pm/state';
|
||||
import { EditorView } from '@tiptap/pm/view';
|
||||
|
||||
interface BubbleMenuPluginProps {
|
||||
/**
|
||||
* The plugin key.
|
||||
* @type {PluginKey | string}
|
||||
* @default 'bubbleMenu'
|
||||
*/
|
||||
pluginKey: PluginKey | string;
|
||||
/**
|
||||
* The editor instance.
|
||||
*/
|
||||
editor: Editor;
|
||||
/**
|
||||
* The DOM element that contains your menu.
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
element: HTMLElement;
|
||||
/**
|
||||
* The delay in milliseconds before the menu should be updated.
|
||||
* This can be useful to prevent performance issues.
|
||||
* @type {number}
|
||||
* @default 250
|
||||
*/
|
||||
updateDelay?: number;
|
||||
/**
|
||||
* The delay in milliseconds before the menu position should be updated on window resize.
|
||||
* This can be useful to prevent performance issues.
|
||||
* @type {number}
|
||||
* @default 60
|
||||
*/
|
||||
resizeDelay?: number;
|
||||
/**
|
||||
* A function that determines whether the menu should be shown or not.
|
||||
* If this function returns `false`, the menu will be hidden, otherwise it will be shown.
|
||||
*/
|
||||
shouldShow?: ((props: {
|
||||
editor: Editor;
|
||||
element: HTMLElement;
|
||||
view: EditorView;
|
||||
state: EditorState;
|
||||
oldState?: EditorState;
|
||||
from: number;
|
||||
to: number;
|
||||
}) => boolean) | null;
|
||||
/**
|
||||
* The DOM element to append your menu to. Default is the editor's parent element.
|
||||
*
|
||||
* Sometimes the menu needs to be appended to a different DOM context due to accessibility, clipping, or z-index issues.
|
||||
*
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
appendTo?: HTMLElement | (() => HTMLElement);
|
||||
/**
|
||||
* A function that returns the virtual element for the menu.
|
||||
* This is useful when the menu needs to be positioned relative to a specific DOM element.
|
||||
* @type {() => VirtualElement | null}
|
||||
* @default Position based on the selection.
|
||||
*/
|
||||
getReferencedVirtualElement?: () => VirtualElement | null;
|
||||
/**
|
||||
* The options for the bubble menu. Those are passed to Floating UI and include options for the placement, offset, flip, shift, arrow, size, autoPlacement,
|
||||
* hide, and inline middlewares.
|
||||
* @default {}
|
||||
* @see https://floating-ui.com/docs/computePosition#options
|
||||
*/
|
||||
options?: {
|
||||
strategy?: 'absolute' | 'fixed';
|
||||
placement?: 'top' | 'right' | 'bottom' | 'left' | 'top-start' | 'top-end' | 'right-start' | 'right-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end';
|
||||
offset?: Parameters<typeof offset>[0] | boolean;
|
||||
flip?: Parameters<typeof flip>[0] | boolean;
|
||||
shift?: Parameters<typeof shift>[0] | boolean;
|
||||
arrow?: Parameters<typeof arrow>[0] | false;
|
||||
size?: Parameters<typeof size>[0] | boolean;
|
||||
autoPlacement?: Parameters<typeof autoPlacement>[0] | boolean;
|
||||
hide?: Parameters<typeof hide>[0] | boolean;
|
||||
inline?: Parameters<typeof inline>[0] | boolean;
|
||||
onShow?: () => void;
|
||||
onHide?: () => void;
|
||||
onUpdate?: () => void;
|
||||
onDestroy?: () => void;
|
||||
/**
|
||||
* The scrollable element that should be listened to when updating the position of the bubble menu.
|
||||
* If not provided, the window will be used.
|
||||
* @type {HTMLElement | Window}
|
||||
*/
|
||||
scrollTarget?: HTMLElement | Window;
|
||||
};
|
||||
}
|
||||
type BubbleMenuViewProps = BubbleMenuPluginProps & {
|
||||
view: EditorView;
|
||||
};
|
||||
declare class BubbleMenuView implements PluginView {
|
||||
editor: Editor;
|
||||
element: HTMLElement;
|
||||
view: EditorView;
|
||||
preventHide: boolean;
|
||||
updateDelay: number;
|
||||
resizeDelay: number;
|
||||
appendTo: HTMLElement | (() => HTMLElement) | undefined;
|
||||
getReferencedVirtualElement: (() => VirtualElement | null) | undefined;
|
||||
private updateDebounceTimer;
|
||||
private resizeDebounceTimer;
|
||||
private isVisible;
|
||||
private scrollTarget;
|
||||
private floatingUIOptions;
|
||||
shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null>;
|
||||
get middlewares(): {
|
||||
options?: any;
|
||||
name: string;
|
||||
fn: (state: _floating_ui_dom.MiddlewareState) => _floating_ui_dom.MiddlewareReturn | Promise<_floating_ui_dom.MiddlewareReturn>;
|
||||
}[];
|
||||
private get virtualElement();
|
||||
constructor({ editor, element, view, updateDelay, resizeDelay, shouldShow, appendTo, getReferencedVirtualElement, options, }: BubbleMenuViewProps);
|
||||
mousedownHandler: () => void;
|
||||
dragstartHandler: () => void;
|
||||
/**
|
||||
* Handles the window resize event to update the position of the bubble menu.
|
||||
* It uses a debounce mechanism to prevent excessive updates.
|
||||
* The delay is defined by the `resizeDelay` property.
|
||||
*/
|
||||
resizeHandler: () => void;
|
||||
focusHandler: () => void;
|
||||
blurHandler: ({ event }: {
|
||||
event: FocusEvent;
|
||||
}) => void;
|
||||
updatePosition(): void;
|
||||
update(view: EditorView, oldState?: EditorState): void;
|
||||
handleDebouncedUpdate: (view: EditorView, oldState?: EditorState) => void;
|
||||
getShouldShow(oldState?: EditorState): boolean;
|
||||
updateHandler: (view: EditorView, selectionChanged: boolean, docChanged: boolean, oldState?: EditorState) => void;
|
||||
show(): void;
|
||||
hide(): void;
|
||||
transactionHandler: ({ transaction: tr }: {
|
||||
transaction: Transaction;
|
||||
}) => void;
|
||||
updateOptions(newProps: Partial<Omit<BubbleMenuPluginProps, 'editor' | 'element' | 'pluginKey'>>): void;
|
||||
destroy(): void;
|
||||
}
|
||||
declare const BubbleMenuPlugin: (options: BubbleMenuPluginProps) => Plugin<any>;
|
||||
|
||||
type BubbleMenuOptions = Omit<BubbleMenuPluginProps, 'editor' | 'element'> & {
|
||||
/**
|
||||
* The DOM element that contains your menu.
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
element: HTMLElement | null;
|
||||
};
|
||||
/**
|
||||
* This extension allows you to create a bubble menu.
|
||||
* @see https://tiptap.dev/api/extensions/bubble-menu
|
||||
*/
|
||||
declare const BubbleMenu: Extension<BubbleMenuOptions, any>;
|
||||
|
||||
export { BubbleMenu, type BubbleMenuOptions, BubbleMenuPlugin, type BubbleMenuPluginProps, BubbleMenuView, type BubbleMenuViewProps, BubbleMenu as default };
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
import { Editor, Extension } from '@tiptap/core';
|
||||
import * as _floating_ui_dom from '@floating-ui/dom';
|
||||
import { VirtualElement, offset, flip, shift, arrow, size, autoPlacement, hide, inline } from '@floating-ui/dom';
|
||||
import { PluginKey, EditorState, PluginView, Transaction, Plugin } from '@tiptap/pm/state';
|
||||
import { EditorView } from '@tiptap/pm/view';
|
||||
|
||||
interface BubbleMenuPluginProps {
|
||||
/**
|
||||
* The plugin key.
|
||||
* @type {PluginKey | string}
|
||||
* @default 'bubbleMenu'
|
||||
*/
|
||||
pluginKey: PluginKey | string;
|
||||
/**
|
||||
* The editor instance.
|
||||
*/
|
||||
editor: Editor;
|
||||
/**
|
||||
* The DOM element that contains your menu.
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
element: HTMLElement;
|
||||
/**
|
||||
* The delay in milliseconds before the menu should be updated.
|
||||
* This can be useful to prevent performance issues.
|
||||
* @type {number}
|
||||
* @default 250
|
||||
*/
|
||||
updateDelay?: number;
|
||||
/**
|
||||
* The delay in milliseconds before the menu position should be updated on window resize.
|
||||
* This can be useful to prevent performance issues.
|
||||
* @type {number}
|
||||
* @default 60
|
||||
*/
|
||||
resizeDelay?: number;
|
||||
/**
|
||||
* A function that determines whether the menu should be shown or not.
|
||||
* If this function returns `false`, the menu will be hidden, otherwise it will be shown.
|
||||
*/
|
||||
shouldShow?: ((props: {
|
||||
editor: Editor;
|
||||
element: HTMLElement;
|
||||
view: EditorView;
|
||||
state: EditorState;
|
||||
oldState?: EditorState;
|
||||
from: number;
|
||||
to: number;
|
||||
}) => boolean) | null;
|
||||
/**
|
||||
* The DOM element to append your menu to. Default is the editor's parent element.
|
||||
*
|
||||
* Sometimes the menu needs to be appended to a different DOM context due to accessibility, clipping, or z-index issues.
|
||||
*
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
appendTo?: HTMLElement | (() => HTMLElement);
|
||||
/**
|
||||
* A function that returns the virtual element for the menu.
|
||||
* This is useful when the menu needs to be positioned relative to a specific DOM element.
|
||||
* @type {() => VirtualElement | null}
|
||||
* @default Position based on the selection.
|
||||
*/
|
||||
getReferencedVirtualElement?: () => VirtualElement | null;
|
||||
/**
|
||||
* The options for the bubble menu. Those are passed to Floating UI and include options for the placement, offset, flip, shift, arrow, size, autoPlacement,
|
||||
* hide, and inline middlewares.
|
||||
* @default {}
|
||||
* @see https://floating-ui.com/docs/computePosition#options
|
||||
*/
|
||||
options?: {
|
||||
strategy?: 'absolute' | 'fixed';
|
||||
placement?: 'top' | 'right' | 'bottom' | 'left' | 'top-start' | 'top-end' | 'right-start' | 'right-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end';
|
||||
offset?: Parameters<typeof offset>[0] | boolean;
|
||||
flip?: Parameters<typeof flip>[0] | boolean;
|
||||
shift?: Parameters<typeof shift>[0] | boolean;
|
||||
arrow?: Parameters<typeof arrow>[0] | false;
|
||||
size?: Parameters<typeof size>[0] | boolean;
|
||||
autoPlacement?: Parameters<typeof autoPlacement>[0] | boolean;
|
||||
hide?: Parameters<typeof hide>[0] | boolean;
|
||||
inline?: Parameters<typeof inline>[0] | boolean;
|
||||
onShow?: () => void;
|
||||
onHide?: () => void;
|
||||
onUpdate?: () => void;
|
||||
onDestroy?: () => void;
|
||||
/**
|
||||
* The scrollable element that should be listened to when updating the position of the bubble menu.
|
||||
* If not provided, the window will be used.
|
||||
* @type {HTMLElement | Window}
|
||||
*/
|
||||
scrollTarget?: HTMLElement | Window;
|
||||
};
|
||||
}
|
||||
type BubbleMenuViewProps = BubbleMenuPluginProps & {
|
||||
view: EditorView;
|
||||
};
|
||||
declare class BubbleMenuView implements PluginView {
|
||||
editor: Editor;
|
||||
element: HTMLElement;
|
||||
view: EditorView;
|
||||
preventHide: boolean;
|
||||
updateDelay: number;
|
||||
resizeDelay: number;
|
||||
appendTo: HTMLElement | (() => HTMLElement) | undefined;
|
||||
getReferencedVirtualElement: (() => VirtualElement | null) | undefined;
|
||||
private updateDebounceTimer;
|
||||
private resizeDebounceTimer;
|
||||
private isVisible;
|
||||
private scrollTarget;
|
||||
private floatingUIOptions;
|
||||
shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null>;
|
||||
get middlewares(): {
|
||||
options?: any;
|
||||
name: string;
|
||||
fn: (state: _floating_ui_dom.MiddlewareState) => _floating_ui_dom.MiddlewareReturn | Promise<_floating_ui_dom.MiddlewareReturn>;
|
||||
}[];
|
||||
private get virtualElement();
|
||||
constructor({ editor, element, view, updateDelay, resizeDelay, shouldShow, appendTo, getReferencedVirtualElement, options, }: BubbleMenuViewProps);
|
||||
mousedownHandler: () => void;
|
||||
dragstartHandler: () => void;
|
||||
/**
|
||||
* Handles the window resize event to update the position of the bubble menu.
|
||||
* It uses a debounce mechanism to prevent excessive updates.
|
||||
* The delay is defined by the `resizeDelay` property.
|
||||
*/
|
||||
resizeHandler: () => void;
|
||||
focusHandler: () => void;
|
||||
blurHandler: ({ event }: {
|
||||
event: FocusEvent;
|
||||
}) => void;
|
||||
updatePosition(): void;
|
||||
update(view: EditorView, oldState?: EditorState): void;
|
||||
handleDebouncedUpdate: (view: EditorView, oldState?: EditorState) => void;
|
||||
getShouldShow(oldState?: EditorState): boolean;
|
||||
updateHandler: (view: EditorView, selectionChanged: boolean, docChanged: boolean, oldState?: EditorState) => void;
|
||||
show(): void;
|
||||
hide(): void;
|
||||
transactionHandler: ({ transaction: tr }: {
|
||||
transaction: Transaction;
|
||||
}) => void;
|
||||
updateOptions(newProps: Partial<Omit<BubbleMenuPluginProps, 'editor' | 'element' | 'pluginKey'>>): void;
|
||||
destroy(): void;
|
||||
}
|
||||
declare const BubbleMenuPlugin: (options: BubbleMenuPluginProps) => Plugin<any>;
|
||||
|
||||
type BubbleMenuOptions = Omit<BubbleMenuPluginProps, 'editor' | 'element'> & {
|
||||
/**
|
||||
* The DOM element that contains your menu.
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
element: HTMLElement | null;
|
||||
};
|
||||
/**
|
||||
* This extension allows you to create a bubble menu.
|
||||
* @see https://tiptap.dev/api/extensions/bubble-menu
|
||||
*/
|
||||
declare const BubbleMenu: Extension<BubbleMenuOptions, any>;
|
||||
|
||||
export { BubbleMenu, type BubbleMenuOptions, BubbleMenuPlugin, type BubbleMenuPluginProps, BubbleMenuView, type BubbleMenuViewProps, BubbleMenu as default };
|
||||
+435
@@ -0,0 +1,435 @@
|
||||
// src/bubble-menu.ts
|
||||
import { Extension } from "@tiptap/core";
|
||||
|
||||
// src/bubble-menu-plugin.ts
|
||||
import {
|
||||
arrow,
|
||||
autoPlacement,
|
||||
computePosition,
|
||||
flip,
|
||||
hide,
|
||||
inline,
|
||||
offset,
|
||||
shift,
|
||||
size
|
||||
} from "@floating-ui/dom";
|
||||
import { isTextSelection, posToDOMRect } from "@tiptap/core";
|
||||
import { NodeSelection, Plugin, PluginKey } from "@tiptap/pm/state";
|
||||
import { CellSelection } from "@tiptap/pm/tables";
|
||||
function combineDOMRects(rect1, rect2) {
|
||||
const top = Math.min(rect1.top, rect2.top);
|
||||
const bottom = Math.max(rect1.bottom, rect2.bottom);
|
||||
const left = Math.min(rect1.left, rect2.left);
|
||||
const right = Math.max(rect1.right, rect2.right);
|
||||
const width = right - left;
|
||||
const height = bottom - top;
|
||||
const x = left;
|
||||
const y = top;
|
||||
return new DOMRect(x, y, width, height);
|
||||
}
|
||||
var BubbleMenuView = class {
|
||||
constructor({
|
||||
editor,
|
||||
element,
|
||||
view,
|
||||
updateDelay = 250,
|
||||
resizeDelay = 60,
|
||||
shouldShow,
|
||||
appendTo,
|
||||
getReferencedVirtualElement,
|
||||
options
|
||||
}) {
|
||||
this.preventHide = false;
|
||||
this.isVisible = false;
|
||||
this.scrollTarget = window;
|
||||
this.floatingUIOptions = {
|
||||
strategy: "absolute",
|
||||
placement: "top",
|
||||
offset: 8,
|
||||
flip: {},
|
||||
shift: {},
|
||||
arrow: false,
|
||||
size: false,
|
||||
autoPlacement: false,
|
||||
hide: false,
|
||||
inline: false,
|
||||
onShow: void 0,
|
||||
onHide: void 0,
|
||||
onUpdate: void 0,
|
||||
onDestroy: void 0
|
||||
};
|
||||
this.shouldShow = ({ view, state, from, to }) => {
|
||||
const { doc, selection } = state;
|
||||
const { empty } = selection;
|
||||
const isEmptyTextBlock = !doc.textBetween(from, to).length && isTextSelection(state.selection);
|
||||
const isChildOfMenu = this.element.contains(document.activeElement);
|
||||
const hasEditorFocus = view.hasFocus() || isChildOfMenu;
|
||||
if (!hasEditorFocus || empty || isEmptyTextBlock || !this.editor.isEditable) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
this.mousedownHandler = () => {
|
||||
this.preventHide = true;
|
||||
};
|
||||
this.dragstartHandler = () => {
|
||||
this.hide();
|
||||
};
|
||||
/**
|
||||
* Handles the window resize event to update the position of the bubble menu.
|
||||
* It uses a debounce mechanism to prevent excessive updates.
|
||||
* The delay is defined by the `resizeDelay` property.
|
||||
*/
|
||||
this.resizeHandler = () => {
|
||||
if (this.resizeDebounceTimer) {
|
||||
clearTimeout(this.resizeDebounceTimer);
|
||||
}
|
||||
this.resizeDebounceTimer = window.setTimeout(() => {
|
||||
this.updatePosition();
|
||||
}, this.resizeDelay);
|
||||
};
|
||||
this.focusHandler = () => {
|
||||
setTimeout(() => this.update(this.editor.view));
|
||||
};
|
||||
this.blurHandler = ({ event }) => {
|
||||
var _a;
|
||||
if (this.editor.isDestroyed) {
|
||||
this.destroy();
|
||||
return;
|
||||
}
|
||||
if (this.preventHide) {
|
||||
this.preventHide = false;
|
||||
return;
|
||||
}
|
||||
if ((event == null ? void 0 : event.relatedTarget) && ((_a = this.element.parentNode) == null ? void 0 : _a.contains(event.relatedTarget))) {
|
||||
return;
|
||||
}
|
||||
if ((event == null ? void 0 : event.relatedTarget) === this.editor.view.dom) {
|
||||
return;
|
||||
}
|
||||
this.hide();
|
||||
};
|
||||
this.handleDebouncedUpdate = (view, oldState) => {
|
||||
const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
|
||||
const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
|
||||
if (!selectionChanged && !docChanged) {
|
||||
return;
|
||||
}
|
||||
if (this.updateDebounceTimer) {
|
||||
clearTimeout(this.updateDebounceTimer);
|
||||
}
|
||||
this.updateDebounceTimer = window.setTimeout(() => {
|
||||
this.updateHandler(view, selectionChanged, docChanged, oldState);
|
||||
}, this.updateDelay);
|
||||
};
|
||||
this.updateHandler = (view, selectionChanged, docChanged, oldState) => {
|
||||
const { composing } = view;
|
||||
const isSame = !selectionChanged && !docChanged;
|
||||
if (composing || isSame) {
|
||||
return;
|
||||
}
|
||||
const shouldShow = this.getShouldShow(oldState);
|
||||
if (!shouldShow) {
|
||||
this.hide();
|
||||
return;
|
||||
}
|
||||
this.updatePosition();
|
||||
this.show();
|
||||
};
|
||||
this.transactionHandler = ({ transaction: tr }) => {
|
||||
const meta = tr.getMeta("bubbleMenu");
|
||||
if (meta === "updatePosition") {
|
||||
this.updatePosition();
|
||||
} else if (meta && typeof meta === "object" && meta.type === "updateOptions") {
|
||||
this.updateOptions(meta.options);
|
||||
}
|
||||
};
|
||||
var _a;
|
||||
this.editor = editor;
|
||||
this.element = element;
|
||||
this.view = view;
|
||||
this.updateDelay = updateDelay;
|
||||
this.resizeDelay = resizeDelay;
|
||||
this.appendTo = appendTo;
|
||||
this.scrollTarget = (_a = options == null ? void 0 : options.scrollTarget) != null ? _a : window;
|
||||
this.getReferencedVirtualElement = getReferencedVirtualElement;
|
||||
this.floatingUIOptions = {
|
||||
...this.floatingUIOptions,
|
||||
...options
|
||||
};
|
||||
this.element.tabIndex = 0;
|
||||
if (shouldShow) {
|
||||
this.shouldShow = shouldShow;
|
||||
}
|
||||
this.element.addEventListener("mousedown", this.mousedownHandler, { capture: true });
|
||||
this.view.dom.addEventListener("dragstart", this.dragstartHandler);
|
||||
this.editor.on("focus", this.focusHandler);
|
||||
this.editor.on("blur", this.blurHandler);
|
||||
this.editor.on("transaction", this.transactionHandler);
|
||||
window.addEventListener("resize", this.resizeHandler);
|
||||
this.scrollTarget.addEventListener("scroll", this.resizeHandler);
|
||||
this.update(view, view.state);
|
||||
if (this.getShouldShow()) {
|
||||
this.show();
|
||||
this.updatePosition();
|
||||
}
|
||||
}
|
||||
get middlewares() {
|
||||
const middlewares = [];
|
||||
if (this.floatingUIOptions.flip) {
|
||||
middlewares.push(flip(typeof this.floatingUIOptions.flip !== "boolean" ? this.floatingUIOptions.flip : void 0));
|
||||
}
|
||||
if (this.floatingUIOptions.shift) {
|
||||
middlewares.push(
|
||||
shift(typeof this.floatingUIOptions.shift !== "boolean" ? this.floatingUIOptions.shift : void 0)
|
||||
);
|
||||
}
|
||||
if (this.floatingUIOptions.offset) {
|
||||
middlewares.push(
|
||||
offset(typeof this.floatingUIOptions.offset !== "boolean" ? this.floatingUIOptions.offset : void 0)
|
||||
);
|
||||
}
|
||||
if (this.floatingUIOptions.arrow) {
|
||||
middlewares.push(arrow(this.floatingUIOptions.arrow));
|
||||
}
|
||||
if (this.floatingUIOptions.size) {
|
||||
middlewares.push(size(typeof this.floatingUIOptions.size !== "boolean" ? this.floatingUIOptions.size : void 0));
|
||||
}
|
||||
if (this.floatingUIOptions.autoPlacement) {
|
||||
middlewares.push(
|
||||
autoPlacement(
|
||||
typeof this.floatingUIOptions.autoPlacement !== "boolean" ? this.floatingUIOptions.autoPlacement : void 0
|
||||
)
|
||||
);
|
||||
}
|
||||
if (this.floatingUIOptions.hide) {
|
||||
middlewares.push(hide(typeof this.floatingUIOptions.hide !== "boolean" ? this.floatingUIOptions.hide : void 0));
|
||||
}
|
||||
if (this.floatingUIOptions.inline) {
|
||||
middlewares.push(
|
||||
inline(typeof this.floatingUIOptions.inline !== "boolean" ? this.floatingUIOptions.inline : void 0)
|
||||
);
|
||||
}
|
||||
return middlewares;
|
||||
}
|
||||
get virtualElement() {
|
||||
var _a, _b, _c;
|
||||
const { selection } = this.editor.state;
|
||||
const referencedVirtualElement = (_a = this.getReferencedVirtualElement) == null ? void 0 : _a.call(this);
|
||||
if (referencedVirtualElement) {
|
||||
return referencedVirtualElement;
|
||||
}
|
||||
if (!((_c = (_b = this.view) == null ? void 0 : _b.dom) == null ? void 0 : _c.parentNode)) {
|
||||
return;
|
||||
}
|
||||
const domRect = posToDOMRect(this.view, selection.from, selection.to);
|
||||
let virtualElement = {
|
||||
getBoundingClientRect: () => domRect,
|
||||
getClientRects: () => [domRect]
|
||||
};
|
||||
if (selection instanceof NodeSelection) {
|
||||
let node = this.view.nodeDOM(selection.from);
|
||||
const nodeViewWrapper = node.dataset.nodeViewWrapper ? node : node.querySelector("[data-node-view-wrapper]");
|
||||
if (nodeViewWrapper) {
|
||||
node = nodeViewWrapper;
|
||||
}
|
||||
if (node) {
|
||||
virtualElement = {
|
||||
getBoundingClientRect: () => node.getBoundingClientRect(),
|
||||
getClientRects: () => [node.getBoundingClientRect()]
|
||||
};
|
||||
}
|
||||
}
|
||||
if (selection instanceof CellSelection) {
|
||||
const { $anchorCell, $headCell } = selection;
|
||||
const from = $anchorCell ? $anchorCell.pos : $headCell.pos;
|
||||
const to = $headCell ? $headCell.pos : $anchorCell.pos;
|
||||
const fromDOM = this.view.nodeDOM(from);
|
||||
const toDOM = this.view.nodeDOM(to);
|
||||
if (!fromDOM || !toDOM) {
|
||||
return;
|
||||
}
|
||||
const clientRect = fromDOM === toDOM ? fromDOM.getBoundingClientRect() : combineDOMRects(
|
||||
fromDOM.getBoundingClientRect(),
|
||||
toDOM.getBoundingClientRect()
|
||||
);
|
||||
virtualElement = {
|
||||
getBoundingClientRect: () => clientRect,
|
||||
getClientRects: () => [clientRect]
|
||||
};
|
||||
}
|
||||
return virtualElement;
|
||||
}
|
||||
updatePosition() {
|
||||
const virtualElement = this.virtualElement;
|
||||
if (!virtualElement) {
|
||||
return;
|
||||
}
|
||||
computePosition(virtualElement, this.element, {
|
||||
placement: this.floatingUIOptions.placement,
|
||||
strategy: this.floatingUIOptions.strategy,
|
||||
middleware: this.middlewares
|
||||
}).then(({ x, y, strategy, middlewareData }) => {
|
||||
var _a, _b;
|
||||
if (((_a = middlewareData.hide) == null ? void 0 : _a.referenceHidden) || ((_b = middlewareData.hide) == null ? void 0 : _b.escaped)) {
|
||||
this.element.style.visibility = "hidden";
|
||||
return;
|
||||
}
|
||||
this.element.style.visibility = "visible";
|
||||
this.element.style.width = "max-content";
|
||||
this.element.style.position = strategy;
|
||||
this.element.style.left = `${x}px`;
|
||||
this.element.style.top = `${y}px`;
|
||||
if (this.isVisible && this.floatingUIOptions.onUpdate) {
|
||||
this.floatingUIOptions.onUpdate();
|
||||
}
|
||||
});
|
||||
}
|
||||
update(view, oldState) {
|
||||
const { state } = view;
|
||||
const hasValidSelection = state.selection.from !== state.selection.to;
|
||||
if (this.updateDelay > 0 && hasValidSelection) {
|
||||
this.handleDebouncedUpdate(view, oldState);
|
||||
return;
|
||||
}
|
||||
const selectionChanged = !(oldState == null ? void 0 : oldState.selection.eq(view.state.selection));
|
||||
const docChanged = !(oldState == null ? void 0 : oldState.doc.eq(view.state.doc));
|
||||
this.updateHandler(view, selectionChanged, docChanged, oldState);
|
||||
}
|
||||
getShouldShow(oldState) {
|
||||
var _a;
|
||||
const { state } = this.view;
|
||||
const { selection } = state;
|
||||
const { ranges } = selection;
|
||||
const from = Math.min(...ranges.map((range) => range.$from.pos));
|
||||
const to = Math.max(...ranges.map((range) => range.$to.pos));
|
||||
const shouldShow = (_a = this.shouldShow) == null ? void 0 : _a.call(this, {
|
||||
editor: this.editor,
|
||||
element: this.element,
|
||||
view: this.view,
|
||||
state,
|
||||
oldState,
|
||||
from,
|
||||
to
|
||||
});
|
||||
return shouldShow || false;
|
||||
}
|
||||
show() {
|
||||
var _a;
|
||||
if (this.isVisible) {
|
||||
return;
|
||||
}
|
||||
this.element.style.visibility = "visible";
|
||||
this.element.style.opacity = "1";
|
||||
const appendToElement = typeof this.appendTo === "function" ? this.appendTo() : this.appendTo;
|
||||
(_a = appendToElement != null ? appendToElement : this.view.dom.parentElement) == null ? void 0 : _a.appendChild(this.element);
|
||||
if (this.floatingUIOptions.onShow) {
|
||||
this.floatingUIOptions.onShow();
|
||||
}
|
||||
this.isVisible = true;
|
||||
}
|
||||
hide() {
|
||||
if (!this.isVisible) {
|
||||
return;
|
||||
}
|
||||
this.element.style.visibility = "hidden";
|
||||
this.element.style.opacity = "0";
|
||||
this.element.remove();
|
||||
if (this.floatingUIOptions.onHide) {
|
||||
this.floatingUIOptions.onHide();
|
||||
}
|
||||
this.isVisible = false;
|
||||
}
|
||||
updateOptions(newProps) {
|
||||
var _a;
|
||||
if (newProps.updateDelay !== void 0) {
|
||||
this.updateDelay = newProps.updateDelay;
|
||||
}
|
||||
if (newProps.resizeDelay !== void 0) {
|
||||
this.resizeDelay = newProps.resizeDelay;
|
||||
}
|
||||
if (newProps.appendTo !== void 0) {
|
||||
this.appendTo = newProps.appendTo;
|
||||
}
|
||||
if (newProps.getReferencedVirtualElement !== void 0) {
|
||||
this.getReferencedVirtualElement = newProps.getReferencedVirtualElement;
|
||||
}
|
||||
if (newProps.shouldShow !== void 0) {
|
||||
if (newProps.shouldShow) {
|
||||
this.shouldShow = newProps.shouldShow;
|
||||
}
|
||||
}
|
||||
if (newProps.options !== void 0) {
|
||||
const newScrollTarget = (_a = newProps.options.scrollTarget) != null ? _a : window;
|
||||
if (newScrollTarget !== this.scrollTarget) {
|
||||
this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
|
||||
this.scrollTarget = newScrollTarget;
|
||||
this.scrollTarget.addEventListener("scroll", this.resizeHandler);
|
||||
}
|
||||
this.floatingUIOptions = {
|
||||
...this.floatingUIOptions,
|
||||
...newProps.options
|
||||
};
|
||||
}
|
||||
}
|
||||
destroy() {
|
||||
this.hide();
|
||||
this.element.removeEventListener("mousedown", this.mousedownHandler, { capture: true });
|
||||
this.view.dom.removeEventListener("dragstart", this.dragstartHandler);
|
||||
window.removeEventListener("resize", this.resizeHandler);
|
||||
this.scrollTarget.removeEventListener("scroll", this.resizeHandler);
|
||||
this.editor.off("focus", this.focusHandler);
|
||||
this.editor.off("blur", this.blurHandler);
|
||||
this.editor.off("transaction", this.transactionHandler);
|
||||
if (this.floatingUIOptions.onDestroy) {
|
||||
this.floatingUIOptions.onDestroy();
|
||||
}
|
||||
}
|
||||
};
|
||||
var BubbleMenuPlugin = (options) => {
|
||||
return new Plugin({
|
||||
key: typeof options.pluginKey === "string" ? new PluginKey(options.pluginKey) : options.pluginKey,
|
||||
view: (view) => new BubbleMenuView({ view, ...options })
|
||||
});
|
||||
};
|
||||
|
||||
// src/bubble-menu.ts
|
||||
var BubbleMenu = Extension.create({
|
||||
name: "bubbleMenu",
|
||||
addOptions() {
|
||||
return {
|
||||
element: null,
|
||||
pluginKey: "bubbleMenu",
|
||||
updateDelay: void 0,
|
||||
appendTo: void 0,
|
||||
shouldShow: null
|
||||
};
|
||||
},
|
||||
addProseMirrorPlugins() {
|
||||
if (!this.options.element) {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
BubbleMenuPlugin({
|
||||
pluginKey: this.options.pluginKey,
|
||||
editor: this.editor,
|
||||
element: this.options.element,
|
||||
updateDelay: this.options.updateDelay,
|
||||
options: this.options.options,
|
||||
appendTo: this.options.appendTo,
|
||||
getReferencedVirtualElement: this.options.getReferencedVirtualElement,
|
||||
shouldShow: this.options.shouldShow
|
||||
})
|
||||
];
|
||||
}
|
||||
});
|
||||
|
||||
// src/index.ts
|
||||
var index_default = BubbleMenu;
|
||||
export {
|
||||
BubbleMenu,
|
||||
BubbleMenuPlugin,
|
||||
BubbleMenuView,
|
||||
index_default as default
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+54
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "@tiptap/extension-bubble-menu",
|
||||
"description": "bubble-menu extension for tiptap",
|
||||
"version": "3.19.0",
|
||||
"homepage": "https://tiptap.dev",
|
||||
"keywords": [
|
||||
"tiptap",
|
||||
"tiptap extension"
|
||||
],
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/ueberdosis"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": {
|
||||
"import": "./dist/index.d.ts",
|
||||
"require": "./dist/index.d.cts"
|
||||
},
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"main": "dist/index.cjs",
|
||||
"module": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ueberdosis/tiptap",
|
||||
"directory": "packages/extension-bubble-menu"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"devDependencies": {
|
||||
"@tiptap/core": "^3.19.0",
|
||||
"@tiptap/pm": "^3.19.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@floating-ui/dom": "^1.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@tiptap/core": "^3.19.0",
|
||||
"@tiptap/pm": "^3.19.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
||||
}
|
||||
}
|
||||
+640
@@ -0,0 +1,640 @@
|
||||
import {
|
||||
type Middleware,
|
||||
type VirtualElement,
|
||||
arrow,
|
||||
autoPlacement,
|
||||
computePosition,
|
||||
flip,
|
||||
hide,
|
||||
inline,
|
||||
offset,
|
||||
shift,
|
||||
size,
|
||||
} from '@floating-ui/dom'
|
||||
import type { Editor } from '@tiptap/core'
|
||||
import { isTextSelection, posToDOMRect } from '@tiptap/core'
|
||||
import type { EditorState, PluginView, Transaction } from '@tiptap/pm/state'
|
||||
import { NodeSelection, Plugin, PluginKey } from '@tiptap/pm/state'
|
||||
import { CellSelection } from '@tiptap/pm/tables'
|
||||
import type { EditorView } from '@tiptap/pm/view'
|
||||
|
||||
function combineDOMRects(rect1: DOMRect, rect2: DOMRect): DOMRect {
|
||||
const top = Math.min(rect1.top, rect2.top)
|
||||
const bottom = Math.max(rect1.bottom, rect2.bottom)
|
||||
const left = Math.min(rect1.left, rect2.left)
|
||||
const right = Math.max(rect1.right, rect2.right)
|
||||
const width = right - left
|
||||
const height = bottom - top
|
||||
const x = left
|
||||
const y = top
|
||||
return new DOMRect(x, y, width, height)
|
||||
}
|
||||
|
||||
export interface BubbleMenuPluginProps {
|
||||
/**
|
||||
* The plugin key.
|
||||
* @type {PluginKey | string}
|
||||
* @default 'bubbleMenu'
|
||||
*/
|
||||
pluginKey: PluginKey | string
|
||||
|
||||
/**
|
||||
* The editor instance.
|
||||
*/
|
||||
editor: Editor
|
||||
|
||||
/**
|
||||
* The DOM element that contains your menu.
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
element: HTMLElement
|
||||
|
||||
/**
|
||||
* The delay in milliseconds before the menu should be updated.
|
||||
* This can be useful to prevent performance issues.
|
||||
* @type {number}
|
||||
* @default 250
|
||||
*/
|
||||
updateDelay?: number
|
||||
|
||||
/**
|
||||
* The delay in milliseconds before the menu position should be updated on window resize.
|
||||
* This can be useful to prevent performance issues.
|
||||
* @type {number}
|
||||
* @default 60
|
||||
*/
|
||||
resizeDelay?: number
|
||||
|
||||
/**
|
||||
* A function that determines whether the menu should be shown or not.
|
||||
* If this function returns `false`, the menu will be hidden, otherwise it will be shown.
|
||||
*/
|
||||
shouldShow?:
|
||||
| ((props: {
|
||||
editor: Editor
|
||||
element: HTMLElement
|
||||
view: EditorView
|
||||
state: EditorState
|
||||
oldState?: EditorState
|
||||
from: number
|
||||
to: number
|
||||
}) => boolean)
|
||||
| null
|
||||
|
||||
/**
|
||||
* The DOM element to append your menu to. Default is the editor's parent element.
|
||||
*
|
||||
* Sometimes the menu needs to be appended to a different DOM context due to accessibility, clipping, or z-index issues.
|
||||
*
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
appendTo?: HTMLElement | (() => HTMLElement)
|
||||
|
||||
/**
|
||||
* A function that returns the virtual element for the menu.
|
||||
* This is useful when the menu needs to be positioned relative to a specific DOM element.
|
||||
* @type {() => VirtualElement | null}
|
||||
* @default Position based on the selection.
|
||||
*/
|
||||
getReferencedVirtualElement?: () => VirtualElement | null
|
||||
|
||||
/**
|
||||
* The options for the bubble menu. Those are passed to Floating UI and include options for the placement, offset, flip, shift, arrow, size, autoPlacement,
|
||||
* hide, and inline middlewares.
|
||||
* @default {}
|
||||
* @see https://floating-ui.com/docs/computePosition#options
|
||||
*/
|
||||
options?: {
|
||||
strategy?: 'absolute' | 'fixed'
|
||||
placement?:
|
||||
| 'top'
|
||||
| 'right'
|
||||
| 'bottom'
|
||||
| 'left'
|
||||
| 'top-start'
|
||||
| 'top-end'
|
||||
| 'right-start'
|
||||
| 'right-end'
|
||||
| 'bottom-start'
|
||||
| 'bottom-end'
|
||||
| 'left-start'
|
||||
| 'left-end'
|
||||
offset?: Parameters<typeof offset>[0] | boolean
|
||||
flip?: Parameters<typeof flip>[0] | boolean
|
||||
shift?: Parameters<typeof shift>[0] | boolean
|
||||
arrow?: Parameters<typeof arrow>[0] | false
|
||||
size?: Parameters<typeof size>[0] | boolean
|
||||
autoPlacement?: Parameters<typeof autoPlacement>[0] | boolean
|
||||
hide?: Parameters<typeof hide>[0] | boolean
|
||||
inline?: Parameters<typeof inline>[0] | boolean
|
||||
|
||||
onShow?: () => void
|
||||
onHide?: () => void
|
||||
onUpdate?: () => void
|
||||
onDestroy?: () => void
|
||||
|
||||
/**
|
||||
* The scrollable element that should be listened to when updating the position of the bubble menu.
|
||||
* If not provided, the window will be used.
|
||||
* @type {HTMLElement | Window}
|
||||
*/
|
||||
scrollTarget?: HTMLElement | Window
|
||||
}
|
||||
}
|
||||
|
||||
export type BubbleMenuViewProps = BubbleMenuPluginProps & {
|
||||
view: EditorView
|
||||
}
|
||||
|
||||
export class BubbleMenuView implements PluginView {
|
||||
public editor: Editor
|
||||
|
||||
public element: HTMLElement
|
||||
|
||||
public view: EditorView
|
||||
|
||||
public preventHide = false
|
||||
|
||||
public updateDelay: number
|
||||
|
||||
public resizeDelay: number
|
||||
|
||||
public appendTo: HTMLElement | (() => HTMLElement) | undefined
|
||||
|
||||
public getReferencedVirtualElement: (() => VirtualElement | null) | undefined
|
||||
|
||||
private updateDebounceTimer: number | undefined
|
||||
|
||||
private resizeDebounceTimer: number | undefined
|
||||
|
||||
private isVisible = false
|
||||
|
||||
private scrollTarget: HTMLElement | Window = window
|
||||
|
||||
private floatingUIOptions: NonNullable<BubbleMenuPluginProps['options']> = {
|
||||
strategy: 'absolute',
|
||||
placement: 'top',
|
||||
offset: 8,
|
||||
flip: {},
|
||||
shift: {},
|
||||
arrow: false,
|
||||
size: false,
|
||||
autoPlacement: false,
|
||||
hide: false,
|
||||
inline: false,
|
||||
onShow: undefined,
|
||||
onHide: undefined,
|
||||
onUpdate: undefined,
|
||||
onDestroy: undefined,
|
||||
}
|
||||
|
||||
public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({ view, state, from, to }) => {
|
||||
const { doc, selection } = state
|
||||
const { empty } = selection
|
||||
|
||||
// Sometime check for `empty` is not enough.
|
||||
// Doubleclick an empty paragraph returns a node size of 2.
|
||||
// So we check also for an empty text size.
|
||||
const isEmptyTextBlock = !doc.textBetween(from, to).length && isTextSelection(state.selection)
|
||||
|
||||
// When clicking on a element inside the bubble menu the editor "blur" event
|
||||
// is called and the bubble menu item is focussed. In this case we should
|
||||
// consider the menu as part of the editor and keep showing the menu
|
||||
const isChildOfMenu = this.element.contains(document.activeElement)
|
||||
|
||||
const hasEditorFocus = view.hasFocus() || isChildOfMenu
|
||||
|
||||
if (!hasEditorFocus || empty || isEmptyTextBlock || !this.editor.isEditable) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
get middlewares() {
|
||||
const middlewares: Middleware[] = []
|
||||
|
||||
if (this.floatingUIOptions.flip) {
|
||||
middlewares.push(flip(typeof this.floatingUIOptions.flip !== 'boolean' ? this.floatingUIOptions.flip : undefined))
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.shift) {
|
||||
middlewares.push(
|
||||
shift(typeof this.floatingUIOptions.shift !== 'boolean' ? this.floatingUIOptions.shift : undefined),
|
||||
)
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.offset) {
|
||||
middlewares.push(
|
||||
offset(typeof this.floatingUIOptions.offset !== 'boolean' ? this.floatingUIOptions.offset : undefined),
|
||||
)
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.arrow) {
|
||||
middlewares.push(arrow(this.floatingUIOptions.arrow))
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.size) {
|
||||
middlewares.push(size(typeof this.floatingUIOptions.size !== 'boolean' ? this.floatingUIOptions.size : undefined))
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.autoPlacement) {
|
||||
middlewares.push(
|
||||
autoPlacement(
|
||||
typeof this.floatingUIOptions.autoPlacement !== 'boolean' ? this.floatingUIOptions.autoPlacement : undefined,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.hide) {
|
||||
middlewares.push(hide(typeof this.floatingUIOptions.hide !== 'boolean' ? this.floatingUIOptions.hide : undefined))
|
||||
}
|
||||
|
||||
if (this.floatingUIOptions.inline) {
|
||||
middlewares.push(
|
||||
inline(typeof this.floatingUIOptions.inline !== 'boolean' ? this.floatingUIOptions.inline : undefined),
|
||||
)
|
||||
}
|
||||
|
||||
return middlewares
|
||||
}
|
||||
|
||||
private get virtualElement(): VirtualElement | undefined {
|
||||
const { selection } = this.editor.state
|
||||
|
||||
const referencedVirtualElement = this.getReferencedVirtualElement?.()
|
||||
if (referencedVirtualElement) {
|
||||
return referencedVirtualElement
|
||||
}
|
||||
|
||||
if (!this.view?.dom?.parentNode) {
|
||||
return
|
||||
}
|
||||
|
||||
const domRect = posToDOMRect(this.view, selection.from, selection.to)
|
||||
let virtualElement = {
|
||||
getBoundingClientRect: () => domRect,
|
||||
getClientRects: () => [domRect],
|
||||
}
|
||||
|
||||
if (selection instanceof NodeSelection) {
|
||||
let node = this.view.nodeDOM(selection.from) as HTMLElement
|
||||
|
||||
const nodeViewWrapper = node.dataset.nodeViewWrapper ? node : node.querySelector('[data-node-view-wrapper]')
|
||||
|
||||
if (nodeViewWrapper) {
|
||||
node = nodeViewWrapper as HTMLElement
|
||||
}
|
||||
|
||||
if (node) {
|
||||
virtualElement = {
|
||||
getBoundingClientRect: () => node.getBoundingClientRect(),
|
||||
getClientRects: () => [node.getBoundingClientRect()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this is a special case for cell selections
|
||||
if (selection instanceof CellSelection) {
|
||||
const { $anchorCell, $headCell } = selection
|
||||
|
||||
const from = $anchorCell ? $anchorCell.pos : $headCell!.pos
|
||||
const to = $headCell ? $headCell.pos : $anchorCell!.pos
|
||||
|
||||
const fromDOM = this.view.nodeDOM(from)
|
||||
const toDOM = this.view.nodeDOM(to)
|
||||
|
||||
if (!fromDOM || !toDOM) {
|
||||
return
|
||||
}
|
||||
|
||||
const clientRect =
|
||||
fromDOM === toDOM
|
||||
? (fromDOM as HTMLElement).getBoundingClientRect()
|
||||
: combineDOMRects(
|
||||
(fromDOM as HTMLElement).getBoundingClientRect(),
|
||||
(toDOM as HTMLElement).getBoundingClientRect(),
|
||||
)
|
||||
|
||||
virtualElement = {
|
||||
getBoundingClientRect: () => clientRect,
|
||||
getClientRects: () => [clientRect],
|
||||
}
|
||||
}
|
||||
|
||||
return virtualElement
|
||||
}
|
||||
|
||||
constructor({
|
||||
editor,
|
||||
element,
|
||||
view,
|
||||
updateDelay = 250,
|
||||
resizeDelay = 60,
|
||||
shouldShow,
|
||||
appendTo,
|
||||
getReferencedVirtualElement,
|
||||
options,
|
||||
}: BubbleMenuViewProps) {
|
||||
this.editor = editor
|
||||
this.element = element
|
||||
this.view = view
|
||||
this.updateDelay = updateDelay
|
||||
this.resizeDelay = resizeDelay
|
||||
this.appendTo = appendTo
|
||||
this.scrollTarget = options?.scrollTarget ?? window
|
||||
this.getReferencedVirtualElement = getReferencedVirtualElement
|
||||
|
||||
this.floatingUIOptions = {
|
||||
...this.floatingUIOptions,
|
||||
...options,
|
||||
}
|
||||
|
||||
this.element.tabIndex = 0
|
||||
|
||||
if (shouldShow) {
|
||||
this.shouldShow = shouldShow
|
||||
}
|
||||
|
||||
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
|
||||
this.view.dom.addEventListener('dragstart', this.dragstartHandler)
|
||||
this.editor.on('focus', this.focusHandler)
|
||||
this.editor.on('blur', this.blurHandler)
|
||||
this.editor.on('transaction', this.transactionHandler)
|
||||
window.addEventListener('resize', this.resizeHandler)
|
||||
this.scrollTarget.addEventListener('scroll', this.resizeHandler)
|
||||
|
||||
this.update(view, view.state)
|
||||
|
||||
if (this.getShouldShow()) {
|
||||
this.show()
|
||||
this.updatePosition()
|
||||
}
|
||||
}
|
||||
|
||||
mousedownHandler = () => {
|
||||
this.preventHide = true
|
||||
}
|
||||
|
||||
dragstartHandler = () => {
|
||||
this.hide()
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the window resize event to update the position of the bubble menu.
|
||||
* It uses a debounce mechanism to prevent excessive updates.
|
||||
* The delay is defined by the `resizeDelay` property.
|
||||
*/
|
||||
resizeHandler = () => {
|
||||
if (this.resizeDebounceTimer) {
|
||||
clearTimeout(this.resizeDebounceTimer)
|
||||
}
|
||||
|
||||
this.resizeDebounceTimer = window.setTimeout(() => {
|
||||
this.updatePosition()
|
||||
}, this.resizeDelay)
|
||||
}
|
||||
|
||||
focusHandler = () => {
|
||||
// we use `setTimeout` to make sure `selection` is already updated
|
||||
setTimeout(() => this.update(this.editor.view))
|
||||
}
|
||||
|
||||
blurHandler = ({ event }: { event: FocusEvent }) => {
|
||||
if (this.editor.isDestroyed) {
|
||||
this.destroy()
|
||||
return
|
||||
}
|
||||
|
||||
if (this.preventHide) {
|
||||
this.preventHide = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (event?.relatedTarget && this.element.parentNode?.contains(event.relatedTarget as Node)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (event?.relatedTarget === this.editor.view.dom) {
|
||||
return
|
||||
}
|
||||
|
||||
this.hide()
|
||||
}
|
||||
|
||||
updatePosition() {
|
||||
const virtualElement = this.virtualElement
|
||||
|
||||
if (!virtualElement) {
|
||||
return
|
||||
}
|
||||
|
||||
computePosition(virtualElement, this.element, {
|
||||
placement: this.floatingUIOptions.placement,
|
||||
strategy: this.floatingUIOptions.strategy,
|
||||
middleware: this.middlewares,
|
||||
}).then(({ x, y, strategy, middlewareData }) => {
|
||||
// Handle hide middleware - hide element if reference is hidden or element has escaped
|
||||
if (middlewareData.hide?.referenceHidden || middlewareData.hide?.escaped) {
|
||||
this.element.style.visibility = 'hidden'
|
||||
return
|
||||
}
|
||||
|
||||
this.element.style.visibility = 'visible'
|
||||
this.element.style.width = 'max-content'
|
||||
this.element.style.position = strategy
|
||||
this.element.style.left = `${x}px`
|
||||
this.element.style.top = `${y}px`
|
||||
|
||||
if (this.isVisible && this.floatingUIOptions.onUpdate) {
|
||||
this.floatingUIOptions.onUpdate()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
update(view: EditorView, oldState?: EditorState) {
|
||||
const { state } = view
|
||||
const hasValidSelection = state.selection.from !== state.selection.to
|
||||
|
||||
if (this.updateDelay > 0 && hasValidSelection) {
|
||||
this.handleDebouncedUpdate(view, oldState)
|
||||
return
|
||||
}
|
||||
|
||||
const selectionChanged = !oldState?.selection.eq(view.state.selection)
|
||||
const docChanged = !oldState?.doc.eq(view.state.doc)
|
||||
|
||||
this.updateHandler(view, selectionChanged, docChanged, oldState)
|
||||
}
|
||||
|
||||
handleDebouncedUpdate = (view: EditorView, oldState?: EditorState) => {
|
||||
const selectionChanged = !oldState?.selection.eq(view.state.selection)
|
||||
const docChanged = !oldState?.doc.eq(view.state.doc)
|
||||
|
||||
if (!selectionChanged && !docChanged) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.updateDebounceTimer) {
|
||||
clearTimeout(this.updateDebounceTimer)
|
||||
}
|
||||
|
||||
this.updateDebounceTimer = window.setTimeout(() => {
|
||||
this.updateHandler(view, selectionChanged, docChanged, oldState)
|
||||
}, this.updateDelay)
|
||||
}
|
||||
|
||||
getShouldShow(oldState?: EditorState) {
|
||||
const { state } = this.view
|
||||
const { selection } = state
|
||||
|
||||
// support for CellSelections
|
||||
const { ranges } = selection
|
||||
const from = Math.min(...ranges.map(range => range.$from.pos))
|
||||
const to = Math.max(...ranges.map(range => range.$to.pos))
|
||||
|
||||
const shouldShow = this.shouldShow?.({
|
||||
editor: this.editor,
|
||||
element: this.element,
|
||||
view: this.view,
|
||||
state,
|
||||
oldState,
|
||||
from,
|
||||
to,
|
||||
})
|
||||
|
||||
return shouldShow || false
|
||||
}
|
||||
|
||||
updateHandler = (view: EditorView, selectionChanged: boolean, docChanged: boolean, oldState?: EditorState) => {
|
||||
const { composing } = view
|
||||
|
||||
const isSame = !selectionChanged && !docChanged
|
||||
|
||||
if (composing || isSame) {
|
||||
return
|
||||
}
|
||||
|
||||
const shouldShow = this.getShouldShow(oldState)
|
||||
|
||||
if (!shouldShow) {
|
||||
this.hide()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
this.updatePosition()
|
||||
this.show()
|
||||
}
|
||||
|
||||
show() {
|
||||
if (this.isVisible) {
|
||||
return
|
||||
}
|
||||
|
||||
this.element.style.visibility = 'visible'
|
||||
this.element.style.opacity = '1'
|
||||
|
||||
// attach to appendTo or editor's parent element
|
||||
const appendToElement = typeof this.appendTo === 'function' ? this.appendTo() : this.appendTo
|
||||
;(appendToElement ?? this.view.dom.parentElement)?.appendChild(this.element)
|
||||
|
||||
if (this.floatingUIOptions.onShow) {
|
||||
this.floatingUIOptions.onShow()
|
||||
}
|
||||
|
||||
this.isVisible = true
|
||||
}
|
||||
|
||||
hide() {
|
||||
if (!this.isVisible) {
|
||||
return
|
||||
}
|
||||
|
||||
this.element.style.visibility = 'hidden'
|
||||
this.element.style.opacity = '0'
|
||||
// remove from the parent element
|
||||
this.element.remove()
|
||||
|
||||
if (this.floatingUIOptions.onHide) {
|
||||
this.floatingUIOptions.onHide()
|
||||
}
|
||||
|
||||
this.isVisible = false
|
||||
}
|
||||
|
||||
transactionHandler = ({ transaction: tr }: { transaction: Transaction }) => {
|
||||
const meta = tr.getMeta('bubbleMenu')
|
||||
if (meta === 'updatePosition') {
|
||||
this.updatePosition()
|
||||
} else if (meta && typeof meta === 'object' && meta.type === 'updateOptions') {
|
||||
this.updateOptions(meta.options)
|
||||
}
|
||||
}
|
||||
|
||||
updateOptions(newProps: Partial<Omit<BubbleMenuPluginProps, 'editor' | 'element' | 'pluginKey'>>) {
|
||||
if (newProps.updateDelay !== undefined) {
|
||||
this.updateDelay = newProps.updateDelay
|
||||
}
|
||||
|
||||
if (newProps.resizeDelay !== undefined) {
|
||||
this.resizeDelay = newProps.resizeDelay
|
||||
}
|
||||
|
||||
if (newProps.appendTo !== undefined) {
|
||||
this.appendTo = newProps.appendTo
|
||||
}
|
||||
|
||||
if (newProps.getReferencedVirtualElement !== undefined) {
|
||||
this.getReferencedVirtualElement = newProps.getReferencedVirtualElement
|
||||
}
|
||||
|
||||
if (newProps.shouldShow !== undefined) {
|
||||
if (newProps.shouldShow) {
|
||||
this.shouldShow = newProps.shouldShow
|
||||
}
|
||||
}
|
||||
|
||||
if (newProps.options !== undefined) {
|
||||
// Handle scrollTarget change - need to remove old listener and add new one
|
||||
// Use nullish coalescing to default to window when scrollTarget is undefined/null
|
||||
const newScrollTarget = newProps.options.scrollTarget ?? window
|
||||
|
||||
if (newScrollTarget !== this.scrollTarget) {
|
||||
this.scrollTarget.removeEventListener('scroll', this.resizeHandler)
|
||||
this.scrollTarget = newScrollTarget
|
||||
this.scrollTarget.addEventListener('scroll', this.resizeHandler)
|
||||
}
|
||||
|
||||
this.floatingUIOptions = {
|
||||
...this.floatingUIOptions,
|
||||
...newProps.options,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.hide()
|
||||
this.element.removeEventListener('mousedown', this.mousedownHandler, { capture: true })
|
||||
this.view.dom.removeEventListener('dragstart', this.dragstartHandler)
|
||||
window.removeEventListener('resize', this.resizeHandler)
|
||||
this.scrollTarget.removeEventListener('scroll', this.resizeHandler)
|
||||
this.editor.off('focus', this.focusHandler)
|
||||
this.editor.off('blur', this.blurHandler)
|
||||
this.editor.off('transaction', this.transactionHandler)
|
||||
|
||||
if (this.floatingUIOptions.onDestroy) {
|
||||
this.floatingUIOptions.onDestroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const BubbleMenuPlugin = (options: BubbleMenuPluginProps) => {
|
||||
return new Plugin({
|
||||
key: typeof options.pluginKey === 'string' ? new PluginKey(options.pluginKey) : options.pluginKey,
|
||||
view: view => new BubbleMenuView({ view, ...options }),
|
||||
})
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import { Extension } from '@tiptap/core'
|
||||
|
||||
import type { BubbleMenuPluginProps } from './bubble-menu-plugin.js'
|
||||
import { BubbleMenuPlugin } from './bubble-menu-plugin.js'
|
||||
|
||||
export type BubbleMenuOptions = Omit<BubbleMenuPluginProps, 'editor' | 'element'> & {
|
||||
/**
|
||||
* The DOM element that contains your menu.
|
||||
* @type {HTMLElement}
|
||||
* @default null
|
||||
*/
|
||||
element: HTMLElement | null
|
||||
}
|
||||
|
||||
/**
|
||||
* This extension allows you to create a bubble menu.
|
||||
* @see https://tiptap.dev/api/extensions/bubble-menu
|
||||
*/
|
||||
export const BubbleMenu = Extension.create<BubbleMenuOptions>({
|
||||
name: 'bubbleMenu',
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
element: null,
|
||||
pluginKey: 'bubbleMenu',
|
||||
updateDelay: undefined,
|
||||
appendTo: undefined,
|
||||
shouldShow: null,
|
||||
}
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
if (!this.options.element) {
|
||||
return []
|
||||
}
|
||||
|
||||
return [
|
||||
BubbleMenuPlugin({
|
||||
pluginKey: this.options.pluginKey,
|
||||
editor: this.editor,
|
||||
element: this.options.element,
|
||||
updateDelay: this.options.updateDelay,
|
||||
options: this.options.options,
|
||||
appendTo: this.options.appendTo,
|
||||
getReferencedVirtualElement: this.options.getReferencedVirtualElement,
|
||||
shouldShow: this.options.shouldShow,
|
||||
}),
|
||||
]
|
||||
},
|
||||
})
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import { BubbleMenu } from './bubble-menu.js'
|
||||
|
||||
export * from './bubble-menu.js'
|
||||
export * from './bubble-menu-plugin.js'
|
||||
|
||||
export default BubbleMenu
|
||||
Reference in New Issue
Block a user