@milkdown/plugin-tooltip 7.6.3 → 7.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.es.js +130 -72
- package/lib/index.es.js.map +1 -1
- package/lib/tooltip-provider.d.ts.map +1 -1
- package/package.json +6 -6
package/lib/index.es.js
CHANGED
|
@@ -1,96 +1,154 @@
|
|
|
1
|
-
var
|
|
2
|
-
throw TypeError(
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
3
|
};
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
8
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
+
var _debounce, _shouldShow, _middleware, _floatingUIOptions, _root, _initialized, _offset, _onUpdate, _TooltipProvider_instances, _shouldShow_fn;
|
|
10
|
+
import { TextSelection, Plugin, PluginKey } from "@milkdown/prose/state";
|
|
11
|
+
import throttle from "lodash.throttle";
|
|
12
|
+
import { computePosition, flip, offset, shift } from "@floating-ui/dom";
|
|
13
|
+
import { posToDOMRect } from "@milkdown/prose";
|
|
14
|
+
import { $ctx, $prose } from "@milkdown/utils";
|
|
15
|
+
class TooltipProvider {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
__privateAdd(this, _TooltipProvider_instances);
|
|
15
18
|
/// @internal
|
|
16
|
-
|
|
19
|
+
__privateAdd(this, _debounce);
|
|
17
20
|
/// @internal
|
|
18
|
-
|
|
21
|
+
__privateAdd(this, _shouldShow);
|
|
19
22
|
/// @internal
|
|
20
|
-
|
|
23
|
+
__privateAdd(this, _middleware);
|
|
21
24
|
/// @internal
|
|
22
|
-
|
|
25
|
+
__privateAdd(this, _floatingUIOptions);
|
|
23
26
|
/// @internal
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
__privateAdd(this, _root);
|
|
28
|
+
__privateAdd(this, _initialized);
|
|
26
29
|
/// @internal
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
__privateAdd(this, _offset);
|
|
31
|
+
__privateAdd(this, _onUpdate);
|
|
32
|
+
__privateSet(this, _initialized, false);
|
|
33
|
+
this.onShow = () => {
|
|
34
|
+
};
|
|
35
|
+
this.onHide = () => {
|
|
36
|
+
};
|
|
37
|
+
__privateSet(this, _onUpdate, (view, prevState) => {
|
|
38
|
+
const { state, composing } = view;
|
|
39
|
+
const { selection, doc } = state;
|
|
40
|
+
const { ranges } = selection;
|
|
41
|
+
const from = Math.min(...ranges.map((range) => range.$from.pos));
|
|
42
|
+
const to = Math.max(...ranges.map((range) => range.$to.pos));
|
|
43
|
+
const isSame = prevState && prevState.doc.eq(doc) && prevState.selection.eq(selection);
|
|
44
|
+
if (!__privateGet(this, _initialized)) {
|
|
45
|
+
const root = __privateGet(this, _root) ?? view.dom.parentElement ?? document.body;
|
|
46
|
+
root.appendChild(this.element);
|
|
47
|
+
__privateSet(this, _initialized, true);
|
|
48
|
+
}
|
|
49
|
+
if (composing || isSame) return;
|
|
50
|
+
if (!__privateGet(this, _shouldShow).call(this, view, prevState)) {
|
|
35
51
|
this.hide();
|
|
36
52
|
return;
|
|
37
53
|
}
|
|
38
|
-
|
|
39
|
-
getBoundingClientRect: () =>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
54
|
+
const virtualEl = {
|
|
55
|
+
getBoundingClientRect: () => posToDOMRect(view, from, to)
|
|
56
|
+
};
|
|
57
|
+
computePosition(virtualEl, this.element, {
|
|
58
|
+
placement: __privateGet(this, _floatingUIOptions).placement ?? "top",
|
|
59
|
+
middleware: [flip(), offset(__privateGet(this, _offset)), shift(), ...__privateGet(this, _middleware)]
|
|
60
|
+
}).then(({ x, y }) => {
|
|
44
61
|
Object.assign(this.element.style, {
|
|
45
|
-
left: `${
|
|
46
|
-
top: `${
|
|
62
|
+
left: `${x}px`,
|
|
63
|
+
top: `${y}px`
|
|
47
64
|
});
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
});
|
|
66
|
+
this.show();
|
|
67
|
+
});
|
|
68
|
+
this.update = (view, prevState) => {
|
|
69
|
+
const updater = throttle(__privateGet(this, _onUpdate), __privateGet(this, _debounce));
|
|
70
|
+
updater(view, prevState);
|
|
71
|
+
};
|
|
72
|
+
this.destroy = () => {
|
|
73
|
+
};
|
|
74
|
+
this.show = (virtualElement) => {
|
|
75
|
+
this.element.dataset.show = "true";
|
|
76
|
+
if (virtualElement) {
|
|
77
|
+
computePosition(virtualElement, this.element, {
|
|
78
|
+
placement: "top",
|
|
79
|
+
middleware: [flip(), offset(__privateGet(this, _offset))],
|
|
80
|
+
...__privateGet(this, _floatingUIOptions)
|
|
81
|
+
}).then(({ x, y }) => {
|
|
82
|
+
Object.assign(this.element.style, {
|
|
83
|
+
left: `${x}px`,
|
|
84
|
+
top: `${y}px`
|
|
85
|
+
});
|
|
61
86
|
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
87
|
+
}
|
|
88
|
+
this.onShow();
|
|
89
|
+
};
|
|
90
|
+
this.hide = () => {
|
|
91
|
+
if (this.element.dataset.show === "false") return;
|
|
92
|
+
this.element.dataset.show = "false";
|
|
93
|
+
this.onHide();
|
|
94
|
+
};
|
|
95
|
+
this.element = options.content;
|
|
96
|
+
__privateSet(this, _debounce, options.debounce ?? 200);
|
|
97
|
+
__privateSet(this, _shouldShow, options.shouldShow ?? __privateMethod(this, _TooltipProvider_instances, _shouldShow_fn));
|
|
98
|
+
__privateSet(this, _offset, options.offset);
|
|
99
|
+
__privateSet(this, _middleware, options.middleware ?? []);
|
|
100
|
+
__privateSet(this, _floatingUIOptions, options.floatingUIOptions ?? {});
|
|
101
|
+
__privateSet(this, _root, options.root);
|
|
102
|
+
this.element.dataset.show = "false";
|
|
66
103
|
}
|
|
67
104
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
105
|
+
_debounce = new WeakMap();
|
|
106
|
+
_shouldShow = new WeakMap();
|
|
107
|
+
_middleware = new WeakMap();
|
|
108
|
+
_floatingUIOptions = new WeakMap();
|
|
109
|
+
_root = new WeakMap();
|
|
110
|
+
_initialized = new WeakMap();
|
|
111
|
+
_offset = new WeakMap();
|
|
112
|
+
_onUpdate = new WeakMap();
|
|
113
|
+
_TooltipProvider_instances = new WeakSet();
|
|
114
|
+
/// @internal
|
|
115
|
+
_shouldShow_fn = function(view) {
|
|
116
|
+
const { doc, selection } = view.state;
|
|
117
|
+
const { empty, from, to } = selection;
|
|
118
|
+
const isEmptyTextBlock = !doc.textBetween(from, to).length && view.state.selection instanceof TextSelection;
|
|
119
|
+
const isTooltipChildren = this.element.contains(document.activeElement);
|
|
120
|
+
const notHasFocus = !view.hasFocus() && !isTooltipChildren;
|
|
121
|
+
const isReadonly = !view.editable;
|
|
122
|
+
if (notHasFocus || empty || isEmptyTextBlock || isReadonly) return false;
|
|
123
|
+
return true;
|
|
72
124
|
};
|
|
73
|
-
function
|
|
74
|
-
const
|
|
125
|
+
function tooltipFactory(id) {
|
|
126
|
+
const tooltipSpec = $ctx(
|
|
75
127
|
{},
|
|
76
|
-
`${
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
128
|
+
`${id}_TOOLTIP_SPEC`
|
|
129
|
+
);
|
|
130
|
+
const tooltipPlugin = $prose((ctx) => {
|
|
131
|
+
const spec = ctx.get(tooltipSpec.key);
|
|
132
|
+
return new Plugin({
|
|
133
|
+
key: new PluginKey(`${id}_TOOLTIP`),
|
|
134
|
+
...spec
|
|
82
135
|
});
|
|
83
|
-
})
|
|
84
|
-
|
|
136
|
+
});
|
|
137
|
+
const result = [tooltipSpec, tooltipPlugin];
|
|
138
|
+
result.key = tooltipSpec.key;
|
|
139
|
+
result.pluginKey = tooltipPlugin.key;
|
|
140
|
+
tooltipSpec.meta = {
|
|
85
141
|
package: "@milkdown/plugin-tooltip",
|
|
86
|
-
displayName: `Ctx<tooltipSpec>|${
|
|
87
|
-
}
|
|
142
|
+
displayName: `Ctx<tooltipSpec>|${id}`
|
|
143
|
+
};
|
|
144
|
+
tooltipPlugin.meta = {
|
|
88
145
|
package: "@milkdown/plugin-tooltip",
|
|
89
|
-
displayName: `Prose<tooltip>|${
|
|
90
|
-
}
|
|
146
|
+
displayName: `Prose<tooltip>|${id}`
|
|
147
|
+
};
|
|
148
|
+
return result;
|
|
91
149
|
}
|
|
92
150
|
export {
|
|
93
|
-
|
|
94
|
-
|
|
151
|
+
TooltipProvider,
|
|
152
|
+
tooltipFactory
|
|
95
153
|
};
|
|
96
154
|
//# sourceMappingURL=index.es.js.map
|
package/lib/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/tooltip-provider.ts","../src/tooltip-plugin.ts"],"sourcesContent":["import type { EditorState } from '@milkdown/prose/state'\nimport { TextSelection } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\nimport throttle from 'lodash.throttle'\nimport type {\n ComputePositionConfig,\n Middleware,\n VirtualElement,\n} from '@floating-ui/dom'\nimport { computePosition, flip, offset, shift } from '@floating-ui/dom'\nimport { posToDOMRect } from '@milkdown/prose'\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?:\n | number\n | {\n mainAxis?: number\n crossAxis?: number\n alignmentAxis?: number | null\n }\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 readonly #offset?:\n | number\n | {\n mainAxis?: number\n crossAxis?: number\n alignmentAxis?: number | null\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.#middleware = options.middleware ?? []\n this.#floatingUIOptions = options.floatingUIOptions ?? {}\n this.#root = options.root\n this.element.dataset.show = 'false'\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 if (!this.#shouldShow(view, prevState)) {\n this.hide()\n return\n }\n\n const virtualEl: VirtualElement = {\n getBoundingClientRect: () => posToDOMRect(view, from, to),\n }\n computePosition(virtualEl, this.element, {\n placement: this.#floatingUIOptions.placement ?? 'top',\n middleware: [flip(), offset(this.#offset), shift(), ...this.#middleware],\n }).then(({ x, y }) => {\n Object.assign(this.element.style, {\n left: `${x}px`,\n top: `${y}px`,\n })\n })\n\n this.show()\n }\n\n /// Update provider state by editor view.\n update = (view: EditorView, prevState?: EditorState): void => {\n const updater = throttle(this.#onUpdate, this.#debounce)\n\n 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\n /// Show the tooltip.\n show = (virtualElement?: VirtualElement) => {\n this.element.dataset.show = 'true'\n\n if (virtualElement) {\n computePosition(virtualElement, this.element, {\n placement: 'top',\n middleware: [flip(), offset(this.#offset)],\n ...this.#floatingUIOptions,\n }).then(({ x, y }) => {\n Object.assign(this.element.style, {\n left: `${x}px`,\n top: `${y}px`,\n })\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 { Plugin, PluginKey } from '@milkdown/prose/state'\nimport type { $Ctx, $Prose } from '@milkdown/utils'\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":["TooltipProvider","options","__privateAdd","_TooltipProvider_instances","_debounce","_shouldShow","_middleware","_floatingUIOptions","_root","_initialized","_offset","_onUpdate","__privateSet","view","prevState","state","composing","selection","doc","ranges","from","range","to","isSame","__privateGet","computePosition","posToDOMRect","flip","offset","shift","x","y","throttle","virtualElement","__privateMethod","_shouldShow_fn","empty","isEmptyTextBlock","TextSelection","isTooltipChildren","notHasFocus","isReadonly","tooltipFactory","id","tooltipSpec","$ctx","tooltipPlugin","$prose","ctx","spec","Plugin","Plugin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AAA,IACjB,SAAS;AAAA,IACT,aAAa,oBAAoBD,CAAE;AAAA,EACrC,GACAG,EAAc,OAAO;AAAA,IACnB,SAAS;AAAA,IACT,aAAa,kBAAkBH,CAAE;AAAA,EACnC,GAEOS;AACT;"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/tooltip-provider.ts","../src/tooltip-plugin.ts"],"sourcesContent":["import type { EditorState } from '@milkdown/prose/state'\nimport { TextSelection } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\nimport throttle from 'lodash.throttle'\nimport type {\n ComputePositionConfig,\n Middleware,\n VirtualElement,\n} from '@floating-ui/dom'\nimport { computePosition, flip, offset, shift } from '@floating-ui/dom'\nimport { posToDOMRect } from '@milkdown/prose'\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?:\n | number\n | {\n mainAxis?: number\n crossAxis?: number\n alignmentAxis?: number | null\n }\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 readonly #offset?:\n | number\n | {\n mainAxis?: number\n crossAxis?: number\n alignmentAxis?: number | null\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.#middleware = options.middleware ?? []\n this.#floatingUIOptions = options.floatingUIOptions ?? {}\n this.#root = options.root\n this.element.dataset.show = 'false'\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 if (!this.#shouldShow(view, prevState)) {\n this.hide()\n return\n }\n\n const virtualEl: VirtualElement = {\n getBoundingClientRect: () => posToDOMRect(view, from, to),\n }\n computePosition(virtualEl, this.element, {\n placement: this.#floatingUIOptions.placement ?? 'top',\n middleware: [flip(), offset(this.#offset), shift(), ...this.#middleware],\n }).then(({ x, y }) => {\n Object.assign(this.element.style, {\n left: `${x}px`,\n top: `${y}px`,\n })\n })\n\n this.show()\n }\n\n /// Update provider state by editor view.\n update = (view: EditorView, prevState?: EditorState): void => {\n const updater = throttle(this.#onUpdate, this.#debounce)\n\n 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\n /// Show the tooltip.\n show = (virtualElement?: VirtualElement) => {\n this.element.dataset.show = 'true'\n\n if (virtualElement) {\n computePosition(virtualElement, this.element, {\n placement: 'top',\n middleware: [flip(), offset(this.#offset)],\n ...this.#floatingUIOptions,\n }).then(({ x, y }) => {\n Object.assign(this.element.style, {\n left: `${x}px`,\n top: `${y}px`,\n })\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 { Plugin, PluginKey } from '@milkdown/prose/state'\nimport type { $Ctx, $Prose } from '@milkdown/utils'\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":";;;;;;;;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ClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAC9B,CAAA;IAEL,UAAU,CAAC,EAAE,UAAU,EAAE,CAAA;IAEzB,iBAAiB,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,CAAA;IAElD,IAAI,CAAC,EAAE,WAAW,CAAA;CACnB;AAGD,qBAAa,eAAe;;IA6B1B,OAAO,EAAE,WAAW,CAAA;IAGpB,MAAM,aAAW;IAGjB,MAAM,aAAW;gBAEL,OAAO,EAAE,sBAAsB;IAmD3C,MAAM,GAAI,MAAM,UAAU,EAAE,YAAY,WAAW,KAAG,IAAI,CAIzD;IAuBD,OAAO,aAAW;IAGlB,IAAI,GAAI,iBAAiB,cAAc,UAiBtC;IAGD,IAAI,aAKH;CACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkdown/plugin-tooltip",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.
|
|
4
|
+
"version": "7.7.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
"lodash.throttle": "^4.1.1",
|
|
36
36
|
"tippy.js": "^6.3.7",
|
|
37
37
|
"tslib": "^2.8.1",
|
|
38
|
-
"@milkdown/exception": "7.
|
|
39
|
-
"@milkdown/utils": "7.
|
|
38
|
+
"@milkdown/exception": "7.7.0",
|
|
39
|
+
"@milkdown/utils": "7.7.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@milkdown/ctx": "7.
|
|
43
|
-
"@milkdown/
|
|
44
|
-
"@milkdown/
|
|
42
|
+
"@milkdown/ctx": "7.7.0",
|
|
43
|
+
"@milkdown/core": "7.7.0",
|
|
44
|
+
"@milkdown/prose": "7.7.0"
|
|
45
45
|
},
|
|
46
46
|
"nx": {
|
|
47
47
|
"targets": {
|