API Docs for: 3.18.1
Show:

File: editor/js/editor-br.js

  1.  
  2.  
  3. /**
  4. * Plugin for Editor to normalize BR's.
  5. * @class Plugin.EditorBR
  6. * @extends Base
  7. * @constructor
  8. * @module editor
  9. * @submodule editor-br
  10. */
  11.  
  12.  
  13. var EditorBR = function() {
  14. EditorBR.superclass.constructor.apply(this, arguments);
  15. }, HOST = 'host', LI = 'li';
  16.  
  17.  
  18. Y.extend(EditorBR, Y.Base, {
  19. /**
  20. * Frame keyDown handler that normalizes BR's when pressing ENTER.
  21. * @private
  22. * @method _onKeyDown
  23. */
  24. _onKeyDown: function(e) {
  25. if (e.stopped) {
  26. e.halt();
  27. return;
  28. }
  29. if (e.keyCode === 13) {
  30. var host = this.get(HOST), inst = host.getInstance(),
  31. sel = new inst.EditorSelection();
  32.  
  33. if (sel) {
  34. if (Y.UA.ie) {
  35. if (!sel.anchorNode || (!sel.anchorNode.test(LI) && !sel.anchorNode.ancestor(LI))) {
  36. host.execCommand('inserthtml', inst.EditorSelection.CURSOR);
  37. e.halt();
  38. }
  39. }
  40. if (Y.UA.webkit) {
  41. if (!sel.anchorNode || (!sel.anchorNode.test(LI) && !sel.anchorNode.ancestor(LI))) {
  42. host.frame._execCommand('insertlinebreak', null);
  43. e.halt();
  44. }
  45. }
  46. }
  47. }
  48. },
  49. /**
  50. * Adds listeners for keydown in IE and Webkit. Also fires insertbeonreturn for supporting browsers.
  51. * @private
  52. * @method _afterEditorReady
  53. */
  54. _afterEditorReady: function() {
  55. var inst = this.get(HOST).getInstance(),
  56. container;
  57.  
  58. try {
  59. inst.config.doc.execCommand('insertbronreturn', null, true);
  60. } catch (bre) {}
  61.  
  62. if (Y.UA.ie || Y.UA.webkit) {
  63. container = inst.EditorSelection.ROOT;
  64.  
  65. if (container.test('body')) {
  66. container = inst.config.doc;
  67. }
  68.  
  69. inst.on('keydown', Y.bind(this._onKeyDown, this), container);
  70. }
  71. },
  72. /**
  73. * Adds a nodeChange listener only for FF, in the event of a backspace or delete, it creates an empy textNode
  74. * inserts it into the DOM after the e.changedNode, then removes it. Causing FF to redraw the content.
  75. * @private
  76. * @method _onNodeChange
  77. * @param {Event} e The nodeChange event.
  78. */
  79. _onNodeChange: function(e) {
  80. switch (e.changedType) {
  81. case 'backspace-up':
  82. case 'backspace-down':
  83. case 'delete-up':
  84. /*
  85. * This forced FF to redraw the content on backspace.
  86. * On some occasions FF will leave a cursor residue after content has been deleted.
  87. * Dropping in the empty textnode and then removing it causes FF to redraw and
  88. * remove the "ghost cursors"
  89. */
  90. var inst = this.get(HOST).getInstance(),
  91. d = e.changedNode,
  92. t = inst.config.doc.createTextNode(' ');
  93. d.appendChild(t);
  94. d.removeChild(t);
  95. break;
  96. }
  97. },
  98. initializer: function() {
  99. var host = this.get(HOST);
  100. if (host.editorPara) {
  101. Y.error('Can not plug EditorBR and EditorPara at the same time.');
  102. return;
  103. }
  104. host.after('ready', Y.bind(this._afterEditorReady, this));
  105. if (Y.UA.gecko) {
  106. host.on('nodeChange', Y.bind(this._onNodeChange, this));
  107. }
  108. }
  109. }, {
  110. /**
  111. * editorBR
  112. * @static
  113. * @property NAME
  114. */
  115. NAME: 'editorBR',
  116. /**
  117. * editorBR
  118. * @static
  119. * @property NS
  120. */
  121. NS: 'editorBR',
  122. ATTRS: {
  123. host: {
  124. value: false
  125. }
  126. }
  127. });
  128.  
  129. Y.namespace('Plugin');
  130.  
  131. Y.Plugin.EditorBR = EditorBR;
  132.  
  133.