Skip to content

ObsidianWidgetType

Unofficial

Base class Obsidian puts between its own block widgets and the CodeMirror @codemirror/view#WidgetType.

Tracks the document range the widget stands for, owns the widget’s container element, and provides the shared action-bar, click-forwarding and resize plumbing used by embeds, tables and callouts.

Import:

import type { ObsidianWidgetType } from '@obsidian-typings/obsidian-public-latest';

Signature:

export interface ObsidianWidgetType extends WidgetType

Extends: WidgetType

Properties

PropertyTypeDescription
actionsElHTMLElement | nullElement holding the widget's action buttons, or null until the first action is added.
containerElHTMLElementContainer element built by ObsidianWidgetType.initDOM and cached by ObsidianWidgetType.toDOM.
endnumberEnd offset of the widget's range in the document, or -1 before ObsidianWidgetType.setPos is called.
resizeObserver?ResizeObserverObserver used by ObsidianWidgetType.resizeWidget, created on first use.
startnumberStart offset of the widget's range in the document, or -1 before ObsidianWidgetType.setPos is called.

Methods

MethodReturnsDescription
addAction(el, callback)thisAdd an action to the widget's action bar, creating the bar on first use.
addEditButton(view, el)voidAdd the standard edit button, which selects the widget's source range when clicked.
become(dom, widget)voidCalled when a previous DOM element created by a widget of the same type is about to be reused. Equivalent to updateDOM, but for when eq returns true.

Can be used as widget ownership transfer.
(Inherited from WidgetType)
coordsAt(dom, pos, side)null | RectOverride the way screen coordinates for positions at/in the widget are found.
(Inherited from WidgetType)
destroy(dom)voidThis is called when an instance of the widget is removed from the editor view.
(Inherited from WidgetType)
hookClickHandler(view, el)voidMake clicks on the element select the widget's source range, unless they land inside a nested editor.
ignoreEvent(event)booleanCan be used to configure which kinds of events inside the widget should be ignored by the editor. The default is to ignore all events.
(Inherited from WidgetType)
initDOM(view)HTMLElementBuild the widget's container element.
resizeWidget(view, el)voidRe-measure the editor whenever the given element resizes.
setOwner(el)voidRegister the widget as the owner of the given element, so that clicks on it can be routed back here.
setPos(start, end)voidRecord the document range the widget stands for.
toDOM(view)HTMLElementGet the widget's container element, building it via ObsidianWidgetType.initDOM on first call.

Links to this page: