@open-document/core 0.10.0 → 0.12.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/dist/{build-DJMLK50m.js → build-DfUfpeTR.js} +1 -1
- package/dist/{check-CUKuufBm.js → check-CHxiOkQG.js} +3 -3
- package/dist/cli/bin.js +20 -33
- package/dist/{config-CvzpyAUA.js → config-9IdbhVuT.js} +2015 -725
- package/dist/{context-B4ip7SoI.js → context-BLI1-64_.js} +2 -2
- package/dist/{dev-dnH7DTlD.js → dev-yNYKv5mm.js} +1 -1
- package/dist/{diff-QTo0A5Rx.js → diff-Bs205mOx.js} +2 -2
- package/dist/{export-BrRFrexj.js → export-tkquS5O5.js} +11 -4
- package/dist/{import-C14Vn9ER.js → import-D1hIKIr3.js} +3 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +63 -28
- package/dist/ops/index.d.ts +111 -2
- package/dist/ops/index.js +3 -3
- package/dist/{ops-DKidjLXP.js → ops-Dgqujq5a.js} +85 -164
- package/dist/package-version-C89gtYSk.js +36 -0
- package/dist/{preview-wKM4C6Uk.js → preview-BImdTH6K.js} +1 -1
- package/dist/{sdk-CpDEGRIo.d.ts → sdk-CHEGeINh.d.ts} +46 -1
- package/dist/templates-DT5zzqHq.js +34 -0
- package/dist/vite/index.js +1 -1
- package/package.json +2 -1
- package/skills/apply-comments/SKILL.md +3 -1
- package/skills/create-doc/SKILL.md +2 -0
- package/skills/doc-authoring/SKILL.md +2 -0
- package/src/app/components/browser/browser-ui.tsx +21 -0
- package/src/app/components/browser/new-document-dialog.tsx +208 -0
- package/src/app/components/changes/changes-overlay.tsx +68 -0
- package/src/app/components/changes/changes-panel.tsx +341 -0
- package/src/app/components/changes/use-changes.ts +85 -0
- package/src/app/components/comments/comments-overlay.tsx +50 -0
- package/src/app/components/comments/comments-panel.tsx +272 -0
- package/src/app/components/comments/use-comments.ts +61 -0
- package/src/app/components/doc-sidebar.tsx +32 -2
- package/src/app/components/inspector/field-source.tsx +41 -0
- package/src/app/components/inspector/format-section.tsx +434 -0
- package/src/app/components/inspector/inspector.tsx +311 -118
- package/src/app/components/inspector/use-style-edits.ts +210 -0
- package/src/app/components/overlay-frame.tsx +79 -0
- package/src/app/components/page-frame.tsx +4 -0
- package/src/app/components/page-placement.ts +68 -0
- package/src/app/components/panel/fields.tsx +95 -7
- package/src/app/components/record-picker.tsx +79 -0
- package/src/app/components/themes/theme-preview.tsx +5 -4
- package/src/app/lib/agent-bridge.ts +29 -1
- package/src/app/lib/docx/ooxml.test.ts +24 -1
- package/src/app/lib/docx/ooxml.ts +27 -1
- package/src/app/lib/export-docx.ts +3 -1
- package/src/app/lib/export-dom.ts +2 -1
- package/src/app/lib/export-html.ts +4 -2
- package/src/app/lib/export-image.ts +2 -1
- package/src/app/lib/flow-measure.test.ts +30 -0
- package/src/app/lib/flow-measure.ts +35 -10
- package/src/app/lib/i18n-ja/browser.ts +10 -0
- package/src/app/lib/i18n-ja/doc.ts +4 -0
- package/src/app/lib/i18n-ja/panels.ts +92 -5
- package/src/app/lib/i18n-ko/browser.ts +10 -0
- package/src/app/lib/i18n-ko/doc.ts +4 -0
- package/src/app/lib/i18n-ko/panels.ts +90 -5
- package/src/app/lib/i18n-zh-cn/browser.ts +10 -0
- package/src/app/lib/i18n-zh-cn/doc.ts +4 -0
- package/src/app/lib/i18n-zh-cn/panels.ts +88 -5
- package/src/app/lib/i18n-zh-tw/browser.ts +10 -0
- package/src/app/lib/i18n-zh-tw/doc.ts +4 -0
- package/src/app/lib/i18n-zh-tw/panels.ts +88 -5
- package/src/app/lib/inspector/format.ts +156 -0
- package/src/app/lib/records.test.ts +38 -0
- package/src/app/lib/records.ts +127 -0
- package/src/app/lib/sdk.ts +29 -0
- package/src/app/lib/use-doc-module.ts +7 -0
- package/src/app/lib/use-doc-pages.ts +15 -9
- package/src/app/routes/doc.tsx +274 -11
- package/src/app/routes/home.tsx +30 -5
- package/src/app/styles.css +23 -0
- package/templates/blank/index.tsx +63 -0
- package/templates/blank/template.json +6 -0
- package/templates/certificate/data/recipients.csv +4 -0
- package/templates/certificate/index.tsx +128 -0
- package/templates/certificate/template.json +6 -0
- package/templates/letter/index.tsx +106 -0
- package/templates/letter/template.json +5 -0
- package/templates/meeting-notes/index.tsx +194 -0
- package/templates/meeting-notes/template.json +6 -0
- package/templates/proposal/index.tsx +264 -0
- package/templates/proposal/template.json +6 -0
- package/templates/report/data/metrics.csv +5 -0
- package/templates/report/index.tsx +224 -0
- package/templates/report/template.json +6 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Award,
|
|
3
|
+
FileText,
|
|
4
|
+
type LucideIcon,
|
|
5
|
+
Mail,
|
|
6
|
+
NotebookPen,
|
|
7
|
+
Presentation,
|
|
8
|
+
Square,
|
|
9
|
+
Users,
|
|
10
|
+
X,
|
|
11
|
+
} from 'lucide-react';
|
|
12
|
+
import { type FormEvent, useEffect, useRef, useState } from 'react';
|
|
13
|
+
import { useNavigate } from 'react-router-dom';
|
|
14
|
+
import type { TemplateSummary } from '../../../ops/templates';
|
|
15
|
+
import { useT } from '../../lib/i18n';
|
|
16
|
+
|
|
17
|
+
const ICONS: Record<string, LucideIcon> = {
|
|
18
|
+
blank: Square,
|
|
19
|
+
report: FileText,
|
|
20
|
+
proposal: Presentation,
|
|
21
|
+
'meeting-notes': Users,
|
|
22
|
+
letter: Mail,
|
|
23
|
+
certificate: Award,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const iconOf = (template: TemplateSummary) => ICONS[template.name] ?? NotebookPen;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Starting a document: the templates as cards, each with the name the CLI and
|
|
30
|
+
* an agent use for it, and a title. A native modal dialog, so focus is trapped
|
|
31
|
+
* while it is open and goes back to the button that opened it.
|
|
32
|
+
*/
|
|
33
|
+
export function NewDocumentDialog({
|
|
34
|
+
open,
|
|
35
|
+
folderId,
|
|
36
|
+
onClose,
|
|
37
|
+
}: {
|
|
38
|
+
open: boolean;
|
|
39
|
+
/** The folder being viewed, which the new document is filed into. */
|
|
40
|
+
folderId: string | null;
|
|
41
|
+
onClose: () => void;
|
|
42
|
+
}) {
|
|
43
|
+
const t = useT();
|
|
44
|
+
const navigate = useNavigate();
|
|
45
|
+
const dialogRef = useRef<HTMLDialogElement>(null);
|
|
46
|
+
const [templates, setTemplates] = useState<TemplateSummary[] | null>(null);
|
|
47
|
+
const [chosen, setChosen] = useState('blank');
|
|
48
|
+
const [title, setTitle] = useState('');
|
|
49
|
+
const [busy, setBusy] = useState(false);
|
|
50
|
+
const [error, setError] = useState<string | null>(null);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const dialog = dialogRef.current;
|
|
54
|
+
if (!dialog) return;
|
|
55
|
+
if (open && !dialog.open) dialog.showModal();
|
|
56
|
+
if (!open && dialog.open) dialog.close();
|
|
57
|
+
}, [open]);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!open) return;
|
|
61
|
+
setError(null);
|
|
62
|
+
setTitle('');
|
|
63
|
+
let cancelled = false;
|
|
64
|
+
fetch('/__templates')
|
|
65
|
+
.then((res) => res.json())
|
|
66
|
+
.then((body: { templates?: TemplateSummary[] }) => {
|
|
67
|
+
if (cancelled) return;
|
|
68
|
+
const list = body.templates ?? [];
|
|
69
|
+
setTemplates(list);
|
|
70
|
+
setChosen((current) =>
|
|
71
|
+
list.some((entry) => entry.name === current) ? current : (list[0]?.name ?? 'blank'),
|
|
72
|
+
);
|
|
73
|
+
})
|
|
74
|
+
.catch(() => {
|
|
75
|
+
if (!cancelled) setTemplates([]);
|
|
76
|
+
});
|
|
77
|
+
return () => {
|
|
78
|
+
cancelled = true;
|
|
79
|
+
};
|
|
80
|
+
}, [open]);
|
|
81
|
+
|
|
82
|
+
const create = async (event: FormEvent) => {
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
if (busy) return;
|
|
85
|
+
setBusy(true);
|
|
86
|
+
setError(null);
|
|
87
|
+
try {
|
|
88
|
+
const res = await fetch(`/__templates/${encodeURIComponent(chosen)}/create`, {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
headers: { 'content-type': 'application/json' },
|
|
91
|
+
body: JSON.stringify({
|
|
92
|
+
...(title.trim() ? { title: title.trim() } : {}),
|
|
93
|
+
...(folderId ? { folderId } : {}),
|
|
94
|
+
}),
|
|
95
|
+
});
|
|
96
|
+
const body = (await res.json()) as { docId?: string; error?: string };
|
|
97
|
+
if (!res.ok || !body.docId) {
|
|
98
|
+
setError(body.error ?? t('Could not create the document'));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
onClose();
|
|
102
|
+
navigate(`/d/${body.docId}`);
|
|
103
|
+
} catch {
|
|
104
|
+
setError(t('Could not create the document'));
|
|
105
|
+
} finally {
|
|
106
|
+
setBusy(false);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const selected = templates?.find((entry) => entry.name === chosen);
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<dialog
|
|
114
|
+
ref={dialogRef}
|
|
115
|
+
aria-labelledby="new-document-title"
|
|
116
|
+
onClose={onClose}
|
|
117
|
+
onCancel={onClose}
|
|
118
|
+
className="m-auto w-[min(720px,calc(100vw-32px))] rounded-xl border border-border bg-background p-0 text-foreground shadow-2xl backdrop:bg-black/40"
|
|
119
|
+
>
|
|
120
|
+
<form onSubmit={create} className="flex max-h-[min(640px,calc(100vh-64px))] flex-col">
|
|
121
|
+
<header className="flex h-12 flex-none items-center justify-between border-border border-b px-4">
|
|
122
|
+
<h2 id="new-document-title" className="font-medium text-sm">
|
|
123
|
+
{t('New document')}
|
|
124
|
+
</h2>
|
|
125
|
+
<button
|
|
126
|
+
type="button"
|
|
127
|
+
aria-label={t('Close')}
|
|
128
|
+
onClick={onClose}
|
|
129
|
+
className="flex size-8 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-foreground/60"
|
|
130
|
+
>
|
|
131
|
+
<X className="size-4" />
|
|
132
|
+
</button>
|
|
133
|
+
</header>
|
|
134
|
+
|
|
135
|
+
<fieldset className="m-0 min-h-0 flex-1 overflow-y-auto border-0 p-4">
|
|
136
|
+
<legend className="sr-only">{t('Template')}</legend>
|
|
137
|
+
{templates === null ? (
|
|
138
|
+
<div className="h-0.5 overflow-hidden rounded bg-muted">
|
|
139
|
+
<div className="h-full w-1/3 animate-pulse bg-foreground/20" />
|
|
140
|
+
</div>
|
|
141
|
+
) : (
|
|
142
|
+
<div className="grid grid-cols-[repeat(auto-fill,minmax(200px,1fr))] gap-2">
|
|
143
|
+
{templates.map((template) => {
|
|
144
|
+
const Icon = iconOf(template);
|
|
145
|
+
return (
|
|
146
|
+
<label
|
|
147
|
+
key={template.name}
|
|
148
|
+
className="flex cursor-pointer flex-col gap-1.5 rounded-lg border border-border p-3 transition-colors hover:bg-accent/50 has-[:checked]:border-foreground/60 has-[:checked]:bg-accent has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-foreground/60"
|
|
149
|
+
>
|
|
150
|
+
<input
|
|
151
|
+
type="radio"
|
|
152
|
+
name="template"
|
|
153
|
+
value={template.name}
|
|
154
|
+
checked={chosen === template.name}
|
|
155
|
+
onChange={() => setChosen(template.name)}
|
|
156
|
+
className="sr-only"
|
|
157
|
+
/>
|
|
158
|
+
<span className="flex items-center gap-2">
|
|
159
|
+
<Icon aria-hidden className="size-4 text-muted-foreground" />
|
|
160
|
+
<span className="flex-1 font-medium text-xs">{t(template.title)}</span>
|
|
161
|
+
{template.source === 'workspace' && (
|
|
162
|
+
<span className="rounded bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">
|
|
163
|
+
{t('This workspace')}
|
|
164
|
+
</span>
|
|
165
|
+
)}
|
|
166
|
+
</span>
|
|
167
|
+
<span className="line-clamp-2 text-[11px] text-muted-foreground leading-snug">
|
|
168
|
+
{template.description}
|
|
169
|
+
</span>
|
|
170
|
+
<code className="font-mono text-[10px] text-muted-foreground/80">
|
|
171
|
+
{template.name}
|
|
172
|
+
</code>
|
|
173
|
+
</label>
|
|
174
|
+
);
|
|
175
|
+
})}
|
|
176
|
+
</div>
|
|
177
|
+
)}
|
|
178
|
+
</fieldset>
|
|
179
|
+
|
|
180
|
+
<footer className="flex flex-none flex-col gap-2 border-border border-t p-4">
|
|
181
|
+
<div className="flex items-center gap-2">
|
|
182
|
+
<input
|
|
183
|
+
autoFocus
|
|
184
|
+
type="text"
|
|
185
|
+
value={title}
|
|
186
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
187
|
+
placeholder={selected ? t(selected.title) : t('Title')}
|
|
188
|
+
aria-label={t('Title')}
|
|
189
|
+
className="h-9 min-w-0 flex-1 rounded-md border border-border bg-transparent px-3 text-sm outline-none focus:border-foreground/40"
|
|
190
|
+
/>
|
|
191
|
+
<button
|
|
192
|
+
type="submit"
|
|
193
|
+
disabled={busy || templates === null}
|
|
194
|
+
className="h-9 rounded-md bg-primary px-4 text-primary-foreground text-sm transition-opacity hover:opacity-90 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-foreground/60 disabled:opacity-60"
|
|
195
|
+
>
|
|
196
|
+
{t('Create')}
|
|
197
|
+
</button>
|
|
198
|
+
</div>
|
|
199
|
+
{error && (
|
|
200
|
+
<p role="alert" className="text-xs" style={{ color: 'var(--change-removed)' }}>
|
|
201
|
+
{error}
|
|
202
|
+
</p>
|
|
203
|
+
)}
|
|
204
|
+
</footer>
|
|
205
|
+
</form>
|
|
206
|
+
</dialog>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import type { ChangeMark } from '../../../ops/changes-diff';
|
|
4
|
+
import { OverlayFrame } from '../overlay-frame';
|
|
5
|
+
import { elementsAtLoc } from '../page-placement';
|
|
6
|
+
import type { ChangeItem } from './use-changes';
|
|
7
|
+
|
|
8
|
+
/** Every element on the page that one change touches, with how it is marked. */
|
|
9
|
+
export function elementsOf(
|
|
10
|
+
container: ParentNode,
|
|
11
|
+
item: ChangeItem,
|
|
12
|
+
): Array<{ el: HTMLElement; mark: ChangeMark }> {
|
|
13
|
+
return item.hunk.targets.flatMap((target) =>
|
|
14
|
+
elementsAtLoc(container, target.loc).map((el) => ({ el, mark: target.mark })),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const COLOR: Record<ChangeMark, string> = {
|
|
19
|
+
added: 'var(--change-added)',
|
|
20
|
+
changed: 'var(--change-changed)',
|
|
21
|
+
'removed-before': 'var(--change-removed)',
|
|
22
|
+
'removed-after': 'var(--change-removed)',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function markStyle(mark: ChangeMark, active: boolean): CSSProperties {
|
|
26
|
+
const color = COLOR[mark];
|
|
27
|
+
// Something taken out has no box of its own: a bar where it used to be.
|
|
28
|
+
if (mark === 'removed-before' || mark === 'removed-after') {
|
|
29
|
+
return {
|
|
30
|
+
[mark === 'removed-before' ? 'borderTop' : 'borderBottom']:
|
|
31
|
+
`${active ? 3 : 2}px dashed ${color}`,
|
|
32
|
+
transform: `translateY(${mark === 'removed-before' ? -5 : 5}px)`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
outline: `${active ? 2 : 1.5}px solid ${color}`,
|
|
37
|
+
outlineOffset: active ? 3 : 2,
|
|
38
|
+
background: `color-mix(in oklch, ${color} ${active ? 14 : 8}%, transparent)`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The changes drawn onto the sheets: a box for what is new or rewritten, a bar where something went. */
|
|
43
|
+
export function ChangesOverlay({
|
|
44
|
+
container,
|
|
45
|
+
items,
|
|
46
|
+
activeId,
|
|
47
|
+
}: {
|
|
48
|
+
container: HTMLElement;
|
|
49
|
+
items: ChangeItem[];
|
|
50
|
+
activeId: string | null;
|
|
51
|
+
}) {
|
|
52
|
+
return createPortal(
|
|
53
|
+
<div aria-hidden className="pointer-events-none absolute inset-0 z-20">
|
|
54
|
+
{items.flatMap((item) =>
|
|
55
|
+
elementsOf(container, item).map(({ el, mark }, at) => (
|
|
56
|
+
<OverlayFrame
|
|
57
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: one hunk can print the same loc twice
|
|
58
|
+
key={`${item.hunk.id}:${at}`}
|
|
59
|
+
anchor={el}
|
|
60
|
+
container={container}
|
|
61
|
+
style={markStyle(mark, item.hunk.id === activeId)}
|
|
62
|
+
/>
|
|
63
|
+
)),
|
|
64
|
+
)}
|
|
65
|
+
</div>,
|
|
66
|
+
container,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { ChevronDown, ChevronUp, CircleCheck, FileText, GitCompare, Undo2, X } from 'lucide-react';
|
|
2
|
+
import { type ReactNode, useState } from 'react';
|
|
3
|
+
import { useT } from '../../lib/i18n';
|
|
4
|
+
import { PanelIconButton, PanelShell } from '../panel/panel-shell';
|
|
5
|
+
import type { ChangeItem, ChangesState, Revision } from './use-changes';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A source line as a reader sees it: tags and braces off, so the words are
|
|
9
|
+
* what show. A line that is only markup reads as nothing and is left out.
|
|
10
|
+
*/
|
|
11
|
+
function readable(line: string): string {
|
|
12
|
+
return line
|
|
13
|
+
.replace(/<\/?[A-Za-z][^>]*>/g, ' ')
|
|
14
|
+
.replace(/[{}]/g, '')
|
|
15
|
+
.replace(/\s+/g, ' ')
|
|
16
|
+
.trim();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const MAX_LINES = 3;
|
|
20
|
+
|
|
21
|
+
function Lines({
|
|
22
|
+
lines,
|
|
23
|
+
kind,
|
|
24
|
+
raw = false,
|
|
25
|
+
}: {
|
|
26
|
+
lines: string[];
|
|
27
|
+
kind: 'added' | 'removed';
|
|
28
|
+
/** Show the source as written — for a change that is markup only, with no words to show. */
|
|
29
|
+
raw?: boolean;
|
|
30
|
+
}) {
|
|
31
|
+
const shown = raw
|
|
32
|
+
? lines.map((line) => line.trim()).filter(Boolean)
|
|
33
|
+
: lines.map(readable).filter(Boolean);
|
|
34
|
+
if (shown.length === 0) return null;
|
|
35
|
+
return (
|
|
36
|
+
<div className="flex flex-col gap-0.5">
|
|
37
|
+
{shown.slice(0, MAX_LINES).map((line, at) => (
|
|
38
|
+
<p
|
|
39
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: lines are positional
|
|
40
|
+
key={at}
|
|
41
|
+
className={`line-clamp-2 break-words text-[11px] leading-snug ${raw ? 'font-mono' : ''} ${
|
|
42
|
+
kind === 'removed' ? 'text-muted-foreground line-through' : 'text-foreground'
|
|
43
|
+
}`}
|
|
44
|
+
style={{
|
|
45
|
+
textDecorationColor: kind === 'removed' ? 'var(--change-removed)' : undefined,
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
<span
|
|
49
|
+
aria-hidden
|
|
50
|
+
className="mr-1 font-mono"
|
|
51
|
+
style={{ color: kind === 'removed' ? 'var(--change-removed)' : 'var(--change-added)' }}
|
|
52
|
+
>
|
|
53
|
+
{kind === 'removed' ? '−' : '+'}
|
|
54
|
+
</span>
|
|
55
|
+
{line}
|
|
56
|
+
</p>
|
|
57
|
+
))}
|
|
58
|
+
{shown.length > MAX_LINES && (
|
|
59
|
+
<span className="pl-3 font-mono text-[10px] text-muted-foreground">
|
|
60
|
+
+{shown.length - MAX_LINES}
|
|
61
|
+
</span>
|
|
62
|
+
)}
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function markColor(item: ChangeItem): string {
|
|
68
|
+
const { hunk } = item;
|
|
69
|
+
if (hunk.newLines === 0) return 'var(--change-removed)';
|
|
70
|
+
if (hunk.oldLines === 0) return 'var(--change-added)';
|
|
71
|
+
return 'var(--change-changed)';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function Item({
|
|
75
|
+
item,
|
|
76
|
+
active,
|
|
77
|
+
onSelect,
|
|
78
|
+
onRevert,
|
|
79
|
+
}: {
|
|
80
|
+
item: ChangeItem;
|
|
81
|
+
active: boolean;
|
|
82
|
+
onSelect: () => void;
|
|
83
|
+
onRevert: () => Promise<string | null>;
|
|
84
|
+
}) {
|
|
85
|
+
const t = useT();
|
|
86
|
+
const [busy, setBusy] = useState(false);
|
|
87
|
+
const [error, setError] = useState<string | null>(null);
|
|
88
|
+
const markupOnly = [...item.hunk.removed, ...item.hunk.added].every((line) => !readable(line));
|
|
89
|
+
return (
|
|
90
|
+
<li className="group relative">
|
|
91
|
+
<button
|
|
92
|
+
type="button"
|
|
93
|
+
aria-current={active || undefined}
|
|
94
|
+
onClick={onSelect}
|
|
95
|
+
className="flex w-full gap-2 rounded-md py-2 pr-9 pl-2 text-left transition-colors hover:bg-accent/60 focus-visible:outline-2 focus-visible:outline-foreground/60 aria-[current]:bg-accent"
|
|
96
|
+
>
|
|
97
|
+
<span
|
|
98
|
+
aria-hidden
|
|
99
|
+
className="w-0.5 flex-none self-stretch rounded-full"
|
|
100
|
+
style={{ background: markColor(item) }}
|
|
101
|
+
/>
|
|
102
|
+
<span className="flex min-w-0 flex-1 flex-col gap-1">
|
|
103
|
+
<Lines lines={item.hunk.removed} kind="removed" raw={markupOnly} />
|
|
104
|
+
<Lines lines={item.hunk.added} kind="added" raw={markupOnly} />
|
|
105
|
+
</span>
|
|
106
|
+
</button>
|
|
107
|
+
{item.file.status !== 'added' && (
|
|
108
|
+
<button
|
|
109
|
+
type="button"
|
|
110
|
+
aria-label={t('Revert this change')}
|
|
111
|
+
title={t('Revert this change')}
|
|
112
|
+
disabled={busy}
|
|
113
|
+
onClick={async () => {
|
|
114
|
+
setBusy(true);
|
|
115
|
+
setError(await onRevert());
|
|
116
|
+
setBusy(false);
|
|
117
|
+
}}
|
|
118
|
+
className="absolute top-1.5 right-1 flex size-8 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-foreground/60 disabled:opacity-50"
|
|
119
|
+
>
|
|
120
|
+
<Undo2 className="size-3.5" />
|
|
121
|
+
</button>
|
|
122
|
+
)}
|
|
123
|
+
{error && (
|
|
124
|
+
<p
|
|
125
|
+
role="alert"
|
|
126
|
+
className="px-3 pb-2 text-[11px]"
|
|
127
|
+
style={{ color: 'var(--change-removed)' }}
|
|
128
|
+
>
|
|
129
|
+
{t(error)}
|
|
130
|
+
</p>
|
|
131
|
+
)}
|
|
132
|
+
</li>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function Group({ title, children }: { title: ReactNode; children: ReactNode }) {
|
|
137
|
+
return (
|
|
138
|
+
<section className="border-border border-b px-2 py-2 last:border-b-0">
|
|
139
|
+
<h3 className="px-2 pb-1 font-mono text-[10px] text-muted-foreground uppercase tracking-wider">
|
|
140
|
+
{title}
|
|
141
|
+
</h3>
|
|
142
|
+
<ul className="flex flex-col">{children}</ul>
|
|
143
|
+
</section>
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function Empty({ icon, children }: { icon: ReactNode; children: ReactNode }) {
|
|
148
|
+
return (
|
|
149
|
+
<div className="flex flex-col items-center gap-2 px-6 py-12 text-center text-muted-foreground text-xs">
|
|
150
|
+
{icon}
|
|
151
|
+
{children}
|
|
152
|
+
</div>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export type ChangesPanelProps = {
|
|
157
|
+
state: ChangesState;
|
|
158
|
+
items: ChangeItem[];
|
|
159
|
+
placement: Map<string, number[]>;
|
|
160
|
+
since: string;
|
|
161
|
+
revisions: Revision[];
|
|
162
|
+
activeId: string | null;
|
|
163
|
+
onSince: (since: string) => void;
|
|
164
|
+
onSelect: (item: ChangeItem) => void;
|
|
165
|
+
onRevert: (item: ChangeItem) => Promise<string | null>;
|
|
166
|
+
onClose: () => void;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* What changed since a revision, in reading order, each change a way to its
|
|
171
|
+
* place on the page and a way back to how it was. The page carries the marks;
|
|
172
|
+
* this is their index.
|
|
173
|
+
*/
|
|
174
|
+
export function ChangesPanel({
|
|
175
|
+
state,
|
|
176
|
+
items,
|
|
177
|
+
placement,
|
|
178
|
+
since,
|
|
179
|
+
revisions,
|
|
180
|
+
activeId,
|
|
181
|
+
onSince,
|
|
182
|
+
onSelect,
|
|
183
|
+
onRevert,
|
|
184
|
+
onClose,
|
|
185
|
+
}: ChangesPanelProps) {
|
|
186
|
+
const t = useT();
|
|
187
|
+
const result = state.status === 'ready' ? state.result : null;
|
|
188
|
+
|
|
189
|
+
const onPage = items
|
|
190
|
+
.filter((item) => (placement.get(item.hunk.id) ?? []).length > 0)
|
|
191
|
+
.sort(
|
|
192
|
+
(a, b) =>
|
|
193
|
+
(placement.get(a.hunk.id)?.[0] ?? 0) - (placement.get(b.hunk.id)?.[0] ?? 0) ||
|
|
194
|
+
a.hunk.newStart - b.hunk.newStart,
|
|
195
|
+
);
|
|
196
|
+
const elsewhere = items.filter((item) => !onPage.includes(item));
|
|
197
|
+
const binary = result?.available ? result.files.filter((file) => file.binary) : [];
|
|
198
|
+
const pages = [...new Set(onPage.map((item) => placement.get(item.hunk.id)?.[0] ?? 0))];
|
|
199
|
+
|
|
200
|
+
const at = onPage.findIndex((item) => item.hunk.id === activeId);
|
|
201
|
+
const step = (by: number) => {
|
|
202
|
+
if (onPage.length === 0) return;
|
|
203
|
+
const next = onPage[(at + by + onPage.length) % onPage.length];
|
|
204
|
+
if (next) onSelect(next);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
<PanelShell
|
|
209
|
+
label={t('Changes')}
|
|
210
|
+
header={
|
|
211
|
+
<>
|
|
212
|
+
<GitCompare aria-hidden className="size-3.5 flex-none text-muted-foreground" />
|
|
213
|
+
<span className="truncate font-medium text-xs">{t('Changes')}</span>
|
|
214
|
+
{items.length > 0 && (
|
|
215
|
+
<span className="rounded bg-muted px-1.5 py-0.5 font-mono text-[10px] text-muted-foreground tabular-nums">
|
|
216
|
+
{items.length}
|
|
217
|
+
</span>
|
|
218
|
+
)}
|
|
219
|
+
</>
|
|
220
|
+
}
|
|
221
|
+
actions={
|
|
222
|
+
<>
|
|
223
|
+
{onPage.length > 1 && (
|
|
224
|
+
<>
|
|
225
|
+
<PanelIconButton label={t('Previous change')} onClick={() => step(-1)}>
|
|
226
|
+
<ChevronUp className="size-3.5" />
|
|
227
|
+
</PanelIconButton>
|
|
228
|
+
<PanelIconButton label={t('Next change')} onClick={() => step(at === -1 ? 0 : 1)}>
|
|
229
|
+
<ChevronDown className="size-3.5" />
|
|
230
|
+
</PanelIconButton>
|
|
231
|
+
</>
|
|
232
|
+
)}
|
|
233
|
+
<PanelIconButton label={t('Close')} onClick={onClose}>
|
|
234
|
+
<X className="size-3.5" />
|
|
235
|
+
</PanelIconButton>
|
|
236
|
+
</>
|
|
237
|
+
}
|
|
238
|
+
banner={
|
|
239
|
+
result?.available !== false && (
|
|
240
|
+
<div className="border-border border-b px-3 py-2">
|
|
241
|
+
<label className="relative flex h-8 items-center rounded border border-border focus-within:border-foreground/40">
|
|
242
|
+
<span className="pl-2 text-[11px] text-muted-foreground">{t('Since')}</span>
|
|
243
|
+
<select
|
|
244
|
+
aria-label={t('Compare with')}
|
|
245
|
+
value={since}
|
|
246
|
+
onChange={(e) => onSince(e.target.value)}
|
|
247
|
+
className="h-8 min-w-0 flex-1 cursor-pointer appearance-none truncate bg-transparent pr-7 pl-1.5 text-xs outline-none"
|
|
248
|
+
>
|
|
249
|
+
<option value="HEAD">{t('Last commit')}</option>
|
|
250
|
+
{revisions.map((revision) => (
|
|
251
|
+
<option key={revision.commit} value={revision.commit}>
|
|
252
|
+
{`${revision.short} · ${revision.subject} · ${revision.when}`}
|
|
253
|
+
</option>
|
|
254
|
+
))}
|
|
255
|
+
</select>
|
|
256
|
+
<ChevronDown
|
|
257
|
+
aria-hidden
|
|
258
|
+
className="pointer-events-none absolute right-2 size-3.5 text-muted-foreground"
|
|
259
|
+
/>
|
|
260
|
+
</label>
|
|
261
|
+
</div>
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
>
|
|
265
|
+
{state.status === 'loading' ? (
|
|
266
|
+
<div className="m-3 h-0.5 overflow-hidden rounded bg-muted">
|
|
267
|
+
<div className="h-full w-1/3 animate-pulse bg-foreground/20" />
|
|
268
|
+
</div>
|
|
269
|
+
) : state.status === 'error' ? (
|
|
270
|
+
<Empty icon={<GitCompare className="size-5" />}>{state.error}</Empty>
|
|
271
|
+
) : !state.result.available ? (
|
|
272
|
+
<Empty icon={<GitCompare className="size-5" />}>
|
|
273
|
+
{state.result.reason === 'no-git' ? (
|
|
274
|
+
t('git is not installed')
|
|
275
|
+
) : (
|
|
276
|
+
<>
|
|
277
|
+
{t('Not a git repository')}
|
|
278
|
+
<code className="rounded bg-muted px-1.5 py-0.5 font-mono text-[11px]">git init</code>
|
|
279
|
+
</>
|
|
280
|
+
)}
|
|
281
|
+
</Empty>
|
|
282
|
+
) : items.length === 0 && binary.length === 0 ? (
|
|
283
|
+
<Empty icon={<CircleCheck className="size-5" />}>{t('No changes')}</Empty>
|
|
284
|
+
) : (
|
|
285
|
+
<>
|
|
286
|
+
{pages.map((page) => (
|
|
287
|
+
<Group key={page} title={t('Page {page}', { page })}>
|
|
288
|
+
{onPage
|
|
289
|
+
.filter((item) => placement.get(item.hunk.id)?.[0] === page)
|
|
290
|
+
.map((item) => (
|
|
291
|
+
<Item
|
|
292
|
+
key={item.hunk.id}
|
|
293
|
+
item={item}
|
|
294
|
+
active={item.hunk.id === activeId}
|
|
295
|
+
onSelect={() => onSelect(item)}
|
|
296
|
+
onRevert={() => onRevert(item)}
|
|
297
|
+
/>
|
|
298
|
+
))}
|
|
299
|
+
</Group>
|
|
300
|
+
))}
|
|
301
|
+
{[...new Set(elsewhere.map((item) => item.file.path))].map((path) => (
|
|
302
|
+
<Group
|
|
303
|
+
key={path}
|
|
304
|
+
title={
|
|
305
|
+
<span className="flex items-center gap-1 normal-case tracking-normal">
|
|
306
|
+
<FileText aria-hidden className="size-3" />
|
|
307
|
+
{path}
|
|
308
|
+
</span>
|
|
309
|
+
}
|
|
310
|
+
>
|
|
311
|
+
{elsewhere
|
|
312
|
+
.filter((item) => item.file.path === path)
|
|
313
|
+
.map((item) => (
|
|
314
|
+
<Item
|
|
315
|
+
key={item.hunk.id}
|
|
316
|
+
item={item}
|
|
317
|
+
active={item.hunk.id === activeId}
|
|
318
|
+
onSelect={() => onSelect(item)}
|
|
319
|
+
onRevert={() => onRevert(item)}
|
|
320
|
+
/>
|
|
321
|
+
))}
|
|
322
|
+
</Group>
|
|
323
|
+
))}
|
|
324
|
+
{binary.length > 0 && (
|
|
325
|
+
<Group title={t('Files')}>
|
|
326
|
+
{binary.map((file) => (
|
|
327
|
+
<li
|
|
328
|
+
key={file.path}
|
|
329
|
+
className="flex items-center gap-2 px-2 py-1.5 font-mono text-[11px] text-muted-foreground"
|
|
330
|
+
>
|
|
331
|
+
<FileText aria-hidden className="size-3" />
|
|
332
|
+
{file.path}
|
|
333
|
+
</li>
|
|
334
|
+
))}
|
|
335
|
+
</Group>
|
|
336
|
+
)}
|
|
337
|
+
</>
|
|
338
|
+
)}
|
|
339
|
+
</PanelShell>
|
|
340
|
+
);
|
|
341
|
+
}
|