Skip to content

Commit

Permalink
Improved plugin browser features for Obsidian 1.x
Browse files Browse the repository at this point in the history
These features now work correctly on Obsidian 1.x:

- Search entered in Community Plugins setting tab carries over to plugin browser
- Installed plugins with hotkey conflicts will show with an error highlight on the Hotkeys button
- Plugin browser is opened by hotkey-helper goto-plugin URLs

(They previously only worked with Obsidian 0.15 and older.)
  • Loading branch information
pjeby committed Apr 29, 2023
1 parent 529e1e5 commit d80991e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "hotkey-helper",
"name": "Hotkey Helper",
"version": "0.3.16",
"version": "0.3.17",
"minAppVersion": "0.15.9",
"description": "Easily see and access any plugin's settings or hotkey assignments (and conflicts) from the Community Plugins tab",
"author": "PJ Eby",
Expand Down
25 changes: 20 additions & 5 deletions pnpm-lock.yaml

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

62 changes: 60 additions & 2 deletions src/hotkey-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import {around, serialize} from "monkey-around";
import {defer, modalSelect, onElement} from "@ophidian/core";
import "./obsidian-internals";

interface OldPluginViewer extends Modal { // pre 1.0
autoopen?: string
showPlugin(manifest: PluginManifest): Promise<void>
updateSearch(): any
searchEl: HTMLInputElement
pluginContentEl: HTMLDivElement
}

interface NewPluginViewer extends Modal { // 1.0+
setAutoOpen(pluginId: string): this
showItem(manifest: PluginManifest): Promise<void>
updateItems(): void
search: { inputEl: HTMLInputElement }
detailsEl: HTMLDivElement
}


function hotkeyToString(hotkey: Hotkey) {
return Keymap.compileModifiers(hotkey.modifiers)+"," + hotkey.key.toLowerCase()
}
Expand All @@ -22,7 +39,7 @@ function settingsAreOpen() {
return app.setting.containerEl.parentElement !== null
}

function isPluginViewer(ob: any) {
function isPluginViewer(ob: any): ob is OldPluginViewer {
return (
ob instanceof Modal &&
ob.hasOwnProperty("autoload") &&
Expand All @@ -32,6 +49,14 @@ function isPluginViewer(ob: any) {
);
}

function isNewPluginViewer(ob: any): ob is NewPluginViewer {
return (
ob instanceof Modal &&
typeof (ob as any).setAutoOpen === "function" &&
typeof (ob as any).search?.inputEl === "object"
)
}

export default class HotkeyHelper extends Plugin {
lastSearch = {} as Record<string, string>
hotkeyButtons = {} as Record<string, Partial<ExtraButtonComponent>>;
Expand Down Expand Up @@ -343,6 +368,39 @@ export default class HotkeyHelper extends Plugin {
setTimeout(around(Modal.prototype, {
open(old) {
return function(...args) {
if (isNewPluginViewer(this)) {
defer(() => {
if (plugin.lastSearch["community-plugins"]) {
this.search.inputEl.value = plugin.lastSearch["community-plugins"];
this.search.inputEl.dispatchEvent(new Event('input'));
}
});
plugin.currentViewer = this;
around(this, {
close(old) { return function(...args: any[]) {
plugin.currentViewer = null;
return old.apply(this, args);
}},

showItem(old) { return async function(manifest: PluginManifest){
const res = await old.call(this, manifest);
if (plugin.app.plugins.plugins[manifest.id]) {
const hotkeysName = i18next.t("setting.hotkeys.name");
const buttons = this.detailsEl.find("button").parentElement;
for (const b of buttons.findAll("button")) {
if (b.textContent === hotkeysName) {
plugin.hotkeyButtons[manifest.id] = {
setTooltip(tip) {b.title = tip; return this; }, extraSettingsEl: b
}
};
}
plugin.refreshButtons(true);
}
return res;
}}
})
}
// Pre 1.0
if (isPluginViewer(this)) {
defer(() => {
if (plugin.lastSearch["community-plugins"]) {
Expand Down Expand Up @@ -464,7 +522,7 @@ export default class HotkeyHelper extends Plugin {
open(old) {
return function(...args) {
remove();
if (id) this.autoload = id;
if (id) { this.autoload = id; this.setAutoOpen?.(id); }
return old.apply(this, args);
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/obsidian-internals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {i18n} from "i18next";

declare global {
const i18next: i18n
}

declare module "obsidian" {
namespace Keymap {
function compileModifiers(mods: string[]): string
Expand Down Expand Up @@ -79,6 +75,7 @@ declare module "obsidian" {
/** The actual internal plugin object (state and methods). */
instance: InternalPluginInstance<T>;
enabled: boolean;
_loaded: boolean;
}

interface InternalPlugins {
Expand Down
4 changes: 3 additions & 1 deletion styles.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.clickable-icon.mod-error, .modal .community-plugin-info button.mod-error {
.clickable-icon.mod-error,
.modal .community-plugin-info button.mod-error,
.modal-container .mod-community-plugin .community-modal-button-container button.mod-error {
background-color: var(--background-modifier-error);
}

Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"0.3.17": "1.1.16",
"0.3.16": "0.15.9",
"0.3.11": "0.13.19",
"0.3.8": "0.12.3",
Expand Down

0 comments on commit d80991e

Please sign in to comment.