@open-press/cli 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +58 -0
- package/dist/cli.js +365 -0
- package/package.json +57 -0
- package/template/core/.turbo/turbo-test.log +341 -0
- package/template/core/CHANGELOG.md +11 -0
- package/template/core/README.md +36 -0
- package/template/core/engine/chrome-pdf.d.mts +34 -0
- package/template/core/engine/chrome-pdf.mjs +344 -0
- package/template/core/engine/cli.mjs +93 -0
- package/template/core/engine/commands/_shared.mjs +170 -0
- package/template/core/engine/commands/deploy.mjs +31 -0
- package/template/core/engine/commands/dev.mjs +26 -0
- package/template/core/engine/commands/export.mjs +8 -0
- package/template/core/engine/commands/init.mjs +24 -0
- package/template/core/engine/commands/inspect.mjs +35 -0
- package/template/core/engine/commands/migrate-to-react.mjs +27 -0
- package/template/core/engine/commands/pdf.mjs +26 -0
- package/template/core/engine/commands/preview.mjs +26 -0
- package/template/core/engine/commands/render.mjs +17 -0
- package/template/core/engine/commands/replace.mjs +41 -0
- package/template/core/engine/commands/search.mjs +33 -0
- package/template/core/engine/commands/typecheck.mjs +5 -0
- package/template/core/engine/commands/validate.mjs +17 -0
- package/template/core/engine/config.d.mts +40 -0
- package/template/core/engine/config.mjs +160 -0
- package/template/core/engine/deploy-sync.mjs +15 -0
- package/template/core/engine/document-export.mjs +15 -0
- package/template/core/engine/file-utils.mjs +106 -0
- package/template/core/engine/fonts.mjs +62 -0
- package/template/core/engine/init.mjs +90 -0
- package/template/core/engine/inspection.mjs +348 -0
- package/template/core/engine/issue-report.mjs +44 -0
- package/template/core/engine/katex-assets.mjs +45 -0
- package/template/core/engine/page-block.mjs +30 -0
- package/template/core/engine/page-renderer.mjs +217 -0
- package/template/core/engine/pdf-media.mjs +45 -0
- package/template/core/engine/public-assets.mjs +19 -0
- package/template/core/engine/react/chapter-css.mjs +53 -0
- package/template/core/engine/react/comment-endpoint.d.mts +11 -0
- package/template/core/engine/react/comment-endpoint.mjs +128 -0
- package/template/core/engine/react/comment-marker.mjs +306 -0
- package/template/core/engine/react/document-entry.mjs +253 -0
- package/template/core/engine/react/document-export.mjs +392 -0
- package/template/core/engine/react/mdx-compile.mjs +295 -0
- package/template/core/engine/react/measurement-css.mjs +44 -0
- package/template/core/engine/react/migrate-to-react.mjs +355 -0
- package/template/core/engine/react/pagination-constants.mjs +3 -0
- package/template/core/engine/react/pagination.mjs +121 -0
- package/template/core/engine/react/project-asset-endpoint.d.mts +10 -0
- package/template/core/engine/react/project-asset-endpoint.mjs +379 -0
- package/template/core/engine/react/workspace-discovery.mjs +156 -0
- package/template/core/engine/source-text-tools.mjs +280 -0
- package/template/core/engine/source-workspace.mjs +76 -0
- package/template/core/engine/static-server.mjs +493 -0
- package/template/core/engine/validation.mjs +172 -0
- package/template/core/index.html +13 -0
- package/template/core/openpress.config.mjs +12 -0
- package/template/core/package.json +86 -0
- package/template/core/src/main.tsx +16 -0
- package/template/core/src/openpress/App.tsx +127 -0
- package/template/core/src/openpress/composerMentions.ts +188 -0
- package/template/core/src/openpress/core/basePages.tsx +87 -0
- package/template/core/src/openpress/core/index.tsx +20 -0
- package/template/core/src/openpress/core/types.ts +71 -0
- package/template/core/src/openpress/frameScheduler.ts +32 -0
- package/template/core/src/openpress/indexes.ts +329 -0
- package/template/core/src/openpress/inspector.ts +282 -0
- package/template/core/src/openpress/pageRoute.ts +21 -0
- package/template/core/src/openpress/pagination.ts +845 -0
- package/template/core/src/openpress/projectIdentity.ts +15 -0
- package/template/core/src/openpress/projectSources.ts +24 -0
- package/template/core/src/openpress/projectWorkspace.tsx +919 -0
- package/template/core/src/openpress/publicPage.tsx +469 -0
- package/template/core/src/openpress/reactDocumentMetadata.ts +41 -0
- package/template/core/src/openpress/readerPageRegistry.ts +41 -0
- package/template/core/src/openpress/readerRuntime.ts +230 -0
- package/template/core/src/openpress/readerScroll.ts +92 -0
- package/template/core/src/openpress/readerState.ts +15 -0
- package/template/core/src/openpress/renderer.tsx +91 -0
- package/template/core/src/openpress/runtimeMode.ts +22 -0
- package/template/core/src/openpress/types.ts +112 -0
- package/template/core/src/openpress/workbench.tsx +1299 -0
- package/template/core/src/openpress/workbenchPanels.tsx +122 -0
- package/template/core/src/openpress/workbenchTypes.ts +4 -0
- package/template/core/src/styles/openpress/app-shell.css +251 -0
- package/template/core/src/styles/openpress/media-workspace.css +230 -0
- package/template/core/src/styles/openpress/print-route.css +186 -0
- package/template/core/src/styles/openpress/project-workspace.css +1318 -0
- package/template/core/src/styles/openpress/public-viewer.css +983 -0
- package/template/core/src/styles/openpress/reader-runtime.css +792 -0
- package/template/core/src/styles/openpress/responsive.css +384 -0
- package/template/core/src/styles/openpress/workbench-panels.css +558 -0
- package/template/core/src/styles/openpress/workbench.css +720 -0
- package/template/core/src/styles/openpress.css +14 -0
- package/template/core/src/vite-env.d.ts +9 -0
- package/template/core/tsconfig.json +37 -0
- package/template/core/vite.config.ts +512 -0
- package/template/skills/chinese-ai-writing-polish/SKILL.md +195 -0
- package/template/skills/claude-document/SKILL.md +66 -0
- package/template/skills/claude-document/starter/document/chapters/01-document-shape/chapter.tsx +30 -0
- package/template/skills/claude-document/starter/document/chapters/01-document-shape/content/01-document-shape.mdx +51 -0
- package/template/skills/claude-document/starter/document/chapters/02-review-loop/chapter.tsx +30 -0
- package/template/skills/claude-document/starter/document/chapters/02-review-loop/content/01-review-loop.mdx +31 -0
- package/template/skills/claude-document/starter/document/components/ChapterOpenerVisual.tsx +96 -0
- package/template/skills/claude-document/starter/document/components/Page.tsx +27 -0
- package/template/skills/claude-document/starter/document/design.md +142 -0
- package/template/skills/claude-document/starter/document/index.tsx +89 -0
- package/template/skills/claude-document/starter/document/media/README.md +13 -0
- package/template/skills/claude-document/starter/document/openpress.config.mjs +26 -0
- package/template/skills/claude-document/starter/document/theme/README.md +15 -0
- package/template/skills/claude-document/starter/document/theme/base/page-contract.css +525 -0
- package/template/skills/claude-document/starter/document/theme/base/print.css +93 -0
- package/template/skills/claude-document/starter/document/theme/base/typography.css +612 -0
- package/template/skills/claude-document/starter/document/theme/fonts.css +4 -0
- package/template/skills/claude-document/starter/document/theme/page-surfaces/back-cover.css +72 -0
- package/template/skills/claude-document/starter/document/theme/page-surfaces/chapter-opener.css +236 -0
- package/template/skills/claude-document/starter/document/theme/page-surfaces/cover.css +309 -0
- package/template/skills/claude-document/starter/document/theme/page-surfaces/toc.css +213 -0
- package/template/skills/claude-document/starter/document/theme/patterns/_chart-frame.css +53 -0
- package/template/skills/claude-document/starter/document/theme/patterns/figure-grid.css +68 -0
- package/template/skills/claude-document/starter/document/theme/patterns/table-utilities.css +66 -0
- package/template/skills/claude-document/starter/document/theme/shell/reader-controls.css +789 -0
- package/template/skills/claude-document/starter/document/theme/tokens.css +89 -0
- package/template/skills/claude-document/starter/openpress.config.mjs +5 -0
- package/template/skills/editorial-monograph/SKILL.md +73 -0
- package/template/skills/editorial-monograph/starter/document/chapters/01-product-and-use-cases/content/01-product-and-use-cases.mdx +31 -0
- package/template/skills/editorial-monograph/starter/document/chapters/02-workflow/content/01-workflow.mdx +89 -0
- package/template/skills/editorial-monograph/starter/document/chapters/03-agent-skills-contributors/content/01-agent-skills-contributors.mdx +52 -0
- package/template/skills/editorial-monograph/starter/document/chapters/04-validation-deploy/content/01-validation-deploy.mdx +39 -0
- package/template/skills/editorial-monograph/starter/document/components/ChapterOpenerVisual/index.tsx +76 -0
- package/template/skills/editorial-monograph/starter/document/components/Page.tsx +27 -0
- package/template/skills/editorial-monograph/starter/document/components/TokenSwatchGrid/index.tsx +46 -0
- package/template/skills/editorial-monograph/starter/document/components/TokenSwatchGrid/style.css +63 -0
- package/template/skills/editorial-monograph/starter/document/components/TypeSpecimen/index.tsx +38 -0
- package/template/skills/editorial-monograph/starter/document/components/TypeSpecimen/style.css +111 -0
- package/template/skills/editorial-monograph/starter/document/design.md +279 -0
- package/template/skills/editorial-monograph/starter/document/index.tsx +73 -0
- package/template/skills/editorial-monograph/starter/document/media/README.md +13 -0
- package/template/skills/editorial-monograph/starter/document/openpress.config.mjs +26 -0
- package/template/skills/editorial-monograph/starter/document/theme/README.md +11 -0
- package/template/skills/editorial-monograph/starter/document/theme/base/page-contract.css +505 -0
- package/template/skills/editorial-monograph/starter/document/theme/base/print.css +93 -0
- package/template/skills/editorial-monograph/starter/document/theme/base/typography.css +336 -0
- package/template/skills/editorial-monograph/starter/document/theme/fonts.css +3 -0
- package/template/skills/editorial-monograph/starter/document/theme/page-surfaces/back-cover.css +43 -0
- package/template/skills/editorial-monograph/starter/document/theme/page-surfaces/chapter-opener.css +205 -0
- package/template/skills/editorial-monograph/starter/document/theme/page-surfaces/cover.css +147 -0
- package/template/skills/editorial-monograph/starter/document/theme/page-surfaces/toc.css +139 -0
- package/template/skills/editorial-monograph/starter/document/theme/patterns/_chart-frame.css +49 -0
- package/template/skills/editorial-monograph/starter/document/theme/patterns/figure-grid.css +68 -0
- package/template/skills/editorial-monograph/starter/document/theme/patterns/table-utilities.css +66 -0
- package/template/skills/editorial-monograph/starter/document/theme/shell/reader-controls.css +761 -0
- package/template/skills/editorial-monograph/starter/document/theme/tokens.css +80 -0
- package/template/skills/editorial-monograph/starter/openpress.config.mjs +5 -0
- package/template/skills/openpress/SKILL.md +114 -0
- package/template/skills/openpress/references/cli-commands.md +31 -0
- package/template/skills/openpress/references/local-review.md +43 -0
- package/template/skills/openpress-deploy/SKILL.md +69 -0
- package/template/skills/openpress-deploy/references/cloudflare-pages.md +51 -0
- package/template/skills/openpress-design/SKILL.md +51 -0
- package/template/skills/openpress-design/references/pdf-safe-css.md +29 -0
- package/template/skills/openpress-design/references/responsive-fixed-layout.md +48 -0
- package/template/skills/openpress-design/references/theme-and-components.md +77 -0
- package/template/skills/openpress-diagram-drawing/SKILL.md +44 -0
- package/template/skills/openpress-diagram-drawing/references/diagram-patterns.md +93 -0
- package/template/skills/openpress-document-hierarchy/SKILL.md +81 -0
- package/template/skills/openpress-document-hierarchy/agents/openai.yaml +4 -0
- package/template/skills/openpress-document-hierarchy/references/data-structures-outline.md +115 -0
- package/template/skills/openpress-init/SKILL.md +84 -0
- package/template/skills/openpress-style-pack-contributor/SKILL.md +62 -0
- package/template/skills/openpress-style-pack-contributor/references/starter-contract.md +49 -0
- package/template/skills/openpress-update/SKILL.md +88 -0
- package/template/skills/openpress-writing/SKILL.md +68 -0
- package/template/skills/openpress-writing/references/source-and-writing-rules.md +120 -0
- package/template/skills/teaching-notes-writing/SKILL.md +54 -0
- package/template/skills/teaching-notes-writing/references/programming.md +65 -0
- package/template/skills/teaching-notes-writing/references/teaching-patterns.md +60 -0
|
@@ -0,0 +1,1299 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
type CSSProperties,
|
|
9
|
+
type FormEvent,
|
|
10
|
+
type RefObject,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { ArrowUp, BookOpen, ExternalLink, Eye, FileText, FolderOpen, MessageSquare, MousePointer2, Pencil, Plus, RefreshCw, Rocket, Trash2, X } from "lucide-react";
|
|
13
|
+
import {
|
|
14
|
+
collectBookmarkIndex,
|
|
15
|
+
collectMediaAssetIndex,
|
|
16
|
+
} from "./indexes";
|
|
17
|
+
import { appendComposerToken, useComposerMentions } from "./composerMentions";
|
|
18
|
+
import {
|
|
19
|
+
clearInspectorComment,
|
|
20
|
+
fetchInspectorComments,
|
|
21
|
+
submitInspectorComment,
|
|
22
|
+
updateInspectorComment,
|
|
23
|
+
useInspector,
|
|
24
|
+
type InspectorIntent,
|
|
25
|
+
type InspectorPlacement,
|
|
26
|
+
type InspectorState,
|
|
27
|
+
type InspectorTarget,
|
|
28
|
+
type PendingComment,
|
|
29
|
+
} from "./inspector";
|
|
30
|
+
import {
|
|
31
|
+
createProjectMentionItems,
|
|
32
|
+
createProjectComponentUsages,
|
|
33
|
+
ProjectEntryPanel,
|
|
34
|
+
type ProjectMentionItem,
|
|
35
|
+
} from "./projectWorkspace";
|
|
36
|
+
import { paginateSourcePages, type PaginatedPage } from "./pagination";
|
|
37
|
+
import { scheduleBrowserFrame } from "./frameScheduler";
|
|
38
|
+
import {
|
|
39
|
+
createAnchorPageMap,
|
|
40
|
+
numberSourceHeadings,
|
|
41
|
+
PUBLIC_DRAWER_BREAKPOINT,
|
|
42
|
+
PublicPage,
|
|
43
|
+
resolveAnchorPageIndex,
|
|
44
|
+
useViewMode,
|
|
45
|
+
} from "./publicPage";
|
|
46
|
+
import { getProjectIdentity } from "./projectIdentity";
|
|
47
|
+
import { hasBuildTimePagination } from "./reactDocumentMetadata";
|
|
48
|
+
import { buildPublicPreviewHref, isLocalWorkspaceHost } from "./runtimeMode";
|
|
49
|
+
import { useReaderRuntime } from "./readerRuntime";
|
|
50
|
+
import type { DeploymentInfo, ReaderDocument, HtmlPageBlock, SourceBlock } from "./types";
|
|
51
|
+
import { Bookmarks, CurrentPagePanel } from "./workbenchPanels";
|
|
52
|
+
import type { DisplayPage } from "./workbenchTypes";
|
|
53
|
+
|
|
54
|
+
type WorkspaceView = "document" | "project" | "comments";
|
|
55
|
+
type DeployStatus = "idle" | "deploying" | "deployed" | "unavailable" | "failed" | "setup";
|
|
56
|
+
type PdfActionStatus = "idle" | "generating" | "opening" | "failed";
|
|
57
|
+
type InspectorCommentStatus = "idle" | "submitting" | "saved" | "failed";
|
|
58
|
+
type CommentsWorkspaceStatus = "idle" | "loading" | "ready" | "failed" | "clearing";
|
|
59
|
+
|
|
60
|
+
interface InlineSavedComment {
|
|
61
|
+
id: string;
|
|
62
|
+
blockId: string;
|
|
63
|
+
placement: InspectorPlacement;
|
|
64
|
+
note: string;
|
|
65
|
+
path?: string;
|
|
66
|
+
line?: number;
|
|
67
|
+
timestamp?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getInitialWorkspaceView(): WorkspaceView {
|
|
71
|
+
if (typeof window === "undefined") return "document";
|
|
72
|
+
const workspace = new URLSearchParams(window.location.search).get("workspace");
|
|
73
|
+
if (workspace === "project") return "project";
|
|
74
|
+
if (workspace === "comments") return "comments";
|
|
75
|
+
return "document";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function WorkspaceSwitcher({
|
|
79
|
+
workspaceView,
|
|
80
|
+
onOpenWorkspace,
|
|
81
|
+
}: {
|
|
82
|
+
workspaceView: WorkspaceView;
|
|
83
|
+
onOpenWorkspace: (view: WorkspaceView) => void;
|
|
84
|
+
}) {
|
|
85
|
+
const items: Array<{ view: WorkspaceView; label: string; icon: typeof FileText }> = [
|
|
86
|
+
{ view: "document", label: "文件", icon: FileText },
|
|
87
|
+
{ view: "project", label: "專案", icon: FolderOpen },
|
|
88
|
+
{ view: "comments", label: "註解", icon: MessageSquare },
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<nav className="openpress-dev-workspace-switcher" data-openpress-dev-workspace-switcher aria-label="Workspace">
|
|
93
|
+
{items.map((item) => {
|
|
94
|
+
const Icon = item.icon;
|
|
95
|
+
return (
|
|
96
|
+
<button
|
|
97
|
+
type="button"
|
|
98
|
+
className={workspaceView === item.view ? "is-active" : ""}
|
|
99
|
+
aria-pressed={workspaceView === item.view}
|
|
100
|
+
onClick={() => onOpenWorkspace(item.view)}
|
|
101
|
+
key={item.view}
|
|
102
|
+
>
|
|
103
|
+
<Icon aria-hidden="true" />
|
|
104
|
+
<span>{item.label}</span>
|
|
105
|
+
</button>
|
|
106
|
+
);
|
|
107
|
+
})}
|
|
108
|
+
</nav>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function HtmlWorkbench({
|
|
113
|
+
document,
|
|
114
|
+
pages,
|
|
115
|
+
style,
|
|
116
|
+
devMode,
|
|
117
|
+
deploymentInfo,
|
|
118
|
+
}: {
|
|
119
|
+
document: ReaderDocument;
|
|
120
|
+
pages: Array<HtmlPageBlock>;
|
|
121
|
+
style: CSSProperties;
|
|
122
|
+
devMode: boolean;
|
|
123
|
+
deploymentInfo: DeploymentInfo;
|
|
124
|
+
}) {
|
|
125
|
+
const sourceContainerRef = useRef<HTMLDivElement | null>(null);
|
|
126
|
+
const numberedPages = useMemo(() => numberSourceHeadings(pages), [pages]);
|
|
127
|
+
const viewModeState = useViewMode();
|
|
128
|
+
const { viewMode } = viewModeState;
|
|
129
|
+
const buildTimePaginated = hasBuildTimePagination(document);
|
|
130
|
+
const [paginatedPages, setPaginatedPages] = useState<PaginatedPage[] | null>(null);
|
|
131
|
+
const displayPages: DisplayPage[] = viewMode === "paged" && !buildTimePaginated
|
|
132
|
+
? (paginatedPages ?? numberedPages)
|
|
133
|
+
: numberedPages;
|
|
134
|
+
const mediaAssets = useMemo(() => collectMediaAssetIndex(displayPages), [displayPages]);
|
|
135
|
+
const anchorPageMap = useMemo(() => createAnchorPageMap(displayPages), [displayPages]);
|
|
136
|
+
const projectComponentUsages = useMemo(() => createProjectComponentUsages(displayPages), [displayPages]);
|
|
137
|
+
const bookmarks = useMemo(() => collectBookmarkIndex(displayPages), [displayPages]);
|
|
138
|
+
const projectMentionItems = useMemo(
|
|
139
|
+
() => createProjectMentionItems(mediaAssets, projectComponentUsages, bookmarks),
|
|
140
|
+
[bookmarks, mediaAssets, projectComponentUsages],
|
|
141
|
+
);
|
|
142
|
+
const [workspaceView, setWorkspaceView] = useState<WorkspaceView>(getInitialWorkspaceView);
|
|
143
|
+
const inspector = useInspector(document, { enabled: devMode && (workspaceView === "document" || workspaceView === "project") });
|
|
144
|
+
const reader = useReaderRuntime({ pageCount: Math.max(displayPages.length, 1), rightPanelBreakpoint: PUBLIC_DRAWER_BREAKPOINT });
|
|
145
|
+
const [deployStatus, setDeployStatus] = useState<DeployStatus>("idle");
|
|
146
|
+
const [pdfActionStatus, setPdfActionStatus] = useState<PdfActionStatus>("idle");
|
|
147
|
+
const [inspectorCommentText, setInspectorCommentText] = useState("");
|
|
148
|
+
const [inspectorCommentStatus, setInspectorCommentStatus] = useState<InspectorCommentStatus>("idle");
|
|
149
|
+
const [inspectorCommentError, setInspectorCommentError] = useState("");
|
|
150
|
+
const [inlineSavedComment, setInlineSavedComment] = useState<InlineSavedComment | null>(null);
|
|
151
|
+
const [pendingComments, setPendingComments] = useState<PendingComment[]>([]);
|
|
152
|
+
const [commentsStatus, setCommentsStatus] = useState<CommentsWorkspaceStatus>("idle");
|
|
153
|
+
const [commentsError, setCommentsError] = useState("");
|
|
154
|
+
const [currentDeploymentInfo, setCurrentDeploymentInfo] = useState(deploymentInfo);
|
|
155
|
+
const staticPdfHref = currentDeploymentInfo.pdf;
|
|
156
|
+
const projectIdentity = getProjectIdentity(document.meta);
|
|
157
|
+
const localDeployEnabled = useMemo(() => {
|
|
158
|
+
if (typeof window === "undefined") return false;
|
|
159
|
+
return isLocalWorkspaceHost(window.location.hostname);
|
|
160
|
+
}, []);
|
|
161
|
+
const deploymentStatusDescription = deploymentStatusText(currentDeploymentInfo, deployStatus);
|
|
162
|
+
const deploymentStatusLabelText = deploymentStatusSummary(currentDeploymentInfo, deployStatus);
|
|
163
|
+
const pdfButtonText = workbenchPdfButtonText(localDeployEnabled, pdfActionStatus, staticPdfHref);
|
|
164
|
+
const pdfStatusMessage = workbenchPdfStatusMessage(localDeployEnabled, pdfActionStatus);
|
|
165
|
+
const pdfButtonDisabled = localDeployEnabled ? pdfActionStatus === "generating" || pdfActionStatus === "opening" : !staticPdfHref;
|
|
166
|
+
const activePaginatedReady = viewMode === "reading" || buildTimePaginated || Boolean(paginatedPages);
|
|
167
|
+
const inspectorSelectionLabel = formatInspectorSelection(inspector.selectedBlock);
|
|
168
|
+
const activeInlineSavedComment = getInlineSavedCommentForTarget(inlineSavedComment, inspector.selectedTarget);
|
|
169
|
+
const inspectorCommentDisabled = !inspector.selectedBlock || !inspectorCommentText.trim() || inspectorCommentStatus === "submitting";
|
|
170
|
+
const inspectorCommentStatusMessage = formatInspectorCommentStatus(inspectorCommentStatus, inspectorCommentError);
|
|
171
|
+
const publicPreviewHref = useMemo(() => {
|
|
172
|
+
if (typeof window === "undefined") return "/";
|
|
173
|
+
return buildPublicPreviewHref(window.location.href, reader.currentPageIndex);
|
|
174
|
+
}, [reader.currentPageIndex]);
|
|
175
|
+
|
|
176
|
+
const refreshPendingComments = useCallback(async () => {
|
|
177
|
+
if (!devMode) return;
|
|
178
|
+
setCommentsStatus("loading");
|
|
179
|
+
setCommentsError("");
|
|
180
|
+
try {
|
|
181
|
+
const comments = await fetchInspectorComments();
|
|
182
|
+
setPendingComments(comments);
|
|
183
|
+
setCommentsStatus("ready");
|
|
184
|
+
} catch (error) {
|
|
185
|
+
setCommentsStatus("failed");
|
|
186
|
+
setCommentsError(error instanceof Error ? error.message : String(error));
|
|
187
|
+
}
|
|
188
|
+
}, [devMode]);
|
|
189
|
+
|
|
190
|
+
const clearPendingComment = useCallback(async (id: string) => {
|
|
191
|
+
setCommentsStatus("clearing");
|
|
192
|
+
setCommentsError("");
|
|
193
|
+
try {
|
|
194
|
+
await clearInspectorComment({ id });
|
|
195
|
+
setPendingComments((comments) => comments.filter((comment) => comment.id !== id));
|
|
196
|
+
setInlineSavedComment((comment) => comment?.id === id ? null : comment);
|
|
197
|
+
setCommentsStatus("ready");
|
|
198
|
+
} catch (error) {
|
|
199
|
+
setCommentsStatus("failed");
|
|
200
|
+
setCommentsError(error instanceof Error ? error.message : String(error));
|
|
201
|
+
}
|
|
202
|
+
}, []);
|
|
203
|
+
|
|
204
|
+
const clearAllPendingComments = useCallback(async () => {
|
|
205
|
+
setCommentsStatus("clearing");
|
|
206
|
+
setCommentsError("");
|
|
207
|
+
try {
|
|
208
|
+
await clearInspectorComment({ all: true });
|
|
209
|
+
setPendingComments([]);
|
|
210
|
+
setInlineSavedComment(null);
|
|
211
|
+
setCommentsStatus("ready");
|
|
212
|
+
} catch (error) {
|
|
213
|
+
setCommentsStatus("failed");
|
|
214
|
+
setCommentsError(error instanceof Error ? error.message : String(error));
|
|
215
|
+
}
|
|
216
|
+
}, []);
|
|
217
|
+
|
|
218
|
+
const handleDeploy = async () => {
|
|
219
|
+
if (deployStatus === "deploying") return;
|
|
220
|
+
if (currentDeploymentInfo.configured === false) {
|
|
221
|
+
setDeployStatus("setup");
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
setDeployStatus("deploying");
|
|
225
|
+
try {
|
|
226
|
+
const response = await fetch("/__openpress/deploy", { method: "POST" });
|
|
227
|
+
if (response.status === 404 || response.status === 405) {
|
|
228
|
+
setDeployStatus("unavailable");
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (!response.ok) {
|
|
232
|
+
const text = await response.text().catch(() => "");
|
|
233
|
+
const result = parseDeployError(text);
|
|
234
|
+
if (result?.deploy_configured === false) {
|
|
235
|
+
setCurrentDeploymentInfo((info) => ({
|
|
236
|
+
...info,
|
|
237
|
+
configured: false,
|
|
238
|
+
adapter: result.deploy_adapter ?? info.adapter,
|
|
239
|
+
source: result.deploy_source ?? info.source,
|
|
240
|
+
projectName: result.deploy_project_name ?? info.projectName,
|
|
241
|
+
setupMessage: result.message ?? info.setupMessage,
|
|
242
|
+
}));
|
|
243
|
+
setDeployStatus("setup");
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
console.error("OpenPress deploy failed", text);
|
|
247
|
+
setDeployStatus("failed");
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const result = (await response.json().catch(() => null)) as { deployed_at?: string; pdf?: string; public_url?: string } | null;
|
|
251
|
+
setCurrentDeploymentInfo((info) => ({
|
|
252
|
+
online: true,
|
|
253
|
+
deployedAt: result?.deployed_at ?? new Date().toISOString(),
|
|
254
|
+
pdf: result?.pdf ?? info.pdf ?? __OPENPRESS_PDF_HREF__,
|
|
255
|
+
publicUrl: result?.public_url ?? info.publicUrl,
|
|
256
|
+
dirty: false,
|
|
257
|
+
}));
|
|
258
|
+
setDeployStatus("deployed");
|
|
259
|
+
setTimeout(() => setDeployStatus("idle"), 3200);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
console.error("OpenPress deploy unavailable", error);
|
|
262
|
+
setDeployStatus("unavailable");
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const handleOpenLatestLocalPdf = async () => {
|
|
267
|
+
if (pdfActionStatus === "generating") return;
|
|
268
|
+
setPdfActionStatus("generating");
|
|
269
|
+
try {
|
|
270
|
+
const response = await fetch("/__openpress/local-pdf-export", { method: "POST" });
|
|
271
|
+
if (!response.ok) {
|
|
272
|
+
const text = await response.text().catch(() => "");
|
|
273
|
+
throw new Error(text || `Local PDF export failed with status ${response.status}`);
|
|
274
|
+
}
|
|
275
|
+
const result = (await response.json().catch(() => null)) as { pdf?: string } | null;
|
|
276
|
+
const pdfHref = result?.pdf ?? "/__openpress/local-pdf-file";
|
|
277
|
+
setPdfActionStatus("opening");
|
|
278
|
+
window.setTimeout(() => window.location.assign(pdfHref), 180);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.error("OpenPress local PDF export failed", error);
|
|
281
|
+
setPdfActionStatus("failed");
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const handleOpenWorkbenchPdf = () => {
|
|
286
|
+
if (localDeployEnabled) {
|
|
287
|
+
void handleOpenLatestLocalPdf();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (!staticPdfHref) return;
|
|
291
|
+
window.open(staticPdfHref, "_blank", "noopener,noreferrer");
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const handleSubmitInspectorComment = async (event?: FormEvent<HTMLFormElement>) => {
|
|
295
|
+
event?.preventDefault();
|
|
296
|
+
if (inspectorCommentDisabled || !inspector.selectedBlock) return;
|
|
297
|
+
setInspectorCommentStatus("submitting");
|
|
298
|
+
setInspectorCommentError("");
|
|
299
|
+
try {
|
|
300
|
+
const note = inspectorCommentText.trim();
|
|
301
|
+
const placement = inspector.selectedTarget?.placement ?? "block";
|
|
302
|
+
if (activeInlineSavedComment) {
|
|
303
|
+
const result = await updateInspectorComment({
|
|
304
|
+
id: activeInlineSavedComment.id,
|
|
305
|
+
note,
|
|
306
|
+
intent: "edit",
|
|
307
|
+
placement,
|
|
308
|
+
});
|
|
309
|
+
setInlineSavedComment({
|
|
310
|
+
...activeInlineSavedComment,
|
|
311
|
+
note,
|
|
312
|
+
path: result.comment?.path ?? activeInlineSavedComment.path,
|
|
313
|
+
line: result.comment?.line ?? activeInlineSavedComment.line,
|
|
314
|
+
timestamp: result.comment?.timestamp ?? activeInlineSavedComment.timestamp,
|
|
315
|
+
});
|
|
316
|
+
} else {
|
|
317
|
+
const result = await submitInspectorComment({
|
|
318
|
+
block: inspector.selectedBlock,
|
|
319
|
+
note,
|
|
320
|
+
intent: inspector.commentIntent,
|
|
321
|
+
placement,
|
|
322
|
+
});
|
|
323
|
+
if (result.comment?.id && inspector.selectedTarget) {
|
|
324
|
+
setInlineSavedComment({
|
|
325
|
+
id: result.comment.id,
|
|
326
|
+
blockId: inspector.selectedTarget.blockId,
|
|
327
|
+
placement,
|
|
328
|
+
note,
|
|
329
|
+
path: result.comment.path,
|
|
330
|
+
line: result.comment.line,
|
|
331
|
+
timestamp: result.comment.timestamp,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
setInspectorCommentText("");
|
|
336
|
+
setInspectorCommentStatus("saved");
|
|
337
|
+
void refreshPendingComments();
|
|
338
|
+
} catch (error) {
|
|
339
|
+
setInspectorCommentStatus("failed");
|
|
340
|
+
setInspectorCommentError(error instanceof Error ? error.message : String(error));
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const handleOpenInlineSavedComment = (comment: InlineSavedComment) => {
|
|
345
|
+
setInlineSavedComment(comment);
|
|
346
|
+
setInspectorCommentText(comment.note);
|
|
347
|
+
setInspectorCommentStatus("idle");
|
|
348
|
+
setInspectorCommentError("");
|
|
349
|
+
inspector.setCommentIntent("edit");
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const handleRemoveInlineSavedComment = async (comment: InlineSavedComment) => {
|
|
353
|
+
setInspectorCommentStatus("submitting");
|
|
354
|
+
setInspectorCommentError("");
|
|
355
|
+
try {
|
|
356
|
+
await clearInspectorComment({ id: comment.id });
|
|
357
|
+
setPendingComments((comments) => comments.filter((item) => item.id !== comment.id));
|
|
358
|
+
setInlineSavedComment((current) => current?.id === comment.id ? null : current);
|
|
359
|
+
setInspectorCommentText("");
|
|
360
|
+
setInspectorCommentStatus("idle");
|
|
361
|
+
inspector.selectTarget(null);
|
|
362
|
+
void refreshPendingComments();
|
|
363
|
+
} catch (error) {
|
|
364
|
+
setInspectorCommentStatus("failed");
|
|
365
|
+
setInspectorCommentError(error instanceof Error ? error.message : String(error));
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const selectWorkspacePage = (pageIndex: number, options?: { behavior?: ScrollBehavior }) => {
|
|
370
|
+
reader.setPage(pageIndex, options);
|
|
371
|
+
if (typeof window !== "undefined" && window.innerWidth < PUBLIC_DRAWER_BREAKPOINT && reader.rightPanelOpen) {
|
|
372
|
+
reader.toggleRightPanel();
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const selectWorkspaceAnchor = (anchorId: string, pageIndex?: number) => {
|
|
377
|
+
const targetPageIndex = resolveAnchorPageIndex(anchorPageMap, displayPages.length, anchorId, pageIndex);
|
|
378
|
+
if (targetPageIndex === null) return false;
|
|
379
|
+
selectWorkspacePage(targetPageIndex, { behavior: "smooth" });
|
|
380
|
+
return true;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const insertProjectMention = useCallback((mention: string) => {
|
|
384
|
+
setInspectorCommentText((text) => appendComposerToken(text, mention));
|
|
385
|
+
setInspectorCommentStatus("idle");
|
|
386
|
+
setInspectorCommentError("");
|
|
387
|
+
}, []);
|
|
388
|
+
|
|
389
|
+
const openWorkspace = (view: WorkspaceView) => {
|
|
390
|
+
setWorkspaceView(view);
|
|
391
|
+
if (typeof window === "undefined") return;
|
|
392
|
+
scheduleBrowserFrame(() => reader.setPage(reader.currentPageIndex, { behavior: "auto" }));
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
useLayoutEffect(() => {
|
|
396
|
+
setPaginatedPages(null);
|
|
397
|
+
}, [numberedPages]);
|
|
398
|
+
|
|
399
|
+
useEffect(() => {
|
|
400
|
+
setInspectorCommentStatus("idle");
|
|
401
|
+
setInspectorCommentError("");
|
|
402
|
+
setInspectorCommentText("");
|
|
403
|
+
}, [inspector.selectedBlockId, inspector.selectedTarget?.placement]);
|
|
404
|
+
|
|
405
|
+
useEffect(() => {
|
|
406
|
+
if (!devMode || workspaceView !== "comments") return;
|
|
407
|
+
void refreshPendingComments();
|
|
408
|
+
}, [devMode, refreshPendingComments, workspaceView]);
|
|
409
|
+
|
|
410
|
+
useLayoutEffect(() => {
|
|
411
|
+
if (buildTimePaginated) return undefined;
|
|
412
|
+
if (viewMode !== "paged" || paginatedPages) return undefined;
|
|
413
|
+
const sourceContainer = sourceContainerRef.current;
|
|
414
|
+
if (!sourceContainer) return undefined;
|
|
415
|
+
|
|
416
|
+
let cancelled = false;
|
|
417
|
+
const cancelFrame = scheduleBrowserFrame(() => {
|
|
418
|
+
const nextPages = paginateSourcePages(sourceContainer, numberedPages);
|
|
419
|
+
if (!cancelled) setPaginatedPages(nextPages);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
return () => {
|
|
423
|
+
cancelled = true;
|
|
424
|
+
cancelFrame();
|
|
425
|
+
};
|
|
426
|
+
}, [buildTimePaginated, numberedPages, paginatedPages, viewMode]);
|
|
427
|
+
|
|
428
|
+
const actionSection = (
|
|
429
|
+
<section className="openpress-public-action-section" aria-label="輸出">
|
|
430
|
+
<span className="openpress-public-action-heading">輸出</span>
|
|
431
|
+
<div className="openpress-public-action-list" aria-label="輸出操作">
|
|
432
|
+
<a
|
|
433
|
+
className="openpress-public-action-entry openpress-public-preview-link"
|
|
434
|
+
data-openpress-open-public-preview
|
|
435
|
+
href={publicPreviewHref}
|
|
436
|
+
target="_blank"
|
|
437
|
+
rel="noreferrer"
|
|
438
|
+
aria-label="開啟公開預覽"
|
|
439
|
+
>
|
|
440
|
+
<Eye aria-hidden="true" />
|
|
441
|
+
<span className="openpress-public-action-entry__label">公開預覽</span>
|
|
442
|
+
</a>
|
|
443
|
+
<button
|
|
444
|
+
type="button"
|
|
445
|
+
className="openpress-public-action-entry"
|
|
446
|
+
data-openpress-public-export
|
|
447
|
+
disabled={pdfButtonDisabled}
|
|
448
|
+
onClick={handleOpenWorkbenchPdf}
|
|
449
|
+
>
|
|
450
|
+
<ExternalLink aria-hidden="true" />
|
|
451
|
+
<span className="openpress-public-action-entry__label">{pdfButtonText}</span>
|
|
452
|
+
{pdfStatusMessage ? (
|
|
453
|
+
<span className="openpress-dev-pdf-status" data-openpress-pdf-status={pdfActionStatus} role="status" aria-live="polite">
|
|
454
|
+
<span className="openpress-dev-pdf-status__spinner" aria-hidden="true" />
|
|
455
|
+
<span>{pdfStatusMessage}</span>
|
|
456
|
+
</span>
|
|
457
|
+
) : null}
|
|
458
|
+
</button>
|
|
459
|
+
{devMode && (workspaceView === "document" || workspaceView === "project") ? (
|
|
460
|
+
<button
|
|
461
|
+
type="button"
|
|
462
|
+
className="openpress-public-action-entry"
|
|
463
|
+
data-openpress-inspector-toggle
|
|
464
|
+
data-openpress-inspector-active={inspector.inspectorMode ? "true" : "false"}
|
|
465
|
+
onClick={inspector.toggleInspectorMode}
|
|
466
|
+
aria-pressed={inspector.inspectorMode}
|
|
467
|
+
title={inspector.inspectorMode ? "關閉註解" : "開啟註解"}
|
|
468
|
+
aria-label={inspector.inspectorMode ? "關閉註解" : "開啟註解"}
|
|
469
|
+
>
|
|
470
|
+
<MousePointer2 aria-hidden="true" />
|
|
471
|
+
<span className="openpress-public-action-entry__label">{inspector.inspectorMode ? "註解中" : "註解"}</span>
|
|
472
|
+
<span className="openpress-dev-inspector-status">{inspectorSelectionLabel}</span>
|
|
473
|
+
</button>
|
|
474
|
+
) : null}
|
|
475
|
+
{devMode && (workspaceView === "document" || workspaceView === "project") && inspector.inspectorMode ? (
|
|
476
|
+
<span className="openpress-dev-inspector-status" role="status" aria-live="polite" data-openpress-inspector-comment-status={inspectorCommentStatus}>
|
|
477
|
+
{inspectorCommentStatusMessage}
|
|
478
|
+
</span>
|
|
479
|
+
) : null}
|
|
480
|
+
{localDeployEnabled ? (
|
|
481
|
+
<button
|
|
482
|
+
type="button"
|
|
483
|
+
className="openpress-public-action-entry"
|
|
484
|
+
data-openpress-deploy
|
|
485
|
+
data-openpress-deploy-status={deploymentStatusKind(currentDeploymentInfo, deployStatus)}
|
|
486
|
+
data-deploy-status={deployStatus}
|
|
487
|
+
disabled={deployStatus === "deploying" || deployStatus === "unavailable" || currentDeploymentInfo.configured === false}
|
|
488
|
+
onClick={handleDeploy}
|
|
489
|
+
title={deploymentStatusDescription}
|
|
490
|
+
aria-label={deploymentStatusDescription}
|
|
491
|
+
>
|
|
492
|
+
<Rocket aria-hidden="true" />
|
|
493
|
+
<span className="openpress-public-action-entry__label">{deployButtonText(currentDeploymentInfo, deployStatus)}</span>
|
|
494
|
+
<span
|
|
495
|
+
className="openpress-dev-deploy-status"
|
|
496
|
+
data-openpress-deploy-status={deploymentStatusKind(currentDeploymentInfo, deployStatus)}
|
|
497
|
+
role="status"
|
|
498
|
+
aria-live="polite"
|
|
499
|
+
>
|
|
500
|
+
<span className="openpress-dev-deploy-status__dot" aria-hidden="true" />
|
|
501
|
+
<span>{deploymentStatusLabelText}</span>
|
|
502
|
+
</span>
|
|
503
|
+
</button>
|
|
504
|
+
) : null}
|
|
505
|
+
</div>
|
|
506
|
+
</section>
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
return (
|
|
510
|
+
<main className="openpress-workbench" style={style} data-dev-mode={devMode ? "true" : "false"}>
|
|
511
|
+
<div
|
|
512
|
+
className={`reader-app openpress-reader-app openpress-public-viewer openpress-dev-public-viewer is-ready${reader.rightPanelOpen ? "" : " is-closed-right"}`}
|
|
513
|
+
data-openpress-react-runtime="true"
|
|
514
|
+
data-openpress-view-mode={viewMode}
|
|
515
|
+
data-openpress-pagination={activePaginatedReady ? "ready" : "pending"}
|
|
516
|
+
data-openpress-inspector-mode={inspector.inspectorMode ? "on" : "off"}
|
|
517
|
+
data-active-workspace={workspaceView}
|
|
518
|
+
>
|
|
519
|
+
{reader.rightPanelOpen ? (
|
|
520
|
+
<div className="openpress-public-scrim" aria-hidden="true" onClick={reader.toggleRightPanel} />
|
|
521
|
+
) : null}
|
|
522
|
+
<button type="button" className="openpress-public-fab" aria-label="開啟目錄" onClick={reader.toggleRightPanel}>
|
|
523
|
+
<BookOpen size={20} aria-hidden="true" />
|
|
524
|
+
</button>
|
|
525
|
+
|
|
526
|
+
<section
|
|
527
|
+
className="openpress-workbench__stage openpress-public-viewer__stage openpress-dev-main-content"
|
|
528
|
+
aria-label="Workspace content"
|
|
529
|
+
data-workspace-view={workspaceView}
|
|
530
|
+
>
|
|
531
|
+
<main className="reader-stage" tabIndex={-1} ref={reader.stageRef}>
|
|
532
|
+
<PublicPage
|
|
533
|
+
pages={displayPages}
|
|
534
|
+
currentPageIndex={reader.currentPageIndex}
|
|
535
|
+
devMode={devMode}
|
|
536
|
+
paginatedReady={activePaginatedReady}
|
|
537
|
+
sourceContainerRef={sourceContainerRef}
|
|
538
|
+
registerPage={reader.registerPage}
|
|
539
|
+
exposeSourceData={devMode}
|
|
540
|
+
inspector={inspector}
|
|
541
|
+
onInternalAnchorNavigate={selectWorkspaceAnchor}
|
|
542
|
+
/>
|
|
543
|
+
{devMode && (workspaceView === "document" || workspaceView === "project") ? (
|
|
544
|
+
<InlineInspectorLayer
|
|
545
|
+
sourceContainerRef={sourceContainerRef}
|
|
546
|
+
inspector={inspector}
|
|
547
|
+
savedComment={activeInlineSavedComment}
|
|
548
|
+
commentText={inspectorCommentText}
|
|
549
|
+
commentStatus={inspectorCommentStatus}
|
|
550
|
+
commentStatusMessage={inspectorCommentStatusMessage}
|
|
551
|
+
submitDisabled={inspectorCommentDisabled}
|
|
552
|
+
mentionItems={projectMentionItems}
|
|
553
|
+
onOpenSavedComment={handleOpenInlineSavedComment}
|
|
554
|
+
onRemoveSavedComment={handleRemoveInlineSavedComment}
|
|
555
|
+
onCommentTextChange={setInspectorCommentText}
|
|
556
|
+
onSubmitComment={handleSubmitInspectorComment}
|
|
557
|
+
/>
|
|
558
|
+
) : null}
|
|
559
|
+
</main>
|
|
560
|
+
</section>
|
|
561
|
+
|
|
562
|
+
<aside className="reader-side-nav openpress-workspace-panel openpress-public-navigation openpress-dev-public-navigation" aria-label="Workspace panel">
|
|
563
|
+
<button type="button" className="openpress-public-drawer-close" aria-label="關閉目錄" onClick={reader.toggleRightPanel}>
|
|
564
|
+
<X size={16} aria-hidden="true" />
|
|
565
|
+
</button>
|
|
566
|
+
<section className="openpress-public-identity" aria-label="文件資訊">
|
|
567
|
+
<strong>
|
|
568
|
+
<span className="openpress-public-title-main">{projectIdentity.name}</span>
|
|
569
|
+
{projectIdentity.subtitle ? <span className="openpress-public-title-sub">{projectIdentity.subtitle}</span> : null}
|
|
570
|
+
</strong>
|
|
571
|
+
{projectIdentity.label ? <span>{projectIdentity.label}</span> : null}
|
|
572
|
+
</section>
|
|
573
|
+
<div className="openpress-dev-public-tools" aria-label="Workspace">
|
|
574
|
+
<WorkspaceSwitcher workspaceView={workspaceView} onOpenWorkspace={openWorkspace} />
|
|
575
|
+
</div>
|
|
576
|
+
{workspaceView === "document" ? (
|
|
577
|
+
<>
|
|
578
|
+
<section id="openpress-bookmarks" className="openpress-panel-section openpress-panel-section--bookmarks" aria-label="章節書籤">
|
|
579
|
+
<nav className="reader-bookmarks" aria-label="章節導覽" data-openpress-react-bookmarks="true">
|
|
580
|
+
<div className="reader-bookmarks-rail" aria-hidden="true" />
|
|
581
|
+
<Bookmarks items={bookmarks} currentPageIndex={reader.currentPageIndex} onSelectPage={selectWorkspacePage} />
|
|
582
|
+
</nav>
|
|
583
|
+
</section>
|
|
584
|
+
{actionSection}
|
|
585
|
+
<CurrentPagePanel
|
|
586
|
+
currentPageLabel={reader.currentPageLabel}
|
|
587
|
+
totalPageLabel={reader.totalPageLabel}
|
|
588
|
+
progressPercent={reader.progressPercent}
|
|
589
|
+
title={displayPages[reader.currentPageIndex]?.title || document.meta.title}
|
|
590
|
+
pageLabelPrefix={viewMode === "reading" ? "節" : "頁"}
|
|
591
|
+
showHeading={false}
|
|
592
|
+
showTitle={false}
|
|
593
|
+
/>
|
|
594
|
+
</>
|
|
595
|
+
) : null}
|
|
596
|
+
{workspaceView === "project" ? (
|
|
597
|
+
<>
|
|
598
|
+
<ProjectEntryPanel
|
|
599
|
+
mediaAssets={mediaAssets}
|
|
600
|
+
componentUsages={projectComponentUsages}
|
|
601
|
+
mentionItems={projectMentionItems}
|
|
602
|
+
currentSource={displayPages[reader.currentPageIndex]?.source}
|
|
603
|
+
onInsertMention={insertProjectMention}
|
|
604
|
+
/>
|
|
605
|
+
{actionSection}
|
|
606
|
+
</>
|
|
607
|
+
) : null}
|
|
608
|
+
{workspaceView === "comments" ? (
|
|
609
|
+
<>
|
|
610
|
+
<CommentsWorkspace
|
|
611
|
+
comments={pendingComments}
|
|
612
|
+
status={commentsStatus}
|
|
613
|
+
error={commentsError}
|
|
614
|
+
onRefresh={refreshPendingComments}
|
|
615
|
+
onClear={clearPendingComment}
|
|
616
|
+
onClearAll={clearAllPendingComments}
|
|
617
|
+
panel
|
|
618
|
+
/>
|
|
619
|
+
{actionSection}
|
|
620
|
+
</>
|
|
621
|
+
) : null}
|
|
622
|
+
</aside>
|
|
623
|
+
</div>
|
|
624
|
+
</main>
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
interface InspectorLayerRect {
|
|
629
|
+
top: number;
|
|
630
|
+
left: number;
|
|
631
|
+
width: number;
|
|
632
|
+
height: number;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
interface InspectorInsertTargetView {
|
|
636
|
+
blockId: string;
|
|
637
|
+
rect: InspectorLayerRect;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const INSPECTOR_INTENTS: Array<{ intent: InspectorIntent; label: string; icon: typeof Plus }> = [
|
|
641
|
+
{ intent: "add", label: "Add", icon: Plus },
|
|
642
|
+
{ intent: "edit", label: "Edit", icon: Pencil },
|
|
643
|
+
{ intent: "delete", label: "Remove", icon: Trash2 },
|
|
644
|
+
];
|
|
645
|
+
|
|
646
|
+
function InlineInspectorLayer({
|
|
647
|
+
sourceContainerRef,
|
|
648
|
+
inspector,
|
|
649
|
+
savedComment,
|
|
650
|
+
commentText,
|
|
651
|
+
commentStatus,
|
|
652
|
+
commentStatusMessage,
|
|
653
|
+
submitDisabled,
|
|
654
|
+
mentionItems,
|
|
655
|
+
onOpenSavedComment,
|
|
656
|
+
onRemoveSavedComment,
|
|
657
|
+
onCommentTextChange,
|
|
658
|
+
onSubmitComment,
|
|
659
|
+
}: {
|
|
660
|
+
sourceContainerRef: RefObject<HTMLDivElement | null>;
|
|
661
|
+
inspector: InspectorState;
|
|
662
|
+
savedComment: InlineSavedComment | null;
|
|
663
|
+
commentText: string;
|
|
664
|
+
commentStatus: InspectorCommentStatus;
|
|
665
|
+
commentStatusMessage: string;
|
|
666
|
+
submitDisabled: boolean;
|
|
667
|
+
mentionItems: ProjectMentionItem[];
|
|
668
|
+
onOpenSavedComment: (comment: InlineSavedComment) => void;
|
|
669
|
+
onRemoveSavedComment: (comment: InlineSavedComment) => Promise<void>;
|
|
670
|
+
onCommentTextChange: (value: string) => void;
|
|
671
|
+
onSubmitComment: (event?: FormEvent<HTMLFormElement>) => Promise<void>;
|
|
672
|
+
}) {
|
|
673
|
+
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
|
|
674
|
+
const rafRef = useRef<number | null>(null);
|
|
675
|
+
const active = inspector.enabled && inspector.inspectorMode;
|
|
676
|
+
const selectedTarget = inspector.selectedTarget;
|
|
677
|
+
const selectedTargetKey = selectedTarget ? `${selectedTarget.blockId}:${selectedTarget.placement}` : null;
|
|
678
|
+
const savedCommentForTarget = getInlineSavedCommentForTarget(savedComment, selectedTarget);
|
|
679
|
+
const [insertTargets, setInsertTargets] = useState<InspectorInsertTargetView[]>([]);
|
|
680
|
+
const [selectionRect, setSelectionRect] = useState<InspectorLayerRect | null>(null);
|
|
681
|
+
const [composerTargetKey, setComposerTargetKey] = useState<string | null>(null);
|
|
682
|
+
const composerOpen = Boolean(selectedTargetKey && composerTargetKey === selectedTargetKey);
|
|
683
|
+
const markerOnly = Boolean(savedCommentForTarget && !composerOpen);
|
|
684
|
+
const {
|
|
685
|
+
activeMention,
|
|
686
|
+
handleMentionKeyDown,
|
|
687
|
+
highlightedMentionIndex,
|
|
688
|
+
mentionSuggestions,
|
|
689
|
+
setHighlightedMentionIndex,
|
|
690
|
+
setComposerCursor,
|
|
691
|
+
syncCursor,
|
|
692
|
+
insertMention,
|
|
693
|
+
} = useComposerMentions({
|
|
694
|
+
text: commentText,
|
|
695
|
+
items: mentionItems,
|
|
696
|
+
textareaRef,
|
|
697
|
+
onTextChange: onCommentTextChange,
|
|
698
|
+
enabled: composerOpen,
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
const updateLayer = useCallback(() => {
|
|
702
|
+
const root = sourceContainerRef.current;
|
|
703
|
+
if (!active || !root) {
|
|
704
|
+
setInsertTargets([]);
|
|
705
|
+
setSelectionRect(null);
|
|
706
|
+
if (root) syncInspectorSelectedBlock(root, null);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const blockElements = collectInspectorBlockElements(root);
|
|
711
|
+
const nextInsertTargets = createInspectorInsertTargets(blockElements);
|
|
712
|
+
setInsertTargets(nextInsertTargets);
|
|
713
|
+
setSelectionRect(resolveInspectorSelectionRect(root, selectedTarget, nextInsertTargets));
|
|
714
|
+
syncInspectorSelectedBlock(root, markerOnly ? null : selectedTarget);
|
|
715
|
+
}, [active, markerOnly, selectedTarget, sourceContainerRef]);
|
|
716
|
+
|
|
717
|
+
const scheduleLayerUpdate = useCallback(() => {
|
|
718
|
+
if (rafRef.current !== null) window.cancelAnimationFrame(rafRef.current);
|
|
719
|
+
rafRef.current = window.requestAnimationFrame(() => {
|
|
720
|
+
rafRef.current = null;
|
|
721
|
+
updateLayer();
|
|
722
|
+
});
|
|
723
|
+
}, [updateLayer]);
|
|
724
|
+
|
|
725
|
+
useLayoutEffect(() => {
|
|
726
|
+
updateLayer();
|
|
727
|
+
}, [updateLayer]);
|
|
728
|
+
|
|
729
|
+
useEffect(() => {
|
|
730
|
+
if (!active) return undefined;
|
|
731
|
+
const root = sourceContainerRef.current;
|
|
732
|
+
const resizeObserver = typeof ResizeObserver === "undefined" || !root
|
|
733
|
+
? null
|
|
734
|
+
: new ResizeObserver(scheduleLayerUpdate);
|
|
735
|
+
if (root && resizeObserver) resizeObserver.observe(root);
|
|
736
|
+
window.addEventListener("resize", scheduleLayerUpdate);
|
|
737
|
+
window.addEventListener("scroll", scheduleLayerUpdate, true);
|
|
738
|
+
|
|
739
|
+
return () => {
|
|
740
|
+
resizeObserver?.disconnect();
|
|
741
|
+
window.removeEventListener("resize", scheduleLayerUpdate);
|
|
742
|
+
window.removeEventListener("scroll", scheduleLayerUpdate, true);
|
|
743
|
+
if (rafRef.current !== null) window.cancelAnimationFrame(rafRef.current);
|
|
744
|
+
rafRef.current = null;
|
|
745
|
+
};
|
|
746
|
+
}, [active, scheduleLayerUpdate, sourceContainerRef]);
|
|
747
|
+
|
|
748
|
+
useEffect(() => {
|
|
749
|
+
if (!selectedTarget || composerTargetKey !== selectedTargetKey) return;
|
|
750
|
+
const frame = window.requestAnimationFrame(() => textareaRef.current?.focus());
|
|
751
|
+
return () => window.cancelAnimationFrame(frame);
|
|
752
|
+
}, [composerTargetKey, selectedTarget, selectedTargetKey]);
|
|
753
|
+
|
|
754
|
+
useEffect(() => {
|
|
755
|
+
setComposerTargetKey(null);
|
|
756
|
+
}, [selectedTargetKey]);
|
|
757
|
+
|
|
758
|
+
useEffect(() => {
|
|
759
|
+
if (commentStatus === "saved") setComposerTargetKey(null);
|
|
760
|
+
}, [commentStatus]);
|
|
761
|
+
|
|
762
|
+
if (!active) return null;
|
|
763
|
+
|
|
764
|
+
const composerStyle = selectionRect ? createInspectorComposerStyle(selectionRect, composerOpen) : undefined;
|
|
765
|
+
const markerStyle = selectionRect ? createInspectorMarkerStyle(selectionRect) : undefined;
|
|
766
|
+
const visibleIntentItems = savedCommentForTarget
|
|
767
|
+
? INSPECTOR_INTENTS.filter((item) => item.intent !== "add")
|
|
768
|
+
: INSPECTOR_INTENTS;
|
|
769
|
+
const chooseIntent = (intent: InspectorIntent) => {
|
|
770
|
+
if (!selectedTargetKey) return;
|
|
771
|
+
inspector.setCommentIntent(intent);
|
|
772
|
+
setComposerTargetKey(selectedTargetKey);
|
|
773
|
+
};
|
|
774
|
+
const openSavedComment = () => {
|
|
775
|
+
if (!selectedTargetKey || !savedCommentForTarget) return;
|
|
776
|
+
onOpenSavedComment(savedCommentForTarget);
|
|
777
|
+
setComposerTargetKey(selectedTargetKey);
|
|
778
|
+
};
|
|
779
|
+
const handleMarkerClick = () => {
|
|
780
|
+
if (!selectedTargetKey) return;
|
|
781
|
+
if (savedCommentForTarget) {
|
|
782
|
+
openSavedComment();
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
setComposerTargetKey(selectedTargetKey);
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
return (
|
|
789
|
+
<div className="openpress-inline-inspector-layer" data-openpress-inline-inspector-layer>
|
|
790
|
+
{insertTargets.map((target) => {
|
|
791
|
+
const isSelected = selectedTarget?.blockId === target.blockId && selectedTarget.placement === "before";
|
|
792
|
+
return (
|
|
793
|
+
<button
|
|
794
|
+
type="button"
|
|
795
|
+
className={`openpress-inline-insert-target${isSelected ? " is-selected" : ""}`}
|
|
796
|
+
data-openpress-insert-before-block-id={target.blockId}
|
|
797
|
+
style={rectToFixedStyle(target.rect)}
|
|
798
|
+
aria-label="在此新增註解"
|
|
799
|
+
key={target.blockId}
|
|
800
|
+
onClick={() => inspector.selectTarget({ blockId: target.blockId, placement: "before" })}
|
|
801
|
+
/>
|
|
802
|
+
);
|
|
803
|
+
})}
|
|
804
|
+
|
|
805
|
+
{selectionRect && selectedTarget ? (
|
|
806
|
+
<>
|
|
807
|
+
<button
|
|
808
|
+
type="button"
|
|
809
|
+
className="openpress-inline-comment-marker"
|
|
810
|
+
data-openpress-inline-comment-marker
|
|
811
|
+
data-openpress-marker-state={savedCommentForTarget ? "saved" : "draft"}
|
|
812
|
+
style={markerStyle}
|
|
813
|
+
aria-label={savedCommentForTarget ? "編輯註解 1" : "目前選取區塊 1"}
|
|
814
|
+
onClick={handleMarkerClick}
|
|
815
|
+
>
|
|
816
|
+
1
|
|
817
|
+
</button>
|
|
818
|
+
{!markerOnly ? (
|
|
819
|
+
<form
|
|
820
|
+
className="openpress-inline-comment-composer"
|
|
821
|
+
data-openpress-inline-comment-composer
|
|
822
|
+
data-openpress-comment-placement={selectedTarget.placement}
|
|
823
|
+
data-openpress-comment-intent={inspector.commentIntent}
|
|
824
|
+
data-openpress-composer-open={composerOpen ? "true" : "false"}
|
|
825
|
+
data-openpress-composer-saved={savedCommentForTarget ? "true" : "false"}
|
|
826
|
+
style={composerStyle}
|
|
827
|
+
onSubmit={(event) => void onSubmitComment(event)}
|
|
828
|
+
>
|
|
829
|
+
<div className="openpress-inline-comment-composer__intents" aria-label="註解意圖">
|
|
830
|
+
{visibleIntentItems.map((item) => {
|
|
831
|
+
const Icon = item.icon;
|
|
832
|
+
return (
|
|
833
|
+
<button
|
|
834
|
+
type="button"
|
|
835
|
+
className={composerOpen && inspector.commentIntent === item.intent ? "is-active" : ""}
|
|
836
|
+
aria-label={item.label}
|
|
837
|
+
title={item.label}
|
|
838
|
+
aria-pressed={composerOpen && inspector.commentIntent === item.intent}
|
|
839
|
+
key={item.intent}
|
|
840
|
+
onClick={() => {
|
|
841
|
+
if (savedCommentForTarget && item.intent === "delete") {
|
|
842
|
+
void onRemoveSavedComment(savedCommentForTarget);
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
chooseIntent(item.intent);
|
|
846
|
+
}}
|
|
847
|
+
>
|
|
848
|
+
<Icon aria-hidden="true" />
|
|
849
|
+
</button>
|
|
850
|
+
);
|
|
851
|
+
})}
|
|
852
|
+
</div>
|
|
853
|
+
{composerOpen ? (
|
|
854
|
+
<div className="openpress-inline-comment-composer__body">
|
|
855
|
+
<textarea
|
|
856
|
+
ref={textareaRef}
|
|
857
|
+
value={commentText}
|
|
858
|
+
disabled={commentStatus === "submitting"}
|
|
859
|
+
onChange={(event) => {
|
|
860
|
+
onCommentTextChange(event.target.value);
|
|
861
|
+
setComposerCursor(event.target.selectionStart ?? event.target.value.length);
|
|
862
|
+
}}
|
|
863
|
+
onClick={syncCursor}
|
|
864
|
+
onKeyUp={syncCursor}
|
|
865
|
+
onKeyDown={(event) => {
|
|
866
|
+
if (handleMentionKeyDown(event)) return;
|
|
867
|
+
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
|
|
868
|
+
event.preventDefault();
|
|
869
|
+
void onSubmitComment();
|
|
870
|
+
}
|
|
871
|
+
}}
|
|
872
|
+
aria-label={savedCommentForTarget ? "編輯註解" : "新增註解"}
|
|
873
|
+
placeholder="新增註解..."
|
|
874
|
+
rows={3}
|
|
875
|
+
/>
|
|
876
|
+
<button type="submit" disabled={submitDisabled} aria-label="送出註解">
|
|
877
|
+
<ArrowUp aria-hidden="true" />
|
|
878
|
+
</button>
|
|
879
|
+
</div>
|
|
880
|
+
) : null}
|
|
881
|
+
{composerOpen && mentionSuggestions.length > 0 ? (
|
|
882
|
+
<div className="openpress-inline-comment-composer__suggestions" role="listbox" aria-label={activeMention?.trigger === "/" ? "Skill suggestions" : "Mention suggestions"}>
|
|
883
|
+
{mentionSuggestions.map((item, index) => (
|
|
884
|
+
<button
|
|
885
|
+
type="button"
|
|
886
|
+
role="option"
|
|
887
|
+
aria-selected={index === highlightedMentionIndex}
|
|
888
|
+
data-highlighted={index === highlightedMentionIndex ? "true" : undefined}
|
|
889
|
+
key={`${item.kind}-${item.value}`}
|
|
890
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
891
|
+
onMouseEnter={() => setHighlightedMentionIndex(index)}
|
|
892
|
+
onClick={() => insertMention(item)}
|
|
893
|
+
>
|
|
894
|
+
<span>{item.label}</span>
|
|
895
|
+
<small>{item.meta}</small>
|
|
896
|
+
</button>
|
|
897
|
+
))}
|
|
898
|
+
</div>
|
|
899
|
+
) : null}
|
|
900
|
+
{composerOpen && commentStatusMessage ? (
|
|
901
|
+
<p role="status" aria-live="polite" data-openpress-inspector-comment-status={commentStatus}>
|
|
902
|
+
{commentStatusMessage}
|
|
903
|
+
</p>
|
|
904
|
+
) : null}
|
|
905
|
+
</form>
|
|
906
|
+
) : null}
|
|
907
|
+
</>
|
|
908
|
+
) : null}
|
|
909
|
+
</div>
|
|
910
|
+
);
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
function CommentsWorkspace({
|
|
914
|
+
comments,
|
|
915
|
+
status,
|
|
916
|
+
error,
|
|
917
|
+
onRefresh,
|
|
918
|
+
onClear,
|
|
919
|
+
onClearAll,
|
|
920
|
+
panel = false,
|
|
921
|
+
}: {
|
|
922
|
+
comments: PendingComment[];
|
|
923
|
+
status: CommentsWorkspaceStatus;
|
|
924
|
+
error: string;
|
|
925
|
+
onRefresh: () => Promise<void>;
|
|
926
|
+
onClear: (id: string) => Promise<void>;
|
|
927
|
+
onClearAll: () => Promise<void>;
|
|
928
|
+
panel?: boolean;
|
|
929
|
+
}) {
|
|
930
|
+
const busy = status === "loading" || status === "clearing";
|
|
931
|
+
|
|
932
|
+
return (
|
|
933
|
+
<section
|
|
934
|
+
className={`openpress-comments-workspace${panel ? " openpress-comments-workspace--panel" : ""}`}
|
|
935
|
+
data-openpress-comments-workspace
|
|
936
|
+
data-openpress-comments-panel={panel ? "true" : undefined}
|
|
937
|
+
aria-label="待處理註解"
|
|
938
|
+
>
|
|
939
|
+
<header className="openpress-comments-workspace__header">
|
|
940
|
+
<div>
|
|
941
|
+
<span className="openpress-comments-workspace__eyebrow">Comments</span>
|
|
942
|
+
<h1>待處理註解</h1>
|
|
943
|
+
<p>{formatCommentsCount(comments.length, status)}</p>
|
|
944
|
+
</div>
|
|
945
|
+
<div className="openpress-comments-workspace__actions" aria-label="註解操作">
|
|
946
|
+
<button type="button" onClick={() => void onRefresh()} disabled={busy}>
|
|
947
|
+
<RefreshCw aria-hidden="true" />
|
|
948
|
+
<span>{status === "loading" ? "讀取中" : "重新整理"}</span>
|
|
949
|
+
</button>
|
|
950
|
+
<button type="button" onClick={() => void onClearAll()} disabled={busy || comments.length === 0}>
|
|
951
|
+
<Trash2 aria-hidden="true" />
|
|
952
|
+
<span>{status === "clearing" ? "清除中" : "清空全部"}</span>
|
|
953
|
+
</button>
|
|
954
|
+
</div>
|
|
955
|
+
</header>
|
|
956
|
+
|
|
957
|
+
{error ? (
|
|
958
|
+
<p className="openpress-comments-workspace__error" role="alert">
|
|
959
|
+
{error}
|
|
960
|
+
</p>
|
|
961
|
+
) : null}
|
|
962
|
+
|
|
963
|
+
{comments.length === 0 && status !== "loading" ? (
|
|
964
|
+
<div className="openpress-comments-workspace__empty" role="status">
|
|
965
|
+
目前沒有註解
|
|
966
|
+
</div>
|
|
967
|
+
) : (
|
|
968
|
+
<ol className="openpress-comments-list" aria-label="待處理註解列表">
|
|
969
|
+
{comments.map((comment) => {
|
|
970
|
+
const hintMeta = parseCommentHint(comment.hint);
|
|
971
|
+
return (
|
|
972
|
+
<li className="openpress-comment-entry" data-openpress-comment-id={comment.id} key={comment.id}>
|
|
973
|
+
<div className="openpress-comment-entry__body">
|
|
974
|
+
<div className="openpress-comment-entry__topline">
|
|
975
|
+
<p className="openpress-comment-entry__note">{comment.note}</p>
|
|
976
|
+
{hintMeta ? (
|
|
977
|
+
<span className="openpress-comment-entry__intent" data-openpress-comment-intent={hintMeta.intent}>
|
|
978
|
+
{hintMeta.intentLabel}
|
|
979
|
+
</span>
|
|
980
|
+
) : null}
|
|
981
|
+
</div>
|
|
982
|
+
<p className="openpress-comment-entry__meta">
|
|
983
|
+
<code>{comment.path}:{comment.line}</code>
|
|
984
|
+
{comment.timestamp ? <span>{formatCommentTimestamp(comment.timestamp)}</span> : null}
|
|
985
|
+
</p>
|
|
986
|
+
{hintMeta ? (
|
|
987
|
+
<p className="openpress-comment-entry__hint">
|
|
988
|
+
{hintMeta.placementLabel}
|
|
989
|
+
</p>
|
|
990
|
+
) : comment.hint ? (
|
|
991
|
+
<p className="openpress-comment-entry__hint">{comment.hint}</p>
|
|
992
|
+
) : null}
|
|
993
|
+
</div>
|
|
994
|
+
<button
|
|
995
|
+
type="button"
|
|
996
|
+
onClick={() => void onClear(comment.id)}
|
|
997
|
+
disabled={busy}
|
|
998
|
+
aria-label={`清除註解 ${comment.id}`}
|
|
999
|
+
>
|
|
1000
|
+
<Trash2 aria-hidden="true" />
|
|
1001
|
+
<span>清除</span>
|
|
1002
|
+
</button>
|
|
1003
|
+
</li>
|
|
1004
|
+
);
|
|
1005
|
+
})}
|
|
1006
|
+
</ol>
|
|
1007
|
+
)}
|
|
1008
|
+
</section>
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
function collectInspectorBlockElements(root: HTMLElement) {
|
|
1013
|
+
return Array.from(root.querySelectorAll<HTMLElement>("[data-openpress-block-id]")).filter((element) => {
|
|
1014
|
+
if (!element.dataset.openpressBlockId) return false;
|
|
1015
|
+
if (element.parentElement?.closest("[data-openpress-block-id]")) return false;
|
|
1016
|
+
const rect = element.getBoundingClientRect();
|
|
1017
|
+
return rect.width > 0 && rect.height > 0;
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
function createInspectorInsertTargets(elements: HTMLElement[]): InspectorInsertTargetView[] {
|
|
1022
|
+
const targets: InspectorInsertTargetView[] = [];
|
|
1023
|
+
const seen = new Set<string>();
|
|
1024
|
+
|
|
1025
|
+
for (let index = 1; index < elements.length; index += 1) {
|
|
1026
|
+
const previous = elements[index - 1];
|
|
1027
|
+
const current = elements[index];
|
|
1028
|
+
const blockId = current.dataset.openpressBlockId;
|
|
1029
|
+
if (!blockId || seen.has(blockId)) continue;
|
|
1030
|
+
|
|
1031
|
+
const previousPage = previous.closest<HTMLElement>(".openpress-html-page");
|
|
1032
|
+
const currentPage = current.closest<HTMLElement>(".openpress-html-page");
|
|
1033
|
+
if (!previousPage || previousPage !== currentPage) continue;
|
|
1034
|
+
|
|
1035
|
+
const previousRect = previous.getBoundingClientRect();
|
|
1036
|
+
const currentRect = current.getBoundingClientRect();
|
|
1037
|
+
const gap = currentRect.top - previousRect.bottom;
|
|
1038
|
+
if (gap < 10) continue;
|
|
1039
|
+
|
|
1040
|
+
const pageRect = currentPage.getBoundingClientRect();
|
|
1041
|
+
const inset = Math.min(56, Math.max(20, pageRect.width * 0.07));
|
|
1042
|
+
const height = Math.min(28, Math.max(14, gap - 4));
|
|
1043
|
+
const rect = {
|
|
1044
|
+
top: previousRect.bottom + ((gap - height) / 2),
|
|
1045
|
+
left: pageRect.left + inset,
|
|
1046
|
+
width: Math.max(96, pageRect.width - (inset * 2)),
|
|
1047
|
+
height,
|
|
1048
|
+
};
|
|
1049
|
+
if (!isInspectorRectNearViewport(rect)) continue;
|
|
1050
|
+
|
|
1051
|
+
targets.push({ blockId, rect });
|
|
1052
|
+
seen.add(blockId);
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
return targets;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
function resolveInspectorSelectionRect(
|
|
1059
|
+
root: HTMLElement,
|
|
1060
|
+
target: InspectorTarget | null,
|
|
1061
|
+
insertTargets: InspectorInsertTargetView[],
|
|
1062
|
+
): InspectorLayerRect | null {
|
|
1063
|
+
if (!target) return null;
|
|
1064
|
+
if (target.placement === "before") {
|
|
1065
|
+
const insertTarget = insertTargets.find((item) => item.blockId === target.blockId);
|
|
1066
|
+
if (insertTarget) return insertTarget.rect;
|
|
1067
|
+
const block = findInspectorBlockElement(root, target.blockId);
|
|
1068
|
+
if (!block) return null;
|
|
1069
|
+
const rect = block.getBoundingClientRect();
|
|
1070
|
+
return {
|
|
1071
|
+
top: rect.top - 22,
|
|
1072
|
+
left: rect.left,
|
|
1073
|
+
width: rect.width,
|
|
1074
|
+
height: 22,
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
const block = findInspectorBlockElement(root, target.blockId);
|
|
1079
|
+
if (!block) return null;
|
|
1080
|
+
const rect = block.getBoundingClientRect();
|
|
1081
|
+
return {
|
|
1082
|
+
top: rect.top,
|
|
1083
|
+
left: rect.left,
|
|
1084
|
+
width: rect.width,
|
|
1085
|
+
height: rect.height,
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
function findInspectorBlockElement(root: HTMLElement, blockId: string) {
|
|
1090
|
+
return collectInspectorBlockElements(root).find((element) => element.dataset.openpressBlockId === blockId) ?? null;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
function getInlineSavedCommentForTarget(comment: InlineSavedComment | null, target: InspectorTarget | null) {
|
|
1094
|
+
if (!comment || !target) return null;
|
|
1095
|
+
return comment.blockId === target.blockId && comment.placement === target.placement ? comment : null;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function syncInspectorSelectedBlock(root: HTMLElement, target: InspectorTarget | null) {
|
|
1099
|
+
root.querySelectorAll<HTMLElement>('[data-openpress-inspector-selected="true"]').forEach((element) => {
|
|
1100
|
+
delete element.dataset.openpressInspectorSelected;
|
|
1101
|
+
});
|
|
1102
|
+
if (!target || target.placement !== "block") return;
|
|
1103
|
+
const selected = findInspectorBlockElement(root, target.blockId);
|
|
1104
|
+
if (selected) selected.dataset.openpressInspectorSelected = "true";
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function rectToFixedStyle(rect: InspectorLayerRect): CSSProperties {
|
|
1108
|
+
return {
|
|
1109
|
+
top: `${rect.top}px`,
|
|
1110
|
+
left: `${rect.left}px`,
|
|
1111
|
+
width: `${rect.width}px`,
|
|
1112
|
+
height: `${rect.height}px`,
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
function createInspectorComposerStyle(rect: InspectorLayerRect, expanded: boolean): CSSProperties {
|
|
1117
|
+
if (typeof window === "undefined") return {};
|
|
1118
|
+
const targetWidth = expanded ? 460 : 292;
|
|
1119
|
+
const width = Math.min(targetWidth, Math.max(240, window.innerWidth - 32));
|
|
1120
|
+
const preferredLeft = rect.left + (rect.width / 2) - (width / 2);
|
|
1121
|
+
const left = clampNumber(preferredLeft, 16, Math.max(16, window.innerWidth - width - 16));
|
|
1122
|
+
const topAbove = rect.top - 66;
|
|
1123
|
+
const top = topAbove > 12 ? topAbove : rect.top + rect.height + 14;
|
|
1124
|
+
return {
|
|
1125
|
+
top: `${top}px`,
|
|
1126
|
+
left: `${left}px`,
|
|
1127
|
+
width: `${width}px`,
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
function createInspectorMarkerStyle(rect: InspectorLayerRect): CSSProperties {
|
|
1132
|
+
if (typeof window === "undefined") return {};
|
|
1133
|
+
return {
|
|
1134
|
+
top: `${clampNumber(rect.top - 16, 8, Math.max(8, window.innerHeight - 34))}px`,
|
|
1135
|
+
left: `${clampNumber(rect.left - 18, 8, Math.max(8, window.innerWidth - 34))}px`,
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
function clampNumber(value: number, min: number, max: number) {
|
|
1140
|
+
return Math.min(Math.max(value, min), Math.max(min, max));
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
function isInspectorRectNearViewport(rect: InspectorLayerRect, margin = 240) {
|
|
1144
|
+
if (typeof window === "undefined") return true;
|
|
1145
|
+
return rect.top + rect.height >= -margin
|
|
1146
|
+
&& rect.top <= window.innerHeight + margin
|
|
1147
|
+
&& rect.left + rect.width >= -margin
|
|
1148
|
+
&& rect.left <= window.innerWidth + margin;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
function deployButtonText(info: DeploymentInfo, status: DeployStatus) {
|
|
1152
|
+
if (info.configured === false || status === "setup") return "設定部署";
|
|
1153
|
+
if (status === "deploying") return "部署中";
|
|
1154
|
+
if (status === "failed") return "重試部署";
|
|
1155
|
+
if (status === "unavailable") return "本機限定";
|
|
1156
|
+
if (isDeploymentDirty(info, status)) return "重新部署";
|
|
1157
|
+
return "部署";
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
function workbenchPdfButtonText(localPdfEnabled: boolean, status: PdfActionStatus, staticPdfHref?: string) {
|
|
1161
|
+
if (localPdfEnabled) {
|
|
1162
|
+
if (status === "generating") return "產生中";
|
|
1163
|
+
if (status === "opening") return "正在開啟";
|
|
1164
|
+
if (status === "failed") return "重試 PDF";
|
|
1165
|
+
return "產生 PDF";
|
|
1166
|
+
}
|
|
1167
|
+
return !staticPdfHref ? "PDF 未部署" : "開啟 PDF";
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
function workbenchPdfStatusMessage(localPdfEnabled: boolean, status: PdfActionStatus) {
|
|
1171
|
+
if (!localPdfEnabled) return null;
|
|
1172
|
+
if (status === "generating") return "正在產生 PDF";
|
|
1173
|
+
if (status === "opening") return "PDF 已完成,正在開啟";
|
|
1174
|
+
if (status === "failed") return "PDF 產生失敗,請重試";
|
|
1175
|
+
return null;
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
function deploymentStatusKind(info: DeploymentInfo, status: DeployStatus) {
|
|
1179
|
+
if (info.configured === false || status === "setup") return "failed";
|
|
1180
|
+
if (status === "deploying") return "deploying";
|
|
1181
|
+
if (status === "failed") return "failed";
|
|
1182
|
+
if (status === "unavailable") return "unavailable";
|
|
1183
|
+
if (isDeploymentDirty(info, status)) return "dirty";
|
|
1184
|
+
if (status === "deployed" || hasOnlineDeployment(info)) return "online";
|
|
1185
|
+
return "offline";
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
function deploymentStatusLabel(info: DeploymentInfo, status: DeployStatus) {
|
|
1189
|
+
if (info.configured === false || status === "setup") return "缺少設定";
|
|
1190
|
+
if (status === "deploying") return "正在部署";
|
|
1191
|
+
if (status === "failed") return "部署失敗";
|
|
1192
|
+
if (status === "unavailable") return "本機限定";
|
|
1193
|
+
if (isDeploymentDirty(info, status)) return "有更新";
|
|
1194
|
+
if (status === "deployed" || hasOnlineDeployment(info)) return "已上線";
|
|
1195
|
+
return "未上線";
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
function deploymentStatusSummary(info: DeploymentInfo, status: DeployStatus) {
|
|
1199
|
+
const label = deploymentStatusLabel(info, status);
|
|
1200
|
+
if ((status === "deployed" || hasOnlineDeployment(info)) && info.deployedAt) {
|
|
1201
|
+
return `${label} · ${formatDeployTime(info.deployedAt)}`;
|
|
1202
|
+
}
|
|
1203
|
+
return label;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
function deploymentStatusText(info: DeploymentInfo, status: DeployStatus) {
|
|
1207
|
+
if (info.configured === false || status === "setup") {
|
|
1208
|
+
return info.setupMessage ?? "部署設定尚未完成,請先設定 deploy.projectName";
|
|
1209
|
+
}
|
|
1210
|
+
if (status === "deploying") return "部署中";
|
|
1211
|
+
if (status === "failed") return "部署失敗,請查看終端機";
|
|
1212
|
+
if (status === "unavailable") return "目前環境沒有本地部署服務";
|
|
1213
|
+
if (isDeploymentDirty(info, status)) return "已上線但內容有更動,點擊重新部署";
|
|
1214
|
+
if (status === "deployed" || hasOnlineDeployment(info)) {
|
|
1215
|
+
return `已上線${info.deployedAt ? `,更新:${formatDeployTime(info.deployedAt)}` : ""}`;
|
|
1216
|
+
}
|
|
1217
|
+
return "未上線";
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function hasOnlineDeployment(info: DeploymentInfo) {
|
|
1221
|
+
if (info.configured === false) return false;
|
|
1222
|
+
return Boolean(info.online || info.deployedAt || info.publicUrl || (info.pdf && /^https?:\/\//i.test(info.pdf)));
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
function parseDeployError(text: string): {
|
|
1226
|
+
message?: string;
|
|
1227
|
+
deploy_configured?: boolean;
|
|
1228
|
+
deploy_adapter?: string;
|
|
1229
|
+
deploy_source?: string;
|
|
1230
|
+
deploy_project_name?: string;
|
|
1231
|
+
} | null {
|
|
1232
|
+
try {
|
|
1233
|
+
return JSON.parse(text) as {
|
|
1234
|
+
message?: string;
|
|
1235
|
+
deploy_configured?: boolean;
|
|
1236
|
+
deploy_adapter?: string;
|
|
1237
|
+
deploy_source?: string;
|
|
1238
|
+
deploy_project_name?: string;
|
|
1239
|
+
};
|
|
1240
|
+
} catch {
|
|
1241
|
+
return null;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
function isDeploymentDirty(info: DeploymentInfo, status: DeployStatus) {
|
|
1246
|
+
return status === "idle" && hasOnlineDeployment(info) && info.dirty === true;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function formatDeployTime(value: string) {
|
|
1250
|
+
const date = new Date(value);
|
|
1251
|
+
if (Number.isNaN(date.getTime())) return "時間未知";
|
|
1252
|
+
return new Intl.DateTimeFormat("zh-TW", {
|
|
1253
|
+
month: "2-digit",
|
|
1254
|
+
day: "2-digit",
|
|
1255
|
+
hour: "2-digit",
|
|
1256
|
+
minute: "2-digit",
|
|
1257
|
+
hour12: false,
|
|
1258
|
+
}).format(date);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
function formatInspectorSelection(block: SourceBlock | null) {
|
|
1262
|
+
if (!block) return "未選取";
|
|
1263
|
+
const line = block.source?.line;
|
|
1264
|
+
return line ? `${block.path}:${line}` : block.path;
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
function formatInspectorCommentStatus(status: InspectorCommentStatus, error: string) {
|
|
1268
|
+
if (status === "submitting") return "寫入中";
|
|
1269
|
+
if (status === "saved") return "已寫入 source";
|
|
1270
|
+
if (status === "failed") return error || "寫入失敗";
|
|
1271
|
+
return "";
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
function formatCommentsCount(count: number, status: CommentsWorkspaceStatus) {
|
|
1275
|
+
if (status === "loading") return "正在讀取";
|
|
1276
|
+
if (status === "clearing") return "正在清除";
|
|
1277
|
+
return `${count} 則待處理`;
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function parseCommentHint(hint?: string) {
|
|
1281
|
+
if (!hint?.startsWith("openpress-react-inspector")) return null;
|
|
1282
|
+
const intent = hint.match(/\bintent=(add|edit|delete)\b/)?.[1] as InspectorIntent | undefined;
|
|
1283
|
+
const placement = hint.match(/\bplacement=(block|before)\b/)?.[1] as InspectorPlacement | undefined;
|
|
1284
|
+
const intentLabel = intent === "add" ? "Add" : intent === "delete" ? "Remove" : "Edit";
|
|
1285
|
+
const placementLabel = placement === "before" ? "插入於區塊前" : "針對目前區塊";
|
|
1286
|
+
return { intent: intent ?? "edit", intentLabel, placement: placement ?? "block", placementLabel };
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
function formatCommentTimestamp(value: string) {
|
|
1290
|
+
const date = new Date(value);
|
|
1291
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
1292
|
+
return new Intl.DateTimeFormat("zh-TW", {
|
|
1293
|
+
month: "2-digit",
|
|
1294
|
+
day: "2-digit",
|
|
1295
|
+
hour: "2-digit",
|
|
1296
|
+
minute: "2-digit",
|
|
1297
|
+
hour12: false,
|
|
1298
|
+
}).format(date);
|
|
1299
|
+
}
|