Skip to content

TextFileView

Official

This class implements a plaintext-based editable file view, which can be loaded and saved given an editor.

Note that by default, this view only saves when it’s closing. To implement auto-save, your editor should call this.requestSave() when the content is changed.

Import:

import { TextFileView } from 'obsidian';

Signature:

export class TextFileView extends EditableFileView

Extends: EditableFileView

Constructor

new TextFileView(leaf: WorkspaceLeaf)

Constructor.

To extract the constructor type, use ExtractConstructor<EditableFileView>.

Properties

PropertyTypeDescription
allowNoFilebooleanWhether the view may be run without an attached file..
(Inherited from EditableFileView)
datastringIn-memory data.
dirtybooleanWhether current file is dirty (different from saved contents).
filenull | TFileThe file that is currently being viewed.
(Inherited from EditableFileView)
fileBeingRenamednull | TFileThe file that is currently being renamed.
(Inherited from EditableFileView)
isPlaintextbooleanWhether editor should be rendered as plaintext.
lastSavedDatanull | stringThe data that was last saved.
navigationbooleanWhether the file view can be navigated.
(Inherited from EditableFileView)
requestSave() => voidDebounced save in 2 seconds from now
saveAgainbooleanWhether on saving, the file should be saved again (for dirtiness checks).
savingbooleanWhether the file is currently saving.

Methods

MethodReturnsDescription
canAcceptExtension(extension)booleanWhether the file view can accept an extension.
(Inherited from EditableFileView)
clear()voidClear the editor. This is usually called when we're about to open a completely different file, so it's best to clear any editor states like undo-redo history, and any caches/indexes associated with the previous file contents.
getDisplayText()stringGet the display text for the file view.
(Inherited from EditableFileView)
getState()Record<string, unknown>Get the state of the file view.
(Inherited from EditableFileView)
getSyncViewState()unknownGet view state for sync plugin.
(Inherited from EditableFileView)
getViewData()stringGets the data from the editor. This will be called to save the editor contents to the file.
loadFile(file)Promise<unknown>Loads the file with the onLoadFile function.
(Inherited from EditableFileView)
loadFileInternal(file, clear)Promise<unknown>Load a file into the editor, optionally clearing existing content.
onDelete(file)Promise<void>Updates the view if it contains the deleted file.
(Inherited from EditableFileView)
onload()voidCalled when the file view is loaded.
(Inherited from EditableFileView)
onLoadFile(file)Promise<void>On load file.
onModify(file)voidIs called when the vault has a 'modify' event. Reloads the file if the view is currently not saving the file and the modified file is the file in this view.
onRename(file)Promise<void>Called when the file is renamed.
(Inherited from EditableFileView)
onTitleBlur()Promise<void>Is called when the titleEl looses focus.

Event type: 'blur'.
(Inherited from EditableFileView)
onTitleChange(titleEl)voidIs called when the titleEl is changed.

Event type: 'input'.
(Inherited from EditableFileView)
onTitleFocus()voidIs called when the titleEl gains focus.

Event type: 'focus'.
(Inherited from EditableFileView)
onTitleKeydown(event)voidIs called when the titleEl is focused and a keydown is triggered.

Event type: 'keydown'.
(Inherited from EditableFileView)
onTitlePaste(titleEl, event)voidIs called when the titleEl is focused and a paste event is triggered.

Event type: 'paste'.
(Inherited from EditableFileView)
onUnloadFile(file)Promise<void>On unload file.
receiveSyncState(view)voidReceives sync state from another file view, opening the other view's file if it differs.
(Inherited from EditableFileView)
renderBreadcrumbs()voidRender the breadcrumb navigation for this file view.
(Inherited from EditableFileView)
save(clear?)Promise<void>Save the file.
saveImmediately()voidIf any changes(dirty = true) in the file forces the file to save.
saveTitle(titleEl)Promise<void>Updates the file to match the updated title.
(Inherited from EditableFileView)
setData(data, clear)voidSet the data to the editor. This is used to load the file contents.
setState(state, result)Promise<void>Set the state of the file view.
(Inherited from EditableFileView)
setViewData(data, clear)voidSet the data to the editor. This is used to load the file contents.

If clear is set, then it means we're opening a completely different file. In that case, you should call TextFileView.clear(), or implement a slightly more efficient clearing mechanism given the new data to be set.
syncState(e)Promise<unknown>Synchronize the view state with the sync plugin.
(Inherited from EditableFileView)

Links to this page: