helloworld.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 A simple plugin that inserts 'Hello World!' on command. This
  16. * plugin is intended to be an example of a very simple plugin for plugin
  17. * developers.
  18. *
  19. * @author gak@google.com (Gregory Kick)
  20. * @see helloworld.html
  21. */
  22. goog.provide('goog.demos.editor.HelloWorld');
  23. goog.require('goog.dom');
  24. goog.require('goog.dom.TagName');
  25. goog.require('goog.editor.Plugin');
  26. /**
  27. * Plugin to insert 'Hello World!' into an editable field.
  28. * @constructor
  29. * @extends {goog.editor.Plugin}
  30. * @final
  31. */
  32. goog.demos.editor.HelloWorld = function() {
  33. goog.editor.Plugin.call(this);
  34. };
  35. goog.inherits(goog.demos.editor.HelloWorld, goog.editor.Plugin);
  36. /** @override */
  37. goog.demos.editor.HelloWorld.prototype.getTrogClassId = function() {
  38. return 'HelloWorld';
  39. };
  40. /**
  41. * Commands implemented by this plugin.
  42. * @enum {string}
  43. */
  44. goog.demos.editor.HelloWorld.COMMAND = {
  45. HELLO_WORLD: '+helloWorld'
  46. };
  47. /** @override */
  48. goog.demos.editor.HelloWorld.prototype.isSupportedCommand = function(command) {
  49. return command == goog.demos.editor.HelloWorld.COMMAND.HELLO_WORLD;
  50. };
  51. /**
  52. * Executes a command. Does not fire any BEFORECHANGE, CHANGE, or
  53. * SELECTIONCHANGE events (these are handled by the super class implementation
  54. * of {@code execCommand}.
  55. * @param {string} command Command to execute.
  56. * @override
  57. * @protected
  58. */
  59. goog.demos.editor.HelloWorld.prototype.execCommandInternal = function(command) {
  60. var domHelper = this.getFieldObject().getEditableDomHelper();
  61. var range = this.getFieldObject().getRange();
  62. range.removeContents();
  63. var newNode =
  64. domHelper.createDom(goog.dom.TagName.SPAN, null, 'Hello World!');
  65. range.insertNode(newNode, false);
  66. };