mirror of
https://github.com/discourse/discourse.git
synced 2024-11-28 00:33:37 +08:00
DEV: Support adding keybindings via plugins (#9177)
This commit is contained in:
parent
6c948f27ea
commit
40f1201b39
|
@ -6,7 +6,7 @@ import { ajax } from "discourse/lib/ajax";
|
|||
import { throttle } from "@ember/runloop";
|
||||
import { INPUT_DELAY } from "discourse-common/config/environment";
|
||||
|
||||
const bindings = {
|
||||
export let bindings = {
|
||||
"!": { postAction: "showFlags" },
|
||||
"#": { handler: "goToPost", anonymous: true },
|
||||
"/": { handler: "toggleSearch", anonymous: true },
|
||||
|
@ -406,14 +406,16 @@ export default {
|
|||
},
|
||||
|
||||
_globalBindToFunction(func, binding) {
|
||||
if (typeof this[func] === "function") {
|
||||
let funcToBind = typeof func === "function" ? func : this[func];
|
||||
if (typeof funcToBind === "function") {
|
||||
this.keyTrapper.bindGlobal(binding, this[func].bind(this));
|
||||
}
|
||||
},
|
||||
|
||||
_bindToFunction(func, binding) {
|
||||
if (typeof this[func] === "function") {
|
||||
this.keyTrapper.bind(binding, this[func].bind(this));
|
||||
let funcToBind = typeof func === "function" ? func : this[func];
|
||||
if (typeof funcToBind === "function") {
|
||||
this.keyTrapper.bind(binding, funcToBind.bind(this));
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/*global Mousetrap:true*/
|
||||
import deprecated from "discourse-common/lib/deprecated";
|
||||
import { iconNode } from "discourse-common/lib/icon-library";
|
||||
import { addDecorator } from "discourse/widgets/post-cooked";
|
||||
|
@ -50,9 +51,10 @@ import { addCategorySortCriteria } from "discourse/components/edit-category-sett
|
|||
import { queryRegistry } from "discourse/widgets/widget";
|
||||
import Composer from "discourse/models/composer";
|
||||
import { on } from "@ember/object/evented";
|
||||
import KeyboardShortcuts, { bindings } from "discourse/lib/keyboard-shortcuts";
|
||||
|
||||
// If you add any methods to the API ensure you bump up this number
|
||||
const PLUGIN_API_VERSION = "0.8.38";
|
||||
const PLUGIN_API_VERSION = "0.8.39";
|
||||
|
||||
class PluginApi {
|
||||
constructor(version, container) {
|
||||
|
@ -227,6 +229,14 @@ class PluginApi {
|
|||
}
|
||||
}
|
||||
|
||||
addKeyboardShortcut(shortcut, callback, opts = {}) {
|
||||
shortcut = shortcut.trim().replace(/\s/g, ""); // Strip all whitespace
|
||||
let newBinding = {};
|
||||
newBinding[shortcut] = Object.assign({ handler: callback }, opts);
|
||||
Object.assign(bindings, newBinding);
|
||||
KeyboardShortcuts.bindEvents(Mousetrap, this.container);
|
||||
}
|
||||
|
||||
/**
|
||||
* addPosterIcon(callback)
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue
Block a user