Workspace class manages the workspace layout, including splits, leaves, and views. It is the central component for managing the UI layout of Obsidian.
Properties
The left sidebar split.Since: 0.9.7
The right sidebar split.Since: 0.9.7
The left ribbon (sidebar icons).Since: 0.9.7
The right ribbon.Deprecated: No longer used
The root split containing the main workspace area.Since: 0.9.7
Indicates the currently focused leaf, if one exists.Since: 0.9.7Deprecated: Use alternatives mentioned above
The container element for the workspace.Since: 0.9.7
Whether the layout of the app has been successfully initialized.To react to the layout becoming ready, use
onLayoutReady().Since: 0.9.7Save the state of the current workspace layout.Since: 0.16.0
A component managing the current editor. This can be null if the active view has no editor.
Methods
onLayoutReady()
Runs the callback function right away if layout is already ready, or pushes it to a queue to be called later when layout is ready.Function to call when layout is ready
Example
changeLayout()
Changes the workspace layout.The workspace layout configuration
Promise that resolves when the layout change is complete
getLayout()
Gets the current workspace layout configuration.The current layout configuration
createLeafInParent()
Creates a new leaf inside a parent split at a specific index.The parent split to create the leaf in
The index position for the new leaf
The newly created leaf
createLeafBySplit()
Creates a new leaf by splitting an existing leaf.The leaf to split
Direction to split (‘vertical’ or ‘horizontal’)
Whether to create the new leaf before or after the existing leaf
The newly created leaf
getLeaf()
Gets or creates a leaf for opening content.falseor not set: Return an existing leaf or create a new onetrueor'tab': Create a new leaf in the preferred location'split': Create a new leaf adjacent to the active leaf'window': Create a popout window with a new leaf
When
newLeaf is 'split':'vertical': New leaf appears to the right'horizontal': New leaf appears below
The leaf to use
Example
moveLeafToPopout()
Migrates a leaf to a new popout window. Only works on desktop.The leaf to move to a popout window
Optional window initialization data (position and size)
The created workspace window
openPopoutLeaf()
Opens a new popout window with a single new leaf. Only works on desktop.Optional window initialization data (position and size)
The newly created leaf in the popout window
openLinkText()
Opens a link in the workspace.The link text to open
The path of the source file (for resolving relative links)
Where to open the link (see
getLeaf() for options)Additional view state options
Promise that resolves when the link is opened
setActiveLeaf()
Sets the active leaf in the workspace.The leaf to make active
Parameter object
focus: Whether to focus the leaf (default: true)
Example
getLeafById()
Retrieves a leaf by its ID.ID of the leaf to retrieve
The leaf with the given ID, or null if not found
getGroupLeaves()
Gets all leaves that belong to a group.Group ID
Array of leaves in the group
getMostRecentLeaf()
Gets the most recently active leaf in a given workspace root.Root for the leaves to search. If not provided, searches
rootSplit and pop-out windowsThe most recently active leaf, or null if none found
getLeftLeaf()
Creates a new leaf inside the left sidebar.Whether to split the existing sidebar
The created leaf, or null if failed
getRightLeaf()
Creates a new leaf inside the right sidebar.Whether to split the existing sidebar
The created leaf, or null if failed
ensureSideLeaf()
Gets a side leaf or creates one if it does not exist.The view type to ensure exists
Which side (‘left’ or ‘right’)
active: Whether to make the leaf activesplit: Whether to split if creating a new leafreveal: Whether to reveal the leafstate: Initial state for the view
The existing or newly created leaf
getActiveViewOfType()
Gets the currently active view of a given type.The constructor of the view type to retrieve
The active view of the specified type, or null if none
Example
getActiveFile()
Returns the file for the current view if it’s aFileView. Otherwise, returns the most recently active file.
The active file, or null if none
Example
iterateRootLeaves()
Iterates through all leaves in the main area of the workspace.Function to call for each leaf
Example
iterateAllLeaves()
Iterates through all leaves, including main area leaves, floating leaves, and sidebar leaves.Function to call for each leaf
getLeavesOfType()
Gets all leaves of a given type.The view type to search for
Array of leaves with the specified view type
detachLeavesOfType()
Removes all leaves of the given type.The view type to remove
revealLeaf()
Brings a given leaf to the foreground. If the leaf is in a sidebar, the sidebar will be uncollapsed.The leaf to reveal
Promise that resolves when the leaf is fully loaded and revealed
Await this function to ensure your view has been fully loaded and is not deferred.
getLastOpenFiles()
Gets the filenames of the 10 most recently opened files.Array of file paths (up to 10)
updateOptions()
Updates/reconfigures the options of all Markdown views.handleLinkContextMenu()
Adds a context menu to internal file links.The menu to add items to
The link text
The source file path
Optional leaf context
Whether the menu was handled
Events
TheWorkspace class extends Events and provides several events you can listen to:
quick-preview
Triggered when the active Markdown file is modified. React to file changes before they are saved to disk.resize
Triggered when aWorkspaceItem is resized or the workspace layout has changed.
active-leaf-change
Triggered when the active leaf changes.file-open
Triggered when the active file changes. The file could be in a new leaf, an existing leaf, or an embed.layout-change
Triggered when the workspace layout changes.window-open
Triggered when a new popout window is created.window-close
Triggered when a popout window is closed.css-change
Triggered when the CSS of the app has changed.file-menu
Triggered when the user opens the context menu on a file.files-menu
Triggered when the user opens the context menu with multiple files selected in the File Explorer.url-menu
Triggered when the user opens the context menu on an external URL.editor-menu
Triggered when the user opens the context menu on an editor.editor-change
Triggered when changes to an editor have been applied, either programmatically or from a user event.editor-paste
Triggered when the editor receives a paste event.Check for
evt.defaultPrevented before handling, and use evt.preventDefault() to indicate that you’ve handled the event.editor-drop
Triggered when the editor receives a drop event.Check for
evt.defaultPrevented before handling, and use evt.preventDefault() to indicate that you’ve handled the event.