@open-press/core 0.7.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/engine/cli.mjs +8 -8
- package/engine/commands/_shared.mjs +37 -15
- package/engine/commands/dev.mjs +2 -2
- package/engine/commands/image.mjs +29 -0
- package/engine/commands/skills-sync.mjs +71 -0
- package/engine/commands/typecheck.mjs +63 -1
- package/engine/commands/upgrade.mjs +3 -3
- package/engine/document-export.mjs +1 -1
- package/engine/output/chrome-pdf.mjs +110 -3
- package/engine/output/static-server.mjs +87 -9
- package/engine/react/comment-endpoint.mjs +13 -39
- package/engine/react/comment-marker.mjs +43 -19
- package/engine/react/document-entry.mjs +46 -28
- package/engine/react/document-export.mjs +328 -164
- package/engine/react/http-json.mjs +24 -0
- package/engine/react/mdx-compile.mjs +126 -3
- package/engine/react/measurement-css.mjs +114 -1
- package/engine/react/object-entities.mjs +204 -0
- package/engine/react/pagination/allocator.mjs +48 -3
- package/engine/react/pagination.mjs +1 -1
- package/engine/react/pipeline/allocate.mjs +41 -72
- package/engine/react/pipeline/frame-measurement.mjs +6 -0
- package/engine/react/press-tree-inspection.mjs +172 -0
- package/engine/react/project-asset-endpoint.mjs +6 -24
- package/engine/react/source-edit-endpoint.d.mts +10 -0
- package/engine/react/source-edit-endpoint.mjs +75 -0
- package/engine/react/sources/mdx-resolver.mjs +13 -15
- package/engine/react/style-discovery.mjs +23 -8
- package/engine/runtime/config.d.mts +8 -0
- package/engine/runtime/config.mjs +57 -60
- package/engine/runtime/file-utils.mjs +9 -1
- package/engine/runtime/file-walk.mjs +22 -0
- package/engine/runtime/inspection.mjs +1 -20
- package/engine/runtime/page-geometry.mjs +131 -0
- package/engine/runtime/path-utils.mjs +20 -0
- package/engine/runtime/source-text-tools.d.mts +102 -0
- package/engine/runtime/source-text-tools.mjs +551 -16
- package/engine/runtime/source-workspace.mjs +16 -34
- package/engine/runtime/validation.mjs +19 -10
- package/package.json +3 -5
- package/src/openpress/app/OpenPressApp.tsx +296 -0
- package/src/openpress/{renderer.tsx → app/OpenPressRuntime.tsx} +20 -9
- package/src/openpress/app/WorkspaceGalleryPage.tsx +219 -0
- package/src/openpress/app/index.ts +2 -0
- package/src/openpress/core/Frame.tsx +26 -15
- package/src/openpress/core/FrameContext.tsx +10 -3
- package/src/openpress/core/MdxArea.tsx +11 -12
- package/src/openpress/core/Press.tsx +25 -4
- package/src/openpress/core/Workspace.tsx +36 -0
- package/src/openpress/core/cn.ts +4 -0
- package/src/openpress/core/index.tsx +11 -3
- package/src/openpress/core/primitives.tsx +74 -6
- package/src/openpress/core/types.ts +94 -41
- package/src/openpress/core/useSource.ts +1 -1
- package/src/openpress/{anchorMap.ts → document-model/anchorMapModel.ts} +1 -1
- package/src/openpress/{indexes.ts → document-model/documentIndexes.ts} +1 -1
- package/src/openpress/{types.ts → document-model/documentTypes.ts} +51 -0
- package/src/openpress/document-model/index.ts +7 -0
- package/src/openpress/document-model/objectEntityModel.ts +55 -0
- package/src/openpress/{projectIdentity.ts → document-model/projectIdentityModel.ts} +1 -1
- package/src/openpress/{reactDocumentMetadata.ts → document-model/reactDocumentMetadataModel.ts} +1 -1
- package/src/openpress/document-model/workspaceManifestModel.ts +57 -0
- package/src/openpress/manuscript/index.tsx +49 -7
- package/src/openpress/mdx/index.ts +15 -7
- package/src/openpress/reader/PageThumbnailsPanel.tsx +168 -0
- package/src/openpress/{publicPage.tsx → reader/PublicReaderPage.tsx} +31 -51
- package/src/openpress/{workbenchPanels.tsx → reader/ReaderNavigationPanel.tsx} +6 -5
- package/src/openpress/reader/index.ts +11 -0
- package/src/openpress/reader/pageViewportScaleModel.ts +73 -0
- package/src/openpress/reader/readerTypes.ts +4 -0
- package/src/openpress/reader/usePageViewportScale.ts +119 -0
- package/src/openpress/reader/usePanelState.ts +56 -0
- package/src/openpress/reader/useReaderHashSync.ts +61 -0
- package/src/openpress/reader/useReaderKeyboardNav.ts +48 -0
- package/src/openpress/reader/useReaderRuntime.ts +146 -0
- package/src/openpress/reader/useReaderScrollAnchor.ts +64 -0
- package/src/openpress/shared/Panel.tsx +77 -0
- package/src/openpress/shared/index.ts +4 -0
- package/src/openpress/shared/numberUtils.ts +3 -0
- package/src/openpress/{runtimeMode.ts → shared/runtimeMode.ts} +0 -11
- package/src/openpress/workbench/Workbench.tsx +506 -0
- package/src/openpress/workbench/actions/DeploymentControl.tsx +157 -0
- package/src/openpress/workbench/actions/ExportImageControl.tsx +96 -0
- package/src/openpress/workbench/actions/PageZoomControl.tsx +182 -0
- package/src/openpress/workbench/actions/SearchControl.tsx +345 -0
- package/src/openpress/workbench/actions/deploymentStatusModel.ts +112 -0
- package/src/openpress/workbench/actions/index.ts +6 -0
- package/src/openpress/workbench/actions/useDeploymentWorkbench.ts +136 -0
- package/src/openpress/workbench/dialog/WorkbenchDialog.tsx +72 -0
- package/src/openpress/workbench/dialog/index.ts +1 -0
- package/src/openpress/workbench/document/components/DocumentPanel.tsx +127 -0
- package/src/openpress/workbench/document/components/InlineSourceEditorLayer.tsx +207 -0
- package/src/openpress/workbench/document/components/ReaderStage.tsx +9 -0
- package/src/openpress/workbench/document/hooks/useDocumentWorkbenchModel.ts +34 -0
- package/src/openpress/workbench/document/hooks/useInlineDocumentEditor.ts +525 -0
- package/src/openpress/workbench/document/index.ts +10 -0
- package/src/openpress/workbench/index.ts +2 -0
- package/src/openpress/workbench/inspector/InlineInspectorLayer.tsx +459 -0
- package/src/openpress/workbench/inspector/index.ts +5 -0
- package/src/openpress/workbench/inspector/inlineCommentModel.ts +125 -0
- package/src/openpress/workbench/inspector/inspectorGeometryModel.ts +160 -0
- package/src/openpress/workbench/inspector/inspectorModel.ts +408 -0
- package/src/openpress/workbench/inspector/useInspectorComments.ts +254 -0
- package/src/openpress/workbench/mentions/MentionSuggestionList.tsx +41 -0
- package/src/openpress/workbench/mentions/index.ts +2 -0
- package/src/openpress/{composerMentions.ts → workbench/mentions/useComposerMentions.ts} +1 -4
- package/src/openpress/workbench/panels/Panel.tsx +1 -0
- package/src/openpress/workbench/panels/PendingCommentsPanel.tsx +80 -0
- package/src/openpress/workbench/panels/WorkbenchControlPanel.tsx +29 -0
- package/src/openpress/workbench/panels/index.ts +3 -0
- package/src/openpress/workbench/project/ProjectEntryPanel.tsx +525 -0
- package/src/openpress/workbench/project/ProjectPreviewDialog.tsx +35 -0
- package/src/openpress/workbench/project/index.ts +2 -0
- package/src/openpress/workbench/project/projectPreviewTypes.ts +11 -0
- package/src/openpress/workbench/shell/WorkbenchShell.tsx +167 -0
- package/src/openpress/workbench/shell/index.ts +1 -0
- package/src/openpress/workbench/workbenchFormatters.ts +120 -0
- package/src/openpress/workbench/workbenchTypes.ts +35 -0
- package/src/styles/openpress/print-route.css +0 -2
- package/src/styles/openpress/{project-workspace.css → project-preview-panel.css} +13 -407
- package/src/styles/openpress/public-viewer.css +25 -320
- package/src/styles/openpress/reader-runtime.css +252 -55
- package/src/styles/openpress/responsive.css +145 -270
- package/src/styles/openpress/workbench-panels.css +327 -178
- package/src/styles/openpress/workbench.css +986 -451
- package/src/styles/openpress/workspace-gallery.css +300 -0
- package/src/styles/openpress.css +2 -1
- package/tsconfig.json +1 -1
- package/vite.config.ts +50 -0
- package/engine/commands/init.mjs +0 -24
- package/engine/init.mjs +0 -90
- package/src/openpress/App.tsx +0 -127
- package/src/openpress/inspector.ts +0 -282
- package/src/openpress/projectWorkspace.tsx +0 -919
- package/src/openpress/readerRuntime.ts +0 -230
- package/src/openpress/workbench.tsx +0 -1265
- package/src/openpress/workbenchTypes.ts +0 -4
- /package/src/openpress/{readerPageRegistry.ts → reader/readerPageRegistry.ts} +0 -0
- /package/src/openpress/{pageRoute.ts → reader/readerPageRoute.ts} +0 -0
- /package/src/openpress/{readerScroll.ts → reader/readerScroll.ts} +0 -0
- /package/src/openpress/{readerState.ts → reader/readerStateModel.ts} +0 -0
- /package/src/openpress/{frameScheduler.ts → shared/frameScheduler.ts} +0 -0
- /package/src/openpress/{projectSources.ts → workbench/project/projectSourceModel.ts} +0 -0
|
@@ -17,6 +17,20 @@ const PUBLIC_DEPLOY_ADAPTERS = new Set([
|
|
|
17
17
|
"vercel",
|
|
18
18
|
]);
|
|
19
19
|
|
|
20
|
+
// A directory is an OpenPress workspace if it contains a
|
|
21
|
+
// press/index.tsx entry, or a package.json with an "openpress" field.
|
|
22
|
+
async function isWorkspaceRoot(dir) {
|
|
23
|
+
try {
|
|
24
|
+
await fs.access(path.join(dir, "press", "index.tsx"));
|
|
25
|
+
return true;
|
|
26
|
+
} catch {}
|
|
27
|
+
try {
|
|
28
|
+
const pkg = JSON.parse(await fs.readFile(path.join(dir, "package.json"), "utf8"));
|
|
29
|
+
if (pkg?.openpress && typeof pkg.openpress === "object") return true;
|
|
30
|
+
} catch {}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
20
34
|
export async function discoverWorkspace(startPath = ".") {
|
|
21
35
|
let current = path.resolve(startPath);
|
|
22
36
|
try {
|
|
@@ -26,15 +40,10 @@ export async function discoverWorkspace(startPath = ".") {
|
|
|
26
40
|
current = path.dirname(current);
|
|
27
41
|
}
|
|
28
42
|
while (true) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
} catch {
|
|
34
|
-
const parent = path.dirname(current);
|
|
35
|
-
if (parent === current) throw new Error(`No OpenPress workspace found from ${startPath}`);
|
|
36
|
-
current = parent;
|
|
37
|
-
}
|
|
43
|
+
if (await isWorkspaceRoot(current)) return current;
|
|
44
|
+
const parent = path.dirname(current);
|
|
45
|
+
if (parent === current) throw new Error(`No OpenPress workspace found from ${startPath}`);
|
|
46
|
+
current = parent;
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
49
|
|
|
@@ -78,7 +87,7 @@ export async function validateWorkspace(root) {
|
|
|
78
87
|
|
|
79
88
|
mark(sourceWorkspace.checkedName);
|
|
80
89
|
if (!(typeof activeConfig.title === "string" && activeConfig.title.trim())) {
|
|
81
|
-
add("warning", "
|
|
90
|
+
add("warning", "press.title", "<Press title> is missing in press/index.tsx; the workbench will show the default placeholder.", activeConfig.configPath);
|
|
82
91
|
}
|
|
83
92
|
if (!(await sourceDirectoryExists(sourceWorkspace))) {
|
|
84
93
|
add("warning", sourceWorkspace.missingCode, sourceWorkspace.missingMessage, sourceWorkspace.sourceDir);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-press/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "open-press core — runtime primitives, CLI, and render pipeline for AI-first fixed-layout documents.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@mdx-js/mdx": "^3.1.1",
|
|
50
50
|
"@mdx-js/react": "^3.1.1",
|
|
51
|
+
"html-to-image": "^1.11.13",
|
|
51
52
|
"js-yaml": "^4.1.1",
|
|
52
53
|
"katex": "^0.16.47",
|
|
53
54
|
"lucide-react": "^1.16.0",
|
|
@@ -80,11 +81,8 @@
|
|
|
80
81
|
"test:e2e:reader": "playwright test --config playwright.reader.config.ts",
|
|
81
82
|
"test:node": "node --test tests/*.test.mjs",
|
|
82
83
|
"test:react": "vitest run",
|
|
83
|
-
"openpress:
|
|
84
|
-
"openpress:export": "node engine/cli.mjs export .",
|
|
84
|
+
"openpress:image": "node engine/cli.mjs image .",
|
|
85
85
|
"openpress:pdf": "node engine/cli.mjs pdf .",
|
|
86
|
-
"openpress:render": "node engine/cli.mjs render . --renderer react",
|
|
87
|
-
"openpress:preview": "node engine/cli.mjs preview . --renderer react",
|
|
88
86
|
"openpress:deploy": "node engine/cli.mjs deploy .",
|
|
89
87
|
"openpress:deploy:dry-run": "node engine/cli.mjs deploy . --confirm --dry-run"
|
|
90
88
|
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { OpenPressRuntime } from "./OpenPressRuntime";
|
|
3
|
+
import { WorkspaceGalleryPage } from "./WorkspaceGalleryPage";
|
|
4
|
+
import { isLocalWorkspaceHost } from "../shared";
|
|
5
|
+
import type {
|
|
6
|
+
DeploymentInfo,
|
|
7
|
+
ReaderDocument,
|
|
8
|
+
WorkspaceManifest,
|
|
9
|
+
WorkspaceManifestPress,
|
|
10
|
+
} from "../document-model";
|
|
11
|
+
import { findManifestPress, manifestHasMultiplePresses } from "../document-model";
|
|
12
|
+
|
|
13
|
+
type LoadState =
|
|
14
|
+
| { status: "loading" }
|
|
15
|
+
| {
|
|
16
|
+
// Gallery state — shown for multi-Press workspaces at the root URL.
|
|
17
|
+
// Single-Press workspaces never reach this state.
|
|
18
|
+
status: "gallery";
|
|
19
|
+
manifest: WorkspaceManifest;
|
|
20
|
+
deploymentInfo: DeploymentInfo;
|
|
21
|
+
}
|
|
22
|
+
| {
|
|
23
|
+
status: "ready";
|
|
24
|
+
document: ReaderDocument;
|
|
25
|
+
deploymentInfo: DeploymentInfo;
|
|
26
|
+
manifest: WorkspaceManifest | null;
|
|
27
|
+
// Empty string for single-Press workspaces (no slug routing needed)
|
|
28
|
+
// or for the root entry of a multi-Press workspace. Otherwise the
|
|
29
|
+
// active press's slug — used by refresh/back/forward to re-resolve.
|
|
30
|
+
activeSlug: string;
|
|
31
|
+
}
|
|
32
|
+
| { status: "error"; message: string };
|
|
33
|
+
|
|
34
|
+
interface DeployConfig {
|
|
35
|
+
pdf?: string;
|
|
36
|
+
deployed_at?: string;
|
|
37
|
+
public_url?: string;
|
|
38
|
+
dirty?: boolean;
|
|
39
|
+
deploy_configured?: boolean;
|
|
40
|
+
deploy_adapter?: string;
|
|
41
|
+
deploy_source?: string;
|
|
42
|
+
deploy_project_name?: string | null;
|
|
43
|
+
deploy_setup_message?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const offlineDeploymentInfo: DeploymentInfo = { online: false };
|
|
47
|
+
|
|
48
|
+
function LoadingScreen() {
|
|
49
|
+
return (
|
|
50
|
+
<div className="openpress-loading-screen" aria-label="載入中" role="status">
|
|
51
|
+
<div className="openpress-loading-screen__inner">
|
|
52
|
+
<div className="openpress-loading-dots" aria-hidden="true">
|
|
53
|
+
<span /><span /><span />
|
|
54
|
+
</div>
|
|
55
|
+
<span className="openpress-loading-screen__label">載入文件</span>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function OpenPressApp() {
|
|
62
|
+
const [state, setState] = useState<LoadState>({ status: "loading" });
|
|
63
|
+
|
|
64
|
+
// Single resolution function — same code path for "boot from URL",
|
|
65
|
+
// "click gallery card", and "browser back button". Given a manifest
|
|
66
|
+
// + slug, decides whether to render gallery or load a press.
|
|
67
|
+
const resolveFromSlug = useCallback(async (
|
|
68
|
+
manifest: WorkspaceManifest | null,
|
|
69
|
+
slug: string,
|
|
70
|
+
deploymentInfo: DeploymentInfo,
|
|
71
|
+
) => {
|
|
72
|
+
// No manifest (legacy deploy): always load /openpress/document.json.
|
|
73
|
+
if (!manifest || manifest.presses.length === 0) {
|
|
74
|
+
const document = await loadReaderDocument("/openpress/document.json");
|
|
75
|
+
setState({ status: "ready", document, deploymentInfo, manifest, activeSlug: "" });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Empty slug + multi-Press: show gallery. Empty slug + single-Press:
|
|
80
|
+
// load the only press. Same expression handles both — array length
|
|
81
|
+
// is the only thing that matters.
|
|
82
|
+
const normalizedSlug = normalizeSlug(slug);
|
|
83
|
+
if (!normalizedSlug && manifestHasMultiplePresses(manifest)) {
|
|
84
|
+
setState({ status: "gallery", manifest, deploymentInfo });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const press = normalizedSlug
|
|
89
|
+
? findManifestPress(manifest, normalizedSlug)
|
|
90
|
+
: manifest.presses[0];
|
|
91
|
+
if (!press) {
|
|
92
|
+
setState({
|
|
93
|
+
status: "error",
|
|
94
|
+
message: `Unknown document slug "/${normalizedSlug}". Known: ${manifest.presses.map((p) => `/${p.slug}`).join(", ")}.`,
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const document = await loadReaderDocument(press.documentUrl);
|
|
99
|
+
setState({ status: "ready", document, deploymentInfo, manifest, activeSlug: press.slug });
|
|
100
|
+
}, []);
|
|
101
|
+
|
|
102
|
+
const refreshDocument = useCallback(async () => {
|
|
103
|
+
if (state.status !== "ready") return;
|
|
104
|
+
const press = state.manifest
|
|
105
|
+
? findManifestPress(state.manifest, state.activeSlug)
|
|
106
|
+
: null;
|
|
107
|
+
const url = press?.documentUrl ?? "/openpress/document.json";
|
|
108
|
+
const document = await loadReaderDocument(url);
|
|
109
|
+
setState((latest) => {
|
|
110
|
+
if (latest.status !== "ready") return latest;
|
|
111
|
+
return { ...latest, document };
|
|
112
|
+
});
|
|
113
|
+
}, [state]);
|
|
114
|
+
|
|
115
|
+
// Gallery click → pushState + load. Bypasses resolveFromSlug's
|
|
116
|
+
// "empty slug + multi-Press → gallery" branch: an explicit click on
|
|
117
|
+
// the unslugged root Press must enter it, not bounce back to gallery.
|
|
118
|
+
const enterPress = useCallback(async (press: WorkspaceManifestPress) => {
|
|
119
|
+
if (state.status !== "gallery") return;
|
|
120
|
+
pushSlug(press.slug);
|
|
121
|
+
setState({ status: "loading" });
|
|
122
|
+
try {
|
|
123
|
+
const document = await loadReaderDocument(press.documentUrl);
|
|
124
|
+
setState({
|
|
125
|
+
status: "ready",
|
|
126
|
+
document,
|
|
127
|
+
deploymentInfo: state.deploymentInfo,
|
|
128
|
+
manifest: state.manifest,
|
|
129
|
+
activeSlug: press.slug,
|
|
130
|
+
});
|
|
131
|
+
} catch (error) {
|
|
132
|
+
setState({
|
|
133
|
+
status: "error",
|
|
134
|
+
message: error instanceof Error ? error.message : "Unable to load OpenPress document.",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}, [state]);
|
|
138
|
+
|
|
139
|
+
// Bootstrap: read URL → load manifest + deploy info → resolve.
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
let cancelled = false;
|
|
142
|
+
|
|
143
|
+
async function bootstrap() {
|
|
144
|
+
try {
|
|
145
|
+
const [manifest, deploymentInfo] = await Promise.all([
|
|
146
|
+
loadWorkspaceManifest(),
|
|
147
|
+
loadDeploymentInfo(),
|
|
148
|
+
]);
|
|
149
|
+
if (cancelled) return;
|
|
150
|
+
await resolveFromSlug(manifest, currentSlugFromLocation(), deploymentInfo);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (!cancelled) {
|
|
153
|
+
setState({
|
|
154
|
+
status: "error",
|
|
155
|
+
message: error instanceof Error ? error.message : "Unable to load OpenPress document.",
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
void bootstrap();
|
|
162
|
+
return () => {
|
|
163
|
+
cancelled = true;
|
|
164
|
+
};
|
|
165
|
+
}, [resolveFromSlug]);
|
|
166
|
+
|
|
167
|
+
// Back / forward button — re-resolve from the new URL.
|
|
168
|
+
useEffect(() => {
|
|
169
|
+
function onPopState() {
|
|
170
|
+
if (state.status === "loading") return;
|
|
171
|
+
const manifest = state.status === "gallery"
|
|
172
|
+
? state.manifest
|
|
173
|
+
: state.status === "ready"
|
|
174
|
+
? state.manifest
|
|
175
|
+
: null;
|
|
176
|
+
const deploymentInfo = state.status === "gallery" || state.status === "ready"
|
|
177
|
+
? state.deploymentInfo
|
|
178
|
+
: offlineDeploymentInfo;
|
|
179
|
+
void resolveFromSlug(manifest, currentSlugFromLocation(), deploymentInfo);
|
|
180
|
+
}
|
|
181
|
+
window.addEventListener("popstate", onPopState);
|
|
182
|
+
return () => window.removeEventListener("popstate", onPopState);
|
|
183
|
+
}, [state, resolveFromSlug]);
|
|
184
|
+
|
|
185
|
+
if (state.status === "loading") return <LoadingScreen />;
|
|
186
|
+
|
|
187
|
+
if (state.status === "error") {
|
|
188
|
+
return <div className="openpress-load-state openpress-load-state--error">{state.message}</div>;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (state.status === "gallery") {
|
|
192
|
+
return <WorkspaceGalleryPage manifest={state.manifest} onSelectPress={enterPress} />;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Only multi-Press workspaces have a gallery to go back to. Single-Press
|
|
196
|
+
// workspaces don't render the button (no destination exists).
|
|
197
|
+
const backToWorkspace = state.manifest && manifestHasMultiplePresses(state.manifest)
|
|
198
|
+
? () => {
|
|
199
|
+
if (state.status !== "ready" || !state.manifest) return;
|
|
200
|
+
pushSlug("");
|
|
201
|
+
setState({
|
|
202
|
+
status: "gallery",
|
|
203
|
+
manifest: state.manifest,
|
|
204
|
+
deploymentInfo: state.deploymentInfo,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
: undefined;
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<OpenPressRuntime
|
|
211
|
+
document={state.document}
|
|
212
|
+
deploymentInfo={state.deploymentInfo}
|
|
213
|
+
onDocumentRefresh={refreshDocument}
|
|
214
|
+
onBackToWorkspace={backToWorkspace}
|
|
215
|
+
/>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function currentSlugFromLocation(): string {
|
|
220
|
+
if (typeof window === "undefined") return "";
|
|
221
|
+
return normalizeSlug(window.location.pathname);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function normalizeSlug(raw: string): string {
|
|
225
|
+
return raw.replace(/^\/+|\/+$/g, "");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function pushSlug(slug: string) {
|
|
229
|
+
if (typeof window === "undefined") return;
|
|
230
|
+
// Preserve the current query string (e.g. ?dev=1 keeps the workbench
|
|
231
|
+
// chrome alive across gallery navigation). Drop the hash — it's a
|
|
232
|
+
// page anchor that means nothing in a different document.
|
|
233
|
+
const pathname = slug ? `/${normalizeSlug(slug)}` : "/";
|
|
234
|
+
const target = `${pathname}${window.location.search}`;
|
|
235
|
+
if (window.location.pathname === pathname) return;
|
|
236
|
+
window.history.pushState({}, "", target);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async function loadWorkspaceManifest(): Promise<WorkspaceManifest | null> {
|
|
240
|
+
// Optional — older deployments don't ship workspace.json. The reader
|
|
241
|
+
// falls back to /openpress/document.json directly when missing, which
|
|
242
|
+
// matches pre-v1.0 behavior.
|
|
243
|
+
try {
|
|
244
|
+
const response = await fetch("/openpress/workspace.json", { cache: "no-store" });
|
|
245
|
+
if (!response.ok) return null;
|
|
246
|
+
return (await response.json()) as WorkspaceManifest;
|
|
247
|
+
} catch {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async function loadReaderDocument(url: string): Promise<ReaderDocument> {
|
|
253
|
+
const response = await fetch(url, { cache: "no-store" });
|
|
254
|
+
if (!response.ok) {
|
|
255
|
+
throw new Error(`Unable to load ${url} (${response.status})`);
|
|
256
|
+
}
|
|
257
|
+
return (await response.json()) as ReaderDocument;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function loadDeploymentInfo(): Promise<DeploymentInfo> {
|
|
261
|
+
if (typeof window !== "undefined" && isLocalWorkspaceHost(window.location.hostname)) {
|
|
262
|
+
const localInfo = await loadDeploymentInfoFrom("/__openpress/status");
|
|
263
|
+
if (localInfo) return localInfo;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return (await loadDeploymentInfoFrom("/openpress/deploy.json")) ?? offlineDeploymentInfo;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function loadDeploymentInfoFrom(path: string): Promise<DeploymentInfo | null> {
|
|
270
|
+
try {
|
|
271
|
+
const response = await fetch(path, { cache: "no-store" });
|
|
272
|
+
if (!response.ok) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
const config = (await response.json()) as DeployConfig;
|
|
276
|
+
return deploymentConfigToInfo(config);
|
|
277
|
+
} catch {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function deploymentConfigToInfo(config: DeployConfig): DeploymentInfo {
|
|
283
|
+
const configured = config.deploy_configured !== false;
|
|
284
|
+
return {
|
|
285
|
+
online: configured && Boolean(config.deployed_at || config.public_url),
|
|
286
|
+
deployedAt: config.deployed_at,
|
|
287
|
+
pdf: typeof config.pdf === "string" ? config.pdf : undefined,
|
|
288
|
+
publicUrl: typeof config.public_url === "string" ? config.public_url : undefined,
|
|
289
|
+
dirty: config.dirty === true,
|
|
290
|
+
configured,
|
|
291
|
+
adapter: typeof config.deploy_adapter === "string" ? config.deploy_adapter : undefined,
|
|
292
|
+
source: typeof config.deploy_source === "string" ? config.deploy_source : undefined,
|
|
293
|
+
projectName: typeof config.deploy_project_name === "string" ? config.deploy_project_name : undefined,
|
|
294
|
+
setupMessage: typeof config.deploy_setup_message === "string" ? config.deploy_setup_message : undefined,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import { useMemo, type CSSProperties } from "react";
|
|
2
|
-
import { PrintDocument, PublicViewer } from "
|
|
3
|
-
import { isPrintModeLocation, isWorkspaceModeLocation } from "
|
|
4
|
-
import { HtmlWorkbench } from "
|
|
2
|
+
import { PrintDocument, PublicViewer } from "../reader";
|
|
3
|
+
import { isPrintModeLocation, isWorkspaceModeLocation } from "../shared";
|
|
4
|
+
import { HtmlWorkbench } from "../workbench";
|
|
5
5
|
import type {
|
|
6
6
|
DeploymentInfo,
|
|
7
7
|
ReaderDocument,
|
|
8
8
|
HtmlPageBlock,
|
|
9
9
|
Theme,
|
|
10
|
-
} from "
|
|
10
|
+
} from "../document-model";
|
|
11
11
|
|
|
12
|
-
interface
|
|
12
|
+
interface OpenPressRuntimeProps {
|
|
13
13
|
document: ReaderDocument;
|
|
14
14
|
deploymentInfo?: DeploymentInfo;
|
|
15
|
+
onDocumentRefresh?: () => void | Promise<void>;
|
|
16
|
+
// Optional — supplied by OpenPressApp when this Press was entered from
|
|
17
|
+
// a multi-Press gallery. Renders a "工作台" home button in the toolbar
|
|
18
|
+
// that returns to the gallery without a full page reload.
|
|
19
|
+
onBackToWorkspace?: () => void;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
export function
|
|
22
|
+
export function OpenPressRuntime({
|
|
18
23
|
document,
|
|
19
24
|
deploymentInfo = { online: false },
|
|
20
|
-
|
|
25
|
+
onDocumentRefresh,
|
|
26
|
+
onBackToWorkspace,
|
|
27
|
+
}: OpenPressRuntimeProps) {
|
|
21
28
|
const style = themeToCssVariables(document.theme);
|
|
22
29
|
const htmlPages = document.blocks.filter((block): block is HtmlPageBlock => block.kind === "htmlPage");
|
|
23
30
|
const workspaceMode = useMemo(() => {
|
|
@@ -45,6 +52,8 @@ export function Renderer({
|
|
|
45
52
|
style={style}
|
|
46
53
|
devMode={workspaceMode}
|
|
47
54
|
deploymentInfo={deploymentInfo}
|
|
55
|
+
onDocumentRefresh={onDocumentRefresh}
|
|
56
|
+
onBackToWorkspace={onBackToWorkspace}
|
|
48
57
|
/>
|
|
49
58
|
);
|
|
50
59
|
}
|
|
@@ -59,11 +68,11 @@ function EmptyState({ style, workspaceMode }: { style: CSSProperties; workspaceM
|
|
|
59
68
|
<p className="openpress-empty-state__eyebrow">OpenPress</p>
|
|
60
69
|
<h1 className="openpress-empty-state__title">This document has no content yet.</h1>
|
|
61
70
|
<p className="openpress-empty-state__body">
|
|
62
|
-
Add React MDX chapter files under <code>
|
|
71
|
+
Add React MDX chapter files under <code>press/chapters/**/content/</code>, then re-build.
|
|
63
72
|
</p>
|
|
64
73
|
{workspaceMode ? (
|
|
65
74
|
<ol className="openpress-empty-state__steps">
|
|
66
|
-
<li><code>npm run
|
|
75
|
+
<li><code>npm run build</code> — validates and refreshes <code>public/openpress/document.json</code></li>
|
|
67
76
|
<li>Reload this page</li>
|
|
68
77
|
</ol>
|
|
69
78
|
) : (
|
|
@@ -85,6 +94,8 @@ function themeToCssVariables(theme?: Theme) {
|
|
|
85
94
|
|
|
86
95
|
if (theme?.pageWidth) style["--openpress-page-width"] = theme.pageWidth;
|
|
87
96
|
if (theme?.pageHeight) style["--openpress-page-height"] = theme.pageHeight;
|
|
97
|
+
if (theme?.pageAspectRatio) style["--openpress-page-aspect-ratio"] = theme.pageAspectRatio;
|
|
98
|
+
if (theme?.pageHeightRatio) style["--openpress-page-height-ratio"] = theme.pageHeightRatio;
|
|
88
99
|
if (theme?.pagePadding) style["--openpress-page-padding"] = theme.pagePadding;
|
|
89
100
|
|
|
90
101
|
return style;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type CSSProperties, type KeyboardEvent } from "react";
|
|
2
|
+
import type { HtmlPageBlock, ReaderDocument, WorkspaceManifest, WorkspaceManifestPress } from "../document-model";
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
manifest: WorkspaceManifest;
|
|
6
|
+
// Called when the reader navigates into a specific Press. The host
|
|
7
|
+
// is responsible for routing (history.pushState, hash, etc.); the
|
|
8
|
+
// gallery just emits the chosen slug.
|
|
9
|
+
onSelectPress: (press: WorkspaceManifestPress) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Reader landing page for multi-Press workspaces. Shows a Figma-style
|
|
13
|
+
// uniform-grid card per Press; each card lazily loads that Press's
|
|
14
|
+
// document.json and renders the first page as a thumbnail preview.
|
|
15
|
+
// Single-Press workspaces skip the gallery entirely.
|
|
16
|
+
export function WorkspaceGalleryPage({ manifest, onSelectPress }: Props) {
|
|
17
|
+
const heading = manifest.name ?? "Workspace";
|
|
18
|
+
const pressCount = String(manifest.presses.length).padStart(2, "0");
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<main className="openpress-workspace-gallery" aria-labelledby="workspace-gallery-heading">
|
|
22
|
+
<header className="openpress-workspace-gallery__header">
|
|
23
|
+
<div className="openpress-workspace-gallery__headline">
|
|
24
|
+
<p className="openpress-workspace-gallery__eyebrow">Workspace</p>
|
|
25
|
+
<h1 id="workspace-gallery-heading">{heading}</h1>
|
|
26
|
+
</div>
|
|
27
|
+
<p className="openpress-workspace-gallery__count">
|
|
28
|
+
<span>{pressCount}</span>
|
|
29
|
+
<small>{manifest.presses.length === 1 ? "document" : "documents"}</small>
|
|
30
|
+
</p>
|
|
31
|
+
</header>
|
|
32
|
+
|
|
33
|
+
<ul className="openpress-workspace-gallery__grid" role="list">
|
|
34
|
+
{manifest.presses.map((press) => (
|
|
35
|
+
<li key={press.slug || "root"} className="openpress-workspace-gallery__item">
|
|
36
|
+
<PressCard press={press} onSelect={() => onSelectPress(press)} />
|
|
37
|
+
</li>
|
|
38
|
+
))}
|
|
39
|
+
</ul>
|
|
40
|
+
</main>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Card is a div+role=button (not <button>) so it can contain the
|
|
45
|
+
// rendered page HTML — buttons may only hold phrasing content, and
|
|
46
|
+
// page HTML is block-level.
|
|
47
|
+
function PressCard({ press, onSelect }: { press: WorkspaceManifestPress; onSelect: () => void }) {
|
|
48
|
+
const handleKey = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
49
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
onSelect();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div
|
|
57
|
+
role="button"
|
|
58
|
+
tabIndex={0}
|
|
59
|
+
className="openpress-workspace-gallery__card"
|
|
60
|
+
onClick={onSelect}
|
|
61
|
+
onKeyDown={handleKey}
|
|
62
|
+
aria-label={`Open ${press.title}`}
|
|
63
|
+
>
|
|
64
|
+
<PressThumbnail press={press} />
|
|
65
|
+
<div className="openpress-workspace-gallery__body">
|
|
66
|
+
<div className="openpress-workspace-gallery__title">{press.title}</div>
|
|
67
|
+
<div className="openpress-workspace-gallery__meta">
|
|
68
|
+
{press.slug ? <span className="openpress-workspace-gallery__slug">{press.slug}</span> : null}
|
|
69
|
+
{press.page?.pageLabel ? (
|
|
70
|
+
<span className="openpress-workspace-gallery__geom">{press.page.pageLabel}</span>
|
|
71
|
+
) : null}
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function PressThumbnail({ press }: { press: WorkspaceManifestPress }) {
|
|
79
|
+
const [state, setState] = useState<ThumbnailState>({ status: "loading" });
|
|
80
|
+
|
|
81
|
+
// Lazy-load each Press's document.json so the gallery doesn't block
|
|
82
|
+
// on a network waterfall when there are many Press. Errors degrade
|
|
83
|
+
// to the geometry-only placeholder used by the loading state.
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
let cancelled = false;
|
|
86
|
+
fetchFirstPage(press.documentUrl).then((page) => {
|
|
87
|
+
if (cancelled) return;
|
|
88
|
+
setState(page ? { status: "ready", page } : { status: "error" });
|
|
89
|
+
}).catch(() => {
|
|
90
|
+
if (!cancelled) setState({ status: "error" });
|
|
91
|
+
});
|
|
92
|
+
return () => { cancelled = true; };
|
|
93
|
+
}, [press.documentUrl]);
|
|
94
|
+
|
|
95
|
+
// Outer card is uniform 4:3 (set in CSS). The page itself letterboxes
|
|
96
|
+
// inside via centered scale, so A4 portrait renders tall-and-narrow,
|
|
97
|
+
// social square renders centered, 16:9 slide stretches edge-to-edge.
|
|
98
|
+
return (
|
|
99
|
+
<div className="openpress-workspace-gallery__thumb" aria-hidden="true">
|
|
100
|
+
{state.status === "ready" ? (
|
|
101
|
+
<PageMiniature page={state.page} press={press} />
|
|
102
|
+
) : (
|
|
103
|
+
<div className="openpress-workspace-gallery__thumb-placeholder" data-state={state.status}>
|
|
104
|
+
<div className="openpress-workspace-gallery__thumb-skel" style={skelAspectStyle(press)} />
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function skelAspectStyle(press: WorkspaceManifestPress): CSSProperties {
|
|
112
|
+
const w = parsePxLength(press.page?.pageWidth);
|
|
113
|
+
const h = parsePxLength(press.page?.pageHeight);
|
|
114
|
+
if (w && h) return { aspectRatio: `${w} / ${h}`, height: "75%" };
|
|
115
|
+
return { aspectRatio: "1 / 1.414", height: "75%" };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function PageMiniature({ page, press }: { page: HtmlPageBlock; press: WorkspaceManifestPress }) {
|
|
119
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
120
|
+
const [scale, setScale] = useState<number | null>(null);
|
|
121
|
+
const pageWidthPx = parsePxLength(press.page?.pageWidth) ?? 1080;
|
|
122
|
+
const pageHeightPx = parsePxLength(press.page?.pageHeight) ?? pageWidthPx;
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
const el = containerRef.current;
|
|
126
|
+
if (!el) return;
|
|
127
|
+
const update = () => {
|
|
128
|
+
const w = el.clientWidth;
|
|
129
|
+
const h = el.clientHeight;
|
|
130
|
+
if (w > 0 && h > 0) {
|
|
131
|
+
setScale(Math.min(w / pageWidthPx, h / pageHeightPx));
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
update();
|
|
135
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
136
|
+
const ro = new ResizeObserver(update);
|
|
137
|
+
ro.observe(el);
|
|
138
|
+
return () => ro.disconnect();
|
|
139
|
+
}, [pageWidthPx, pageHeightPx]);
|
|
140
|
+
|
|
141
|
+
const scaledWidth = scale ? pageWidthPx * scale : 0;
|
|
142
|
+
const scaledHeight = scale ? pageHeightPx * scale : 0;
|
|
143
|
+
const frameStyle: CSSProperties = {
|
|
144
|
+
width: `${scaledWidth}px`,
|
|
145
|
+
height: `${scaledHeight}px`,
|
|
146
|
+
position: "relative",
|
|
147
|
+
visibility: scale ? "visible" : "hidden",
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Match the wrapping used by PublicReaderPage so scoped CSS targeting
|
|
151
|
+
// `.openpress-html-page__html` selectors lights up identically. The
|
|
152
|
+
// outer frame owns centering; the page only scales from its top-left
|
|
153
|
+
// origin, which avoids mixed translate/scale centering drift.
|
|
154
|
+
const pageStyle: CSSProperties = {
|
|
155
|
+
"--openpress-page-width": `${pageWidthPx}px`,
|
|
156
|
+
"--openpress-page-height": `${pageHeightPx}px`,
|
|
157
|
+
width: `${pageWidthPx}px`,
|
|
158
|
+
height: `${pageHeightPx}px`,
|
|
159
|
+
transform: scale ? `scale(${scale})` : undefined,
|
|
160
|
+
transformOrigin: "top left",
|
|
161
|
+
position: "absolute",
|
|
162
|
+
top: 0,
|
|
163
|
+
left: 0,
|
|
164
|
+
} as CSSProperties;
|
|
165
|
+
const pageClass = page.className
|
|
166
|
+
? `openpress-html-page ${page.className}`
|
|
167
|
+
: "openpress-html-page";
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<div className="openpress-workspace-gallery__thumb-stage" ref={containerRef}>
|
|
171
|
+
<div className="openpress-workspace-gallery__thumb-frame" style={frameStyle}>
|
|
172
|
+
<div className={pageClass} style={pageStyle} data-openpress-thumb-page="true">
|
|
173
|
+
<div
|
|
174
|
+
className="openpress-html-page__html"
|
|
175
|
+
// Trusted HTML — same source as the reader's main render path.
|
|
176
|
+
dangerouslySetInnerHTML={{ __html: page.html }}
|
|
177
|
+
/>
|
|
178
|
+
</div>
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
type ThumbnailState =
|
|
185
|
+
| { status: "loading" }
|
|
186
|
+
| { status: "error" }
|
|
187
|
+
| { status: "ready"; page: HtmlPageBlock };
|
|
188
|
+
|
|
189
|
+
async function fetchFirstPage(url: string): Promise<HtmlPageBlock | null> {
|
|
190
|
+
try {
|
|
191
|
+
const response = await fetch(url, { cache: "no-store" });
|
|
192
|
+
if (!response.ok) return null;
|
|
193
|
+
const doc = (await response.json()) as ReaderDocument;
|
|
194
|
+
const firstPage = doc.blocks.find((b): b is HtmlPageBlock => b.kind === "htmlPage");
|
|
195
|
+
return firstPage ?? null;
|
|
196
|
+
} catch {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Convert a CSS length string (px / mm / cm / in) into device pixels
|
|
202
|
+
// at 96 dpi. A4 pages are stored as "210mm" / "297mm" so the gallery
|
|
203
|
+
// and thumbnail scalers need this to compute their fit ratio — using
|
|
204
|
+
// the bare string would always fall back to the default fallback.
|
|
205
|
+
function parsePxLength(value: string | undefined): number | null {
|
|
206
|
+
if (!value) return null;
|
|
207
|
+
const match = value.trim().match(/^([\d.]+)\s*(px|mm|cm|in)$/i);
|
|
208
|
+
if (!match) return null;
|
|
209
|
+
const n = Number(match[1]);
|
|
210
|
+
if (!Number.isFinite(n) || n <= 0) return null;
|
|
211
|
+
const unit = match[2].toLowerCase();
|
|
212
|
+
switch (unit) {
|
|
213
|
+
case "px": return n;
|
|
214
|
+
case "mm": return n * (96 / 25.4);
|
|
215
|
+
case "cm": return n * (96 / 2.54);
|
|
216
|
+
case "in": return n * 96;
|
|
217
|
+
default: return null;
|
|
218
|
+
}
|
|
219
|
+
}
|