Skip to content

Workspace

Official

A workspace.

Import:

import { Workspace } from 'obsidian';

Signature:

export class Workspace extends Events

Extends: Events

Constructor

new Workspace(app: App, containerEl: HTMLElement)

Constructor.

To get the constructor instance, use getWorkspaceConstructor from obsidian-typings/implementations.

Properties

PropertyTypeDescription
_Record<string, EventsEntry[]>Internal storage of registered event handlers by event name.
(Inherited from Events)
_activeEditorMarkdownFileInfo | nullInternal reference to the active editor.
activeEditorMarkdownFileInfo | nullA component managing the current editor. This can be null if the active view has no editor.
activeLeafnull | WorkspaceLeafIndicates the currently focused leaf, if one exists.

Please avoid using activeLeaf directly, especially without checking whether activeLeaf is null.
activeTabGroupWorkspaceTabsCurrently active tab group.
appAppReference to the obsidian#App.
backlinkInDocument?unknownBacklink in document plugin instance, if enabled.
containerElHTMLElementThe container element of the workspace.
editorExtensionsExtension[]Registered CodeMirror editor extensions, to be applied to all CM instances.
editorSuggestEditorSuggestExThe global editor suggest manager for live autocomplete.
floatingSplitWorkspaceSplitThe workspace split that contains popout (floating) windows.
hoverLinkSourcesWorkspaceHoverLinkSourcesRecordRegistered sources for hover link previews.
initializedbooleanWhether the workspace has finished its initial layout load.
lastActiveFileTFileLast opened file in the vault.
lastTabGroupStackedbooleanWhether the last active tab group had stacked tabs enabled.
layoutItemQueueunknown[]Queue of layout items pending initialization.
layoutReadybooleanWhether the layout of the app has been successfully initialized. To react to the layout becoming ready, use Workspace.onLayoutReady
leftRibbonWorkspaceRibbonThe left ribbon of the workspace.
leftSidebarToggleButtonElHTMLElementButton element for toggling the left sidebar.
leftSplitWorkspaceMobileDrawer | WorkspaceSidedockThe left split of the workspace.
mobileFileInfosunknown[]Array of renderCallbacks
onLayoutReadyCallbacks?unknownCallbacks queued to run when the layout is ready.
operatorFuncConfigsRecord<string, unknown>Registered operator function configurations.
protocolHandlerWorkspaceProtocolHandlerProtocol handlers registered on the workspace.
recentFileTrackerRecentFileTrackerTracks last opened files in the vault.
requestSaveLayoutDebouncer<[], Promise<void>>Save the state of the current workspace layout.
rightRibbonWorkspaceRibbonThe right ribbon of the workspace.
rightSidebarToggleButtonElHTMLElementButton element for toggling the right sidebar.
rightSplitWorkspaceMobileDrawer | WorkspaceSidedockThe right split of the workspace.
rootSplitWorkspaceRootThe root split of the workspace.
scopeScopeKeyscope registered to the vault
undoHistoryStateHistory[]List of states that were closed and may be reopened.

Methods

