plugin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. // All Rights Reserved.
  15. /**
  16. * @fileoverview Abstract API for TrogEdit plugins.
  17. *
  18. * @see ../demos/editor/editor.html
  19. */
  20. goog.provide('goog.editor.Plugin');
  21. // TODO(user): Remove the dependency on goog.editor.Command asap. Currently only
  22. // needed for execCommand issues with links.
  23. goog.require('goog.events.EventTarget');
  24. goog.require('goog.functions');
  25. goog.require('goog.log');
  26. goog.require('goog.object');
  27. goog.require('goog.reflect');
  28. goog.require('goog.userAgent');
  29. /**
  30. * Abstract API for trogedit plugins.
  31. * @constructor
  32. * @extends {goog.events.EventTarget}
  33. */
  34. goog.editor.Plugin = function() {
  35. goog.events.EventTarget.call(this);
  36. /**
  37. * Whether this plugin is enabled for the registered field object.
  38. * @type {boolean}
  39. * @private
  40. */
  41. this.enabled_ = this.activeOnUneditableFields();
  42. /**
  43. * The field object this plugin is attached to.
  44. * @type {goog.editor.Field}
  45. * @protected
  46. * @deprecated Use goog.editor.Plugin.getFieldObject and
  47. * goog.editor.Plugin.setFieldObject.
  48. */
  49. this.fieldObject = null;
  50. /**
  51. * Indicates if this plugin should be automatically disposed when the
  52. * registered field is disposed. This should be changed to false for
  53. * plugins used as multi-field plugins.
  54. * @type {boolean}
  55. * @private
  56. */
  57. this.autoDispose_ = true;
  58. /**
  59. * The logger for this plugin.
  60. * @type {?goog.log.Logger}
  61. * @protected
  62. */
  63. this.logger = goog.log.getLogger('goog.editor.Plugin');
  64. };
  65. goog.inherits(goog.editor.Plugin, goog.events.EventTarget);
  66. /**
  67. * @return {goog.dom.DomHelper?} The dom helper object associated with the
  68. * currently active field.
  69. */
  70. goog.editor.Plugin.prototype.getFieldDomHelper = function() {
  71. return this.getFieldObject() && this.getFieldObject().getEditableDomHelper();
  72. };
  73. /**
  74. * Sets the field object for use with this plugin.
  75. * @return {goog.editor.Field} The editable field object.
  76. * @protected
  77. * @suppress {deprecated} Until fieldObject can be made private.
  78. */
  79. goog.editor.Plugin.prototype.getFieldObject = function() {
  80. return this.fieldObject;
  81. };
  82. /**
  83. * Sets the field object for use with this plugin.
  84. * @param {goog.editor.Field} fieldObject The editable field object.
  85. * @protected
  86. * @suppress {deprecated} Until fieldObject can be made private.
  87. */
  88. goog.editor.Plugin.prototype.setFieldObject = function(fieldObject) {
  89. this.fieldObject = fieldObject;
  90. };
  91. /**
  92. * Registers the field object for use with this plugin.
  93. * @param {goog.editor.Field} fieldObject The editable field object.
  94. */
  95. goog.editor.Plugin.prototype.registerFieldObject = function(fieldObject) {
  96. this.setFieldObject(fieldObject);
  97. };
  98. /**
  99. * Unregisters and disables this plugin for the current field object.
  100. * @param {goog.editor.Field} fieldObj The field object. For single-field
  101. * plugins, this parameter is ignored.
  102. */
  103. goog.editor.Plugin.prototype.unregisterFieldObject = function(fieldObj) {
  104. if (this.getFieldObject()) {
  105. this.disable(this.getFieldObject());
  106. this.setFieldObject(null);
  107. }
  108. };
  109. /**
  110. * Enables this plugin for the specified, registered field object. A field
  111. * object should only be enabled when it is loaded.
  112. * @param {goog.editor.Field} fieldObject The field object.
  113. */
  114. goog.editor.Plugin.prototype.enable = function(fieldObject) {
  115. if (this.getFieldObject() == fieldObject) {
  116. this.enabled_ = true;
  117. } else {
  118. goog.log.error(
  119. this.logger, 'Trying to enable an unregistered field with ' +
  120. 'this plugin.');
  121. }
  122. };
  123. /**
  124. * Disables this plugin for the specified, registered field object.
  125. * @param {goog.editor.Field} fieldObject The field object.
  126. */
  127. goog.editor.Plugin.prototype.disable = function(fieldObject) {
  128. if (this.getFieldObject() == fieldObject) {
  129. this.enabled_ = false;
  130. } else {
  131. goog.log.error(
  132. this.logger, 'Trying to disable an unregistered field ' +
  133. 'with this plugin.');
  134. }
  135. };
  136. /**
  137. * Returns whether this plugin is enabled for the field object.
  138. *
  139. * @param {goog.editor.Field} fieldObject The field object.
  140. * @return {boolean} Whether this plugin is enabled for the field object.
  141. */
  142. goog.editor.Plugin.prototype.isEnabled = function(fieldObject) {
  143. return this.getFieldObject() == fieldObject ? this.enabled_ : false;
  144. };
  145. /**
  146. * Set if this plugin should automatically be disposed when the registered
  147. * field is disposed.
  148. * @param {boolean} autoDispose Whether to autoDispose.
  149. */
  150. goog.editor.Plugin.prototype.setAutoDispose = function(autoDispose) {
  151. this.autoDispose_ = autoDispose;
  152. };
  153. /**
  154. * @return {boolean} Whether or not this plugin should automatically be disposed
  155. * when it's registered field is disposed.
  156. */
  157. goog.editor.Plugin.prototype.isAutoDispose = function() {
  158. return this.autoDispose_;
  159. };
  160. /**
  161. * @return {boolean} If true, field will not disable the command
  162. * when the field becomes uneditable.
  163. */
  164. goog.editor.Plugin.prototype.activeOnUneditableFields = goog.functions.FALSE;
  165. /**
  166. * @param {string} command The command to check.
  167. * @return {boolean} If true, field will not dispatch change events
  168. * for commands of this type. This is useful for "seamless" plugins like
  169. * dialogs and lorem ipsum.
  170. */
  171. goog.editor.Plugin.prototype.isSilentCommand = goog.functions.FALSE;
  172. /** @override */
  173. goog.editor.Plugin.prototype.disposeInternal = function() {
  174. if (this.getFieldObject()) {
  175. this.unregisterFieldObject(this.getFieldObject());
  176. }
  177. goog.editor.Plugin.superClass_.disposeInternal.call(this);
  178. };
  179. /**
  180. * @return {string} The ID unique to this plugin class. Note that different
  181. * instances off the plugin share the same classId.
  182. */
  183. goog.editor.Plugin.prototype.getTrogClassId;
  184. /**
  185. * An enum of operations that plugins may support.
  186. * @enum {number}
  187. */
  188. goog.editor.Plugin.Op = {
  189. KEYDOWN: 1,
  190. KEYPRESS: 2,
  191. KEYUP: 3,
  192. SELECTION: 4,
  193. SHORTCUT: 5,
  194. EXEC_COMMAND: 6,
  195. QUERY_COMMAND: 7,
  196. PREPARE_CONTENTS_HTML: 8,
  197. CLEAN_CONTENTS_HTML: 10,
  198. CLEAN_CONTENTS_DOM: 11
  199. };
  200. /**
  201. * A map from plugin operations to the names of the methods that
  202. * invoke those operations.
  203. */
  204. goog.editor.Plugin.OPCODE =
  205. goog.object.transpose(goog.reflect.object(goog.editor.Plugin, {
  206. handleKeyDown: goog.editor.Plugin.Op.KEYDOWN,
  207. handleKeyPress: goog.editor.Plugin.Op.KEYPRESS,
  208. handleKeyUp: goog.editor.Plugin.Op.KEYUP,
  209. handleSelectionChange: goog.editor.Plugin.Op.SELECTION,
  210. handleKeyboardShortcut: goog.editor.Plugin.Op.SHORTCUT,
  211. execCommand: goog.editor.Plugin.Op.EXEC_COMMAND,
  212. queryCommandValue: goog.editor.Plugin.Op.QUERY_COMMAND,
  213. prepareContentsHtml: goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML,
  214. cleanContentsHtml: goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML,
  215. cleanContentsDom: goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM
  216. }));
  217. /**
  218. * A set of op codes that run even on disabled plugins.
  219. */
  220. goog.editor.Plugin.IRREPRESSIBLE_OPS = goog.object.createSet(
  221. goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML,
  222. goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML,
  223. goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM);
  224. /**
  225. * Handles keydown. It is run before handleKeyboardShortcut and if it returns
  226. * true handleKeyboardShortcut will not be called.
  227. * @param {!goog.events.BrowserEvent} e The browser event.
  228. * @return {boolean} Whether the event was handled and thus should *not* be
  229. * propagated to other plugins or handleKeyboardShortcut.
  230. */
  231. goog.editor.Plugin.prototype.handleKeyDown;
  232. /**
  233. * Handles keypress. It is run before handleKeyboardShortcut and if it returns
  234. * true handleKeyboardShortcut will not be called.
  235. * @param {!goog.events.BrowserEvent} e The browser event.
  236. * @return {boolean} Whether the event was handled and thus should *not* be
  237. * propagated to other plugins or handleKeyboardShortcut.
  238. */
  239. goog.editor.Plugin.prototype.handleKeyPress;
  240. /**
  241. * Handles keyup.
  242. * @param {!goog.events.BrowserEvent} e The browser event.
  243. * @return {boolean} Whether the event was handled and thus should *not* be
  244. * propagated to other plugins.
  245. */
  246. goog.editor.Plugin.prototype.handleKeyUp;
  247. /**
  248. * Handles selection change.
  249. * @param {!goog.events.BrowserEvent=} opt_e The browser event.
  250. * @param {!Node=} opt_target The node the selection changed to.
  251. * @return {boolean} Whether the event was handled and thus should *not* be
  252. * propagated to other plugins.
  253. */
  254. goog.editor.Plugin.prototype.handleSelectionChange;
  255. /**
  256. * Handles keyboard shortcuts. Preferred to using handleKey* as it will use
  257. * the proper event based on browser and will be more performant. If
  258. * handleKeyPress/handleKeyDown returns true, this will not be called. If the
  259. * plugin handles the shortcut, it is responsible for dispatching appropriate
  260. * events (change, selection change at the time of this comment). If the plugin
  261. * calls execCommand on the editable field, then execCommand already takes care
  262. * of dispatching events.
  263. * NOTE: For performance reasons this is only called when any key is pressed
  264. * in conjunction with ctrl/meta keys OR when a small subset of keys (defined
  265. * in goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_) are pressed without
  266. * ctrl/meta keys. We specifically don't invoke it when altKey is pressed since
  267. * alt key is used in many i8n UIs to enter certain characters.
  268. * @param {!goog.events.BrowserEvent} e The browser event.
  269. * @param {string} key The key pressed.
  270. * @param {boolean} isModifierPressed Whether the ctrl/meta key was pressed or
  271. * not.
  272. * @return {boolean} Whether the event was handled and thus should *not* be
  273. * propagated to other plugins. We also call preventDefault on the event if
  274. * the return value is true.
  275. */
  276. goog.editor.Plugin.prototype.handleKeyboardShortcut;
  277. /**
  278. * Handles execCommand. This default implementation handles dispatching
  279. * BEFORECHANGE, CHANGE, and SELECTIONCHANGE events, and calls
  280. * execCommandInternal to perform the actual command. Plugins that want to
  281. * do their own event dispatching should override execCommand, otherwise
  282. * it is preferred to only override execCommandInternal.
  283. *
  284. * This version of execCommand will only work for single field plugins.
  285. * Multi-field plugins must override execCommand.
  286. *
  287. * @param {string} command The command to execute.
  288. * @param {...*} var_args Any additional parameters needed to
  289. * execute the command.
  290. * @return {*} The result of the execCommand, if any.
  291. */
  292. goog.editor.Plugin.prototype.execCommand = function(command, var_args) {
  293. // TODO(user): Replace all uses of isSilentCommand with plugins that just
  294. // override this base execCommand method.
  295. var silent = this.isSilentCommand(command);
  296. if (!silent) {
  297. // Stop listening to mutation events in Firefox while text formatting
  298. // is happening. This prevents us from trying to size the field in the
  299. // middle of an execCommand, catching the field in a strange intermediary
  300. // state where both replacement nodes and original nodes are appended to
  301. // the dom. Note that change events get turned back on by
  302. // fieldObj.dispatchChange.
  303. if (goog.userAgent.GECKO) {
  304. this.getFieldObject().stopChangeEvents(true, true);
  305. }
  306. this.getFieldObject().dispatchBeforeChange();
  307. }
  308. try {
  309. var result = this.execCommandInternal.apply(this, arguments);
  310. } finally {
  311. // If the above execCommandInternal call throws an exception, we still need
  312. // to turn change events back on (see http://b/issue?id=1471355).
  313. // NOTE: If if you add to or change the methods called in this finally
  314. // block, please add them as expected calls to the unit test function
  315. // testExecCommandException().
  316. if (!silent) {
  317. // dispatchChange includes a call to startChangeEvents, which unwinds the
  318. // call to stopChangeEvents made before the try block.
  319. this.getFieldObject().dispatchChange();
  320. this.getFieldObject().dispatchSelectionChangeEvent();
  321. }
  322. }
  323. return result;
  324. };
  325. /**
  326. * Handles execCommand. This default implementation does nothing, and is
  327. * called by execCommand, which handles event dispatching. This method should
  328. * be overriden by plugins that don't need to do their own event dispatching.
  329. * If custom event dispatching is needed, execCommand shoul be overriden
  330. * instead.
  331. *
  332. * @param {string} command The command to execute.
  333. * @param {...*} var_args Any additional parameters needed to
  334. * execute the command.
  335. * @return {*} The result of the execCommand, if any.
  336. * @protected
  337. */
  338. goog.editor.Plugin.prototype.execCommandInternal;
  339. /**
  340. * Gets the state of this command if this plugin serves that command.
  341. * @param {string} command The command to check.
  342. * @return {*} The value of the command.
  343. */
  344. goog.editor.Plugin.prototype.queryCommandValue;
  345. /**
  346. * Prepares the given HTML for editing. Strips out content that should not
  347. * appear in an editor, and normalizes content as appropriate. The inverse
  348. * of cleanContentsHtml.
  349. *
  350. * This op is invoked even on disabled plugins.
  351. *
  352. * @param {string} originalHtml The original HTML.
  353. * @param {Object} styles A map of strings. If the plugin wants to add
  354. * any styles to the field element, it should add them as key-value
  355. * pairs to this object.
  356. * @return {string} New HTML that's ok for editing.
  357. */
  358. goog.editor.Plugin.prototype.prepareContentsHtml;
  359. /**
  360. * Cleans the contents of the node passed to it. The node contents are modified
  361. * directly, and the modifications will subsequently be used, for operations
  362. * such as saving the innerHTML of the editor etc. Since the plugins act on
  363. * the DOM directly, this method can be very expensive.
  364. *
  365. * This op is invoked even on disabled plugins.
  366. *
  367. * @param {!Element} fieldCopy The copy of the editable field which
  368. * needs to be cleaned up.
  369. */
  370. goog.editor.Plugin.prototype.cleanContentsDom;
  371. /**
  372. * Cleans the html contents of Trogedit. Both cleanContentsDom and
  373. * and cleanContentsHtml will be called on contents extracted from Trogedit.
  374. * The inverse of prepareContentsHtml.
  375. *
  376. * This op is invoked even on disabled plugins.
  377. *
  378. * @param {string} originalHtml The trogedit HTML.
  379. * @return {string} Cleaned-up HTML.
  380. */
  381. goog.editor.Plugin.prototype.cleanContentsHtml;
  382. /**
  383. * Whether the string corresponds to a command this plugin handles.
  384. * @param {string} command Command string to check.
  385. * @return {boolean} Whether the plugin handles this type of command.
  386. */
  387. goog.editor.Plugin.prototype.isSupportedCommand = function(command) {
  388. return false;
  389. };
  390. /**
  391. * Saves the field's scroll position. See b/7279077 for context.
  392. * Currently only does anything in Edge, since all other browsers
  393. * already seem to work correctly.
  394. * @return {function()} A function to restore the current scroll position.
  395. * @protected
  396. */
  397. goog.editor.Plugin.prototype.saveScrollPosition = function() {
  398. if (this.getFieldObject() && goog.userAgent.EDGE) {
  399. var win = this.getFieldObject().getEditableDomHelper().getWindow();
  400. return win.scrollTo.bind(win, win.scrollX, win.scrollY);
  401. }
  402. return function() {};
  403. };