{"version":3,"file":"shell-state-D4Oq8pNf.mjs","names":[],"sources":["../src/canvas/canvas-zoom.ts","../src/inspector/panel-folds.ts","../src/shell-state.ts"],"sourcesContent":["/**\n * How large the canvas draws the page it is previewing.\n *\n * The canvas has always had a scale, and it has never been the author's. It is\n * derived to make the requested width fit the region left over after the\n * panels, so opening a panel silently shrinks the page — measured at 89% with\n * the rail alone and 59.5% with a panel open. Nothing said which of those an\n * author was looking at, and nothing let them choose.\n *\n * ## Fit is a CHOICE, not the absence of one\n *\n * Modelled as a discriminated union rather than a nullable number, because\n * \"fit\" and \"a scale I picked\" are different intentions and a caller has to\n * tell them apart. `null` meaning fit would be the same overloaded absence\n * that a `number | null` scale invites: is it unset, is it fitting, or did\n * something fail to measure?\n *\n * Fit RE-DERIVES on every layout change, which is what makes it fit; a fixed\n * scale deliberately does not, so opening a panel narrows what is visible\n * rather than shrinking the page under the author.\n *\n * ## Why a percentage is absolute, and not a multiplier of the fit\n *\n * A multiplier is the obvious composition and it makes the number a lie: an\n * author choosing 100% is asking for actual size, and \"fit × 1\" is whatever\n * the panels happen to leave. Every editor that shows a zoom percentage means\n * the absolute one, so the number here means what it says and Fit is the mode\n * that computes one.\n *\n * @module canvas-zoom\n */\n\n/** What the canvas was asked to draw at. */\nexport type CanvasZoom =\n  | { readonly kind: \"fit\" }\n  | { readonly kind: \"fixed\"; readonly scale: number };\n\n/** The default, and what every canvas did before there was a choice. */\nexport const FIT_ZOOM: CanvasZoom = { kind: \"fit\" };\n\n/**\n * The steps the control offers, as fractions.\n *\n * Discrete rather than continuous. A slider or a free number invites values\n * whose arithmetic is exact and whose result is unreadable — 37% of a page is\n * not a view of anything — and every comparable editor offers steps for the\n * same reason. `1` is in the middle of the list because actual size is the\n * value an author returns to.\n */\nexport const ZOOM_STEPS: readonly number[] = [0.5, 0.75, 1, 1.5, 2];\n\n/**\n * The bounds a stored or typed scale is held to.\n *\n * Wider than the steps, because a value can arrive from storage written by a\n * later version, and clamping is what keeps that from painting the canvas\n * somewhere unreachable. Below the floor the page is illegible; above the\n * ceiling one block fills the region and the author has lost the page.\n */\nexport const MIN_ZOOM = 0.25;\nexport const MAX_ZOOM = 4;\n\n/**\n * A fixed scale the canvas can actually paint at, or `null` when it cannot.\n *\n * The bounds are not only about what STORAGE may hold. `CanvasZoom` is\n * exported, so a host can construct `{ kind: \"fixed\", scale }` directly with\n * any number the type admits — `NaN`, `Infinity`, zero, or something far\n * outside these bounds — and none of those reach `readZoom` on the way in.\n * Interpolated into a `zoom` declaration they produce an invalid rule or a\n * canvas nobody can see, so the check belongs wherever a scale is USED rather\n * than only where one is parsed.\n */\nexport function usableScale(scale: number): number | null {\n  if (!Number.isFinite(scale)) return null;\n  return scale < MIN_ZOOM || scale > MAX_ZOOM ? null : scale;\n}\n\n/**\n * A stored value read back as a zoom, or `null` when it is not one.\n *\n * `null` rather than a silent fall back to fit: a caller restoring preferences\n * needs to know it found nothing, and a fixed scale that quietly became fit\n * would look to an author like the editor forgetting what they chose.\n */\nexport function readZoom(value: unknown): CanvasZoom | null {\n  if (value === \"fit\") return FIT_ZOOM;\n  if (typeof value !== \"number\") return null;\n  const scale = usableScale(value);\n  return scale === null ? null : { kind: \"fixed\", scale };\n}\n\n/** A zoom as it is stored: the string `\"fit\"`, or the scale itself. */\nexport function writeZoom(zoom: CanvasZoom): \"fit\" | number {\n  return zoom.kind === \"fit\" ? \"fit\" : zoom.scale;\n}\n\n/**\n * The next step in a direction, or the same zoom when there is none.\n *\n * From FIT it steps off the scale the fit produced, so the first press moves\n * from what the author is looking at rather than jumping to an end of the\n * list. That needs the fit scale passed in, because only the canvas knows it.\n *\n * A scale the canvas cannot paint steps from the fit scale as well, for the\n * same reason and by the rule {@link usableScale} states: a host can construct\n * `{ kind: \"fixed\", scale }` with any number the type admits, and the canvas\n * already falls back to fit when it cannot use one. Stepping from the raw value\n * instead makes every comparison against `NaN` false, so both directions return\n * the zoom unchanged — leaving a stepper that cannot move off an unusable\n * scale, while the canvas is meanwhile painting the fit the steps would come\n * from.\n */\nexport function steppedZoom(\n  zoom: CanvasZoom,\n  fitScale: number,\n  direction: \"in\" | \"out\"\n): CanvasZoom {\n  const from =\n    zoom.kind === \"fit\" ? fitScale : (usableScale(zoom.scale) ?? fitScale);\n  const candidates =\n    direction === \"in\"\n      ? ZOOM_STEPS.filter(step => step > from + 1e-9)\n      : [...ZOOM_STEPS].reverse().filter(step => step < from - 1e-9);\n  const next = candidates[0];\n  return next === undefined ? zoom : { kind: \"fixed\", scale: next };\n}\n","/**\n * Which sections of the block panel are open: each tab's sections fold on\n * their own, and stay as the author left them across selections and visits.\n *\n * Kept per tab and section NAME (`style:Spacing`), not per block, because an\n * author working on spacing selects one block after another and means to keep\n * Spacing open on each. \"Expand all\" and \"Collapse all\" store one entry for\n * the tab (`style:*`) and drop the tab's own, so they reach sections the panel\n * has not drawn yet.\n *\n * Held with the shell's other preferences (`panel-folds-context` lends them\n * to the panel).\n *\n * @module panel-folds\n */\n/** The open or closed state of each section the author has folded. */\nexport type SectionFolds = Readonly<Record<string, boolean>>;\n\n/** The most sections remembered; the oldest are dropped past it. */\nexport const MAX_SECTION_FOLDS = 200;\n\n/** The longest key remembered: a tab, a colon and a section's name. */\nconst MAX_FOLD_KEY_LENGTH = 120;\n\n/** The key one section's fold is kept under. */\nfunction foldKey(tab: string, label: string): string {\n  return `${tab}:${label}`;\n}\n\n/** The key a whole tab's Expand all or Collapse all is kept under. */\nfunction allKey(tab: string): string {\n  return `${tab}:*`;\n}\n\n/** What a section's opening falls back to when nothing is remembered. */\nexport interface SectionDefault {\n  /** Whether it holds a value for what is being edited. */\n  readonly holdsValue: boolean;\n  /** Whether it is the tab's first section. */\n  readonly first: boolean;\n}\n\n/**\n * Whether a section is open: as the author last left it, else as they last\n * left the whole tab, else open when it holds a value or is the tab's first.\n */\nexport function sectionOpens(\n  folds: SectionFolds,\n  tab: string,\n  label: string,\n  fallback: SectionDefault\n): boolean {\n  return (\n    folds[foldKey(tab, label)] ??\n    folds[allKey(tab)] ??\n    (fallback.holdsValue || fallback.first)\n  );\n}\n\n/** The folds with the oldest dropped past {@link MAX_SECTION_FOLDS}. */\nfunction bounded(folds: Record<string, boolean>): SectionFolds {\n  const keys = Object.keys(folds);\n  if (keys.length <= MAX_SECTION_FOLDS) return folds;\n  return Object.fromEntries(\n    keys.slice(keys.length - MAX_SECTION_FOLDS).map(key => [key, folds[key]])\n  );\n}\n\n/** The folds with one section set open or closed, kept newest last. */\nexport function withSectionFold(\n  folds: SectionFolds,\n  tab: string,\n  label: string,\n  open: boolean\n): SectionFolds {\n  const key = foldKey(tab, label);\n  const { [key]: _previous, ...rest } = folds;\n  return bounded({ ...rest, [key]: open });\n}\n\n/** The folds with every section of a tab open or closed. */\nexport function withTabFold(\n  folds: SectionFolds,\n  tab: string,\n  open: boolean\n): SectionFolds {\n  const prefix = `${tab}:`;\n  const rest = Object.fromEntries(\n    Object.entries(folds).filter(([key]) => !key.startsWith(prefix))\n  );\n  return bounded({ ...rest, [allKey(tab)]: open });\n}\n\n/**\n * Stored folds as the panel may read them: a record of booleans under keys a\n * section could have, bounded, and nothing else.\n */\nexport function readSectionFolds(stored: unknown): SectionFolds {\n  if (typeof stored !== \"object\" || stored === null || Array.isArray(stored)) {\n    return {};\n  }\n  const kept: Record<string, boolean> = {};\n  for (const [key, open] of Object.entries(stored)) {\n    if (typeof open !== \"boolean\") continue;\n    if (key.length > MAX_FOLD_KEY_LENGTH || !key.includes(\":\")) continue;\n    kept[key] = open;\n  }\n  return bounded(kept);\n}\n","/**\n * The editor shell's state, kept apart from the components that render it.\n *\n * Two reasons, and the second is why this file exists at all rather than the\n * state living in the shell component:\n *\n * - It is arithmetic and set membership — which panel is open, how wide it may\n *   be, whether the viewport can carry the full layout. None of it needs React.\n * - A layout cannot be checked by rendering it in jsdom, which reports every\n *   element as zero-sized. A component test of \"the panel is 320px\" measures\n *   nothing and passes whatever the CSS does. The decisions are made here,\n *   where they can be asserted, and the browser verifies the LAYOUT separately.\n *\n * @module shell-state\n */\n\nimport {\n  FIT_ZOOM,\n  readZoom,\n  writeZoom,\n  type CanvasZoom,\n} from \"./canvas/canvas-zoom\";\nimport { readSectionFolds, type SectionFolds } from \"./inspector/panel-folds\";\n\n/**\n * The panels the left rail switches between, in the order an author meets\n * them.\n *\n * One at a time, by decision (PB-D17 D10-1): fixed sides, no docking, no\n * floating. The rail selects; it never opens a second panel beside the first.\n * Settings and Help sit at the rail's foot as dialogs, never panels, so\n * neither is one of these.\n */\nexport const LEFT_PANELS = [\n  \"add\",\n  \"layers\",\n  \"pages\",\n  \"components\",\n  \"design\",\n] as const;\n\nexport type LeftPanel = (typeof LEFT_PANELS)[number];\n\n/**\n * The panel and view a stored preference names.\n *\n * A panel this shell no longer has, renamed or removed, opens the default\n * panel with no part chosen: no table of old names is kept, since no site\n * stored them for long, and the part chosen belonged to the old panel. With\n * no panel open, the part an author last chose is kept, so the panel reopens\n * on it, as it does when a panel is closed and opened again in one session.\n */\nfunction storedPanel(\n  panel: unknown,\n  view: unknown\n): { panel: LeftPanel | null; view: string | null } {\n  const shown = typeof view === \"string\" ? view : null;\n  if (isLeftPanel(panel)) return { panel, view: shown };\n  if (panel === null || panel === undefined)\n    return { panel: null, view: shown };\n  return {\n    panel: DEFAULT_PREFERENCES.leftPanel,\n    view: DEFAULT_PREFERENCES.leftPanelView,\n  };\n}\n\n/**\n * Whether a stored value still names a panel.\n *\n * Persisted preferences outlive the code that wrote them. A panel removed in a\n * later release leaves a string nothing answers to, and restoring it blindly\n * opens a panel that renders nothing — an empty region with no way back to a\n * real one.\n */\nexport function isLeftPanel(value: unknown): value is LeftPanel {\n  return (\n    typeof value === \"string\" &&\n    (LEFT_PANELS as readonly string[]).includes(value)\n  );\n}\n\n/**\n * Width bounds for the two resizable regions, in CSS pixels.\n *\n * DECLARED here and enforced by `react-resizable-panels`, which takes pixel\n * bounds directly (`minSize={240}`) and solves them — including which region\n * yields when a window resize leaves no room. This module deliberately does NOT\n * solve them a second time: a hand-rolled clamp beside a library that already\n * constrains the same drag is two answers to one question, and the two disagree\n * the first time the library changes how it distributes a shortfall.\n *\n * Lower bounds are the point below which a region stops being usable rather than\n * round numbers: a layers tree needs an indent plus a label, and the inspector\n * carries two-column controls.\n *\n * `MIN_CANVAS_WIDTH` is the reason bounds cannot be read per-panel. At the\n * minimum supported viewport, both panels at their individual maximums leave the\n * canvas at 232px — narrower than either panel, in an editor whose subject IS\n * the canvas — and no per-panel bound is violated, because the constraint was\n * never per-panel. Expressed as the canvas panel's OWN minimum, the library\n * enforces it jointly during a drag.\n */\nexport const PANEL_BOUNDS = {\n  left: { min: 240, max: 480, initial: 300 },\n  /*\n   * The inspector's minimum is the width its widest fixed control needs: the\n   * spacing box holds whole from a 264px field column (16.5rem), and the\n   * panel adds 24px of padding and up to 17px of classic scrollbar, which\n   * it keeps room for. Narrower, the box falls back to a list of rows.\n   */\n  inspector: { min: 308, max: 520, initial: 320 },\n} as const;\n\n/**\n * The rail's fixed width in CSS pixels: a 36px icon square with 6px either\n * side. The rail shows icons alone, each named by its tooltip, so it gives the\n * canvas back the width a labelled rail took.\n */\nexport const RAIL_WIDTH = 48;\n\n/**\n * The narrowest the canvas may become before a drag stops taking from it.\n *\n * The canvas is the thing being edited, so it is the region with a floor rather\n * than the one that absorbs whatever is left.\n *\n * This is the EDITING SURFACE, and {@link MIN_CANVAS_PANEL_WIDTH} is what a\n * bound is read from.\n */\nexport const MIN_CANVAS_WIDTH = 480;\n\n/**\n * The narrowest the canvas PANEL may become.\n *\n * The page meets the panel's edges with no gap around it, so the panel and the\n * editing surface it holds are one width, and the panel's floor is the\n * surface's own. Named apart from {@link MIN_CANVAS_WIDTH} because it is what\n * a resize bound is read from, and the surface is what the floor protects.\n */\nexport const MIN_CANVAS_PANEL_WIDTH: number = MIN_CANVAS_WIDTH;\n\n/**\n * The narrowest viewport the full shell is supported at (PB-D17 D10-5).\n *\n * Below this the rail, both panels and a usable canvas do not fit at their\n * minimum widths, so the shell does not try: it shows the canvas and says where\n * to edit instead. A builder that merely gets cramped is worse than one that\n * says it needs more width, because the author discovers the limit by failing\n * at a task.\n */\nexport const MIN_SHELL_WIDTH = 1280;\n\n/**\n * Whether a box of this width can carry the full shell.\n *\n * The parameter is the width of the space the shell was GIVEN, not the\n * window's. The shell sizes to its container, so the two differ whenever it is\n * embedded — a narrow column on a wide display being the case that matters.\n */\nexport function fitsFullShell(width: number): boolean {\n  return width >= MIN_SHELL_WIDTH;\n}\n\n/**\n * What the shell remembers between sessions.\n *\n * The layout is stored as the PROPORTIONAL map `react-resizable-panels` reports\n * from `onLayoutChanged` — panel id to a percentage — never as pixel widths. A\n * pixel layout is wrong on the next monitor; a proportional one survives a\n * window resize, and the pixel BOUNDS still hold because the library re-applies\n * them to whatever the proportions resolve to.\n *\n * Deliberately NOT stored: selection, scroll position, or anything describing\n * the document. Those belong to the document's own state, and a copy here is a\n * second source that goes stale the first time the document changes underneath\n * it.\n */\nexport interface ShellPreferences {\n  leftPanel: LeftPanel | null;\n  /**\n   * Which part of a panel is showing, for a panel with parts — the Design\n   * panel's tab. The host decides what the names mean; the shell only keeps\n   * the one the author last chose.\n   */\n  leftPanelView: string | null;\n  leftPinned: boolean;\n  /**\n   * Whether the author has hidden a side of the editor to give the canvas the\n   * room. Kept apart from which left panel is open, so hiding the left side\n   * and showing it again comes back to the same panel. The top bar's panel\n   * buttons and mod+backslash set them.\n   */\n  leftPanelHidden: boolean;\n  inspectorHidden: boolean;\n  /**\n   * How large the canvas draws the page, as the author last left it.\n   *\n   * Chrome rather than document state, like everything else here: it describes\n   * how one person is looking at the editor, not what the page IS, so it must\n   * not travel with the document to another author.\n   */\n  zoom: CanvasZoom;\n  /**\n   * The breakpoint the author is styling, as they last chose it: an id from the\n   * site's breakpoints, or null for the base one, which is named Desktop.\n   *\n   * A choice, never a reading. Taking it from the width the canvas measured\n   * made opening a panel, or a narrower window, move every edit to a narrower\n   * breakpoint the author never picked. Opening and closing panels changes\n   * only how far the canvas is zoomed to fit.\n   *\n   * Stored as the author left it even when the site no longer defines that\n   * breakpoint: the host resolves an unknown id to the base one, so removing a\n   * breakpoint and adding it back does not lose the choice.\n   */\n  editingBreakpoint: string | null;\n  /**\n   * Layouts, one per PANEL TOPOLOGY.\n   *\n   * Keyed rather than singular because a layout only means anything alongside\n   * the panel set it was measured for. The left panel is conditionally\n   * rendered, so the group has two shapes — with it and without — and their\n   * layouts have different keys and different arithmetic. Stored as one record\n   * they overwrote each other: resizing with a panel open and then closing it\n   * left a three-key layout that no two-panel group could accept, so the widths\n   * were dropped on the next load.\n   *\n   * This is the shape the panel library's own persistence helper uses, for the\n   * same reason — its `panelIds` prop exists so a group with conditionally\n   * rendered panels can \"save and restore multiple layouts\".\n   *\n   * Outer key: {@link topologyKey} over the mounted panel ids.\n   * Inner: panel id to percentage.\n   */\n  layouts: Record<string, Record<string, number>>;\n  /**\n   * Whether the rail's single-letter keys (A, L, P, C, D) open its panels.\n   *\n   * ON by default, and a preference because a key with no modifier can be\n   * set off by speech input or a stray press: WCAG 2.1.4 asks that such a\n   * shortcut can be turned off. Help holds the switch, beside the keys.\n   *\n   * Optional in this published type, so a host that builds preferences\n   * written before the switch still compiles; absent reads as on.\n   */\n  panelKeys?: boolean;\n  /**\n   * Which sections of the block panel the author left open or closed, by tab\n   * and section name (`panel-folds`).\n   *\n   * Optional in this published type, as `panelKeys` is; absent reads as none\n   * remembered.\n   */\n  sectionFolds?: SectionFolds;\n  /**\n   * Whether the block panel prints each control's CSS names under its label\n   * (\"Show CSS names\" in the panel's options menu). Off by default, and\n   * optional in this published type, as `panelKeys` is: absent reads as off.\n   */\n  showCssNames?: boolean;\n}\n\n/**\n * The class marking the editor shell's own root element.\n *\n * The scope every piece of editor chrome is drawn inside. Kept here and away\n * from the component because `builder-chrome.css` has to spell it out\n * literally, and a marker spelled out literally is one a rename can break\n * silently: so it has one exported spelling that the shell writing it, the\n * code asking about it and the test pinning the stylesheet all take from.\n */\nexport const BUILDER_CHROME_CLASS = \"nx-builder-chrome\";\n\n/**\n * A scope that resolves `--nx-builder-*` without claiming to be the chrome root.\n *\n * For a surface the shell mounts OUTSIDE {@link BUILDER_CHROME_CLASS} — custom\n * properties inherit down and never across, so such a surface needs the tokens\n * declared on an ancestor of its own. Giving it the chrome class instead would\n * put a second chrome root in the document, and every selector and query\n * meaning \"the editor\" would match whichever came first.\n */\nexport const BUILDER_TOKENS_CLASS = \"nx-builder-tokens\";\n\n/**\n * The class marking the canvas root, and the boundary the hit-test stops at.\n *\n * Here rather than in `canvas.tsx` for the reason above: it is the middle term\n * of the empty-container affordance's selector, which `empty-slot.ts` composes\n * — and reaching into the canvas COMPONENT for it would pull a React module\n * into every consumer of that constant, for one string. `canvas.tsx` re-exports\n * it, so callers reading it as the canvas's own marker are unaffected.\n *\n * The hit-test walk needs an upper bound (see `nodeIdFromEvent`), and that\n * bound has to be identifiable from a DOM node rather than from React state,\n * because the walk starts at an event target and climbs.\n */\nexport const CANVAS_ROOT_CLASS = \"nx-canvas\";\n\n/**\n * The identity of a panel arrangement, from the panels themselves.\n *\n * Sorted and joined rather than taken from `leftPanel`, so it is derived from\n * what is actually MOUNTED rather than from a second description of it that can\n * disagree. Sorting makes it independent of the order the ids arrive in, which\n * varies with how a host's JSON was serialised.\n */\nexport function topologyKey(panelIds: readonly string[]): string {\n  return [...panelIds].sort().join(\",\");\n}\n\nexport const DEFAULT_PREFERENCES: ShellPreferences = {\n  leftPanel: null,\n  leftPanelView: null,\n  leftPinned: true,\n  leftPanelHidden: false,\n  inspectorHidden: false,\n  zoom: FIT_ZOOM,\n  editingBreakpoint: null,\n  layouts: {},\n  panelKeys: true,\n  sectionFolds: {},\n  showCssNames: false,\n};\n\n/**\n * Where preferences are read and written.\n *\n * A PORT rather than a direct `localStorage` call, for two reasons that both\n * bite later. The ratified plan wants these durable per user on the server\n * eventually, and a component reaching for `localStorage` makes that a rewrite\n * instead of a different argument. And `localStorage` does not exist while the\n * shell renders on a server, so a direct call is a crash rather than a default.\n *\n * Both methods may fail and the shell must not care: storage is unavailable in\n * private browsing on some engines, and a quota error is possible on write.\n */\nexport interface PreferenceStore {\n  read: () => string | null;\n  write: (value: string) => void;\n}\n\n/** A store that remembers nothing, for a server render and for a browser that refuses storage. */\nexport const NO_STORAGE: PreferenceStore = {\n  read: () => null,\n  write: () => undefined,\n};\n\n/**\n * A `localStorage`-backed store under one key.\n *\n * Takes the key rather than owning one, so the shell's chrome preferences and\n * anything else the editor remembers share this port instead of each writing\n * their own `typeof window` check and their own swallow. Outside a browser it\n * degrades to {@link NO_STORAGE} rather than throwing, which is the whole\n * reason the port exists.\n *\n * @param key - where to store the value\n * @returns a store, or one that remembers nothing outside a browser\n *\n * @experimental\n */\nexport function browserStore(key: string): PreferenceStore {\n  if (typeof window === \"undefined\") return NO_STORAGE;\n  return {\n    read: () => window.localStorage.getItem(key),\n    write: value => window.localStorage.setItem(key, value),\n  };\n}\n\n/**\n * Where the shell keeps its preferences in a browser: under this key alone\n * for a host that names no user, and under this key and the user's id for\n * one that does.\n */\nexport const SHELL_PREFERENCES_KEY = \"nextly.builder.shell\";\n\n/**\n * The shell's preferences for one signed-in user, so two authors of one\n * browser each get their own panel, device and zoom.\n *\n * Preferences stored before they were kept per user sit under the shell's key\n * alone. The first user to load with no record of their own takes that one:\n * it is copied to theirs and removed, so it moves once and no second author\n * inherits it. With no user known (signed out, or not read yet), nothing is\n * remembered: a record kept for a guess would be read by the wrong author.\n *\n * A host whose browser storage follows rules of its own (a site that keeps\n * nothing on the device, a sign-out that forgets the person) passes `open`,\n * which opens the store under a key; both records, the user's and the one\n * from before, are then read, written and removed through it alone.\n *\n * @param userId - the signed-in user's id, or null when there is none\n * @param open - opens the store under a key; this browser's storage by default\n * @returns a store under that user's own key, or one that keeps nothing\n */\nexport function userPreferenceStore(\n  userId: string | null,\n  open: OpenPreferenceStore = browserStoreWithRemove\n): PreferenceStore {\n  if (userId === null || typeof window === \"undefined\") return NO_STORAGE;\n  // One store per user and opener, so a host may call this on every render:\n  // a new object would read as a different store, and the shell would reload.\n  let stores = userStores.get(open);\n  if (stores === undefined) {\n    stores = new Map();\n    userStores.set(open, stores);\n  }\n  const cached = stores.get(userId);\n  if (cached !== undefined) return cached;\n  const store = keyedStore(userId, open);\n  stores.set(userId, store);\n  return store;\n}\n\n/**\n * Opens the store under a key, one that can also remove what it holds: the\n * record from before is removed once moved, so no second author inherits it.\n *\n * @experimental\n */\nexport type OpenPreferenceStore = (\n  key: string\n) => PreferenceStore & { remove: () => void };\n\n/** {@link browserStore}, able to remove what it holds. */\nfunction browserStoreWithRemove(\n  key: string\n): PreferenceStore & { remove: () => void } {\n  return {\n    ...browserStore(key),\n    remove: () => window.localStorage.removeItem(key),\n  };\n}\n\n/** The stores handed out so far, by opener and then user id. */\nconst userStores = new WeakMap<\n  OpenPreferenceStore,\n  Map<string, PreferenceStore>\n>();\n\n/** The store under one user's own key, moving the earlier record on a read. */\nfunction keyedStore(\n  userId: string,\n  open: OpenPreferenceStore\n): PreferenceStore {\n  const own = open(`${SHELL_PREFERENCES_KEY}:${userId}`);\n  const before = open(SHELL_PREFERENCES_KEY);\n  return {\n    read: () => {\n      const kept = own.read();\n      if (kept !== null) return kept;\n      const moved = before.read();\n      if (moved === null) return null;\n      own.write(moved);\n      before.remove();\n      return moved;\n    },\n    write: own.write,\n  };\n}\n\n/**\n * A stored layout, accepted only if every entry is a usable percentage.\n *\n * Partial acceptance would be worse than rejection here: the library\n * distributes a layout across ALL panels, so a map missing one or carrying a\n * `NaN` resolves to a layout nobody chose. The whole map is the unit.\n */\n/**\n * The stored layouts, dropping any entry that is not usable.\n *\n * Per-topology rather than all-or-nothing: a layout written under a panel set\n * that no longer exists should cost the author only that arrangement's widths,\n * not the ones they set for every other arrangement.\n */\nfunction readLayouts(value: unknown): Record<string, Record<string, number>> {\n  if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n    return {};\n  }\n  const layouts: Record<string, Record<string, number>> = {};\n  for (const [key, layout] of Object.entries(value)) {\n    if (isLayout(layout)) layouts[key] = layout;\n  }\n  return layouts;\n}\n\nfunction isLayout(value: unknown): value is Record<string, number> {\n  if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n    return false;\n  }\n  const entries = Object.entries(value as Record<string, unknown>);\n  return (\n    entries.length > 0 &&\n    entries.every(\n      ([, size]) =>\n        typeof size === \"number\" && Number.isFinite(size) && size > 0\n    )\n  );\n}\n\n/**\n * Preferences parsed from whatever was stored.\n *\n * Every field is validated separately and falls back on its own, rather than\n * one malformed field discarding the rest. A layout written under a different\n * panel set should reset the layout, not the author's panel choice with it.\n *\n * Only the fields named below are read, and anything else in the record is\n * ignored rather than refused. Records written by an earlier release still\n * carry `showEmptyElements`, a preference that no longer exists because empty\n * containers are always shown: such a record loads whole, without it.\n */\nexport function readPreferences(store: PreferenceStore): ShellPreferences {\n  let raw: unknown;\n  try {\n    const stored = store.read();\n    if (stored === null) return DEFAULT_PREFERENCES;\n    raw = JSON.parse(stored);\n  } catch {\n    // Unreadable storage or malformed JSON. Neither says anything about the\n    // author's intent, so the defaults are the honest answer.\n    return DEFAULT_PREFERENCES;\n  }\n\n  if (typeof raw !== \"object\" || raw === null) return DEFAULT_PREFERENCES;\n  const record = raw as Record<string, unknown>;\n\n  const left = storedPanel(record.leftPanel, record.leftPanelView);\n  return {\n    leftPanel: left.panel,\n    leftPanelView: left.view,\n    leftPinned:\n      typeof record.leftPinned === \"boolean\"\n        ? record.leftPinned\n        : DEFAULT_PREFERENCES.leftPinned,\n    // Shown unless a stored true says otherwise, which covers every\n    // preference written before a side could be hidden.\n    leftPanelHidden: record.leftPanelHidden === true,\n    inspectorHidden: record.inspectorHidden === true,\n    // Fit when the stored value is not a zoom, which includes every preference\n    // written before there was one. `readZoom` refuses a scale outside the\n    // bounds rather than painting the canvas somewhere the control cannot be\n    // reached to undo it.\n    zoom: readZoom(record.zoom) ?? DEFAULT_PREFERENCES.zoom,\n    editingBreakpoint:\n      typeof record.editingBreakpoint === \"string\" &&\n      record.editingBreakpoint !== \"\"\n        ? record.editingBreakpoint\n        : DEFAULT_PREFERENCES.editingBreakpoint,\n    layouts: readLayouts(record.layouts),\n    // On unless a stored false says otherwise, which covers every preference\n    // written before the keys could be turned off.\n    panelKeys: record.panelKeys !== false,\n    sectionFolds: readSectionFolds(record.sectionFolds),\n    // Off unless a stored true says otherwise.\n    showCssNames: record.showCssNames === true,\n  };\n}\n\n/** Preferences written back, ignoring a store that refuses. */\nexport function writePreferences(\n  store: PreferenceStore,\n  preferences: ShellPreferences\n): void {\n  try {\n    // Narrowed on the way out for the reason it is checked on the way in: what\n    // is stored is a value this can read back, not whatever shape the running\n    // editor happens to hold.\n    store.write(\n      JSON.stringify({ ...preferences, zoom: writeZoom(preferences.zoom) })\n    );\n  } catch {\n    // A quota error or unavailable storage. Losing a panel width is not worth\n    // interrupting an edit over, and there is nothing the author could do.\n  }\n}\n\n/**\n * The panel a rail click selects.\n *\n * Clicking the open panel's own rail item CLOSES it. That is what makes the\n * rail a toggle rather than a one-way switch, and it is the only way to reach a\n * full-width canvas without a separate control.\n */\nexport function panelAfterRailClick(\n  current: LeftPanel | null,\n  clicked: LeftPanel\n): LeftPanel | null {\n  return current === clicked ? null : clicked;\n}\n"],"mappings":";;AAsCA,MAAa,WAAuB,EAAE,MAAM,MAAM;;;;;;;;;;AAWlD,MAAa,aAAgC;CAAC;CAAK;CAAM;CAAG;CAAK;AAAC;;;;;;;;;AAUlE,MAAa,WAAW;AACxB,MAAa,WAAW;;;;;;;;;;;;AAaxB,SAAgB,YAAY,OAA8B;CACxD,IAAI,CAAC,OAAO,SAAS,KAAK,GAAG,OAAO;CACpC,OAAO,QAAA,OAAoB,QAAA,IAAmB,OAAO;AACvD;;;;;;;;AASA,SAAgB,SAAS,OAAmC;CAC1D,IAAI,UAAU,OAAO,OAAO;CAC5B,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,MAAM,QAAQ,YAAY,KAAK;CAC/B,OAAO,UAAU,OAAO,OAAO;EAAE,MAAM;EAAS;CAAM;AACxD;;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