MethodReturnsDescription
activeLeafEvents()voidChange active leaf and trigger leaf change event
addMobileFileInfo(file)voidAdd file to mobile file info
addRecentFile(file)voidAdd a file to the recent files list.
changeLayout(workspace)Promise<void>Change the layout of the workspace.
clearLayout()Promise<void>Clear layout of workspace and destruct all leaves
createLeafBySplit(leaf, direction?, before?)WorkspaceLeafCreate a leaf by a split.
createLeafInParent(parent, index)WorkspaceLeafCreate a leaf in a parent.
createLeafInTabGroup(tabs?)WorkspaceLeafCreate a leaf in the selected tab group or last used tab group.
deserializeLayout(leaf, ribbon?)Promise<WorkspaceLeaf>Deserialize workspace entries into actual Leaf objects.
detachLeavesOfType(viewType)voidRemove all leaves of the given type.
duplicateLeaf(leaf, direction?)Promise<WorkspaceLeaf>Duplicate a leaf.
duplicateLeaf(leaf, leafType, direction?)Promise<WorkspaceLeaf>Duplicate a leaf.
ensureSideLeaf(type, side, options?)Promise<WorkspaceLeaf>Get side leaf or create one if one does not exist.
focusLeaf(leaf)voidFocus on a specific leaf.
getActiveFile()TFile | nullReturns the file for the current view if it's a FileView. Otherwise, it will return the most recently active file.
getActiveFileView()FileView | nullGet active file view if exists.
getActiveLeafOfViewType(type)null | TGet the active leaf of the given view type.
getActiveViewOfType(type)T | nullGet the currently active view of a given type.
getAdjacentLeafInDirection(leaf, direction)null | WorkspaceLeafGet adjacent leaf in specified direction.
getDropDirection(e, rect, directions, leaf)'bottom' | 'center' | 'left' | 'right' | 'top'Get the direction where the leaf should be dropped on dragevent
getDropLocation(e)null | WorkspaceLeafGet the leaf where the leaf should be dropped on dragevent.
getFocusedContainer()WorkspaceSplitGet the workspace split for the currently focused container.
getGroupLeaves(group)WorkspaceLeaf[]Get all leaves that belong to a group.
getLastOpenFiles()string[]Get the filenames of the 10 most recently opened files.
getLayout()Record<string, unknown>Get the layout of the workspace.
getLeaf(newLeaf?, direction?)WorkspaceLeafCreates a new leaf in a leaf adjacent to the currently active leaf. If direction is 'vertical', the leaf will appear to the right. If direction is 'horizontal', the leaf will appear below the current leaf.
getLeaf(newLeaf?)WorkspaceLeafIf newLeaf is false (or not set) then an existing leaf which can be navigated is returned, or a new leaf will be created if there was no leaf available.

If newLeaf is 'tab' or true then a new leaf will be created in the preferred location within the root split and returned.

If newLeaf is 'split' then a new leaf will be created adjacent to the currently active leaf.

