payload-richtext-tiptap 0.0.49 → 0.0.51

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.
@@ -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;AAEvC,OAAO,EAEL,KAAK,EACN,MAAM,uDAAuD,CAAC;AAW/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,qFA6U1B,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;AAEvC,OAAO,EAEL,KAAK,EACN,MAAM,uDAAuD,CAAC;AAW/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,qFAiV1B,CAAC"}
@@ -69,7 +69,7 @@ export const AICommandPanel = /*#__PURE__*/ forwardRef(({ editor, onOpenChange,
69
69
  complete(text, {
70
70
  body: {
71
71
  option: "zap",
72
- command: (editor.storage.aiCommand.title ? editor.storage.aiCommand.title + "," : "") + inputValue,
72
+ command: (editor.storage.aiCommand.title ? "Context Title:" + editor.storage.aiCommand.title + ";\n" : "") + (editor.storage.aiCommand.language ? "Context language:" + editor.storage.aiCommand.language + ";\n" : "") + inputValue,
73
73
  language: editor.storage.aiCommand.language ?? code ?? "en"
74
74
  }
75
75
  }).then(()=>setInputValue(""));
@@ -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 { isRTL } from \"../../../lib/utils/isRtl.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\";\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 // const { status, messages, input, submitMessage, handleInputChange } = useAssistant({\n // // id: \"novel\",\n // api: \"/api/assistant\",\n // onResponse: (response) => {\n // if (response.status === 429) {\n // toast.error(\"You have reached your request limit for the day.\");\n // return;\n // }\n // },\n // onError: (e) => {\n // toast.error(e.message);\n // },\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 if (completion) {\n return complete(completion, {\n body: { option: \"zap\", command: inputValue, language: code ?? \"en\" },\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 ? editor.storage.aiCommand.title + \",\"\n : \"\") + inputValue,\n language: editor.storage.aiCommand.language ?? code ?? \"en\",\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`}\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={isRTL(completion) ? \"rtl\" : \"ltr\"}\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 ? \"Tell AI what to do next\"\n : \"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 console.log(\"VALUE\", 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","isRTL","CrazySpinner","DropdownButton","Icon","ScrollArea","Surface","Toolbar","AISelectorCommands","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","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","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,SAASC,KAAK,QAAQ,8BAA8B;AACpD,OAAOC,kBAAkB,4BAA4B;AACrD,SAASC,cAAc,QAAQ,gCAAgC;AAC/D,SAASC,IAAI,QAAQ,mBAAmB;AACxC,SAASC,UAAU,QAAQ,0BAA0B;AACrD,SAASC,OAAO,QAAQ,sBAAsB;AAC9C,SAASC,OAAO,QAAQ,sBAAsB;AAC9C,OAAOC,wBAAwB,0DAA0D;AASzF,OAAO,MAAMC,+BAAiBlB,WAC5B,CACE,EACEmB,MAAM,EACNC,YAAY,EACZC,UAAU,EACVC,KAAK,EACL,GAAGC,WACiB,EACtBC;IAEA,MAAM,CAACC,YAAYC,cAAc,GAAGrB,SAASgB,cAAc;IAC3D,MAAM,EAAEM,IAAI,EAAE,GAAGnB;IACjB,MAAM,EAAEoB,IAAI,EAAE,GAAGT;IACjB,MAAMU,kBAAkBzB,OAAuB;IAC/C,MAAM0B,aAAa1B,OAA0B;IAC7C,MAAM,CAAC2B,oBAAoBC,sBAAsB,GAAG3B,SAAS;IAC7D,MAAM,CAAC4B,sBAAsBC,wBAAwB,GAAG7B,SAAS;IAEjE,MAAM8B,aAAaP,KAAKQ,GAAG;IAC3B,MA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jB,MAACvD;wCAECuF,UACEpE,uBAAuBmC,cACvBjC,yBAAyBkC;wCAE3ByB,SAASb,0BACPb,YACAC;;0DAGF,KAACtD;gDAAKuF,MAAM3C,QAAQ2C,IAAI;gDAAEpB,WAAU;;4CACnCvB,QAAQ4C,KAAK;;uCAXT,CAAC,EAAE5C,QAAQ4C,KAAK,CAAC,CAAC;;2BAVV,CAAC,EAAEJ,MAAMjC,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 {\n Command,\n Group,\n} from \"src/fields/TiptapEditor/extensions/AICommand/types.js\";\nimport { getPrevText } from \"../../../lib/utils/index.js\";\nimport { isRTL } from \"../../../lib/utils/isRtl.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\";\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 // const { status, messages, input, submitMessage, handleInputChange } = useAssistant({\n // // id: \"novel\",\n // api: \"/api/assistant\",\n // onResponse: (response) => {\n // if (response.status === 429) {\n // toast.error(\"You have reached your request limit for the day.\");\n // return;\n // }\n // },\n // onError: (e) => {\n // toast.error(e.message);\n // },\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 if (completion) {\n return complete(completion, {\n body: { option: \"zap\", command: inputValue, language: code ?? \"en\" },\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 },\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`}\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={isRTL(completion) ? \"rtl\" : \"ltr\"}\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 ? \"Tell AI what to do next\"\n : \"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 console.log(\"VALUE\", 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","isRTL","CrazySpinner","DropdownButton","Icon","ScrollArea","Surface","Toolbar","AISelectorCommands","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","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","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,SAASC,KAAK,QAAQ,8BAA8B;AACpD,OAAOC,kBAAkB,4BAA4B;AACrD,SAASC,cAAc,QAAQ,gCAAgC;AAC/D,SAASC,IAAI,QAAQ,mBAAmB;AACxC,SAASC,UAAU,QAAQ,0BAA0B;AACrD,SAASC,OAAO,QAAQ,sBAAsB;AAC9C,SAASC,OAAO,QAAQ,sBAAsB;AAC9C,OAAOC,wBAAwB,0DAA0D;AASzF,OAAO,MAAMC,+BAAiBlB,WAC5B,CACE,EACEmB,MAAM,EACNC,YAAY,EACZC,UAAU,EACVC,KAAK,EACL,GAAGC,WACiB,EACtBC;IAEA,MAAM,CAACC,YAAYC,cAAc,GAAGrB,SAASgB,cAAc;IAC3D,MAAM,EAAEM,IAAI,EAAE,GAAGnB;IACjB,MAAM,EAAEoB,IAAI,EAAE,GAAGT;IACjB,MAAMU,kBAAkBzB,OAAuB;IAC/C,MAAM0B,aAAa1B,OAA0B;IAC7C,MAAM,CAAC2B,oBAAoBC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B,MAAMwC;4BAAQ;wBAClC;yBAGFzE,MAAM0E,GAAG,CAAC,CAACC,OAAO/B,2BAChB,MAACnE,MAAMmG,QAAQ;;8CACb,KAACf;oCACCH,WAAU;8CAGTiB,MAAMjC,KAAK;mCAFP,CAAC,EAAEiC,MAAMjC,KAAK,CAAC,CAAC;gCAItBiC,MAAM7B,QAAQ,CAAC4B,GAAG,CACjB,CAACvC,SAAkBU,6BACjB,MAACvD;wCAECuF,UACEpE,uBAAuBmC,cACvBjC,yBAAyBkC;wCAE3ByB,SAASb,0BACPb,YACAC;;0DAGF,KAACtD;gDAAKuF,MAAM3C,QAAQ2C,IAAI;gDAAEpB,WAAU;;4CACnCvB,QAAQ4C,KAAK;;uCAXT,CAAC,EAAE5C,QAAQ4C,KAAK,CAAC,CAAC;;2BAVV,CAAC,EAAEJ,MAAMjC,KAAK,CAAC,QAAQ,CAAC;;;;;AAiC7D,GACA"}
@@ -1 +1 @@
1
- {"version":3,"file":"useBlockEditor.d.ts","sourceRoot":"","sources":["../../../../../src/fields/TiptapEditor/hooks/useBlockEditor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAa,MAAM,eAAe,CAAC;AASlD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;KACvB;CACF;AAED,eAAO,MAAM,cAAc,sEAKxB;IACD,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,CAAC;IACrC,YAAY,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;IAClC,kBAAkB,EAAE,eAAe,CAAC;IACpC,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACzC;;;;;CA2FA,CAAC"}
1
+ {"version":3,"file":"useBlockEditor.d.ts","sourceRoot":"","sources":["../../../../../src/fields/TiptapEditor/hooks/useBlockEditor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAa,MAAM,eAAe,CAAC;AASlD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;KACvB;CACF;AAED,eAAO,MAAM,cAAc,sEAKxB;IACD,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,CAAC;IACrC,YAAY,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;IAClC,kBAAkB,EAAE,eAAe,CAAC;IACpC,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACzC;;;;;CA+FA,CAAC"}
@@ -47,16 +47,19 @@ export const useBlockEditor = ({ content, handleChange, openAssetHQHandler, addi
47
47
  }
48
48
  }, []);
49
49
  useEffect(()=>{
50
- editor.storage.aiCommand.title = additionalContext?.title ?? "";
51
- editor.storage.aiCommand.language = additionalContext?.language ?? "";
50
+ if (editor?.storage?.aiCommand) {
51
+ editor.storage.aiCommand.title = additionalContext?.title ?? "";
52
+ editor.storage.aiCommand.language = additionalContext?.language ?? "";
53
+ }
52
54
  }, [
55
+ editor,
53
56
  additionalContext
54
57
  ]);
55
58
  const users = useMemo(()=>{
56
- if (!editor?.storage.collaborationCursor?.users) {
59
+ if (!editor?.storage?.collaborationCursor?.users) {
57
60
  return [];
58
61
  }
59
- return editor.storage.collaborationCursor?.users.map((user)=>{
62
+ return editor?.storage?.collaborationCursor?.users.map((user)=>{
60
63
  const names = user.name?.split(" ");
61
64
  const firstName = names?.[0];
62
65
  const lastName = names?.[names.length - 1];
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/fields/TiptapEditor/hooks/useBlockEditor.ts"],"sourcesContent":["\"use client\";\nimport { useEffect, useMemo } from \"react\";\n\nimport { Editor, useEditor } from \"@tiptap/react\";\n\nimport ExtensionKit from \"../extensions/extension-kit.js\";\nimport { EditorUser } from \"../features/BlockEditor/types.js\";\nimport { useSidebar } from \"./useSidebar.js\";\n\nimport { useTranslation } from \"@payloadcms/ui/providers/Translation\";\nimport TurndownService from \"turndown\";\nimport { gfm } from \"turndown-plugin-gfm\";\nimport { openAssetHQType } from \"../types.js\";\ndeclare global {\n interface Window {\n editor: Editor | null;\n }\n}\n\nexport const useBlockEditor = ({\n content,\n handleChange,\n openAssetHQHandler,\n additionalContext,\n}: {\n content: { html: string; json: any };\n handleChange: (data: any) => void;\n openAssetHQHandler: openAssetHQType;\n additionalContext?: Record<string, any>;\n}) => {\n const leftSidebar = useSidebar();\n\n const turndownService = new TurndownService({});\n // Use the gfm plugin\n turndownService.use(gfm);\n turndownService.addRule(\"twitter\", {\n filter: \"div\",\n replacement: function (content) {\n return \"\\n\\n\" + content + \"\\n\\n\";\n },\n });\n const { t, i18n } = useTranslation();\n\n const editor = useEditor(\n {\n content: content?.json ?? content ?? {},\n autofocus: true,\n onCreate: ({ editor }) => {\n // provider?.on('synced', () => {\n // if (editor.isEmpty) {\n // editor.commands.setContent(initialContent)\n // }\n // })\n },\n extensions: [\n ...ExtensionKit({\n openAssetHQHandler,\n // provider,\n }),\n // Collaboration.configure({\n // document: ydoc,\n // }),\n // CollaborationCursor.configure({\n // provider,\n // user: {\n // name: randomElement(userNames),\n // color: randomElement(userColors),\n // },\n // }),\n ],\n editorProps: {\n attributes: {\n languages: i18n.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 useEffect(() => {\n editor.storage.aiCommand.title = additionalContext?.title ?? \"\";\n editor.storage.aiCommand.language = additionalContext?.language ?? \"\";\n }, [additionalContext]);\n\n const users = useMemo(() => {\n if (!editor?.storage.collaborationCursor?.users) {\n return [];\n }\n\n return editor.storage.collaborationCursor?.users.map((user: EditorUser) => {\n const names = user.name?.split(\" \");\n const firstName = names?.[0];\n const lastName = names?.[names.length - 1];\n const initials = `${firstName?.[0] || \"?\"}${lastName?.[0] || \"?\"}`;\n\n return { ...user, initials: initials.length ? initials : \"?\" };\n });\n }, [editor?.storage.collaborationCursor?.users]);\n\n const characterCount = editor?.storage.characterCount || {\n characters: () => 0,\n words: () => 0,\n };\n\n // useEffect(() => {\n // provider?.on('status', (event: { status: WebSocketStatus }) => {\n // setCollabState(event.status)\n // })\n // }, [provider])\n\n if (typeof window != \"undefined\") window.editor = editor;\n\n return { editor, users, characterCount, leftSidebar };\n};\n"],"names":["useEffect","useMemo","useEditor","ExtensionKit","useSidebar","useTranslation","TurndownService","gfm","useBlockEditor","content","handleChange","openAssetHQHandler","additionalContext","leftSidebar","turndownService","use","addRule","filter","replacement","t","i18n","editor","json","autofocus","onCreate","extensions","editorProps","attributes","languages","language","autocomplete","autocorrect","autocapitalize","class","onUpdate","getJSON","storage","aiCommand","title","users","collaborationCursor","map","user","names","name","split","firstName","lastName","length","initials","characterCount","characters","words","window"],"mappings":"AAAA;AACA,SAASA,SAAS,EAAEC,OAAO,QAAQ,QAAQ;AAE3C,SAAiBC,SAAS,QAAQ,gBAAgB;AAElD,OAAOC,kBAAkB,iCAAiC;AAE1D,SAASC,UAAU,QAAQ,kBAAkB;AAE7C,SAASC,cAAc,QAAQ,uCAAuC;AACtE,OAAOC,qBAAqB,WAAW;AACvC,SAASC,GAAG,QAAQ,sBAAsB;AAQ1C,OAAO,MAAMC,iBAAiB,CAAC,EAC7BC,OAAO,EACPC,YAAY,EACZC,kBAAkB,EAClBC,iBAAiB,EAMlB;IACC,MAAMC,cAAcT;IAEpB,MAAMU,kBAAkB,IAAIR,gBAAgB,CAAC;IAC7C,qBAAqB;IACrBQ,gBAAgBC,GAAG,CAACR;IACpBO,gBAAgBE,OAAO,CAAC,WAAW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SAASD,MAAM,GAAGC,WAAW;YAAI;QAC/D;IACF,GAAG;QAAC5B,QAAQe,QAAQI,qBAAqBD;KAAM;IAE/C,MAAMW,iBAAiB7B,QAAQe,QAAQc,kBAAkB;QACvDC,YAAY,IAAM;QAClBC,OAAO,IAAM;IACf;IAEA,oBAAoB;IACpB,qEAAqE;IACrE,mCAAmC;IACnC,OAAO;IACP,iBAAiB;IAEjB,IAAI,OAAOC,UAAU,aAAaA,OAAOhC,MAAM,GAAGA;IAElD,OAAO;QAAEA;QAAQkB;QAAOW;QAAgBrC;IAAY;AACtD,EAAE"}
1
+ {"version":3,"sources":["../../../../../src/fields/TiptapEditor/hooks/useBlockEditor.ts"],"sourcesContent":["\"use client\";\nimport { useEffect, useMemo } from \"react\";\n\nimport { Editor, useEditor } from \"@tiptap/react\";\n\nimport ExtensionKit from \"../extensions/extension-kit.js\";\nimport { EditorUser } from \"../features/BlockEditor/types.js\";\nimport { useSidebar } from \"./useSidebar.js\";\n\nimport { useTranslation } from \"@payloadcms/ui/providers/Translation\";\nimport TurndownService from \"turndown\";\nimport { gfm } from \"turndown-plugin-gfm\";\nimport { openAssetHQType } from \"../types.js\";\ndeclare global {\n interface Window {\n editor: Editor | null;\n }\n}\n\nexport const useBlockEditor = ({\n content,\n handleChange,\n openAssetHQHandler,\n additionalContext,\n}: {\n content: { html: string; json: any };\n handleChange: (data: any) => void;\n openAssetHQHandler: openAssetHQType;\n additionalContext?: Record<string, any>;\n}) => {\n const leftSidebar = useSidebar();\n\n const turndownService = new TurndownService({});\n // Use the gfm plugin\n turndownService.use(gfm);\n turndownService.addRule(\"twitter\", {\n filter: \"div\",\n replacement: function (content) {\n return \"\\n\\n\" + content + \"\\n\\n\";\n },\n });\n const { t, i18n } = useTranslation();\n\n const editor = useEditor(\n {\n content: content?.json ?? content ?? {},\n autofocus: true,\n onCreate: ({ editor }) => {\n // provider?.on('synced', () => {\n // if (editor.isEmpty) {\n // editor.commands.setContent(initialContent)\n // }\n // })\n },\n extensions: [\n ...ExtensionKit({\n openAssetHQHandler,\n // provider,\n }),\n // Collaboration.configure({\n // document: ydoc,\n // }),\n // CollaborationCursor.configure({\n // provider,\n // user: {\n // name: randomElement(userNames),\n // color: randomElement(userColors),\n // },\n // }),\n ],\n editorProps: {\n attributes: {\n languages: i18n.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 useEffect(() => {\n if (editor?.storage?.aiCommand) {\n editor.storage.aiCommand.title = additionalContext?.title ?? \"\";\n editor.storage.aiCommand.language = additionalContext?.language ?? \"\";\n }\n }, [editor, additionalContext]);\n\n const users = useMemo(() => {\n if (!editor?.storage?.collaborationCursor?.users) {\n return [];\n }\n\n return editor?.storage?.collaborationCursor?.users.map(\n (user: EditorUser) => {\n const names = user.name?.split(\" \");\n const firstName = names?.[0];\n const lastName = names?.[names.length - 1];\n const initials = `${firstName?.[0] || \"?\"}${lastName?.[0] || \"?\"}`;\n\n return { ...user, initials: initials.length ? initials : \"?\" };\n }\n );\n }, [editor?.storage.collaborationCursor?.users]);\n\n const characterCount = editor?.storage.characterCount || {\n characters: () => 0,\n words: () => 0,\n };\n\n // useEffect(() => {\n // provider?.on('status', (event: { status: WebSocketStatus }) => {\n // setCollabState(event.status)\n // })\n // }, [provider])\n\n if (typeof window != \"undefined\") window.editor = editor;\n\n return { editor, users, characterCount, leftSidebar };\n};\n"],"names":["useEffect","useMemo","useEditor","ExtensionKit","useSidebar","useTranslation","TurndownService","gfm","useBlockEditor","content","handleChange","openAssetHQHandler","additionalContext","leftSidebar","turndownService","use","addRule","filter","replacement","t","i18n","editor","json","autofocus","onCreate","extensions","editorProps","attributes","languages","language","autocomplete","autocorrect","autocapitalize","class","onUpdate","getJSON","storage","aiCommand","title","users","collaborationCursor","map","user","names","name","split","firstName","lastName","length","initials","characterCount","characters","words","window"],"mappings":"AAAA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