@milkdown/plugin-slash 7.3.6 → 7.5.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.d.ts.map +1 -1
- package/lib/index.es.js +54 -57
- package/lib/index.es.js.map +1 -1
- package/lib/slash-plugin.d.ts.map +1 -1
- package/lib/slash-provider.d.ts +7 -3
- package/lib/slash-provider.d.ts.map +1 -1
- package/package.json +10 -16
- package/src/index.ts +0 -1
- package/src/slash-plugin.ts +0 -1
- package/src/slash-provider.ts +45 -30
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA"}
|
package/lib/index.es.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
var
|
|
1
|
+
var P = (s, e, t) => {
|
|
2
2
|
if (!e.has(s))
|
|
3
3
|
throw TypeError("Cannot " + t);
|
|
4
4
|
};
|
|
5
|
-
var o = (s, e, t) => (
|
|
5
|
+
var o = (s, e, t) => (P(s, e, "read from private field"), t ? t.call(s) : e.get(s)), a = (s, e, t) => {
|
|
6
6
|
if (e.has(s))
|
|
7
7
|
throw TypeError("Cannot add the same private member more than once");
|
|
8
8
|
e instanceof WeakSet ? e.add(s) : e.set(s, t);
|
|
9
|
-
},
|
|
10
|
-
var
|
|
11
|
-
import { Plugin as
|
|
12
|
-
import { $ctx as
|
|
13
|
-
import {
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
function
|
|
17
|
-
const e =
|
|
18
|
-
const
|
|
19
|
-
return new
|
|
20
|
-
key: new
|
|
21
|
-
...
|
|
9
|
+
}, r = (s, e, t, n) => (P(s, e, "write to private field"), n ? n.call(s, t) : e.set(s, t), t);
|
|
10
|
+
var E = (s, e, t) => (P(s, e, "access private method"), t);
|
|
11
|
+
import { Plugin as B, PluginKey as M, TextSelection as N } from "@milkdown/prose/state";
|
|
12
|
+
import { $ctx as O, $prose as T } from "@milkdown/utils";
|
|
13
|
+
import { findParentNode as _, posToDOMRect as F } from "@milkdown/prose";
|
|
14
|
+
import R from "lodash.debounce";
|
|
15
|
+
import { computePosition as q, flip as K, offset as L } from "@floating-ui/dom";
|
|
16
|
+
function J(s) {
|
|
17
|
+
const e = O({}, `${s}_SLASH_SPEC`), t = T((i) => {
|
|
18
|
+
const m = i.get(e.key);
|
|
19
|
+
return new B({
|
|
20
|
+
key: new M(`${s}_SLASH`),
|
|
21
|
+
...m
|
|
22
22
|
});
|
|
23
23
|
}), n = [e, t];
|
|
24
24
|
return n.key = e.key, n.pluginKey = t.key, e.meta = {
|
|
@@ -29,61 +29,58 @@ function D(s) {
|
|
|
29
29
|
displayName: `Prose<slash>|${s}`
|
|
30
30
|
}, n;
|
|
31
31
|
}
|
|
32
|
-
var
|
|
33
|
-
class
|
|
32
|
+
var l, u, h, d, f, g, S, H;
|
|
33
|
+
class Q {
|
|
34
34
|
constructor(e) {
|
|
35
35
|
/// @internal
|
|
36
|
-
a(this,
|
|
37
|
-
|
|
38
|
-
a(this, i, void 0);
|
|
36
|
+
a(this, S);
|
|
37
|
+
a(this, l, void 0);
|
|
39
38
|
/// @internal
|
|
40
39
|
a(this, u, void 0);
|
|
41
40
|
/// @internal
|
|
42
|
-
a(this, d, void 0);
|
|
43
|
-
/// @internal
|
|
44
41
|
a(this, h, void 0);
|
|
45
42
|
/// @internal
|
|
46
|
-
a(this,
|
|
43
|
+
a(this, d, void 0);
|
|
44
|
+
/// The offset to get the block. Default is 0.
|
|
47
45
|
a(this, f, void 0);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
})), !(p || C)) {
|
|
60
|
-
if (!o(this, g).call(this, t, n)) {
|
|
61
|
-
this.hide();
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
o(this, i).setProps({
|
|
65
|
-
getReferenceClientRect: () => T(t, x, P)
|
|
66
|
-
}), this.show();
|
|
46
|
+
a(this, g, void 0);
|
|
47
|
+
r(this, l, !1), this.onShow = () => {
|
|
48
|
+
}, this.onHide = () => {
|
|
49
|
+
}, r(this, g, (t, n) => {
|
|
50
|
+
var b;
|
|
51
|
+
const { state: i, composing: m } = t, { selection: c, doc: k } = i, { ranges: y } = c, x = Math.min(...y.map((p) => p.$from.pos)), $ = Math.max(...y.map((p) => p.$to.pos)), w = n && n.doc.eq(k) && n.selection.eq(c);
|
|
52
|
+
if (o(this, l) || ((b = t.dom.parentElement) == null || b.appendChild(this.element), r(this, l, !0)), m || w)
|
|
53
|
+
return;
|
|
54
|
+
if (!o(this, d).call(this, t, n)) {
|
|
55
|
+
this.hide();
|
|
56
|
+
return;
|
|
67
57
|
}
|
|
58
|
+
q({
|
|
59
|
+
getBoundingClientRect: () => F(t, x, $)
|
|
60
|
+
}, this.element, {
|
|
61
|
+
placement: "bottom-start",
|
|
62
|
+
middleware: [K(), L(o(this, f))]
|
|
63
|
+
}).then(({ x: p, y: A }) => {
|
|
64
|
+
Object.assign(this.element.style, {
|
|
65
|
+
left: `${p}px`,
|
|
66
|
+
top: `${A}px`
|
|
67
|
+
});
|
|
68
|
+
}), this.show();
|
|
68
69
|
}), this.update = (t, n) => {
|
|
69
|
-
|
|
70
|
-
}, this.getContent = (t, n = (
|
|
71
|
-
const { selection:
|
|
72
|
-
if (!(x ||
|
|
73
|
-
return
|
|
70
|
+
R(o(this, g), o(this, u))(t, n);
|
|
71
|
+
}, this.getContent = (t, n = (i) => i.type.name === "paragraph") => {
|
|
72
|
+
const { selection: i } = t.state, { empty: m, $from: c } = i, k = t.state.selection instanceof N, y = this.element.contains(document.activeElement), x = !t.hasFocus() && !y, $ = !t.editable, C = !_(n)(t.state.selection);
|
|
73
|
+
if (!(x || $ || !m || !k || C))
|
|
74
|
+
return c.parent.textBetween(Math.max(0, c.parentOffset - 500), c.parentOffset, void 0, "");
|
|
74
75
|
}, this.destroy = () => {
|
|
75
|
-
var t;
|
|
76
|
-
(t = o(this, i)) == null || t.destroy();
|
|
77
76
|
}, this.show = () => {
|
|
78
|
-
|
|
79
|
-
(t = o(this, i)) == null || t.show();
|
|
77
|
+
this.element.dataset.show = "true", this.onShow();
|
|
80
78
|
}, this.hide = () => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}, this.getInstance = () => o(this, i), this.element = e.content, c(this, u, e.tippyOptions ?? {}), c(this, d, e.debounce ?? 200), c(this, g, e.shouldShow ?? b(this, k, w)), c(this, h, e.trigger ?? "/");
|
|
79
|
+
this.element.dataset.show = "false", this.onHide();
|
|
80
|
+
}, this.element = e.content, r(this, u, e.debounce ?? 200), r(this, d, e.shouldShow ?? E(this, S, H)), r(this, h, e.trigger ?? "/"), r(this, f, e.offset);
|
|
84
81
|
}
|
|
85
82
|
}
|
|
86
|
-
|
|
83
|
+
l = new WeakMap(), u = new WeakMap(), h = new WeakMap(), d = new WeakMap(), f = new WeakMap(), g = new WeakMap(), S = new WeakSet(), H = function(e) {
|
|
87
84
|
const t = this.getContent(e);
|
|
88
85
|
if (!t)
|
|
89
86
|
return !1;
|
|
@@ -91,7 +88,7 @@ i = new WeakMap(), u = new WeakMap(), d = new WeakMap(), h = new WeakMap(), g =
|
|
|
91
88
|
return n ? Array.isArray(o(this, h)) ? o(this, h).includes(n) : o(this, h) === n : !1;
|
|
92
89
|
};
|
|
93
90
|
export {
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
Q as SlashProvider,
|
|
92
|
+
J as slashFactory
|
|
96
93
|
};
|
|
97
94
|
//# sourceMappingURL=index.es.js.map
|
package/lib/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/slash-plugin.ts","../src/slash-provider.ts"],"sourcesContent":["/* Copyright 2021, Milkdown by Mirone. */\nimport 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 SlashPluginSpecId<Id extends string> = `${Id}_SLASH_SPEC`\n\n/// @internal\nexport type SlashPlugin<Id extends string, State = any> = [$Ctx<PluginSpec<State>, SlashPluginSpecId<Id>>, $Prose] & {\n key: SliceType<PluginSpec<State>, SlashPluginSpecId<Id>>\n pluginKey: $Prose['key']\n}\n\n/// Create a slash plugin with a unique id.\nexport function slashFactory<Id extends string, State = any>(id: Id) {\n const slashSpec = $ctx<PluginSpec<State>, SlashPluginSpecId<Id>>({}, `${id}_SLASH_SPEC`)\n const slashPlugin = $prose((ctx) => {\n const spec = ctx.get(slashSpec.key)\n return new Plugin({\n key: new PluginKey(`${id}_SLASH`),\n ...spec,\n })\n })\n const result = [slashSpec, slashPlugin] as SlashPlugin<Id>\n result.key = slashSpec.key\n result.pluginKey = slashPlugin.key\n slashSpec.meta = {\n package: '@milkdown/plugin-slash',\n displayName: `Ctx<slashSpec>|${id}`,\n }\n slashPlugin.meta = {\n package: '@milkdown/plugin-slash',\n displayName: `Prose<slash>|${id}`,\n }\n\n return result\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport { findParentNode, posToDOMRect } from '@milkdown/prose'\nimport type { EditorState } from '@milkdown/prose/state'\nimport type { Node } from '@milkdown/prose/model'\nimport { TextSelection } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\nimport debounce from 'lodash.debounce'\nimport type { Instance, Props } from 'tippy.js'\nimport tippy from 'tippy.js'\n\n/// Options for slash provider.\nexport interface SlashProviderOptions {\n /// The slash content.\n content: HTMLElement\n /// The options for creating [tippy.js](https://atomiks.github.io/tippyjs/) instance.\n tippyOptions?: Partial<Props>\n /// The debounce time for updating slash, 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 key trigger for shouldShow, '/' by default.\n trigger?: string | string[]\n}\n\n/// A provider for creating slash.\nexport class SlashProvider {\n /// The root element of the slash.\n element: HTMLElement\n\n /// @internal\n #tippy: Instance | undefined\n\n /// @internal\n #tippyOptions: Partial<Props>\n\n /// @internal\n #debounce: number\n\n /// @internal\n #trigger: string | string[]\n\n /// @internal\n #shouldShow: (view: EditorView, prevState?: EditorState) => boolean\n\n constructor(options: SlashProviderOptions) {\n this.element = options.content\n this.#tippyOptions = options.tippyOptions ?? {}\n this.#debounce = options.debounce ?? 200\n this.#shouldShow = options.shouldShow ?? this.#_shouldShow\n this.#trigger = options.trigger ?? '/'\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 = prevState && prevState.doc.eq(doc) && prevState.selection.eq(selection)\n\n this.#tippy ??= tippy(view.dom, {\n trigger: 'manual',\n placement: 'bottom-start',\n interactive: true,\n delay: 0,\n arrow: false,\n duration: 0,\n ...this.#tippyOptions,\n content: this.element,\n })\n\n if (composing || isSame)\n return\n\n if (!this.#shouldShow(view, prevState)) {\n this.hide()\n return\n }\n\n this.#tippy.setProps({\n getReferenceClientRect: () => posToDOMRect(view, from, to),\n })\n\n this.show()\n }\n\n /// @internal\n #_shouldShow(view: EditorView): boolean {\n const currentTextBlockContent = this.getContent(view)\n\n if (!currentTextBlockContent)\n return false\n\n const target = currentTextBlockContent.at(-1)\n\n if (!target)\n return false\n\n return Array.isArray(this.#trigger) ? this.#trigger.includes(target) : this.#trigger === target\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 /// Get the content of the current text block.\n /// Pass the `matchNode` function to determine whether the current node should be matched, by default, it will match the paragraph node.\n getContent = (view: EditorView, matchNode: (node: Node) => boolean = node => node.type.name === 'paragraph'): string | undefined => {\n const { selection } = view.state\n const { empty, $from } = selection\n const isTextBlock = view.state.selection instanceof TextSelection\n\n const isSlashChildren = this.element.contains(document.activeElement)\n\n const notHasFocus = !view.hasFocus() && !isSlashChildren\n\n const isReadonly = !view.editable\n\n const paragraph = findParentNode(matchNode)(view.state.selection)\n\n const isNotInParagraph = !paragraph\n\n if (notHasFocus || isReadonly || !empty || !isTextBlock || isNotInParagraph)\n return\n\n return $from.parent.textBetween(Math.max(0, $from.parentOffset - 500), $from.parentOffset, undefined, '\\uFFFC')\n }\n\n /// Destroy the slash.\n destroy = () => {\n this.#tippy?.destroy()\n }\n\n /// Show the slash.\n show = () => {\n this.#tippy?.show()\n }\n\n /// Hide the slash.\n hide = () => {\n this.#tippy?.hide()\n }\n\n /// Get the [tippy.js](https://atomiks.github.io/tippyjs/) instance.\n getInstance = () => this.#tippy\n}\n"],"names":["slashFactory","id","slashSpec","$ctx","slashPlugin","$prose","ctx","spec","Plugin","PluginKey","result","SlashProvider","options","__privateAdd","__shouldShow","_tippy","_tippyOptions","_debounce","_trigger","_shouldShow","_onUpdate","__privateSet","view","prevState","state","composing","selection","doc","ranges","from","range","to","isSame","__privateGet","tippy","posToDOMRect","debounce","matchNode","node","empty","$from","isTextBlock","TextSelection","isSlashChildren","notHasFocus","isReadonly","isNotInParagraph","findParentNode","_a","__privateMethod","_shouldShow_fn","currentTextBlockContent","target"],"mappings":";;;;;;;;;;;;;;;AAiBO,SAASA,EAA6CC,GAAQ;AACnE,QAAMC,IAAYC,EAA+C,CAAI,GAAA,GAAGF,CAAE,aAAa,GACjFG,IAAcC,EAAO,CAACC,MAAQ;AAClC,UAAMC,IAAOD,EAAI,IAAIJ,EA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vB,KAAA,OAAO,MAAM;;AACX,OAAAA,IAAAf,EAAA,MAAKlB,OAAL,QAAAiC,EAAa;AAAA,IAAK,GAIpB,KAAA,OAAO,MAAM;;AACX,OAAAA,IAAAf,EAAA,MAAKlB,OAAL,QAAAiC,EAAa;AAAA,IAAK,GAIpB,KAAA,cAAc,MAAMf,EAAA,MAAKlB,IAvGvB,KAAK,UAAUH,EAAQ,SAClBS,EAAA,MAAAL,GAAgBJ,EAAQ,gBAAgB,CAAA,IACxCS,EAAA,MAAAJ,GAAYL,EAAQ,YAAY,MAChCS,EAAA,MAAAF,GAAcP,EAAQ,cAAcqC,EAAA,MAAKnC,GAAAoC,KACzC7B,EAAA,MAAAH,GAAWN,EAAQ,WAAW;AAAA,EACrC;AAmGF;AAvHEG,IAAA,eAGAC,IAAA,eAGAC,IAAA,eAGAC,IAAA,eAGAC,IAAA,eAWAC,IAAA,eAmCAN,IAAA,eAAAoC,aAAa5B,GAA2B;AAChC,QAAA6B,IAA0B,KAAK,WAAW7B,CAAI;AAEpD,MAAI,CAAC6B;AACI,WAAA;AAEH,QAAAC,IAASD,EAAwB,GAAG,EAAE;AAE5C,SAAKC,IAGE,MAAM,QAAQnB,EAAA,MAAKf,EAAQ,IAAIe,EAAA,MAAKf,GAAS,SAASkC,CAAM,IAAInB,EAAA,MAAKf,OAAakC,IAFhF;AAGX;"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/slash-plugin.ts","../src/slash-provider.ts"],"sourcesContent":["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 SlashPluginSpecId<Id extends string> = `${Id}_SLASH_SPEC`\n\n/// @internal\nexport type SlashPlugin<Id extends string, State = any> = [$Ctx<PluginSpec<State>, SlashPluginSpecId<Id>>, $Prose] & {\n key: SliceType<PluginSpec<State>, SlashPluginSpecId<Id>>\n pluginKey: $Prose['key']\n}\n\n/// Create a slash plugin with a unique id.\nexport function slashFactory<Id extends string, State = any>(id: Id) {\n const slashSpec = $ctx<PluginSpec<State>, SlashPluginSpecId<Id>>({}, `${id}_SLASH_SPEC`)\n const slashPlugin = $prose((ctx) => {\n const spec = ctx.get(slashSpec.key)\n return new Plugin({\n key: new PluginKey(`${id}_SLASH`),\n ...spec,\n })\n })\n const result = [slashSpec, slashPlugin] as SlashPlugin<Id>\n result.key = slashSpec.key\n result.pluginKey = slashPlugin.key\n slashSpec.meta = {\n package: '@milkdown/plugin-slash',\n displayName: `Ctx<slashSpec>|${id}`,\n }\n slashPlugin.meta = {\n package: '@milkdown/plugin-slash',\n displayName: `Prose<slash>|${id}`,\n }\n\n return result\n}\n","import { findParentNode, posToDOMRect } from '@milkdown/prose'\nimport type { EditorState } from '@milkdown/prose/state'\nimport type { Node } from '@milkdown/prose/model'\nimport { TextSelection } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\nimport debounce from 'lodash.debounce'\nimport type { VirtualElement } from '@floating-ui/dom'\nimport { computePosition, flip, offset } from '@floating-ui/dom'\n\n/// Options for slash provider.\nexport interface SlashProviderOptions {\n /// The slash content.\n content: HTMLElement\n /// The debounce time for updating slash, 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 key trigger for shouldShow, '/' by default.\n trigger?: string | string[]\n /// The offset to get the block. Default is 0.\n offset?: number | {\n mainAxis?: number\n crossAxis?: number\n alignmentAxis?: number | null\n }\n}\n\n/// A provider for creating slash.\nexport class SlashProvider {\n /// The root element of the slash.\n element: HTMLElement\n\n /// @internal\n #initialized = false\n\n /// @internal\n readonly #debounce: number\n\n /// @internal\n readonly #trigger: string | string[]\n\n /// @internal\n readonly #shouldShow: (view: EditorView, prevState?: EditorState) => boolean\n\n /// The offset to get the block. Default is 0.\n readonly #offset?: number | {\n mainAxis?: number\n crossAxis?: number\n alignmentAxis?: number | null\n }\n\n /// On show callback.\n onShow = () => {}\n\n /// On hide callback.\n onHide = () => {}\n\n constructor(options: SlashProviderOptions) {\n this.element = options.content\n this.#debounce = options.debounce ?? 200\n this.#shouldShow = options.shouldShow ?? this.#_shouldShow\n this.#trigger = options.trigger ?? '/'\n this.#offset = options.offset\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 = 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)\n 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: 'bottom-start',\n middleware: [flip(), offset(this.#offset)],\n })\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 /// @internal\n #_shouldShow(view: EditorView): boolean {\n const currentTextBlockContent = this.getContent(view)\n\n if (!currentTextBlockContent)\n return false\n\n const target = currentTextBlockContent.at(-1)\n\n if (!target)\n return false\n\n return Array.isArray(this.#trigger) ? this.#trigger.includes(target) : this.#trigger === target\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 /// Get the content of the current text block.\n /// Pass the `matchNode` function to determine whether the current node should be matched, by default, it will match the paragraph node.\n getContent = (view: EditorView, matchNode: (node: Node) => boolean = node => node.type.name === 'paragraph'): string | undefined => {\n const { selection } = view.state\n const { empty, $from } = selection\n const isTextBlock = view.state.selection instanceof TextSelection\n\n const isSlashChildren = this.element.contains(document.activeElement)\n\n const notHasFocus = !view.hasFocus() && !isSlashChildren\n\n const isReadonly = !view.editable\n\n const paragraph = findParentNode(matchNode)(view.state.selection)\n\n const isNotInParagraph = !paragraph\n\n if (notHasFocus || isReadonly || !empty || !isTextBlock || isNotInParagraph)\n return\n\n return $from.parent.textBetween(Math.max(0, $from.parentOffset - 500), $from.parentOffset, undefined, '\\uFFFC')\n }\n\n /// Destroy the slash.\n destroy = () => {\n }\n\n /// Show the slash.\n show = () => {\n this.element.dataset.show = 'true'\n this.onShow()\n }\n\n /// Hide the slash.\n hide = () => {\n this.element.dataset.show = 'false'\n this.onHide()\n }\n}\n"],"names":["slashFactory","id","slashSpec","$ctx","slashPlugin","$prose","ctx","spec","Plugin","PluginKey","result","SlashProvider","options","__privateAdd","__shouldShow","_initialized","_debounce","_trigger","_shouldShow","_offset","_onUpdate","__privateSet","view","prevState","state","composing","selection","doc","ranges","from","range","to","isSame","__privateGet","_a","computePosition","posToDOMRect","flip","offset","x","y","debounce","matchNode","node","empty","$from","isTextBlock","TextSelection","isSlashChildren","notHasFocus","isReadonly","isNotInParagraph","findParentNode","__privateMethod","_shouldShow_fn","currentTextBlockContent","target"],"mappings":";;;;;;;;;;;;;;;AAgBO,SAASA,EAA6CC,GAAQ;AACnE,QAAMC,IAAYC,EAA+C,CAAI,GAAA,GAAGF,CAAE,aAAa,GACjFG,IAAcC,EAAO,CAACC,MAAQ;AAClC,UAAMC,IAAOD,EAAI,IAAIJ,EAAU,GAAG;AAClC,WAAO,IAAIM,EAAO;AAAA,MAChB,KAAK,IAAIC,EAAU,GAAGR,CAAE,QAAQ;AAAA,MAChC,GAAGM;AAAA,IAAA,CACJ;AAAA,EAAA,CACF,GACKG,IAAS,CAACR,GAAWE,CAAW;AACtC,SAAAM,EAAO,MAAMR,EAAU,KACvBQ,EAAO,YAAYN,EAAY,KAC/BF,EAAU,OAAO;AAAA,IACf,SAAS;AAAA,IACT,aAA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ACN,WAAA,QAAQ,QAAQ,OAAO,SAC5B,KAAK,OAAO;AAAA,IAAA,GAxGZ,KAAK,UAAUjC,EAAQ,SAClBS,EAAA,MAAAL,GAAYJ,EAAQ,YAAY,MAChCS,EAAA,MAAAH,GAAcN,EAAQ,cAAcyC,EAAA,MAAKvC,GAAAwC,KACzCjC,EAAA,MAAAJ,GAAWL,EAAQ,WAAW,MACnCS,EAAA,MAAKF,GAAUP,EAAQ;AAAA,EACzB;AAqGF;AAnIEG,IAAA,eAGSC,IAAA,eAGAC,IAAA,eAGAC,IAAA,eAGAC,IAAA,eAqBTC,IAAA,eAuCAN,IAAA,eAAAwC,aAAahC,GAA2B;AAChC,QAAAiC,IAA0B,KAAK,WAAWjC,CAAI;AAEpD,MAAI,CAACiC;AACI,WAAA;AAEH,QAAAC,IAASD,EAAwB,GAAG,EAAE;AAE5C,SAAKC,IAGE,MAAM,QAAQvB,EAAA,MAAKhB,EAAQ,IAAIgB,EAAA,MAAKhB,GAAS,SAASuC,CAAM,IAAIvB,EAAA,MAAKhB,OAAauC,IAFhF;AAGX;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slash-plugin.d.ts","sourceRoot":"","sources":["../src/slash-plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"slash-plugin.d.ts","sourceRoot":"","sources":["../src/slash-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAEvD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAInD,MAAM,MAAM,iBAAiB,CAAC,EAAE,SAAS,MAAM,IAAI,GAAG,EAAE,aAAa,CAAA;AAGrE,MAAM,MAAM,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,KAAK,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,iBAAiB,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG;IACnH,GAAG,EAAE,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,iBAAiB,CAAC,EAAE,CAAC,CAAC,CAAA;IACxD,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;CACzB,CAAA;AAGD,wBAAgB,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,KAAK,GAAG,GAAG,EAAE,EAAE,EAAE,EAAE,wBAsBlE"}
|
package/lib/slash-provider.d.ts
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import type { EditorState } from '@milkdown/prose/state';
|
|
2
2
|
import type { Node } from '@milkdown/prose/model';
|
|
3
3
|
import type { EditorView } from '@milkdown/prose/view';
|
|
4
|
-
import type { Instance, Props } from 'tippy.js';
|
|
5
4
|
export interface SlashProviderOptions {
|
|
6
5
|
content: HTMLElement;
|
|
7
|
-
tippyOptions?: Partial<Props>;
|
|
8
6
|
debounce?: number;
|
|
9
7
|
shouldShow?: (view: EditorView, prevState?: EditorState) => boolean;
|
|
10
8
|
trigger?: string | string[];
|
|
9
|
+
offset?: number | {
|
|
10
|
+
mainAxis?: number;
|
|
11
|
+
crossAxis?: number;
|
|
12
|
+
alignmentAxis?: number | null;
|
|
13
|
+
};
|
|
11
14
|
}
|
|
12
15
|
export declare class SlashProvider {
|
|
13
16
|
#private;
|
|
14
17
|
element: HTMLElement;
|
|
18
|
+
onShow: () => void;
|
|
19
|
+
onHide: () => void;
|
|
15
20
|
constructor(options: SlashProviderOptions);
|
|
16
21
|
update: (view: EditorView, prevState?: EditorState) => void;
|
|
17
22
|
getContent: (view: EditorView, matchNode?: (node: Node) => boolean) => string | undefined;
|
|
18
23
|
destroy: () => void;
|
|
19
24
|
show: () => void;
|
|
20
25
|
hide: () => void;
|
|
21
|
-
getInstance: () => Instance<Props> | undefined;
|
|
22
26
|
}
|
|
23
27
|
//# sourceMappingURL=slash-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slash-provider.d.ts","sourceRoot":"","sources":["../src/slash-provider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"slash-provider.d.ts","sourceRoot":"","sources":["../src/slash-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAEjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAMtD,MAAM,WAAW,oBAAoB;IAEnC,OAAO,EAAE,WAAW,CAAA;IAEpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,CAAC,EAAE,WAAW,KAAK,OAAO,CAAA;IAEnE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAE3B,MAAM,CAAC,EAAE,MAAM,GAAG;QAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,SAAS,CAAC,EAAE,MAAM,CAAA;QAClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAC9B,CAAA;CACF;AAGD,qBAAa,aAAa;;IAExB,OAAO,EAAE,WAAW,CAAA;IAsBpB,MAAM,aAAW;IAGjB,MAAM,aAAW;gBAEL,OAAO,EAAE,oBAAoB;IA+DzC,MAAM,SAAU,UAAU,cAAc,WAAW,KAAG,IAAI,CAIzD;IAID,UAAU,SAAU,UAAU,cAAa,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,KAA4C,MAAM,GAAG,SAAS,CAmB/H;IAGD,OAAO,aACN;IAGD,IAAI,aAGH;IAGD,IAAI,aAGH;CACF"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkdown/plugin-slash",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.
|
|
4
|
+
"version": "7.5.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/Milkdown/milkdown.git",
|
|
9
|
-
"directory": "packages/plugin-slash"
|
|
9
|
+
"directory": "packages/plugins/plugin-slash"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
12
|
"milkdown",
|
|
@@ -30,17 +30,17 @@
|
|
|
30
30
|
"@milkdown/prose": "^7.2.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
+
"@floating-ui/dom": "^1.5.1",
|
|
33
34
|
"@types/lodash.debounce": "^4.0.7",
|
|
34
35
|
"lodash.debounce": "^4.0.8",
|
|
35
|
-
"tippy.js": "^6.3.7",
|
|
36
36
|
"tslib": "^2.5.0",
|
|
37
|
-
"@milkdown/exception": "7.
|
|
38
|
-
"@milkdown/utils": "7.
|
|
37
|
+
"@milkdown/exception": "7.5.0",
|
|
38
|
+
"@milkdown/utils": "7.5.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@milkdown/core": "7.
|
|
42
|
-
"@milkdown/ctx": "7.
|
|
43
|
-
"@milkdown/prose": "7.
|
|
41
|
+
"@milkdown/core": "7.5.0",
|
|
42
|
+
"@milkdown/ctx": "7.5.0",
|
|
43
|
+
"@milkdown/prose": "7.5.0"
|
|
44
44
|
},
|
|
45
45
|
"nx": {
|
|
46
46
|
"targets": {
|
|
@@ -49,19 +49,13 @@
|
|
|
49
49
|
"{projectRoot}/lib"
|
|
50
50
|
],
|
|
51
51
|
"dependsOn": [
|
|
52
|
-
|
|
53
|
-
"target": "build",
|
|
54
|
-
"projects": "dependencies"
|
|
55
|
-
}
|
|
52
|
+
"build"
|
|
56
53
|
]
|
|
57
54
|
},
|
|
58
55
|
"tsc": {
|
|
59
56
|
"outputs": [],
|
|
60
57
|
"dependsOn": [
|
|
61
|
-
|
|
62
|
-
"target": "build",
|
|
63
|
-
"projects": "dependencies"
|
|
64
|
-
}
|
|
58
|
+
"build"
|
|
65
59
|
]
|
|
66
60
|
}
|
|
67
61
|
}
|
package/src/index.ts
CHANGED
package/src/slash-plugin.ts
CHANGED
package/src/slash-provider.ts
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
1
|
import { findParentNode, posToDOMRect } from '@milkdown/prose'
|
|
3
2
|
import type { EditorState } from '@milkdown/prose/state'
|
|
4
3
|
import type { Node } from '@milkdown/prose/model'
|
|
5
4
|
import { TextSelection } from '@milkdown/prose/state'
|
|
6
5
|
import type { EditorView } from '@milkdown/prose/view'
|
|
7
6
|
import debounce from 'lodash.debounce'
|
|
8
|
-
import type {
|
|
9
|
-
import
|
|
7
|
+
import type { VirtualElement } from '@floating-ui/dom'
|
|
8
|
+
import { computePosition, flip, offset } from '@floating-ui/dom'
|
|
10
9
|
|
|
11
10
|
/// Options for slash provider.
|
|
12
11
|
export interface SlashProviderOptions {
|
|
13
12
|
/// The slash content.
|
|
14
13
|
content: HTMLElement
|
|
15
|
-
/// The options for creating [tippy.js](https://atomiks.github.io/tippyjs/) instance.
|
|
16
|
-
tippyOptions?: Partial<Props>
|
|
17
14
|
/// The debounce time for updating slash, 200ms by default.
|
|
18
15
|
debounce?: number
|
|
19
16
|
/// The function to determine whether the tooltip should be shown.
|
|
20
17
|
shouldShow?: (view: EditorView, prevState?: EditorState) => boolean
|
|
21
18
|
/// The key trigger for shouldShow, '/' by default.
|
|
22
19
|
trigger?: string | string[]
|
|
20
|
+
/// The offset to get the block. Default is 0.
|
|
21
|
+
offset?: number | {
|
|
22
|
+
mainAxis?: number
|
|
23
|
+
crossAxis?: number
|
|
24
|
+
alignmentAxis?: number | null
|
|
25
|
+
}
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
/// A provider for creating slash.
|
|
@@ -28,26 +31,36 @@ export class SlashProvider {
|
|
|
28
31
|
element: HTMLElement
|
|
29
32
|
|
|
30
33
|
/// @internal
|
|
31
|
-
#
|
|
34
|
+
#initialized = false
|
|
32
35
|
|
|
33
36
|
/// @internal
|
|
34
|
-
#
|
|
37
|
+
readonly #debounce: number
|
|
35
38
|
|
|
36
39
|
/// @internal
|
|
37
|
-
#
|
|
40
|
+
readonly #trigger: string | string[]
|
|
38
41
|
|
|
39
42
|
/// @internal
|
|
40
|
-
#
|
|
43
|
+
readonly #shouldShow: (view: EditorView, prevState?: EditorState) => boolean
|
|
41
44
|
|
|
42
|
-
///
|
|
43
|
-
#
|
|
45
|
+
/// The offset to get the block. Default is 0.
|
|
46
|
+
readonly #offset?: number | {
|
|
47
|
+
mainAxis?: number
|
|
48
|
+
crossAxis?: number
|
|
49
|
+
alignmentAxis?: number | null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// On show callback.
|
|
53
|
+
onShow = () => {}
|
|
54
|
+
|
|
55
|
+
/// On hide callback.
|
|
56
|
+
onHide = () => {}
|
|
44
57
|
|
|
45
58
|
constructor(options: SlashProviderOptions) {
|
|
46
59
|
this.element = options.content
|
|
47
|
-
this.#tippyOptions = options.tippyOptions ?? {}
|
|
48
60
|
this.#debounce = options.debounce ?? 200
|
|
49
61
|
this.#shouldShow = options.shouldShow ?? this.#_shouldShow
|
|
50
62
|
this.#trigger = options.trigger ?? '/'
|
|
63
|
+
this.#offset = options.offset
|
|
51
64
|
}
|
|
52
65
|
|
|
53
66
|
/// @internal
|
|
@@ -59,16 +72,10 @@ export class SlashProvider {
|
|
|
59
72
|
const to = Math.max(...ranges.map(range => range.$to.pos))
|
|
60
73
|
const isSame = prevState && prevState.doc.eq(doc) && prevState.selection.eq(selection)
|
|
61
74
|
|
|
62
|
-
this.#
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
delay: 0,
|
|
67
|
-
arrow: false,
|
|
68
|
-
duration: 0,
|
|
69
|
-
...this.#tippyOptions,
|
|
70
|
-
content: this.element,
|
|
71
|
-
})
|
|
75
|
+
if (!this.#initialized) {
|
|
76
|
+
view.dom.parentElement?.appendChild(this.element)
|
|
77
|
+
this.#initialized = true
|
|
78
|
+
}
|
|
72
79
|
|
|
73
80
|
if (composing || isSame)
|
|
74
81
|
return
|
|
@@ -78,9 +85,19 @@ export class SlashProvider {
|
|
|
78
85
|
return
|
|
79
86
|
}
|
|
80
87
|
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
const virtualEl: VirtualElement = {
|
|
89
|
+
getBoundingClientRect: () => posToDOMRect(view, from, to),
|
|
90
|
+
}
|
|
91
|
+
computePosition(virtualEl, this.element, {
|
|
92
|
+
placement: 'bottom-start',
|
|
93
|
+
middleware: [flip(), offset(this.#offset)],
|
|
83
94
|
})
|
|
95
|
+
.then(({ x, y }) => {
|
|
96
|
+
Object.assign(this.element.style, {
|
|
97
|
+
left: `${x}px`,
|
|
98
|
+
top: `${y}px`,
|
|
99
|
+
})
|
|
100
|
+
})
|
|
84
101
|
|
|
85
102
|
this.show()
|
|
86
103
|
}
|
|
@@ -132,19 +149,17 @@ export class SlashProvider {
|
|
|
132
149
|
|
|
133
150
|
/// Destroy the slash.
|
|
134
151
|
destroy = () => {
|
|
135
|
-
this.#tippy?.destroy()
|
|
136
152
|
}
|
|
137
153
|
|
|
138
154
|
/// Show the slash.
|
|
139
155
|
show = () => {
|
|
140
|
-
this
|
|
156
|
+
this.element.dataset.show = 'true'
|
|
157
|
+
this.onShow()
|
|
141
158
|
}
|
|
142
159
|
|
|
143
160
|
/// Hide the slash.
|
|
144
161
|
hide = () => {
|
|
145
|
-
this
|
|
162
|
+
this.element.dataset.show = 'false'
|
|
163
|
+
this.onHide()
|
|
146
164
|
}
|
|
147
|
-
|
|
148
|
-
/// Get the [tippy.js](https://atomiks.github.io/tippyjs/) instance.
|
|
149
|
-
getInstance = () => this.#tippy
|
|
150
165
|
}
|