@sixb/app 0.2.3 → 0.2.5
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/assets/connector-icons/README.md +1 -0
- package/assets/connector-icons/ctm.png +0 -0
- package/dist/agent/activity-label.d.ts.map +1 -1
- package/dist/agent/agent-chat.d.ts +3 -1
- package/dist/agent/agent-chat.d.ts.map +1 -1
- package/dist/agent/agent-panel.d.ts.map +1 -1
- package/dist/agent/artifact-panel.d.ts +3 -1
- package/dist/agent/artifact-panel.d.ts.map +1 -1
- package/dist/agent/background/background-tool-row.d.ts.map +1 -1
- package/dist/agent/background/interpret.d.ts +1 -1
- package/dist/agent/background/interpret.d.ts.map +1 -1
- package/dist/agent/components/composer-settings.d.ts +1 -1
- package/dist/agent/components/composer-settings.d.ts.map +1 -1
- package/dist/agent/components/composer.d.ts +22 -5
- package/dist/agent/components/composer.d.ts.map +1 -1
- package/dist/agent/components/context-meter.d.ts +1 -1
- package/dist/agent/components/context-meter.d.ts.map +1 -1
- package/dist/agent/components/conversation-panel.d.ts +8 -2
- package/dist/agent/components/conversation-panel.d.ts.map +1 -1
- package/dist/agent/components/message-parts.d.ts +2 -1
- package/dist/agent/components/message-parts.d.ts.map +1 -1
- package/dist/agent/components/message-view.d.ts.map +1 -1
- package/dist/agent/components/pending-messages.d.ts +20 -0
- package/dist/agent/components/pending-messages.d.ts.map +1 -0
- package/dist/agent/components/sandbox-recovery.d.ts +11 -2
- package/dist/agent/components/sandbox-recovery.d.ts.map +1 -1
- package/dist/agent/components/threads-button.d.ts +1 -1
- package/dist/agent/components/threads-button.d.ts.map +1 -1
- package/dist/agent/components/transcript.d.ts +5 -0
- package/dist/agent/components/transcript.d.ts.map +1 -1
- package/dist/agent/conversation.d.ts +3 -6
- package/dist/agent/conversation.d.ts.map +1 -1
- package/dist/agent/delegation/agent-exchange.d.ts +16 -0
- package/dist/agent/delegation/agent-exchange.d.ts.map +1 -0
- package/dist/agent/delegation/child-card.d.ts +3 -3
- package/dist/agent/delegation/child-card.d.ts.map +1 -1
- package/dist/agent/delegation/child-parts.d.ts +1 -10
- package/dist/agent/delegation/child-parts.d.ts.map +1 -1
- package/dist/agent/delegation/child-work.d.ts.map +1 -1
- package/dist/agent/delegation/interpret.d.ts +9 -10
- package/dist/agent/delegation/interpret.d.ts.map +1 -1
- package/dist/agent/delegation/use-children.d.ts.map +1 -1
- package/dist/agent/file-edits/file-edit-tool-view.d.ts +10 -0
- package/dist/agent/file-edits/file-edit-tool-view.d.ts.map +1 -0
- package/dist/agent/file-edits/interpret.d.ts +25 -0
- package/dist/agent/file-edits/interpret.d.ts.map +1 -0
- package/dist/agent/hooks/use-agent-conversation.d.ts +16 -4
- package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
- package/dist/agent/hooks/use-thread-lists.d.ts.map +1 -1
- package/dist/agent/hooks/use-thread-stream.d.ts.map +1 -1
- package/dist/agent/i18n/en.d.ts +114 -5
- package/dist/agent/i18n/en.d.ts.map +1 -1
- package/dist/agent/i18n/fr.d.ts.map +1 -1
- package/dist/agent/i18n/index.d.ts +190 -8
- package/dist/agent/i18n/index.d.ts.map +1 -1
- package/dist/agent/inbox-handoff.d.ts +10 -0
- package/dist/agent/inbox-handoff.d.ts.map +1 -0
- package/dist/agent/live-run.d.ts +8 -1
- package/dist/agent/live-run.d.ts.map +1 -1
- package/dist/agent/message-agent/interpret.d.ts +2 -2
- package/dist/agent/message-agent/interpret.d.ts.map +1 -1
- package/dist/agent/publish-artifact/interpret.d.ts +1 -1
- package/dist/agent/publish-artifact/interpret.d.ts.map +1 -1
- package/dist/agent/questions/interpret.d.ts +31 -0
- package/dist/agent/questions/interpret.d.ts.map +1 -0
- package/dist/agent/questions/question-card.d.ts +16 -0
- package/dist/agent/questions/question-card.d.ts.map +1 -0
- package/dist/agent/questions/question-composer.d.ts +15 -0
- package/dist/agent/questions/question-composer.d.ts.map +1 -0
- package/dist/agent/read/interpret.d.ts +8 -1
- package/dist/agent/read/interpret.d.ts.map +1 -1
- package/dist/agent/read/read-tool-view.d.ts.map +1 -1
- package/dist/agent/search/interpret.d.ts +101 -0
- package/dist/agent/search/interpret.d.ts.map +1 -0
- package/dist/agent/search/search-tool-view.d.ts +10 -0
- package/dist/agent/search/search-tool-view.d.ts.map +1 -0
- package/dist/agent/thread-state.d.ts +26 -0
- package/dist/agent/thread-state.d.ts.map +1 -0
- package/dist/agent/tool-views/built-in.d.ts.map +1 -1
- package/dist/agent/tool-views/registry.d.ts +5 -6
- package/dist/agent/tool-views/registry.d.ts.map +1 -1
- package/dist/agent/types.d.ts +5 -1
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/view-file/view-file-tool-view.d.ts +2 -1
- package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
- package/dist/agent/waits.d.ts +14 -0
- package/dist/agent/waits.d.ts.map +1 -0
- package/dist/artifacts/card-preview.d.ts.map +1 -1
- package/dist/artifacts/edit-session.d.ts +11 -1
- package/dist/artifacts/edit-session.d.ts.map +1 -1
- package/dist/artifacts/editor/block-handle.d.ts +20 -0
- package/dist/artifacts/editor/block-handle.d.ts.map +1 -0
- package/dist/artifacts/editor/blocks.d.ts +34 -0
- package/dist/artifacts/editor/blocks.d.ts.map +1 -0
- package/dist/artifacts/editor/callout-view.d.ts +7 -0
- package/dist/artifacts/editor/callout-view.d.ts.map +1 -0
- package/dist/artifacts/editor/document-editor.d.ts +30 -0
- package/dist/artifacts/editor/document-editor.d.ts.map +1 -0
- package/dist/artifacts/editor/figure-view.d.ts +12 -0
- package/dist/artifacts/editor/figure-view.d.ts.map +1 -0
- package/dist/artifacts/editor/find-blocks.d.ts +6 -0
- package/dist/artifacts/editor/find-blocks.d.ts.map +1 -0
- package/dist/artifacts/editor/markdown-document.d.ts +12 -0
- package/dist/artifacts/editor/markdown-document.d.ts.map +1 -0
- package/dist/artifacts/editor/messages.d.ts +4 -0
- package/dist/artifacts/editor/messages.d.ts.map +1 -0
- package/dist/artifacts/editor/schema.d.ts +21 -0
- package/dist/artifacts/editor/schema.d.ts.map +1 -0
- package/dist/artifacts/editor/selection-toolbar.d.ts +35 -0
- package/dist/artifacts/editor/selection-toolbar.d.ts.map +1 -0
- package/dist/artifacts/editor/slash-menu.d.ts +43 -0
- package/dist/artifacts/editor/slash-menu.d.ts.map +1 -0
- package/dist/artifacts/editor/upload.d.ts +12 -0
- package/dist/artifacts/editor/upload.d.ts.map +1 -0
- package/dist/artifacts/file-source.d.ts +5 -0
- package/dist/artifacts/file-source.d.ts.map +1 -1
- package/dist/artifacts/frame-editor/bundle.d.ts +7 -0
- package/dist/artifacts/frame-editor/bundle.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/commands.d.ts +93 -0
- package/dist/artifacts/frame-editor/commands.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/entry.d.ts +2 -0
- package/dist/artifacts/frame-editor/entry.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/frame.d.ts +5 -0
- package/dist/artifacts/frame-editor/frame.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/keys.d.ts +8 -0
- package/dist/artifacts/frame-editor/keys.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/protocol.d.ts +186 -0
- package/dist/artifacts/frame-editor/protocol.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/schema.d.ts +34 -0
- package/dist/artifacts/frame-editor/schema.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/style.d.ts +2 -0
- package/dist/artifacts/frame-editor/style.d.ts.map +1 -0
- package/dist/artifacts/frame.d.ts +2 -2
- package/dist/artifacts/frame.d.ts.map +1 -1
- package/dist/artifacts/live/boards.d.ts +2 -0
- package/dist/artifacts/live/boards.d.ts.map +1 -1
- package/dist/artifacts/live/document.d.ts +32 -0
- package/dist/artifacts/live/document.d.ts.map +1 -0
- package/dist/artifacts/live/header.d.ts +4 -4
- package/dist/artifacts/live/header.d.ts.map +1 -1
- package/dist/artifacts/live/presence.d.ts.map +1 -1
- package/dist/artifacts/live/session.d.ts +2 -0
- package/dist/artifacts/live/session.d.ts.map +1 -1
- package/dist/artifacts/live/view.d.ts +4 -3
- package/dist/artifacts/live/view.d.ts.map +1 -1
- package/dist/artifacts/messages.d.ts +278 -2
- package/dist/artifacts/messages.d.ts.map +1 -1
- package/dist/artifacts/pdf.d.ts +8 -0
- package/dist/artifacts/pdf.d.ts.map +1 -0
- package/dist/artifacts/queries.d.ts +80 -0
- package/dist/artifacts/queries.d.ts.map +1 -1
- package/dist/artifacts/references/references.d.ts +3 -3
- package/dist/artifacts/references/references.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/board-editor.d.ts +11 -4
- package/dist/artifacts/renderers/canvas/board-editor.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/board.d.ts +14 -2
- package/dist/artifacts/renderers/canvas/board.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/canvas.d.ts +4 -3
- package/dist/artifacts/renderers/canvas/canvas.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/comment-pins.d.ts +4 -2
- package/dist/artifacts/renderers/canvas/comment-pins.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/comments-panel.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/comments.d.ts +2 -1
- package/dist/artifacts/renderers/canvas/comments.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/controls.d.ts +15 -4
- package/dist/artifacts/renderers/canvas/controls.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/focus.d.ts +19 -5
- package/dist/artifacts/renderers/canvas/focus.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/gestures.d.ts +46 -0
- package/dist/artifacts/renderers/canvas/gestures.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-resize.d.ts +21 -0
- package/dist/artifacts/renderers/canvas/page-resize.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-size-controls.d.ts +21 -0
- package/dist/artifacts/renderers/canvas/page-size-controls.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-size.d.ts +93 -0
- package/dist/artifacts/renderers/canvas/page-size.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/page-zoom.d.ts +64 -0
- package/dist/artifacts/renderers/canvas/page-zoom.d.ts.map +1 -0
- package/dist/artifacts/renderers/canvas/shown-size.d.ts +26 -0
- package/dist/artifacts/renderers/canvas/shown-size.d.ts.map +1 -0
- package/dist/artifacts/renderers/designed-document.d.ts +28 -0
- package/dist/artifacts/renderers/designed-document.d.ts.map +1 -0
- package/dist/artifacts/renderers/document-tree.d.ts +2 -0
- package/dist/artifacts/renderers/document-tree.d.ts.map +1 -1
- package/dist/artifacts/renderers/document.d.ts +9 -1
- package/dist/artifacts/renderers/document.d.ts.map +1 -1
- package/dist/artifacts/renderers/live-frame.d.ts +145 -0
- package/dist/artifacts/renderers/live-frame.d.ts.map +1 -0
- package/dist/artifacts/renderers/page-editor.d.ts +16 -0
- package/dist/artifacts/renderers/page-editor.d.ts.map +1 -0
- package/dist/artifacts/renderers/page.d.ts +10 -1
- package/dist/artifacts/renderers/page.d.ts.map +1 -1
- package/dist/artifacts/viewer.d.ts.map +1 -1
- package/dist/{chunk-001c4gnk.js → chunk-4fex83k2.js} +19965 -13027
- package/dist/chunk-4fex83k2.js.map +239 -0
- package/dist/{chunk-5crvhjp2.js → chunk-4wm09prf.js} +1633 -1999
- package/dist/chunk-4wm09prf.js.map +27 -0
- package/dist/{chunk-fwb9mxwm.js → chunk-5sgac68y.js} +1 -1
- package/dist/{chunk-kes4ygmz.js → chunk-69qgh20f.js} +1 -1
- package/dist/chunk-y51c4tsz.js +11 -0
- package/dist/chunk-y51c4tsz.js.map +10 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -6
- package/dist/index.js.map +5 -5
- package/dist/pages/console/data/messages.d.ts +6 -0
- package/dist/pages/console/data/messages.d.ts.map +1 -1
- package/dist/pages/console/data/shared.d.ts.map +1 -1
- package/dist/pages.js +12 -5
- package/dist/pages.js.map +6 -6
- package/dist/shell/search-command.d.ts.map +1 -1
- package/package.json +22 -5
- package/src/agent/activity-label.ts +9 -1
- package/src/agent/agent-chat.tsx +57 -37
- package/src/agent/agent-execution-trace.tsx +2 -2
- package/src/agent/agent-panel.tsx +6 -4
- package/src/agent/agent.css +254 -0
- package/src/agent/artifact-panel.tsx +77 -22
- package/src/agent/background/background-tool-row.tsx +24 -10
- package/src/agent/background/interpret.ts +60 -51
- package/src/agent/components/composer-settings.tsx +20 -10
- package/src/agent/components/composer.tsx +342 -104
- package/src/agent/components/context-meter.tsx +7 -3
- package/src/agent/components/conversation-panel.tsx +96 -37
- package/src/agent/components/message-parts.tsx +390 -64
- package/src/agent/components/message-view.tsx +12 -2
- package/src/agent/components/pending-messages.tsx +338 -0
- package/src/agent/components/sandbox-recovery.tsx +36 -17
- package/src/agent/components/threads-button.tsx +3 -2
- package/src/agent/components/transcript.tsx +92 -45
- package/src/agent/conversation.ts +203 -54
- package/src/agent/delegation/agent-exchange.tsx +53 -0
- package/src/agent/delegation/child-card.tsx +6 -40
- package/src/agent/delegation/child-parts.tsx +1 -84
- package/src/agent/delegation/child-work.tsx +19 -5
- package/src/agent/delegation/interpret.ts +64 -61
- package/src/agent/delegation/model.ts +1 -1
- package/src/agent/file-edits/file-edit-tool-view.tsx +126 -0
- package/src/agent/file-edits/interpret.ts +120 -0
- package/src/agent/hooks/use-agent-conversation.ts +360 -92
- package/src/agent/hooks/use-thread-stream.ts +10 -0
- package/src/agent/i18n/en.ts +134 -16
- package/src/agent/i18n/fr.ts +122 -16
- package/src/agent/inbox-handoff.ts +47 -0
- package/src/agent/live-run.ts +23 -2
- package/src/agent/message-agent/interpret.ts +4 -3
- package/src/agent/publish-artifact/interpret.ts +14 -5
- package/src/agent/publish-artifact/publish-artifact-tool-view.tsx +2 -2
- package/src/agent/questions/interpret.ts +90 -0
- package/src/agent/questions/question-card.tsx +57 -0
- package/src/agent/questions/question-composer.tsx +257 -0
- package/src/agent/react-router.tsx +2 -2
- package/src/agent/read/interpret.ts +34 -1
- package/src/agent/read/read-tool-view.tsx +20 -4
- package/src/agent/search/interpret.ts +367 -0
- package/src/agent/search/search-tool-view.tsx +235 -0
- package/src/agent/thread-state.ts +114 -0
- package/src/agent/tool-views/built-in.tsx +33 -6
- package/src/agent/tool-views/registry.tsx +28 -2
- package/src/agent/types.ts +7 -0
- package/src/agent/view-file/view-file-tool-view.tsx +33 -19
- package/src/agent/waits.ts +50 -0
- package/src/artifacts/artifacts.css +434 -0
- package/src/artifacts/card-preview.tsx +6 -1
- package/src/artifacts/edit-session.tsx +15 -1
- package/src/artifacts/editor/block-handle.tsx +278 -0
- package/src/artifacts/editor/blocks.ts +216 -0
- package/src/artifacts/editor/callout-view.tsx +31 -0
- package/src/artifacts/editor/document-editor.tsx +484 -0
- package/src/artifacts/editor/figure-view.tsx +66 -0
- package/src/artifacts/editor/find-blocks.ts +19 -0
- package/src/artifacts/editor/markdown-document.tsx +72 -0
- package/src/artifacts/editor/messages.ts +4 -0
- package/src/artifacts/editor/schema.ts +159 -0
- package/src/artifacts/editor/selection-toolbar.tsx +364 -0
- package/src/artifacts/editor/slash-menu.tsx +251 -0
- package/src/artifacts/editor/upload.ts +40 -0
- package/src/artifacts/file-source.ts +11 -0
- package/src/artifacts/frame-editor/bundle.ts +8 -0
- package/src/artifacts/frame-editor/commands.ts +565 -0
- package/src/artifacts/frame-editor/entry.ts +6 -0
- package/src/artifacts/frame-editor/frame.ts +847 -0
- package/src/artifacts/frame-editor/generated/editor.txt +149 -0
- package/src/artifacts/frame-editor/generated.d.ts +5 -0
- package/src/artifacts/frame-editor/keys.ts +323 -0
- package/src/artifacts/frame-editor/protocol.ts +151 -0
- package/src/artifacts/frame-editor/schema.ts +293 -0
- package/src/artifacts/frame-editor/style.ts +58 -0
- package/src/artifacts/frame.tsx +14 -5
- package/src/artifacts/live/boards.ts +46 -3
- package/src/artifacts/live/document.ts +89 -0
- package/src/artifacts/live/header.tsx +9 -14
- package/src/artifacts/live/presence.ts +5 -1
- package/src/artifacts/live/session.ts +4 -2
- package/src/artifacts/live/view.ts +14 -8
- package/src/artifacts/messages.ts +192 -2
- package/src/artifacts/pdf.ts +50 -0
- package/src/artifacts/references/references.tsx +46 -11
- package/src/artifacts/renderers/canvas/board-editor.tsx +44 -155
- package/src/artifacts/renderers/canvas/board.tsx +70 -8
- package/src/artifacts/renderers/canvas/canvas.tsx +66 -15
- package/src/artifacts/renderers/canvas/comment-pins.tsx +5 -0
- package/src/artifacts/renderers/canvas/comments-panel.tsx +58 -1
- package/src/artifacts/renderers/canvas/comments.ts +8 -4
- package/src/artifacts/renderers/canvas/controls.tsx +64 -30
- package/src/artifacts/renderers/canvas/focus.tsx +200 -56
- package/src/artifacts/renderers/canvas/gestures.ts +265 -0
- package/src/artifacts/renderers/canvas/page-resize.tsx +542 -0
- package/src/artifacts/renderers/canvas/page-size-controls.tsx +193 -0
- package/src/artifacts/renderers/canvas/page-size.ts +218 -0
- package/src/artifacts/renderers/canvas/page-zoom.ts +280 -0
- package/src/artifacts/renderers/canvas/shown-size.ts +105 -0
- package/src/artifacts/renderers/designed-document.tsx +277 -0
- package/src/artifacts/renderers/document-tree.ts +9 -0
- package/src/artifacts/renderers/document.tsx +13 -102
- package/src/artifacts/renderers/live-frame.ts +163 -0
- package/src/artifacts/renderers/page-editor.tsx +613 -0
- package/src/artifacts/renderers/page.tsx +21 -4
- package/src/artifacts/viewer.tsx +101 -41
- package/src/index.ts +8 -0
- package/src/pages/console/data/messages.ts +6 -0
- package/src/pages/console/data/projections.tsx +4 -0
- package/src/pages/console/data/shared.tsx +1 -0
- package/src/pages/console/ontology/lineage/trail.tsx +3 -0
- package/src/shell/search-command.tsx +2 -0
- package/dist/chunk-001c4gnk.js.map +0 -204
- package/dist/chunk-5crvhjp2.js.map +0 -28
- /package/dist/{chunk-fwb9mxwm.js.map → chunk-5sgac68y.js.map} +0 -0
- /package/dist/{chunk-kes4ygmz.js.map → chunk-69qgh20f.js.map} +0 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { createHeadingSlugger, isArtifactAddress } from "@sixb/core/artifacts"
|
|
2
|
+
import { blocksToMarkdown, LIVE_FILES, markdownToBlocks } from "@sixb/core/artifacts/live"
|
|
3
|
+
import { toast } from "@sixb/ui/components"
|
|
4
|
+
import { cn } from "@sixb/ui/lib/utils"
|
|
5
|
+
import { type Editor, Extension } from "@tiptap/core"
|
|
6
|
+
import { Collaboration } from "@tiptap/extension-collaboration"
|
|
7
|
+
import { CollaborationCaret } from "@tiptap/extension-collaboration-caret"
|
|
8
|
+
import { Placeholder } from "@tiptap/extensions"
|
|
9
|
+
import { Fragment, type Node as ProseMirrorNode, Slice } from "@tiptap/pm/model"
|
|
10
|
+
import { Plugin, type Transaction } from "@tiptap/pm/state"
|
|
11
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view"
|
|
12
|
+
import { EditorContent, ReactNodeViewRenderer, useEditor } from "@tiptap/react"
|
|
13
|
+
import { useEffect, useMemo, useRef, useState } from "react"
|
|
14
|
+
import type { Awareness } from "y-protocols/awareness"
|
|
15
|
+
import type * as Y from "yjs"
|
|
16
|
+
import { BlockHandle } from "./block-handle"
|
|
17
|
+
import { blockTypes } from "./blocks"
|
|
18
|
+
import { CalloutView } from "./callout-view"
|
|
19
|
+
import { FigureContext, FigureView } from "./figure-view"
|
|
20
|
+
import type { EditorMessages } from "./messages"
|
|
21
|
+
import { CalloutNode, documentSchemaExtensions, Figure } from "./schema"
|
|
22
|
+
import { SelectionToolbar } from "./selection-toolbar"
|
|
23
|
+
import { SlashCommand, SlashMenu, SlashMenuStore } from "./slash-menu"
|
|
24
|
+
|
|
25
|
+
// A Markdown document edited as a page of notes: click anywhere and type. It edits the live
|
|
26
|
+
// document's blocks, so everyone editing it at once sees each other's words and carets, and the
|
|
27
|
+
// server writes them back as the document's Markdown. It reads as the document page does, with
|
|
28
|
+
// the same type and column.
|
|
29
|
+
|
|
30
|
+
export interface DocumentEditorProps {
|
|
31
|
+
/** `document.md` as the live document holds it. */
|
|
32
|
+
readonly fragment: Y.XmlFragment
|
|
33
|
+
readonly awareness: Awareness
|
|
34
|
+
/** The reader may change it. */
|
|
35
|
+
readonly editable: boolean
|
|
36
|
+
/** Where one of its image paths is served from. */
|
|
37
|
+
readonly imageSource: (src: string) => string | null
|
|
38
|
+
readonly messages: EditorMessages
|
|
39
|
+
/** The artifact's theme, as the document page applies it. */
|
|
40
|
+
readonly theme?: { readonly className?: string; readonly style?: React.CSSProperties }
|
|
41
|
+
/**
|
|
42
|
+
* Stores an image a person adds as a file of the document, and says where its blocks point to
|
|
43
|
+
* it; throws, saying why, when it cannot. Without it, images are not added.
|
|
44
|
+
*/
|
|
45
|
+
readonly uploadImage?: (file: File) => Promise<{ readonly src: string; readonly alt: string }>
|
|
46
|
+
/** The section to open at: the slug of its heading, as the document page names it. */
|
|
47
|
+
readonly part?: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function DocumentEditor({
|
|
51
|
+
fragment,
|
|
52
|
+
awareness,
|
|
53
|
+
editable,
|
|
54
|
+
imageSource,
|
|
55
|
+
messages,
|
|
56
|
+
theme,
|
|
57
|
+
uploadImage,
|
|
58
|
+
part,
|
|
59
|
+
}: DocumentEditorProps) {
|
|
60
|
+
const picker = useRef<HTMLInputElement | null>(null)
|
|
61
|
+
const editorRef = useRef<Editor | null>(null)
|
|
62
|
+
const types = useMemo(
|
|
63
|
+
() =>
|
|
64
|
+
blockTypes(messages.blocks, uploadImage ? { pickImage: () => picker.current?.click() } : {}),
|
|
65
|
+
[messages.blocks, uploadImage]
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
/** Adds images where `at` is, else where the selection is, each as a figure of its own. */
|
|
69
|
+
const addImages = async (files: readonly File[], at?: number) => {
|
|
70
|
+
const editor = editorRef.current
|
|
71
|
+
if (!editor || !uploadImage) return
|
|
72
|
+
// Where they go moves with what anyone changes while they upload.
|
|
73
|
+
let pos = at ?? editor.state.selection.to
|
|
74
|
+
const follow = ({ transaction }: { readonly transaction: Transaction }) => {
|
|
75
|
+
pos = transaction.mapping.map(pos)
|
|
76
|
+
}
|
|
77
|
+
editor.on("transaction", follow)
|
|
78
|
+
try {
|
|
79
|
+
for (const file of files) {
|
|
80
|
+
try {
|
|
81
|
+
const { src, alt } = await uploadImage(file)
|
|
82
|
+
const figure = { type: "image", attrs: { src, alt, title: null } }
|
|
83
|
+
editor
|
|
84
|
+
.chain()
|
|
85
|
+
.insertContentAt(Math.min(pos, editor.state.doc.content.size), figure)
|
|
86
|
+
.focus()
|
|
87
|
+
.run()
|
|
88
|
+
pos = editor.state.selection.to
|
|
89
|
+
} catch (error) {
|
|
90
|
+
toast.error(
|
|
91
|
+
error instanceof Error && error.message ? error.message : messages.imageFailed
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
} finally {
|
|
96
|
+
editor.off("transaction", follow)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const slash = useMemo(() => new SlashMenuStore(), [])
|
|
100
|
+
const [linkRequest, setLinkRequest] = useState(0)
|
|
101
|
+
|
|
102
|
+
// A picture publishing takes: another artifact's, or one of this artifact's files.
|
|
103
|
+
const isOwnImage = (src: string) => {
|
|
104
|
+
const files = fragment.doc?.getMap<unknown>(LIVE_FILES)
|
|
105
|
+
if (isArtifactAddress(src)) return true
|
|
106
|
+
const path = artifactPath(src)
|
|
107
|
+
return path !== null && files?.has(path) === true
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const editor = useEditor(
|
|
111
|
+
{
|
|
112
|
+
editable,
|
|
113
|
+
extensions: [
|
|
114
|
+
...documentSchemaExtensions({
|
|
115
|
+
callout: CalloutNode.extend({
|
|
116
|
+
addNodeView: () => ReactNodeViewRenderer(CalloutView),
|
|
117
|
+
}),
|
|
118
|
+
}).map((extension) =>
|
|
119
|
+
extension.name === "image"
|
|
120
|
+
? Figure.extend({ addNodeView: () => ReactNodeViewRenderer(FigureView) })
|
|
121
|
+
: extension
|
|
122
|
+
),
|
|
123
|
+
Collaboration.configure({ fragment }),
|
|
124
|
+
CollaborationCaret.configure({
|
|
125
|
+
provider: { awareness },
|
|
126
|
+
// Who someone is, the server says: what this reader says of itself is replaced.
|
|
127
|
+
user: {},
|
|
128
|
+
render: caret,
|
|
129
|
+
selectionRender: (user: { readonly color?: string }) => ({
|
|
130
|
+
nodeName: "span",
|
|
131
|
+
class: "sixb-caret-selection",
|
|
132
|
+
style: `--caret: ${user.color ?? "#888"}`,
|
|
133
|
+
}),
|
|
134
|
+
}),
|
|
135
|
+
Placeholder.configure({
|
|
136
|
+
includeChildren: true,
|
|
137
|
+
showOnlyCurrent: true,
|
|
138
|
+
placeholder: ({ editor, node, pos }) => placeholder(editor, node, pos, messages),
|
|
139
|
+
}),
|
|
140
|
+
SlashCommand(slash, () => types),
|
|
141
|
+
BlockShortcuts.configure({ onLink: () => setLinkRequest((count) => count + 1) }),
|
|
142
|
+
SoftBreaks,
|
|
143
|
+
],
|
|
144
|
+
editorProps: {
|
|
145
|
+
attributes: {
|
|
146
|
+
class: "prose sixb-document sixb-editor",
|
|
147
|
+
spellcheck: "true",
|
|
148
|
+
"aria-multiline": "true",
|
|
149
|
+
role: "textbox",
|
|
150
|
+
},
|
|
151
|
+
// An image dropped or pasted is added where it lands.
|
|
152
|
+
handleDrop: (view, event, _slice, moved) => {
|
|
153
|
+
const files = imagesOf(event.dataTransfer)
|
|
154
|
+
if (moved || files.length === 0 || !uploadImage) return false
|
|
155
|
+
event.preventDefault()
|
|
156
|
+
const at = view.posAtCoords({ left: event.clientX, top: event.clientY })
|
|
157
|
+
void addImages(files, at?.pos)
|
|
158
|
+
return true
|
|
159
|
+
},
|
|
160
|
+
// What is pasted or dropped from elsewhere keeps only the artifact's own pictures: the
|
|
161
|
+
// document could not be saved with any other.
|
|
162
|
+
transformPasted: (slice) => {
|
|
163
|
+
const content = withOwnImages(slice.content, isOwnImage)
|
|
164
|
+
if (content === slice.content) return slice
|
|
165
|
+
const open = Slice.maxOpen(content)
|
|
166
|
+
return new Slice(
|
|
167
|
+
content,
|
|
168
|
+
Math.min(slice.openStart, open.openStart),
|
|
169
|
+
Math.min(slice.openEnd, open.openEnd)
|
|
170
|
+
)
|
|
171
|
+
},
|
|
172
|
+
// Markdown pasted as text becomes its blocks; copied, blocks are Markdown again.
|
|
173
|
+
handlePaste: (view, event) => {
|
|
174
|
+
const files = imagesOf(event.clipboardData)
|
|
175
|
+
if (files.length > 0 && uploadImage) {
|
|
176
|
+
void addImages(files)
|
|
177
|
+
return true
|
|
178
|
+
}
|
|
179
|
+
const text = event.clipboardData?.getData("text/plain") ?? ""
|
|
180
|
+
if (event.clipboardData?.getData("text/html") || !looksLikeMarkdown(text)) return false
|
|
181
|
+
// Code is pasted as it is.
|
|
182
|
+
if (view.state.selection.$from.parent.type.spec.code) return false
|
|
183
|
+
const content = markdownToBlocks(text).content ?? []
|
|
184
|
+
// Words alone go into the line they are pasted in; blocks, as blocks.
|
|
185
|
+
const [only] = content
|
|
186
|
+
const inline = content.length === 1 && only?.type === "paragraph"
|
|
187
|
+
// Markdown trims a paragraph's spaces; words pasted into a line keep theirs.
|
|
188
|
+
const space = (match: RegExpMatchArray | null) =>
|
|
189
|
+
match ? [{ type: "text", text: match[0] }] : []
|
|
190
|
+
const nodes = inline
|
|
191
|
+
? [
|
|
192
|
+
...space(text.match(/^[ \t]+/)),
|
|
193
|
+
...(only.content ?? []),
|
|
194
|
+
...space(text.match(/[ \t]+$/)),
|
|
195
|
+
]
|
|
196
|
+
: content
|
|
197
|
+
try {
|
|
198
|
+
const fragment = withOwnImages(Fragment.fromJSON(view.state.schema, nodes), isOwnImage)
|
|
199
|
+
view.dispatch(view.state.tr.replaceSelection(Slice.maxOpen(fragment)).scrollIntoView())
|
|
200
|
+
return true
|
|
201
|
+
} catch {
|
|
202
|
+
return false
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
clipboardTextSerializer: (slice) => {
|
|
206
|
+
const plain = () => slice.content.textBetween(0, slice.content.size, "\n\n", "\n")
|
|
207
|
+
const json = slice.content.toJSON() as { readonly type: string }[] | null
|
|
208
|
+
if (!json?.length) return ""
|
|
209
|
+
const inline = slice.content.firstChild?.isInline === true
|
|
210
|
+
const blocks = inline ? [{ type: "paragraph", content: json }] : json
|
|
211
|
+
try {
|
|
212
|
+
// A slice of list items or cells writes nothing as Markdown alone: its words, then.
|
|
213
|
+
return blocksToMarkdown({ type: "doc", content: blocks as never }).trimEnd() || plain()
|
|
214
|
+
} catch {
|
|
215
|
+
return plain()
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
[fragment, awareness, types]
|
|
221
|
+
)
|
|
222
|
+
editorRef.current = editor
|
|
223
|
+
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
if (editor && editor.isEditable !== editable) editor.setEditable(editable)
|
|
226
|
+
}, [editor, editable])
|
|
227
|
+
|
|
228
|
+
// A link to one of its sections opens there, as the document page does.
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
if (!editor || !part) return
|
|
231
|
+
const slug = createHeadingSlugger()
|
|
232
|
+
let found: number | null = null
|
|
233
|
+
editor.state.doc.forEach((node, pos) => {
|
|
234
|
+
if (found !== null || node.type.name !== "heading") return
|
|
235
|
+
if (slug(node.textContent.replace(/\s+/g, " ").trim()) === part) found = pos
|
|
236
|
+
})
|
|
237
|
+
if (found === null) return
|
|
238
|
+
const dom = editor.view.nodeDOM(found)
|
|
239
|
+
if (dom instanceof HTMLElement) dom.scrollIntoView({ block: "start" })
|
|
240
|
+
}, [editor, part])
|
|
241
|
+
|
|
242
|
+
return (
|
|
243
|
+
<FigureContext.Provider value={{ source: imageSource, caption: messages.caption }}>
|
|
244
|
+
<div
|
|
245
|
+
data-sixb-artifact=""
|
|
246
|
+
className={cn(
|
|
247
|
+
"@container min-h-0 flex-1 overflow-y-auto bg-background text-foreground",
|
|
248
|
+
theme?.className
|
|
249
|
+
)}
|
|
250
|
+
style={theme?.style}
|
|
251
|
+
>
|
|
252
|
+
<div className="sixb-document-page">
|
|
253
|
+
<article className="relative">
|
|
254
|
+
<EditorContent editor={editor} />
|
|
255
|
+
{editor && editable ? (
|
|
256
|
+
<>
|
|
257
|
+
<BlockHandle editor={editor} types={types} messages={messages} />
|
|
258
|
+
<SelectionToolbar
|
|
259
|
+
editor={editor}
|
|
260
|
+
types={types}
|
|
261
|
+
messages={messages}
|
|
262
|
+
linkRequest={linkRequest}
|
|
263
|
+
/>
|
|
264
|
+
</>
|
|
265
|
+
) : null}
|
|
266
|
+
</article>
|
|
267
|
+
</div>
|
|
268
|
+
</div>
|
|
269
|
+
<SlashMenu store={slash} messages={messages} />
|
|
270
|
+
{uploadImage ? (
|
|
271
|
+
<input
|
|
272
|
+
ref={picker}
|
|
273
|
+
type="file"
|
|
274
|
+
accept="image/*"
|
|
275
|
+
multiple
|
|
276
|
+
hidden
|
|
277
|
+
onChange={(event) => {
|
|
278
|
+
const files = [...(event.target.files ?? [])]
|
|
279
|
+
event.target.value = ""
|
|
280
|
+
void addImages(files)
|
|
281
|
+
}}
|
|
282
|
+
/>
|
|
283
|
+
) : null}
|
|
284
|
+
</FigureContext.Provider>
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The images among the files a drop or a paste carries. */
|
|
289
|
+
function imagesOf(data: DataTransfer | null | undefined): File[] {
|
|
290
|
+
return [...(data?.files ?? [])].filter((file) => file.type.startsWith("image/"))
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* `content` without the pictures `own` does not take, nor the Markdown kept as written that shows
|
|
295
|
+
* one; the same fragment when it holds none. A block of blocks they leave empty gets what it must
|
|
296
|
+
* hold.
|
|
297
|
+
*/
|
|
298
|
+
function withOwnImages(content: Fragment, own: (src: string) => boolean): Fragment {
|
|
299
|
+
let changed = false
|
|
300
|
+
const kept: ProseMirrorNode[] = []
|
|
301
|
+
content.forEach((node) => {
|
|
302
|
+
const shown =
|
|
303
|
+
node.type.name === "image"
|
|
304
|
+
? [String(node.attrs.src ?? "")]
|
|
305
|
+
: node.type.name === "markdown" || node.type.name === "markdownInline"
|
|
306
|
+
? imageSources(String(node.attrs.source ?? ""))
|
|
307
|
+
: []
|
|
308
|
+
if (!shown.every(own)) {
|
|
309
|
+
changed = true
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const inner = node.isLeaf ? node.content : withOwnImages(node.content, own)
|
|
314
|
+
if (inner === node.content) {
|
|
315
|
+
kept.push(node)
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
changed = true
|
|
319
|
+
// Words that were pictures alone go with them.
|
|
320
|
+
if (node.isTextblock && inner.size === 0) return
|
|
321
|
+
const rebuilt = node.type.createAndFill(node.attrs, inner, node.marks)
|
|
322
|
+
if (rebuilt) kept.push(rebuilt)
|
|
323
|
+
})
|
|
324
|
+
return changed ? Fragment.from(kept) : content
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** The pictures Markdown or HTML written as it was shows: `` and `<img src>`. */
|
|
328
|
+
function imageSources(source: string): string[] {
|
|
329
|
+
const pictures = source.matchAll(
|
|
330
|
+
/!\[[^\]]*\]\(\s*<?([^\s)>]*)|<img\b[^>]*?\bsrc\s*=\s*["']?([^"'\s>]*)/gi
|
|
331
|
+
)
|
|
332
|
+
return [...pictures].map((match) => match[1] ?? match[2] ?? "")
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The file of the artifact a picture's path names, as publishing reads it; null for a path that
|
|
337
|
+
* cannot name one: an address elsewhere, a `data:` image, or a path from the root.
|
|
338
|
+
*/
|
|
339
|
+
function artifactPath(src: string): string | null {
|
|
340
|
+
if (src === "" || src.startsWith("/") || /^[a-z][a-z\d+.-]*:/i.test(src)) return null
|
|
341
|
+
let path = src.split(/[?#]/)[0] ?? ""
|
|
342
|
+
try {
|
|
343
|
+
path = decodeURI(path)
|
|
344
|
+
} catch {
|
|
345
|
+
// Kept as written: it simply names no file.
|
|
346
|
+
}
|
|
347
|
+
const segments: string[] = []
|
|
348
|
+
for (const segment of path.split("/")) {
|
|
349
|
+
if (segment === "..") segments.pop()
|
|
350
|
+
else if (segment !== "" && segment !== ".") segments.push(segment)
|
|
351
|
+
}
|
|
352
|
+
return segments.join("/")
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Another editor's caret: a line in their color, their name above it. */
|
|
356
|
+
function caret(user: { readonly name?: string; readonly color?: string }): HTMLElement {
|
|
357
|
+
const element = document.createElement("span")
|
|
358
|
+
element.className = "sixb-caret"
|
|
359
|
+
element.style.setProperty("--caret", user.color ?? "#888")
|
|
360
|
+
const label = document.createElement("span")
|
|
361
|
+
label.className = "sixb-caret-label"
|
|
362
|
+
label.textContent = user.name || "·"
|
|
363
|
+
element.append(label)
|
|
364
|
+
return element
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** What an empty block says it is for, while the caret is in it. */
|
|
368
|
+
function placeholder(
|
|
369
|
+
editor: Editor,
|
|
370
|
+
node: ProseMirrorNode,
|
|
371
|
+
pos: number,
|
|
372
|
+
messages: EditorMessages
|
|
373
|
+
): string {
|
|
374
|
+
if (node.type.name === "heading") {
|
|
375
|
+
return pos === 0 ? messages.titlePlaceholder : messages.heading(Number(node.attrs.level))
|
|
376
|
+
}
|
|
377
|
+
if (node.type.name !== "paragraph") return ""
|
|
378
|
+
const parent = editor.state.doc.resolve(pos).parent.type.name
|
|
379
|
+
if (parent === "listItem") return messages.listItem
|
|
380
|
+
if (parent === "taskItem") return messages.taskItem
|
|
381
|
+
if (pos === 0 && editor.state.doc.childCount === 1) return messages.titlePlaceholder
|
|
382
|
+
return messages.placeholder
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Whether pasted text reads as Markdown: a heading, a list, a quote, a fence, a table, a link. */
|
|
386
|
+
function looksLikeMarkdown(text: string): boolean {
|
|
387
|
+
return (
|
|
388
|
+
/^(#{1,6} |[-*+] |\d+\. |> |```|\|.+\|$|- \[[ x]\] )/m.test(text) ||
|
|
389
|
+
/\*\*[^*\n]+\*\*|\[[^\]\n]+\]\([^)\n]+\)/.test(text)
|
|
390
|
+
)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* A line break within a paragraph's Markdown, as the Agent writes them: the document page reads
|
|
395
|
+
* it as a space, and so does the editor, while the text keeps it, so the Markdown written back
|
|
396
|
+
* is the same. Code keeps its lines.
|
|
397
|
+
*/
|
|
398
|
+
const SoftBreaks = Extension.create({
|
|
399
|
+
name: "softBreaks",
|
|
400
|
+
addProseMirrorPlugins() {
|
|
401
|
+
return [
|
|
402
|
+
new Plugin<DecorationSet>({
|
|
403
|
+
state: {
|
|
404
|
+
init: (_, state) => softBreaks(state.doc),
|
|
405
|
+
apply: (transaction, set) => (transaction.docChanged ? softBreaks(transaction.doc) : set),
|
|
406
|
+
},
|
|
407
|
+
props: {
|
|
408
|
+
decorations(state) {
|
|
409
|
+
return this.getState(state)
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
}),
|
|
413
|
+
]
|
|
414
|
+
},
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
function softBreaks(doc: ProseMirrorNode): DecorationSet {
|
|
418
|
+
const breaks: Decoration[] = []
|
|
419
|
+
doc.descendants((node, pos) => {
|
|
420
|
+
if (node.type.spec.code) return false
|
|
421
|
+
if (!node.isText) return true
|
|
422
|
+
|
|
423
|
+
for (const match of (node.text ?? "").matchAll(/\r?\n|\r/g)) {
|
|
424
|
+
const at = pos + match.index
|
|
425
|
+
breaks.push(Decoration.inline(at, at + match[0].length, { class: "sixb-soft-break" }))
|
|
426
|
+
}
|
|
427
|
+
return false
|
|
428
|
+
})
|
|
429
|
+
return DecorationSet.create(doc, breaks)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Keys for whole blocks: ⌘D duplicates the one the caret is in, ⌥⇧↑ and ⌥⇧↓ move it, ⌘K opens
|
|
434
|
+
* the link field over the selection.
|
|
435
|
+
*/
|
|
436
|
+
const BlockShortcuts = Extension.create<{ readonly onLink: () => void }>({
|
|
437
|
+
name: "blockShortcuts",
|
|
438
|
+
addOptions: () => ({ onLink: () => {} }),
|
|
439
|
+
addKeyboardShortcuts() {
|
|
440
|
+
const current = () => {
|
|
441
|
+
const { $from } = this.editor.state.selection
|
|
442
|
+
if ($from.depth < 1) return null
|
|
443
|
+
const pos = $from.before(1)
|
|
444
|
+
const node = this.editor.state.doc.nodeAt(pos)
|
|
445
|
+
return node ? { node, pos } : null
|
|
446
|
+
}
|
|
447
|
+
const move = (by: -1 | 1) => {
|
|
448
|
+
const at = current()
|
|
449
|
+
if (!at) return false
|
|
450
|
+
const index = this.editor.state.doc.resolve(at.pos).index()
|
|
451
|
+
const sibling = this.editor.state.doc.maybeChild(index + by)
|
|
452
|
+
if (!sibling) return true
|
|
453
|
+
const offset = this.editor.state.selection.from - at.pos
|
|
454
|
+
const target = by === 1 ? at.pos + sibling.nodeSize : at.pos - sibling.nodeSize
|
|
455
|
+
return this.editor
|
|
456
|
+
.chain()
|
|
457
|
+
.command(({ tr }) => {
|
|
458
|
+
tr.delete(at.pos, at.pos + at.node.nodeSize)
|
|
459
|
+
tr.insert(target, at.node)
|
|
460
|
+
return true
|
|
461
|
+
})
|
|
462
|
+
.setTextSelection(target + offset)
|
|
463
|
+
.scrollIntoView()
|
|
464
|
+
.run()
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
"Mod-d": () => {
|
|
468
|
+
const at = current()
|
|
469
|
+
if (!at) return false
|
|
470
|
+
return this.editor
|
|
471
|
+
.chain()
|
|
472
|
+
.insertContentAt(at.pos + at.node.nodeSize, at.node.toJSON())
|
|
473
|
+
.run()
|
|
474
|
+
},
|
|
475
|
+
"Alt-Shift-ArrowUp": () => move(-1),
|
|
476
|
+
"Alt-Shift-ArrowDown": () => move(1),
|
|
477
|
+
"Mod-k": () => {
|
|
478
|
+
if (this.editor.state.selection.empty) return false
|
|
479
|
+
this.options.onLink()
|
|
480
|
+
return true
|
|
481
|
+
},
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isArtifactAddress } from "@sixb/core/artifacts"
|
|
2
|
+
import { cn } from "@sixb/ui/lib/utils"
|
|
3
|
+
import { type NodeViewProps, NodeViewWrapper } from "@tiptap/react"
|
|
4
|
+
import { ImageOff } from "lucide-react"
|
|
5
|
+
import { createContext, useContext, useEffect, useState } from "react"
|
|
6
|
+
import { ArtifactEmbed } from "../references/references"
|
|
7
|
+
|
|
8
|
+
/** Where a document's image path is served from, and what an empty caption says. */
|
|
9
|
+
export const FigureContext = createContext<{
|
|
10
|
+
readonly source: (src: string) => string | null
|
|
11
|
+
readonly caption: string
|
|
12
|
+
}>({ source: () => null, caption: "" })
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A figure as it reads: the image, and its caption, which is typed into in place. The caption is
|
|
16
|
+
* the image's title, as Markdown keeps it.
|
|
17
|
+
*/
|
|
18
|
+
export function FigureView({ node, updateAttributes, selected, editor }: NodeViewProps) {
|
|
19
|
+
const { source: resolve, caption: captionPlaceholder } = useContext(FigureContext)
|
|
20
|
+
const src = String(node.attrs.src ?? "")
|
|
21
|
+
const embedded = isArtifactAddress(src)
|
|
22
|
+
const url = /^(https?:|data:image\/)/.test(src) ? src : resolve(src)
|
|
23
|
+
const [caption, setCaption] = useState(String(node.attrs.title ?? ""))
|
|
24
|
+
useEffect(() => setCaption(String(node.attrs.title ?? "")), [node.attrs.title])
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<NodeViewWrapper as="figure" className={cn("sixb-editor-figure", selected && "is-selected")}>
|
|
28
|
+
<div data-drag-handle="" className="sixb-editor-figure-frame">
|
|
29
|
+
{embedded ? (
|
|
30
|
+
// Another artifact, shown as the page shows it; its alt text is its caption.
|
|
31
|
+
<div contentEditable={false}>
|
|
32
|
+
<ArtifactEmbed address={src.trim()} caption={String(node.attrs.alt ?? "")} />
|
|
33
|
+
</div>
|
|
34
|
+
) : url ? (
|
|
35
|
+
<img src={url} alt={String(node.attrs.alt ?? "")} draggable={false} />
|
|
36
|
+
) : (
|
|
37
|
+
<div className="flex aspect-[16/7] flex-col items-center justify-center gap-2 rounded-[var(--radius)] bg-muted text-sm text-muted-foreground">
|
|
38
|
+
<ImageOff className="size-5" />
|
|
39
|
+
<span className="font-mono text-xs">{src}</span>
|
|
40
|
+
</div>
|
|
41
|
+
)}
|
|
42
|
+
</div>
|
|
43
|
+
{!embedded && (editor.isEditable || caption) ? (
|
|
44
|
+
<input
|
|
45
|
+
className="sixb-editor-caption"
|
|
46
|
+
value={caption}
|
|
47
|
+
placeholder={captionPlaceholder}
|
|
48
|
+
readOnly={!editor.isEditable}
|
|
49
|
+
aria-label={String(node.attrs.alt ?? "")}
|
|
50
|
+
onChange={(event) => setCaption(event.target.value)}
|
|
51
|
+
onBlur={() => {
|
|
52
|
+
const title = caption.trim() || null
|
|
53
|
+
if (title !== (node.attrs.title ?? null)) updateAttributes({ title })
|
|
54
|
+
}}
|
|
55
|
+
onKeyDown={(event) => {
|
|
56
|
+
if (event.key === "Enter") {
|
|
57
|
+
event.preventDefault()
|
|
58
|
+
event.currentTarget.blur()
|
|
59
|
+
editor.commands.focus()
|
|
60
|
+
}
|
|
61
|
+
}}
|
|
62
|
+
/>
|
|
63
|
+
) : null}
|
|
64
|
+
</NodeViewWrapper>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The kinds of block a query finds, best first: by label, then by keyword. */
|
|
2
|
+
export function findBlockTypes<
|
|
3
|
+
Type extends { readonly label: string; readonly keywords: readonly string[] },
|
|
4
|
+
>(types: readonly Type[], query: string): Type[] {
|
|
5
|
+
const wanted = query.trim().toLowerCase()
|
|
6
|
+
if (!wanted) return [...types]
|
|
7
|
+
const score = (type: Type) => {
|
|
8
|
+
const label = type.label.toLowerCase()
|
|
9
|
+
if (label.startsWith(wanted)) return 0
|
|
10
|
+
if (label.includes(wanted)) return 1
|
|
11
|
+
if (type.keywords.some((keyword) => keyword.startsWith(wanted))) return 2
|
|
12
|
+
return -1
|
|
13
|
+
}
|
|
14
|
+
return types
|
|
15
|
+
.map((type) => ({ type, rank: score(type) }))
|
|
16
|
+
.filter(({ rank }) => rank >= 0)
|
|
17
|
+
.sort((left, right) => left.rank - right.rank)
|
|
18
|
+
.map(({ type }) => type)
|
|
19
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { LIVE_FILES } from "@sixb/core/artifacts/live"
|
|
2
|
+
import { useCallback, useEffect, useRef } from "react"
|
|
3
|
+
import * as Y from "yjs"
|
|
4
|
+
import { useLiveArtifactState, useViewerLiveArtifact } from "../live/hooks"
|
|
5
|
+
import { useArtifactMessages } from "../messages"
|
|
6
|
+
import { type ArtifactVersion, versionImageSource } from "../queries"
|
|
7
|
+
import { DocumentRenderer } from "../renderers/document"
|
|
8
|
+
import { useArtifactThemeAttributes } from "../theme"
|
|
9
|
+
import { DocumentEditor } from "./document-editor"
|
|
10
|
+
import { useImageUpload } from "./upload"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A Markdown document's body: once the live document holds its blocks, the editor, which
|
|
14
|
+
* everyone who may change it types into as it is shown; until then, and for an older version, the
|
|
15
|
+
* document page.
|
|
16
|
+
*/
|
|
17
|
+
export function MarkdownDocument({
|
|
18
|
+
version,
|
|
19
|
+
latestVersion,
|
|
20
|
+
part,
|
|
21
|
+
}: {
|
|
22
|
+
readonly version: ArtifactVersion
|
|
23
|
+
readonly latestVersion: number
|
|
24
|
+
readonly part?: string
|
|
25
|
+
}) {
|
|
26
|
+
const messages = useArtifactMessages()
|
|
27
|
+
const theme = useArtifactThemeAttributes(version)
|
|
28
|
+
const live = useViewerLiveArtifact()
|
|
29
|
+
const state = useLiveArtifactState(live)
|
|
30
|
+
// An image added since the version was read shows from the reader's own copy until one holds it.
|
|
31
|
+
const previews = useRef(new Map<string, string>())
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const kept = previews.current
|
|
34
|
+
return () => {
|
|
35
|
+
for (const url of kept.values()) URL.revokeObjectURL(url)
|
|
36
|
+
}
|
|
37
|
+
}, [])
|
|
38
|
+
const imageSource = useCallback(
|
|
39
|
+
(src: string) =>
|
|
40
|
+
previews.current.get(src) ?? versionImageSource(version, src)?.inlineUrl ?? null,
|
|
41
|
+
[version]
|
|
42
|
+
)
|
|
43
|
+
const upload = useImageUpload(live, version)
|
|
44
|
+
const uploadImage = useCallback(
|
|
45
|
+
async (file: File) => {
|
|
46
|
+
const added = await upload(file)
|
|
47
|
+
previews.current.set(added.src, URL.createObjectURL(file))
|
|
48
|
+
return added
|
|
49
|
+
},
|
|
50
|
+
[upload]
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
const fragment =
|
|
54
|
+
live && state.synced && version.version === latestVersion
|
|
55
|
+
? live.doc.getMap(LIVE_FILES).get(version.entry)
|
|
56
|
+
: null
|
|
57
|
+
if (live && fragment instanceof Y.XmlFragment) {
|
|
58
|
+
return (
|
|
59
|
+
<DocumentEditor
|
|
60
|
+
fragment={fragment}
|
|
61
|
+
awareness={live.awareness}
|
|
62
|
+
editable={state.access === "edit" && !state.left}
|
|
63
|
+
imageSource={imageSource}
|
|
64
|
+
messages={messages.editor}
|
|
65
|
+
theme={theme}
|
|
66
|
+
{...(part ? { part } : {})}
|
|
67
|
+
{...(state.access === "edit" ? { uploadImage } : {})}
|
|
68
|
+
/>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
return <DocumentRenderer version={version} latestVersion={latestVersion} part={part} />
|
|
72
|
+
}
|