UNPKG

@wordpress/commands

Version:
112 lines (110 loc) 3.48 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/commands/src/hooks/use-command.js var use_command_exports = {}; __export(use_command_exports, { useCommand: () => useCommand, useCommands: () => useCommands }); module.exports = __toCommonJS(use_command_exports); var import_element = require("@wordpress/element"); var import_data = require("@wordpress/data"); var import_store = require("../store"); function useCommand(command) { const { registerCommand, unregisterCommand } = (0, import_data.useDispatch)(import_store.store); const currentCallbackRef = (0, import_element.useRef)(command.callback); (0, import_element.useEffect)(() => { currentCallbackRef.current = command.callback; }, [command.callback]); (0, import_element.useEffect)(() => { if (command.disabled) { return; } registerCommand({ name: command.name, context: command.context, label: command.label, searchLabel: command.searchLabel, icon: command.icon, keywords: command.keywords, callback: (...args) => currentCallbackRef.current(...args) }); return () => { unregisterCommand(command.name); }; }, [ command.name, command.label, command.searchLabel, command.icon, command.context, command.keywords, command.disabled, registerCommand, unregisterCommand ]); } function useCommands(commands) { const { registerCommand, unregisterCommand } = (0, import_data.useDispatch)(import_store.store); const currentCallbacksRef = (0, import_element.useRef)({}); (0, import_element.useEffect)(() => { if (!commands) { return; } commands.forEach((command) => { if (command.callback) { currentCallbacksRef.current[command.name] = command.callback; } }); }, [commands]); (0, import_element.useEffect)(() => { if (!commands) { return; } commands.forEach((command) => { if (command.disabled) { return; } registerCommand({ name: command.name, context: command.context, label: command.label, searchLabel: command.searchLabel, icon: command.icon, keywords: command.keywords, callback: (...args) => { const callback = currentCallbacksRef.current[command.name]; if (callback) { callback(...args); } } }); }); return () => { commands.forEach((command) => { unregisterCommand(command.name); }); }; }, [commands, registerCommand, unregisterCommand]); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useCommand, useCommands }); //# sourceMappingURL=use-command.js.map