@copilotkit/react-textarea 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/.turbo/turbo-build.log +166 -162
  2. package/CHANGELOG.md +12 -0
  3. package/dist/{chunk-6LKDH2UJ.mjs → chunk-7KWRVIYV.mjs} +3 -2
  4. package/dist/chunk-7KWRVIYV.mjs.map +1 -0
  5. package/dist/{chunk-24IOWJS4.mjs → chunk-E2UEE45I.mjs} +11 -3
  6. package/dist/chunk-E2UEE45I.mjs.map +1 -0
  7. package/dist/{chunk-ZPACRCJ6.mjs → chunk-EHQ5TN4R.mjs} +2 -2
  8. package/dist/{chunk-4EA7I44O.mjs → chunk-YICIML6J.mjs} +31 -9
  9. package/dist/chunk-YICIML6J.mjs.map +1 -0
  10. package/dist/{chunk-ROH375UB.mjs → chunk-YTT5NE6B.mjs} +3 -3
  11. package/dist/chunk-YTT5NE6B.mjs.map +1 -0
  12. package/dist/components/base-copilot-textarea/base-copilot-textarea.d.ts +4 -13
  13. package/dist/components/base-copilot-textarea/base-copilot-textarea.mjs +4 -4
  14. package/dist/components/copilot-textarea/copilot-textarea.d.ts +2 -3
  15. package/dist/components/copilot-textarea/copilot-textarea.mjs +6 -6
  16. package/dist/components/index.d.ts +3 -2
  17. package/dist/components/index.mjs +6 -6
  18. package/dist/hooks/use-autosuggestions.d.ts +1 -1
  19. package/dist/hooks/use-autosuggestions.mjs +1 -1
  20. package/dist/index.d.ts +2 -1
  21. package/dist/index.mjs +6 -6
  22. package/dist/types/base/base-autosuggestions-config.d.ts +1 -0
  23. package/dist/types/base/base-autosuggestions-config.mjs +1 -1
  24. package/dist/types/base/base-copilot-textarea-props.d.ts +38 -0
  25. package/dist/types/base/base-copilot-textarea-props.mjs +3 -0
  26. package/dist/types/base/base-copilot-textarea-props.mjs.map +1 -0
  27. package/dist/types/base/index.d.ts +2 -0
  28. package/dist/types/base/index.mjs +1 -1
  29. package/dist/types/index.d.ts +2 -0
  30. package/dist/types/index.mjs +2 -2
  31. package/dist/types/standard-autosuggestions/autosuggestions-config.mjs +2 -2
  32. package/dist/types/standard-autosuggestions/index.mjs +2 -2
  33. package/package.json +3 -3
  34. package/src/components/base-copilot-textarea/base-copilot-textarea.tsx +39 -22
  35. package/src/components/copilot-textarea/copilot-textarea.tsx +2 -4
  36. package/src/components/index.ts +0 -1
  37. package/src/hooks/use-autosuggestions.ts +14 -2
  38. package/src/types/base/base-autosuggestions-config.tsx +2 -0
  39. package/src/types/base/base-copilot-textarea-props.tsx +37 -0
  40. package/src/types/base/index.ts +3 -1
  41. package/dist/chunk-24IOWJS4.mjs.map +0 -1
  42. package/dist/chunk-4EA7I44O.mjs.map +0 -1
  43. package/dist/chunk-6LKDH2UJ.mjs.map +0 -1
  44. package/dist/chunk-ROH375UB.mjs.map +0 -1
  45. /package/dist/{chunk-ZPACRCJ6.mjs.map → chunk-EHQ5TN4R.mjs.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/base-copilot-textarea/base-copilot-textarea.tsx"],"names":["_a","value"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,aAAa,WAAW,SAAS,gBAAgB;AAC1D,SAAqB,cAAc;AACnC,SAAS,UAAU,aAAa;AAChC,SAAS,eAAe;AAiKlB;AA3IC,SAAS,oBACd,OAGa;AACb,QAAM,wBAAmD,kCACpD,mCACA,MAAM;AAGX,QAAM,uBAAuB,QAAQ,MAAG;AAnC1C,QAAAA;AAmC6C,YAAAA,MAAA,MAAM,UAAN,OAAAA,MAAe;AAAA,KAAI,CAAC,CAAC;AAChE,QAAM,CAAC,yBAAyB,0BAA0B,IACxD,SAAS,oBAAoB;AAE/B,QAAM,eAA6B,QAAQ,MAAM;AAC/C,WAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,UAAU,CAAC,EAAE,MAAM,qBAAqB,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF,GAAG,CAAC,oBAAoB,CAAC;AAEzB,QAAM,SAAS,yBAAyB;AAExC,QAAM,aAAa;AAAA,IACjB,CAAC,mBAAwC;AACvC,aAAO,WAAW,QAAQ,eAAe,MAAM;AAAA,QAC7C,IAAI,eAAe;AAAA,MACrB,CAAC;AAAA,IACH;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AAEA,QAAM;AAAA,IACJ;AAAA,IACA,iBAAiB;AAAA,IACjB,kBAAkB;AAAA,EACpB,IAAI;AAAA,IACF,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,IACtB,MAAM;AAAA,IACN;AAAA,IACA,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,EACxB;AAGA,YAAU,MAAM;AACd,mCAA+B,MAAM;AACrC,QAAI,+BAA+B;AACjC;AAAA,QACE;AAAA,QACA,8BAA8B;AAAA,QAC9B,8BAA8B;AAAA,MAChC;AAAA,IACF;AAAA,EACF,GAAG,CAAC,6BAA6B,CAAC;AAElC,QAAM,2BAAgD,QAAQ,MAAM;AAClE,WAAO;AAAA,MACL,WAAW;AAAA,MACX,OAAO;AAAA,OACJ,MAAM;AAAA,EAEb,GAAG,CAAC,MAAM,gBAAgB,CAAC;AAE3B,oBAAkB,0BAA0B,MAAM,eAAe;AAEjE,QAAM,wBAAwB,QAAQ,MAAM;AAC1C,WAAO,0BAA0B,wBAAwB;AAAA,EAC3D,GAAG,CAAC,wBAAwB,CAAC;AAE7B,QAAM,4BAA4B,QAAQ,MAAM;AAE9C,UAAM,mCAAwD;AAAA,MAC5D,KAAK;AAAA,IACP;AAEA,UAAM,4BAAiD,kCAClD,mCACA,MAAM;AAGX,WAAO,8BAA8B,yBAAyB;AAAA,EAChE,GAAG,CAAC,MAAM,gBAAgB,CAAC;AAG3B,YAAU,MAAM;AAjHlB,QAAAA,KAAA;AAkHI,QAAI,MAAM,UAAU,yBAAyB;AAC3C;AAAA,IACF;AAEA,gCAA2BA,MAAA,MAAM,UAAN,OAAAA,MAAe,EAAE;AAC5C,sBAAkB,SAAQ,WAAM,UAAN,YAAe,EAAE;AAAA,EAC7C,GAAG,CAAC,MAAM,KAAK,CAAC;AAGhB,QAUI,YATF;AAAA;AAAA,IACA;AAAA,IACA;AAAA,IACA,uBAAuB;AAAA,IACvB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAnIJ,IAqIM,IADC,2BACD,IADC;AAAA,IARH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAIF,QAAM,mBAAmB,MAAM;AAC7B,UAAM,gBAAgB;AACtB,UAAM,gBAAgB,MAAM,kBACxB,gBACA;AACJ,UAAM,2BAA2B;AACjC,UAAM,kBAAkB,QAAQ,0BAA0B,gCAAa,EAAE;AACzE,WAAO,GAAG,iBAAiB,iBAAiB;AAAA,EAC9C,GAAG;AAEH,SAEE,oBAAC;AAAA,IACC;AAAA,IACA;AAAA,IACA,UAAU,CAACC,WAAU;AAtJ3B,UAAAD,KAAA;AAuJQ,YAAM,iBAAiB,oBAAoB,MAAM;AAEjD,YAAM,iBAAiB,iBACnB,eAAe,mBAAmB,eAAe,kBACjD,8BAA8B,MAAM;AAExC,iCAA2B,cAAc;AACzC,qCAA+B,cAAc;AAE7C,OAAAA,MAAA,MAAM,kBAAN,gBAAAA,IAAA,YAAsB;AACtB,kBAAM,aAAN,+BAAiB,+BAA+B,cAAc;AAAA,IAChE;AAAA,IAEA,8BAAC;AAAA,MACC,eAAe;AAAA,MACf,mBAAmB;AAAA,MACnB,WAAW,CAAC,UAAU;AAvK9B,YAAAA;AAwKU,wCAAgC,KAAK;AACrC,SAAAA,MAAA,MAAM,cAAN,gBAAAA,IAAA,YAAkB;AAAA,MACpB;AAAA,MACA,WAAW;AAAA,OACP,eACN;AAAA,GACF;AAEJ;AAaA,SAAS,+BACP,aACwC;AACxC,SAAO;AAAA,IACL,QAAQ;AAAA,MACN,OAAO;AAAA,MACP,MAAM;AAAA,IACR;AAAA,IACA,eAAe;AAAA,MACb,OAAO;AAAA,MACP,MAAM;AAAA,IACR;AAAA,EACF;AACF","sourcesContent":["import { useCallback, useEffect, useMemo, useState } from \"react\";\nimport { Descendant, Editor } from \"slate\";\nimport { Editable, Slate } from \"slate-react\";\nimport { twMerge } from \"tailwind-merge\";\nimport { useAutosuggestions } from \"../../hooks/use-autosuggestions\";\nimport { useCopilotTextareaEditor } from \"../../hooks/use-copilot-textarea-editor\";\nimport {\n getFullEditorTextWithNewlines,\n getTextAroundCursor,\n} from \"../../lib/get-text-around-cursor\";\nimport { addAutocompletionsToEditor } from \"../../lib/slatejs-edits/add-autocompletions\";\nimport { clearAutocompletionsFromEditor } from \"../../lib/slatejs-edits/clear-autocompletions\";\nimport { replaceEditorText } from \"../../lib/slatejs-edits/replace-text\";\nimport {\n AutosuggestionsBareFunction,\n BaseAutosuggestionsConfig,\n defaultBaseAutosuggestionsConfig,\n} from \"../../types/base\";\nimport { AutosuggestionState } from \"../../types/base/autosuggestion-state\";\nimport { BaseCopilotTextareaProps } from \"../../types/base/base-copilot-textarea-props\";\nimport \"./base-copilot-textarea.css\";\nimport { makeRenderElementFunction } from \"./render-element\";\nimport { makeRenderPlaceholderFunction } from \"./render-placeholder\";\nimport { useAddBrandingCss } from \"./use-add-branding-css\";\n\nexport function BaseCopilotTextarea(\n props: BaseCopilotTextareaProps & {\n autosuggestionsFunction: AutosuggestionsBareFunction;\n }\n): JSX.Element {\n const autosuggestionsConfig: BaseAutosuggestionsConfig = {\n ...defaultBaseAutosuggestionsConfig,\n ...props.autosuggestionsConfig,\n };\n\n const valueOnInitialRender = useMemo(() => props.value ?? \"\", []);\n const [lastKnownFullEditorText, setLastKnownFullEditorText] =\n useState(valueOnInitialRender);\n\n const initialValue: Descendant[] = useMemo(() => {\n return [\n {\n type: \"paragraph\",\n children: [{ text: valueOnInitialRender }],\n },\n ];\n }, [valueOnInitialRender]);\n\n const editor = useCopilotTextareaEditor();\n\n const insertText = useCallback(\n (autosuggestion: AutosuggestionState) => {\n Editor.insertText(editor, autosuggestion.text, {\n at: autosuggestion.point,\n });\n },\n [editor]\n );\n\n const {\n currentAutocompleteSuggestion,\n onChangeHandler: onChangeHandlerForAutocomplete,\n onKeyDownHandler: onKeyDownHandlerForAutocomplete,\n } = useAutosuggestions(\n autosuggestionsConfig.debounceTime,\n autosuggestionsConfig.acceptAutosuggestionKey,\n props.autosuggestionsFunction,\n insertText,\n autosuggestionsConfig.disableWhenEmpty,\n autosuggestionsConfig.disabled\n );\n\n // sync autosuggestions state with the editor\n useEffect(() => {\n clearAutocompletionsFromEditor(editor);\n if (currentAutocompleteSuggestion) {\n addAutocompletionsToEditor(\n editor,\n currentAutocompleteSuggestion.text,\n currentAutocompleteSuggestion.point\n );\n }\n }, [currentAutocompleteSuggestion]);\n\n const suggestionStyleAugmented: React.CSSProperties = useMemo(() => {\n return {\n fontStyle: \"italic\",\n color: \"gray\",\n ...props.suggestionsStyle,\n };\n }, [props.suggestionsStyle]);\n\n useAddBrandingCss(suggestionStyleAugmented, props.disableBranding);\n\n const renderElementMemoized = useMemo(() => {\n return makeRenderElementFunction(suggestionStyleAugmented);\n }, [suggestionStyleAugmented]);\n\n const renderPlaceholderMemoized = useMemo(() => {\n // For some reason slateJS specifies a top value of 0, which makes for strange styling. We override this here.\n const placeholderStyleSlatejsOverrides: React.CSSProperties = {\n top: undefined,\n };\n\n const placeholderStyleAugmented: React.CSSProperties = {\n ...placeholderStyleSlatejsOverrides,\n ...props.placeholderStyle,\n };\n\n return makeRenderPlaceholderFunction(placeholderStyleAugmented);\n }, [props.placeholderStyle]);\n\n // update the editor text, but only when the value changes from outside the component\n useEffect(() => {\n if (props.value === lastKnownFullEditorText) {\n return;\n }\n\n setLastKnownFullEditorText(props.value ?? \"\");\n replaceEditorText(editor, props.value ?? \"\");\n }, [props.value]);\n\n // separate into TextareaHTMLAttributes<HTMLDivElement> and CopilotTextareaProps\n const {\n placeholderStyle,\n value,\n onValueChange,\n autosuggestionsConfig: autosuggestionsConfigFromProps,\n autosuggestionsFunction,\n className,\n onChange,\n onKeyDown,\n ...propsToForward\n } = props;\n\n const moddedClassName = (() => {\n const baseClassName = \"copilot-textarea\";\n const brandingClass = props.disableBranding\n ? \"no-branding\"\n : \"with-branding\";\n const defaultTailwindClassName = \"bg-white overflow-y-auto resize-y\";\n const mergedClassName = twMerge(defaultTailwindClassName, className ?? \"\");\n return `${baseClassName} ${brandingClass} ${mergedClassName}`;\n })();\n\n return (\n // Add the editable component inside the context.\n <Slate\n editor={editor}\n initialValue={initialValue}\n onChange={(value) => {\n const newEditorState = getTextAroundCursor(editor);\n\n const fullEditorText = newEditorState\n ? newEditorState.textBeforeCursor + newEditorState.textAfterCursor\n : getFullEditorTextWithNewlines(editor); // we don't double-parse the editor. When `newEditorState` is null, we didn't parse the editor yet.\n\n setLastKnownFullEditorText(fullEditorText);\n onChangeHandlerForAutocomplete(newEditorState);\n\n props.onValueChange?.(fullEditorText);\n props.onChange?.(makeSemiFakeReactTextAreaEvent(fullEditorText));\n }}\n >\n <Editable\n renderElement={renderElementMemoized}\n renderPlaceholder={renderPlaceholderMemoized}\n onKeyDown={(event) => {\n onKeyDownHandlerForAutocomplete(event); // forward the event for internal use\n props.onKeyDown?.(event); // forward the event for external use\n }}\n className={moddedClassName}\n {...propsToForward}\n />\n </Slate>\n );\n}\n\n// Consumers of <textarea> expect a `onChange: (React.ChangeEvent<HTMLTextAreaElement>) => void` event handler to be passed in.\n// This is *extremely* common, and we want to support it.\n//\n// We can't support the full functionality, but in 99% of cases, the consumer only cares about the `event.target.value` property --\n// that's how they get the new value of the textarea.\n//\n// So, the tradeoff we are making is minimizing compiler complaint, with a small chance of runtime error.\n// The alternative would be defining a different onChange entrypoint (we actually do have that in `onValueChange`),\n// And starting to explain subtleties to users the moment they try to use the component for the first time for very basic functionality.\n//\n// If this proves problematic, we can always revisit this decision.\nfunction makeSemiFakeReactTextAreaEvent(\n currentText: string\n): React.ChangeEvent<HTMLTextAreaElement> {\n return {\n target: {\n value: currentText,\n type: \"copilot-textarea\",\n },\n currentTarget: {\n value: currentText,\n type: \"copilot-textarea\",\n },\n } as React.ChangeEvent<HTMLTextAreaElement>;\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import { useMakeStandardAutosuggestionFunction } from './chunk-A3SFEHWZ.mjs';
2
- import { defaultAutosuggestionsConfig } from './chunk-ZPACRCJ6.mjs';
3
- import { BaseCopilotTextarea } from './chunk-4EA7I44O.mjs';
2
+ import { defaultAutosuggestionsConfig } from './chunk-EHQ5TN4R.mjs';
3
+ import { BaseCopilotTextarea } from './chunk-YICIML6J.mjs';
4
4
  import { __spreadValues, __spreadProps } from './chunk-MRXNTQOX.mjs';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
 
@@ -22,4 +22,4 @@ function CopilotTextarea(props) {
22
22
 
23
23
  export { CopilotTextarea };
24
24
  //# sourceMappingURL=out.js.map
25
- //# sourceMappingURL=chunk-ROH375UB.mjs.map
25
+ //# sourceMappingURL=chunk-YTT5NE6B.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/copilot-textarea/copilot-textarea.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;AA+BI;AAhBG,SAAS,gBAAgB,OAA0C;AACxE,QAAM,wBAA+C,kCAChD,+BACA,MAAM;AAGX,QAAM,0BAA0B;AAAA,IAC9B,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,IACtB,sBAAsB;AAAA,EACxB;AAEA,SACE,oBAAC,sDACK,QADL;AAAA,IAEC;AAAA,IACA;AAAA,IACF;AAEJ","sourcesContent":["// This example is for an Editor with `ReactEditor` and `HistoryEditor`\nimport { useMakeStandardAutosuggestionFunction } from \"../../hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function\";\nimport { BaseCopilotTextareaProps } from \"../../types/base/base-copilot-textarea-props\";\nimport {\n AutosuggestionsConfig,\n defaultAutosuggestionsConfig,\n} from \"../../types/standard-autosuggestions\";\nimport { BaseCopilotTextarea } from \"../base-copilot-textarea/base-copilot-textarea\";\n\nexport interface CopilotTextareaProps extends BaseCopilotTextareaProps {\n autosuggestionsConfig: Partial<AutosuggestionsConfig> & {\n purposePrompt: string;\n };\n}\n\nexport function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {\n const autosuggestionsConfig: AutosuggestionsConfig = {\n ...defaultAutosuggestionsConfig,\n ...props.autosuggestionsConfig,\n };\n\n const autosuggestionsFunction = useMakeStandardAutosuggestionFunction(\n autosuggestionsConfig.purposePrompt,\n autosuggestionsConfig.apiEndpoint,\n autosuggestionsConfig.makeSystemPrompt,\n autosuggestionsConfig.fewShotMessages,\n autosuggestionsConfig.externalContextCategories,\n autosuggestionsConfig.forwardedParams\n );\n\n return (\n <BaseCopilotTextarea\n {...props}\n autosuggestionsConfig={autosuggestionsConfig}\n autosuggestionsFunction={autosuggestionsFunction}\n />\n );\n}\n"]}
@@ -1,19 +1,10 @@
1
- import { TextareaHTMLAttributes } from 'react';
2
- import { BaseAutosuggestionsConfig } from '../../types/base/base-autosuggestions-config.js';
3
1
  import { AutosuggestionsBareFunction } from '../../types/base/autosuggestions-bare-function.js';
2
+ import { BaseCopilotTextareaProps } from '../../types/base/base-copilot-textarea-props.js';
3
+ import 'react';
4
+ import '../../types/base/base-autosuggestions-config.js';
4
5
 
5
- interface BaseCopilotTextareaProps extends TextareaHTMLAttributes<HTMLDivElement> {
6
- disableBranding?: boolean;
7
- placeholderStyle?: React.CSSProperties;
8
- suggestionsStyle?: React.CSSProperties;
9
- value?: string;
10
- onValueChange?: (value: string) => void;
11
- autosuggestionsConfig: Partial<BaseAutosuggestionsConfig> & {
12
- purposePrompt: string;
13
- };
14
- }
15
6
  declare function BaseCopilotTextarea(props: BaseCopilotTextareaProps & {
16
7
  autosuggestionsFunction: AutosuggestionsBareFunction;
17
8
  }): JSX.Element;
18
9
 
19
- export { BaseCopilotTextarea, BaseCopilotTextareaProps };
10
+ export { BaseCopilotTextarea };
@@ -1,14 +1,14 @@
1
- export { BaseCopilotTextarea } from '../../chunk-4EA7I44O.mjs';
2
- import '../../chunk-THWPPG6M.mjs';
1
+ export { BaseCopilotTextarea } from '../../chunk-YICIML6J.mjs';
3
2
  import '../../chunk-WJYQWL4I.mjs';
4
3
  import '../../chunk-4XYYSJ5C.mjs';
5
4
  import '../../chunk-TSF4AJIK.mjs';
6
5
  import '../../chunk-3UQM3NLM.mjs';
7
6
  import '../../chunk-WJHSY5T6.mjs';
8
- import '../../chunk-6LKDH2UJ.mjs';
7
+ import '../../chunk-7KWRVIYV.mjs';
9
8
  import '../../chunk-LNAIMEB2.mjs';
10
9
  import '../../chunk-OELUUJZY.mjs';
11
- import '../../chunk-24IOWJS4.mjs';
10
+ import '../../chunk-THWPPG6M.mjs';
11
+ import '../../chunk-E2UEE45I.mjs';
12
12
  import '../../chunk-UW3ITU2Y.mjs';
13
13
  import '../../chunk-KGKLUWKW.mjs';
14
14
  import '../../chunk-6SYD77G6.mjs';
@@ -1,9 +1,8 @@
1
+ import { BaseCopilotTextareaProps } from '../../types/base/base-copilot-textarea-props.js';
1
2
  import { AutosuggestionsConfig } from '../../types/standard-autosuggestions/autosuggestions-config.js';
2
- import { BaseCopilotTextareaProps } from '../base-copilot-textarea/base-copilot-textarea.js';
3
+ import 'react';
3
4
  import '../../types/base/base-autosuggestions-config.js';
4
5
  import '../../types/standard-autosuggestions/minimal-chat-gpt-message.js';
5
- import 'react';
6
- import '../../types/base/autosuggestions-bare-function.js';
7
6
 
8
7
  interface CopilotTextareaProps extends BaseCopilotTextareaProps {
9
8
  autosuggestionsConfig: Partial<AutosuggestionsConfig> & {
@@ -1,18 +1,18 @@
1
- export { CopilotTextarea } from '../../chunk-ROH375UB.mjs';
1
+ export { CopilotTextarea } from '../../chunk-YTT5NE6B.mjs';
2
2
  import '../../chunk-A3SFEHWZ.mjs';
3
3
  import '../../chunk-AZQCUNBV.mjs';
4
- import '../../chunk-ZPACRCJ6.mjs';
5
- import '../../chunk-4EA7I44O.mjs';
6
- import '../../chunk-THWPPG6M.mjs';
4
+ import '../../chunk-EHQ5TN4R.mjs';
5
+ import '../../chunk-YICIML6J.mjs';
7
6
  import '../../chunk-WJYQWL4I.mjs';
8
7
  import '../../chunk-4XYYSJ5C.mjs';
9
8
  import '../../chunk-TSF4AJIK.mjs';
10
9
  import '../../chunk-3UQM3NLM.mjs';
11
10
  import '../../chunk-WJHSY5T6.mjs';
12
- import '../../chunk-6LKDH2UJ.mjs';
11
+ import '../../chunk-7KWRVIYV.mjs';
13
12
  import '../../chunk-LNAIMEB2.mjs';
14
13
  import '../../chunk-OELUUJZY.mjs';
15
- import '../../chunk-24IOWJS4.mjs';
14
+ import '../../chunk-THWPPG6M.mjs';
15
+ import '../../chunk-E2UEE45I.mjs';
16
16
  import '../../chunk-UW3ITU2Y.mjs';
17
17
  import '../../chunk-KGKLUWKW.mjs';
18
18
  import '../../chunk-6SYD77G6.mjs';
@@ -1,7 +1,8 @@
1
- export { BaseCopilotTextarea, BaseCopilotTextareaProps } from './base-copilot-textarea/base-copilot-textarea.js';
1
+ export { BaseCopilotTextarea } from './base-copilot-textarea/base-copilot-textarea.js';
2
2
  export { CopilotTextarea, CopilotTextareaProps } from './copilot-textarea/copilot-textarea.js';
3
+ import '../types/base/autosuggestions-bare-function.js';
4
+ import '../types/base/base-copilot-textarea-props.js';
3
5
  import 'react';
4
6
  import '../types/base/base-autosuggestions-config.js';
5
- import '../types/base/autosuggestions-bare-function.js';
6
7
  import '../types/standard-autosuggestions/autosuggestions-config.js';
7
8
  import '../types/standard-autosuggestions/minimal-chat-gpt-message.js';
@@ -1,19 +1,19 @@
1
1
  import '../chunk-MMVDU6DF.mjs';
2
- export { CopilotTextarea } from '../chunk-ROH375UB.mjs';
2
+ export { CopilotTextarea } from '../chunk-YTT5NE6B.mjs';
3
3
  import '../chunk-A3SFEHWZ.mjs';
4
4
  import '../chunk-AZQCUNBV.mjs';
5
- import '../chunk-ZPACRCJ6.mjs';
6
- export { BaseCopilotTextarea } from '../chunk-4EA7I44O.mjs';
7
- import '../chunk-THWPPG6M.mjs';
5
+ import '../chunk-EHQ5TN4R.mjs';
6
+ export { BaseCopilotTextarea } from '../chunk-YICIML6J.mjs';
8
7
  import '../chunk-WJYQWL4I.mjs';
9
8
  import '../chunk-4XYYSJ5C.mjs';
10
9
  import '../chunk-TSF4AJIK.mjs';
11
10
  import '../chunk-3UQM3NLM.mjs';
12
11
  import '../chunk-WJHSY5T6.mjs';
13
- import '../chunk-6LKDH2UJ.mjs';
12
+ import '../chunk-7KWRVIYV.mjs';
14
13
  import '../chunk-LNAIMEB2.mjs';
15
14
  import '../chunk-OELUUJZY.mjs';
16
- import '../chunk-24IOWJS4.mjs';
15
+ import '../chunk-THWPPG6M.mjs';
16
+ import '../chunk-E2UEE45I.mjs';
17
17
  import '../chunk-UW3ITU2Y.mjs';
18
18
  import '../chunk-KGKLUWKW.mjs';
19
19
  import '../chunk-6SYD77G6.mjs';
@@ -8,6 +8,6 @@ interface UseAutosuggestionsResult {
8
8
  onChangeHandler: (newEditorState: EditorAutocompleteState | null) => void;
9
9
  onKeyDownHandler: (event: React.KeyboardEvent<HTMLDivElement>) => void;
10
10
  }
11
- declare function useAutosuggestions(debounceTime: number, acceptAutosuggestionKey: string, autosuggestionFunction: AutosuggestionsBareFunction, insertAutocompleteSuggestion: (suggestion: AutosuggestionState) => void, disableWhenEmpty: boolean): UseAutosuggestionsResult;
11
+ declare function useAutosuggestions(debounceTime: number, acceptAutosuggestionKey: string, autosuggestionFunction: AutosuggestionsBareFunction, insertAutocompleteSuggestion: (suggestion: AutosuggestionState) => void, disableWhenEmpty: boolean, disabled: boolean): UseAutosuggestionsResult;
12
12
 
13
13
  export { UseAutosuggestionsResult, useAutosuggestions };
@@ -1,4 +1,4 @@
1
- export { useAutosuggestions } from '../chunk-24IOWJS4.mjs';
1
+ export { useAutosuggestions } from '../chunk-E2UEE45I.mjs';
2
2
  import '../chunk-UW3ITU2Y.mjs';
3
3
  import '../chunk-KGKLUWKW.mjs';
4
4
  import '../chunk-NKW5OU2S.mjs';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export { BaseCopilotTextarea, BaseCopilotTextareaProps } from './components/base-copilot-textarea/base-copilot-textarea.js';
1
+ export { BaseCopilotTextarea } from './components/base-copilot-textarea/base-copilot-textarea.js';
2
2
  export { CopilotTextarea, CopilotTextareaProps } from './components/copilot-textarea/copilot-textarea.js';
3
3
  export { BaseAutosuggestionsConfig, defaultBaseAutosuggestionsConfig } from './types/base/base-autosuggestions-config.js';
4
4
  export { AutosuggestionsBareFunction } from './types/base/autosuggestions-bare-function.js';
5
+ export { BaseCopilotTextareaProps } from './types/base/base-copilot-textarea-props.js';
5
6
  export { AutosuggestionsConfig, MakeSystemPrompt, defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from './types/standard-autosuggestions/autosuggestions-config.js';
6
7
  export { MinimalChatGPTMessage } from './types/standard-autosuggestions/minimal-chat-gpt-message.js';
7
8
  import 'react';
package/dist/index.mjs CHANGED
@@ -1,22 +1,22 @@
1
1
  import './chunk-FD6FGKYY.mjs';
2
2
  import './chunk-MMVDU6DF.mjs';
3
- export { CopilotTextarea } from './chunk-ROH375UB.mjs';
3
+ export { CopilotTextarea } from './chunk-YTT5NE6B.mjs';
4
4
  import './chunk-A3SFEHWZ.mjs';
5
5
  import './chunk-AZQCUNBV.mjs';
6
- export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from './chunk-ZPACRCJ6.mjs';
7
- export { BaseCopilotTextarea } from './chunk-4EA7I44O.mjs';
8
- import './chunk-THWPPG6M.mjs';
6
+ export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from './chunk-EHQ5TN4R.mjs';
7
+ export { BaseCopilotTextarea } from './chunk-YICIML6J.mjs';
9
8
  import './chunk-WJYQWL4I.mjs';
10
9
  import './chunk-4XYYSJ5C.mjs';
11
10
  import './chunk-TSF4AJIK.mjs';
12
11
  import './chunk-3UQM3NLM.mjs';
13
12
  import './chunk-WJHSY5T6.mjs';
14
- export { defaultBaseAutosuggestionsConfig } from './chunk-6LKDH2UJ.mjs';
13
+ export { defaultBaseAutosuggestionsConfig } from './chunk-7KWRVIYV.mjs';
15
14
  import './chunk-LNAIMEB2.mjs';
16
15
  import './chunk-OELUUJZY.mjs';
16
+ import './chunk-THWPPG6M.mjs';
17
17
  import './chunk-IU3WTXLQ.mjs';
18
18
  import './chunk-H4VKQGVU.mjs';
19
- import './chunk-24IOWJS4.mjs';
19
+ import './chunk-E2UEE45I.mjs';
20
20
  import './chunk-UW3ITU2Y.mjs';
21
21
  import './chunk-KGKLUWKW.mjs';
22
22
  import './chunk-6SYD77G6.mjs';
@@ -3,6 +3,7 @@ interface BaseAutosuggestionsConfig {
3
3
  debounceTime: number;
4
4
  acceptAutosuggestionKey: string;
5
5
  disableWhenEmpty: boolean;
6
+ disabled: boolean;
6
7
  }
7
8
  declare const defaultBaseAutosuggestionsConfig: Omit<BaseAutosuggestionsConfig, "purposePrompt">;
8
9
 
@@ -1,4 +1,4 @@
1
- export { defaultBaseAutosuggestionsConfig } from '../../chunk-6LKDH2UJ.mjs';
1
+ export { defaultBaseAutosuggestionsConfig } from '../../chunk-7KWRVIYV.mjs';
2
2
  import '../../chunk-MRXNTQOX.mjs';
3
3
  //# sourceMappingURL=out.js.map
4
4
  //# sourceMappingURL=base-autosuggestions-config.mjs.map
@@ -0,0 +1,38 @@
1
+ import { TextareaHTMLAttributes } from 'react';
2
+ import { BaseAutosuggestionsConfig } from './base-autosuggestions-config.js';
3
+
4
+ /**
5
+ * `BaseCopilotTextareaProps` defines the properties for the `BaseCopilotTextarea` component.
6
+ *
7
+ * @extends {Omit<TextareaHTMLAttributes<HTMLDivElement>, "onChange">}
8
+ *
9
+ * @property {boolean} [disableBranding=false] - Determines whether branding should be disabled. Default is `false`.
10
+ *
11
+ * @property {React.CSSProperties} [placeholderStyle] - Specifies the CSS styles to apply to the placeholder text.
12
+ *
13
+ * @property {React.CSSProperties} [suggestionsStyle] - Specifies the CSS styles to apply to the suggestions list.
14
+ *
15
+ * @property {string} [value] - The initial value of the textarea. Can be controlled via `onValueChange`.
16
+ *
17
+ * @property {(value: string) => void} [onValueChange] - Callback invoked when the value of the textarea changes.
18
+ *
19
+ * @property {(event: React.ChangeEvent<HTMLTextAreaElement>) => void} [onChange] - Callback invoked when a `change` event is triggered on the textarea element. The event only actually includes the `event.target.value` and `event.currentTarget.value` properties (all that is required in 99% of cases).
20
+ *
21
+ * @property {Partial<BaseAutosuggestionsConfig> & {
22
+ * purposePrompt: string;
23
+ * }} autosuggestionsConfig - Configuration settings for the autosuggestions feature.
24
+ * Includes a mandatory `purposePrompt` to guide the autosuggestions.
25
+ */
26
+ interface BaseCopilotTextareaProps extends Omit<TextareaHTMLAttributes<HTMLDivElement>, "onChange"> {
27
+ disableBranding?: boolean;
28
+ placeholderStyle?: React.CSSProperties;
29
+ suggestionsStyle?: React.CSSProperties;
30
+ value?: string;
31
+ onValueChange?: (value: string) => void;
32
+ onChange?: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
33
+ autosuggestionsConfig: Partial<BaseAutosuggestionsConfig> & {
34
+ purposePrompt: string;
35
+ };
36
+ }
37
+
38
+ export { BaseCopilotTextareaProps };
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=out.js.map
3
+ //# sourceMappingURL=base-copilot-textarea-props.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,2 +1,4 @@
1
1
  export { BaseAutosuggestionsConfig, defaultBaseAutosuggestionsConfig } from './base-autosuggestions-config.js';
2
2
  export { AutosuggestionsBareFunction } from './autosuggestions-bare-function.js';
3
+ export { BaseCopilotTextareaProps } from './base-copilot-textarea-props.js';
4
+ import 'react';
@@ -1,5 +1,5 @@
1
1
  import '../../chunk-WJHSY5T6.mjs';
2
- export { defaultBaseAutosuggestionsConfig } from '../../chunk-6LKDH2UJ.mjs';
2
+ export { defaultBaseAutosuggestionsConfig } from '../../chunk-7KWRVIYV.mjs';
3
3
  import '../../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=index.mjs.map
@@ -1,4 +1,6 @@
1
1
  export { BaseAutosuggestionsConfig, defaultBaseAutosuggestionsConfig } from './base/base-autosuggestions-config.js';
2
2
  export { AutosuggestionsBareFunction } from './base/autosuggestions-bare-function.js';
3
+ export { BaseCopilotTextareaProps } from './base/base-copilot-textarea-props.js';
3
4
  export { AutosuggestionsConfig, MakeSystemPrompt, defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from './standard-autosuggestions/autosuggestions-config.js';
4
5
  export { MinimalChatGPTMessage } from './standard-autosuggestions/minimal-chat-gpt-message.js';
6
+ import 'react';
@@ -1,8 +1,8 @@
1
1
  import '../chunk-FD6FGKYY.mjs';
2
2
  import '../chunk-AZQCUNBV.mjs';
3
- export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from '../chunk-ZPACRCJ6.mjs';
3
+ export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from '../chunk-EHQ5TN4R.mjs';
4
4
  import '../chunk-WJHSY5T6.mjs';
5
- export { defaultBaseAutosuggestionsConfig } from '../chunk-6LKDH2UJ.mjs';
5
+ export { defaultBaseAutosuggestionsConfig } from '../chunk-7KWRVIYV.mjs';
6
6
  import '../chunk-MRXNTQOX.mjs';
7
7
  //# sourceMappingURL=out.js.map
8
8
  //# sourceMappingURL=index.mjs.map
@@ -1,6 +1,6 @@
1
- export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from '../../chunk-ZPACRCJ6.mjs';
1
+ export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from '../../chunk-EHQ5TN4R.mjs';
2
2
  import '../../chunk-WJHSY5T6.mjs';
3
- import '../../chunk-6LKDH2UJ.mjs';
3
+ import '../../chunk-7KWRVIYV.mjs';
4
4
  import '../../chunk-MRXNTQOX.mjs';
5
5
  //# sourceMappingURL=out.js.map
6
6
  //# sourceMappingURL=autosuggestions-config.mjs.map
@@ -1,7 +1,7 @@
1
1
  import '../../chunk-AZQCUNBV.mjs';
2
- export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from '../../chunk-ZPACRCJ6.mjs';
2
+ export { defaultAutosuggestionsConfig, defaultFewShotMessages, defaultMakeSystemPrompt } from '../../chunk-EHQ5TN4R.mjs';
3
3
  import '../../chunk-WJHSY5T6.mjs';
4
- import '../../chunk-6LKDH2UJ.mjs';
4
+ import '../../chunk-7KWRVIYV.mjs';
5
5
  import '../../chunk-MRXNTQOX.mjs';
6
6
  //# sourceMappingURL=out.js.map
7
7
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0",
7
+ "version": "0.11.0",
8
8
  "sideEffects": [
9
9
  "**/*.css"
10
10
  ],
@@ -31,8 +31,8 @@
31
31
  "tsup": "^6.1.3",
32
32
  "typescript": "^4.9.4",
33
33
  "eslint-config-custom": "0.2.0",
34
- "tsconfig": "0.3.0",
35
- "tailwind-config": "0.1.0"
34
+ "tailwind-config": "0.1.0",
35
+ "tsconfig": "0.3.0"
36
36
  },
37
37
  "dependencies": {
38
38
  "ai": "^2.1.22",
@@ -1,11 +1,4 @@
1
- import "./base-copilot-textarea.css";
2
- import {
3
- TextareaHTMLAttributes,
4
- useCallback,
5
- useEffect,
6
- useMemo,
7
- useState,
8
- } from "react";
1
+ import { useCallback, useEffect, useMemo, useState } from "react";
9
2
  import { Descendant, Editor } from "slate";
10
3
  import { Editable, Slate } from "slate-react";
11
4
  import { twMerge } from "tailwind-merge";
@@ -24,22 +17,12 @@ import {
24
17
  defaultBaseAutosuggestionsConfig,
25
18
  } from "../../types/base";
26
19
  import { AutosuggestionState } from "../../types/base/autosuggestion-state";
20
+ import { BaseCopilotTextareaProps } from "../../types/base/base-copilot-textarea-props";
21
+ import "./base-copilot-textarea.css";
27
22
  import { makeRenderElementFunction } from "./render-element";
28
23
  import { makeRenderPlaceholderFunction } from "./render-placeholder";
29
24
  import { useAddBrandingCss } from "./use-add-branding-css";
30
25
 
31
- export interface BaseCopilotTextareaProps
32
- extends TextareaHTMLAttributes<HTMLDivElement> {
33
- disableBranding?: boolean;
34
- placeholderStyle?: React.CSSProperties;
35
- suggestionsStyle?: React.CSSProperties;
36
- value?: string;
37
- onValueChange?: (value: string) => void;
38
- autosuggestionsConfig: Partial<BaseAutosuggestionsConfig> & {
39
- purposePrompt: string;
40
- };
41
- }
42
-
43
26
  export function BaseCopilotTextarea(
44
27
  props: BaseCopilotTextareaProps & {
45
28
  autosuggestionsFunction: AutosuggestionsBareFunction;
@@ -83,7 +66,8 @@ export function BaseCopilotTextarea(
83
66
  autosuggestionsConfig.acceptAutosuggestionKey,
84
67
  props.autosuggestionsFunction,
85
68
  insertText,
86
- autosuggestionsConfig.disableWhenEmpty
69
+ autosuggestionsConfig.disableWhenEmpty,
70
+ autosuggestionsConfig.disabled
87
71
  );
88
72
 
89
73
  // sync autosuggestions state with the editor
@@ -144,6 +128,8 @@ export function BaseCopilotTextarea(
144
128
  autosuggestionsConfig: autosuggestionsConfigFromProps,
145
129
  autosuggestionsFunction,
146
130
  className,
131
+ onChange,
132
+ onKeyDown,
147
133
  ...propsToForward
148
134
  } = props;
149
135
 
@@ -171,16 +157,47 @@ export function BaseCopilotTextarea(
171
157
 
172
158
  setLastKnownFullEditorText(fullEditorText);
173
159
  onChangeHandlerForAutocomplete(newEditorState);
160
+
174
161
  props.onValueChange?.(fullEditorText);
162
+ props.onChange?.(makeSemiFakeReactTextAreaEvent(fullEditorText));
175
163
  }}
176
164
  >
177
165
  <Editable
178
166
  renderElement={renderElementMemoized}
179
167
  renderPlaceholder={renderPlaceholderMemoized}
180
- onKeyDown={onKeyDownHandlerForAutocomplete}
168
+ onKeyDown={(event) => {
169
+ onKeyDownHandlerForAutocomplete(event); // forward the event for internal use
170
+ props.onKeyDown?.(event); // forward the event for external use
171
+ }}
181
172
  className={moddedClassName}
182
173
  {...propsToForward}
183
174
  />
184
175
  </Slate>
185
176
  );
186
177
  }
178
+
179
+ // Consumers of <textarea> expect a `onChange: (React.ChangeEvent<HTMLTextAreaElement>) => void` event handler to be passed in.
180
+ // This is *extremely* common, and we want to support it.
181
+ //
182
+ // We can't support the full functionality, but in 99% of cases, the consumer only cares about the `event.target.value` property --
183
+ // that's how they get the new value of the textarea.
184
+ //
185
+ // So, the tradeoff we are making is minimizing compiler complaint, with a small chance of runtime error.
186
+ // The alternative would be defining a different onChange entrypoint (we actually do have that in `onValueChange`),
187
+ // And starting to explain subtleties to users the moment they try to use the component for the first time for very basic functionality.
188
+ //
189
+ // If this proves problematic, we can always revisit this decision.
190
+ function makeSemiFakeReactTextAreaEvent(
191
+ currentText: string
192
+ ): React.ChangeEvent<HTMLTextAreaElement> {
193
+ return {
194
+ target: {
195
+ value: currentText,
196
+ type: "copilot-textarea",
197
+ },
198
+ currentTarget: {
199
+ value: currentText,
200
+ type: "copilot-textarea",
201
+ },
202
+ } as React.ChangeEvent<HTMLTextAreaElement>;
203
+ }
@@ -1,13 +1,11 @@
1
1
  // This example is for an Editor with `ReactEditor` and `HistoryEditor`
2
2
  import { useMakeStandardAutosuggestionFunction } from "../../hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function";
3
+ import { BaseCopilotTextareaProps } from "../../types/base/base-copilot-textarea-props";
3
4
  import {
4
5
  AutosuggestionsConfig,
5
6
  defaultAutosuggestionsConfig,
6
7
  } from "../../types/standard-autosuggestions";
7
- import {
8
- BaseCopilotTextarea,
9
- BaseCopilotTextareaProps,
10
- } from "../base-copilot-textarea/base-copilot-textarea";
8
+ import { BaseCopilotTextarea } from "../base-copilot-textarea/base-copilot-textarea";
11
9
 
12
10
  export interface CopilotTextareaProps extends BaseCopilotTextareaProps {
13
11
  autosuggestionsConfig: Partial<AutosuggestionsConfig> & {
@@ -1,5 +1,4 @@
1
1
  export { BaseCopilotTextarea } from "./base-copilot-textarea/base-copilot-textarea";
2
- export type { BaseCopilotTextareaProps } from "./base-copilot-textarea/base-copilot-textarea";
3
2
 
4
3
  export { CopilotTextarea } from "./copilot-textarea/copilot-textarea";
5
4
  export type { CopilotTextareaProps } from "./copilot-textarea/copilot-textarea";
@@ -19,7 +19,8 @@ export function useAutosuggestions(
19
19
  acceptAutosuggestionKey: string,
20
20
  autosuggestionFunction: AutosuggestionsBareFunction,
21
21
  insertAutocompleteSuggestion: (suggestion: AutosuggestionState) => void,
22
- disableWhenEmpty: boolean
22
+ disableWhenEmpty: boolean,
23
+ disabled: boolean
23
24
  ): UseAutosuggestionsResult {
24
25
  const [previousAutocompleteState, setPreviousAutocompleteState] =
25
26
  useState<EditorAutocompleteState | null>(null);
@@ -35,6 +36,11 @@ export function useAutosuggestions(
35
36
  editorAutocompleteState: EditorAutocompleteState,
36
37
  abortSignal: AbortSignal
37
38
  ) => {
39
+ // early return if disabled
40
+ if (disabled) {
41
+ return;
42
+ }
43
+
38
44
  if (
39
45
  disableWhenEmpty &&
40
46
  editorAutocompleteState.textBeforeCursor === "" &&
@@ -43,6 +49,7 @@ export function useAutosuggestions(
43
49
  return;
44
50
  }
45
51
 
52
+ // fetch the suggestion
46
53
  const suggestion = await autosuggestionFunction(
47
54
  editorAutocompleteState.textBeforeCursor,
48
55
  editorAutocompleteState.textAfterCursor,
@@ -59,7 +66,12 @@ export function useAutosuggestions(
59
66
  point: editorAutocompleteState.cursorPoint,
60
67
  });
61
68
  },
62
- [autosuggestionFunction, setCurrentAutocompleteSuggestion, disableWhenEmpty]
69
+ [
70
+ autosuggestionFunction,
71
+ setCurrentAutocompleteSuggestion,
72
+ disableWhenEmpty,
73
+ disabled,
74
+ ]
63
75
  );
64
76
 
65
77
  const debouncedFunction = useMemo(
@@ -3,6 +3,7 @@ export interface BaseAutosuggestionsConfig {
3
3
  debounceTime: number;
4
4
  acceptAutosuggestionKey: string;
5
5
  disableWhenEmpty: boolean;
6
+ disabled: boolean;
6
7
  }
7
8
 
8
9
  export const defaultBaseAutosuggestionsConfig: Omit<
@@ -12,4 +13,5 @@ export const defaultBaseAutosuggestionsConfig: Omit<
12
13
  debounceTime: 500,
13
14
  acceptAutosuggestionKey: "Tab",
14
15
  disableWhenEmpty: true,
16
+ disabled: false,
15
17
  };
@@ -0,0 +1,37 @@
1
+ import { TextareaHTMLAttributes } from "react";
2
+ import { BaseAutosuggestionsConfig } from ".";
3
+
4
+ /**
5
+ * `BaseCopilotTextareaProps` defines the properties for the `BaseCopilotTextarea` component.
6
+ *
7
+ * @extends {Omit<TextareaHTMLAttributes<HTMLDivElement>, "onChange">}
8
+ *
9
+ * @property {boolean} [disableBranding=false] - Determines whether branding should be disabled. Default is `false`.
10
+ *
11
+ * @property {React.CSSProperties} [placeholderStyle] - Specifies the CSS styles to apply to the placeholder text.
12
+ *
13
+ * @property {React.CSSProperties} [suggestionsStyle] - Specifies the CSS styles to apply to the suggestions list.
14
+ *
15
+ * @property {string} [value] - The initial value of the textarea. Can be controlled via `onValueChange`.
16
+ *
17
+ * @property {(value: string) => void} [onValueChange] - Callback invoked when the value of the textarea changes.
18
+ *
19
+ * @property {(event: React.ChangeEvent<HTMLTextAreaElement>) => void} [onChange] - Callback invoked when a `change` event is triggered on the textarea element. The event only actually includes the `event.target.value` and `event.currentTarget.value` properties (all that is required in 99% of cases).
20
+ *
21
+ * @property {Partial<BaseAutosuggestionsConfig> & {
22
+ * purposePrompt: string;
23
+ * }} autosuggestionsConfig - Configuration settings for the autosuggestions feature.
24
+ * Includes a mandatory `purposePrompt` to guide the autosuggestions.
25
+ */
26
+ export interface BaseCopilotTextareaProps
27
+ extends Omit<TextareaHTMLAttributes<HTMLDivElement>, "onChange"> {
28
+ disableBranding?: boolean;
29
+ placeholderStyle?: React.CSSProperties;
30
+ suggestionsStyle?: React.CSSProperties;
31
+ value?: string;
32
+ onValueChange?: (value: string) => void;
33
+ onChange?: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
34
+ autosuggestionsConfig: Partial<BaseAutosuggestionsConfig> & {
35
+ purposePrompt: string;
36
+ };
37
+ }
@@ -1,4 +1,6 @@
1
1
  export { defaultBaseAutosuggestionsConfig } from "./base-autosuggestions-config";
2
2
 
3
- export type { BaseAutosuggestionsConfig } from "./base-autosuggestions-config";
4
3
  export type { AutosuggestionsBareFunction } from "./autosuggestions-bare-function";
4
+ export type { BaseAutosuggestionsConfig } from "./base-autosuggestions-config";
5
+
6
+ export type { BaseCopilotTextareaProps } from "./base-copilot-textarea-props";
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-autosuggestions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,aAAa,SAAS,gBAAgB;AAgBxC,SAAS,mBACd,cACA,yBACA,wBACA,8BACA,kBAC0B;AAC1B,QAAM,CAAC,2BAA2B,4BAA4B,IAC5D,SAAyC,IAAI;AAE/C,QAAM,CAAC,+BAA+B,gCAAgC,IACpE,SAAqC,IAAI;AAE3C,QAAM,8BAGe;AAAA,IACnB,CACE,yBACA,gBACG;AACH,UACE,oBACA,wBAAwB,qBAAqB,MAC7C,wBAAwB,oBAAoB,IAC5C;AACA;AAAA,MACF;AAEA,YAAM,aAAa,MAAM;AAAA,QACvB,wBAAwB;AAAA,QACxB,wBAAwB;AAAA,QACxB;AAAA,MACF;AAGA,UAAI,CAAC,cAAc,YAAY,SAAS;AACtC,cAAM,IAAI,aAAa,WAAW,YAAY;AAAA,MAChD;AAEA,uCAAiC;AAAA,QAC/B,MAAM;AAAA,QACN,OAAO,wBAAwB;AAAA,MACjC,CAAC;AAAA,IACH;AAAA,IACA,CAAC,wBAAwB,kCAAkC,gBAAgB;AAAA,EAC7E;AAEA,QAAM,oBAAoB;AAAA,IACxB,MACE,IAAI;AAAA,MACF;AAAA,IACF;AAAA,IACF,CAAC,YAAY;AAAA,EACf;AAEA,QAAM,WAAW;AAAA,IACf,CAAC,mBAAmD;AAClD,YAAM,wBAAwB,CAAC;AAAA,QAC7B;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACA,mCAA6B,cAAc;AAG3C,UAAI,CAAC,uBAAuB;AAC1B;AAAA,MACF;AAGA,uCAAiC,IAAI;AAGrC,UAAI,gBAAgB;AAClB,0BAAkB,SAAS,6BAA6B,cAAc;AAAA,MACxE,OAAO;AACL,0BAAkB,OAAO;AAAA,MAC3B;AAAA,IACF;AAAA,IACA;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,iBAAiB;AAAA,IACrB,CAAC,UAA+C;AAC9C,UAAI,+BAA+B;AACjC,YAAI,MAAM,QAAQ,yBAAyB;AACzC,gBAAM,eAAe;AACrB,uCAA6B,6BAA6B;AAC1D,2CAAiC,IAAI;AAAA,QACvC;AAAA,MACF;AAAA,IACF;AAAA,IACA;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,iBAAiB;AAAA,IACjB,kBAAkB;AAAA,EACpB;AACF","sourcesContent":["import { useCallback, useMemo, useState } from \"react\";\nimport { Debouncer } from \"../lib/debouncer\";\nimport { nullableCompatibleEqualityCheck } from \"../lib/utils\";\nimport { AutosuggestionsBareFunction } from \"../types/base\";\nimport { AutosuggestionState } from \"../types/base/autosuggestion-state\";\nimport {\n EditorAutocompleteState,\n areEqual_autocompleteState,\n} from \"../types/base/editor-autocomplete-state\";\n\nexport interface UseAutosuggestionsResult {\n currentAutocompleteSuggestion: AutosuggestionState | null;\n onChangeHandler: (newEditorState: EditorAutocompleteState | null) => void;\n onKeyDownHandler: (event: React.KeyboardEvent<HTMLDivElement>) => void;\n}\n\nexport function useAutosuggestions(\n debounceTime: number,\n acceptAutosuggestionKey: string,\n autosuggestionFunction: AutosuggestionsBareFunction,\n insertAutocompleteSuggestion: (suggestion: AutosuggestionState) => void,\n disableWhenEmpty: boolean\n): UseAutosuggestionsResult {\n const [previousAutocompleteState, setPreviousAutocompleteState] =\n useState<EditorAutocompleteState | null>(null);\n\n const [currentAutocompleteSuggestion, setCurrentAutocompleteSuggestion] =\n useState<AutosuggestionState | null>(null);\n\n const awaitForAndAppendSuggestion: (\n editorAutocompleteState: EditorAutocompleteState,\n abortSignal: AbortSignal\n ) => Promise<void> = useCallback(\n async (\n editorAutocompleteState: EditorAutocompleteState,\n abortSignal: AbortSignal\n ) => {\n if (\n disableWhenEmpty &&\n editorAutocompleteState.textBeforeCursor === \"\" &&\n editorAutocompleteState.textAfterCursor === \"\"\n ) {\n return;\n }\n\n const suggestion = await autosuggestionFunction(\n editorAutocompleteState.textBeforeCursor,\n editorAutocompleteState.textAfterCursor,\n abortSignal\n );\n\n // We'll assume for now that the autocomplete function might or might not respect the abort signal.\n if (!suggestion || abortSignal.aborted) {\n throw new DOMException(\"Aborted\", \"AbortError\");\n }\n\n setCurrentAutocompleteSuggestion({\n text: suggestion,\n point: editorAutocompleteState.cursorPoint,\n });\n },\n [autosuggestionFunction, setCurrentAutocompleteSuggestion, disableWhenEmpty]\n );\n\n const debouncedFunction = useMemo(\n () =>\n new Debouncer<[editorAutocompleteState: EditorAutocompleteState]>(\n debounceTime\n ),\n [debounceTime]\n );\n\n const onChange = useCallback(\n (newEditorState: EditorAutocompleteState | null) => {\n const editorStateHasChanged = !nullableCompatibleEqualityCheck(\n areEqual_autocompleteState,\n previousAutocompleteState,\n newEditorState\n );\n setPreviousAutocompleteState(newEditorState);\n\n // if no change, do nothing\n if (!editorStateHasChanged) {\n return;\n }\n\n // if change, then first null out the current suggestion\n setCurrentAutocompleteSuggestion(null);\n\n // then try to get a new suggestion, debouncing to avoid too many requests while typing\n if (newEditorState) {\n debouncedFunction.debounce(awaitForAndAppendSuggestion, newEditorState);\n } else {\n debouncedFunction.cancel();\n }\n },\n [\n previousAutocompleteState,\n setPreviousAutocompleteState,\n debouncedFunction,\n awaitForAndAppendSuggestion,\n setCurrentAutocompleteSuggestion,\n ]\n );\n\n const keyDownHandler = useCallback(\n (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (currentAutocompleteSuggestion) {\n if (event.key === acceptAutosuggestionKey) {\n event.preventDefault();\n insertAutocompleteSuggestion(currentAutocompleteSuggestion);\n setCurrentAutocompleteSuggestion(null);\n }\n }\n },\n [\n currentAutocompleteSuggestion,\n setCurrentAutocompleteSuggestion,\n insertAutocompleteSuggestion,\n acceptAutosuggestionKey,\n ]\n );\n\n return {\n currentAutocompleteSuggestion,\n onChangeHandler: onChange,\n onKeyDownHandler: keyDownHandler,\n };\n}\n"]}