@alpaca-editor/core 1.0.3939 → 1.0.3942
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/components/ActionButton.d.ts +1 -0
- package/dist/components/ActionButton.js +2 -2
- package/dist/components/ActionButton.js.map +1 -1
- package/dist/editor/FieldActionsOverlay.d.ts +1 -0
- package/dist/editor/FieldActionsOverlay.js +2 -11
- package/dist/editor/FieldActionsOverlay.js.map +1 -1
- package/dist/editor/PictureCropper.js +65 -23
- package/dist/editor/PictureCropper.js.map +1 -1
- package/dist/editor/PictureEditor.js +16 -2
- package/dist/editor/PictureEditor.js.map +1 -1
- package/dist/editor/Titlebar.js +1 -1
- package/dist/editor/Titlebar.js.map +1 -1
- package/dist/editor/ai/GhostWriter.js +21 -2
- package/dist/editor/ai/GhostWriter.js.map +1 -1
- package/dist/editor/commands/componentCommands.js +7 -3
- package/dist/editor/commands/componentCommands.js.map +1 -1
- package/dist/editor/media-selector/MediaFolderBrowser.d.ts +5 -0
- package/dist/editor/media-selector/MediaFolderBrowser.js +77 -0
- package/dist/editor/media-selector/MediaFolderBrowser.js.map +1 -0
- package/dist/editor/media-selector/MediaSelector.js +1 -1
- package/dist/editor/media-selector/MediaSelector.js.map +1 -1
- package/dist/editor/media-selector/Thumbnails.js +2 -2
- package/dist/editor/media-selector/index.d.ts +8 -0
- package/dist/editor/media-selector/index.js +9 -0
- package/dist/editor/media-selector/index.js.map +1 -0
- package/dist/editor/menubar/BrowseHistory.js +1 -1
- package/dist/editor/menubar/PageSelector.js +39 -15
- package/dist/editor/menubar/PageSelector.js.map +1 -1
- package/dist/editor/page-editor-chrome/useInlineAICompletion.js +37 -11
- package/dist/editor/page-editor-chrome/useInlineAICompletion.js.map +1 -1
- package/dist/editor/reviews/Comment.js +1 -1
- package/dist/editor/reviews/Comment.js.map +1 -1
- package/dist/editor/services/aiService.js +0 -1
- package/dist/editor/services/aiService.js.map +1 -1
- package/dist/editor/sidebar/ComponentTree.js +3 -4
- package/dist/editor/sidebar/ComponentTree.js.map +1 -1
- package/dist/editor/ui/SimpleTabs.d.ts +1 -0
- package/dist/editor/ui/SimpleTabs.js +3 -3
- package/dist/editor/ui/SimpleTabs.js.map +1 -1
- package/dist/editor/ui/Splitter.js +61 -6
- package/dist/editor/ui/Splitter.js.map +1 -1
- package/dist/editor/views/MediaFolderEditView.d.ts +4 -0
- package/dist/editor/views/MediaFolderEditView.js +40 -0
- package/dist/editor/views/MediaFolderEditView.js.map +1 -0
- package/dist/editor/views/SingleEditView.js +9 -1
- package/dist/editor/views/SingleEditView.js.map +1 -1
- package/dist/revision.d.ts +2 -2
- package/dist/revision.js +2 -2
- package/dist/styles.css +61 -5
- package/package.json +8 -2
- package/.prettierrc +0 -3
- package/build.css +0 -3
- package/components.json +0 -21
- package/eslint.config.mjs +0 -4
- package/images/bg-shape-black.webp +0 -0
- package/images/wizard-bg.png +0 -0
- package/images/wizard-tour.png +0 -0
- package/images/wizard.png +0 -0
- package/src/client-components/api.ts +0 -6
- package/src/client-components/index.ts +0 -19
- package/src/components/ActionButton.tsx +0 -41
- package/src/components/Error.tsx +0 -57
- package/src/components/ui/CardConnector.tsx +0 -56
- package/src/components/ui/button.tsx +0 -62
- package/src/components/ui/card.tsx +0 -372
- package/src/components/ui/context-menu.tsx +0 -250
- package/src/config/config.tsx +0 -917
- package/src/config/types.ts +0 -286
- package/src/editor/ComponentInfo.tsx +0 -90
- package/src/editor/ConfirmationDialog.tsx +0 -103
- package/src/editor/ContentTree.tsx +0 -730
- package/src/editor/ContextMenu.tsx +0 -230
- package/src/editor/Editor.tsx +0 -90
- package/src/editor/EditorWarning.tsx +0 -34
- package/src/editor/EditorWarnings.tsx +0 -33
- package/src/editor/FieldActionsOverlay.tsx +0 -307
- package/src/editor/FieldEditorPopup.tsx +0 -65
- package/src/editor/FieldHistory.tsx +0 -75
- package/src/editor/FieldList.tsx +0 -190
- package/src/editor/FieldListField.tsx +0 -391
- package/src/editor/FieldListFieldWithFallbacks.tsx +0 -217
- package/src/editor/FloatingToolbar.tsx +0 -163
- package/src/editor/ImageEditor.tsx +0 -128
- package/src/editor/ItemInfo.tsx +0 -90
- package/src/editor/LinkEditorDialog.tsx +0 -196
- package/src/editor/MainLayout.tsx +0 -95
- package/src/editor/MobileLayout.tsx +0 -68
- package/src/editor/NewEditorClient.tsx +0 -11
- package/src/editor/PictureCropper.tsx +0 -503
- package/src/editor/PictureEditor.tsx +0 -277
- package/src/editor/PictureEditorDialog.tsx +0 -381
- package/src/editor/PublishDialog.ignore +0 -74
- package/src/editor/ScrollingContentTree.tsx +0 -67
- package/src/editor/Terminal.tsx +0 -227
- package/src/editor/Titlebar.tsx +0 -104
- package/src/editor/ai/AiPopup.tsx +0 -59
- package/src/editor/ai/AiResponseMessage.tsx +0 -106
- package/src/editor/ai/AiTerminal.tsx +0 -503
- package/src/editor/ai/AiToolCall.tsx +0 -61
- package/src/editor/ai/EditorAiTerminal.tsx +0 -20
- package/src/editor/ai/GhostWriter.tsx +0 -432
- package/src/editor/ai/aiPageModel.ts +0 -108
- package/src/editor/ai/editorAiContext.ts +0 -18
- package/src/editor/client/AboutDialog.tsx +0 -44
- package/src/editor/client/EditorClient.tsx +0 -2241
- package/src/editor/client/GenericDialog.tsx +0 -50
- package/src/editor/client/editContext.ts +0 -416
- package/src/editor/client/helpers.ts +0 -44
- package/src/editor/client/itemsRepository.ts +0 -574
- package/src/editor/client/operations.ts +0 -768
- package/src/editor/client/pageModelBuilder.ts +0 -219
- package/src/editor/commands/commands.ts +0 -22
- package/src/editor/commands/componentCommands.tsx +0 -424
- package/src/editor/commands/createVersionCommand.ts +0 -33
- package/src/editor/commands/deleteVersionCommand.ts +0 -71
- package/src/editor/commands/itemCommands.tsx +0 -351
- package/src/editor/commands/localizeItem/LocalizeItemDialog.tsx +0 -201
- package/src/editor/commands/localizeItem/LocalizeItemUtils.ts +0 -27
- package/src/editor/commands/undo.ts +0 -39
- package/src/editor/component-designer/ComponentDesigner.tsx +0 -70
- package/src/editor/component-designer/ComponentDesignerAiTerminal.tsx +0 -11
- package/src/editor/component-designer/ComponentDesignerMenu.tsx +0 -91
- package/src/editor/component-designer/ComponentEditor.tsx +0 -97
- package/src/editor/component-designer/ComponentRenderingCodeEditor.tsx +0 -31
- package/src/editor/component-designer/ComponentRenderingEditor.tsx +0 -104
- package/src/editor/component-designer/ComponentsDropdown.tsx +0 -39
- package/src/editor/component-designer/PlaceholdersEditor.tsx +0 -179
- package/src/editor/component-designer/RenderingsDropdown.tsx +0 -36
- package/src/editor/component-designer/TemplateEditor.tsx +0 -236
- package/src/editor/component-designer/aiContext.ts +0 -23
- package/src/editor/componentTreeHelper.tsx +0 -116
- package/src/editor/context-menu/CopyMoveMenu.tsx +0 -103
- package/src/editor/context-menu/InsertMenu.tsx +0 -347
- package/src/editor/control-center/About.tsx +0 -342
- package/src/editor/control-center/ControlCenterMenu.tsx +0 -76
- package/src/editor/control-center/IndexOverview.tsx +0 -50
- package/src/editor/control-center/IndexSettings.tsx +0 -266
- package/src/editor/control-center/Info.tsx +0 -104
- package/src/editor/control-center/QuotaInfo.tsx +0 -301
- package/src/editor/control-center/Status.tsx +0 -113
- package/src/editor/control-center/WebSocketMessages.tsx +0 -155
- package/src/editor/editor-warnings/ItemLocked.tsx +0 -63
- package/src/editor/editor-warnings/NoLanguageWriteAccess.tsx +0 -22
- package/src/editor/editor-warnings/NoWorkflowWriteAccess.tsx +0 -23
- package/src/editor/editor-warnings/NoWriteAccess.tsx +0 -16
- package/src/editor/editor-warnings/ValidationErrors.tsx +0 -54
- package/src/editor/field-types/AttachmentEditor.tsx +0 -9
- package/src/editor/field-types/CheckboxEditor.tsx +0 -47
- package/src/editor/field-types/DropLinkEditor.tsx +0 -80
- package/src/editor/field-types/DropListEditor.tsx +0 -84
- package/src/editor/field-types/ImageFieldEditor.tsx +0 -65
- package/src/editor/field-types/InternalLinkFieldEditor.tsx +0 -117
- package/src/editor/field-types/LinkFieldEditor.tsx +0 -85
- package/src/editor/field-types/MultiLineText.tsx +0 -82
- package/src/editor/field-types/PictureFieldEditor.tsx +0 -121
- package/src/editor/field-types/RawEditor.tsx +0 -53
- package/src/editor/field-types/ReactQuill.tsx +0 -580
- package/src/editor/field-types/RichTextEditor.tsx +0 -22
- package/src/editor/field-types/RichTextEditorComponent.tsx +0 -127
- package/src/editor/field-types/SingleLineText.tsx +0 -174
- package/src/editor/field-types/TreeListEditor.tsx +0 -261
- package/src/editor/fieldTypes.ts +0 -140
- package/src/editor/media-selector/AiImageSearch.tsx +0 -185
- package/src/editor/media-selector/AiImageSearchPrompt.tsx +0 -94
- package/src/editor/media-selector/MediaSelector.tsx +0 -42
- package/src/editor/media-selector/Preview.tsx +0 -14
- package/src/editor/media-selector/Thumbnails.tsx +0 -48
- package/src/editor/media-selector/TreeSelector.tsx +0 -292
- package/src/editor/media-selector/UploadZone.tsx +0 -137
- package/src/editor/menubar/ActionsMenu.tsx +0 -94
- package/src/editor/menubar/ActiveUsers.tsx +0 -17
- package/src/editor/menubar/ApproveAndPublish.tsx +0 -18
- package/src/editor/menubar/BrowseHistory.tsx +0 -28
- package/src/editor/menubar/ItemLanguageVersion.tsx +0 -76
- package/src/editor/menubar/LanguageSelector.tsx +0 -226
- package/src/editor/menubar/Menu.tsx +0 -83
- package/src/editor/menubar/NavButtons.tsx +0 -74
- package/src/editor/menubar/PageSelector.tsx +0 -166
- package/src/editor/menubar/PageViewerControls.tsx +0 -120
- package/src/editor/menubar/PreviewSecondaryControls.tsx +0 -18
- package/src/editor/menubar/SecondaryControls.tsx +0 -45
- package/src/editor/menubar/Separator.tsx +0 -12
- package/src/editor/menubar/SiteInfo.tsx +0 -53
- package/src/editor/menubar/User.tsx +0 -27
- package/src/editor/menubar/VersionSelector.tsx +0 -142
- package/src/editor/page-editor-chrome/CommentHighlighting.tsx +0 -307
- package/src/editor/page-editor-chrome/CommentHighlightings.tsx +0 -35
- package/src/editor/page-editor-chrome/FieldActionIndicator.tsx +0 -59
- package/src/editor/page-editor-chrome/FieldActionIndicators.tsx +0 -23
- package/src/editor/page-editor-chrome/FieldEditedIndicator.tsx +0 -64
- package/src/editor/page-editor-chrome/FieldEditedIndicators.tsx +0 -35
- package/src/editor/page-editor-chrome/FrameMenu.tsx +0 -338
- package/src/editor/page-editor-chrome/FrameMenus.tsx +0 -48
- package/src/editor/page-editor-chrome/InlineEditor.tsx +0 -765
- package/src/editor/page-editor-chrome/LockedFieldIndicator.tsx +0 -61
- package/src/editor/page-editor-chrome/NoLayout.tsx +0 -36
- package/src/editor/page-editor-chrome/PageEditorChrome.tsx +0 -122
- package/src/editor/page-editor-chrome/PictureEditorOverlay.tsx +0 -161
- package/src/editor/page-editor-chrome/PlaceholderDropZone.tsx +0 -169
- package/src/editor/page-editor-chrome/PlaceholderDropZones.tsx +0 -315
- package/src/editor/page-editor-chrome/SuggestionHighlighting.tsx +0 -300
- package/src/editor/page-editor-chrome/SuggestionHighlightings.tsx +0 -40
- package/src/editor/page-editor-chrome/useInlineAICompletion.tsx +0 -791
- package/src/editor/page-viewer/DeviceToolbar.tsx +0 -70
- package/src/editor/page-viewer/EditorForm.tsx +0 -258
- package/src/editor/page-viewer/MiniMap.tsx +0 -362
- package/src/editor/page-viewer/PageViewer.tsx +0 -169
- package/src/editor/page-viewer/PageViewerFrame.tsx +0 -1022
- package/src/editor/page-viewer/pageModelSkeletonBuilder.ts +0 -412
- package/src/editor/page-viewer/pageViewContext.ts +0 -186
- package/src/editor/pageModel.ts +0 -220
- package/src/editor/picture-shared.tsx +0 -53
- package/src/editor/reviews/Comment.tsx +0 -307
- package/src/editor/reviews/Comments.tsx +0 -125
- package/src/editor/reviews/DiffView.tsx +0 -109
- package/src/editor/reviews/PreviewInfo.tsx +0 -35
- package/src/editor/reviews/Reviews.tsx +0 -280
- package/src/editor/reviews/SuggestedEdit.tsx +0 -316
- package/src/editor/reviews/reviewCommands.tsx +0 -47
- package/src/editor/reviews/useReviews.tsx +0 -70
- package/src/editor/services/aiService.ts +0 -174
- package/src/editor/services/componentDesignerService.ts +0 -151
- package/src/editor/services/contentService.ts +0 -180
- package/src/editor/services/editService.ts +0 -488
- package/src/editor/services/indexService.ts +0 -24
- package/src/editor/services/reviewsService.ts +0 -53
- package/src/editor/services/serviceHelper.ts +0 -95
- package/src/editor/services/suggestedEditsService.ts +0 -39
- package/src/editor/services/systemService.ts +0 -5
- package/src/editor/services/translationService.ts +0 -21
- package/src/editor/services-server/api.ts +0 -150
- package/src/editor/services-server/graphQL.ts +0 -106
- package/src/editor/sidebar/ComponentPalette.tsx +0 -161
- package/src/editor/sidebar/ComponentTree.tsx +0 -548
- package/src/editor/sidebar/ComponentTree2.tsxx +0 -490
- package/src/editor/sidebar/Debug.tsx +0 -111
- package/src/editor/sidebar/DictionaryEditor.tsx +0 -261
- package/src/editor/sidebar/EditHistory.tsx +0 -134
- package/src/editor/sidebar/GraphQL.tsx +0 -164
- package/src/editor/sidebar/Insert.tsx +0 -35
- package/src/editor/sidebar/MainContentTree.tsx +0 -102
- package/src/editor/sidebar/Performance.tsx +0 -53
- package/src/editor/sidebar/Sessions.tsx +0 -35
- package/src/editor/sidebar/Sidebar.tsx +0 -20
- package/src/editor/sidebar/SidebarView.tsx +0 -152
- package/src/editor/sidebar/Translations.tsx +0 -295
- package/src/editor/sidebar/Validation.tsx +0 -102
- package/src/editor/sidebar/ViewSelector.tsx +0 -60
- package/src/editor/sidebar/Workbox.tsx +0 -209
- package/src/editor/ui/CenteredMessage.tsx +0 -7
- package/src/editor/ui/CopyMoveTargetSelectorDialog.tsx +0 -81
- package/src/editor/ui/CopyToClipboardButton.tsx +0 -24
- package/src/editor/ui/DialogButtons.tsx +0 -11
- package/src/editor/ui/Icons.tsx +0 -709
- package/src/editor/ui/ItemList.tsx +0 -76
- package/src/editor/ui/ItemNameDialogNew.tsx +0 -118
- package/src/editor/ui/ItemSearch.tsx +0 -153
- package/src/editor/ui/PerfectTree.tsx +0 -571
- package/src/editor/ui/Section.tsx +0 -35
- package/src/editor/ui/SimpleIconButton.tsx +0 -54
- package/src/editor/ui/SimpleMenu.tsx +0 -40
- package/src/editor/ui/SimpleTable.tsx +0 -60
- package/src/editor/ui/SimpleTabs.tsx +0 -55
- package/src/editor/ui/SimpleToolbar.tsx +0 -7
- package/src/editor/ui/Spinner.tsx +0 -9
- package/src/editor/ui/Splitter.tsx +0 -314
- package/src/editor/ui/StackedPanels.tsx +0 -134
- package/src/editor/ui/Toolbar.tsx +0 -7
- package/src/editor/utils/id-helper.ts +0 -3
- package/src/editor/utils/insertOptions.ts +0 -69
- package/src/editor/utils/itemutils.ts +0 -29
- package/src/editor/utils/useMemoDebug.ts +0 -28
- package/src/editor/utils.ts +0 -486
- package/src/editor/views/CompareView.tsx +0 -245
- package/src/editor/views/EditView.tsx +0 -27
- package/src/editor/views/ItemEditor.tsx +0 -58
- package/src/editor/views/SingleEditView.tsx +0 -46
- package/src/fonts/Geist-Black.woff2 +0 -0
- package/src/fonts/Geist-Bold.woff2 +0 -0
- package/src/fonts/Geist-ExtraBold.woff2 +0 -0
- package/src/fonts/Geist-ExtraLight.woff2 +0 -0
- package/src/fonts/Geist-Light.woff2 +0 -0
- package/src/fonts/Geist-Medium.woff2 +0 -0
- package/src/fonts/Geist-Regular.woff2 +0 -0
- package/src/fonts/Geist-SemiBold.woff2 +0 -0
- package/src/fonts/Geist-Thin.woff2 +0 -0
- package/src/fonts/Geist[wght].woff2 +0 -0
- package/src/fonts/index.ts +0 -10
- package/src/index.ts +0 -23
- package/src/lib/safelist.tsx +0 -16
- package/src/lib/utils.ts +0 -6
- package/src/page-wizard/PageWizard.tsx +0 -139
- package/src/page-wizard/WizardBox.tsx +0 -4
- package/src/page-wizard/WizardBoxConnector.tsx +0 -56
- package/src/page-wizard/WizardSteps.tsx +0 -458
- package/src/page-wizard/service.ts +0 -35
- package/src/page-wizard/startPageWizardCommand.ts +0 -26
- package/src/page-wizard/steps/BuildPageStep.tsx +0 -259
- package/src/page-wizard/steps/CollectStep.tsx +0 -296
- package/src/page-wizard/steps/ComponentTypesSelector.tsx +0 -454
- package/src/page-wizard/steps/Components.tsx +0 -193
- package/src/page-wizard/steps/ContentStep.tsx +0 -890
- package/src/page-wizard/steps/EditButton.tsx +0 -34
- package/src/page-wizard/steps/FieldEditor.tsx +0 -102
- package/src/page-wizard/steps/Generate.tsx +0 -60
- package/src/page-wizard/steps/ImagesStep.tsx +0 -382
- package/src/page-wizard/steps/LayoutStep.tsx +0 -227
- package/src/page-wizard/steps/MetaDataStep.tsx +0 -173
- package/src/page-wizard/steps/SelectStep.tsx +0 -281
- package/src/page-wizard/steps/schema.ts +0 -180
- package/src/page-wizard/steps/usePageCreator.ts +0 -325
- package/src/page-wizard/usePageWizard.ts +0 -79
- package/src/revision.ts +0 -2
- package/src/splash-screen/NewPage.tsx +0 -294
- package/src/splash-screen/OpenPage.tsx +0 -113
- package/src/splash-screen/RecentPages.tsx +0 -123
- package/src/splash-screen/SectionHeadline.tsx +0 -21
- package/src/splash-screen/SplashScreen.tsx +0 -195
- package/src/tour/Tour.tsx +0 -566
- package/src/tour/default-tour.tsx +0 -301
- package/src/tour/preview-tour.tsx +0 -128
- package/src/types.ts +0 -335
- package/styles.css +0 -765
- package/tsconfig.build.json +0 -31
- package/tsconfig.json +0 -14
|
@@ -1,791 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useEffect,
|
|
3
|
-
useCallback,
|
|
4
|
-
useState,
|
|
5
|
-
useMemo,
|
|
6
|
-
useRef,
|
|
7
|
-
MutableRefObject,
|
|
8
|
-
} from "react";
|
|
9
|
-
import { useDebouncedCallback } from "use-debounce";
|
|
10
|
-
import { PageViewContext } from "../page-viewer/pageViewContext";
|
|
11
|
-
import { useEditContext } from "../client/editContext";
|
|
12
|
-
import { executePrompt } from "../services/aiService";
|
|
13
|
-
|
|
14
|
-
export function useInlineAiCompletion({
|
|
15
|
-
pageViewContext,
|
|
16
|
-
cursorSpanId,
|
|
17
|
-
isUpdatingRef,
|
|
18
|
-
}: {
|
|
19
|
-
pageViewContext: PageViewContext;
|
|
20
|
-
cursorSpanId: string;
|
|
21
|
-
isUpdatingRef: MutableRefObject<boolean>;
|
|
22
|
-
}) {
|
|
23
|
-
const editContext = useEditContext();
|
|
24
|
-
const [currentCompletion, setCurrentCompletion] = useState<string | null>(
|
|
25
|
-
null,
|
|
26
|
-
);
|
|
27
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
28
|
-
const abortControllerRef = useRef<AbortController | null>(null);
|
|
29
|
-
const loadingAnimationRef = useRef<number | null>(null);
|
|
30
|
-
|
|
31
|
-
// Clean up hint element on unmount
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
return () => {
|
|
34
|
-
const hintElement = document.getElementById(`${cursorSpanId}-hint`);
|
|
35
|
-
if (hintElement) {
|
|
36
|
-
hintElement.remove();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (loadingAnimationRef.current) {
|
|
40
|
-
cancelAnimationFrame(loadingAnimationRef.current);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}, [cursorSpanId]);
|
|
44
|
-
|
|
45
|
-
const lastCaretPosRef = useRef<{
|
|
46
|
-
node: Node | null;
|
|
47
|
-
offset: number;
|
|
48
|
-
textLength?: number;
|
|
49
|
-
} | null>(null);
|
|
50
|
-
|
|
51
|
-
// Simple function to track caret position without inserting spans
|
|
52
|
-
const positionCursorSpan = () => {
|
|
53
|
-
if (isUpdatingRef.current) return;
|
|
54
|
-
isUpdatingRef.current = true;
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
const iframeWindow =
|
|
58
|
-
pageViewContext.editorIframeRef.current?.contentWindow;
|
|
59
|
-
const iframeDocument = iframeWindow?.document;
|
|
60
|
-
const editableElement = editContext?.inlineEditingFieldElement;
|
|
61
|
-
|
|
62
|
-
if (!iframeWindow || !iframeDocument || !editableElement) return;
|
|
63
|
-
|
|
64
|
-
// Get current selection
|
|
65
|
-
const selection = iframeWindow.getSelection();
|
|
66
|
-
if (!selection || selection.rangeCount === 0) return;
|
|
67
|
-
|
|
68
|
-
const range = selection.getRangeAt(0);
|
|
69
|
-
|
|
70
|
-
// Only proceed if selection is within our editing element
|
|
71
|
-
if (!editableElement.contains(range.commonAncestorContainer)) return;
|
|
72
|
-
|
|
73
|
-
// Save detailed information about the cursor position
|
|
74
|
-
const currentNode = range.startContainer;
|
|
75
|
-
const currentOffset = range.startOffset;
|
|
76
|
-
|
|
77
|
-
// Create a temporary range to get text up to cursor
|
|
78
|
-
const tempRange = document.createRange();
|
|
79
|
-
tempRange.selectNodeContents(editableElement);
|
|
80
|
-
tempRange.setEnd(currentNode, currentOffset);
|
|
81
|
-
const textUpToCursor = tempRange.toString();
|
|
82
|
-
|
|
83
|
-
// Check if cursor position has changed
|
|
84
|
-
const cursorMoved =
|
|
85
|
-
!lastCaretPosRef.current ||
|
|
86
|
-
lastCaretPosRef.current.node !== currentNode ||
|
|
87
|
-
lastCaretPosRef.current.offset !== currentOffset ||
|
|
88
|
-
textUpToCursor.length !== (lastCaretPosRef.current.textLength || 0);
|
|
89
|
-
|
|
90
|
-
if (cursorMoved) {
|
|
91
|
-
// Reset completion if cursor position changed
|
|
92
|
-
|
|
93
|
-
lastCaretPosRef.current = {
|
|
94
|
-
node: currentNode,
|
|
95
|
-
offset: currentOffset,
|
|
96
|
-
textLength: textUpToCursor.length,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Save a copy of the current range
|
|
101
|
-
const originalRange = range.cloneRange();
|
|
102
|
-
|
|
103
|
-
// Remove any existing cursor spans
|
|
104
|
-
const existingSpan = iframeDocument.getElementById(cursorSpanId);
|
|
105
|
-
if (existingSpan) {
|
|
106
|
-
existingSpan.parentNode?.removeChild(existingSpan);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Create our cursor span
|
|
110
|
-
const cursorSpan = existingSpan ?? iframeDocument.createElement("span");
|
|
111
|
-
cursorSpan.id = cursorSpanId;
|
|
112
|
-
|
|
113
|
-
cursorSpan.style.fontWeight = "bold";
|
|
114
|
-
cursorSpan.style.display = "inline";
|
|
115
|
-
cursorSpan.style.pointerEvents = "none";
|
|
116
|
-
cursorSpan.contentEditable = "false";
|
|
117
|
-
cursorSpan.setAttribute("data-cursor-indicator", "true");
|
|
118
|
-
|
|
119
|
-
// Get a working range for insertion
|
|
120
|
-
const workingRange = range.cloneRange();
|
|
121
|
-
workingRange.collapse(true);
|
|
122
|
-
|
|
123
|
-
// Insert the span at cursor position
|
|
124
|
-
workingRange.insertNode(cursorSpan);
|
|
125
|
-
|
|
126
|
-
// Restore original selection
|
|
127
|
-
selection.removeAllRanges();
|
|
128
|
-
selection.addRange(originalRange);
|
|
129
|
-
|
|
130
|
-
// setTimeout(() => {
|
|
131
|
-
// getCompletionDebounced();
|
|
132
|
-
// }, 100);
|
|
133
|
-
} catch (error) {
|
|
134
|
-
console.error("Error updating cursor:", error);
|
|
135
|
-
} finally {
|
|
136
|
-
setTimeout(() => {
|
|
137
|
-
isUpdatingRef.current = false;
|
|
138
|
-
}, 10);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
useEffect(() => {
|
|
143
|
-
// Handle keydown events - especially for cursor movement
|
|
144
|
-
const keyHandler = (e: KeyboardEvent) => {
|
|
145
|
-
if (
|
|
146
|
-
e.key === "ArrowLeft" ||
|
|
147
|
-
e.key === "ArrowRight" ||
|
|
148
|
-
e.key === "ArrowUp" ||
|
|
149
|
-
e.key === "ArrowDown" ||
|
|
150
|
-
e.key === " " ||
|
|
151
|
-
e.key === "Tab" ||
|
|
152
|
-
e.key === "End" ||
|
|
153
|
-
e.key === "Backspace" ||
|
|
154
|
-
e.key === "Delete" // Add Delete key handling
|
|
155
|
-
) {
|
|
156
|
-
// Clear the completion when arrow keys are used
|
|
157
|
-
setCurrentCompletion(null);
|
|
158
|
-
clearCursorSpan();
|
|
159
|
-
|
|
160
|
-
// Let the browser handle the cursor movement/deletion
|
|
161
|
-
// Then update our cursor span after a small delay
|
|
162
|
-
setTimeout(() => {
|
|
163
|
-
if (!isUpdatingRef.current) {
|
|
164
|
-
positionCursorSpan();
|
|
165
|
-
}
|
|
166
|
-
}, 10);
|
|
167
|
-
|
|
168
|
-
// Special handling for right arrow and delete which seems to have issues
|
|
169
|
-
if (e.key === "ArrowRight" || e.key === "Delete") {
|
|
170
|
-
// Make sure the cursor span doesn't block the movement/deletion
|
|
171
|
-
const cursorSpan =
|
|
172
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document.getElementById(
|
|
173
|
-
cursorSpanId,
|
|
174
|
-
);
|
|
175
|
-
if (cursorSpan) {
|
|
176
|
-
// Temporarily make it display none so it doesn't interfere with selection
|
|
177
|
-
const originalDisplay = cursorSpan.style.display;
|
|
178
|
-
cursorSpan.style.display = "none";
|
|
179
|
-
|
|
180
|
-
// Restore after the browser has processed the movement/deletion
|
|
181
|
-
setTimeout(() => {
|
|
182
|
-
if (cursorSpan.parentNode) {
|
|
183
|
-
cursorSpan.style.display = originalDisplay;
|
|
184
|
-
}
|
|
185
|
-
}, 0);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document.addEventListener(
|
|
192
|
-
"keydown",
|
|
193
|
-
keyHandler,
|
|
194
|
-
);
|
|
195
|
-
return () => {
|
|
196
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document.removeEventListener(
|
|
197
|
-
"keydown",
|
|
198
|
-
keyHandler,
|
|
199
|
-
);
|
|
200
|
-
};
|
|
201
|
-
}, [currentCompletion, pageViewContext.page]);
|
|
202
|
-
|
|
203
|
-
// Extracts the text up to the cursor position in the editable element
|
|
204
|
-
const getContentUpToCursor = (element: HTMLElement) => {
|
|
205
|
-
const iframeWindow = pageViewContext.editorIframeRef.current?.contentWindow;
|
|
206
|
-
const selection = iframeWindow?.getSelection();
|
|
207
|
-
if (!element || !selection || selection.rangeCount === 0) return null;
|
|
208
|
-
const range = selection.getRangeAt(0);
|
|
209
|
-
const cursorPosition = range.startOffset;
|
|
210
|
-
let contentUpToCursor = "";
|
|
211
|
-
if (element.childNodes.length === 0) {
|
|
212
|
-
contentUpToCursor = "";
|
|
213
|
-
} else if (
|
|
214
|
-
element.childNodes.length === 1 &&
|
|
215
|
-
element.firstChild?.nodeType === Node.TEXT_NODE
|
|
216
|
-
) {
|
|
217
|
-
const full = element.innerText || "";
|
|
218
|
-
contentUpToCursor = full.substring(
|
|
219
|
-
0,
|
|
220
|
-
Math.min(cursorPosition, full.length),
|
|
221
|
-
);
|
|
222
|
-
} else {
|
|
223
|
-
const tempRange = document.createRange();
|
|
224
|
-
tempRange.selectNodeContents(element);
|
|
225
|
-
tempRange.setEnd(range.startContainer, range.startOffset);
|
|
226
|
-
contentUpToCursor = tempRange.toString();
|
|
227
|
-
}
|
|
228
|
-
return contentUpToCursor;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
// Loading animation with three dots changing color
|
|
232
|
-
const startLoadingAnimation = () => {
|
|
233
|
-
const doc =
|
|
234
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document;
|
|
235
|
-
const span = doc?.getElementById(cursorSpanId);
|
|
236
|
-
if (!doc || !span) return;
|
|
237
|
-
|
|
238
|
-
// Create dots container
|
|
239
|
-
span.innerHTML = "";
|
|
240
|
-
span.style.display = "inline-flex";
|
|
241
|
-
span.style.gap = "4px";
|
|
242
|
-
span.style.alignItems = "center";
|
|
243
|
-
|
|
244
|
-
// Create three dots
|
|
245
|
-
for (let i = 0; i < 3; i++) {
|
|
246
|
-
const dot = doc.createElement("span");
|
|
247
|
-
dot.textContent = "•";
|
|
248
|
-
dot.style.transition = "color 0.3s ease";
|
|
249
|
-
dot.style.color = "#ccc";
|
|
250
|
-
dot.style.fontSize = "16px";
|
|
251
|
-
span.appendChild(dot);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Animate dots
|
|
255
|
-
let step = 0;
|
|
256
|
-
const animate = () => {
|
|
257
|
-
const dots = span.querySelectorAll("span");
|
|
258
|
-
dots.forEach((dot, index) => {
|
|
259
|
-
if (index === step % 3) {
|
|
260
|
-
(dot as HTMLElement).style.color = "#666";
|
|
261
|
-
} else {
|
|
262
|
-
(dot as HTMLElement).style.color = "#ccc";
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
step++;
|
|
266
|
-
loadingAnimationRef.current = requestAnimationFrame(() => {
|
|
267
|
-
// Slow down animation by only updating every 15 frames (~250ms at 60fps)
|
|
268
|
-
if (step % 15 === 0) {
|
|
269
|
-
animate();
|
|
270
|
-
} else {
|
|
271
|
-
loadingAnimationRef.current = requestAnimationFrame(animate);
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
animate();
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const stopLoadingAnimation = () => {
|
|
280
|
-
if (loadingAnimationRef.current) {
|
|
281
|
-
cancelAnimationFrame(loadingAnimationRef.current);
|
|
282
|
-
loadingAnimationRef.current = null;
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
const getCompletion = async (element: HTMLElement) => {
|
|
287
|
-
const contentUpToCursor = getContentUpToCursor(element);
|
|
288
|
-
if (!contentUpToCursor?.trim()) return null;
|
|
289
|
-
|
|
290
|
-
// Abort any in-flight request
|
|
291
|
-
if (abortControllerRef.current) {
|
|
292
|
-
abortControllerRef.current.abort();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Create a new abort controller for this request
|
|
296
|
-
abortControllerRef.current = new AbortController();
|
|
297
|
-
const signal = abortControllerRef.current.signal;
|
|
298
|
-
|
|
299
|
-
// Get field attributes
|
|
300
|
-
const fieldId = element.getAttribute("data-fieldid");
|
|
301
|
-
const fieldName = element.getAttribute("data-fieldname");
|
|
302
|
-
const itemId = element.getAttribute("data-itemid");
|
|
303
|
-
const language = element.getAttribute("data-language");
|
|
304
|
-
const version = element.getAttribute("data-version");
|
|
305
|
-
if (!fieldId || !itemId || !language || !version) return null;
|
|
306
|
-
|
|
307
|
-
// Only trigger completion after a space to ensure clean separation between
|
|
308
|
-
// user text and completion suggestion
|
|
309
|
-
const lastChar = contentUpToCursor.slice(-1);
|
|
310
|
-
if (lastChar !== " " && lastChar !== "\u00A0") {
|
|
311
|
-
return null;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const messages = [
|
|
315
|
-
{
|
|
316
|
-
name: "system",
|
|
317
|
-
role: "system",
|
|
318
|
-
content:
|
|
319
|
-
"You are a sentence completion tool. ONLY provide the completion text (what comes after the user's text), never repeat any part of what the user wrote. Your completion should flow naturally from the user's text.",
|
|
320
|
-
},
|
|
321
|
-
{
|
|
322
|
-
name: "user",
|
|
323
|
-
role: "user",
|
|
324
|
-
content: `Complete this sentence. ONLY provide the completion (do not repeat my text): ${contentUpToCursor}`,
|
|
325
|
-
},
|
|
326
|
-
];
|
|
327
|
-
|
|
328
|
-
if (!editContext) return null;
|
|
329
|
-
|
|
330
|
-
// Show loading indicator
|
|
331
|
-
setIsLoading(true);
|
|
332
|
-
startLoadingAnimation();
|
|
333
|
-
|
|
334
|
-
try {
|
|
335
|
-
const result = await executePrompt(
|
|
336
|
-
messages,
|
|
337
|
-
editContext,
|
|
338
|
-
() => ({
|
|
339
|
-
endpoint: "/alpaca/editor/ai/complete",
|
|
340
|
-
promptData: {
|
|
341
|
-
itemid: itemId,
|
|
342
|
-
language,
|
|
343
|
-
version,
|
|
344
|
-
fieldid: fieldId,
|
|
345
|
-
fieldname: fieldName,
|
|
346
|
-
content: contentUpToCursor,
|
|
347
|
-
},
|
|
348
|
-
}),
|
|
349
|
-
{},
|
|
350
|
-
{ signal },
|
|
351
|
-
"gpt-4.1-nano",
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
return result;
|
|
355
|
-
} catch (error: unknown) {
|
|
356
|
-
// Ignore AbortError as it's expected when cancelling
|
|
357
|
-
if (error instanceof Error && error.name !== "AbortError") {
|
|
358
|
-
console.error("Error getting completion:", error);
|
|
359
|
-
}
|
|
360
|
-
return null;
|
|
361
|
-
} finally {
|
|
362
|
-
// Hide loading indicator
|
|
363
|
-
setIsLoading(false);
|
|
364
|
-
stopLoadingAnimation();
|
|
365
|
-
}
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
// Debounced AI call: recompute the sentence, call getCompletion, and extract only the "tail" for the ghost text
|
|
369
|
-
const getCompletionDebounced = useDebouncedCallback(async () => {
|
|
370
|
-
const el = editContext?.inlineEditingFieldElement;
|
|
371
|
-
if (!el) return;
|
|
372
|
-
|
|
373
|
-
// 1) Recompute the exact sentence at this moment
|
|
374
|
-
const full = getContentUpToCursor(el) || "";
|
|
375
|
-
const sentence = full.split(/[.?!]\s*/).pop() || "";
|
|
376
|
-
|
|
377
|
-
// 2) Ask AI for a completion
|
|
378
|
-
const result = await getCompletion(el);
|
|
379
|
-
const rawSuggestion = result?.messages?.[0]?.content;
|
|
380
|
-
if (!rawSuggestion) {
|
|
381
|
-
setCurrentCompletion(null);
|
|
382
|
-
clearCursorSpan();
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// 3) Strip off the already-typed sentence to leave just the "completion tail"
|
|
387
|
-
const suggestion = rawSuggestion.startsWith(sentence)
|
|
388
|
-
? rawSuggestion
|
|
389
|
-
: sentence + rawSuggestion;
|
|
390
|
-
|
|
391
|
-
setCurrentCompletion(suggestion);
|
|
392
|
-
|
|
393
|
-
updateCursorSpan(suggestion.substring(sentence.length));
|
|
394
|
-
}, 250);
|
|
395
|
-
|
|
396
|
-
// Inserts or clears the ghost text inside the cursor span
|
|
397
|
-
const updateCursorSpan = (text: string) => {
|
|
398
|
-
const doc =
|
|
399
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document;
|
|
400
|
-
const span = doc?.getElementById(cursorSpanId);
|
|
401
|
-
if (!doc || !span) return;
|
|
402
|
-
|
|
403
|
-
// Update the completion text
|
|
404
|
-
if (text) {
|
|
405
|
-
// // Create a temporary span to measure the text width
|
|
406
|
-
// const measureSpan = doc.createElement("span");
|
|
407
|
-
// measureSpan.style.visibility = "hidden";
|
|
408
|
-
// measureSpan.style.position = "absolute";
|
|
409
|
-
// measureSpan.style.whiteSpace = "pre"; // Preserve whitespace
|
|
410
|
-
// measureSpan.style.font = window.getComputedStyle(span).font; // Match the font
|
|
411
|
-
// measureSpan.textContent = text;
|
|
412
|
-
// doc.body.appendChild(measureSpan);
|
|
413
|
-
// const textWidth = measureSpan.getBoundingClientRect().width;
|
|
414
|
-
// measureSpan.remove();
|
|
415
|
-
|
|
416
|
-
span.textContent = text;
|
|
417
|
-
span.style.color = "#888";
|
|
418
|
-
span.style.fontStyle = "italic";
|
|
419
|
-
//span.innerHTML = "hello";
|
|
420
|
-
|
|
421
|
-
// Create or update hint element in the main document
|
|
422
|
-
let hintElement = document.getElementById(`${cursorSpanId}-hint`);
|
|
423
|
-
if (!hintElement) {
|
|
424
|
-
hintElement = document.createElement("div");
|
|
425
|
-
hintElement.id = `${cursorSpanId}-hint`;
|
|
426
|
-
document.body.appendChild(hintElement);
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
hintElement.textContent = "Press Tab to accept ⇥";
|
|
430
|
-
|
|
431
|
-
// Apply styles that match Tailwind's utility classes
|
|
432
|
-
Object.assign(hintElement.style, {
|
|
433
|
-
position: "fixed",
|
|
434
|
-
fontSize: "12px",
|
|
435
|
-
fontWeight: "normal",
|
|
436
|
-
color: "rgb(75, 85, 99)",
|
|
437
|
-
backgroundColor: "white",
|
|
438
|
-
padding: "0.5rem",
|
|
439
|
-
marginLeft: "0.25rem",
|
|
440
|
-
marginRight: "0.25rem",
|
|
441
|
-
borderRadius: "0.25rem",
|
|
442
|
-
border: "1px solid rgb(229, 231, 235)",
|
|
443
|
-
fontStyle: "normal",
|
|
444
|
-
display: "block",
|
|
445
|
-
lineHeight: "1.4",
|
|
446
|
-
zIndex: "9999",
|
|
447
|
-
pointerEvents: "none",
|
|
448
|
-
boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
// Position the hint element relative to the iframe and cursor span
|
|
452
|
-
const iframeRect =
|
|
453
|
-
pageViewContext.editorIframeRef.current?.getBoundingClientRect();
|
|
454
|
-
const spanRect = span.getBoundingClientRect();
|
|
455
|
-
|
|
456
|
-
if (iframeRect) {
|
|
457
|
-
// Create a range for the last character to get proper position
|
|
458
|
-
const textLength = span.textContent?.length || 0;
|
|
459
|
-
if (textLength > 0 && span.firstChild) {
|
|
460
|
-
const range = doc.createRange();
|
|
461
|
-
range.setStart(span.firstChild, Math.max(0, textLength - 1));
|
|
462
|
-
range.setEnd(span.firstChild, textLength);
|
|
463
|
-
|
|
464
|
-
const rangeRect = range.getBoundingClientRect();
|
|
465
|
-
|
|
466
|
-
// Position at the end of the actual text
|
|
467
|
-
const absoluteLeft = iframeRect.left + rangeRect.right;
|
|
468
|
-
const absoluteTop = iframeRect.top + rangeRect.top;
|
|
469
|
-
|
|
470
|
-
hintElement.style.left = `${absoluteLeft}px`;
|
|
471
|
-
hintElement.style.top = `${absoluteTop}px`;
|
|
472
|
-
} else {
|
|
473
|
-
// Fallback to the old positioning if there's no text
|
|
474
|
-
const absoluteLeft = iframeRect.left + spanRect.right;
|
|
475
|
-
const absoluteTop = iframeRect.top + spanRect.top;
|
|
476
|
-
|
|
477
|
-
hintElement.style.left = `${absoluteLeft}px`;
|
|
478
|
-
hintElement.style.top = `${absoluteTop}px`;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
} else {
|
|
482
|
-
span.textContent = "";
|
|
483
|
-
// Remove hint element if it exists
|
|
484
|
-
const hintElement = document.getElementById(`${cursorSpanId}-hint`);
|
|
485
|
-
if (hintElement) {
|
|
486
|
-
hintElement.remove();
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
const clearCursorSpan = () => {
|
|
492
|
-
const doc =
|
|
493
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document;
|
|
494
|
-
if (!doc) return;
|
|
495
|
-
|
|
496
|
-
// Clear the completion text
|
|
497
|
-
updateCursorSpan("");
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
// On every input: either reuse the existing suggestion or fire a new one
|
|
501
|
-
const handleInput = useCallback(
|
|
502
|
-
(e: KeyboardEvent) => {
|
|
503
|
-
const el = editContext?.inlineEditingFieldElement;
|
|
504
|
-
if (!el) return;
|
|
505
|
-
|
|
506
|
-
// Clear completion when Escape is pressed
|
|
507
|
-
if (e.key === "Escape") {
|
|
508
|
-
e.preventDefault();
|
|
509
|
-
e.stopPropagation();
|
|
510
|
-
setCurrentCompletion(null);
|
|
511
|
-
clearCursorSpan();
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
// Request new completion when Ctrl+Space is pressed
|
|
516
|
-
if (e.key === " " && (e.ctrlKey || e.metaKey)) {
|
|
517
|
-
e.preventDefault();
|
|
518
|
-
e.stopPropagation();
|
|
519
|
-
setCurrentCompletion(null);
|
|
520
|
-
clearCursorSpan();
|
|
521
|
-
getCompletionDebounced();
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
// Apply completion when Tab is pressed
|
|
526
|
-
if (e.key === "Tab") {
|
|
527
|
-
// Check if there's an actual completion in the cursor span
|
|
528
|
-
const cursorSpan =
|
|
529
|
-
pageViewContext.editorIframeRef.current?.contentWindow?.document.getElementById(
|
|
530
|
-
cursorSpanId,
|
|
531
|
-
);
|
|
532
|
-
if (
|
|
533
|
-
cursorSpan &&
|
|
534
|
-
cursorSpan.textContent &&
|
|
535
|
-
cursorSpan.textContent.trim() !== ""
|
|
536
|
-
) {
|
|
537
|
-
e.preventDefault();
|
|
538
|
-
e.stopPropagation();
|
|
539
|
-
applyCompletion();
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
const full = getContentUpToCursor(el) || "";
|
|
545
|
-
const sentence = full.split(/[.?!]\s*/).pop() || "";
|
|
546
|
-
|
|
547
|
-
// If we already have a suggestion, check if the user is still typing along it
|
|
548
|
-
// Normalize spaces to handle both regular and non-breaking spaces
|
|
549
|
-
const normalizedSentence = sentence.replace(/\s/g, " ");
|
|
550
|
-
const normalizedCompletion = currentCompletion?.replace(/\s/g, " ");
|
|
551
|
-
|
|
552
|
-
if (
|
|
553
|
-
normalizedCompletion &&
|
|
554
|
-
normalizedCompletion.startsWith(normalizedSentence)
|
|
555
|
-
) {
|
|
556
|
-
// user is still matching the suggestion → update the remaining tail
|
|
557
|
-
const remaining = normalizedCompletion.slice(normalizedSentence.length);
|
|
558
|
-
|
|
559
|
-
updateCursorSpan(remaining);
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
// else: user diverged → drop it
|
|
563
|
-
setCurrentCompletion(null);
|
|
564
|
-
clearCursorSpan();
|
|
565
|
-
|
|
566
|
-
// No active suggestion → fire new request once they've typed 2+ words
|
|
567
|
-
const words = sentence.trim().split(/\s+/).filter(Boolean);
|
|
568
|
-
|
|
569
|
-
// Check if cursor is at the end of the text
|
|
570
|
-
const isAtEnd = () => {
|
|
571
|
-
const el = editContext?.inlineEditingFieldElement;
|
|
572
|
-
if (!el) return false;
|
|
573
|
-
|
|
574
|
-
const iframeWindow =
|
|
575
|
-
pageViewContext.editorIframeRef.current?.contentWindow;
|
|
576
|
-
const selection = iframeWindow?.getSelection();
|
|
577
|
-
if (!selection || selection.rangeCount === 0) return false;
|
|
578
|
-
|
|
579
|
-
const range = selection.getRangeAt(0);
|
|
580
|
-
|
|
581
|
-
// Create a range for the whole editable element
|
|
582
|
-
const fullRange = document.createRange();
|
|
583
|
-
fullRange.selectNodeContents(el);
|
|
584
|
-
|
|
585
|
-
// Get the full text content
|
|
586
|
-
const fullText = el.textContent || "";
|
|
587
|
-
|
|
588
|
-
// Get text up to cursor
|
|
589
|
-
const tempRange = document.createRange();
|
|
590
|
-
tempRange.selectNodeContents(el);
|
|
591
|
-
tempRange.setEnd(range.startContainer, range.startOffset);
|
|
592
|
-
const textUpToCursor = tempRange.toString();
|
|
593
|
-
|
|
594
|
-
// Cursor is at the end if the text up to cursor equals the full text
|
|
595
|
-
// (accounting for whitespace differences)
|
|
596
|
-
return (
|
|
597
|
-
textUpToCursor.trim() === fullText.trim() ||
|
|
598
|
-
(fullText.trim().startsWith(textUpToCursor.trim()) &&
|
|
599
|
-
fullText.trim().length === textUpToCursor.trim().length)
|
|
600
|
-
);
|
|
601
|
-
};
|
|
602
|
-
|
|
603
|
-
if (words.length >= 2 && isAtEnd()) {
|
|
604
|
-
getCompletionDebounced();
|
|
605
|
-
} else {
|
|
606
|
-
setCurrentCompletion(null);
|
|
607
|
-
clearCursorSpan();
|
|
608
|
-
}
|
|
609
|
-
},
|
|
610
|
-
[
|
|
611
|
-
editContext?.inlineEditingFieldElement,
|
|
612
|
-
currentCompletion,
|
|
613
|
-
getCompletionDebounced,
|
|
614
|
-
],
|
|
615
|
-
);
|
|
616
|
-
|
|
617
|
-
// Wire up the input listener
|
|
618
|
-
useEffect(() => {
|
|
619
|
-
if (!editContext?.enableCompletions) return;
|
|
620
|
-
const el = editContext?.inlineEditingFieldElement;
|
|
621
|
-
if (!el) return;
|
|
622
|
-
el.addEventListener("keydown", handleInput);
|
|
623
|
-
return () => el.removeEventListener("keydown", handleInput);
|
|
624
|
-
}, [
|
|
625
|
-
editContext?.inlineEditingFieldElement,
|
|
626
|
-
handleInput,
|
|
627
|
-
editContext?.enableCompletions,
|
|
628
|
-
]);
|
|
629
|
-
|
|
630
|
-
// Add mouse click handler to update cursor span position
|
|
631
|
-
useEffect(() => {
|
|
632
|
-
if (!editContext?.enableCompletions) return;
|
|
633
|
-
const el = editContext?.inlineEditingFieldElement;
|
|
634
|
-
if (!el) return;
|
|
635
|
-
|
|
636
|
-
const handleMouseUp = () => {
|
|
637
|
-
setTimeout(() => {
|
|
638
|
-
if (!isUpdatingRef.current) {
|
|
639
|
-
positionCursorSpan();
|
|
640
|
-
|
|
641
|
-
// Clear existing completion when cursor position changes via mouse
|
|
642
|
-
setCurrentCompletion(null);
|
|
643
|
-
clearCursorSpan();
|
|
644
|
-
}
|
|
645
|
-
}, 10);
|
|
646
|
-
};
|
|
647
|
-
|
|
648
|
-
el.addEventListener("mouseup", handleMouseUp);
|
|
649
|
-
return () => el.removeEventListener("mouseup", handleMouseUp);
|
|
650
|
-
}, [editContext?.inlineEditingFieldElement, editContext?.enableCompletions]);
|
|
651
|
-
|
|
652
|
-
// Clean up abort controller on unmount
|
|
653
|
-
useEffect(() => {
|
|
654
|
-
return () => {
|
|
655
|
-
if (abortControllerRef.current) {
|
|
656
|
-
abortControllerRef.current.abort();
|
|
657
|
-
abortControllerRef.current = null;
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
if (loadingAnimationRef.current) {
|
|
661
|
-
cancelAnimationFrame(loadingAnimationRef.current);
|
|
662
|
-
loadingAnimationRef.current = null;
|
|
663
|
-
}
|
|
664
|
-
};
|
|
665
|
-
}, []);
|
|
666
|
-
|
|
667
|
-
// Function to apply the completion
|
|
668
|
-
const applyCompletion = () => {
|
|
669
|
-
// Get the cursor span to read the most up-to-date completion
|
|
670
|
-
const iframeWindow = pageViewContext.editorIframeRef.current?.contentWindow;
|
|
671
|
-
const iframeDocument = iframeWindow?.document;
|
|
672
|
-
if (
|
|
673
|
-
!iframeWindow ||
|
|
674
|
-
!iframeDocument ||
|
|
675
|
-
!editContext?.inlineEditingFieldElement
|
|
676
|
-
)
|
|
677
|
-
return;
|
|
678
|
-
|
|
679
|
-
const cursorSpan = iframeDocument.getElementById(cursorSpanId);
|
|
680
|
-
if (!cursorSpan) return;
|
|
681
|
-
|
|
682
|
-
// Get the completion text directly from the cursor span, which should be the most current
|
|
683
|
-
const completionToApply = cursorSpan.textContent || "";
|
|
684
|
-
if (!completionToApply) return;
|
|
685
|
-
|
|
686
|
-
const element = editContext?.inlineEditingFieldElement;
|
|
687
|
-
|
|
688
|
-
// Get the current selection position
|
|
689
|
-
const selection = iframeWindow.getSelection();
|
|
690
|
-
if (!selection || selection.rangeCount === 0) return;
|
|
691
|
-
|
|
692
|
-
// Get the text up to the cursor to analyze current word
|
|
693
|
-
const range = selection.getRangeAt(0);
|
|
694
|
-
const tempRange = document.createRange();
|
|
695
|
-
tempRange.selectNodeContents(element);
|
|
696
|
-
tempRange.setEnd(range.startContainer, range.startOffset);
|
|
697
|
-
const textUpToCursor = tempRange.toString();
|
|
698
|
-
|
|
699
|
-
// Get the current partial word by finding text from the last word boundary to cursor
|
|
700
|
-
// Look for last word boundary (space, punctuation, etc.)
|
|
701
|
-
const wordBoundaryRegex =
|
|
702
|
-
/[\s.,;:!?"'()[\]{}<>\/\\|=+\-*&^%$#@~`](?=[^\s.,;:!?"'()[\]{}<>\/\\|=+\-*&^%$#@~`]*$)/;
|
|
703
|
-
const match = textUpToCursor.match(wordBoundaryRegex);
|
|
704
|
-
const lastWordBoundaryIndex =
|
|
705
|
-
match && match.index !== undefined ? match.index + 1 : 0;
|
|
706
|
-
|
|
707
|
-
const currentPartialWord = textUpToCursor
|
|
708
|
-
.substring(lastWordBoundaryIndex)
|
|
709
|
-
.trim();
|
|
710
|
-
|
|
711
|
-
// Check if completion overlaps with current partial word
|
|
712
|
-
// (e.g., if user typed "int" and completion is "integer")
|
|
713
|
-
const isOverlapping =
|
|
714
|
-
currentPartialWord.length > 0 &&
|
|
715
|
-
completionToApply
|
|
716
|
-
.toLowerCase()
|
|
717
|
-
.startsWith(currentPartialWord.toLowerCase());
|
|
718
|
-
|
|
719
|
-
console.log("Is overlapping:", isOverlapping);
|
|
720
|
-
|
|
721
|
-
// If there's overlap, we need to delete the current partial word
|
|
722
|
-
if (isOverlapping) {
|
|
723
|
-
// Create a range to select the current partial word
|
|
724
|
-
const wordRange = document.createRange();
|
|
725
|
-
|
|
726
|
-
// Position where the current word starts
|
|
727
|
-
let startContainer = range.startContainer;
|
|
728
|
-
let startOffset = range.startOffset - currentPartialWord.length;
|
|
729
|
-
|
|
730
|
-
// We need to handle the case where the word spans multiple text nodes
|
|
731
|
-
// For simplicity, we'll try to handle the common case first
|
|
732
|
-
if (startOffset >= 0 && startContainer.nodeType === Node.TEXT_NODE) {
|
|
733
|
-
// Simple case: word is in the same text node as cursor
|
|
734
|
-
wordRange.setStart(startContainer, startOffset);
|
|
735
|
-
wordRange.setEnd(range.startContainer, range.startOffset);
|
|
736
|
-
wordRange.deleteContents(); // Delete the partial word
|
|
737
|
-
} else {
|
|
738
|
-
// Complex case: use a simpler approach - just insert, user can delete manually if needed
|
|
739
|
-
// This is a fallback for complex DOM structures
|
|
740
|
-
// Add a space if we're in the middle of a sentence
|
|
741
|
-
if (textUpToCursor.length > 0 && !textUpToCursor.endsWith(" ")) {
|
|
742
|
-
const spaceNode = document.createTextNode(" ");
|
|
743
|
-
range.insertNode(spaceNode);
|
|
744
|
-
range.setStartAfter(spaceNode);
|
|
745
|
-
range.setEndAfter(spaceNode);
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
} else {
|
|
749
|
-
// Not overlapping, add a space if we're in the middle of text
|
|
750
|
-
// and not already at the beginning of text or after a space
|
|
751
|
-
if (
|
|
752
|
-
textUpToCursor.length > 0 &&
|
|
753
|
-
!textUpToCursor.endsWith(" ") &&
|
|
754
|
-
// Don't add space at the start of a line or after punctuation that shouldn't have a space
|
|
755
|
-
!textUpToCursor.endsWith("\n") &&
|
|
756
|
-
!/[.!?\-—:;({[\s]$/.test(textUpToCursor)
|
|
757
|
-
) {
|
|
758
|
-
const spaceNode = document.createTextNode(" ");
|
|
759
|
-
range.insertNode(spaceNode);
|
|
760
|
-
range.setStartAfter(spaceNode);
|
|
761
|
-
range.setEndAfter(spaceNode);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
// Now insert the completion text
|
|
766
|
-
const textNode = document.createTextNode(
|
|
767
|
-
isOverlapping
|
|
768
|
-
? completionToApply // If overlapping, use the full completion
|
|
769
|
-
: completionToApply, // If not overlapping, use as is
|
|
770
|
-
);
|
|
771
|
-
range.insertNode(textNode);
|
|
772
|
-
|
|
773
|
-
// Move the cursor after the inserted text
|
|
774
|
-
range.setStartAfter(textNode);
|
|
775
|
-
range.setEndAfter(textNode);
|
|
776
|
-
selection.removeAllRanges();
|
|
777
|
-
selection.addRange(range);
|
|
778
|
-
setCurrentCompletion(null);
|
|
779
|
-
clearCursorSpan();
|
|
780
|
-
};
|
|
781
|
-
|
|
782
|
-
// Exposed manual trigger (if needed)
|
|
783
|
-
return useMemo(
|
|
784
|
-
() => () => {
|
|
785
|
-
setCurrentCompletion(null);
|
|
786
|
-
clearCursorSpan();
|
|
787
|
-
getCompletionDebounced();
|
|
788
|
-
},
|
|
789
|
-
[getCompletionDebounced],
|
|
790
|
-
);
|
|
791
|
-
}
|