Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffCollapse.ts

34 lines1.2 KB

1export function areAllDiffFilesCollapsed(
2 fileKeys: ReadonlyArray<string>,
3 collapsedFileKeys: ReadonlySet<string>,
4): boolean {
5 return fileKeys.length > 0 && fileKeys.every((fileKey) => collapsedFileKeys.has(fileKey));
6}
7​
8/** Expands or collapses all of `fileKeys`; every other file, such as a hidden one, keeps its fold. */
9export function toggleAllDiffFiles(
10 fileKeys: ReadonlyArray<string>,
11 collapsedFileKeys: ReadonlySet<string>,
12): ReadonlySet<string> {
13 const collapse = !areAllDiffFilesCollapsed(fileKeys, collapsedFileKeys);
14 const next = new Set(collapsedFileKeys);
15 for (const fileKey of fileKeys) {
16 if (collapse) next.add(fileKey);
17 else next.delete(fileKey);
18 }
19 return next;
21​
22/**
23 * The folds a panel shows once the user has changed some: a file that was not there at the last
24 * change starts as the default says, not as the files that were there.
25 */
26export function withNewFilesFolded(
27 stored: ReadonlySet<string> | undefined,
28 known: ReadonlySet<string> | undefined,
29 defaults: ReadonlySet<string>,
30): ReadonlySet<string> {
31 if (stored === undefined) return defaults;
32 const added = [...defaults].filter((fileKey) => !known?.has(fileKey));
33 return added.length === 0 ? stored : new Set([...stored, ...added]);