Workspace
Official
A workspace.
Import:
import { Workspace } from 'obsidian';Signature:
export class Workspace extends EventsExtends: Events
Constructor
new Workspace(app: App, containerEl: HTMLElement)Constructor.
To get the constructor instance, use getWorkspaceConstructor from obsidian-typings/implementations.
Properties
| Property | Type | Description | |
|---|---|---|---|
| _ | Record<string, EventsEntry[]> | Internal storage of registered event handlers by event name. (Inherited from Events) | |
| _activeEditor | MarkdownFileInfo | null | Internal reference to the active editor. | |
| activeEditor | MarkdownFileInfo | null | A component managing the current editor. This can be null if the active view has no editor. | |
| activeLeaf | null | WorkspaceLeaf | Indicates the currently focused leaf, if one exists. Please avoid using activeLeaf directly, especially without checking whether activeLeaf is null. | |
| activeTabGroup | null | WorkspaceTabs | Currently active tab group. | |
| app | App | Reference to the obsidian#App. | |
| backlinkInDocument? | unknown | Backlink in document plugin instance, if enabled. | |
| containerEl | HTMLElement | The container element of the workspace. | |
| editorExtensions | Extension[] | Registered CodeMirror editor extensions, to be applied to all CM instances. | |
| editorSuggest | EditorSuggestEx | The global editor suggest manager for live autocomplete. | |
| floatingSplit | null | WorkspaceFloating | The workspace container that holds the popout (floating) windows. | |
| hoverLinkSources | WorkspaceHoverLinkSourcesRecord | Registered sources for hover link previews. | |
| initialized | boolean | Whether the workspace has finished its initial layout load. | |
| lastActiveFile | null | TFile | Last opened file in the vault, or null when no file has been opened yet. | |
| lastTabGroupStacked | boolean | Whether the last active tab group had stacked tabs enabled. | |
| layoutItemQueue | unknown[] | Queue of layout items pending initialization. | |
| layoutReady | boolean | Whether the layout of the app has been successfully initialized. To react to the layout becoming ready, use Workspace.onLayoutReady | |
| layoutReadyCallbacksDone | null | Promise<void> | Settles once every callback queued by Workspace.onLayoutReady before the layout was ready has run, awaiting those that return a promise. null until the layout is ready. | |
| leftRibbon | WorkspaceRibbon | The left ribbon of the workspace. | |
| leftSidebarToggleButtonEl | HTMLElement | Button element for toggling the left sidebar. | |
| leftSplit | WorkspaceMobileDrawer | WorkspaceSidedock | The left split of the workspace. | |
| mobileFileInfos | unknown[] | Array of renderCallbacks | |
| onLayoutReadyCallbacks? | unknown | Callbacks queued to run when the layout is ready. | |
| operatorFuncConfigs | Record<string, unknown> | Registered operator function configurations. | |
| protocolHandler | WorkspaceProtocolHandler | Protocol handlers registered on the workspace. | |
| recentFileTracker | RecentFileTracker | Tracks last opened files in the vault. | |
| requestSaveLayout | Debouncer<[], Promise<void>> | Save the state of the current workspace layout. | |
| rightRibbon | WorkspaceRibbon | The right ribbon of the workspace. | |
| rightSidebarToggleButtonEl | HTMLElement | Button element for toggling the right sidebar. | |
| rightSplit | WorkspaceMobileDrawer | WorkspaceSidedock | The right split of the workspace. | |
| rootSplit | WorkspaceRoot | The root split of the workspace. | |
| scope | WorkspaceScope | The workspace's key scope, a child of the app scope that delegates to the active leaf's view scope. | |
| undoHistory | StateHistory[] | List of states that were closed and may be reopened. |
Methods
| Method | Returns | Description | |
|---|---|---|---|
| activeLeafEvents() | void | Change active leaf and trigger leaf change event | |
| addMobileFileInfo(file) | void | Add file to mobile file info | |
| addRecentFile(file) | void | Add 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?) | WorkspaceLeaf | Create a leaf by a split. | |
| createLeafInParent(parent, index) | WorkspaceLeaf | Create a leaf in a parent. | |
| createLeafInTabGroup(tabs?) | WorkspaceLeaf | Create 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) | void | Remove 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) | void | Focus on a specific leaf. | |
| getActiveFile() | TFile | null | Returns the file for the current view if it's a FileView. Otherwise, it will return the most recently active file. | |
| getActiveFileView() | FileView | null | Get active file view if exists. | |
| getActiveLeafOfViewType(type) | null | T | Get the active leaf of the given view type. | |
| getActiveViewOfType(type) | T | null | Get the currently active view of a given type. | |
| getAdjacentLeafInDirection(leaf, direction) | null | WorkspaceLeaf | Get 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 | WorkspaceLeaf | Get the leaf where the leaf should be dropped on dragevent. | |
| getFocusedContainer() | WorkspaceSplit | Get 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?) | WorkspaceLeaf | Creates 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?) | WorkspaceLeaf | If 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 | null | Retrieve a leaf by its id. | |
| getLeavesOfType(viewType) | WorkspaceLeaf[] | Get all leaves of a given type. | |
| getLeavesOfType(viewType) | TypedWorkspaceLeaf<ViewTypeViewMapping[TViewType]>[] | Get leaves of a specific view type. Declared before the string overload, which would otherwise match every literal first. | |
| getLeftLeaf(split) | WorkspaceLeaf | null | Create a new leaf inside the left sidebar. | |
| getMostRecentLeaf(root?) | WorkspaceLeaf | null | Get 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 | null | Create a new leaf inside the right sidebar. | |
| getSideLeaf(sideRibbon, split) | WorkspaceLeaf | Get leaf in the side ribbon/dock and split if necessary. | |
| getUnpinnedLeaf() | WorkspaceLeaf | Get the unpinned leaf. | |
| handleExternalLinkContextMenu(menu, linkText) | void | Handle the context menu for an external URL link. | |
| handleLinkContextMenu(menu, linktext, sourcePath, leaf?) | boolean | Add a context menu to internal file links. | |
| handleLinkContextMenu(menu, linkText, sourcePath, leaf?) | void | Handle the context menu for an internal link. | |
| hasUndoHistory() | boolean | Check if there is any undo history available. | |
| isAttached(leaf?) | boolean | Check if leaf has been attached to the workspace | |
| isInSidebar(leaf) | boolean | Check if a leaf is in the sidebar. | |
| isWorkspaceFocused() | boolean | Checks whether the workspace (the main window or a focused popout window) currently has focus. | |
| iterateAllLeaves(callback) | void | Iterate through all leaves, including main area leaves, floating leaves, and sidebar leaves. | |
| iterateCodeMirrors(_unused) | void | Iterate through all CodeMirror instances in the workspace. | |
| iterateLeaves(split, callback) | void | Iterate the leaves of a split. | |
| iterateRootLeaves(callback) | void | Iterate through all leaves in the main area of the workspace. | |
| iterateTabs(tabs, cb) | boolean | Iterate the tabs of a split till meeting a condition. | |
| loadLayout() | Promise<void> | Load workspace from disk and initialize | |
| moveLeafToPopout(leaf, data?) | WorkspaceWindow | Migrates this leaf to a new popout window. Only works on the desktop app. | |
| off(name, callback) | void | Remove an event listener. (Inherited from Events) | |
| offref(ref) | void | Remove an event listener by reference. (Inherited from Events) | |
| on('quick-preview', callback, ctx?) | EventRef | Triggered when the active Markdown file is modified. React to file changes before they are saved to disk. | |
| on('resize', callback, ctx?) | EventRef | Triggered when a WorkspaceItem is resized or the workspace layout has changed. | |
| on('active-leaf-change', callback, ctx?) | EventRef | Triggered when the active leaf changes. | |
| on('file-open', callback, ctx?) | EventRef | Triggered when the active file changes. The file could be in a new leaf, an existing leaf, or an embed. Despite its name, this is a "focused file changed" event, not a "file was opened" one. It fires whenever Workspace.getActiveFile returns a different file than it did last time, so moving the focus to another leaf that already shows another file fires it too, while opening the file that is already active does not. The file is null when the newly active leaf shows no file. | |
| on('layout-change', callback, ctx?) | EventRef | Triggered when the layout of the workspace changes. | |
| on('window-open', callback, ctx?) | EventRef | Triggered when a new popout window is created. | |
| on('window-close', callback, ctx?) | EventRef | Triggered when a popout window is closed. | |
| on('css-change', callback, ctx?) | EventRef | Triggered when the CSS of the app has changed. | |
| on('file-menu', callback, ctx?) | EventRef | Triggered when the user opens the context menu on a file. | |
| on('files-menu', callback, ctx?) | EventRef | Triggered when the user opens the context menu with multiple files selected in the File Explorer. | |
| on('url-menu', callback, ctx?) | EventRef | Triggered when the user opens the context menu on an external URL. | |
| on('editor-menu', callback, ctx?) | EventRef | Triggered when the user opens the context menu on an editor. | |
| on('editor-change', callback, ctx?) | EventRef | Triggered when changes to an editor has been applied, either programmatically or from a user event. | |
| on('editor-paste', callback, ctx?) | EventRef | Triggered 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?) | EventRef | Triggered 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?) | EventRef | Triggered when the app is about to quit. Not guaranteed to actually run. Perform some best effort cleanup here. | |
| on('bookmarks:bookmarks-menu', callback, ctx?) | EventRef | Triggers when the context menu of a bookmark is opened in the Bookmarks view. | |
| on('browser:update-history', callback, ctx?) | EventRef | Triggers when the browser history is updated. | |
| on('canvas:edge-menu', callback, ctx?) | EventRef | Triggers when the user opens a context menu on a connection in the canvas. | |
| on('canvas:node-connection-drop-menu', callback, ctx?) | EventRef | Triggers when the user drops edge connection to empty space in the canvas. | |
| on('canvas:node-menu', callback, ctx?) | EventRef | Triggers when the user opens a context menu on a single node in the canvas. | |
| on('canvas:selection-menu', callback, ctx?) | EventRef | Triggers when the user opens a context menu on a selection of multiple nodes in the canvas. | |
| on('editor-selection-change', callback, ctx?) | EventRef | Triggers when the editor selection changes. | |
| on('hover-link', callback, ctx?) | EventRef | Triggers when user hovers over any note link element (file explorer, editor, ...). | |
| on('layout-ready', callback, ctx?) | EventRef | Triggers when workspace layout is loaded. | |
| on('leaf-menu', callback, ctx?) | EventRef | Triggers when the leaf menu is opened. | |
| on('markdown-properties-menu', callback, ctx?) | EventRef | Triggers when the properties menu of a note's metadata editor is opened. | |
| on('markdown-scroll', callback, ctx?) | EventRef | Triggers when the markdown scroll event is fired. | |
| on('markdown-viewport-menu', callback, ctx?) | EventRef | Triggers when the markdown viewport menu is opened. | |
| on('receive-files-menu', callback, ctx?) | EventRef | Triggered when user shares files on mobile. | |
| on('receive-text-menu', callback, ctx?) | EventRef | Triggered when user shares text on mobile. | |
| on('search:results-menu', callback, ctx?) | EventRef | Triggers when user clicks on 'N results' button in search view. | |
| on('swipe', callback, ctx?) | EventRef | Triggers when user swipes open left/right sidebar | |
| on('tab-group-menu', callback, ctx?) | EventRef | Called whenever user opens tab group menu (contains e.g. stacked tabs button) | |
| on(name, callback, ctx?) | EventRef | Add an event listener. (Inherited from Events) | |
| onDragLeaf(e, leaf) | void | Handles drag event on leaf | |
| onLayoutChange(leaf?) | void | Handles layout change and saves layout to disk | |
| onLayoutReady(callback) | void | Runs 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) | void | Trigger the quick preview event with the given arguments. | |
| onResize() | void | Handle a resize event on the workspace. | |
| onStartLink(leaf) | void | Handle the start of a link drag operation from a leaf. | |
| openLinkText(linktext, sourcePath, newLeaf?, openViewState?) | Promise<void> | Open a link text. | |
| openPopout(data?) | WorkspaceWindow | Open a leaf in a popup window. | |
| openPopoutLeaf(data?) | WorkspaceLeaf | Open a new popout window with a single new leaf and return that leaf. Only works on the desktop app. | |
| pushUndoHistory(leaf, parentID, rootID) | void | Push leaf change to history | |
| readWorkspaceFile() | Promise<SerializedWorkspace> | Read workspace layout data from disk. | |
| recursiveGetTarget(e, leaf) | null | WorkspaceTabs | Get drag event target location | |
| registerEditorExtension(extension) | void | Register a CodeMirror editor extension. | |
| registerHoverLinkSource(key, source) | void | Registers hover link source | |
| registerObsidianProtocolHandler(protocol, handler) | void | Registers Obsidian protocol handler | |
| registerOpenFileHandler() | void | Installs the desktop external-file open handler and processes a pending open request. | |
| registerOperatorFuncConfigs(id, configs) | void | Register operator function configurations. | |
| registerUriHook() | void | Constructs hook for receiving URI actions | |
| requestActiveLeafEvents() | void | Request execution of activeLeaf change events | |
| requestLayoutChangeEvents() | void | Request execution of layout change events. | |
| requestResize() | void | Request execution of resize event | |
| requestUpdateLayout() | void | Request 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?) | void | Sets the active leaf. | |
| setActiveLeaf(leaf, pushHistory, focus) | void | Sets the active leaf. | |
| setLayout(data) | Promise<void> | Use deserialized layout data to reconstruct the workspace | |
| splitActiveLeaf(direction?) | WorkspaceLeaf | Split the active leaf. | |
| splitLeaf(leaf, newLeaf, direction?, before?) | void | Split leaves in specified direction | |
| splitLeafOrActive(leaf?, direction?) | WorkspaceLeaf | Split provided leaf, or active leaf if none provided. | |
| trigger(name, data?) | void | Trigger an event, executing all the listeners in order even if some of them throw an error. (Inherited from Events) | |
| tryTrigger(evt, args) | void | Try to trigger an event, executing all the listeners in order even if some of them throw an error. (Inherited from Events) | |
| unregisterEditorExtension(extension) | void | Unregister a CodeMirror editor extension | |
| unregisterHoverLinkSource(key) | void | Unregister hover link source | |
| unregisterObsidianProtocolHandler(protocol, handler) | void | Unregister Obsidian protocol handler | |
| unregisterOperatorFuncConfigs(id) | void | Unregister operator function configurations. | |
| unsetActiveEditor(editor) | void | Unset the active editor. | |
| updateFrameless() | void | Update the frameless window chrome (title bar) display. | |
| updateLayout() | void | Invoke workspace layout update, redraw and save | |
| updateMobileVisibleTabGroup() | void | Update visibility of tab group | |
| updateOptions() | void | Calling this function will update/reconfigure the options of all Markdown views. It is fairly expensive, so it should not be called frequently. | |
| updateTitle() | void | Update the internal title of the application. |
Links to this page: