@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
52 lines (51 loc) • 1.56 kB
JavaScript
import { useCallback, useSyncExternalStore } from "react";
//#region src/Image/viewer/registry.ts
let state = null;
let nextToken = 0;
const listeners = /* @__PURE__ */ new Set();
const emit = () => {
for (const listener of listeners) listener();
};
const flushClosed = () => {
if (!state || state.closing) return;
state.closing = true;
state.onOpenChange?.(false);
};
const openPreview = (entry, entries, index = 0, openerFocusElement = null) => {
flushClosed();
nextToken += 1;
state = {
closing: false,
entries: entries && entries.length > 0 ? entries : [entry],
index: entries && entries.length > 0 ? Math.min(Math.max(index, 0), entries.length - 1) : 0,
onOpenChange: entry.options.onOpenChange,
openerFocusElement,
token: nextToken
};
emit();
entry.options.onOpenChange?.(true);
};
const beginClosePreview = (token) => {
if (state?.token !== token) return;
flushClosed();
};
const endClosePreview = (token) => {
if (state?.token !== token) return;
flushClosed();
state = null;
emit();
};
const subscribe = (listener) => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
};
const getServerSnapshot = () => null;
const usePreviewSession = (elementRef) => {
const getSnapshot = useCallback(() => state && state.entries[state.index]?.element === elementRef.current ? state : null, [elementRef]);
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
};
//#endregion
export { beginClosePreview, endClosePreview, openPreview, usePreviewSession };
//# sourceMappingURL=registry.mjs.map