@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,99 @@
1
+ import { useState } from 'react';
2
+ import type { FolderIcon } from '../../lib/sdk';
3
+ import { cn } from '../../lib/utils';
4
+
5
+ export const PRESET_COLORS = [
6
+ '#e5484d',
7
+ '#f76b15',
8
+ '#ffb224',
9
+ '#30a46c',
10
+ '#12a594',
11
+ '#0091ff',
12
+ '#6e56cf',
13
+ '#60646c',
14
+ ];
15
+
16
+ // A curated set beats an emoji-picker dependency for a folder chip.
17
+ const PRESET_EMOJI = [
18
+ '📁',
19
+ '📄',
20
+ '📊',
21
+ '📈',
22
+ '🧾',
23
+ '🗂️',
24
+ '📌',
25
+ '⭐',
26
+ '🏛️',
27
+ '💼',
28
+ '🧪',
29
+ '⚙️',
30
+ '🚀',
31
+ '🔒',
32
+ '💰',
33
+ '🗓️',
34
+ ];
35
+
36
+ export function IconPicker({
37
+ value,
38
+ onChange,
39
+ }: {
40
+ value: FolderIcon;
41
+ onChange: (icon: FolderIcon) => void;
42
+ }) {
43
+ const [tab, setTab] = useState<'emoji' | 'color'>(value.type);
44
+
45
+ return (
46
+ <div className="w-[232px]">
47
+ <div className="mb-2 flex gap-1 rounded-md bg-muted p-0.5">
48
+ {(['emoji', 'color'] as const).map((next) => (
49
+ <button
50
+ key={next}
51
+ type="button"
52
+ onClick={() => setTab(next)}
53
+ className={cn(
54
+ 'flex-1 rounded px-2 py-1 text-[11px] capitalize transition-colors',
55
+ tab === next ? 'bg-background font-medium' : 'text-muted-foreground',
56
+ )}
57
+ >
58
+ {next}
59
+ </button>
60
+ ))}
61
+ </div>
62
+
63
+ {tab === 'emoji' ? (
64
+ <div className="grid grid-cols-8 gap-1">
65
+ {PRESET_EMOJI.map((emoji) => (
66
+ <button
67
+ key={emoji}
68
+ type="button"
69
+ onClick={() => onChange({ type: 'emoji', value: emoji })}
70
+ className={cn(
71
+ 'grid size-6 place-items-center rounded text-[13px] transition-transform hover:scale-110',
72
+ value.type === 'emoji' && value.value === emoji && 'bg-accent',
73
+ )}
74
+ aria-label={emoji}
75
+ >
76
+ {emoji}
77
+ </button>
78
+ ))}
79
+ </div>
80
+ ) : (
81
+ <div className="grid grid-cols-8 gap-1">
82
+ {PRESET_COLORS.map((color) => (
83
+ <button
84
+ key={color}
85
+ type="button"
86
+ onClick={() => onChange({ type: 'color', value: color })}
87
+ className={cn(
88
+ 'size-6 rounded ring-1 ring-black/10 transition-transform hover:scale-110',
89
+ value.type === 'color' && value.value === color && 'ring-2 ring-foreground',
90
+ )}
91
+ style={{ background: color }}
92
+ aria-label={color}
93
+ />
94
+ ))}
95
+ </div>
96
+ )}
97
+ </div>
98
+ );
99
+ }
@@ -0,0 +1,252 @@
1
+ import config from 'virtual:open-doc/config';
2
+ import { Plus } from 'lucide-react';
3
+ import { useEffect, useRef, useState } from 'react';
4
+ import type { Folder, FolderIcon } from '../../lib/sdk';
5
+ import { cn } from '../../lib/utils';
6
+ import { ThemeToggle } from '../theme-toggle';
7
+ import { FolderIconChip, FolderItem } from './folder-item';
8
+ import { IconPicker, PRESET_COLORS } from './icon-picker';
9
+
10
+ export const ALL_DOCS_ID = '__all__';
11
+ export const DRAFT_ID = 'draft';
12
+ export const THEMES_ID = '__themes__';
13
+ export const ASSETS_ID = '__assets__';
14
+
15
+ export const FOLDER_DND_MIME = 'application/x-open-doc-folder-id';
16
+
17
+ type Props = {
18
+ folders: Folder[];
19
+ countFor: (folderId: string | null) => number;
20
+ allCount: number;
21
+ themesCount: number;
22
+ assetsCount: number;
23
+ selectedId: string;
24
+ onSelect: (id: string) => void;
25
+ onCreate: (name: string, icon: FolderIcon) => Promise<Folder>;
26
+ onRename: (id: string, name: string) => void;
27
+ onChangeIcon: (id: string, icon: FolderIcon) => void;
28
+ onDelete: (id: string) => void;
29
+ onDropToFolder: (folderId: string, docId: string) => void;
30
+ onDropToDraft: (docId: string) => void;
31
+ onReorder: (ids: string[]) => void;
32
+ };
33
+
34
+ export function Sidebar({
35
+ folders,
36
+ countFor,
37
+ allCount,
38
+ themesCount,
39
+ assetsCount,
40
+ selectedId,
41
+ onSelect,
42
+ onCreate,
43
+ onRename,
44
+ onChangeIcon,
45
+ onDelete,
46
+ onDropToFolder,
47
+ onDropToDraft,
48
+ onReorder,
49
+ }: Props) {
50
+ const [dragId, setDragId] = useState<string | null>(null);
51
+ const [dropTarget, setDropTarget] = useState<{ id: string; before: boolean } | null>(null);
52
+ const [creating, setCreating] = useState(false);
53
+ const [newName, setNewName] = useState('');
54
+ const [newIcon, setNewIcon] = useState<FolderIcon>({ type: 'color', value: PRESET_COLORS[0] });
55
+ const inputRef = useRef<HTMLInputElement>(null);
56
+
57
+ useEffect(() => {
58
+ if (creating) inputRef.current?.focus();
59
+ }, [creating]);
60
+
61
+ const finishReorder = (toId: string, before: boolean) => {
62
+ const fromId = dragId;
63
+ setDragId(null);
64
+ setDropTarget(null);
65
+ if (!fromId || fromId === toId) return;
66
+ const ids = folders.map((f) => f.id);
67
+ if (!ids.includes(fromId) || !ids.includes(toId)) return;
68
+ const next = ids.filter((id) => id !== fromId);
69
+ next.splice(next.indexOf(toId) + (before ? 0 : 1), 0, fromId);
70
+ if (next.every((id, i) => id === ids[i])) return;
71
+ onReorder(next);
72
+ };
73
+
74
+ const startCreating = () => {
75
+ setNewIcon({ type: 'color', value: PRESET_COLORS[folders.length % PRESET_COLORS.length] });
76
+ setNewName('');
77
+ setCreating(true);
78
+ };
79
+
80
+ const commitCreate = async () => {
81
+ const trimmed = newName.trim();
82
+ setCreating(false);
83
+ setNewName('');
84
+ if (!trimmed) return;
85
+ await onCreate(trimmed, newIcon).catch(() => {});
86
+ };
87
+
88
+ return (
89
+ <aside className="flex h-full w-[16.5rem] shrink-0 flex-col border-border border-r bg-background">
90
+ <div className="flex items-center justify-between px-4 pt-4 pb-3">
91
+ <h1 className="font-semibold text-base tracking-tight">open-doc</h1>
92
+ <div className="-mr-1 flex items-center gap-0.5">
93
+ <ThemeToggle />
94
+ </div>
95
+ </div>
96
+
97
+ <div className="px-2">
98
+ <FolderItem
99
+ row={{ kind: 'all' }}
100
+ count={allCount}
101
+ selected={selectedId === ALL_DOCS_ID}
102
+ onSelect={() => onSelect(ALL_DOCS_ID)}
103
+ onDropDoc={() => {}}
104
+ />
105
+ <FolderItem
106
+ row={{ kind: 'themes' }}
107
+ count={themesCount}
108
+ selected={selectedId === THEMES_ID}
109
+ onSelect={() => onSelect(THEMES_ID)}
110
+ onDropDoc={() => {}}
111
+ />
112
+ {import.meta.env.DEV && (
113
+ <FolderItem
114
+ row={{ kind: 'assets' }}
115
+ count={assetsCount}
116
+ selected={selectedId === ASSETS_ID}
117
+ onSelect={() => onSelect(ASSETS_ID)}
118
+ onDropDoc={() => {}}
119
+ />
120
+ )}
121
+ </div>
122
+
123
+ <div className="mt-5 flex items-center gap-2 px-4 pb-1">
124
+ <span className="text-[10px] text-muted-foreground uppercase tracking-wider">Folders</span>
125
+ <span className="h-px flex-1 bg-border" aria-hidden />
126
+ </div>
127
+
128
+ <div className="min-h-0 flex-1 overflow-y-auto px-2 pb-2">
129
+ <FolderItem
130
+ row={{ kind: 'draft' }}
131
+ count={countFor(null)}
132
+ selected={selectedId === DRAFT_ID}
133
+ onSelect={() => onSelect(DRAFT_ID)}
134
+ onDropDoc={onDropToDraft}
135
+ />
136
+
137
+ {folders.map((folder) => {
138
+ const isTarget = dropTarget?.id === folder.id;
139
+ return (
140
+ // biome-ignore lint/a11y/noStaticElementInteractions: drag handle wraps the row, which keeps its own button
141
+ <div
142
+ key={folder.id}
143
+ className={cn(
144
+ 'relative',
145
+ isTarget &&
146
+ dropTarget.before &&
147
+ 'before:-top-px before:absolute before:inset-x-2 before:h-[2px] before:rounded-full before:bg-foreground',
148
+ isTarget &&
149
+ !dropTarget.before &&
150
+ 'after:-bottom-px after:absolute after:inset-x-2 after:h-[2px] after:rounded-full after:bg-foreground',
151
+ dragId === folder.id && 'opacity-50',
152
+ )}
153
+ draggable={import.meta.env.DEV}
154
+ onDragStart={(e) => {
155
+ if (!import.meta.env.DEV) return;
156
+ e.dataTransfer.setData(FOLDER_DND_MIME, folder.id);
157
+ e.dataTransfer.effectAllowed = 'move';
158
+ setDragId(folder.id);
159
+ }}
160
+ onDragEnd={() => {
161
+ setDragId(null);
162
+ setDropTarget(null);
163
+ }}
164
+ onDragOver={(e) => {
165
+ if (!e.dataTransfer.types.includes(FOLDER_DND_MIME)) return;
166
+ e.preventDefault();
167
+ e.dataTransfer.dropEffect = 'move';
168
+ const rect = e.currentTarget.getBoundingClientRect();
169
+ const before = e.clientY < rect.top + rect.height / 2;
170
+ if (!dropTarget || dropTarget.id !== folder.id || dropTarget.before !== before) {
171
+ setDropTarget({ id: folder.id, before });
172
+ }
173
+ }}
174
+ onDragLeave={(e) => {
175
+ if (e.currentTarget.contains(e.relatedTarget as Node | null)) return;
176
+ if (dropTarget?.id === folder.id) setDropTarget(null);
177
+ }}
178
+ onDrop={(e) => {
179
+ const fromId = e.dataTransfer.getData(FOLDER_DND_MIME);
180
+ if (!fromId) return;
181
+ e.preventDefault();
182
+ e.stopPropagation();
183
+ const rect = e.currentTarget.getBoundingClientRect();
184
+ finishReorder(folder.id, e.clientY < rect.top + rect.height / 2);
185
+ }}
186
+ >
187
+ <FolderItem
188
+ row={{
189
+ kind: 'folder',
190
+ folder,
191
+ onRename: (name) => onRename(folder.id, name),
192
+ onChangeIcon: (icon) => onChangeIcon(folder.id, icon),
193
+ onDelete: () => onDelete(folder.id),
194
+ }}
195
+ count={countFor(folder.id)}
196
+ selected={selectedId === folder.id}
197
+ onSelect={() => onSelect(folder.id)}
198
+ onDropDoc={(docId) => onDropToFolder(folder.id, docId)}
199
+ />
200
+ </div>
201
+ );
202
+ })}
203
+
204
+ {import.meta.env.DEV &&
205
+ (creating ? (
206
+ <div className="mt-1 flex items-center gap-2 rounded-md border border-foreground/30 border-dashed px-2 py-1">
207
+ <FolderIconChip icon={newIcon} />
208
+ <input
209
+ ref={inputRef}
210
+ value={newName}
211
+ onChange={(e) => setNewName(e.target.value)}
212
+ onBlur={commitCreate}
213
+ onKeyDown={(e) => {
214
+ if (e.nativeEvent.isComposing) return;
215
+ if (e.key === 'Enter') void commitCreate();
216
+ if (e.key === 'Escape') {
217
+ setCreating(false);
218
+ setNewName('');
219
+ }
220
+ }}
221
+ placeholder="Folder name"
222
+ maxLength={40}
223
+ className="min-w-0 flex-1 bg-transparent text-[13px] outline-none placeholder:text-muted-foreground/60"
224
+ />
225
+ </div>
226
+ ) : (
227
+ <button
228
+ type="button"
229
+ onClick={startCreating}
230
+ className="mt-1 flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-[12px] text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
231
+ >
232
+ <Plus className="size-3.5" />
233
+ New folder
234
+ </button>
235
+ ))}
236
+
237
+ {creating && (
238
+ <div className="mt-1 rounded-md border border-border p-2">
239
+ <IconPicker value={newIcon} onChange={setNewIcon} />
240
+ </div>
241
+ )}
242
+ </div>
243
+
244
+ <div className="border-border border-t px-4 py-3 text-[11px] text-muted-foreground">
245
+ <div className="flex items-center justify-between">
246
+ <span>v{config.version}</span>
247
+ <span className="font-mono">{import.meta.env.DEV ? 'dev' : 'static'}</span>
248
+ </div>
249
+ </div>
250
+ </aside>
251
+ );
252
+ }
@@ -0,0 +1,81 @@
1
+ import type { CSSProperties } from 'react';
2
+ import { useDocOutline } from '../lib/outline';
3
+ import { useDocPageNumber } from '../lib/page-context';
4
+
5
+ export type TableOfContentsProps = {
6
+ /** Deepest heading level to list. Defaults to 2 (h1 + h2). */
7
+ maxLevel?: number;
8
+ /** Show the page number column with dot leaders. Defaults to true. */
9
+ showPageNumbers?: boolean;
10
+ /** List headings that sit on the same page as the TOC itself. Defaults to false. */
11
+ includeOwnPage?: boolean;
12
+ /** Indent per heading level, in px. Defaults to 20. */
13
+ indent?: number;
14
+ style?: CSSProperties;
15
+ className?: string;
16
+ };
17
+
18
+ /**
19
+ * Renders the document's headings as a contents list. The outline is filled in
20
+ * by a DOM scan after the pages render, so this component is empty on the very
21
+ * first pass and populated on the next — including during PDF/HTML export,
22
+ * which scans before serializing.
23
+ */
24
+ export function TableOfContents({
25
+ maxLevel = 2,
26
+ showPageNumbers = true,
27
+ includeOwnPage = false,
28
+ indent = 20,
29
+ style,
30
+ className,
31
+ }: TableOfContentsProps) {
32
+ const outline = useDocOutline();
33
+ const ownPage = useDocPageNumber();
34
+
35
+ const entries = outline.filter(
36
+ (entry) => entry.level <= maxLevel && (includeOwnPage || entry.page !== ownPage),
37
+ );
38
+
39
+ return (
40
+ <div
41
+ data-od-toc=""
42
+ className={className}
43
+ style={{
44
+ fontFamily: 'var(--od-font-body)',
45
+ fontSize: 'var(--od-size-body)',
46
+ color: 'var(--od-text)',
47
+ ...style,
48
+ }}
49
+ >
50
+ {entries.map((entry) => (
51
+ <div
52
+ key={entry.id}
53
+ style={{
54
+ display: 'flex',
55
+ alignItems: 'baseline',
56
+ gap: 8,
57
+ paddingLeft: (entry.level - 1) * indent,
58
+ marginBottom: entry.level === 1 ? 10 : 6,
59
+ fontWeight: entry.level === 1 ? 600 : 400,
60
+ color: entry.level === 1 ? 'var(--od-text)' : 'var(--od-muted)',
61
+ }}
62
+ >
63
+ <span>{entry.text}</span>
64
+ {showPageNumbers && (
65
+ <>
66
+ <span
67
+ aria-hidden
68
+ style={{
69
+ flex: 1,
70
+ borderBottom: '1px dotted var(--od-rule)',
71
+ transform: 'translateY(-3px)',
72
+ }}
73
+ />
74
+ <span style={{ fontVariantNumeric: 'tabular-nums' }}>{entry.page}</span>
75
+ </>
76
+ )}
77
+ </div>
78
+ ))}
79
+ </div>
80
+ );
81
+ }
@@ -0,0 +1,50 @@
1
+ import { Monitor, Moon, Sun } from 'lucide-react';
2
+ import { useTheme } from 'next-themes';
3
+ import { useEffect, useState } from 'react';
4
+ import { Menu, MenuItem } from './ui/menu';
5
+
6
+ const OPTIONS = [
7
+ { value: 'light', label: 'Light', icon: Sun },
8
+ { value: 'dark', label: 'Dark', icon: Moon },
9
+ { value: 'system', label: 'System', icon: Monitor },
10
+ ] as const;
11
+
12
+ export function ThemeToggle() {
13
+ const { theme, setTheme } = useTheme();
14
+ const [mounted, setMounted] = useState(false);
15
+
16
+ useEffect(() => setMounted(true), []);
17
+
18
+ return (
19
+ <Menu
20
+ trigger={(props) => (
21
+ <button
22
+ type="button"
23
+ aria-label="Toggle theme"
24
+ title="Theme"
25
+ className="relative flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground aria-expanded:bg-accent"
26
+ {...props}
27
+ >
28
+ <Sun className="size-3.5 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
29
+ <Moon className="absolute size-3.5 rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
30
+ </button>
31
+ )}
32
+ >
33
+ {(close) =>
34
+ OPTIONS.map(({ value, label, icon: Icon }) => (
35
+ <MenuItem
36
+ key={value}
37
+ active={mounted && theme === value}
38
+ onClick={() => {
39
+ setTheme(value);
40
+ close();
41
+ }}
42
+ >
43
+ <Icon className="size-3.5" />
44
+ {label}
45
+ </MenuItem>
46
+ ))
47
+ }
48
+ </Menu>
49
+ );
50
+ }