@elytracms/studio 0.0.37 → 0.0.38
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/dist/components/cms/rich-text/editor.d.ts +3 -0
- package/dist/components/cms/rich-text/editor.js +21 -3
- package/dist/components/cms/rich-text/editor.js.map +1 -1
- package/dist/components/cms/rich-text/embed-picker.d.ts +17 -0
- package/dist/components/cms/rich-text/embed-picker.js +34 -0
- package/dist/components/cms/rich-text/embed-picker.js.map +1 -0
- package/dist/components/cms/rich-text/extensions.d.ts +7 -1
- package/dist/components/cms/rich-text/extensions.js +6 -1
- package/dist/components/cms/rich-text/extensions.js.map +1 -1
- package/dist/components/cms/rich-text/insert.d.ts +21 -0
- package/dist/components/cms/rich-text/insert.js +55 -0
- package/dist/components/cms/rich-text/insert.js.map +1 -0
- package/dist/components/cms/rich-text/slash-command.d.ts +27 -0
- package/dist/components/cms/rich-text/slash-command.js +158 -0
- package/dist/components/cms/rich-text/slash-command.js.map +1 -0
- package/dist/components/cms/rich-text/toolbar.d.ts +5 -6
- package/dist/components/cms/rich-text/toolbar.js +3 -5
- package/dist/components/cms/rich-text/toolbar.js.map +1 -1
- package/dist/lib/i18n/messages.en.js +2 -0
- package/dist/lib/i18n/messages.en.js.map +1 -1
- package/package.json +3 -2
- package/src/styles/studio.css +17 -0
|
@@ -19,6 +19,9 @@ interface RichTextEditorProps {
|
|
|
19
19
|
* on its stored meaning, the *exact stored value* is re-emitted so the field never
|
|
20
20
|
* shows a spurious dirty state (the same "commit-nothing-when-equivalent" contract
|
|
21
21
|
* the canvas inline editor uses).
|
|
22
|
+
*
|
|
23
|
+
* The image and component-embed pickers are owned here (not the toolbar) so both the
|
|
24
|
+
* toolbar buttons and the `/` slash menu open the same modal — one insertion path.
|
|
22
25
|
*/
|
|
23
26
|
export declare function RichTextEditor({ value, onChange, readOnly, id, allow }: RichTextEditorProps): import("react").JSX.Element | null;
|
|
24
27
|
export {};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
3
|
import { EditorContent, useEditor } from '@tiptap/react';
|
|
4
4
|
import { cn } from '@elytracms/studio/lib/utils';
|
|
5
5
|
import { localizedLabel } from '@elytracms/studio/lib/i18n';
|
|
6
6
|
import { useOptionalCollectionView } from '../../collection-view/collection-view-context';
|
|
7
7
|
import { useOptionalAssets } from '../../assets/assets-store';
|
|
8
|
+
import { AssetPicker } from '../asset-picker';
|
|
8
9
|
import { buildRichTextExtensions } from './extensions';
|
|
9
10
|
import { isEffectivelyEmpty, isEquivalentToStored, normalizeEditorDoc, richTextValueFromDoc, toEditorDoc, } from './value';
|
|
11
|
+
import { insertAssetImage, insertComponentEmbed } from './insert';
|
|
12
|
+
import { EmbedPicker, hasEmbeddable } from './embed-picker';
|
|
10
13
|
import { RichTextToolbar } from './toolbar';
|
|
11
14
|
/**
|
|
12
15
|
* The CMS rich-text field editor (EC-314) — a real TipTap editor over the shared
|
|
@@ -20,12 +23,19 @@ import { RichTextToolbar } from './toolbar';
|
|
|
20
23
|
* on its stored meaning, the *exact stored value* is re-emitted so the field never
|
|
21
24
|
* shows a spurious dirty state (the same "commit-nothing-when-equivalent" contract
|
|
22
25
|
* the canvas inline editor uses).
|
|
26
|
+
*
|
|
27
|
+
* The image and component-embed pickers are owned here (not the toolbar) so both the
|
|
28
|
+
* toolbar buttons and the `/` slash menu open the same modal — one insertion path.
|
|
23
29
|
*/
|
|
24
30
|
export function RichTextEditor({ value, onChange, readOnly = false, id, allow }) {
|
|
25
31
|
// Ambient context for reference-aware insertion (null outside a collection view /
|
|
26
32
|
// asset backend — the editor then degrades to external links and no image insert).
|
|
27
33
|
const source = useOptionalCollectionView();
|
|
28
34
|
const assets = useOptionalAssets();
|
|
35
|
+
const [imageOpen, setImageOpen] = useState(false);
|
|
36
|
+
const [embedOpen, setEmbedOpen] = useState(false);
|
|
37
|
+
const canImage = assets != null;
|
|
38
|
+
const canEmbed = useMemo(() => hasEmbeddable(allow), [allow]);
|
|
29
39
|
// Every linkable document across the workspace (excluding the asset rail — you link
|
|
30
40
|
// to content, not to raw files). Recomputed only when the source changes.
|
|
31
41
|
const linkTargets = useMemo(() => {
|
|
@@ -50,8 +60,12 @@ export function RichTextEditor({ value, onChange, readOnly = false, id, allow })
|
|
|
50
60
|
onChangeRef.current = onChange;
|
|
51
61
|
// What we last emitted upward — lets the sync effect ignore our own echoes.
|
|
52
62
|
const lastEmitted = useRef(value);
|
|
63
|
+
// Media openers for the toolbar + slash menu. setState fns are stable, so these
|
|
64
|
+
// stay valid for the life of the editor even though it's built once.
|
|
65
|
+
const openImage = canImage ? () => setImageOpen(true) : undefined;
|
|
66
|
+
const openEmbed = canEmbed ? () => setEmbedOpen(true) : undefined;
|
|
53
67
|
const editor = useEditor({
|
|
54
|
-
extensions: buildRichTextExtensions(),
|
|
68
|
+
extensions: buildRichTextExtensions({ slash: { onImage: openImage, onEmbed: openEmbed } }),
|
|
55
69
|
content: toEditorDoc(value),
|
|
56
70
|
editable: !readOnly,
|
|
57
71
|
editorProps: {
|
|
@@ -88,6 +102,10 @@ export function RichTextEditor({ value, onChange, readOnly = false, id, allow })
|
|
|
88
102
|
}, [editor, readOnly]);
|
|
89
103
|
if (!editor)
|
|
90
104
|
return null;
|
|
91
|
-
return (_jsxs("div", { className: cn('ec-rte overflow-hidden rounded-md border border-input bg-background shadow-sm', 'focus-within:border-ring focus-within:ring-2 focus-within:ring-ring/40', readOnly && 'opacity-70'), children: [!readOnly ? (_jsx(RichTextToolbar, { editor: editor, targets: linkTargets,
|
|
105
|
+
return (_jsxs("div", { className: cn('ec-rte overflow-hidden rounded-md border border-input bg-background shadow-sm', 'focus-within:border-ring focus-within:ring-2 focus-within:ring-ring/40', readOnly && 'opacity-70'), children: [!readOnly ? (_jsx(RichTextToolbar, { editor: editor, targets: linkTargets, onOpenImage: openImage, onOpenEmbed: openEmbed })) : null, _jsx(EditorContent, { editor: editor }), canImage ? (_jsx(AssetPicker, { open: imageOpen, onOpenChange: setImageOpen, value: null, accept: "image/*", onChange: (assetId) => {
|
|
106
|
+
setImageOpen(false);
|
|
107
|
+
if (assetId)
|
|
108
|
+
insertAssetImage(editor, assetId, assets);
|
|
109
|
+
} })) : null, canEmbed ? (_jsx(EmbedPicker, { open: embedOpen, onOpenChange: setEmbedOpen, allow: allow, onSelect: (manifest) => insertComponentEmbed(editor, manifest) })) : null] }));
|
|
92
110
|
}
|
|
93
111
|
//# sourceMappingURL=editor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"editor.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/editor.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAExD,OAAO,EAAE,EAAE,EAAE,MAAM,6BAA6B,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,yBAAyB,EAAE,MAAM,+CAA+C,CAAA;AACzF,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAC7D,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AACtD,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,WAAW,GACZ,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAa3C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,EAAE,EAAE,EAAE,KAAK,EAAuB;IAClG,kFAAkF;IAClF,mFAAmF;IACn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,MAAM,EAAE,KAAK,CAAC,CAAC,CAAA;IAEnB,kFAAkF;IAClF,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,EAAE,WAAW,CAAC,CAAC,QAAQ,CAAC,CAAA;IAChC,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAA;IAEtB,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,OAAO,CACL,eACE,SAAS,EAAE,EAAE,CACX,+EAA+E,EAC/E,wEAAwE,EACxE,QAAQ,IAAI,YAAY,CACzB,aAEA,CAAC,QAAQ,CAAC,CAAC,CAAC,CACX,KAAC,eAAe,IAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAI,CACxF,CAAC,CAAC,CAAC,IAAI,EACR,KAAC,aAAa,IAAC,MAAM,EAAE,MAAM,GAAI,IAC7B,CACP,CAAA;AACH,CAAC","sourcesContent":["import { useEffect, useMemo, useRef } from 'react'\nimport { EditorContent, useEditor } from '@tiptap/react'\nimport type { RichTextDoc } from '@elytracms/core/rich-text'\nimport { cn } from '@elytracms/studio/lib/utils'\nimport { localizedLabel } from '@elytracms/studio/lib/i18n'\nimport { useOptionalCollectionView } from '../../collection-view/collection-view-context'\nimport { useOptionalAssets } from '../../assets/assets-store'\nimport { buildRichTextExtensions } from './extensions'\nimport {\n isEffectivelyEmpty,\n isEquivalentToStored,\n normalizeEditorDoc,\n richTextValueFromDoc,\n toEditorDoc,\n} from './value'\nimport { RichTextToolbar } from './toolbar'\nimport type { LinkTarget } from './link-control'\n\ninterface RichTextEditorProps {\n /** The stored `richText` value (`{ version, doc }`, legacy string, or empty). */\n value: unknown\n onChange: (value: unknown) => void\n readOnly?: boolean\n id?: string\n /** The field's embed `allow` list (component ids editors may embed). */\n allow?: string[]\n}\n\n/**\n * The CMS rich-text field editor (EC-314) — a real TipTap editor over the shared\n * `@elytracms/core/rich-text` storage, replacing the plaintext textarea stub.\n *\n * It stays *controlled without churn*: the editor is created once from the initial\n * value; the stored value is only re-pushed into ProseMirror when it changes\n * externally (locale switch, reset, compare) and is not already equivalent to what\n * is on screen. On edit, output is pruned to the lean storage convention\n * ({@link normalizeEditorDoc}) and — crucially — when an edit lands the document back\n * on its stored meaning, the *exact stored value* is re-emitted so the field never\n * shows a spurious dirty state (the same \"commit-nothing-when-equivalent\" contract\n * the canvas inline editor uses).\n */\nexport function RichTextEditor({ value, onChange, readOnly = false, id, allow }: RichTextEditorProps) {\n // Ambient context for reference-aware insertion (null outside a collection view /\n // asset backend — the editor then degrades to external links and no image insert).\n const source = useOptionalCollectionView()\n const assets = useOptionalAssets()\n\n // Every linkable document across the workspace (excluding the asset rail — you link\n // to content, not to raw files). Recomputed only when the source changes.\n const linkTargets = useMemo<LinkTarget[]>(() => {\n if (!source) return []\n const out: LinkTarget[] = []\n for (const collection of source.collections) {\n if (collection.id === source.assetCollectionId) continue\n const collectionLabel = localizedLabel(collection.form?.labelSingular, collection.id)\n for (const doc of source.referenceCandidates(collection.id)) {\n const ref = { collection: collection.id, id: doc.id }\n out.push({ ref, label: source.titleOf(ref), collectionLabel })\n }\n }\n return out\n }, [source])\n\n // Latest value/onChange, read inside the stable onUpdate closure.\n const valueRef = useRef(value)\n valueRef.current = value\n const onChangeRef = useRef(onChange)\n onChangeRef.current = onChange\n // What we last emitted upward — lets the sync effect ignore our own echoes.\n const lastEmitted = useRef<unknown>(value)\n\n const editor = useEditor({\n extensions: buildRichTextExtensions(),\n content: toEditorDoc(value),\n editable: !readOnly,\n editorProps: {\n attributes: { class: 'ec-rte-content', ...(id ? { id } : {}) },\n },\n onUpdate: ({ editor }) => {\n const current = normalizeEditorDoc(editor.getJSON() as unknown as RichTextDoc)\n const stored = valueRef.current\n // Three outcomes: unchanged → re-emit the stored value verbatim (no dirty);\n // emptied → clear the field (undefined); otherwise → the new stamped value.\n const next = isEquivalentToStored(current, stored)\n ? stored\n : isEffectivelyEmpty(current)\n ? undefined\n : richTextValueFromDoc(current)\n lastEmitted.current = next\n onChangeRef.current(next)\n },\n })\n\n // Reflect external value changes into the editor, never echoing our own emits.\n useEffect(() => {\n if (!editor) return\n if (value === lastEmitted.current) return\n const current = normalizeEditorDoc(editor.getJSON() as unknown as RichTextDoc)\n if (isEquivalentToStored(current, value)) return\n editor.commands.setContent(toEditorDoc(value), { emitUpdate: false })\n }, [editor, value])\n\n // Toggle editability without rebuilding the editor (preserves history/selection).\n useEffect(() => {\n editor?.setEditable(!readOnly)\n }, [editor, readOnly])\n\n if (!editor) return null\n\n return (\n <div\n className={cn(\n 'ec-rte overflow-hidden rounded-md border border-input bg-background shadow-sm',\n 'focus-within:border-ring focus-within:ring-2 focus-within:ring-ring/40',\n readOnly && 'opacity-70',\n )}\n >\n {!readOnly ? (\n <RichTextToolbar editor={editor} targets={linkTargets} assets={assets} allow={allow} />\n ) : null}\n <EditorContent editor={editor} />\n </div>\n )\n}\n"]}
|
|
1
|
+
{"version":3,"file":"editor.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/editor.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAC5D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAExD,OAAO,EAAE,EAAE,EAAE,MAAM,6BAA6B,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,yBAAyB,EAAE,MAAM,+CAA+C,CAAA;AACzF,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AACtD,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,WAAW,G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QAAQ,CAAC,CAAC,CAAA;IAEtB,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,OAAO,CACL,eACE,SAAS,EAAE,EAAE,CACX,+EAA+E,EAC/E,wEAAwE,EACxE,QAAQ,IAAI,YAAY,CACzB,aAEA,CAAC,QAAQ,CAAC,CAAC,CAAC,CACX,KAAC,eAAe,IACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,WAAW,EACpB,WAAW,EAAE,SAAS,EACtB,WAAW,EAAE,SAAS,GACtB,CACH,CAAC,CAAC,CAAC,IAAI,EACR,KAAC,aAAa,IAAC,MAAM,EAAE,MAAM,GAAI,EAChC,QAAQ,CAAC,CAAC,CAAC,CACV,KAAC,WAAW,IACV,IAAI,EAAE,SAAS,EACf,YAAY,EAAE,YAAY,EAC1B,KAAK,EAAE,IAAI,EACX,MAAM,EAAC,SAAS,EAChB,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE;oBACpB,YAAY,CAAC,KAAK,CAAC,CAAA;oBACnB,IAAI,OAAO;wBAAE,gBAAgB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;gBACxD,CAAC,GACD,CACH,CAAC,CAAC,CAAC,IAAI,EACP,QAAQ,CAAC,CAAC,CAAC,CACV,KAAC,WAAW,IACV,IAAI,EAAE,SAAS,EACf,YAAY,EAAE,YAAY,EAC1B,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,oBAAoB,CAAC,MAAM,EAAE,QAAQ,CAAC,GAC9D,CACH,CAAC,CAAC,CAAC,IAAI,IACJ,CACP,CAAA;AACH,CAAC","sourcesContent":["import { useEffect, useMemo, useRef, useState } from 'react'\nimport { EditorContent, useEditor } from '@tiptap/react'\nimport type { RichTextDoc } from '@elytracms/core/rich-text'\nimport { cn } from '@elytracms/studio/lib/utils'\nimport { localizedLabel } from '@elytracms/studio/lib/i18n'\nimport { useOptionalCollectionView } from '../../collection-view/collection-view-context'\nimport { useOptionalAssets } from '../../assets/assets-store'\nimport { AssetPicker } from '../asset-picker'\nimport { buildRichTextExtensions } from './extensions'\nimport {\n isEffectivelyEmpty,\n isEquivalentToStored,\n normalizeEditorDoc,\n richTextValueFromDoc,\n toEditorDoc,\n} from './value'\nimport { insertAssetImage, insertComponentEmbed } from './insert'\nimport { EmbedPicker, hasEmbeddable } from './embed-picker'\nimport { RichTextToolbar } from './toolbar'\nimport type { LinkTarget } from './link-control'\n\ninterface RichTextEditorProps {\n /** The stored `richText` value (`{ version, doc }`, legacy string, or empty). */\n value: unknown\n onChange: (value: unknown) => void\n readOnly?: boolean\n id?: string\n /** The field's embed `allow` list (component ids editors may embed). */\n allow?: string[]\n}\n\n/**\n * The CMS rich-text field editor (EC-314) — a real TipTap editor over the shared\n * `@elytracms/core/rich-text` storage, replacing the plaintext textarea stub.\n *\n * It stays *controlled without churn*: the editor is created once from the initial\n * value; the stored value is only re-pushed into ProseMirror when it changes\n * externally (locale switch, reset, compare) and is not already equivalent to what\n * is on screen. On edit, output is pruned to the lean storage convention\n * ({@link normalizeEditorDoc}) and — crucially — when an edit lands the document back\n * on its stored meaning, the *exact stored value* is re-emitted so the field never\n * shows a spurious dirty state (the same \"commit-nothing-when-equivalent\" contract\n * the canvas inline editor uses).\n *\n * The image and component-embed pickers are owned here (not the toolbar) so both the\n * toolbar buttons and the `/` slash menu open the same modal — one insertion path.\n */\nexport function RichTextEditor({ value, onChange, readOnly = false, id, allow }: RichTextEditorProps) {\n // Ambient context for reference-aware insertion (null outside a collection view /\n // asset backend — the editor then degrades to external links and no image insert).\n const source = useOptionalCollectionView()\n const assets = useOptionalAssets()\n\n const [imageOpen, setImageOpen] = useState(false)\n const [embedOpen, setEmbedOpen] = useState(false)\n\n const canImage = assets != null\n const canEmbed = useMemo(() => hasEmbeddable(allow), [allow])\n\n // Every linkable document across the workspace (excluding the asset rail — you link\n // to content, not to raw files). Recomputed only when the source changes.\n const linkTargets = useMemo<LinkTarget[]>(() => {\n if (!source) return []\n const out: LinkTarget[] = []\n for (const collection of source.collections) {\n if (collection.id === source.assetCollectionId) continue\n const collectionLabel = localizedLabel(collection.form?.labelSingular, collection.id)\n for (const doc of source.referenceCandidates(collection.id)) {\n const ref = { collection: collection.id, id: doc.id }\n out.push({ ref, label: source.titleOf(ref), collectionLabel })\n }\n }\n return out\n }, [source])\n\n // Latest value/onChange, read inside the stable onUpdate closure.\n const valueRef = useRef(value)\n valueRef.current = value\n const onChangeRef = useRef(onChange)\n onChangeRef.current = onChange\n // What we last emitted upward — lets the sync effect ignore our own echoes.\n const lastEmitted = useRef<unknown>(value)\n\n // Media openers for the toolbar + slash menu. setState fns are stable, so these\n // stay valid for the life of the editor even though it's built once.\n const openImage = canImage ? () => setImageOpen(true) : undefined\n const openEmbed = canEmbed ? () => setEmbedOpen(true) : undefined\n\n const editor = useEditor({\n extensions: buildRichTextExtensions({ slash: { onImage: openImage, onEmbed: openEmbed } }),\n content: toEditorDoc(value),\n editable: !readOnly,\n editorProps: {\n attributes: { class: 'ec-rte-content', ...(id ? { id } : {}) },\n },\n onUpdate: ({ editor }) => {\n const current = normalizeEditorDoc(editor.getJSON() as unknown as RichTextDoc)\n const stored = valueRef.current\n // Three outcomes: unchanged → re-emit the stored value verbatim (no dirty);\n // emptied → clear the field (undefined); otherwise → the new stamped value.\n const next = isEquivalentToStored(current, stored)\n ? stored\n : isEffectivelyEmpty(current)\n ? undefined\n : richTextValueFromDoc(current)\n lastEmitted.current = next\n onChangeRef.current(next)\n },\n })\n\n // Reflect external value changes into the editor, never echoing our own emits.\n useEffect(() => {\n if (!editor) return\n if (value === lastEmitted.current) return\n const current = normalizeEditorDoc(editor.getJSON() as unknown as RichTextDoc)\n if (isEquivalentToStored(current, value)) return\n editor.commands.setContent(toEditorDoc(value), { emitUpdate: false })\n }, [editor, value])\n\n // Toggle editability without rebuilding the editor (preserves history/selection).\n useEffect(() => {\n editor?.setEditable(!readOnly)\n }, [editor, readOnly])\n\n if (!editor) return null\n\n return (\n <div\n className={cn(\n 'ec-rte overflow-hidden rounded-md border border-input bg-background shadow-sm',\n 'focus-within:border-ring focus-within:ring-2 focus-within:ring-ring/40',\n readOnly && 'opacity-70',\n )}\n >\n {!readOnly ? (\n <RichTextToolbar\n editor={editor}\n targets={linkTargets}\n onOpenImage={openImage}\n onOpenEmbed={openEmbed}\n />\n ) : null}\n <EditorContent editor={editor} />\n {canImage ? (\n <AssetPicker\n open={imageOpen}\n onOpenChange={setImageOpen}\n value={null}\n accept=\"image/*\"\n onChange={(assetId) => {\n setImageOpen(false)\n if (assetId) insertAssetImage(editor, assetId, assets)\n }}\n />\n ) : null}\n {canEmbed ? (\n <EmbedPicker\n open={embedOpen}\n onOpenChange={setEmbedOpen}\n allow={allow}\n onSelect={(manifest) => insertComponentEmbed(editor, manifest)}\n />\n ) : null}\n </div>\n )\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ComponentManifest } from '@elytracms/core/component-registry';
|
|
2
|
+
interface EmbedPickerProps {
|
|
3
|
+
open: boolean;
|
|
4
|
+
onOpenChange: (open: boolean) => void;
|
|
5
|
+
/** The field's embed `allow` list (component ids). Empty/absent ⇒ any non-primitive. */
|
|
6
|
+
allow?: string[];
|
|
7
|
+
onSelect: (manifest: ComponentManifest) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The component-embed picker dialog (EC-314/EC-189): a searchable list of the
|
|
11
|
+
* components permitted for inline embedding. Rendered once at the editor level and
|
|
12
|
+
* opened by the toolbar button or the `/embed` slash command.
|
|
13
|
+
*/
|
|
14
|
+
export declare function EmbedPicker({ open, onOpenChange, allow, onSelect }: EmbedPickerProps): import("react").JSX.Element;
|
|
15
|
+
/** Whether any component is embeddable — gates showing the embed affordances. */
|
|
16
|
+
export declare function hasEmbeddable(allow: string[] | undefined): boolean;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo, useState } from 'react';
|
|
3
|
+
import { Blocks } from 'lucide-react';
|
|
4
|
+
import { getStudioRegistry } from '@elytracms/studio/lib/registry';
|
|
5
|
+
import { Dialog, DialogContent, DialogHeader, DialogTitle, Input, Stack, Text, } from '@elytracms/studio/ui';
|
|
6
|
+
import { translate } from '@elytracms/studio/lib/i18n';
|
|
7
|
+
import { cn } from '@elytracms/studio/lib/utils';
|
|
8
|
+
import { listEmbeddable } from './insert';
|
|
9
|
+
/**
|
|
10
|
+
* The component-embed picker dialog (EC-314/EC-189): a searchable list of the
|
|
11
|
+
* components permitted for inline embedding. Rendered once at the editor level and
|
|
12
|
+
* opened by the toolbar button or the `/embed` slash command.
|
|
13
|
+
*/
|
|
14
|
+
export function EmbedPicker({ open, onOpenChange, allow, onSelect }) {
|
|
15
|
+
const [query, setQuery] = useState('');
|
|
16
|
+
const embeddable = useMemo(() => listEmbeddable(allow, getStudioRegistry().registry.list()), [allow]);
|
|
17
|
+
const results = useMemo(() => {
|
|
18
|
+
const q = query.trim().toLowerCase();
|
|
19
|
+
if (!q)
|
|
20
|
+
return embeddable;
|
|
21
|
+
return embeddable.filter((m) => (m.title ?? m.id).toLowerCase().includes(q) || m.id.toLowerCase().includes(q));
|
|
22
|
+
}, [embeddable, query]);
|
|
23
|
+
function choose(manifest) {
|
|
24
|
+
onSelect(manifest);
|
|
25
|
+
onOpenChange(false);
|
|
26
|
+
setQuery('');
|
|
27
|
+
}
|
|
28
|
+
return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "max-w-md", children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: translate('richText.embed.add') }) }), _jsxs(Stack, { gap: "2", children: [_jsx(Input, { autoFocus: true, value: query, placeholder: translate('richText.link.documentSearch'), onChange: (e) => setQuery(e.target.value) }), _jsx(Stack, { gap: "none", className: "max-h-72 overflow-y-auto", children: results.length === 0 ? (_jsx(Text, { as: "span", size: "xs", tone: "muted", className: "px-1 py-2", children: translate('richText.embed.empty') })) : (results.map((manifest) => (_jsxs("button", { type: "button", onClick: () => choose(manifest), className: cn('flex w-full items-center gap-2.5 rounded-md px-2 py-2 text-left', 'hover:bg-muted focus-visible:bg-muted outline-none'), children: [_jsx(Blocks, { className: "size-4 shrink-0 text-muted-foreground" }), _jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [_jsx("span", { className: "truncate text-sm font-medium", children: manifest.title ?? manifest.id }), _jsx("span", { className: "truncate text-xs text-muted-foreground", children: manifest.id })] })] }, manifest.id)))) })] })] }) }));
|
|
29
|
+
}
|
|
30
|
+
/** Whether any component is embeddable — gates showing the embed affordances. */
|
|
31
|
+
export function hasEmbeddable(allow) {
|
|
32
|
+
return listEmbeddable(allow, getStudioRegistry().registry.list()).length > 0;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=embed-picker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embed-picker.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/embed-picker.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAClE,OAAO,EACL,MAAM,EACN,aAAa,EACb,YAAY,EACZ,WAAW,EACX,KAAK,EACL,KAAK,EACL,IAAI,GACL,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AACtD,OAAO,EAAE,EAAE,EAAE,MAAM,6BAA6B,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAUzC;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,QAAQ,EAAoB;IACnF,MAAM,CAA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CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GACzC,EACF,KAAC,KAAK,IAAC,GAAG,EAAC,MAAM,EAAC,SAAS,EAAC,0BAA0B,YACnD,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CACtB,KAAC,IAAI,IAAC,EAAE,EAAC,MAAM,EAAC,IAAI,EAAC,IAAI,EAAC,IAAI,EAAC,OAAO,EAAC,SAAS,EAAC,WAAW,YACzD,SAAS,CAAC,sBAAsB,CAAC,GAC7B,CACR,CAAC,CAAC,CAAC,CACF,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CACxB,kBAEE,IAAI,EAAC,QAAQ,EACb,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,EAC/B,SAAS,EAAE,EAAE,CACX,iEAAiE,EACjE,oDAAoD,CACrD,aAED,KAAC,MAAM,IAAC,SAAS,EAAC,uCAAuC,GAAG,EAC5D,gBAAM,SAAS,EAAC,8BAA8B,aAC5C,eAAM,SAAS,EAAC,8BAA8B,YAAE,QAAQ,CAAC,KAAK,IAAI,QAAQ,CAAC,EAAE,GAAQ,EACrF,eAAM,SAAS,EAAC,wCAAwC,YAAE,QAAQ,CAAC,EAAE,GAAQ,IACxE,KAZF,QAAQ,CAAC,EAAE,CAaT,CACV,CAAC,CACH,GACK,IACF,IACM,GACT,CACV,CAAA;AACH,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,aAAa,CAAC,KAA2B;IACvD,OAAO,cAAc,CAAC,KAAK,EAAE,iBAAiB,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAA;AAC9E,CAAC","sourcesContent":["import { useMemo, useState } from 'react'\nimport { Blocks } from 'lucide-react'\nimport type { ComponentManifest } from '@elytracms/core/component-registry'\nimport { getStudioRegistry } from '@elytracms/studio/lib/registry'\nimport {\n Dialog,\n DialogContent,\n DialogHeader,\n DialogTitle,\n Input,\n Stack,\n Text,\n} from '@elytracms/studio/ui'\nimport { translate } from '@elytracms/studio/lib/i18n'\nimport { cn } from '@elytracms/studio/lib/utils'\nimport { listEmbeddable } from './insert'\n\ninterface EmbedPickerProps {\n open: boolean\n onOpenChange: (open: boolean) => void\n /** The field's embed `allow` list (component ids). Empty/absent ⇒ any non-primitive. */\n allow?: string[]\n onSelect: (manifest: ComponentManifest) => void\n}\n\n/**\n * The component-embed picker dialog (EC-314/EC-189): a searchable list of the\n * components permitted for inline embedding. Rendered once at the editor level and\n * opened by the toolbar button or the `/embed` slash command.\n */\nexport function EmbedPicker({ open, onOpenChange, allow, onSelect }: EmbedPickerProps) {\n const [query, setQuery] = useState('')\n\n const embeddable = useMemo(\n () => listEmbeddable(allow, getStudioRegistry().registry.list()),\n [allow],\n )\n\n const results = useMemo(() => {\n const q = query.trim().toLowerCase()\n if (!q) return embeddable\n return embeddable.filter(\n (m) => (m.title ?? m.id).toLowerCase().includes(q) || m.id.toLowerCase().includes(q),\n )\n }, [embeddable, query])\n\n function choose(manifest: ComponentManifest) {\n onSelect(manifest)\n onOpenChange(false)\n setQuery('')\n }\n\n return (\n <Dialog open={open} onOpenChange={onOpenChange}>\n <DialogContent className=\"max-w-md\">\n <DialogHeader>\n <DialogTitle>{translate('richText.embed.add')}</DialogTitle>\n </DialogHeader>\n <Stack gap=\"2\">\n <Input\n autoFocus\n value={query}\n placeholder={translate('richText.link.documentSearch')}\n onChange={(e) => setQuery(e.target.value)}\n />\n <Stack gap=\"none\" className=\"max-h-72 overflow-y-auto\">\n {results.length === 0 ? (\n <Text as=\"span\" size=\"xs\" tone=\"muted\" className=\"px-1 py-2\">\n {translate('richText.embed.empty')}\n </Text>\n ) : (\n results.map((manifest) => (\n <button\n key={manifest.id}\n type=\"button\"\n onClick={() => choose(manifest)}\n className={cn(\n 'flex w-full items-center gap-2.5 rounded-md px-2 py-2 text-left',\n 'hover:bg-muted focus-visible:bg-muted outline-none',\n )}\n >\n <Blocks className=\"size-4 shrink-0 text-muted-foreground\" />\n <span className=\"flex min-w-0 flex-1 flex-col\">\n <span className=\"truncate text-sm font-medium\">{manifest.title ?? manifest.id}</span>\n <span className=\"truncate text-xs text-muted-foreground\">{manifest.id}</span>\n </span>\n </button>\n ))\n )}\n </Stack>\n </Stack>\n </DialogContent>\n </Dialog>\n )\n}\n\n/** Whether any component is embeddable — gates showing the embed affordances. */\nexport function hasEmbeddable(allow: string[] | undefined): boolean {\n return listEmbeddable(allow, getStudioRegistry().registry.list()).length > 0\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Extensions } from '@tiptap/react';
|
|
2
|
+
import type { SlashCommandOptions } from './slash-command';
|
|
2
3
|
/**
|
|
3
4
|
* The TipTap extension set for the CMS rich-text editor (EC-314).
|
|
4
5
|
*
|
|
@@ -19,5 +20,10 @@ import type { Extensions } from '@tiptap/react';
|
|
|
19
20
|
*
|
|
20
21
|
* No `allow` gating lives here — that governs *insertion* (the embed picker) and
|
|
21
22
|
* *validation* (`validateRichTextEmbeds`), not which nodes the schema tolerates.
|
|
23
|
+
*
|
|
24
|
+
* `slash` wires the Notion-style `/` menu to the editor's shared image/embed pickers
|
|
25
|
+
* (omit an action to hide that slash item).
|
|
22
26
|
*/
|
|
23
|
-
export declare function buildRichTextExtensions(
|
|
27
|
+
export declare function buildRichTextExtensions(options?: {
|
|
28
|
+
slash?: SlashCommandOptions;
|
|
29
|
+
}): Extensions;
|
|
@@ -4,6 +4,7 @@ import { translate } from '@elytracms/studio/lib/i18n';
|
|
|
4
4
|
import { RefLink } from './ref-link';
|
|
5
5
|
import { AssetImage } from './asset-image';
|
|
6
6
|
import { ComponentEmbed } from './component-embed';
|
|
7
|
+
import { createSlashCommand } from './slash-command';
|
|
7
8
|
/**
|
|
8
9
|
* The TipTap extension set for the CMS rich-text editor (EC-314).
|
|
9
10
|
*
|
|
@@ -24,8 +25,11 @@ import { ComponentEmbed } from './component-embed';
|
|
|
24
25
|
*
|
|
25
26
|
* No `allow` gating lives here — that governs *insertion* (the embed picker) and
|
|
26
27
|
* *validation* (`validateRichTextEmbeds`), not which nodes the schema tolerates.
|
|
28
|
+
*
|
|
29
|
+
* `slash` wires the Notion-style `/` menu to the editor's shared image/embed pickers
|
|
30
|
+
* (omit an action to hide that slash item).
|
|
27
31
|
*/
|
|
28
|
-
export function buildRichTextExtensions() {
|
|
32
|
+
export function buildRichTextExtensions(options = {}) {
|
|
29
33
|
return [
|
|
30
34
|
StarterKit.configure({
|
|
31
35
|
link: false,
|
|
@@ -35,6 +39,7 @@ export function buildRichTextExtensions() {
|
|
|
35
39
|
AssetImage,
|
|
36
40
|
ComponentEmbed,
|
|
37
41
|
Placeholder.configure({ placeholder: translate('richText.placeholder') }),
|
|
42
|
+
createSlashCommand(options.slash ?? {}),
|
|
38
43
|
];
|
|
39
44
|
}
|
|
40
45
|
//# sourceMappingURL=extensions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extensions.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/extensions.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"extensions.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/extensions.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAGpD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,uBAAuB,CAAC,UAA2C,EAAE;IACnF,OAAO;QACL,UAAU,CAAC,SAAS,CAAC;YACnB,IAAI,EAAE,KAAK;YACX,OAAO,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE;SACxC,CAAC;QACF,OAAO,CAAC,SAAS,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;QAC5E,UAAU;QACV,cAAc;QACd,WAAW,CAAC,SAAS,CAAC,EAAE,WAAW,EAAE,SAAS,CAAC,sBAAsB,CAAC,EAAE,CAAC;QACzE,kBAAkB,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;KACxC,CAAA;AACH,CAAC","sourcesContent":["import StarterKit from '@tiptap/starter-kit'\nimport { Placeholder } from '@tiptap/extensions'\nimport type { Extensions } from '@tiptap/react'\nimport { translate } from '@elytracms/studio/lib/i18n'\nimport { RefLink } from './ref-link'\nimport { AssetImage } from './asset-image'\nimport { ComponentEmbed } from './component-embed'\nimport { createSlashCommand } from './slash-command'\nimport type { SlashCommandOptions } from './slash-command'\n\n/**\n * The TipTap extension set for the CMS rich-text editor (EC-314).\n *\n * Deliberately matches the storage vocabulary (`@elytracms/core/rich-text` schema)\n * one-for-one so every editor document round-trips through `parseRichTextValue` /\n * the delivery renderer with no format change:\n *\n * - **StarterKit** supplies `doc/paragraph/text/heading/bulletList/orderedList/\n * listItem/blockquote/codeBlock/hardBreak/horizontalRule` and the marks\n * `bold/italic/code/strike/underline` — all ProseMirror-standard names identical\n * to storage. Its bundled `link` is disabled and replaced by {@link RefLink} so\n * internal-document links can carry a structured `ref` (EC-290). Headings allow\n * all six levels so existing `h4`–`h6` content is preserved even though the\n * toolbar surfaces only 1–3.\n * - {@link AssetImage} / {@link ComponentEmbed} declare the two reference-carrying\n * atoms (asset image, embedded registry component) so existing content of those\n * kinds survives a round-trip verbatim rather than being dropped by the schema.\n *\n * No `allow` gating lives here — that governs *insertion* (the embed picker) and\n * *validation* (`validateRichTextEmbeds`), not which nodes the schema tolerates.\n *\n * `slash` wires the Notion-style `/` menu to the editor's shared image/embed pickers\n * (omit an action to hide that slash item).\n */\nexport function buildRichTextExtensions(options: { slash?: SlashCommandOptions } = {}): Extensions {\n return [\n StarterKit.configure({\n link: false,\n heading: { levels: [1, 2, 3, 4, 5, 6] },\n }),\n RefLink.configure({ openOnClick: false, autolink: true, linkOnPaste: true }),\n AssetImage,\n ComponentEmbed,\n Placeholder.configure({ placeholder: translate('richText.placeholder') }),\n createSlashCommand(options.slash ?? {}),\n ]\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/react';
|
|
2
|
+
import type { ComponentNode } from '@elytracms/core/project-graph';
|
|
3
|
+
import type { ComponentManifest } from '@elytracms/core/component-registry';
|
|
4
|
+
import type { AssetsStore } from '../../assets/assets-store';
|
|
5
|
+
/**
|
|
6
|
+
* Reference-carrying node insertions shared by the toolbar controls and the slash
|
|
7
|
+
* menu (EC-314). Centralised so both entry points produce byte-identical envelopes
|
|
8
|
+
* (the shapes `reference-extraction` indexes).
|
|
9
|
+
*/
|
|
10
|
+
/** Insert an `image` node for a project asset (canonical `attrs.ref`, snapshot `src`). */
|
|
11
|
+
export declare function insertAssetImage(editor: Editor, assetId: string, assets: AssetsStore | null): void;
|
|
12
|
+
/** Insert a `componentEmbed` wrapping a fresh ComponentNode built from manifest defaults. */
|
|
13
|
+
export declare function insertComponentEmbed(editor: Editor, manifest: ComponentManifest): void;
|
|
14
|
+
/** A fresh ComponentNode from a manifest's prop defaults (mirrors builder node-factory). */
|
|
15
|
+
export declare function embedNodeFromManifest(manifest: ComponentManifest): ComponentNode;
|
|
16
|
+
/**
|
|
17
|
+
* The components embeddable in a rich-text field: the field's `allow` list, or every
|
|
18
|
+
* non-primitive component when open — matching the `blocks` field and the
|
|
19
|
+
* `validateRichTextEmbeds` leash.
|
|
20
|
+
*/
|
|
21
|
+
export declare function listEmbeddable(allow: string[] | undefined, registered: ComponentManifest[]): ComponentManifest[];
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { assetNode } from '@elytracms/core/cms-core';
|
|
2
|
+
import { COMPONENT_EMBED_NODE_TYPE } from '@elytracms/core/rich-text';
|
|
3
|
+
import { staticProp } from '@elytracms/core/project-graph';
|
|
4
|
+
import { isPrimitive } from '@elytracms/core/component-registry';
|
|
5
|
+
/**
|
|
6
|
+
* Reference-carrying node insertions shared by the toolbar controls and the slash
|
|
7
|
+
* menu (EC-314). Centralised so both entry points produce byte-identical envelopes
|
|
8
|
+
* (the shapes `reference-extraction` indexes).
|
|
9
|
+
*/
|
|
10
|
+
/** Insert an `image` node for a project asset (canonical `attrs.ref`, snapshot `src`). */
|
|
11
|
+
export function insertAssetImage(editor, assetId, assets) {
|
|
12
|
+
const record = assets?.assets.find((a) => a.id === assetId);
|
|
13
|
+
editor
|
|
14
|
+
.chain()
|
|
15
|
+
.focus()
|
|
16
|
+
.insertContent({
|
|
17
|
+
type: 'image',
|
|
18
|
+
attrs: { src: record?.url ?? '', alt: record?.alt ?? '', ref: assetNode(assetId) },
|
|
19
|
+
})
|
|
20
|
+
.run();
|
|
21
|
+
}
|
|
22
|
+
/** Insert a `componentEmbed` wrapping a fresh ComponentNode built from manifest defaults. */
|
|
23
|
+
export function insertComponentEmbed(editor, manifest) {
|
|
24
|
+
editor
|
|
25
|
+
.chain()
|
|
26
|
+
.focus()
|
|
27
|
+
.insertContent({ type: COMPONENT_EMBED_NODE_TYPE, attrs: { node: embedNodeFromManifest(manifest) } })
|
|
28
|
+
.run();
|
|
29
|
+
}
|
|
30
|
+
/** A fresh ComponentNode from a manifest's prop defaults (mirrors builder node-factory). */
|
|
31
|
+
export function embedNodeFromManifest(manifest) {
|
|
32
|
+
const props = {};
|
|
33
|
+
for (const [name, spec] of Object.entries(manifest.props)) {
|
|
34
|
+
if (spec.default !== undefined)
|
|
35
|
+
props[name] = staticProp(spec.default);
|
|
36
|
+
}
|
|
37
|
+
return { id: newEmbedId(), componentId: manifest.id, props };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The components embeddable in a rich-text field: the field's `allow` list, or every
|
|
41
|
+
* non-primitive component when open — matching the `blocks` field and the
|
|
42
|
+
* `validateRichTextEmbeds` leash.
|
|
43
|
+
*/
|
|
44
|
+
export function listEmbeddable(allow, registered) {
|
|
45
|
+
const permitted = allow && allow.length > 0
|
|
46
|
+
? registered.filter((m) => allow.includes(m.id))
|
|
47
|
+
: registered.filter((m) => !isPrimitive(m.id));
|
|
48
|
+
return permitted.filter((m) => m.clientRenderable !== false);
|
|
49
|
+
}
|
|
50
|
+
/** A unique id for an embedded node (studio runs in the browser; crypto is present). */
|
|
51
|
+
function newEmbedId() {
|
|
52
|
+
const uuid = globalThis.crypto?.randomUUID?.();
|
|
53
|
+
return uuid ? `embed-${uuid}` : `embed-${Math.random().toString(36).slice(2)}`;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=insert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"insert.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/insert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAA;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAE1D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WAAW,EAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,EAAE,CAAA;AAC9D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC5B,KAA2B,EAC3B,UAA+B;IAE/B,MAAM,SAAS,GACb,KAAK,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QACvB,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAChD,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAClD,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,gBAAgB,KAAK,KAAK,CAAC,CAAA;AAC9D,CAAC;AAED,wFAAwF;AACxF,SAAS,UAAU;IACjB,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,CAAA;IAC9C,OAAO,IAAI,CAAC,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,CAAC,SAAS,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAA;AAChF,CAAC","sourcesContent":["import type { Editor } from '@tiptap/react'\nimport { assetNode } from '@elytracms/core/cms-core'\nimport { COMPONENT_EMBED_NODE_TYPE } from '@elytracms/core/rich-text'\nimport { staticProp } from '@elytracms/core/project-graph'\nimport type { ComponentNode, JsonValue } from '@elytracms/core/project-graph'\nimport { isPrimitive } from '@elytracms/core/component-registry'\nimport type { ComponentManifest } from '@elytracms/core/component-registry'\nimport type { AssetsStore } from '../../assets/assets-store'\n\n/**\n * Reference-carrying node insertions shared by the toolbar controls and the slash\n * menu (EC-314). Centralised so both entry points produce byte-identical envelopes\n * (the shapes `reference-extraction` indexes).\n */\n\n/** Insert an `image` node for a project asset (canonical `attrs.ref`, snapshot `src`). */\nexport function insertAssetImage(editor: Editor, assetId: string, assets: AssetsStore | null): void {\n const record = assets?.assets.find((a) => a.id === assetId)\n editor\n .chain()\n .focus()\n .insertContent({\n type: 'image',\n attrs: { src: record?.url ?? '', alt: record?.alt ?? '', ref: assetNode(assetId) },\n })\n .run()\n}\n\n/** Insert a `componentEmbed` wrapping a fresh ComponentNode built from manifest defaults. */\nexport function insertComponentEmbed(editor: Editor, manifest: ComponentManifest): void {\n editor\n .chain()\n .focus()\n .insertContent({ type: COMPONENT_EMBED_NODE_TYPE, attrs: { node: embedNodeFromManifest(manifest) } })\n .run()\n}\n\n/** A fresh ComponentNode from a manifest's prop defaults (mirrors builder node-factory). */\nexport function embedNodeFromManifest(manifest: ComponentManifest): ComponentNode {\n const props: ComponentNode['props'] = {}\n for (const [name, spec] of Object.entries(manifest.props)) {\n if (spec.default !== undefined) props[name] = staticProp(spec.default as JsonValue)\n }\n return { id: newEmbedId(), componentId: manifest.id, props }\n}\n\n/**\n * The components embeddable in a rich-text field: the field's `allow` list, or every\n * non-primitive component when open — matching the `blocks` field and the\n * `validateRichTextEmbeds` leash.\n */\nexport function listEmbeddable(\n allow: string[] | undefined,\n registered: ComponentManifest[],\n): ComponentManifest[] {\n const permitted =\n allow && allow.length > 0\n ? registered.filter((m) => allow.includes(m.id))\n : registered.filter((m) => !isPrimitive(m.id))\n return permitted.filter((m) => m.clientRenderable !== false)\n}\n\n/** A unique id for an embedded node (studio runs in the browser; crypto is present). */\nfunction newEmbedId(): string {\n const uuid = globalThis.crypto?.randomUUID?.()\n return uuid ? `embed-${uuid}` : `embed-${Math.random().toString(36).slice(2)}`\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/react';
|
|
2
|
+
import type { Editor, Range } from '@tiptap/react';
|
|
3
|
+
import type { LucideIcon } from 'lucide-react';
|
|
4
|
+
/**
|
|
5
|
+
* Notion-style slash menu (EC-314): typing `/` at the caret opens a filterable
|
|
6
|
+
* command palette that inserts block-level content — headings, lists, quote, code,
|
|
7
|
+
* divider — and, when available, an image or a component embed (delegated to the
|
|
8
|
+
* editor's shared pickers via {@link SlashCommandOptions}). Built on TipTap's
|
|
9
|
+
* `Suggestion` utility; the menu is a React overlay positioned at the caret.
|
|
10
|
+
*/
|
|
11
|
+
export interface SlashCommandOptions {
|
|
12
|
+
/** Open the asset picker (omitted ⇒ no `/image` item). */
|
|
13
|
+
onImage?: () => void;
|
|
14
|
+
/** Open the component-embed picker (omitted ⇒ no `/embed` item). */
|
|
15
|
+
onEmbed?: () => void;
|
|
16
|
+
}
|
|
17
|
+
export interface SlashItem {
|
|
18
|
+
key: string;
|
|
19
|
+
title: string;
|
|
20
|
+
keywords: string;
|
|
21
|
+
icon: LucideIcon;
|
|
22
|
+
run: (editor: Editor, range: Range) => void;
|
|
23
|
+
}
|
|
24
|
+
/** The command list, gated by which media actions the editor provided. */
|
|
25
|
+
export declare function buildSlashItems(options: SlashCommandOptions): SlashItem[];
|
|
26
|
+
/** Build the slash-command extension bound to the editor's shared media pickers. */
|
|
27
|
+
export declare function createSlashCommand(options: SlashCommandOptions): Extension<SlashCommandOptions, any>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
|
|
3
|
+
import { Extension, ReactRenderer } from '@tiptap/react';
|
|
4
|
+
import Suggestion from '@tiptap/suggestion';
|
|
5
|
+
import { Blocks, Heading1, Heading2, Heading3, ImagePlus, List, ListOrdered, Minus, Pilcrow, Quote, SquareCode, } from 'lucide-react';
|
|
6
|
+
import { translate } from '@elytracms/studio/lib/i18n';
|
|
7
|
+
import { cn } from '@elytracms/studio/lib/utils';
|
|
8
|
+
/** The command list, gated by which media actions the editor provided. */
|
|
9
|
+
export function buildSlashItems(options) {
|
|
10
|
+
const block = (run) => (editor, range) => run(editor.chain().focus().deleteRange(range)).run();
|
|
11
|
+
const items = [
|
|
12
|
+
{ key: 'text', title: translate('richText.slash.text'), keywords: 'paragraph text plain', icon: Pilcrow, run: block((c) => c.setParagraph()) },
|
|
13
|
+
{ key: 'h1', title: translate('richText.heading', { level: 1 }), keywords: 'heading title h1', icon: Heading1, run: block((c) => c.toggleHeading({ level: 1 })) },
|
|
14
|
+
{ key: 'h2', title: translate('richText.heading', { level: 2 }), keywords: 'heading subtitle h2', icon: Heading2, run: block((c) => c.toggleHeading({ level: 2 })) },
|
|
15
|
+
{ key: 'h3', title: translate('richText.heading', { level: 3 }), keywords: 'heading h3', icon: Heading3, run: block((c) => c.toggleHeading({ level: 3 })) },
|
|
16
|
+
{ key: 'bulletList', title: translate('richText.bulletList'), keywords: 'bullet unordered list ul', icon: List, run: block((c) => c.toggleBulletList()) },
|
|
17
|
+
{ key: 'orderedList', title: translate('richText.orderedList'), keywords: 'ordered numbered list ol', icon: ListOrdered, run: block((c) => c.toggleOrderedList()) },
|
|
18
|
+
{ key: 'blockquote', title: translate('richText.blockquote'), keywords: 'quote blockquote', icon: Quote, run: block((c) => c.toggleBlockquote()) },
|
|
19
|
+
{ key: 'codeBlock', title: translate('richText.codeBlock'), keywords: 'code block pre', icon: SquareCode, run: block((c) => c.toggleCodeBlock()) },
|
|
20
|
+
{ key: 'divider', title: translate('richText.horizontalRule'), keywords: 'divider rule hr separator', icon: Minus, run: block((c) => c.setHorizontalRule()) },
|
|
21
|
+
];
|
|
22
|
+
if (options.onImage) {
|
|
23
|
+
items.push({
|
|
24
|
+
key: 'image',
|
|
25
|
+
title: translate('richText.image.add'),
|
|
26
|
+
keywords: 'image picture photo media',
|
|
27
|
+
icon: ImagePlus,
|
|
28
|
+
run: (editor, range) => {
|
|
29
|
+
editor.chain().focus().deleteRange(range).run();
|
|
30
|
+
options.onImage?.();
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
if (options.onEmbed) {
|
|
35
|
+
items.push({
|
|
36
|
+
key: 'embed',
|
|
37
|
+
title: translate('richText.embed.add'),
|
|
38
|
+
keywords: 'embed component block',
|
|
39
|
+
icon: Blocks,
|
|
40
|
+
run: (editor, range) => {
|
|
41
|
+
editor.chain().focus().deleteRange(range).run();
|
|
42
|
+
options.onEmbed?.();
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return items;
|
|
47
|
+
}
|
|
48
|
+
const SlashMenu = forwardRef((props, ref) => {
|
|
49
|
+
const [selected, setSelected] = useState(0);
|
|
50
|
+
useEffect(() => setSelected(0), [props.items]);
|
|
51
|
+
useImperativeHandle(ref, () => ({
|
|
52
|
+
onKeyDown: ({ event }) => {
|
|
53
|
+
const count = props.items.length;
|
|
54
|
+
if (count === 0)
|
|
55
|
+
return false;
|
|
56
|
+
if (event.key === 'ArrowUp') {
|
|
57
|
+
setSelected((s) => (s + count - 1) % count);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (event.key === 'ArrowDown') {
|
|
61
|
+
setSelected((s) => (s + 1) % count);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
if (event.key === 'Enter') {
|
|
65
|
+
const item = props.items[selected];
|
|
66
|
+
if (item)
|
|
67
|
+
props.command(item);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
73
|
+
if (props.items.length === 0) {
|
|
74
|
+
return (_jsx("div", { className: "ec-rte-slash", children: _jsx("span", { className: "px-2 py-1.5 text-xs text-muted-foreground", children: translate('richText.slash.empty') }) }));
|
|
75
|
+
}
|
|
76
|
+
return (_jsx("div", { className: "ec-rte-slash", role: "menu", children: props.items.map((item, i) => {
|
|
77
|
+
const Icon = item.icon;
|
|
78
|
+
return (_jsxs("button", { type: "button", role: "menuitem", onMouseEnter: () => setSelected(i), onMouseDown: (e) => {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
props.command(item);
|
|
81
|
+
}, className: cn('flex w-full items-center gap-2.5 rounded-md px-2 py-1.5 text-left text-sm outline-none', i === selected ? 'bg-muted' : 'hover:bg-muted/60'), children: [_jsx(Icon, { className: "size-4 shrink-0 text-muted-foreground" }), _jsx("span", { className: "truncate", children: item.title })] }, item.key));
|
|
82
|
+
}) }));
|
|
83
|
+
});
|
|
84
|
+
SlashMenu.displayName = 'SlashMenu';
|
|
85
|
+
/** Position a floating element under the caret, flipping above near the viewport edge. */
|
|
86
|
+
function place(el, rect) {
|
|
87
|
+
if (!rect)
|
|
88
|
+
return;
|
|
89
|
+
const margin = 8;
|
|
90
|
+
const height = el.offsetHeight || 280;
|
|
91
|
+
const below = rect.bottom + 4;
|
|
92
|
+
const flip = below + height > window.innerHeight - margin && rect.top - height > margin;
|
|
93
|
+
el.style.position = 'fixed';
|
|
94
|
+
el.style.left = `${Math.min(rect.left, window.innerWidth - 260)}px`;
|
|
95
|
+
el.style.top = flip ? `${rect.top - height - 4}px` : `${below}px`;
|
|
96
|
+
el.style.zIndex = '60';
|
|
97
|
+
}
|
|
98
|
+
/** The suggestion config (everything but the `editor`, which the extension injects). */
|
|
99
|
+
function slashSuggestion(options) {
|
|
100
|
+
return {
|
|
101
|
+
char: '/',
|
|
102
|
+
startOfLine: false,
|
|
103
|
+
items: ({ query }) => {
|
|
104
|
+
const q = query.trim().toLowerCase();
|
|
105
|
+
const all = buildSlashItems(options);
|
|
106
|
+
if (!q)
|
|
107
|
+
return all;
|
|
108
|
+
return all.filter((item) => item.title.toLowerCase().includes(q) || item.keywords.includes(q));
|
|
109
|
+
},
|
|
110
|
+
command: ({ editor, range, props }) => props.run(editor, range),
|
|
111
|
+
render: () => {
|
|
112
|
+
let renderer = null;
|
|
113
|
+
let wrapper = null;
|
|
114
|
+
return {
|
|
115
|
+
onStart: (props) => {
|
|
116
|
+
renderer = new ReactRenderer(SlashMenu, {
|
|
117
|
+
props,
|
|
118
|
+
editor: props.editor,
|
|
119
|
+
});
|
|
120
|
+
wrapper = document.createElement('div');
|
|
121
|
+
wrapper.appendChild(renderer.element);
|
|
122
|
+
document.body.appendChild(wrapper);
|
|
123
|
+
place(wrapper, props.clientRect?.());
|
|
124
|
+
},
|
|
125
|
+
onUpdate: (props) => {
|
|
126
|
+
renderer?.updateProps(props);
|
|
127
|
+
if (wrapper)
|
|
128
|
+
place(wrapper, props.clientRect?.());
|
|
129
|
+
},
|
|
130
|
+
onKeyDown: (props) => {
|
|
131
|
+
if (props.event.key === 'Escape') {
|
|
132
|
+
wrapper?.remove();
|
|
133
|
+
wrapper = null;
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return renderer?.ref?.onKeyDown(props) ?? false;
|
|
137
|
+
},
|
|
138
|
+
onExit: () => {
|
|
139
|
+
wrapper?.remove();
|
|
140
|
+
wrapper = null;
|
|
141
|
+
renderer?.destroy();
|
|
142
|
+
renderer = null;
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/** Build the slash-command extension bound to the editor's shared media pickers. */
|
|
149
|
+
export function createSlashCommand(options) {
|
|
150
|
+
return Extension.create({
|
|
151
|
+
name: 'slashCommand',
|
|
152
|
+
addOptions: () => options,
|
|
153
|
+
addProseMirrorPlugins() {
|
|
154
|
+
return [Suggestion({ editor: this.editor, ...slashSuggestion(this.options) })];
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=slash-command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slash-command.js","sourceRoot":"","sources":["../../../../src/components/cms/rich-text/slash-command.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAE5E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,eAAe,C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QAAQ,EAAE,OAAO,EAAE,CAAA;oBACnB,QAAQ,GAAG,IAAI,CAAA;gBACjB,CAAC;aACF,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,kBAAkB,CAAC,OAA4B;IAC7D,OAAO,SAAS,CAAC,MAAM,CAAsB;QAC3C,IAAI,EAAE,cAAc;QACpB,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO;QACzB,qBAAqB;YACnB,OAAO,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAA;QAChF,CAAC;KACF,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import { forwardRef, useEffect, useImperativeHandle, useState } from 'react'\nimport type { ComponentType } from 'react'\nimport { Extension, ReactRenderer } from '@tiptap/react'\nimport type { Editor, Range } from '@tiptap/react'\nimport Suggestion from '@tiptap/suggestion'\nimport type { SuggestionKeyDownProps, SuggestionOptions, SuggestionProps } from '@tiptap/suggestion'\nimport {\n Blocks,\n Heading1,\n Heading2,\n Heading3,\n ImagePlus,\n List,\n ListOrdered,\n Minus,\n Pilcrow,\n Quote,\n SquareCode,\n} from 'lucide-react'\nimport type { LucideIcon } from 'lucide-react'\nimport { translate } from '@elytracms/studio/lib/i18n'\nimport { cn } from '@elytracms/studio/lib/utils'\n\n/**\n * Notion-style slash menu (EC-314): typing `/` at the caret opens a filterable\n * command palette that inserts block-level content — headings, lists, quote, code,\n * divider — and, when available, an image or a component embed (delegated to the\n * editor's shared pickers via {@link SlashCommandOptions}). Built on TipTap's\n * `Suggestion` utility; the menu is a React overlay positioned at the caret.\n */\n\nexport interface SlashCommandOptions {\n /** Open the asset picker (omitted ⇒ no `/image` item). */\n onImage?: () => void\n /** Open the component-embed picker (omitted ⇒ no `/embed` item). */\n onEmbed?: () => void\n}\n\nexport interface SlashItem {\n key: string\n title: string\n keywords: string\n icon: LucideIcon\n run: (editor: Editor, range: Range) => void\n}\n\n/** The command list, gated by which media actions the editor provided. */\nexport function buildSlashItems(options: SlashCommandOptions): SlashItem[] {\n const block = (run: (chain: ReturnType<Editor['chain']>) => ReturnType<Editor['chain']>) =>\n (editor: Editor, range: Range) => run(editor.chain().focus().deleteRange(range)).run()\n\n const items: SlashItem[] = [\n { key: 'text', title: translate('richText.slash.text'), keywords: 'paragraph text plain', icon: Pilcrow, run: block((c) => c.setParagraph()) },\n { key: 'h1', title: translate('richText.heading', { level: 1 }), keywords: 'heading title h1', icon: Heading1, run: block((c) => c.toggleHeading({ level: 1 })) },\n { key: 'h2', title: translate('richText.heading', { level: 2 }), keywords: 'heading subtitle h2', icon: Heading2, run: block((c) => c.toggleHeading({ level: 2 })) },\n { key: 'h3', title: translate('richText.heading', { level: 3 }), keywords: 'heading h3', icon: Heading3, run: block((c) => c.toggleHeading({ level: 3 })) },\n { key: 'bulletList', title: translate('richText.bulletList'), keywords: 'bullet unordered list ul', icon: List, run: block((c) => c.toggleBulletList()) },\n { key: 'orderedList', title: translate('richText.orderedList'), keywords: 'ordered numbered list ol', icon: ListOrdered, run: block((c) => c.toggleOrderedList()) },\n { key: 'blockquote', title: translate('richText.blockquote'), keywords: 'quote blockquote', icon: Quote, run: block((c) => c.toggleBlockquote()) },\n { key: 'codeBlock', title: translate('richText.codeBlock'), keywords: 'code block pre', icon: SquareCode, run: block((c) => c.toggleCodeBlock()) },\n { key: 'divider', title: translate('richText.horizontalRule'), keywords: 'divider rule hr separator', icon: Minus, run: block((c) => c.setHorizontalRule()) },\n ]\n if (options.onImage) {\n items.push({\n key: 'image',\n title: translate('richText.image.add'),\n keywords: 'image picture photo media',\n icon: ImagePlus,\n run: (editor, range) => {\n editor.chain().focus().deleteRange(range).run()\n options.onImage?.()\n },\n })\n }\n if (options.onEmbed) {\n items.push({\n key: 'embed',\n title: translate('richText.embed.add'),\n keywords: 'embed component block',\n icon: Blocks,\n run: (editor, range) => {\n editor.chain().focus().deleteRange(range).run()\n options.onEmbed?.()\n },\n })\n }\n return items\n}\n\n/** Imperative handle the suggestion render loop calls for keyboard navigation. */\ninterface SlashMenuHandle {\n onKeyDown: (props: SuggestionKeyDownProps) => boolean\n}\n\nconst SlashMenu = forwardRef<SlashMenuHandle, SuggestionProps<SlashItem, SlashItem>>((props, ref) => {\n const [selected, setSelected] = useState(0)\n useEffect(() => setSelected(0), [props.items])\n\n useImperativeHandle(ref, () => ({\n onKeyDown: ({ event }) => {\n const count = props.items.length\n if (count === 0) return false\n if (event.key === 'ArrowUp') {\n setSelected((s) => (s + count - 1) % count)\n return true\n }\n if (event.key === 'ArrowDown') {\n setSelected((s) => (s + 1) % count)\n return true\n }\n if (event.key === 'Enter') {\n const item = props.items[selected]\n if (item) props.command(item)\n return true\n }\n return false\n },\n }))\n\n if (props.items.length === 0) {\n return (\n <div className=\"ec-rte-slash\">\n <span className=\"px-2 py-1.5 text-xs text-muted-foreground\">\n {translate('richText.slash.empty')}\n </span>\n </div>\n )\n }\n\n return (\n <div className=\"ec-rte-slash\" role=\"menu\">\n {props.items.map((item, i) => {\n const Icon = item.icon\n return (\n <button\n key={item.key}\n type=\"button\"\n role=\"menuitem\"\n onMouseEnter={() => setSelected(i)}\n onMouseDown={(e) => {\n e.preventDefault()\n props.command(item)\n }}\n className={cn(\n 'flex w-full items-center gap-2.5 rounded-md px-2 py-1.5 text-left text-sm outline-none',\n i === selected ? 'bg-muted' : 'hover:bg-muted/60',\n )}\n >\n <Icon className=\"size-4 shrink-0 text-muted-foreground\" />\n <span className=\"truncate\">{item.title}</span>\n </button>\n )\n })}\n </div>\n )\n})\nSlashMenu.displayName = 'SlashMenu'\n\n/** Position a floating element under the caret, flipping above near the viewport edge. */\nfunction place(el: HTMLElement, rect: DOMRect | null | undefined): void {\n if (!rect) return\n const margin = 8\n const height = el.offsetHeight || 280\n const below = rect.bottom + 4\n const flip = below + height > window.innerHeight - margin && rect.top - height > margin\n el.style.position = 'fixed'\n el.style.left = `${Math.min(rect.left, window.innerWidth - 260)}px`\n el.style.top = flip ? `${rect.top - height - 4}px` : `${below}px`\n el.style.zIndex = '60'\n}\n\n/** The suggestion config (everything but the `editor`, which the extension injects). */\nfunction slashSuggestion(options: SlashCommandOptions): Omit<SuggestionOptions<SlashItem, SlashItem>, 'editor'> {\n return {\n char: '/',\n startOfLine: false,\n items: ({ query }) => {\n const q = query.trim().toLowerCase()\n const all = buildSlashItems(options)\n if (!q) return all\n return all.filter((item) => item.title.toLowerCase().includes(q) || item.keywords.includes(q))\n },\n command: ({ editor, range, props }) => props.run(editor, range),\n render: () => {\n let renderer: ReactRenderer<SlashMenuHandle, SuggestionProps<SlashItem, SlashItem>> | null = null\n let wrapper: HTMLDivElement | null = null\n\n return {\n onStart: (props) => {\n renderer = new ReactRenderer(SlashMenu as ComponentType<SuggestionProps<SlashItem, SlashItem>>, {\n props,\n editor: props.editor,\n })\n wrapper = document.createElement('div')\n wrapper.appendChild(renderer.element)\n document.body.appendChild(wrapper)\n place(wrapper, props.clientRect?.())\n },\n onUpdate: (props) => {\n renderer?.updateProps(props)\n if (wrapper) place(wrapper, props.clientRect?.())\n },\n onKeyDown: (props) => {\n if (props.event.key === 'Escape') {\n wrapper?.remove()\n wrapper = null\n return true\n }\n return renderer?.ref?.onKeyDown(props) ?? false\n },\n onExit: () => {\n wrapper?.remove()\n wrapper = null\n renderer?.destroy()\n renderer = null\n },\n }\n },\n }\n}\n\n/** Build the slash-command extension bound to the editor's shared media pickers. */\nexport function createSlashCommand(options: SlashCommandOptions) {\n return Extension.create<SlashCommandOptions>({\n name: 'slashCommand',\n addOptions: () => options,\n addProseMirrorPlugins() {\n return [Suggestion({ editor: this.editor, ...slashSuggestion(this.options) })]\n },\n })\n}\n"]}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type { Editor } from '@tiptap/react';
|
|
2
|
-
import type { AssetsStore } from '../../assets/assets-store';
|
|
3
2
|
import type { LinkTarget } from './link-control';
|
|
4
3
|
interface RichTextToolbarProps {
|
|
5
4
|
editor: Editor;
|
|
6
5
|
/** Internal-link destinations across the workspace (empty ⇒ external-only). */
|
|
7
6
|
targets: LinkTarget[];
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
|
|
7
|
+
/** Open the shared image picker (omitted ⇒ no image button). */
|
|
8
|
+
onOpenImage?: () => void;
|
|
9
|
+
/** Open the shared component-embed picker (omitted ⇒ no embed button). */
|
|
10
|
+
onOpenEmbed?: () => void;
|
|
12
11
|
}
|
|
13
12
|
/**
|
|
14
13
|
* The CMS rich-text toolbar (EC-314) — a compact, Notion-grade control strip that
|
|
@@ -16,5 +15,5 @@ interface RichTextToolbarProps {
|
|
|
16
15
|
* pressed control always mirrors the caret. Every button prevents the default
|
|
17
16
|
* mousedown so clicking it never steals the editor selection.
|
|
18
17
|
*/
|
|
19
|
-
export declare function RichTextToolbar({ editor, targets,
|
|
18
|
+
export declare function RichTextToolbar({ editor, targets, onOpenImage, onOpenEmbed }: RichTextToolbarProps): import("react").JSX.Element;
|
|
20
19
|
export {};
|