export type FullscreenTurnViewMode = 'edit' | 'split' | 'preview'; export interface StudioLayoutSizes { sidebarWidth: number; jsonViewerWidth: number; tagModalSidebarWidth: number; imagesModalSidebarWidth: number; templateModalSidebarWidth: number; exportModalSidebarWidth: number; imageAttachmentPreviewWidth: number; systemPromptHeight: number; fullscreenEditorWidth: number; showInvisibleChars: boolean; fullscreenViewMode: FullscreenTurnViewMode; syncSplitLocation: boolean; } const LAYOUT_STORAGE_KEY = 'sharegpt_dialogue_studio_layout_v1'; export const DEFAULT_LAYOUT_SIZES: StudioLayoutSizes = { sidebarWidth: 296, jsonViewerWidth: 440, tagModalSidebarWidth: 320, imagesModalSidebarWidth: 310, templateModalSidebarWidth: 340, exportModalSidebarWidth: 360, imageAttachmentPreviewWidth: 440, systemPromptHeight: 96, fullscreenEditorWidth: 620, showInvisibleChars: false, fullscreenViewMode: 'split', syncSplitLocation: true, }; export function clampNumber(value: number, min: number, max: number): number { if (!Number.isFinite(value)) return min; return Math.min(max, Math.max(min, Math.round(value))); } export function loadLayoutSizes(): StudioLayoutSizes { try { const raw = localStorage.getItem(LAYOUT_STORAGE_KEY); if (!raw) return { ...DEFAULT_LAYOUT_SIZES }; const parsed = JSON.parse(raw) as Partial; const validViewMode: FullscreenTurnViewMode = parsed.fullscreenViewMode === 'edit' || parsed.fullscreenViewMode === 'split' || parsed.fullscreenViewMode === 'preview' ? parsed.fullscreenViewMode : DEFAULT_LAYOUT_SIZES.fullscreenViewMode; return { sidebarWidth: clampNumber( parsed.sidebarWidth ?? DEFAULT_LAYOUT_SIZES.sidebarWidth, 240, 520 ), jsonViewerWidth: clampNumber( parsed.jsonViewerWidth ?? DEFAULT_LAYOUT_SIZES.jsonViewerWidth, 320, 760 ), tagModalSidebarWidth: clampNumber( parsed.tagModalSidebarWidth ?? DEFAULT_LAYOUT_SIZES.tagModalSidebarWidth, 240, 520 ), imagesModalSidebarWidth: clampNumber( parsed.imagesModalSidebarWidth ?? DEFAULT_LAYOUT_SIZES.imagesModalSidebarWidth, 230, 500 ), templateModalSidebarWidth: clampNumber( parsed.templateModalSidebarWidth ?? DEFAULT_LAYOUT_SIZES.templateModalSidebarWidth, 250, 520 ), exportModalSidebarWidth: clampNumber( parsed.exportModalSidebarWidth ?? DEFAULT_LAYOUT_SIZES.exportModalSidebarWidth, 260, 560 ), imageAttachmentPreviewWidth: clampNumber( parsed.imageAttachmentPreviewWidth ?? DEFAULT_LAYOUT_SIZES.imageAttachmentPreviewWidth, 280, 680 ), systemPromptHeight: clampNumber( parsed.systemPromptHeight ?? DEFAULT_LAYOUT_SIZES.systemPromptHeight, 68, 380 ), fullscreenEditorWidth: clampNumber( parsed.fullscreenEditorWidth ?? DEFAULT_LAYOUT_SIZES.fullscreenEditorWidth, 320, 980 ), showInvisibleChars: typeof parsed.showInvisibleChars === 'boolean' ? parsed.showInvisibleChars : DEFAULT_LAYOUT_SIZES.showInvisibleChars, fullscreenViewMode: validViewMode, syncSplitLocation: typeof parsed.syncSplitLocation === 'boolean' ? parsed.syncSplitLocation : DEFAULT_LAYOUT_SIZES.syncSplitLocation, }; } catch { return { ...DEFAULT_LAYOUT_SIZES }; } } export function saveLayoutSizes(sizes: StudioLayoutSizes): void { try { localStorage.setItem(LAYOUT_STORAGE_KEY, JSON.stringify(sizes)); } catch { // Ignore storage quota or iframe restrictions (e.g. Hugging Face Spaces) } }