View class is the abstract base class for all views in Obsidian. Views are the content displayed within workspace leaves.
Properties
The app instance.Since: 0.9.7
The icon for this view.Since: 1.1.0
Whether or not the view is intended for navigation.
- Set to
falsefor static views that are not intended to be navigated away (e.g., File explorer, calendar) - Set to
truefor views that open files or can be otherwise navigated (e.g., Markdown editor, Kanban view, PDF view)
The leaf containing this view.Since: 0.9.7
The container element for this view.Since: 0.9.7
An optional scope to register hotkeys for when the view is in focus.Default:
nullSince: 1.5.7Constructor
The workspace leaf this view will be attached to
Methods
onOpen()
Called when the view is opened. Override this to perform initialization.Promise that resolves when the view is ready
Example
onClose()
Called when the view is closed. Override this to perform cleanup.Promise that resolves when cleanup is complete
Example
getViewType()
Returns the unique type identifier for this view. This must be implemented by all views.The unique view type identifier
Example
getState()
Gets the current state of this view. Override this to save view state.The current view state
setState()
Sets the state of this view. Override this to restore view state.The state to set
Result object to indicate if state change should be recorded in history
Promise that resolves when state is set
getEphemeralState()
Gets ephemeral state (temporary state not saved to disk).The ephemeral state
setEphemeralState()
Sets ephemeral state (temporary state not saved to disk).The ephemeral state to set
getIcon()
Gets the icon for this view.The icon name for this view
onResize()
Called when the size of this view is changed. Override this to handle resize events.getDisplayText()
Gets the display text for this view (shown in the tab). This must be implemented by all views.The display text for this view
Example
onPaneMenu()
Populates the pane menu. Override this to add custom menu items.The menu to populate
The source of the menu request
Example
Lifecycle
Views follow this lifecycle:- Construction - The view is created with
new YourView(leaf) - Loading -
load()is called (inherited fromComponent) - Opening -
onOpen()is called - Active - The view is active and responsive
- Closing -
onClose()is called - Unloading -
unload()is called (inherited fromComponent)
Creating a Custom View
Here’s a complete example of creating a custom view:See Also
- ItemView - Extends View with a content area
- FileView - For views that display files
- TextFileView - For views that edit text files
- WorkspaceLeaf - Container for views