From d80991e5a5384bc4da293e9da92c5c822044a1ef Mon Sep 17 00:00:00 2001 From: PJ Date: Sat, 29 Apr 2023 18:19:53 -0400 Subject: [PATCH] Improved plugin browser features for Obsidian 1.x 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.) --- manifest.json | 2 +- pnpm-lock.yaml | 25 ++++++++++++---- src/hotkey-helper.ts | 62 +++++++++++++++++++++++++++++++++++++-- src/obsidian-internals.ts | 5 +--- styles.css | 4 ++- versions.json | 1 + 6 files changed, 86 insertions(+), 13 deletions(-) diff --git a/manifest.json b/manifest.json index 6a0c877..c1b3a00 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 371b660..837958a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,7 +9,7 @@ specifiers: devDependencies: '@ophidian/build': 1.0.0 - '@ophidian/core': github.com/ophidian-lib/core/65ad44e4d0039aa026e12e32e772a36b8bf2650a + '@ophidian/core': github.com/ophidian-lib/core/7638db4cded07464dc15acda7f6118f0999f7804 i18next: 20.6.1 monkey-around: 2.3.0 obsidian: 0.15.4 @@ -670,6 +670,10 @@ packages: resolution: {integrity: sha512-c6YRvhEo//6T2Jz/vVtYzqBzwvPT95JBQ+smCytzf7c50oMZRsR/a4w88aD34I+/QVSfnoAnSBFPJHItlOMJVw==} dev: true + /moment/2.29.4: + resolution: {integrity: sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==} + dev: true + /monkey-around/2.3.0: resolution: {integrity: sha512-QWcCUWjqE/MCk9cXlSKZ1Qc486LD439xw/Ak8Nt6l2PuL9+yrc9TJakt7OHDuOqPRYY4nTWBAEFKn32PE/SfXA==} dev: true @@ -694,6 +698,16 @@ packages: moment: 2.29.3 dev: true + /obsidian/0.15.9: + resolution: {integrity: sha512-w3JL/IM3/U61rjFSFIFDSv+pcHn3mH1EIRN40kBkC/lGYqjFSPbr6daQe08QkskBz/GAYIeBoaKQIcgU9vV3LQ==} + peerDependencies: + '@codemirror/state': ^6.0.0 + '@codemirror/view': ^6.0.0 + dependencies: + '@types/codemirror': 0.0.108 + moment: 2.29.4 + dev: true + /once/1.4.0: resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} dependencies: @@ -854,13 +868,14 @@ packages: resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} dev: true - github.com/ophidian-lib/core/65ad44e4d0039aa026e12e32e772a36b8bf2650a: - resolution: {tarball: https://codeload.github.com/ophidian-lib/core/tar.gz/65ad44e4d0039aa026e12e32e772a36b8bf2650a} + github.com/ophidian-lib/core/7638db4cded07464dc15acda7f6118f0999f7804: + resolution: {tarball: https://codeload.github.com/ophidian-lib/core/tar.gz/7638db4cded07464dc15acda7f6118f0999f7804} name: '@ophidian/core' - version: 0.0.11 + version: 0.0.12 dependencies: + i18next: 20.6.1 monkey-around: 2.3.0 - obsidian: 0.15.4 + obsidian: 0.15.9 to-use: 0.2.1 transitivePeerDependencies: - '@codemirror/state' diff --git a/src/hotkey-helper.ts b/src/hotkey-helper.ts index 771f68e..fb93143 100644 --- a/src/hotkey-helper.ts +++ b/src/hotkey-helper.ts @@ -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 + updateSearch(): any + searchEl: HTMLInputElement + pluginContentEl: HTMLDivElement +} + +interface NewPluginViewer extends Modal { // 1.0+ + setAutoOpen(pluginId: string): this + showItem(manifest: PluginManifest): Promise + updateItems(): void + search: { inputEl: HTMLInputElement } + detailsEl: HTMLDivElement +} + + function hotkeyToString(hotkey: Hotkey) { return Keymap.compileModifiers(hotkey.modifiers)+"," + hotkey.key.toLowerCase() } @@ -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") && @@ -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 hotkeyButtons = {} as Record>; @@ -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"]) { @@ -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); } } diff --git a/src/obsidian-internals.ts b/src/obsidian-internals.ts index 08ead93..f9f12c1 100644 --- a/src/obsidian-internals.ts +++ b/src/obsidian-internals.ts @@ -1,9 +1,5 @@ import {i18n} from "i18next"; -declare global { - const i18next: i18n -} - declare module "obsidian" { namespace Keymap { function compileModifiers(mods: string[]): string @@ -79,6 +75,7 @@ declare module "obsidian" { /** The actual internal plugin object (state and methods). */ instance: InternalPluginInstance; enabled: boolean; + _loaded: boolean; } interface InternalPlugins { diff --git a/styles.css b/styles.css index 47df72d..5df6d0d 100644 --- a/styles.css +++ b/styles.css @@ -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); } diff --git a/versions.json b/versions.json index ee86842..e772a37 100644 --- a/versions.json +++ b/versions.json @@ -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",