@vite-hub/ui 0.1.0 → 0.4.1
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 +247 -21
- package/dist/attachments-BzTwqRTU.js +90 -0
- package/dist/{avatar-stack-DXmCQhxd.d.ts → avatar-stack-DPRFuezh.d.ts} +1 -1
- package/dist/choices-input-ByXJvDn1.d.ts +94 -0
- package/dist/{code-view-J4OsgEdh.js → code-view-DsU-xzic.js} +20 -4
- package/dist/colors-BghReA9C-CjF9gnmy.js +28 -0
- package/dist/comment-attachments-C4yNbcx5.js +20 -0
- package/dist/comment-composer-fINL5bxo.d.ts +243 -0
- package/dist/comment-composer-jYv2ULNP.js +591 -0
- package/dist/comment-layer-C_sbPIR5.d.ts +176 -0
- package/dist/comment-list-BcSaLngS.d.ts +62 -0
- package/dist/comment-marks.css +234 -0
- package/dist/comment-panel-CfvdRUMX.d.ts +208 -0
- package/dist/comment-person-CLPlW_SS.js +40 -0
- package/dist/comment-pin-DHOdRvUw.d.ts +68 -0
- package/dist/comment-row-Bn3nr8l5.js +39 -0
- package/dist/comment-selection-bar-CyVBNYl9.d.ts +42 -0
- package/dist/comment-thread-DKJjmWN0.d.ts +385 -0
- package/dist/comment-visibility-DsVuVndP.d.ts +40 -0
- package/dist/{comments-C08J3z4e.js → comments-D0d26ghV.js} +210 -59
- package/dist/comments-D8hulVPy.d.ts +198 -0
- package/dist/comments-context-C2lOZyQ5.js +38 -0
- package/dist/config-DgxBW013.d.ts +80 -0
- package/dist/config-Dk67emQt.js +63 -0
- package/dist/copy-button-CRN3kmSD.js +253 -0
- package/dist/draw-DzSmD7uo-Dwz8zoH2.js +72976 -0
- package/dist/editor-C_wt4RRV.js +582 -0
- package/dist/error-diagnostics-SXtnk6bz.js +38 -0
- package/dist/{file-tree-C1pv4_AI.js → file-tree-C4OYgbV-.js} +292 -145
- package/dist/{file-tree-Ctb8hZ_g.d.ts → file-tree-DlT9RGhu.d.ts} +51 -15
- package/dist/format-orwcdZL_.js +39 -0
- package/dist/handlers-DMl-Na3x.js +182 -0
- package/dist/headless.d.ts +5 -176
- package/dist/headless.js +5 -3
- package/dist/icon-swap.css +69 -0
- package/dist/{image-preview-VfknBEFu.js → image-preview-C9tdkmEj.js} +8 -5
- package/dist/{index-DcH0QSI5.d.ts → index-BYRFQfCd.d.ts} +14 -0
- package/dist/{code-view-vDM-hph-.d.ts → index-CNY1uNTh.d.ts} +14 -228
- package/dist/index.d.ts +55 -57
- package/dist/index.js +678 -25
- package/dist/markdown-editor-CMbNZOdN.js +2394 -0
- package/dist/markdown-editor-wQF0yD5o.d.ts +195 -0
- package/dist/markdown-editor.d.ts +108 -0
- package/dist/markdown-editor.js +3 -0
- package/dist/message-scroller-jrYN9dG9.d.ts +175 -0
- package/dist/nuxt-ui-DSQFzq3s.js +38 -0
- package/dist/nuxt.d.ts +84 -2
- package/dist/nuxt.js +243 -35
- package/dist/parts/components/agent-session.d.ts +254 -0
- package/dist/parts/components/agent-session.js +640 -0
- package/dist/{attachment-BBndG9_P.d.ts → parts/components/attachment.d.ts} +14 -15
- package/dist/{attachment-B3dXC7jM.js → parts/components/attachment.js} +6 -4
- package/dist/{avatar-stack.d.ts → parts/components/avatar-stack.d.ts} +1 -1
- package/dist/{avatar-stack-DM6HRnc0.js → parts/components/avatar-stack.js} +2 -5
- package/dist/{bubble-BJH2rfii.d.ts → parts/components/bubble.d.ts} +7 -8
- package/dist/{bubble-BPyFQlW7.js → parts/components/bubble.js} +2 -2
- package/dist/{chat-message-mpDmZbIC.d.ts → parts/components/chat-message.d.ts} +2 -3
- package/dist/{chat-message-BNK9G7xF.js → parts/components/chat-message.js} +4 -4
- package/dist/{chat-session-D6C7VJBR.d.ts → parts/components/chat-session.d.ts} +3 -4
- package/dist/{chat-session-DYEHB1HC.js → parts/components/chat-session.js} +2 -2
- package/dist/{chat-B9HlZWA2.d.ts → parts/components/chat.d.ts} +8 -4
- package/dist/{chat-BoFHlu9V.js → parts/components/chat.js} +42 -9
- package/dist/{highlighted-code-Dzf6o54i.d.ts → parts/components/code-block.d.ts} +15 -23
- package/dist/{code-block-Br-02hn-.js → parts/components/code-block.js} +9 -38
- package/dist/parts/components/code-view.d.ts +270 -0
- package/dist/parts/components/code-view.js +2 -0
- package/dist/parts/components/comment-composer.d.ts +2 -0
- package/dist/parts/components/comment-composer.js +2 -0
- package/dist/parts/components/comment-layer.d.ts +2 -0
- package/dist/parts/components/comment-layer.js +631 -0
- package/dist/parts/components/comment-list.d.ts +2 -0
- package/dist/parts/components/comment-list.js +168 -0
- package/dist/parts/components/comment-panel.d.ts +2 -0
- package/dist/parts/components/comment-panel.js +311 -0
- package/dist/parts/components/comment-pin.d.ts +2 -0
- package/dist/parts/components/comment-pin.js +75 -0
- package/dist/parts/components/comment-selection-bar.d.ts +2 -0
- package/dist/parts/components/comment-selection-bar.js +116 -0
- package/dist/parts/components/comment-thread.d.ts +2 -0
- package/dist/parts/components/comment-thread.js +598 -0
- package/dist/parts/components/comments.d.ts +8 -0
- package/dist/parts/components/comments.js +9 -0
- package/dist/{connection-status-DjGBw6xX.d.ts → parts/components/connection-status.d.ts} +6 -7
- package/dist/{connection-status-CzuWBENf.js → parts/components/connection-status.js} +1 -1
- package/dist/parts/components/copy-button.d.ts +118 -0
- package/dist/parts/components/copy-button.js +2 -0
- package/dist/parts/components/device-frame.d.ts +130 -0
- package/dist/parts/components/device-frame.js +471 -0
- package/dist/parts/components/file-tree.d.ts +2 -0
- package/dist/parts/components/file-tree.js +2 -0
- package/dist/parts/components/filter-search.d.ts +47 -0
- package/dist/parts/components/filter-search.js +58 -0
- package/dist/parts/components/markdown-editor.d.ts +2 -0
- package/dist/parts/components/markdown-editor.js +2 -0
- package/dist/parts/components/markdown-renderer.d.ts +52 -0
- package/dist/parts/components/markdown-renderer.js +23077 -0
- package/dist/{markdown-Dse5ldJe.d.ts → parts/components/markdown.d.ts} +7 -7
- package/dist/parts/components/markdown.js +47 -0
- package/dist/{marker-ea-tvx-J.d.ts → parts/components/marker.d.ts} +6 -7
- package/dist/{marker-Di2ZG_k1.js → parts/components/marker.js} +2 -2
- package/dist/{message-parts-BBFUkluC.d.ts → parts/components/message-parts.d.ts} +2 -3
- package/dist/{message-parts-DQnfioAH.js → parts/components/message-parts.js} +6 -6
- package/dist/{message-CW_paC1k.d.ts → parts/components/message.d.ts} +8 -9
- package/dist/{message-DiyBthIq.js → parts/components/message.js} +2 -2
- package/dist/parts/components/patch-parsers.d.ts +2 -0
- package/dist/parts/components/patch-parsers.js +2 -0
- package/dist/{prompt-input-Cy_Ue6B5.d.ts → parts/components/prompt-input.d.ts} +53 -10
- package/dist/{prompt-input-BbnCb80O.js → parts/components/prompt-input.js} +66 -111
- package/dist/parts/components/relative-time.d.ts +73 -0
- package/dist/parts/components/relative-time.js +2 -0
- package/dist/parts/components/status.d.ts +60 -0
- package/dist/parts/components/status.js +48 -0
- package/dist/{sync-status-refELOZn.d.ts → parts/components/sync-status.d.ts} +5 -6
- package/dist/{sync-status-BpxYdZIJ.js → parts/components/sync-status.js} +1 -1
- package/dist/{tool-list-E7CA52rz.d.ts → parts/components/tool-list.d.ts} +3 -4
- package/dist/{tool-list-BdSr5CT4.js → parts/components/tool-list.js} +7 -38
- package/dist/{trace-run-COeXn46Z.d.ts → parts/components/trace-run.d.ts} +3 -4
- package/dist/{trace-run-BIQ17CED.js → parts/components/trace-run.js} +15 -23
- package/dist/{trace-V7rM3Pyd.d.ts → parts/components/trace.d.ts} +11 -12
- package/dist/{trace-llBCDiSW.js → parts/components/trace.js} +20 -13
- package/dist/parts/composables/attachments.d.ts +32 -0
- package/dist/parts/composables/attachments.js +2 -0
- package/dist/parts/composables/highlighted-code.d.ts +9 -0
- package/dist/{highlighted-code-Ck4zexJX.js → parts/composables/highlighted-code.js} +5 -2
- package/dist/parts/headless/choices-input.d.ts +2 -0
- package/dist/parts/headless/choices-input.js +162 -0
- package/dist/parts/headless/comment-visibility.d.ts +2 -0
- package/dist/parts/headless/comment-visibility.js +128 -0
- package/dist/parts/headless/comments.d.ts +2 -0
- package/dist/parts/headless/comments.js +2 -0
- package/dist/parts/headless/message-scroller.d.ts +2 -0
- package/dist/{message-scroller-CX6NJTdm.js → parts/headless/message-scroller.js} +2 -2
- package/dist/parts/playground/highlight.d.ts +5 -0
- package/dist/parts/playground/highlight.js +23 -0
- package/dist/parts/playground/paths.d.ts +4 -0
- package/dist/parts/playground/paths.js +7 -0
- package/dist/parts/playground/playground-render.d.ts +126 -0
- package/dist/parts/playground/playground-render.js +356 -0
- package/dist/parts/playground/share.d.ts +2 -0
- package/dist/parts/playground/share.js +92 -0
- package/dist/parts/playground/zip.d.ts +6 -0
- package/dist/parts/playground/zip.js +76 -0
- package/dist/parts/workspace/browser-support-notice.d.ts +76 -0
- package/dist/parts/workspace/browser-support-notice.js +105 -0
- package/dist/parts/workspace/codemirror.d.ts +16 -0
- package/dist/parts/workspace/codemirror.js +243 -0
- package/dist/parts/workspace/connect-panel.d.ts +129 -0
- package/dist/parts/workspace/connect-panel.js +285 -0
- package/dist/parts/workspace/connect-store.d.ts +26 -0
- package/dist/parts/workspace/connect-store.js +59 -0
- package/dist/parts/workspace/icons.d.ts +34 -0
- package/dist/parts/workspace/icons.js +46 -0
- package/dist/parts/workspace/playground-container.d.ts +192 -0
- package/dist/parts/workspace/playground-container.js +603 -0
- package/dist/parts/workspace/workspace-boot-status.d.ts +75 -0
- package/dist/parts/workspace/workspace-boot-status.js +207 -0
- package/dist/parts/workspace/workspace-editor.d.ts +43 -0
- package/dist/parts/workspace/workspace-editor.js +54 -0
- package/dist/parts/workspace/workspace-file-tree.d.ts +53 -0
- package/dist/parts/workspace/workspace-file-tree.js +68 -0
- package/dist/parts/workspace/workspace-preview.d.ts +72 -0
- package/dist/parts/workspace/workspace-preview.js +307 -0
- package/dist/parts/workspace/workspace-shell.d.ts +88 -0
- package/dist/parts/workspace/workspace-shell.js +291 -0
- package/dist/parts/workspace/workspace-terminal.d.ts +39 -0
- package/dist/parts/workspace/workspace-terminal.js +130 -0
- package/dist/payload-text-sjkYAwvw.js +32 -0
- package/dist/{pierre-code-view-T7V67hwT.js → pierre-code-view-NE35tkmR.js} +245 -102
- package/dist/{pierre-dark-DDWDl8g6.js → pierre-dark-CzUmjETS.js} +3 -1
- package/dist/{pierre-light-CWhW4NrH.js → pierre-light-Dn6E0sWv.js} +3 -1
- package/dist/playground.d.ts +4 -0
- package/dist/playground.js +4 -0
- package/dist/relative-time-BVmjOsYG.js +309 -0
- package/dist/rolldown-runtime-C9mWXvCC.js +29 -0
- package/dist/runtime/nuxt-plugin.js +23 -4
- package/dist/sankey-CAYwt82B-D4uDQoMR.js +2887 -0
- package/dist/share-DM4eNy4q.d.ts +37 -0
- package/dist/{slot-part-C7Ocxn3b.js → slot-part-Doic71uh.js} +7 -1
- package/dist/styles.css +2609 -620
- package/dist/temml-BilSAAws.js +12362 -0
- package/dist/theme-focus-ring.css +42 -0
- package/dist/theme.css +376 -0
- package/dist/theme.d.ts +355 -0
- package/dist/theme.js +265 -0
- package/dist/{types-CqDwuJAm.d.ts → types-Dj7vp0lZ.d.ts} +1 -1
- package/dist/vue-BDmSCtay.js +30779 -0
- package/dist/vue-DGstVdGn.js +2492 -0
- package/dist/workspace.d.ts +15 -0
- package/dist/workspace.js +12 -0
- package/package.json +135 -149
- package/dist/andromeeda-BASpqCeJ.js +0 -4
- package/dist/attachment.d.ts +0 -2
- package/dist/attachment.js +0 -2
- package/dist/aurora-x-DugsLcii.js +0 -4
- package/dist/avatar-stack.js +0 -2
- package/dist/ayu-dark-BT3qdi3o.js +0 -4
- package/dist/ayu-light-BVZyHoh9.js +0 -4
- package/dist/ayu-mirage-XFwUVZQZ.js +0 -4
- package/dist/bubble.d.ts +0 -2
- package/dist/bubble.js +0 -2
- package/dist/catppuccin-frappe-D20KODDN.js +0 -4
- package/dist/catppuccin-latte-eGLVHIUJ.js +0 -4
- package/dist/catppuccin-macchiato-bQGch4PY.js +0 -4
- package/dist/catppuccin-mocha-Bes8-0Mr.js +0 -4
- package/dist/chat-message.d.ts +0 -2
- package/dist/chat-message.js +0 -2
- package/dist/chat-session.d.ts +0 -2
- package/dist/chat-session.js +0 -2
- package/dist/chat.d.ts +0 -2
- package/dist/chat.js +0 -2
- package/dist/code-block.d.ts +0 -2
- package/dist/code-block.js +0 -3
- package/dist/code-view.d.ts +0 -2
- package/dist/code-view.js +0 -2
- package/dist/comment-layer-CBlxJ8eK.js +0 -1113
- package/dist/comments-ChNJkV1d.d.ts +0 -108
- package/dist/comments-Cq5eewsu.d.ts +0 -537
- package/dist/comments.d.ts +0 -2
- package/dist/comments.js +0 -2
- package/dist/config-Bux_AJzm.d.ts +0 -29
- package/dist/config-bHqE4JOs.js +0 -38
- package/dist/connection-status.d.ts +0 -2
- package/dist/connection-status.js +0 -2
- package/dist/dark-plus-BGuiqa1B.js +0 -4
- package/dist/dracula-0CVA2XY5.js +0 -4
- package/dist/dracula-soft-Dk9bpbVU.js +0 -4
- package/dist/error-diagnostics-B4hMpJRL.js +0 -13
- package/dist/everforest-dark-CRjTbhh5.js +0 -4
- package/dist/everforest-light-CEeYFLn8.js +0 -4
- package/dist/file-diff.d.ts +0 -2
- package/dist/file-diff.js +0 -2
- package/dist/file-tree.d.ts +0 -2
- package/dist/file-tree.js +0 -2
- package/dist/file-view.d.ts +0 -2
- package/dist/file-view.js +0 -2
- package/dist/github-dark-BXgKmOw_.js +0 -4
- package/dist/github-dark-default-DzJQXksd.js +0 -4
- package/dist/github-dark-dimmed-B1pEmGtS.js +0 -4
- package/dist/github-dark-high-contrast-BkHIa6jc.js +0 -4
- package/dist/github-light-BKRlUbaB.js +0 -4
- package/dist/github-light-default-BU3eYkzs.js +0 -4
- package/dist/github-light-high-contrast-B3MumDcI.js +0 -4
- package/dist/gruvbox-dark-hard-B0ecF443.js +0 -4
- package/dist/gruvbox-dark-medium-BQnpC39k.js +0 -4
- package/dist/gruvbox-dark-soft-DFZUgESr.js +0 -4
- package/dist/gruvbox-light-hard-CuB_LOJr.js +0 -4
- package/dist/gruvbox-light-medium-idDHgxjs.js +0 -4
- package/dist/gruvbox-light-soft-CfWH49MY.js +0 -4
- package/dist/horizon-bright-Ba6M8QSV.js +0 -4
- package/dist/horizon-h92C5OlZ.js +0 -4
- package/dist/houston-EQnPegWq.js +0 -4
- package/dist/kanagawa-dragon-DgDvwi3B.js +0 -4
- package/dist/kanagawa-lotus-BgoQro20.js +0 -4
- package/dist/kanagawa-wave-DgXEowHs.js +0 -4
- package/dist/laserwave-IxBH_3yW.js +0 -4
- package/dist/light-plus-S8JBx6is.js +0 -4
- package/dist/markdown-BoLBN0qH.js +0 -197
- package/dist/markdown.d.ts +0 -2
- package/dist/markdown.js +0 -2
- package/dist/marker.d.ts +0 -2
- package/dist/marker.js +0 -2
- package/dist/material-theme-D1f3HBq-.js +0 -4
- package/dist/material-theme-darker-ExzYhT0j.js +0 -4
- package/dist/material-theme-lighter-D6gH9nnA.js +0 -4
- package/dist/material-theme-ocean-D1w5BPAa.js +0 -4
- package/dist/material-theme-palenight-CkqWmVpF.js +0 -4
- package/dist/message-parts.d.ts +0 -2
- package/dist/message-parts.js +0 -2
- package/dist/message.d.ts +0 -2
- package/dist/message.js +0 -2
- package/dist/min-dark-npsJH98j.js +0 -4
- package/dist/min-light-DUk1XsVt.js +0 -4
- package/dist/monokai-CfFRWUNl.js +0 -4
- package/dist/multi-file-diff.d.ts +0 -2
- package/dist/multi-file-diff.js +0 -2
- package/dist/night-owl-BS7V-av_.js +0 -4
- package/dist/night-owl-light-CZtRawYE.js +0 -4
- package/dist/nord-BFh54Nqz.js +0 -4
- package/dist/one-dark-pro-zDovGosB.js +0 -4
- package/dist/one-light-gmHK3iK6.js +0 -4
- package/dist/patch-diff.d.ts +0 -2
- package/dist/patch-diff.js +0 -2
- package/dist/plastic-DtdaCjub.js +0 -4
- package/dist/poimandres-Hdda6aHK.js +0 -4
- package/dist/primitive-rail.d.ts +0 -222
- package/dist/primitive-rail.js +0 -348
- package/dist/prompt-input.d.ts +0 -2
- package/dist/prompt-input.js +0 -2
- package/dist/red-VqZFRByZ.js +0 -4
- package/dist/rose-pine-DiTkEueR.js +0 -4
- package/dist/rose-pine-dawn-tfo5y2X0.js +0 -4
- package/dist/rose-pine-moon-C0gON9XN.js +0 -4
- package/dist/runtime-type-DlCscMfw.js +0 -59
- package/dist/slack-dark-v8P_eXr6.js +0 -4
- package/dist/slack-ochin-D8SyEdKz.js +0 -4
- package/dist/snazzy-light-BtkQkvXb.js +0 -4
- package/dist/solarized-dark-B8nAdMfM.js +0 -4
- package/dist/solarized-light-B-3F1INj.js +0 -4
- package/dist/sync-status.d.ts +0 -2
- package/dist/sync-status.js +0 -2
- package/dist/synthwave-84-DcsXCF_c.js +0 -4
- package/dist/tokyo-night-Bvpqduda.js +0 -4
- package/dist/tool-list.d.ts +0 -2
- package/dist/tool-list.js +0 -2
- package/dist/trace-run.d.ts +0 -2
- package/dist/trace-run.js +0 -2
- package/dist/trace.d.ts +0 -2
- package/dist/trace.js +0 -2
- package/dist/unresolved-file.d.ts +0 -2
- package/dist/unresolved-file.js +0 -2
- package/dist/vesper-CfxmMOy3.js +0 -4
- package/dist/vitesse-black-D9-_1mjV.js +0 -4
- package/dist/vitesse-dark-DlSMLlr9.js +0 -4
- package/dist/vitesse-light-KRWWWztf.js +0 -4
|
@@ -0,0 +1,2394 @@
|
|
|
1
|
+
import { r as useUIComponent } from "./nuxt-ui-DSQFzq3s.js";
|
|
2
|
+
import { t as uiErrorDiagnostics } from "./error-diagnostics-SXtnk6bz.js";
|
|
3
|
+
import { a as addStyles, c as src_default, d as src_default$1, f as src_default$3, i as rowsOf, l as src_default$2, m as isElement$2, o as typesetCss, p as childrenOf$1, s as theme, t as Properties } from "./vue-DGstVdGn.js";
|
|
4
|
+
import { n as editorHandlersKey, t as createEditorHandlers } from "./handlers-DMl-Na3x.js";
|
|
5
|
+
import { Comment, Fragment, computed, defineComponent, h, isVNode, onBeforeUnmount, onMounted, onScopeDispose, provide, ref, shallowRef, watch, watchEffect } from "vue";
|
|
6
|
+
import { defineDiagnostics } from "nostics";
|
|
7
|
+
import { Editor, EditorContent, NodeViewContent, NodeViewWrapper, VueNodeViewRenderer, nodeViewProps } from "@tiptap/vue-3";
|
|
8
|
+
import { defineComarkPlugin, parseMarkdown } from "comark";
|
|
9
|
+
import { indent } from "comark/utils";
|
|
10
|
+
import { renderFrontmatter, renderMarkdown } from "comark/render";
|
|
11
|
+
import { Extension, Mark, Node } from "@tiptap/core";
|
|
12
|
+
import Image from "@tiptap/extension-image";
|
|
13
|
+
import { TaskItem, TaskList } from "@tiptap/extension-list";
|
|
14
|
+
import { TableKit } from "@tiptap/extension-table";
|
|
15
|
+
import StarterKit from "@tiptap/starter-kit";
|
|
16
|
+
import attributes from "comark/plugins/attributes";
|
|
17
|
+
import components from "comark/plugins/components";
|
|
18
|
+
import frontmatterPlugin from "comark/plugins/frontmatter";
|
|
19
|
+
import taskList from "comark/plugins/task-list";
|
|
20
|
+
import { parseFrontmatter } from "comark/parse";
|
|
21
|
+
import { Plugin as Plugin$1, PluginKey } from "@tiptap/pm/state";
|
|
22
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view";
|
|
23
|
+
//#region ../blob/dist/object-D8pRdprI.js
|
|
24
|
+
/** A non-null object that is not an array. Functions do not pass. */
|
|
25
|
+
function isRecord(value) {
|
|
26
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region ../blob/dist/browser-diagnostics-LWw4MIqX.js
|
|
30
|
+
const blobBrowserDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
31
|
+
docsBase: "https://vitehub.dev/docs/blob/diagnostics",
|
|
32
|
+
codes: {
|
|
33
|
+
BLOB_IMAGE_VARIANT_INVALID: {
|
|
34
|
+
why: ({ expected, parameter }) => `Image variant \`${parameter}\` must be ${expected}.`,
|
|
35
|
+
fix: "Change the query of the image URL: `w` and `h` from 1 to 10000, `q` from 1 to 100, `fit` as `cover` or `inside`, `fmt` as `webp`, `jpeg`, `png`, or `avif`, and `crop` as a crop name. `imageVariantUrl()` writes a valid query."
|
|
36
|
+
},
|
|
37
|
+
BLOB_REFERENCE_OPTION_INVALID: {
|
|
38
|
+
why: ({ kind, option, value }) => option === "accept" ? `[vitehub] ${kind}() accept entry "${value}" is not a MIME type or pattern such as "image/png" or "image/*".` : option === "maxSize" ? `[vitehub] ${kind}() maxSize "${value}" is not a size such as "512KB" or "8MB".` : `[vitehub] ${kind}() store must be a non-empty Blob Store name.`,
|
|
39
|
+
fix: "Pass `accept` as MIME types or patterns such as `\"image/png\"` or `\"image/*\"`, `maxSize` as a size such as `\"8MB\"`, and `store` as a non-empty Blob Store name to `image()` or `file()`."
|
|
40
|
+
},
|
|
41
|
+
BLOB_REFERENCE_SCHEMA_TARGET_UNSUPPORTED: {
|
|
42
|
+
why: ({ target }) => `[vitehub] Blob reference schemas emit draft-07, draft-2020-12, or openapi-3.0 JSON Schema, not "${target}".`,
|
|
43
|
+
fix: "Pass `target: \"draft-07\"`, `\"draft-2020-12\"`, or `\"openapi-3.0\"` to `[\"~standard\"].jsonSchema.input()` or `.output()` of the `image()` or `file()` schema."
|
|
44
|
+
},
|
|
45
|
+
BLOB_RESPONSIVE_IMAGE_URL_MISSING: {
|
|
46
|
+
why: "[vitehub] responsiveImage() needs the served URL of the image. Read the record from the served Blob Store or pass `url`.",
|
|
47
|
+
fix: "Pass `url` to `responsiveImage()`, or pass a media record from the Blob Store that `blob.serve` serves, because that record has a `url`."
|
|
48
|
+
},
|
|
49
|
+
BLOB_UPLOAD_OPTION_INVALID: {
|
|
50
|
+
why: ({ option }) => option === "partSize" ? "partSize must be a finite positive number." : "concurrency must be a positive integer.",
|
|
51
|
+
fix: "Pass `partSize` as a positive number of bytes and `concurrency` as a positive integer to `createMultipartUploader()`, or omit them."
|
|
52
|
+
},
|
|
53
|
+
BLOB_UPLOAD_REQUEST_FAILED: {
|
|
54
|
+
why: ({ body, status }) => `Upload request failed with status ${status}${body ? `: ${body}` : ""}`,
|
|
55
|
+
fix: "Read the status and the server text in the message. Check the upload route and its limits, such as `accept` and `maxSize` in `blob.handleUpload()`, then retry."
|
|
56
|
+
},
|
|
57
|
+
BLOB_UPLOAD_RESPONSE_INVALID: {
|
|
58
|
+
why: ({ partMismatch }) => partMismatch ? "Upload request returned a part number that does not match the requested part." : "Upload request returned malformed JSON.",
|
|
59
|
+
fix: "Return the result of `blob.handleUpload()` or `blob.handleMultipartUpload()` from the upload route without changes, and point `uploadFiles()` or `createMultipartUploader()` at that route."
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region ../blob/dist/client.js
|
|
65
|
+
function isFile(value) {
|
|
66
|
+
return "name" in value && typeof value.name === "string" && "arrayBuffer" in value && typeof value.arrayBuffer === "function";
|
|
67
|
+
}
|
|
68
|
+
function toFiles(input) {
|
|
69
|
+
if (isFile(input)) return [input];
|
|
70
|
+
if ("files" in input) return Array.from(input.files ?? []);
|
|
71
|
+
return Array.from(input);
|
|
72
|
+
}
|
|
73
|
+
function mergeHeaders(...sources) {
|
|
74
|
+
const headers = new Headers();
|
|
75
|
+
for (const source of sources) new Headers(source).forEach((value, key) => headers.set(key, value));
|
|
76
|
+
return headers;
|
|
77
|
+
}
|
|
78
|
+
function isString$1(value) {
|
|
79
|
+
return typeof value === "string";
|
|
80
|
+
}
|
|
81
|
+
function isFiniteNonNegativeNumber(value) {
|
|
82
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
83
|
+
}
|
|
84
|
+
function isStringRecord(value) {
|
|
85
|
+
return isRecord(value) && Object.values(value).every(isString$1);
|
|
86
|
+
}
|
|
87
|
+
function isDenseArray(value) {
|
|
88
|
+
if (!Array.isArray(value)) return false;
|
|
89
|
+
for (let index = 0; index < value.length; index++) if (!Object.hasOwn(value, index)) return false;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
function isSerializedBlobObject(value) {
|
|
93
|
+
if (!isRecord(value) || !isString$1(value.pathname) || value.contentType !== void 0 && !isString$1(value.contentType) || value.httpEtag !== void 0 && !isString$1(value.httpEtag) || value.size !== void 0 && !isFiniteNonNegativeNumber(value.size) || !isString$1(value.uploadedAt) || !isStringRecord(value.httpMetadata) || !isStringRecord(value.customMetadata) || value.url !== void 0 && !isString$1(value.url)) return false;
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
function isSerializedBlobObjectArray(value) {
|
|
97
|
+
return isDenseArray(value) && value.every(isSerializedBlobObject);
|
|
98
|
+
}
|
|
99
|
+
function parseSerializedBlobObjectArray(value) {
|
|
100
|
+
if (!isSerializedBlobObjectArray(value)) throw blobBrowserDiagnostics.BLOB_UPLOAD_RESPONSE_INVALID({});
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
function malformedJson() {
|
|
104
|
+
throw blobBrowserDiagnostics.BLOB_UPLOAD_RESPONSE_INVALID({});
|
|
105
|
+
}
|
|
106
|
+
function parseDirectUploadStep(value) {
|
|
107
|
+
if (!isRecord(value)) return malformedJson();
|
|
108
|
+
if (value.action === "form") return { action: "form" };
|
|
109
|
+
const request = value.request;
|
|
110
|
+
if (value.action !== "sign" || !isString$1(value.ticket) || !isRecord(request) || !isString$1(request.url) || !isString$1(request.method) || !isStringRecord(request.headers)) return malformedJson();
|
|
111
|
+
return {
|
|
112
|
+
action: "sign",
|
|
113
|
+
request: {
|
|
114
|
+
headers: request.headers,
|
|
115
|
+
method: request.method,
|
|
116
|
+
url: request.url
|
|
117
|
+
},
|
|
118
|
+
ticket: value.ticket
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
async function assertOk(response) {
|
|
122
|
+
if (response.ok) return;
|
|
123
|
+
const message = await response.text().catch(() => "");
|
|
124
|
+
throw blobBrowserDiagnostics.BLOB_UPLOAD_REQUEST_FAILED({
|
|
125
|
+
body: message,
|
|
126
|
+
status: response.status
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async function readJson(response, parse) {
|
|
130
|
+
await assertOk(response);
|
|
131
|
+
let body;
|
|
132
|
+
try {
|
|
133
|
+
body = await response.json();
|
|
134
|
+
} catch {
|
|
135
|
+
throw blobBrowserDiagnostics.BLOB_UPLOAD_RESPONSE_INVALID({});
|
|
136
|
+
}
|
|
137
|
+
return parse(body);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Send files as `multipart/form-data` to a route that returns the result of `blob.handleUpload()`.
|
|
141
|
+
* Resolves with the stored objects in the order of the input files.
|
|
142
|
+
*/
|
|
143
|
+
async function uploadFiles(apiBase, input, options = {}) {
|
|
144
|
+
const files = toFiles(input);
|
|
145
|
+
if (!files.length) return [];
|
|
146
|
+
if (options.direct) return await uploadDirect(apiBase, files, options);
|
|
147
|
+
const form = new FormData();
|
|
148
|
+
for (const file of files) form.append(options.formKey ?? "files", file, file.name);
|
|
149
|
+
const init = {
|
|
150
|
+
body: form,
|
|
151
|
+
headers: mergeHeaders(options.headers),
|
|
152
|
+
method: options.method ?? "POST"
|
|
153
|
+
};
|
|
154
|
+
return await readJson(options.onProgress ? await sendWithProgress(apiBase, init, options.onProgress) : await (options.fetch ?? globalThis.fetch)(apiBase, init), parseSerializedBlobObjectArray);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Upload each file in three requests: `sign` asks the route for a signed `PUT`, the `PUT` sends the file to storage,
|
|
158
|
+
* and `complete` asks the route to check the stored file. The files go one after the other.
|
|
159
|
+
*/
|
|
160
|
+
async function uploadDirect(apiBase, files, options) {
|
|
161
|
+
const request = options.fetch ?? globalThis.fetch;
|
|
162
|
+
const step = (body) => request(apiBase, {
|
|
163
|
+
body: JSON.stringify(body),
|
|
164
|
+
headers: mergeHeaders(options.headers, { "content-type": "application/json" }),
|
|
165
|
+
method: options.method ?? "POST"
|
|
166
|
+
});
|
|
167
|
+
const total = files.reduce((sum, file) => sum + file.size, 0);
|
|
168
|
+
let sent = 0;
|
|
169
|
+
const objects = [];
|
|
170
|
+
for (const file of files) {
|
|
171
|
+
const signed = await readJson(await step({
|
|
172
|
+
action: "sign",
|
|
173
|
+
contentType: file.type,
|
|
174
|
+
name: file.name,
|
|
175
|
+
size: file.size
|
|
176
|
+
}), parseDirectUploadStep);
|
|
177
|
+
if (signed.action === "form") return [...objects, ...await uploadFiles(apiBase, files.slice(objects.length), {
|
|
178
|
+
...options,
|
|
179
|
+
direct: false
|
|
180
|
+
})];
|
|
181
|
+
const url = resolveUrl(signed.request.url, apiBase);
|
|
182
|
+
const init = {
|
|
183
|
+
body: file,
|
|
184
|
+
headers: new Headers(signed.request.headers),
|
|
185
|
+
method: signed.request.method
|
|
186
|
+
};
|
|
187
|
+
const onProgress = options.onProgress;
|
|
188
|
+
await assertOk(onProgress ? await sendWithProgress(url, init, (percent) => onProgress(total ? Math.round((sent + file.size * percent / 100) / total * 100) : percent)) : await request(url, init));
|
|
189
|
+
sent += file.size;
|
|
190
|
+
const [object] = await readJson(await step({
|
|
191
|
+
action: "complete",
|
|
192
|
+
ticket: signed.ticket
|
|
193
|
+
}), parseSerializedBlobObjectArray);
|
|
194
|
+
if (!object) return malformedJson();
|
|
195
|
+
objects.push(object);
|
|
196
|
+
}
|
|
197
|
+
return objects;
|
|
198
|
+
}
|
|
199
|
+
function resolveUrl(url, apiBase) {
|
|
200
|
+
try {
|
|
201
|
+
return new URL(url, apiBase).href;
|
|
202
|
+
} catch {
|
|
203
|
+
return url;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function sendWithProgress(url, init, onProgress) {
|
|
207
|
+
return new Promise((resolve, reject) => {
|
|
208
|
+
const request = new (Reflect.get(globalThis, "XMLHttpRequest"))();
|
|
209
|
+
request.open(init.method, url);
|
|
210
|
+
init.headers.forEach((value, key) => request.setRequestHeader(key, value));
|
|
211
|
+
request.upload.addEventListener("progress", (event) => {
|
|
212
|
+
if (event.lengthComputable && event.total) onProgress(Math.round(event.loaded / event.total * 100));
|
|
213
|
+
});
|
|
214
|
+
request.addEventListener("load", () => {
|
|
215
|
+
const contentType = request.getResponseHeader("content-type");
|
|
216
|
+
const body = [
|
|
217
|
+
101,
|
|
218
|
+
204,
|
|
219
|
+
205,
|
|
220
|
+
304
|
|
221
|
+
].includes(request.status) ? null : request.responseText;
|
|
222
|
+
resolve(new Response(body, {
|
|
223
|
+
status: request.status,
|
|
224
|
+
headers: contentType ? { "content-type": contentType } : {}
|
|
225
|
+
}));
|
|
226
|
+
});
|
|
227
|
+
request.addEventListener("error", () => reject(/* @__PURE__ */ new Error("Upload request failed: the browser could not reach the server.")));
|
|
228
|
+
request.addEventListener("abort", () => reject(/* @__PURE__ */ new Error("Upload request was aborted.")));
|
|
229
|
+
request.send(init.body);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region ../../node_modules/.pnpm/comark-ui@0.12.0_@comark+html@0.9.0_katex@0.19.1_shiki@4.5.0__@tiptap+core@3.31.4_@tipt_529d07a13f99d0823ee3fccd15d28268/node_modules/comark-ui/dist/text-BaJTbiwp.mjs
|
|
234
|
+
/**
|
|
235
|
+
* A prop value as text. Comark writes strings, numbers, and booleans, so an object, array, function, or missing value
|
|
236
|
+
* is no text. The `typeof` check, unlike `instanceof`, can't throw on a revoked proxy.
|
|
237
|
+
*/
|
|
238
|
+
function scalar(value) {
|
|
239
|
+
switch (typeof value) {
|
|
240
|
+
case "string": return value;
|
|
241
|
+
case "number":
|
|
242
|
+
case "boolean":
|
|
243
|
+
case "bigint":
|
|
244
|
+
case "symbol": return String(value);
|
|
245
|
+
default: return;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/** FNV-1a, in base 36: short, and the same every time the same text is hashed, so ids derived from it are stable. */
|
|
249
|
+
function hash(source) {
|
|
250
|
+
let value = 2166136261;
|
|
251
|
+
for (let at = 0; at < source.length; at++) value = Math.imul(value ^ source.charCodeAt(at), 16777619);
|
|
252
|
+
return (value >>> 0).toString(36);
|
|
253
|
+
}
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region ../../node_modules/.pnpm/comark-ui@0.12.0_@comark+html@0.9.0_katex@0.19.1_shiki@4.5.0__@tiptap+core@3.31.4_@tipt_529d07a13f99d0823ee3fccd15d28268/node_modules/comark-ui/dist/editor-BWYpRh4o.mjs
|
|
256
|
+
const typesWithProps = [
|
|
257
|
+
"paragraph",
|
|
258
|
+
"heading",
|
|
259
|
+
"blockquote",
|
|
260
|
+
"bulletList",
|
|
261
|
+
"orderedList",
|
|
262
|
+
"listItem",
|
|
263
|
+
"taskList",
|
|
264
|
+
"taskItem",
|
|
265
|
+
"codeBlock",
|
|
266
|
+
"horizontalRule",
|
|
267
|
+
"hardBreak",
|
|
268
|
+
"image",
|
|
269
|
+
"table",
|
|
270
|
+
"tableRow",
|
|
271
|
+
"tableHeader",
|
|
272
|
+
"tableCell",
|
|
273
|
+
"bold",
|
|
274
|
+
"italic",
|
|
275
|
+
"strike",
|
|
276
|
+
"code",
|
|
277
|
+
"link",
|
|
278
|
+
"definitionList",
|
|
279
|
+
"definitionTerm",
|
|
280
|
+
"definitionDescription"
|
|
281
|
+
];
|
|
282
|
+
function readProps(element) {
|
|
283
|
+
try {
|
|
284
|
+
return JSON.parse(element.getAttribute("data-comark-props") ?? "null");
|
|
285
|
+
} catch {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Comark attributes the editor doesn't show, carried as they were parsed so writing the node back keeps them. They're
|
|
291
|
+
* in the HTML too, since the clipboard copies nodes as HTML: a pasted `::screenshot` keeps its `src`.
|
|
292
|
+
*/
|
|
293
|
+
const props = {
|
|
294
|
+
default: null,
|
|
295
|
+
parseHTML: readProps,
|
|
296
|
+
renderHTML: (attrs) => attrs.props ? { "data-comark-props": JSON.stringify(attrs.props) } : {}
|
|
297
|
+
};
|
|
298
|
+
const name = (attribute) => ({
|
|
299
|
+
default: "",
|
|
300
|
+
parseHTML: (element) => element.getAttribute(attribute) ?? "",
|
|
301
|
+
renderHTML: (attrs) => ({ [attribute]: attrs.name })
|
|
302
|
+
});
|
|
303
|
+
function mediaKind(src) {
|
|
304
|
+
const path = (src ?? "").replace(/[?#].*$/, "");
|
|
305
|
+
return /\.(?:mp4|webm|mov|m4v)$/i.test(path) ? "video" : /\.(?:mp3|wav|ogg|m4a)$/i.test(path) ? "audio" : void 0;
|
|
306
|
+
}
|
|
307
|
+
/** Markdown's image, inline as Comark parses it, drawn as a player when comark-media would play it. */
|
|
308
|
+
const ComarkImage = Image.extend({ renderHTML({ node, HTMLAttributes }) {
|
|
309
|
+
const kind = mediaKind(node.attrs.src);
|
|
310
|
+
if (!kind) return this.parent?.({
|
|
311
|
+
node,
|
|
312
|
+
HTMLAttributes
|
|
313
|
+
}) ?? ["img", HTMLAttributes];
|
|
314
|
+
return [kind, {
|
|
315
|
+
src: node.attrs.src,
|
|
316
|
+
title: node.attrs.title,
|
|
317
|
+
controls: "",
|
|
318
|
+
preload: "metadata"
|
|
319
|
+
}];
|
|
320
|
+
} }).configure({ inline: true });
|
|
321
|
+
/** A block component, `::name`, or a block of raw HTML. Its children are blocks and its `#slot`s. */
|
|
322
|
+
const ComarkBlock = Node.create({
|
|
323
|
+
name: "comarkBlock",
|
|
324
|
+
group: "block",
|
|
325
|
+
content: "(block | comarkSlot)*",
|
|
326
|
+
defining: true,
|
|
327
|
+
addAttributes: () => ({
|
|
328
|
+
name: name("data-comark-block"),
|
|
329
|
+
props
|
|
330
|
+
}),
|
|
331
|
+
parseHTML: () => [{ tag: "div[data-comark-block]" }],
|
|
332
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
333
|
+
"div",
|
|
334
|
+
HTMLAttributes,
|
|
335
|
+
0
|
|
336
|
+
]
|
|
337
|
+
});
|
|
338
|
+
/** A component's named slot, `#name`. */
|
|
339
|
+
const ComarkSlot = Node.create({
|
|
340
|
+
name: "comarkSlot",
|
|
341
|
+
content: "block*",
|
|
342
|
+
defining: true,
|
|
343
|
+
addAttributes: () => ({
|
|
344
|
+
name: name("data-comark-slot"),
|
|
345
|
+
props
|
|
346
|
+
}),
|
|
347
|
+
parseHTML: () => [{ tag: "div[data-comark-slot]" }],
|
|
348
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
349
|
+
"div",
|
|
350
|
+
HTMLAttributes,
|
|
351
|
+
0
|
|
352
|
+
]
|
|
353
|
+
});
|
|
354
|
+
/** An inline component, `:name[text]{props}`, or inline raw HTML. */
|
|
355
|
+
const ComarkInline = Node.create({
|
|
356
|
+
name: "comarkInline",
|
|
357
|
+
group: "inline",
|
|
358
|
+
inline: true,
|
|
359
|
+
content: "inline*",
|
|
360
|
+
addAttributes: () => ({
|
|
361
|
+
name: name("data-comark-inline"),
|
|
362
|
+
props
|
|
363
|
+
}),
|
|
364
|
+
parseHTML: () => [{ tag: "span[data-comark-inline]" }],
|
|
365
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
366
|
+
"span",
|
|
367
|
+
HTMLAttributes,
|
|
368
|
+
0
|
|
369
|
+
]
|
|
370
|
+
});
|
|
371
|
+
/** An HTML comment between blocks. */
|
|
372
|
+
const ComarkComment = Node.create({
|
|
373
|
+
name: "comarkComment",
|
|
374
|
+
group: "block",
|
|
375
|
+
atom: true,
|
|
376
|
+
addAttributes: () => ({ text: {
|
|
377
|
+
default: "",
|
|
378
|
+
parseHTML: (element) => element.getAttribute("data-comark-comment") ?? "",
|
|
379
|
+
renderHTML: (attrs) => ({ "data-comark-comment": attrs.text })
|
|
380
|
+
} }),
|
|
381
|
+
parseHTML: () => [{ tag: "div[data-comark-comment]" }],
|
|
382
|
+
renderHTML: ({ HTMLAttributes }) => ["div", HTMLAttributes]
|
|
383
|
+
});
|
|
384
|
+
const label = {
|
|
385
|
+
default: "",
|
|
386
|
+
parseHTML: (element) => element.getAttribute("data-label") ?? "",
|
|
387
|
+
renderHTML: (attrs) => ({ "data-label": attrs.label })
|
|
388
|
+
};
|
|
389
|
+
/** A footnote reference, `[^label]`. */
|
|
390
|
+
const FootnoteReference = Node.create({
|
|
391
|
+
name: "footnoteReference",
|
|
392
|
+
group: "inline",
|
|
393
|
+
inline: true,
|
|
394
|
+
atom: true,
|
|
395
|
+
addAttributes: () => ({ label }),
|
|
396
|
+
parseHTML: () => [{ tag: "sup[data-footnote-reference]" }],
|
|
397
|
+
renderHTML: ({ node, HTMLAttributes }) => [
|
|
398
|
+
"sup",
|
|
399
|
+
{
|
|
400
|
+
...HTMLAttributes,
|
|
401
|
+
"data-footnote-reference": ""
|
|
402
|
+
},
|
|
403
|
+
node.attrs.label
|
|
404
|
+
]
|
|
405
|
+
});
|
|
406
|
+
/** A note written where it applies, `^[text]`. */
|
|
407
|
+
const InlineFootnote = Node.create({
|
|
408
|
+
name: "inlineFootnote",
|
|
409
|
+
group: "inline",
|
|
410
|
+
inline: true,
|
|
411
|
+
content: "inline*",
|
|
412
|
+
parseHTML: () => [{ tag: "span[data-inline-footnote]" }],
|
|
413
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
414
|
+
"span",
|
|
415
|
+
{
|
|
416
|
+
...HTMLAttributes,
|
|
417
|
+
"data-inline-footnote": ""
|
|
418
|
+
},
|
|
419
|
+
0
|
|
420
|
+
]
|
|
421
|
+
});
|
|
422
|
+
/** A footnote's text, `[^label]: text`, with the blocks indented under it. */
|
|
423
|
+
const FootnoteDefinition = Node.create({
|
|
424
|
+
name: "footnoteDefinition",
|
|
425
|
+
group: "block",
|
|
426
|
+
content: "block+",
|
|
427
|
+
defining: true,
|
|
428
|
+
addAttributes: () => ({ label }),
|
|
429
|
+
parseHTML: () => [{ tag: "div[data-footnote-definition]" }],
|
|
430
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
431
|
+
"div",
|
|
432
|
+
{
|
|
433
|
+
...HTMLAttributes,
|
|
434
|
+
"data-footnote-definition": ""
|
|
435
|
+
},
|
|
436
|
+
0
|
|
437
|
+
]
|
|
438
|
+
});
|
|
439
|
+
/** A definition list, `Term` then `: Definition`. */
|
|
440
|
+
const DefinitionList = Node.create({
|
|
441
|
+
name: "definitionList",
|
|
442
|
+
group: "block",
|
|
443
|
+
content: "(definitionTerm definitionDescription+)+",
|
|
444
|
+
parseHTML: () => [{ tag: "dl" }],
|
|
445
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
446
|
+
"dl",
|
|
447
|
+
HTMLAttributes,
|
|
448
|
+
0
|
|
449
|
+
]
|
|
450
|
+
});
|
|
451
|
+
const DefinitionTerm = Node.create({
|
|
452
|
+
name: "definitionTerm",
|
|
453
|
+
content: "inline*",
|
|
454
|
+
defining: true,
|
|
455
|
+
parseHTML: () => [{ tag: "dt" }],
|
|
456
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
457
|
+
"dt",
|
|
458
|
+
HTMLAttributes,
|
|
459
|
+
0
|
|
460
|
+
]
|
|
461
|
+
});
|
|
462
|
+
const DefinitionDescription = Node.create({
|
|
463
|
+
name: "definitionDescription",
|
|
464
|
+
content: "block+",
|
|
465
|
+
defining: true,
|
|
466
|
+
parseHTML: () => [{ tag: "dd" }],
|
|
467
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
468
|
+
"dd",
|
|
469
|
+
HTMLAttributes,
|
|
470
|
+
0
|
|
471
|
+
]
|
|
472
|
+
});
|
|
473
|
+
const substitution$2 = {
|
|
474
|
+
default: false,
|
|
475
|
+
parseHTML: (element) => element.hasAttribute("data-substitution"),
|
|
476
|
+
renderHTML: (attrs) => attrs.substitution ? { "data-substitution": "" } : {}
|
|
477
|
+
};
|
|
478
|
+
/** CriticMarkup's suggested insertion, `{++text++}`. */
|
|
479
|
+
const CriticInsertion = Mark.create({
|
|
480
|
+
name: "criticInsertion",
|
|
481
|
+
addAttributes: () => ({ substitution: substitution$2 }),
|
|
482
|
+
parseHTML: () => [{ tag: "ins" }],
|
|
483
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
484
|
+
"ins",
|
|
485
|
+
HTMLAttributes,
|
|
486
|
+
0
|
|
487
|
+
]
|
|
488
|
+
});
|
|
489
|
+
/** CriticMarkup's suggested deletion, `{--text--}`. */
|
|
490
|
+
const CriticDeletion = Mark.create({
|
|
491
|
+
name: "criticDeletion",
|
|
492
|
+
addAttributes: () => ({ substitution: substitution$2 }),
|
|
493
|
+
parseHTML: () => [{
|
|
494
|
+
tag: "del[data-critic]",
|
|
495
|
+
priority: 60
|
|
496
|
+
}],
|
|
497
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
498
|
+
"del",
|
|
499
|
+
{
|
|
500
|
+
...HTMLAttributes,
|
|
501
|
+
"data-critic": ""
|
|
502
|
+
},
|
|
503
|
+
0
|
|
504
|
+
]
|
|
505
|
+
});
|
|
506
|
+
/** CriticMarkup's highlight, `{==text==}`. */
|
|
507
|
+
const CriticHighlight = Mark.create({
|
|
508
|
+
name: "criticHighlight",
|
|
509
|
+
parseHTML: () => [{ tag: "mark" }],
|
|
510
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
511
|
+
"mark",
|
|
512
|
+
HTMLAttributes,
|
|
513
|
+
0
|
|
514
|
+
]
|
|
515
|
+
});
|
|
516
|
+
/** CriticMarkup's comment, `{>>text<<}`. */
|
|
517
|
+
const CriticComment = Node.create({
|
|
518
|
+
name: "criticComment",
|
|
519
|
+
group: "inline",
|
|
520
|
+
inline: true,
|
|
521
|
+
content: "inline*",
|
|
522
|
+
parseHTML: () => [{ tag: "span[data-critic-comment]" }],
|
|
523
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
524
|
+
"span",
|
|
525
|
+
{
|
|
526
|
+
...HTMLAttributes,
|
|
527
|
+
"data-critic-comment": ""
|
|
528
|
+
},
|
|
529
|
+
0
|
|
530
|
+
]
|
|
531
|
+
});
|
|
532
|
+
/**
|
|
533
|
+
* The schema `fromMarkdown` and `toMarkdown` read and write: StarterKit's nodes and marks, task lists, inline images,
|
|
534
|
+
* tables, footnotes, definition lists, CriticMarkup, and Comark's components, slots, and comments. It needs no DOM, so a server can load it to convert a document
|
|
535
|
+
* for a collaborative room.
|
|
536
|
+
*/
|
|
537
|
+
const ComarkKit = Extension.create({
|
|
538
|
+
name: "comarkKit",
|
|
539
|
+
addOptions: () => ({
|
|
540
|
+
starterKit: {},
|
|
541
|
+
componentView: void 0
|
|
542
|
+
}),
|
|
543
|
+
addGlobalAttributes: () => [{
|
|
544
|
+
types: typesWithProps,
|
|
545
|
+
attributes: { props }
|
|
546
|
+
}],
|
|
547
|
+
addExtensions() {
|
|
548
|
+
const view = this.options.componentView;
|
|
549
|
+
const drawn = (node) => view ? node.extend({ addNodeView: () => view }) : node;
|
|
550
|
+
return [
|
|
551
|
+
StarterKit.extend({ addExtensions() {
|
|
552
|
+
return (this.parent?.() ?? []).map((extension) => extension.name === "code" ? extension.extend({ excludes: "" }) : extension);
|
|
553
|
+
} }).configure({
|
|
554
|
+
...this.options.starterKit,
|
|
555
|
+
underline: false
|
|
556
|
+
}),
|
|
557
|
+
TaskList,
|
|
558
|
+
TaskItem.configure({ nested: true }),
|
|
559
|
+
ComarkImage,
|
|
560
|
+
TableKit.configure({ table: { resizable: false } }),
|
|
561
|
+
drawn(ComarkBlock),
|
|
562
|
+
ComarkSlot,
|
|
563
|
+
drawn(ComarkInline),
|
|
564
|
+
ComarkComment,
|
|
565
|
+
FootnoteReference,
|
|
566
|
+
InlineFootnote,
|
|
567
|
+
FootnoteDefinition,
|
|
568
|
+
DefinitionList,
|
|
569
|
+
DefinitionTerm,
|
|
570
|
+
DefinitionDescription,
|
|
571
|
+
CriticInsertion,
|
|
572
|
+
CriticDeletion,
|
|
573
|
+
CriticHighlight,
|
|
574
|
+
CriticComment
|
|
575
|
+
];
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
const handlers = /* @__PURE__ */ new WeakMap();
|
|
579
|
+
const isElement$1 = (node) => Array.isArray(node) && node[0] !== null;
|
|
580
|
+
/** Marks `node` to be written by `handler`. */
|
|
581
|
+
function own(node, handler) {
|
|
582
|
+
handlers.set(node, handler);
|
|
583
|
+
return node;
|
|
584
|
+
}
|
|
585
|
+
const writeOptions = {
|
|
586
|
+
headingIds: false,
|
|
587
|
+
maxInlineAttributes: Number.POSITIVE_INFINITY,
|
|
588
|
+
components: { owned: {
|
|
589
|
+
match: (node) => handlers.has(node),
|
|
590
|
+
handler: (node, state, parent) => handlers.get(node)?.(node, state, parent) ?? ""
|
|
591
|
+
} }
|
|
592
|
+
};
|
|
593
|
+
const TABLE = "comark-editor-table";
|
|
594
|
+
/**
|
|
595
|
+
* A block component, `::name`. Written with no parent, since Comark indents a component inside another, and authors
|
|
596
|
+
* write them flush: the colons already say how deep each one is.
|
|
597
|
+
*/
|
|
598
|
+
const blockComponent = async (node, state) => {
|
|
599
|
+
const [tag, attrs, ...children] = node;
|
|
600
|
+
if (tag !== "table") return state.handlers.mdc(node, state);
|
|
601
|
+
return (await state.handlers.mdc([
|
|
602
|
+
TABLE,
|
|
603
|
+
attrs,
|
|
604
|
+
...children
|
|
605
|
+
], state)).replace(TABLE, "table");
|
|
606
|
+
};
|
|
607
|
+
/** An inline component, `:name[text]{props}`, which Comark would write as a block when it's alone in its paragraph. */
|
|
608
|
+
const inlineComponent = async (node, state, parent) => {
|
|
609
|
+
const revert = state.applyContext({ inline: true });
|
|
610
|
+
try {
|
|
611
|
+
return await state.handlers.mdc(node, state, parent);
|
|
612
|
+
} finally {
|
|
613
|
+
state.applyContext(revert);
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
/**
|
|
617
|
+
* A task, written as a list item with its check box added after the marker. Comark's own writer indents the blocks
|
|
618
|
+
* under a task by the width of `- [ ] `, which reads them back as code.
|
|
619
|
+
*/
|
|
620
|
+
const task = (checked) => async (node, state, parent) => (await state.handlers.li(node, state, parent)).replace(/^(- |\d+\. )/, `$1[${checked ? "x" : " "}] `);
|
|
621
|
+
const END = "";
|
|
622
|
+
/** A code block that ends in blank lines, which Comark's writer trims off. */
|
|
623
|
+
const trailingLines = async ([tag, attrs, ...children], state) => {
|
|
624
|
+
const written = await state.handlers.pre([
|
|
625
|
+
tag,
|
|
626
|
+
attrs,
|
|
627
|
+
...children,
|
|
628
|
+
END
|
|
629
|
+
], state);
|
|
630
|
+
const at = written.lastIndexOf(END);
|
|
631
|
+
return written.slice(0, at) + written.slice(at + 1);
|
|
632
|
+
};
|
|
633
|
+
/** Inline content between two delimiters, such as `{++` and `++}`. */
|
|
634
|
+
const between = (open, close) => async (node, state) => `${open}${await state.flow(node, state)}${close}`;
|
|
635
|
+
/** `{~~old~>new~~}`, from its `del` and `ins` halves. */
|
|
636
|
+
const substitution$1 = async ([, , removed, added], state) => {
|
|
637
|
+
const flow = async (half) => isElement$1(half) ? state.flow(half, state) : "";
|
|
638
|
+
return `{~~${await flow(removed)}~>${await flow(added)}~~}`;
|
|
639
|
+
};
|
|
640
|
+
const footnoteReference = (label) => () => `[^${label}]`;
|
|
641
|
+
/** `[^label]: text`, with the rest of its blocks indented under it. */
|
|
642
|
+
const footnoteDefinition = (label) => async (node, state) => {
|
|
643
|
+
const content = (await state.flow(node, state)).trim();
|
|
644
|
+
return `[^${label}]: ${indent(content, {
|
|
645
|
+
width: 4,
|
|
646
|
+
ignoreFirstLine: true
|
|
647
|
+
})}${state.context.blockSeparator}`;
|
|
648
|
+
};
|
|
649
|
+
/** Each term on its line, each definition after `: ` with its blocks indented under it, and a blank line before the next term. */
|
|
650
|
+
const definitionList = async (node, state) => {
|
|
651
|
+
let written = "";
|
|
652
|
+
for (const item of node.slice(2)) {
|
|
653
|
+
if (!isElement$1(item)) continue;
|
|
654
|
+
const content = (await state.flow(item, state)).trim();
|
|
655
|
+
if (item[0] === "dt") written += `${written ? "\n" : ""}${content}\n`;
|
|
656
|
+
else written += `: ${indent(content, {
|
|
657
|
+
width: 4,
|
|
658
|
+
ignoreFirstLine: true
|
|
659
|
+
})}\n`;
|
|
660
|
+
}
|
|
661
|
+
return written.trimEnd() + state.context.blockSeparator;
|
|
662
|
+
};
|
|
663
|
+
/**
|
|
664
|
+
* An inline element and a block element, by the prefix of their `_open` and `_close` types. Not `mdc_block_slot` for
|
|
665
|
+
* blocks: directly inside a `::component` Comark reads those as named slots and splits the block apart.
|
|
666
|
+
*/
|
|
667
|
+
const SPAN = "mdc_inline_span";
|
|
668
|
+
/**
|
|
669
|
+
* Adds a rule before or after the first of `anchors` the parser has, else last. Comark's own rules exist only with
|
|
670
|
+
* its default plugins, and markdown-exit throws on a missing anchor, which would make every parse throw.
|
|
671
|
+
*/
|
|
672
|
+
function addRule(ruler, place, anchors, name, fn, options) {
|
|
673
|
+
for (const anchor of anchors) try {
|
|
674
|
+
ruler[place](anchor, name, fn, options);
|
|
675
|
+
return;
|
|
676
|
+
} catch {}
|
|
677
|
+
ruler.push(name, fn, options);
|
|
678
|
+
}
|
|
679
|
+
/** The element around the two halves of `{~~old~>new~~}`, which comark-critic writes as a `del` beside an `ins`. */
|
|
680
|
+
const SUBSTITUTION = "critic-sub";
|
|
681
|
+
let inside = false;
|
|
682
|
+
function substitution(state, silent) {
|
|
683
|
+
if (silent || inside || !state.src.startsWith("{~~", state.pos)) return false;
|
|
684
|
+
const start = state.pos;
|
|
685
|
+
const max = state.posMax;
|
|
686
|
+
const rules = state.md.inline.ruler.getRules("");
|
|
687
|
+
const criticRule = rules[rules.indexOf(substitution) + 1];
|
|
688
|
+
if (!criticRule?.(state, true)) return false;
|
|
689
|
+
const end = state.pos;
|
|
690
|
+
state.pos = start;
|
|
691
|
+
state.push(`${SPAN}_open`, SUBSTITUTION, 1);
|
|
692
|
+
state.posMax = end;
|
|
693
|
+
inside = true;
|
|
694
|
+
try {
|
|
695
|
+
state.md.inline.tokenize(state);
|
|
696
|
+
} finally {
|
|
697
|
+
inside = false;
|
|
698
|
+
}
|
|
699
|
+
state.posMax = max;
|
|
700
|
+
state.push(`${SPAN}_close`, SUBSTITUTION, -1);
|
|
701
|
+
return true;
|
|
702
|
+
}
|
|
703
|
+
const substitutions = defineComarkPlugin(() => ({
|
|
704
|
+
name: "editor-critic-substitutions",
|
|
705
|
+
markdownItPlugins: [(md) => addRule(md.inline.ruler, "before", ["critic"], "critic_substitution", substitution)]
|
|
706
|
+
}));
|
|
707
|
+
const footnoteMarkers = defineComarkPlugin(() => ({
|
|
708
|
+
name: "editor-footnotes",
|
|
709
|
+
markdownItPlugins: src_default().markdownItPlugins
|
|
710
|
+
}));
|
|
711
|
+
const noAlert = defineComarkPlugin(() => ({ name: "alert" }));
|
|
712
|
+
/** The prop on a component written `:name{props}` alone on its line, which Comark reads like an empty `::name`. */
|
|
713
|
+
const SHORTHAND = "data-comark-editor-shorthand";
|
|
714
|
+
function markShorthands(tokens) {
|
|
715
|
+
for (const token of tokens) {
|
|
716
|
+
if (token.type === "mdc_block_shorthand" || token.type === "mdc_block_shorthand_open") token.attrSet(SHORTHAND, "");
|
|
717
|
+
if (token.children) markShorthands(token.children);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const shorthands = defineComarkPlugin(() => ({
|
|
721
|
+
name: "editor-shorthands",
|
|
722
|
+
markdownItPost: (state) => markShorthands(state.tokens)
|
|
723
|
+
}));
|
|
724
|
+
/**
|
|
725
|
+
* The syntax the codec reads beyond Comark's own: CriticMarkup, definition lists, footnotes, and emphasis next to CJK
|
|
726
|
+
* punctuation. Each would otherwise reach the tree as text, or not at all, and be written back escaped or dropped.
|
|
727
|
+
* It also marks the components written `:name` alone on their line, so they're written back that way.
|
|
728
|
+
*/
|
|
729
|
+
const syntax = [
|
|
730
|
+
noAlert(),
|
|
731
|
+
src_default$1(),
|
|
732
|
+
substitutions(),
|
|
733
|
+
src_default$2(),
|
|
734
|
+
src_default$3(),
|
|
735
|
+
footnoteMarkers(),
|
|
736
|
+
shorthands()
|
|
737
|
+
];
|
|
738
|
+
const parseOptions = {
|
|
739
|
+
autoUnwrap: false,
|
|
740
|
+
headingIds: false
|
|
741
|
+
};
|
|
742
|
+
const readOptions = {
|
|
743
|
+
...parseOptions,
|
|
744
|
+
plugins: syntax
|
|
745
|
+
};
|
|
746
|
+
const followed = ["linkify", "html"];
|
|
747
|
+
function readOptionsFor({ plugins = [], registerDefaultPlugins }) {
|
|
748
|
+
if (registerDefaultPlugins !== false) return readOptions;
|
|
749
|
+
const page = (name) => plugins.filter((plugin) => plugin.name === name);
|
|
750
|
+
return {
|
|
751
|
+
...parseOptions,
|
|
752
|
+
registerDefaultPlugins: false,
|
|
753
|
+
plugins: [
|
|
754
|
+
...page(followed[0]),
|
|
755
|
+
frontmatterPlugin(),
|
|
756
|
+
...page(followed[1]),
|
|
757
|
+
taskList(),
|
|
758
|
+
components(),
|
|
759
|
+
attributes(),
|
|
760
|
+
...syntax
|
|
761
|
+
]
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
const marks = /* @__PURE__ */ new Map([
|
|
765
|
+
["strong", "bold"],
|
|
766
|
+
["em", "italic"],
|
|
767
|
+
["del", "strike"],
|
|
768
|
+
["code", "code"],
|
|
769
|
+
["a", "link"]
|
|
770
|
+
]);
|
|
771
|
+
const DEFINITION = "cfn-def:";
|
|
772
|
+
const REFERENCE = "cfn-ref:";
|
|
773
|
+
const NOTE = "cfn-note";
|
|
774
|
+
const markTags = new Map([...marks].map(([tag, mark]) => [mark, tag]));
|
|
775
|
+
const inlineTags = /* @__PURE__ */ new Set([
|
|
776
|
+
...marks.keys(),
|
|
777
|
+
"br",
|
|
778
|
+
"img",
|
|
779
|
+
"span",
|
|
780
|
+
"input",
|
|
781
|
+
"ins",
|
|
782
|
+
"mark",
|
|
783
|
+
SUBSTITUTION,
|
|
784
|
+
NOTE
|
|
785
|
+
]);
|
|
786
|
+
const headings = /* @__PURE__ */ new Set([
|
|
787
|
+
"h1",
|
|
788
|
+
"h2",
|
|
789
|
+
"h3",
|
|
790
|
+
"h4",
|
|
791
|
+
"h5",
|
|
792
|
+
"h6"
|
|
793
|
+
]);
|
|
794
|
+
const isText = (node) => !Array.isArray(node);
|
|
795
|
+
const isElement = (node) => Array.isArray(node) && node[0] !== null;
|
|
796
|
+
const childrenOf = (element) => element.slice(2);
|
|
797
|
+
const inlineHtml = ({ $: meta }) => meta instanceof Object && "block" in meta && meta.block === 0;
|
|
798
|
+
const isInline = (node) => isText(node) || isElement(node) && (inlineTags.has(node[0]) || node[0].startsWith(REFERENCE) || inlineHtml(node[1]));
|
|
799
|
+
const isHtml = (props) => props.$ instanceof Object;
|
|
800
|
+
/** The props a node has no field for, or `undefined` when none are left. */
|
|
801
|
+
function rest(props, ...keys) {
|
|
802
|
+
const left = Object.fromEntries(Object.entries(props).filter(([key]) => !keys.includes(key)));
|
|
803
|
+
return Object.keys(left).length ? left : void 0;
|
|
804
|
+
}
|
|
805
|
+
const classes = (props) => scalar(props.class)?.split(/\s+/) ?? [];
|
|
806
|
+
function withoutClass(props, name) {
|
|
807
|
+
const left = classes(props).filter((value) => value && value !== name);
|
|
808
|
+
const { class: _, ...others } = props;
|
|
809
|
+
return left.length ? {
|
|
810
|
+
...others,
|
|
811
|
+
class: left.join(" ")
|
|
812
|
+
} : others;
|
|
813
|
+
}
|
|
814
|
+
function withClass(props, name) {
|
|
815
|
+
return {
|
|
816
|
+
...props,
|
|
817
|
+
class: [name, ...classes(props).filter((value) => value && value !== name)].join(" ")
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
const withProps = (attrs, props) => props ? {
|
|
821
|
+
...attrs,
|
|
822
|
+
props
|
|
823
|
+
} : attrs;
|
|
824
|
+
const node = (type, attrs, content) => {
|
|
825
|
+
const json = { type };
|
|
826
|
+
if (Object.keys(attrs).length) json.attrs = attrs;
|
|
827
|
+
if (content?.length) json.content = content;
|
|
828
|
+
return json;
|
|
829
|
+
};
|
|
830
|
+
function readBlocks(children, reading) {
|
|
831
|
+
const out = [];
|
|
832
|
+
let run = [];
|
|
833
|
+
const flush = () => {
|
|
834
|
+
if (run.some((child) => !isText(child) || child.trim())) out.push(node("paragraph", {}, readInline(run, [], reading)));
|
|
835
|
+
run = [];
|
|
836
|
+
};
|
|
837
|
+
for (const child of children) if (isInline(child)) run.push(child);
|
|
838
|
+
else {
|
|
839
|
+
flush();
|
|
840
|
+
out.push(...readBlock(child, reading));
|
|
841
|
+
}
|
|
842
|
+
flush();
|
|
843
|
+
return out;
|
|
844
|
+
}
|
|
845
|
+
function readBlock(child, reading) {
|
|
846
|
+
if (isText(child)) return [];
|
|
847
|
+
if (!isElement(child)) return [node("comarkComment", { text: child[2] })];
|
|
848
|
+
const [tag, props] = child;
|
|
849
|
+
const children = childrenOf(child);
|
|
850
|
+
if ("data-comark-editor-shorthand" in props) {
|
|
851
|
+
const { [SHORTHAND]: _, ...attrs } = props;
|
|
852
|
+
return [node("paragraph", {}, readInline([[
|
|
853
|
+
tag,
|
|
854
|
+
attrs,
|
|
855
|
+
...children
|
|
856
|
+
]], [], reading))];
|
|
857
|
+
}
|
|
858
|
+
if (tag === "p") return [node("paragraph", withProps({}, rest(props)), readInline(children, [], reading))];
|
|
859
|
+
if (headings.has(tag)) return [node("heading", withProps({ level: Number(tag[1]) }, rest(props)), readInline(children, [], reading))];
|
|
860
|
+
if (tag === "blockquote") return [node("blockquote", withProps({}, rest(props)), readBlocks(children, reading))];
|
|
861
|
+
if (tag === "hr") return [node("horizontalRule", withProps({}, rest(props)))];
|
|
862
|
+
if (tag === "pre") return [readCode(props, children)];
|
|
863
|
+
if (tag === "table" && children.length && children.every((part) => isElement(part) && [
|
|
864
|
+
"thead",
|
|
865
|
+
"tbody",
|
|
866
|
+
"tr"
|
|
867
|
+
].includes(part[0]))) return [readTable(props, children, reading)];
|
|
868
|
+
if (tag === "ul" || tag === "ol") return [readList(tag, props, children, reading)];
|
|
869
|
+
if (tag.startsWith(DEFINITION)) return [node("footnoteDefinition", { label: tag.slice(8) }, filled(readBlocks(children, reading)))];
|
|
870
|
+
if (tag === "dl") return [readDefinitions(props, children, reading)];
|
|
871
|
+
if (tag === "template") reading.lossy = true;
|
|
872
|
+
return [node("comarkBlock", withProps({ name: tag }, rest(props)), readComponent(children, reading))];
|
|
873
|
+
}
|
|
874
|
+
/** Blocks that need at least one, as an empty item or definition writes nothing. */
|
|
875
|
+
const filled = (blocks) => blocks.length ? blocks : [{ type: "paragraph" }];
|
|
876
|
+
function readDefinitions(props, children, reading) {
|
|
877
|
+
return node("definitionList", withProps({}, rest(props)), children.flatMap((item) => {
|
|
878
|
+
if (!isElement(item)) return [];
|
|
879
|
+
return item[0] === "dt" ? [node("definitionTerm", withProps({}, rest(item[1])), readInline(childrenOf(item), [], reading))] : [node("definitionDescription", withProps({}, rest(item[1])), filled(readBlocks(childrenOf(item), reading)))];
|
|
880
|
+
}));
|
|
881
|
+
}
|
|
882
|
+
function readComponent(children, reading) {
|
|
883
|
+
const out = [];
|
|
884
|
+
let run = [];
|
|
885
|
+
const flush = () => {
|
|
886
|
+
out.push(...readBlocks(run, reading));
|
|
887
|
+
run = [];
|
|
888
|
+
};
|
|
889
|
+
for (const child of children) if (isElement(child) && child[0] === "template") {
|
|
890
|
+
flush();
|
|
891
|
+
const { name, ...props } = child[1];
|
|
892
|
+
out.push(node("comarkSlot", withProps({ name: scalar(name) ?? "default" }, rest(props)), readBlocks(childrenOf(child), reading)));
|
|
893
|
+
} else run.push(child);
|
|
894
|
+
flush();
|
|
895
|
+
return out;
|
|
896
|
+
}
|
|
897
|
+
function readCode(props, children) {
|
|
898
|
+
const code = children.find(isElement);
|
|
899
|
+
const text = (code ? childrenOf(code) : children).filter(isText).join("");
|
|
900
|
+
const language = scalar(props.language) ?? null;
|
|
901
|
+
return node("codeBlock", withProps(language ? { language } : {}, rest(props, "language")), text ? [{
|
|
902
|
+
type: "text",
|
|
903
|
+
text
|
|
904
|
+
}] : []);
|
|
905
|
+
}
|
|
906
|
+
const isTaskItem = (child) => isElement(child) && child[0] === "li" && classes(child[1]).includes("task-list-item");
|
|
907
|
+
const isCheckbox = (child) => isElement(child) && child[0] === "input" && child[1].type === "checkbox";
|
|
908
|
+
function readList(tag, props, children, reading) {
|
|
909
|
+
const items = children.filter(isElement);
|
|
910
|
+
if (tag === "ol" && items.some(isTaskItem)) reading.lossy = true;
|
|
911
|
+
if (tag === "ul" && items.length && items.every(isTaskItem)) return node("taskList", withProps({}, rest(withoutClass(props, "contains-task-list"))), items.map((item) => readTaskItem(item, reading)));
|
|
912
|
+
const listItems = items.map((item) => {
|
|
913
|
+
const blocks = readBlocks(childrenOf(item), reading);
|
|
914
|
+
if (blocks[0]?.type !== "paragraph") blocks.unshift({ type: "paragraph" });
|
|
915
|
+
return node("listItem", withProps({}, rest(item[1])), blocks);
|
|
916
|
+
});
|
|
917
|
+
if (tag === "ul") return node("bulletList", withProps({}, rest(props)), listItems);
|
|
918
|
+
const start = Number(props.start ?? 1);
|
|
919
|
+
return node("orderedList", withProps(start === 1 ? {} : { start }, rest(props, "start")), listItems);
|
|
920
|
+
}
|
|
921
|
+
function readTaskItem(item, reading) {
|
|
922
|
+
let checked = false;
|
|
923
|
+
const strip = (content) => {
|
|
924
|
+
const [box, next, ...after] = content;
|
|
925
|
+
if (box === void 0 || !isCheckbox(box)) return content;
|
|
926
|
+
checked = box[1][":checked"] === "true" || box[1].checked === true;
|
|
927
|
+
if (next === void 0) return after;
|
|
928
|
+
return isText(next) ? [next.replace(/^\s+/, ""), ...after] : [next, ...after];
|
|
929
|
+
};
|
|
930
|
+
const [first, ...others] = childrenOf(item);
|
|
931
|
+
let content;
|
|
932
|
+
if (first !== void 0 && isElement(first) && first[0] === "p") content = [[
|
|
933
|
+
first[0],
|
|
934
|
+
first[1],
|
|
935
|
+
...strip(childrenOf(first))
|
|
936
|
+
], ...others];
|
|
937
|
+
else content = strip(childrenOf(item));
|
|
938
|
+
const blocks = readBlocks(content, reading);
|
|
939
|
+
if (blocks[0]?.type !== "paragraph") blocks.unshift({ type: "paragraph" });
|
|
940
|
+
return node("taskItem", withProps({ checked }, rest(withoutClass(item[1], "task-list-item"))), blocks);
|
|
941
|
+
}
|
|
942
|
+
function readTable(props, children, reading) {
|
|
943
|
+
const rows = children.filter(isElement).flatMap((section) => section[0] === "tr" ? [section] : childrenOf(section).filter(isElement));
|
|
944
|
+
return node("table", withProps({}, rest(props)), rows.map((row) => node("tableRow", withProps({}, rest(row[1])), childrenOf(row).filter(isElement).map((cell) => node(cell[0] === "th" ? "tableHeader" : "tableCell", withProps({}, rest(cell[1])), [node("paragraph", {}, readInline(childrenOf(cell), [], reading))])))));
|
|
945
|
+
}
|
|
946
|
+
const critics = /* @__PURE__ */ new Map([
|
|
947
|
+
["ins", {
|
|
948
|
+
className: "ccm-ins",
|
|
949
|
+
type: "criticInsertion"
|
|
950
|
+
}],
|
|
951
|
+
["del", {
|
|
952
|
+
className: "ccm-del",
|
|
953
|
+
type: "criticDeletion"
|
|
954
|
+
}],
|
|
955
|
+
["mark", {
|
|
956
|
+
className: "ccm-mark",
|
|
957
|
+
type: "criticHighlight"
|
|
958
|
+
}]
|
|
959
|
+
]);
|
|
960
|
+
const COMMENT = "ccm-comment";
|
|
961
|
+
function criticMark([tag, props]) {
|
|
962
|
+
const critic = critics.get(tag);
|
|
963
|
+
if (!critic || !classes(props).includes(critic.className)) return;
|
|
964
|
+
return {
|
|
965
|
+
className: critic.className,
|
|
966
|
+
mark: { type: critic.type }
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
function readInline(children, active, reading) {
|
|
970
|
+
const out = [];
|
|
971
|
+
const marked = (json) => active.length ? {
|
|
972
|
+
...json,
|
|
973
|
+
marks: active
|
|
974
|
+
} : json;
|
|
975
|
+
for (const child of children) {
|
|
976
|
+
if (isText(child)) {
|
|
977
|
+
if (child) out.push(marked({
|
|
978
|
+
type: "text",
|
|
979
|
+
text: child
|
|
980
|
+
}));
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
if (!isElement(child)) {
|
|
984
|
+
reading.lossy = true;
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
const [tag, props] = child;
|
|
988
|
+
const content = childrenOf(child);
|
|
989
|
+
const critic = criticMark(child);
|
|
990
|
+
const mark = marks.get(tag);
|
|
991
|
+
if (critic) {
|
|
992
|
+
if (rest(withoutClass(props, critic.className))) reading.lossy = true;
|
|
993
|
+
out.push(...readInline(content, [...active, critic.mark], reading));
|
|
994
|
+
} else if (tag === "critic-sub") {
|
|
995
|
+
const [removed, added] = content.filter(isElement);
|
|
996
|
+
const half = (element, type) => element ? readInline(childrenOf(element), [...active, {
|
|
997
|
+
type,
|
|
998
|
+
attrs: { substitution: true }
|
|
999
|
+
}], reading) : [];
|
|
1000
|
+
out.push(...half(removed, "criticDeletion"), ...half(added, "criticInsertion"));
|
|
1001
|
+
} else if (tag === "span" && classes(props).includes(COMMENT)) {
|
|
1002
|
+
if (rest(withoutClass(props, COMMENT))) reading.lossy = true;
|
|
1003
|
+
out.push(marked(node("criticComment", {}, readInline(content, [], reading))));
|
|
1004
|
+
} else if (tag.startsWith(REFERENCE)) out.push(marked(node("footnoteReference", { label: tag.slice(8) })));
|
|
1005
|
+
else if (tag === NOTE) out.push(marked(node("inlineFootnote", {}, readInline(content, [], reading))));
|
|
1006
|
+
else if (mark && !(tag === "code" && !content.every(isText))) {
|
|
1007
|
+
const attrs = mark === "link" ? withProps({
|
|
1008
|
+
href: props.href ?? null,
|
|
1009
|
+
title: props.title ?? null
|
|
1010
|
+
}, rest(props, "href", "title")) : withProps({}, rest(props));
|
|
1011
|
+
const next = Object.keys(attrs).length ? {
|
|
1012
|
+
type: mark,
|
|
1013
|
+
attrs
|
|
1014
|
+
} : { type: mark };
|
|
1015
|
+
out.push(...readInline(content, [...active, next], reading));
|
|
1016
|
+
} else if (tag === "br") out.push(marked(node("hardBreak", withProps({}, rest(props)))));
|
|
1017
|
+
else if (tag === "img") out.push(marked(node("image", withProps({
|
|
1018
|
+
src: props.src ?? null,
|
|
1019
|
+
alt: props.alt ?? null,
|
|
1020
|
+
title: props.title ?? null
|
|
1021
|
+
}, rest(props, "src", "alt", "title")))));
|
|
1022
|
+
else out.push(marked(node("comarkInline", withProps({ name: tag }, rest(props)), readInline(content, [], reading))));
|
|
1023
|
+
}
|
|
1024
|
+
return out;
|
|
1025
|
+
}
|
|
1026
|
+
const propsOf = (attrs) => attrs?.props ?? {};
|
|
1027
|
+
const plainText = (content = []) => content.map((child) => child.text ?? "").join("");
|
|
1028
|
+
function writeBlocks(content = []) {
|
|
1029
|
+
return content.flatMap(writeBlock);
|
|
1030
|
+
}
|
|
1031
|
+
function writeBlock(json) {
|
|
1032
|
+
const props = propsOf(json.attrs);
|
|
1033
|
+
const attrs = json.attrs ?? {};
|
|
1034
|
+
switch (json.type) {
|
|
1035
|
+
case "paragraph": return json.content?.length ? [[
|
|
1036
|
+
"p",
|
|
1037
|
+
props,
|
|
1038
|
+
...writeInline(json.content)
|
|
1039
|
+
]] : [];
|
|
1040
|
+
case "heading": return [[
|
|
1041
|
+
`h${Number(attrs.level ?? 1)}`,
|
|
1042
|
+
props,
|
|
1043
|
+
...writeInline(json.content)
|
|
1044
|
+
]];
|
|
1045
|
+
case "blockquote": return [[
|
|
1046
|
+
"blockquote",
|
|
1047
|
+
props,
|
|
1048
|
+
...writeBlocks(json.content)
|
|
1049
|
+
]];
|
|
1050
|
+
case "horizontalRule": return [["hr", props]];
|
|
1051
|
+
case "codeBlock": {
|
|
1052
|
+
const language = scalar(attrs.language) || void 0;
|
|
1053
|
+
const code = plainText(json.content);
|
|
1054
|
+
const pre = [
|
|
1055
|
+
"pre",
|
|
1056
|
+
language ? {
|
|
1057
|
+
...props,
|
|
1058
|
+
language
|
|
1059
|
+
} : props,
|
|
1060
|
+
[
|
|
1061
|
+
"code",
|
|
1062
|
+
language ? { class: `language-${language}` } : {},
|
|
1063
|
+
code
|
|
1064
|
+
]
|
|
1065
|
+
];
|
|
1066
|
+
return [code.trimEnd() === code ? pre : own(pre, trailingLines)];
|
|
1067
|
+
}
|
|
1068
|
+
case "bulletList": return [[
|
|
1069
|
+
"ul",
|
|
1070
|
+
props,
|
|
1071
|
+
...writeBlocks(json.content)
|
|
1072
|
+
]];
|
|
1073
|
+
case "orderedList": {
|
|
1074
|
+
const start = Number(attrs.start ?? 1);
|
|
1075
|
+
return [[
|
|
1076
|
+
"ol",
|
|
1077
|
+
start === 1 ? props : {
|
|
1078
|
+
...props,
|
|
1079
|
+
start
|
|
1080
|
+
},
|
|
1081
|
+
...writeBlocks(json.content)
|
|
1082
|
+
]];
|
|
1083
|
+
}
|
|
1084
|
+
case "listItem": return [[
|
|
1085
|
+
"li",
|
|
1086
|
+
props,
|
|
1087
|
+
...writeBlocks(json.content)
|
|
1088
|
+
]];
|
|
1089
|
+
case "taskList": return [[
|
|
1090
|
+
"ul",
|
|
1091
|
+
withClass(props, "contains-task-list"),
|
|
1092
|
+
...writeBlocks(json.content)
|
|
1093
|
+
]];
|
|
1094
|
+
case "taskItem": return [own([
|
|
1095
|
+
"li",
|
|
1096
|
+
props,
|
|
1097
|
+
...writeBlocks(json.content)
|
|
1098
|
+
], task(Boolean(attrs.checked)))];
|
|
1099
|
+
case "table": return [writeTable(json, props)];
|
|
1100
|
+
case "comarkBlock": {
|
|
1101
|
+
const component = [
|
|
1102
|
+
String(attrs.name),
|
|
1103
|
+
props,
|
|
1104
|
+
...(json.content ?? []).flatMap((child) => child.type === "comarkSlot" ? [[
|
|
1105
|
+
"template",
|
|
1106
|
+
{
|
|
1107
|
+
...propsOf(child.attrs),
|
|
1108
|
+
name: child.attrs?.name
|
|
1109
|
+
},
|
|
1110
|
+
...writeBlocks(child.content)
|
|
1111
|
+
]] : writeBlock(child))
|
|
1112
|
+
];
|
|
1113
|
+
return [isHtml(props) ? component : own(component, blockComponent)];
|
|
1114
|
+
}
|
|
1115
|
+
case "footnoteDefinition": {
|
|
1116
|
+
const label = scalar(attrs.label) ?? "";
|
|
1117
|
+
return [own([
|
|
1118
|
+
"footnote",
|
|
1119
|
+
{},
|
|
1120
|
+
...writeBlocks(json.content)
|
|
1121
|
+
], footnoteDefinition(label))];
|
|
1122
|
+
}
|
|
1123
|
+
case "definitionList": return [own([
|
|
1124
|
+
"dl",
|
|
1125
|
+
props,
|
|
1126
|
+
...(json.content ?? []).map((item) => item.type === "definitionTerm" ? [
|
|
1127
|
+
"dt",
|
|
1128
|
+
propsOf(item.attrs),
|
|
1129
|
+
...writeInline(item.content)
|
|
1130
|
+
] : [
|
|
1131
|
+
"dd",
|
|
1132
|
+
propsOf(item.attrs),
|
|
1133
|
+
...writeBlocks(item.content)
|
|
1134
|
+
])
|
|
1135
|
+
], definitionList)];
|
|
1136
|
+
case "comarkComment": return [[
|
|
1137
|
+
null,
|
|
1138
|
+
{},
|
|
1139
|
+
scalar(attrs.text) ?? ""
|
|
1140
|
+
]];
|
|
1141
|
+
default: return [];
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
function writeTable(json, props) {
|
|
1145
|
+
const rows = json.content ?? [];
|
|
1146
|
+
const isHeader = (row) => row.content?.every((cell) => cell.type === "tableHeader") ?? false;
|
|
1147
|
+
const firstBody = rows.findIndex((row) => !isHeader(row));
|
|
1148
|
+
const split = firstBody === -1 ? rows.length : Math.max(1, firstBody);
|
|
1149
|
+
const row = (tag) => (json) => [
|
|
1150
|
+
"tr",
|
|
1151
|
+
propsOf(json.attrs),
|
|
1152
|
+
...(json.content ?? []).map((cell) => [
|
|
1153
|
+
tag,
|
|
1154
|
+
propsOf(cell.attrs),
|
|
1155
|
+
...(cell.content ?? []).flatMap((paragraph, index) => [...index ? [["br", {}]] : [], ...writeInline(paragraph.content)])
|
|
1156
|
+
])
|
|
1157
|
+
];
|
|
1158
|
+
const body = rows.slice(split).map(row("td"));
|
|
1159
|
+
return [
|
|
1160
|
+
"table",
|
|
1161
|
+
props,
|
|
1162
|
+
[
|
|
1163
|
+
"thead",
|
|
1164
|
+
{},
|
|
1165
|
+
...rows.slice(0, split).map(row("th"))
|
|
1166
|
+
],
|
|
1167
|
+
...body.length ? [[
|
|
1168
|
+
"tbody",
|
|
1169
|
+
{},
|
|
1170
|
+
...body
|
|
1171
|
+
]] : []
|
|
1172
|
+
];
|
|
1173
|
+
}
|
|
1174
|
+
const sameMark = (a, b) => a.type === b.type && JSON.stringify(a.attrs ?? {}) === JSON.stringify(b.attrs ?? {});
|
|
1175
|
+
const criticDelimiters = /* @__PURE__ */ new Map([
|
|
1176
|
+
["criticInsertion", ["{++", "++}"]],
|
|
1177
|
+
["criticDeletion", ["{--", "--}"]],
|
|
1178
|
+
["criticHighlight", ["{==", "==}"]]
|
|
1179
|
+
]);
|
|
1180
|
+
function writeMark(mark, children) {
|
|
1181
|
+
const delimiters = criticDelimiters.get(mark.type);
|
|
1182
|
+
if (delimiters) return own([
|
|
1183
|
+
mark.attrs?.substitution ? mark.type : "critic",
|
|
1184
|
+
{},
|
|
1185
|
+
...children
|
|
1186
|
+
], between(...delimiters));
|
|
1187
|
+
const props = propsOf(mark.attrs);
|
|
1188
|
+
if (mark.type !== "link") return [
|
|
1189
|
+
markTags.get(mark.type) ?? "span",
|
|
1190
|
+
props,
|
|
1191
|
+
...children
|
|
1192
|
+
];
|
|
1193
|
+
const href = scalar(mark.attrs?.href);
|
|
1194
|
+
const title = scalar(mark.attrs?.title);
|
|
1195
|
+
const link = {
|
|
1196
|
+
...props,
|
|
1197
|
+
href
|
|
1198
|
+
};
|
|
1199
|
+
if (title) link.title = title;
|
|
1200
|
+
return [
|
|
1201
|
+
"a",
|
|
1202
|
+
link,
|
|
1203
|
+
...children
|
|
1204
|
+
];
|
|
1205
|
+
}
|
|
1206
|
+
function append(children, child) {
|
|
1207
|
+
const last = children.at(-1);
|
|
1208
|
+
if (last !== void 0 && isText(last) && isText(child)) children[children.length - 1] = last + child;
|
|
1209
|
+
else children.push(child);
|
|
1210
|
+
}
|
|
1211
|
+
function writeInline(content = []) {
|
|
1212
|
+
const open = [];
|
|
1213
|
+
const root = [];
|
|
1214
|
+
const close = (depth) => {
|
|
1215
|
+
for (let frame = open.pop(); frame; frame = open.length > depth ? open.pop() : void 0) append(open.at(-1)?.children ?? root, writeMark(frame.mark, frame.children));
|
|
1216
|
+
};
|
|
1217
|
+
for (const json of content) {
|
|
1218
|
+
const active = [...json.marks ?? []].sort((left, right) => markOrder(left) - markOrder(right));
|
|
1219
|
+
const same = (index) => {
|
|
1220
|
+
const frame = open[index];
|
|
1221
|
+
const mark = active[index];
|
|
1222
|
+
return frame !== void 0 && mark !== void 0 && sameMark(frame.mark, mark);
|
|
1223
|
+
};
|
|
1224
|
+
let keep = 0;
|
|
1225
|
+
while (same(keep)) keep++;
|
|
1226
|
+
if (open.length > keep) close(keep);
|
|
1227
|
+
for (const mark of active.slice(keep)) open.push({
|
|
1228
|
+
mark,
|
|
1229
|
+
children: []
|
|
1230
|
+
});
|
|
1231
|
+
for (const child of writeLeaf(json)) append(open.at(-1)?.children ?? root, child);
|
|
1232
|
+
}
|
|
1233
|
+
if (open.length) close(0);
|
|
1234
|
+
return pairSubstitutions(root);
|
|
1235
|
+
}
|
|
1236
|
+
const markOrder = (mark) => criticDelimiters.has(mark.type) ? -1 : mark.type === "code" ? 1 : 0;
|
|
1237
|
+
/** Joins a deletion and the insertion right after it, both from one `{~~old~>new~~}`, back into one. */
|
|
1238
|
+
function pairSubstitutions(children) {
|
|
1239
|
+
const out = [];
|
|
1240
|
+
for (const child of children) {
|
|
1241
|
+
const last = out.at(-1);
|
|
1242
|
+
if (isElement(child) && child[0] === "criticInsertion" && last !== void 0 && isElement(last) && last[0] === "criticDeletion") out[out.length - 1] = own([
|
|
1243
|
+
SUBSTITUTION,
|
|
1244
|
+
{},
|
|
1245
|
+
last,
|
|
1246
|
+
child
|
|
1247
|
+
], substitution$1);
|
|
1248
|
+
else out.push(child);
|
|
1249
|
+
}
|
|
1250
|
+
return out;
|
|
1251
|
+
}
|
|
1252
|
+
function writeLeaf(json) {
|
|
1253
|
+
const props = propsOf(json.attrs);
|
|
1254
|
+
const attrs = json.attrs ?? {};
|
|
1255
|
+
if (json.type === "text") return json.text ? [json.text] : [];
|
|
1256
|
+
if (json.type === "hardBreak") return [["br", props]];
|
|
1257
|
+
if (json.type === "image") {
|
|
1258
|
+
const image = {
|
|
1259
|
+
...props,
|
|
1260
|
+
src: attrs.src
|
|
1261
|
+
};
|
|
1262
|
+
if (attrs.alt) image.alt = attrs.alt;
|
|
1263
|
+
if (attrs.title) image.title = attrs.title;
|
|
1264
|
+
return [["img", image]];
|
|
1265
|
+
}
|
|
1266
|
+
if (json.type === "comarkInline") {
|
|
1267
|
+
const component = [
|
|
1268
|
+
String(attrs.name),
|
|
1269
|
+
props,
|
|
1270
|
+
...writeInline(json.content)
|
|
1271
|
+
];
|
|
1272
|
+
return [isHtml(props) ? component : own(component, inlineComponent)];
|
|
1273
|
+
}
|
|
1274
|
+
if (json.type === "footnoteReference") return [own(["footnote", {}], footnoteReference(scalar(attrs.label) ?? ""))];
|
|
1275
|
+
if (json.type === "inlineFootnote") return [own([
|
|
1276
|
+
"footnote",
|
|
1277
|
+
{},
|
|
1278
|
+
...writeInline(json.content)
|
|
1279
|
+
], between("^[", "]"))];
|
|
1280
|
+
if (json.type === "criticComment") return [own([
|
|
1281
|
+
"span",
|
|
1282
|
+
{},
|
|
1283
|
+
...writeInline(json.content)
|
|
1284
|
+
], between("{>>", "<<}"))];
|
|
1285
|
+
return [];
|
|
1286
|
+
}
|
|
1287
|
+
function equal(a, b) {
|
|
1288
|
+
if (a === b) return true;
|
|
1289
|
+
if (!(a instanceof Object) || !(b instanceof Object) || Array.isArray(a) !== Array.isArray(b)) return false;
|
|
1290
|
+
const left = Object.entries(a).filter(([, value]) => value !== void 0);
|
|
1291
|
+
const right = new Map(Object.entries(b).filter(([, value]) => value !== void 0));
|
|
1292
|
+
return left.length === right.size && left.every(([key, value]) => right.has(key) && equal(value, right.get(key)));
|
|
1293
|
+
}
|
|
1294
|
+
/** Reads Markdown into the editor's document, as a page that parses it with `page` reads it. */
|
|
1295
|
+
async function fromMarkdown(markdown, page = {}) {
|
|
1296
|
+
const tree = await parseMarkdown(markdown, readOptionsFor(page));
|
|
1297
|
+
const reading = { lossy: false };
|
|
1298
|
+
const content = readBlocks(tree.nodes, reading);
|
|
1299
|
+
return {
|
|
1300
|
+
doc: {
|
|
1301
|
+
type: "doc",
|
|
1302
|
+
content: content.length ? content : [{ type: "paragraph" }]
|
|
1303
|
+
},
|
|
1304
|
+
frontmatter: tree.frontmatter ?? {},
|
|
1305
|
+
lossy: reading.lossy
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
/** Writes the editor's document, such as `editor.getJSON()`, back to Markdown. */
|
|
1309
|
+
async function toMarkdown(doc, options = {}) {
|
|
1310
|
+
const markdown = await renderMarkdown({
|
|
1311
|
+
nodes: writeBlocks(doc.content),
|
|
1312
|
+
frontmatter: {},
|
|
1313
|
+
meta: {}
|
|
1314
|
+
}, writeOptions);
|
|
1315
|
+
return withFrontmatter(markdown.trim() ? `${markdown.trimEnd()}\n` : "", options.frontmatter);
|
|
1316
|
+
}
|
|
1317
|
+
/** Finds the frontmatter block where Comark does, and says why a save wouldn't write it back. */
|
|
1318
|
+
function readFrontmatter(markdown) {
|
|
1319
|
+
if (!markdown.startsWith("---") || !markdown.includes("\n---")) return { kind: "none" };
|
|
1320
|
+
let read;
|
|
1321
|
+
try {
|
|
1322
|
+
read = parseFrontmatter(markdown);
|
|
1323
|
+
} catch (error) {
|
|
1324
|
+
return {
|
|
1325
|
+
kind: "lost",
|
|
1326
|
+
problem: `The frontmatter isn't valid YAML (${String(error instanceof Error && "reason" in error ? error.reason : error)}); fix it to edit the document.`,
|
|
1327
|
+
body: markdown.slice(markdown.indexOf("\n---") + 4).replace(/^(?:\r?\n)+/, "")
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
const { data, frontmatterText: yaml } = read;
|
|
1331
|
+
const value = data;
|
|
1332
|
+
if (!(value instanceof Object) || Array.isArray(value)) return {
|
|
1333
|
+
kind: "lost",
|
|
1334
|
+
problem: `The frontmatter is a YAML ${Array.isArray(value) ? "list" : "scalar"}, which a save would rewrite; turn it into \`key: value\` lines to edit the document.`
|
|
1335
|
+
};
|
|
1336
|
+
const comments = hasComment(yaml);
|
|
1337
|
+
if (!Object.keys(data).length) return {
|
|
1338
|
+
kind: "lost",
|
|
1339
|
+
problem: comments ? "The frontmatter holds only YAML comments, which a save would drop; delete them or move them into the text to edit the document." : "The frontmatter block is empty, so a save wouldn't write it back; delete its `---` lines or add a `key: value` line to edit the document."
|
|
1340
|
+
};
|
|
1341
|
+
if (comments) return {
|
|
1342
|
+
kind: "lost",
|
|
1343
|
+
problem: "The frontmatter has YAML comments, which a save would drop; delete them or move them into the text to edit the document."
|
|
1344
|
+
};
|
|
1345
|
+
if (hasProtoKey(data)) return {
|
|
1346
|
+
kind: "lost",
|
|
1347
|
+
problem: "The frontmatter has a `__proto__` key, which the room's fields would drop on save; rename it to edit the document."
|
|
1348
|
+
};
|
|
1349
|
+
return {
|
|
1350
|
+
kind: "kept",
|
|
1351
|
+
body: read.content.replace(/^(?:\r?\n)+/, ""),
|
|
1352
|
+
fields: data
|
|
1353
|
+
};
|
|
1354
|
+
}
|
|
1355
|
+
function hasProtoKey(value, seen = /* @__PURE__ */ new Set()) {
|
|
1356
|
+
if (!(value instanceof Object) || seen.has(value)) return false;
|
|
1357
|
+
seen.add(value);
|
|
1358
|
+
return Object.hasOwn(value, "__proto__") || Object.values(value).some((child) => hasProtoKey(child, seen));
|
|
1359
|
+
}
|
|
1360
|
+
/**
|
|
1361
|
+
* Whether the YAML has a comment. A `#` after a space may sit inside a quoted value, so each candidate counts only when
|
|
1362
|
+
* cutting the line there leaves the same data.
|
|
1363
|
+
*/
|
|
1364
|
+
function hasComment(yaml) {
|
|
1365
|
+
const lines = yaml.split("\n");
|
|
1366
|
+
const data = (text) => {
|
|
1367
|
+
try {
|
|
1368
|
+
return { value: parseFrontmatter(`---\n${text}\n---\n`).data };
|
|
1369
|
+
} catch {
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
};
|
|
1373
|
+
const original = data(yaml);
|
|
1374
|
+
return lines.some((line, index) => [...line.matchAll(/(?:^|\s)#/g)].some((match) => {
|
|
1375
|
+
const cut = [...lines];
|
|
1376
|
+
cut[index] = line.slice(0, match.index);
|
|
1377
|
+
const read = data(cut.join("\n"));
|
|
1378
|
+
return read !== void 0 && equal(read.value, original?.value);
|
|
1379
|
+
}));
|
|
1380
|
+
}
|
|
1381
|
+
/** Writes the frontmatter above the body. Comark's own writer leaves out the `---` lines when the body is empty. */
|
|
1382
|
+
function withFrontmatter(body, frontmatter = {}) {
|
|
1383
|
+
if (!Object.keys(frontmatter).length) return body;
|
|
1384
|
+
return `---\n${renderFrontmatter(frontmatter, "")}\n---\n${body ? `\n${body}` : ""}`;
|
|
1385
|
+
}
|
|
1386
|
+
/**
|
|
1387
|
+
* Whether the editor can open this Markdown and save it without changing what readers see. Pass how the page parses
|
|
1388
|
+
* it, so syntax only its plugins read, such as footnote definitions, counts too, and the page's tree before and after a
|
|
1389
|
+
* save must match.
|
|
1390
|
+
*/
|
|
1391
|
+
async function checkMarkdown(markdown, page = {}) {
|
|
1392
|
+
const frontmatter = readFrontmatter(markdown);
|
|
1393
|
+
if (frontmatter.kind === "lost") return {
|
|
1394
|
+
editable: false,
|
|
1395
|
+
reason: frontmatter.problem
|
|
1396
|
+
};
|
|
1397
|
+
const first = await fromMarkdown(markdown, page);
|
|
1398
|
+
if (first.lossy) return { editable: false };
|
|
1399
|
+
const written = await toMarkdown(first.doc, first);
|
|
1400
|
+
const again = await fromMarkdown(written, page);
|
|
1401
|
+
if (again.lossy || !equal(first.doc, again.doc) || !equal(first.frontmatter, again.frontmatter)) return { editable: false };
|
|
1402
|
+
if (page.plugins?.length || page.registerDefaultPlugins === false) {
|
|
1403
|
+
const [before, after] = await Promise.all([readPage(markdown, page), readPage(written, page)]);
|
|
1404
|
+
if (!equal(before, after)) return { editable: false };
|
|
1405
|
+
}
|
|
1406
|
+
return {
|
|
1407
|
+
editable: true,
|
|
1408
|
+
markdown: written
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1412
|
+
* The page's tree, with the key that plugins such as comark-tabs derive their ids from, a hash of the source, masked:
|
|
1413
|
+
* any rewrite changes it, and readers don't see it.
|
|
1414
|
+
*/
|
|
1415
|
+
async function readPage(markdown, { plugins = [], registerDefaultPlugins }) {
|
|
1416
|
+
let key;
|
|
1417
|
+
const sourceKey = defineComarkPlugin(() => ({
|
|
1418
|
+
name: "editor-source-key",
|
|
1419
|
+
post(state) {
|
|
1420
|
+
key = hash(state.markdown);
|
|
1421
|
+
}
|
|
1422
|
+
}));
|
|
1423
|
+
const tree = await parseMarkdown(markdown, {
|
|
1424
|
+
registerDefaultPlugins,
|
|
1425
|
+
plugins: [sourceKey(), ...plugins]
|
|
1426
|
+
});
|
|
1427
|
+
const read = JSON.stringify({
|
|
1428
|
+
nodes: tree.nodes,
|
|
1429
|
+
frontmatter: tree.frontmatter
|
|
1430
|
+
}, (_, value) => key && typeof value === "string" ? value.replaceAll(key, "#") : value);
|
|
1431
|
+
return JSON.parse(read);
|
|
1432
|
+
}
|
|
1433
|
+
const uploadKey = new PluginKey("comarkUpload");
|
|
1434
|
+
const acceptMedia = (file) => /^(?:image|video|audio)\//.test(file.type);
|
|
1435
|
+
/** Where placeholder `id` sits now, after every edit since it was added. */
|
|
1436
|
+
function findUpload(state, id) {
|
|
1437
|
+
return uploadKey.getState(state)?.find(void 0, void 0, (spec) => spec.id === id)[0]?.from;
|
|
1438
|
+
}
|
|
1439
|
+
/** The placeholder's styles, which the editor adds to the page with the first one. */
|
|
1440
|
+
const uploadCss = `@layer comark{${[
|
|
1441
|
+
theme(".cue-upload", "primary"),
|
|
1442
|
+
`.cue-upload{display:inline-grid;position:relative;min-width:12rem;max-width:15rem;overflow:hidden;vertical-align:middle;border:1px solid var(--_comark-border);border-radius:var(--_comark-radius);background:var(--_comark-accent);color:var(--_comark-foreground);font:500 .75rem/1.4 var(--_comark-font)}`,
|
|
1443
|
+
`.cue-upload>img,.cue-upload>video{grid-area:1/1;width:100%;max-height:10rem;object-fit:cover;opacity:.55}`,
|
|
1444
|
+
`.cue-upload-label{grid-area:1/1;align-self:end;padding:.375rem .5rem .5rem;font-variant-numeric:tabular-nums;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}`,
|
|
1445
|
+
`.cue-upload>img~.cue-upload-label,.cue-upload>video~.cue-upload-label{background:color-mix(in srgb,var(--_comark-background) 80%,transparent)}`,
|
|
1446
|
+
`.cue-upload::after{content:"";position:absolute;inset:auto 0 0;height:2px;width:var(--cue-upload);background:var(--_comark-primary);transition:width .2s ease-out}`,
|
|
1447
|
+
`@media (prefers-reduced-motion:reduce){.cue-upload::after{transition:none}}`
|
|
1448
|
+
].join("")}}`;
|
|
1449
|
+
function placeholder(pending) {
|
|
1450
|
+
addStyles("editor-upload", uploadCss);
|
|
1451
|
+
const root = document.createElement("span");
|
|
1452
|
+
root.className = "cue-upload";
|
|
1453
|
+
root.contentEditable = "false";
|
|
1454
|
+
root.dataset.kind = pending.file.type.split("/")[0];
|
|
1455
|
+
if (pending.preview && root.dataset.kind === "image") {
|
|
1456
|
+
const image = document.createElement("img");
|
|
1457
|
+
image.src = pending.preview;
|
|
1458
|
+
image.alt = "";
|
|
1459
|
+
root.append(image);
|
|
1460
|
+
} else if (pending.preview && root.dataset.kind === "video") {
|
|
1461
|
+
const video = document.createElement("video");
|
|
1462
|
+
video.src = pending.preview;
|
|
1463
|
+
video.muted = true;
|
|
1464
|
+
root.append(video);
|
|
1465
|
+
}
|
|
1466
|
+
const label = document.createElement("span");
|
|
1467
|
+
label.className = "cue-upload-label";
|
|
1468
|
+
label.setAttribute("role", "status");
|
|
1469
|
+
root.append(label);
|
|
1470
|
+
const show = (fraction) => {
|
|
1471
|
+
root.style.setProperty("--cue-upload", fraction === null ? "0%" : `${Math.round(fraction * 100)}%`);
|
|
1472
|
+
label.textContent = fraction === null ? `Uploading ${pending.file.name}` : `Uploading ${pending.file.name}, ${Math.round(fraction * 100)}%`;
|
|
1473
|
+
};
|
|
1474
|
+
show(pending.fraction);
|
|
1475
|
+
return {
|
|
1476
|
+
root,
|
|
1477
|
+
show
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
/**
|
|
1481
|
+
* Uploads `files` into the document at `pos`. Each shows a placeholder there until its URL comes back, then becomes an
|
|
1482
|
+
* image node, which comark-media plays when its URL ends in a video or audio extension. The placeholder is a
|
|
1483
|
+
* decoration, so a collaborative room never sees a file that hasn't finished.
|
|
1484
|
+
*/
|
|
1485
|
+
function startUploads(view, files, pos, options) {
|
|
1486
|
+
const { upload } = options;
|
|
1487
|
+
if (!upload) return [];
|
|
1488
|
+
const batch = Symbol();
|
|
1489
|
+
return files.flatMap((file) => options.accept(file) ? [send(view, file, pos, upload, options, batch)] : []);
|
|
1490
|
+
}
|
|
1491
|
+
function send(view, file, pos, upload, options, batch) {
|
|
1492
|
+
const pending = {
|
|
1493
|
+
id: nextId++,
|
|
1494
|
+
file,
|
|
1495
|
+
fraction: null
|
|
1496
|
+
};
|
|
1497
|
+
if (/^(?:image|video)\//.test(file.type)) pending.preview = URL.createObjectURL(file);
|
|
1498
|
+
pendingUploads.set(pending.id, pending);
|
|
1499
|
+
view.dispatch(view.state.tr.setMeta(uploadKey, { add: {
|
|
1500
|
+
id: pending.id,
|
|
1501
|
+
pos,
|
|
1502
|
+
batch
|
|
1503
|
+
} }));
|
|
1504
|
+
const finish = () => {
|
|
1505
|
+
pendingUploads.delete(pending.id);
|
|
1506
|
+
if (pending.preview) URL.revokeObjectURL(pending.preview);
|
|
1507
|
+
};
|
|
1508
|
+
return upload(file, (fraction) => {
|
|
1509
|
+
pending.fraction = Math.min(1, Math.max(0, fraction));
|
|
1510
|
+
elements.get(pending.id)?.show(pending.fraction);
|
|
1511
|
+
}).then((src) => {
|
|
1512
|
+
const at = findUpload(view.state, pending.id);
|
|
1513
|
+
const tr = view.state.tr.setMeta(uploadKey, {
|
|
1514
|
+
remove: pending.id,
|
|
1515
|
+
batch
|
|
1516
|
+
});
|
|
1517
|
+
if (at !== void 0) insertImage(tr, at, view.state.schema.nodes.image.create({
|
|
1518
|
+
src,
|
|
1519
|
+
alt: file.name
|
|
1520
|
+
}));
|
|
1521
|
+
view.dispatch(tr);
|
|
1522
|
+
finish();
|
|
1523
|
+
return src;
|
|
1524
|
+
}, (error) => {
|
|
1525
|
+
view.dispatch(view.state.tr.setMeta(uploadKey, { remove: pending.id }));
|
|
1526
|
+
finish();
|
|
1527
|
+
options.onError?.(error, file);
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
/**
|
|
1531
|
+
* Opens the file picker and uploads what's picked at the cursor, with the same placeholders as a paste. Returns false
|
|
1532
|
+
* without an `upload` callback. A "/" menu's Image item calls it through the `chooseFiles` command.
|
|
1533
|
+
*/
|
|
1534
|
+
function chooseFiles(view, options) {
|
|
1535
|
+
if (!options.upload) return false;
|
|
1536
|
+
const input = document.createElement("input");
|
|
1537
|
+
input.type = "file";
|
|
1538
|
+
input.multiple = true;
|
|
1539
|
+
input.accept = options.pick ?? "image/*,video/*,audio/*";
|
|
1540
|
+
input.style.cssText = "position:fixed;left:-9999px";
|
|
1541
|
+
input.tabIndex = -1;
|
|
1542
|
+
input.addEventListener("change", () => {
|
|
1543
|
+
const files = Array.from(input.files ?? []);
|
|
1544
|
+
input.remove();
|
|
1545
|
+
if (files.length) Promise.all(startUploads(view, files, view.state.selection.from, options));
|
|
1546
|
+
}, { once: true });
|
|
1547
|
+
input.addEventListener("cancel", () => input.remove(), { once: true });
|
|
1548
|
+
document.body.append(input);
|
|
1549
|
+
input.click();
|
|
1550
|
+
return true;
|
|
1551
|
+
}
|
|
1552
|
+
function insertImage(tr, pos, image) {
|
|
1553
|
+
if (tr.doc.resolve(pos).parent.inlineContent) tr.insert(pos, image);
|
|
1554
|
+
else tr.insert(pos, tr.doc.type.schema.nodes.paragraph.create(null, image));
|
|
1555
|
+
}
|
|
1556
|
+
let nextId = 1;
|
|
1557
|
+
const pendingUploads = /* @__PURE__ */ new Map();
|
|
1558
|
+
const elements = /* @__PURE__ */ new Map();
|
|
1559
|
+
function uploadWidget(pos, id, batch) {
|
|
1560
|
+
return Decoration.widget(pos, () => {
|
|
1561
|
+
const element = placeholder(pendingUploads.get(id) ?? {
|
|
1562
|
+
id,
|
|
1563
|
+
file: new File([], ""),
|
|
1564
|
+
fraction: null
|
|
1565
|
+
});
|
|
1566
|
+
elements.set(id, element);
|
|
1567
|
+
return element.root;
|
|
1568
|
+
}, {
|
|
1569
|
+
id,
|
|
1570
|
+
batch,
|
|
1571
|
+
key: String(id),
|
|
1572
|
+
side: -1,
|
|
1573
|
+
destroy: () => elements.delete(id)
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
/** The plugin alone, for a ProseMirror editor without Tiptap. */
|
|
1577
|
+
function uploadPlugin(options) {
|
|
1578
|
+
const filesOf = (list) => Array.from(list ?? []).filter(options.accept);
|
|
1579
|
+
return new Plugin$1({
|
|
1580
|
+
key: uploadKey,
|
|
1581
|
+
state: {
|
|
1582
|
+
init: () => DecorationSet.empty,
|
|
1583
|
+
apply(tr, set) {
|
|
1584
|
+
let next = set.map(tr.mapping, tr.doc);
|
|
1585
|
+
const meta = tr.getMeta(uploadKey);
|
|
1586
|
+
if (meta && "add" in meta) {
|
|
1587
|
+
const { id, pos, batch } = meta.add;
|
|
1588
|
+
next = next.add(tr.doc, [uploadWidget(pos, id, batch)]);
|
|
1589
|
+
} else if (meta && "remove" in meta) {
|
|
1590
|
+
if (meta.batch) for (const widget of set.find(void 0, void 0, (spec) => spec.batch === meta.batch && spec.id > meta.remove)) {
|
|
1591
|
+
next = next.remove(next.find(void 0, void 0, (spec) => spec.id === widget.spec.id));
|
|
1592
|
+
next = next.add(tr.doc, [uploadWidget(tr.mapping.map(widget.from, 1), widget.spec.id, meta.batch)]);
|
|
1593
|
+
}
|
|
1594
|
+
next = next.remove(next.find(void 0, void 0, (spec) => spec.id === meta.remove));
|
|
1595
|
+
}
|
|
1596
|
+
return next;
|
|
1597
|
+
}
|
|
1598
|
+
},
|
|
1599
|
+
props: {
|
|
1600
|
+
decorations: (state) => uploadKey.getState(state),
|
|
1601
|
+
handlePaste(view, event) {
|
|
1602
|
+
const files = filesOf(event.clipboardData?.files);
|
|
1603
|
+
if (!files.length || !options.upload) return false;
|
|
1604
|
+
Promise.all(startUploads(view, files, view.state.selection.from, options));
|
|
1605
|
+
return true;
|
|
1606
|
+
},
|
|
1607
|
+
handleDrop(view, event, _slice, moved) {
|
|
1608
|
+
const files = filesOf(event.dataTransfer?.files);
|
|
1609
|
+
if (moved || !files.length || !options.upload) return false;
|
|
1610
|
+
const pos = view.posAtCoords({
|
|
1611
|
+
left: event.clientX,
|
|
1612
|
+
top: event.clientY
|
|
1613
|
+
})?.pos ?? view.state.selection.from;
|
|
1614
|
+
Promise.all(startUploads(view, files, pos, options));
|
|
1615
|
+
return true;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
Extension.create({
|
|
1621
|
+
name: "comarkUpload",
|
|
1622
|
+
addOptions: () => ({
|
|
1623
|
+
upload: void 0,
|
|
1624
|
+
accept: acceptMedia,
|
|
1625
|
+
pick: void 0,
|
|
1626
|
+
onError: void 0
|
|
1627
|
+
}),
|
|
1628
|
+
addProseMirrorPlugins() {
|
|
1629
|
+
return [uploadPlugin(this.options)];
|
|
1630
|
+
},
|
|
1631
|
+
addCommands() {
|
|
1632
|
+
return {
|
|
1633
|
+
uploadFiles: (files) => ({ view }) => {
|
|
1634
|
+
if (!this.options.upload) return false;
|
|
1635
|
+
Promise.all(startUploads(view, files, view.state.selection.from, this.options));
|
|
1636
|
+
return true;
|
|
1637
|
+
},
|
|
1638
|
+
chooseFiles: () => ({ view, dispatch }) => dispatch ? chooseFiles(view, this.options) : Boolean(this.options.upload)
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
});
|
|
1642
|
+
//#endregion
|
|
1643
|
+
//#region ../../node_modules/.pnpm/comark-ui@0.12.0_@comark+html@0.9.0_katex@0.19.1_shiki@4.5.0__@tiptap+core@3.31.4_@tipt_529d07a13f99d0823ee3fccd15d28268/node_modules/comark-ui/dist/editor-vue.mjs
|
|
1644
|
+
function readProp(props, name, type) {
|
|
1645
|
+
if (Object.hasOwn(props, name)) {
|
|
1646
|
+
const value = props[name];
|
|
1647
|
+
if (type === "boolean") return value !== "false";
|
|
1648
|
+
if (type === "number") return value === "" ? void 0 : Number(value);
|
|
1649
|
+
return value;
|
|
1650
|
+
}
|
|
1651
|
+
const bound = props[`:${name}`];
|
|
1652
|
+
if (bound === void 0) return void 0;
|
|
1653
|
+
const parsed = isString(bound) ? parse(bound) : bound;
|
|
1654
|
+
if (type === "boolean") return parsed === true || parsed === "true";
|
|
1655
|
+
if (type === "number") return Number(parsed);
|
|
1656
|
+
if (type === "json") return parsed;
|
|
1657
|
+
return isString(parsed) ? parsed : JSON.stringify(parsed);
|
|
1658
|
+
}
|
|
1659
|
+
const isString = (value) => String(value) === value;
|
|
1660
|
+
function parse(text) {
|
|
1661
|
+
try {
|
|
1662
|
+
return JSON.parse(text);
|
|
1663
|
+
} catch {
|
|
1664
|
+
return text;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
function writeProp(props, name, value) {
|
|
1668
|
+
const key = value === void 0 || isString(value) ? name : `:${name}`;
|
|
1669
|
+
const next = {};
|
|
1670
|
+
let placed = value === void 0;
|
|
1671
|
+
for (const [current, old] of Object.entries(props)) if (current !== name && current !== `:${name}`) next[current] = old;
|
|
1672
|
+
else if (!placed) {
|
|
1673
|
+
next[key] = isString(value) ? value : JSON.stringify(value);
|
|
1674
|
+
placed = true;
|
|
1675
|
+
}
|
|
1676
|
+
if (!placed) next[key] = isString(value) ? value : JSON.stringify(value);
|
|
1677
|
+
return Object.keys(next).length ? next : void 0;
|
|
1678
|
+
}
|
|
1679
|
+
const valueOf = (event) => event.target instanceof HTMLInputElement || event.target instanceof HTMLSelectElement ? event.target.value : "";
|
|
1680
|
+
const shown = (value) => isString(value) ? value : JSON.stringify(value);
|
|
1681
|
+
const CHROME = "data-cev-chrome";
|
|
1682
|
+
const stopEvent = ({ event }) => event.target instanceof Element && event.target.closest(`[${CHROME}]`) !== null;
|
|
1683
|
+
let count = 0;
|
|
1684
|
+
/** Places an open panel under its button, or above it when there's no room below, inside the viewport. */
|
|
1685
|
+
function place(panel, anchor) {
|
|
1686
|
+
const box = anchor.getBoundingClientRect();
|
|
1687
|
+
const { offsetWidth: width, offsetHeight: height } = panel;
|
|
1688
|
+
const below = box.bottom + 4 + height <= innerHeight || box.top < height + 4;
|
|
1689
|
+
panel.style.top = `${below ? box.bottom + 4 : box.top - height - 4}px`;
|
|
1690
|
+
panel.style.left = `${Math.max(8, Math.min(box.left, innerWidth - width - 8))}px`;
|
|
1691
|
+
}
|
|
1692
|
+
function icon(info) {
|
|
1693
|
+
if (!info?.icon) return null;
|
|
1694
|
+
return isString(info.icon) ? h("span", {
|
|
1695
|
+
class: ["cev-icon", info.icon],
|
|
1696
|
+
"aria-hidden": "true"
|
|
1697
|
+
}) : h(info.icon, {
|
|
1698
|
+
class: "cev-icon",
|
|
1699
|
+
"aria-hidden": "true"
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
const ComponentView = defineComponent({
|
|
1703
|
+
name: "ComarkComponentView",
|
|
1704
|
+
props: {
|
|
1705
|
+
...nodeViewProps,
|
|
1706
|
+
options: {
|
|
1707
|
+
type: Object,
|
|
1708
|
+
required: true
|
|
1709
|
+
}
|
|
1710
|
+
},
|
|
1711
|
+
setup(props) {
|
|
1712
|
+
const id = `cev-${++count}`;
|
|
1713
|
+
const button = ref();
|
|
1714
|
+
const panel = ref();
|
|
1715
|
+
const editable = ref(props.editor.isEditable);
|
|
1716
|
+
const adding = ref({
|
|
1717
|
+
key: "",
|
|
1718
|
+
value: ""
|
|
1719
|
+
});
|
|
1720
|
+
const drafts = ref(/* @__PURE__ */ new Map());
|
|
1721
|
+
const reposition = () => {
|
|
1722
|
+
if (panel.value && button.value && panel.value.matches(":popover-open")) place(panel.value, button.value);
|
|
1723
|
+
};
|
|
1724
|
+
function toggle() {
|
|
1725
|
+
if (!panel.value?.matches(":popover-open")) return;
|
|
1726
|
+
editable.value = props.editor.isEditable;
|
|
1727
|
+
reposition();
|
|
1728
|
+
panel.value?.querySelector("input,select,textarea")?.focus();
|
|
1729
|
+
}
|
|
1730
|
+
onMounted(() => {
|
|
1731
|
+
addStyles("editor-component-view", componentViewCss);
|
|
1732
|
+
addEventListener("scroll", reposition, true);
|
|
1733
|
+
addEventListener("resize", reposition);
|
|
1734
|
+
});
|
|
1735
|
+
onBeforeUnmount(() => {
|
|
1736
|
+
removeEventListener("scroll", reposition, true);
|
|
1737
|
+
removeEventListener("resize", reposition);
|
|
1738
|
+
});
|
|
1739
|
+
const setProps = (update) => {
|
|
1740
|
+
if (props.editor.isEditable) props.updateAttributes({ props: update(props.node.attrs.props ?? {}) ?? null });
|
|
1741
|
+
};
|
|
1742
|
+
return () => {
|
|
1743
|
+
const { node, options } = props;
|
|
1744
|
+
const name = String(node.attrs.name);
|
|
1745
|
+
const current = node.attrs.props ?? {};
|
|
1746
|
+
const inline = node.isInline;
|
|
1747
|
+
const html = current.$ instanceof Object;
|
|
1748
|
+
const info = options.components?.[name];
|
|
1749
|
+
const schema = info?.props ?? {};
|
|
1750
|
+
const typed = Object.keys(schema);
|
|
1751
|
+
const rest = Object.keys(current).filter((key) => key !== "$" && !typed.includes(key.replace(/^:/, "")));
|
|
1752
|
+
const can = editable.value;
|
|
1753
|
+
function typedField(prop) {
|
|
1754
|
+
const spec = schema[prop];
|
|
1755
|
+
const type = spec.type ?? "string";
|
|
1756
|
+
const value = readProp(current, prop, type);
|
|
1757
|
+
const fieldId = `${id}-${prop}`;
|
|
1758
|
+
const update = (next) => setProps((current) => writeProp(current, prop, next));
|
|
1759
|
+
const custom = options.field?.({
|
|
1760
|
+
component: name,
|
|
1761
|
+
name: prop,
|
|
1762
|
+
prop: spec,
|
|
1763
|
+
value,
|
|
1764
|
+
update,
|
|
1765
|
+
editable: can,
|
|
1766
|
+
id: fieldId
|
|
1767
|
+
});
|
|
1768
|
+
const placeholder = spec.default === void 0 ? void 0 : shown(spec.default);
|
|
1769
|
+
let control;
|
|
1770
|
+
if (custom !== void 0) control = custom;
|
|
1771
|
+
else if (type === "boolean") control = h("input", {
|
|
1772
|
+
id: fieldId,
|
|
1773
|
+
type: "checkbox",
|
|
1774
|
+
checked: value ?? spec.default === true,
|
|
1775
|
+
disabled: !can,
|
|
1776
|
+
onChange: (event) => {
|
|
1777
|
+
const checked = event.target instanceof HTMLInputElement && event.target.checked;
|
|
1778
|
+
update(checked || spec.default === true ? checked : void 0);
|
|
1779
|
+
}
|
|
1780
|
+
});
|
|
1781
|
+
else if (spec.options) control = h("select", {
|
|
1782
|
+
id: fieldId,
|
|
1783
|
+
value: value ?? "",
|
|
1784
|
+
disabled: !can,
|
|
1785
|
+
onChange: (event) => update(valueOf(event) || void 0)
|
|
1786
|
+
}, [h("option", { value: "" }, placeholder ? `Default (${placeholder})` : "—"), ...spec.options.map((option) => h("option", { value: option }, option))]);
|
|
1787
|
+
else if (type === "json") control = h("textarea", {
|
|
1788
|
+
id: fieldId,
|
|
1789
|
+
class: "cev-code",
|
|
1790
|
+
rows: 3,
|
|
1791
|
+
value: drafts.value.get(prop) ?? (value === void 0 ? "" : JSON.stringify(value)),
|
|
1792
|
+
placeholder,
|
|
1793
|
+
disabled: !can,
|
|
1794
|
+
spellcheck: false,
|
|
1795
|
+
onInput: (event) => {
|
|
1796
|
+
if (event.target instanceof HTMLTextAreaElement) drafts.value.set(prop, event.target.value);
|
|
1797
|
+
},
|
|
1798
|
+
onChange: (event) => {
|
|
1799
|
+
const target = event.target;
|
|
1800
|
+
if (!(target instanceof HTMLTextAreaElement)) return;
|
|
1801
|
+
try {
|
|
1802
|
+
update(target.value.trim() ? JSON.parse(target.value) : void 0);
|
|
1803
|
+
drafts.value.delete(prop);
|
|
1804
|
+
target.removeAttribute("aria-invalid");
|
|
1805
|
+
} catch {
|
|
1806
|
+
target.setAttribute("aria-invalid", "true");
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
});
|
|
1810
|
+
else control = h("input", {
|
|
1811
|
+
id: fieldId,
|
|
1812
|
+
type: type === "number" ? "number" : "text",
|
|
1813
|
+
value: value ?? "",
|
|
1814
|
+
placeholder,
|
|
1815
|
+
disabled: !can,
|
|
1816
|
+
onInput: (event) => {
|
|
1817
|
+
const text = valueOf(event);
|
|
1818
|
+
update(text === "" ? void 0 : type === "number" ? Number(text) : text);
|
|
1819
|
+
}
|
|
1820
|
+
});
|
|
1821
|
+
const label = h("label", { for: fieldId }, spec.label ?? prop);
|
|
1822
|
+
const check = custom === void 0 && type === "boolean";
|
|
1823
|
+
return h("div", { class: check ? "cev-field cev-check" : "cev-field" }, [
|
|
1824
|
+
check ? control : label,
|
|
1825
|
+
check ? label : control,
|
|
1826
|
+
spec.description ? h("small", spec.description) : null
|
|
1827
|
+
]);
|
|
1828
|
+
}
|
|
1829
|
+
function rawField(key) {
|
|
1830
|
+
const fieldId = `${id}-raw-${key}`;
|
|
1831
|
+
return h("div", { class: "cev-field cev-raw" }, [
|
|
1832
|
+
h("label", {
|
|
1833
|
+
for: fieldId,
|
|
1834
|
+
class: "cev-code"
|
|
1835
|
+
}, key),
|
|
1836
|
+
h("input", {
|
|
1837
|
+
id: fieldId,
|
|
1838
|
+
value: shown(current[key]),
|
|
1839
|
+
disabled: !can,
|
|
1840
|
+
onInput: (event) => setProps((current) => ({
|
|
1841
|
+
...current,
|
|
1842
|
+
[key]: valueOf(event)
|
|
1843
|
+
}))
|
|
1844
|
+
}),
|
|
1845
|
+
can ? h("button", {
|
|
1846
|
+
type: "button",
|
|
1847
|
+
class: "cev-remove",
|
|
1848
|
+
"aria-label": `Remove ${key}`,
|
|
1849
|
+
onClick: () => setProps((current) => writeProp(current, key.replace(/^:/, ""), void 0))
|
|
1850
|
+
}, "×") : null
|
|
1851
|
+
]);
|
|
1852
|
+
}
|
|
1853
|
+
const add = can ? h("form", {
|
|
1854
|
+
class: "cev-add",
|
|
1855
|
+
onSubmit: (event) => {
|
|
1856
|
+
event.preventDefault();
|
|
1857
|
+
const key = adding.value.key.trim();
|
|
1858
|
+
if (!/^:?[\w-]+$/.test(key) || key.replace(/^:/, "") === "$") return;
|
|
1859
|
+
const value = adding.value.value;
|
|
1860
|
+
setProps((current) => {
|
|
1861
|
+
return {
|
|
1862
|
+
...writeProp(current, key.replace(/^:/, ""), void 0) ?? {},
|
|
1863
|
+
[key]: value
|
|
1864
|
+
};
|
|
1865
|
+
});
|
|
1866
|
+
adding.value = {
|
|
1867
|
+
key: "",
|
|
1868
|
+
value: ""
|
|
1869
|
+
};
|
|
1870
|
+
}
|
|
1871
|
+
}, [
|
|
1872
|
+
h("input", {
|
|
1873
|
+
class: "cev-code",
|
|
1874
|
+
placeholder: "prop",
|
|
1875
|
+
"aria-label": "New prop's name",
|
|
1876
|
+
value: adding.value.key,
|
|
1877
|
+
onInput: (event) => adding.value.key = valueOf(event)
|
|
1878
|
+
}),
|
|
1879
|
+
h("input", {
|
|
1880
|
+
placeholder: "value",
|
|
1881
|
+
"aria-label": "New prop's value",
|
|
1882
|
+
value: adding.value.value,
|
|
1883
|
+
onInput: (event) => adding.value.value = valueOf(event)
|
|
1884
|
+
}),
|
|
1885
|
+
h("button", { type: "submit" }, "Add")
|
|
1886
|
+
]) : null;
|
|
1887
|
+
const tag = `${inline ? ":" : "::"}${name}`;
|
|
1888
|
+
const title = html ? `<${name}>` : info?.label ?? name;
|
|
1889
|
+
const content = h("div", {
|
|
1890
|
+
ref: panel,
|
|
1891
|
+
id,
|
|
1892
|
+
class: "cev-panel",
|
|
1893
|
+
popover: "auto",
|
|
1894
|
+
[CHROME]: "",
|
|
1895
|
+
contenteditable: "false",
|
|
1896
|
+
onToggle: toggle
|
|
1897
|
+
}, [
|
|
1898
|
+
h("p", { class: "cev-panel-title" }, [title, info?.label ? h("span", { class: "cev-code" }, tag) : null]),
|
|
1899
|
+
...typed.map(typedField),
|
|
1900
|
+
...rest.map(rawField),
|
|
1901
|
+
typed.length || rest.length || can ? null : h("p", { class: "cev-empty" }, "No props."),
|
|
1902
|
+
add
|
|
1903
|
+
]);
|
|
1904
|
+
const summary = [...typed.flatMap((prop) => {
|
|
1905
|
+
const value = readProp(current, prop, schema[prop].type ?? "string");
|
|
1906
|
+
return value === void 0 ? [] : [`${prop}=${shown(value)}`];
|
|
1907
|
+
}), ...rest.map((key) => `${key}=${shown(current[key])}`)].join(" ");
|
|
1908
|
+
if (inline) return h(NodeViewWrapper, {
|
|
1909
|
+
as: "span",
|
|
1910
|
+
class: "cev-inline",
|
|
1911
|
+
"data-comark-inline": name
|
|
1912
|
+
}, () => [
|
|
1913
|
+
h(NodeViewContent, {
|
|
1914
|
+
as: "span",
|
|
1915
|
+
class: "cev-text"
|
|
1916
|
+
}),
|
|
1917
|
+
h("button", {
|
|
1918
|
+
ref: button,
|
|
1919
|
+
type: "button",
|
|
1920
|
+
class: "cev-tag",
|
|
1921
|
+
popovertarget: id,
|
|
1922
|
+
title: summary || void 0,
|
|
1923
|
+
contenteditable: "false",
|
|
1924
|
+
[CHROME]: ""
|
|
1925
|
+
}, [
|
|
1926
|
+
icon(info),
|
|
1927
|
+
h("span", { class: "cev-visually-hidden" }, "Props of "),
|
|
1928
|
+
tag
|
|
1929
|
+
]),
|
|
1930
|
+
content
|
|
1931
|
+
]);
|
|
1932
|
+
return h(NodeViewWrapper, {
|
|
1933
|
+
as: "div",
|
|
1934
|
+
class: "cev-block",
|
|
1935
|
+
"data-comark-block": name
|
|
1936
|
+
}, () => [h("div", {
|
|
1937
|
+
class: "cev-bar",
|
|
1938
|
+
contenteditable: "false",
|
|
1939
|
+
[CHROME]: ""
|
|
1940
|
+
}, [
|
|
1941
|
+
icon(info),
|
|
1942
|
+
h("span", { class: "cev-name" }, title),
|
|
1943
|
+
info?.label ? h("span", { class: "cev-code cev-tag-name" }, tag) : null,
|
|
1944
|
+
html ? null : h("button", {
|
|
1945
|
+
ref: button,
|
|
1946
|
+
type: "button",
|
|
1947
|
+
class: "cev-props",
|
|
1948
|
+
popovertarget: id
|
|
1949
|
+
}, [h("span", { class: "cev-visually-hidden" }, `Props of ${title}: `), summary || (can ? "Add props" : "No props")]),
|
|
1950
|
+
html ? null : content
|
|
1951
|
+
]), h(NodeViewContent, {
|
|
1952
|
+
as: "div",
|
|
1953
|
+
class: "cev-body"
|
|
1954
|
+
})]);
|
|
1955
|
+
};
|
|
1956
|
+
}
|
|
1957
|
+
});
|
|
1958
|
+
/**
|
|
1959
|
+
* The node view `ComarkKit`'s `componentView` option takes: a block component shows its name and props above its
|
|
1960
|
+
* editable content and slots, and an inline one a tag after its text. Each opens a panel that edits the props through
|
|
1961
|
+
* a transaction on the node's `props` attribute, so the schema and the Markdown the codec writes stay as they are.
|
|
1962
|
+
*/
|
|
1963
|
+
function componentView(options = {}) {
|
|
1964
|
+
const component = defineComponent({
|
|
1965
|
+
props: nodeViewProps,
|
|
1966
|
+
setup: (props) => () => h(ComponentView, {
|
|
1967
|
+
...props,
|
|
1968
|
+
options
|
|
1969
|
+
})
|
|
1970
|
+
});
|
|
1971
|
+
return VueNodeViewRenderer(component, { stopEvent });
|
|
1972
|
+
}
|
|
1973
|
+
/** The view's styles, which it adds to the page with the first component. */
|
|
1974
|
+
const componentViewCss = `@layer comark{${[
|
|
1975
|
+
theme(".cev-block,.cev-inline,.cev-panel", "shadow", "status"),
|
|
1976
|
+
`.cev-block{margin:.75rem 0;border:1px solid var(--_comark-border);border-radius:var(--_comark-radius)}`,
|
|
1977
|
+
`.cev-bar{display:flex;align-items:center;gap:.5rem;min-height:2rem;padding:0 .5rem 0 .75rem;border-bottom:1px solid var(--_comark-border);color:var(--_comark-muted);font:500 .75rem/1.4 var(--_comark-font);user-select:none}`,
|
|
1978
|
+
`.cev-name{color:var(--_comark-foreground)}`,
|
|
1979
|
+
`.cev-code{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.6875rem}`,
|
|
1980
|
+
`.cev-icon{display:inline-block;width:1em;height:1em;flex:none}`,
|
|
1981
|
+
`.cev-props{min-width:0;overflow:hidden;padding:.125rem .375rem;border:0;border-radius:calc(var(--_comark-radius)*.75);background:none;color:inherit;font:inherit;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.6875rem;text-align:start;text-overflow:ellipsis;white-space:nowrap;cursor:pointer}`,
|
|
1982
|
+
`.cev-props:hover,.cev-tag:hover{background:var(--_comark-accent);color:var(--_comark-foreground)}`,
|
|
1983
|
+
`.cev-props:focus-visible,.cev-tag:focus-visible,.cev-panel :focus-visible{outline:2px solid var(--_comark-ring);outline-offset:1px}`,
|
|
1984
|
+
`.cev-body{padding:.25rem .75rem}`,
|
|
1985
|
+
`.cev-block [data-comark-slot]{margin:.5rem -.75rem 0;padding:0 .75rem;border-top:1px dashed var(--_comark-border)}`,
|
|
1986
|
+
`.cev-block [data-comark-slot]::before{content:"#" attr(data-comark-slot);display:block;padding-top:.25rem;color:var(--_comark-muted);font:500 .6875rem/1.4 ui-monospace,SFMono-Regular,Menlo,monospace;user-select:none}`,
|
|
1987
|
+
`.cev-inline{padding:0 .125rem 0 .25rem;border:1px solid var(--_comark-border);border-radius:calc(var(--_comark-radius)*.75);background:var(--_comark-accent)}`,
|
|
1988
|
+
`.cev-tag{display:inline-flex;align-items:center;gap:.25rem;margin-inline-start:.25rem;padding:0 .25rem;border:0;border-radius:calc(var(--_comark-radius)*.5);background:none;color:var(--_comark-muted);font:500 max(.75em,.6875rem)/1.4 ui-monospace,SFMono-Regular,Menlo,monospace;vertical-align:baseline;cursor:pointer;user-select:none}`,
|
|
1989
|
+
`.cev-panel{position:fixed;inset:auto;margin:0;width:18rem;max-height:min(24rem,80vh);overflow:auto;padding:.75rem;border:1px solid var(--_comark-border);border-radius:var(--_comark-radius);background:var(--_comark-background);color:var(--_comark-foreground);box-shadow:var(--_comark-shadow);font:400 .8125rem/1.4 var(--_comark-font);white-space:normal}`,
|
|
1990
|
+
`.cev-panel:popover-open{display:grid;gap:.625rem}`,
|
|
1991
|
+
`.cev-panel-title{display:flex;align-items:baseline;justify-content:space-between;gap:.5rem;margin:0;font-weight:600}`,
|
|
1992
|
+
`.cev-panel-title .cev-code,.cev-tag-name{color:var(--_comark-muted);font-weight:500}`,
|
|
1993
|
+
`.cev-field{display:grid;gap:.25rem}`,
|
|
1994
|
+
`.cev-field>label{color:var(--_comark-muted);font-size:.75rem}`,
|
|
1995
|
+
`.cev-field>small{color:var(--_comark-muted);font-size:.6875rem}`,
|
|
1996
|
+
`.cev-check{grid-template-columns:auto minmax(0,1fr);align-items:center;column-gap:.5rem}`,
|
|
1997
|
+
`.cev-check>small{grid-column:1/-1}`,
|
|
1998
|
+
`.cev-raw{grid-template-columns:minmax(0,1fr) auto}`,
|
|
1999
|
+
`.cev-raw>label{grid-column:1/-1}`,
|
|
2000
|
+
`.cev-panel input:not([type=checkbox]),.cev-panel select,.cev-panel textarea{width:100%;min-width:0;padding:.25rem .5rem;border:1px solid var(--_comark-border);border-radius:calc(var(--_comark-radius)*.75);background:var(--_comark-background);color:inherit;font:inherit}`,
|
|
2001
|
+
`.cev-panel input[type=checkbox]{justify-self:start;accent-color:var(--_comark-foreground)}`,
|
|
2002
|
+
`.cev-panel textarea{resize:vertical}`,
|
|
2003
|
+
`.cev-panel [aria-invalid=true]{border-color:var(--_comark-danger)}`,
|
|
2004
|
+
`.cev-panel button{padding:.25rem .5rem;border:1px solid var(--_comark-border);border-radius:calc(var(--_comark-radius)*.75);background:var(--_comark-accent);color:inherit;font:inherit;cursor:pointer}`,
|
|
2005
|
+
`.cev-add{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr) auto;gap:.25rem;padding-top:.625rem;border-top:1px solid var(--_comark-border)}`,
|
|
2006
|
+
`.cev-empty{margin:0;color:var(--_comark-muted)}`,
|
|
2007
|
+
`.cev-visually-hidden{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}`
|
|
2008
|
+
].join("")}}`;
|
|
2009
|
+
/**
|
|
2010
|
+
* A Tiptap editor bound to a Markdown string: an edit writes the Markdown back, and a change from outside reloads the
|
|
2011
|
+
* editor. A document the editor can't save unchanged (see `checkMarkdown`) opens read-only, with `editable` false.
|
|
2012
|
+
*/
|
|
2013
|
+
function useComarkEditor(markdown, options = {}) {
|
|
2014
|
+
const editor = shallowRef();
|
|
2015
|
+
const editable = ref(false);
|
|
2016
|
+
/** The document's frontmatter, which the editor keeps beside the text and writes back on each save. */
|
|
2017
|
+
const frontmatter = shallowRef({});
|
|
2018
|
+
let written;
|
|
2019
|
+
let loads = 0;
|
|
2020
|
+
let saves = 0;
|
|
2021
|
+
async function save() {
|
|
2022
|
+
const current = editor.value;
|
|
2023
|
+
if (!current) return;
|
|
2024
|
+
const run = ++saves;
|
|
2025
|
+
const next = await toMarkdown(current.getJSON(), { frontmatter: frontmatter.value });
|
|
2026
|
+
if (run !== saves) return;
|
|
2027
|
+
written = next;
|
|
2028
|
+
markdown.value = next;
|
|
2029
|
+
}
|
|
2030
|
+
async function load(source) {
|
|
2031
|
+
const run = ++loads;
|
|
2032
|
+
++saves;
|
|
2033
|
+
const page = {
|
|
2034
|
+
plugins: options.plugins,
|
|
2035
|
+
registerDefaultPlugins: options.registerDefaultPlugins
|
|
2036
|
+
};
|
|
2037
|
+
const [check, read] = await Promise.all([checkMarkdown(source, page), fromMarkdown(source, page)]);
|
|
2038
|
+
if (run !== loads) return;
|
|
2039
|
+
frontmatter.value = read.frontmatter;
|
|
2040
|
+
editable.value = check.editable;
|
|
2041
|
+
if (editor.value) {
|
|
2042
|
+
editor.value.commands.setContent(read.doc, { emitUpdate: false });
|
|
2043
|
+
editor.value.setEditable(check.editable, false);
|
|
2044
|
+
return;
|
|
2045
|
+
}
|
|
2046
|
+
const { components = {}, field } = options;
|
|
2047
|
+
const view = components === false ? void 0 : componentView({
|
|
2048
|
+
components,
|
|
2049
|
+
field
|
|
2050
|
+
});
|
|
2051
|
+
editor.value = new Editor({
|
|
2052
|
+
extensions: [ComarkKit.configure({
|
|
2053
|
+
componentView: view,
|
|
2054
|
+
...options.kit
|
|
2055
|
+
}), ...options.extensions ?? []],
|
|
2056
|
+
content: read.doc,
|
|
2057
|
+
editable: check.editable,
|
|
2058
|
+
onUpdate: () => void save()
|
|
2059
|
+
});
|
|
2060
|
+
}
|
|
2061
|
+
watch(markdown, (value) => {
|
|
2062
|
+
if (value !== written) load(value);
|
|
2063
|
+
}, { immediate: true });
|
|
2064
|
+
onScopeDispose(() => editor.value?.destroy());
|
|
2065
|
+
return {
|
|
2066
|
+
editor,
|
|
2067
|
+
editable,
|
|
2068
|
+
frontmatter
|
|
2069
|
+
};
|
|
2070
|
+
}
|
|
2071
|
+
/** A comark node as a Vue node, for the few elements the properties rows hold. */
|
|
2072
|
+
function vnode(node) {
|
|
2073
|
+
if (!isElement$2(node)) return Array.isArray(node) ? null : node;
|
|
2074
|
+
return h(node[0], node[1], childrenOf$1(node).map(vnode));
|
|
2075
|
+
}
|
|
2076
|
+
const renders = (nodes) => Array.isArray(nodes) && nodes.some((node) => !isVNode(node) || node.type !== Comment && (node.type !== Fragment || renders(node.children)));
|
|
2077
|
+
defineComponent({
|
|
2078
|
+
name: "ComarkEditor",
|
|
2079
|
+
inheritAttrs: false,
|
|
2080
|
+
props: {
|
|
2081
|
+
properties: {
|
|
2082
|
+
type: [Object, Boolean],
|
|
2083
|
+
default: true
|
|
2084
|
+
},
|
|
2085
|
+
modelValue: {
|
|
2086
|
+
type: String,
|
|
2087
|
+
required: true
|
|
2088
|
+
},
|
|
2089
|
+
extensions: {
|
|
2090
|
+
type: Array,
|
|
2091
|
+
default: void 0
|
|
2092
|
+
},
|
|
2093
|
+
plugins: {
|
|
2094
|
+
type: Array,
|
|
2095
|
+
default: void 0
|
|
2096
|
+
},
|
|
2097
|
+
components: {
|
|
2098
|
+
type: [Object, Boolean],
|
|
2099
|
+
default: void 0
|
|
2100
|
+
},
|
|
2101
|
+
registerDefaultPlugins: {
|
|
2102
|
+
type: Boolean,
|
|
2103
|
+
default: void 0
|
|
2104
|
+
}
|
|
2105
|
+
},
|
|
2106
|
+
emits: ["update:modelValue"],
|
|
2107
|
+
slots: Object,
|
|
2108
|
+
setup(props, { attrs, emit, slots }) {
|
|
2109
|
+
const markdown = ref(props.modelValue);
|
|
2110
|
+
watch(() => props.modelValue, (value) => markdown.value = value);
|
|
2111
|
+
watch(markdown, (value) => {
|
|
2112
|
+
if (value !== props.modelValue) emit("update:modelValue", value);
|
|
2113
|
+
});
|
|
2114
|
+
const { editor, frontmatter } = useComarkEditor(markdown, {
|
|
2115
|
+
extensions: props.extensions,
|
|
2116
|
+
plugins: props.plugins,
|
|
2117
|
+
components: props.components,
|
|
2118
|
+
field: (field) => {
|
|
2119
|
+
const nodes = slots.field?.(field);
|
|
2120
|
+
return renders(nodes) ? nodes : void 0;
|
|
2121
|
+
},
|
|
2122
|
+
registerDefaultPlugins: props.registerDefaultPlugins
|
|
2123
|
+
});
|
|
2124
|
+
return () => {
|
|
2125
|
+
if (!editor.value) return null;
|
|
2126
|
+
const content = h(EditorContent, {
|
|
2127
|
+
...attrs,
|
|
2128
|
+
editor: editor.value
|
|
2129
|
+
});
|
|
2130
|
+
if (props.properties === false) return content;
|
|
2131
|
+
const options = props.properties === true ? {} : props.properties;
|
|
2132
|
+
const { rows } = rowsOf(frontmatter.value, options);
|
|
2133
|
+
return rows.length ? [h(Properties, null, () => rows.map(vnode)), content] : content;
|
|
2134
|
+
};
|
|
2135
|
+
}
|
|
2136
|
+
});
|
|
2137
|
+
//#endregion
|
|
2138
|
+
//#region src/components/markdown-editor.ts
|
|
2139
|
+
/**
|
|
2140
|
+
* Uploads to a route that returns the result of `blob.handleUpload()`, with progress, and resolves with the stored
|
|
2141
|
+
* object's URL. The Blob store has to be served (`blob.serve`), so the object has a URL to link.
|
|
2142
|
+
*/
|
|
2143
|
+
function blobUpload(route) {
|
|
2144
|
+
return async (file, progress) => {
|
|
2145
|
+
const [object] = await uploadFiles(route, [file], { onProgress: (percent) => progress(percent / 100) });
|
|
2146
|
+
if (!object?.url) throw uiErrorDiagnostics.UI_MARKDOWN_UPLOAD_URL_MISSING({
|
|
2147
|
+
route,
|
|
2148
|
+
file: file.name
|
|
2149
|
+
});
|
|
2150
|
+
return object.url;
|
|
2151
|
+
};
|
|
2152
|
+
}
|
|
2153
|
+
const toolbar = [
|
|
2154
|
+
[
|
|
2155
|
+
{
|
|
2156
|
+
kind: "mark",
|
|
2157
|
+
mark: "bold",
|
|
2158
|
+
icon: "i-lucide-bold",
|
|
2159
|
+
"aria-label": "Bold"
|
|
2160
|
+
},
|
|
2161
|
+
{
|
|
2162
|
+
kind: "mark",
|
|
2163
|
+
mark: "italic",
|
|
2164
|
+
icon: "i-lucide-italic",
|
|
2165
|
+
"aria-label": "Italic"
|
|
2166
|
+
},
|
|
2167
|
+
{
|
|
2168
|
+
kind: "mark",
|
|
2169
|
+
mark: "strike",
|
|
2170
|
+
icon: "i-lucide-strikethrough",
|
|
2171
|
+
"aria-label": "Strikethrough"
|
|
2172
|
+
},
|
|
2173
|
+
{
|
|
2174
|
+
kind: "mark",
|
|
2175
|
+
mark: "code",
|
|
2176
|
+
icon: "i-lucide-code",
|
|
2177
|
+
"aria-label": "Code"
|
|
2178
|
+
}
|
|
2179
|
+
],
|
|
2180
|
+
[
|
|
2181
|
+
{
|
|
2182
|
+
kind: "heading",
|
|
2183
|
+
level: 1,
|
|
2184
|
+
icon: "i-lucide-heading-1",
|
|
2185
|
+
"aria-label": "Heading 1"
|
|
2186
|
+
},
|
|
2187
|
+
{
|
|
2188
|
+
kind: "heading",
|
|
2189
|
+
level: 2,
|
|
2190
|
+
icon: "i-lucide-heading-2",
|
|
2191
|
+
"aria-label": "Heading 2"
|
|
2192
|
+
},
|
|
2193
|
+
{
|
|
2194
|
+
kind: "heading",
|
|
2195
|
+
level: 3,
|
|
2196
|
+
icon: "i-lucide-heading-3",
|
|
2197
|
+
"aria-label": "Heading 3"
|
|
2198
|
+
}
|
|
2199
|
+
],
|
|
2200
|
+
[
|
|
2201
|
+
{
|
|
2202
|
+
kind: "bulletList",
|
|
2203
|
+
icon: "i-lucide-list",
|
|
2204
|
+
"aria-label": "Bulleted list"
|
|
2205
|
+
},
|
|
2206
|
+
{
|
|
2207
|
+
kind: "orderedList",
|
|
2208
|
+
icon: "i-lucide-list-ordered",
|
|
2209
|
+
"aria-label": "Numbered list"
|
|
2210
|
+
},
|
|
2211
|
+
{
|
|
2212
|
+
kind: "blockquote",
|
|
2213
|
+
icon: "i-lucide-quote",
|
|
2214
|
+
"aria-label": "Quote"
|
|
2215
|
+
}
|
|
2216
|
+
]
|
|
2217
|
+
];
|
|
2218
|
+
const blocks = [
|
|
2219
|
+
{
|
|
2220
|
+
type: "label",
|
|
2221
|
+
label: "Blocks"
|
|
2222
|
+
},
|
|
2223
|
+
{
|
|
2224
|
+
kind: "paragraph",
|
|
2225
|
+
label: "Text",
|
|
2226
|
+
icon: "i-lucide-type"
|
|
2227
|
+
},
|
|
2228
|
+
{
|
|
2229
|
+
kind: "heading",
|
|
2230
|
+
level: 1,
|
|
2231
|
+
label: "Heading 1",
|
|
2232
|
+
icon: "i-lucide-heading-1"
|
|
2233
|
+
},
|
|
2234
|
+
{
|
|
2235
|
+
kind: "heading",
|
|
2236
|
+
level: 2,
|
|
2237
|
+
label: "Heading 2",
|
|
2238
|
+
icon: "i-lucide-heading-2"
|
|
2239
|
+
},
|
|
2240
|
+
{
|
|
2241
|
+
kind: "heading",
|
|
2242
|
+
level: 3,
|
|
2243
|
+
label: "Heading 3",
|
|
2244
|
+
icon: "i-lucide-heading-3"
|
|
2245
|
+
},
|
|
2246
|
+
{
|
|
2247
|
+
kind: "bulletList",
|
|
2248
|
+
label: "Bulleted list",
|
|
2249
|
+
icon: "i-lucide-list"
|
|
2250
|
+
},
|
|
2251
|
+
{
|
|
2252
|
+
kind: "orderedList",
|
|
2253
|
+
label: "Numbered list",
|
|
2254
|
+
icon: "i-lucide-list-ordered"
|
|
2255
|
+
},
|
|
2256
|
+
{
|
|
2257
|
+
kind: "blockquote",
|
|
2258
|
+
label: "Quote",
|
|
2259
|
+
icon: "i-lucide-quote"
|
|
2260
|
+
},
|
|
2261
|
+
{
|
|
2262
|
+
kind: "codeBlock",
|
|
2263
|
+
label: "Code block",
|
|
2264
|
+
icon: "i-lucide-square-code"
|
|
2265
|
+
},
|
|
2266
|
+
{
|
|
2267
|
+
kind: "horizontalRule",
|
|
2268
|
+
label: "Divider",
|
|
2269
|
+
icon: "i-lucide-minus"
|
|
2270
|
+
}
|
|
2271
|
+
];
|
|
2272
|
+
const image = {
|
|
2273
|
+
kind: "image",
|
|
2274
|
+
label: "Image",
|
|
2275
|
+
description: "Upload an image, video, or audio file",
|
|
2276
|
+
icon: "i-lucide-image"
|
|
2277
|
+
};
|
|
2278
|
+
/**
|
|
2279
|
+
* A Markdown editor on comark-ui's editor, with a bubble toolbar, a "/" menu, and a drag handle: Nuxt UI's, or the ones
|
|
2280
|
+
* given to `createViteHubUI({ components })`, such as `uiSdkComponents`. Bind Markdown with
|
|
2281
|
+
* `v-model`, or pass an `editor` the app made, such as a Realtime room's. With `upload`, pasted and dropped files and the
|
|
2282
|
+
* "/" menu's Image item upload with a placeholder that shows the percentage; a string is a Blob upload route.
|
|
2283
|
+
*/
|
|
2284
|
+
const MarkdownEditor = defineComponent({
|
|
2285
|
+
name: "MarkdownEditor",
|
|
2286
|
+
inheritAttrs: false,
|
|
2287
|
+
props: {
|
|
2288
|
+
modelValue: {
|
|
2289
|
+
type: String,
|
|
2290
|
+
default: void 0
|
|
2291
|
+
},
|
|
2292
|
+
/** An editor the app made, used instead of one bound to `modelValue`. */
|
|
2293
|
+
editor: {
|
|
2294
|
+
type: Object,
|
|
2295
|
+
default: void 0
|
|
2296
|
+
},
|
|
2297
|
+
/** A Blob upload route, such as `/api/uploads`, or a function that uploads one file and resolves with its URL. */
|
|
2298
|
+
upload: {
|
|
2299
|
+
type: [String, Function],
|
|
2300
|
+
default: void 0
|
|
2301
|
+
},
|
|
2302
|
+
/** Which files to take. Images, video, and audio by default. */
|
|
2303
|
+
accept: {
|
|
2304
|
+
type: Function,
|
|
2305
|
+
default: void 0
|
|
2306
|
+
},
|
|
2307
|
+
/** Parse plugins the page renders with, so a document only they read opens read-only. */
|
|
2308
|
+
plugins: {
|
|
2309
|
+
type: Array,
|
|
2310
|
+
default: void 0
|
|
2311
|
+
},
|
|
2312
|
+
/** Extra Tiptap extensions for the editor bound to `modelValue`. */
|
|
2313
|
+
extensions: {
|
|
2314
|
+
type: Array,
|
|
2315
|
+
default: void 0
|
|
2316
|
+
}
|
|
2317
|
+
},
|
|
2318
|
+
emits: {
|
|
2319
|
+
"update:modelValue": (_value) => true,
|
|
2320
|
+
/** An upload failed; its placeholder is gone. */
|
|
2321
|
+
uploadError: (_error, _file) => true
|
|
2322
|
+
},
|
|
2323
|
+
setup(props, { attrs, emit, slots }) {
|
|
2324
|
+
const UEditorToolbar = useUIComponent("UEditorToolbar");
|
|
2325
|
+
const UEditorSuggestionMenu = useUIComponent("UEditorSuggestionMenu");
|
|
2326
|
+
const UEditorDragHandle = useUIComponent("UEditorDragHandle");
|
|
2327
|
+
const markdown = ref(props.modelValue ?? "");
|
|
2328
|
+
watch(() => props.modelValue, (value) => {
|
|
2329
|
+
if (value !== void 0) markdown.value = value;
|
|
2330
|
+
});
|
|
2331
|
+
watch(markdown, (value) => {
|
|
2332
|
+
if (value !== props.modelValue) emit("update:modelValue", value);
|
|
2333
|
+
});
|
|
2334
|
+
const initialEditor = props.editor;
|
|
2335
|
+
const own = initialEditor ? void 0 : useComarkEditor(markdown, {
|
|
2336
|
+
extensions: props.extensions,
|
|
2337
|
+
plugins: props.plugins
|
|
2338
|
+
});
|
|
2339
|
+
const editor = computed(() => initialEditor ?? own?.editor.value);
|
|
2340
|
+
const options = shallowRef();
|
|
2341
|
+
watchEffect(() => {
|
|
2342
|
+
const send = typeof props.upload === "string" ? blobUpload(props.upload) : props.upload;
|
|
2343
|
+
options.value = send && {
|
|
2344
|
+
upload: send,
|
|
2345
|
+
accept: props.accept ?? ((file) => /^(?:image|video|audio)\//.test(file.type)),
|
|
2346
|
+
onError: (error, file) => emit("uploadError", error, file)
|
|
2347
|
+
};
|
|
2348
|
+
});
|
|
2349
|
+
watch([editor, options], ([current, upload], _, onCleanup) => {
|
|
2350
|
+
if (!current || !upload || uploadKey.getState(current.state)) return;
|
|
2351
|
+
current.registerPlugin(uploadPlugin(upload));
|
|
2352
|
+
onCleanup(() => {
|
|
2353
|
+
if (!current.isDestroyed) current.unregisterPlugin(uploadKey);
|
|
2354
|
+
});
|
|
2355
|
+
}, { immediate: true });
|
|
2356
|
+
const handlers = computed(() => ({
|
|
2357
|
+
...createEditorHandlers(),
|
|
2358
|
+
image: {
|
|
2359
|
+
canExecute: () => Boolean(options.value),
|
|
2360
|
+
execute: (current) => ({ run: () => options.value ? chooseFiles(current.view, options.value) : false }),
|
|
2361
|
+
isActive: () => false,
|
|
2362
|
+
isDisabled: () => !options.value
|
|
2363
|
+
}
|
|
2364
|
+
}));
|
|
2365
|
+
provide(editorHandlersKey, handlers);
|
|
2366
|
+
const menu = computed(() => options.value ? [...blocks, image] : blocks);
|
|
2367
|
+
return () => {
|
|
2368
|
+
const current = editor.value;
|
|
2369
|
+
if (!current) return null;
|
|
2370
|
+
return h("div", {
|
|
2371
|
+
...attrs,
|
|
2372
|
+
class: ["vh-markdown-editor", attrs.class]
|
|
2373
|
+
}, [
|
|
2374
|
+
h("div", { class: "vh-markdown-editor-content vh-typeset" }, [h("style", {
|
|
2375
|
+
"data-comark-typeset": "",
|
|
2376
|
+
innerHTML: typesetCss
|
|
2377
|
+
}), h(EditorContent, { editor: current })]),
|
|
2378
|
+
h(UEditorToolbar, {
|
|
2379
|
+
editor: current,
|
|
2380
|
+
items: toolbar,
|
|
2381
|
+
layout: "bubble"
|
|
2382
|
+
}),
|
|
2383
|
+
h(UEditorSuggestionMenu, {
|
|
2384
|
+
editor: current,
|
|
2385
|
+
items: menu.value
|
|
2386
|
+
}),
|
|
2387
|
+
h(UEditorDragHandle, { editor: current }),
|
|
2388
|
+
slots.default?.({ editor: current })
|
|
2389
|
+
]);
|
|
2390
|
+
};
|
|
2391
|
+
}
|
|
2392
|
+
});
|
|
2393
|
+
//#endregion
|
|
2394
|
+
export { blobUpload as n, MarkdownEditor as t };
|