Skip to content

Commit

Permalink
feat: specify openmode
Browse files Browse the repository at this point in the history
close #79
  • Loading branch information
Vinzent03 committed Nov 8, 2022
1 parent 5b9f810 commit dfc8f6b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
9 changes: 7 additions & 2 deletions docs/docs/concepts/navigation_parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ Every action opening or focusing a pane supports the parameter `viewmode`. Accep
- `live`: Sets the editor to reading:live mode
- `preview`: Sets the editor to preview mode

## New pane
## Open mode

Every action opening a pane supports the parameter `newpane`. Accepted values:
Every action opening a pane supports the parameter `openmode`. Accepted values:
- `true` opens file in a new pane if not already opened
- `false` opens file in current pane if not already opened
- `window`
- `split`
- `tab`
- `silent` doesn't open the file
- `popover` which requires the [Hover Editor plugin](obsidian://show-plugin?id=obsidian-hover-editor) to be installed and enabled

If the file is already opened in another pane, it gets focused.

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 58 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FileModal } from "./modals/file_modal";
import { ReplaceModal } from "./modals/replace_modal";
import { SearchModal } from "./modals/search_modal";
import { SettingsTab } from "./settings";
import { AdvancedURISettings, FileModalData, HookParameters, Parameters, SearchModalData } from "./types";
import { AdvancedURISettings, FileModalData, HookParameters, OpenMode, Parameters, SearchModalData } from "./types";

const DEFAULT_SETTINGS: AdvancedURISettings = {
openFileOnWrite: true,
Expand Down Expand Up @@ -331,9 +331,7 @@ export default class AdvancedURI extends Plugin {
parameters.filepath = this.getAlternativeFilePath(file);
}
}
await this.app.workspace.openLinkText(parameters.filepath, "/", undefined, {
state: { mode: "source" }
});
await this.open({ file: parameters.filepath, mode: "source", parameters: parameters });
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
const editor = view.editor;
Expand All @@ -350,12 +348,11 @@ export default class AdvancedURI extends Plugin {
}
}
} else if (parameters.line) {
await this.app.workspace.openLinkText(parameters.filepath, "/", undefined, {
state: { mode: "source" }
});
await this.open({ file: parameters.filepath, mode: "source", parameters: parameters });

this.setCursorInLine(parameters.line);
} else {
await this.app.workspace.openLinkText(parameters.filepath, "/", this.settings.openFileWithoutWriteInNewPane, this.getViewStateFromMode(parameters));
await this.open({ file: parameters.filepath, setting: this.settings.openFileWithoutWriteInNewPane, parameters: parameters });
}
}
if (parameters.commandid) {
Expand Down Expand Up @@ -489,9 +486,13 @@ export default class AdvancedURI extends Plugin {
}
}

const openInNewPane = parameters.newpane !== undefined ? parameters.newpane == "true" : this.settings.openFileWithoutWriteInNewPane;
if (parameters.heading != undefined) {
await this.app.workspace.openLinkText(parameters.filepath + "#" + parameters.heading, "", openInNewPane, this.getViewStateFromMode(parameters));
await this.open({
file: parameters.filepath + "#" + parameters.heading,
setting: this.settings.openFileWithoutWriteInNewPane,
parameters: parameters,
supportPopover: false,
});
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const cache = this.app.metadataCache.getFileCache(view.file);
Expand All @@ -500,7 +501,12 @@ export default class AdvancedURI extends Plugin {
view.editor.setCursor({ line: heading.position.start.line + 1, ch: 0 });
}
else if (parameters.block != undefined) {
await this.app.workspace.openLinkText(parameters.filepath + "#^" + parameters.block, "", openInNewPane, this.getViewStateFromMode(parameters));
await this.open({
file: parameters.filepath + "#^" + parameters.block,
setting: this.settings.openFileWithoutWriteInNewPane,
parameters: parameters,
supportPopover: false,
});
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const cache = this.app.metadataCache.getFileCache(view.file);
Expand All @@ -510,7 +516,11 @@ export default class AdvancedURI extends Plugin {
}
else {
if (!fileIsAlreadyOpened)
await this.app.workspace.openLinkText(parameters.filepath, "", openInNewPane, this.getViewStateFromMode(parameters));
await this.open({
file: parameters.filepath,
setting: this.settings.openFileWithoutWriteInNewPane,
parameters: parameters, supportPopover: false
});
if (parameters.line != undefined) {
this.setCursorInLine(parameters.line);
}
Expand Down Expand Up @@ -620,12 +630,12 @@ export default class AdvancedURI extends Plugin {
this.app.workspace.iterateAllLeaves(leaf => {
if (leaf.view.file?.path === outputFileName) {
fileIsAlreadyOpened = true;
this.app.workspace.setActiveLeaf(leaf, true, true);
this.app.workspace.setActiveLeaf(leaf, { focus: true });
}
});

if (!fileIsAlreadyOpened)
await this.app.workspace.openLinkText(outputFileName, "", parameters.newpane !== undefined ? parameters.newpane == "true" : this.settings.openFileOnWriteInNewPane, this.getViewStateFromMode(parameters));
await this.open({ file: outputFileName, setting: this.settings.openFileOnWriteInNewPane, parameters });
if (parameters.line != undefined) {
this.setCursorInLine(parameters.line);
}
Expand All @@ -634,6 +644,40 @@ export default class AdvancedURI extends Plugin {
return this.app.vault.getAbstractFileByPath(outputFileName) as TFile;
}

open({ file, setting, parameters, supportPopover, mode }: { file: string | TFile, setting?: boolean, parameters: Parameters, supportPopover?: boolean, mode?: "source"; }): Promise<void> {
if (parameters.openmode == "popover" && (supportPopover ?? true)) {

const hoverEditor = this.app.plugins.plugins["obsidian-hover-editor"];
if (!hoverEditor) {
new Notice("Cannot find Workspaces plugin. Please file an issue.");
this.failure(parameters);
}

const leaf = hoverEditor.spawnPopover(undefined, () => {
this.app.workspace.setActiveLeaf(leaf, { focus: true });
});
const tfile = file instanceof TFile ? file : this.app.vault.getAbstractFileByPath(file) as TFile;
leaf.openFile(tfile);
} else {
let openMode: OpenMode | boolean = setting;
if (parameters.newpane !== undefined) {
openMode = parameters.newpane == "true";
}
if (parameters.openmode !== undefined) {
if (parameters.openmode == "true" || parameters.openmode == "false") {
openMode = parameters.openmode == "true";
} else if (parameters.openmode == "popover") {
openMode = false;
} else {
openMode = parameters.openmode;
}
}
if (openMode == "silent") {
return;
}
return this.app.workspace.openLinkText(file instanceof TFile ? file.path : file, "", openMode, mode != undefined ? { state: { mode: mode } } : this.getViewStateFromMode(parameters));
}
}
getEndAndBeginningOfHeading(file: TFile, heading: string): { "lastLine": number, "firstLine": number; } {
const cache = this.app.metadataCache.getFileCache(file);
const sections = cache.sections;
Expand Down
17 changes: 12 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginManifest } from "obsidian";
import { PaneType } from "obsidian";

declare module 'obsidian' {
interface App {
Expand All @@ -8,9 +8,6 @@ declare module 'obsidian' {
pluginTabs: Array<{
id: string;
name: string;
plugin: {
[key: string]: PluginManifest;
};
instance?: {
description: string;
id: string;
Expand All @@ -28,7 +25,11 @@ declare module 'obsidian' {
};
plugins: {
plugins: {
[key: string]: PluginManifest;
[key: string]: { manifest: PluginManifest; };
"obsidian-hover-editor": {
spawnPopover(initiatingEl?: HTMLElement, onShowCallback?: () => unknown): WorkspaceLeaf;
manifest: PluginManifest;
};
};
enablePluginAndSave(plugin: string): void;
disablePluginAndSave(plugin: string): void;
Expand Down Expand Up @@ -103,19 +104,25 @@ export interface Parameters {
filename?: string;
exists?: string;
viewmode?: "source" | "preview" | "live";
openmode?: OpenMode;
settingid?: string;
"x-success"?: string;
"x-error"?: string;
saveworkspace?: "true";
updateplugins?: "true";
line?: number;
/**
* @deprecated Use "openMode" instead
*/
newpane?: "true" | "false";
clipboard?: "true";
"enable-plugin"?: string;
"disable-plugin"?: string;
frontmatterkey?: string;
}

export type OpenMode = "silent" | "popover" | PaneType | "true" | "false";

export interface HookParameters {
"x-success": string;
"x-error": string;
Expand Down

0 comments on commit dfc8f6b

Please sign in to comment.