Add API for propagating cursor locations #61
2 changed files with 27 additions and 16 deletions
Extract getCursorsFromEditor
commit
14db4bf240
|
|
@ -7,6 +7,7 @@ import type {
|
||||||
} from "sync-client";
|
} from "sync-client";
|
||||||
import { lineAndColumnToPosition } from "./utils/line-and-column-to-position";
|
import { lineAndColumnToPosition } from "./utils/line-and-column-to-position";
|
||||||
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
|
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
|
||||||
|
import { getCursorsFromEditor } from "./utils/get-cursors-from-editor";
|
||||||
|
|
||||||
export class ObsidianFileSystemOperations implements FileSystemOperations {
|
export class ObsidianFileSystemOperations implements FileSystemOperations {
|
||||||
public constructor(
|
public constructor(
|
||||||
|
|
@ -78,26 +79,19 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
|
||||||
|
|
||||||
if (view?.file?.path === path) {
|
if (view?.file?.path === path) {
|
||||||
const text = view.editor.getValue();
|
const text = view.editor.getValue();
|
||||||
const cursors = view.editor
|
|
||||||
.listSelections()
|
const cursors = getCursorsFromEditor(view.editor).flatMap(
|
||||||
.flatMap(({ anchor, head }, i) => [
|
({ id, start: anchor, end: head }) => [
|
||||||
{
|
{
|
||||||
id: 2 * i,
|
id: 2 * id,
|
||||||
characterPosition: lineAndColumnToPosition(
|
characterPosition: anchor
|
||||||
text,
|
|
||||||
anchor.line,
|
|
||||||
anchor.ch
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2 * i + 1,
|
id: 2 * id + 1,
|
||||||
characterPosition: lineAndColumnToPosition(
|
characterPosition: head
|
||||||
text,
|
|
||||||
head.line,
|
|
||||||
head.ch
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
]);
|
]
|
||||||
|
);
|
||||||
|
|
||||||
const result = updater({
|
const result = updater({
|
||||||
text,
|
text,
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}));
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue