Skip to content

Commit

Permalink
add canExecute method to CommandManager
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc committed Jun 6, 2024
1 parent 4597b27 commit a5e52ee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion ace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ export namespace Ace {
removeListener(name: string, callback: Function): void;
removeEventListener(name: string, callback: Function): void;

exec(command: string, editor: Editor, args: any): boolean;
exec(command: string | string[] | Command, editor: Editor, args: any): boolean;
canExecute(command: string | Command, editor: Editor): boolean;
toggleRecording(editor: Editor): void;
replay(editor: Editor): void;
addCommand(command: Command): void;
Expand Down
32 changes: 25 additions & 7 deletions src/commands/command_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,31 @@ class CommandManager extends MultiHashHandler{
}
return false;
}

if (typeof command === "string")
command = this.commands[command];

if (!this.canExecute(command, editor)) {
return false;
}

var e = {editor: editor, command: command, args: args};
e.returnValue = this._emit("exec", e);
this._signal("afterExec", e);

return e.returnValue === false ? false : true;
}

/**
*
* @param {string | import("../../ace-internal").Ace.Command} command
* @param {Editor} editor
* @returns {boolean}
*/
canExecute(command, editor) {
if (typeof command === "string")
command = this.commands[command];

if (!command)
return false;

Expand All @@ -50,13 +71,10 @@ class CommandManager extends MultiHashHandler{

if (this.$checkCommandState != false && command.isAvailable && !command.isAvailable(editor))
return false;

var e = {editor: editor, command: command, args: args};
e.returnValue = this._emit("exec", e);
this._signal("afterExec", e);

return e.returnValue === false ? false : true;

return true;
}


/**
* @param {Editor} editor
Expand Down
20 changes: 12 additions & 8 deletions src/mouse/touch_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ exports.addTouchListeners = function(el, editor) {
var updateMenu = function() {
var selected = editor.getCopyText();
var hasUndo = editor.session.getUndoManager().hasUndo();
var commands = editor.commands.byName;
contextMenu.replaceChild(
dom.buildDom(isOpen ? ["span",
!selected && commands["selectall"] && ["span", { class: "ace_mobile-button", action: "selectall" }, "Select All"],
selected && commands["copy"] && ["span", { class: "ace_mobile-button", action: "copy" }, "Copy"],
selected && commands["cut"] && ["span", { class: "ace_mobile-button", action: "cut" }, "Cut"],
clipboard && commands["paste"] && ["span", { class: "ace_mobile-button", action: "paste" }, "Paste"],
hasUndo && commands["undo"] && ["span", { class: "ace_mobile-button", action: "undo" }, "Undo"],
commands["find"] && ["span", { class: "ace_mobile-button", action: "find" }, "Find"],
commands["openCommandPalette"] && ["span", { class: "ace_mobile-button", action: "openCommandPalette" }, "Palette"]
!selected && canExecuteCommand("selectall") && ["span", { class: "ace_mobile-button", action: "selectall" }, "Select All"],
selected && canExecuteCommand("copy") && ["span", { class: "ace_mobile-button", action: "copy" }, "Copy"],
selected && canExecuteCommand("cut") && ["span", { class: "ace_mobile-button", action: "cut" }, "Cut"],
clipboard && canExecuteCommand("paste") && ["span", { class: "ace_mobile-button", action: "paste" }, "Paste"],
hasUndo && canExecuteCommand("undo") && ["span", { class: "ace_mobile-button", action: "undo" }, "Undo"],
canExecuteCommand("find") && ["span", { class: "ace_mobile-button", action: "find" }, "Find"],
canExecuteCommand("openCommandPalette") && ["span", { class: "ace_mobile-button", action: "openCommandPalette" }, "Palette"]
] : ["span"]),
contextMenu.firstChild
);
};

var canExecuteCommand = function (/** @type {string} */ cmd) {
return editor.commands.canExecute(cmd, editor);
};

var handleClick = function(e) {
var action = e.target.getAttribute("action");

Expand Down

0 comments on commit a5e52ee

Please sign in to comment.