Skip to content

Commit

Permalink
0.1.5 (#3)
Browse files Browse the repository at this point in the history
* Hid features that will not work on mobile.

* Fixed alias icon spacing on mobile.

* Added ability to show all files (including non-redirected files) in file lists.

* Fixed bug whereby inline suggestion filtering was not working.

* Fixed bug whereby non-markdown files were not being correctly filtered.

* Set background for overlay images.

* Added "Open current file's origin file" command.
  • Loading branch information
jglev authored Jul 2, 2022
1 parent c8e110d commit f771830
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 47 deletions.
165 changes: 131 additions & 34 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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,
]
Expand Down Expand Up @@ -126,6 +131,7 @@ const getRedirectFiles = (
const queryWords = filterString
.toLowerCase()
.split(/\s{1,}/);

return queryWords.every((word) => {
return (
a.alias.toLowerCase().contains(word) ||
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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();
},
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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));
Expand Down Expand Up @@ -601,6 +683,7 @@ class RedirectEditorSuggester extends EditorSuggest<{
return getRedirectFiles(
this.plugin,
this.plugin.app.vault.getFiles(),
this.plugin.settings.limitToRedirectedFiles,
context.query
);
}
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
28 changes: 18 additions & 10 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.redirect-suggester-el {
display: relative;
display: relative;
margin-left: 0.5em;
}

Expand All @@ -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);
Expand All @@ -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%;
Expand All @@ -45,23 +47,29 @@ 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 {
visibility: visible;
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;
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit f771830

Please sign in to comment.