| @@ -1,4 +1,6 @@ | ||
| 1 | +(function() { | |
| 2 | +"use strict"; | |
| 1 | 3 | var wp; |
| 2 | 4 | (wp ||= {}).keyboardShortcuts = (() => { |
| 3 | 5 | var __create = Object.create; |
| 4 | 6 | var __defProp = Object.defineProperty; |
| @@ -126,8 +128,9 @@ | ||
| 126 | 128 | __export(selectors_exports, { |
| 127 | 129 | getAllShortcutKeyCombinations: () => getAllShortcutKeyCombinations, |
| 128 | 130 | getAllShortcutRawKeyCombinations: () => getAllShortcutRawKeyCombinations, |
| 129 | 131 | getCategoryShortcuts: () => getCategoryShortcuts, |
| 132 | + getKeyboardShortcut: () => getKeyboardShortcut, | |
| 130 | 133 | getShortcutAliases: () => getShortcutAliases, |
| 131 | 134 | getShortcutDescription: () => getShortcutDescription, |
| 132 | 135 | getShortcutKeyCombination: () => getShortcutKeyCombination, |
| 133 | 136 | getShortcutRepresentation: () => getShortcutRepresentation |
| @@ -135,10 +138,19 @@ | ||
| 135 | 138 | var import_data = __toESM(require_data(), 1); |
| 136 | 139 | var import_keycodes = __toESM(require_keycodes(), 1); |
| 137 | 140 | var EMPTY_ARRAY = []; |
| 138 | 141 | var FORMATTING_METHODS = { |
| 142 | + /** | |
| 143 | + * Display formatting. | |
| 144 | + */ | |
| 139 | 145 | display: import_keycodes.displayShortcut, |
| 146 | + /** | |
| 147 | + * Raw shortcut formatting. | |
| 148 | + */ | |
| 140 | 149 | raw: import_keycodes.rawShortcut, |
| 150 | + /** | |
| 151 | + * ARIA label formatting. | |
| 152 | + */ | |
| 141 | 153 | ariaLabel: import_keycodes.shortcutAriaLabel |
| 142 | 154 | }; |
| 143 | 155 | function getKeyCombinationRepresentation(shortcut, representation) { |
| 144 | 156 | if (!shortcut) { |
| @@ -154,8 +166,20 @@ | ||
| 154 | 166 | function getShortcutRepresentation(state, name, representation = "display") { |
| 155 | 167 | const shortcut = getShortcutKeyCombination(state, name); |
| 156 | 168 | return getKeyCombinationRepresentation(shortcut, representation); |
| 157 | 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 | + ); | |
| 158 | 182 | function getShortcutDescription(state, name) { |
| 159 | 183 | return state[name] ? state[name].description : null; |
| 160 | 184 | } |
| 161 | 185 | function getShortcutAliases(state, name) { |
| @@ -165,9 +189,11 @@ | ||
| 165 | 189 | (state, name) => { |
| 166 | 190 | return [ |
| 167 | 191 | getShortcutKeyCombination(state, name), |
| 168 | 192 | ...getShortcutAliases(state, name) |
| 169 | - ].filter(Boolean); | |
| 193 | + ].filter( | |
| 194 | + (combination) => !!combination | |
| 195 | + ); | |
| 170 | 196 | }, |
| 171 | 197 | (state, name) => [state[name]] |
| 172 | 198 | ); |
| 173 | 199 | var getAllShortcutRawKeyCombinations = (0, import_data.createSelector)( |
| @@ -194,36 +220,42 @@ | ||
| 194 | 220 | }); |
| 195 | 221 | (0, import_data2.register)(store); |
| 196 | 222 | |
| 197 | 223 | // packages/keyboard-shortcuts/build-module/hooks/use-shortcut.mjs |
| 198 | - var import_element2 = __toESM(require_element(), 1); | |
| 224 | + var import_element3 = __toESM(require_element(), 1); | |
| 199 | 225 | |
| 200 | 226 | // packages/keyboard-shortcuts/build-module/hooks/use-shortcut-event-match.mjs |
| 201 | 227 | var import_data3 = __toESM(require_data(), 1); |
| 228 | + var import_element = __toESM(require_element(), 1); | |
| 202 | 229 | var import_keycodes2 = __toESM(require_keycodes(), 1); |
| 203 | 230 | function useShortcutEventMatch() { |
| 204 | 231 | const { getAllShortcutKeyCombinations: getAllShortcutKeyCombinations2 } = (0, import_data3.useSelect)( |
| 205 | 232 | store |
| 206 | 233 | ); |
| 207 | - function isMatch(name, event) { | |
| 208 | - return getAllShortcutKeyCombinations2(name).some( | |
| 209 | - ({ modifier, character }) => { | |
| 210 | - return import_keycodes2.isKeyboardEvent[modifier](event, character); | |
| 211 | - } | |
| 212 | - ); | |
| 213 | - } | |
| 214 | - return isMatch; | |
| 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 | + ); | |
| 215 | 247 | } |
| 216 | 248 | |
| 217 | 249 | // packages/keyboard-shortcuts/build-module/context.mjs |
| 218 | - var import_element = __toESM(require_element(), 1); | |
| 250 | + var import_element2 = __toESM(require_element(), 1); | |
| 219 | 251 | var globalShortcuts = /* @__PURE__ */ new Set(); |
| 220 | 252 | var globalListener = (event) => { |
| 221 | - for (const keyboardShortcut of globalShortcuts) { | |
| 222 | - keyboardShortcut(event); | |
| 253 | + for (const keyboardShortcut2 of globalShortcuts) { | |
| 254 | + keyboardShortcut2(event); | |
| 223 | 255 | } |
| 224 | 256 | }; |
| 225 | - var context = (0, import_element.createContext)({ | |
| 257 | + var context = (0, import_element2.createContext)({ | |
| 226 | 258 | add: (shortcut) => { |
| 227 | 259 | if (globalShortcuts.size === 0) { |
| 228 | 260 | document.addEventListener("keydown", globalListener); |
| 229 | 261 | } |
| @@ -239,20 +271,20 @@ | ||
| 239 | 271 | context.displayName = "KeyboardShortcutsContext"; |
| 240 | 272 | |
| 241 | 273 | // packages/keyboard-shortcuts/build-module/hooks/use-shortcut.mjs |
| 242 | 274 | function useShortcut(name, callback, { isDisabled = false } = {}) { |
| 243 | - const shortcuts = (0, import_element2.useContext)(context); | |
| 275 | + const shortcuts = (0, import_element3.useContext)(context); | |
| 244 | 276 | const isMatch = useShortcutEventMatch(); |
| 245 | - const callbackRef = (0, import_element2.useRef)(); | |
| 246 | - (0, import_element2.useEffect)(() => { | |
| 277 | + const callbackRef = (0, import_element3.useRef)(); | |
| 278 | + (0, import_element3.useEffect)(() => { | |
| 247 | 279 | callbackRef.current = callback; |
| 248 | 280 | }, [callback]); |
| 249 | - (0, import_element2.useEffect)(() => { | |
| 281 | + (0, import_element3.useEffect)(() => { | |
| 250 | 282 | if (isDisabled) { |
| 251 | 283 | return; |
| 252 | 284 | } |
| 253 | 285 | function _callback(event) { |
| 254 | - if (isMatch(name, event)) { | |
| 286 | + if (isMatch(name, event) && callbackRef.current) { | |
| 255 | 287 | callbackRef.current(event); |
| 256 | 288 | } |
| 257 | 289 | } |
| 258 | 290 | shortcuts.add(_callback); |
| @@ -258,26 +290,31 @@ | ||
| 258 | 290 | shortcuts.add(_callback); |
| 259 | 291 | return () => { |
| 260 | 292 | shortcuts.delete(_callback); |
| 261 | 293 | }; |
| 262 | - }, [name, isDisabled, shortcuts]); | |
| 294 | + }, [name, isDisabled, shortcuts, isMatch]); | |
| 263 | 295 | } |
| 264 | 296 | |
| 265 | 297 | // packages/keyboard-shortcuts/build-module/components/shortcut-provider.mjs |
| 266 | - var import_element3 = __toESM(require_element(), 1); | |
| 298 | + var import_element4 = __toESM(require_element(), 1); | |
| 267 | 299 | var import_jsx_runtime = __toESM(require_jsx_runtime(), 1); |
| 268 | 300 | var { Provider } = context; |
| 269 | 301 | function ShortcutProvider(props) { |
| 270 | - const [keyboardShortcuts] = (0, import_element3.useState)(() => /* @__PURE__ */ new Set()); | |
| 302 | + const [keyboardShortcuts] = (0, import_element4.useState)( | |
| 303 | + () => /* @__PURE__ */ new Set() | |
| 304 | + ); | |
| 271 | 305 | function onKeyDown(event) { |
| 272 | 306 | if (props.onKeyDown) { |
| 273 | 307 | props.onKeyDown(event); |
| 274 | 308 | } |
| 275 | - for (const keyboardShortcut of keyboardShortcuts) { | |
| 276 | - keyboardShortcut(event); | |
| 309 | + const nativeEvent = event.nativeEvent; | |
| 310 | + for (const keyboardShortcut2 of keyboardShortcuts) { | |
| 311 | + keyboardShortcut2(nativeEvent); | |
| 277 | 312 | } |
| 278 | 313 | } |
| 279 | 314 | return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Provider, { value: keyboardShortcuts, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...props, onKeyDown }) }); |
| 280 | 315 | } |
| 281 | 316 | return __toCommonJS(index_exports); |
| 317 | +})(); | |
| 318 | +(window.wp ||= {}).keyboardShortcuts = wp.keyboardShortcuts; | |
| 282 | 319 | })(); |
| 283 | 320 | //# sourceMappingURL=index.js.map |