Skip to content

Commit

Permalink
fix: active file is undefined in binary files
Browse files Browse the repository at this point in the history
refs: #1210
  • Loading branch information
Zachatoo committed Oct 2, 2023
1 parent 516d2bf commit 27af7a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/core/Templater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
delay,
generate_dynamic_command_regex,
get_active_file,
resolve_tfile,
} from "utils/Utils";
import TemplaterPlugin from "main";
Expand Down Expand Up @@ -60,7 +61,7 @@ export class Templater {
target_file: TFile,
run_mode: RunMode
): RunningConfig {
const active_file = app.workspace.activeEditor?.file;
const active_file = get_active_file(app);

return {
template_file: template_file,
Expand Down Expand Up @@ -104,7 +105,7 @@ export class Templater {
const new_file_location = app.vault.getConfig("newFileLocation");
switch (new_file_location) {
case "current": {
const active_file = app.workspace.activeEditor?.file;
const active_file = get_active_file(app);
if (active_file) {
folder = active_file.parent;
}
Expand Down
3 changes: 2 additions & 1 deletion src/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TemplaterPlugin from "main";
import { TemplaterError } from "utils/Error";
import { CursorJumper } from "editor/CursorJumper";
import { log_error } from "utils/Log";
import { get_active_file } from "utils/Utils";
import { Autocomplete } from "editor/Autocomplete";

import "editor/mode/javascript";
Expand Down Expand Up @@ -58,7 +59,7 @@ export class Editor {
if (auto_jump && !this.plugin.settings.auto_jump_to_cursor) {
return;
}
if (file && app.workspace.activeEditor?.file !== file) {
if (file && get_active_file(app) !== file) {
return;
}
await this.cursor_jumper.jump_to_next_cursor_location();
Expand Down
13 changes: 12 additions & 1 deletion src/utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { TemplaterError } from "./Error";
import { normalizePath, TAbstractFile, TFile, TFolder, Vault } from "obsidian";
import {
App,
normalizePath,
TAbstractFile,
TFile,
TFolder,
Vault,
} from "obsidian";

export function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -74,3 +81,7 @@ export function arraymove<T>(
arr[fromIndex] = arr[toIndex];
arr[toIndex] = element;
}

export function get_active_file(app: App) {
return app.workspace.activeEditor?.file ?? app.workspace.getActiveFile();
}

0 comments on commit 27af7a3

Please sign in to comment.