@milkdown/plugin-tooltip 7.17.2 → 7.18.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.js CHANGED
@@ -1,4 +1,4 @@
1
- import { computePosition, flip, offset, shift } from "@floating-ui/dom";
1
+ import { computePosition, flip, offset, shift, autoUpdate } from "@floating-ui/dom";
2
2
  import { posToDOMRect } from "@milkdown/prose";
3
3
  import { TextSelection, Plugin, PluginKey } from "@milkdown/prose/state";
4
4
  import { throttle } from "lodash-es";
@@ -10,6 +10,26 @@ class TooltipProvider {
10
10
  };
11
11
  this.onHide = () => {
12
12
  };
13
+ this.#updatePosition = (reference) => {
14
+ computePosition(reference, this.element, {
15
+ placement: this.#floatingUIOptions.placement ?? "top",
16
+ middleware: [
17
+ flip(),
18
+ offset(this.#offset),
19
+ shift(this.#shift),
20
+ ...this.#middleware
21
+ ],
22
+ ...this.#floatingUIOptions
23
+ }).then(({ x, y }) => {
24
+ Object.assign(this.element.style, {
25
+ left: `${x}px`,
26
+ top: `${y}px`
27
+ });
28
+ }).catch(console.error);
29
+ };
30
+ this.#shouldAutoUpdate = (editorView) => {
31
+ return this.#root !== editorView.dom.parentElement;
32
+ };
13
33
  this.#onUpdate = (view, prevState) => {
14
34
  const { state, composing } = view;
15
35
  const { selection, doc } = state;
@@ -23,53 +43,49 @@ class TooltipProvider {
23
43
  this.#initialized = true;
24
44
  }
25
45
  if (composing || isSame) return;
46
+ this.#cleanupAutoUpdate?.();
47
+ this.#cleanupAutoUpdate = void 0;
26
48
  if (!this.#shouldShow(view, prevState)) {
27
49
  this.hide();
28
50
  return;
29
51
  }
30
52
  const virtualEl = {
31
- getBoundingClientRect: () => posToDOMRect(view, from, to)
53
+ getBoundingClientRect: () => posToDOMRect(view, from, to),
54
+ contextElement: view.dom
32
55
  };
33
- computePosition(virtualEl, this.element, {
34
- placement: this.#floatingUIOptions.placement ?? "top",
35
- middleware: [
36
- flip(),
37
- offset(this.#offset),
38
- shift(this.#shift),
39
- ...this.#middleware
40
- ]
41
- }).then(({ x, y }) => {
42
- Object.assign(this.element.style, {
43
- left: `${x}px`,
44
- top: `${y}px`
45
- });
46
- }).catch(console.error);
56
+ if (this.#shouldAutoUpdate(view)) {
57
+ this.#cleanupAutoUpdate = autoUpdate(
58
+ virtualEl,
59
+ this.element,
60
+ () => this.#updatePosition(virtualEl)
61
+ );
62
+ } else {
63
+ this.#updatePosition(virtualEl);
64
+ }
47
65
  this.show();
48
66
  };
49
67
  this.update = (view, prevState) => {
50
68
  this.#updater(view, prevState);
51
69
  };
52
70
  this.destroy = () => {
71
+ this.#cleanupAutoUpdate?.();
53
72
  this.#updater.cancel();
54
73
  };
55
- this.show = (virtualElement) => {
74
+ this.show = (virtualElement, editorView) => {
56
75
  this.element.dataset.show = "true";
57
76
  if (virtualElement) {
58
- computePosition(virtualElement, this.element, {
59
- placement: "top",
60
- middleware: [
61
- flip(),
62
- offset(this.#offset),
63
- shift(this.#shift),
64
- ...this.#middleware
65
- ],
66
- ...this.#floatingUIOptions
67
- }).then(({ x, y }) => {
68
- Object.assign(this.element.style, {
69
- left: `${x}px`,
70
- top: `${y}px`
71
- });
72
- }).catch(console.error);
77
+ this.#cleanupAutoUpdate?.();
78
+ this.#cleanupAutoUpdate = void 0;
79
+ const reference = { ...virtualElement, contextElement: editorView?.dom };
80
+ if (editorView && this.#shouldAutoUpdate(editorView)) {
81
+ this.#cleanupAutoUpdate = autoUpdate(
82
+ reference,
83
+ this.element,
84
+ () => this.#updatePosition(reference)
85
+ );
86
+ } else {
87
+ this.#updatePosition(reference);
88
+ }
73
89
  }
74
90
  this.onShow();
75
91
  };
@@ -101,11 +117,15 @@ class TooltipProvider {
101
117
  #root;
102
118
  #initialized;
103
119
  /// @internal
120
+ #cleanupAutoUpdate;
121
+ /// @internal
104
122
  #offset;
105
123
  /// @internal
106
124
  #shift;
107
125
  /// @internal
108
126
  #updater;
127
+ #updatePosition;
128
+ #shouldAutoUpdate;
109
129
  #onUpdate;
110
130
  /// @internal
111
131
  #_shouldShow(view) {
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 { computePosition, flip, offset, shift } 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 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 /// @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: [\n flip(),\n offset(this.#offset),\n shift(this.#shift),\n ...this.#middleware,\n ],\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 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.#updater.cancel()\n }\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: [\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 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 tooltipP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AA,EAAA;AAGnC,SAAO;AACT;"}
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,cAA8B;AAC/C,sBAAgB,WAAW,KAAK,SAAS;AAAA,QACvC,WAAW,KAAK,mBAAmB,aAAa;AAAA,QAChD,YAAY;AAAA,UACV,KAAA;AAAA,UACA,OAAO,KAAK,OAAO;AAAA,UACnB,MAAM,KAAK,MAAM;AAAA,UACjB,GAAG,KAAK;AAAA,QAAA;AAAA,QAEV,GAAG,KAAK;AAAA,MAAA,CACT,EACE,KAAK,CAAC,EAAE,GAAG,QAAQ;AAClB,eAAO,OAAO,KAAK,QAAQ,OAAO;AAAA,UAChC,MAAM,GAAG,CAAC;AAAA,UACV,KAAK,GAAG,CAAC;AAAA,QAAA,CACV;AAAA,MACH,CAAC,EACA,MAAM,QAAQ,KAAK;AAAA,IACxB;AAGA,SAAA,oBAAoB,CAAC,eAA2B;AAC9C,aAAO,KAAK,UAAU,WAAW,IAAI;AAAA,IACvC;AAGA,SAAA,YAAY,CAAC,MAAkB,cAAkC;AAC/D,YAAM,EAAE,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BAAoB,KAAK,QAAQ,SAAS,SAAS,aAAa;AAEtE,UAAM,cAAc,CAAC,KAAK,SAAA,KAAc,CAAC;AAEzC,UAAM,aAAa,CAAC,KAAK;AAEzB,QAAI,eAAe,SAAS,oBAAoB,WAAY,QAAO;AAEnE,WAAO;AAAA,EACT;AAqCF;AC9MO,SAAS,eAA+C,IAAQ;AACrE,QAAM,cAAc;AAAA,IAClB,CAAA;AAAA,IACA,GAAG,EAAE;AAAA,EAAA;AAEP,QAAM,gBAAgB,OAAO,CAAC,QAAQ;AACpC,UAAM,OAAO,IAAI,IAAI,YAAY,GAAG;AACpC,WAAO,IAAI,OAAO;AAAA,MAChB,KAAK,IAAI,UAAU,GAAG,EAAE,UAAU;AAAA,MAClC,GAAG;AAAA,IAAA,CACJ;AAAA,EACH,CAAC;AACD,QAAM,SAAS,CAAC,aAAa,aAAa;AAC1C,SAAO,MAAM,YAAY;AACzB,SAAO,YAAY,cAAc;AACjC,cAAY,OAAO;AAAA,IACjB,SAAS;AAAA,IACT,aAAa,oBAAoB,EAAE;AAAA,EAAA;AAErC,gBAAc,OAAO;AAAA,IACnB,SAAS;AAAA,IACT,aAAa,kBAAkB,EAAE;AAAA,EAAA;AAGnC,SAAO;AACT;"}
@@ -19,7 +19,7 @@ export declare class TooltipProvider {
19
19
  constructor(options: TooltipProviderOptions);
20
20
  update: (view: EditorView, prevState?: EditorState) => void;
21
21
  destroy: () => void;
22
- show: (virtualElement?: VirtualElement) => void;
22
+ show: (virtualElement?: VirtualElement, editorView?: EditorView) => void;
23
23
  hide: () => void;
24
24
  }
25
25
  //# sourceMappingURL=tooltip-provider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tooltip-provider.d.ts","sourceRoot":"","sources":["../src/tooltip-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,UAAU,EACV,aAAa,EACb,YAAY,EACZ,cAAc,EACf,MAAM,kBAAkB,CAAA;AACzB,OAAO,KAAK,EAAE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