Skip to content

About views and registries

Falcion edited this page Jan 26, 2025 · 4 revisions

Note

This page is a work-in-progress. Some topics might be incomplete due to limited information. Please keep this in mind while reading.

Reference List

View (& Extensions) Registry

The view registry, referenced as viewRegistry in Obsidian's runtime API, provides several useful functions. One notable feature is its ability to "read" everything related to views and their extensions (types).

  • Hidden State of View Registry (Field _):

    Contains three types of view-extensions: "extensions updated," "view-registered," and "view-unregistered," each storing relevant data.

Note

Their type is [{}], so to access the data, use [0].

Each contains various instances with names and more. More details will be added to the API module as .d.ts files are specified.

Here is an example of viewRegistry._['extensions-updated'] in the Developer Console:

preview

Through these parameters, you can access leafs, parent methods, functions, files, etc. Keep in mind, the view registry's subdata state can already be "ejected."

Important

While "extensions-updated" provides ample information on the view registry, other data types ("view-registered" and "view-unregistered") often contain empty objects.

  • Accessing "Dictionaries" in the View Registry:

    The view registry has two main dictionaries: one maps the view by type (name) with keys for view type and values as factory functions (or delegates).

    view-by-type

    The second is a dictionary where the key is a file extension, and the value is the assigned view type as a string:

    {
        "md": "markdown",
        "bmp": "image",
        "png": "image",
        "jpg": "image",
        "jpeg": "image",
        "gif": "image",
        "svg": "image",
        "webp": "image",
        "avif": "image",
        "mp3": "audio",
        "wav": "audio",
        "m4a": "audio",
        "3gp": "audio",
        "flac": "audio",
        "ogg": "audio",
        "oga": "audio",
        "opus": "audio",
        "mp4": "video",
        "webm": "video",
        "ogv": "video",
        "mov": "video",
        "mkv": "video",
        "pdf": "pdf",
        "canvas": "canvas"
    }
  • Various Methods:

    The view registry provides methods to register extensions for views, register views by extension, check if an extension is registered, and more.

Views

Views are essential to Obsidian’s ecosystem, defining how content is displayed. Examples include the file explorer, graph view, and Markdown view. You can also create custom views for displaying plugin content.

Each view has a unique identifier. Using a constant, VIEW_TYPE_EXAMPLE, to identify the view is recommended:

  • getViewType() returns a unique view identifier.
  • getDisplayText() returns a human-friendly view name.
  • onOpen() is triggered when the view is opened in a new leaf and builds the view’s content.
  • onClose() cleans up resources when the view closes.

Register and clean up custom views in the plugin lifecycle:

import { Plugin, WorkspaceLeaf } from 'obsidian';
import { ExampleView, VIEW_TYPE_EXAMPLE } from './view';

export default class ExamplePlugin extends Plugin {
  async onload() {
    this.registerView(
      VIEW_TYPE_EXAMPLE,
      (leaf) => new ExampleView(leaf)
    );

    this.addRibbonIcon('dice', 'Activate view', () => {
      this.activateView();
    });
  }

  async onunload() {}

  async activateView() {
    const { workspace } = this.app;

    let leaf: WorkspaceLeaf | null = null;
    const leaves = workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE);

    if (leaves.length > 0) {
      leaf = leaves[0];
    } else {
      leaf = workspace.getRightLeaf(false);
      await leaf.setViewState({ type: VIEW_TYPE_EXAMPLE, active: true });
    }

    workspace.revealLeaf(leaf);
  }
}

The second argument to registerView() is a factory function that returns an instance of the view.

Warning

Do not manage view references in your plugin. Obsidian may call the view factory function multiple times. Avoid side effects, and use getLeavesOfType() to access view instances:

this.app.workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE).forEach((leaf) => {
    if (leaf.view instanceof ExampleView) {
        // Access your view instance.
    }
});

Default views

Obsidian contains "default views", meaning views assigned in the instance by default, there is a list of this views (with ids):

Note

More context about table of views, their extensions and ids see in specified page: https://github.com/Falcion/UNITADE.md/wiki/Views-and-their-extensions

Assignment (which data represented by view) ID
Markdown markdown
Images image
Audio audio
Video video
PDF pdf
Canvas (Obsidian) canvas

For extensions read next chapter, and keep in mind: you can assign custom views, even from different plugins (MAKE.md for example), but it is not guaranteed to work properly with given file.

Practical Note on Views

There is a significant difference between ItemView in the API example and TextFileView. The latter implements a plaintext-based, editable file view that can load and save with an editor.

Note

By default, this view only saves when closing. For auto-save, call this.requestSave() when content changes.

For custom text views, using super calls implements this auto-save.

While TextFileView is suited to text editing, ItemView is more abstract, designed for specialized views like canvases or graphs.

Registries

Extensions registry in UNITADE represents rendered and supported file extensions accessed by UNITADE, though unrelated to vanilla Obsidian.

By default, Obsidian recognizes the following file formats:

  1. Markdown: md
  2. Images: avif, bmp, gif, jpeg, jpg, png, svg, webp
  3. Audio: flac, m4a, mp3, ogg, wav, webm, 3gp
  4. Video: mkv, mov, mp4, ogv, webm
  5. PDF: pdf

Note

Audio and video format support depends on device codecs.

Obsidian stores notes as Markdown-formatted plain text files in a vault. Enabling “Options > Files and links > Detect all file extensions” allows viewing non-markdown files in the vault.

Two main registry policies are:

  1. Extensions are case-sensitive: Upper and lower case distinctions matter.
  2. Unique extensions: Duplicate extensions are not allowed in the registry.

Practical Note on Extensions

While Obsidian lacks direct classes, registries, or methods representing extensions, they remain essential components of its ecosystem.

Important

In Obsidian, an "extension" is the last part after the dot in a filename. Only the last item is recognized as the true extension.

With UNITADE and Obsidian’s API, entering the full extension is supported, though the file may not render as intended unless the extension is in the registry.

Clone this wiki locally