payload-richtext-tiptap 0.0.95 → 0.0.97
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/src/fields/TiptapEditor/features/BlockEditor/OneLineEditor.d.ts +9 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/OneLineEditor.d.ts.map +1 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/OneLineEditor.js +110 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/OneLineEditor.js.map +1 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/components/OneLiner.d.ts +4 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/components/OneLiner.d.ts.map +1 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/components/OneLiner.js +9 -0
- package/dist/src/fields/TiptapEditor/features/BlockEditor/components/OneLiner.js.map +1 -0
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/AITextMenu.d.ts +7 -0
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/AITextMenu.d.ts.map +1 -0
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/AITextMenu.js +54 -0
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/AITextMenu.js.map +1 -0
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-completion-command.js +1 -1
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-completion-command.js.map +1 -1
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.d.ts.map +1 -1
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.js +4 -3
- package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.js.map +1 -1
- package/dist/src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.d.ts.map +1 -1
- package/dist/src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.js +15 -17
- package/dist/src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.js.map +1 -1
- package/dist/src/fields/TiptapEditor/features/panels/AIEditorPanel/AIEditorPanel.js +6 -5
- package/dist/src/fields/TiptapEditor/features/panels/AIEditorPanel/AIEditorPanel.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AdditionalContextType, TiptapProps } from "./types.js";
|
|
2
|
+
export declare const OneLineEditor: ({ handleChange, content, additionalContext, isEditable, autoFocus, dir, }: TiptapProps & {
|
|
3
|
+
additionalContext?: AdditionalContextType;
|
|
4
|
+
autoFocus?: boolean;
|
|
5
|
+
isEditable?: boolean;
|
|
6
|
+
dir?: "ltr" | "rtl" | "auto";
|
|
7
|
+
}) => import("react").JSX.Element;
|
|
8
|
+
export default OneLineEditor;
|
|
9
|
+
//# sourceMappingURL=OneLineEditor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OneLineEditor.d.ts","sourceRoot":"","sources":["../../../../../../src/fields/TiptapEditor/features/BlockEditor/OneLineEditor.tsx"],"names":[],"mappings":"AAYA,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAKhE,eAAO,MAAM,aAAa,8EAOvB,WAAW,GAAG;IACf,iBAAiB,CAAC,EAAE,qBAAqB,CAAC;IAC1C,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;CAC9B,gCA8GA,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { EditorContent, useEditor } from "@tiptap/react";
|
|
4
|
+
import i18n from "i18next";
|
|
5
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
6
|
+
import { EditorContext } from "../../context/EditorContext.js";
|
|
7
|
+
import i18next from "i18next";
|
|
8
|
+
import Text from '@tiptap/extension-text';
|
|
9
|
+
import Highlight from '@tiptap/extension-highlight';
|
|
10
|
+
import { AICommand, Paragraph } from "../../extensions/index.js";
|
|
11
|
+
import OneLiner from "./components/OneLiner.js";
|
|
12
|
+
import { AITextMenu } from "../menus/TextMenu/AITextMenu.js";
|
|
13
|
+
const DEFAULT_LANG = "en";
|
|
14
|
+
export const OneLineEditor = ({ handleChange, content, additionalContext, isEditable = true, autoFocus = true, dir })=>{
|
|
15
|
+
const menuContainerRef = useRef(null);
|
|
16
|
+
const editorRef = useRef(null);
|
|
17
|
+
const editor = useEditor({
|
|
18
|
+
content: content?.json ?? content ?? {},
|
|
19
|
+
immediatelyRender: false,
|
|
20
|
+
autofocus: autoFocus ?? true,
|
|
21
|
+
editable: isEditable ?? true,
|
|
22
|
+
onCreate: ({ editor })=>({
|
|
23
|
+
}),
|
|
24
|
+
extensions: [
|
|
25
|
+
OneLiner,
|
|
26
|
+
Text,
|
|
27
|
+
Paragraph,
|
|
28
|
+
AICommand,
|
|
29
|
+
Highlight
|
|
30
|
+
],
|
|
31
|
+
editorProps: {
|
|
32
|
+
attributes: {
|
|
33
|
+
languages: i18next.language,
|
|
34
|
+
autocomplete: "off",
|
|
35
|
+
autocorrect: "off",
|
|
36
|
+
autocapitalize: "off",
|
|
37
|
+
class: "min-h-full"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
onUpdate: ({ editor })=>{
|
|
41
|
+
handleChange(editor?.getJSON() ?? {});
|
|
42
|
+
}
|
|
43
|
+
}, []);
|
|
44
|
+
const providerValue = useMemo(()=>{
|
|
45
|
+
return {};
|
|
46
|
+
}, []);
|
|
47
|
+
useEffect(()=>{
|
|
48
|
+
if (!additionalContext) {
|
|
49
|
+
i18n.init({
|
|
50
|
+
fallbackLng: DEFAULT_LANG,
|
|
51
|
+
lng: DEFAULT_LANG,
|
|
52
|
+
returnEmptyString: true,
|
|
53
|
+
resources: {}
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (additionalContext.language && additionalContext.translations) {
|
|
58
|
+
i18n.init({
|
|
59
|
+
fallbackLng: additionalContext.language,
|
|
60
|
+
lng: additionalContext.language,
|
|
61
|
+
returnEmptyString: true,
|
|
62
|
+
resources: {
|
|
63
|
+
[additionalContext.language]: {
|
|
64
|
+
translation: additionalContext.translations
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (additionalContext.language) {
|
|
71
|
+
i18n.init({
|
|
72
|
+
fallbackLng: additionalContext?.language,
|
|
73
|
+
lng: additionalContext?.language,
|
|
74
|
+
returnEmptyString: true,
|
|
75
|
+
resources: {}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}, []);
|
|
79
|
+
return editor && /*#__PURE__*/ _jsx(EditorContext.Provider, {
|
|
80
|
+
value: providerValue,
|
|
81
|
+
children: /*#__PURE__*/ _jsx("div", {
|
|
82
|
+
className: "flex h-full overflow-visible",
|
|
83
|
+
ref: menuContainerRef,
|
|
84
|
+
dir: i18next.dir(),
|
|
85
|
+
children: /*#__PURE__*/ _jsxs("div", {
|
|
86
|
+
className: "one-liner relative flex flex-col flex-1 h-full justify-center items-center ",
|
|
87
|
+
children: [
|
|
88
|
+
/*#__PURE__*/ _jsx(EditorContent, {
|
|
89
|
+
dir: i18next.dir(),
|
|
90
|
+
editor: editor,
|
|
91
|
+
ref: editorRef,
|
|
92
|
+
className: "flex-1 overflow-y-visible w-full h-full outline-none outline-offset-0",
|
|
93
|
+
style: {
|
|
94
|
+
outline: "none"
|
|
95
|
+
}
|
|
96
|
+
}),
|
|
97
|
+
isEditable && /*#__PURE__*/ _jsx(_Fragment, {
|
|
98
|
+
children: /*#__PURE__*/ _jsx(AITextMenu, {
|
|
99
|
+
editor: editor,
|
|
100
|
+
dir: i18next.dir()
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
]
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
export default OneLineEditor;
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=OneLineEditor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../src/fields/TiptapEditor/features/BlockEditor/OneLineEditor.tsx"],"sourcesContent":["\"use client\";\nimport { EditorContent, useEditor } from \"@tiptap/react\";\nimport i18n from \"i18next\";\nimport { useEffect, useMemo, useRef } from \"react\";\n\nimport { EditorContext } from \"../../context/EditorContext.js\";\n\nimport i18next from \"i18next\";\nimport Text from '@tiptap/extension-text'\nimport Highlight from '@tiptap/extension-highlight'\nimport { AICommand, Heading, Paragraph, StarterKit } from \"../../extensions/index.js\";\nimport OneLiner from \"./components/OneLiner.js\";\nimport { AdditionalContextType, TiptapProps } from \"./types.js\";\nimport { AITextMenu } from \"../menus/TextMenu/AITextMenu.js\";\n\nconst DEFAULT_LANG = \"en\"\n\nexport const OneLineEditor = ({\n handleChange,\n content,\n additionalContext,\n isEditable = true,\n autoFocus = true,\n dir,\n}: TiptapProps & {\n additionalContext?: AdditionalContextType;\n autoFocus?: boolean;\n isEditable?: boolean;\n dir?: \"ltr\" | \"rtl\" | \"auto\";\n}) => {\n const menuContainerRef = useRef(null);\n const editorRef = useRef<HTMLDivElement>(null);\n\n const editor = useEditor(\n {\n content: content?.json ?? content ?? {},\n immediatelyRender: false,\n autofocus: autoFocus ?? true,\n editable: isEditable ?? true,\n onCreate: ({ editor }) => ({\n // provider?.on('synced', () => {\n // if (editor.isEmpty) {\n // editor.commands.setContent(initialContent)\n // }\n // })\n }),\n extensions: [\n OneLiner,\n Text,\n Paragraph,\n AICommand,\n Highlight\n\n ],\n editorProps: {\n attributes: {\n languages: i18next.language,\n autocomplete: \"off\",\n autocorrect: \"off\",\n autocapitalize: \"off\",\n class: \"min-h-full\",\n },\n },\n onUpdate: ({ editor }) => {\n handleChange(editor?.getJSON() ?? {});\n },\n },\n []\n );\n\n\n const providerValue = useMemo(() => {\n return {};\n }, []);\n useEffect(() => {\n if (!additionalContext) {\n i18n.init({\n fallbackLng: DEFAULT_LANG,\n lng: DEFAULT_LANG,\n returnEmptyString: true,\n resources: {},\n });\n return\n }\n\n if (additionalContext.language && additionalContext.translations) {\n i18n.init({\n fallbackLng: additionalContext.language,\n lng: additionalContext.language,\n returnEmptyString: true,\n resources: {\n [additionalContext.language]: {\n translation: additionalContext.translations,\n },\n },\n });\n return\n }\n if (additionalContext.language) {\n i18n.init({\n fallbackLng: additionalContext?.language,\n lng: additionalContext?.language,\n returnEmptyString: true,\n resources: {},\n });\n }\n }, []);\n\n return (\n editor && (\n <EditorContext.Provider value={providerValue}>\n <div\n className=\"flex h-full overflow-visible\"\n ref={menuContainerRef}\n dir={i18next.dir()}\n >\n <div className=\"one-liner relative flex flex-col flex-1 h-full justify-center items-center \">\n <EditorContent\n dir={i18next.dir()}\n editor={editor}\n ref={editorRef}\n className=\"flex-1 overflow-y-visible w-full h-full outline-none outline-offset-0\"\n style={{\n outline: \"none\",\n }}\n />\n {isEditable && (\n <>\n <AITextMenu\n editor={editor}\n dir={i18next.dir()}\n />\n </>\n )}\n </div>\n </div>\n </EditorContext.Provider>\n )\n );\n};\n\nexport default OneLineEditor;\n"],"names":["EditorContent","useEditor","i18n","useEffect","useMemo","useRef","EditorContext","i18next","Text","Highlight","AICommand","Paragraph","OneLiner","AITextMenu","DEFAULT_LANG","OneLineEditor","handleChange","content","additionalContext","isEditable","autoFocus","dir","menuContainerRef","editorRef","editor","json","immediatelyRender","autofocus","editable","onCreate","extensions","editorProps","attributes","languages","language","autocomplete","autocorrect","autocapitalize","class","onUpdate","getJSON","providerValue","init","fallbackLng","lng","returnEmptyString","resources","translations","translation","Provider","value","div","className","ref","style","outline"],"mappings":"AAAA;;AACA,SAASA,aAAa,EAAEC,SAAS,QAAQ,gBAAgB;AACzD,OAAOC,UAAU,UAAU;AAC3B,SAASC,SAAS,EAAEC,OAAO,EAAEC,MAAM,QAAQ,QAAQ;AAEnD,SAASC,aAAa,QAAQ,iCAAiC;AAE/D,OAAOC,aAAa,UAAU;AAC9B,OAAOC,UAAU,yBAAwB;AACzC,OAAOC,eAAe,8BAA6B;AACnD,SAASC,SAAS,EAAWC,SAAS,QAAoB,4BAA4B;AACtF,OAAOC,cAAc,2BAA2B;AAEhD,SAASC,UAAU,QAAQ,kCAAkC;AAE7D,MAAMC,eAAe;AAErB,OAAO,MAAMC,gBAAgB,CAAC,EAC5BC,YAAY,EACZC,OAAO,EACPC,iBAAiB,EACjBC,aAAa,IAAI,EACjBC,YAAY,IAAI,EAChBC,GAAG,EAMJ;IACC,MAAMC,mBAAmBjB,OAAO;IAChC,MAAMkB,YAAYlB,OAAuB;IAEzC,MAAMmB,SAASvB,UACb;QACEgB,SAASA,SAASQ,QAAQR,WAAW,CAAC;QACtCS,mBAAmB;QACnBC,WAAWP,aAAa;QACxBQ,UAAUT,cAAc;QACxBU,UAAU,CAAC,EAAEL,MAAM,EAAE,GAAM,CAAA;YAM3B,CAAA;QACAM,YAAY;YACVlB;YACAJ;YACAG;YACAD;YACAD;SAED;QACDsB,aAAa;YACXC,YAAY;gBACVC,WAAW1B,QAAQ2B,QAAQ;gBAC3BC,cAAc;gBACdC,aAAa;gBACbC,gBAAgB;gBAChBC,OAAO;YACT;QACF;QACAC,UAAU,CAAC,EAAEf,MAAM,EAAE;YACnBR,aAAaQ,QAAQgB,aAAa,CAAC;QACrC;IACF,GACA,EAAE;IAIJ,MAAMC,gBAAgBrC,QAAQ;QAC5B,OAAO,CAAC;IACV,GAAG,EAAE;IACLD,UAAU;QACR,IAAI,CAACe,mBAAmB;YACtBhB,KAAKwC,IAAI,CAAC;gBACRC,aAAa7B;gBACb8B,KAAK9B;gBACL+B,mBAAmB;gBACnBC,WAAW,CAAC;YACd;YACA;QACF;QAEA,IAAI5B,kBAAkBgB,QAAQ,IAAIhB,kBAAkB6B,YAAY,EAAE;YAChE7C,KAAKwC,IAAI,CAAC;gBACRC,aAAazB,kBAAkBgB,QAAQ;gBACvCU,KAAK1B,kBAAkBgB,QAAQ;gBAC/BW,mBAAmB;gBACnBC,WAAW;oBACT,CAAC5B,kBAAkBgB,QAAQ,CAAC,EAAE;wBAC5Bc,aAAa9B,kBAAkB6B,YAAY;oBAC7C;gBACF;YACF;YACA;QACF;QACA,IAAI7B,kBAAkBgB,QAAQ,EAAE;YAC9BhC,KAAKwC,IAAI,CAAC;gBACRC,aAAazB,mBAAmBgB;gBAChCU,KAAK1B,mBAAmBgB;gBACxBW,mBAAmB;gBACnBC,WAAW,CAAC;YACd;QACF;IACF,GAAG,EAAE;IAEL,OACEtB,wBACE,KAAClB,cAAc2C,QAAQ;QAACC,OAAOT;kBAC7B,cAAA,KAACU;YACCC,WAAU;YACVC,KAAK/B;YACLD,KAAKd,QAAQc,GAAG;sBAEhB,cAAA,MAAC8B;gBAAIC,WAAU;;kCACb,KAACpD;wBACCqB,KAAKd,QAAQc,GAAG;wBAChBG,QAAQA;wBACR6B,KAAK9B;wBACL6B,WAAU;wBACVE,OAAO;4BACLC,SAAS;wBACX;;oBAEDpC,4BACC;kCACE,cAAA,KAACN;4BACCW,QAAQA;4BACRH,KAAKd,QAAQc,GAAG;;;;;;;AASlC,EAAE;AAEF,eAAeN,cAAc"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OneLiner.d.ts","sourceRoot":"","sources":["../../../../../../../src/fields/TiptapEditor/features/BlockEditor/components/OneLiner.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC,QAAA,MAAM,QAAQ,gBAIZ,CAAC;AAEH,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../src/fields/TiptapEditor/features/BlockEditor/components/OneLiner.tsx"],"sourcesContent":["import { Node } from \"@tiptap/core\";\n\nconst OneLiner = Node.create({\n name: \"oneLiner\",\n topNode: true,\n content: \"block\",\n});\n\nexport default OneLiner;"],"names":["Node","OneLiner","create","name","topNode","content"],"mappings":"AAAA,SAASA,IAAI,QAAQ,eAAe;AAEpC,MAAMC,WAAWD,KAAKE,MAAM,CAAC;IACzBC,MAAM;IACNC,SAAS;IACTC,SAAS;AACb;AAEA,eAAeJ,SAAS"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Editor } from "@tiptap/react";
|
|
2
|
+
export type TextMenuProps = {
|
|
3
|
+
editor: Editor;
|
|
4
|
+
dir: "rtl" | "ltr" | "auto";
|
|
5
|
+
};
|
|
6
|
+
export declare const AITextMenu: ({ editor, dir }: TextMenuProps) => import("react").JSX.Element;
|
|
7
|
+
//# sourceMappingURL=AITextMenu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AITextMenu.d.ts","sourceRoot":"","sources":["../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/AITextMenu.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAc,MAAM,EAAE,MAAM,eAAe,CAAC;AAkBnD,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;CAC7B,CAAC;AAEF,eAAO,MAAM,UAAU,oBAAqB,aAAa,gCA2CxD,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { BubbleMenu } from "@tiptap/react";
|
|
3
|
+
import { Sparkles } from "lucide-react";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { AIEditorPanel } from "../../panels/AIEditorPanel/AIEditorPanel.js";
|
|
6
|
+
import { Icon } from "../../ui/Icon.js";
|
|
7
|
+
import { Toolbar } from "../../ui/Toolbar.js";
|
|
8
|
+
// import { FontFamilyPicker } from "./components/FontFamilyPicker.js";
|
|
9
|
+
import i18next from "i18next";
|
|
10
|
+
import { useTextmenuCommands } from "./hooks/useTextmenuCommands.js";
|
|
11
|
+
import { useTextmenuContentTypes } from "./hooks/useTextmenuContentTypes.js";
|
|
12
|
+
import { useTextmenuStates } from "./hooks/useTextmenuStates.js";
|
|
13
|
+
export const AITextMenu = ({ editor, dir })=>{
|
|
14
|
+
const commands = useTextmenuCommands(editor);
|
|
15
|
+
const states = useTextmenuStates(editor);
|
|
16
|
+
const blockOptions = useTextmenuContentTypes(editor);
|
|
17
|
+
const [openAi, setOpenAi] = useState(false);
|
|
18
|
+
return /*#__PURE__*/ _jsx(BubbleMenu, {
|
|
19
|
+
tippyOptions: {
|
|
20
|
+
placement: "auto",
|
|
21
|
+
popperOptions: {
|
|
22
|
+
placement: "auto"
|
|
23
|
+
},
|
|
24
|
+
onHide: ()=>{
|
|
25
|
+
setOpenAi(false);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
editor: editor,
|
|
29
|
+
pluginKey: "textMenu",
|
|
30
|
+
shouldShow: states.shouldShow,
|
|
31
|
+
updateDelay: 100,
|
|
32
|
+
children: openAi ? /*#__PURE__*/ _jsx(AIEditorPanel, {
|
|
33
|
+
editor: editor,
|
|
34
|
+
onOpenChange: (value)=>{
|
|
35
|
+
setOpenAi(value);
|
|
36
|
+
}
|
|
37
|
+
}) : /*#__PURE__*/ _jsx(Toolbar.Wrapper, {
|
|
38
|
+
children: /*#__PURE__*/ _jsxs(Toolbar.Button, {
|
|
39
|
+
className: "text-purple-500 hover:text-purple-600 active:text-purple-600 dark:text-purple-400 dark:hover:text-purple-300 dark:active:text-purple-400",
|
|
40
|
+
activeClassname: "text-purple-600 hover:text-purple-600 dark:text-purple-400 dark:hover:text-purple-200",
|
|
41
|
+
onClick: ()=>setOpenAi(true),
|
|
42
|
+
children: [
|
|
43
|
+
/*#__PURE__*/ _jsx(Icon, {
|
|
44
|
+
icon: Sparkles,
|
|
45
|
+
className: "mr-1"
|
|
46
|
+
}),
|
|
47
|
+
i18next.t("aiTools") || "AI Tools"
|
|
48
|
+
]
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=AITextMenu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/AITextMenu.tsx"],"sourcesContent":["import { BubbleMenu, Editor } from \"@tiptap/react\";\nimport {\n Sparkles\n} from \"lucide-react\";\nimport { memo, useState } from \"react\";\nimport { AIEditorPanel } from \"../../panels/AIEditorPanel/AIEditorPanel.js\";\nimport { ColorPicker } from \"../../panels/index.js\";\nimport { Icon } from \"../../ui/Icon.js\";\nimport { Toolbar } from \"../../ui/Toolbar.js\";\nimport { ContentTypePicker } from \"./components/ContentTypePicker.js\";\n// import { FontFamilyPicker } from \"./components/FontFamilyPicker.js\";\nimport i18next from \"i18next\";\nimport { FontSizePicker } from \"./components/FontSizePicker.js\";\nimport { useTextmenuCommands } from \"./hooks/useTextmenuCommands.js\";\nimport { useTextmenuContentTypes } from \"./hooks/useTextmenuContentTypes.js\";\nimport { useTextmenuStates } from \"./hooks/useTextmenuStates.js\";\n\n\nexport type TextMenuProps = {\n editor: Editor;\n dir: \"rtl\" | \"ltr\" | \"auto\";\n};\n\nexport const AITextMenu = ({ editor, dir }: TextMenuProps) => {\n const commands = useTextmenuCommands(editor);\n const states = useTextmenuStates(editor);\n const blockOptions = useTextmenuContentTypes(editor);\n const [openAi, setOpenAi] = useState(false);\n\n return (\n <BubbleMenu\n tippyOptions={{\n placement: \"auto\",\n popperOptions: {\n placement: \"auto\",\n },\n onHide: () => {\n setOpenAi(false);\n },\n }}\n editor={editor}\n pluginKey=\"textMenu\"\n shouldShow={states.shouldShow}\n updateDelay={100}\n >\n {openAi ? (\n <AIEditorPanel\n editor={editor}\n onOpenChange={(value) => {\n setOpenAi(value);\n }}\n />\n ) : (\n <Toolbar.Wrapper>\n <Toolbar.Button\n className=\"text-purple-500 hover:text-purple-600 active:text-purple-600 dark:text-purple-400 dark:hover:text-purple-300 dark:active:text-purple-400\"\n activeClassname=\"text-purple-600 hover:text-purple-600 dark:text-purple-400 dark:hover:text-purple-200\"\n onClick={() => setOpenAi(true)}\n >\n <Icon icon={Sparkles} className=\"mr-1\" />\n {i18next.t(\"aiTools\") || \"AI Tools\"}\n </Toolbar.Button>\n </Toolbar.Wrapper>\n )}\n </BubbleMenu>\n );\n};\n"],"names":["BubbleMenu","Sparkles","useState","AIEditorPanel","Icon","Toolbar","i18next","useTextmenuCommands","useTextmenuContentTypes","useTextmenuStates","AITextMenu","editor","dir","commands","states","blockOptions","openAi","setOpenAi","tippyOptions","placement","popperOptions","onHide","pluginKey","shouldShow","updateDelay","onOpenChange","value","Wrapper","Button","className","activeClassname","onClick","icon","t"],"mappings":";AAAA,SAASA,UAAU,QAAgB,gBAAgB;AACnD,SACEC,QAAQ,QACH,eAAe;AACtB,SAAeC,QAAQ,QAAQ,QAAQ;AACvC,SAASC,aAAa,QAAQ,8CAA8C;AAE5E,SAASC,IAAI,QAAQ,mBAAmB;AACxC,SAASC,OAAO,QAAQ,sBAAsB;AAE9C,uEAAuE;AACvE,OAAOC,aAAa,UAAU;AAE9B,SAASC,mBAAmB,QAAQ,iCAAiC;AACrE,SAASC,uBAAuB,QAAQ,qCAAqC;AAC7E,SAASC,iBAAiB,QAAQ,+BAA+B;AAQjE,OAAO,MAAMC,aAAa,CAAC,EAAEC,MAAM,EAAEC,GAAG,EAAiB;IACvD,MAAMC,WAAWN,oBAAoBI;IACrC,MAAMG,SAASL,kBAAkBE;IACjC,MAAMI,eAAeP,wBAAwBG;IAC7C,MAAM,CAACK,QAAQC,UAAU,GAAGf,SAAS;IAErC,qBACE,KAACF;QACCkB,cAAc;YACZC,WAAW;YACXC,eAAe;gBACbD,WAAW;YACb;YACAE,QAAQ;gBACNJ,UAAU;YACZ;QACF;QACAN,QAAQA;QACRW,WAAU;QACVC,YAAYT,OAAOS,UAAU;QAC7BC,aAAa;kBAEZR,uBACC,KAACb;YACCQ,QAAQA;YACRc,cAAc,CAACC;gBACbT,UAAUS;YACZ;2BAGF,KAACrB,QAAQsB,OAAO;sBACd,cAAA,MAACtB,QAAQuB,MAAM;gBACbC,WAAU;gBACVC,iBAAgB;gBAChBC,SAAS,IAAMd,UAAU;;kCAEzB,KAACb;wBAAK4B,MAAM/B;wBAAU4B,WAAU;;oBAC/BvB,QAAQ2B,CAAC,CAAC,cAAc;;;;;AAMrC,EAAE"}
|
package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-completion-command.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { Check, TextQuote, TrashIcon } from "lucide-react";
|
|
3
|
-
import { Toolbar } from "../../../ui/Toolbar.js";
|
|
4
3
|
import { checkJsonCompletion, formatCompletion } from "../../../panels/AIEditorPanel/utils.js";
|
|
4
|
+
import { Toolbar } from "../../../ui/Toolbar.js";
|
|
5
5
|
const AICompletionCommands = ({ completion, onDiscard, editor })=>{
|
|
6
6
|
return /*#__PURE__*/ _jsxs(_Fragment, {
|
|
7
7
|
children: [
|
package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-completion-command.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/components/ai-completion-command.tsx"],"sourcesContent":["import { Editor } from \"@tiptap/react\";\nimport { Check, TextQuote, TrashIcon } from \"lucide-react\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/components/ai-completion-command.tsx"],"sourcesContent":["import { Editor } from \"@tiptap/react\";\nimport { Check, TextQuote, TrashIcon } from \"lucide-react\";\nimport { checkJsonCompletion, formatCompletion } from \"../../../panels/AIEditorPanel/utils.js\";\nimport { Toolbar } from \"../../../ui/Toolbar.js\";\n\nconst AICompletionCommands = ({\n completion,\n onDiscard,\n editor,\n}: {\n editor: Editor;\n completion: string;\n onDiscard: () => void;\n}) => {\n return (\n <>\n <Toolbar.Button\n className=\"gap-2 px-4 w-full flex justify-start \"\n value=\"replace\"\n onClick={() => {\n const selection = editor.view.state.selection;\n\n editor\n .chain()\n .focus()\n .insertContentAt(\n {\n from: selection.from,\n to: selection.to,\n },\n checkJsonCompletion(completion) ? formatCompletion(completion) : completion\n )\n .run();\n }}\n >\n <Check className=\"h-4 w-4 text-muted-foreground\" />\n Replace selection\n </Toolbar.Button>\n <Toolbar.Button\n className=\"gap-2 px-4 w-full flex justify-start\"\n value=\"insert\"\n onClick={() => {\n const selection = editor.view.state.selection;\n editor\n .chain()\n .focus()\n .insertContentAt(selection.to + 1,\n checkJsonCompletion(completion) ? formatCompletion(completion) : completion\n )\n .run();\n }}\n >\n <TextQuote className=\"h-4 w-4 text-muted-foreground\" />\n Insert below\n </Toolbar.Button>\n <Toolbar.Divider horizontal />\n\n <Toolbar.Button\n onClick={onDiscard}\n value=\"thrash\"\n className=\"w-full text-red-500 bg-red-500 dark:text-red-500 hover:bg-red-500 dark:hover:text-red-500 dark:hover:bg-red-500 bg-opacity-10 hover:bg-opacity-20 dark:hover:bg-opacity-20\"\n >\n <TrashIcon className=\"h-4 w-4 text-muted-foreground\" />\n Discard\n </Toolbar.Button>\n </>\n );\n};\n\nexport default AICompletionCommands;\n"],"names":["Check","TextQuote","TrashIcon","checkJsonCompletion","formatCompletion","Toolbar","AICompletionCommands","completion","onDiscard","editor","Button","className","value","onClick","selection","view","state","chain","focus","insertContentAt","from","to","run","Divider","horizontal"],"mappings":";AACA,SAASA,KAAK,EAAEC,SAAS,EAAEC,SAAS,QAAQ,eAAe;AAC3D,SAASC,mBAAmB,EAAEC,gBAAgB,QAAQ,yCAAyC;AAC/F,SAASC,OAAO,QAAQ,yBAAyB;AAEjD,MAAMC,uBAAuB,CAAC,EAC5BC,UAAU,EACVC,SAAS,EACTC,MAAM,EAKP;IACC,qBACE;;0BACE,MAACJ,QAAQK,MAAM;gBACbC,WAAU;gBACVC,OAAM;gBACNC,SAAS;oBACP,MAAMC,YAAYL,OAAOM,IAAI,CAACC,KAAK,CAACF,SAAS;oBAE7CL,OACGQ,KAAK,GACLC,KAAK,GACLC,eAAe,CACd;wBACEC,MAAMN,UAAUM,IAAI;wBACpBC,IAAIP,UAAUO,EAAE;oBAClB,GACAlB,oBAAoBI,cAAcH,iBAAiBG,cAAcA,YAElEe,GAAG;gBACR;;kCAEA,KAACtB;wBAAMW,WAAU;;oBAAkC;;;0BAGrD,MAACN,QAAQK,MAAM;gBACbC,WAAU;gBACVC,OAAM;gBACNC,SAAS;oBACP,MAAMC,YAAYL,OAAOM,IAAI,CAACC,KAAK,CAACF,SAAS;oBAC7CL,OACGQ,KAAK,GACLC,KAAK,GACLC,eAAe,CAACL,UAAUO,EAAE,GAAG,GAC9BlB,oBAAoBI,cAAcH,iBAAiBG,cAAcA,YAElEe,GAAG;gBACR;;kCAEA,KAACrB;wBAAUU,WAAU;;oBAAkC;;;0BAGzD,KAACN,QAAQkB,OAAO;gBAACC,UAAU;;0BAE3B,MAACnB,QAAQK,MAAM;gBACbG,SAASL;gBACTI,OAAM;gBACND,WAAU;;kCAEV,KAACT;wBAAUS,WAAU;;oBAAkC;;;;;AAK/D;AAEA,eAAeL,qBAAqB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-selector-commands.d.ts","sourceRoot":"","sources":["../../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"ai-selector-commands.d.ts","sourceRoot":"","sources":["../../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAyDvC,UAAU,uBAAuB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,CACR,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,IAAI,CAAA;KAAE,KAC7D,IAAI,CAAC;CACX;AAED,QAAA,MAAM,kBAAkB,oCAIrB,uBAAuB,gCA8FzB,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
|
package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.js
CHANGED
|
@@ -9,6 +9,7 @@ import { Icon } from "../../../ui/Icon.js";
|
|
|
9
9
|
import { Surface } from "../../../ui/Surface.js";
|
|
10
10
|
import { Toolbar } from "../../../ui/Toolbar.js";
|
|
11
11
|
import i18next from "i18next";
|
|
12
|
+
import { formatCompletion } from "../../../panels/AIEditorPanel/utils.js";
|
|
12
13
|
const options = [
|
|
13
14
|
// {
|
|
14
15
|
// value: 'translate',
|
|
@@ -92,7 +93,7 @@ const AISelectorCommands = ({ messages, onSelect, editor })=>{
|
|
|
92
93
|
onSelect("", {
|
|
93
94
|
option: "translate",
|
|
94
95
|
language: lang.value,
|
|
95
|
-
selection: editorSelection
|
|
96
|
+
selection: messages ? formatCompletion(messages) : editorSelection
|
|
96
97
|
});
|
|
97
98
|
},
|
|
98
99
|
className: "gap-2 px-4 w-full flex justify-start ",
|
|
@@ -116,7 +117,7 @@ const AISelectorCommands = ({ messages, onSelect, editor })=>{
|
|
|
116
117
|
onSelect(content, {
|
|
117
118
|
option: option.value,
|
|
118
119
|
language: editor?.storage?.aiCommand?.language ?? code ?? "en",
|
|
119
|
-
selection: editorSelection
|
|
120
|
+
selection: messages ? formatCompletion(messages) : editorSelection
|
|
120
121
|
});
|
|
121
122
|
},
|
|
122
123
|
className: " gap-2 px-4 w-full flex justify-start",
|
|
@@ -143,7 +144,7 @@ const AISelectorCommands = ({ messages, onSelect, editor })=>{
|
|
|
143
144
|
onSelect(text, {
|
|
144
145
|
option: "continue",
|
|
145
146
|
language: editor?.storage?.aiCommand?.language ?? code ?? "en",
|
|
146
|
-
selection: editorSelection
|
|
147
|
+
selection: messages ? formatCompletion(messages) : editorSelection
|
|
147
148
|
});
|
|
148
149
|
},
|
|
149
150
|
value: "continue",
|
package/dist/src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.tsx"],"sourcesContent":["import { useLocale } from \"@payloadcms/ui/providers/Locale\";\nimport * as Popover from \"@radix-ui/react-popover\";\nimport { Editor } from \"@tiptap/react\";\nimport {\n ArrowDownWideNarrow,\n CheckCheck,\n ChevronDown,\n Languages,\n RefreshCcwDot,\n StepForward,\n WrapText,\n} from \"lucide-react\";\nimport { languages } from \"../../../../lib/constants.js\";\nimport { getPrevText } from \"../../../../lib/utils/index.js\";\nimport { DropdownCategoryTitle } from \"../../../ui/Dropdown/Dropdown.js\";\nimport { Icon } from \"../../../ui/Icon.js\";\nimport { Surface } from \"../../../ui/Surface.js\";\nimport { Toolbar } from \"../../../ui/Toolbar.js\";\nimport i18next from \"i18next\";\nconst options = [\n // {\n // value: 'translate',\n // label: 'Translate text',\n // icon: WrapText,\n // },\n {\n value: \"summarize\",\n label: \"Summarize text\",\n icon: WrapText,\n },\n {\n value: \"rewrite\",\n label: \"Rewrite into Axios\",\n icon: WrapText,\n },\n {\n value: \"improve\",\n label: \"Improve writing\",\n icon: RefreshCcwDot,\n },\n\n {\n value: \"fix\",\n label: \"Fix grammar\",\n icon: CheckCheck,\n },\n {\n value: \"shorter\",\n label: \"Make shorter\",\n icon: ArrowDownWideNarrow,\n },\n {\n value: \"longer\",\n label: \"Make longer\",\n icon: WrapText,\n },\n];\n\ninterface AISelectorCommandsProps {\n editor: Editor;\n messages?: string;\n onSelect: (\n value: string,\n options: { option: string; language?: string, selection?: JSON }\n ) => void;\n}\n\nconst AISelectorCommands = ({\n messages,\n onSelect,\n editor,\n}: AISelectorCommandsProps) => {\n const { code } = useLocale();\n return (\n <>\n <DropdownCategoryTitle>Edit or review selection</DropdownCategoryTitle>\n <Popover.Root>\n <Popover.Trigger asChild>\n <Toolbar.Button\n className=\"gap-2 px-4 w-full flex justify-start \"\n type=\"button\"\n // active={!!states.currentHighlight}\n tooltip={i18next.t(\"highlightText\") || \"Highlight text\"}\n >\n <Icon icon={Languages} className=\"h-4 w-4 text-blue-500\" />\n Translate text\n <Icon icon={ChevronDown} className=\"h-4 w-4 text-blue-500\" />\n </Toolbar.Button>\n </Popover.Trigger>\n <Popover.Content side=\"right\" align=\"start\" sideOffset={8} asChild>\n <Surface className=\"flex flex-col min-w-[15rem] p-2 max-h-[20rem] overflow-auto\">\n <DropdownCategoryTitle>Languages</DropdownCategoryTitle>\n\n {languages.map((lang) => (\n <Toolbar.Button\n onClick={() => {\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n onSelect(\"\", {\n option: \"translate\",\n language: lang.value,\n selection: editorSelection\n });\n }}\n className=\"gap-2 px-4 w-full flex justify-start \"\n key={lang.value}\n >\n {lang.label}\n </Toolbar.Button>\n ))}\n </Surface>\n </Popover.Content>\n </Popover.Root>\n\n {options.map((option) => (\n <Toolbar.Button\n // onSelect={(value) => {\n // const slice = editor.state.selection.content()\n // const text = editor?.storage?.markdown?.serializer.serialize(slice.content)\n // onSelect(text, option.value)\n // }}\n onClick={() => {\n const { from, to, empty } = editor.state.selection;\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n const content = messages\n ? messages ?? \"\"\n : editor.state.doc.textBetween(from, to, \" \");\n onSelect(content, {\n option: option.value,\n language: editor?.storage?.aiCommand?.language ?? code ?? \"en\",\n selection: editorSelection\n });\n }}\n className=\" gap-2 px-4 w-full flex justify-start\"\n key={option.value}\n value={option.value}\n >\n <option.icon className=\"h-4 w-4 text-blue-500\" />\n {option.label}\n </Toolbar.Button>\n ))}\n <Toolbar.Divider horizontal />\n <DropdownCategoryTitle>Use AI to do more</DropdownCategoryTitle>\n <Toolbar.Button\n onClick={() => {\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n const text = messages\n ? messages\n : getPrevText(editor, { chars: 5000 });\n onSelect(text, {\n option: \"continue\",\n language: editor?.storage?.aiCommand?.language ?? code ?? \"en\",\n selection: editorSelection\n });\n }}\n value=\"continue\"\n className=\"gap-2 px-4 w-full flex justify-start\"\n >\n <StepForward className=\"h-4 w-4 text-blue-500\" />\n Continue writing\n </Toolbar.Button>\n </>\n );\n};\n\nexport default AISelectorCommands;\n"],"names":["useLocale","Popover","ArrowDownWideNarrow","CheckCheck","ChevronDown","Languages","RefreshCcwDot","StepForward","WrapText","languages","getPrevText","DropdownCategoryTitle","Icon","Surface","Toolbar","i18next","options","value","label","icon","AISelectorCommands","messages","onSelect","editor","code","Root","Trigger","asChild","Button","className","type","tooltip","t","Content","side","align","sideOffset","map","lang","onClick","editorSelection","state","selection","content","toJSON","option","language","from","to","empty","doc","textBetween","storage","aiCommand","Divider","horizontal","text","chars"],"mappings":";AAAA,SAASA,SAAS,QAAQ,kCAAkC;AAC5D,YAAYC,aAAa,0BAA0B;AAEnD,SACEC,mBAAmB,EACnBC,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,aAAa,EACbC,WAAW,EACXC,QAAQ,QACH,eAAe;AACtB,SAASC,SAAS,QAAQ,+BAA+B;AACzD,SAASC,WAAW,QAAQ,iCAAiC;AAC7D,SAASC,qBAAqB,QAAQ,mCAAmC;AACzE,SAASC,IAAI,QAAQ,sBAAsB;AAC3C,SAASC,OAAO,QAAQ,yBAAyB;AACjD,SAASC,OAAO,QAAQ,yBAAyB;AACjD,OAAOC,aAAa,UAAU;AAC9B,MAAMC,UAAU;IACd,IAAI;IACJ,wBAAwB;IACxB,6BAA6B;IAC7B,oBAAoB;IACpB,KAAK;IACL;QACEC,OAAO;QACPC,OAAO;QACPC,
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../src/fields/TiptapEditor/features/menus/TextMenu/components/ai-selector-commands.tsx"],"sourcesContent":["import { useLocale } from \"@payloadcms/ui/providers/Locale\";\nimport * as Popover from \"@radix-ui/react-popover\";\nimport { Editor } from \"@tiptap/react\";\nimport {\n ArrowDownWideNarrow,\n CheckCheck,\n ChevronDown,\n Languages,\n RefreshCcwDot,\n StepForward,\n WrapText,\n} from \"lucide-react\";\nimport { languages } from \"../../../../lib/constants.js\";\nimport { getPrevText } from \"../../../../lib/utils/index.js\";\nimport { DropdownCategoryTitle } from \"../../../ui/Dropdown/Dropdown.js\";\nimport { Icon } from \"../../../ui/Icon.js\";\nimport { Surface } from \"../../../ui/Surface.js\";\nimport { Toolbar } from \"../../../ui/Toolbar.js\";\nimport i18next from \"i18next\";\nimport { formatCompletion } from \"../../../panels/AIEditorPanel/utils.js\";\nconst options = [\n // {\n // value: 'translate',\n // label: 'Translate text',\n // icon: WrapText,\n // },\n {\n value: \"summarize\",\n label: \"Summarize text\",\n icon: WrapText,\n },\n {\n value: \"rewrite\",\n label: \"Rewrite into Axios\",\n icon: WrapText,\n },\n {\n value: \"improve\",\n label: \"Improve writing\",\n icon: RefreshCcwDot,\n },\n\n {\n value: \"fix\",\n label: \"Fix grammar\",\n icon: CheckCheck,\n },\n {\n value: \"shorter\",\n label: \"Make shorter\",\n icon: ArrowDownWideNarrow,\n },\n {\n value: \"longer\",\n label: \"Make longer\",\n icon: WrapText,\n },\n];\n\ninterface AISelectorCommandsProps {\n editor: Editor;\n messages?: string;\n onSelect: (\n value: string,\n options: { option: string; language?: string, selection?: JSON }\n ) => void;\n}\n\nconst AISelectorCommands = ({\n messages,\n onSelect,\n editor,\n}: AISelectorCommandsProps) => {\n const { code } = useLocale();\n return (\n <>\n <DropdownCategoryTitle>Edit or review selection</DropdownCategoryTitle>\n <Popover.Root>\n <Popover.Trigger asChild>\n <Toolbar.Button\n className=\"gap-2 px-4 w-full flex justify-start \"\n type=\"button\"\n // active={!!states.currentHighlight}\n tooltip={i18next.t(\"highlightText\") || \"Highlight text\"}\n >\n <Icon icon={Languages} className=\"h-4 w-4 text-blue-500\" />\n Translate text\n <Icon icon={ChevronDown} className=\"h-4 w-4 text-blue-500\" />\n </Toolbar.Button>\n </Popover.Trigger>\n <Popover.Content side=\"right\" align=\"start\" sideOffset={8} asChild>\n <Surface className=\"flex flex-col min-w-[15rem] p-2 max-h-[20rem] overflow-auto\">\n <DropdownCategoryTitle>Languages</DropdownCategoryTitle>\n\n {languages.map((lang) => (\n <Toolbar.Button\n onClick={() => {\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n onSelect(\"\", {\n option: \"translate\",\n language: lang.value,\n selection: messages ? formatCompletion(messages) : editorSelection\n });\n }}\n className=\"gap-2 px-4 w-full flex justify-start \"\n key={lang.value}\n >\n {lang.label}\n </Toolbar.Button>\n ))}\n </Surface>\n </Popover.Content>\n </Popover.Root>\n\n {options.map((option) => (\n <Toolbar.Button\n // onSelect={(value) => {\n // const slice = editor.state.selection.content()\n // const text = editor?.storage?.markdown?.serializer.serialize(slice.content)\n // onSelect(text, option.value)\n // }}\n onClick={() => {\n const { from, to, empty } = editor.state.selection;\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n const content = messages\n ? messages ?? \"\"\n : editor.state.doc.textBetween(from, to, \" \");\n onSelect(content, {\n option: option.value,\n language: editor?.storage?.aiCommand?.language ?? code ?? \"en\",\n selection: messages ? formatCompletion(messages) : editorSelection\n });\n }}\n className=\" gap-2 px-4 w-full flex justify-start\"\n key={option.value}\n value={option.value}\n >\n <option.icon className=\"h-4 w-4 text-blue-500\" />\n {option.label}\n </Toolbar.Button>\n ))}\n <Toolbar.Divider horizontal />\n <DropdownCategoryTitle>Use AI to do more</DropdownCategoryTitle>\n <Toolbar.Button\n onClick={() => {\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n const text = messages\n ? messages\n : getPrevText(editor, { chars: 5000 });\n onSelect(text, {\n option: \"continue\",\n language: editor?.storage?.aiCommand?.language ?? code ?? \"en\",\n selection: messages ? formatCompletion(messages) : editorSelection\n });\n }}\n value=\"continue\"\n className=\"gap-2 px-4 w-full flex justify-start\"\n >\n <StepForward className=\"h-4 w-4 text-blue-500\" />\n Continue writing\n </Toolbar.Button>\n </>\n );\n};\n\nexport default AISelectorCommands;\n"],"names":["useLocale","Popover","ArrowDownWideNarrow","CheckCheck","ChevronDown","Languages","RefreshCcwDot","StepForward","WrapText","languages","getPrevText","DropdownCategoryTitle","Icon","Surface","Toolbar","i18next","formatCompletion","options","value","label","icon","AISelectorCommands","messages","onSelect","editor","code","Root","Trigger","asChild","Button","className","type","tooltip","t","Content","side","align","sideOffset","map","lang","onClick","editorSelection","state","selection","content","toJSON","option","language","from","to","empty","doc","textBetween","storage","aiCommand","Divider","horizontal","text","chars"],"mappings":";AAAA,SAASA,SAAS,QAAQ,kCAAkC;AAC5D,YAAYC,aAAa,0BAA0B;AAEnD,SACEC,mBAAmB,EACnBC,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,aAAa,EACbC,WAAW,EACXC,QAAQ,QACH,eAAe;AACtB,SAASC,SAAS,QAAQ,+BAA+B;AACzD,SAASC,WAAW,QAAQ,iCAAiC;AAC7D,SAASC,qBAAqB,QAAQ,mCAAmC;AACzE,SAASC,IAAI,QAAQ,sBAAsB;AAC3C,SAASC,OAAO,QAAQ,yBAAyB;AACjD,SAASC,OAAO,QAAQ,yBAAyB;AACjD,OAAOC,aAAa,UAAU;AAC9B,SAASC,gBAAgB,QAAQ,yCAAyC;AAC1E,MAAMC,UAAU;IACd,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BACrD;gBACF;gBACAvB,OAAM;gBACNY,WAAU;;kCAEV,KAACvB;wBAAYuB,WAAU;;oBAA0B;;;;;AAKzD;AAEA,eAAeT,mBAAmB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AICommandPanel.d.ts","sourceRoot":"","sources":["../../../../../../../src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.tsx"],"names":[],"mappings":"AAIA,OAAO,KAON,MAAM,OAAO,CAAC;AAKf,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"AICommandPanel.d.ts","sourceRoot":"","sources":["../../../../../../../src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.tsx"],"names":[],"mappings":"AAIA,OAAO,KAON,MAAM,OAAO,CAAC;AAKf,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAGvC,OAAO,EAEL,KAAK,EACN,MAAM,uDAAuD,CAAC;AAY/D,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IACvC,KAAK,EAAE,KAAK,EAAE,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,cAAc,qFAgU1B,CAAC"}
|
|
@@ -6,15 +6,17 @@ import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef,
|
|
|
6
6
|
import Markdown from "react-markdown";
|
|
7
7
|
import TextareaAutosize from "react-textarea-autosize";
|
|
8
8
|
import { useLocale } from "@payloadcms/ui/providers/Locale";
|
|
9
|
+
import i18next from "i18next";
|
|
10
|
+
import { extractMarkdown } from "../../../lib/extract.js";
|
|
9
11
|
import { getPrevText } from "../../../lib/utils/index.js";
|
|
12
|
+
import AISelectorCommands from "../../menus/TextMenu/components/ai-selector-commands.js";
|
|
10
13
|
import CrazySpinner from "../../ui/crazy-spinner.js";
|
|
11
14
|
import { DropdownButton } from "../../ui/Dropdown/Dropdown.js";
|
|
12
15
|
import { Icon } from "../../ui/Icon.js";
|
|
13
16
|
import { ScrollArea } from "../../ui/scroll-area.js";
|
|
14
17
|
import { Surface } from "../../ui/Surface.js";
|
|
15
18
|
import { Toolbar } from "../../ui/Toolbar.js";
|
|
16
|
-
import
|
|
17
|
-
import i18next from "i18next";
|
|
19
|
+
import { checkJsonCompletion, formatCompletion } from "../AIEditorPanel/utils.js";
|
|
18
20
|
export const AICommandPanel = /*#__PURE__*/ forwardRef(({ editor, onOpenChange, userPrompt, items, ...restProps }, ref)=>{
|
|
19
21
|
const [inputValue, setInputValue] = useState(userPrompt ?? "");
|
|
20
22
|
const { code } = useLocale();
|
|
@@ -48,7 +50,7 @@ export const AICommandPanel = /*#__PURE__*/ forwardRef(({ editor, onOpenChange,
|
|
|
48
50
|
option: "zap",
|
|
49
51
|
command: inputValue,
|
|
50
52
|
language: code ?? "en",
|
|
51
|
-
selection:
|
|
53
|
+
selection: formatCompletion(completion)
|
|
52
54
|
}
|
|
53
55
|
}).then(()=>setInputValue(""));
|
|
54
56
|
}
|
|
@@ -65,7 +67,8 @@ export const AICommandPanel = /*#__PURE__*/ forwardRef(({ editor, onOpenChange,
|
|
|
65
67
|
}).then(()=>setInputValue(""));
|
|
66
68
|
}, [
|
|
67
69
|
code,
|
|
68
|
-
inputValue
|
|
70
|
+
inputValue,
|
|
71
|
+
completion
|
|
69
72
|
]);
|
|
70
73
|
useEffect(()=>{
|
|
71
74
|
if (userPrompt) setInputValue(userPrompt);
|
|
@@ -73,16 +76,9 @@ export const AICommandPanel = /*#__PURE__*/ forwardRef(({ editor, onOpenChange,
|
|
|
73
76
|
userPrompt
|
|
74
77
|
]);
|
|
75
78
|
useEffect(()=>{
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// editor
|
|
80
|
-
// .chain()
|
|
81
|
-
// .focus()
|
|
82
|
-
// .insertContentAt(selection.to - 1, completion)
|
|
83
|
-
// .insertContentAt(selection.to, " ")
|
|
84
|
-
// .run();
|
|
85
|
-
// }
|
|
79
|
+
if (!isLoading && hasCompletion) {
|
|
80
|
+
editor.storage.aiCommand.completion = checkJsonCompletion(completion) ? formatCompletion(completion) : completion;
|
|
81
|
+
}
|
|
86
82
|
}, [
|
|
87
83
|
completion,
|
|
88
84
|
isLoading
|
|
@@ -177,14 +173,16 @@ export const AICommandPanel = /*#__PURE__*/ forwardRef(({ editor, onOpenChange,
|
|
|
177
173
|
},
|
|
178
174
|
children: /*#__PURE__*/ _jsxs(_Fragment, {
|
|
179
175
|
children: [
|
|
180
|
-
hasCompletion && /*#__PURE__*/ _jsx("div", {
|
|
176
|
+
hasCompletion && !isLoading && /*#__PURE__*/ _jsx("div", {
|
|
181
177
|
className: "flex w-full",
|
|
182
178
|
children: /*#__PURE__*/ _jsx(ScrollArea, {
|
|
183
179
|
className: "h-72 prose p-2 px-4 prose-sm w-full max-w-none",
|
|
184
180
|
dir: i18next.dir(),
|
|
185
181
|
children: /*#__PURE__*/ _jsx(Markdown, {
|
|
186
|
-
|
|
187
|
-
|
|
182
|
+
children: checkJsonCompletion(completion) ? extractMarkdown({
|
|
183
|
+
type: 'doc',
|
|
184
|
+
content: formatCompletion(completion)
|
|
185
|
+
}) : completion
|
|
188
186
|
})
|
|
189
187
|
})
|
|
190
188
|
}),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.tsx"],"sourcesContent":["\"use client\";\n\nimport { useCompletion } from \"ai/react\";\nimport { ArrowUpCircle, Sparkles } from \"lucide-react\";\nimport React, {\n forwardRef,\n useCallback,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\nimport Markdown from \"react-markdown\";\nimport TextareaAutosize from \"react-textarea-autosize\";\n\nimport { useLocale } from \"@payloadcms/ui/providers/Locale\";\nimport { Editor } from \"@tiptap/react\";\n\nimport {\n Command,\n Group,\n} from \"src/fields/TiptapEditor/extensions/AICommand/types.js\";\nimport { getPrevText } from \"../../../lib/utils/index.js\";\nimport CrazySpinner from \"../../ui/crazy-spinner.js\";\nimport { DropdownButton } from \"../../ui/Dropdown/Dropdown.js\";\nimport { Icon } from \"../../ui/Icon.js\";\nimport { ScrollArea } from \"../../ui/scroll-area.js\";\nimport { Surface } from \"../../ui/Surface.js\";\nimport { Toolbar } from \"../../ui/Toolbar.js\";\nimport AISelectorCommands from \"../../menus/TextMenu/components/ai-selector-commands.js\";\nimport i18next from \"i18next\";\n\nexport type AICommandPanelProps = {\n editor: Editor;\n userPrompt?: string;\n onOpenChange: (value: boolean) => void;\n items: Group[];\n};\n\nexport const AICommandPanel = forwardRef(\n (\n {\n editor,\n onOpenChange,\n userPrompt,\n items,\n ...restProps\n }: AICommandPanelProps,\n ref\n ) => {\n const [inputValue, setInputValue] = useState(userPrompt ?? \"\");\n const { code } = useLocale();\n const { view } = editor;\n const scrollContainer = useRef<HTMLDivElement>(null);\n const activeItem = useRef<HTMLButtonElement>(null);\n const [selectedGroupIndex, setSelectedGroupIndex] = useState(0);\n const [selectedCommandIndex, setSelectedCommandIndex] = useState(0);\n\n const editorNode = view.dom as HTMLElement;\n const boundigClient = editorNode.getBoundingClientRect();\n const inputRef = useRef<HTMLTextAreaElement>(null);\n\n\n const { completion, complete, isLoading } = useCompletion({\n // id: \"novel\",\n api: \"/api/generate\",\n onResponse: (response) => {\n if (response.status === 429) {\n console.log(\"You have reached your request limit for the day.\");\n\n return;\n }\n },\n\n onError: (e) => {\n console.log(\"ERROR\", e.message);\n },\n });\n\n const hasCompletion = completion.length > 0;\n const handleClick = useCallback(() => {\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n if (completion) {\n return complete(completion, {\n body: { option: \"zap\", command: inputValue, language: code ?? \"en\", selection: editorSelection },\n }).then(() => setInputValue(\"\"));\n }\n const text = getPrevText(editor, { chars: 5000 });\n\n complete(text, {\n body: {\n option: \"zap\",\n command:\n (editor.storage.aiCommand.title\n ? \"Context Title:\" + editor.storage.aiCommand.title + \";\\n\"\n : \"\") +\n (editor.storage.aiCommand.language\n ? \"Context language:\" + editor.storage.aiCommand.language + \";\\n\"\n : \"\") +\n inputValue,\n language: editor.storage.aiCommand.language ?? code ?? \"en\",\n selection: editorSelection\n },\n }).then(() => setInputValue(\"\"));\n }, [code, inputValue]);\n\n useEffect(() => {\n if (userPrompt) setInputValue(userPrompt);\n }, [userPrompt]);\n useEffect(() => {\n editor.storage.aiCommand.completion = completion;\n // if (!isLoading && completion) {\n // const selection = editor.state.selection;\n // editor\n // .chain()\n // .focus()\n // .insertContentAt(selection.to - 1, completion)\n // .insertContentAt(selection.to, \" \")\n // .run();\n\n // }\n }, [completion, isLoading]);\n\n // Anytime the groups change, i.e. the user types to narrow it down, we want to\n // reset the current selection to the first menu item\n useEffect(() => {\n setSelectedGroupIndex(0);\n setSelectedCommandIndex(0);\n }, [items]);\n\n const selectItem = useCallback(\n (groupIndex: number, commandIndex: number) => {\n const command = items[groupIndex].commands[commandIndex];\n\n setInputValue(command.action(editor));\n // props.command(command);\n },\n [items, editor]\n );\n\n useImperativeHandle(ref, () => ({\n onKeyDown: ({ event }: { event: React.KeyboardEvent }) => {\n if (event.key === \"ArrowDown\") {\n if (!items.length) {\n return false;\n }\n\n const commands = items[selectedGroupIndex].commands;\n\n let newCommandIndex = selectedCommandIndex + 1;\n let newGroupIndex = selectedGroupIndex;\n\n if (commands.length - 1 < newCommandIndex) {\n newCommandIndex = 0;\n newGroupIndex = selectedGroupIndex + 1;\n }\n\n if (items.length - 1 < newGroupIndex) {\n newGroupIndex = 0;\n }\n\n setSelectedCommandIndex(newCommandIndex);\n setSelectedGroupIndex(newGroupIndex);\n\n return true;\n }\n\n if (event.key === \"ArrowUp\") {\n if (!items.length) {\n return false;\n }\n\n let newCommandIndex = selectedCommandIndex - 1;\n let newGroupIndex = selectedGroupIndex;\n\n if (newCommandIndex < 0) {\n newGroupIndex = selectedGroupIndex - 1;\n newCommandIndex = items[newGroupIndex]?.commands.length - 1 || 0;\n }\n\n if (newGroupIndex < 0) {\n newGroupIndex = items.length - 1;\n newCommandIndex = items[newGroupIndex].commands.length - 1;\n }\n\n setSelectedCommandIndex(newCommandIndex);\n setSelectedGroupIndex(newGroupIndex);\n\n return true;\n }\n\n if (event.key === \"Enter\") {\n if (\n !items.length ||\n selectedGroupIndex === -1 ||\n selectedCommandIndex === -1\n ) {\n return false;\n }\n\n selectItem(selectedGroupIndex, selectedCommandIndex);\n\n return true;\n }\n\n return false;\n },\n }));\n\n useEffect(() => {\n if (activeItem.current && scrollContainer.current) {\n const offsetTop = activeItem.current.offsetTop;\n const offsetHeight = activeItem.current.offsetHeight;\n\n scrollContainer.current.scrollTop = offsetTop - offsetHeight;\n }\n }, [selectedCommandIndex, selectedGroupIndex]);\n\n const createCommandClickHandler = useCallback(\n (groupIndex: number, commandIndex: number) => {\n return () => {\n selectItem(groupIndex, commandIndex);\n };\n },\n [selectItem]\n );\n\n return (\n <>\n <Surface\n className={`p-2 min-w-[20rem] flex flex-col gap-2 w-full ${hasCompletion ? \"-mt-72\" : \"\"\n }`}\n style={{\n width: boundigClient?.width,\n }}\n >\n <>\n {hasCompletion && (\n <div className=\"flex w-full\">\n <ScrollArea\n className=\"h-72 prose p-2 px-4 prose-sm w-full max-w-none\"\n dir={i18next.dir()}\n >\n <Markdown className=\"w-full\">{completion}</Markdown>\n </ScrollArea>\n </div>\n )}\n\n {isLoading && (\n <div\n className=\"flex h-12 w-full items-center px-4 text-sm font-medium text-muted-foreground text-blue-500\"\n style={{\n width: boundigClient?.width,\n }}\n >\n <Sparkles className=\"mr-2 h-4 w-4 shrink-0 \" />\n AI is thinking\n <div className=\"ml-2 mt-1\">\n <CrazySpinner />\n </div>\n </div>\n )}\n\n {!isLoading && (\n <>\n <div className=\"flex justify-between items-center \">\n {/* <CommandInput\n value={inputValue}\n onValueChange={setInputValue}\n autoFocus\n placeholder={\n hasCompletion ? 'Tell AI what to do next' : 'Ask AI to edit or generate...'\n }\n // onFocus={() => addAIHighlight(editor)}\n onKeyDown={(e) => {\n if (e.key === 'Enter') handleClick()\n }}\n /> */}\n <Sparkles className=\"mr-2 h-4 w-4 shrink-0 text-blue-500 \" />\n <TextareaAutosize\n ref={inputRef}\n style={{ resize: \"none\" }}\n className=\"w-full p-2 text-black bg-white rounded dark:bg-black dark:text-white focus:outline-none outline-none border-0 \"\n value={inputValue}\n onChange={(e) => {\n editor.storage.aiCommand.userPrompt = e.target.value;\n\n setInputValue(e.target.value);\n }}\n placeholder={\n hasCompletion\n ? i18next.t(\"tellAiPlaceholder\") || \"Tell AI what to do next\"\n : i18next.t(\"askAiPlaceholder\") || \"Ask AI to edit or generate...\"\n }\n autoFocus\n // onFocus={() => {\n // addAIHighlight(editor)}}\n\n onKeyDown={(e) => {\n if (e.key === \"Enter\") handleClick();\n }}\n />\n <Toolbar.Button\n // size=\"icon\"\n\n // className=\"absolute right-2 top-1/2 h-6 w-6 -translate-y-1/2 rounded-full bg-zinc-100 hover:bg-zinc-150\"\n onClick={handleClick}\n >\n <ArrowUpCircle />\n </Toolbar.Button>\n </div>\n </>\n )}\n </>\n </Surface>\n {!isLoading && (\n <Surface\n ref={scrollContainer}\n className=\"text-black max-h-[min(80vh,24rem)] overflow-auto flex-wrap mb-8 p-2\"\n >\n <div className=\"grid grid-cols-1 gap-0.5\">\n {hasCompletion ? (\n <AISelectorCommands\n editor={editor}\n messages={completion}\n onSelect={(value, options) => {\n complete(value, { body: options });\n }}\n />\n ) : (\n items.map((group, groupIndex: number) => (\n <React.Fragment key={`${group.title}-wrapper`}>\n <div\n className=\"text-neutral-500 text-[0.65rem] col-[1/-1] mx-2 mt-4 font-semibold tracking-wider select-none uppercase first:mt-0.5\"\n key={`${group.title}`}\n >\n {group.title}\n </div>\n {group.commands.map(\n (command: Command, commandIndex: number) => (\n <DropdownButton\n key={`${command.label}`}\n isActive={\n selectedGroupIndex === groupIndex &&\n selectedCommandIndex === commandIndex\n }\n onClick={createCommandClickHandler(\n groupIndex,\n commandIndex\n )}\n >\n <Icon icon={command.icon} className=\"mr-1\" />\n {command.label}\n </DropdownButton>\n )\n )}\n </React.Fragment>\n ))\n )}\n </div>\n </Surface>\n )}\n </>\n );\n }\n);\n"],"names":["useCompletion","ArrowUpCircle","Sparkles","React","forwardRef","useCallback","useEffect","useImperativeHandle","useRef","useState","Markdown","TextareaAutosize","useLocale","getPrevText","CrazySpinner","DropdownButton","Icon","ScrollArea","Surface","Toolbar","AISelectorCommands","i18next","AICommandPanel","editor","onOpenChange","userPrompt","items","restProps","ref","inputValue","setInputValue","code","view","scrollContainer","activeItem","selectedGroupIndex","setSelectedGroupIndex","selectedCommandIndex","setSelectedCommandIndex","editorNode","dom","boundigClient","getBoundingClientRect","inputRef","completion","complete","isLoading","api","onResponse","response","status","console","log","onError","e","message","hasCompletion","length","handleClick","editorSelection","state","selection","content","toJSON","body","option","command","language","then","text","chars","storage","aiCommand","title","selectItem","groupIndex","commandIndex","commands","action","onKeyDown","event","key","newCommandIndex","newGroupIndex","current","offsetTop","offsetHeight","scrollTop","createCommandClickHandler","className","style","width","div","dir","resize","value","onChange","target","placeholder","t","autoFocus","Button","onClick","messages","onSelect","options","map","group","Fragment","isActive","icon","label"],"mappings":"AAAA;;AAEA,SAASA,aAAa,QAAQ,WAAW;AACzC,SAASC,aAAa,EAAEC,QAAQ,QAAQ,eAAe;AACvD,OAAOC,SACLC,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,mBAAmB,EACnBC,MAAM,EACNC,QAAQ,QACH,QAAQ;AACf,OAAOC,cAAc,iBAAiB;AACtC,OAAOC,sBAAsB,0BAA0B;AAEvD,SAASC,SAAS,QAAQ,kCAAkC;AAO5D,SAASC,WAAW,QAAQ,8BAA8B;AAC1D,OAAOC,kBAAkB,4BAA4B;AACrD,SAASC,cAAc,QAAQ,gCAAgC;AAC/D,SAASC,IAAI,QAAQ,mBAAmB;AACxC,SAASC,UAAU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,wBAAwB,4BAClC9E,QAAQ8E,CAAC,CAAC,uBAAuB;wCAEvCC,SAAS;wCACT,mBAAmB;wCACnB,6BAA6B;wCAE7BrB,WAAW,CAACzB;4CACV,IAAIA,EAAE2B,GAAG,KAAK,SAASvB;wCACzB;;kDAEF,KAACvC,QAAQkF,MAAM;wCACb,cAAc;wCAEd,4GAA4G;wCAC5GC,SAAS5C;kDAET,cAAA,KAACzD;;;;;;;;YAOZ,CAAC6C,2BACA,KAAC5B;gBACCU,KAAKK;gBACLwD,WAAU;0BAEV,cAAA,KAACG;oBAAIH,WAAU;8BACZjC,8BACC,KAACpC;wBACCG,QAAQA;wBACRgF,UAAU3D;wBACV4D,UAAU,CAACT,OAAOU;4BAChB5D,SAASkD,OAAO;gCAAE/B,MAAMyC;4BAAQ;wBAClC;yBAGF/E,MAAMgF,GAAG,CAAC,CAACC,OAAOhC,2BAChB,MAACxE,MAAMyG,QAAQ;;8CACb,KAAChB;oCACCH,WAAU;8CAGTkB,MAAMlC,KAAK;mCAFP,CAAC,EAAEkC,MAAMlC,KAAK,CAAC,CAAC;gCAItBkC,MAAM9B,QAAQ,CAAC6B,GAAG,CACjB,CAACxC,SAAkBU,6BACjB,MAAC7D;wCAEC8F,UACE1E,uBAAuBwC,cACvBtC,yBAAyBuC;wCAE3B0B,SAASd,0BACPb,YACAC;;0DAGF,KAAC5D;gDAAK8F,MAAM5C,QAAQ4C,IAAI;gDAAErB,WAAU;;4CACnCvB,QAAQ6C,KAAK;;uCAXT,CAAC,EAAE7C,QAAQ6C,KAAK,CAAC,CAAC;;2BAVV,CAAC,EAAEJ,MAAMlC,KAAK,CAAC,QAAQ,CAAC;;;;;AAiC7D,GACA"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../src/fields/TiptapEditor/features/panels/AICommandPanel/AICommandPanel.tsx"],"sourcesContent":["\"use client\";\n\nimport { useCompletion } from \"ai/react\";\nimport { ArrowUpCircle, Sparkles } from \"lucide-react\";\nimport React, {\n forwardRef,\n useCallback,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\nimport Markdown from \"react-markdown\";\nimport TextareaAutosize from \"react-textarea-autosize\";\n\nimport { useLocale } from \"@payloadcms/ui/providers/Locale\";\nimport { Editor } from \"@tiptap/react\";\n\nimport i18next from \"i18next\";\nimport {\n Command,\n Group,\n} from \"src/fields/TiptapEditor/extensions/AICommand/types.js\";\nimport { extractMarkdown } from \"../../../lib/extract.js\";\nimport { getPrevText } from \"../../../lib/utils/index.js\";\nimport AISelectorCommands from \"../../menus/TextMenu/components/ai-selector-commands.js\";\nimport CrazySpinner from \"../../ui/crazy-spinner.js\";\nimport { DropdownButton } from \"../../ui/Dropdown/Dropdown.js\";\nimport { Icon } from \"../../ui/Icon.js\";\nimport { ScrollArea } from \"../../ui/scroll-area.js\";\nimport { Surface } from \"../../ui/Surface.js\";\nimport { Toolbar } from \"../../ui/Toolbar.js\";\nimport { checkJsonCompletion, formatCompletion } from \"../AIEditorPanel/utils.js\";\n\nexport type AICommandPanelProps = {\n editor: Editor;\n userPrompt?: string;\n onOpenChange: (value: boolean) => void;\n items: Group[];\n};\n\nexport const AICommandPanel = forwardRef(\n (\n {\n editor,\n onOpenChange,\n userPrompt,\n items,\n ...restProps\n }: AICommandPanelProps,\n ref\n ) => {\n const [inputValue, setInputValue] = useState(userPrompt ?? \"\");\n const { code } = useLocale();\n const { view } = editor;\n const scrollContainer = useRef<HTMLDivElement>(null);\n const activeItem = useRef<HTMLButtonElement>(null);\n const [selectedGroupIndex, setSelectedGroupIndex] = useState(0);\n const [selectedCommandIndex, setSelectedCommandIndex] = useState(0);\n\n const editorNode = view.dom as HTMLElement;\n const boundigClient = editorNode.getBoundingClientRect();\n const inputRef = useRef<HTMLTextAreaElement>(null);\n\n\n const { completion, complete, isLoading } = useCompletion({\n // id: \"novel\",\n api: \"/api/generate\",\n onResponse: (response) => {\n if (response.status === 429) {\n console.log(\"You have reached your request limit for the day.\");\n\n return;\n }\n },\n\n onError: (e) => {\n console.log(\"ERROR\", e.message);\n },\n });\n\n const hasCompletion = completion.length > 0;\n const handleClick = useCallback(() => {\n const editorSelection = editor.state.selection.content().content.toJSON()\n\n if (completion) {\n return complete(completion, {\n body: { option: \"zap\", command: inputValue, language: code ?? \"en\", selection: formatCompletion(completion) },\n }).then(() => setInputValue(\"\"));\n }\n const text = getPrevText(editor, { chars: 5000 });\n\n complete(text, {\n body: {\n option: \"zap\",\n command:\n (editor.storage.aiCommand.title\n ? \"Context Title:\" + editor.storage.aiCommand.title + \";\\n\"\n : \"\") +\n (editor.storage.aiCommand.language\n ? \"Context language:\" + editor.storage.aiCommand.language + \";\\n\"\n : \"\") +\n inputValue,\n language: editor.storage.aiCommand.language ?? code ?? \"en\",\n selection: editorSelection\n },\n }).then(() => setInputValue(\"\"));\n }, [code, inputValue, completion]);\n\n useEffect(() => {\n if (userPrompt) setInputValue(userPrompt);\n }, [userPrompt]);\n useEffect(() => {\n if (!isLoading && hasCompletion) {\n editor.storage.aiCommand.completion = checkJsonCompletion(completion) ? formatCompletion(completion) : completion;\n }\n }, [completion, isLoading]);\n\n // Anytime the groups change, i.e. the user types to narrow it down, we want to\n // reset the current selection to the first menu item\n useEffect(() => {\n setSelectedGroupIndex(0);\n setSelectedCommandIndex(0);\n }, [items]);\n\n const selectItem = useCallback(\n (groupIndex: number, commandIndex: number) => {\n const command = items[groupIndex].commands[commandIndex];\n\n setInputValue(command.action(editor));\n // props.command(command);\n },\n [items, editor]\n );\n\n useImperativeHandle(ref, () => ({\n onKeyDown: ({ event }: { event: React.KeyboardEvent }) => {\n if (event.key === \"ArrowDown\") {\n if (!items.length) {\n return false;\n }\n\n const commands = items[selectedGroupIndex].commands;\n\n let newCommandIndex = selectedCommandIndex + 1;\n let newGroupIndex = selectedGroupIndex;\n\n if (commands.length - 1 < newCommandIndex) {\n newCommandIndex = 0;\n newGroupIndex = selectedGroupIndex + 1;\n }\n\n if (items.length - 1 < newGroupIndex) {\n newGroupIndex = 0;\n }\n\n setSelectedCommandIndex(newCommandIndex);\n setSelectedGroupIndex(newGroupIndex);\n\n return true;\n }\n\n if (event.key === \"ArrowUp\") {\n if (!items.length) {\n return false;\n }\n\n let newCommandIndex = selectedCommandIndex - 1;\n let newGroupIndex = selectedGroupIndex;\n\n if (newCommandIndex < 0) {\n newGroupIndex = selectedGroupIndex - 1;\n newCommandIndex = items[newGroupIndex]?.commands.length - 1 || 0;\n }\n\n if (newGroupIndex < 0) {\n newGroupIndex = items.length - 1;\n newCommandIndex = items[newGroupIndex].commands.length - 1;\n }\n\n setSelectedCommandIndex(newCommandIndex);\n setSelectedGroupIndex(newGroupIndex);\n\n return true;\n }\n\n if (event.key === \"Enter\") {\n if (\n !items.length ||\n selectedGroupIndex === -1 ||\n selectedCommandIndex === -1\n ) {\n return false;\n }\n\n selectItem(selectedGroupIndex, selectedCommandIndex);\n\n return true;\n }\n\n return false;\n },\n }));\n\n useEffect(() => {\n if (activeItem.current && scrollContainer.current) {\n const offsetTop = activeItem.current.offsetTop;\n const offsetHeight = activeItem.current.offsetHeight;\n\n scrollContainer.current.scrollTop = offsetTop - offsetHeight;\n }\n }, [selectedCommandIndex, selectedGroupIndex]);\n\n const createCommandClickHandler = useCallback(\n (groupIndex: number, commandIndex: number) => {\n return () => {\n selectItem(groupIndex, commandIndex);\n };\n },\n [selectItem]\n );\n\n return (\n <>\n <Surface\n className={`p-2 min-w-[20rem] flex flex-col gap-2 w-full ${hasCompletion ? \"-mt-72\" : \"\"\n }`}\n style={{\n width: boundigClient?.width,\n }}\n >\n <>\n {hasCompletion && !isLoading && (\n <div className=\"flex w-full\">\n <ScrollArea\n className=\"h-72 prose p-2 px-4 prose-sm w-full max-w-none\"\n dir={i18next.dir()}\n >\n <Markdown>{checkJsonCompletion(completion) ? extractMarkdown({ type: 'doc', content: formatCompletion(completion) as any }) : completion}</Markdown>\n\n </ScrollArea>\n </div>\n )}\n\n {isLoading && (\n <div\n className=\"flex h-12 w-full items-center px-4 text-sm font-medium text-muted-foreground text-blue-500\"\n style={{\n width: boundigClient?.width,\n }}\n >\n <Sparkles className=\"mr-2 h-4 w-4 shrink-0 \" />\n AI is thinking\n <div className=\"ml-2 mt-1\">\n <CrazySpinner />\n </div>\n </div>\n )}\n\n {!isLoading && (\n <>\n <div className=\"flex justify-between items-center \">\n {/* <CommandInput\n value={inputValue}\n onValueChange={setInputValue}\n autoFocus\n placeholder={\n hasCompletion ? 'Tell AI what to do next' : 'Ask AI to edit or generate...'\n }\n // onFocus={() => addAIHighlight(editor)}\n onKeyDown={(e) => {\n if (e.key === 'Enter') handleClick()\n }}\n /> */}\n <Sparkles className=\"mr-2 h-4 w-4 shrink-0 text-blue-500 \" />\n <TextareaAutosize\n ref={inputRef}\n style={{ resize: \"none\" }}\n className=\"w-full p-2 text-black bg-white rounded dark:bg-black dark:text-white focus:outline-none outline-none border-0 \"\n value={inputValue}\n onChange={(e) => {\n editor.storage.aiCommand.userPrompt = e.target.value;\n\n setInputValue(e.target.value);\n }}\n placeholder={\n hasCompletion\n ? i18next.t(\"tellAiPlaceholder\") || \"Tell AI what to do next\"\n : i18next.t(\"askAiPlaceholder\") || \"Ask AI to edit or generate...\"\n }\n autoFocus\n // onFocus={() => {\n // addAIHighlight(editor)}}\n\n onKeyDown={(e) => {\n if (e.key === \"Enter\") handleClick();\n }}\n />\n <Toolbar.Button\n // size=\"icon\"\n\n // className=\"absolute right-2 top-1/2 h-6 w-6 -translate-y-1/2 rounded-full bg-zinc-100 hover:bg-zinc-150\"\n onClick={handleClick}\n >\n <ArrowUpCircle />\n </Toolbar.Button>\n </div>\n </>\n )}\n </>\n </Surface>\n {!isLoading && (\n <Surface\n ref={scrollContainer}\n className=\"text-black max-h-[min(80vh,24rem)] overflow-auto flex-wrap mb-8 p-2\"\n >\n <div className=\"grid grid-cols-1 gap-0.5\">\n {hasCompletion ? (\n <AISelectorCommands\n editor={editor}\n messages={completion}\n onSelect={(value, options) => {\n complete(value, { body: options });\n }}\n />\n ) : (\n items.map((group, groupIndex: number) => (\n <React.Fragment key={`${group.title}-wrapper`}>\n <div\n className=\"text-neutral-500 text-[0.65rem] col-[1/-1] mx-2 mt-4 font-semibold tracking-wider select-none uppercase first:mt-0.5\"\n key={`${group.title}`}\n >\n {group.title}\n </div>\n {group.commands.map(\n (command: Command, commandIndex: number) => (\n <DropdownButton\n key={`${command.label}`}\n isActive={\n selectedGroupIndex === groupIndex &&\n selectedCommandIndex === commandIndex\n }\n onClick={createCommandClickHandler(\n groupIndex,\n commandIndex\n )}\n >\n <Icon icon={command.icon} className=\"mr-1\" />\n {command.label}\n </DropdownButton>\n )\n )}\n </React.Fragment>\n ))\n )}\n </div>\n </Surface>\n )}\n </>\n );\n }\n);\n"],"names":["useCompletion","ArrowUpCircle","Sparkles","React","forwardRef","useCallback","useEffect","useImperativeHandle","useRef","useState","Markdown","TextareaAutosize","useLocale","i18next","extractMarkdown","getPrevText","AISelectorCommands","CrazySpinner","DropdownButton","Icon","ScrollArea","Surface","Toolbar","checkJsonCompletion","formatCompletion","AICommandPanel","editor","onOpenChange","userPrompt","items","restProps","ref","inputValue","setInputValue","code","view","scrollContainer","activeItem","selectedGroupIndex","setSelectedGroupIndex","selectedCommandIndex","setSelectedCommandIndex","editorNode","dom","boundigClient","getBoundingClientRect","inputRef","completion","complete","isLoading","api","onResponse","response","status","console","log","onError","e","message","hasCompletion","length","handleClick","editorSelection","state","selection","content","toJSON","body","option","command","language","then","text","chars","storage","aiCommand","title","selectItem","groupIndex","commandIndex","commands","action","onKeyDown","event","key","newCommandIndex","newGroupIndex","current","offsetTop","offsetHeight","scrollTop","createCommandClickHandler","className","style","width","div","dir","type","resize","value","onChange","target","placeholder","t","autoFocus","Button","onClick","messages","onSelect","options","map","group","Fragment","isActive","icon","label"],"mappings":"AAAA;;AAEA,SAASA,aAAa,QAAQ,WAAW;AACzC,SAASC,aAAa,EAAEC,QAAQ,QAAQ,eAAe;AACvD,OAAOC,SACLC,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,mBA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cACvBtC,yBAAyBuC;wCAE3B2B,SAASf,0BACPb,YACAC;;0DAGF,KAAC5D;gDAAK+F,MAAM7C,QAAQ6C,IAAI;gDAAEtB,WAAU;;4CACnCvB,QAAQ8C,KAAK;;uCAXT,CAAC,EAAE9C,QAAQ8C,KAAK,CAAC,CAAC;;2BAVV,CAAC,EAAEJ,MAAMnC,KAAK,CAAC,QAAQ,CAAC;;;;;AAiC7D,GACA"}
|
|
@@ -5,7 +5,9 @@ import { useCompletion } from "ai/react";
|
|
|
5
5
|
import { ArrowRight, Sparkles } from "lucide-react";
|
|
6
6
|
import { useCallback, useRef, useState } from "react";
|
|
7
7
|
import TextareaAutosize from "react-textarea-autosize";
|
|
8
|
+
import i18next from "i18next";
|
|
8
9
|
import Markdown from "react-markdown";
|
|
10
|
+
import { extractMarkdown } from "../../../lib/extract.js";
|
|
9
11
|
import { getPrevText } from "../../../lib/utils/index.js";
|
|
10
12
|
import AICompletionCommands from "../../menus/TextMenu/components/ai-completion-command.js";
|
|
11
13
|
import AISelectorCommands from "../../menus/TextMenu/components/ai-selector-commands.js";
|
|
@@ -13,8 +15,6 @@ import { Surface } from "../../ui/Surface.js";
|
|
|
13
15
|
import { Toolbar } from "../../ui/Toolbar.js";
|
|
14
16
|
import CrazySpinner from "../../ui/crazy-spinner.js";
|
|
15
17
|
import { ScrollArea } from "../../ui/scroll-area.js";
|
|
16
|
-
import i18next from "i18next";
|
|
17
|
-
import { extractMarkdown } from "../../../lib/extract.js";
|
|
18
18
|
import { checkJsonCompletion, formatCompletion } from "./utils.js";
|
|
19
19
|
export const AIEditorPanel = ({ editor, onOpenChange })=>{
|
|
20
20
|
const [inputValue, setInputValue] = useState("");
|
|
@@ -57,12 +57,13 @@ export const AIEditorPanel = ({ editor, onOpenChange })=>{
|
|
|
57
57
|
option: "zap",
|
|
58
58
|
command: inputValue,
|
|
59
59
|
language: code ?? "en",
|
|
60
|
-
selection:
|
|
60
|
+
selection: formatCompletion(completion)
|
|
61
61
|
}
|
|
62
62
|
}).then(()=>setInputValue(""));
|
|
63
63
|
}, [
|
|
64
64
|
code,
|
|
65
|
-
inputValue
|
|
65
|
+
inputValue,
|
|
66
|
+
completion
|
|
66
67
|
]);
|
|
67
68
|
return /*#__PURE__*/ _jsx(Surface, {
|
|
68
69
|
className: `p-2 min-w-[20rem] `,
|
|
@@ -133,7 +134,7 @@ export const AIEditorPanel = ({ editor, onOpenChange })=>{
|
|
|
133
134
|
hasCompletion ? /*#__PURE__*/ _jsx(AICompletionCommands, {
|
|
134
135
|
editor: editor,
|
|
135
136
|
onDiscard: ()=>{
|
|
136
|
-
editor
|
|
137
|
+
editor?.chain()?.unsetHighlight()?.focus()?.run();
|
|
137
138
|
onOpenChange(false);
|
|
138
139
|
},
|
|
139
140
|
completion: completion
|