@milkdown/plugin-tooltip 7.19.0 → 7.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +137 -166
- package/lib/index.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
package/lib/index.js
CHANGED
|
@@ -1,171 +1,142 @@
|
|
|
1
|
-
import { computePosition, flip, offset, shift
|
|
1
|
+
import { autoUpdate, computePosition, flip, offset, shift } from "@floating-ui/dom";
|
|
2
2
|
import { posToDOMRect } from "@milkdown/prose";
|
|
3
|
-
import {
|
|
3
|
+
import { Plugin, PluginKey, TextSelection } from "@milkdown/prose/state";
|
|
4
4
|
import { throttle } from "lodash-es";
|
|
5
5
|
import { $ctx, $prose } from "@milkdown/utils";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
#root;
|
|
118
|
-
#initialized;
|
|
119
|
-
/// @internal
|
|
120
|
-
#cleanupAutoUpdate;
|
|
121
|
-
/// @internal
|
|
122
|
-
#offset;
|
|
123
|
-
/// @internal
|
|
124
|
-
#shift;
|
|
125
|
-
/// @internal
|
|
126
|
-
#updater;
|
|
127
|
-
#updatePosition;
|
|
128
|
-
#shouldAutoUpdate;
|
|
129
|
-
#onUpdate;
|
|
130
|
-
/// @internal
|
|
131
|
-
#_shouldShow(view) {
|
|
132
|
-
const { doc, selection } = view.state;
|
|
133
|
-
const { empty, from, to } = selection;
|
|
134
|
-
const isEmptyTextBlock = !doc.textBetween(from, to).length && view.state.selection instanceof TextSelection;
|
|
135
|
-
const isTooltipChildren = this.element.contains(document.activeElement);
|
|
136
|
-
const notHasFocus = !view.hasFocus() && !isTooltipChildren;
|
|
137
|
-
const isReadonly = !view.editable;
|
|
138
|
-
if (notHasFocus || empty || isEmptyTextBlock || isReadonly) return false;
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
6
|
+
//#region src/tooltip-provider.ts
|
|
7
|
+
var TooltipProvider = class {
|
|
8
|
+
#debounce;
|
|
9
|
+
#shouldShow;
|
|
10
|
+
#middleware;
|
|
11
|
+
#floatingUIOptions;
|
|
12
|
+
#root;
|
|
13
|
+
#initialized = false;
|
|
14
|
+
#cleanupAutoUpdate;
|
|
15
|
+
#offset;
|
|
16
|
+
#shift;
|
|
17
|
+
#updater;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.onShow = () => {};
|
|
20
|
+
this.onHide = () => {};
|
|
21
|
+
this.update = (view, prevState) => {
|
|
22
|
+
this.#updater(view, prevState);
|
|
23
|
+
};
|
|
24
|
+
this.destroy = () => {
|
|
25
|
+
this.#cleanupAutoUpdate?.();
|
|
26
|
+
this.#updater.cancel();
|
|
27
|
+
};
|
|
28
|
+
this.show = (virtualElement, editorView) => {
|
|
29
|
+
this.element.dataset.show = "true";
|
|
30
|
+
if (virtualElement) {
|
|
31
|
+
this.#cleanupAutoUpdate?.();
|
|
32
|
+
this.#cleanupAutoUpdate = void 0;
|
|
33
|
+
const reference = {
|
|
34
|
+
...virtualElement,
|
|
35
|
+
contextElement: editorView?.dom
|
|
36
|
+
};
|
|
37
|
+
if (editorView && this.#shouldAutoUpdate(editorView)) this.#cleanupAutoUpdate = autoUpdate(reference, this.element, () => this.#updatePosition(reference));
|
|
38
|
+
else this.#updatePosition(reference);
|
|
39
|
+
}
|
|
40
|
+
this.onShow();
|
|
41
|
+
};
|
|
42
|
+
this.hide = () => {
|
|
43
|
+
if (this.element.dataset.show === "false") return;
|
|
44
|
+
this.element.dataset.show = "false";
|
|
45
|
+
this.onHide();
|
|
46
|
+
};
|
|
47
|
+
this.element = options.content;
|
|
48
|
+
this.#debounce = options.debounce ?? 200;
|
|
49
|
+
this.#shouldShow = options.shouldShow ?? this.#_shouldShow;
|
|
50
|
+
this.#offset = options.offset;
|
|
51
|
+
this.#shift = options.shift;
|
|
52
|
+
this.#middleware = options.middleware ?? [];
|
|
53
|
+
this.#floatingUIOptions = options.floatingUIOptions ?? {};
|
|
54
|
+
this.#root = options.root;
|
|
55
|
+
this.element.dataset.show = "false";
|
|
56
|
+
this.#updater = throttle(this.#onUpdate, this.#debounce);
|
|
57
|
+
}
|
|
58
|
+
#updatePosition = (reference) => {
|
|
59
|
+
computePosition(reference, this.element, {
|
|
60
|
+
placement: this.#floatingUIOptions.placement ?? "top",
|
|
61
|
+
middleware: [
|
|
62
|
+
flip(),
|
|
63
|
+
offset(this.#offset),
|
|
64
|
+
shift(this.#shift),
|
|
65
|
+
...this.#middleware
|
|
66
|
+
],
|
|
67
|
+
...this.#floatingUIOptions
|
|
68
|
+
}).then(({ x, y }) => {
|
|
69
|
+
Object.assign(this.element.style, {
|
|
70
|
+
left: `${x}px`,
|
|
71
|
+
top: `${y}px`
|
|
72
|
+
});
|
|
73
|
+
}).catch(console.error);
|
|
74
|
+
};
|
|
75
|
+
#shouldAutoUpdate = (editorView) => {
|
|
76
|
+
return this.#root !== editorView.dom.parentElement;
|
|
77
|
+
};
|
|
78
|
+
#onUpdate = (view, prevState) => {
|
|
79
|
+
const { state, composing } = view;
|
|
80
|
+
const { selection, doc } = state;
|
|
81
|
+
const { ranges } = selection;
|
|
82
|
+
const from = Math.min(...ranges.map((range) => range.$from.pos));
|
|
83
|
+
const to = Math.max(...ranges.map((range) => range.$to.pos));
|
|
84
|
+
const isSame = prevState && prevState.doc.eq(doc) && prevState.selection.eq(selection);
|
|
85
|
+
if (!this.#initialized) {
|
|
86
|
+
(this.#root ?? view.dom.parentElement ?? document.body).appendChild(this.element);
|
|
87
|
+
this.#initialized = true;
|
|
88
|
+
}
|
|
89
|
+
if (composing || isSame) return;
|
|
90
|
+
this.#cleanupAutoUpdate?.();
|
|
91
|
+
this.#cleanupAutoUpdate = void 0;
|
|
92
|
+
if (!this.#shouldShow(view, prevState)) {
|
|
93
|
+
this.hide();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const virtualEl = {
|
|
97
|
+
getBoundingClientRect: () => posToDOMRect(view, from, to),
|
|
98
|
+
contextElement: view.dom
|
|
99
|
+
};
|
|
100
|
+
if (this.#shouldAutoUpdate(view)) this.#cleanupAutoUpdate = autoUpdate(virtualEl, this.element, () => this.#updatePosition(virtualEl));
|
|
101
|
+
else this.#updatePosition(virtualEl);
|
|
102
|
+
this.show();
|
|
103
|
+
};
|
|
104
|
+
#_shouldShow(view) {
|
|
105
|
+
const { doc, selection } = view.state;
|
|
106
|
+
const { empty, from, to } = selection;
|
|
107
|
+
const isEmptyTextBlock = !doc.textBetween(from, to).length && view.state.selection instanceof TextSelection;
|
|
108
|
+
const isTooltipChildren = this.element.contains(document.activeElement);
|
|
109
|
+
const notHasFocus = !view.hasFocus() && !isTooltipChildren;
|
|
110
|
+
const isReadonly = !view.editable;
|
|
111
|
+
if (notHasFocus || empty || isEmptyTextBlock || isReadonly) return false;
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/tooltip-plugin.ts
|
|
142
117
|
function tooltipFactory(id) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
displayName: `Prose<tooltip>|${id}`
|
|
164
|
-
};
|
|
165
|
-
return result;
|
|
118
|
+
const tooltipSpec = $ctx({}, `${id}_TOOLTIP_SPEC`);
|
|
119
|
+
const tooltipPlugin = $prose((ctx) => {
|
|
120
|
+
const spec = ctx.get(tooltipSpec.key);
|
|
121
|
+
return new Plugin({
|
|
122
|
+
key: new PluginKey(`${id}_TOOLTIP`),
|
|
123
|
+
...spec
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
const result = [tooltipSpec, tooltipPlugin];
|
|
127
|
+
result.key = tooltipSpec.key;
|
|
128
|
+
result.pluginKey = tooltipPlugin.key;
|
|
129
|
+
tooltipSpec.meta = {
|
|
130
|
+
package: "@milkdown/plugin-tooltip",
|
|
131
|
+
displayName: `Ctx<tooltipSpec>|${id}`
|
|
132
|
+
};
|
|
133
|
+
tooltipPlugin.meta = {
|
|
134
|
+
package: "@milkdown/plugin-tooltip",
|
|
135
|
+
displayName: `Prose<tooltip>|${id}`
|
|
136
|
+
};
|
|
137
|
+
return result;
|
|
166
138
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
//# sourceMappingURL=index.js.map
|
|
139
|
+
//#endregion
|
|
140
|
+
export { TooltipProvider, tooltipFactory };
|
|
141
|
+
|
|
142
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/tooltip-provider.ts","../src/tooltip-plugin.ts"],"sourcesContent":["import type {\n ComputePositionConfig,\n Middleware,\n OffsetOptions,\n ShiftOptions,\n VirtualElement,\n} from '@floating-ui/dom'\nimport type { EditorState } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\n\nimport {\n autoUpdate,\n computePosition,\n flip,\n offset,\n shift,\n} from '@floating-ui/dom'\nimport { posToDOMRect } from '@milkdown/prose'\nimport { TextSelection } from '@milkdown/prose/state'\nimport { throttle } from 'lodash-es'\n\n/// Options for tooltip provider.\nexport interface TooltipProviderOptions {\n /// The tooltip content.\n content: HTMLElement\n /// The debounce time for updating tooltip, 200ms by default.\n debounce?: number\n /// The function to determine whether the tooltip should be shown.\n shouldShow?: (view: EditorView, prevState?: EditorState) => boolean\n /// The offset to get the block. Default is 0.\n offset?: OffsetOptions\n /// The amount to shift options the block by.\n shift?: ShiftOptions\n /// Other middlewares for floating ui. This will be added after the internal middlewares.\n middleware?: Middleware[]\n /// Options for floating ui. If you pass `middleware` or `placement`, it will override the internal settings.\n floatingUIOptions?: Partial<ComputePositionConfig>\n /// The root element that the tooltip will be appended to.\n root?: HTMLElement\n}\n\n/// A provider for creating tooltip.\nexport class TooltipProvider {\n /// @internal\n readonly #debounce: number\n\n /// @internal\n readonly #shouldShow: (view: EditorView, prevState?: EditorState) => boolean\n\n /// @internal\n readonly #middleware: Middleware[]\n\n /// @internal\n readonly #floatingUIOptions: Partial<ComputePositionConfig>\n\n /// @internal\n readonly #root?: HTMLElement\n\n /// @internal\n #initialized = false\n\n /// @internal\n #cleanupAutoUpdate?: () => void\n\n /// @internal\n readonly #offset?: OffsetOptions\n\n /// @internal\n readonly #shift?: ShiftOptions\n\n /// @internal\n readonly #updater: {\n (view: EditorView, prevState?: EditorState): void\n cancel: () => void\n }\n\n /// The root element of the tooltip.\n element: HTMLElement\n\n /// On show callback.\n onShow = () => {}\n\n /// On hide callback.\n onHide = () => {}\n\n constructor(options: TooltipProviderOptions) {\n this.element = options.content\n this.#debounce = options.debounce ?? 200\n this.#shouldShow = options.shouldShow ?? this.#_shouldShow\n this.#offset = options.offset\n this.#shift = options.shift\n this.#middleware = options.middleware ?? []\n this.#floatingUIOptions = options.floatingUIOptions ?? {}\n this.#root = options.root\n this.element.dataset.show = 'false'\n this.#updater = throttle(this.#onUpdate, this.#debounce)\n }\n\n /// @internel\n #updatePosition = (reference: VirtualElement) => {\n computePosition(reference, this.element, {\n placement: this.#floatingUIOptions.placement ?? 'top',\n middleware: [\n flip(),\n offset(this.#offset),\n shift(this.#shift),\n ...this.#middleware,\n ],\n ...this.#floatingUIOptions,\n })\n .then(({ x, y }) => {\n Object.assign(this.element.style, {\n left: `${x}px`,\n top: `${y}px`,\n })\n })\n .catch(console.error)\n }\n\n /// @internal\n #shouldAutoUpdate = (editorView: EditorView) => {\n return this.#root !== editorView.dom.parentElement\n }\n\n /// @internal\n #onUpdate = (view: EditorView, prevState?: EditorState): void => {\n const { state, composing } = view\n const { selection, doc } = state\n const { ranges } = selection\n const from = Math.min(...ranges.map((range) => range.$from.pos))\n const to = Math.max(...ranges.map((range) => range.$to.pos))\n const isSame =\n prevState && prevState.doc.eq(doc) && prevState.selection.eq(selection)\n\n if (!this.#initialized) {\n const root = this.#root ?? view.dom.parentElement ?? document.body\n root.appendChild(this.element)\n this.#initialized = true\n }\n\n if (composing || isSame) return\n\n this.#cleanupAutoUpdate?.()\n this.#cleanupAutoUpdate = void 0\n\n if (!this.#shouldShow(view, prevState)) {\n this.hide()\n return\n }\n\n const virtualEl: VirtualElement = {\n getBoundingClientRect: () => posToDOMRect(view, from, to),\n contextElement: view.dom,\n }\n\n if (this.#shouldAutoUpdate(view)) {\n this.#cleanupAutoUpdate = autoUpdate(virtualEl, this.element, () =>\n this.#updatePosition(virtualEl)\n )\n } else {\n this.#updatePosition(virtualEl)\n }\n\n this.show()\n }\n\n /// Update provider state by editor view.\n update = (view: EditorView, prevState?: EditorState): void => {\n this.#updater(view, prevState)\n }\n\n /// @internal\n #_shouldShow(view: EditorView): boolean {\n const { doc, selection } = view.state\n const { empty, from, to } = selection\n\n const isEmptyTextBlock =\n !doc.textBetween(from, to).length &&\n view.state.selection instanceof TextSelection\n\n const isTooltipChildren = this.element.contains(document.activeElement)\n\n const notHasFocus = !view.hasFocus() && !isTooltipChildren\n\n const isReadonly = !view.editable\n\n if (notHasFocus || empty || isEmptyTextBlock || isReadonly) return false\n\n return true\n }\n\n /// Destroy the tooltip.\n destroy = () => {\n this.#cleanupAutoUpdate?.()\n this.#updater.cancel()\n }\n\n /// Show the tooltip.\n show = (virtualElement?: VirtualElement, editorView?: EditorView) => {\n this.element.dataset.show = 'true'\n\n if (virtualElement) {\n this.#cleanupAutoUpdate?.()\n this.#cleanupAutoUpdate = void 0\n\n const reference = { ...virtualElement, contextElement: editorView?.dom }\n\n if (editorView && this.#shouldAutoUpdate(editorView)) {\n this.#cleanupAutoUpdate = autoUpdate(reference, this.element, () =>\n this.#updatePosition(reference)\n )\n } else {\n this.#updatePosition(reference)\n }\n }\n\n this.onShow()\n }\n\n /// Hide the tooltip.\n hide = () => {\n if (this.element.dataset.show === 'false') return\n this.element.dataset.show = 'false'\n\n this.onHide()\n }\n}\n","import type { SliceType } from '@milkdown/ctx'\nimport type { PluginSpec } from '@milkdown/prose/state'\nimport type { $Ctx, $Prose } from '@milkdown/utils'\n\nimport { Plugin, PluginKey } from '@milkdown/prose/state'\nimport { $ctx, $prose } from '@milkdown/utils'\n\n/// @internal\nexport type TooltipSpecId<Id extends string> = `${Id}_TOOLTIP_SPEC`\n\n/// @internal\nexport type TooltipPlugin<Id extends string, State = any> = [\n $Ctx<PluginSpec<State>, TooltipSpecId<Id>>,\n $Prose,\n] & {\n key: SliceType<PluginSpec<State>, TooltipSpecId<Id>>\n pluginKey: $Prose['key']\n}\n\n/// Create a tooltip plugin with a unique id.\nexport function tooltipFactory<Id extends string, State = any>(id: Id) {\n const tooltipSpec = $ctx<PluginSpec<State>, TooltipSpecId<Id>>(\n {},\n `${id}_TOOLTIP_SPEC`\n )\n const tooltipPlugin = $prose((ctx) => {\n const spec = ctx.get(tooltipSpec.key)\n return new Plugin({\n key: new PluginKey(`${id}_TOOLTIP`),\n ...spec,\n })\n })\n const result = [tooltipSpec, tooltipPlugin] as TooltipPlugin<Id>\n result.key = tooltipSpec.key\n result.pluginKey = tooltipPlugin.key\n tooltipSpec.meta = {\n package: '@milkdown/plugin-tooltip',\n displayName: `Ctx<tooltipSpec>|${id}`,\n }\n tooltipPlugin.meta = {\n package: '@milkdown/plugin-tooltip',\n displayName: `Prose<tooltip>|${id}`,\n }\n\n return result\n}\n"],"names":[],"mappings":";;;;;AA0CO,MAAM,gBAAgB;AAAA,EA2C3B,YAAY,SAAiC;AA1B7C,SAAA,eAAe;AAqBf,SAAA,SAAS,MAAM;AAAA,IAAC;AAGhB,SAAA,SAAS,MAAM;AAAA,IAAC;AAgBhB,SAAA,kBAAkB,CAAC,cA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AAoB,EAAE;AAAA,EAAA;AAErC,gBAAc,OAAO;AAAA,IACnB,SAAS;AAAA,IACT,aAAa,kBAAkB,EAAE;AAAA,EAAA;AAGnC,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["#debounce","#shouldShow","#middleware","#floatingUIOptions","#root","#offset","#shift","#updater","#cleanupAutoUpdate","#shouldAutoUpdate","#updatePosition","#_shouldShow","#onUpdate","#initialized"],"sources":["../src/tooltip-provider.ts","../src/tooltip-plugin.ts"],"sourcesContent":["import type {\n ComputePositionConfig,\n Middleware,\n OffsetOptions,\n ShiftOptions,\n VirtualElement,\n} from '@floating-ui/dom'\nimport type { EditorState } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\n\nimport {\n autoUpdate,\n computePosition,\n flip,\n offset,\n shift,\n} from '@floating-ui/dom'\nimport { posToDOMRect } from '@milkdown/prose'\nimport { TextSelection } from '@milkdown/prose/state'\nimport { throttle } from 'lodash-es'\n\n/// Options for tooltip provider.\nexport interface TooltipProviderOptions {\n /// The tooltip content.\n content: HTMLElement\n /// The debounce time for updating tooltip, 200ms by default.\n debounce?: number\n /// The function to determine whether the tooltip should be shown.\n shouldShow?: (view: EditorView, prevState?: EditorState) => boolean\n /// The offset to get the block. Default is 0.\n offset?: OffsetOptions\n /// The amount to shift options the block by.\n shift?: ShiftOptions\n /// Other middlewares for floating ui. This will be added after the internal middlewares.\n middleware?: Middleware[]\n /// Options for floating ui. If you pass `middleware` or `placement`, it will override the internal settings.\n floatingUIOptions?: Partial<ComputePositionConfig>\n /// The root element that the tooltip will be appended to.\n root?: HTMLElement\n}\n\n/// A provider for creating tooltip.\nexport class TooltipProvider {\n /// @internal\n readonly #debounce: number\n\n /// @internal\n readonly #shouldShow: (view: EditorView, prevState?: EditorState) => boolean\n\n /// @internal\n readonly #middleware: Middleware[]\n\n /// @internal\n readonly #floatingUIOptions: Partial<ComputePositionConfig>\n\n /// @internal\n readonly #root?: HTMLElement\n\n /// @internal\n #initialized = false\n\n /// @internal\n #cleanupAutoUpdate?: () => void\n\n /// @internal\n readonly #offset?: OffsetOptions\n\n /// @internal\n readonly #shift?: ShiftOptions\n\n /// @internal\n readonly #updater: {\n (view: EditorView, prevState?: EditorState): void\n cancel: () => void\n }\n\n /// The root element of the tooltip.\n element: HTMLElement\n\n /// On show callback.\n onShow = () => {}\n\n /// On hide callback.\n onHide = () => {}\n\n constructor(options: TooltipProviderOptions) {\n this.element = options.content\n this.#debounce = options.debounce ?? 200\n this.#shouldShow = options.shouldShow ?? this.#_shouldShow\n this.#offset = options.offset\n this.#shift = options.shift\n this.#middleware = options.middleware ?? []\n this.#floatingUIOptions = options.floatingUIOptions ?? {}\n this.#root = options.root\n this.element.dataset.show = 'false'\n this.#updater = throttle(this.#onUpdate, this.#debounce)\n }\n\n /// @internel\n #updatePosition = (reference: VirtualElement) => {\n computePosition(reference, this.element, {\n placement: this.#floatingUIOptions.placement ?? 'top',\n middleware: [\n flip(),\n offset(this.#offset),\n shift(this.#shift),\n ...this.#middleware,\n ],\n ...this.#floatingUIOptions,\n })\n .then(({ x, y }) => {\n Object.assign(this.element.style, {\n left: `${x}px`,\n top: `${y}px`,\n })\n })\n .catch(console.error)\n }\n\n /// @internal\n #shouldAutoUpdate = (editorView: EditorView) => {\n return this.#root !== editorView.dom.parentElement\n }\n\n /// @internal\n #onUpdate = (view: EditorView, prevState?: EditorState): void => {\n const { state, composing } = view\n const { selection, doc } = state\n const { ranges } = selection\n const from = Math.min(...ranges.map((range) => range.$from.pos))\n const to = Math.max(...ranges.map((range) => range.$to.pos))\n const isSame =\n prevState && prevState.doc.eq(doc) && prevState.selection.eq(selection)\n\n if (!this.#initialized) {\n const root = this.#root ?? view.dom.parentElement ?? document.body\n root.appendChild(this.element)\n this.#initialized = true\n }\n\n if (composing || isSame) return\n\n this.#cleanupAutoUpdate?.()\n this.#cleanupAutoUpdate = void 0\n\n if (!this.#shouldShow(view, prevState)) {\n this.hide()\n return\n }\n\n const virtualEl: VirtualElement = {\n getBoundingClientRect: () => posToDOMRect(view, from, to),\n contextElement: view.dom,\n }\n\n if (this.#shouldAutoUpdate(view)) {\n this.#cleanupAutoUpdate = autoUpdate(virtualEl, this.element, () =>\n this.#updatePosition(virtualEl)\n )\n } else {\n this.#updatePosition(virtualEl)\n }\n\n this.show()\n }\n\n /// Update provider state by editor view.\n update = (view: EditorView, prevState?: EditorState): void => {\n this.#updater(view, prevState)\n }\n\n /// @internal\n #_shouldShow(view: EditorView): boolean {\n const { doc, selection } = view.state\n const { empty, from, to } = selection\n\n const isEmptyTextBlock =\n !doc.textBetween(from, to).length &&\n view.state.selection instanceof TextSelection\n\n const isTooltipChildren = this.element.contains(document.activeElement)\n\n const notHasFocus = !view.hasFocus() && !isTooltipChildren\n\n const isReadonly = !view.editable\n\n if (notHasFocus || empty || isEmptyTextBlock || isReadonly) return false\n\n return true\n }\n\n /// Destroy the tooltip.\n destroy = () => {\n this.#cleanupAutoUpdate?.()\n this.#updater.cancel()\n }\n\n /// Show the tooltip.\n show = (virtualElement?: VirtualElement, editorView?: EditorView) => {\n this.element.dataset.show = 'true'\n\n if (virtualElement) {\n this.#cleanupAutoUpdate?.()\n this.#cleanupAutoUpdate = void 0\n\n const reference = { ...virtualElement, contextElement: editorView?.dom }\n\n if (editorView && this.#shouldAutoUpdate(editorView)) {\n this.#cleanupAutoUpdate = autoUpdate(reference, this.element, () =>\n this.#updatePosition(reference)\n )\n } else {\n this.#updatePosition(reference)\n }\n }\n\n this.onShow()\n }\n\n /// Hide the tooltip.\n hide = () => {\n if (this.element.dataset.show === 'false') return\n this.element.dataset.show = 'false'\n\n this.onHide()\n }\n}\n","import type { SliceType } from '@milkdown/ctx'\nimport type { PluginSpec } from '@milkdown/prose/state'\nimport type { $Ctx, $Prose } from '@milkdown/utils'\n\nimport { Plugin, PluginKey } from '@milkdown/prose/state'\nimport { $ctx, $prose } from '@milkdown/utils'\n\n/// @internal\nexport type TooltipSpecId<Id extends string> = `${Id}_TOOLTIP_SPEC`\n\n/// @internal\nexport type TooltipPlugin<Id extends string, State = any> = [\n $Ctx<PluginSpec<State>, TooltipSpecId<Id>>,\n $Prose,\n] & {\n key: SliceType<PluginSpec<State>, TooltipSpecId<Id>>\n pluginKey: $Prose['key']\n}\n\n/// Create a tooltip plugin with a unique id.\nexport function tooltipFactory<Id extends string, State = any>(id: Id) {\n const tooltipSpec = $ctx<PluginSpec<State>, TooltipSpecId<Id>>(\n {},\n `${id}_TOOLTIP_SPEC`\n )\n const tooltipPlugin = $prose((ctx) => {\n const spec = ctx.get(tooltipSpec.key)\n return new Plugin({\n key: new PluginKey(`${id}_TOOLTIP`),\n ...spec,\n })\n })\n const result = [tooltipSpec, tooltipPlugin] as TooltipPlugin<Id>\n result.key = tooltipSpec.key\n result.pluginKey = tooltipPlugin.key\n tooltipSpec.meta = {\n package: '@milkdown/plugin-tooltip',\n displayName: `Ctx<tooltipSpec>|${id}`,\n }\n tooltipPlugin.meta = {\n package: '@milkdown/plugin-tooltip',\n displayName: `Prose<tooltip>|${id}`,\n }\n\n return result\n}\n"],"mappings":";;;;;;AA0CA,IAAa,kBAAb,MAA6B;CAE3B;CAGA;CAGA;CAGA;CAGA;CAGA,eAAe;CAGf;CAGA;CAGA;CAGA;CAcA,YAAY,SAAiC;sBAL9B;sBAGA;iBAoFL,MAAkB,cAAkC;AAC5D,SAAA,QAAc,MAAM,UAAU;;uBAwBhB;AACd,SAAA,qBAA2B;AAC3B,SAAA,QAAc,QAAQ;;eAIhB,gBAAiC,eAA4B;AACnE,QAAK,QAAQ,QAAQ,OAAO;AAE5B,OAAI,gBAAgB;AAClB,UAAA,qBAA2B;AAC3B,UAAA,oBAA0B,KAAK;IAE/B,MAAM,YAAY;KAAE,GAAG;KAAgB,gBAAgB,YAAY;KAAK;AAExE,QAAI,cAAc,MAAA,iBAAuB,WAAW,CAClD,OAAA,oBAA0B,WAAW,WAAW,KAAK,eACnD,MAAA,eAAqB,UAAU,CAChC;QAED,OAAA,eAAq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