├── styles.css ├── versions.json ├── .gitignore ├── manifest.json ├── .github └── workflows │ ├── ci.yaml │ └── publish.yaml ├── .eslintrc.js ├── tsconfig.json ├── package.json ├── LICENSE ├── esbuild.config.mjs ├── README.md └── src ├── StatisticsModal.ts ├── settings.ts └── main.ts /styles.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /versions.json: -------------------------------------------------------------------------------- 1 | { 2 | "1.3.0": "0.9.12", 3 | "1.3.1": "1.4.1", 4 | "1.4.0": "1.11.0" 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Intellij 2 | *.iml 3 | .idea 4 | 5 | # npm 6 | node_modules 7 | package-lock.json 8 | 9 | # build 10 | main.js 11 | *.js.map 12 | 13 | # obsidian 14 | data.json 15 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "key-promoter", 3 | "name": "Key Promoter", 4 | "version": "1.4.0", 5 | "minAppVersion": "1.11.0", 6 | "description": "Learn keyboard shortcuts by showing them when using the mouse", 7 | "author": "Johannes Theiner", 8 | "authorUrl": "https://github.com/joethei", 9 | "isDesktopOnly": false 10 | } 11 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | lint-and-test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - name: Install modules 16 | run: yarn 17 | 18 | - name: Lint 19 | run: yarn run lint 20 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: "@typescript-eslint/parser", 4 | plugins: ["@typescript-eslint"], 5 | extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], 6 | rules: { 7 | "@typescript-eslint/no-unused-vars": [ 8 | 2, 9 | { args: "all", argsIgnorePattern: "^_" }, 10 | ], 11 | "@typescript-eslint/ban-ts-comment": "off", 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "inlineSourceMap": true, 5 | "inlineSources": true, 6 | "module": "ESNext", 7 | "target": "es6", 8 | "allowJs": true, 9 | "noImplicitAny": true, 10 | "moduleResolution": "node", 11 | "importHelpers": true, 12 | "lib": [ 13 | "dom", 14 | "es5", 15 | "scripthost", 16 | "es2015" 17 | ] 18 | }, 19 | "include": [ 20 | "**/*.ts" 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "key-promoter", 3 | "version": "1.3.0", 4 | "description": "Obsidian plugin to promote keyboard shortcuts", 5 | "main": "main.js", 6 | "scripts": { 7 | "dev": "node esbuild.config.mjs", 8 | "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", 9 | "lint": "eslint . --ext .ts" 10 | }, 11 | "keywords": [], 12 | "author": "Johannes Theiner", 13 | "license": "MIT", 14 | "devDependencies": { 15 | "esbuild": "0.17.3", 16 | "@types/node": "^14.14.37", 17 | "@typescript-eslint/eslint-plugin": "^4.33.0", 18 | "@typescript-eslint/parser": "^4.33.0", 19 | "builtin-modules": "3.3.0", 20 | "eslint": "^7.32.0", 21 | "obsidian": "1.11.0", 22 | "tslib": "^2.2.0", 23 | "typescript": "^4.2.4", 24 | "monkey-around": "3.0.0" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021-2025 Johannes Theiner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /esbuild.config.mjs: -------------------------------------------------------------------------------- 1 | import esbuild from "esbuild"; 2 | import process from "process"; 3 | import builtins from "builtin-modules"; 4 | 5 | const banner = 6 | `/* 7 | THIS IS A GENERATED/BUNDLED FILE BY ESBUILD 8 | if you want to view the source, please visit the github repository of this plugin 9 | https://github.com/joethei/obsidian-key-promoter 10 | */ 11 | `; 12 | 13 | const prod = (process.argv[2] === "production"); 14 | 15 | const context = await esbuild.context({ 16 | banner: { 17 | js: banner, 18 | }, 19 | entryPoints: ["src/main.ts"], 20 | bundle: true, 21 | external: [ 22 | "obsidian", 23 | "electron", 24 | "@codemirror/autocomplete", 25 | "@codemirror/collab", 26 | "@codemirror/commands", 27 | "@codemirror/language", 28 | "@codemirror/lint", 29 | "@codemirror/search", 30 | "@codemirror/state", 31 | "@codemirror/view", 32 | "@lezer/common", 33 | "@lezer/highlight", 34 | "@lezer/lr", 35 | ...builtins], 36 | format: "cjs", 37 | target: "es2018", 38 | logLevel: "info", 39 | sourcemap: prod ? false : "inline", 40 | treeShaking: true, 41 | outfile: "main.js", 42 | minify: prod, 43 | }); 44 | 45 | if (prod) { 46 | await context.rebuild(); 47 | process.exit(0); 48 | } else { 49 | await context.watch(); 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Key Promoter 2 | 3 | ![GitHub package.json version](https://img.shields.io/github/package-json/v/joethei/obsidian-key-promoter) 4 | ![GitHub manifest.json dynamic (path)](https://img.shields.io/github/manifest-json/minAppVersion/joethei/obsidian-key-promoter?label=lowest%20supported%20app%20version) 5 | ![GitHub](https://img.shields.io/github/license/joethei/obsidian-key-promoter) 6 | [![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com) 7 | 8 | Plugin for [Obsidian](https://obsidian.md/) 9 | 10 | --- 11 | ## Key Promotion 12 | 13 | When you use the mouse on a button this plugin will show a notification with the assigned shortcut for that action. 14 | 15 | For buttons that don't have shortcuts assigned the plugin 16 | will show you the name of the command to assign in the settings. 17 | 18 | > [!WARNING] 19 | > **Buttons are not directly associated with commands in Obsidian.** 20 | > Sometimes even the name of a button does not match the name of the command 21 | > that executes the same action. 22 | > 23 | > As a result you might get multiple different notifications when pressing one button. 24 | 25 | ## Description of Actions 26 | Shows a notification when pressing a hotkey with the called command and its 27 | associated hotkey. 28 | Useful for presentations. 29 | 30 | Needs to be enabled in the settings. 31 | 32 | ## Statistics 33 | Shows your most used commands. 34 | You can open this menu with the `Key Promoter: Statistics` Command. 35 | 36 | --- 37 | 38 | Inspired by the [Key Promoter X](https://plugins.jetbrains.com/plugin/9792-key-promoter-x) and [Presentation Assistant](https://plugins.jetbrains.com/plugin/7345-presentation-assistant) plugins for [IntelliJ IDEA](https://jetbrains.com/idea). 39 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: Build plugin 2 | 3 | on: 4 | push: 5 | # Sequence of patterns matched against refs/tags 6 | tags: 7 | - "*" # Push events to matching any tag format, i.e. 1.0, 20.15.10 8 | 9 | env: 10 | PLUGIN_NAME: key-promoter 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - uses: actions/checkout@v2 18 | - name: Use Node.js 19 | uses: actions/setup-node@v1 20 | with: 21 | node-version: "14.x" # You might need to adjust this value to your own version 22 | - name: Build 23 | id: build 24 | run: | 25 | npm install -g yarn 26 | yarn 27 | yarn run build 28 | mkdir ${{ env.PLUGIN_NAME }} 29 | cp main.js manifest.json ${{ env.PLUGIN_NAME }} 30 | zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }} 31 | ls 32 | echo "::set-output name=tag_name::$(git tag --sort version:refname | tail -n 1)" 33 | - name: Create Release 34 | id: create_release 35 | uses: actions/create-release@v1 36 | env: 37 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 38 | VERSION: ${{ github.ref }} 39 | with: 40 | tag_name: ${{ github.ref }} 41 | release_name: ${{ github.ref }} 42 | draft: false 43 | prerelease: false 44 | - name: Upload zip file 45 | id: upload-zip 46 | uses: actions/upload-release-asset@v1 47 | env: 48 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 49 | with: 50 | upload_url: ${{ steps.create_release.outputs.upload_url }} 51 | asset_path: ./${{ env.PLUGIN_NAME }}.zip 52 | asset_name: ${{ env.PLUGIN_NAME }}-${{ steps.build.outputs.tag_name }}.zip 53 | asset_content_type: application/zip 54 | - name: Upload main.js 55 | id: upload-main 56 | uses: actions/upload-release-asset@v1 57 | env: 58 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 59 | with: 60 | upload_url: ${{ steps.create_release.outputs.upload_url }} 61 | asset_path: ./main.js 62 | asset_name: main.js 63 | asset_content_type: text/javascript 64 | - name: Upload manifest.json 65 | id: upload-manifest 66 | uses: actions/upload-release-asset@v1 67 | env: 68 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 69 | with: 70 | upload_url: ${{ steps.create_release.outputs.upload_url }} 71 | asset_path: ./manifest.json 72 | asset_name: manifest.json 73 | asset_content_type: application/json 74 | -------------------------------------------------------------------------------- /src/StatisticsModal.ts: -------------------------------------------------------------------------------- 1 | import {Command, Modal, SettingGroup} from "obsidian"; 2 | import KeyPromoterPlugin from "./main"; 3 | 4 | export class StatisticsModal extends Modal { 5 | 6 | plugin: KeyPromoterPlugin; 7 | 8 | constructor(plugin: KeyPromoterPlugin) { 9 | super(plugin.app); 10 | this.plugin = plugin; 11 | } 12 | 13 | onOpen(): void { 14 | this.modalEl.addClass("key-promoter-modal"); 15 | const {contentEl} = this; 16 | contentEl.empty(); 17 | 18 | this.setTitle("Command statistics"); 19 | 20 | const mouseGroup = new SettingGroup(contentEl).setHeading("Mouse"); 21 | 22 | const mouseMap = new Map(); 23 | Object.each(this.plugin.settings.mouseStatistics, (value, key) => { 24 | mouseMap.set(key, value); 25 | }); 26 | const sortedMouseMap = new Map([...mouseMap.entries()].sort((a, b) => b[1] - a[1])); 27 | 28 | sortedMouseMap.forEach((value, key) => { 29 | const command: Command = this.plugin.app.commands.findCommand(key); 30 | if(!command) return; 31 | 32 | mouseGroup.addSetting(setting => { 33 | setting 34 | .setName(command.name) 35 | .setDesc(this.plugin.getHotkeysForCommand(command).join()) 36 | .controlEl.createSpan({text: String(value)}); 37 | setting.addExtraButton(button => { 38 | button 39 | .setIcon('command') 40 | .setTooltip('Assign new hotkey') 41 | .onClick(() => { 42 | this.close(); 43 | this.app.setting.open(); 44 | const hotkeySettings = this.app.setting.openTabById('hotkeys'); 45 | hotkeySettings.setQuery(command.id); 46 | }); 47 | }); 48 | }); 49 | }); 50 | 51 | const keyboardGroup = new SettingGroup(contentEl).setHeading("Keyboard"); 52 | 53 | const keyboardMap = new Map(); 54 | Object.each(this.plugin.settings.keyboardStatistics, (value, key) => { 55 | keyboardMap.set(key, value); 56 | }); 57 | const sortedKeyboardMap = new Map([...keyboardMap.entries()].sort((a, b) => b[1] - a[1])); 58 | 59 | sortedKeyboardMap.forEach((value, key) => { 60 | const command: Command = this.plugin.app.commands.findCommand(key); 61 | if (!command) return; 62 | keyboardGroup.addSetting(setting => { 63 | setting 64 | .setName(command.name) 65 | .setDesc(this.plugin.getHotkeysForCommand(command).join()) 66 | .controlEl.createSpan({text: String(value)}); 67 | setting.addExtraButton(button => { 68 | button 69 | .setIcon('command') 70 | .setTooltip('Assign new hotkey') 71 | .onClick(() => { 72 | this.close(); 73 | this.app.setting.open(); 74 | const hotkeySettings = this.app.setting.openTabById('hotkeys'); 75 | hotkeySettings.setQuery(command.id); 76 | }); 77 | }); 78 | }); 79 | }); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/settings.ts: -------------------------------------------------------------------------------- 1 | import {Notice, PluginSettingTab, SettingGroup, TextAreaComponent} from "obsidian"; 2 | import KeyPromoterPlugin from "./main"; 3 | 4 | 5 | export interface KeyPromoterSettings { 6 | showUnassigned: boolean; 7 | showAssigned: boolean; 8 | threshold: number; 9 | template: string; 10 | notificationTimeout: number; 11 | descriptionOfActions: boolean; 12 | mouseStatistics: {[index: string]: number}, 13 | keyboardStatistics: {[index: string]: number} 14 | } 15 | 16 | export const DEFAULT_SETTINGS: KeyPromoterSettings = { 17 | showUnassigned: true, 18 | showAssigned: true, 19 | threshold: 0, 20 | notificationTimeout: 5, 21 | template: "`{{commandId}}` - {{commandName}} - `{{hotkey}}`\n", 22 | descriptionOfActions: false, 23 | mouseStatistics: {}, 24 | keyboardStatistics: {}, 25 | } 26 | 27 | export class KeyPromoterSettingsTab extends PluginSettingTab { 28 | plugin: KeyPromoterPlugin; 29 | icon = 'keyboard'; 30 | 31 | constructor(plugin: KeyPromoterPlugin) { 32 | super(plugin.app, plugin); 33 | this.plugin = plugin; 34 | } 35 | 36 | display(): void { 37 | const {containerEl} = this; 38 | 39 | containerEl.empty(); 40 | 41 | new SettingGroup(containerEl) 42 | .addSetting(setting => { 43 | setting 44 | .setName('Show for assigned commands') 45 | .setDesc('show a notification for commands that do have a hotkey assigned') 46 | .addToggle(toggle => { 47 | toggle 48 | .setValue(this.plugin.settings.showAssigned) 49 | .onChange(async (value) => { 50 | this.plugin.settings.showAssigned = value; 51 | await this.plugin.saveSettings(); 52 | }); 53 | }); 54 | }) 55 | .addSetting(setting => { 56 | setting 57 | .setName('Show for unassigned commands') 58 | .setDesc('show a notification for commands that do not have a hotkey assigned') 59 | .addToggle(toggle => { 60 | toggle 61 | .setValue(this.plugin.settings.showUnassigned) 62 | .onChange(async (value) => { 63 | this.plugin.settings.showUnassigned = value; 64 | await this.plugin.saveSettings(); 65 | }); 66 | }); 67 | }) 68 | .addSetting(setting => { 69 | setting 70 | .setName('Threshold') 71 | .setDesc('Only show notification if there are less than X possible commands (use 0 to disable)') 72 | .addText(text => { 73 | text 74 | .setValue(String(this.plugin.settings.threshold)) 75 | .onChange(async (value) => { 76 | if(isNaN(Number(value)) || value === undefined) { 77 | new Notice("please specify a valid number"); 78 | return; 79 | } 80 | this.plugin.settings.threshold = Number(value); 81 | await this.plugin.saveSettings(); 82 | }); 83 | 84 | }); 85 | }) 86 | .addSetting(setting => { 87 | setting 88 | .setName('Description of actions') 89 | .setDesc("Show name and Win/Mac shortcuts of any command you invoke") 90 | .addToggle(toggle => { 91 | toggle 92 | .setValue(this.plugin.settings.descriptionOfActions) 93 | .onChange(async (value) => { 94 | this.plugin.settings.descriptionOfActions = value; 95 | await this.plugin.saveSettings(); 96 | }); 97 | }); 98 | }) 99 | .addSetting(setting => { 100 | setting 101 | .setName('Notification timeout') 102 | .setDesc('show notifications for x seconds') 103 | .addText(text => { 104 | text 105 | .setValue(String(this.plugin.settings.notificationTimeout)) 106 | .onChange(async (value) => { 107 | if(isNaN(Number(value)) || value === undefined) { 108 | new Notice("please specify a valid number"); 109 | return; 110 | } 111 | this.plugin.settings.notificationTimeout = Number(value); 112 | await this.plugin.saveSettings(); 113 | }); 114 | 115 | }).addExtraButton(button => { 116 | button 117 | .setIcon("rotate-ccw") 118 | .setTooltip("Reset to default") 119 | .onClick(async () => { 120 | this.plugin.settings.notificationTimeout = DEFAULT_SETTINGS.notificationTimeout; 121 | await this.plugin.saveSettings(); 122 | this.display(); 123 | }); 124 | }); 125 | }) 126 | .addSetting(setting => { 127 | setting 128 | .setName("Export template") 129 | .setDesc('Available variables are: {{commandId}}, {{commandName}}, {{hotkey}}') 130 | .addTextArea((textArea: TextAreaComponent) => { 131 | textArea 132 | .setValue(this.plugin.settings.template) 133 | .setPlaceholder(DEFAULT_SETTINGS.template) 134 | .onChange(async (value) => { 135 | this.plugin.settings.template = value; 136 | await this.plugin.saveSettings(); 137 | }); 138 | textArea.inputEl.setAttr("rows", 8); 139 | textArea.inputEl.setAttr("cols", 50); 140 | }).addExtraButton(button => { 141 | button 142 | .setIcon("rotate-ccw") 143 | .setTooltip("Reset to default") 144 | .onClick(async () => { 145 | this.plugin.settings.template = DEFAULT_SETTINGS.template; 146 | await this.plugin.saveSettings(); 147 | this.display(); 148 | }); 149 | }); 150 | }); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import {Command, Hotkey, normalizePath, Notice, Platform, Plugin, TFile, UserEvent} from 'obsidian'; 2 | import {DEFAULT_SETTINGS, KeyPromoterSettings, KeyPromoterSettingsTab} from "./settings"; 3 | import {around, dedupe} from "monkey-around"; 4 | import {StatisticsModal} from "./StatisticsModal"; 5 | 6 | declare module "obsidian" { 7 | interface App { 8 | setting: SettingModal, 9 | commands: CommandManager, 10 | hotkeyManager: HotkeyManager, 11 | } 12 | interface SettingModal { 13 | open(): void; 14 | openTabById(id: string): HotkeySettingTab; 15 | } 16 | interface HotkeySettingTab { 17 | setQuery(query: string): void; 18 | } 19 | interface CommandManager { 20 | executeCommandById(id: string, evt?: UserEvent): boolean; 21 | findCommand(id: string): Command | undefined; 22 | commands: Record; 23 | editorCommands: Record; 24 | } 25 | interface CustomHotkey { 26 | modifiers: string[] 27 | key: string 28 | } 29 | interface HotkeyManager { 30 | customKeys: Record; 31 | } 32 | } 33 | 34 | 35 | export default class KeyPromoterPlugin extends Plugin { 36 | settings: KeyPromoterSettings; 37 | uninstallCommand: any; 38 | 39 | hasParentSelector(el: HTMLElement, clazz: string): boolean { 40 | return Boolean(el.closest(clazz)); 41 | } 42 | 43 | async onload(): Promise { 44 | console.log('loading plugin Key Promoter ' + this.manifest.version); 45 | await this.loadSettings(); 46 | 47 | 48 | this.uninstallCommand = around(this.app.commands, { 49 | //@ts-expect-error 50 | // eslint-disable-next-line @typescript-eslint/ban-types 51 | executeCommandById(oldMethod: Function) { 52 | return dedupe("key-promoter", oldMethod, function (...args: any) { 53 | console.log("testing"); 54 | const result = oldMethod && oldMethod.apply(this, args); 55 | 56 | //@ts-ignore 57 | const command: Command = this.app.commands.findCommand(args[0]); 58 | if(!command) { 59 | return result; 60 | } 61 | //@ts-ignore 62 | const keyPromoterPlugin: KeyPromoterPlugin = this.app.plugins.plugins["key-promoter"]; 63 | 64 | if (keyPromoterPlugin.settings.keyboardStatistics[command.id]) { 65 | keyPromoterPlugin.settings.keyboardStatistics[command.id]++; 66 | } else { 67 | keyPromoterPlugin.settings.keyboardStatistics[command.id] = 1; 68 | } 69 | keyPromoterPlugin.saveSettings(); 70 | 71 | if(!keyPromoterPlugin.settings.descriptionOfActions) { 72 | return result; 73 | } 74 | 75 | const timeout = keyPromoterPlugin.settings.notificationTimeout; 76 | new Notice(command.name + " via " + this.app.plugins.plugins["key-promoter"].getHotkeysForCommand(command).join(), timeout * 1000); 77 | return result; 78 | }) 79 | } 80 | }); 81 | 82 | this.addSettingTab(new KeyPromoterSettingsTab(this)); 83 | 84 | this.addCommand({ 85 | id: "statistics", 86 | name: "Statistics", 87 | callback: async () => { 88 | new StatisticsModal(this).open(); 89 | } 90 | }); 91 | 92 | this.addCommand({ 93 | id: 'key-promoter', 94 | name: 'Export hotkeys', 95 | callback: async () => { 96 | if (this.app.vault.getAbstractFileByPath("hotkeys-export.md") instanceof TFile) { 97 | new Notice("There is already a exported file, delete the hotkeys-export.md file from your vault first"); 98 | return; 99 | } 100 | let content = ""; 101 | 102 | 103 | for (const command of this.getCommands()) { 104 | const hotkeys = this.getHotkeysForCommand(command); 105 | 106 | const singleCommand = this.settings.template 107 | .replace('{{commandId}}', command.id) 108 | .replace('{{commandName}}', command.name) 109 | .replace('{{hotkey}}', hotkeys.join()); 110 | content = content.concat(singleCommand); 111 | } 112 | 113 | 114 | const file = await this.app.vault.create(normalizePath("hotkeys-export.md"), content); 115 | await this.app.workspace.getLeaf().openFile(file, { 116 | state: {mode: 'edit'}, 117 | }) 118 | 119 | new Notice("Exported hotkeys"); 120 | } 121 | }); 122 | 123 | this.registerDomEvent(document, 'click', (event: MouseEvent) => { 124 | if (event.target == undefined) return; 125 | 126 | 127 | //@ts-ignore 128 | let label = event.target.ariaLabel; 129 | //@ts-ignore 130 | if (!label) label = event.target.innerText; 131 | if (!label) return; 132 | 133 | const ignoredSelectors = [ 134 | ".mod-settings", 135 | ".nav-files-container", 136 | ".markdown-preview-view", 137 | ".markdown-source-view", 138 | ".cm-editor", 139 | ".CodeMirror-line", 140 | ".modal", 141 | ".rss-feeds-folders" 142 | ]; 143 | 144 | //don't show notifications when in settings, file explorer, etc. 145 | for (const ignoredSelector of ignoredSelectors) { 146 | if (this.hasParentSelector(event.target as HTMLElement, ignoredSelector)) return; 147 | } 148 | 149 | //@ts-ignore 150 | let commands: Command[] = Object.values(this.app.commands.commands); 151 | commands = commands.filter((command: Command) => { 152 | /* 153 | due to different capitalisation and different text content check for contains, not equals 154 | i.e. the button named 'close' executes the command 'close active pane' 155 | */ 156 | return command.name.toLowerCase().contains(label.toLowerCase()); 157 | }); 158 | if (this.settings.threshold != 0 && commands.length > this.settings.threshold) { 159 | return; 160 | } 161 | 162 | commands.forEach((command) => { 163 | if (this.settings.mouseStatistics[command.id]) { 164 | this.settings.mouseStatistics[command.id]++; 165 | } else { 166 | this.settings.mouseStatistics[command.id] = 1; 167 | } 168 | this.saveSettings(); 169 | 170 | const hotkeys = this.getHotkeysForCommand(command); 171 | if(hotkeys.length == 0) { 172 | if (!this.settings.showUnassigned) { 173 | return; 174 | } 175 | const notice = new Notice("", this.settings.notificationTimeout * 1000); 176 | notice.messageEl.createEl('span', {text: 'Hotkey for '}); 177 | notice.messageEl.createEl('b', {text: command.name}); 178 | notice.messageEl.createEl('span', {text: ' is not set'}); 179 | notice.messageEl.onClickEvent(async () => { 180 | this.app.setting.open(); 181 | const hotkeySettings = this.app.setting.openTabById('hotkeys'); 182 | hotkeySettings.setQuery(command.id); 183 | }); 184 | } else { 185 | for (const hotkey of hotkeys) { 186 | const notice = new Notice("", this.settings.notificationTimeout * 1000); 187 | notice.messageEl.createEl('span', {text: 'Hotkey for '}); 188 | notice.messageEl.createEl('b', {text: command.name}); 189 | notice.messageEl.createEl('span', {text: ' is '}); 190 | notice.messageEl.createEl('code', {text: hotkey}); 191 | notice.messageEl.onClickEvent(async () => { 192 | this.app.setting.open(); 193 | const hotkeySettings = this.app.setting.openTabById('hotkeys'); 194 | hotkeySettings.setQuery(command.id); 195 | }); 196 | } 197 | 198 | } 199 | }); 200 | }); 201 | } 202 | 203 | onunload(): void { 204 | if (this.uninstallCommand) { 205 | this.uninstallCommand(); 206 | } 207 | console.log('unloading plugin key promoter'); 208 | } 209 | 210 | getCommands(): Set { 211 | const result = new Set(); 212 | for (const command of Object.values(this.app.commands.commands)) { 213 | result.add(command); 214 | } 215 | for (const command of Object.values(this.app.commands.editorCommands)) { 216 | result.add(command); 217 | } 218 | return result; 219 | } 220 | 221 | getHotkeysForCommand(command: Command): string[] { 222 | let hotkeys: string[] = []; 223 | if (this.app.hotkeyManager.customKeys[command.id]) { 224 | this.app.hotkeyManager.customKeys[command.id].forEach((hotkey: Hotkey) => { 225 | if (hotkey.modifiers) { 226 | const modifiers = hotkey.modifiers.join("+").replace('Mod', Platform.isMacOS ? 'Cmd' : 'Ctrl'); 227 | hotkeys = hotkeys.concat(modifiers + " + " + hotkey.key); 228 | } else { 229 | hotkeys = hotkeys.concat(hotkey.key); 230 | } 231 | }) 232 | }else if (command.hotkeys){ 233 | command.hotkeys.forEach((hotkey: Hotkey) => { 234 | if (hotkey.modifiers) { 235 | const modifiers = hotkey.modifiers.join("+").replace('Mod', Platform.isMacOS ? 'Cmd' : 'Ctrl'); 236 | hotkeys = hotkeys.concat(modifiers + " + " + hotkey.key); 237 | } 238 | }) 239 | } 240 | return hotkeys; 241 | } 242 | 243 | async loadSettings(): Promise { 244 | this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); 245 | if (!this.settings.keyboardStatistics) { 246 | this.settings.keyboardStatistics = {}; 247 | } 248 | if (!this.settings.mouseStatistics) { 249 | this.settings.mouseStatistics = {}; 250 | } 251 | await this.saveSettings(); 252 | } 253 | 254 | async saveSettings(): Promise { 255 | await this.saveData(this.settings); 256 | } 257 | } 258 | --------------------------------------------------------------------------------