obsidian-api icon indicating copy to clipboard operation
obsidian-api copied to clipboard

Expose an API to handle multiple cursors programmatically

Open MrLokans opened this issue 3 years ago • 3 comments

I am trying to develop a functionality similar to pycharm's or sublime text's multiple selections (when you select some text, press hotkey and capture the following occurrences of the text, getting a way to edit multiple instances of the text).

Multiple cursor functionality is available in the Obsidian (https://help.obsidian.md/How+to/Working+with+multiple+cursors) from the UI but I see no quick way to achieve that programmatically.

The only way to create cursors is by using the setCursor method but it does not allow creating multiple cursors

        /** Set the cursor position. You can either pass a single {line, ch} object, or the line and the character as two separate parameters.
        Will replace all selections with a single, empty selection at the given position.
        The supported options are the same as for setSelection */
        setCursor(
            pos: CodeMirror.Position | number,
            ch?: number,
            options?: { bias?: number; origin?: string; scroll?: boolean },
        ): void;

this code sample

        editor.setCursor(
            { line: cursorStart.line, ch: cursorStart.ch + 2 },
        )
        editor.setCursor(
            { line: cursorStart.line, ch: cursorStart.ch + 5 },
        )
        editor.setCursor(
            { line: cursorStart.line, ch: cursorStart.ch + 8 },
        )

obviously just sets the cursor to the latest value.

Thanks

MrLokans avatar Apr 14 '21 13:04 MrLokans

@MrLokans I had the same exact request! Check https://discord.com/channels/686053708261228577/707816848615407697/831885870511554560 for the discussion I had with Licat.

SilentVoid13 avatar Apr 14 '21 13:04 SilentVoid13

Will add an API for this in the next release.

lishid avatar Apr 14 '21 13:04 lishid

@lishid thanks a lot!

MrLokans avatar Apr 15 '21 07:04 MrLokans