@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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -0
  3. package/README.zh-TW.md +50 -0
  4. package/bin.js +2 -0
  5. package/dist/build-BWZJ1gvL.js +18 -0
  6. package/dist/cli/bin.d.ts +1 -0
  7. package/dist/cli/bin.js +86 -0
  8. package/dist/config-DBp4mOOf.js +1121 -0
  9. package/dist/config-EBFfKtWH.d.ts +17 -0
  10. package/dist/design-BJLvOcCO.js +53 -0
  11. package/dist/dev-CZtw1oqs.js +24 -0
  12. package/dist/edit-ops-DSdUroVO.js +1225 -0
  13. package/dist/index.d.ts +206 -0
  14. package/dist/index.js +240 -0
  15. package/dist/ops/index.d.ts +160 -0
  16. package/dist/ops/index.js +202 -0
  17. package/dist/preview-DUfQl7Ij.js +23 -0
  18. package/dist/sync-6GTPAt2t.js +86 -0
  19. package/dist/sync-C6CsPmyK.js +3 -0
  20. package/dist/vite/index.d.ts +22 -0
  21. package/dist/vite/index.js +5 -0
  22. package/env.d.ts +51 -0
  23. package/package.json +91 -0
  24. package/skills/apply-comments/SKILL.md +74 -0
  25. package/skills/create-doc/SKILL.md +102 -0
  26. package/skills/create-theme/SKILL.md +184 -0
  27. package/skills/doc-authoring/SKILL.md +354 -0
  28. package/skills/doc-authoring/references/assets.md +47 -0
  29. package/skills/doc-authoring/references/design-system.md +81 -0
  30. package/skills/doc-authoring/references/pagination.md +117 -0
  31. package/skills/doc-authoring/references/tables-and-charts.md +161 -0
  32. package/src/app/app.tsx +42 -0
  33. package/src/app/components/design-panel/design-panel.tsx +318 -0
  34. package/src/app/components/design-panel/design-provider.tsx +121 -0
  35. package/src/app/components/design-panel/use-design.ts +85 -0
  36. package/src/app/components/doc-assets.tsx +129 -0
  37. package/src/app/components/doc-sidebar.tsx +162 -0
  38. package/src/app/components/flow-page.tsx +50 -0
  39. package/src/app/components/image-placeholder.tsx +50 -0
  40. package/src/app/components/inspector/inspector.tsx +426 -0
  41. package/src/app/components/page-frame.tsx +70 -0
  42. package/src/app/components/sidebar/folder-item.tsx +212 -0
  43. package/src/app/components/sidebar/icon-picker.tsx +99 -0
  44. package/src/app/components/sidebar/sidebar.tsx +252 -0
  45. package/src/app/components/table-of-contents.tsx +81 -0
  46. package/src/app/components/theme-toggle.tsx +50 -0
  47. package/src/app/components/themes/markdown.tsx +249 -0
  48. package/src/app/components/themes/theme-preview.tsx +74 -0
  49. package/src/app/components/ui/menu.tsx +139 -0
  50. package/src/app/index.html +12 -0
  51. package/src/app/lib/assets.ts +151 -0
  52. package/src/app/lib/design-presets.ts +109 -0
  53. package/src/app/lib/design.ts +88 -0
  54. package/src/app/lib/doc-preview.tsx +29 -0
  55. package/src/app/lib/docs.ts +26 -0
  56. package/src/app/lib/export-html.ts +309 -0
  57. package/src/app/lib/export-pdf.ts +166 -0
  58. package/src/app/lib/flow-measure.ts +109 -0
  59. package/src/app/lib/flow.test.ts +64 -0
  60. package/src/app/lib/flow.ts +111 -0
  61. package/src/app/lib/folders.ts +192 -0
  62. package/src/app/lib/inspector/fiber.ts +99 -0
  63. package/src/app/lib/outline.ts +109 -0
  64. package/src/app/lib/page-context.tsx +38 -0
  65. package/src/app/lib/print-ready.ts +69 -0
  66. package/src/app/lib/sdk.test.ts +24 -0
  67. package/src/app/lib/sdk.ts +86 -0
  68. package/src/app/lib/themes.ts +31 -0
  69. package/src/app/lib/use-doc-module.ts +53 -0
  70. package/src/app/lib/use-doc-pages.ts +99 -0
  71. package/src/app/lib/utils.ts +6 -0
  72. package/src/app/main.tsx +14 -0
  73. package/src/app/routes/assets.tsx +257 -0
  74. package/src/app/routes/doc.tsx +480 -0
  75. package/src/app/routes/home-shell.tsx +203 -0
  76. package/src/app/routes/home.tsx +269 -0
  77. package/src/app/routes/themes.tsx +121 -0
  78. package/src/app/styles.css +72 -0
  79. package/src/app/virtual.d.ts +30 -0
