hostlet 0.0.0-stage → 0.1.0-alpha.2
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/LICENSE +21 -0
- package/README.md +233 -2
- package/bin/hostlet.js +61 -0
- package/console/app/api.js +1223 -0
- package/console/app/boot.js +165 -0
- package/console/app/cards.js +222 -0
- package/console/app/compose-quote.js +127 -0
- package/console/app/config-form.js +727 -0
- package/console/app/document-drafts.js +62 -0
- package/console/app/document-tools.mjs +67 -0
- package/console/app/documents.js +106 -0
- package/console/app/pages.js +6483 -0
- package/console/app/plugins.js +509 -0
- package/console/app/preview-tools.js +172 -0
- package/console/app/reference-labels.js +118 -0
- package/console/app/selection.js +372 -0
- package/console/app/store.js +57 -0
- package/console/app/text-anchor.js +26 -0
- package/console/app/topic-local.js +322 -0
- package/console/app/topic-nav.js +414 -0
- package/console/app/topic-status.js +109 -0
- package/console/app/views.js +43 -0
- package/console/apple-touch-icon.png +0 -0
- package/console/avatars/agy.svg +34 -0
- package/console/avatars/alhena.svg +36 -0
- package/console/avatars/castor.svg +38 -0
- package/console/avatars/claude.svg +34 -0
- package/console/avatars/codex.svg +34 -0
- package/console/avatars/devin.svg +34 -0
- package/console/avatars/echo.svg +34 -0
- package/console/avatars/kimi.svg +33 -0
- package/console/avatars/opencode.svg +33 -0
- package/console/avatars/pinion.svg +34 -0
- package/console/avatars/sprocket.svg +34 -0
- package/console/avatars/vela.svg +44 -0
- package/console/avatars/wasat.svg +38 -0
- package/console/clients/agy.svg +1 -0
- package/console/clients/claude.svg +1 -0
- package/console/clients/codex.svg +1 -0
- package/console/clients/devin.svg +1 -0
- package/console/clients/kimi.svg +1 -0
- package/console/clients/opencode.svg +1 -0
- package/console/favicon.ico +0 -0
- package/console/favicon.svg +16 -0
- package/console/index.html +99 -0
- package/console/pdl/engine/README.md +13 -0
- package/console/pdl/engine/anchor.js +195 -0
- package/console/pdl/engine/core.js +1340 -0
- package/console/pdl/engine/frame.js +1252 -0
- package/console/pdl/engine/i18n.js +67 -0
- package/console/pdl/engine/pdl-check.js +102 -0
- package/console/pdl/engine/pdl-doc.js +353 -0
- package/console/pdl/engine/pdl-view.js +280 -0
- package/console/pdl/engine/pdl.js +1064 -0
- package/console/pdl/engine/platform.js +18 -0
- package/console/pdl/engine/proj.js +279 -0
- package/console/pdl/engine/theme.js +176 -0
- package/console/pdl/engine/tree.js +171 -0
- package/console/pdl/engine/units.js +546 -0
- package/console/pdl/library/README.md +28 -0
- package/console/pdl/library/blocks.js +1512 -0
- package/console/pdl/library/buttons.js +86 -0
- package/console/pdl/library/capsules.js +130 -0
- package/console/pdl/library/charts.js +287 -0
- package/console/pdl/library/code.js +98 -0
- package/console/pdl/library/css/base.css +12 -0
- package/console/pdl/library/css/blocks.css +98 -0
- package/console/pdl/library/css/capsules.css +42 -0
- package/console/pdl/library/css/catalog-page.css +24 -0
- package/console/pdl/library/css/chat.css +100 -0
- package/console/pdl/library/css/composer-top.css +145 -0
- package/console/pdl/library/css/controls.css +195 -0
- package/console/pdl/library/css/drag.css +29 -0
- package/console/pdl/library/css/edit-layout.css +93 -0
- package/console/pdl/library/css/floating-window.css +30 -0
- package/console/pdl/library/css/hover.css +50 -0
- package/console/pdl/library/css/label-search.css +37 -0
- package/console/pdl/library/css/layers.css +43 -0
- package/console/pdl/library/css/library-parts.css +33 -0
- package/console/pdl/library/css/library.css +117 -0
- package/console/pdl/library/css/member-select.css +34 -0
- package/console/pdl/library/css/message.css +72 -0
- package/console/pdl/library/css/page.css +104 -0
- package/console/pdl/library/css/parts.css +338 -0
- package/console/pdl/library/css/responsive.css +40 -0
- package/console/pdl/library/css/roles.css +28 -0
- package/console/pdl/library/css/rows.css +231 -0
- package/console/pdl/library/css/settings-rows.css +37 -0
- package/console/pdl/library/css/shell.css +59 -0
- package/console/pdl/library/css/stream.css +296 -0
- package/console/pdl/library/css/theme-calculator.css +18 -0
- package/console/pdl/library/css/thumb-choice.css +69 -0
- package/console/pdl/library/css/tweaks.css +16 -0
- package/console/pdl/library/css/widgets.css +211 -0
- package/console/pdl/library/css/workbench.css +59 -0
- package/console/pdl/library/feedback.js +83 -0
- package/console/pdl/library/floating-window.mjs +100 -0
- package/console/pdl/library/fold.js +238 -0
- package/console/pdl/library/format.js +53 -0
- package/console/pdl/library/frames.js +123 -0
- package/console/pdl/library/icons.js +165 -0
- package/console/pdl/library/identity.js +149 -0
- package/console/pdl/library/inputs.js +801 -0
- package/console/pdl/library/label-search.js +214 -0
- package/console/pdl/library/markdown-text.js +115 -0
- package/console/pdl/library/markdown.js +150 -0
- package/console/pdl/library/media-viewer.js +293 -0
- package/console/pdl/library/member-select.mjs +32 -0
- package/console/pdl/library/message.js +105 -0
- package/console/pdl/library/motion.js +56 -0
- package/console/pdl/library/overlays.js +165 -0
- package/console/pdl/library/page.js +142 -0
- package/console/pdl/library/plugin-form.js +154 -0
- package/console/pdl/library/registry.js +198 -0
- package/console/pdl/library/rows.js +523 -0
- package/console/pdl/library/shell.js +151 -0
- package/console/pdl/library/stream.js +58 -0
- package/console/pdl/library/tabs.js +259 -0
- package/console/pdl/library/theme-calculator.js +300 -0
- package/console/pdl/library/widgets.js +114 -0
- package/console/pdl/load-order.json +74 -0
- package/console/pdl/platform/README.md +3 -0
- package/console/pdl/platform/rich-markdown.mjs +17 -0
- package/console/pdl/platform/web-links.js +26 -0
- package/console/pdl/platform/web.js +80 -0
- package/console/sdk-browser/mentions.d.mts +16 -0
- package/console/sdk-browser/mentions.mjs +84 -0
- package/console/theme/assets.js +21 -0
- package/console/theme/theme.json +9417 -0
- package/console/theme/tokens.json +444 -0
- package/dist/core/a2a.d.ts +288 -0
- package/dist/core/a2a.js +2197 -0
- package/dist/core/address.d.ts +34 -0
- package/dist/core/address.js +33 -0
- package/dist/core/approval-inputs.d.ts +17 -0
- package/dist/core/approval-inputs.js +70 -0
- package/dist/core/archive.d.ts +19 -0
- package/dist/core/archive.js +135 -0
- package/dist/core/attachments.d.ts +17 -0
- package/dist/core/attachments.js +143 -0
- package/dist/core/blocked-handoffs.d.ts +132 -0
- package/dist/core/blocked-handoffs.js +349 -0
- package/dist/core/config-schema.d.ts +124 -0
- package/dist/core/config-schema.js +192 -0
- package/dist/core/config.d.ts +117 -0
- package/dist/core/config.js +638 -0
- package/dist/core/disk-usage.d.ts +26 -0
- package/dist/core/disk-usage.js +83 -0
- package/dist/core/documents.d.ts +9 -0
- package/dist/core/documents.js +48 -0
- package/dist/core/entry.d.ts +316 -0
- package/dist/core/entry.js +711 -0
- package/dist/core/home.d.ts +76 -0
- package/dist/core/home.js +449 -0
- package/dist/core/i18n.d.ts +36 -0
- package/dist/core/i18n.js +458 -0
- package/dist/core/instances.d.ts +224 -0
- package/dist/core/instances.js +1272 -0
- package/dist/core/members.d.ts +190 -0
- package/dist/core/members.js +358 -0
- package/dist/core/mentions.d.ts +12 -0
- package/dist/core/mentions.js +49 -0
- package/dist/core/messages.d.ts +876 -0
- package/dist/core/messages.js +916 -0
- package/dist/core/notices.d.ts +88 -0
- package/dist/core/notices.js +54 -0
- package/dist/core/ops.d.ts +117 -0
- package/dist/core/ops.js +146 -0
- package/dist/core/package.d.ts +2 -0
- package/dist/core/package.js +14 -0
- package/dist/core/parts.d.ts +25 -0
- package/dist/core/parts.js +119 -0
- package/dist/core/plugins/catalog.d.ts +14 -0
- package/dist/core/plugins/catalog.js +54 -0
- package/dist/core/plugins/external.d.ts +7 -0
- package/dist/core/plugins/external.js +921 -0
- package/dist/core/plugins/index.d.ts +5 -0
- package/dist/core/plugins/index.js +51 -0
- package/dist/core/plugins/npm.d.ts +12 -0
- package/dist/core/plugins/npm.js +99 -0
- package/dist/core/plugins/plugin-manifest.d.ts +12 -0
- package/dist/core/plugins/plugin-manifest.js +231 -0
- package/dist/core/push.d.ts +22 -0
- package/dist/core/push.js +63 -0
- package/dist/core/resource.d.ts +77 -0
- package/dist/core/resource.js +48 -0
- package/dist/core/spawn.d.ts +12 -0
- package/dist/core/spawn.js +30 -0
- package/dist/core/storage-inventory.d.ts +118 -0
- package/dist/core/storage-inventory.js +279 -0
- package/dist/core/storage.d.ts +45 -0
- package/dist/core/storage.js +602 -0
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.js +33 -0
- package/dist/core/topic-index.d.ts +19 -0
- package/dist/core/topic-index.js +70 -0
- package/dist/core/topic-ops.d.ts +139 -0
- package/dist/core/topic-ops.js +507 -0
- package/dist/core/topic-status.d.ts +32 -0
- package/dist/core/topic-status.js +105 -0
- package/dist/core/topics.d.ts +327 -0
- package/dist/core/topics.js +786 -0
- package/dist/core/turn-clients.d.ts +1 -0
- package/dist/core/turn-clients.js +5 -0
- package/dist/core/types.d.ts +2866 -0
- package/dist/core/types.js +166 -0
- package/dist/core/util.d.ts +10 -0
- package/dist/core/util.js +47 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +82 -0
- package/dist/plugins/adapter-acp/index.d.ts +3 -0
- package/dist/plugins/adapter-acp/index.js +57 -0
- package/dist/plugins/adapter-acp/plugin.yaml +100 -0
- package/dist/plugins/adapter-agy/command.d.ts +9 -0
- package/dist/plugins/adapter-agy/command.js +34 -0
- package/dist/plugins/adapter-agy/index.d.ts +5 -0
- package/dist/plugins/adapter-agy/index.js +65 -0
- package/dist/plugins/adapter-agy/quota.d.ts +2 -0
- package/dist/plugins/adapter-agy/quota.js +155 -0
- package/dist/plugins/adapter-agy/terminal.d.ts +6 -0
- package/dist/plugins/adapter-agy/terminal.js +132 -0
- package/dist/plugins/adapter-agy/version.d.ts +9 -0
- package/dist/plugins/adapter-agy/version.js +17 -0
- package/dist/plugins/adapter-claude/index.d.ts +43 -0
- package/dist/plugins/adapter-claude/index.js +412 -0
- package/dist/plugins/adapter-claude/quota.d.ts +14 -0
- package/dist/plugins/adapter-claude/quota.js +103 -0
- package/dist/plugins/adapter-codex/index.d.ts +4 -0
- package/dist/plugins/adapter-codex/index.js +688 -0
- package/dist/plugins/adapter-codex/responses-model.d.ts +9 -0
- package/dist/plugins/adapter-codex/responses-model.js +82 -0
- package/dist/plugins/adapter-kimi/index.d.ts +4 -0
- package/dist/plugins/adapter-kimi/index.js +51 -0
- package/dist/plugins/adapter-kimi/quota.d.ts +3 -0
- package/dist/plugins/adapter-kimi/quota.js +171 -0
- package/dist/plugins/adapter-opencode/index.d.ts +12 -0
- package/dist/plugins/adapter-opencode/index.js +64 -0
- package/dist/plugins/annotations/console/cards.css +189 -0
- package/dist/plugins/annotations/console/cards.mjs +439 -0
- package/dist/plugins/annotations/console/pdl.json +52 -0
- package/dist/plugins/annotations/console/view.mjs +241 -0
- package/dist/plugins/annotations/console/window.mjs +219 -0
- package/dist/plugins/annotations/followups.d.ts +51 -0
- package/dist/plugins/annotations/followups.js +112 -0
- package/dist/plugins/annotations/index.d.ts +3 -0
- package/dist/plugins/annotations/index.js +298 -0
- package/dist/plugins/annotations/plugin.yaml +3 -0
- package/dist/plugins/ask-choice/index.d.ts +9 -0
- package/dist/plugins/ask-choice/index.js +24 -0
- package/dist/plugins/ask-choice/plugin.yaml +2 -0
- package/dist/plugins/background/index.d.ts +2 -0
- package/dist/plugins/background/index.js +398 -0
- package/dist/plugins/background/plugin.yaml +7 -0
- package/dist/plugins/builtin.json +37 -0
- package/dist/plugins/cards/console/pdl.json +516 -0
- package/dist/plugins/cards/console/view.mjs +67 -0
- package/dist/plugins/cards/index.d.ts +8 -0
- package/dist/plugins/cards/index.js +627 -0
- package/dist/plugins/cards/plugin.yaml +3 -0
- package/dist/plugins/console/browser.d.ts +2 -0
- package/dist/plugins/console/browser.js +115 -0
- package/dist/plugins/console/console/pdl.json +38 -0
- package/dist/plugins/console/console/view.mjs +26 -0
- package/dist/plugins/console/index.d.ts +4 -0
- package/dist/plugins/console/index.js +94 -0
- package/dist/plugins/console/picker.d.ts +2 -0
- package/dist/plugins/console/picker.js +48 -0
- package/dist/plugins/console/plugin.yaml +3 -0
- package/dist/plugins/console/topic-view.d.ts +3 -0
- package/dist/plugins/console/topic-view.js +388 -0
- package/dist/plugins/handoff/index.d.ts +2 -0
- package/dist/plugins/handoff/index.js +147 -0
- package/dist/plugins/handoff/plugin.yaml +8 -0
- package/dist/plugins/host-api/index.d.ts +7 -0
- package/dist/plugins/host-api/index.js +310 -0
- package/dist/plugins/host-api/plugin.yaml +2 -0
- package/dist/plugins/host-api/signing.d.ts +63 -0
- package/dist/plugins/host-api/signing.js +427 -0
- package/dist/plugins/host-api/skills/host-ops/SKILL.md +28 -0
- package/dist/plugins/host-api/skills/hostlet-plugin-dev/SKILL.md +38 -0
- package/dist/plugins/inbox/index.d.ts +15 -0
- package/dist/plugins/inbox/index.js +161 -0
- package/dist/plugins/inbox/plugin.yaml +2 -0
- package/dist/plugins/keys/account-directories.d.ts +18 -0
- package/dist/plugins/keys/account-directories.js +86 -0
- package/dist/plugins/keys/account-storage.d.ts +18 -0
- package/dist/plugins/keys/account-storage.js +112 -0
- package/dist/plugins/keys/console/pdl.json +710 -0
- package/dist/plugins/keys/console/view.mjs +619 -0
- package/dist/plugins/keys/index.d.ts +7 -0
- package/dist/plugins/keys/index.js +842 -0
- package/dist/plugins/keys/login-link.d.ts +2 -0
- package/dist/plugins/keys/login-link.js +15 -0
- package/dist/plugins/keys/plugin.yaml +3 -0
- package/dist/plugins/keys/quota-history.d.ts +91 -0
- package/dist/plugins/keys/quota-history.js +420 -0
- package/dist/plugins/mcp/console/pdl.json +412 -0
- package/dist/plugins/mcp/console/view.mjs +207 -0
- package/dist/plugins/mcp/index.d.ts +7 -0
- package/dist/plugins/mcp/index.js +494 -0
- package/dist/plugins/mcp/plugin.yaml +3 -0
- package/dist/plugins/member-health/index.d.ts +2 -0
- package/dist/plugins/member-health/index.js +478 -0
- package/dist/plugins/member-health/plugin.yaml +2 -0
- package/dist/plugins/memory/capabilities.d.ts +10 -0
- package/dist/plugins/memory/capabilities.js +70 -0
- package/dist/plugins/memory/console/pdl.json +459 -0
- package/dist/plugins/memory/console/view.mjs +254 -0
- package/dist/plugins/memory/corpus.d.ts +60 -0
- package/dist/plugins/memory/corpus.js +521 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.d.ts +8 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.js +49 -0
- package/dist/plugins/memory/engines/fts-tree-text.d.ts +12 -0
- package/dist/plugins/memory/engines/fts-tree-text.js +93 -0
- package/dist/plugins/memory/engines/fts-tree.d.ts +4 -0
- package/dist/plugins/memory/engines/fts-tree.js +387 -0
- package/dist/plugins/memory/engines/search.d.ts +9 -0
- package/dist/plugins/memory/engines/search.js +35 -0
- package/dist/plugins/memory/index.d.ts +3 -0
- package/dist/plugins/memory/index.js +465 -0
- package/dist/plugins/memory/notes.d.ts +22 -0
- package/dist/plugins/memory/notes.js +140 -0
- package/dist/plugins/memory/plugin.yaml +23 -0
- package/dist/plugins/model-profiles/index.d.ts +2 -0
- package/dist/plugins/model-profiles/index.js +919 -0
- package/dist/plugins/model-profiles/plugin.yaml +13 -0
- package/dist/plugins/model-profiles/review-source.d.ts +11 -0
- package/dist/plugins/model-profiles/review-source.js +640 -0
- package/dist/plugins/model-profiles/rules.d.ts +132 -0
- package/dist/plugins/model-profiles/rules.js +767 -0
- package/dist/plugins/model-profiles/skills/model-profile/SKILL.md +60 -0
- package/dist/plugins/model-profiles/sources.d.ts +61 -0
- package/dist/plugins/model-profiles/sources.js +375 -0
- package/dist/plugins/model-profiles/storage.d.ts +104 -0
- package/dist/plugins/model-profiles/storage.js +352 -0
- package/dist/plugins/model-profiles/types.d.ts +187 -0
- package/dist/plugins/model-profiles/types.js +12 -0
- package/dist/plugins/model-profiles/wakeup.d.ts +29 -0
- package/dist/plugins/model-profiles/wakeup.js +390 -0
- package/dist/plugins/models/index.d.ts +2 -0
- package/dist/plugins/models/index.js +63 -0
- package/dist/plugins/models/plugin.yaml +8 -0
- package/dist/plugins/ops/collector.d.ts +22 -0
- package/dist/plugins/ops/collector.js +48 -0
- package/dist/plugins/ops/config.d.ts +11 -0
- package/dist/plugins/ops/config.js +13 -0
- package/dist/plugins/ops/console/pdl.json +676 -0
- package/dist/plugins/ops/console/view.mjs +700 -0
- package/dist/plugins/ops/index.d.ts +3 -0
- package/dist/plugins/ops/index.js +20 -0
- package/dist/plugins/ops/journal.d.ts +7 -0
- package/dist/plugins/ops/journal.js +109 -0
- package/dist/plugins/ops/metrics.d.ts +3 -0
- package/dist/plugins/ops/metrics.js +81 -0
- package/dist/plugins/ops/plugin.yaml +63 -0
- package/dist/plugins/ops/samples.d.ts +54 -0
- package/dist/plugins/ops/samples.js +100 -0
- package/dist/plugins/ops/trace.d.ts +3 -0
- package/dist/plugins/ops/trace.js +104 -0
- package/dist/plugins/ops/usage.d.ts +2 -0
- package/dist/plugins/ops/usage.js +152 -0
- package/dist/plugins/plans/acceptance.d.ts +284 -0
- package/dist/plugins/plans/acceptance.js +247 -0
- package/dist/plugins/plans/blocked.d.ts +25 -0
- package/dist/plugins/plans/blocked.js +66 -0
- package/dist/plugins/plans/console/pdl.json +639 -0
- package/dist/plugins/plans/console/view.d.mts +2 -0
- package/dist/plugins/plans/console/view.mjs +855 -0
- package/dist/plugins/plans/dependencies.d.ts +5 -0
- package/dist/plugins/plans/dependencies.js +21 -0
- package/dist/plugins/plans/index.d.ts +72 -0
- package/dist/plugins/plans/index.js +2057 -0
- package/dist/plugins/plans/plugin.yaml +44 -0
- package/dist/plugins/plans/progress.d.ts +82 -0
- package/dist/plugins/plans/progress.js +516 -0
- package/dist/plugins/prompt/index.d.ts +8 -0
- package/dist/plugins/prompt/index.js +361 -0
- package/dist/plugins/prompt/plugin.yaml +2 -0
- package/dist/plugins/recovery/console/pdl.json +50 -0
- package/dist/plugins/recovery/console/view.mjs +46 -0
- package/dist/plugins/recovery/index.d.ts +9 -0
- package/dist/plugins/recovery/index.js +454 -0
- package/dist/plugins/recovery/plugin.yaml +9 -0
- package/dist/plugins/recovery/probe.d.ts +11 -0
- package/dist/plugins/recovery/probe.js +128 -0
- package/dist/plugins/restart/console/pdl.json +112 -0
- package/dist/plugins/restart/console/view.mjs +85 -0
- package/dist/plugins/restart/index.d.ts +10 -0
- package/dist/plugins/restart/index.js +669 -0
- package/dist/plugins/restart/plugin.yaml +9 -0
- package/dist/plugins/restart/scope.d.ts +6 -0
- package/dist/plugins/restart/scope.js +44 -0
- package/dist/plugins/schedule/console/pdl.json +696 -0
- package/dist/plugins/schedule/console/view.mjs +398 -0
- package/dist/plugins/schedule/events.d.ts +76 -0
- package/dist/plugins/schedule/events.js +158 -0
- package/dist/plugins/schedule/index.d.ts +32 -0
- package/dist/plugins/schedule/index.js +1199 -0
- package/dist/plugins/schedule/plugin.yaml +26 -0
- package/dist/plugins/schedule/when.d.ts +14 -0
- package/dist/plugins/schedule/when.js +83 -0
- package/dist/plugins/sessions/console/pdl.json +259 -0
- package/dist/plugins/sessions/console/view.mjs +355 -0
- package/dist/plugins/sessions/index.d.ts +3 -0
- package/dist/plugins/sessions/index.js +126 -0
- package/dist/plugins/sessions/participants.d.ts +28 -0
- package/dist/plugins/sessions/participants.js +83 -0
- package/dist/plugins/sessions/plugin.yaml +3 -0
- package/dist/plugins/signals/console/pdl.json +802 -0
- package/dist/plugins/signals/console/view.mjs +284 -0
- package/dist/plugins/signals/content.d.ts +31 -0
- package/dist/plugins/signals/content.js +151 -0
- package/dist/plugins/signals/index.d.ts +2 -0
- package/dist/plugins/signals/index.js +601 -0
- package/dist/plugins/signals/plugin.yaml +32 -0
- package/dist/plugins/signals/skills/signal-sources/SKILL.md +27 -0
- package/dist/plugins/skills/console/pdl.json +254 -0
- package/dist/plugins/skills/console/view.mjs +204 -0
- package/dist/plugins/skills/index.d.ts +11 -0
- package/dist/plugins/skills/index.js +567 -0
- package/dist/plugins/skills/install.d.ts +69 -0
- package/dist/plugins/skills/install.js +265 -0
- package/dist/plugins/skills/plugin.yaml +24 -0
- package/dist/plugins/skills/safety-scan.d.ts +17 -0
- package/dist/plugins/skills/safety-scan.js +78 -0
- package/dist/plugins/skills/skills/skill-ops/SKILL.md +29 -0
- package/dist/plugins/terminal/console/pdl.json +115 -0
- package/dist/plugins/terminal/console/view.mjs +178 -0
- package/dist/plugins/terminal/index.d.ts +3 -0
- package/dist/plugins/terminal/index.js +204 -0
- package/dist/plugins/terminal/plugin.yaml +2 -0
- package/dist/plugins/tools/index.d.ts +3 -0
- package/dist/plugins/tools/index.js +458 -0
- package/dist/plugins/tools/plugin.yaml +2 -0
- package/dist/plugins/tools/token.d.ts +61 -0
- package/dist/plugins/tools/token.js +179 -0
- package/dist/plugins/topic-tools/index.d.ts +9 -0
- package/dist/plugins/topic-tools/index.js +29 -0
- package/dist/plugins/topic-tools/plugin.yaml +2 -0
- package/dist/plugins/topic-tools/skills/topic-tidy/SKILL.md +108 -0
- package/dist/plugins/works/console/pdl.json +120 -0
- package/dist/plugins/works/console/view.mjs +91 -0
- package/dist/plugins/works/document-words.d.ts +50 -0
- package/dist/plugins/works/document-words.js +27 -0
- package/dist/plugins/works/editors.d.ts +3 -0
- package/dist/plugins/works/editors.js +162 -0
- package/dist/plugins/works/events.d.ts +58 -0
- package/dist/plugins/works/events.js +82 -0
- package/dist/plugins/works/files.d.ts +23 -0
- package/dist/plugins/works/files.js +53 -0
- package/dist/plugins/works/index.d.ts +9 -0
- package/dist/plugins/works/index.js +1088 -0
- package/dist/plugins/works/plugin.yaml +9 -0
- package/dist/plugins/works/preview.d.ts +10 -0
- package/dist/plugins/works/preview.js +30 -0
- package/dist/plugins/works/project.d.ts +3 -0
- package/dist/plugins/works/project.js +245 -0
- package/dist/plugins/works/resource.d.ts +28 -0
- package/dist/plugins/works/resource.js +71 -0
- package/dist/plugins/works/retention.d.ts +36 -0
- package/dist/plugins/works/retention.js +295 -0
- package/dist/plugins/works/skills/works-ops/SKILL.md +14 -0
- package/dist/plugins/works/viewers.d.ts +13 -0
- package/dist/plugins/works/viewers.js +87 -0
- package/dist/plugins/works/web/anchors.js +94 -0
- package/dist/plugins/works/web/document-edit.js +245 -0
- package/dist/plugins/works/web/preview-bridge.js +142 -0
- package/dist/plugins/works/web/reading-position.js +74 -0
- package/dist/plugins/works/web/view.html +1118 -0
- package/dist/runtime/runtime.d.ts +210 -0
- package/dist/runtime/runtime.js +338 -0
- package/dist/sdk/adapter-kit.d.ts +1 -0
- package/dist/sdk/adapter-kit.js +1 -0
- package/dist/sdk/client.d.ts +163 -0
- package/dist/sdk/client.js +43 -0
- package/dist/sdk/contracts.d.ts +1263 -0
- package/dist/sdk/contracts.js +1 -0
- package/dist/sdk/index.d.ts +57 -0
- package/dist/sdk/index.js +27 -0
- package/dist/sdk/source-choice.d.ts +40 -0
- package/dist/sdk/source-choice.js +217 -0
- package/dist/sdk/topic-text.d.ts +5 -0
- package/dist/sdk/topic-text.js +26 -0
- package/dist/sdk/transport.d.ts +12 -0
- package/dist/sdk/transport.js +34 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.d.ts +88 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.js +674 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.d.ts +18 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.js +39 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.d.ts +8 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.js +6 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.js +108 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.d.ts +3 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.js +4 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.js +84 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.d.ts +5 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.js +37 -0
- package/node_modules/@hostlet/adapter-kit/package.json +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.d.ts +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.js +276 -0
- package/node_modules/@hostlet/platform/dist/files.d.ts +24 -0
- package/node_modules/@hostlet/platform/dist/files.js +113 -0
- package/node_modules/@hostlet/platform/dist/index.d.ts +6 -0
- package/node_modules/@hostlet/platform/dist/index.js +4 -0
- package/node_modules/@hostlet/platform/dist/paths.d.ts +4 -0
- package/node_modules/@hostlet/platform/dist/paths.js +9 -0
- package/node_modules/@hostlet/platform/dist/process.d.ts +45 -0
- package/node_modules/@hostlet/platform/dist/process.js +297 -0
- package/node_modules/@hostlet/platform/package.json +14 -0
- package/package.json +60 -4
|
@@ -0,0 +1,1118 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>产物</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root {
|
|
9
|
+
color-scheme: light;
|
|
10
|
+
--bg-page: #f6f1ea; --bg-raised: #fff; --bg-code: #eee7dd;
|
|
11
|
+
--fg: #2b2622; --fg-muted: #75695d; --line: #e3dbcf; --line-strong: #cfc5b6;
|
|
12
|
+
--accent: #175e8a; --accent-soft: #ece3d6;
|
|
13
|
+
--font-text: system-ui, sans-serif; --font-mono: ui-monospace, monospace;
|
|
14
|
+
}
|
|
15
|
+
@media (prefers-color-scheme: dark) {
|
|
16
|
+
:root {
|
|
17
|
+
color-scheme: dark;
|
|
18
|
+
--bg-page: #1c1914; --bg-raised: #24211c; --bg-code: #2d2821;
|
|
19
|
+
--fg: #d6d4d1; --fg-muted: #a3a19e; --line: #3a342c; --line-strong: #564c40;
|
|
20
|
+
--accent: #7fb0d0; --accent-soft: #303b44;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
html, body { margin: 0; height: 100%; font: 14px/1.6 var(--font-text); color: var(--fg); background: var(--bg-page); }
|
|
24
|
+
body { display: flex; flex-direction: column; }
|
|
25
|
+
header { flex: none; display: flex; gap: 8px; align-items: center; padding: 6px 12px; border-bottom: 1px solid var(--line); }
|
|
26
|
+
header b { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
27
|
+
header :is(button, select) { font: inherit; color: inherit; padding: 2px 10px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--bg-raised); cursor: pointer; }
|
|
28
|
+
header button:hover { background: var(--accent-soft); }
|
|
29
|
+
#document-modes:not([hidden]) { display: flex; border: 1px solid var(--line-strong); border-radius: 6px; overflow: hidden; }
|
|
30
|
+
#document-modes button { border: 0; border-radius: 0; white-space: nowrap; }
|
|
31
|
+
#document-modes button[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); }
|
|
32
|
+
#file-actions { position: relative; flex: none; }
|
|
33
|
+
#file-actions summary { display: grid; place-items: center; list-style: none; box-sizing: border-box; width: 30px; height: 28px; border-radius: 5px; color: var(--fg-muted); cursor: pointer; }
|
|
34
|
+
#file-actions[open] summary, #file-actions summary:hover { background: var(--accent-soft); color: var(--fg); }
|
|
35
|
+
#file-actions summary::-webkit-details-marker { display: none; }
|
|
36
|
+
#file-actions > div { position: absolute; inset: calc(100% + 5px) 0 auto auto; z-index: 6; min-inline-size: 9rem; padding: 4px; background: var(--bg-raised); border: 1px solid var(--line-strong); border-radius: 7px; box-shadow: 0 8px 20px #0002; }
|
|
37
|
+
#file-actions button:not([hidden]) { display: block; width: 100%; padding: 5px 8px; text-align: start; border: 0; white-space: nowrap; }
|
|
38
|
+
#file-actions button:disabled { opacity: .5; cursor: default; }
|
|
39
|
+
#file-status { flex: none; padding: 5px 12px; color: var(--fg-muted); overflow-wrap: anywhere; }
|
|
40
|
+
#file-editor { flex: 1; min-height: 0; overflow: auto; padding: 12px; }
|
|
41
|
+
#file-editor textarea { box-sizing: border-box; width: 100%; height: 100%; resize: none; padding: 9px; color: var(--fg); background: var(--bg-raised); border: 1px solid var(--line-strong); border-radius: 6px; font: 13px/1.65 var(--font-mono); }
|
|
42
|
+
.word-edit-hint { color: var(--fg-muted); font-size: 12px; }
|
|
43
|
+
#file-editor p { margin: 10px 0; }
|
|
44
|
+
.word-table-text { border-inline-start: 2px solid var(--line-strong); padding-inline-start: 8px; }
|
|
45
|
+
#file-editor [contenteditable] { display: inline-block; min-inline-size: 2ch; max-inline-size: 100%; padding: 1px 3px; vertical-align: top; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg-raised); border-block-end: 1px solid var(--line-strong); }
|
|
46
|
+
#file-editor [contenteditable]:focus { outline: 1px solid var(--accent); outline-offset: 1px; }
|
|
47
|
+
#sheet-picker { max-inline-size: 8rem; }
|
|
48
|
+
#pos, #sheet-rows { white-space: nowrap; }
|
|
49
|
+
#stage { flex: 1; min-height: 0; overflow: auto; padding: 12px; }
|
|
50
|
+
#stage.pdf { position: relative; padding: 0; overflow: hidden; }
|
|
51
|
+
.pdf-scroll { position: absolute; inset: 0; overflow: auto; }
|
|
52
|
+
#stage .docx-wrapper { background: transparent; }
|
|
53
|
+
#stage .docx-wrapper > section.docx { color-scheme: light; color: #222; background: #fff; }
|
|
54
|
+
#stage img, #stage video, #stage canvas { max-width: 100%; display: block; margin: 0 auto; }
|
|
55
|
+
.image-size { display: block; width: max-content; max-width: 100%; margin: 0 auto; padding: 0; border: 0; background: none; cursor: zoom-in; }
|
|
56
|
+
.image-size[aria-pressed="true"] { max-width: none; cursor: zoom-out; }
|
|
57
|
+
#stage .image-size[aria-pressed="true"] img { max-width: none; }
|
|
58
|
+
#stage pre { white-space: pre-wrap; word-break: break-word; margin: 0; font: 13px/1.5 var(--font-mono); }
|
|
59
|
+
.markdown { max-width: 72ch; margin-inline: auto; padding: 4px clamp(0px, 2vw, 12px) 12px; overflow-wrap: anywhere; line-height: 1.7; }
|
|
60
|
+
.markdown :is(h1, h2, h3, h4, h5, h6) { line-height: 1.3; margin: 1.2em 0 .5em; }
|
|
61
|
+
.markdown > :first-child { margin-top: 0; }
|
|
62
|
+
.markdown p { margin: .75em 0; }
|
|
63
|
+
.markdown li > p { margin: .2em 0; }
|
|
64
|
+
.markdown :is(ol, ul) { padding-inline-start: 1.65em; }
|
|
65
|
+
.markdown blockquote { margin: 1em 0; padding-inline-start: 1em; border-inline-start: 3px solid var(--line-strong); }
|
|
66
|
+
.markdown table { border-collapse: collapse; }
|
|
67
|
+
.markdown :is(th, td) { padding: .35em .6em; border: 1px solid var(--line); vertical-align: top; }
|
|
68
|
+
.markdown th { background: var(--bg-code); }
|
|
69
|
+
.markdown a { color: var(--accent); }
|
|
70
|
+
.markdown img { max-width: 100%; height: auto; }
|
|
71
|
+
.markdown code { font-family: var(--font-mono); background: var(--bg-code); border-radius: 3px; padding: .1em .25em; }
|
|
72
|
+
#stage .markdown pre { padding: .75em; margin-block: 1em; background: var(--bg-code); border-radius: 5px; }
|
|
73
|
+
.markdown pre code { padding: 0; background: transparent; }
|
|
74
|
+
.markdown hr { border: 0; border-top: 1px solid var(--line); margin-block: 1.5em; }
|
|
75
|
+
.sheet { border-collapse: separate; border-spacing: 0; border-inline-start: 1px solid var(--line); }
|
|
76
|
+
.sheet :is(th, td) { padding: .25em .55em; border-inline-end: 1px solid var(--line); border-block-end: 1px solid var(--line); vertical-align: top; }
|
|
77
|
+
.sheet th { background: var(--bg-code); color: var(--fg-muted); font-variant-numeric: tabular-nums; }
|
|
78
|
+
.sheet thead th { position: sticky; top: 0; z-index: 2; border-block-start: 1px solid var(--line); }
|
|
79
|
+
.sheet th[scope="row"] { position: sticky; inset-inline-start: 0; z-index: 1; text-align: end; }
|
|
80
|
+
.sheet thead th:first-child { inset-inline-start: 0; z-index: 3; }
|
|
81
|
+
.sheet td { min-inline-size: 3em; max-inline-size: 28ch; background: var(--bg-raised); white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
82
|
+
.sheet td.number { text-align: end; font-variant-numeric: tabular-nums; }
|
|
83
|
+
#note { display: flex; gap: 6px; min-width: 0; color: var(--fg-muted); font-size: 12px; }
|
|
84
|
+
#note span:empty { display: none; }
|
|
85
|
+
#notice { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
86
|
+
#selection-actions { position: fixed; z-index: 3; display: flex; gap: 8px; padding: 8px 12px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--bg-raised); }
|
|
87
|
+
#selection-actions[hidden] { display: none; }
|
|
88
|
+
#selection-actions button:not([hidden]) { display: grid; place-items: center; inline-size: 28px; block-size: 28px; padding: 0; border: 0; border-radius: 5px; background: transparent; color: var(--fg-muted); cursor: pointer; }
|
|
89
|
+
#selection-actions button:is(:hover, :focus-visible) { color: var(--accent); }
|
|
90
|
+
#reference-status { overflow-wrap: anywhere; white-space: pre-wrap; }
|
|
91
|
+
#reference-status { flex: none; padding: 5px 12px; max-height: 6rem; overflow: auto; color: var(--fg-muted); background: var(--bg-raised); }
|
|
92
|
+
::highlight(work-location) { background: #e4b950aa; }
|
|
93
|
+
</style>
|
|
94
|
+
</head>
|
|
95
|
+
<body>
|
|
96
|
+
<header>
|
|
97
|
+
<b id="title">…</b><span id="note"><span id="sheet-rows"></span><span id="notice"></span></span>
|
|
98
|
+
<button id="md-mode" hidden>源文件</button><select id="sheet-picker" aria-label="工作表" hidden></select>
|
|
99
|
+
<button id="prev" hidden>上一页</button><span id="pos" hidden></span><button id="next" hidden>下一页</button>
|
|
100
|
+
<div id="document-modes" role="group" hidden>
|
|
101
|
+
<button id="file-read-mode" type="button"></button><button id="file-edit-mode" type="button"></button>
|
|
102
|
+
</div>
|
|
103
|
+
<details id="file-actions" hidden>
|
|
104
|
+
<summary id="file-more"></summary>
|
|
105
|
+
<div>
|
|
106
|
+
<button type="button" id="file-reload"></button>
|
|
107
|
+
<button type="button" id="file-reveal"></button>
|
|
108
|
+
<button type="button" id="file-save" hidden></button>
|
|
109
|
+
<button type="button" id="file-discard" hidden></button>
|
|
110
|
+
</div>
|
|
111
|
+
</details>
|
|
112
|
+
</header>
|
|
113
|
+
<div id="document-tools"></div>
|
|
114
|
+
<div id="file-status" role="status" hidden></div>
|
|
115
|
+
<div id="stage"></div>
|
|
116
|
+
<div id="file-editor" hidden></div>
|
|
117
|
+
<div id="reference-status" role="status" hidden></div>
|
|
118
|
+
<div id="selection-actions" hidden>
|
|
119
|
+
<button id="annotate-selection" type="button" aria-label="加批注" title="加批注" hidden></button>
|
|
120
|
+
<button id="quote-selection" type="button" aria-label="问成员" title="问成员" hidden></button>
|
|
121
|
+
</div>
|
|
122
|
+
<script src="/pdl/library/icons.js"></script>
|
|
123
|
+
<script src="/pdl/library/markdown-text.js"></script>
|
|
124
|
+
<script src="/pdl/platform/web-links.js"></script>
|
|
125
|
+
<script src="/app/document-drafts.js"></script>
|
|
126
|
+
<script src="/app/reference-labels.js"></script>
|
|
127
|
+
<script src="/app/text-anchor.js"></script>
|
|
128
|
+
<script type="module">
|
|
129
|
+
import { renderRichMarkdown } from '/pdl/platform/rich-markdown.mjs';
|
|
130
|
+
import { captureRange, locatorFromParam, rangeFor } from './anchors.js';
|
|
131
|
+
import { documentEdit } from './document-edit.js';
|
|
132
|
+
import { keepReadingPosition } from './reading-position.js';
|
|
133
|
+
|
|
134
|
+
const q = new URLSearchParams(location.search), topic = q.get('topic');
|
|
135
|
+
const annotationId = q.get('annotation'), linkedLocator = locatorFromParam(q.get('locator'));
|
|
136
|
+
let resource = q.get('resource'), linkedReference, capabilities = {};
|
|
137
|
+
const api = '/api/topics/' + encodeURIComponent(topic) + '/works';
|
|
138
|
+
const stage = document.getElementById('stage'), $ = (id) => document.getElementById(id);
|
|
139
|
+
let documentTools, viewerEvents, fileEditor, documentReadOnly = true, toolsRevision = 0;
|
|
140
|
+
const dispatchedOpen = location.hash === '#dispatched-open';
|
|
141
|
+
if (dispatchedOpen) {
|
|
142
|
+
q.delete('locator');
|
|
143
|
+
history.replaceState(history.state, '', location.pathname + '?' + q);
|
|
144
|
+
}
|
|
145
|
+
async function observe(event, locator, payload) {
|
|
146
|
+
const response = await fetch(api + '/events', {
|
|
147
|
+
method: 'POST', headers: { 'content-type': 'application/json' },
|
|
148
|
+
body: JSON.stringify({ resource, event, locator, payload }),
|
|
149
|
+
});
|
|
150
|
+
const data = await response.json();
|
|
151
|
+
if (!response.ok) throw new Error(data.error);
|
|
152
|
+
return data;
|
|
153
|
+
}
|
|
154
|
+
const say = (event, locator) => resource && observe(event, locator).catch((error) => console.debug('Work observation rejected:', error.message));
|
|
155
|
+
async function workLog(filter) {
|
|
156
|
+
const response = await fetch(api + '/ops?' + new URLSearchParams(filter));
|
|
157
|
+
const data = await response.json();
|
|
158
|
+
if (!response.ok) throw new Error(data.error);
|
|
159
|
+
return data;
|
|
160
|
+
}
|
|
161
|
+
async function readDocumentIndex() {
|
|
162
|
+
const response = await fetch('/api/console/pdl');
|
|
163
|
+
return response.ok ? response.json() : {};
|
|
164
|
+
}
|
|
165
|
+
async function documentLanguage() {
|
|
166
|
+
const { lang, packs } = await (await fetch('/api/i18n')).json();
|
|
167
|
+
document.documentElement.lang = lang === 'en' ? 'en' : 'zh-CN';
|
|
168
|
+
referenceLabelFallback = () => packs[lang].host.reference.open;
|
|
169
|
+
void referenceLabelsIn(stage);
|
|
170
|
+
return packs[lang].works.document;
|
|
171
|
+
}
|
|
172
|
+
async function readResource(address, locator) {
|
|
173
|
+
const params = new URLSearchParams({ resource: address, ...(locator ? { locator: JSON.stringify(locator) } : {}) });
|
|
174
|
+
const response = await fetch(api + '/resource?' + params);
|
|
175
|
+
const view = await response.json();
|
|
176
|
+
if (!response.ok) throw new Error(view.error);
|
|
177
|
+
return view;
|
|
178
|
+
}
|
|
179
|
+
async function annotationReference(id) {
|
|
180
|
+
const index = await readDocumentIndex();
|
|
181
|
+
if (Object.values(index).some(({ documentTools }) => documentTools?.references?.includes('annotation'))) {
|
|
182
|
+
const { resolveDocumentReference } = await import('/app/document-tools.mjs');
|
|
183
|
+
const resolved = await resolveDocumentReference(index, { kind: 'annotation', topicId: topic, id });
|
|
184
|
+
if (resolved) return resolved;
|
|
185
|
+
}
|
|
186
|
+
const [observation] = await workLog({ annotationId: id, limit: 1 });
|
|
187
|
+
if (observation?.resource) return { resource: observation.resource, locator: observation.locator };
|
|
188
|
+
}
|
|
189
|
+
const script = (src) => new Promise((ok, no) => { const s = document.createElement('script'); s.src = src; s.onload = ok; s.onerror = () => no(new Error('读不到 ' + src)); document.head.append(s); });
|
|
190
|
+
let goPage, shownPage, media, showSource, mode, anchorRoot = stage, pdfLayer, waitForPdfLayer;
|
|
191
|
+
const mediaCommands = new Set();
|
|
192
|
+
const standalone = window.parent === window;
|
|
193
|
+
let canQuote = standalone;
|
|
194
|
+
|
|
195
|
+
if (!standalone) {
|
|
196
|
+
let parentRoot;
|
|
197
|
+
try { parentRoot = window.parent.document.documentElement; }
|
|
198
|
+
catch { /* A cross-origin embed uses the system palette. */ }
|
|
199
|
+
if (parentRoot) {
|
|
200
|
+
canQuote = true;
|
|
201
|
+
const tokens = ['--bg-page', '--bg-raised', '--bg-code', '--fg', '--fg-muted', '--line', '--line-strong', '--accent', '--accent-soft', '--font-text', '--font-mono'];
|
|
202
|
+
const rootStyle = document.documentElement.style;
|
|
203
|
+
const sync = () => {
|
|
204
|
+
const parentStyle = window.parent.getComputedStyle(parentRoot);
|
|
205
|
+
for (const token of tokens) rootStyle.setProperty(token, parentStyle.getPropertyValue(token));
|
|
206
|
+
rootStyle.colorScheme = parentStyle.colorScheme;
|
|
207
|
+
};
|
|
208
|
+
const observer = new MutationObserver(sync);
|
|
209
|
+
const scheme = window.parent.matchMedia('(prefers-color-scheme: dark)');
|
|
210
|
+
observer.observe(parentRoot, { attributes: true, attributeFilter: ['data-theme', 'data-skin', 'style'] });
|
|
211
|
+
scheme.addEventListener('change', sync);
|
|
212
|
+
window.addEventListener('pagehide', () => {
|
|
213
|
+
observer.disconnect();
|
|
214
|
+
scheme.removeEventListener('change', sync);
|
|
215
|
+
}, { once: true });
|
|
216
|
+
sync();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const draftOwner = canQuote ? window.parent : window;
|
|
221
|
+
|
|
222
|
+
/* Render Markdown as text nodes and known elements. Local navigation uses the console bridge. */
|
|
223
|
+
const mdTokens = /\\(?<escaped>[\\`*_[\]<>|$])|(?<ticks>`+)(?<code>[^`]+)\k<ticks>|(?<image>!?)\[(?<label>[^\]\n]+)\]\((?<href><[^>\n]+>|(?:[^\s()]|\([^\s()]*\))+)\)|(?<![\w$])\$(?<formula>[^\s$](?:[^$]*?[^\s$])?)\$(?![\w$])|(?<lineBreak><br[ \t]*\/?>)|(?<marker>\*{1,3}|(?<!\w)_{1,3})(?=\S)(?<emphasis>.+?)(?<=\S)\k<marker>/g;
|
|
224
|
+
const mdItem = /^( {0,3})([-+*]|\d{1,9}[.)])([ \t]+)(.*)$/;
|
|
225
|
+
const mdQuote = /^ {0,3}> ?/;
|
|
226
|
+
const mdRule = /^ {0,3}(?:(?:\* *){3,}|(?:- *){3,}|(?:_ *){3,})$/;
|
|
227
|
+
const mdCells = (line) => line.trim().replace(/^\||(?<!\\)\|$/g, '').split(/(?<!\\)\|/).map((cell) => cell.trim());
|
|
228
|
+
|
|
229
|
+
function element(parent, tag, text = '') {
|
|
230
|
+
const node = document.createElement(tag);
|
|
231
|
+
node.textContent = text;
|
|
232
|
+
parent.append(node);
|
|
233
|
+
return node;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function mdRich(parent, kind, source, original = source, inline = false) {
|
|
237
|
+
const host = element(parent, inline ? 'span' : 'div');
|
|
238
|
+
host.className = 'md-rich';
|
|
239
|
+
if (inline) host.textContent = original;
|
|
240
|
+
else element(element(host, 'pre'), 'code', original);
|
|
241
|
+
void renderRichMarkdown(kind, source, inline).then((html) => {
|
|
242
|
+
// Keep the source in the document's text so drawing cannot shift saved selection offsets.
|
|
243
|
+
host.attachShadow({ mode: 'open' }).innerHTML = html;
|
|
244
|
+
}, () => { /* Keep the source visible if drawing fails. */ });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function mdRelativeImage(address, previewUrl) {
|
|
248
|
+
const base = previewUrl && URL.parse(previewUrl, location.origin);
|
|
249
|
+
const mount = base && /^\/p\/[\w-]+\/[\w-]+\//.exec(base.pathname)?.[0];
|
|
250
|
+
const url = mount && URL.parse(address, base);
|
|
251
|
+
if (!url || url.origin !== location.origin || !url.pathname.startsWith(mount)) return;
|
|
252
|
+
try {
|
|
253
|
+
const parts = url.pathname.slice(mount.length).split('/').map(decodeURIComponent);
|
|
254
|
+
if (parts.some((part) => !part || part === '.' || part === '..' || /[/\\\0]/.test(part))) return;
|
|
255
|
+
} catch { return; } // Malformed URL escaping in the Markdown source.
|
|
256
|
+
return url;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function mdRelativeFile(address, resource) {
|
|
260
|
+
const [kind, id, ...parts] = resource.split('/');
|
|
261
|
+
if (kind !== 'res:topic' && kind !== 'res:ref') return;
|
|
262
|
+
parts.pop();
|
|
263
|
+
const segments = address.split(/[?#]/, 1)[0].split('/');
|
|
264
|
+
try {
|
|
265
|
+
for (const [index, segment] of segments.entries()) {
|
|
266
|
+
const part = decodeURIComponent(segment);
|
|
267
|
+
if (!part || /[/\\\0]/.test(part)) return;
|
|
268
|
+
if (part === '.' || part === '..') {
|
|
269
|
+
if (index === segments.length - 1 || (part === '..' && parts.pop() === undefined)) return;
|
|
270
|
+
} else {
|
|
271
|
+
parts.push(encodeURIComponent(part));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
} catch { return; } // Malformed path escaping in the Markdown source.
|
|
275
|
+
return { topicId: kind === 'res:topic' ? id : topic, resource: [kind, id, ...parts].join('/') };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function mdResourceLink(address) {
|
|
279
|
+
const raw = /^res:(?:topic|ref)\//.test(address) ? address.replace(/(?:#L\d+(?:-L?\d+)?|:\d+(?:-\d+)?)$/, '') : address;
|
|
280
|
+
const [kind, id, ...rest] = raw.split('/');
|
|
281
|
+
if (kind !== 'res:topic' && kind !== 'res:ref' && kind !== 'res:work') return;
|
|
282
|
+
if (!id || !/^[\w-]+$/.test(id) || !rest.length || (kind === 'res:work' && rest.length !== 1)) return;
|
|
283
|
+
let parts;
|
|
284
|
+
try {
|
|
285
|
+
parts = rest.map(decodeURIComponent);
|
|
286
|
+
if (parts.some((part) => !part || /[/\\\0]/.test(part) || part === '.' || part === '..')) return;
|
|
287
|
+
} catch { return; } // Malformed path escaping in the Markdown source.
|
|
288
|
+
const topicId = kind === 'res:ref' ? topic : id;
|
|
289
|
+
if (!topicId) return;
|
|
290
|
+
return { topicId, resource: [kind, id, ...parts.map(encodeURIComponent)].join('/') };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function mdLink(parent, token, view) {
|
|
294
|
+
const { image, label, href } = token.groups;
|
|
295
|
+
const address = href.replace(/^<|>$/g, '');
|
|
296
|
+
const path = image ? address : address.replace(/(?:#L\d+(?:-L?\d+)?|:\d+(?:-\d+)?)$/, '');
|
|
297
|
+
const relative = path && !/^(?:[a-z][a-z\d+.-]*:|[/\\#~])/i.test(path);
|
|
298
|
+
let url, file;
|
|
299
|
+
if (!image && /^res:(?:topic|ref|work)\//.test(address)) file = mdResourceLink(address);
|
|
300
|
+
else if (!relative) url = URL.parse(address, location.href);
|
|
301
|
+
else if (image) url = mdRelativeImage(address, view.url);
|
|
302
|
+
else file = mdRelativeFile(path, view.resource);
|
|
303
|
+
if (!url && !file) return parent.append(token[0]);
|
|
304
|
+
const web = url && !address.startsWith('#') && /^https?:$/.test(url.protocol);
|
|
305
|
+
if (image) {
|
|
306
|
+
if (!web || url.origin === location.origin && !url.pathname.startsWith('/p/')) return parent.append(token[0]);
|
|
307
|
+
const picture = element(parent, 'img');
|
|
308
|
+
picture.alt = label;
|
|
309
|
+
picture.src = url.href;
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
const embedded = !standalone && canQuote;
|
|
313
|
+
const localFile = !relative && embedded && /^(file:|\/(?!\/)|~\/)/i.test(address);
|
|
314
|
+
if (url && !localFile && !/^(https?|mailto):$/.test(url.protocol)) return parent.append(token[0]);
|
|
315
|
+
const reference = referenceLabelTarget(address, label);
|
|
316
|
+
const link = element(parent, 'a');
|
|
317
|
+
if (reference) element(link, 'span', referenceLabelFallback()).dataset.referenceLabel = reference;
|
|
318
|
+
else mdInline(link, label, view);
|
|
319
|
+
if (file) {
|
|
320
|
+
if (!embedded) {
|
|
321
|
+
link.href = '/works/view.html?' + new URLSearchParams({ topic: file.topicId, resource: file.resource });
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const source = encodeURIComponent(JSON.stringify({ resource: file.resource }))
|
|
325
|
+
.replace(/[!'()*]/g, (c) => '%' + c.charCodeAt(0).toString(16));
|
|
326
|
+
const href = '#chat-' + file.topicId + '?source=' + source;
|
|
327
|
+
link.href = '/' + href;
|
|
328
|
+
link.onclick = (event) => {
|
|
329
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
330
|
+
event.preventDefault();
|
|
331
|
+
window.parent.postMessage({ channel: 'hostlet-frame/v1', type: 'nav.open', topicId: topic, href }, location.origin);
|
|
332
|
+
};
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
link.href = localFile ? '#' : address.startsWith('#') ? '/' + address : url.href;
|
|
336
|
+
const target = web && webLinkTarget(url);
|
|
337
|
+
const navigation = localFile ? { file: address }
|
|
338
|
+
: embedded && (address.startsWith('#') || (target && target !== 'host')) ? { href: web ? url.href : address } : null;
|
|
339
|
+
if (!localFile && web) {
|
|
340
|
+
if (standalone && target === 'preview') link.href = '/#chat-' + topic + '?' + new URLSearchParams({ preview: url.href });
|
|
341
|
+
link.target = '_blank';
|
|
342
|
+
link.rel = 'noopener noreferrer';
|
|
343
|
+
}
|
|
344
|
+
const opensBrowser = standalone && target === 'browser';
|
|
345
|
+
if (navigation || opensBrowser) link.onclick = (event) => {
|
|
346
|
+
if (web && (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)) return;
|
|
347
|
+
event.preventDefault();
|
|
348
|
+
if (navigation) window.parent.postMessage({
|
|
349
|
+
channel: 'hostlet-frame/v1', type: web && navigation.href ? 'nav.link' : 'nav.open', topicId: topic, ...navigation,
|
|
350
|
+
}, location.origin);
|
|
351
|
+
else webOpenExternal(url).catch((error) => { $('notice').textContent = error.message; });
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function mdInline(parent, text, view) {
|
|
356
|
+
let at = 0;
|
|
357
|
+
for (const token of text.matchAll(mdTokens)) {
|
|
358
|
+
parent.append(text.slice(at, token.index));
|
|
359
|
+
const { escaped, code, href, formula, lineBreak, marker, emphasis } = token.groups;
|
|
360
|
+
if (escaped) parent.append(escaped);
|
|
361
|
+
else if (code) element(parent, 'code', code);
|
|
362
|
+
else if (formula) mdRich(parent, 'math', formula, token[0], true);
|
|
363
|
+
else if (lineBreak) element(parent, 'br');
|
|
364
|
+
else if (href) mdLink(parent, token, view);
|
|
365
|
+
else {
|
|
366
|
+
const node = element(parent, marker.length === 1 ? 'em' : 'strong');
|
|
367
|
+
mdInline(marker.length === 3 ? element(node, 'em') : node, emphasis, view);
|
|
368
|
+
}
|
|
369
|
+
at = token.index + token[0].length;
|
|
370
|
+
}
|
|
371
|
+
parent.append(text.slice(at));
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function mdBlocks(parent, lines, view) {
|
|
375
|
+
let paragraph = [];
|
|
376
|
+
const flush = () => {
|
|
377
|
+
if (paragraph.length) mdInline(element(parent, 'p'), paragraph.join(' '), view);
|
|
378
|
+
paragraph = [];
|
|
379
|
+
};
|
|
380
|
+
for (let i = 0; i < lines.length;) {
|
|
381
|
+
const line = lines[i++];
|
|
382
|
+
const fence = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
383
|
+
const formula = /^ {0,3}\$\$(?:(.+?)\$\$)?[ \t]*$/.exec(line);
|
|
384
|
+
let mathEnd = i - 1;
|
|
385
|
+
if (formula && formula[1] === undefined) {
|
|
386
|
+
mathEnd = i;
|
|
387
|
+
while (mathEnd < lines.length && lines[mathEnd].trim() !== '$$') mathEnd++;
|
|
388
|
+
}
|
|
389
|
+
const mathBlock = formula && mathEnd < lines.length;
|
|
390
|
+
const heading = /^ {0,3}(#{1,6})(?:[ \t]+|$)(.*)$/.exec(line);
|
|
391
|
+
const item = mdItem.exec(line);
|
|
392
|
+
const quote = mdQuote.test(line);
|
|
393
|
+
const rule = mdRule.test(line);
|
|
394
|
+
const header = line.includes('|') ? mdCells(line) : [];
|
|
395
|
+
const separator = header.length ? mdCells(lines[i] ?? '') : [];
|
|
396
|
+
const table = header.length > 0 && separator.length === header.length && separator.every((cell) => /^:?-{3,}:?$/.test(cell));
|
|
397
|
+
if (line.trim() && !fence && !mathBlock && !heading && !rule && !quote && !item && !table) {
|
|
398
|
+
paragraph.push(line.trim());
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
flush();
|
|
402
|
+
if (fence) {
|
|
403
|
+
const end = new RegExp('^ {0,3}' + fence[1][0] + '{' + fence[1].length + ',}\\s*$');
|
|
404
|
+
const start = i;
|
|
405
|
+
while (i < lines.length && !end.test(lines[i])) i++;
|
|
406
|
+
const source = lines.slice(start, i).join('\n');
|
|
407
|
+
if (i < lines.length && fence[2].trim() === 'mermaid') mdRich(parent, 'mermaid', source);
|
|
408
|
+
else element(element(parent, 'pre'), 'code', source);
|
|
409
|
+
if (i < lines.length) i++;
|
|
410
|
+
} else if (mathBlock) {
|
|
411
|
+
const source = formula[1]?.trim() ?? lines.slice(i, mathEnd).join('\n');
|
|
412
|
+
mdRich(parent, 'math', source, lines.slice(i - 1, mathEnd + 1).join('\n'));
|
|
413
|
+
i = mathEnd + 1;
|
|
414
|
+
} else if (heading) {
|
|
415
|
+
mdInline(element(parent, 'h' + heading[1].length), heading[2].replace(/[ \t]+#+[ \t]*$/, ''), view);
|
|
416
|
+
} else if (rule) {
|
|
417
|
+
element(parent, 'hr');
|
|
418
|
+
} else if (quote) {
|
|
419
|
+
const quoted = [line.replace(mdQuote, '')];
|
|
420
|
+
while (mdQuote.test(lines[i] ?? '')) quoted.push(lines[i++].replace(mdQuote, ''));
|
|
421
|
+
mdBlocks(element(parent, 'blockquote'), quoted, view);
|
|
422
|
+
} else if (item) {
|
|
423
|
+
const ordered = /^\d/.test(item[2]);
|
|
424
|
+
const list = element(parent, ordered ? 'ol' : 'ul');
|
|
425
|
+
if (ordered) list.start = parseInt(item[2], 10);
|
|
426
|
+
let next = item;
|
|
427
|
+
do {
|
|
428
|
+
const indent = ' '.repeat(next[1].length + next[2].length + next[3].length);
|
|
429
|
+
const body = [next[4]];
|
|
430
|
+
while (lines[i]?.startsWith(indent) || (lines[i] === '' && lines[i + 1]?.startsWith(indent))) {
|
|
431
|
+
body.push(lines[i++].slice(indent.length));
|
|
432
|
+
}
|
|
433
|
+
mdBlocks(element(list, 'li'), body, view);
|
|
434
|
+
next = mdItem.exec(lines[i] ?? '');
|
|
435
|
+
if (!next || next[2].at(-1) !== item[2].at(-1) || mdRule.test(lines[i])) break;
|
|
436
|
+
i++;
|
|
437
|
+
} while (next);
|
|
438
|
+
} else if (table) {
|
|
439
|
+
const grid = element(parent, 'table');
|
|
440
|
+
const head = element(element(grid, 'thead'), 'tr');
|
|
441
|
+
for (const cell of header) mdInline(element(head, 'th'), cell, view);
|
|
442
|
+
const body = element(grid, 'tbody');
|
|
443
|
+
i++;
|
|
444
|
+
while (lines[i]?.includes('|')) {
|
|
445
|
+
const cells = mdCells(lines[i++]), row = element(body, 'tr');
|
|
446
|
+
for (let col = 0; col < header.length; col++) mdInline(element(row, 'td'), cells[col] ?? '', view);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
flush();
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const pageControls = (count, move) => {
|
|
454
|
+
for (const id of ['prev', 'next', 'pos']) $(id).hidden = false;
|
|
455
|
+
$('prev').onclick = () => move(-1); $('next').onclick = () => move(1);
|
|
456
|
+
return (at) => { shownPage = at; $('pos').textContent = at + ' / ' + count; };
|
|
457
|
+
};
|
|
458
|
+
const pager = (count, show) => {
|
|
459
|
+
let at = 1;
|
|
460
|
+
const label = pageControls(count, (step) => goPage(at + step));
|
|
461
|
+
goPage = async (n, told) => {
|
|
462
|
+
const page = at = Math.max(1, Math.min(count, Math.trunc(n)));
|
|
463
|
+
label(page);
|
|
464
|
+
await show(page);
|
|
465
|
+
documentTools?.update();
|
|
466
|
+
if (!told) say('goto', { page });
|
|
467
|
+
};
|
|
468
|
+
return goPage(1, true);
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
function sourceMode(rendered, text, renderedLabel, toggleVisible = true) {
|
|
472
|
+
const source = element(stage, 'pre', text), toggle = $('md-mode');
|
|
473
|
+
toggle.hidden = !toggleVisible;
|
|
474
|
+
showSource = (on) => {
|
|
475
|
+
mode = on ? 'source' : 'rendered';
|
|
476
|
+
source.hidden = !on;
|
|
477
|
+
rendered.hidden = on;
|
|
478
|
+
anchorRoot = on ? source : rendered;
|
|
479
|
+
toggle.textContent = on ? renderedLabel : '源文件';
|
|
480
|
+
CSS.highlights?.delete('work-location');
|
|
481
|
+
documentTools?.update();
|
|
482
|
+
};
|
|
483
|
+
toggle.onclick = () => showSource(source.hidden);
|
|
484
|
+
showSource(false);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function delimitedRows(text, separator) {
|
|
488
|
+
const rows = [];
|
|
489
|
+
let row = [], field = '', quoted = false, rowStart = 0;
|
|
490
|
+
for (let i = 0; i < text.length; i++) {
|
|
491
|
+
const ch = text[i];
|
|
492
|
+
if (quoted) {
|
|
493
|
+
if (ch !== '"') field += ch;
|
|
494
|
+
else if (text[i + 1] === '"') { field += '"'; i++; }
|
|
495
|
+
else quoted = false;
|
|
496
|
+
} else if (ch === '"' && !field) quoted = true;
|
|
497
|
+
else if (ch === separator) {
|
|
498
|
+
row.push(field);
|
|
499
|
+
field = '';
|
|
500
|
+
} else if (ch === '\r' || ch === '\n') {
|
|
501
|
+
row.push(field);
|
|
502
|
+
rows.push(row);
|
|
503
|
+
row = [];
|
|
504
|
+
field = '';
|
|
505
|
+
if (ch === '\r' && text[i + 1] === '\n') i++;
|
|
506
|
+
rowStart = i + 1;
|
|
507
|
+
} else field += ch;
|
|
508
|
+
}
|
|
509
|
+
if (quoted) throw new Error('表格字段的引号未闭合');
|
|
510
|
+
if (rowStart < text.length) {
|
|
511
|
+
row.push(field);
|
|
512
|
+
rows.push(row);
|
|
513
|
+
}
|
|
514
|
+
return rows;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function columnName(index) {
|
|
518
|
+
let name = '';
|
|
519
|
+
for (let n = index + 1; n > 0; n = Math.floor((n - 1) / 26)) name = String.fromCharCode(65 + (n - 1) % 26) + name;
|
|
520
|
+
return name;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function cellText(value) {
|
|
524
|
+
if (value == null) return '';
|
|
525
|
+
if (typeof value === 'boolean') return value ? 'TRUE' : 'FALSE';
|
|
526
|
+
if (value instanceof Date) {
|
|
527
|
+
const iso = value.toISOString();
|
|
528
|
+
return iso.endsWith('T00:00:00.000Z') ? iso.slice(0, 10) : iso.replace('T', ' ').replace(/(?:\.000)?Z$/, '');
|
|
529
|
+
}
|
|
530
|
+
return String(value);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const SHEET_ROW_LIMIT = 2000;
|
|
534
|
+
function showSheet(view, rows) {
|
|
535
|
+
view.replaceChildren();
|
|
536
|
+
const count = rows.length;
|
|
537
|
+
$('sheet-rows').textContent = count + ' 行' + (count > SHEET_ROW_LIMIT ? ' · 显示前 ' + SHEET_ROW_LIMIT + ' 行' : '');
|
|
538
|
+
if (!count) {
|
|
539
|
+
element(view, 'p', '这张工作表没有数据');
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const preview = rows.slice(0, SHEET_ROW_LIMIT);
|
|
543
|
+
const columns = Math.max(...preview.map((row) => row.length));
|
|
544
|
+
const table = element(view, 'table');
|
|
545
|
+
table.className = 'sheet';
|
|
546
|
+
const head = element(element(table, 'thead'), 'tr');
|
|
547
|
+
element(head, 'th').setAttribute('aria-label', '行号');
|
|
548
|
+
for (let col = 0; col < columns; col++) element(head, 'th', columnName(col)).scope = 'col';
|
|
549
|
+
const body = element(table, 'tbody');
|
|
550
|
+
for (const [index, row] of preview.entries()) {
|
|
551
|
+
const line = element(body, 'tr');
|
|
552
|
+
element(line, 'th', index + 1).scope = 'row';
|
|
553
|
+
for (let col = 0; col < columns; col++) {
|
|
554
|
+
const value = row[col];
|
|
555
|
+
const cell = element(line, 'td', cellText(value));
|
|
556
|
+
if (typeof value === 'number') cell.className = 'number';
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
async function showSpreadsheet(content, ext) {
|
|
562
|
+
const response = await fetch(content);
|
|
563
|
+
if (!response.ok) throw new Error('读不到表格文件(HTTP ' + response.status + ')');
|
|
564
|
+
let sheets, source;
|
|
565
|
+
if (ext === 'xlsx') {
|
|
566
|
+
await script('/works/lib/read-excel-file/read-excel-file.min.js');
|
|
567
|
+
sheets = await window.readXlsxFile(await response.blob());
|
|
568
|
+
} else {
|
|
569
|
+
source = await response.text();
|
|
570
|
+
sheets = [{ data: delimitedRows(source, ext === 'tsv' ? '\t' : ',') }];
|
|
571
|
+
}
|
|
572
|
+
const view = element(stage, 'div');
|
|
573
|
+
if (source !== undefined) sourceMode(view, source, '表格');
|
|
574
|
+
if (sheets.length < 2) {
|
|
575
|
+
showSheet(view, sheets[0]?.data ?? []);
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const picker = $('sheet-picker');
|
|
579
|
+
for (const [index, sheet] of sheets.entries()) {
|
|
580
|
+
const page = index + 1;
|
|
581
|
+
element(picker, 'option', sheet.sheet).value = page;
|
|
582
|
+
}
|
|
583
|
+
picker.hidden = false;
|
|
584
|
+
picker.onchange = () => goPage(Number(picker.value));
|
|
585
|
+
await pager(sheets.length, (page) => {
|
|
586
|
+
picker.value = page;
|
|
587
|
+
showSheet(view, sheets[page - 1].data);
|
|
588
|
+
stage.scrollTo(0, 0);
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
async function showPdf(content) {
|
|
593
|
+
const base = '/works/lib/pdfjs/';
|
|
594
|
+
const stylesheet = document.createElement('link');
|
|
595
|
+
stylesheet.rel = 'stylesheet';
|
|
596
|
+
stylesheet.href = base + 'web/pdf_viewer.css';
|
|
597
|
+
const styled = new Promise((resolve, reject) => {
|
|
598
|
+
stylesheet.onload = resolve;
|
|
599
|
+
stylesheet.onerror = () => reject(new Error('读不到 PDF 查看器样式'));
|
|
600
|
+
});
|
|
601
|
+
document.head.append(stylesheet);
|
|
602
|
+
const [{ getDocument, GlobalWorkerOptions, AnnotationMode, AnnotationEditorType }] = await Promise.all([
|
|
603
|
+
import(base + 'build/pdf.mjs'), styled,
|
|
604
|
+
]);
|
|
605
|
+
const { EventBus, PDFLinkService, PDFViewer } = await import(base + 'web/pdf_viewer.mjs');
|
|
606
|
+
GlobalWorkerOptions.workerSrc = base + 'build/pdf.worker.mjs';
|
|
607
|
+
|
|
608
|
+
stage.classList.add('pdf');
|
|
609
|
+
const container = element(stage, 'div');
|
|
610
|
+
container.className = 'pdf-scroll';
|
|
611
|
+
container.tabIndex = 0;
|
|
612
|
+
container.setAttribute('aria-label', 'PDF 正文');
|
|
613
|
+
const pages = element(container, 'div');
|
|
614
|
+
pages.className = 'pdfViewer';
|
|
615
|
+
const eventBus = new EventBus();
|
|
616
|
+
const linkService = new PDFLinkService({ eventBus });
|
|
617
|
+
linkService.externalLinkEnabled = false;
|
|
618
|
+
const lifetime = new AbortController();
|
|
619
|
+
const viewer = new PDFViewer({
|
|
620
|
+
container, viewer: pages, eventBus, linkService, abortSignal: lifetime.signal,
|
|
621
|
+
annotationMode: AnnotationMode.ENABLE, annotationEditorMode: AnnotationEditorType.DISABLE,
|
|
622
|
+
imageResourcesPath: base + 'web/images/',
|
|
623
|
+
});
|
|
624
|
+
linkService.setViewer(viewer);
|
|
625
|
+
const loading = getDocument({
|
|
626
|
+
url: content, cMapUrl: base + 'cmaps/', iccUrl: base + 'iccs/',
|
|
627
|
+
standardFontDataUrl: base + 'standard_fonts/', wasmUrl: base + 'wasm/',
|
|
628
|
+
});
|
|
629
|
+
const fit = () => { if (container.clientWidth) viewer.currentScaleValue = 'page-width'; };
|
|
630
|
+
const resize = new ResizeObserver(fit);
|
|
631
|
+
window.addEventListener('pagehide', (event) => {
|
|
632
|
+
if (event.persisted) return;
|
|
633
|
+
resize.disconnect();
|
|
634
|
+
viewer.setDocument(null);
|
|
635
|
+
lifetime.abort();
|
|
636
|
+
void loading.destroy();
|
|
637
|
+
});
|
|
638
|
+
const pdf = await loading.promise;
|
|
639
|
+
const initialized = new Promise((resolve) => eventBus.on('pagesinit', resolve, { once: true }));
|
|
640
|
+
linkService.setDocument(pdf);
|
|
641
|
+
viewer.setDocument(pdf);
|
|
642
|
+
await Promise.race([initialized, viewer.pagesPromise]);
|
|
643
|
+
fit();
|
|
644
|
+
resize.observe(container);
|
|
645
|
+
let commandedPage;
|
|
646
|
+
goPage = (page, told) => {
|
|
647
|
+
const next = Math.max(1, Math.min(pdf.numPages, Math.trunc(page)));
|
|
648
|
+
if (told && viewer.currentPageNumber !== next) commandedPage = next;
|
|
649
|
+
viewer.currentPageNumber = next;
|
|
650
|
+
};
|
|
651
|
+
const label = pageControls(pdf.numPages, (step) => goPage(viewer.currentPageNumber + step));
|
|
652
|
+
label(viewer.currentPageNumber);
|
|
653
|
+
pdfLayer = (page) => viewer.getPageView(page - 1)?.textLayer?.div;
|
|
654
|
+
waitForPdfLayer = (page) => {
|
|
655
|
+
if (pdfLayer(page)?.childElementCount) return Promise.resolve();
|
|
656
|
+
return new Promise((resolve) => {
|
|
657
|
+
const displayed = ({ pageNumber }) => {
|
|
658
|
+
if (pageNumber !== page) return;
|
|
659
|
+
clearTimeout(timer);
|
|
660
|
+
eventBus.off('textlayerrendered', displayed);
|
|
661
|
+
resolve();
|
|
662
|
+
};
|
|
663
|
+
const timer = setTimeout(() => displayed({ pageNumber: page }), 3000);
|
|
664
|
+
eventBus.on('textlayerrendered', displayed);
|
|
665
|
+
});
|
|
666
|
+
};
|
|
667
|
+
eventBus.on('pagechanging', ({ pageNumber }) => {
|
|
668
|
+
label(pageNumber);
|
|
669
|
+
if (pageNumber !== commandedPage) say('goto', { page: pageNumber });
|
|
670
|
+
commandedPage = undefined;
|
|
671
|
+
documentTools?.update();
|
|
672
|
+
});
|
|
673
|
+
eventBus.on('textlayerrendered', () => documentTools?.update());
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
let mediaReady, sourceReady = false;
|
|
677
|
+
async function renderViewer({ content, open: { renderer }, ...view }) {
|
|
678
|
+
$('title').textContent = view.title;
|
|
679
|
+
if (renderer === 'commit') element(stage, 'pre', (await (await fetch(content)).json()).text);
|
|
680
|
+
else if (renderer === 'note') element(stage, 'pre', view.text);
|
|
681
|
+
else if (renderer === 'markdown') {
|
|
682
|
+
const text = await (await fetch(content)).text();
|
|
683
|
+
const article = element(stage, 'article');
|
|
684
|
+
article.className = 'markdown';
|
|
685
|
+
mdBlocks(article, text.split(/\r\n?|\n/), view);
|
|
686
|
+
sourceMode(article, text, '渲染', false);
|
|
687
|
+
await referenceLabelsIn(article);
|
|
688
|
+
} else if (renderer === 'xlsx' || renderer === 'csv' || renderer === 'tsv') {
|
|
689
|
+
await showSpreadsheet(content, renderer);
|
|
690
|
+
} else if (renderer === 'image') {
|
|
691
|
+
const button = element(stage, 'button');
|
|
692
|
+
button.className = 'image-size';
|
|
693
|
+
button.setAttribute('aria-label', '原尺寸');
|
|
694
|
+
button.setAttribute('aria-pressed', 'false');
|
|
695
|
+
const image = element(button, 'img');
|
|
696
|
+
image.src = content;
|
|
697
|
+
image.alt = $('title').textContent;
|
|
698
|
+
button.onclick = () => button.setAttribute('aria-pressed', String(button.getAttribute('aria-pressed') === 'false'));
|
|
699
|
+
await image.decode();
|
|
700
|
+
// A ratio-only SVG needs a definite size inside the shrink-to-fit button.
|
|
701
|
+
// Its natural width can round to zero for a very tall viewBox.
|
|
702
|
+
if (image.naturalWidth) image.width = image.naturalWidth;
|
|
703
|
+
else image.height = image.naturalHeight;
|
|
704
|
+
} else if (renderer === 'audio' || renderer === 'video') {
|
|
705
|
+
media = document.createElement(renderer);
|
|
706
|
+
media.controls = true;
|
|
707
|
+
media.preload = 'metadata';
|
|
708
|
+
mediaReady = new Promise((resolve) => {
|
|
709
|
+
media.addEventListener('loadedmetadata', () => resolve(true), { once: true });
|
|
710
|
+
media.addEventListener('error', () => resolve(false), { once: true });
|
|
711
|
+
});
|
|
712
|
+
media.src = content;
|
|
713
|
+
stage.append(media);
|
|
714
|
+
for (const [name, action] of [['play', 'play'], ['pause', 'pause'], ['seeked', 'goto']]) {
|
|
715
|
+
media.addEventListener(name, () => {
|
|
716
|
+
if (!mediaCommands.delete(action)) say(action, { time: media.currentTime });
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
} else if (renderer === 'pdf') {
|
|
720
|
+
await showPdf(content);
|
|
721
|
+
} else if (renderer === 'docx') {
|
|
722
|
+
await script('/works/lib/jszip/jszip.min.js'); await script('/works/lib/docx-preview/docx-preview.min.js');
|
|
723
|
+
await window.docx.renderAsync(await (await fetch(content)).arrayBuffer(), stage);
|
|
724
|
+
} else if (renderer === 'pptx') {
|
|
725
|
+
const { Presentation } = await import('/works/lib/pptx-wasm/index.js');
|
|
726
|
+
const deck = await Presentation.open(await (await fetch(content)).arrayBuffer());
|
|
727
|
+
const canvas = document.createElement('canvas'); stage.append(canvas);
|
|
728
|
+
const notes = document.createElement('pre'); notes.style.marginTop = '8px'; stage.append(notes);
|
|
729
|
+
const size = deck.slideSize;
|
|
730
|
+
let page = 1, renderedPage = 0, renderedWidth = 0, drawing = Promise.resolve();
|
|
731
|
+
canvas.style.width = '100%';
|
|
732
|
+
const draw = async () => {
|
|
733
|
+
const width = Math.min(1600, Math.round(stage.clientWidth * devicePixelRatio)), at = page;
|
|
734
|
+
if (!width || (width === renderedWidth && at === renderedPage)) return;
|
|
735
|
+
canvas.width = width;
|
|
736
|
+
canvas.height = Math.round(width * size.height / size.width);
|
|
737
|
+
await deck.render(at - 1, canvas, { fit: 'contain' });
|
|
738
|
+
renderedPage = at;
|
|
739
|
+
renderedWidth = width;
|
|
740
|
+
notes.textContent = deck.text(at - 1);
|
|
741
|
+
$('notice').textContent = '画面下面是这一页的文字,可以选中';
|
|
742
|
+
};
|
|
743
|
+
const show = (n) => {
|
|
744
|
+
page = n;
|
|
745
|
+
drawing = drawing.then(draw, draw);
|
|
746
|
+
return drawing;
|
|
747
|
+
};
|
|
748
|
+
await pager(deck.slideCount, show);
|
|
749
|
+
const resize = new ResizeObserver(() => { void show(page).catch((error) => { $('notice').textContent = '打不开:' + error.message; }); });
|
|
750
|
+
resize.observe(stage);
|
|
751
|
+
window.addEventListener('pagehide', () => resize.disconnect(), { once: true });
|
|
752
|
+
} else if (renderer === 'binary') {
|
|
753
|
+
const download = element(stage, 'a', '下载原件');
|
|
754
|
+
download.href = content;
|
|
755
|
+
download.download = view.title;
|
|
756
|
+
} else {
|
|
757
|
+
const response = await fetch(content);
|
|
758
|
+
if (!response.ok) throw new Error('读取文件失败(' + response.status + ')');
|
|
759
|
+
mode = 'source';
|
|
760
|
+
element(stage, 'pre', await response.text());
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
try {
|
|
765
|
+
if (annotationId) {
|
|
766
|
+
linkedReference = await annotationReference(annotationId);
|
|
767
|
+
if (!linkedReference) {
|
|
768
|
+
if (!resource) throw new Error('这个批注不在了');
|
|
769
|
+
$('reference-status').textContent = '这个批注不在了';
|
|
770
|
+
$('reference-status').hidden = false;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
resource = linkedReference?.resource ?? resource;
|
|
774
|
+
if (linkedReference?.available === false) throw new Error('这份原文不在了');
|
|
775
|
+
if (!resource) throw new Error('需要资源地址');
|
|
776
|
+
const locator = linkedReference?.locator ?? linkedLocator;
|
|
777
|
+
const view = await readResource(resource, locator ?? (linkedReference ? {} : undefined));
|
|
778
|
+
capabilities = view.capabilities;
|
|
779
|
+
if (view.open.renderer) {
|
|
780
|
+
const [words] = await Promise.all([documentLanguage(), renderViewer(view)]);
|
|
781
|
+
sourceReady = true;
|
|
782
|
+
if (view.reveal) {
|
|
783
|
+
$('file-more').innerHTML = icon('more', 16);
|
|
784
|
+
fileEditor = documentEdit({
|
|
785
|
+
topic, resource, view, words, windowWithDrafts: draftOwner,
|
|
786
|
+
onMode: () => { documentTools?.update(); positionQuote(); },
|
|
787
|
+
async refreshReading() {
|
|
788
|
+
const current = await readResource(resource, locator);
|
|
789
|
+
stage.replaceChildren();
|
|
790
|
+
await renderViewer(current);
|
|
791
|
+
if (locator) await restoreLocator(locator);
|
|
792
|
+
documentTools?.update();
|
|
793
|
+
},
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
} else {
|
|
797
|
+
location.replace(view.open.initialUrl ?? view.open.url);
|
|
798
|
+
}
|
|
799
|
+
} catch (e) {
|
|
800
|
+
stage.classList.remove('pdf');
|
|
801
|
+
stage.replaceChildren();
|
|
802
|
+
element(stage, 'pre', '打不开:' + (e && e.message || e));
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function currentLocator() {
|
|
806
|
+
return {
|
|
807
|
+
...(shownPage !== undefined ? { page: shownPage } : {}),
|
|
808
|
+
...(media ? { time: media.currentTime } : {}),
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
function selectedLocator({ text, range }) {
|
|
812
|
+
const start = range.startContainer;
|
|
813
|
+
const layer = pdfLayer && (start.nodeType === Node.ELEMENT_NODE ? start : start.parentElement)?.closest('.textLayer');
|
|
814
|
+
const page = layer ? Number(layer.closest('.page').dataset.pageNumber) : shownPage;
|
|
815
|
+
const exact = captureRange(pdfLayer ? layer : anchorRoot, range);
|
|
816
|
+
return { ...currentLocator(), ...(page ? { page } : {}), text: text.slice(0, 500), ...(mode ? { view: mode } : {}), ...(exact ? { range: exact } : {}) };
|
|
817
|
+
}
|
|
818
|
+
const matchesPage = (locator) => !Number.isInteger(locator.page) || shownPage === locator.page;
|
|
819
|
+
function highlight(name, ranges) {
|
|
820
|
+
if (!CSS.highlights) return;
|
|
821
|
+
if (ranges.length) CSS.highlights.set(name, new Highlight(...ranges));
|
|
822
|
+
else CSS.highlights.delete(name);
|
|
823
|
+
}
|
|
824
|
+
function documentRange(locator) {
|
|
825
|
+
if (locator.view && locator.view !== mode) return null;
|
|
826
|
+
if (!pdfLayer && !matchesPage(locator)) return null;
|
|
827
|
+
return rangeFor(pdfLayer ? pdfLayer(locator.page ?? shownPage) : anchorRoot, locator);
|
|
828
|
+
}
|
|
829
|
+
async function seek(time) {
|
|
830
|
+
if (!media || !Number.isFinite(time) || time < 0 || !await mediaReady) return false;
|
|
831
|
+
if (media.currentTime !== time) {
|
|
832
|
+
mediaCommands.add('goto');
|
|
833
|
+
media.currentTime = time;
|
|
834
|
+
}
|
|
835
|
+
return time <= media.duration;
|
|
836
|
+
}
|
|
837
|
+
function locationLabel(locator) {
|
|
838
|
+
return [
|
|
839
|
+
Number.isInteger(locator?.page) && locator.page > 0 && '第 ' + locator.page + ' 页',
|
|
840
|
+
Number.isFinite(locator?.time) && locator.time >= 0 && Math.round(locator.time * 10) / 10 + ' 秒',
|
|
841
|
+
typeof locator?.text === 'string' && locator.text && '「' + locator.text + '」',
|
|
842
|
+
].filter(Boolean).join(' · ');
|
|
843
|
+
}
|
|
844
|
+
async function restoreLocator(locator, status = $('reference-status')) {
|
|
845
|
+
fileEditor?.read();
|
|
846
|
+
const page = Number.isInteger(locator.page) && locator.page > 0 ? locator.page : undefined;
|
|
847
|
+
const time = Number.isFinite(locator.time) && locator.time >= 0 ? locator.time : undefined;
|
|
848
|
+
const hasText = typeof locator.text === 'string' && !!locator.text;
|
|
849
|
+
if (showSource && (locator.view === 'source' || locator.view === 'rendered')) showSource(locator.view === 'source');
|
|
850
|
+
if (page !== undefined && goPage) await goPage(page, true);
|
|
851
|
+
const timeFound = time === undefined || await seek(time);
|
|
852
|
+
const pageFound = page === undefined || matchesPage(locator);
|
|
853
|
+
if (hasText && pageFound && waitForPdfLayer) await waitForPdfLayer(page ?? shownPage);
|
|
854
|
+
const range = pageFound && rangeFor(pdfLayer ? pdfLayer(page ?? shownPage) : anchorRoot, locator);
|
|
855
|
+
highlight('work-location', range ? [range] : []);
|
|
856
|
+
if (range) {
|
|
857
|
+
const rect = range.getBoundingClientRect();
|
|
858
|
+
const scroll = range.startContainer.parentElement.closest('.pdf-scroll') ?? stage;
|
|
859
|
+
const box = scroll.getBoundingClientRect();
|
|
860
|
+
scroll.scrollBy({ top: rect.top - box.top - scroll.clientHeight / 3, left: rect.left < box.left || rect.right > box.right ? rect.left - box.left : 0 });
|
|
861
|
+
}
|
|
862
|
+
const valid = pageFound && timeFound && (!hasText || !!range);
|
|
863
|
+
if (status) {
|
|
864
|
+
status.textContent = valid ? '' : '无法确认原位置' + (locationLabel(locator) ? ' · ' + locationLabel(locator) : '');
|
|
865
|
+
status.hidden = !status.textContent;
|
|
866
|
+
}
|
|
867
|
+
return valid;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/* A selection is recorded once using the same anchor that is passed to either action. */
|
|
871
|
+
const actions = $('selection-actions'), quoteButton = $('quote-selection'), annotateButton = $('annotate-selection');
|
|
872
|
+
annotateButton.innerHTML = icon('stickyNote');
|
|
873
|
+
quoteButton.innerHTML = icon('chat');
|
|
874
|
+
let resting, lastLogged = '', commandRange;
|
|
875
|
+
function selectedText() {
|
|
876
|
+
if (!sourceReady || !capabilities.annotatable || fileEditor?.active) return null;
|
|
877
|
+
const selection = getSelection();
|
|
878
|
+
if (!selection?.rangeCount || selection.isCollapsed || !stage.contains(selection.anchorNode) || !stage.contains(selection.focusNode)) return null;
|
|
879
|
+
const range = selection.getRangeAt(0);
|
|
880
|
+
if (commandRange && range.compareBoundaryPoints(Range.START_TO_START, commandRange) === 0 &&
|
|
881
|
+
range.compareBoundaryPoints(Range.END_TO_END, commandRange) === 0) return null;
|
|
882
|
+
commandRange = undefined;
|
|
883
|
+
const text = String(selection).trim();
|
|
884
|
+
return text ? { text, range } : null;
|
|
885
|
+
}
|
|
886
|
+
function logSelection(locator) {
|
|
887
|
+
const key = JSON.stringify(locator);
|
|
888
|
+
if (lastLogged !== key) { lastLogged = key; say('select', locator); }
|
|
889
|
+
}
|
|
890
|
+
function positionQuote() {
|
|
891
|
+
annotateButton.hidden = !sourceReady || documentReadOnly || !documentTools?.has('annotate');
|
|
892
|
+
const rect = selectedText()?.range.getBoundingClientRect();
|
|
893
|
+
const stageBox = stage.getBoundingClientRect();
|
|
894
|
+
actions.hidden = (annotateButton.hidden && quoteButton.hidden) || !rect || rect.bottom < stageBox.top || rect.top >= stageBox.bottom;
|
|
895
|
+
if (actions.hidden) return;
|
|
896
|
+
actions.style.left = Math.max(4, Math.min(rect.left, innerWidth - actions.offsetWidth - 4)) + 'px';
|
|
897
|
+
const below = rect.bottom + 5;
|
|
898
|
+
actions.style.top = (below + actions.offsetHeight < stageBox.bottom ? below : Math.max(stageBox.top, rect.top - actions.offsetHeight - 5)) + 'px';
|
|
899
|
+
}
|
|
900
|
+
document.addEventListener('selectionchange', () => {
|
|
901
|
+
positionQuote();
|
|
902
|
+
clearTimeout(resting);
|
|
903
|
+
if (!selectedText()) {
|
|
904
|
+
if (getSelection().isCollapsed) commandRange = undefined;
|
|
905
|
+
lastLogged = '';
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
resting = setTimeout(() => {
|
|
909
|
+
const selected = selectedText();
|
|
910
|
+
if (selected) logSelection(selectedLocator(selected));
|
|
911
|
+
}, 700);
|
|
912
|
+
});
|
|
913
|
+
document.addEventListener('scroll', positionQuote, true);
|
|
914
|
+
quoteButton.hidden = !canQuote;
|
|
915
|
+
actions.onpointerdown = (event) => event.preventDefault();
|
|
916
|
+
quoteButton.onclick = () => {
|
|
917
|
+
const selected = selectedText();
|
|
918
|
+
if (!selected) return;
|
|
919
|
+
clearTimeout(resting);
|
|
920
|
+
const locator = selectedLocator(selected);
|
|
921
|
+
logSelection(locator);
|
|
922
|
+
const ref = { topicId: topic, kind: 'selection', label: $('title').textContent,
|
|
923
|
+
excerpt: selected.text.slice(0, 600), source: { resource, locator } };
|
|
924
|
+
if (standalone) {
|
|
925
|
+
sessionStorage.setItem('hostlet.viewerQuote', JSON.stringify(ref));
|
|
926
|
+
location.assign('/#chat-' + topic);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
window.parent.postMessage({
|
|
930
|
+
channel: 'hostlet-frame/v1', type: 'compose.quote', topicId: topic, ref,
|
|
931
|
+
}, location.origin);
|
|
932
|
+
getSelection().removeAllRanges();
|
|
933
|
+
actions.hidden = true;
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
annotateButton.onclick = () => {
|
|
937
|
+
const selected = selectedText();
|
|
938
|
+
if (!selected || !documentTools?.has('annotate')) return;
|
|
939
|
+
const locator = selectedLocator(selected);
|
|
940
|
+
clearTimeout(resting);
|
|
941
|
+
logSelection(locator);
|
|
942
|
+
if (documentTools.run('annotate', { locator, excerpt: locator.text })) {
|
|
943
|
+
getSelection().removeAllRanges();
|
|
944
|
+
actions.hidden = true;
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
let receiveCommand;
|
|
949
|
+
|
|
950
|
+
if (sourceReady) {
|
|
951
|
+
/* Only the host's command stream can drive the viewer. */
|
|
952
|
+
const find = (text) => {
|
|
953
|
+
const selection = getSelection();
|
|
954
|
+
selection.removeAllRanges();
|
|
955
|
+
if (window.find?.(text, false, false, true) && selection.rangeCount) {
|
|
956
|
+
commandRange = selection.getRangeAt(0).cloneRange();
|
|
957
|
+
selection.anchorNode?.parentElement?.scrollIntoView({ block: 'nearest' });
|
|
958
|
+
}
|
|
959
|
+
};
|
|
960
|
+
const perform = async (op) => {
|
|
961
|
+
const locator = op.locator ?? {};
|
|
962
|
+
if (locator.range || locator.view || locator.prefix || locator.suffix) await restoreLocator(locator);
|
|
963
|
+
else {
|
|
964
|
+
if (locator.page !== undefined && goPage) await goPage(locator.page, true);
|
|
965
|
+
if (locator.time !== undefined && media) await seek(locator.time);
|
|
966
|
+
if (locator.text) {
|
|
967
|
+
if (waitForPdfLayer) await waitForPdfLayer(shownPage);
|
|
968
|
+
find(locator.text);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
if (op.event === 'play' && media?.paused) {
|
|
972
|
+
mediaCommands.add('play');
|
|
973
|
+
try { await media.play(); }
|
|
974
|
+
catch (error) { mediaCommands.delete('play'); throw error; }
|
|
975
|
+
}
|
|
976
|
+
if (op.event === 'pause' && media && !media.paused) {
|
|
977
|
+
mediaCommands.add('pause');
|
|
978
|
+
media.pause();
|
|
979
|
+
}
|
|
980
|
+
$('notice').textContent = '执行 ' + op.event + (locationLabel(locator) ? ' · ' + locationLabel(locator) : '');
|
|
981
|
+
};
|
|
982
|
+
let commands = Promise.resolve();
|
|
983
|
+
receiveCommand = (e) => {
|
|
984
|
+
const { topicId, op } = JSON.parse(e.data);
|
|
985
|
+
if (topicId !== topic || op.resource !== resource) return;
|
|
986
|
+
commands = commands.then(() => perform(op)).catch((error) => { $('notice').textContent = error.message; });
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
function updateTopicState(state) {
|
|
991
|
+
documentReadOnly = !sourceReady || state.archivedAt != null || state.deletedAt != null;
|
|
992
|
+
fileEditor?.updateAvailability(documentReadOnly);
|
|
993
|
+
documentTools?.update();
|
|
994
|
+
positionQuote();
|
|
995
|
+
}
|
|
996
|
+
async function syncDocumentTools(stream) {
|
|
997
|
+
const request = ++toolsRevision;
|
|
998
|
+
const index = await readDocumentIndex();
|
|
999
|
+
if (stream !== viewerEvents || request !== toolsRevision) return;
|
|
1000
|
+
if (!documentTools && (sourceReady || annotationId) && Object.values(index).some((entry) => entry.documentTools)) {
|
|
1001
|
+
const { mountDocumentTools, documentRunAddress } = await import('/app/document-tools.mjs');
|
|
1002
|
+
if (stream !== viewerEvents || request !== toolsRevision) return;
|
|
1003
|
+
documentTools = mountDocumentTools($('document-tools'), {
|
|
1004
|
+
topicId: topic, resource, reference: annotationId,
|
|
1005
|
+
language: () => document.documentElement.lang,
|
|
1006
|
+
readOnly: () => documentReadOnly || fileEditor?.active,
|
|
1007
|
+
root: () => fileEditor?.active ? null : pdfLayer ? stage : anchorRoot,
|
|
1008
|
+
openRun: (standalone || canQuote) ? (turn) => {
|
|
1009
|
+
const href = documentRunAddress(topic, turn);
|
|
1010
|
+
if (standalone) window.open('/' + href, '_blank', 'noopener');
|
|
1011
|
+
else window.parent.postMessage({ channel: 'hostlet-frame/v1', type: 'nav.open', href }, location.origin);
|
|
1012
|
+
} : undefined,
|
|
1013
|
+
range: documentRange,
|
|
1014
|
+
go: (locator) => restoreLocator(locator, null),
|
|
1015
|
+
position: () => capabilities.annotatable && (shownPage !== undefined || media) ? currentLocator() : undefined,
|
|
1016
|
+
location: (locator) => locationLabel({ ...locator, text: undefined }),
|
|
1017
|
+
changed: positionQuote,
|
|
1018
|
+
listen: (event, handler, signal) => stream.addEventListener(event, (e) => handler(e.data ? JSON.parse(e.data) : {}), { signal }),
|
|
1019
|
+
link(card) {
|
|
1020
|
+
const params = new URLSearchParams({ topic, resource: card.resource });
|
|
1021
|
+
params.set('annotation', card.id);
|
|
1022
|
+
return location.pathname + '?' + params;
|
|
1023
|
+
},
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
documentTools?.sync(index);
|
|
1027
|
+
positionQuote();
|
|
1028
|
+
}
|
|
1029
|
+
function connectViewer() {
|
|
1030
|
+
if (viewerEvents) return;
|
|
1031
|
+
const stream = viewerEvents = new EventSource('/api/events');
|
|
1032
|
+
referenceLabelsListen(stream);
|
|
1033
|
+
if (receiveCommand) stream.addEventListener('works.op', receiveCommand);
|
|
1034
|
+
const readTools = () => syncDocumentTools(stream).catch((error) => { $('notice').textContent = error.message; });
|
|
1035
|
+
stream.addEventListener('open', () => {
|
|
1036
|
+
readTools();
|
|
1037
|
+
fetch('/api/topics/' + encodeURIComponent(topic)).then(async (response) => {
|
|
1038
|
+
if (!response.ok) return;
|
|
1039
|
+
const state = await response.json();
|
|
1040
|
+
if (viewerEvents === stream) updateTopicState(state);
|
|
1041
|
+
}).catch((error) => { $('notice').textContent = error.message; });
|
|
1042
|
+
});
|
|
1043
|
+
stream.addEventListener('console.pdl.changed', readTools);
|
|
1044
|
+
stream.addEventListener('config.changed', (event) => {
|
|
1045
|
+
const { scope, key } = JSON.parse(event.data);
|
|
1046
|
+
if (scope === 'settings' && (!key || key === 'host.lang')) documentLanguage()
|
|
1047
|
+
.then((words) => { if (stream === viewerEvents) fileEditor?.translate(words); })
|
|
1048
|
+
.catch((error) => { $('notice').textContent = error.message; });
|
|
1049
|
+
});
|
|
1050
|
+
stream.addEventListener('topic.updated', (event) => {
|
|
1051
|
+
const { topic: state } = JSON.parse(event.data);
|
|
1052
|
+
if (state.id === topic) updateTopicState(state);
|
|
1053
|
+
});
|
|
1054
|
+
stream.addEventListener('resource.changed', (event) => {
|
|
1055
|
+
const change = JSON.parse(event.data);
|
|
1056
|
+
if (change.kind === 'updated' && change.resource === resource) fileEditor?.changed();
|
|
1057
|
+
if (change.kind === 'moved') {
|
|
1058
|
+
// Embedded viewers share the console's store, which already handles this event.
|
|
1059
|
+
if (draftOwner === window) documentDraftStore(draftOwner).move(change);
|
|
1060
|
+
const next = documentResourceAfterMove(resource, change);
|
|
1061
|
+
if (!next || next === resource) return;
|
|
1062
|
+
q.set('resource', next);
|
|
1063
|
+
if (!q.has('locator') && !annotationId) q.set('locator', JSON.stringify(currentLocator()));
|
|
1064
|
+
const url = location.pathname + '?' + q;
|
|
1065
|
+
if (draftOwner !== window || !fileEditor?.followInMemory(next)) {
|
|
1066
|
+
location.replace(url);
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
// A new page would lose a draft that exceeds session storage.
|
|
1070
|
+
resource = next;
|
|
1071
|
+
history.replaceState(history.state, '', url);
|
|
1072
|
+
documentTools?.dispose();
|
|
1073
|
+
documentTools = undefined;
|
|
1074
|
+
++toolsRevision;
|
|
1075
|
+
readResource(next).then((view) => {
|
|
1076
|
+
if (resource !== next || stream !== viewerEvents) return;
|
|
1077
|
+
$('title').textContent = view.title;
|
|
1078
|
+
capabilities = view.capabilities;
|
|
1079
|
+
fileEditor.updateAvailability(documentReadOnly, view);
|
|
1080
|
+
readTools();
|
|
1081
|
+
}).catch((error) => { $('notice').textContent = error.message; });
|
|
1082
|
+
} else if (change.kind === 'deleted' && change.entry &&
|
|
1083
|
+
(resource === change.resource || resource?.startsWith(change.resource + '/'))) location.reload();
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
if (sourceReady || (resource && annotationId)) {
|
|
1087
|
+
if (sourceReady && !dispatchedOpen) say('open');
|
|
1088
|
+
if (!standalone && canQuote && capabilities.annotatable) window.parent.postMessage({
|
|
1089
|
+
channel: 'hostlet-frame/v1', type: 'document.source', topicId: topic, resource,
|
|
1090
|
+
}, location.origin);
|
|
1091
|
+
if (!standalone && canQuote) window.addEventListener('message', (event) => {
|
|
1092
|
+
const command = event.data;
|
|
1093
|
+
if (event.source !== window.parent || event.origin !== location.origin ||
|
|
1094
|
+
command?.channel !== 'hostlet-frame/v1' || command.type !== 'document.goto' || command.resource !== resource) return;
|
|
1095
|
+
restoreLocator(command.locator ?? {}).catch((error) => { $('notice').textContent = error.message; });
|
|
1096
|
+
});
|
|
1097
|
+
window.addEventListener('pagehide', () => {
|
|
1098
|
+
documentTools?.dispose();
|
|
1099
|
+
documentTools = undefined;
|
|
1100
|
+
viewerEvents?.close();
|
|
1101
|
+
viewerEvents = undefined;
|
|
1102
|
+
});
|
|
1103
|
+
window.addEventListener('pageshow', connectViewer);
|
|
1104
|
+
connectViewer();
|
|
1105
|
+
}
|
|
1106
|
+
const initialLocator = linkedReference?.locator ?? linkedLocator;
|
|
1107
|
+
if (sourceReady) {
|
|
1108
|
+
keepReadingPosition(stage, () => ({ resource, mode, root: anchorRoot }), !initialLocator && !annotationId);
|
|
1109
|
+
if (initialLocator) {
|
|
1110
|
+
await restoreLocator(initialLocator).catch((error) => {
|
|
1111
|
+
$('reference-status').textContent = error.message;
|
|
1112
|
+
$('reference-status').hidden = false;
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
</script>
|
|
1117
|
+
</body>
|
|
1118
|
+
</html>
|