Extract getCursorsFromEditor

This commit is contained in:
Andras Schmelczer 2025-06-07 21:51:14 +01:00
parent 4f691b33a4
commit 14db4bf240
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 27 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import type {
} from "sync-client";
import { lineAndColumnToPosition } from "./utils/line-and-column-to-position";
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
import { getCursorsFromEditor } from "./utils/get-cursors-from-editor";
export class ObsidianFileSystemOperations implements FileSystemOperations {
public constructor(
@ -78,26 +79,19 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
if (view?.file?.path === path) {
const text = view.editor.getValue();
const cursors = view.editor
.listSelections()
.flatMap(({ anchor, head }, i) => [
const cursors = getCursorsFromEditor(view.editor).flatMap(
({ id, start: anchor, end: head }) => [
{
id: 2 * i,
characterPosition: lineAndColumnToPosition(
text,
anchor.line,
anchor.ch
)
id: 2 * id,
characterPosition: anchor
},
{
id: 2 * i + 1,
characterPosition: lineAndColumnToPosition(
text,
head.line,
head.ch
)
id: 2 * id + 1,
characterPosition: head
}
]);
]
);
const result = updater({
text,

View file

@ -0,0 +1,17 @@
import { Editor } from "obsidian";
import { lineAndColumnToPosition } from "./line-and-column-to-position";
export interface Cursor {
id: number;
start: number;
end: number;
}
export function getCursorsFromEditor(editor: Editor): Cursor[] {
const text = editor.getValue();
return editor.listSelections().map(({ anchor, head }, i) => ({
id: i,
start: lineAndColumnToPosition(text, anchor.line, anchor.ch),
end: lineAndColumnToPosition(text, head.line, head.ch)
}));
}