@agent-native/toolkit 0.5.1 → 0.7.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.
- package/README.md +17 -1
- package/dist/chat-history/ChatHistoryList.d.ts +103 -0
- package/dist/chat-history/ChatHistoryList.d.ts.map +1 -0
- package/dist/chat-history/ChatHistoryList.js +109 -0
- package/dist/chat-history/ChatHistoryList.js.map +1 -0
- package/dist/chat-history/ChatHistoryList.spec.d.ts +2 -0
- package/dist/chat-history/ChatHistoryList.spec.d.ts.map +1 -0
- package/dist/chat-history/ChatHistoryList.spec.js +283 -0
- package/dist/chat-history/ChatHistoryList.spec.js.map +1 -0
- package/dist/chat-history/index.d.ts +2 -0
- package/dist/chat-history/index.d.ts.map +1 -0
- package/dist/chat-history/index.js +2 -0
- package/dist/chat-history/index.js.map +1 -0
- package/dist/chat-history.css +314 -0
- package/dist/composer/AgentComposerFrame.d.ts +19 -0
- package/dist/composer/AgentComposerFrame.d.ts.map +1 -0
- package/dist/composer/AgentComposerFrame.js +14 -0
- package/dist/composer/AgentComposerFrame.js.map +1 -0
- package/dist/composer/ComposerPlusMenu.d.ts +17 -0
- package/dist/composer/ComposerPlusMenu.d.ts.map +1 -0
- package/dist/composer/ComposerPlusMenu.js +501 -0
- package/dist/composer/ComposerPlusMenu.js.map +1 -0
- package/dist/composer/MentionPopover.d.ts +29 -0
- package/dist/composer/MentionPopover.d.ts.map +1 -0
- package/dist/composer/MentionPopover.js +180 -0
- package/dist/composer/MentionPopover.js.map +1 -0
- package/dist/composer/PastedTextChip.d.ts +9 -0
- package/dist/composer/PastedTextChip.d.ts.map +1 -0
- package/dist/composer/PastedTextChip.js +47 -0
- package/dist/composer/PastedTextChip.js.map +1 -0
- package/dist/composer/PromptComposer.d.ts +114 -0
- package/dist/composer/PromptComposer.d.ts.map +1 -0
- package/dist/composer/PromptComposer.js +302 -0
- package/dist/composer/PromptComposer.js.map +1 -0
- package/dist/composer/PromptComposer.spec.d.ts +2 -0
- package/dist/composer/PromptComposer.spec.d.ts.map +1 -0
- package/dist/composer/PromptComposer.spec.js +64 -0
- package/dist/composer/PromptComposer.spec.js.map +1 -0
- package/dist/composer/RealtimeVoiceMode.d.ts +104 -0
- package/dist/composer/RealtimeVoiceMode.d.ts.map +1 -0
- package/dist/composer/RealtimeVoiceMode.js +245 -0
- package/dist/composer/RealtimeVoiceMode.js.map +1 -0
- package/dist/composer/RealtimeVoiceMode.spec.d.ts +2 -0
- package/dist/composer/RealtimeVoiceMode.spec.d.ts.map +1 -0
- package/dist/composer/RealtimeVoiceMode.spec.js +439 -0
- package/dist/composer/RealtimeVoiceMode.spec.js.map +1 -0
- package/dist/composer/TiptapComposer.d.ts +221 -0
- package/dist/composer/TiptapComposer.d.ts.map +1 -0
- package/dist/composer/TiptapComposer.js +1838 -0
- package/dist/composer/TiptapComposer.js.map +1 -0
- package/dist/composer/TiptapComposer.spec.d.ts +2 -0
- package/dist/composer/TiptapComposer.spec.d.ts.map +1 -0
- package/dist/composer/TiptapComposer.spec.js +277 -0
- package/dist/composer/TiptapComposer.spec.js.map +1 -0
- package/dist/composer/VoiceButton.d.ts +25 -0
- package/dist/composer/VoiceButton.d.ts.map +1 -0
- package/dist/composer/VoiceButton.js +100 -0
- package/dist/composer/VoiceButton.js.map +1 -0
- package/dist/composer/VoiceButton.spec.d.ts +2 -0
- package/dist/composer/VoiceButton.spec.d.ts.map +1 -0
- package/dist/composer/VoiceButton.spec.js +19 -0
- package/dist/composer/VoiceButton.spec.js.map +1 -0
- package/dist/composer/asset-picker-url.d.ts +16 -0
- package/dist/composer/asset-picker-url.d.ts.map +1 -0
- package/dist/composer/asset-picker-url.js +64 -0
- package/dist/composer/asset-picker-url.js.map +1 -0
- package/dist/composer/asset-picker-url.spec.d.ts +2 -0
- package/dist/composer/asset-picker-url.spec.d.ts.map +1 -0
- package/dist/composer/asset-picker-url.spec.js +23 -0
- package/dist/composer/asset-picker-url.spec.js.map +1 -0
- package/dist/composer/attachment-accept.d.ts +9 -0
- package/dist/composer/attachment-accept.d.ts.map +1 -0
- package/dist/composer/attachment-accept.js +65 -0
- package/dist/composer/attachment-accept.js.map +1 -0
- package/dist/composer/attachment-accept.spec.d.ts +2 -0
- package/dist/composer/attachment-accept.spec.d.ts.map +1 -0
- package/dist/composer/attachment-accept.spec.js +15 -0
- package/dist/composer/attachment-accept.spec.js.map +1 -0
- package/dist/composer/draft-key.d.ts +2 -0
- package/dist/composer/draft-key.d.ts.map +1 -0
- package/dist/composer/draft-key.js +8 -0
- package/dist/composer/draft-key.js.map +1 -0
- package/dist/composer/draft-key.spec.d.ts +2 -0
- package/dist/composer/draft-key.spec.d.ts.map +1 -0
- package/dist/composer/draft-key.spec.js +13 -0
- package/dist/composer/draft-key.spec.js.map +1 -0
- package/dist/composer/extensions/FileReference.d.ts +3 -0
- package/dist/composer/extensions/FileReference.d.ts.map +1 -0
- package/dist/composer/extensions/FileReference.js +36 -0
- package/dist/composer/extensions/FileReference.js.map +1 -0
- package/dist/composer/extensions/MentionReference.d.ts +3 -0
- package/dist/composer/extensions/MentionReference.d.ts.map +1 -0
- package/dist/composer/extensions/MentionReference.js +66 -0
- package/dist/composer/extensions/MentionReference.js.map +1 -0
- package/dist/composer/extensions/SkillReference.d.ts +3 -0
- package/dist/composer/extensions/SkillReference.d.ts.map +1 -0
- package/dist/composer/extensions/SkillReference.js +40 -0
- package/dist/composer/extensions/SkillReference.js.map +1 -0
- package/dist/composer/index.d.ts +14 -0
- package/dist/composer/index.d.ts.map +1 -0
- package/dist/composer/index.js +13 -0
- package/dist/composer/index.js.map +1 -0
- package/dist/composer/pasted-text.d.ts +31 -0
- package/dist/composer/pasted-text.d.ts.map +1 -0
- package/dist/composer/pasted-text.js +130 -0
- package/dist/composer/pasted-text.js.map +1 -0
- package/dist/composer/pasted-text.spec.d.ts +2 -0
- package/dist/composer/pasted-text.spec.d.ts.map +1 -0
- package/dist/composer/pasted-text.spec.js +107 -0
- package/dist/composer/pasted-text.spec.js.map +1 -0
- package/dist/composer/prompt-attachments.d.ts +18 -0
- package/dist/composer/prompt-attachments.d.ts.map +1 -0
- package/dist/composer/prompt-attachments.js +65 -0
- package/dist/composer/prompt-attachments.js.map +1 -0
- package/dist/composer/prompt-attachments.spec.d.ts +2 -0
- package/dist/composer/prompt-attachments.spec.d.ts.map +1 -0
- package/dist/composer/prompt-attachments.spec.js +63 -0
- package/dist/composer/prompt-attachments.spec.js.map +1 -0
- package/dist/composer/realtime-voice-audio-level.d.ts +14 -0
- package/dist/composer/realtime-voice-audio-level.d.ts.map +1 -0
- package/dist/composer/realtime-voice-audio-level.js +47 -0
- package/dist/composer/realtime-voice-audio-level.js.map +1 -0
- package/dist/composer/realtime-voice-audio-level.spec.d.ts +2 -0
- package/dist/composer/realtime-voice-audio-level.spec.d.ts.map +1 -0
- package/dist/composer/realtime-voice-audio-level.spec.js +25 -0
- package/dist/composer/realtime-voice-audio-level.spec.js.map +1 -0
- package/dist/composer/realtime-voice-transcript.d.ts +26 -0
- package/dist/composer/realtime-voice-transcript.d.ts.map +1 -0
- package/dist/composer/realtime-voice-transcript.js +103 -0
- package/dist/composer/realtime-voice-transcript.js.map +1 -0
- package/dist/composer/realtime-voice-transcript.spec.d.ts +2 -0
- package/dist/composer/realtime-voice-transcript.spec.d.ts.map +1 -0
- package/dist/composer/realtime-voice-transcript.spec.js +76 -0
- package/dist/composer/realtime-voice-transcript.spec.js.map +1 -0
- package/dist/composer/runtime-adapters.d.ts +163 -0
- package/dist/composer/runtime-adapters.d.ts.map +1 -0
- package/dist/composer/runtime-adapters.js +130 -0
- package/dist/composer/runtime-adapters.js.map +1 -0
- package/dist/composer/types.d.ts +60 -0
- package/dist/composer/types.d.ts.map +1 -0
- package/dist/composer/types.js +2 -0
- package/dist/composer/types.js.map +1 -0
- package/dist/composer/use-file-search.d.ts +6 -0
- package/dist/composer/use-file-search.d.ts.map +1 -0
- package/dist/composer/use-file-search.js +53 -0
- package/dist/composer/use-file-search.js.map +1 -0
- package/dist/composer/use-mention-search.d.ts +6 -0
- package/dist/composer/use-mention-search.d.ts.map +1 -0
- package/dist/composer/use-mention-search.js +75 -0
- package/dist/composer/use-mention-search.js.map +1 -0
- package/dist/composer/use-skills.d.ts +7 -0
- package/dist/composer/use-skills.d.ts.map +1 -0
- package/dist/composer/use-skills.js +40 -0
- package/dist/composer/use-skills.js.map +1 -0
- package/dist/composer/useRealtimeVoiceMode.d.ts +179 -0
- package/dist/composer/useRealtimeVoiceMode.d.ts.map +1 -0
- package/dist/composer/useRealtimeVoiceMode.js +1657 -0
- package/dist/composer/useRealtimeVoiceMode.js.map +1 -0
- package/dist/composer/useRealtimeVoiceMode.spec.d.ts +2 -0
- package/dist/composer/useRealtimeVoiceMode.spec.d.ts.map +1 -0
- package/dist/composer/useRealtimeVoiceMode.spec.js +618 -0
- package/dist/composer/useRealtimeVoiceMode.spec.js.map +1 -0
- package/dist/composer/useVoiceDictation.d.ts +57 -0
- package/dist/composer/useVoiceDictation.d.ts.map +1 -0
- package/dist/composer/useVoiceDictation.js +1004 -0
- package/dist/composer/useVoiceDictation.js.map +1 -0
- package/dist/composer/useVoiceDictation.spec.d.ts +2 -0
- package/dist/composer/useVoiceDictation.spec.d.ts.map +1 -0
- package/dist/composer/useVoiceDictation.spec.js +32 -0
- package/dist/composer/useVoiceDictation.spec.js.map +1 -0
- package/dist/editor/RegistryBlockContext.d.ts +68 -0
- package/dist/editor/RegistryBlockContext.d.ts.map +1 -0
- package/dist/editor/RegistryBlockContext.js +10 -0
- package/dist/editor/RegistryBlockContext.js.map +1 -0
- package/dist/editor/RegistryBlockNode.d.ts +66 -0
- package/dist/editor/RegistryBlockNode.d.ts.map +1 -0
- package/dist/editor/RegistryBlockNode.js +406 -0
- package/dist/editor/RegistryBlockNode.js.map +1 -0
- package/dist/editor/RegistryBlockNode.spec.d.ts +2 -0
- package/dist/editor/RegistryBlockNode.spec.d.ts.map +1 -0
- package/dist/editor/RegistryBlockNode.spec.js +215 -0
- package/dist/editor/RegistryBlockNode.spec.js.map +1 -0
- package/dist/editor/index.d.ts +2 -0
- package/dist/editor/index.d.ts.map +1 -1
- package/dist/editor/index.js +2 -0
- package/dist/editor/index.js.map +1 -1
- package/dist/editor/registrySlashCommands.d.ts +68 -0
- package/dist/editor/registrySlashCommands.d.ts.map +1 -0
- package/dist/editor/registrySlashCommands.js +51 -0
- package/dist/editor/registrySlashCommands.js.map +1 -0
- package/package.json +32 -1
- package/src/app-shell/header-actions.tsx +84 -0
- package/src/app-shell/index.ts +1 -0
- package/src/chat-history/ChatHistoryList.spec.tsx +453 -0
- package/src/chat-history/ChatHistoryList.tsx +469 -0
- package/src/chat-history/index.ts +7 -0
- package/src/chat-history.css +314 -0
- package/src/collab-ui/AgentPresenceChip.tsx +69 -0
- package/src/collab-ui/LiveCursorOverlay.tsx +241 -0
- package/src/collab-ui/PresenceBar.tsx +346 -0
- package/src/collab-ui/RecentEditHighlights.tsx +225 -0
- package/src/collab-ui/RemoteSelectionRings.tsx +227 -0
- package/src/collab-ui/agent-identity.ts +16 -0
- package/src/collab-ui/index.ts +39 -0
- package/src/collab-ui/lead-client.ts +32 -0
- package/src/collab-ui/types.spec.ts +18 -0
- package/src/collab-ui/types.ts +96 -0
- package/src/composer/AgentComposerFrame.tsx +59 -0
- package/src/composer/ComposerPlusMenu.tsx +983 -0
- package/src/composer/MentionPopover.tsx +412 -0
- package/src/composer/PastedTextChip.tsx +150 -0
- package/src/composer/PromptComposer.spec.ts +73 -0
- package/src/composer/PromptComposer.tsx +718 -0
- package/src/composer/RealtimeVoiceMode.spec.tsx +771 -0
- package/src/composer/RealtimeVoiceMode.tsx +856 -0
- package/src/composer/TiptapComposer.spec.ts +390 -0
- package/src/composer/TiptapComposer.tsx +2936 -0
- package/src/composer/VoiceButton.spec.ts +33 -0
- package/src/composer/VoiceButton.tsx +255 -0
- package/src/composer/asset-picker-url.spec.ts +56 -0
- package/src/composer/asset-picker-url.ts +80 -0
- package/src/composer/attachment-accept.spec.ts +22 -0
- package/src/composer/attachment-accept.ts +71 -0
- package/src/composer/draft-key.spec.ts +19 -0
- package/src/composer/draft-key.ts +7 -0
- package/src/composer/extensions/FileReference.tsx +55 -0
- package/src/composer/extensions/MentionReference.tsx +93 -0
- package/src/composer/extensions/SkillReference.tsx +56 -0
- package/src/composer/index.ts +79 -0
- package/src/composer/pasted-text.spec.ts +162 -0
- package/src/composer/pasted-text.ts +160 -0
- package/src/composer/prompt-attachments.spec.ts +80 -0
- package/src/composer/prompt-attachments.ts +93 -0
- package/src/composer/realtime-voice-audio-level.spec.ts +32 -0
- package/src/composer/realtime-voice-audio-level.ts +64 -0
- package/src/composer/realtime-voice-transcript.spec.ts +95 -0
- package/src/composer/realtime-voice-transcript.ts +134 -0
- package/src/composer/runtime-adapters.tsx +343 -0
- package/src/composer/types.ts +66 -0
- package/src/composer/use-file-search.ts +64 -0
- package/src/composer/use-mention-search.ts +90 -0
- package/src/composer/use-skills.ts +45 -0
- package/src/composer/useRealtimeVoiceMode.spec.ts +818 -0
- package/src/composer/useRealtimeVoiceMode.tsx +2117 -0
- package/src/composer/useVoiceDictation.spec.ts +45 -0
- package/src/composer/useVoiceDictation.ts +1191 -0
- package/src/context-ui/ContextMeter.tsx +101 -0
- package/src/context-ui/ContextSegmentRow.tsx +184 -0
- package/src/context-ui/ContextTreemap.tsx +125 -0
- package/src/context-ui/ContextXRayPanel.tsx +377 -0
- package/src/context-ui/format.ts +42 -0
- package/src/context-ui/index.ts +15 -0
- package/src/context-ui/types.ts +43 -0
- package/src/design-tweaks/index.ts +1 -0
- package/src/design-tweaks/visual-style-controls.tsx +1084 -0
- package/src/editor/BubbleToolbar.tsx +253 -0
- package/src/editor/CodeBlockNode.tsx +254 -0
- package/src/editor/DragHandle.spec.ts +588 -0
- package/src/editor/DragHandle.ts +1437 -0
- package/src/editor/ImageExtension.roundtrip.spec.ts +117 -0
- package/src/editor/ImageExtension.ts +306 -0
- package/src/editor/RegistryBlockContext.tsx +107 -0
- package/src/editor/RegistryBlockNode.spec.ts +279 -0
- package/src/editor/RegistryBlockNode.tsx +652 -0
- package/src/editor/RichMarkdownEditor.collab.spec.ts +79 -0
- package/src/editor/RichMarkdownEditor.roundtrip.spec.ts +226 -0
- package/src/editor/RichMarkdownEditor.tsx +81 -0
- package/src/editor/RunId.ts +61 -0
- package/src/editor/SharedRichEditor.tsx +321 -0
- package/src/editor/SlashCommandMenu.tsx +310 -0
- package/src/editor/disableHistory.spec.ts +85 -0
- package/src/editor/extensions.ts +402 -0
- package/src/editor/gfmDoc.spec.ts +230 -0
- package/src/editor/gfmDoc.ts +92 -0
- package/src/editor/index.ts +93 -0
- package/src/editor/registrySlashCommands.ts +131 -0
- package/src/editor/structuralReorderUndo.spec.ts +144 -0
- package/src/editor/surgical-apply.spec.ts +279 -0
- package/src/editor/surgical-apply.ts +192 -0
- package/src/editor/useCollabReconcile.concurrent.spec.ts +422 -0
- package/src/editor/useCollabReconcile.idempotent.spec.ts +324 -0
- package/src/editor/useCollabReconcile.ts +589 -0
- package/src/editor.css +510 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/use-mobile.tsx +21 -0
- package/src/hooks/use-toast.ts +188 -0
- package/src/index.ts +8 -0
- package/src/onboarding/index.ts +1 -0
- package/src/provider.tsx +47 -0
- package/src/sharing/VisibilityBadge.spec.ts +96 -0
- package/src/sharing/VisibilityBadge.tsx +37 -0
- package/src/sharing/index.ts +4 -0
- package/src/styles.css +21 -0
- package/src/ui/accordion.tsx +56 -0
- package/src/ui/alert-dialog.tsx +141 -0
- package/src/ui/alert.tsx +59 -0
- package/src/ui/aspect-ratio.tsx +5 -0
- package/src/ui/avatar.tsx +48 -0
- package/src/ui/badge.tsx +37 -0
- package/src/ui/breadcrumb.tsx +115 -0
- package/src/ui/button.tsx +78 -0
- package/src/ui/calendar.tsx +214 -0
- package/src/ui/card.tsx +86 -0
- package/src/ui/carousel.tsx +260 -0
- package/src/ui/chart.tsx +375 -0
- package/src/ui/checkbox.tsx +28 -0
- package/src/ui/collapsible.tsx +9 -0
- package/src/ui/command.spec.tsx +51 -0
- package/src/ui/command.tsx +174 -0
- package/src/ui/context-menu.tsx +202 -0
- package/src/ui/date-picker.tsx +64 -0
- package/src/ui/dialog.spec.tsx +43 -0
- package/src/ui/dialog.tsx +146 -0
- package/src/ui/drawer.tsx +126 -0
- package/src/ui/dropdown-menu.tsx +218 -0
- package/src/ui/form.tsx +177 -0
- package/src/ui/hover-card.tsx +27 -0
- package/src/ui/index.ts +52 -0
- package/src/ui/input-otp.tsx +69 -0
- package/src/ui/input.tsx +22 -0
- package/src/ui/label.tsx +24 -0
- package/src/ui/logo.tsx +36 -0
- package/src/ui/menubar.tsx +235 -0
- package/src/ui/navigation-menu.tsx +128 -0
- package/src/ui/pagination.tsx +121 -0
- package/src/ui/popover.tsx +44 -0
- package/src/ui/progress.tsx +26 -0
- package/src/ui/radio-group.tsx +42 -0
- package/src/ui/resizable.tsx +43 -0
- package/src/ui/scroll-area.tsx +46 -0
- package/src/ui/select.tsx +158 -0
- package/src/ui/separator.tsx +29 -0
- package/src/ui/sheet.tsx +160 -0
- package/src/ui/sidebar.tsx +777 -0
- package/src/ui/skeleton.tsx +18 -0
- package/src/ui/slider.tsx +26 -0
- package/src/ui/sonner.tsx +58 -0
- package/src/ui/spinner.tsx +17 -0
- package/src/ui/switch.tsx +27 -0
- package/src/ui/table.tsx +117 -0
- package/src/ui/tabs.tsx +53 -0
- package/src/ui/textarea.tsx +23 -0
- package/src/ui/toast.tsx +127 -0
- package/src/ui/toaster.tsx +33 -0
- package/src/ui/toggle-group.tsx +59 -0
- package/src/ui/toggle.tsx +43 -0
- package/src/ui/tooltip.tsx +53 -0
- package/src/ui/use-toast.ts +3 -0
- package/src/utils.ts +8 -0
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import { IconPencil } from "@tabler/icons-react";
|
|
2
|
+
import {
|
|
3
|
+
NodeSelection,
|
|
4
|
+
Plugin,
|
|
5
|
+
PluginKey,
|
|
6
|
+
type EditorState,
|
|
7
|
+
} from "@tiptap/pm/state";
|
|
8
|
+
import {
|
|
9
|
+
Node,
|
|
10
|
+
NodeViewWrapper,
|
|
11
|
+
ReactNodeViewRenderer,
|
|
12
|
+
mergeAttributes,
|
|
13
|
+
type NodeViewProps,
|
|
14
|
+
} from "@tiptap/react";
|
|
15
|
+
import {
|
|
16
|
+
useEffect,
|
|
17
|
+
useMemo,
|
|
18
|
+
useState,
|
|
19
|
+
type ReactNode,
|
|
20
|
+
type MouseEvent as ReactMouseEvent,
|
|
21
|
+
} from "react";
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
RegistryBlockDataProvider,
|
|
25
|
+
useRegistryBlockData,
|
|
26
|
+
type RegistryBlockDataChangeMeta,
|
|
27
|
+
type RegistryBlockDataValue,
|
|
28
|
+
type RegistryBlockEditSurfaceOptions,
|
|
29
|
+
type RegistryBlockNestedBlock,
|
|
30
|
+
type RegistryBlockRenderOptions,
|
|
31
|
+
type RegistryBlockRenderResult,
|
|
32
|
+
type RegistryBlockSideMapBlock,
|
|
33
|
+
} from "./RegistryBlockContext.js";
|
|
34
|
+
|
|
35
|
+
export {
|
|
36
|
+
RegistryBlockDataProvider,
|
|
37
|
+
useRegistryBlockData,
|
|
38
|
+
type RegistryBlockDataChangeMeta,
|
|
39
|
+
type RegistryBlockDataValue,
|
|
40
|
+
type RegistryBlockEditSurfaceOptions,
|
|
41
|
+
type RegistryBlockNestedBlock,
|
|
42
|
+
type RegistryBlockRenderOptions,
|
|
43
|
+
type RegistryBlockRenderResult,
|
|
44
|
+
type RegistryBlockSideMapBlock,
|
|
45
|
+
} from "./RegistryBlockContext.js";
|
|
46
|
+
|
|
47
|
+
/* -------------------------------------------------------------------------- */
|
|
48
|
+
/* The generic registry-block side-map + Tiptap NodeView. */
|
|
49
|
+
/* */
|
|
50
|
+
/* This is the app-agnostic NodeView that renders registered block specs */
|
|
51
|
+
/* inside a `SharedRichEditor` document. Hosts mount the node produced by */
|
|
52
|
+
/* {@link createRegistryBlockNode} as an extra extension and wrap the editor */
|
|
53
|
+
/* in a {@link RegistryBlockDataProvider}, sourcing the typed block `data` */
|
|
54
|
+
/* from their own authoritative store (for example, PlanContent.blocks). The */
|
|
55
|
+
/* node itself carries only lightweight identity attrs (type/id/title/summary) */
|
|
56
|
+
/* plus an optional `__raw` verbatim-MDX attr for byte-stable source */
|
|
57
|
+
/* round-trips; the heavy typed `data` is threaded through the side-map */
|
|
58
|
+
/* context, keeping the doc small and the block data the single source of */
|
|
59
|
+
/* truth. */
|
|
60
|
+
/* -------------------------------------------------------------------------- */
|
|
61
|
+
|
|
62
|
+
function clickedInteractiveChild(target: HTMLElement) {
|
|
63
|
+
if (target.closest("button,input,textarea,select,a,[role='textbox']")) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The block drag-handle grip is a `role="button"` div that lives in the editor
|
|
68
|
+
// wrapper, and nested container blocks (columns/tabs) render their own inner
|
|
69
|
+
// editors — each its own grip. A container block's `onMouseDownCapture`
|
|
70
|
+
// selection handler runs in a SEPARATE React root (Tiptap mounts every node
|
|
71
|
+
// view as its own root), so its `stopPropagation()` halts the NATIVE mousedown
|
|
72
|
+
// right there, before it can reach an inner grip or inner editor. Treat the
|
|
73
|
+
// grip and anything inside a nested editor region as interactive so the outer
|
|
74
|
+
// container never hijacks a mousedown meant for inner machinery — this is what
|
|
75
|
+
// makes dragging a block OUT of / BETWEEN columns (a nested grip) work at all.
|
|
76
|
+
if (
|
|
77
|
+
target.closest(".drag-handle") ||
|
|
78
|
+
target.closest(".plan-nested-document-editor-region")
|
|
79
|
+
) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const blockNode = target.closest(".plan-block-node");
|
|
84
|
+
const editable = target.closest("[contenteditable='true']");
|
|
85
|
+
return !!blockNode && !!editable && blockNode.contains(editable);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* -------------------------------------------------------------------------- */
|
|
89
|
+
/* B. RegistryBlockNodeView (React) */
|
|
90
|
+
/* -------------------------------------------------------------------------- */
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Renders one registry-block atom. The block is non-editable as far as
|
|
94
|
+
* ProseMirror is concerned (`contentEditable={false}`); all interaction happens
|
|
95
|
+
* inside the registry-driven `<BlockView>`. Read vs edit is toggled by
|
|
96
|
+
* `props.selected` (the node is "selected" in the editor) AND the document being
|
|
97
|
+
* editable. `data-plan-interactive` keeps existing host click-guards from
|
|
98
|
+
* treating clicks inside the block as document clicks.
|
|
99
|
+
*/
|
|
100
|
+
export function RegistryBlockNodeView(props: NodeViewProps) {
|
|
101
|
+
const blockType = String(props.node.attrs.blockType ?? "");
|
|
102
|
+
const blockId = String(props.node.attrs.blockId ?? "");
|
|
103
|
+
const [panelOpen, setPanelOpen] = useState(false);
|
|
104
|
+
const [shellHovered, setShellHovered] = useState(false);
|
|
105
|
+
|
|
106
|
+
const sideMap = useRegistryBlockData();
|
|
107
|
+
|
|
108
|
+
// Optimistic edit override. `onBlockDataChange` commits into the host's own
|
|
109
|
+
// store (a ref the side-map context can't observe), so an edit does NOT
|
|
110
|
+
// re-render this node — the new data only reaches the view on the next full
|
|
111
|
+
// document reconcile, which lands after the autosave round-trip (seconds
|
|
112
|
+
// later) and is skipped entirely when the host treats the save as its own
|
|
113
|
+
// echo. That left quick toggles like the callout tone buttons visually frozen
|
|
114
|
+
// until reload. Holding the just-edited data locally re-renders this one node
|
|
115
|
+
// immediately, then releases once the authoritative block catches up to (or
|
|
116
|
+
// moves past) the value the edit was based on.
|
|
117
|
+
const [pendingEdit, setPendingEdit] = useState<{
|
|
118
|
+
data: unknown;
|
|
119
|
+
base: unknown;
|
|
120
|
+
} | null>(null);
|
|
121
|
+
const liveBlock = sideMap?.getBlock(blockId);
|
|
122
|
+
const liveData = liveBlock?.data;
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
if (pendingEdit && !Object.is(liveData, pendingEdit.base)) {
|
|
125
|
+
setPendingEdit(null);
|
|
126
|
+
}
|
|
127
|
+
}, [liveData, pendingEdit]);
|
|
128
|
+
const block =
|
|
129
|
+
liveBlock && pendingEdit && Object.is(liveData, pendingEdit.base)
|
|
130
|
+
? { ...liveBlock, data: pendingEdit.data }
|
|
131
|
+
: liveBlock;
|
|
132
|
+
const commitBlockData = (
|
|
133
|
+
nextData: unknown,
|
|
134
|
+
meta?: RegistryBlockDataChangeMeta,
|
|
135
|
+
) => {
|
|
136
|
+
setPendingEdit({ data: nextData, base: liveData });
|
|
137
|
+
sideMap?.onBlockDataChange(blockId, nextData, meta);
|
|
138
|
+
};
|
|
139
|
+
const editable = sideMap?.editable ?? false;
|
|
140
|
+
// In Notion-sync mode, flag blocks that have no Notion (NFM) analog so the
|
|
141
|
+
// author sees what won't push. Prose blocks aren't registry-block nodes, so
|
|
142
|
+
// this only ever covers structured blocks.
|
|
143
|
+
const incompatibleWithNotion =
|
|
144
|
+
(sideMap?.notionSync ?? false) &&
|
|
145
|
+
(sideMap?.isNotionIncompatibleType?.(blockType) ?? false);
|
|
146
|
+
|
|
147
|
+
// The block data isn't in the side-map yet (e.g. a freshly inserted node whose
|
|
148
|
+
// store entry hasn't been seeded). Render a graceful placeholder.
|
|
149
|
+
if (!block) {
|
|
150
|
+
return (
|
|
151
|
+
<NodeViewWrapper className="plan-block-node" data-block-id={blockId}>
|
|
152
|
+
<div
|
|
153
|
+
contentEditable={false}
|
|
154
|
+
data-plan-interactive
|
|
155
|
+
className="plan-block-node__placeholder rounded-md border border-dashed border-border px-3 py-2 text-sm text-muted-foreground"
|
|
156
|
+
>
|
|
157
|
+
{blockType ? `Loading ${blockType} block…` : "Loading block…"}
|
|
158
|
+
</div>
|
|
159
|
+
</NodeViewWrapper>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const selectNode = (event: ReactMouseEvent<HTMLElement>) => {
|
|
164
|
+
if (!editable) return;
|
|
165
|
+
const target = event.target;
|
|
166
|
+
if (target instanceof HTMLElement && clickedInteractiveChild(target))
|
|
167
|
+
return;
|
|
168
|
+
const pos = typeof props.getPos === "function" ? props.getPos() : null;
|
|
169
|
+
if (typeof pos !== "number") return;
|
|
170
|
+
try {
|
|
171
|
+
event.preventDefault();
|
|
172
|
+
event.stopPropagation();
|
|
173
|
+
const { view } = props.editor;
|
|
174
|
+
view.dispatch(
|
|
175
|
+
view.state.tr.setSelection(NodeSelection.create(view.state.doc, pos)),
|
|
176
|
+
);
|
|
177
|
+
view.focus();
|
|
178
|
+
} catch {
|
|
179
|
+
// Ignore stale positions during React/ProseMirror reconciliation.
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const updateShellHover = (event: ReactMouseEvent<HTMLElement>) => {
|
|
183
|
+
const target = event.target;
|
|
184
|
+
setShellHovered(
|
|
185
|
+
target instanceof HTMLElement &&
|
|
186
|
+
target.closest(".plan-block-node__shell") === event.currentTarget,
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// Choose how to render the block body:
|
|
191
|
+
// 1. A host registry adapter renders a registered block.
|
|
192
|
+
// 2. No spec, but the side-map provides `renderLegacyBlock` → delegate to the
|
|
193
|
+
// host's dispatcher (decision, legacy visual-questions, image, and any
|
|
194
|
+
// other type rendered by a bespoke component rather than the registry), so
|
|
195
|
+
// EVERY block type renders in the document exactly as it does in the
|
|
196
|
+
// per-block reader — never a bare title fallback.
|
|
197
|
+
// 3. Neither → a small non-crashing fallback.
|
|
198
|
+
let body: ReactNode;
|
|
199
|
+
let editSurface: ReactNode = null;
|
|
200
|
+
const registered = sideMap?.renderRegisteredBlock?.(block, {
|
|
201
|
+
blockType,
|
|
202
|
+
editable,
|
|
203
|
+
selected: props.selected,
|
|
204
|
+
shellHovered,
|
|
205
|
+
panelOpen,
|
|
206
|
+
setPanelOpen,
|
|
207
|
+
onChange: commitBlockData,
|
|
208
|
+
});
|
|
209
|
+
if (registered) {
|
|
210
|
+
body = registered.body;
|
|
211
|
+
editSurface = registered.editSurface ?? null;
|
|
212
|
+
} else if (sideMap?.renderLegacyBlock) {
|
|
213
|
+
// Self-editing legacy blocks (e.g. image) render their own edit affordance
|
|
214
|
+
// inside their overlay, so render them in edit mode and add NO separate
|
|
215
|
+
// corner edit surface — the block owns a single, self-contained overlay.
|
|
216
|
+
const selfEdits =
|
|
217
|
+
editable && Boolean(sideMap.legacyBlockSelfEdits?.(blockType));
|
|
218
|
+
body = sideMap.renderLegacyBlock(block, { editing: selfEdits });
|
|
219
|
+
if (editable && !selfEdits) {
|
|
220
|
+
// Prefer a host-provided schema/custom editor (a real form) over the raw
|
|
221
|
+
// JSON fallback when the host knows how to edit this legacy block type.
|
|
222
|
+
const customEditor = sideMap.renderLegacyBlockEditor?.(block, {
|
|
223
|
+
onChange: (nextData) => commitBlockData(nextData),
|
|
224
|
+
});
|
|
225
|
+
editSurface = (
|
|
226
|
+
<LegacyJsonEditSurface
|
|
227
|
+
block={block}
|
|
228
|
+
blockType={blockType}
|
|
229
|
+
open={panelOpen}
|
|
230
|
+
onOpenChange={setPanelOpen}
|
|
231
|
+
renderEditSurface={sideMap.renderEditSurface}
|
|
232
|
+
onChange={(nextBlock) => commitBlockData(nextBlock)}
|
|
233
|
+
selected={shellHovered}
|
|
234
|
+
customEditor={customEditor}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
body = (
|
|
240
|
+
<div className="plan-block-node__fallback rounded-md border border-border px-3 py-2 text-sm text-muted-foreground">
|
|
241
|
+
{block.title || blockType || "Unsupported block"}
|
|
242
|
+
</div>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<NodeViewWrapper
|
|
248
|
+
className="plan-block-node"
|
|
249
|
+
data-block-id={blockId}
|
|
250
|
+
// Mirror the block type onto the wrapper so the document flow can detect a
|
|
251
|
+
// RUN of consecutive blocks of the same type (e.g. api-endpoint) and
|
|
252
|
+
// collapse the divider + gap between them, matching the read-only path.
|
|
253
|
+
data-block-type={blockType || undefined}
|
|
254
|
+
data-plan-block-selected={props.selected ? "" : undefined}
|
|
255
|
+
data-notion-incompatible={incompatibleWithNotion ? "" : undefined}
|
|
256
|
+
onMouseDownCapture={selectNode}
|
|
257
|
+
>
|
|
258
|
+
<div
|
|
259
|
+
contentEditable={false}
|
|
260
|
+
data-plan-interactive
|
|
261
|
+
className="plan-block-node__shell relative"
|
|
262
|
+
onMouseEnter={updateShellHover}
|
|
263
|
+
onMouseMove={updateShellHover}
|
|
264
|
+
onMouseLeave={() => setShellHovered(false)}
|
|
265
|
+
>
|
|
266
|
+
{incompatibleWithNotion && (
|
|
267
|
+
<span
|
|
268
|
+
className="plan-block-notion-badge"
|
|
269
|
+
title="This block type has no Notion equivalent and won't sync to Notion."
|
|
270
|
+
>
|
|
271
|
+
Won't sync to Notion
|
|
272
|
+
</span>
|
|
273
|
+
)}
|
|
274
|
+
{body}
|
|
275
|
+
{editSurface && (
|
|
276
|
+
<div className="plan-block-node__edit absolute right-2 top-2 z-20">
|
|
277
|
+
{editSurface}
|
|
278
|
+
</div>
|
|
279
|
+
)}
|
|
280
|
+
</div>
|
|
281
|
+
</NodeViewWrapper>
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function LegacyJsonEditSurface({
|
|
286
|
+
block,
|
|
287
|
+
blockType,
|
|
288
|
+
open,
|
|
289
|
+
onOpenChange,
|
|
290
|
+
renderEditSurface,
|
|
291
|
+
onChange,
|
|
292
|
+
selected,
|
|
293
|
+
customEditor,
|
|
294
|
+
}: {
|
|
295
|
+
block: RegistryBlockSideMapBlock;
|
|
296
|
+
blockType?: string;
|
|
297
|
+
open: boolean;
|
|
298
|
+
onOpenChange: (open: boolean) => void;
|
|
299
|
+
renderEditSurface?: (options: RegistryBlockEditSurfaceOptions) => ReactNode;
|
|
300
|
+
onChange: (nextData: unknown) => void;
|
|
301
|
+
selected: boolean;
|
|
302
|
+
/**
|
|
303
|
+
* A host-provided form editor. When present it
|
|
304
|
+
* replaces the raw-JSON textarea + Save button; the form autosaves through its
|
|
305
|
+
* own `onChange`.
|
|
306
|
+
*/
|
|
307
|
+
customEditor?: ReactNode;
|
|
308
|
+
}) {
|
|
309
|
+
const serializedBlockData = useMemo(
|
|
310
|
+
() => JSON.stringify(block.data, null, 2),
|
|
311
|
+
[block.data],
|
|
312
|
+
);
|
|
313
|
+
const [draft, setDraft] = useState(serializedBlockData);
|
|
314
|
+
const [parseError, setParseError] = useState<string | null>(null);
|
|
315
|
+
|
|
316
|
+
useEffect(() => {
|
|
317
|
+
setDraft(serializedBlockData);
|
|
318
|
+
setParseError(null);
|
|
319
|
+
}, [block.id, serializedBlockData]);
|
|
320
|
+
|
|
321
|
+
const saveDraft = () => {
|
|
322
|
+
try {
|
|
323
|
+
const nextData = JSON.parse(draft) as unknown;
|
|
324
|
+
setParseError(null);
|
|
325
|
+
onChange(nextData);
|
|
326
|
+
onOpenChange(false);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
setParseError(
|
|
329
|
+
error instanceof Error ? error.message : "Invalid JSON data.",
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const trigger = (
|
|
335
|
+
<button
|
|
336
|
+
type="button"
|
|
337
|
+
data-plan-interactive
|
|
338
|
+
aria-label={`Edit ${block.title ?? "block"}`}
|
|
339
|
+
onClick={() => onOpenChange(true)}
|
|
340
|
+
className="an-block-edit-trigger flex size-7 items-center justify-center rounded-md border border-border bg-background text-muted-foreground opacity-0 shadow-sm transition-opacity hover:bg-muted hover:text-foreground focus-visible:opacity-100 data-[visible=true]:opacity-100"
|
|
341
|
+
data-visible={selected || open}
|
|
342
|
+
>
|
|
343
|
+
<IconPencil className="size-4" />
|
|
344
|
+
</button>
|
|
345
|
+
);
|
|
346
|
+
const jsonEditor = (
|
|
347
|
+
<div className="grid gap-3">
|
|
348
|
+
<textarea
|
|
349
|
+
data-plan-interactive
|
|
350
|
+
className="min-h-64 w-full rounded-md border border-input bg-background px-3 py-2 font-mono text-xs leading-5 text-foreground shadow-sm focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
|
|
351
|
+
value={draft}
|
|
352
|
+
aria-invalid={parseError ? true : undefined}
|
|
353
|
+
onChange={(event) => {
|
|
354
|
+
setDraft(event.target.value);
|
|
355
|
+
if (parseError) setParseError(null);
|
|
356
|
+
}}
|
|
357
|
+
/>
|
|
358
|
+
{parseError ? (
|
|
359
|
+
<p className="text-xs text-destructive" role="alert">
|
|
360
|
+
Invalid JSON: {parseError}
|
|
361
|
+
</p>
|
|
362
|
+
) : null}
|
|
363
|
+
<div className="flex justify-end">
|
|
364
|
+
<button
|
|
365
|
+
type="button"
|
|
366
|
+
data-plan-interactive
|
|
367
|
+
className="inline-flex h-8 items-center justify-center rounded-md bg-primary px-3 text-xs font-medium text-primary-foreground"
|
|
368
|
+
onClick={saveDraft}
|
|
369
|
+
>
|
|
370
|
+
Save
|
|
371
|
+
</button>
|
|
372
|
+
</div>
|
|
373
|
+
</div>
|
|
374
|
+
);
|
|
375
|
+
const editor = customEditor ?? jsonEditor;
|
|
376
|
+
if (!renderEditSurface) return open ? editor : trigger;
|
|
377
|
+
return renderEditSurface({
|
|
378
|
+
title: block.title ?? "Block",
|
|
379
|
+
open,
|
|
380
|
+
onOpenChange,
|
|
381
|
+
blockId: block.id,
|
|
382
|
+
blockType:
|
|
383
|
+
blockType ||
|
|
384
|
+
(typeof (block as { type?: unknown }).type === "string"
|
|
385
|
+
? ((block as unknown as { type: string }).type ?? "")
|
|
386
|
+
: "legacy"),
|
|
387
|
+
blockTitle: block.title,
|
|
388
|
+
blockSummary: block.summary,
|
|
389
|
+
blockData: block.data,
|
|
390
|
+
trigger,
|
|
391
|
+
children: editor,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/* -------------------------------------------------------------------------- */
|
|
396
|
+
/* A. registry-block Tiptap node factory */
|
|
397
|
+
/* -------------------------------------------------------------------------- */
|
|
398
|
+
|
|
399
|
+
/** Options for {@link createRegistryBlockNode}. */
|
|
400
|
+
export interface CreateRegistryBlockNodeOptions {
|
|
401
|
+
/**
|
|
402
|
+
* The Tiptap node name (e.g. `"planBlock"`). Hosts that serialize the doc by
|
|
403
|
+
* node name (plan's `plan-doc.ts` keys off `"planBlock"`) must pass the exact
|
|
404
|
+
* name their serializer expects.
|
|
405
|
+
*/
|
|
406
|
+
nodeName: string;
|
|
407
|
+
/**
|
|
408
|
+
* The HTML data-attribute that marks a serialized registry block on copy/paste
|
|
409
|
+
* round-trip (e.g. `"data-plan-block"`).
|
|
410
|
+
*/
|
|
411
|
+
dataTag: string;
|
|
412
|
+
/**
|
|
413
|
+
* Mint a fresh, unique block id for a given block type. Used by the dedupe
|
|
414
|
+
* plugin to re-mint duplicate / missing ids (paste/duplicate). Plan passes
|
|
415
|
+
* `createPlanBlockId`.
|
|
416
|
+
*/
|
|
417
|
+
mintId: (blockType: string) => string;
|
|
418
|
+
/** Node group (default `"block"`). */
|
|
419
|
+
group?: string;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Build the generic registry-block Tiptap atom node. Returns a Tiptap `Node`
|
|
424
|
+
* that:
|
|
425
|
+
* - carries identity attrs `blockType` / `blockId` / `title` / `summary`, a
|
|
426
|
+
* `sourceBlockId` (set when a duplicate is re-minted, so the host can copy the
|
|
427
|
+
* original block's data), and an optional `__raw` verbatim-MDX attr for
|
|
428
|
+
* byte-stable source round-trips;
|
|
429
|
+
* - is an atom + isolating + draggable block that renders through
|
|
430
|
+
* {@link RegistryBlockNodeView} (via `ReactNodeViewRenderer`);
|
|
431
|
+
* - installs a dedupe `appendTransaction` plugin that re-mints any duplicate or
|
|
432
|
+
* empty `blockId` (the classic paste/duplicate case), preserving the original
|
|
433
|
+
* block's id + side-map data and tagging its own transaction so it never
|
|
434
|
+
* loops.
|
|
435
|
+
*/
|
|
436
|
+
export function createRegistryBlockNode(
|
|
437
|
+
options: CreateRegistryBlockNodeOptions,
|
|
438
|
+
) {
|
|
439
|
+
const { nodeName, dataTag, mintId, group = "block" } = options;
|
|
440
|
+
const dedupeKey = new PluginKey(`${nodeName}DedupeIds`);
|
|
441
|
+
const keyboardGuardKey = new PluginKey(`${nodeName}KeyboardGuard`);
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Collect every `blockId` currently present on this node type in a doc, with
|
|
445
|
+
* the position of each node, so duplicate ids (from paste/duplicate) can be
|
|
446
|
+
* detected and re-minted.
|
|
447
|
+
*/
|
|
448
|
+
function collectEntries(state: EditorState): Array<{
|
|
449
|
+
pos: number;
|
|
450
|
+
blockType: string;
|
|
451
|
+
blockId: string;
|
|
452
|
+
sourceBlockId?: string;
|
|
453
|
+
}> {
|
|
454
|
+
const found: Array<{
|
|
455
|
+
pos: number;
|
|
456
|
+
blockType: string;
|
|
457
|
+
blockId: string;
|
|
458
|
+
sourceBlockId?: string;
|
|
459
|
+
}> = [];
|
|
460
|
+
state.doc.descendants((node, pos) => {
|
|
461
|
+
if (node.type.name === nodeName) {
|
|
462
|
+
found.push({
|
|
463
|
+
pos,
|
|
464
|
+
blockType: String(node.attrs.blockType ?? ""),
|
|
465
|
+
blockId: String(node.attrs.blockId ?? ""),
|
|
466
|
+
sourceBlockId:
|
|
467
|
+
typeof node.attrs.sourceBlockId === "string"
|
|
468
|
+
? node.attrs.sourceBlockId
|
|
469
|
+
: undefined,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
return true;
|
|
473
|
+
});
|
|
474
|
+
return found;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Build a transaction that re-mints any duplicate / missing ids in `state`, or
|
|
479
|
+
* `null` when nothing needs changing. Only the *later* duplicate (and any node
|
|
480
|
+
* with an empty id) is re-minted, so the original keeps its id and side-map
|
|
481
|
+
* data.
|
|
482
|
+
*/
|
|
483
|
+
function buildDedupeTransaction(state: EditorState) {
|
|
484
|
+
const entries = collectEntries(state);
|
|
485
|
+
if (entries.length === 0) return null;
|
|
486
|
+
|
|
487
|
+
const seen = new Set<string>();
|
|
488
|
+
let tr = state.tr;
|
|
489
|
+
let changed = false;
|
|
490
|
+
|
|
491
|
+
for (const entry of entries) {
|
|
492
|
+
const needsNewId = !entry.blockId || seen.has(entry.blockId);
|
|
493
|
+
if (needsNewId) {
|
|
494
|
+
const freshId = mintId(entry.blockType || "block");
|
|
495
|
+
const node = state.doc.nodeAt(entry.pos);
|
|
496
|
+
if (node) {
|
|
497
|
+
tr = tr.setNodeMarkup(entry.pos, undefined, {
|
|
498
|
+
...node.attrs,
|
|
499
|
+
blockId: freshId,
|
|
500
|
+
sourceBlockId: entry.sourceBlockId || entry.blockId || null,
|
|
501
|
+
});
|
|
502
|
+
changed = true;
|
|
503
|
+
}
|
|
504
|
+
seen.add(freshId);
|
|
505
|
+
} else {
|
|
506
|
+
seen.add(entry.blockId);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return changed ? tr.setMeta(dedupeKey, true) : null;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const selectedRegistryBlock = (state: EditorState) =>
|
|
514
|
+
state.selection instanceof NodeSelection &&
|
|
515
|
+
state.selection.node.type.name === nodeName;
|
|
516
|
+
|
|
517
|
+
const isMutatingKey = (event: KeyboardEvent) => {
|
|
518
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return false;
|
|
519
|
+
if (event.key === "Enter") return true;
|
|
520
|
+
return event.key.length === 1;
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
return Node.create({
|
|
524
|
+
name: nodeName,
|
|
525
|
+
group,
|
|
526
|
+
atom: true,
|
|
527
|
+
draggable: true,
|
|
528
|
+
selectable: true,
|
|
529
|
+
isolating: true,
|
|
530
|
+
|
|
531
|
+
addAttributes() {
|
|
532
|
+
return {
|
|
533
|
+
blockType: { default: "" },
|
|
534
|
+
blockId: { default: "" },
|
|
535
|
+
title: { default: null },
|
|
536
|
+
summary: { default: null },
|
|
537
|
+
sourceBlockId: { default: null },
|
|
538
|
+
// Optional verbatim source for hosts that need byte-identical
|
|
539
|
+
// source-format round-trips without React (server pull, hashing). Plan
|
|
540
|
+
// never sets this.
|
|
541
|
+
__raw: { default: null, rendered: false },
|
|
542
|
+
};
|
|
543
|
+
},
|
|
544
|
+
|
|
545
|
+
parseHTML() {
|
|
546
|
+
return [
|
|
547
|
+
{
|
|
548
|
+
tag: `div[${dataTag}]`,
|
|
549
|
+
getAttrs: (element) => {
|
|
550
|
+
const node = element as HTMLElement;
|
|
551
|
+
return {
|
|
552
|
+
blockType: node.getAttribute("data-block-type") || "",
|
|
553
|
+
blockId: node.getAttribute("data-block-id") || "",
|
|
554
|
+
title: node.getAttribute("data-title") || null,
|
|
555
|
+
summary: node.getAttribute("data-summary") || null,
|
|
556
|
+
sourceBlockId: node.getAttribute("data-source-block-id") || null,
|
|
557
|
+
};
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
];
|
|
561
|
+
},
|
|
562
|
+
|
|
563
|
+
renderHTML({ HTMLAttributes }) {
|
|
564
|
+
return [
|
|
565
|
+
"div",
|
|
566
|
+
mergeAttributes(HTMLAttributes, {
|
|
567
|
+
[dataTag]: "",
|
|
568
|
+
"data-block-type": HTMLAttributes.blockType ?? "",
|
|
569
|
+
"data-block-id": HTMLAttributes.blockId ?? "",
|
|
570
|
+
"data-title": HTMLAttributes.title ?? undefined,
|
|
571
|
+
"data-summary": HTMLAttributes.summary ?? undefined,
|
|
572
|
+
"data-source-block-id": HTMLAttributes.sourceBlockId ?? undefined,
|
|
573
|
+
}),
|
|
574
|
+
];
|
|
575
|
+
},
|
|
576
|
+
|
|
577
|
+
addNodeView() {
|
|
578
|
+
return ReactNodeViewRenderer(RegistryBlockNodeView);
|
|
579
|
+
},
|
|
580
|
+
|
|
581
|
+
addProseMirrorPlugins() {
|
|
582
|
+
return [
|
|
583
|
+
new Plugin({
|
|
584
|
+
key: dedupeKey,
|
|
585
|
+
appendTransaction(transactions, _oldState, newState) {
|
|
586
|
+
// Ignore our own re-mint, and skip when nothing changed the doc.
|
|
587
|
+
if (
|
|
588
|
+
transactions.some((transaction) =>
|
|
589
|
+
transaction.getMeta(dedupeKey),
|
|
590
|
+
) ||
|
|
591
|
+
!transactions.some((transaction) => transaction.docChanged)
|
|
592
|
+
) {
|
|
593
|
+
return null;
|
|
594
|
+
}
|
|
595
|
+
return buildDedupeTransaction(newState);
|
|
596
|
+
},
|
|
597
|
+
}),
|
|
598
|
+
new Plugin({
|
|
599
|
+
key: keyboardGuardKey,
|
|
600
|
+
props: {
|
|
601
|
+
handleClickOn(view, _pos, node, nodePos, event, direct) {
|
|
602
|
+
if (node.type.name !== nodeName || !direct) return false;
|
|
603
|
+
if (
|
|
604
|
+
event.target instanceof HTMLElement &&
|
|
605
|
+
clickedInteractiveChild(event.target)
|
|
606
|
+
) {
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
event.preventDefault();
|
|
610
|
+
view.dispatch(
|
|
611
|
+
view.state.tr.setSelection(
|
|
612
|
+
NodeSelection.create(view.state.doc, nodePos),
|
|
613
|
+
),
|
|
614
|
+
);
|
|
615
|
+
view.focus();
|
|
616
|
+
return true;
|
|
617
|
+
},
|
|
618
|
+
handleKeyDown(view, event) {
|
|
619
|
+
if (!selectedRegistryBlock(view.state) || !isMutatingKey(event))
|
|
620
|
+
return false;
|
|
621
|
+
event.preventDefault();
|
|
622
|
+
return true;
|
|
623
|
+
},
|
|
624
|
+
handleTextInput(view) {
|
|
625
|
+
return selectedRegistryBlock(view.state);
|
|
626
|
+
},
|
|
627
|
+
handlePaste(view, event) {
|
|
628
|
+
if (!selectedRegistryBlock(view.state)) return false;
|
|
629
|
+
event.preventDefault();
|
|
630
|
+
return true;
|
|
631
|
+
},
|
|
632
|
+
handleDOMEvents: {
|
|
633
|
+
beforeinput(view, event) {
|
|
634
|
+
if (!selectedRegistryBlock(view.state)) return false;
|
|
635
|
+
const inputEvent = event as InputEvent;
|
|
636
|
+
if (
|
|
637
|
+
!inputEvent.inputType ||
|
|
638
|
+
(!inputEvent.inputType.startsWith("insert") &&
|
|
639
|
+
inputEvent.inputType !== "formatSetBlockTextDirection")
|
|
640
|
+
) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
event.preventDefault();
|
|
644
|
+
return true;
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
},
|
|
648
|
+
}),
|
|
649
|
+
];
|
|
650
|
+
},
|
|
651
|
+
});
|
|
652
|
+
}
|