PluginProbe
Gutenberg / 23.7.2
Gutenberg v23.7.2
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 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / keyboard-shortcuts / index.js

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

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