OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) | 266 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) |
267 res += isMac ? shiftKey : "Shift + "; | 267 res += isMac ? shiftKey : "Shift + "; |
268 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) | 268 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) |
269 res += isMac ? cmdKey : "Win + "; | 269 res += isMac ? cmdKey : "Win + "; |
270 | 270 |
271 return res; | 271 return res; |
272 }; | 272 }; |
273 | 273 |
274 /** | 274 /** |
275 * @param {!KeyboardEvent} event | 275 * @param {!KeyboardEvent} event |
| 276 * @param {string} context |
276 */ | 277 */ |
277 WebInspector.KeyboardShortcut.handleShortcut = function(event) | 278 WebInspector.KeyboardShortcut.handleShortcut = function(event, context) |
278 { | 279 { |
279 var key = WebInspector.KeyboardShortcut.makeKeyFromEvent(event); | 280 var key = WebInspector.KeyboardShortcut.makeKeyFromEvent(event); |
280 var extensions = WebInspector.KeyboardShortcut._keysToActionExtensions[key]; | 281 var entries = WebInspector.KeyboardShortcut._keyToAction[key]; |
281 if (!extensions) | 282 if (!entries) |
282 return; | 283 return; |
283 | 284 |
284 function handler(extension) | 285 for (var i = 0; i < entries.length; ++i) { |
| 286 var entry = entries[i]; |
| 287 if (entry.context && entry.context !== context) |
| 288 continue; |
| 289 var ident = event.keyIdentifier; |
| 290 if (/^F\d+|Control|Shift|Alt|Meta|Win|U\+001B$/.test(ident) || event.ctr
lKey || event.altKey || event.metaKey) { |
| 291 if (handler(entry.actionId)) |
| 292 return; |
| 293 } else { |
| 294 WebInspector.KeyboardShortcut._pendingActionTimer = setTimeout(handl
er.bind(null, entry.actionId), 0); |
| 295 break; |
| 296 } |
| 297 } |
| 298 |
| 299 function actionRegistry() |
285 { | 300 { |
286 var result = extension.instance().handleAction(event); | 301 if (!WebInspector.KeyboardShortcut._actionRegistry) |
| 302 WebInspector.KeyboardShortcut._actionRegistry = WebInspector.moduleM
anager.instance(WebInspector.ActionRegistryAPI); |
| 303 return WebInspector.KeyboardShortcut._actionRegistry; |
| 304 } |
| 305 |
| 306 /** |
| 307 * @param {string} actionId |
| 308 */ |
| 309 function handler(actionId) |
| 310 { |
| 311 var result = actionRegistry().execute(actionId, event); |
287 if (result) | 312 if (result) |
288 event.consume(true); | 313 event.consume(true); |
289 delete WebInspector.KeyboardShortcut._pendingActionTimer; | 314 delete WebInspector.KeyboardShortcut._pendingActionTimer; |
290 return result; | 315 return result; |
291 } | 316 } |
292 | |
293 for (var i = 0; i < extensions.length; ++i) { | |
294 var ident = event.keyIdentifier; | |
295 if (/^F\d+|Control|Shift|Alt|Meta|Win|U\+001B$/.test(ident) || event.ctr
lKey || event.altKey || event.metaKey) { | |
296 if (handler(extensions[i])) | |
297 return; | |
298 } else { | |
299 WebInspector.KeyboardShortcut._pendingActionTimer = setTimeout(handl
er.bind(null, extensions[i]), 0); | |
300 break; | |
301 } | |
302 } | |
303 } | 317 } |
304 | 318 |
305 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey(
"a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); | 319 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey(
"a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); |
306 | 320 |
307 WebInspector.KeyboardShortcut._onKeyPress = function(event) | 321 WebInspector.KeyboardShortcut._onKeyPress = function(event) |
308 { | 322 { |
309 if (!WebInspector.KeyboardShortcut._pendingActionTimer) | 323 if (!WebInspector.KeyboardShortcut._pendingActionTimer) |
310 return; | 324 return; |
311 | 325 |
312 var target = event.target; | 326 var target = event.target; |
313 if (WebInspector.isBeingEdited(event.target)) { | 327 if (WebInspector.isBeingEdited(event.target)) { |
314 clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer); | 328 clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer); |
315 delete WebInspector.KeyboardShortcut._pendingActionTimer; | 329 delete WebInspector.KeyboardShortcut._pendingActionTimer; |
316 } | 330 } |
317 } | 331 } |
318 | 332 |
319 WebInspector.KeyboardShortcut.registerActions = function() | 333 WebInspector.KeyboardShortcut.registerBindings = function() |
320 { | 334 { |
321 document.addEventListener("keypress", WebInspector.KeyboardShortcut._onKeyPr
ess, true); | 335 document.addEventListener("keypress", WebInspector.KeyboardShortcut._onKeyPr
ess, true); |
322 WebInspector.KeyboardShortcut._keysToActionExtensions = {}; | 336 WebInspector.KeyboardShortcut._keyToAction = {}; |
323 var extensions = WebInspector.moduleManager.extensions(WebInspector.ActionDe
legate); | 337 var extensions = WebInspector.moduleManager.extensions("action-bindings"); |
324 extensions.forEach(registerBindings); | 338 extensions.forEach(registerExtension); |
| 339 |
| 340 for (var key in WebInspector.KeyboardShortcut._keyToAction) |
| 341 WebInspector.KeyboardShortcut._keyToAction[key].sort(comparator); |
| 342 |
| 343 /** |
| 344 * @param {{context: (string|undefined), actionId: string}} left |
| 345 * @param {{context: (string|undefined), actionId: string}} right |
| 346 */ |
| 347 function comparator(left, right) |
| 348 { |
| 349 if (left.context) |
| 350 return right.context ? 0 : -1; |
| 351 else |
| 352 return right.context ? 1 : 0; |
| 353 } |
325 | 354 |
326 /** | 355 /** |
327 * @param {!WebInspector.ModuleManager.Extension} extension | 356 * @param {!WebInspector.ModuleManager.Extension} extension |
328 */ | 357 */ |
329 function registerBindings(extension) | 358 function registerExtension(extension) |
330 { | 359 { |
331 var bindings = extension.descriptor().bindings; | 360 var bindings = extension.descriptor().bindings; |
332 for (var i = 0; bindings && i < bindings.length; ++i) { | 361 for (var i = 0; bindings && i < bindings.length; ++i) { |
333 if (!platformMatches(bindings[i].platform)) | 362 if (!platformMatches(bindings[i].platform)) |
334 continue; | 363 continue; |
335 var shortcuts = bindings[i].shortcut.split(/\s+/); | 364 var shortcuts = bindings[i].shortcut.split(/\s+/); |
336 shortcuts.forEach(registerShortcut.bind(null, extension)); | 365 shortcuts.forEach(registerShortcut.bind(null, bindings[i]["actionId"
], bindings[i].context)); |
337 } | 366 } |
338 } | 367 } |
339 | 368 |
340 /** | 369 /** |
341 * @param {!WebInspector.ModuleManager.Extension} extension | 370 * @param {string} actionId |
| 371 * @param {string|undefined} context |
342 * @param {string} shortcut | 372 * @param {string} shortcut |
343 */ | 373 */ |
344 function registerShortcut(extension, shortcut) | 374 function registerShortcut(actionId, context, shortcut) |
345 { | 375 { |
346 var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(short
cut); | 376 var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(short
cut); |
347 if (!key) | 377 if (!key) |
348 return; | 378 return; |
349 if (WebInspector.KeyboardShortcut._keysToActionExtensions[key]) | 379 var entry = { context: context, actionId: actionId }; |
350 WebInspector.KeyboardShortcut._keysToActionExtensions[key].push(exte
nsion); | 380 if (WebInspector.KeyboardShortcut._keyToAction[key]) |
| 381 WebInspector.KeyboardShortcut._keyToAction[key].push(entry); |
351 else | 382 else |
352 WebInspector.KeyboardShortcut._keysToActionExtensions[key] = [extens
ion]; | 383 WebInspector.KeyboardShortcut._keyToAction[key] = [entry]; |
353 } | 384 } |
354 | 385 |
355 /** | 386 /** |
356 * @param {string=} platformsString | 387 * @param {string=} platformsString |
357 * @return {boolean} | 388 * @return {boolean} |
358 */ | 389 */ |
359 function platformMatches(platformsString) | 390 function platformMatches(platformsString) |
360 { | 391 { |
361 if (!platformsString) | 392 if (!platformsString) |
362 return true; | 393 return true; |
363 var platforms = platformsString.split(","); | 394 var platforms = platformsString.split(","); |
364 var isMatch = false; | 395 var isMatch = false; |
365 var currentPlatform = WebInspector.platform(); | 396 var currentPlatform = WebInspector.platform(); |
366 for (var i = 0; !isMatch && i < platforms.length; ++i) | 397 for (var i = 0; !isMatch && i < platforms.length; ++i) |
367 isMatch = platforms[i] === currentPlatform; | 398 isMatch = platforms[i] === currentPlatform; |
368 return isMatch; | 399 return isMatch; |
369 } | 400 } |
370 } | 401 } |
OLD | NEW |