@milkdown/plugin-tooltip 7.5.9 → 7.6.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.es.js +38 -38
- package/lib/index.es.js.map +1 -1
- package/package.json +9 -9
- package/src/tooltip-provider.ts +3 -3
package/lib/index.es.js
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
var S = (
|
|
2
|
-
throw TypeError(
|
|
1
|
+
var S = (s) => {
|
|
2
|
+
throw TypeError(s);
|
|
3
3
|
};
|
|
4
|
-
var P = (
|
|
5
|
-
var i = (
|
|
4
|
+
var P = (s, t, e) => t.has(s) || S("Cannot " + e);
|
|
5
|
+
var i = (s, t, e) => (P(s, t, "read from private field"), e ? e.call(s) : t.get(s)), n = (s, t, e) => t.has(s) ? S("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(s) : t.set(s, e), l = (s, t, e, o) => (P(s, t, "write to private field"), o ? o.call(s, e) : t.set(s, e), e), b = (s, t, e) => (P(s, t, "access private method"), e);
|
|
6
6
|
import { TextSelection as H, Plugin as M, PluginKey as R } from "@milkdown/prose/state";
|
|
7
|
-
import U from "lodash.
|
|
7
|
+
import U from "lodash.throttle";
|
|
8
8
|
import { computePosition as C, flip as E, offset as I, shift as j } from "@floating-ui/dom";
|
|
9
9
|
import { posToDOMRect as q } from "@milkdown/prose";
|
|
10
10
|
import { $ctx as K, $prose as L } from "@milkdown/utils";
|
|
11
|
-
var
|
|
11
|
+
var d, f, u, a, c, m, g, $, _;
|
|
12
12
|
class V {
|
|
13
13
|
constructor(t) {
|
|
14
14
|
n(this, $);
|
|
15
15
|
/// @internal
|
|
16
|
-
n(this, r);
|
|
17
|
-
/// @internal
|
|
18
16
|
n(this, d);
|
|
19
17
|
/// @internal
|
|
18
|
+
n(this, f);
|
|
19
|
+
/// @internal
|
|
20
20
|
n(this, u);
|
|
21
21
|
/// @internal
|
|
22
|
-
n(this, f);
|
|
23
22
|
n(this, a);
|
|
24
|
-
/// @internal
|
|
25
23
|
n(this, c);
|
|
24
|
+
/// @internal
|
|
25
|
+
n(this, m);
|
|
26
26
|
n(this, g);
|
|
27
|
-
l(this,
|
|
27
|
+
l(this, c, !1), this.onShow = () => {
|
|
28
28
|
}, this.onHide = () => {
|
|
29
|
-
}, l(this, g, (e,
|
|
29
|
+
}, l(this, g, (e, o) => {
|
|
30
30
|
var T;
|
|
31
|
-
const { state: h, composing:
|
|
32
|
-
if (i(this,
|
|
33
|
-
if (!i(this,
|
|
31
|
+
const { state: h, composing: p } = e, { selection: y, doc: x } = h, { ranges: w } = y, k = Math.min(...w.map((r) => r.$from.pos)), O = Math.max(...w.map((r) => r.$to.pos)), B = o && o.doc.eq(x) && o.selection.eq(y);
|
|
32
|
+
if (i(this, c) || ((T = e.dom.parentElement) == null || T.appendChild(this.element), l(this, c, !0)), p || B) return;
|
|
33
|
+
if (!i(this, f).call(this, e, o)) {
|
|
34
34
|
this.hide();
|
|
35
35
|
return;
|
|
36
36
|
}
|
|
37
37
|
C({
|
|
38
38
|
getBoundingClientRect: () => q(e, k, O)
|
|
39
39
|
}, this.element, {
|
|
40
|
-
placement: "top",
|
|
41
|
-
middleware: [E(), I(i(this,
|
|
42
|
-
}).then(({ x:
|
|
40
|
+
placement: i(this, a).placement ?? "top",
|
|
41
|
+
middleware: [E(), I(i(this, m)), j(), ...i(this, u)]
|
|
42
|
+
}).then(({ x: r, y: F }) => {
|
|
43
43
|
Object.assign(this.element.style, {
|
|
44
|
-
left: `${
|
|
44
|
+
left: `${r}px`,
|
|
45
45
|
top: `${F}px`
|
|
46
46
|
});
|
|
47
47
|
}), this.show();
|
|
48
|
-
}), this.update = (e,
|
|
49
|
-
U(i(this, g), i(this,
|
|
48
|
+
}), this.update = (e, o) => {
|
|
49
|
+
U(i(this, g), i(this, d))(e, o);
|
|
50
50
|
}, this.destroy = () => {
|
|
51
51
|
}, this.show = (e) => {
|
|
52
52
|
this.element.dataset.show = "true", e && C(e, this.element, {
|
|
53
53
|
placement: "top",
|
|
54
|
-
middleware: [E(), I(i(this,
|
|
55
|
-
...i(this,
|
|
56
|
-
}).then(({ x:
|
|
54
|
+
middleware: [E(), I(i(this, m))],
|
|
55
|
+
...i(this, a)
|
|
56
|
+
}).then(({ x: o, y: h }) => {
|
|
57
57
|
Object.assign(this.element.style, {
|
|
58
|
-
left: `${
|
|
58
|
+
left: `${o}px`,
|
|
59
59
|
top: `${h}px`
|
|
60
60
|
});
|
|
61
61
|
}), this.onShow();
|
|
62
62
|
}, this.hide = () => {
|
|
63
63
|
this.element.dataset.show !== "false" && (this.element.dataset.show = "false", this.onHide());
|
|
64
|
-
}, this.element = t.content, l(this,
|
|
64
|
+
}, this.element = t.content, l(this, d, t.debounce ?? 200), l(this, f, t.shouldShow ?? b(this, $, _)), l(this, m, t.offset), l(this, u, t.middleware ?? []), l(this, a, t.floatingUIOptions ?? {}), this.element.dataset.show = "false";
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
d = new WeakMap(), f = new WeakMap(), u = new WeakMap(), a = new WeakMap(), c = new WeakMap(), m = new WeakMap(), g = new WeakMap(), $ = new WeakSet(), /// @internal
|
|
68
68
|
_ = function(t) {
|
|
69
|
-
const { doc: e, selection:
|
|
69
|
+
const { doc: e, selection: o } = t.state, { empty: h, from: p, to: y } = o, x = !e.textBetween(p, y).length && t.state.selection instanceof H, w = this.element.contains(document.activeElement), k = !t.hasFocus() && !w, O = !t.editable;
|
|
70
70
|
return !(k || h || x || O);
|
|
71
71
|
};
|
|
72
|
-
function W(
|
|
72
|
+
function W(s) {
|
|
73
73
|
const t = K(
|
|
74
74
|
{},
|
|
75
|
-
`${
|
|
75
|
+
`${s}_TOOLTIP_SPEC`
|
|
76
76
|
), e = L((h) => {
|
|
77
|
-
const
|
|
77
|
+
const p = h.get(t.key);
|
|
78
78
|
return new M({
|
|
79
|
-
key: new R(`${
|
|
80
|
-
...
|
|
79
|
+
key: new R(`${s}_TOOLTIP`),
|
|
80
|
+
...p
|
|
81
81
|
});
|
|
82
|
-
}),
|
|
83
|
-
return
|
|
82
|
+
}), o = [t, e];
|
|
83
|
+
return o.key = t.key, o.pluginKey = e.key, t.meta = {
|
|
84
84
|
package: "@milkdown/plugin-tooltip",
|
|
85
|
-
displayName: `Ctx<tooltipSpec>|${
|
|
85
|
+
displayName: `Ctx<tooltipSpec>|${s}`
|
|
86
86
|
}, e.meta = {
|
|
87
87
|
package: "@milkdown/plugin-tooltip",
|
|
88
|
-
displayName: `Prose<tooltip>|${
|
|
89
|
-
},
|
|
88
|
+
displayName: `Prose<tooltip>|${s}`
|
|
89
|
+
}, o;
|
|
90
90
|
}
|
|
91
91
|
export {
|
|
92
92
|
V as TooltipProvider,
|
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 debounce from 'lodash.debounce'\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}\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 #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.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 view.dom.parentElement?.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: '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 = debounce(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","_initialized","_offset","_onUpdate","__privateSet","view","prevState","state","composing","selection","doc","ranges","from","range","to","isSame","__privateGet","_a","computePosition","posToDOMRect","flip","offset","shift","x","y","debounce","virtualElement","__privateMethod","_shouldShow_fn","empty","isEmptyTextBlock","TextSelection","isTooltipChildren","notHasFocus","isReadonly","tooltipFactory","id","tooltipSpec","$ctx","tooltipPlugin","$prose","ctx","spec","Plugin","PluginKey","result"],"mappings":";;;;;;;;;;;AAmCO,MAAMA,EAAgB;AAAA,EAkC3B,YAAYC,GAAiC;AAlCxC,IAAAC,EAAA,MAAAC;AAEI;AAAA,IAAAD,EAAA,MAAAE;AAGA;AAAA,IAAAF,EAAA,MAAAG;AAGA;AAAA,IAAAH,EAAA,MAAAI;AAGA;AAAA,IAAAJ,EAAA,MAAAK;AAGT,IAAAL,EAAA,MAAAM;AAGS;AAAA,IAAAN,EAAA,MAAAO;AA4BT,IAAAP,EAAA,MAAAQ;AA/Be,IAAAC,EAAA,MAAAH,GAAA,KAef,KAAA,SAAS,MAAM;AAAA,IAAC,GAGhB,KAAA,SAAS,MAAM;AAAA,IAAC,GAaJG,EAAA,MAAAD,GAAA,CAACE,GAAkBC,MAAkC;;AACzD,YAAA,EAAE,OAAAC,GAAO,WAAAC,EAAA,IAAcH,GACvB,EAAE,WAAAI,GAAW,KAAAC,EAAA,IAAQH,GACrB,EAAE,QAAAI,MAAWF,GACbG,IAAO,KAAK,IAAI,GAAGD,EAAO,IAAI,CAACE,MAAUA,EAAM,MAAM,GAAG,CAAC,GACzDC,IAAK,KAA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AAQ;AACrE,QAAMC,IAAcC;AAAA,IAClB,CAAC;AAAA,IACD,GAAGF,CAAE;AAAA,EACP,GACMG,IAAgBC,EAAO,CAACC,MAAQ;AACpC,UAAMC,IAAOD,EAAI,IAAIJ,EAAY,GAAG;AACpC,WAAO,IAAIM,EAAO;AAAA,MAChB,KAAK,IAAIC,EAAU,GAAGR,CAAE,UAAU;AAAA,MAClC,GAAGM;AAAA,IAAA,CACJ;AAAA,EAAA,CACF,GACKG,IAAS,CAACR,GAAaE,CAAa;AAC1C,SAAAM,EAAO,MAAMR,EAAY,KACzBQ,EAAO,YAAYN,EAAc,KACjCF,EAAY,OAAO;AAAA,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}\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 #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.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 view.dom.parentElement?.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","_initialized","_offset","_onUpdate","__privateSet","view","prevState","state","composing","selection","doc","ranges","from","range","to","isSame","__privateGet","_a","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","PluginKey","result"],"mappings":";;;;;;;;;;;AAmCO,MAAMA,EAAgB;AAAA,EAkC3B,YAAYC,GAAiC;AAlCxC,IAAAC,EAAA,MAAAC;AAEI;AAAA,IAAAD,EAAA,MAAAE;AAGA;AAAA,IAAAF,EAAA,MAAAG;AAGA;AAAA,IAAAH,EAAA,MAAAI;AAGA;AAAA,IAAAJ,EAAA,MAAAK;AAGT,IAAAL,EAAA,MAAAM;AAGS;AAAA,IAAAN,EAAA,MAAAO;AA4BT,IAAAP,EAAA,MAAAQ;AA/Be,IAAAC,EAAA,MAAAH,GAAA,KAef,KAAA,SAAS,MAAM;AAAA,IAAC,GAGhB,KAAA,SAAS,MAAM;AAAA,IAAC,GAaJG,EAAA,MAAAD,GAAA,CAACE,GAAkBC,MAAkC;;AACzD,YAAA,EAAE,OAAAC,GAAO,WAAAC,EAAA,IAAcH,GACvB,EAAE,WAAAI,GAAW,KAAAC,EAAA,IAAQH,GACrB,EAAE,QAAAI,MAAWF,GACbG,IAAO,KAAK,IAAI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,GAEnCE,IAAa,CAAC7B,EAAK;AAEzB,SAAI,EAAA4B,KAAeJ,KAASC,KAAoBI;AAEzC;AC1HJ,SAASC,EAA+CC,GAAQ;AACrE,QAAMC,IAAcC;AAAA,IAClB,CAAC;AAAA,IACD,GAAGF,CAAE;AAAA,EACP,GACMG,IAAgBC,EAAO,CAACC,MAAQ;AACpC,UAAMC,IAAOD,EAAI,IAAIJ,EAAY,GAAG;AACpC,WAAO,IAAIM,EAAO;AAAA,MAChB,KAAK,IAAIC,EAAU,GAAGR,CAAE,UAAU;AAAA,MAClC,GAAGM;AAAA,IAAA,CACJ;AAAA,EAAA,CACF,GACKG,IAAS,CAACR,GAAaE,CAAa;AAC1C,SAAAM,EAAO,MAAMR,EAAY,KACzBQ,EAAO,YAAYN,EAAc,KACjCF,EAAY,OAAO;AAAA,IACjB,SAAS;AAAA,IACT,aAAa,oBAAoBD,CAAE;AAAA,EACrC,GACAG,EAAc,OAAO;AAAA,IACnB,SAAS;AAAA,IACT,aAAa,kBAAkBH,CAAE;AAAA,EACnC,GAEOS;AACT;"}
|
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.6.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -31,17 +31,17 @@
|
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@floating-ui/dom": "^1.5.1",
|
|
34
|
-
"@types/lodash.
|
|
35
|
-
"lodash.
|
|
34
|
+
"@types/lodash.throttle": "^4.1.9",
|
|
35
|
+
"lodash.throttle": "^4.1.1",
|
|
36
36
|
"tippy.js": "^6.3.7",
|
|
37
|
-
"tslib": "^2.
|
|
38
|
-
"@milkdown/
|
|
39
|
-
"@milkdown/
|
|
37
|
+
"tslib": "^2.8.1",
|
|
38
|
+
"@milkdown/utils": "7.6.1",
|
|
39
|
+
"@milkdown/exception": "7.6.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@milkdown/
|
|
43
|
-
"@milkdown/
|
|
44
|
-
"@milkdown/
|
|
42
|
+
"@milkdown/ctx": "7.6.1",
|
|
43
|
+
"@milkdown/prose": "7.6.1",
|
|
44
|
+
"@milkdown/core": "7.6.1"
|
|
45
45
|
},
|
|
46
46
|
"nx": {
|
|
47
47
|
"targets": {
|
package/src/tooltip-provider.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EditorState } from '@milkdown/prose/state'
|
|
2
2
|
import { TextSelection } from '@milkdown/prose/state'
|
|
3
3
|
import type { EditorView } from '@milkdown/prose/view'
|
|
4
|
-
import
|
|
4
|
+
import throttle from 'lodash.throttle'
|
|
5
5
|
import type {
|
|
6
6
|
ComputePositionConfig,
|
|
7
7
|
Middleware,
|
|
@@ -103,7 +103,7 @@ export class TooltipProvider {
|
|
|
103
103
|
getBoundingClientRect: () => posToDOMRect(view, from, to),
|
|
104
104
|
}
|
|
105
105
|
computePosition(virtualEl, this.element, {
|
|
106
|
-
placement: 'top',
|
|
106
|
+
placement: this.#floatingUIOptions.placement ?? 'top',
|
|
107
107
|
middleware: [flip(), offset(this.#offset), shift(), ...this.#middleware],
|
|
108
108
|
}).then(({ x, y }) => {
|
|
109
109
|
Object.assign(this.element.style, {
|
|
@@ -117,7 +117,7 @@ export class TooltipProvider {
|
|
|
117
117
|
|
|
118
118
|
/// Update provider state by editor view.
|
|
119
119
|
update = (view: EditorView, prevState?: EditorState): void => {
|
|
120
|
-
const updater =
|
|
120
|
+
const updater = throttle(this.#onUpdate, this.#debounce)
|
|
121
121
|
|
|
122
122
|
updater(view, prevState)
|
|
123
123
|
}
|