obsidian#EditorSelectionOrCaret TypeScript Examples

The following examples show how to use obsidian#EditorSelectionOrCaret. 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: custom-selection-handlers.ts    From obsidian-editor-shortcuts with MIT License 6 votes vote down vote up
insertLineBelowHandler: CustomSelectionHandler = (selections) => {
  const seenLines: number[] = [];
  let lineIncrement = 0;
  let processedPos: EditorSelectionOrCaret;

  return selections.reduce((processed, currentPos) => {
    const currentLine = currentPos.anchor.line;
    if (!seenLines.includes(currentLine)) {
      seenLines.push(currentLine);
      lineIncrement = 0;
      processedPos = currentPos;
    } else {
      lineIncrement++;
      processedPos = {
        anchor: {
          line: currentLine + lineIncrement,
          ch: currentPos.anchor.ch,
        },
      };
    }
    processed.push(processedPos);
    return processed;
  }, []);
}
Example #2
Source File: utils.ts    From obsidian-editor-shortcuts with MIT License 6 votes vote down vote up
withMultipleSelections = (
  editor: Editor,
  callback: EditorActionCallback,
  options: MultipleSelectionOptions = defaultMultipleSelectionOptions,
) => {
  // @ts-expect-error: Obsidian's Editor interface does not explicitly
  // include the CodeMirror cm object, but it is there when using the
  // legacy editor
  const { cm } = editor;

  const selections = editor.listSelections();
  let selectionIndexesToProcess: number[];
  let newSelections: EditorSelectionOrCaret[] = [];

  if (!options.repeatSameLineActions) {
    const seenLines: number[] = [];
    selectionIndexesToProcess = selections.reduce(
      (indexes, currSelection, currIndex) => {
        const currentLine = currSelection.head.line;
        if (!seenLines.includes(currentLine)) {
          seenLines.push(currentLine);
          indexes.push(currIndex);
        }
        return indexes;
      },
      [],
    );
  }

  const applyCallbackOnSelections = () => {
    for (let i = 0; i < selections.length; i++) {
      // Controlled by repeatSameLineActions
      if (selectionIndexesToProcess && !selectionIndexesToProcess.includes(i)) {
        continue;
      }

      // Can't reuse selections variable as positions may change on each iteration
      const selection = editor.listSelections()[i];

      // Selections may disappear (e.g. running delete line for two cursors on the same line)
      if (selection) {
        const newSelection = callback(editor, selection, options.args);
        newSelections.push(newSelection);
      }
    }

    if (options.customSelectionHandler) {
      newSelections = options.customSelectionHandler(newSelections);
    }
    editor.setSelections(newSelections);
  };

  if (cm && cm.operation) {
    // Group all the updates into one atomic operation (so undo/redo work as expected)
    cm.operation(applyCallbackOnSelections);
  } else {
    // Safe fallback if cm doesn't exist (so undo/redo will step through each change)
    console.debug('cm object not found, operations will not be buffered');
    applyCallbackOnSelections();
  }
}