Skip to content

Commit

Permalink
feat: set line and switch to reading/preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Mar 30, 2023
1 parent d6776d1 commit 917a4eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
9 changes: 4 additions & 5 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class Handlers {
parameters: parameters,
});

this.plugin.setCursorInLine(parameters.line);
await this.plugin.setCursorInLine(parameters);
} else {
await this.plugin.open({
file: parameters.filepath,
Expand Down Expand Up @@ -178,7 +178,7 @@ export default class Handlers {
parameters: parameters,
});

this.plugin.setCursorInLine(parameters.line);
await this.plugin.setCursorInLine(parameters);
} else {
await this.plugin.open({
file: parameters.filepath,
Expand Down Expand Up @@ -369,14 +369,13 @@ export default class Handlers {
file: parameters.filepath,
setting: this.plugin.settings.openFileWithoutWriteInNewPane,
parameters: parameters,
mode: parameters.line != undefined ? "source" : undefined,
});
if (parameters.line != undefined) {
this.plugin.setCursorInLine(parameters.line);
await this.plugin.setCursorInLine(parameters);
}
}
if (parameters.mode != undefined) {
await this.plugin.setCursor(parameters.mode);
await this.plugin.setCursor(parameters);
}
if (parameters.uid) {
const view = app.workspace.getActiveViewOfType(MarkdownView);
Expand Down
53 changes: 33 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export default class AdvancedURI extends Plugin {
parameters,
});
if (parameters.line != undefined) {
this.setCursorInLine(parameters.line);
await this.setCursorInLine(parameters);
}
}
}
Expand Down Expand Up @@ -541,43 +541,56 @@ export default class AdvancedURI extends Plugin {
}
}

async setCursor(mode: Parameters["mode"]) {
async setCursor(parameters: Parameters) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
const editor = view.editor;
if (!view) return;
const mode = parameters.mode;
const editor = view.editor;

let viewState = view.leaf.getViewState();
viewState.state.mode = "source";
let viewState = view.leaf.getViewState();
viewState.state.mode = "source";

if (mode === "append") {
const lastLine = editor.lastLine();
const lastLineLength = editor.getLine(lastLine).length;
await view.leaf.setViewState(viewState, { focus: true });
if (mode === "append") {
const lastLine = editor.lastLine();
const lastLineLength = editor.getLine(lastLine).length;
await view.leaf.setViewState(viewState, { focus: true });

editor.setCursor({ ch: lastLineLength, line: lastLine });
} else if (mode === "prepend") {
await view.leaf.setViewState(viewState, { focus: true });
editor.setCursor({ ch: lastLineLength, line: lastLine });
} else if (mode === "prepend") {
await view.leaf.setViewState(viewState, { focus: true });

editor.setCursor({ ch: 0, line: 0 });
}
editor.setCursor({ ch: 0, line: 0 });
}

await new Promise((resolve) => setTimeout(resolve, 10));

if (parameters.viewmode == "preview") {
viewState.state.mode = "preview";
await view.leaf.setViewState(viewState);
}
}

setCursorInLine(rawLine: number) {
async setCursorInLine(parameters: Parameters) {
const rawLine = parameters.line;
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const viewState = view.leaf.getViewState();
if (viewState.state.mode !== "source") {
viewState.state.mode = "source";
view.leaf.setViewState(viewState);
}
viewState.state.mode = "source";
await view.leaf.setViewState(viewState);

const line = Math.min(rawLine - 1, view.editor.lineCount() - 1);
view.editor.focus();
view.editor.setCursor({
line: line,
ch: view.editor.getLine(line).length,
});

await new Promise((resolve) => setTimeout(resolve, 10));

if (parameters.viewmode == "preview") {
viewState.state.mode = "preview";
await view.leaf.setViewState(viewState);
}
}

async loadSettings() {
Expand Down

0 comments on commit 917a4eb

Please sign in to comment.