@open-press/cli 1.0.0 → 1.1.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.
Files changed (175) hide show
  1. package/README.md +11 -12
  2. package/dist/cli.js +298 -79
  3. package/package.json +9 -7
  4. package/template/core/AGENTS.md +0 -130
  5. package/template/core/CHANGELOG.md +0 -218
  6. package/template/core/README.md +0 -43
  7. package/template/core/engine/cli.mjs +0 -96
  8. package/template/core/engine/commands/_shared.mjs +0 -199
  9. package/template/core/engine/commands/deploy.mjs +0 -31
  10. package/template/core/engine/commands/dev.mjs +0 -49
  11. package/template/core/engine/commands/doctor.mjs +0 -229
  12. package/template/core/engine/commands/export.mjs +0 -8
  13. package/template/core/engine/commands/image.mjs +0 -29
  14. package/template/core/engine/commands/inspect.mjs +0 -35
  15. package/template/core/engine/commands/pdf.mjs +0 -26
  16. package/template/core/engine/commands/preview.mjs +0 -26
  17. package/template/core/engine/commands/render.mjs +0 -17
  18. package/template/core/engine/commands/replace.mjs +0 -41
  19. package/template/core/engine/commands/search.mjs +0 -33
  20. package/template/core/engine/commands/skills-sync.mjs +0 -71
  21. package/template/core/engine/commands/typecheck.mjs +0 -67
  22. package/template/core/engine/commands/upgrade.mjs +0 -159
  23. package/template/core/engine/commands/validate.mjs +0 -17
  24. package/template/core/engine/document-export.mjs +0 -15
  25. package/template/core/engine/output/chrome-pdf.d.mts +0 -34
  26. package/template/core/engine/output/chrome-pdf.mjs +0 -450
  27. package/template/core/engine/output/deploy-sync.mjs +0 -15
  28. package/template/core/engine/output/fonts.mjs +0 -62
  29. package/template/core/engine/output/katex-assets.mjs +0 -45
  30. package/template/core/engine/output/page-block.mjs +0 -30
  31. package/template/core/engine/output/pdf-media.mjs +0 -45
  32. package/template/core/engine/output/public-assets.mjs +0 -19
  33. package/template/core/engine/output/static-server.mjs +0 -571
  34. package/template/core/engine/react/caption-numbering.mjs +0 -73
  35. package/template/core/engine/react/comment-endpoint.d.mts +0 -11
  36. package/template/core/engine/react/comment-endpoint.mjs +0 -102
  37. package/template/core/engine/react/comment-marker.mjs +0 -374
  38. package/template/core/engine/react/document-entry.mjs +0 -331
  39. package/template/core/engine/react/document-export.mjs +0 -512
  40. package/template/core/engine/react/http-json.mjs +0 -24
  41. package/template/core/engine/react/mdx-compile.mjs +0 -629
  42. package/template/core/engine/react/measurement-css.mjs +0 -157
  43. package/template/core/engine/react/object-entities.mjs +0 -204
  44. package/template/core/engine/react/pagination/allocator.mjs +0 -167
  45. package/template/core/engine/react/pagination/regions.mjs +0 -81
  46. package/template/core/engine/react/pagination-constants.mjs +0 -3
  47. package/template/core/engine/react/pagination.mjs +0 -9
  48. package/template/core/engine/react/pipeline/allocate.mjs +0 -217
  49. package/template/core/engine/react/pipeline/final-render.mjs +0 -94
  50. package/template/core/engine/react/pipeline/frame-measurement.mjs +0 -306
  51. package/template/core/engine/react/pipeline/press-tree.mjs +0 -135
  52. package/template/core/engine/react/press-tree-inspection.mjs +0 -172
  53. package/template/core/engine/react/project-asset-endpoint.d.mts +0 -10
  54. package/template/core/engine/react/project-asset-endpoint.mjs +0 -361
  55. package/template/core/engine/react/section-css.mjs +0 -56
  56. package/template/core/engine/react/source-edit-endpoint.d.mts +0 -10
  57. package/template/core/engine/react/source-edit-endpoint.mjs +0 -75
  58. package/template/core/engine/react/sources/heading-numbering.mjs +0 -132
  59. package/template/core/engine/react/sources/mdx-resolver.mjs +0 -439
  60. package/template/core/engine/react/style-discovery.mjs +0 -160
  61. package/template/core/engine/runtime/config.d.mts +0 -48
  62. package/template/core/engine/runtime/config.mjs +0 -172
  63. package/template/core/engine/runtime/file-utils.mjs +0 -114
  64. package/template/core/engine/runtime/file-walk.mjs +0 -22
  65. package/template/core/engine/runtime/inspection.mjs +0 -328
  66. package/template/core/engine/runtime/issue-report.mjs +0 -44
  67. package/template/core/engine/runtime/page-geometry.mjs +0 -131
  68. package/template/core/engine/runtime/path-utils.mjs +0 -20
  69. package/template/core/engine/runtime/source-text-tools.d.mts +0 -102
  70. package/template/core/engine/runtime/source-text-tools.mjs +0 -832
  71. package/template/core/engine/runtime/source-workspace.mjs +0 -168
  72. package/template/core/engine/runtime/validation.mjs +0 -183
  73. package/template/core/index.html +0 -13
  74. package/template/core/openpress.config.mjs +0 -8
  75. package/template/core/package.json +0 -89
  76. package/template/core/src/main.tsx +0 -16
  77. package/template/core/src/openpress/app/OpenPressApp.tsx +0 -296
  78. package/template/core/src/openpress/app/OpenPressRuntime.tsx +0 -102
  79. package/template/core/src/openpress/app/WorkspaceGalleryPage.tsx +0 -219
  80. package/template/core/src/openpress/app/index.ts +0 -2
  81. package/template/core/src/openpress/core/Frame.tsx +0 -91
  82. package/template/core/src/openpress/core/FrameContext.tsx +0 -26
  83. package/template/core/src/openpress/core/MdxArea.tsx +0 -34
  84. package/template/core/src/openpress/core/Press.tsx +0 -55
  85. package/template/core/src/openpress/core/Workspace.tsx +0 -36
  86. package/template/core/src/openpress/core/cn.ts +0 -4
  87. package/template/core/src/openpress/core/index.tsx +0 -47
  88. package/template/core/src/openpress/core/primitives.tsx +0 -91
  89. package/template/core/src/openpress/core/types.ts +0 -236
  90. package/template/core/src/openpress/core/useSource.ts +0 -28
  91. package/template/core/src/openpress/document-model/anchorMapModel.ts +0 -27
  92. package/template/core/src/openpress/document-model/documentIndexes.ts +0 -329
  93. package/template/core/src/openpress/document-model/documentTypes.ts +0 -147
  94. package/template/core/src/openpress/document-model/index.ts +0 -7
  95. package/template/core/src/openpress/document-model/objectEntityModel.ts +0 -55
  96. package/template/core/src/openpress/document-model/projectIdentityModel.ts +0 -15
  97. package/template/core/src/openpress/document-model/reactDocumentMetadataModel.ts +0 -27
  98. package/template/core/src/openpress/document-model/workspaceManifestModel.ts +0 -57
  99. package/template/core/src/openpress/manuscript/index.tsx +0 -238
  100. package/template/core/src/openpress/mdx/index.ts +0 -96
  101. package/template/core/src/openpress/numbering/index.ts +0 -294
  102. package/template/core/src/openpress/reader/PageThumbnailsPanel.tsx +0 -168
  103. package/template/core/src/openpress/reader/PublicReaderPage.tsx +0 -267
  104. package/template/core/src/openpress/reader/ReaderNavigationPanel.tsx +0 -123
  105. package/template/core/src/openpress/reader/index.ts +0 -11
  106. package/template/core/src/openpress/reader/pageViewportScaleModel.ts +0 -73
  107. package/template/core/src/openpress/reader/readerPageRegistry.ts +0 -41
  108. package/template/core/src/openpress/reader/readerPageRoute.ts +0 -21
  109. package/template/core/src/openpress/reader/readerScroll.ts +0 -92
  110. package/template/core/src/openpress/reader/readerStateModel.ts +0 -15
  111. package/template/core/src/openpress/reader/readerTypes.ts +0 -4
  112. package/template/core/src/openpress/reader/usePageViewportScale.ts +0 -119
  113. package/template/core/src/openpress/reader/usePanelState.ts +0 -56
  114. package/template/core/src/openpress/reader/useReaderHashSync.ts +0 -61
  115. package/template/core/src/openpress/reader/useReaderKeyboardNav.ts +0 -48
  116. package/template/core/src/openpress/reader/useReaderRuntime.ts +0 -146
  117. package/template/core/src/openpress/reader/useReaderScrollAnchor.ts +0 -64
  118. package/template/core/src/openpress/shared/Panel.tsx +0 -77
  119. package/template/core/src/openpress/shared/frameScheduler.ts +0 -32
  120. package/template/core/src/openpress/shared/index.ts +0 -4
  121. package/template/core/src/openpress/shared/numberUtils.ts +0 -3
  122. package/template/core/src/openpress/shared/runtimeMode.ts +0 -11
  123. package/template/core/src/openpress/workbench/Workbench.tsx +0 -506
  124. package/template/core/src/openpress/workbench/actions/DeploymentControl.tsx +0 -157
  125. package/template/core/src/openpress/workbench/actions/ExportImageControl.tsx +0 -96
  126. package/template/core/src/openpress/workbench/actions/PageZoomControl.tsx +0 -182
  127. package/template/core/src/openpress/workbench/actions/SearchControl.tsx +0 -345
  128. package/template/core/src/openpress/workbench/actions/deploymentStatusModel.ts +0 -112
  129. package/template/core/src/openpress/workbench/actions/index.ts +0 -6
  130. package/template/core/src/openpress/workbench/actions/useDeploymentWorkbench.ts +0 -136
  131. package/template/core/src/openpress/workbench/dialog/WorkbenchDialog.tsx +0 -72
  132. package/template/core/src/openpress/workbench/dialog/index.ts +0 -1
  133. package/template/core/src/openpress/workbench/document/components/DocumentPanel.tsx +0 -127
  134. package/template/core/src/openpress/workbench/document/components/InlineSourceEditorLayer.tsx +0 -207
  135. package/template/core/src/openpress/workbench/document/components/ReaderStage.tsx +0 -9
  136. package/template/core/src/openpress/workbench/document/hooks/useDocumentWorkbenchModel.ts +0 -34
  137. package/template/core/src/openpress/workbench/document/hooks/useInlineDocumentEditor.ts +0 -525
  138. package/template/core/src/openpress/workbench/document/index.ts +0 -10
  139. package/template/core/src/openpress/workbench/index.ts +0 -2
  140. package/template/core/src/openpress/workbench/inspector/InlineInspectorLayer.tsx +0 -459
  141. package/template/core/src/openpress/workbench/inspector/index.ts +0 -5
  142. package/template/core/src/openpress/workbench/inspector/inlineCommentModel.ts +0 -125
  143. package/template/core/src/openpress/workbench/inspector/inspectorGeometryModel.ts +0 -160
  144. package/template/core/src/openpress/workbench/inspector/inspectorModel.ts +0 -408
  145. package/template/core/src/openpress/workbench/inspector/useInspectorComments.ts +0 -254
  146. package/template/core/src/openpress/workbench/mentions/MentionSuggestionList.tsx +0 -41
  147. package/template/core/src/openpress/workbench/mentions/index.ts +0 -2
  148. package/template/core/src/openpress/workbench/mentions/useComposerMentions.ts +0 -185
  149. package/template/core/src/openpress/workbench/panels/Panel.tsx +0 -1
  150. package/template/core/src/openpress/workbench/panels/PendingCommentsPanel.tsx +0 -80
  151. package/template/core/src/openpress/workbench/panels/WorkbenchControlPanel.tsx +0 -29
  152. package/template/core/src/openpress/workbench/panels/index.ts +0 -3
  153. package/template/core/src/openpress/workbench/project/ProjectEntryPanel.tsx +0 -525
  154. package/template/core/src/openpress/workbench/project/ProjectPreviewDialog.tsx +0 -35
  155. package/template/core/src/openpress/workbench/project/index.ts +0 -2
  156. package/template/core/src/openpress/workbench/project/projectPreviewTypes.ts +0 -11
  157. package/template/core/src/openpress/workbench/project/projectSourceModel.ts +0 -24
  158. package/template/core/src/openpress/workbench/shell/WorkbenchShell.tsx +0 -167
  159. package/template/core/src/openpress/workbench/shell/index.ts +0 -1
  160. package/template/core/src/openpress/workbench/workbenchFormatters.ts +0 -120
  161. package/template/core/src/openpress/workbench/workbenchTypes.ts +0 -35
  162. package/template/core/src/styles/openpress/app-shell.css +0 -251
  163. package/template/core/src/styles/openpress/media-workspace.css +0 -230
  164. package/template/core/src/styles/openpress/print-route.css +0 -184
  165. package/template/core/src/styles/openpress/project-preview-panel.css +0 -924
  166. package/template/core/src/styles/openpress/public-viewer.css +0 -688
  167. package/template/core/src/styles/openpress/reader-runtime.css +0 -989
  168. package/template/core/src/styles/openpress/responsive.css +0 -245
  169. package/template/core/src/styles/openpress/workbench-panels.css +0 -707
  170. package/template/core/src/styles/openpress/workbench.css +0 -1255
  171. package/template/core/src/styles/openpress/workspace-gallery.css +0 -300
  172. package/template/core/src/styles/openpress.css +0 -15
  173. package/template/core/src/vite-env.d.ts +0 -9
  174. package/template/core/tsconfig.json +0 -40
  175. package/template/core/vite.config.ts +0 -584
@@ -1,160 +0,0 @@
1
- import type { CSSProperties } from "react";
2
- import type { ObjectSelection } from "./inspectorModel";
3
- import { clampNumber } from "../../shared";
4
- import type { InspectorInsertTargetView, InspectorLayerRect } from "../workbenchTypes";
5
-
6
- export const INSPECTOR_MARKER_RAIL_WIDTH = 36;
7
- const INSPECTOR_MARKER_MIN_HEIGHT = 32;
8
- const INSPECTOR_MARKER_VIEWPORT_PADDING = 8;
9
-
10
- export function collectInspectorBlockElements(root: HTMLElement) {
11
- return Array.from(root.querySelectorAll<HTMLElement>("[data-openpress-block-id]")).filter((element) => {
12
- if (!element.dataset.openpressBlockId) return false;
13
- if (element.parentElement?.closest("[data-openpress-block-id]")) return false;
14
- const rect = element.getBoundingClientRect();
15
- return rect.width > 0 && rect.height > 0;
16
- });
17
- }
18
-
19
- export function createInspectorInsertTargets(elements: HTMLElement[]): InspectorInsertTargetView[] {
20
- const targets: InspectorInsertTargetView[] = [];
21
- const seen = new Set<string>();
22
-
23
- for (let index = 1; index < elements.length; index += 1) {
24
- const previous = elements[index - 1];
25
- const current = elements[index];
26
- const blockId = current.dataset.openpressBlockId;
27
- if (!blockId || seen.has(blockId)) continue;
28
-
29
- const previousPage = previous.closest<HTMLElement>(".openpress-html-page");
30
- const currentPage = current.closest<HTMLElement>(".openpress-html-page");
31
- if (!previousPage || previousPage !== currentPage) continue;
32
-
33
- const previousRect = previous.getBoundingClientRect();
34
- const currentRect = current.getBoundingClientRect();
35
- const gap = currentRect.top - previousRect.bottom;
36
- if (gap < 10) continue;
37
-
38
- const pageRect = currentPage.getBoundingClientRect();
39
- const inset = Math.min(56, Math.max(20, pageRect.width * 0.07));
40
- const height = Math.min(28, Math.max(14, gap - 4));
41
- const rect = {
42
- top: previousRect.bottom + ((gap - height) / 2),
43
- left: pageRect.left + inset,
44
- width: Math.max(96, pageRect.width - (inset * 2)),
45
- height,
46
- };
47
- if (!isInspectorRectNearViewport(rect)) continue;
48
-
49
- targets.push({ blockId, rect });
50
- seen.add(blockId);
51
- }
52
-
53
- return targets;
54
- }
55
-
56
- export function resolveInspectorSelectionRect(
57
- root: HTMLElement,
58
- target: ObjectSelection | null,
59
- insertTargets: InspectorInsertTargetView[],
60
- ): InspectorLayerRect | null {
61
- if (!target) return null;
62
- if (target.placement === "before" && target.blockId) {
63
- const insertTarget = insertTargets.find((item) => item.blockId === target.blockId);
64
- if (insertTarget) return insertTarget.rect;
65
- const block = findInspectorBlockElement(root, target.blockId);
66
- if (!block) return null;
67
- const rect = block.getBoundingClientRect();
68
- return {
69
- top: rect.top - 22,
70
- left: rect.left,
71
- width: rect.width,
72
- height: 22,
73
- };
74
- }
75
-
76
- const selector = target.objectId
77
- ? `[data-openpress-object-id="${cssEscape(target.objectId)}"]`
78
- : target.blockId
79
- ? `[data-openpress-block-id="${cssEscape(target.blockId)}"]`
80
- : "";
81
- const element = selector ? root.querySelector<HTMLElement>(selector) : null;
82
- if (!element) return null;
83
- const rect = element.getBoundingClientRect();
84
- return {
85
- top: rect.top,
86
- left: rect.left,
87
- width: rect.width,
88
- height: rect.height,
89
- };
90
- }
91
-
92
- export function findInspectorBlockElement(root: HTMLElement, blockId: string) {
93
- return collectInspectorBlockElements(root).find((element) => element.dataset.openpressBlockId === blockId) ?? null;
94
- }
95
-
96
- export function syncInspectorSelectedBlock(root: HTMLElement, target: ObjectSelection | null) {
97
- root.querySelectorAll<HTMLElement>('[data-openpress-inspector-selected="true"]').forEach((element) => {
98
- delete element.dataset.openpressInspectorSelected;
99
- });
100
- if (!target || target.placement !== "block") return;
101
- const selected = target.objectId
102
- ? root.querySelector<HTMLElement>(`[data-openpress-object-id="${cssEscape(target.objectId)}"]`)
103
- : target.blockId
104
- ? findInspectorBlockElement(root, target.blockId)
105
- : null;
106
- if (selected) selected.dataset.openpressInspectorSelected = "true";
107
- }
108
-
109
- export function rectToFixedStyle(rect: InspectorLayerRect): CSSProperties {
110
- return {
111
- top: `${rect.top}px`,
112
- left: `${rect.left}px`,
113
- width: `${rect.width}px`,
114
- height: `${rect.height}px`,
115
- };
116
- }
117
-
118
- export function createInspectorComposerStyle(rect: InspectorLayerRect, expanded: boolean): CSSProperties {
119
- if (typeof window === "undefined") return {};
120
- const targetWidth = expanded ? 460 : 292;
121
- const width = Math.min(targetWidth, Math.max(240, window.innerWidth - 32));
122
- const preferredLeft = rect.left + (rect.width / 2) - (width / 2);
123
- const left = clampNumber(preferredLeft, 16, Math.max(16, window.innerWidth - width - 16));
124
- const topAbove = rect.top - 66;
125
- const top = topAbove > 12 ? topAbove : rect.top + rect.height + 14;
126
- return {
127
- top: `${top}px`,
128
- left: `${left}px`,
129
- width: `${width}px`,
130
- };
131
- }
132
-
133
- export function createInspectorMarkerStyle(rect: InspectorLayerRect): CSSProperties {
134
- if (typeof window === "undefined") return {};
135
- const height = Math.max(INSPECTOR_MARKER_MIN_HEIGHT, rect.height);
136
- const maxTop = Math.max(INSPECTOR_MARKER_VIEWPORT_PADDING, window.innerHeight - height - INSPECTOR_MARKER_VIEWPORT_PADDING);
137
- const maxLeft = Math.max(
138
- INSPECTOR_MARKER_VIEWPORT_PADDING,
139
- window.innerWidth - INSPECTOR_MARKER_RAIL_WIDTH - INSPECTOR_MARKER_VIEWPORT_PADDING,
140
- );
141
-
142
- return {
143
- top: `${clampNumber(rect.top, INSPECTOR_MARKER_VIEWPORT_PADDING, maxTop)}px`,
144
- left: `${clampNumber(rect.left - INSPECTOR_MARKER_RAIL_WIDTH, INSPECTOR_MARKER_VIEWPORT_PADDING, maxLeft)}px`,
145
- width: `${INSPECTOR_MARKER_RAIL_WIDTH}px`,
146
- height: `${height}px`,
147
- };
148
- }
149
-
150
- function isInspectorRectNearViewport(rect: InspectorLayerRect, margin = 240) {
151
- if (typeof window === "undefined") return true;
152
- return rect.top + rect.height >= -margin
153
- && rect.top <= window.innerHeight + margin
154
- && rect.left + rect.width >= -margin
155
- && rect.left <= window.innerWidth + margin;
156
- }
157
-
158
- function cssEscape(value: string) {
159
- return globalThis.CSS?.escape ? globalThis.CSS.escape(value) : value.replace(/["\\]/g, "\\$&");
160
- }
@@ -1,408 +0,0 @@
1
- import { useCallback, useEffect, useMemo, useState, type MouseEvent as ReactMouseEvent } from "react";
2
- import { getObjectEntityMap, getSourceBlockMap } from "../../document-model";
3
- import type {
4
- EditableSourceRef,
5
- ObjectEntity,
6
- ObjectEntityRef,
7
- ReaderDocument,
8
- SourceBlock,
9
- } from "../../document-model";
10
-
11
- const DEFAULT_INSPECTOR_STORAGE_KEY = "openpress:inspector-mode";
12
-
13
- export type InspectorPlacement = "block" | "before";
14
-
15
- export interface ObjectSelection {
16
- objectId?: string;
17
- entityRef?: ObjectEntityRef;
18
- blockId?: string;
19
- placement: InspectorPlacement;
20
- objectEntity?: ObjectEntity;
21
- }
22
-
23
- export type InspectorTarget = ObjectSelection;
24
-
25
- export interface CommentDraft {
26
- entityRef: ObjectEntityRef;
27
- label?: string;
28
- blockId?: string;
29
- targetObjectId?: string;
30
- placement: InspectorPlacement;
31
- source: EditableSourceRef;
32
- note: string;
33
- }
34
-
35
- export interface InspectorState {
36
- enabled: boolean;
37
- inspectorMode: boolean;
38
- selectedBlockId: string | null;
39
- selectedBlock: SourceBlock | null;
40
- selectedObjectEntity: ObjectEntity | null;
41
- selectedSelection: ObjectSelection | null;
42
- selectedTarget: ObjectSelection | null;
43
- setInspectorMode: (enabled: boolean) => void;
44
- toggleInspectorMode: () => void;
45
- selectSelection: (target: ObjectSelection | null) => SourceBlock | null;
46
- inspectSelection: (target: EventTarget | null) => SourceBlock | null;
47
- selectTarget: (target: ObjectSelection | null) => SourceBlock | null;
48
- inspectTarget: (target: EventTarget | null) => SourceBlock | null;
49
- handleClick: (event: ReactMouseEvent) => boolean;
50
- }
51
-
52
- export interface InspectorCommentResult {
53
- ok: boolean;
54
- comment?: {
55
- id?: string;
56
- timestamp?: string;
57
- path?: string;
58
- line?: number;
59
- note?: string;
60
- hint?: string;
61
- };
62
- message?: string;
63
- }
64
-
65
- export interface PendingComment {
66
- id: string;
67
- timestamp?: string;
68
- path: string;
69
- absolutePath?: string;
70
- line: number;
71
- marker?: string;
72
- note: string;
73
- hint?: string;
74
- }
75
-
76
- export interface CommentListResult {
77
- ok: boolean;
78
- comments?: PendingComment[];
79
- message?: string;
80
- }
81
-
82
- export interface CommentClearResult {
83
- ok: boolean;
84
- removedCount: number;
85
- comments?: PendingComment[];
86
- message?: string;
87
- }
88
-
89
- async function requestInspectorJson<T extends { message?: string }>({
90
- endpoint,
91
- fetchImpl,
92
- method,
93
- body,
94
- errorPrefix,
95
- }: {
96
- endpoint: string;
97
- fetchImpl?: typeof fetch;
98
- method: "GET" | "POST" | "PATCH" | "DELETE";
99
- body?: unknown;
100
- errorPrefix: string;
101
- }): Promise<T | null> {
102
- if (typeof fetchImpl !== "function") throw new Error("OpenPress inspector comment endpoint is unavailable.");
103
- const response = await fetchImpl(endpoint, {
104
- method,
105
- ...(body === undefined
106
- ? {}
107
- : {
108
- headers: { "Content-Type": "application/json" },
109
- body: JSON.stringify(body),
110
- }),
111
- });
112
- const result = await response.json().catch(() => null) as T | null;
113
- if (!response.ok) {
114
- throw new Error(result?.message ?? `${errorPrefix} with status ${response.status}`);
115
- }
116
- return result;
117
- }
118
-
119
- export async function submitInspectorComment({
120
- draft,
121
- endpoint = "/__openpress/comment",
122
- fetchImpl = globalThis.fetch?.bind(globalThis),
123
- }: {
124
- draft: CommentDraft;
125
- endpoint?: string;
126
- fetchImpl?: typeof fetch;
127
- }): Promise<InspectorCommentResult> {
128
- const result = await requestInspectorJson<InspectorCommentResult>({
129
- endpoint,
130
- fetchImpl,
131
- method: "POST",
132
- errorPrefix: "OpenPress inspector comment failed",
133
- body: {
134
- target: {
135
- objectId: draft.entityRef.id,
136
- targetObjectId: draft.targetObjectId,
137
- kind: draft.entityRef.kind,
138
- label: draft.label,
139
- blockId: draft.blockId,
140
- path: draft.source.path,
141
- source: draft.source.source,
142
- },
143
- note: draft.note,
144
- hint: formatInspectorHint({ placement: draft.placement, targetObjectId: draft.targetObjectId }),
145
- },
146
- });
147
- return result ?? { ok: true };
148
- }
149
-
150
- export function createInspectorCommentDraft({
151
- block,
152
- entity,
153
- note,
154
- placement,
155
- target,
156
- }: {
157
- block: SourceBlock | null;
158
- entity: ObjectEntity | null;
159
- note: string;
160
- placement: InspectorPlacement;
161
- target?: ObjectSelection | null;
162
- }): CommentDraft {
163
- const normalizedNote = note.trim();
164
- if (!block) throw new Error("OpenPress inspector comment requires a selected object.");
165
- if (!normalizedNote) throw new Error("OpenPress inspector comment note must not be empty.");
166
- if (!block.path || !block.source?.line) throw new Error("OpenPress inspector selected object has no editable source location.");
167
-
168
- return {
169
- entityRef: entity ? { id: entity.id, kind: entity.kind } : { id: block.id, kind: "mdx-block" },
170
- label: entity?.label ?? block.name ?? block.id,
171
- blockId: block.id,
172
- targetObjectId: target?.objectId && target.objectId !== entity?.id ? target.objectId : undefined,
173
- placement,
174
- source: {
175
- path: block.path,
176
- source: block.source,
177
- line: block.source.line,
178
- column: block.source.column,
179
- },
180
- note: normalizedNote,
181
- };
182
- }
183
-
184
- export async function fetchInspectorComments({
185
- endpoint = "/__openpress/comment",
186
- fetchImpl = globalThis.fetch?.bind(globalThis),
187
- }: {
188
- endpoint?: string;
189
- fetchImpl?: typeof fetch;
190
- } = {}): Promise<PendingComment[]> {
191
- const result = await requestInspectorJson<CommentListResult>({
192
- endpoint,
193
- fetchImpl,
194
- method: "GET",
195
- errorPrefix: "OpenPress inspector comment list failed",
196
- });
197
- return Array.isArray(result?.comments) ? result.comments : [];
198
- }
199
-
200
- export async function clearInspectorComment({
201
- id,
202
- all = false,
203
- endpoint = "/__openpress/comment",
204
- fetchImpl = globalThis.fetch?.bind(globalThis),
205
- }: {
206
- id?: string;
207
- all?: boolean;
208
- endpoint?: string;
209
- fetchImpl?: typeof fetch;
210
- } = {}): Promise<CommentClearResult> {
211
- if (!all && !id) throw new Error("OpenPress inspector comment clear requires an id or all=true.");
212
-
213
- const result = await requestInspectorJson<CommentClearResult>({
214
- endpoint,
215
- fetchImpl,
216
- method: "DELETE",
217
- errorPrefix: "OpenPress inspector comment clear failed",
218
- body: all ? { all: true } : { id },
219
- });
220
- return result ?? { ok: true, removedCount: 0 };
221
- }
222
-
223
- export async function updateInspectorComment({
224
- id,
225
- note,
226
- placement,
227
- endpoint = "/__openpress/comment",
228
- fetchImpl = globalThis.fetch?.bind(globalThis),
229
- }: {
230
- id: string;
231
- note: string;
232
- placement?: InspectorPlacement;
233
- endpoint?: string;
234
- fetchImpl?: typeof fetch;
235
- }): Promise<InspectorCommentResult> {
236
- const normalizedNote = note.trim();
237
- if (!id.trim()) throw new Error("OpenPress inspector comment update requires an id.");
238
- if (!normalizedNote) throw new Error("OpenPress inspector comment note must not be empty.");
239
-
240
- const result = await requestInspectorJson<InspectorCommentResult>({
241
- endpoint,
242
- fetchImpl,
243
- method: "PATCH",
244
- errorPrefix: "OpenPress inspector comment update failed",
245
- body: {
246
- id,
247
- note: normalizedNote,
248
- hint: formatInspectorHint({ placement }),
249
- },
250
- });
251
- return result ?? { ok: true };
252
- }
253
-
254
- export function useInspector(
255
- document: ReaderDocument,
256
- {
257
- enabled = false,
258
- storageKey = DEFAULT_INSPECTOR_STORAGE_KEY,
259
- }: {
260
- enabled?: boolean;
261
- storageKey?: string;
262
- } = {},
263
- ): InspectorState {
264
- const blockMap = useMemo(() => getSourceBlockMap(document), [document]);
265
- const objectEntityMap = useMemo(() => getObjectEntityMap(document), [document]);
266
- const [inspectorMode, setInspectorModeState] = useState(() => {
267
- if (!enabled || typeof window === "undefined") return false;
268
- return window.localStorage.getItem(storageKey) === "on";
269
- });
270
- const [selectedSelection, setSelectedSelection] = useState<ObjectSelection | null>(null);
271
-
272
- useEffect(() => {
273
- if (!enabled && inspectorMode) setInspectorModeState(false);
274
- }, [enabled, inspectorMode]);
275
-
276
- const setInspectorMode = useCallback((nextEnabled: boolean) => {
277
- setInspectorModeState(nextEnabled);
278
- if (typeof window !== "undefined") {
279
- window.localStorage.setItem(storageKey, nextEnabled ? "on" : "off");
280
- }
281
- if (!nextEnabled) setSelectedSelection(null);
282
- }, [storageKey]);
283
-
284
- const resolveSourceBlock = useCallback((target: ObjectSelection | null): SourceBlock | null => {
285
- if (!target) return null;
286
- if (target.blockId && blockMap[target.blockId]) return blockMap[target.blockId] ?? null;
287
- const entity = target.objectEntity;
288
- const source = entity?.source;
289
- if (!entity || !source?.path) return null;
290
- return {
291
- id: entity.blockId ?? entity.id,
292
- kind: entity.kind,
293
- name: entity.label,
294
- path: source.path,
295
- frameKey: entity.frameKey,
296
- chainId: entity.chainId,
297
- source: source.source ?? (source.line ? { line: source.line, column: source.column ?? 1 } : undefined),
298
- };
299
- }, [blockMap]);
300
-
301
- const selectSelection = useCallback((target: ObjectSelection | null) => {
302
- const resolvedTarget = target?.objectId && !target.objectEntity
303
- ? { ...target, objectEntity: objectEntityMap[target.objectId] }
304
- : target;
305
- const withRef = resolvedTarget?.objectEntity && !resolvedTarget.entityRef
306
- ? {
307
- ...resolvedTarget,
308
- entityRef: {
309
- id: resolvedTarget.objectEntity.id,
310
- kind: resolvedTarget.objectEntity.kind,
311
- },
312
- }
313
- : resolvedTarget;
314
-
315
- setSelectedSelection(withRef ?? null);
316
- return resolveSourceBlock(withRef ?? null);
317
- }, [objectEntityMap, resolveSourceBlock]);
318
-
319
- const inspectSelection = useCallback((target: EventTarget | null) => {
320
- const objectSelection = findObjectSelection(target, objectEntityMap);
321
- return selectSelection(objectSelection);
322
- }, [objectEntityMap, selectSelection]);
323
-
324
- const handleClick = useCallback((event: ReactMouseEvent) => {
325
- if (!enabled || !inspectorMode) return false;
326
- const objectSelection = findObjectSelection(event.target, objectEntityMap);
327
- if (!objectSelection) return false;
328
- selectSelection(objectSelection);
329
- event.preventDefault();
330
- event.stopPropagation();
331
- return true;
332
- }, [enabled, inspectorMode, objectEntityMap, selectSelection]);
333
-
334
- const selectedObjectEntity = selectedSelection?.objectEntity ?? null;
335
- const selectedBlock = resolveSourceBlock(selectedSelection);
336
- const selectedBlockId = selectedBlock?.id ?? selectedSelection?.blockId ?? null;
337
-
338
- return {
339
- enabled,
340
- inspectorMode: enabled && inspectorMode,
341
- selectedBlockId,
342
- selectedBlock,
343
- selectedObjectEntity,
344
- selectedSelection,
345
- selectedTarget: selectedSelection,
346
- setInspectorMode,
347
- toggleInspectorMode: () => setInspectorMode(!inspectorMode),
348
- selectSelection,
349
- inspectSelection,
350
- selectTarget: selectSelection,
351
- inspectTarget: inspectSelection,
352
- handleClick,
353
- };
354
- }
355
-
356
- export function findObjectSelection(
357
- target: EventTarget | null,
358
- objectEntities: Record<string, ObjectEntity> = {},
359
- ): ObjectSelection | null {
360
- if (typeof Element === "undefined") return null;
361
- if (!(target instanceof Element)) return null;
362
- const insertTarget = target.closest<HTMLElement>("[data-openpress-insert-before-block-id]");
363
- const insertBeforeBlockId = insertTarget?.dataset.openpressInsertBeforeBlockId;
364
- if (insertBeforeBlockId) {
365
- const objectId = insertTarget?.dataset.openpressObjectId;
366
- const objectEntity = objectId ? objectEntities[objectId] : undefined;
367
- const selection: ObjectSelection = { blockId: insertBeforeBlockId, placement: "before" };
368
- if (objectId) selection.objectId = objectId;
369
- if (objectEntity) {
370
- selection.objectEntity = objectEntity;
371
- selection.entityRef = { id: objectEntity.id, kind: objectEntity.kind };
372
- }
373
- return selection;
374
- }
375
-
376
- const renderedObject = target.closest<HTMLElement>("[data-openpress-object-id]");
377
- const objectId = renderedObject?.dataset.openpressObjectId;
378
- if (objectId) {
379
- const objectEntity = objectEntities[objectId];
380
- const blockId = renderedObject?.dataset.openpressBlockId || objectEntity?.blockId;
381
- const selection: ObjectSelection = { objectId, placement: "block" };
382
- if (blockId) selection.blockId = blockId;
383
- if (objectEntity) {
384
- selection.objectEntity = objectEntity;
385
- selection.entityRef = { id: objectEntity.id, kind: objectEntity.kind };
386
- }
387
- return selection;
388
- }
389
-
390
- const element = target.closest<HTMLElement>("[data-openpress-block-id]");
391
- const blockId = element?.dataset.openpressBlockId;
392
- return blockId ? { blockId, placement: "block" } : null;
393
- }
394
-
395
- export const findInspectorTarget = findObjectSelection;
396
-
397
- export function formatInspectorHint({
398
- placement,
399
- targetObjectId,
400
- }: {
401
- placement?: InspectorPlacement;
402
- targetObjectId?: string;
403
- } = {}) {
404
- const parts = ["openpress-react-inspector"];
405
- if (placement) parts.push(`placement=${placement}`);
406
- if (targetObjectId?.trim()) parts.push(`target=${encodeURIComponent(targetObjectId.trim())}`);
407
- return parts.join(" ");
408
- }