@ai-stack/payloadcms 3.2.20-beta → 3.2.21-beta
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/ai/models/anthropic/index.js +9 -15
- package/dist/ai/models/anthropic/index.js.map +1 -1
- package/dist/ai/models/generateObject.d.ts +11 -0
- package/dist/ai/models/generateObject.js +22 -0
- package/dist/ai/models/generateObject.js.map +1 -0
- package/dist/ai/models/openai/index.js +19 -17
- package/dist/ai/models/openai/index.js.map +1 -1
- package/dist/collections/Instructions.js +2 -0
- package/dist/collections/Instructions.js.map +1 -1
- package/dist/endpoints/index.js +55 -6
- package/dist/endpoints/index.js.map +1 -1
- package/dist/fields/ComposeField/ComposeField.js +5 -3
- package/dist/fields/ComposeField/ComposeField.js.map +1 -1
- package/dist/fields/ComposeField/ComposeField.jsx +32 -0
- package/dist/fields/LexicalEditor/ComposeFeatureComponent.js +1 -1
- package/dist/fields/LexicalEditor/ComposeFeatureComponent.js.map +1 -1
- package/dist/fields/LexicalEditor/ComposeFeatureComponent.jsx +24 -0
- package/dist/fields/LexicalEditor/feature.client.jsx +21 -0
- package/dist/fields/PromptEditorField/PromptEditorField.jsx +42 -0
- package/dist/fields/SelectField/SelectField.jsx +38 -0
- package/dist/providers/FieldProvider/FieldProvider.d.ts +7 -4
- package/dist/providers/FieldProvider/FieldProvider.js +7 -6
- package/dist/providers/FieldProvider/FieldProvider.js.map +1 -1
- package/dist/providers/FieldProvider/FieldProvider.jsx +27 -0
- package/dist/providers/FieldProvider/useFieldProps.d.ts +1 -1
- package/dist/providers/FieldProvider/useFieldProps.js +2 -2
- package/dist/providers/FieldProvider/useFieldProps.js.map +1 -1
- package/dist/providers/InstructionsProvider/InstructionsProvider.jsx +45 -0
- package/dist/types.d.ts +4 -4
- package/dist/types.js.map +1 -1
- package/dist/ui/Compose/Compose.js +12 -81
- package/dist/ui/Compose/Compose.js.map +1 -1
- package/dist/ui/Compose/Compose.jsx +141 -0
- package/dist/ui/Compose/UndoRedoActions.jsx +34 -0
- package/dist/ui/Compose/compose.module.css +99 -12
- package/dist/ui/Compose/hooks/menu/Item.jsx +15 -0
- package/dist/ui/Compose/hooks/menu/TranslateMenu.jsx +58 -0
- package/dist/ui/Compose/hooks/menu/items.jsx +10 -0
- package/dist/ui/Compose/hooks/menu/useMenu.js +1 -1
- package/dist/ui/Compose/hooks/menu/useMenu.js.map +1 -1
- package/dist/ui/Compose/hooks/menu/useMenu.jsx +89 -0
- package/dist/ui/Compose/hooks/useActiveFieldTracking.d.ts +5 -0
- package/dist/ui/Compose/hooks/useActiveFieldTracking.js +148 -0
- package/dist/ui/Compose/hooks/useActiveFieldTracking.js.map +1 -0
- package/dist/ui/Compose/hooks/useGenerate.js +46 -79
- package/dist/ui/Compose/hooks/useGenerate.js.map +1 -1
- package/dist/ui/Icons/Icons.jsx +78 -0
- package/dist/ui/Icons/LottieAnimation.jsx +64 -0
- package/dist/utilities/extractPromptAttachments.d.ts +2 -2
- package/dist/utilities/extractPromptAttachments.js.map +1 -1
- package/dist/utilities/fieldToJsonSchema.d.ts +37 -0
- package/dist/utilities/fieldToJsonSchema.js +274 -0
- package/dist/utilities/fieldToJsonSchema.js.map +1 -0
- package/dist/utilities/getFieldBySchemaPath.d.ts +12 -1
- package/dist/utilities/getFieldBySchemaPath.js +63 -29
- package/dist/utilities/getFieldBySchemaPath.js.map +1 -1
- package/package.json +2 -1
- package/dist/ai/models/anthropic/generateRichText.d.ts +0 -1
- package/dist/ai/models/anthropic/generateRichText.js +0 -36
- package/dist/ai/models/anthropic/generateRichText.js.map +0 -1
- package/dist/ai/models/openai/generateRichText.d.ts +0 -1
- package/dist/ai/models/openai/generateRichText.js +0 -37
- package/dist/ai/models/openai/generateRichText.js.map +0 -1
|
@@ -2,92 +2,24 @@
|
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { useEditorConfigContext } from '@payloadcms/richtext-lexical/client';
|
|
4
4
|
import { Popup, useDocumentDrawer, useField } from '@payloadcms/ui';
|
|
5
|
-
import React, { useCallback,
|
|
5
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
6
6
|
import { PLUGIN_INSTRUCTIONS_TABLE } from '../../defaults.js';
|
|
7
7
|
import { setSafeLexicalState } from '../../utilities/setSafeLexicalState.js';
|
|
8
8
|
import { PluginIcon } from '../Icons/Icons.js';
|
|
9
9
|
import styles from './compose.module.css';
|
|
10
10
|
import { useMenu } from './hooks/menu/useMenu.js';
|
|
11
|
+
import { useActiveFieldTracking } from './hooks/useActiveFieldTracking.js';
|
|
11
12
|
import { useGenerate } from './hooks/useGenerate.js';
|
|
12
13
|
import { UndoRedoActions } from './UndoRedoActions.js';
|
|
13
|
-
function findParentWithClass(element, className) {
|
|
14
|
-
// Base case: if the element is null, or we've reached the top of the DOM
|
|
15
|
-
if (!element || element === document.body) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
// Check if the current element has the class we're looking for
|
|
19
|
-
if (element.classList.contains(className)) {
|
|
20
|
-
return element;
|
|
21
|
-
}
|
|
22
|
-
// Recursively call the function on the parent element
|
|
23
|
-
return findParentWithClass(element.parentElement, className);
|
|
24
|
-
}
|
|
25
14
|
export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
26
15
|
const [DocumentDrawer, _, { closeDrawer, openDrawer }] = useDocumentDrawer({
|
|
27
16
|
id: instructionId,
|
|
28
17
|
collectionSlug: PLUGIN_INSTRUCTIONS_TABLE
|
|
29
18
|
});
|
|
30
|
-
const fieldType = descriptionProps?.field?.type;
|
|
31
19
|
const pathFromContext = descriptionProps?.path;
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const [input, setInput] = useState(null);
|
|
36
|
-
const actionsRef = useRef(null);
|
|
37
|
-
// Set input element for current field
|
|
38
|
-
useEffect(()=>{
|
|
39
|
-
if (!actionsRef.current) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (!pathFromContext) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const fieldId = `field-${pathFromContext.replace(/\./g, '__')}`;
|
|
46
|
-
const inputElement = document.getElementById(fieldId);
|
|
47
|
-
if (!inputElement && fieldType === 'richText') {
|
|
48
|
-
setInput(editorContainerRef.current);
|
|
49
|
-
} else {
|
|
50
|
-
actionsRef.current?.setAttribute('for', fieldId);
|
|
51
|
-
setInput(inputElement);
|
|
52
|
-
}
|
|
53
|
-
}, [
|
|
54
|
-
pathFromContext,
|
|
55
|
-
schemaPath,
|
|
56
|
-
actionsRef,
|
|
57
|
-
editorContainerRef,
|
|
58
|
-
fieldType
|
|
59
|
-
]);
|
|
60
|
-
// Show or hide actions menu on field
|
|
61
|
-
useEffect(()=>{
|
|
62
|
-
if (!input || !actionsRef.current) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
actionsRef.current?.classList.add(styles.actions_hidden);
|
|
66
|
-
// Create the handler function
|
|
67
|
-
const clickHandler = (event)=>{
|
|
68
|
-
document.querySelectorAll('.ai-plugin-active')?.forEach((element)=>{
|
|
69
|
-
const actionElement = element.querySelector(`.${styles.actions}`);
|
|
70
|
-
if (actionElement) {
|
|
71
|
-
actionElement.classList.add(styles.actions_hidden);
|
|
72
|
-
element.classList.remove('ai-plugin-active');
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
actionsRef.current?.classList.remove(styles.actions_hidden);
|
|
76
|
-
const parentWithClass = findParentWithClass(event.target, 'field-type');
|
|
77
|
-
if (parentWithClass) {
|
|
78
|
-
parentWithClass.classList.add('ai-plugin-active');
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
// Add the event listener
|
|
82
|
-
input?.addEventListener('click', clickHandler);
|
|
83
|
-
// Clean up the event listener when the component unmounts or input changes
|
|
84
|
-
return ()=>{
|
|
85
|
-
input?.removeEventListener('click', clickHandler);
|
|
86
|
-
};
|
|
87
|
-
}, [
|
|
88
|
-
input,
|
|
89
|
-
actionsRef
|
|
90
|
-
]);
|
|
20
|
+
const { editor: lexicalEditor } = useEditorConfigContext();
|
|
21
|
+
// Initialize global active-field tracking
|
|
22
|
+
useActiveFieldTracking();
|
|
91
23
|
const [isProcessing, setIsProcessing] = useState(false);
|
|
92
24
|
const { generate, isLoading, stop } = useGenerate({
|
|
93
25
|
instructionId
|
|
@@ -99,7 +31,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
99
31
|
generate({
|
|
100
32
|
action: 'Compose'
|
|
101
33
|
}).catch((reason)=>{
|
|
102
|
-
console.error(
|
|
34
|
+
console.error('Compose : ', reason);
|
|
103
35
|
}).finally(()=>{
|
|
104
36
|
setIsProcessing(false);
|
|
105
37
|
});
|
|
@@ -109,7 +41,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
109
41
|
generate({
|
|
110
42
|
action: 'Expand'
|
|
111
43
|
}).catch((reason)=>{
|
|
112
|
-
console.error(
|
|
44
|
+
console.error('Compose : ', reason);
|
|
113
45
|
}).finally(()=>{
|
|
114
46
|
setIsProcessing(false);
|
|
115
47
|
});
|
|
@@ -119,7 +51,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
119
51
|
generate({
|
|
120
52
|
action: 'Proofread'
|
|
121
53
|
}).catch((reason)=>{
|
|
122
|
-
console.error(
|
|
54
|
+
console.error('Compose : ', reason);
|
|
123
55
|
}).finally(()=>{
|
|
124
56
|
setIsProcessing(false);
|
|
125
57
|
});
|
|
@@ -129,7 +61,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
129
61
|
generate({
|
|
130
62
|
action: 'Rephrase'
|
|
131
63
|
}).catch((reason)=>{
|
|
132
|
-
console.error(
|
|
64
|
+
console.error('Compose : ', reason);
|
|
133
65
|
}).finally(()=>{
|
|
134
66
|
setIsProcessing(false);
|
|
135
67
|
});
|
|
@@ -140,7 +72,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
140
72
|
generate({
|
|
141
73
|
action: 'Simplify'
|
|
142
74
|
}).catch((reason)=>{
|
|
143
|
-
console.error(
|
|
75
|
+
console.error('Compose : ', reason);
|
|
144
76
|
}).finally(()=>{
|
|
145
77
|
setIsProcessing(false);
|
|
146
78
|
});
|
|
@@ -150,7 +82,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
150
82
|
generate({
|
|
151
83
|
action: 'Summarize'
|
|
152
84
|
}).catch((reason)=>{
|
|
153
|
-
console.error(
|
|
85
|
+
console.error('Compose : ', reason);
|
|
154
86
|
}).finally(()=>{
|
|
155
87
|
setIsProcessing(false);
|
|
156
88
|
});
|
|
@@ -161,7 +93,7 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
161
93
|
action: 'Translate',
|
|
162
94
|
params: data
|
|
163
95
|
}).catch((reason)=>{
|
|
164
|
-
console.error(
|
|
96
|
+
console.error('Compose : ', reason);
|
|
165
97
|
}).finally(()=>{
|
|
166
98
|
setIsProcessing(false);
|
|
167
99
|
});
|
|
@@ -204,7 +136,6 @@ export const Compose = ({ descriptionProps, instructionId, isConfigAllowed })=>{
|
|
|
204
136
|
return /*#__PURE__*/ _jsxs("label", {
|
|
205
137
|
className: `payloadai-compose__actions ${styles.actions}`,
|
|
206
138
|
onClick: (e)=>e.preventDefault(),
|
|
207
|
-
ref: actionsRef,
|
|
208
139
|
role: "presentation",
|
|
209
140
|
children: [
|
|
210
141
|
/*#__PURE__*/ _jsx(DocumentDrawer, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/ui/Compose/Compose.tsx"],"sourcesContent":["'use client'\n\nimport type { ClientField } from 'payload'\nimport type { FC } from 'react'\n\nimport { useEditorConfigContext } from '@payloadcms/richtext-lexical/client'\nimport { FieldDescription, Popup, useDocumentDrawer, useField } from '@payloadcms/ui'\nimport React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { PLUGIN_INSTRUCTIONS_TABLE } from '../../defaults.js'\nimport { setSafeLexicalState } from '../../utilities/setSafeLexicalState.js'\nimport { PluginIcon } from '../Icons/Icons.js'\nimport styles from './compose.module.css'\nimport { useMenu } from './hooks/menu/useMenu.js'\nimport { useGenerate } from './hooks/useGenerate.js'\nimport { UndoRedoActions } from './UndoRedoActions.js'\n\nfunction findParentWithClass(element: HTMLElement | null, className: string): HTMLElement | null {\n // Base case: if the element is null, or we've reached the top of the DOM\n if (!element || element === document.body) {\n return null\n }\n\n // Check if the current element has the class we're looking for\n if (element.classList.contains(className)) {\n return element\n }\n\n // Recursively call the function on the parent element\n return findParentWithClass(element.parentElement, className)\n}\n\ntype ComposeProps = {\n descriptionProps?: {\n field: ClientField\n path: string\n schemaPath: string\n }\n instructionId: string\n isConfigAllowed: boolean\n}\n\nexport const Compose: FC<ComposeProps> = ({ descriptionProps, instructionId, isConfigAllowed }) => {\n const [DocumentDrawer, _, { closeDrawer, openDrawer }] = useDocumentDrawer({\n id: instructionId,\n collectionSlug: PLUGIN_INSTRUCTIONS_TABLE,\n })\n\n const fieldType = descriptionProps?.field?.type\n const pathFromContext = descriptionProps?.path\n const schemaPath = descriptionProps?.schemaPath\n const { editor: lexicalEditor, editorContainerRef } = useEditorConfigContext()\n\n // The below snippet is used to show/hide the action menu on AI-enabled fields\n const [input, setInput] = useState<HTMLElement | null>(null)\n const actionsRef = useRef<HTMLLabelElement | null>(null)\n\n // Set input element for current field\n useEffect(() => {\n if (!actionsRef.current) {\n return\n }\n\n if (!pathFromContext) {\n return\n }\n\n const fieldId = `field-${pathFromContext.replace(/\\./g, '__')}`\n const inputElement = document.getElementById(fieldId)\n\n if (!inputElement && fieldType === 'richText') {\n setInput(editorContainerRef.current as HTMLElement | null)\n } else {\n actionsRef.current?.setAttribute('for', fieldId)\n setInput(inputElement)\n }\n }, [pathFromContext, schemaPath, actionsRef, editorContainerRef, fieldType])\n\n // Show or hide actions menu on field\n useEffect(() => {\n if (!input || !actionsRef.current) {\n return\n }\n\n actionsRef.current?.classList.add(styles.actions_hidden)\n\n // Create the handler function\n const clickHandler = (event: MouseEvent) => {\n document.querySelectorAll('.ai-plugin-active')?.forEach((element) => {\n const actionElement = (element as HTMLElement).querySelector(`.${styles.actions}`)\n if (actionElement) {\n actionElement.classList.add(styles.actions_hidden)\n element.classList.remove('ai-plugin-active')\n }\n })\n\n actionsRef.current?.classList.remove(styles.actions_hidden)\n const parentWithClass = findParentWithClass(event.target as HTMLElement, 'field-type')\n if (parentWithClass) {\n parentWithClass.classList.add('ai-plugin-active')\n }\n }\n\n // Add the event listener\n input?.addEventListener('click', clickHandler)\n\n // Clean up the event listener when the component unmounts or input changes\n return () => {\n input?.removeEventListener('click', clickHandler)\n }\n }, [input, actionsRef])\n\n const [isProcessing, setIsProcessing] = useState<boolean>(false)\n const { generate, isLoading, stop } = useGenerate({ instructionId })\n\n const { ActiveComponent, Menu } = useMenu(\n {\n onCompose: () => {\n console.log('Composing...')\n setIsProcessing(true)\n generate({\n action: 'Compose',\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n onExpand: () => {\n console.log('Expanding...')\n generate({\n action: 'Expand',\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n onProofread: () => {\n console.log('Proofreading...')\n generate({\n action: 'Proofread',\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n onRephrase: () => {\n console.log('Rephrasing...')\n generate({\n action: 'Rephrase',\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n onSettings: isConfigAllowed ? openDrawer : undefined,\n onSimplify: () => {\n console.log('Simplifying...')\n generate({\n action: 'Simplify',\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n onSummarize: () => {\n console.log('Summarizing...')\n generate({\n action: 'Summarize',\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n onTranslate: (data) => {\n console.log('Translating...')\n generate({\n action: 'Translate',\n params: data,\n }).catch((reason)=>{\n console.error(\"Compose : \",reason)\n }).finally(() => {\n setIsProcessing(false)\n })\n },\n },\n {\n isConfigAllowed,\n },\n )\n\n const { setValue } = useField<string>({\n path: pathFromContext,\n })\n\n const setIfValueIsLexicalState = useCallback((val: any) => {\n if (val && typeof val === 'object' && 'root' in val && lexicalEditor) {\n setSafeLexicalState(JSON.stringify(val), lexicalEditor)\n }\n\n // DO NOT PROVIDE lexicalEditor as a dependency, it freaks out and does not update the editor after first undo/redo\n }, [])\n\n const popupRender = useCallback(\n ({ close }: { close: () => void }) => {\n return <Menu isLoading={isProcessing || isLoading} onClose={close} />\n },\n [isProcessing, isLoading, Menu],\n )\n\n const memoizedPopup = useMemo(() => {\n return (\n <Popup\n button={<PluginIcon isLoading={isProcessing || isLoading} />}\n render={popupRender}\n verticalAlign=\"bottom\"\n />\n )\n }, [popupRender, isProcessing, isLoading])\n\n return (\n <label\n className={`payloadai-compose__actions ${styles.actions}`}\n onClick={(e) => e.preventDefault()}\n ref={actionsRef}\n role=\"presentation\"\n >\n <DocumentDrawer\n onSave={() => {\n closeDrawer()\n }}\n />\n {memoizedPopup}\n <ActiveComponent isLoading={isProcessing || isLoading} stop={stop} />\n <UndoRedoActions\n onChange={(val) => {\n setValue(val)\n setIfValueIsLexicalState(val)\n }}\n />\n </label>\n )\n}\n"],"names":["useEditorConfigContext","Popup","useDocumentDrawer","useField","React","useCallback","useEffect","useMemo","useRef","useState","PLUGIN_INSTRUCTIONS_TABLE","setSafeLexicalState","PluginIcon","styles","useMenu","useGenerate","UndoRedoActions","findParentWithClass","element","className","document","body","classList","contains","parentElement","Compose","descriptionProps","instructionId","isConfigAllowed","DocumentDrawer","_","closeDrawer","openDrawer","id","collectionSlug","fieldType","field","type","pathFromContext","path","schemaPath","editor","lexicalEditor","editorContainerRef","input","setInput","actionsRef","current","fieldId","replace","inputElement","getElementById","setAttribute","add","actions_hidden","clickHandler","event","querySelectorAll","forEach","actionElement","querySelector","actions","remove","parentWithClass","target","addEventListener","removeEventListener","isProcessing","setIsProcessing","generate","isLoading","stop","ActiveComponent","Menu","onCompose","console","log","action","catch","reason","error","finally","onExpand","onProofread","onRephrase","onSettings","undefined","onSimplify","onSummarize","onTranslate","data","params","setValue","setIfValueIsLexicalState","val","JSON","stringify","popupRender","close","onClose","memoizedPopup","button","render","verticalAlign","label","onClick","e","preventDefault","ref","role","onSave","onChange"],"mappings":"AAAA;;AAKA,SAASA,sBAAsB,QAAQ,sCAAqC;AAC5E,SAA2BC,KAAK,EAAEC,iBAAiB,EAAEC,QAAQ,QAAQ,iBAAgB;AACrF,OAAOC,SAASC,WAAW,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAO;AAEhF,SAASC,yBAAyB,QAAQ,oBAAmB;AAC7D,SAASC,mBAAmB,QAAQ,yCAAwC;AAC5E,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,OAAOC,YAAY,uBAAsB;AACzC,SAASC,OAAO,QAAQ,0BAAyB;AACjD,SAASC,WAAW,QAAQ,yBAAwB;AACpD,SAASC,eAAe,QAAQ,uBAAsB;AAEtD,SAASC,oBAAoBC,OAA2B,EAAEC,SAAiB;IACzE,yEAAyE;IACzE,IAAI,CAACD,WAAWA,YAAYE,SAASC,IAAI,EAAE;QACzC,OAAO;IACT;IAEA,+DAA+D;IAC/D,IAAIH,QAAQI,SAAS,CAACC,QAAQ,CAACJ,YAAY;QACzC,OAAOD;IACT;IAEA,sDAAsD;IACtD,OAAOD,oBAAoBC,QAAQM,aAAa,EAAEL;AACpD;AAYA,OAAO,MAAMM,UAA4B,CAAC,EAAEC,gBAAgB,EAAEC,aAAa,EAAEC,e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AAQ,KAACzF;gBAAW0D,WAAWH,gBAAgBG;;YAC/CgC,QAAQL;YACRM,eAAc;;IAGpB,GAAG;QAACN;QAAa9B;QAAcG;KAAU;IAEzC,qBACE,MAACkC;QACCrF,WAAW,CAAC,2BAA2B,EAAEN,OAAOgD,OAAO,EAAE;QACzD4C,SAAS,CAACC,IAAMA,EAAEC,cAAc;QAChCC,KAAK9D;QACL+D,MAAK;;0BAEL,KAAChF;gBACCiF,QAAQ;oBACN/E;gBACF;;YAEDqE;0BACD,KAAC5B;gBAAgBF,WAAWH,gBAAgBG;gBAAWC,MAAMA;;0BAC7D,KAACvD;gBACC+F,UAAU,CAACjB;oBACTF,SAASE;oBACTD,yBAAyBC;gBAC3B;;;;AAIR,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../../src/ui/Compose/Compose.tsx"],"sourcesContent":["'use client'\n\nimport type { ClientField } from 'payload'\nimport type { FC } from 'react'\n\nimport { useEditorConfigContext } from '@payloadcms/richtext-lexical/client'\nimport { Popup, useDocumentDrawer, useField } from '@payloadcms/ui'\nimport React, { useCallback, useMemo, useState } from 'react'\n\nimport { PLUGIN_INSTRUCTIONS_TABLE } from '../../defaults.js'\nimport { setSafeLexicalState } from '../../utilities/setSafeLexicalState.js'\nimport { PluginIcon } from '../Icons/Icons.js'\nimport styles from './compose.module.css'\nimport { useMenu } from './hooks/menu/useMenu.js'\nimport { useActiveFieldTracking } from './hooks/useActiveFieldTracking.js'\nimport { useGenerate } from './hooks/useGenerate.js'\nimport { UndoRedoActions } from './UndoRedoActions.js'\n\ntype ComposeProps = {\n descriptionProps?: {\n field: ClientField\n path: string\n schemaPath: string\n }\n instructionId: string\n isConfigAllowed: boolean\n}\n\nexport const Compose: FC<ComposeProps> = ({ descriptionProps, instructionId, isConfigAllowed }) => {\n const [DocumentDrawer, _, { closeDrawer, openDrawer }] = useDocumentDrawer({\n id: instructionId,\n collectionSlug: PLUGIN_INSTRUCTIONS_TABLE,\n })\n\n const pathFromContext = descriptionProps?.path\n const { editor: lexicalEditor } = useEditorConfigContext()\n\n // Initialize global active-field tracking\n useActiveFieldTracking()\n\n const [isProcessing, setIsProcessing] = useState<boolean>(false)\n const { generate, isLoading, stop } = useGenerate({ instructionId })\n\n const { ActiveComponent, Menu } = useMenu(\n {\n onCompose: () => {\n console.log('Composing...')\n setIsProcessing(true)\n generate({\n action: 'Compose',\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n onExpand: () => {\n console.log('Expanding...')\n generate({\n action: 'Expand',\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n onProofread: () => {\n console.log('Proofreading...')\n generate({\n action: 'Proofread',\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n onRephrase: () => {\n console.log('Rephrasing...')\n generate({\n action: 'Rephrase',\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n onSettings: isConfigAllowed ? openDrawer : undefined,\n onSimplify: () => {\n console.log('Simplifying...')\n generate({\n action: 'Simplify',\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n onSummarize: () => {\n console.log('Summarizing...')\n generate({\n action: 'Summarize',\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n onTranslate: (data) => {\n console.log('Translating...')\n generate({\n action: 'Translate',\n params: data,\n })\n .catch((reason) => {\n console.error('Compose : ', reason)\n })\n .finally(() => {\n setIsProcessing(false)\n })\n },\n },\n {\n isConfigAllowed,\n },\n )\n\n const { setValue } = useField<string>({\n path: pathFromContext,\n })\n\n const setIfValueIsLexicalState = useCallback((val: any) => {\n if (val && typeof val === 'object' && 'root' in val && lexicalEditor) {\n setSafeLexicalState(JSON.stringify(val), lexicalEditor)\n }\n\n // DO NOT PROVIDE lexicalEditor as a dependency, it freaks out and does not update the editor after first undo/redo\n }, [])\n\n const popupRender = useCallback(\n ({ close }: { close: () => void }) => {\n return <Menu isLoading={isProcessing || isLoading} onClose={close} />\n },\n [isProcessing, isLoading, Menu],\n )\n\n const memoizedPopup = useMemo(() => {\n return (\n <Popup\n button={<PluginIcon isLoading={isProcessing || isLoading} />}\n render={popupRender}\n verticalAlign=\"bottom\"\n />\n )\n }, [popupRender, isProcessing, isLoading])\n\n return (\n <label\n className={`payloadai-compose__actions ${styles.actions}`}\n onClick={(e) => e.preventDefault()}\n role=\"presentation\"\n >\n <DocumentDrawer\n onSave={() => {\n closeDrawer()\n }}\n />\n {memoizedPopup}\n <ActiveComponent isLoading={isProcessing || isLoading} stop={stop} />\n <UndoRedoActions\n onChange={(val) => {\n setValue(val)\n setIfValueIsLexicalState(val)\n }}\n />\n </label>\n )\n}\n"],"names":["useEditorConfigContext","Popup","useDocumentDrawer","useField","React","useCallback","useMemo","useState","PLUGIN_INSTRUCTIONS_TABLE","setSafeLexicalState","PluginIcon","styles","useMenu","useActiveFieldTracking","useGenerate","UndoRedoActions","Compose","descriptionProps","instructionId","isConfigAllowed","DocumentDrawer","_","closeDrawer","openDrawer","id","collectionSlug","pathFromContext","path","editor","lexicalEditor","isProcessing","setIsProcessing","generate","isLoading","stop","ActiveComponent","Menu","onCompose","console","log","action","catch","reason","error","finally","onExpand","onProofread","onRephrase","onSettings","undefined","onSimplify","onSummarize","onTranslate","data","params","setValue","setIfValueIsLexicalState","val","JSON","stringify","popupRender","close","onClose","memoizedPopup","button","render","verticalAlign","label","className","actions","onClick","e","preventDefault","role","onSave","onChange"],"mappings":"AAAA;;AAKA,SAASA,sBAAsB,QAAQ,sCAAqC;AAC5E,SAASC,KAAK,EAAEC,iBAAiB,EAAEC,QAAQ,QAAQ,iBAAgB;AACnE,OAAOC,SAASC,WAAW,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,QAAO;AAE7D,SAASC,yB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QAC/B,qBAAO,KAACzB;YAAKH,WAAWH,gBAAgBG;YAAW6B,SAASD;;IAC9D,GACA;QAAC/B;QAAcG;QAAWG;KAAK;IAGjC,MAAM2B,gBAAgBzD,QAAQ;QAC5B,qBACE,KAACL;YACC+D,sBAAQ,KAACtD;gBAAWuB,WAAWH,gBAAgBG;;YAC/CgC,QAAQL;YACRM,eAAc;;IAGpB,GAAG;QAACN;QAAa9B;QAAcG;KAAU;IAEzC,qBACE,MAACkC;QACCC,WAAW,CAAC,2BAA2B,EAAEzD,OAAO0D,OAAO,EAAE;QACzDC,SAAS,CAACC,IAAMA,EAAEC,cAAc;QAChCC,MAAK;;0BAEL,KAACrD;gBACCsD,QAAQ;oBACNpD;gBACF;;YAEDyC;0BACD,KAAC5B;gBAAgBF,WAAWH,gBAAgBG;gBAAWC,MAAMA;;0BAC7D,KAACnB;gBACC4D,UAAU,CAAClB;oBACTF,SAASE;oBACTD,yBAAyBC;gBAC3B;;;;AAIR,EAAC"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useEditorConfigContext } from '@payloadcms/richtext-lexical/client';
|
|
3
|
+
import { Popup, useDocumentDrawer, useField } from '@payloadcms/ui';
|
|
4
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
5
|
+
import { PLUGIN_INSTRUCTIONS_TABLE } from '../../defaults.js';
|
|
6
|
+
import { setSafeLexicalState } from '../../utilities/setSafeLexicalState.js';
|
|
7
|
+
import { PluginIcon } from '../Icons/Icons.js';
|
|
8
|
+
import styles from './compose.module.css';
|
|
9
|
+
import { useMenu } from './hooks/menu/useMenu.js';
|
|
10
|
+
import { useActiveFieldTracking } from './hooks/useActiveFieldTracking.js';
|
|
11
|
+
import { useGenerate } from './hooks/useGenerate.js';
|
|
12
|
+
import { UndoRedoActions } from './UndoRedoActions.js';
|
|
13
|
+
export const Compose = ({ descriptionProps, instructionId, isConfigAllowed }) => {
|
|
14
|
+
const [DocumentDrawer, _, { closeDrawer, openDrawer }] = useDocumentDrawer({
|
|
15
|
+
id: instructionId,
|
|
16
|
+
collectionSlug: PLUGIN_INSTRUCTIONS_TABLE,
|
|
17
|
+
});
|
|
18
|
+
const pathFromContext = descriptionProps?.path;
|
|
19
|
+
const { editor: lexicalEditor } = useEditorConfigContext();
|
|
20
|
+
// Initialize global active-field tracking
|
|
21
|
+
useActiveFieldTracking();
|
|
22
|
+
const [isProcessing, setIsProcessing] = useState(false);
|
|
23
|
+
const { generate, isLoading, stop } = useGenerate({ instructionId });
|
|
24
|
+
const { ActiveComponent, Menu } = useMenu({
|
|
25
|
+
onCompose: () => {
|
|
26
|
+
console.log('Composing...');
|
|
27
|
+
setIsProcessing(true);
|
|
28
|
+
generate({
|
|
29
|
+
action: 'Compose',
|
|
30
|
+
})
|
|
31
|
+
.catch((reason) => {
|
|
32
|
+
console.error('Compose : ', reason);
|
|
33
|
+
})
|
|
34
|
+
.finally(() => {
|
|
35
|
+
setIsProcessing(false);
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
onExpand: () => {
|
|
39
|
+
console.log('Expanding...');
|
|
40
|
+
generate({
|
|
41
|
+
action: 'Expand',
|
|
42
|
+
})
|
|
43
|
+
.catch((reason) => {
|
|
44
|
+
console.error('Compose : ', reason);
|
|
45
|
+
})
|
|
46
|
+
.finally(() => {
|
|
47
|
+
setIsProcessing(false);
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
onProofread: () => {
|
|
51
|
+
console.log('Proofreading...');
|
|
52
|
+
generate({
|
|
53
|
+
action: 'Proofread',
|
|
54
|
+
})
|
|
55
|
+
.catch((reason) => {
|
|
56
|
+
console.error('Compose : ', reason);
|
|
57
|
+
})
|
|
58
|
+
.finally(() => {
|
|
59
|
+
setIsProcessing(false);
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
onRephrase: () => {
|
|
63
|
+
console.log('Rephrasing...');
|
|
64
|
+
generate({
|
|
65
|
+
action: 'Rephrase',
|
|
66
|
+
})
|
|
67
|
+
.catch((reason) => {
|
|
68
|
+
console.error('Compose : ', reason);
|
|
69
|
+
})
|
|
70
|
+
.finally(() => {
|
|
71
|
+
setIsProcessing(false);
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
onSettings: isConfigAllowed ? openDrawer : undefined,
|
|
75
|
+
onSimplify: () => {
|
|
76
|
+
console.log('Simplifying...');
|
|
77
|
+
generate({
|
|
78
|
+
action: 'Simplify',
|
|
79
|
+
})
|
|
80
|
+
.catch((reason) => {
|
|
81
|
+
console.error('Compose : ', reason);
|
|
82
|
+
})
|
|
83
|
+
.finally(() => {
|
|
84
|
+
setIsProcessing(false);
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
onSummarize: () => {
|
|
88
|
+
console.log('Summarizing...');
|
|
89
|
+
generate({
|
|
90
|
+
action: 'Summarize',
|
|
91
|
+
})
|
|
92
|
+
.catch((reason) => {
|
|
93
|
+
console.error('Compose : ', reason);
|
|
94
|
+
})
|
|
95
|
+
.finally(() => {
|
|
96
|
+
setIsProcessing(false);
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
onTranslate: (data) => {
|
|
100
|
+
console.log('Translating...');
|
|
101
|
+
generate({
|
|
102
|
+
action: 'Translate',
|
|
103
|
+
params: data,
|
|
104
|
+
})
|
|
105
|
+
.catch((reason) => {
|
|
106
|
+
console.error('Compose : ', reason);
|
|
107
|
+
})
|
|
108
|
+
.finally(() => {
|
|
109
|
+
setIsProcessing(false);
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
}, {
|
|
113
|
+
isConfigAllowed,
|
|
114
|
+
});
|
|
115
|
+
const { setValue } = useField({
|
|
116
|
+
path: pathFromContext,
|
|
117
|
+
});
|
|
118
|
+
const setIfValueIsLexicalState = useCallback((val) => {
|
|
119
|
+
if (val && typeof val === 'object' && 'root' in val && lexicalEditor) {
|
|
120
|
+
setSafeLexicalState(JSON.stringify(val), lexicalEditor);
|
|
121
|
+
}
|
|
122
|
+
// DO NOT PROVIDE lexicalEditor as a dependency, it freaks out and does not update the editor after first undo/redo
|
|
123
|
+
}, []);
|
|
124
|
+
const popupRender = useCallback(({ close }) => {
|
|
125
|
+
return <Menu isLoading={isProcessing || isLoading} onClose={close}/>;
|
|
126
|
+
}, [isProcessing, isLoading, Menu]);
|
|
127
|
+
const memoizedPopup = useMemo(() => {
|
|
128
|
+
return (<Popup button={<PluginIcon isLoading={isProcessing || isLoading}/>} render={popupRender} verticalAlign="bottom"/>);
|
|
129
|
+
}, [popupRender, isProcessing, isLoading]);
|
|
130
|
+
return (<label className={`payloadai-compose__actions ${styles.actions}`} onClick={(e) => e.preventDefault()} role="presentation">
|
|
131
|
+
<DocumentDrawer onSave={() => {
|
|
132
|
+
closeDrawer();
|
|
133
|
+
}}/>
|
|
134
|
+
{memoizedPopup}
|
|
135
|
+
<ActiveComponent isLoading={isProcessing || isLoading} stop={stop}/>
|
|
136
|
+
<UndoRedoActions onChange={(val) => {
|
|
137
|
+
setValue(val);
|
|
138
|
+
setIfValueIsLexicalState(val);
|
|
139
|
+
}}/>
|
|
140
|
+
</label>);
|
|
141
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { useHistory } from './hooks/useHistory.js';
|
|
3
|
+
export const UndoRedoActions = ({ onChange }) => {
|
|
4
|
+
const { canRedo, canUndo, redo, undo } = useHistory();
|
|
5
|
+
const redoHistoryValue = useCallback((event) => {
|
|
6
|
+
event.stopPropagation();
|
|
7
|
+
const value = redo();
|
|
8
|
+
if (value) {
|
|
9
|
+
onChange(value);
|
|
10
|
+
}
|
|
11
|
+
}, [redo]);
|
|
12
|
+
const undoHistoryValue = useCallback((event) => {
|
|
13
|
+
event.stopPropagation();
|
|
14
|
+
const value = undo();
|
|
15
|
+
if (value) {
|
|
16
|
+
onChange(value);
|
|
17
|
+
}
|
|
18
|
+
}, [undo]);
|
|
19
|
+
// Delay rendering until the client-side hydration is complete
|
|
20
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
setIsMounted(true);
|
|
23
|
+
}, []);
|
|
24
|
+
if (!isMounted || (!canUndo && !canRedo))
|
|
25
|
+
return null;
|
|
26
|
+
return (<React.Fragment>
|
|
27
|
+
<button className={`btn btn--size-small btn--style-secondary ${!canUndo && 'btn--disabled'}`} disabled={!canUndo} onClick={undoHistoryValue} style={{ marginBlock: 0 }} type="button">
|
|
28
|
+
Undo
|
|
29
|
+
</button>
|
|
30
|
+
<button className={`btn btn--size-small btn--style-secondary ${!canRedo && 'btn--disabled'}`} disabled={!canRedo} onClick={redoHistoryValue} style={{ marginBlock: 0 }} type="button">
|
|
31
|
+
Redo
|
|
32
|
+
</button>
|
|
33
|
+
</React.Fragment>);
|
|
34
|
+
};
|
|
@@ -1,21 +1,108 @@
|
|
|
1
1
|
.actions {
|
|
2
2
|
position: relative;
|
|
3
|
-
margin: 5px 0;
|
|
4
3
|
display: flex;
|
|
5
4
|
gap: 10px;
|
|
5
|
+
align-items: center;
|
|
6
|
+
opacity: 0;
|
|
7
|
+
visibility: hidden;
|
|
8
|
+
pointer-events: none;
|
|
9
|
+
padding: 0 5px;
|
|
10
|
+
margin: 0;
|
|
11
|
+
z-index: 1;
|
|
12
|
+
transform: scaleY(0) skewX(90deg) translateY(-5px);
|
|
13
|
+
transform-origin: top;
|
|
14
|
+
max-height: 0;
|
|
15
|
+
|
|
16
|
+
will-change: transform, opacity;
|
|
17
|
+
backface-visibility: hidden;
|
|
18
|
+
perspective: 1000px;
|
|
19
|
+
|
|
20
|
+
transition:
|
|
21
|
+
opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
|
|
22
|
+
transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
|
|
23
|
+
max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1),
|
|
24
|
+
visibility 0s linear 0.3s;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
:global(.field-type.ai-plugin-active) .actions {
|
|
28
|
+
opacity: 1;
|
|
29
|
+
visibility: visible;
|
|
30
|
+
pointer-events: auto;
|
|
6
31
|
padding: 5px 0;
|
|
7
|
-
|
|
32
|
+
margin: 5px 0;
|
|
8
33
|
max-height: 100px;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
34
|
+
transform: scaleY(1) skewX(0) translateY(0) translateZ(0);
|
|
35
|
+
|
|
36
|
+
transition:
|
|
37
|
+
opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
|
|
38
|
+
transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
|
|
39
|
+
max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
|
12
40
|
}
|
|
13
41
|
|
|
14
|
-
.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
42
|
+
:global(.field-type.ai-plugin-active) .actions {
|
|
43
|
+
animation: removeWillChange 0.3s forwards;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@keyframes removeWillChange {
|
|
47
|
+
to {
|
|
48
|
+
will-change: auto;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.actions > * {
|
|
19
53
|
opacity: 0;
|
|
20
|
-
|
|
21
|
-
|
|
54
|
+
transform: translateY(-3px) translateZ(0);
|
|
55
|
+
|
|
56
|
+
will-change: transform, opacity;
|
|
57
|
+
backface-visibility: hidden;
|
|
58
|
+
|
|
59
|
+
transition:
|
|
60
|
+
opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1),
|
|
61
|
+
transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
:global(.field-type.ai-plugin-active) .actions > * {
|
|
65
|
+
opacity: 1;
|
|
66
|
+
transform: translateY(0) translateZ(0);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
:global(.field-type.ai-plugin-active) .actions > *:nth-child(1) {
|
|
70
|
+
transition-delay: 0.05s;
|
|
71
|
+
animation: removeWillChange 0.25s 0.05s forwards;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
:global(.field-type.ai-plugin-active) .actions > *:nth-child(2) {
|
|
75
|
+
transition-delay: 0.08s;
|
|
76
|
+
animation: removeWillChange 0.28s 0.08s forwards;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
:global(.field-type.ai-plugin-active) .actions > *:nth-child(3) {
|
|
80
|
+
transition-delay: 0.11s;
|
|
81
|
+
animation: removeWillChange 0.31s 0.11s forwards;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
:global(.field-type.ai-plugin-active) .actions > *:nth-child(4) {
|
|
85
|
+
transition-delay: 0.14s;
|
|
86
|
+
animation: removeWillChange 0.34s 0.14s forwards;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
:global(.field-type.ai-plugin-active) .actions > *:nth-child(5) {
|
|
90
|
+
transition-delay: 0.17s;
|
|
91
|
+
animation: removeWillChange 0.37s 0.17s forwards;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@media (prefers-reduced-motion: no-preference) and (min-resolution: 120dpi) {
|
|
95
|
+
.actions,
|
|
96
|
+
.actions > * {
|
|
97
|
+
transition-duration: 0.2s;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@media (prefers-reduced-motion: reduce) {
|
|
102
|
+
.actions,
|
|
103
|
+
.actions > * {
|
|
104
|
+
transition-duration: 0.01ms !important;
|
|
105
|
+
animation-duration: 0.01ms !important;
|
|
106
|
+
will-change: auto !important;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React, { memo } from 'react';
|
|
2
|
+
import { ArrowIcon } from '../../../Icons/Icons.js';
|
|
3
|
+
import styles from './menu.module.scss';
|
|
4
|
+
export const Item = memo(({ children, disabled, isActive, onClick, ...rest }) => (<span className={styles.generate_button + ' ' + (isActive ? styles.active : '')} data-disabled={disabled} onClick={!disabled && typeof onClick === 'function'
|
|
5
|
+
? onClick
|
|
6
|
+
: undefined} onKeyDown={!disabled && typeof onClick === 'function'
|
|
7
|
+
? onClick
|
|
8
|
+
: undefined} role="presentation" {...rest}>
|
|
9
|
+
{children}
|
|
10
|
+
</span>));
|
|
11
|
+
export const createMenuItem = (IconComponent, initialText) => memo(({ children, disabled, hideIcon, isMenu, onClick, ...rest }) => (<Item disabled={disabled} onClick={onClick} {...rest}>
|
|
12
|
+
{hideIcon || <IconComponent size={18}/>}
|
|
13
|
+
{children || <span className={styles.text}>{initialText}</span>}
|
|
14
|
+
{isMenu && <ArrowIcon size={18}/>}
|
|
15
|
+
</Item>));
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import locales from 'locale-codes';
|
|
2
|
+
import React, { memo, useState } from 'react';
|
|
3
|
+
import { useInstructions } from '../../../../providers/InstructionsProvider/useInstructions.js';
|
|
4
|
+
import { Item } from './Item.js';
|
|
5
|
+
import { Translate } from './items.js';
|
|
6
|
+
import styles from './menu.module.scss';
|
|
7
|
+
export const TranslateMenu = ({ onClick }) => {
|
|
8
|
+
const [show, setShow] = useState(false);
|
|
9
|
+
const { enabledLanguages = [] } = useInstructions();
|
|
10
|
+
let filteredLocales = locales.all.filter((a) => {
|
|
11
|
+
return a.tag && a.location;
|
|
12
|
+
});
|
|
13
|
+
if (enabledLanguages?.length) {
|
|
14
|
+
filteredLocales = filteredLocales.filter((a) => enabledLanguages?.includes(a.tag));
|
|
15
|
+
}
|
|
16
|
+
const [languages, setLanguages] = useState(filteredLocales);
|
|
17
|
+
const [inputFocus, setInputFocus] = useState(false);
|
|
18
|
+
return (<div className={styles.menu} onMouseLeave={() => {
|
|
19
|
+
if (!inputFocus) {
|
|
20
|
+
setShow(false);
|
|
21
|
+
}
|
|
22
|
+
}}>
|
|
23
|
+
<Translate isActive={show} isMenu onClick={() => {
|
|
24
|
+
setShow(!show);
|
|
25
|
+
}} onMouseEnter={() => setShow(true)}/>
|
|
26
|
+
<div className={styles.hoverMenu} data-show={show}>
|
|
27
|
+
<div className={`${styles.menu} ${styles.subMenu}`} style={{
|
|
28
|
+
background: 'var(--popup-bg)',
|
|
29
|
+
// minHeight: '300px',
|
|
30
|
+
}}>
|
|
31
|
+
<Item onClick={() => { }} style={{
|
|
32
|
+
background: 'transparent',
|
|
33
|
+
padding: '0 0 5px 0',
|
|
34
|
+
position: 'sticky',
|
|
35
|
+
top: 0,
|
|
36
|
+
}}>
|
|
37
|
+
<input className={styles.menuInput} onBlur={() => setInputFocus(false)} onChange={(event) => {
|
|
38
|
+
const value = event.target.value;
|
|
39
|
+
setLanguages(filteredLocales.filter((l) => {
|
|
40
|
+
const lowerCaseValue = value.toLowerCase();
|
|
41
|
+
return (l.name.toLowerCase().startsWith(lowerCaseValue) ||
|
|
42
|
+
(l.location && l.location.toLowerCase().startsWith(lowerCaseValue)) ||
|
|
43
|
+
l.tag.toLowerCase().startsWith(lowerCaseValue));
|
|
44
|
+
}));
|
|
45
|
+
}} onFocus={() => setInputFocus(true)} placeholder="Search..."/>
|
|
46
|
+
</Item>
|
|
47
|
+
{languages.map((locale) => {
|
|
48
|
+
return (<Item key={locale.tag} onClick={() => {
|
|
49
|
+
onClick({ locale: locale.tag });
|
|
50
|
+
}}>
|
|
51
|
+
<span className={styles.ellipsis}>{`${locale.location} (${locale.tag})`}</span>
|
|
52
|
+
</Item>);
|
|
53
|
+
})}
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
</div>);
|
|
57
|
+
};
|
|
58
|
+
export const MemoizedTranslateMenu = memo(TranslateMenu);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DocsAddOnIcon, EditNoteIcon, SegmentIcon, SpellCheckIcon, StylusNoteIcon, SummarizeIcon, TranslateIcon, TuneIcon, } from '../../../Icons/Icons.js';
|
|
2
|
+
import { createMenuItem } from './Item.js';
|
|
3
|
+
export const Proofread = createMenuItem(SpellCheckIcon, 'Proofread');
|
|
4
|
+
export const Rephrase = createMenuItem(EditNoteIcon, 'Rephrase');
|
|
5
|
+
export const Translate = createMenuItem(TranslateIcon, 'Translate');
|
|
6
|
+
export const Expand = createMenuItem(DocsAddOnIcon, 'Expand');
|
|
7
|
+
export const Summarize = createMenuItem(SummarizeIcon, 'Summarize');
|
|
8
|
+
export const Simplify = createMenuItem(SegmentIcon, 'Simplify');
|
|
9
|
+
export const Compose = createMenuItem(StylusNoteIcon, 'Compose');
|
|
10
|
+
export const Settings = createMenuItem(TuneIcon, 'Settings');
|
|
@@ -19,7 +19,7 @@ const getActiveComponent = (ac)=>{
|
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
export const useMenu = (menuEvents, options)=>{
|
|
22
|
-
const { type: fieldType, path: pathFromContext } = useFieldProps();
|
|
22
|
+
const { field: { type: fieldType } = {}, path: pathFromContext } = useFieldProps();
|
|
23
23
|
const field = useField({
|
|
24
24
|
path: pathFromContext ?? ''
|
|
25
25
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/ui/Compose/hooks/menu/useMenu.tsx"],"sourcesContent":["'use client'\n\nimport { useField } from '@payloadcms/ui'\nimport React, { useEffect, useMemo, useState } from 'react'\n\nimport type { ActionMenuItems, UseMenuEvents, UseMenuOptions } from '../../../../types.js'\n\nimport { useFieldProps } from '../../../../providers/FieldProvider/useFieldProps.js'\nimport { Compose, Proofread, Rephrase } from './items.js'\nimport { menuItemsMap } from './itemsMap.js'\nimport styles from './menu.module.scss'\n\nconst getActiveComponent = (ac: ActionMenuItems) => {\n switch (ac) {\n case 'Compose':\n return Compose\n case 'Proofread':\n return Proofread\n case 'Rephrase':\n return Rephrase\n default:\n return Rephrase\n }\n}\n\nexport const useMenu = (menuEvents: UseMenuEvents, options: UseMenuOptions) => {\n const { type: fieldType, path: pathFromContext } = useFieldProps()\n const field = useField({ path: pathFromContext ?? '' })\n const [activeComponent, setActiveComponent] = useState<ActionMenuItems>('Rephrase')\n\n const { initialValue, value } = field\n\n useEffect(() => {\n if (!value) {\n setActiveComponent('Compose')\n return\n }\n\n if (menuItemsMap.some((i) => i.excludedFor?.includes(fieldType ?? ''))) {\n setActiveComponent('Compose')\n return\n }\n\n if (typeof value === 'string' && value !== initialValue) {\n setActiveComponent('Proofread')\n } else {\n setActiveComponent('Rephrase')\n }\n }, [initialValue, value, fieldType])\n\n const MemoizedActiveComponent = useMemo(() => {\n return ({ isLoading, stop }: { isLoading: boolean; stop: () => void }) => {\n const ActiveComponent = getActiveComponent(activeComponent)\n const activeItem = menuItemsMap.find((i) => i.name === activeComponent)!\n return (\n <ActiveComponent\n hideIcon\n onClick={(data: unknown) => {\n if (!isLoading) {\n const trigger = menuEvents[`on${activeComponent}`]\n if (typeof trigger === 'function') {\n trigger(data)\n } else {\n console.error('No trigger found for', activeComponent)\n }\n } else {\n stop()\n }\n }}\n title={isLoading ? 'Click to stop' : activeItem.name}\n >\n {isLoading && activeItem.loadingText}\n </ActiveComponent>\n )\n }\n }, [activeComponent, menuEvents])\n\n const filteredMenuItems = useMemo(\n () =>\n menuItemsMap.filter((i) => {\n if (i.name === 'Settings' && !options.isConfigAllowed) {\n return false\n } // Disable settings if a user role is not permitted\n return i.name !== activeComponent && !i.excludedFor?.includes(fieldType ?? '')\n }),\n [activeComponent, fieldType, options.isConfigAllowed],\n )\n\n const MemoizedMenu = useMemo(() => {\n return ({ isLoading, onClose }: { isLoading: boolean; onClose: () => void }) => (\n <div className={styles.menu}>\n {filteredMenuItems.map((i) => {\n const Action = i.component\n return (\n <Action\n disabled={isLoading}\n key={i.name}\n onClick={(data: unknown) => {\n if (i.name !== 'Settings') {\n setActiveComponent(i.name)\n }\n\n menuEvents[`on${i.name}`]?.(data)\n onClose()\n }}\n >\n {isLoading && i.loadingText}\n </Action>\n )\n })}\n </div>\n )\n }, [filteredMenuItems, menuEvents])\n\n return {\n ActiveComponent: MemoizedActiveComponent,\n Menu: MemoizedMenu,\n }\n}\n"],"names":["useField","React","useEffect","useMemo","useState","useFieldProps","Compose","Proofread","Rephrase","menuItemsMap","styles","getActiveComponent","ac","useMenu","menuEvents","options","type","fieldType","path","pathFromContext","
|
|
1
|
+
{"version":3,"sources":["../../../../../src/ui/Compose/hooks/menu/useMenu.tsx"],"sourcesContent":["'use client'\n\nimport { useField } from '@payloadcms/ui'\nimport React, { useEffect, useMemo, useState } from 'react'\n\nimport type { ActionMenuItems, UseMenuEvents, UseMenuOptions } from '../../../../types.js'\n\nimport { useFieldProps } from '../../../../providers/FieldProvider/useFieldProps.js'\nimport { Compose, Proofread, Rephrase } from './items.js'\nimport { menuItemsMap } from './itemsMap.js'\nimport styles from './menu.module.scss'\n\nconst getActiveComponent = (ac: ActionMenuItems) => {\n switch (ac) {\n case 'Compose':\n return Compose\n case 'Proofread':\n return Proofread\n case 'Rephrase':\n return Rephrase\n default:\n return Rephrase\n }\n}\n\nexport const useMenu = (menuEvents: UseMenuEvents, options: UseMenuOptions) => {\n const { field:{ type: fieldType } = {}, path: pathFromContext } = useFieldProps()\n const field = useField({ path: pathFromContext ?? '' })\n const [activeComponent, setActiveComponent] = useState<ActionMenuItems>('Rephrase')\n\n const { initialValue, value } = field\n\n useEffect(() => {\n if (!value) {\n setActiveComponent('Compose')\n return\n }\n\n if (menuItemsMap.some((i) => i.excludedFor?.includes(fieldType ?? ''))) {\n setActiveComponent('Compose')\n return\n }\n\n if (typeof value === 'string' && value !== initialValue) {\n setActiveComponent('Proofread')\n } else {\n setActiveComponent('Rephrase')\n }\n }, [initialValue, value, fieldType])\n\n const MemoizedActiveComponent = useMemo(() => {\n return ({ isLoading, stop }: { isLoading: boolean; stop: () => void }) => {\n const ActiveComponent = getActiveComponent(activeComponent)\n const activeItem = menuItemsMap.find((i) => i.name === activeComponent)!\n return (\n <ActiveComponent\n hideIcon\n onClick={(data: unknown) => {\n if (!isLoading) {\n const trigger = menuEvents[`on${activeComponent}`]\n if (typeof trigger === 'function') {\n trigger(data)\n } else {\n console.error('No trigger found for', activeComponent)\n }\n } else {\n stop()\n }\n }}\n title={isLoading ? 'Click to stop' : activeItem.name}\n >\n {isLoading && activeItem.loadingText}\n </ActiveComponent>\n )\n }\n }, [activeComponent, menuEvents])\n\n const filteredMenuItems = useMemo(\n () =>\n menuItemsMap.filter((i) => {\n if (i.name === 'Settings' && !options.isConfigAllowed) {\n return false\n } // Disable settings if a user role is not permitted\n return i.name !== activeComponent && !i.excludedFor?.includes(fieldType ?? '')\n }),\n [activeComponent, fieldType, options.isConfigAllowed],\n )\n\n const MemoizedMenu = useMemo(() => {\n return ({ isLoading, onClose }: { isLoading: boolean; onClose: () => void }) => (\n <div className={styles.menu}>\n {filteredMenuItems.map((i) => {\n const Action = i.component\n return (\n <Action\n disabled={isLoading}\n key={i.name}\n onClick={(data: unknown) => {\n if (i.name !== 'Settings') {\n setActiveComponent(i.name)\n }\n\n menuEvents[`on${i.name}`]?.(data)\n onClose()\n }}\n >\n {isLoading && i.loadingText}\n </Action>\n )\n })}\n </div>\n )\n }, [filteredMenuItems, menuEvents])\n\n return {\n ActiveComponent: MemoizedActiveComponent,\n Menu: MemoizedMenu,\n }\n}\n"],"names":["useField","React","useEffect","useMemo","useState","useFieldProps","Compose","Proofread","Rephrase","menuItemsMap","styles","getActiveComponent","ac","useMenu","menuEvents","options","field","type","fieldType","path","pathFromContext","activeComponent","setActiveComponent","initialValue","value","some","i","excludedFor","includes","MemoizedActiveComponent","isLoading","stop","ActiveComponent","activeItem","find","name","hideIcon","onClick","data","trigger","console","error","title","loadingText","filteredMenuItems","filter","isConfigAllowed","MemoizedMenu","onClose","div","className","menu","map","Action","component","disabled","Menu"],"mappings":"AAAA;;AAEA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,OAAOC,SAASC,SAAS,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,QAAO;AAI3D,SAASC,aAAa,QAAQ,uDAAsD;AACpF,SAASC,OAAO,EAAEC,SAAS,EAAEC,QAAQ,QAAQ,aAAY;AACzD,SAASC,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