obsidian#EditorRange TypeScript Examples

The following examples show how to use obsidian#EditorRange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From nldates-obsidian with MIT License 8 votes vote down vote up
export default function getWordBoundaries(editor: any): EditorRange {
  const cursor = editor.getCursor();

  let word;

  if (editor.cm instanceof window.CodeMirror) {
    // CM5
    const line = cursor.line;
    word = editor.cm.findWordAt({
      line: line,
      ch: cursor.ch,
    });
    const wordStart = word.anchor.ch;
    const wordEnd = word.head.ch;

    return {
      from: {
        line: line,
        ch: wordStart,
      },
      to: {
        line: line,
        ch: wordEnd,
      },
    };
  } else {
    // CM6
    const pos = editor.posToOffset(cursor);
    word = editor.cm.state.wordAt(pos);
    const wordStart = editor.offsetToPos(word.from);
    const wordEnd = editor.offsetToPos(word.to);
    return {
      from: wordStart,
      to: wordEnd,
    };
  }
}