1
1
mirror of https://github.com/go-gitea/gitea synced 2025-12-06 21:08:25 +00:00
Files
gitea/web_src/js/modules/diff-file.ts
silverwind 46d7adefe0 Enable TypeScript strictNullChecks (#35843)
A big step towards enabling strict mode in Typescript.

There was definitely a good share of potential bugs while refactoring
this. When in doubt, I opted to keep the potentially broken behaviour.
Notably, the `DOMEvent` type is gone, it was broken and we're better of
with type assertions on `e.target`.

---------

Signed-off-by: silverwind <me@silverwind.io>
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2025-12-03 02:13:16 +00:00

83 lines
2.2 KiB
TypeScript

import {reactive} from 'vue';
import type {Reactive} from 'vue';
const {pageData} = window.config;
export type DiffStatus = '' | 'added' | 'modified' | 'deleted' | 'renamed' | 'copied' | 'typechange';
export type DiffTreeEntry = {
FullName: string,
DisplayName: string,
NameHash: string,
DiffStatus: DiffStatus,
EntryMode: string,
IsViewed: boolean,
Children: DiffTreeEntry[] | null,
FileIcon: string,
ParentEntry?: DiffTreeEntry,
};
type DiffFileTreeData = {
TreeRoot: DiffTreeEntry,
};
type DiffFileTree = {
folderIcon: string;
folderOpenIcon: string;
diffFileTree: DiffFileTreeData;
fullNameMap: Record<string, DiffTreeEntry>
fileTreeIsVisible: boolean;
selectedItem: string;
};
let diffTreeStoreReactive: Reactive<DiffFileTree>;
export function diffTreeStore() {
if (!diffTreeStoreReactive) {
diffTreeStoreReactive = reactiveDiffTreeStore(pageData.DiffFileTree, pageData.FolderIcon, pageData.FolderOpenIcon);
}
return diffTreeStoreReactive;
}
export function diffTreeStoreSetViewed(store: Reactive<DiffFileTree>, fullName: string, viewed: boolean) {
const entry = store.fullNameMap[fullName];
if (!entry) return;
entry.IsViewed = viewed;
for (let parent = entry.ParentEntry; parent; parent = parent.ParentEntry) {
parent.IsViewed = isEntryViewed(parent);
}
}
function fillFullNameMap(map: Record<string, DiffTreeEntry>, entry: DiffTreeEntry) {
map[entry.FullName] = entry;
if (!entry.Children) return;
entry.IsViewed = isEntryViewed(entry);
for (const child of entry.Children) {
child.ParentEntry = entry;
fillFullNameMap(map, child);
}
}
export function reactiveDiffTreeStore(data: DiffFileTreeData, folderIcon: string, folderOpenIcon: string): Reactive<DiffFileTree> {
const store = reactive({
diffFileTree: data,
folderIcon,
folderOpenIcon,
fileTreeIsVisible: false,
selectedItem: '',
fullNameMap: {},
});
fillFullNameMap(store.fullNameMap, data.TreeRoot);
return store;
}
function isEntryViewed(entry: DiffTreeEntry): boolean {
if (entry.Children) {
let count = 0;
for (const child of entry.Children) {
if (child.IsViewed) count++;
}
return count === entry.Children.length;
}
return entry.IsViewed;
}