|
| 1 | +import type { ExtendConfig } from '@udecode/plate-core'; |
| 2 | +import type { AriakitTypes } from '@udecode/plate-menu'; |
| 3 | +import type { NodeEntry, Path } from 'slate'; |
| 4 | + |
| 5 | +import { |
| 6 | + type PlateEditor, |
| 7 | + toDOMNode, |
| 8 | + toTPlatePlugin, |
| 9 | +} from '@udecode/plate-common/react'; |
| 10 | + |
| 11 | +import { type BaseAIPluginConfig, BaseAIPlugin } from '../../lib'; |
| 12 | +import { useAIHooks } from './useAIHook'; |
| 13 | + |
| 14 | +export const KEY_AI = 'ai'; |
| 15 | + |
| 16 | +export interface FetchAISuggestionProps { |
| 17 | + abortSignal: AbortController; |
| 18 | + prompt: string; |
| 19 | + system?: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface ExposeOptions { |
| 23 | + createAIEditor: () => PlateEditor; |
| 24 | + scrollContainerSelector: string; |
| 25 | + fetchStream?: (props: FetchAISuggestionProps) => Promise<ReadableStream>; |
| 26 | + trigger?: RegExp | string[] | string; |
| 27 | + |
| 28 | + triggerPreviousCharPattern?: RegExp; |
| 29 | +} |
| 30 | + |
| 31 | +export type AISelectors = { |
| 32 | + isOpen: (editorId: string) => boolean; |
| 33 | +}; |
| 34 | + |
| 35 | +export type AIApi = { |
| 36 | + abort: () => void; |
| 37 | + clearLast: () => void; |
| 38 | + focusMenu: () => void; |
| 39 | + hide: () => void; |
| 40 | + setAnchorElement: (dom: HTMLElement) => void; |
| 41 | + show: (editorId: string, dom: HTMLElement, nodeEntry: NodeEntry) => void; |
| 42 | +}; |
| 43 | + |
| 44 | +export type AIActionGroup = { |
| 45 | + group?: string; |
| 46 | + value?: string; |
| 47 | +}; |
| 48 | + |
| 49 | +export type AIPluginConfig = ExtendConfig< |
| 50 | + BaseAIPluginConfig, |
| 51 | + { |
| 52 | + abortController: AbortController | null; |
| 53 | + action: AIActionGroup | null; |
| 54 | + aiEditor: PlateEditor | null; |
| 55 | + aiState: 'done' | 'generating' | 'idle' | 'requesting'; |
| 56 | + anchorDom: HTMLElement | null; |
| 57 | + curNodeEntry: NodeEntry | null; |
| 58 | + initNodeEntry: NodeEntry | null; |
| 59 | + lastGenerate: string | null; |
| 60 | + lastPrompt: string | null; |
| 61 | + lastWorkPath: Path | null; |
| 62 | + menuType: 'cursor' | 'selection' | null; |
| 63 | + openEditorId: string | null; |
| 64 | + store: AriakitTypes.MenuStore | null; |
| 65 | + } & ExposeOptions & |
| 66 | + AIApi & |
| 67 | + AISelectors, |
| 68 | + { |
| 69 | + ai: AIApi; |
| 70 | + } |
| 71 | +>; |
| 72 | + |
| 73 | +export const AIPlugin = toTPlatePlugin<AIPluginConfig>(BaseAIPlugin, { |
| 74 | + options: { |
| 75 | + abortController: null, |
| 76 | + action: null, |
| 77 | + aiEditor: null, |
| 78 | + aiState: 'idle', |
| 79 | + anchorDom: null, |
| 80 | + curNodeEntry: null, |
| 81 | + initNodeEntry: null, |
| 82 | + lastGenerate: null, |
| 83 | + lastPrompt: null, |
| 84 | + lastWorkPath: null, |
| 85 | + menuType: null, |
| 86 | + openEditorId: null, |
| 87 | + store: null, |
| 88 | + }, |
| 89 | +}) |
| 90 | + .extendOptions<AISelectors>(({ getOptions }) => ({ |
| 91 | + isOpen: (editorId: string) => { |
| 92 | + const { openEditorId, store } = getOptions(); |
| 93 | + const anchorElement = store?.getState().anchorElement; |
| 94 | + const isAnchor = !!anchorElement && document.contains(anchorElement); |
| 95 | + |
| 96 | + return !!editorId && openEditorId === editorId && isAnchor; |
| 97 | + }, |
| 98 | + })) |
| 99 | + .extendApi< |
| 100 | + Required<Pick<AIApi, 'clearLast' | 'focusMenu' | 'setAnchorElement'>> |
| 101 | + >(({ getOptions, setOptions }) => ({ |
| 102 | + clearLast: () => { |
| 103 | + setOptions({ |
| 104 | + lastGenerate: null, |
| 105 | + lastPrompt: null, |
| 106 | + lastWorkPath: null, |
| 107 | + }); |
| 108 | + }, |
| 109 | + focusMenu: () => { |
| 110 | + const { store } = getOptions(); |
| 111 | + |
| 112 | + setTimeout(() => { |
| 113 | + const searchInput = document.querySelector( |
| 114 | + '#__potion_ai_menu_searchRef' |
| 115 | + ) as HTMLInputElement; |
| 116 | + |
| 117 | + if (store) { |
| 118 | + store.setAutoFocusOnShow(true); |
| 119 | + store.setInitialFocus('first'); |
| 120 | + searchInput?.focus(); |
| 121 | + } |
| 122 | + }, 0); |
| 123 | + }, |
| 124 | + setAnchorElement: (dom: HTMLElement) => { |
| 125 | + const { store } = getOptions(); |
| 126 | + |
| 127 | + if (store) { |
| 128 | + store.setAnchorElement(dom); |
| 129 | + } |
| 130 | + }, |
| 131 | + })) |
| 132 | + .extendApi<Required<Pick<AIApi, 'abort' | 'hide' | 'show'>>>( |
| 133 | + ({ api, getOptions, setOption }) => ({ |
| 134 | + abort: () => { |
| 135 | + const { abortController } = getOptions(); |
| 136 | + |
| 137 | + abortController?.abort(); |
| 138 | + setOption('aiState', 'idle'); |
| 139 | + setTimeout(() => { |
| 140 | + api.ai.focusMenu(); |
| 141 | + }, 0); |
| 142 | + }, |
| 143 | + hide: () => { |
| 144 | + setOption('openEditorId', null); |
| 145 | + getOptions().store?.setAnchorElement(null); |
| 146 | + }, |
| 147 | + show: (editorId: string, dom: HTMLElement, nodeEntry: NodeEntry) => { |
| 148 | + const { store } = getOptions(); |
| 149 | + |
| 150 | + setOption('openEditorId', editorId); |
| 151 | + api.ai.clearLast(); |
| 152 | + setOption('initNodeEntry', nodeEntry); |
| 153 | + api.ai.setAnchorElement(dom); |
| 154 | + store?.show(); |
| 155 | + api.ai.focusMenu(); |
| 156 | + }, |
| 157 | + }) |
| 158 | + ) |
| 159 | + .extend(({ api, getOptions, setOptions }) => ({ |
| 160 | + options: { |
| 161 | + onOpenAI(editor, [node, path]) { |
| 162 | + // NOTE: toDOMNode is dependent on the React make it to an options if want to support other frame. |
| 163 | + const dom = toDOMNode(editor, node); |
| 164 | + |
| 165 | + if (!dom) return; |
| 166 | + |
| 167 | + const { scrollContainerSelector } = getOptions(); |
| 168 | + |
| 169 | + // TODO popup animation |
| 170 | + if (scrollContainerSelector) { |
| 171 | + const scrollContainer = document.querySelector( |
| 172 | + scrollContainerSelector |
| 173 | + ); |
| 174 | + |
| 175 | + if (!scrollContainer) return; |
| 176 | + |
| 177 | + // Make sure when popup in very bottom the menu within the viewport range. |
| 178 | + const rect = dom.getBoundingClientRect(); |
| 179 | + const windowHeight = window.innerHeight; |
| 180 | + const distanceToBottom = windowHeight - rect.bottom; |
| 181 | + |
| 182 | + // 261 is height of the menu. |
| 183 | + if (distanceToBottom < 261) { |
| 184 | + // TODO: scroll animation |
| 185 | + scrollContainer.scrollTop += 261 - distanceToBottom; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + api.ai.show(editor.id, dom, [node, path]); |
| 190 | + setOptions({ |
| 191 | + aiState: 'idle', |
| 192 | + menuType: 'cursor', |
| 193 | + }); |
| 194 | + }, |
| 195 | + }, |
| 196 | + useHooks: useAIHooks, |
| 197 | + })); |
0 commit comments