refac
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
export const normalizeDocumentTargetPage = (page: unknown): number | null => {
|
||||
if (page === undefined || page === null || page === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const value = Number(page);
|
||||
if (!Number.isFinite(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const targetPage = Math.trunc(value);
|
||||
return targetPage > 0 ? targetPage : null;
|
||||
};
|
||||
|
||||
export const clampDocumentTargetPage = (page: number | null | undefined, pageCount: number) => {
|
||||
if (!page || pageCount < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Math.min(Math.max(1, page), pageCount);
|
||||
};
|
||||
@@ -3,6 +3,8 @@ import { v4 as uuidv4 } from 'uuid';
|
||||
import sha256 from 'js-sha256';
|
||||
import DOMPurify from 'dompurify';
|
||||
import { WEBUI_BASE_URL } from '$lib/constants';
|
||||
import type { FileNavOpenRequest } from '$lib/stores';
|
||||
import { normalizeDocumentTargetPage } from '$lib/utils/documentPreview';
|
||||
|
||||
import dayjs from 'dayjs';
|
||||
import relativeTime from 'dayjs/plugin/relativeTime';
|
||||
@@ -2321,10 +2323,12 @@ export const formatSkillName = (name) => {
|
||||
*/
|
||||
export const displayFileHandler = (
|
||||
path: string,
|
||||
stores: { showControls: Writable<boolean>; showFileNavPath: Writable<string | null> }
|
||||
stores: { showControls: Writable<boolean>; showFileNavPath: Writable<FileNavOpenRequest | null> },
|
||||
options: { page?: unknown } = {}
|
||||
) => {
|
||||
if (path) {
|
||||
stores.showControls.set(true);
|
||||
stores.showFileNavPath.set(path);
|
||||
const page = normalizeDocumentTargetPage(options.page);
|
||||
stores.showFileNavPath.set(page ? { path, page } : path);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user