@@ -0,0 +1,257 @@
1
+ import { Check, Copy, FileIcon, Loader2, Trash2, Upload } from 'lucide-react';
2
+ import { type DragEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react';
3
+ import {
4
+ type Asset,
5
+ deleteAsset,
6
+ formatBytes,
7
+ GLOBAL_SCOPE,
8
+ importSnippet,
9
+ isPreviewable,
10
+ listAssets,
11
+ renameAsset,
12
+ uploadAsset,
13
+ } from '../lib/assets';
14
+ import { docIds } from '../lib/docs';
15
+ import { cn } from '../lib/utils';
16
+
17
+ export function AssetsPage() {
18
+ const scopes = useMemo(() => [GLOBAL_SCOPE, ...[...docIds].sort()], []);
19
+ const [scope, setScope] = useState<string>(GLOBAL_SCOPE);
20
+ const [assets, setAssets] = useState<Asset[] | null>(null);
21
+ const [error, setError] = useState<string | null>(null);
22
+ const [busy, setBusy] = useState(false);
23
+ const [dragging, setDragging] = useState(false);
24
+ const inputRef = useRef<HTMLInputElement>(null);
25
+
26
+ const refresh = useCallback(async (target: string) => {
27
+ const result = await listAssets(target);
28
+ if (result.ok) {
29
+ setAssets(result.value);
30
+ setError(null);
31
+ } else {
32
+ setAssets([]);
33
+ setError(result.error);
34
+ }
35
+ }, []);
36
+
37
+ useEffect(() => {
38
+ setAssets(null);
39
+ void refresh(scope);
40
+ }, [scope, refresh]);
41
+
42
+ const upload = useCallback(
43
+ async (files: FileList | File[]) => {
44
+ setBusy(true);
45
+ setError(null);
46
+ for (const file of Array.from(files)) {
47
+ let result = await uploadAsset(scope, file);
48
+ if (!result.ok && result.error === 'asset exists') {
49
+ const replace = window.confirm(`"${file.name}" already exists in ${scope}. Replace it?`);
50
+ if (!replace) continue;
51
+ result = await uploadAsset(scope, file, { overwrite: true });
52
+ }
53
+ if (!result.ok) setError(`${file.name}: ${result.error}`);
54
+ }
55
+ await refresh(scope);
56
+ setBusy(false);
57
+ },
58
+ [scope, refresh],
59
+ );
60
+
61
+ const onDrop = (e: DragEvent<HTMLDivElement>) => {
62
+ e.preventDefault();
63
+ setDragging(false);
64
+ if (e.dataTransfer.files.length > 0) void upload(e.dataTransfer.files);
65
+ };
66
+
67
+ return (
68
+ <div>
69
+ <header className="border-border border-b px-8 py-6">
70
+ <h1 className="font-medium text-lg tracking-tight">Assets</h1>
71
+ <p className="mt-1 text-muted-foreground text-sm">
72
+ Images and fonts documents can import. Global files live in{' '}
73
+ <code className="font-mono">assets/</code>; per-document files in{' '}
74
+ <code className="font-mono">docs/&lt;id&gt;/assets/</code>.
75
+ </p>
76
+ </header>
77
+
78
+ <div className="flex flex-wrap items-center gap-2 px-8 pt-6">
79
+ {scopes.map((s) => (
80
+ <button
81
+ key={s}
82
+ type="button"
83
+ onClick={() => setScope(s)}
84
+ className={cn(
85
+ 'rounded-full border px-3 py-1 text-xs transition-colors',
86
+ s === scope
87
+ ? 'border-transparent bg-primary text-primary-foreground'
88
+ : 'border-border text-muted-foreground hover:bg-accent hover:text-foreground',
89
+ )}
90
+ >
91
+ {s === GLOBAL_SCOPE ? 'Global' : s}
92
+ </button>
93
+ ))}
94
+ </div>
95
+
96
+ <div className="px-8 py-6">
97
+ {/* biome-ignore lint/a11y/noStaticElementInteractions: drop target is an enhancement — the "Choose files" button is the keyboard path */}
98
+ <div
99
+ onDragOver={(e) => {
100
+ e.preventDefault();
101
+ setDragging(true);
102
+ }}
103
+ onDragLeave={() => setDragging(false)}
104
+ onDrop={onDrop}
105
+ className={cn(
106
+ 'flex items-center justify-between gap-4 rounded-lg border border-dashed px-5 py-4 transition-colors',
107
+ dragging ? 'border-primary bg-accent' : 'border-border',
108
+ )}
109
+ >
110
+ <div className="text-sm">
111
+ <p className="font-medium">Drop files to upload</p>
112
+ <p className="mt-0.5 text-muted-foreground text-xs">
113
+ Up to 25 MB each · png, jpg, svg, webp, pdf, woff2, csv…
114
+ </p>
115
+ </div>
116
+ <button
117
+ type="button"
118
+ onClick={() => inputRef.current?.click()}
119
+ disabled={busy}
120
+ className="flex flex-none items-center gap-1.5 rounded-md bg-primary px-3 py-1.5 text-primary-foreground text-xs transition-opacity hover:opacity-90 disabled:opacity-60"
121
+ >
122
+ {busy ? <Loader2 className="size-3.5 animate-spin" /> : <Upload className="size-3.5" />}
123
+ Choose files
124
+ </button>
125
+ <input
126
+ ref={inputRef}
127
+ type="file"
128
+ multiple
129
+ hidden
130
+ onChange={(e) => {
131
+ if (e.target.files?.length) void upload(e.target.files);
132
+ e.target.value = '';
133
+ }}
134
+ />
135
+ </div>
136
+
137
+ {error && (
138
+ <p className="mt-3 rounded-md border border-border bg-muted px-3 py-2 text-xs">{error}</p>
139
+ )}
140
+
141
+ {assets === null ? (
142
+ <div className="grid place-items-center py-16">
143
+ <Loader2 className="size-4 animate-spin text-muted-foreground" />
144
+ </div>
145
+ ) : assets.length === 0 ? (
146
+ <p className="py-16 text-center text-muted-foreground text-sm">
147
+ No assets in {scope === GLOBAL_SCOPE ? 'the global folder' : scope} yet.
148
+ </p>
149
+ ) : (
150
+ <div className="mt-6 grid gap-5 [grid-template-columns:repeat(auto-fill,minmax(180px,1fr))]">
151
+ {assets.map((asset) => (
152
+ <AssetCard
153
+ key={asset.name}
154
+ asset={asset}
155
+ scope={scope}
156
+ onChanged={() => refresh(scope)}
157
+ onError={setError}
158
+ />
159
+ ))}
160
+ </div>
161
+ )}
162
+ </div>
163
+ </div>
164
+ );
165
+ }
166
+
167
+ function AssetCard({
168
+ asset,
169
+ scope,
170
+ onChanged,
171
+ onError,
172
+ }: {
173
+ asset: Asset;
174
+ scope: string;
175
+ onChanged: () => void;
176
+ onError: (message: string) => void;
177
+ }) {
178
+ const [copied, setCopied] = useState(false);
179
+
180
+ const copyImport = async () => {
181
+ try {
182
+ await navigator.clipboard.writeText(importSnippet(asset));
183
+ setCopied(true);
184
+ setTimeout(() => setCopied(false), 1200);
185
+ } catch (err) {
186
+ onError(String((err as Error).message));
187
+ }
188
+ };
189
+
190
+ const rename = async () => {
191
+ const next = window.prompt('New file name', asset.name);
192
+ if (!next || next === asset.name) return;
193
+ const result = await renameAsset(scope, asset.name, next);
194
+ if (!result.ok) onError(`${asset.name}: ${result.error}`);
195
+ onChanged();
196
+ };
197
+
198
+ const remove = async () => {
199
+ if (!window.confirm(`Delete "${asset.name}"? This removes the file from disk.`)) return;
200
+ const result = await deleteAsset(scope, asset.name);
201
+ if (!result.ok) onError(`${asset.name}: ${result.error}`);
202
+ onChanged();
203
+ };
204
+
205
+ return (
206
+ <div className="flex flex-col gap-2">
207
+ <div className="relative grid h-32 place-items-center overflow-hidden rounded-md border border-border bg-muted">
208
+ {isPreviewable(asset.mime) ? (
209
+ <img
210
+ src={asset.url}
211
+ alt={asset.name}
212
+ className="max-h-full max-w-full object-contain p-2"
213
+ />
214
+ ) : (
215
+ <FileIcon className="size-6 text-muted-foreground" />
216
+ )}
217
+ {asset.unused && (
218
+ <span className="absolute top-1.5 left-1.5 rounded-full bg-background/90 px-1.5 py-0.5 text-[10px] text-muted-foreground">
219
+ unused
220
+ </span>
221
+ )}
222
+ </div>
223
+
224
+ <div className="min-w-0">
225
+ <button
226
+ type="button"
227
+ onClick={rename}
228
+ title="Rename"
229
+ className="block w-full truncate text-left text-xs hover:underline"
230
+ >
231
+ {asset.name}
232
+ </button>
233
+ <p className="mt-0.5 text-[11px] text-muted-foreground">{formatBytes(asset.size)}</p>
234
+ </div>
235
+
236
+ <div className="flex items-center gap-1">
237
+ <button
238
+ type="button"
239
+ onClick={copyImport}
240
+ title={importSnippet(asset)}
241
+ className="flex flex-1 items-center justify-center gap-1 rounded-md border border-border px-2 py-1 text-[11px] transition-colors hover:bg-accent"
242
+ >
243
+ {copied ? <Check className="size-3" /> : <Copy className="size-3" />}
244
+ {copied ? 'Copied' : 'Import'}
245
+ </button>
246
+ <button
247
+ type="button"
248
+ onClick={remove}
249
+ aria-label={`Delete ${asset.name}`}
250
+ className="flex size-6 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
251
+ >
252
+ <Trash2 className="size-3" />
253
+ </button>
254
+ </div>
255
+ </div>
256
+ );
257
+ }