Vandrite Docs

Editor API

Interact with the text editor

Editor API

The Editor API (app.editor) provides methods to interact with the text editor, including inserting text, getting selections, and formatting content.

Inserting Text

// Insert at cursor position
this.app.editor.insertText("Hello World");

// Insert at specific position
this.app.editor.insertText("Hello", { line: 5, ch: 0 });

Getting Content

// Get all content
const content = this.app.editor.getContent();

// Get selected text
const selection = this.app.editor.getSelectedText();

// Check if there's a selection
const hasSelection = this.app.editor.hasSelection();

Selection

// Get selection range
const range = this.app.editor.getSelection();
// { start: { line: 0, ch: 5 }, end: { line: 0, ch: 10 } }

// Set selection
this.app.editor.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 10 });

// Select all
this.app.editor.selectAll();

Formatting

// Toggle formatting
this.app.editor.toggleBold(); // **text**
this.app.editor.toggleItalic(); // *text*
this.app.editor.toggleCode(); // `text`
this.app.editor.toggleStrikethrough(); // ~~text~~

// Headings
this.app.editor.setHeading(1); // # Heading
this.app.editor.setHeading(2); // ## Heading
this.app.editor.setHeading(3); // ### Heading

// Lists
this.app.editor.toggleBulletList(); // - item
this.app.editor.toggleNumberedList(); // 1. item
this.app.editor.toggleCheckbox(); // - [ ] item

// Quotes and code blocks
this.app.editor.toggleBlockquote(); // > quote
this.app.editor.toggleCodeBlock(); // ```code```

Cursor Position

// Get cursor position
const pos = this.app.editor.getCursor();
// { line: 5, ch: 10 }

// Set cursor position
this.app.editor.setCursor({ line: 0, ch: 0 });

// Scroll to line
this.app.editor.scrollToLine(100);

Statistics

// Get document statistics
const wordCount = this.app.editor.getWordCount();
const charCount = this.app.editor.getCharacterCount();
const lineCount = this.app.editor.getLineCount();

Replace Content

// Replace selection
this.app.editor.replaceSelection("new text");

// Replace range
this.app.editor.replaceRange(
  "replacement",
  { line: 0, ch: 5 },
  { line: 0, ch: 10 }
);

// Replace all content
this.app.editor.setContent("# New Document\n\nContent here...");

API Reference

MethodParametersReturnsDescription
getContent()-stringGet all editor content
setContent(text)stringvoidSet editor content
insertText(text, pos?)string, Position?voidInsert text
getSelectedText()-stringGet selected text
hasSelection()-booleanCheck for selection
replaceSelection(text)stringvoidReplace selection
getCursor()-PositionGet cursor position
setCursor(pos)PositionvoidSet cursor position
getSelection()-RangeGet selection range
setSelection(from, to)Position, PositionvoidSet selection
getWordCount()-numberCount words
getCharacterCount()-numberCount characters
getLineCount()-numberCount lines
isActive()-booleanCheck if editor is active
toggleBold()-voidToggle bold formatting
toggleItalic()-voidToggle italic formatting
setHeading(level)numbervoidSet heading level
scrollToLine(line)numbervoidScroll to line

Examples

Word Counter Status

async onload() {
  this.registerEvent(
    this.app.events.on('editor-change', () => {
      const words = this.app.editor.getWordCount();
      this.updateStatusBar(`${words} words`);
    })
  );
}

Text Transformer

this.addCommand({
  id: "my-plugin:uppercase",
  name: "Transform to Uppercase",
  callback: () => {
    const selected = this.app.editor.getSelectedText();
    if (selected) {
      this.app.editor.replaceSelection(selected.toUpperCase());
    }
  },
});

Insert Template

this.addCommand({
  id: "my-plugin:insert-date",
  name: "Insert Current Date",
  callback: () => {
    const date = new Date().toLocaleDateString();
    this.app.editor.insertText(date);
  },
});

Smart List Continuation

this.registerEvent(
  this.app.events.on("editor-keydown", (event) => {
    if (event.key === "Enter") {
      const cursor = this.app.editor.getCursor();
      const line = this.app.editor.getLine(cursor.line);

      if (line.match(/^- \[ \] /)) {
        event.preventDefault();
        this.app.editor.insertText("\n- [ ] ");
      }
    }
  })
);

On this page