If newLeaf is 'window' then a popout window will be created with a new leaf inside.
getLeafById(id)WorkspaceLeaf | nullRetrieve a leaf by its id.
getLeavesOfType(viewType)TypedWorkspaceLeaf<ViewTypeViewMapping[TViewType]>[]Get leaves of a specific view type.
getLeftLeaf(split)WorkspaceLeaf | nullCreate a new leaf inside the left sidebar.
getMostRecentLeaf(root?)WorkspaceLeaf | nullGet the most recently active leaf in a given workspace root. Useful for interacting with the leaf in the root split while a sidebar leaf might be active.
getRecentFiles(options?)string[]Get n last opened files of type (defaults to 10).
getRightLeaf(split)WorkspaceLeaf | nullCreate a new leaf inside the right sidebar.
getSideLeaf(sideRibbon, split)WorkspaceLeafGet leaf in the side ribbon/dock and split if necessary.
getUnpinnedLeaf()WorkspaceLeafGet the unpinned leaf.
handleExternalLinkContextMenu(menu, linkText)voidHandle the context menu for an external URL link.
handleLinkContextMenu(menu, linkText, sourcePath, leaf?)voidHandle the context menu for an internal link.
handleXCallback(params, file)booleanHandler for x-callback-url actions.
hasUndoHistory()booleanCheck if there is any undo history available.
isAttached(leaf?)booleanCheck if leaf has been attached to the workspace
isInSidebar(leaf)booleanCheck if a leaf is in the sidebar.
isWorkspaceFocused()booleanChecks whether the workspace (the main window or a focused popout window) currently has focus.
iterateAllLeaves(callback)voidIterate through all leaves, including main area leaves, floating leaves, and sidebar leaves.
iterateCodeMirrors(_unused)voidIterate through all CodeMirror instances in the workspace.
iterateLeaves(split, callback)voidIterate the leaves of a split.
iterateRootLeaves(callback)voidIterate through all leaves in the main area of the workspace.
iterateTabs(tabs, cb)booleanIterate the tabs of a split till meeting a condition.
loadLayout()Promise<void>Load workspace from disk and initialize
moveLeafToPopout(leaf, data?)WorkspaceWindowMigrates this leaf to a new popout window. Only works on the desktop app.
off(name, callback)voidRemove an event listener.
(Inherited from Events)
offref(ref)voidRemove an event listener by reference.
(Inherited from Events)
on('quick-preview', callback, ctx?)EventRefTriggered when the active Markdown file is modified. React to file changes before they are saved to disk.
on('resize', callback, ctx?)EventRefTriggered when a WorkspaceItem is resized or the workspace layout has changed.
on('active-leaf-change', callback, ctx?)EventRefTriggered when the active leaf changes.
on('file-open', callback, ctx?)EventRefTriggered when the active file changes. The file could be in a new leaf, an existing leaf, or an embed.
on('layout-change', callback, ctx?)EventRefTriggered when the layout of the workspace changes.
on('window-open', callback, ctx?)EventRefTriggered when a new popout window is created.
on('window-close', callback, ctx?)EventRefTriggered when a popout window is closed.
on('css-change', callback, ctx?)EventRefTriggered when the CSS of the app has changed.
on('file-menu', callback, ctx?)EventRefTriggered when the user opens the context menu on a file.
on('files-menu', callback, ctx?)EventRefTriggered when the user opens the context menu with multiple files selected in the File Explorer.
on('url-menu', callback, ctx?)EventRefTriggered when the user opens the context menu on an external URL.
on('editor-menu', callback, ctx?)EventRefTriggered when the user opens the context menu on an editor.
on('editor-change', callback, ctx?)EventRefTriggered when changes to an editor has been applied, either programmatically or from a user event.
on('editor-paste', callback, ctx?)EventRefTriggered when the editor receives a paste event. Check for evt.defaultPrevented before attempting to handle this event, and return if it has been already handled. Use evt.preventDefault() to indicate that you've handled the event.
on('editor-drop', callback, ctx?)EventRefTriggered when the editor receives a drop event. Check for evt.defaultPrevented before attempting to handle this event, and return if it has been already handled. Use evt.preventDefault() to indicate that you've handled the event.
on('quit', callback, ctx?)EventRefTriggered when the app is about to quit. Not guaranteed to actually run. Perform some best effort cleanup here.
on('browser:update-history', callback, ctx?)EventRefTriggers when the browser history is updated.
on('canvas:edge-menu', callback, ctx?)EventRefTriggers when the user opens a context menu on a connection in the canvas.
on('canvas:node-connection-drop-menu', callback, ctx?)EventRefTriggers when the user drops edge connection to empty space in the canvas.
on('canvas:node-menu', callback, ctx?)EventRefTriggers when the user opens a context menu on a single node in the canvas.
on('canvas:selection-menu', callback, ctx?)EventRefTriggers when the user opens a context menu on a selection of multiple nodes in the canvas.
on('editor-selection-change', callback, ctx?)EventRefTriggers when the editor selection changes.
on('hover-link', callback, ctx?)EventRefTriggers when user hovers over any note link element (file explorer, editor, ...).
on('layout-ready', callback, ctx?)EventRefTriggers when workspace layout is loaded.
on('leaf-menu', callback, ctx?)EventRefTriggers when the leaf menu is opened.
on('markdown-scroll', callback, ctx?)EventRefTriggers when the markdown scroll event is fired.
on('markdown-viewport-menu', callback, ctx?)EventRefTriggers when the markdown viewport menu is opened.
on('receive-files-menu', callback, ctx?)EventRefTriggered when user shares files on mobile.
on('receive-text-menu', callback, ctx?)EventRefTriggered when user shares text on mobile.
on('search:results-menu', callback, ctx?)EventRefTriggers when user clicks on 'N results' button in search view.
on('swipe', callback, ctx?)EventRefTriggers when user swipes open left/right sidebar
on('tab-group-menu', callback, ctx?)EventRefCalled whenever user opens tab group menu (contains e.g. stacked tabs button)
on(name, callback, ctx?)EventRefAdd an event listener.
(Inherited from Events)
onDragLeaf(e, leaf)voidHandles drag event on leaf
onLayoutChange(leaf?)voidHandles layout change and saves layout to disk
onLayoutReady(callback)voidRuns the callback function right away if layout is already ready, or push it to a queue to be called later when layout is ready.
onQuickPreview(file, data)voidTrigger the quick preview event with the given arguments.
onResize()voidHandle a resize event on the workspace.
onStartLink(leaf)voidHandle the start of a link drag operation from a leaf.
openLinkText(linktext, sourcePath, newLeaf?, openViewState?)Promise<void>Open a link text.
openPopout(data?)WorkspaceWindowOpen a leaf in a popup window.
openPopoutLeaf(data?)WorkspaceLeafOpen a new popout window with a single new leaf and return that leaf. Only works on the desktop app.
pushUndoHistory(leaf, parentID, rootID)voidPush leaf change to history
readWorkspaceFile()Promise<SerializedWorkspace>Read workspace layout data from disk.
recursiveGetTarget(e, leaf)null | WorkspaceTabsGet drag event target location
registerEditorExtension(extension)voidRegister a CodeMirror editor extension.
registerHoverLinkSource(key, source)voidRegisters hover link source
registerObsidianProtocolHandler(protocol, handler)voidRegisters Obsidian protocol handler
registerOperatorFuncConfigs(id, configs)voidRegister operator function configurations.
registerUriHook()voidConstructs hook for receiving URI actions
requestActiveLeafEvents()voidRequest execution of activeLeaf change events
requestLayoutChangeEvents()voidRequest execution of layout change events.
requestResize()voidRequest execution of resize event
requestUpdateLayout()voidRequest execution of layout update event
revealLeaf(leaf)Promise<void>Bring a given leaf to the foreground. If the leaf is in a sidebar, the sidebar will be uncollapsed. await this function to ensure your view has been fully loaded and is not deferred.
saveLayout()Promise<void>Save workspace layout to disk.
setActiveLeaf(leaf, params?)voidSets the active leaf.
setActiveLeaf(leaf, pushHistory, focus)voidSets the active leaf.
setLayout(data)Promise<void>Use deserialized layout data to reconstruct the workspace
splitActiveLeaf(direction?)WorkspaceLeafSplit the active leaf.
splitLeaf(leaf, newLeaf, direction?, before?)voidSplit leaves in specified direction
splitLeafOrActive(leaf?, direction?)WorkspaceLeafSplit provided leaf, or active leaf if none provided.
trigger(name, data?)voidTrigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
tryTrigger(evt, args)voidTry to trigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
unregisterEditorExtension(extension)voidUnregister a CodeMirror editor extension
unregisterHoverLinkSource(key)voidUnregister hover link source
unregisterObsidianProtocolHandler(protocol, handler)voidUnregister Obsidian protocol handler
unregisterOperatorFuncConfigs(id)voidUnregister operator function configurations.
unsetActiveEditor(editor)voidUnset the active editor.
updateFrameless()voidUpdate the frameless window chrome (title bar) display.
updateLayout()voidInvoke workspace layout update, redraw and save
updateMobileVisibleTabGroup()voidUpdate visibility of tab group
updateOptions()voidCalling this function will update/reconfigure the options of all Markdown views. It is fairly expensive, so it should not be called frequently.
updateTitle()voidUpdate the internal title of the application.

Links to this page: