diff --git a/main.ts b/main.ts index e1cbd42..2682abb 100644 --- a/main.ts +++ b/main.ts @@ -36,6 +36,7 @@ interface RedirectPluginSettings { triggerString: string; mode: Mode; apiVersion: number; + limitToRedirectedFiles: boolean; } // From https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types: @@ -67,12 +68,14 @@ const DEFAULT_SETTINGS: RedirectPluginSettings = { limitToNonMarkdown: true, triggerString: "r[", mode: Mode.Standard, - apiVersion: 1, + limitToRedirectedFiles: true, + apiVersion: 2, }; const getRedirectFiles = ( plugin: RedirectPlugin, files: TFile[], + limitToRedirectedFiles: boolean, filterString?: string ) => { let redirectsGathered = files @@ -81,8 +84,10 @@ const getRedirectFiles = ( plugin.app.metadataCache.getFileCache(file)?.frontmatter; const aliases = frontMatter?.alias || frontMatter?.aliases || []; const redirects = - frontMatter?.redirects || frontMatter?.redirect || []; - const output = [ + frontMatter?.redirects || + frontMatter?.redirect || + (limitToRedirectedFiles ? [] : [file.path]); + let output = [ ...(Array.isArray(aliases) ? aliases : [aliases]), file.basename, ] @@ -126,6 +131,7 @@ const getRedirectFiles = ( const queryWords = filterString .toLowerCase() .split(/\s{1,}/); + return queryWords.every((word) => { return ( a.alias.toLowerCase().contains(word) || @@ -195,9 +201,11 @@ const handleFilesWithModal = ( files: FileWithPath[] | TFile[], ctrlKey: boolean ) => { - const redirectFiles = getRedirectFiles(plugin, app.vault.getFiles()); - - const f = files[0]; + const redirectFiles = getRedirectFiles( + plugin, + app.vault.getFiles(), + plugin.settings.limitToRedirectedFiles + ); [...files].forEach((f: FileWithPath | TFile) => { let filePath = f.path; @@ -305,20 +313,6 @@ export default class RedirectPlugin extends Plugin { }, }); - this.addCommand({ - id: "change-mode", - icon: "switch", - name: "Change mode", - editorCallback: async (editor: Editor, view: MarkdownView) => { - this.settings.mode = - this.settings.mode === Mode.Standard - ? Mode.RedirectOpen - : Mode.Standard; - this.statusBar.setText(`Redirect drop: ${this.settings.mode}`); - await this.saveSettings(); - }, - }); - this.addCommand({ id: "redirect-insert-file-path", icon: "pin", @@ -351,7 +345,7 @@ export default class RedirectPlugin extends Plugin { this.addCommand({ id: "redirect-open-file", icon: "go-to-file", - name: "Open redirected file", + name: "Open file", callback: () => { const fileModal = new FilePathModal({ app: this.app, @@ -365,7 +359,11 @@ export default class RedirectPlugin extends Plugin { .openFile(file.redirectTFile); }, limitToNonMarkdown: this.settings.limitToNonMarkdown, - files: getRedirectFiles(this, app.vault.getFiles()), + files: getRedirectFiles( + this, + app.vault.getFiles(), + this.settings.limitToRedirectedFiles + ), }); fileModal.open(); }, @@ -374,7 +372,7 @@ export default class RedirectPlugin extends Plugin { this.addCommand({ id: "redirect-open-origin-file", icon: "go-to-file", - name: "Open redirect origin file", + name: "Open origin file", callback: () => { const fileModal = new FilePathModal({ app: this.app, @@ -388,12 +386,76 @@ export default class RedirectPlugin extends Plugin { .openFile(file.originTFile); }, limitToNonMarkdown: this.settings.limitToNonMarkdown, - files: getRedirectFiles(this, app.vault.getFiles()), + files: getRedirectFiles( + this, + app.vault.getFiles(), + this.settings.limitToRedirectedFiles + ), }); fileModal.open(); }, }); + this.addCommand({ + id: "redirect-open-current-file-origin", + icon: "popup-open", + name: "Open current file's origin file", + checkCallback: (checking: boolean) => { + const currentFile = this.app.workspace.getActiveFile(); + let redirectFiles = getRedirectFiles( + this, + app.vault.getFiles(), + true + ).filter( + (a: SuggestionObject) => a.redirectTFile === currentFile + ); + if (checking) { + if (!redirectFiles.length) { + return false; + } + return true; + } + + const redirectFilesSet = [ + ...new Set( + redirectFiles.map( + (a: SuggestionObject) => a.originTFile + ) + ), + ]; + + // If all redirect files are the same (even with + // different aliases), collapse them to one: + if (redirectFilesSet.length === 1) { + this.app.workspace + .getLeaf(false) + .openFile(redirectFiles[0].originTFile); + + return; + } + + if (redirectFilesSet.length > 1) { + const fileModal = new FilePathModal({ + app: this.app, + fileOpener: true, + onChooseFile: ( + file: SuggestionObject, + newPane: boolean + ): void => { + this.app.workspace + .getLeaf(newPane) + .openFile(file.originTFile); + }, + limitToNonMarkdown: this.settings.limitToNonMarkdown, + files: redirectFiles, + }); + fileModal.open(); + + return; + } + }, + }); + // Add to the right-click file menu. For another example // of this, see https://github.com/Oliver-Akins/file-hider/blob/main/src/main.ts#L24-L64 this.registerEvent( @@ -420,17 +482,37 @@ export default class RedirectPlugin extends Plugin { }) ); - this.statusBar = this.addStatusBarItem(); - this.statusBar.setText(`Redirect drop: ${this.settings.mode}`); - - this.statusBar.onClickEvent(async () => { - this.settings.mode = - this.settings.mode === Mode.Standard - ? Mode.RedirectOpen - : Mode.Standard; + // From https://discord.com/channels/686053708261228577/840286264964022302/851183938542108692: + if (this.app.vault.adapter instanceof FileSystemAdapter) { + // On desktop. + this.statusBar = this.addStatusBarItem(); this.statusBar.setText(`Redirect drop: ${this.settings.mode}`); - await this.saveSettings(); - }); + + this.statusBar.onClickEvent(async () => { + this.settings.mode = + this.settings.mode === Mode.Standard + ? Mode.RedirectOpen + : Mode.Standard; + this.statusBar.setText(`Redirect drop: ${this.settings.mode}`); + await this.saveSettings(); + }); + + this.addCommand({ + id: "change-mode", + icon: "switch", + name: "Change mode", + editorCallback: async (editor: Editor, view: MarkdownView) => { + this.settings.mode = + this.settings.mode === Mode.Standard + ? Mode.RedirectOpen + : Mode.Standard; + this.statusBar.setText( + `Redirect drop: ${this.settings.mode}` + ); + await this.saveSettings(); + }, + }); + } // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new RedirectSettingsTab(this.app, this)); @@ -601,6 +683,7 @@ class RedirectEditorSuggester extends EditorSuggest<{ return getRedirectFiles( this.plugin, this.plugin.app.vault.getFiles(), + this.plugin.settings.limitToRedirectedFiles, context.query ); } @@ -666,6 +749,20 @@ class RedirectSettingsTab extends PluginSettingTab { }) ); + new Setting(containerEl) + .setName("Limit to redirected files") + .setDesc( + `Look for only files that are redirected. If this is off, all files in the Vault will be listed (subject to the "Limit to non-Markdown files" setting above), supplemented with a list of redirected files.` + ) + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.limitToRedirectedFiles) + .onChange(async (value) => { + this.plugin.settings.limitToRedirectedFiles = value; + await this.plugin.saveSettings(); + }) + ); + new Setting(containerEl) .setName("Trigger string") .setDesc( diff --git a/manifest.json b/manifest.json index 2070262..59fc554 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-redirect", "name": "Redirect", - "version": "0.1.4", + "version": "0.1.5", "minAppVersion": "0.14.15", "description": "An Obsidian (https://obsidian.md) plugin for redirecting links based on YAML frontmatter.", "author": "Jacob Levernier", diff --git a/package.json b/package.json index b5516aa..db0fcde 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-redirect", - "version": "0.1.4", + "version": "0.1.5", "description": "An Obsidian (https://obsidian.md) plugin for redirecting links based on YAML frontmatter.", "main": "main.js", "scripts": { diff --git a/styles.css b/styles.css index aa9d81b..2a7c4ff 100644 --- a/styles.css +++ b/styles.css @@ -1,5 +1,5 @@ .redirect-suggester-el { -display: relative; + display: relative; margin-left: 0.5em; } @@ -8,11 +8,11 @@ display: relative; } .redirect-suggestion-text { -margin-right: 3em; + margin-right: 3em; } .redirect-item { -font-size: smaller; + font-size: smaller; clear: left; display: block; color: var(--text-muted); @@ -26,16 +26,18 @@ font-size: smaller; right: 1em; top: 0.5em; background-size: cover; - background-position: center; - background-repeat: no-repeat; + background-position: center; + background-repeat: no-repeat; } + .redirect-suggestion-image-container .redirect-suggestion-image-large { z-index: 10; max-width: 100%; width: auto; max-height: 50vh; overflow: scroll; -/* Center the image on the screen: */ + + /* Center the image on the screen: */ position: fixed; left: 50%; top: 50%; @@ -45,8 +47,8 @@ font-size: smaller; box-shadow: 0.1em 1em 2em 0.5em var(--background-modifier-cover); visibility: hidden; opacity: 0; -z-index: 1000; -transition-delay: 0.2s; + z-index: 1000; + transition-delay: 0.2s; } .redirect-suggestion-image-container:hover .redirect-suggestion-image-large { @@ -54,14 +56,20 @@ transition-delay: 0.2s; opacity: 1.0; transition: all 0.3s ease-in-out; transition-delay: 0.3s; + background: white; } + .redirect-is-alias { -color: var(--text-muted); + color: var(--text-muted); font-weight: bold; /* margin-left: -1em; -margin-right: 1em; + margin-right: 1em; */ position: absolute; left: 0.75em; transform: scale(1, -1); font-size: larger; +} + +.is-mobile .redirect-is-alias { + position: unset; } \ No newline at end of file diff --git a/versions.json b/versions.json index e16f29f..283bdb0 100644 --- a/versions.json +++ b/versions.json @@ -3,5 +3,6 @@ "0.1.1": "0.14.15", "0.1.2": "0.14.15", "0.1.3": "0.14.15", - "0.1.4": "0.14.15" + "0.1.4": "0.14.15", + "0.1.5": "0.14.15" }