@open-document/core 0.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.
- package/LICENSE +21 -0
- package/README.md +50 -0
- package/README.zh-TW.md +50 -0
- package/bin.js +2 -0
- package/dist/build-BWZJ1gvL.js +18 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +86 -0
- package/dist/config-DBp4mOOf.js +1121 -0
- package/dist/config-EBFfKtWH.d.ts +17 -0
- package/dist/design-BJLvOcCO.js +53 -0
- package/dist/dev-CZtw1oqs.js +24 -0
- package/dist/edit-ops-DSdUroVO.js +1225 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.js +240 -0
- package/dist/ops/index.d.ts +160 -0
- package/dist/ops/index.js +202 -0
- package/dist/preview-DUfQl7Ij.js +23 -0
- package/dist/sync-6GTPAt2t.js +86 -0
- package/dist/sync-C6CsPmyK.js +3 -0
- package/dist/vite/index.d.ts +22 -0
- package/dist/vite/index.js +5 -0
- package/env.d.ts +51 -0
- package/package.json +91 -0
- package/skills/apply-comments/SKILL.md +74 -0
- package/skills/create-doc/SKILL.md +102 -0
- package/skills/create-theme/SKILL.md +184 -0
- package/skills/doc-authoring/SKILL.md +354 -0
- package/skills/doc-authoring/references/assets.md +47 -0
- package/skills/doc-authoring/references/design-system.md +81 -0
- package/skills/doc-authoring/references/pagination.md +117 -0
- package/skills/doc-authoring/references/tables-and-charts.md +161 -0
- package/src/app/app.tsx +42 -0
- package/src/app/components/design-panel/design-panel.tsx +318 -0
- package/src/app/components/design-panel/design-provider.tsx +121 -0
- package/src/app/components/design-panel/use-design.ts +85 -0
- package/src/app/components/doc-assets.tsx +129 -0
- package/src/app/components/doc-sidebar.tsx +162 -0
- package/src/app/components/flow-page.tsx +50 -0
- package/src/app/components/image-placeholder.tsx +50 -0
- package/src/app/components/inspector/inspector.tsx +426 -0
- package/src/app/components/page-frame.tsx +70 -0
- package/src/app/components/sidebar/folder-item.tsx +212 -0
- package/src/app/components/sidebar/icon-picker.tsx +99 -0
- package/src/app/components/sidebar/sidebar.tsx +252 -0
- package/src/app/components/table-of-contents.tsx +81 -0
- package/src/app/components/theme-toggle.tsx +50 -0
- package/src/app/components/themes/markdown.tsx +249 -0
- package/src/app/components/themes/theme-preview.tsx +74 -0
- package/src/app/components/ui/menu.tsx +139 -0
- package/src/app/index.html +12 -0
- package/src/app/lib/assets.ts +151 -0
- package/src/app/lib/design-presets.ts +109 -0
- package/src/app/lib/design.ts +88 -0
- package/src/app/lib/doc-preview.tsx +29 -0
- package/src/app/lib/docs.ts +26 -0
- package/src/app/lib/export-html.ts +309 -0
- package/src/app/lib/export-pdf.ts +166 -0
- package/src/app/lib/flow-measure.ts +109 -0
- package/src/app/lib/flow.test.ts +64 -0
- package/src/app/lib/flow.ts +111 -0
- package/src/app/lib/folders.ts +192 -0
- package/src/app/lib/inspector/fiber.ts +99 -0
- package/src/app/lib/outline.ts +109 -0
- package/src/app/lib/page-context.tsx +38 -0
- package/src/app/lib/print-ready.ts +69 -0
- package/src/app/lib/sdk.test.ts +24 -0
- package/src/app/lib/sdk.ts +86 -0
- package/src/app/lib/themes.ts +31 -0
- package/src/app/lib/use-doc-module.ts +53 -0
- package/src/app/lib/use-doc-pages.ts +99 -0
- package/src/app/lib/utils.ts +6 -0
- package/src/app/main.tsx +14 -0
- package/src/app/routes/assets.tsx +257 -0
- package/src/app/routes/doc.tsx +480 -0
- package/src/app/routes/home-shell.tsx +203 -0
- package/src/app/routes/home.tsx +269 -0
- package/src/app/routes/themes.tsx +121 -0
- package/src/app/styles.css +72 -0
- package/src/app/virtual.d.ts +30 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type Block =
|
|
4
|
+
| { kind: 'heading'; level: number; text: string }
|
|
5
|
+
| { kind: 'code'; lang: string; text: string }
|
|
6
|
+
| { kind: 'list'; items: string[] }
|
|
7
|
+
| { kind: 'quote'; text: string }
|
|
8
|
+
| { kind: 'rule' }
|
|
9
|
+
| { kind: 'table'; head: string[]; rows: string[][] }
|
|
10
|
+
| { kind: 'paragraph'; text: string };
|
|
11
|
+
|
|
12
|
+
function splitRow(line: string): string[] {
|
|
13
|
+
return line
|
|
14
|
+
.trim()
|
|
15
|
+
.replace(/^\||\|$/g, '')
|
|
16
|
+
.split('|')
|
|
17
|
+
.map((cell) => cell.trim());
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TABLE_DIVIDER_RE = /^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A theme file is prose written by the `create-theme` skill — headings, prose,
|
|
24
|
+
* lists, and paste-ready code. This renders exactly that much Markdown rather
|
|
25
|
+
* than pulling a parser into the runtime bundle.
|
|
26
|
+
*/
|
|
27
|
+
export function parseMarkdown(source: string): Block[] {
|
|
28
|
+
const lines = source.split(/\r?\n/);
|
|
29
|
+
const blocks: Block[] = [];
|
|
30
|
+
let i = 0;
|
|
31
|
+
|
|
32
|
+
while (i < lines.length) {
|
|
33
|
+
const line = lines[i];
|
|
34
|
+
|
|
35
|
+
if (line.trim() === '') {
|
|
36
|
+
i++;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const fence = line.match(/^```(\w*)/);
|
|
41
|
+
if (fence) {
|
|
42
|
+
const lang = fence[1] ?? '';
|
|
43
|
+
const body: string[] = [];
|
|
44
|
+
i++;
|
|
45
|
+
while (i < lines.length && !lines[i].startsWith('```')) {
|
|
46
|
+
body.push(lines[i]);
|
|
47
|
+
i++;
|
|
48
|
+
}
|
|
49
|
+
i++; // closing fence
|
|
50
|
+
blocks.push({ kind: 'code', lang, text: body.join('\n') });
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// A pipe row followed by a divider row is a table. Theme files lean on
|
|
55
|
+
// tables for palettes and type scales, so leaving them as raw text makes
|
|
56
|
+
// the theme page unreadable.
|
|
57
|
+
if (line.trim().startsWith('|') && TABLE_DIVIDER_RE.test(lines[i + 1] ?? '')) {
|
|
58
|
+
const head = splitRow(line);
|
|
59
|
+
i += 2;
|
|
60
|
+
const rows: string[][] = [];
|
|
61
|
+
while (i < lines.length && lines[i].trim().startsWith('|')) {
|
|
62
|
+
rows.push(splitRow(lines[i]));
|
|
63
|
+
i++;
|
|
64
|
+
}
|
|
65
|
+
blocks.push({ kind: 'table', head, rows });
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const heading = line.match(/^(#{1,6})\s+(.*)$/);
|
|
70
|
+
if (heading) {
|
|
71
|
+
blocks.push({ kind: 'heading', level: heading[1].length, text: heading[2].trim() });
|
|
72
|
+
i++;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (/^(-{3,}|\*{3,})$/.test(line.trim())) {
|
|
77
|
+
blocks.push({ kind: 'rule' });
|
|
78
|
+
i++;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (/^>\s?/.test(line)) {
|
|
83
|
+
const body: string[] = [];
|
|
84
|
+
while (i < lines.length && /^>\s?/.test(lines[i])) {
|
|
85
|
+
body.push(lines[i].replace(/^>\s?/, ''));
|
|
86
|
+
i++;
|
|
87
|
+
}
|
|
88
|
+
blocks.push({ kind: 'quote', text: body.join(' ') });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (/^\s*([-*+]|\d+\.)\s+/.test(line)) {
|
|
93
|
+
const items: string[] = [];
|
|
94
|
+
while (i < lines.length && /^\s*([-*+]|\d+\.)\s+/.test(lines[i])) {
|
|
95
|
+
items.push(lines[i].replace(/^\s*([-*+]|\d+\.)\s+/, ''));
|
|
96
|
+
i++;
|
|
97
|
+
}
|
|
98
|
+
blocks.push({ kind: 'list', items });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const body: string[] = [];
|
|
103
|
+
while (
|
|
104
|
+
i < lines.length &&
|
|
105
|
+
lines[i].trim() !== '' &&
|
|
106
|
+
!/^(#{1,6}\s|```|>\s?|\||\s*([-*+]|\d+\.)\s)/.test(lines[i])
|
|
107
|
+
) {
|
|
108
|
+
body.push(lines[i].trim());
|
|
109
|
+
i++;
|
|
110
|
+
}
|
|
111
|
+
blocks.push({ kind: 'paragraph', text: body.join(' ') });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return blocks;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function renderInline(text: string): ReactNode[] {
|
|
118
|
+
const nodes: ReactNode[] = [];
|
|
119
|
+
const pattern = /(`[^`]+`|\*\*[^*]+\*\*|\[[^\]]+\]\([^)]+\))/g;
|
|
120
|
+
let last = 0;
|
|
121
|
+
let key = 0;
|
|
122
|
+
for (const match of text.matchAll(pattern)) {
|
|
123
|
+
const index = match.index ?? 0;
|
|
124
|
+
if (index > last) nodes.push(text.slice(last, index));
|
|
125
|
+
const token = match[0];
|
|
126
|
+
if (token.startsWith('`')) {
|
|
127
|
+
nodes.push(
|
|
128
|
+
<code
|
|
129
|
+
key={`i${key++}`}
|
|
130
|
+
className="rounded bg-muted px-1 py-0.5 font-mono text-[0.85em] text-foreground"
|
|
131
|
+
>
|
|
132
|
+
{token.slice(1, -1)}
|
|
133
|
+
</code>,
|
|
134
|
+
);
|
|
135
|
+
} else if (token.startsWith('**')) {
|
|
136
|
+
nodes.push(
|
|
137
|
+
<strong key={`i${key++}`} className="font-semibold text-foreground">
|
|
138
|
+
{token.slice(2, -2)}
|
|
139
|
+
</strong>,
|
|
140
|
+
);
|
|
141
|
+
} else {
|
|
142
|
+
const label = token.slice(1, token.indexOf(']'));
|
|
143
|
+
const href = token.slice(token.indexOf('](') + 2, -1);
|
|
144
|
+
nodes.push(
|
|
145
|
+
<a key={`i${key++}`} href={href} className="underline underline-offset-2">
|
|
146
|
+
{label}
|
|
147
|
+
</a>,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
last = index + token.length;
|
|
151
|
+
}
|
|
152
|
+
if (last < text.length) nodes.push(text.slice(last));
|
|
153
|
+
return nodes;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const HEADING_CLASS: Record<number, string> = {
|
|
157
|
+
1: 'mt-10 mb-3 font-semibold text-xl tracking-tight',
|
|
158
|
+
2: 'mt-8 mb-2.5 font-semibold text-base tracking-tight',
|
|
159
|
+
3: 'mt-6 mb-2 font-medium text-sm',
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export function Markdown({ source }: { source: string }) {
|
|
163
|
+
const blocks = parseMarkdown(source);
|
|
164
|
+
return (
|
|
165
|
+
<div className="text-muted-foreground text-sm leading-relaxed">
|
|
166
|
+
{blocks.map((block, index) => {
|
|
167
|
+
const key = `${block.kind}-${index}`;
|
|
168
|
+
switch (block.kind) {
|
|
169
|
+
case 'heading': {
|
|
170
|
+
const className = HEADING_CLASS[block.level] ?? HEADING_CLASS[3];
|
|
171
|
+
return (
|
|
172
|
+
<p key={key} className={`text-foreground first:mt-0 ${className}`}>
|
|
173
|
+
{renderInline(block.text)}
|
|
174
|
+
</p>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
case 'code':
|
|
178
|
+
return (
|
|
179
|
+
<pre
|
|
180
|
+
key={key}
|
|
181
|
+
className="my-3 overflow-x-auto rounded-md border border-border bg-muted px-3 py-2.5 font-mono text-[11.5px] text-foreground leading-relaxed"
|
|
182
|
+
>
|
|
183
|
+
{block.text}
|
|
184
|
+
</pre>
|
|
185
|
+
);
|
|
186
|
+
case 'list':
|
|
187
|
+
return (
|
|
188
|
+
<ul key={key} className="my-3 list-disc space-y-1 pl-5">
|
|
189
|
+
{block.items.map((item, itemIndex) => (
|
|
190
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: parsed markdown has no stable id, and the list is re-rendered wholesale
|
|
191
|
+
<li key={`${key}-${itemIndex}`}>{renderInline(item)}</li>
|
|
192
|
+
))}
|
|
193
|
+
</ul>
|
|
194
|
+
);
|
|
195
|
+
case 'quote':
|
|
196
|
+
return (
|
|
197
|
+
<blockquote key={key} className="my-3 border-border border-l-2 pl-3 italic">
|
|
198
|
+
{renderInline(block.text)}
|
|
199
|
+
</blockquote>
|
|
200
|
+
);
|
|
201
|
+
case 'table':
|
|
202
|
+
return (
|
|
203
|
+
<div key={key} className="my-4 overflow-x-auto">
|
|
204
|
+
<table className="w-full border-collapse text-xs">
|
|
205
|
+
<thead>
|
|
206
|
+
<tr>
|
|
207
|
+
{block.head.map((cell, cellIndex) => (
|
|
208
|
+
<th
|
|
209
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: column position is the identity
|
|
210
|
+
key={`h${cellIndex}`}
|
|
211
|
+
className="border-border border-b px-2 py-1.5 text-left font-medium text-muted-foreground"
|
|
212
|
+
>
|
|
213
|
+
{renderInline(cell)}
|
|
214
|
+
</th>
|
|
215
|
+
))}
|
|
216
|
+
</tr>
|
|
217
|
+
</thead>
|
|
218
|
+
<tbody>
|
|
219
|
+
{block.rows.map((row, rowIndex) => (
|
|
220
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: row position is the identity
|
|
221
|
+
<tr key={`r${rowIndex}`}>
|
|
222
|
+
{row.map((cell, cellIndex) => (
|
|
223
|
+
<td
|
|
224
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: column position is the identity
|
|
225
|
+
key={`c${cellIndex}`}
|
|
226
|
+
className="border-border border-b px-2 py-1.5 align-top text-foreground"
|
|
227
|
+
>
|
|
228
|
+
{renderInline(cell)}
|
|
229
|
+
</td>
|
|
230
|
+
))}
|
|
231
|
+
</tr>
|
|
232
|
+
))}
|
|
233
|
+
</tbody>
|
|
234
|
+
</table>
|
|
235
|
+
</div>
|
|
236
|
+
);
|
|
237
|
+
case 'rule':
|
|
238
|
+
return <hr key={key} className="my-6 border-border" />;
|
|
239
|
+
default:
|
|
240
|
+
return (
|
|
241
|
+
<p key={key} className="my-3">
|
|
242
|
+
{renderInline(block.text)}
|
|
243
|
+
</p>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
})}
|
|
247
|
+
</div>
|
|
248
|
+
);
|
|
249
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { type DocPage, resolvePageGeometry } from '../../lib/sdk';
|
|
3
|
+
import { loadThemeDemo, type ThemeDemoModule, type ThemeMeta } from '../../lib/themes';
|
|
4
|
+
import { PageFrame } from '../page-frame';
|
|
5
|
+
|
|
6
|
+
type Props = {
|
|
7
|
+
theme: ThemeMeta;
|
|
8
|
+
width: number;
|
|
9
|
+
/** Render every demo page instead of just the first. */
|
|
10
|
+
all?: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function ThemePreview({ theme, width, all = false }: Props) {
|
|
14
|
+
const [demo, setDemo] = useState<ThemeDemoModule | null>(null);
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (!theme.hasDemo) return;
|
|
18
|
+
let cancelled = false;
|
|
19
|
+
loadThemeDemo(theme.id)
|
|
20
|
+
.then((mod) => {
|
|
21
|
+
if (!cancelled) setDemo(mod);
|
|
22
|
+
})
|
|
23
|
+
.catch(() => {
|
|
24
|
+
if (!cancelled) setDemo(null);
|
|
25
|
+
});
|
|
26
|
+
return () => {
|
|
27
|
+
cancelled = true;
|
|
28
|
+
};
|
|
29
|
+
}, [theme.id, theme.hasDemo]);
|
|
30
|
+
|
|
31
|
+
const geometry = resolvePageGeometry(
|
|
32
|
+
theme.pageSize ? { pageSize: theme.pageSize as never } : undefined,
|
|
33
|
+
);
|
|
34
|
+
const scale = width / geometry.width;
|
|
35
|
+
const pages = (demo?.default ?? []).filter(
|
|
36
|
+
(entry): entry is DocPage => typeof entry === 'function',
|
|
37
|
+
);
|
|
38
|
+
const shown = all ? pages : pages.slice(0, 1);
|
|
39
|
+
|
|
40
|
+
if (shown.length === 0) {
|
|
41
|
+
return (
|
|
42
|
+
<div
|
|
43
|
+
className="grid place-items-center rounded-md border border-border border-dashed bg-muted text-[11px] text-muted-foreground"
|
|
44
|
+
style={{ width, height: geometry.height * scale }}
|
|
45
|
+
>
|
|
46
|
+
{theme.hasDemo ? 'Loading…' : 'No demo'}
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<div className="flex flex-wrap gap-4">
|
|
53
|
+
{shown.map((Page, index) => (
|
|
54
|
+
<div
|
|
55
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: page order is the identity
|
|
56
|
+
key={index}
|
|
57
|
+
className="overflow-hidden rounded-md ring-1 ring-border"
|
|
58
|
+
style={{ width, height: geometry.height * scale }}
|
|
59
|
+
>
|
|
60
|
+
<PageFrame
|
|
61
|
+
index={index}
|
|
62
|
+
total={pages.length}
|
|
63
|
+
geometry={geometry}
|
|
64
|
+
scale={scale}
|
|
65
|
+
design={demo?.design}
|
|
66
|
+
flat
|
|
67
|
+
>
|
|
68
|
+
<Page />
|
|
69
|
+
</PageFrame>
|
|
70
|
+
</div>
|
|
71
|
+
))}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ReactNode,
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useId,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react';
|
|
10
|
+
import { cn } from '../../lib/utils';
|
|
11
|
+
|
|
12
|
+
type Placement = 'bottom-start' | 'bottom-end' | 'right-start';
|
|
13
|
+
|
|
14
|
+
type MenuProps = {
|
|
15
|
+
trigger: (props: {
|
|
16
|
+
onClick: (e: React.MouseEvent) => void;
|
|
17
|
+
'aria-expanded': boolean;
|
|
18
|
+
}) => ReactNode;
|
|
19
|
+
children: (close: () => void) => ReactNode;
|
|
20
|
+
placement?: Placement;
|
|
21
|
+
className?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Minimal anchored menu. The runtime ships to users, so the browser UI stays
|
|
26
|
+
* dependency-free rather than pulling in a headless-component library.
|
|
27
|
+
*/
|
|
28
|
+
export function Menu({ trigger, children, placement = 'bottom-end', className }: MenuProps) {
|
|
29
|
+
const [open, setOpen] = useState(false);
|
|
30
|
+
const [coords, setCoords] = useState<{ top: number; left: number } | null>(null);
|
|
31
|
+
const anchorRef = useRef<HTMLSpanElement>(null);
|
|
32
|
+
const panelRef = useRef<HTMLDivElement>(null);
|
|
33
|
+
const id = useId();
|
|
34
|
+
|
|
35
|
+
const close = useCallback(() => setOpen(false), []);
|
|
36
|
+
|
|
37
|
+
useLayoutEffect(() => {
|
|
38
|
+
if (!open) return;
|
|
39
|
+
const anchor = anchorRef.current?.firstElementChild ?? anchorRef.current;
|
|
40
|
+
const panel = panelRef.current;
|
|
41
|
+
if (!anchor || !panel) return;
|
|
42
|
+
const a = anchor.getBoundingClientRect();
|
|
43
|
+
const p = panel.getBoundingClientRect();
|
|
44
|
+
const gap = 4;
|
|
45
|
+
let top = placement === 'right-start' ? a.top : a.bottom + gap;
|
|
46
|
+
let left = placement === 'bottom-end' ? a.right - p.width : a.left;
|
|
47
|
+
if (placement === 'right-start') left = a.right + gap;
|
|
48
|
+
left = Math.max(8, Math.min(left, window.innerWidth - p.width - 8));
|
|
49
|
+
top = Math.max(8, Math.min(top, window.innerHeight - p.height - 8));
|
|
50
|
+
setCoords({ top, left });
|
|
51
|
+
}, [open, placement]);
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!open) return;
|
|
55
|
+
const onPointerDown = (e: MouseEvent) => {
|
|
56
|
+
const target = e.target as Node;
|
|
57
|
+
if (panelRef.current?.contains(target)) return;
|
|
58
|
+
if (anchorRef.current?.contains(target)) return;
|
|
59
|
+
setOpen(false);
|
|
60
|
+
};
|
|
61
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
62
|
+
if (e.key === 'Escape') setOpen(false);
|
|
63
|
+
};
|
|
64
|
+
document.addEventListener('mousedown', onPointerDown);
|
|
65
|
+
document.addEventListener('keydown', onKeyDown);
|
|
66
|
+
window.addEventListener('resize', close);
|
|
67
|
+
window.addEventListener('scroll', close, true);
|
|
68
|
+
return () => {
|
|
69
|
+
document.removeEventListener('mousedown', onPointerDown);
|
|
70
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
71
|
+
window.removeEventListener('resize', close);
|
|
72
|
+
window.removeEventListener('scroll', close, true);
|
|
73
|
+
};
|
|
74
|
+
}, [open, close]);
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<>
|
|
78
|
+
<span ref={anchorRef} className="contents">
|
|
79
|
+
{trigger({
|
|
80
|
+
onClick: (e) => {
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
e.stopPropagation();
|
|
83
|
+
setOpen((v) => !v);
|
|
84
|
+
},
|
|
85
|
+
'aria-expanded': open,
|
|
86
|
+
})}
|
|
87
|
+
</span>
|
|
88
|
+
{open && (
|
|
89
|
+
<div
|
|
90
|
+
ref={panelRef}
|
|
91
|
+
id={id}
|
|
92
|
+
role="menu"
|
|
93
|
+
className={cn(
|
|
94
|
+
'fixed z-50 min-w-[180px] rounded-md border border-border bg-background p-1 shadow-lg',
|
|
95
|
+
coords ? 'visible' : 'invisible',
|
|
96
|
+
className,
|
|
97
|
+
)}
|
|
98
|
+
style={{ top: coords?.top ?? 0, left: coords?.left ?? 0 }}
|
|
99
|
+
>
|
|
100
|
+
{children(close)}
|
|
101
|
+
</div>
|
|
102
|
+
)}
|
|
103
|
+
</>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function MenuItem({
|
|
108
|
+
onClick,
|
|
109
|
+
children,
|
|
110
|
+
destructive = false,
|
|
111
|
+
active = false,
|
|
112
|
+
}: {
|
|
113
|
+
onClick: () => void;
|
|
114
|
+
children: ReactNode;
|
|
115
|
+
destructive?: boolean;
|
|
116
|
+
active?: boolean;
|
|
117
|
+
}) {
|
|
118
|
+
return (
|
|
119
|
+
<button
|
|
120
|
+
type="button"
|
|
121
|
+
role="menuitem"
|
|
122
|
+
onClick={(e) => {
|
|
123
|
+
e.stopPropagation();
|
|
124
|
+
onClick();
|
|
125
|
+
}}
|
|
126
|
+
className={cn(
|
|
127
|
+
'flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs transition-colors hover:bg-accent',
|
|
128
|
+
active && 'bg-accent',
|
|
129
|
+
destructive && 'text-red-600 dark:text-red-400',
|
|
130
|
+
)}
|
|
131
|
+
>
|
|
132
|
+
{children}
|
|
133
|
+
</button>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function MenuSeparator() {
|
|
138
|
+
return <div className="my-1 h-px bg-border" />;
|
|
139
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>open-doc</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="./main.tsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
export const GLOBAL_SCOPE = '@global';
|
|
4
|
+
|
|
5
|
+
export type Asset = {
|
|
6
|
+
name: string;
|
|
7
|
+
size: number;
|
|
8
|
+
createdAt: number;
|
|
9
|
+
mtime: number;
|
|
10
|
+
mime: string;
|
|
11
|
+
url: string;
|
|
12
|
+
importPath: string;
|
|
13
|
+
unused: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type AssetUsages = {
|
|
17
|
+
usages: Array<{ docId: string; count: number }>;
|
|
18
|
+
totalCount: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type ApiResult<T> = { ok: true; value: T } | { ok: false; error: string };
|
|
22
|
+
|
|
23
|
+
const base = (scope: string) => `/__assets/${encodeURIComponent(scope)}`;
|
|
24
|
+
const fileUrl = (scope: string, name: string) => `${base(scope)}/${encodeURIComponent(name)}`;
|
|
25
|
+
|
|
26
|
+
async function errorFrom(res: Response): Promise<string> {
|
|
27
|
+
try {
|
|
28
|
+
const body = (await res.json()) as { error?: string };
|
|
29
|
+
return body.error ?? `HTTP ${res.status}`;
|
|
30
|
+
} catch {
|
|
31
|
+
return `HTTP ${res.status}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function listAssets(scope: string): Promise<ApiResult<Asset[]>> {
|
|
36
|
+
try {
|
|
37
|
+
const res = await fetch(base(scope));
|
|
38
|
+
if (!res.ok) return { ok: false, error: await errorFrom(res) };
|
|
39
|
+
const body = (await res.json()) as { assets: Asset[] };
|
|
40
|
+
return { ok: true, value: body.assets };
|
|
41
|
+
} catch (err) {
|
|
42
|
+
return { ok: false, error: String((err as Error).message) };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function uploadAsset(
|
|
47
|
+
scope: string,
|
|
48
|
+
file: File,
|
|
49
|
+
opts: { overwrite?: boolean } = {},
|
|
50
|
+
): Promise<ApiResult<Asset>> {
|
|
51
|
+
try {
|
|
52
|
+
const url = `${fileUrl(scope, file.name)}${opts.overwrite ? '?overwrite=1' : ''}`;
|
|
53
|
+
const res = await fetch(url, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers: { 'content-type': file.type || 'application/octet-stream' },
|
|
56
|
+
body: file,
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok) return { ok: false, error: await errorFrom(res) };
|
|
59
|
+
const body = (await res.json()) as Asset;
|
|
60
|
+
return { ok: true, value: { ...body, unused: true } };
|
|
61
|
+
} catch (err) {
|
|
62
|
+
return { ok: false, error: String((err as Error).message) };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function renameAsset(
|
|
67
|
+
scope: string,
|
|
68
|
+
name: string,
|
|
69
|
+
next: string,
|
|
70
|
+
): Promise<ApiResult<string>> {
|
|
71
|
+
try {
|
|
72
|
+
const res = await fetch(fileUrl(scope, name), {
|
|
73
|
+
method: 'PATCH',
|
|
74
|
+
headers: { 'content-type': 'application/json' },
|
|
75
|
+
body: JSON.stringify({ name: next }),
|
|
76
|
+
});
|
|
77
|
+
if (!res.ok) return { ok: false, error: await errorFrom(res) };
|
|
78
|
+
const body = (await res.json()) as { name: string };
|
|
79
|
+
return { ok: true, value: body.name };
|
|
80
|
+
} catch (err) {
|
|
81
|
+
return { ok: false, error: String((err as Error).message) };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function deleteAsset(scope: string, name: string): Promise<ApiResult<true>> {
|
|
86
|
+
try {
|
|
87
|
+
const res = await fetch(fileUrl(scope, name), { method: 'DELETE' });
|
|
88
|
+
if (!res.ok) return { ok: false, error: await errorFrom(res) };
|
|
89
|
+
return { ok: true, value: true };
|
|
90
|
+
} catch (err) {
|
|
91
|
+
return { ok: false, error: String((err as Error).message) };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function assetUsages(scope: string, name: string): Promise<ApiResult<AssetUsages>> {
|
|
96
|
+
try {
|
|
97
|
+
const res = await fetch(`${fileUrl(scope, name)}/usages`);
|
|
98
|
+
if (!res.ok) return { ok: false, error: await errorFrom(res) };
|
|
99
|
+
return { ok: true, value: (await res.json()) as AssetUsages };
|
|
100
|
+
} catch (err) {
|
|
101
|
+
return { ok: false, error: String((err as Error).message) };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Count of global assets, for the sidebar badge. Zero outside dev. */
|
|
106
|
+
export function useAssetCount(): number {
|
|
107
|
+
const [count, setCount] = useState(0);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (!import.meta.env.DEV) return;
|
|
111
|
+
let cancelled = false;
|
|
112
|
+
const read = () => {
|
|
113
|
+
listAssets(GLOBAL_SCOPE).then((result) => {
|
|
114
|
+
if (!cancelled && result.ok) setCount(result.value.length);
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
read();
|
|
118
|
+
if (!import.meta.hot) {
|
|
119
|
+
return () => {
|
|
120
|
+
cancelled = true;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
import.meta.hot.on('open-doc:files-changed', read);
|
|
124
|
+
return () => {
|
|
125
|
+
cancelled = true;
|
|
126
|
+
import.meta.hot?.off('open-doc:files-changed', read);
|
|
127
|
+
};
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
130
|
+
return count;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function formatBytes(bytes: number): string {
|
|
134
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
135
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10240 ? 1 : 0)} KB`;
|
|
136
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function isPreviewable(mime: string): boolean {
|
|
140
|
+
return mime.startsWith('image/');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The line an author pastes into a document to use this asset. */
|
|
144
|
+
export function importSnippet(asset: Asset): string {
|
|
145
|
+
const stem = asset.name.replace(/\.[^.]+$/, '');
|
|
146
|
+
const ident = stem
|
|
147
|
+
.replace(/[^A-Za-z0-9]+(.)/g, (_, c: string) => c.toUpperCase())
|
|
148
|
+
.replace(/[^A-Za-z0-9]/g, '');
|
|
149
|
+
const safeIdent = /^[A-Za-z_$]/.test(ident) ? ident : `asset${ident}`;
|
|
150
|
+
return `import ${safeIdent} from '${asset.importPath}';`;
|
|
151
|
+
}
|