PluginProbe
Gutenberg / trunk
Gutenberg vtrunk
24.1.0 24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 All 404 releases
gutenberg / build / scripts / keyboard-shortcuts / index.js

index.js in Gutenberg trunk, at build/scripts/keyboard-shortcuts/index.js

321 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function() {
2 "use strict";
3 var wp;
4 (wp ||= {}).keyboardShortcuts = (() => {
5 var __create = Object.create;
6 var __defProp = Object.defineProperty;
7 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
8 var __getOwnPropNames = Object.getOwnPropertyNames;
9 var __getProtoOf = Object.getPrototypeOf;
10 var __hasOwnProp = Object.prototype.hasOwnProperty;
11 var __commonJS = (cb, mod) => function __require() {
12 return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
13 };
14 var __export = (target, all) => {
15 for (var name in all)
16 __defProp(target, name, { get: all[name], enumerable: true });
17 };
18 var __copyProps = (to, from, except, desc) => {
19 if (from && typeof from === "object" || typeof from === "function") {
20 for (let key of __getOwnPropNames(from))
21 if (!__hasOwnProp.call(to, key) && key !== except)
22 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23 }
24 return to;
25 };
26 var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
27 // If the importer is in node compatibility mode or this is not an ESM
28 // file that has been converted to a CommonJS file using a Babel-
29 // compatible transform (i.e. "__esModule" has not been set), then set
30 // "default" to the CommonJS "module.exports" for node compatibility.
31 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
32 mod
33 ));
34 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
36 // package-external:@wordpress/data
37 var require_data = __commonJS({
38 "package-external:@wordpress/data"(exports, module) {
39 module.exports = window.wp.data;
40 }
41 });
42
43 // package-external:@wordpress/keycodes
44 var require_keycodes = __commonJS({
45 "package-external:@wordpress/keycodes"(exports, module) {
46 module.exports = window.wp.keycodes;
47 }
48 });
49
50 // package-external:@wordpress/element
51 var require_element = __commonJS({
52 "package-external:@wordpress/element"(exports, module) {
53 module.exports = window.wp.element;
54 }
55 });
56
57 // vendor-external:react/jsx-runtime
58 var require_jsx_runtime = __commonJS({
59 "vendor-external:react/jsx-runtime"(exports, module) {
60 module.exports = window.ReactJSXRuntime;
61 }
62 });
63
64 // packages/keyboard-shortcuts/build-module/index.mjs
65 var index_exports = {};
66 __export(index_exports, {
67 ShortcutProvider: () => ShortcutProvider,
68 __unstableUseShortcutEventMatch: () => useShortcutEventMatch,
69 store: () => store,
70 useShortcut: () => useShortcut
71 });
72
73 // packages/keyboard-shortcuts/build-module/store/index.mjs
74 var import_data2 = __toESM(require_data(), 1);
75
76 // packages/keyboard-shortcuts/build-module/store/reducer.mjs
77 function reducer(state = {}, action) {
78 switch (action.type) {
79 case "REGISTER_SHORTCUT":
80 return {
81 ...state,
82 [action.name]: {
83 category: action.category,
84 keyCombination: action.keyCombination,
85 aliases: action.aliases,
86 description: action.description
87 }
88 };
89 case "UNREGISTER_SHORTCUT":
90 const { [action.name]: actionName, ...remainingState } = state;
91 return remainingState;
92 }
93 return state;
94 }
95 var reducer_default = reducer;
96
97 // packages/keyboard-shortcuts/build-module/store/actions.mjs
98 var actions_exports = {};
99 __export(actions_exports, {
100 registerShortcut: () => registerShortcut,
101 unregisterShortcut: () => unregisterShortcut
102 });
103 function registerShortcut({
104 name,
105 category,
106 description,
107 keyCombination,
108 aliases
109 }) {
110 return {
111 type: "REGISTER_SHORTCUT",
112 name,
113 category,
114 keyCombination,
115 aliases,
116 description
117 };
118 }
119 function unregisterShortcut(name) {
120 return {
121 type: "UNREGISTER_SHORTCUT",
122 name
123 };
124 }
125
126 // packages/keyboard-shortcuts/build-module/store/selectors.mjs
127 var selectors_exports = {};
128 __export(selectors_exports, {
129 getAllShortcutKeyCombinations: () => getAllShortcutKeyCombinations,
130 getAllShortcutRawKeyCombinations: () => getAllShortcutRawKeyCombinations,
131 getCategoryShortcuts: () => getCategoryShortcuts,
132 getKeyboardShortcut: () => getKeyboardShortcut,
133 getShortcutAliases: () => getShortcutAliases,
134 getShortcutDescription: () => getShortcutDescription,
135 getShortcutKeyCombination: () => getShortcutKeyCombination,
136 getShortcutRepresentation: () => getShortcutRepresentation
137 });
138 var import_data = __toESM(require_data(), 1);
139 var import_keycodes = __toESM(require_keycodes(), 1);
140 var EMPTY_ARRAY = [];
141 var FORMATTING_METHODS = {
142 /**
143 * Display formatting.
144 */
145 display: import_keycodes.displayShortcut,
146 /**
147 * Raw shortcut formatting.
148 */
149 raw: import_keycodes.rawShortcut,
150 /**
151 * ARIA label formatting.
152 */
153 ariaLabel: import_keycodes.shortcutAriaLabel
154 };
155 function getKeyCombinationRepresentation(shortcut, representation) {
156 if (!shortcut) {
157 return null;
158 }
159 return shortcut.modifier ? FORMATTING_METHODS[representation][shortcut.modifier](
160 shortcut.character
161 ) : shortcut.character;
162 }
163 function getShortcutKeyCombination(state, name) {
164 return state[name] ? state[name].keyCombination : null;
165 }
166 function getShortcutRepresentation(state, name, representation = "display") {
167 const shortcut = getShortcutKeyCombination(state, name);
168 return getKeyCombinationRepresentation(shortcut, representation);
169 }
170 var getKeyboardShortcut = (0, import_data.createSelector)(
171 (state, name) => {
172 const combination = getShortcutKeyCombination(state, name);
173 if (!combination) {
174 return null;
175 }
176 return import_keycodes.keyboardShortcut[combination.modifier ?? "undefined"](
177 combination.character
178 );
179 },
180 (state, name) => [state[name]]
181 );
182 function getShortcutDescription(state, name) {
183 return state[name] ? state[name].description : null;
184 }
185 function getShortcutAliases(state, name) {
186 return state[name] && state[name].aliases ? state[name].aliases : EMPTY_ARRAY;
187 }
188 var getAllShortcutKeyCombinations = (0, import_data.createSelector)(
189 (state, name) => {
190 return [
191 getShortcutKeyCombination(state, name),
192 ...getShortcutAliases(state, name)
193 ].filter(
194 (combination) => !!combination
195 );
196 },
197 (state, name) => [state[name]]
198 );
199 var getAllShortcutRawKeyCombinations = (0, import_data.createSelector)(
200 (state, name) => {
201 return getAllShortcutKeyCombinations(state, name).map(
202 (combination) => getKeyCombinationRepresentation(combination, "raw")
203 );
204 },
205 (state, name) => [state[name]]
206 );
207 var getCategoryShortcuts = (0, import_data.createSelector)(
208 (state, categoryName) => {
209 return Object.entries(state).filter(([, shortcut]) => shortcut.category === categoryName).map(([name]) => name);
210 },
211 (state) => [state]
212 );
213
214 // packages/keyboard-shortcuts/build-module/store/index.mjs
215 var STORE_NAME = "core/keyboard-shortcuts";
216 var store = (0, import_data2.createReduxStore)(STORE_NAME, {
217 reducer: reducer_default,
218 actions: actions_exports,
219 selectors: selectors_exports
220 });
221 (0, import_data2.register)(store);
222
223 // packages/keyboard-shortcuts/build-module/hooks/use-shortcut.mjs
224 var import_element3 = __toESM(require_element(), 1);
225
226 // packages/keyboard-shortcuts/build-module/hooks/use-shortcut-event-match.mjs
227 var import_data3 = __toESM(require_data(), 1);
228 var import_element = __toESM(require_element(), 1);
229 var import_keycodes2 = __toESM(require_keycodes(), 1);
230 function useShortcutEventMatch() {
231 const { getAllShortcutKeyCombinations: getAllShortcutKeyCombinations2 } = (0, import_data3.useSelect)(
232 store
233 );
234 return (0, import_element.useCallback)(
235 (name, event) => {
236 return getAllShortcutKeyCombinations2(name).some(
237 (combination) => {
238 if (!combination) {
239 return false;
240 }
241 return import_keycodes2.isKeyboardEvent[combination.modifier ?? "undefined"](event, combination.character);
242 }
243 );
244 },
245 [getAllShortcutKeyCombinations2]
246 );
247 }
248
249 // packages/keyboard-shortcuts/build-module/context.mjs
250 var import_element2 = __toESM(require_element(), 1);
251 var globalShortcuts = /* @__PURE__ */ new Set();
252 var globalListener = (event) => {
253 for (const keyboardShortcut2 of globalShortcuts) {
254 keyboardShortcut2(event);
255 }
256 };
257 var context = (0, import_element2.createContext)({
258 add: (shortcut) => {
259 if (globalShortcuts.size === 0) {
260 document.addEventListener("keydown", globalListener);
261 }
262 globalShortcuts.add(shortcut);
263 },
264 delete: (shortcut) => {
265 globalShortcuts.delete(shortcut);
266 if (globalShortcuts.size === 0) {
267 document.removeEventListener("keydown", globalListener);
268 }
269 }
270 });
271 context.displayName = "KeyboardShortcutsContext";
272
273 // packages/keyboard-shortcuts/build-module/hooks/use-shortcut.mjs
274 function useShortcut(name, callback, { isDisabled = false } = {}) {
275 const shortcuts = (0, import_element3.useContext)(context);
276 const isMatch = useShortcutEventMatch();
277 const callbackRef = (0, import_element3.useRef)();
278 (0, import_element3.useEffect)(() => {
279 callbackRef.current = callback;
280 }, [callback]);
281 (0, import_element3.useEffect)(() => {
282 if (isDisabled) {
283 return;
284 }
285 function _callback(event) {
286 if (isMatch(name, event) && callbackRef.current) {
287 callbackRef.current(event);
288 }
289 }
290 shortcuts.add(_callback);
291 return () => {
292 shortcuts.delete(_callback);
293 };
294 }, [name, isDisabled, shortcuts, isMatch]);
295 }
296
297 // packages/keyboard-shortcuts/build-module/components/shortcut-provider.mjs
298 var import_element4 = __toESM(require_element(), 1);
299 var import_jsx_runtime = __toESM(require_jsx_runtime(), 1);
300 var { Provider } = context;
301 function ShortcutProvider(props) {
302 const [keyboardShortcuts] = (0, import_element4.useState)(
303 () => /* @__PURE__ */ new Set()
304 );
305 function onKeyDown(event) {
306 if (props.onKeyDown) {
307 props.onKeyDown(event);
308 }
309 const nativeEvent = event.nativeEvent;
310 for (const keyboardShortcut2 of keyboardShortcuts) {
311 keyboardShortcut2(nativeEvent);
312 }
313 }
314 return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Provider, { value: keyboardShortcuts, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...props, onKeyDown }) });
315 }
316 return __toCommonJS(index_exports);
317 })();
318 (window.wp ||= {}).keyboardShortcuts = wp.keyboardShortcuts;
319 })();
320 //# sourceMappingURL=index.js.map
321