components/workspace-layout-state-component
Keeps a plugin’s layout-scoped state in the workspace layout itself, so each saved workspace remembers its own value and switching workspaces restores it, instead of one global value in data.json.
Read from Obsidian 1.15.1: Workspace.getLayout() builds a fresh object (main, left, right, left-ribbon, floating, active) and Workspace.setLayout() ignores every other top-level key. deserializeLayout keeps each item’s id, popout leaves included. The Workspaces core plugin saves through getLayout() and loads through changeLayout(), which calls setLayout(). Startup goes through loadLayout(), readWorkspaceFile() and setLayout(), after the plugins enabled at startup have loaded. So two patches on the app.workspace instance carry the whole thing: getLayout writes the records under WORKSPACE_LAYOUT_STATE_LAYOUT_KEY, and setLayout replaces them from the layout it is handed before any view is restored. Per-leaf state is keyed by leaf id at the layout level, so no WorkspaceItem.serialize or deserializeLayout patch is needed.
A view the plugin owns needs none of this: View.getState / setState already ride in each leaf’s state. This component is for the scope those methods cannot represent: the whole layout, and leaves of views the plugin does not own.
Every plugin bundles its own copy of this library, so the records, the subscriptions and ONE installation of the patches live in the shared-state bag under a versioned key, owned by one subscriber. When the owner unloads while others remain, the next subscriber installs again from its own copy. What crosses that boundary is a wire format between copies of different versions: plain objects and plain functions only, fields only ever added, and a change of meaning moves to a new key. The layout record is a wire format too, persisted in workspace.json and in every saved workspace.
Classes
Section titled “Classes”| Class | Description |
|---|---|
| WorkspaceLayoutStateComponent | Keeps a plugin’s layout-scoped values in the workspace layout, so saved workspaces each restore their own. Any number of these, in any number of plugins, share one installation per App. Values are restored when a layout loads, which REPLACES every value: write them after Workspace.onLayoutReady, and re-read them in onLayoutLoad. |
Interfaces
Section titled “Interfaces”| Interface | Description |
|---|---|
| WorkspaceLayoutStateChange | A change of one key’s value, in one scope. |
| WorkspaceLayoutStateComponentConstructorParams | Parameters for WorkspaceLayoutStateComponent. |
| WorkspaceLayoutStateKey | A typed handle to one layout-scoped key. Every method takes an optional leaf. Without one, the value belongs to the whole layout (the workspace scope). |
| WorkspaceLayoutStateKeyDefinition | Defines a layout-scoped key. |
| WorkspaceLayoutStatePluginRecord | One plugin’s records. Fields a newer copy adds are preserved. |
| WorkspaceLayoutStateRecord | The record persisted under WORKSPACE_LAYOUT_STATE_LAYOUT_KEY. Fields a newer copy adds are preserved. |
Functions
Section titled “Functions”| Function | Description |
|---|---|
| parseWorkspaceLayoutStateRecord | Parses a persisted record. A part that is malformed is dropped one level at a time: a malformed record reads as empty, a malformed plugin record drops only that plugin, and a malformed leaf bucket drops only that leaf. Fields this copy does not know are kept. |
| serializeWorkspaceLayoutStateRecord | Builds the record to persist into a serialized layout: leaf buckets for leaves absent from the layout are left out, and so are plugins with nothing left. |
Variables
Section titled “Variables”| Variable | Description |
|---|---|
| WORKSPACE_LAYOUT_STATE_LAYOUT_KEY | The top-level key of a serialized layout that holds the records. |