@kabmo-labs/kabmo 0.29.0 → 0.30.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/client/index.html +1 -1
- package/dist/client/index.js +3 -3
- package/dist/client/static/js/{index-43z47ngg.js → index-5vjbp04s.js} +590 -590
- package/package.json +1 -1
- package/src/client/glass-palette.test.ts +72 -0
- package/src/client/glass.test.ts +1 -77
- package/src/client/hooks/editor/use-dirty-tracking.test.ts +73 -0
- package/src/client/hooks/editor/use-dirty-tracking.ts +96 -0
- package/src/client/hooks/editor/use-file-cache.ts +1 -0
- package/src/client/hooks/editor/use-file-editor.ts +15 -60
- package/src/server/lib/fs/archive.test.ts +61 -2
- package/src/server/lib/fs/archive.ts +28 -9
- package/src/shared/lib/icons/tint-hues.generated.ts +14 -13
- package/src/test-support/file-editor.test-util.ts +3 -2
- package/src/test-support/glass-css.ts +30 -0
package/package.json
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, test } from 'bun:test';
|
|
7
|
+
import { THEMES } from '@/shared/lib/theme/catalog';
|
|
8
|
+
import { mixHex } from '@/shared/lib/theme/color';
|
|
9
|
+
import { contrastRatio } from '@/shared/lib/theme/legibility';
|
|
10
|
+
import { readClientCss as read, ruleBody } from '@/test-support/glass-css';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The material has to hold on EVERY palette in the catalog, not on the eighteen
|
|
14
|
+
* it was tuned against.
|
|
15
|
+
*
|
|
16
|
+
* The material is DERIVED from the palette — `--glass-panel` is
|
|
17
|
+
* `color-mix(paper N%, transparent)` and the chrome is one more mix over it — so
|
|
18
|
+
* a theme does not "support glass" by declaring something; it supports it when
|
|
19
|
+
* its two surfaces and its ink survive the composite. That makes a bad port
|
|
20
|
+
* invisible in the classic look and illegible under the material, which is the
|
|
21
|
+
* failure this sweep exists to catch.
|
|
22
|
+
*
|
|
23
|
+
* The alphas are read out of `glass.css` rather than repeated here, so the test
|
|
24
|
+
* cannot drift from the shipped values, and the composite is the browser's own:
|
|
25
|
+
* plane = paper over canvas, chrome/card = paper over plane. The invariant a new
|
|
26
|
+
* palette breaks without anyone noticing is body text keeping the 4.5:1 floor on
|
|
27
|
+
* the chrome and on a card. Measured across all 63 palettes, the worst is
|
|
28
|
+
* `solarized-light` at 4.89:1 — the same number its opaque paper has, which is
|
|
29
|
+
* what "the tint costs no contrast" means.
|
|
30
|
+
*/
|
|
31
|
+
describe('glass material on every catalog palette', () => {
|
|
32
|
+
/** A percentage out of a token, whether bare (`50%`) or inside a `color-mix`. */
|
|
33
|
+
const percentOf = (body: string, token: string): number => {
|
|
34
|
+
const match = new RegExp(`--glass-${token}:\\s*(?:color-mix\\(in srgb, var\\(--theme-paper\\)\\s*)?(\\d+(?:\\.\\d+)?)%`).exec(body);
|
|
35
|
+
if (!match) throw new Error(`--glass-${token} is not a percentage in this block`);
|
|
36
|
+
return Number(match[1]) / 100;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
test('body text keeps the 4.5:1 floor on the plane, the chrome and a card', async () => {
|
|
40
|
+
const glass = await read('./glass.css');
|
|
41
|
+
const surface = ruleBody(glass, '[data-surface="glass"]');
|
|
42
|
+
const surfaceDark = ruleBody(glass, '[data-surface="glass"][data-theme-variant="dark"]');
|
|
43
|
+
const root = ruleBody(glass, ':root');
|
|
44
|
+
const variantDark = ruleBody(glass, '[data-theme-variant="dark"]');
|
|
45
|
+
|
|
46
|
+
// The card's fill is surface-INDEPENDENT (the composer's `glass` style is
|
|
47
|
+
// offered in the classic look too), so it lives on `:root` and the variant
|
|
48
|
+
// block, not under the surface attribute.
|
|
49
|
+
const alphas = {
|
|
50
|
+
light: { tint: percentOf(surface, 'tint'), chrome: percentOf(surface, 'chrome'), card: percentOf(root, 'card') },
|
|
51
|
+
dark: { tint: percentOf(surfaceDark, 'tint'), chrome: percentOf(surfaceDark, 'chrome'), card: percentOf(variantDark, 'card') },
|
|
52
|
+
};
|
|
53
|
+
expect(alphas.light.tint).toBeGreaterThan(alphas.dark.tint - 1);
|
|
54
|
+
expect(alphas.light.chrome).toBeLessThan(alphas.dark.chrome + 0.5);
|
|
55
|
+
|
|
56
|
+
const failures: string[] = [];
|
|
57
|
+
for (const theme of THEMES) {
|
|
58
|
+
const alpha = alphas[theme.variant];
|
|
59
|
+
const plane = mixHex(theme.canvas, theme.paper, alpha.tint);
|
|
60
|
+
const tiers = {
|
|
61
|
+
plane,
|
|
62
|
+
chrome: mixHex(plane, theme.paper, alpha.chrome),
|
|
63
|
+
card: mixHex(plane, theme.paper, alpha.card),
|
|
64
|
+
};
|
|
65
|
+
for (const [tier, fill] of Object.entries(tiers)) {
|
|
66
|
+
const ratio = contrastRatio(theme.ink, fill);
|
|
67
|
+
if (ratio < 4.5) failures.push(`${theme.id} ${tier} ${ratio.toFixed(2)}:1`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
expect(failures).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
});
|
package/src/client/glass.test.ts
CHANGED
|
@@ -4,9 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { describe, expect, test } from 'bun:test';
|
|
7
|
-
import {
|
|
8
|
-
import { mixHex } from '@/shared/lib/theme/color';
|
|
9
|
-
import { contrastRatio } from '@/shared/lib/theme/legibility';
|
|
7
|
+
import { readClientCss as read, ruleBody } from '@/test-support/glass-css';
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
10
|
* The composer's `glass` STYLE is not a surface style: it is offered in the
|
|
@@ -21,18 +19,6 @@ import { contrastRatio } from '@/shared/lib/theme/legibility';
|
|
|
21
19
|
* the composer rule READS must be declared where the rule can see it — `:root`
|
|
22
20
|
* or a variant block, never only under the surface attribute.
|
|
23
21
|
*/
|
|
24
|
-
const read = (file: string): Promise<string> => Bun.file(new URL(file, import.meta.url)).text();
|
|
25
|
-
|
|
26
|
-
/** The body of the first rule whose selector starts a line, e.g. `[x] {`. */
|
|
27
|
-
function ruleBody(css: string, selector: string): string {
|
|
28
|
-
const start = css.search(new RegExp(`^${selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')} \\{`, 'm'));
|
|
29
|
-
if (start < 0) throw new Error(`no rule for ${selector}`);
|
|
30
|
-
const open = css.indexOf('{', start);
|
|
31
|
-
const close = css.indexOf('\n}', open);
|
|
32
|
-
if (close < 0) throw new Error(`unterminated rule for ${selector}`);
|
|
33
|
-
return css.slice(open + 1, close);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
22
|
describe('glass composer material', () => {
|
|
37
23
|
test('every --glass token the composer reads is declared in :root', async () => {
|
|
38
24
|
const [glass, tailwind] = await Promise.all([read('./glass.css'), read('./tailwind.css')]);
|
|
@@ -323,65 +309,3 @@ describe('glass backdrop cost', () => {
|
|
|
323
309
|
}
|
|
324
310
|
});
|
|
325
311
|
});
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* The material has to hold on EVERY palette in the catalog, not on the eighteen
|
|
329
|
-
* it was tuned against.
|
|
330
|
-
*
|
|
331
|
-
* The material is DERIVED from the palette — `--glass-panel` is
|
|
332
|
-
* `color-mix(paper N%, transparent)` and the chrome is one more mix over it — so
|
|
333
|
-
* a theme does not "support glass" by declaring something; it supports it when
|
|
334
|
-
* its two surfaces and its ink survive the composite. That makes a bad port
|
|
335
|
-
* invisible in the classic look and illegible under the material, which is the
|
|
336
|
-
* failure this sweep exists to catch.
|
|
337
|
-
*
|
|
338
|
-
* The alphas are read out of `glass.css` rather than repeated here, so the test
|
|
339
|
-
* cannot drift from the shipped values, and the composite is the browser's own:
|
|
340
|
-
* plane = paper over canvas, chrome/card = paper over plane. The invariant a new
|
|
341
|
-
* palette breaks without anyone noticing is body text keeping the 4.5:1 floor on
|
|
342
|
-
* the chrome and on a card. Measured across all 63 palettes, the worst is
|
|
343
|
-
* `solarized-light` at 4.89:1 — the same number its opaque paper has, which is
|
|
344
|
-
* what "the tint costs no contrast" means.
|
|
345
|
-
*/
|
|
346
|
-
describe('glass material on every catalog palette', () => {
|
|
347
|
-
/** A percentage out of a token, whether bare (`50%`) or inside a `color-mix`. */
|
|
348
|
-
const percentOf = (body: string, token: string): number => {
|
|
349
|
-
const match = new RegExp(`--glass-${token}:\\s*(?:color-mix\\(in srgb, var\\(--theme-paper\\)\\s*)?(\\d+(?:\\.\\d+)?)%`).exec(body);
|
|
350
|
-
if (!match) throw new Error(`--glass-${token} is not a percentage in this block`);
|
|
351
|
-
return Number(match[1]) / 100;
|
|
352
|
-
};
|
|
353
|
-
|
|
354
|
-
test('body text keeps the 4.5:1 floor on the plane, the chrome and a card', async () => {
|
|
355
|
-
const glass = await read('./glass.css');
|
|
356
|
-
const surface = ruleBody(glass, '[data-surface="glass"]');
|
|
357
|
-
const surfaceDark = ruleBody(glass, '[data-surface="glass"][data-theme-variant="dark"]');
|
|
358
|
-
const root = ruleBody(glass, ':root');
|
|
359
|
-
const variantDark = ruleBody(glass, '[data-theme-variant="dark"]');
|
|
360
|
-
|
|
361
|
-
// The card's fill is surface-INDEPENDENT (the composer's `glass` style is
|
|
362
|
-
// offered in the classic look too), so it lives on `:root` and the variant
|
|
363
|
-
// block, not under the surface attribute.
|
|
364
|
-
const alphas = {
|
|
365
|
-
light: { tint: percentOf(surface, 'tint'), chrome: percentOf(surface, 'chrome'), card: percentOf(root, 'card') },
|
|
366
|
-
dark: { tint: percentOf(surfaceDark, 'tint'), chrome: percentOf(surfaceDark, 'chrome'), card: percentOf(variantDark, 'card') },
|
|
367
|
-
};
|
|
368
|
-
expect(alphas.light.tint).toBeGreaterThan(alphas.dark.tint - 1);
|
|
369
|
-
expect(alphas.light.chrome).toBeLessThan(alphas.dark.chrome + 0.5);
|
|
370
|
-
|
|
371
|
-
const failures: string[] = [];
|
|
372
|
-
for (const theme of THEMES) {
|
|
373
|
-
const alpha = alphas[theme.variant];
|
|
374
|
-
const plane = mixHex(theme.canvas, theme.paper, alpha.tint);
|
|
375
|
-
const tiers = {
|
|
376
|
-
plane,
|
|
377
|
-
chrome: mixHex(plane, theme.paper, alpha.chrome),
|
|
378
|
-
card: mixHex(plane, theme.paper, alpha.card),
|
|
379
|
-
};
|
|
380
|
-
for (const [tier, fill] of Object.entries(tiers)) {
|
|
381
|
-
const ratio = contrastRatio(theme.ink, fill);
|
|
382
|
-
if (ratio < 4.5) failures.push(`${theme.id} ${tier} ${ratio.toFixed(2)}:1`);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
expect(failures).toEqual([]);
|
|
386
|
-
});
|
|
387
|
-
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The dirty set is the tab strip's and the close guard's one answer, and it has
|
|
8
|
+
* two sources: a text buffer that differs from its baseline, and a BINARY
|
|
9
|
+
* document (a workbook) whose edits live in the viewer's own draft store — for
|
|
10
|
+
* which all this side keeps is a flag.
|
|
11
|
+
*
|
|
12
|
+
* Driven through `useFileEditor`, because that is the only caller: the hook is
|
|
13
|
+
* an extraction of its state, and a test that mounted it alone would pin the
|
|
14
|
+
* wiring rather than the answer the tab strip reads.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { describe, expect, test } from 'bun:test';
|
|
18
|
+
import { TARGET, createEditorHarness, freshState, stubFetch } from '@/test-support/file-editor.test-util';
|
|
19
|
+
|
|
20
|
+
const editor = createEditorHarness();
|
|
21
|
+
|
|
22
|
+
describe('useDirtyTracking', () => {
|
|
23
|
+
test('a binary document marks its own tab dirty, and its save clears the flag', async () => {
|
|
24
|
+
const state = freshState();
|
|
25
|
+
stubFetch(state);
|
|
26
|
+
await editor.mount();
|
|
27
|
+
expect(editor.mounted?.isDirty).toBe(false);
|
|
28
|
+
|
|
29
|
+
await editor.run(() => editor.mounted?.setBinaryDirty(true));
|
|
30
|
+
expect(editor.mounted?.isDirty).toBe(true);
|
|
31
|
+
expect([...(editor.mounted?.dirtyKeys ?? [])]).toEqual([String(TARGET.id)]);
|
|
32
|
+
|
|
33
|
+
await editor.run(() => editor.mounted?.setBinaryDirty(false));
|
|
34
|
+
expect(editor.mounted?.isDirty).toBe(false);
|
|
35
|
+
expect([...(editor.mounted?.dirtyKeys ?? [])]).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// An INLINE target (an attachment chip) deliberately, because it never reads:
|
|
39
|
+
// a tab closed on a read file leaves its baseline behind on purpose, and that
|
|
40
|
+
// baseline alone would read as dirty and hide the flag this test is about.
|
|
41
|
+
test('retain drops the flag of a closed tab', async () => {
|
|
42
|
+
const state = freshState();
|
|
43
|
+
stubFetch(state);
|
|
44
|
+
await editor.mount({ id: 9, name: 'book.xlsx', path: 'book.xlsx', content: 'x' });
|
|
45
|
+
await editor.run(() => editor.mounted?.setBinaryDirty(true));
|
|
46
|
+
expect(editor.mounted?.isDirty).toBe(true);
|
|
47
|
+
|
|
48
|
+
// A closed tab's flag would otherwise keep the close guard armed for a file
|
|
49
|
+
// that is not on screen.
|
|
50
|
+
await editor.run(() => editor.mounted?.retain([]));
|
|
51
|
+
expect(editor.mounted?.isDirty).toBe(false);
|
|
52
|
+
expect([...(editor.mounted?.dirtyKeys ?? [])]).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('a text edit and a workbook flag in one tab are one answer', async () => {
|
|
56
|
+
const state = freshState();
|
|
57
|
+
stubFetch(state);
|
|
58
|
+
await editor.mount();
|
|
59
|
+
|
|
60
|
+
await editor.run(() => editor.mounted?.setBinaryDirty(true));
|
|
61
|
+
editor.mounted?.onChange('edited by hand');
|
|
62
|
+
await editor.settle();
|
|
63
|
+
expect([...(editor.mounted?.dirtyKeys ?? [])]).toEqual([String(TARGET.id)]);
|
|
64
|
+
|
|
65
|
+
// Saving the text leaves the workbook's own draft unsaved, so the tab stays
|
|
66
|
+
// dirty until the viewer clears its flag.
|
|
67
|
+
editor.mounted?.saveNow();
|
|
68
|
+
await editor.settle();
|
|
69
|
+
expect(editor.mounted?.isDirty).toBe(true);
|
|
70
|
+
await editor.run(() => editor.mounted?.setBinaryDirty(false));
|
|
71
|
+
expect(editor.mounted?.isDirty).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Which open files have unsaved edits.
|
|
8
|
+
*
|
|
9
|
+
* Two sources, one answer: the TEXT buffers (a buffer that differs from the
|
|
10
|
+
* baseline it was read at) and the BINARY documents (a workbook whose draft
|
|
11
|
+
* lives in the viewer's own store, so all this side keeps is a flag). The dirty
|
|
12
|
+
* set is DERIVED from those two rather than tracked separately — a second copy
|
|
13
|
+
* would be a second thing to keep in step, and the comparison is a string
|
|
14
|
+
* identity check per open buffer.
|
|
15
|
+
*
|
|
16
|
+
* Split out of `use-file-editor.ts` so that file stays under the repo's
|
|
17
|
+
* per-file ceiling; the text comparison stays with the caller because it owns
|
|
18
|
+
* the buffer and its line-ending normalization.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { useCallback, useMemo, useState } from 'preact/hooks';
|
|
22
|
+
import { editorTargetKey as targetKey } from '@/client/hooks/editor/target-key';
|
|
23
|
+
import type { FileEditorTarget } from '@/client/hooks/editor/file-editor-types';
|
|
24
|
+
|
|
25
|
+
interface Options {
|
|
26
|
+
target: FileEditorTarget | null;
|
|
27
|
+
/** The active file's cache key, `''` when nothing is open. */
|
|
28
|
+
key: string;
|
|
29
|
+
/** Whether the active TEXT buffer differs from its baseline. */
|
|
30
|
+
textDirty: boolean;
|
|
31
|
+
contents: Record<string, string>;
|
|
32
|
+
baselines: Record<string, string>;
|
|
33
|
+
/** Drops the caches of tabs that are no longer open — see `useFileCache`. */
|
|
34
|
+
retain: (ids: Array<number | string>) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function useDirtyTracking({ target, key, textDirty, contents, baselines, retain }: Options) {
|
|
38
|
+
/**
|
|
39
|
+
* Files whose BINARY document (a workbook) holds unsaved edits.
|
|
40
|
+
*
|
|
41
|
+
* Kept beside the text caches rather than inside them because the value is a
|
|
42
|
+
* flag, not a buffer: the edited workbook itself lives in the viewer's draft
|
|
43
|
+
* store, and this set is what the tab strip and the close guard read.
|
|
44
|
+
*/
|
|
45
|
+
const [binaryDirtyKeys, setBinaryDirtyKeys] = useState<ReadonlySet<string>>(() => new Set());
|
|
46
|
+
|
|
47
|
+
const isDirty = textDirty || binaryDirtyKeys.has(key);
|
|
48
|
+
|
|
49
|
+
const dirtyKeys = useMemo(() => {
|
|
50
|
+
const next = new Set<string>(binaryDirtyKeys);
|
|
51
|
+
for (const [bufferKey, text] of Object.entries(contents)) {
|
|
52
|
+
const baseline = baselines[bufferKey];
|
|
53
|
+
if (baseline !== undefined && text !== baseline) next.add(bufferKey);
|
|
54
|
+
}
|
|
55
|
+
return next;
|
|
56
|
+
}, [contents, baselines, binaryDirtyKeys]);
|
|
57
|
+
|
|
58
|
+
const setBinaryDirty = useCallback(
|
|
59
|
+
(dirty: boolean) => {
|
|
60
|
+
if (!target) return;
|
|
61
|
+
const fileKey = targetKey(target);
|
|
62
|
+
setBinaryDirtyKeys((previous) => {
|
|
63
|
+
if (previous.has(fileKey) === dirty) return previous;
|
|
64
|
+
const next = new Set(previous);
|
|
65
|
+
if (dirty) next.add(fileKey);
|
|
66
|
+
else next.delete(fileKey);
|
|
67
|
+
return next;
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
[target],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* `retain` also drops the binary flags of tabs that are no longer open: a
|
|
75
|
+
* closed tab's flag would otherwise keep the close guard armed for a file
|
|
76
|
+
* that is not on screen.
|
|
77
|
+
*/
|
|
78
|
+
const retainAll = useCallback(
|
|
79
|
+
(ids: Array<number | string>) => {
|
|
80
|
+
retain(ids);
|
|
81
|
+
const keep = new Set(ids.map(String));
|
|
82
|
+
setBinaryDirtyKeys((previous) => {
|
|
83
|
+
let changed = false;
|
|
84
|
+
const next = new Set<string>();
|
|
85
|
+
for (const fileKey of previous) {
|
|
86
|
+
if (keep.has(fileKey)) next.add(fileKey);
|
|
87
|
+
else changed = true;
|
|
88
|
+
}
|
|
89
|
+
return changed ? next : previous;
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
[retain],
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return { isDirty, dirtyKeys, setBinaryDirty, retain: retainAll };
|
|
96
|
+
}
|
|
@@ -92,6 +92,7 @@ export function useFileCache({
|
|
|
92
92
|
if (!keep.has(loaded)) loadedRef.current.delete(loaded);
|
|
93
93
|
}
|
|
94
94
|
setContents((previous) => retainKeys(previous, keep));
|
|
95
|
+
setBaselines((previous) => retainKeys(previous, keep));
|
|
95
96
|
setLineEndings((previous) => retainKeys(previous, keep));
|
|
96
97
|
setMtimes((previous) => retainKeys(previous, keep));
|
|
97
98
|
setHashes((previous) => retainKeys(previous, keep));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useEffect,
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
|
2
2
|
import { getLanguageFromPath } from '@/shared/lib/code/language';
|
|
3
3
|
import { resolveViewer } from '@/client/hooks/editor/resolve-viewer';
|
|
4
4
|
import { toDiskText, toLf, type LineEnding } from '@/shared/lib/code/line-endings';
|
|
@@ -10,6 +10,7 @@ import { useFileWrites } from '@/client/hooks/editor/use-file-writes';
|
|
|
10
10
|
import { useWriteConflict } from '@/client/hooks/editor/use-write-conflict';
|
|
11
11
|
import { useDiskSync } from '@/client/hooks/editor/use-disk-sync';
|
|
12
12
|
import { useFileCache } from '@/client/hooks/editor/use-file-cache';
|
|
13
|
+
import { useDirtyTracking } from '@/client/hooks/editor/use-dirty-tracking';
|
|
13
14
|
import { editorTargetKey as targetKey } from '@/client/hooks/editor/target-key';
|
|
14
15
|
import type { FileEditorSaveStatus, FileEditorTarget, UseFileEditorOptions, UseFileEditorResult } from '@/client/hooks/editor/file-editor-types';
|
|
15
16
|
|
|
@@ -61,14 +62,6 @@ export function useFileEditor(
|
|
|
61
62
|
*/
|
|
62
63
|
const [hashes, setHashes] = useState<Record<string, string | null>>({});
|
|
63
64
|
const [saveStatus, setSaveStatus] = useState<FileEditorSaveStatus>('idle');
|
|
64
|
-
/**
|
|
65
|
-
* Files whose BINARY document (a workbook) holds unsaved edits.
|
|
66
|
-
*
|
|
67
|
-
* Kept beside the text caches rather than inside them because the value is a
|
|
68
|
-
* flag, not a buffer: the edited workbook itself lives in the viewer's draft
|
|
69
|
-
* store, and this set is what the tab strip and the close guard read.
|
|
70
|
-
*/
|
|
71
|
-
const [binaryDirtyKeys, setBinaryDirtyKeys] = useState<ReadonlySet<string>>(() => new Set());
|
|
72
65
|
/** Bumped by `reset()` to re-run the read effect for the active file. */
|
|
73
66
|
const [revision, setRevision] = useState(0);
|
|
74
67
|
|
|
@@ -77,20 +70,6 @@ export function useFileEditor(
|
|
|
77
70
|
// feeds is LF-only — normalizing here keeps `isDirty` comparing like with like.
|
|
78
71
|
const content = target ? (contents[key] ?? (target.content !== undefined ? toLf(target.content) : '')) : '';
|
|
79
72
|
const textDirty = content !== (baselines[key] ?? content);
|
|
80
|
-
const isDirty = textDirty || binaryDirtyKeys.has(key);
|
|
81
|
-
/**
|
|
82
|
-
* The dirty set, derived from the two caches rather than tracked separately:
|
|
83
|
-
* a second copy would be a second thing to keep in step, and the comparison is
|
|
84
|
-
* a string identity check per open buffer.
|
|
85
|
-
*/
|
|
86
|
-
const dirtyKeys = useMemo(() => {
|
|
87
|
-
const next = new Set<string>(binaryDirtyKeys);
|
|
88
|
-
for (const [bufferKey, text] of Object.entries(contents)) {
|
|
89
|
-
const baseline = baselines[bufferKey];
|
|
90
|
-
if (baseline !== undefined && text !== baseline) next.add(bufferKey);
|
|
91
|
-
}
|
|
92
|
-
return next;
|
|
93
|
-
}, [contents, baselines, binaryDirtyKeys]);
|
|
94
73
|
|
|
95
74
|
// The viewer, its bytes and the image URL — see `resolve-viewer.ts` for the
|
|
96
75
|
// three rules this derivation encodes (the kind comes from the path; a
|
|
@@ -136,6 +115,19 @@ export function useFileEditor(
|
|
|
136
115
|
setHashes,
|
|
137
116
|
});
|
|
138
117
|
|
|
118
|
+
/**
|
|
119
|
+
* The dirty set (text buffers plus binary flags) lives in its own hook — see
|
|
120
|
+
* `use-dirty-tracking.ts` for why it is derived rather than tracked.
|
|
121
|
+
*/
|
|
122
|
+
const { isDirty, dirtyKeys, setBinaryDirty, retain: retainAll } = useDirtyTracking({
|
|
123
|
+
target,
|
|
124
|
+
key,
|
|
125
|
+
textDirty,
|
|
126
|
+
contents,
|
|
127
|
+
baselines,
|
|
128
|
+
retain,
|
|
129
|
+
});
|
|
130
|
+
|
|
139
131
|
/**
|
|
140
132
|
* The read path lives in its own hook — see `use-file-read.ts` for the two
|
|
141
133
|
* flags it reports and why the kind refusal is separate from a failed read.
|
|
@@ -274,43 +266,6 @@ export function useFileEditor(
|
|
|
274
266
|
setSaveStatus('idle');
|
|
275
267
|
}, [key]);
|
|
276
268
|
|
|
277
|
-
const setBinaryDirty = useCallback(
|
|
278
|
-
(dirty: boolean) => {
|
|
279
|
-
if (!target) return;
|
|
280
|
-
const fileKey = targetKey(target);
|
|
281
|
-
setBinaryDirtyKeys((previous) => {
|
|
282
|
-
if (previous.has(fileKey) === dirty) return previous;
|
|
283
|
-
const next = new Set(previous);
|
|
284
|
-
if (dirty) next.add(fileKey);
|
|
285
|
-
else next.delete(fileKey);
|
|
286
|
-
return next;
|
|
287
|
-
});
|
|
288
|
-
},
|
|
289
|
-
[target],
|
|
290
|
-
);
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* `retain` also drops the binary flags of tabs that are no longer open: a
|
|
294
|
-
* closed tab's flag would otherwise keep the close guard armed for a file
|
|
295
|
-
* that is not on screen.
|
|
296
|
-
*/
|
|
297
|
-
const retainAll = useCallback(
|
|
298
|
-
(ids: Array<number | string>) => {
|
|
299
|
-
retain(ids);
|
|
300
|
-
const keep = new Set(ids.map(String));
|
|
301
|
-
setBinaryDirtyKeys((previous) => {
|
|
302
|
-
let changed = false;
|
|
303
|
-
const next = new Set<string>();
|
|
304
|
-
for (const fileKey of previous) {
|
|
305
|
-
if (keep.has(fileKey)) next.add(fileKey);
|
|
306
|
-
else changed = true;
|
|
307
|
-
}
|
|
308
|
-
return changed ? next : previous;
|
|
309
|
-
});
|
|
310
|
-
},
|
|
311
|
-
[retain],
|
|
312
|
-
);
|
|
313
|
-
|
|
314
269
|
const onChange = useCallback(
|
|
315
270
|
(next: string) => {
|
|
316
271
|
if (!target || isBinaryViewer) return;
|
|
@@ -18,6 +18,7 @@ import os from 'node:os';
|
|
|
18
18
|
import path from 'node:path';
|
|
19
19
|
import { gzipSync } from 'node:zlib';
|
|
20
20
|
import { compressEntries, compressFormats, extractArchive, findTool } from '@/server/lib/fs/archive';
|
|
21
|
+
import { runShell } from '@/server/lib/fs/shell';
|
|
21
22
|
|
|
22
23
|
let dir: string;
|
|
23
24
|
|
|
@@ -39,6 +40,8 @@ function write(rel: string, content: string): string {
|
|
|
39
40
|
const hasZip = Bun.which('zip') !== null && Bun.which('unzip') !== null;
|
|
40
41
|
const hasTar = Bun.which('tar') !== null;
|
|
41
42
|
const hasGzip = Bun.which('gzip') !== null;
|
|
43
|
+
const hasBzip2 = Bun.which('bzip2') !== null;
|
|
44
|
+
const hasXz = Bun.which('xz') !== null;
|
|
42
45
|
|
|
43
46
|
describe('compressFormats', () => {
|
|
44
47
|
test('names the tool each format needs and whether it is installed', () => {
|
|
@@ -84,11 +87,11 @@ describe('compressEntries / extractArchive', () => {
|
|
|
84
87
|
format: 'tar.gz',
|
|
85
88
|
sources: [path.join(dir, 'src')],
|
|
86
89
|
});
|
|
87
|
-
expect(compressed.ok).toBe(true);
|
|
90
|
+
expect(compressed.ok, compressed.ok ? '' : compressed.error).toBe(true);
|
|
88
91
|
|
|
89
92
|
const out = path.join(dir, 'unpacked');
|
|
90
93
|
const extracted = await extractArchive({ archivePath: path.join(dir, 'src.tar.gz'), destinationDir: out });
|
|
91
|
-
expect(extracted.ok).toBe(true);
|
|
94
|
+
expect(extracted.ok, extracted.ok ? '' : extracted.error).toBe(true);
|
|
92
95
|
expect(fs.readFileSync(path.join(out, 'src/a.ts'), 'utf8')).toBe('A');
|
|
93
96
|
});
|
|
94
97
|
|
|
@@ -130,6 +133,62 @@ describe('compressEntries / extractArchive', () => {
|
|
|
130
133
|
if (!outcome.ok) expect(outcome.error).toContain('contains every selected item');
|
|
131
134
|
});
|
|
132
135
|
|
|
136
|
+
// The refusal is a property of the REQUEST, so it must not depend on which
|
|
137
|
+
// tools this machine happens to have: the Windows gate has no `zip`, and both
|
|
138
|
+
// refusal cases above answered "zip is not installed" there instead of the
|
|
139
|
+
// 409/400 they assert. `rar` is the format least likely to be installed, so
|
|
140
|
+
// this case fails on the ordering wherever it runs.
|
|
141
|
+
test('an invalid request is refused before the tool is looked up', async () => {
|
|
142
|
+
const source = write('src/a.ts', 'A');
|
|
143
|
+
write('src/bundle.rar', 'not really a rar');
|
|
144
|
+
|
|
145
|
+
const outcome = await compressEntries({
|
|
146
|
+
destinationDir: path.join(dir, 'src'),
|
|
147
|
+
name: 'bundle.rar',
|
|
148
|
+
format: 'rar',
|
|
149
|
+
sources: [source],
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
expect(outcome.ok).toBe(false);
|
|
153
|
+
if (!outcome.ok) {
|
|
154
|
+
expect(outcome.status).toBe(409);
|
|
155
|
+
expect(outcome.error).toContain('already exists');
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// The birth of the regression this pins: `tar -xf -` opened a `.tar.gz` that
|
|
160
|
+
// `tar -xzf -` opened, on one machine, and refused it on another with "Archive
|
|
161
|
+
// is compressed. Use -z option". A compressed stream cannot be sniffed the way
|
|
162
|
+
// a named file can, so the decompressor is named per kind — and each kind is
|
|
163
|
+
// checked against the tool that produces it rather than assumed.
|
|
164
|
+
test.skipIf(!hasTar || !hasBzip2)('a .tar.bz2 names its decompressor', async () => {
|
|
165
|
+
write('src/a.ts', 'A');
|
|
166
|
+
const archive = path.join(dir, 'src.tar.bz2');
|
|
167
|
+
// Relative names with the temp dir as the cwd: GNU tar reads a `C:\…`
|
|
168
|
+
// argument as a REMOTE `host:path` and dies with "Cannot connect to C:",
|
|
169
|
+
// which is the same trap `extractArchive` avoids by feeding the archive on
|
|
170
|
+
// stdin. Measured on the Windows gate.
|
|
171
|
+
const packed = await runShell('tar -cjf src.tar.bz2 ./src', { cwd: dir });
|
|
172
|
+
expect(packed.exitCode).toBe(0);
|
|
173
|
+
|
|
174
|
+
const out = path.join(dir, 'unpacked-bz2');
|
|
175
|
+
const extracted = await extractArchive({ archivePath: archive, destinationDir: out });
|
|
176
|
+
expect(extracted.ok, extracted.ok ? '' : extracted.error).toBe(true);
|
|
177
|
+
expect(fs.readFileSync(path.join(out, 'src/a.ts'), 'utf8')).toBe('A');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test.skipIf(!hasTar || !hasXz)('a .tar.xz names its decompressor', async () => {
|
|
181
|
+
write('src/a.ts', 'A');
|
|
182
|
+
const archive = path.join(dir, 'src.tar.xz');
|
|
183
|
+
const packed = await runShell('tar -cJf src.tar.xz ./src', { cwd: dir });
|
|
184
|
+
expect(packed.exitCode).toBe(0);
|
|
185
|
+
|
|
186
|
+
const out = path.join(dir, 'unpacked-xz');
|
|
187
|
+
const extracted = await extractArchive({ archivePath: archive, destinationDir: out });
|
|
188
|
+
expect(extracted.ok, extracted.ok ? '' : extracted.error).toBe(true);
|
|
189
|
+
expect(fs.readFileSync(path.join(out, 'src/a.ts'), 'utf8')).toBe('A');
|
|
190
|
+
});
|
|
191
|
+
|
|
133
192
|
test('refuses a file that is not an archive', async () => {
|
|
134
193
|
const archive = write('notes.txt', 'hello');
|
|
135
194
|
const outcome = await extractArchive({ archivePath: archive, destinationDir: path.join(dir, 'out') });
|
|
@@ -128,14 +128,6 @@ export type ArchiveOutcome = { ok: true; path: string } | { ok: false; error: st
|
|
|
128
128
|
*/
|
|
129
129
|
export async function compressEntries(options: CompressOptions): Promise<ArchiveOutcome> {
|
|
130
130
|
const { destinationDir, name, format, sources } = options;
|
|
131
|
-
const binary = compressToolFor(format);
|
|
132
|
-
if (!binary) {
|
|
133
|
-
return {
|
|
134
|
-
ok: false,
|
|
135
|
-
status: 501,
|
|
136
|
-
error: `${toolLabelFor(format)} is not installed on this machine`,
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
131
|
if (sources.length === 0) return { ok: false, status: 400, error: 'Nothing to compress' };
|
|
140
132
|
|
|
141
133
|
const destination = path.join(destinationDir, name);
|
|
@@ -156,6 +148,19 @@ export async function compressEntries(options: CompressOptions): Promise<Archive
|
|
|
156
148
|
relative.push(rel);
|
|
157
149
|
}
|
|
158
150
|
|
|
151
|
+
// The tool is looked up LAST: a request that is invalid whatever is installed
|
|
152
|
+
// must answer 400/409, not "install zip". Measured on the Windows gate, which
|
|
153
|
+
// has no `zip`: both refusal cases reported the missing tool instead of the
|
|
154
|
+
// refusal they assert.
|
|
155
|
+
const binary = compressToolFor(format);
|
|
156
|
+
if (!binary) {
|
|
157
|
+
return {
|
|
158
|
+
ok: false,
|
|
159
|
+
status: 501,
|
|
160
|
+
error: `${toolLabelFor(format)} is not installed on this machine`,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
159
164
|
const args = relative.map((rel) => `./${rel}`).join(' ');
|
|
160
165
|
const command = (() => {
|
|
161
166
|
switch (format) {
|
|
@@ -230,7 +235,21 @@ export async function extractArchive(options: ExtractOptions): Promise<ArchiveOu
|
|
|
230
235
|
case 'tar.bz2':
|
|
231
236
|
case 'tar.xz': {
|
|
232
237
|
const tar = findTool(TOOLS.tar!);
|
|
233
|
-
|
|
238
|
+
// Two mechanisms, each measured on a gate:
|
|
239
|
+
//
|
|
240
|
+
// - The archive arrives on STDIN, not as `-f <path>`: GNU tar reads a
|
|
241
|
+
// `C:\…` argument as a REMOTE `host:path` and dies with "Cannot
|
|
242
|
+
// connect to C: resolve failed" (the Windows gate, where `tar` is
|
|
243
|
+
// Git's GNU tar). A redirect is the shell's business, so the path
|
|
244
|
+
// never reaches tar's file-spec parser, and bsdtar reads the same
|
|
245
|
+
// stream.
|
|
246
|
+
// - The compression is named EXPLICITLY, because a stream is not
|
|
247
|
+
// sniffed the way a named file is: the Linux gate answered "tar:
|
|
248
|
+
// Archive is compressed. Use -z option" for a `.tar.gz` the same
|
|
249
|
+
// command opened by path. `-z`/`-j`/`-J` are accepted by GNU tar and
|
|
250
|
+
// bsdtar alike.
|
|
251
|
+
const compression = kind === 'tar.gz' ? 'z' : kind === 'tar.bz2' ? 'j' : kind === 'tar.xz' ? 'J' : '';
|
|
252
|
+
if (tar) return outcome(await run(`${tar} -x${compression}f - -C ${dest} < ${archive}`), archivePath);
|
|
234
253
|
if (bsdtar) return outcome(await run(`${bsdtar} -xf ${archive} -C ${dest}`), archivePath);
|
|
235
254
|
return missingTool('tar');
|
|
236
255
|
}
|
|
@@ -54,19 +54,20 @@ export const ICON_HUES: Readonly<Record<string, number>> = {
|
|
|
54
54
|
Maximize2: 270, Menu: 100, MessageSquare: 150, MessageSquareOff: 40, MessageSquarePlus: 350, MessagesSquare: 220,
|
|
55
55
|
Mic: 160, MicOff: 170, Minimize: 290, Minimize2: 130, Minus: 110, Monitor: 250,
|
|
56
56
|
Moon: 80, MoreHorizontal: 160, MousePointerClick: 70, MoveVertical: 300, Music: 280, Package: 270,
|
|
57
|
-
Paintbrush: 250, Palette: 320, PanelLeft: 280, PanelLeftClose: 160, PanelRight: 330,
|
|
58
|
-
Paperclip: 310, Pause: 10, Pencil: 0, PencilLine: 260, Pin: 0,
|
|
59
|
-
Pipette: 120, Play: 310, Plug: 50, Plus: 210, Power: 340,
|
|
60
|
-
Quote: 110, Radio: 320, Ratio: 160, RefreshCw: 180, Regex: 280,
|
|
61
|
-
ReplaceAll: 140, Rocket: 210, RotateCcw: 330, RotateCw: 0, Rows: 160,
|
|
62
|
-
Save: 40, SaveAll: 190, Scissors: 260, Search: 250, Send: 110,
|
|
63
|
-
ServerCog: 50, Settings: 240, Settings2: 20, Shield: 160, Shrink: 160,
|
|
64
|
-
SlidersHorizontal: 50, Smartphone: 20, Space: 290, Sparkles: 180, Square: 60,
|
|
65
|
-
SquarePen: 10, Star: 170, StickyNote: 100, Store: 340, Sun: 90,
|
|
66
|
-
Tag: 310, Target: 200, Terminal: 210, TextSelect: 220, Ticket: 170,
|
|
67
|
-
Type: 170, Undo2: 310, Upload: 0, UploadCloud: 50, User: 140,
|
|
68
|
-
Volume2: 270, VolumeX: 50, Wand2: 270, WandSparkles: 40, Waypoints: 30,
|
|
69
|
-
WrapText: 340, Wrench: 0, X: 230, Zap: 120, ZoomIn: 250,
|
|
57
|
+
PackageOpen: 230, Paintbrush: 250, Palette: 320, PanelLeft: 280, PanelLeftClose: 160, PanelRight: 330,
|
|
58
|
+
PanelRightClose: 350, Paperclip: 310, Pause: 10, Pencil: 0, PencilLine: 260, Pin: 0,
|
|
59
|
+
PinOff: 50, Pipette: 120, Play: 310, Plug: 50, Plus: 210, Power: 340,
|
|
60
|
+
Puzzle: 50, Quote: 110, Radio: 320, Ratio: 160, RefreshCw: 180, Regex: 280,
|
|
61
|
+
Replace: 190, ReplaceAll: 140, Rocket: 210, RotateCcw: 330, RotateCw: 0, Rows: 160,
|
|
62
|
+
Rows3: 250, Save: 40, SaveAll: 190, Scissors: 260, Search: 250, Send: 110,
|
|
63
|
+
Server: 300, ServerCog: 50, Settings: 240, Settings2: 20, Shield: 160, Shrink: 160,
|
|
64
|
+
Sliders: 290, SlidersHorizontal: 50, Smartphone: 20, Space: 290, Sparkles: 180, Square: 60,
|
|
65
|
+
SquareFunction: 220, SquarePen: 10, Star: 170, StickyNote: 100, Store: 340, Sun: 90,
|
|
66
|
+
Tablet: 290, Tag: 310, Target: 200, Terminal: 210, TextSelect: 220, Ticket: 170,
|
|
67
|
+
TriangleAlert: 210, Type: 170, Undo2: 310, Upload: 0, UploadCloud: 50, User: 140,
|
|
68
|
+
Variable: 330, Volume2: 270, VolumeX: 50, Wand2: 270, WandSparkles: 40, Waypoints: 30,
|
|
69
|
+
WholeWord: 240, WrapText: 340, Wrench: 0, X: 230, Zap: 120, ZoomIn: 250,
|
|
70
|
+
ZoomOut: 340,
|
|
70
71
|
};
|
|
71
72
|
|
|
72
73
|
/**
|