feat(webui): polish tabbed previews and image presentation
This commit is contained in:
1 parent
fccf415283
commit
b555164a17
41 files changed
+1459
-621
No files matched your search
@@ -1,12 +1,18 @@
|
||||
import { useCallback, useState, useSyncExternalStore } from "react";
|
||||
|
||||
interface FilePreviewState {
|
||||
path: string | null;
|
||||
webUrl?: string | null;
|
||||
export interface PreviewTab {
|
||||
id: string;
|
||||
kind: "file" | "web";
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface FilePreviewState {
|
||||
tabs: PreviewTab[];
|
||||
activeId: string | null;
|
||||
width: number;
|
||||
}
|
||||
|
||||
const EMPTY_PREVIEW: FilePreviewState = { path: null, width: 544 };
|
||||
const EMPTY_PREVIEW: FilePreviewState = { tabs: [], activeId: null, width: 544 };
|
||||
|
||||
/** App-lifetime view state only. Never stores file contents or writes to browser storage. */
|
||||
export class FilePreviewStore {
|
||||
@@ -25,13 +31,37 @@ export class FilePreviewStore {
|
||||
update(key: string, patch: Partial<FilePreviewState>) {
|
||||
const previous = this.get(key);
|
||||
const next = { ...previous, ...patch };
|
||||
if (patch.path) next.webUrl = null;
|
||||
if (patch.webUrl) next.path = null;
|
||||
if (previous.path === next.path && previous.width === next.width && previous.webUrl === next.webUrl) return;
|
||||
if (previous.tabs === next.tabs && previous.width === next.width && previous.activeId === next.activeId) return;
|
||||
this.states.set(key, next);
|
||||
this.listeners.forEach((listener) => listener());
|
||||
}
|
||||
|
||||
open(key: string, kind: PreviewTab["kind"], value: string) {
|
||||
const previous = this.get(key);
|
||||
const id = `${kind}:${value}`;
|
||||
const tabs = previous.tabs.some((tab) => tab.id === id)
|
||||
? previous.tabs : [...previous.tabs, { id, kind, value }];
|
||||
this.update(key, { tabs, activeId: id });
|
||||
}
|
||||
|
||||
select(key: string, id: string) {
|
||||
if (this.get(key).tabs.some((tab) => tab.id === id)) this.update(key, { activeId: id });
|
||||
}
|
||||
|
||||
closeTab(key: string, id: string) {
|
||||
const previous = this.get(key);
|
||||
const index = previous.tabs.findIndex((tab) => tab.id === id);
|
||||
if (index < 0) return;
|
||||
const tabs = previous.tabs.filter((tab) => tab.id !== id);
|
||||
const activeId = previous.activeId === id
|
||||
? (tabs[Math.min(index, tabs.length - 1)]?.id ?? null) : previous.activeId;
|
||||
this.update(key, { tabs, activeId });
|
||||
}
|
||||
|
||||
close(key: string) {
|
||||
this.update(key, { tabs: [], activeId: null });
|
||||
}
|
||||
|
||||
delete(key: string) {
|
||||
if (this.states.delete(key)) this.listeners.forEach((listener) => listener());
|
||||
}
|
||||
@@ -47,14 +77,23 @@ export function useFilePreviewState(key: string | null, sharedStore?: FilePrevie
|
||||
const store = sharedStore ?? localStore;
|
||||
const snapshot = useCallback(() => store.get(key), [key, store]);
|
||||
const state = useSyncExternalStore(store.subscribe, snapshot, snapshot);
|
||||
const setPath = useCallback((path: string | null) => {
|
||||
if (key) store.update(key, { path });
|
||||
const openFile = useCallback((path: string) => {
|
||||
if (key) store.open(key, "file", path);
|
||||
}, [key, store]);
|
||||
const setWidth = useCallback((width: number) => {
|
||||
if (key) store.update(key, { width });
|
||||
}, [key, store]);
|
||||
const setWebUrl = useCallback((webUrl: string | null) => {
|
||||
if (key) store.update(key, { webUrl });
|
||||
const openWeb = useCallback((url: string) => {
|
||||
if (key) store.open(key, "web", url);
|
||||
}, [key, store]);
|
||||
return { state, setPath, setWidth, setWebUrl };
|
||||
const selectTab = useCallback((id: string) => {
|
||||
if (key) store.select(key, id);
|
||||
}, [key, store]);
|
||||
const closeTab = useCallback((id: string) => {
|
||||
if (key) store.closeTab(key, id);
|
||||
}, [key, store]);
|
||||
const close = useCallback(() => {
|
||||
if (key) store.close(key);
|
||||
}, [key, store]);
|
||||
return { state, openFile, openWeb, selectTab, closeTab, close, setWidth };
|
||||
}
|
||||
Reference in new issue
Block a user