Send cursors instantly
This commit is contained in:
parent
57b2b76932
commit
f4c77ddd25
2 changed files with 87 additions and 9 deletions
|
|
@ -0,0 +1,17 @@
|
|||
import type { Editor } from "obsidian";
|
||||
import { lineAndColumnToPosition } from "../../utils/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)
|
||||
}));
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
import {
|
||||
EventRef,
|
||||
Workspace,
|
||||
Editor,
|
||||
MarkdownView,
|
||||
MarkdownFileInfo
|
||||
} from "obsidian";
|
||||
import { SyncClient } from "sync-client";
|
||||
import { Cursor, getCursorsFromEditor } from "./get-cursors-from-editor";
|
||||
|
||||
export class LocalCursorUpdateListener {
|
||||
private static readonly UPDATE_INTERVAL_MS = 50;
|
||||
private readonly eventHandle: NodeJS.Timeout;
|
||||
private lastCursorState: Record<string, Cursor[]> = {};
|
||||
|
||||
public constructor(
|
||||
private readonly client: SyncClient,
|
||||
private readonly workspace: Workspace
|
||||
) {
|
||||
this.eventHandle = setInterval(
|
||||
() => this.updateAllCursors(),
|
||||
LocalCursorUpdateListener.UPDATE_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
|
||||
private updateAllCursors(): void {
|
||||
const currentCursors = this.getAllCursors();
|
||||
if (
|
||||
JSON.stringify(this.lastCursorState) ===
|
||||
JSON.stringify(currentCursors)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
this.lastCursorState = currentCursors;
|
||||
this.client.updateLocalCursors(currentCursors);
|
||||
}
|
||||
|
||||
private getAllCursors(): Record<string, Cursor[]> {
|
||||
const cursors: Record<string, Cursor[]> = {};
|
||||
this.workspace
|
||||
.getLeavesOfType("markdown")
|
||||
.map((leaf) => leaf.view)
|
||||
.filter((view) => view instanceof MarkdownView)
|
||||
.forEach((view) => {
|
||||
const { file } = view;
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
cursors[file.path] = getCursorsFromEditor(view.editor);
|
||||
});
|
||||
return cursors;
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
clearInterval(this.eventHandle);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue