headerformatter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Handles applying header styles to text.
  16. *
  17. */
  18. goog.provide('goog.editor.plugins.HeaderFormatter');
  19. goog.require('goog.editor.Command');
  20. goog.require('goog.editor.Plugin');
  21. goog.require('goog.userAgent');
  22. /**
  23. * Applies header styles to text.
  24. * @constructor
  25. * @extends {goog.editor.Plugin}
  26. * @final
  27. */
  28. goog.editor.plugins.HeaderFormatter = function() {
  29. goog.editor.Plugin.call(this);
  30. };
  31. goog.inherits(goog.editor.plugins.HeaderFormatter, goog.editor.Plugin);
  32. /** @override */
  33. goog.editor.plugins.HeaderFormatter.prototype.getTrogClassId = function() {
  34. return 'HeaderFormatter';
  35. };
  36. // TODO(user): Move execCommand functionality from basictextformatter into
  37. // here for headers. I'm not doing this now because it depends on the
  38. // switch statements in basictextformatter and we'll need to abstract that out
  39. // in order to separate out any of the functions from basictextformatter.
  40. /**
  41. * Commands that can be passed as the optional argument to execCommand.
  42. * @enum {string}
  43. */
  44. goog.editor.plugins.HeaderFormatter.HEADER_COMMAND = {
  45. H1: 'H1',
  46. H2: 'H2',
  47. H3: 'H3',
  48. H4: 'H4'
  49. };
  50. /**
  51. * @override
  52. */
  53. goog.editor.plugins.HeaderFormatter.prototype.handleKeyboardShortcut = function(
  54. e, key, isModifierPressed) {
  55. if (!isModifierPressed) {
  56. return false;
  57. }
  58. var command = null;
  59. switch (key) {
  60. case '1':
  61. command = goog.editor.plugins.HeaderFormatter.HEADER_COMMAND.H1;
  62. break;
  63. case '2':
  64. command = goog.editor.plugins.HeaderFormatter.HEADER_COMMAND.H2;
  65. break;
  66. case '3':
  67. command = goog.editor.plugins.HeaderFormatter.HEADER_COMMAND.H3;
  68. break;
  69. case '4':
  70. command = goog.editor.plugins.HeaderFormatter.HEADER_COMMAND.H4;
  71. break;
  72. }
  73. if (command) {
  74. this.getFieldObject().execCommand(
  75. goog.editor.Command.FORMAT_BLOCK, command);
  76. // Prevent default isn't enough to cancel tab navigation in FF.
  77. if (goog.userAgent.GECKO) {
  78. e.stopPropagation();
  79. }
  80. return true;
  81. }
  82. return false;
  83. };