plugin.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. ( function() {
  6. function noBlockLeft( bqBlock ) {
  7. for ( var i = 0, length = bqBlock.getChildCount(), child; i < length && ( child = bqBlock.getChild( i ) ); i++ ) {
  8. if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() )
  9. return false;
  10. }
  11. return true;
  12. }
  13. var commandObject = {
  14. exec: function( editor ) {
  15. var state = editor.getCommand( 'blockquote' ).state,
  16. selection = editor.getSelection(),
  17. range = selection && selection.getRanges()[ 0 ];
  18. if ( !range )
  19. return;
  20. var bookmarks = selection.createBookmarks();
  21. // Kludge for #1592: if the bookmark nodes are in the beginning of
  22. // blockquote, then move them to the nearest block element in the
  23. // blockquote.
  24. if ( CKEDITOR.env.ie ) {
  25. var bookmarkStart = bookmarks[ 0 ].startNode,
  26. bookmarkEnd = bookmarks[ 0 ].endNode,
  27. cursor;
  28. if ( bookmarkStart && bookmarkStart.getParent().getName() == 'blockquote' ) {
  29. cursor = bookmarkStart;
  30. while ( ( cursor = cursor.getNext() ) ) {
  31. if ( cursor.type == CKEDITOR.NODE_ELEMENT && cursor.isBlockBoundary() ) {
  32. bookmarkStart.move( cursor, true );
  33. break;
  34. }
  35. }
  36. }
  37. if ( bookmarkEnd && bookmarkEnd.getParent().getName() == 'blockquote' ) {
  38. cursor = bookmarkEnd;
  39. while ( ( cursor = cursor.getPrevious() ) ) {
  40. if ( cursor.type == CKEDITOR.NODE_ELEMENT && cursor.isBlockBoundary() ) {
  41. bookmarkEnd.move( cursor );
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. var iterator = range.createIterator(),
  48. block;
  49. iterator.enlargeBr = editor.config.enterMode != CKEDITOR.ENTER_BR;
  50. if ( state == CKEDITOR.TRISTATE_OFF ) {
  51. var paragraphs = [];
  52. while ( ( block = iterator.getNextParagraph() ) )
  53. paragraphs.push( block );
  54. // If no paragraphs, create one from the current selection position.
  55. if ( paragraphs.length < 1 ) {
  56. var para = editor.document.createElement( editor.config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ),
  57. firstBookmark = bookmarks.shift();
  58. range.insertNode( para );
  59. para.append( new CKEDITOR.dom.text( '\ufeff', editor.document ) );
  60. range.moveToBookmark( firstBookmark );
  61. range.selectNodeContents( para );
  62. range.collapse( true );
  63. firstBookmark = range.createBookmark();
  64. paragraphs.push( para );
  65. bookmarks.unshift( firstBookmark );
  66. }
  67. // Make sure all paragraphs have the same parent.
  68. var commonParent = paragraphs[ 0 ].getParent(),
  69. tmp = [];
  70. for ( var i = 0; i < paragraphs.length; i++ ) {
  71. block = paragraphs[ i ];
  72. commonParent = commonParent.getCommonAncestor( block.getParent() );
  73. }
  74. // The common parent must not be the following tags: table, tbody, tr, ol, ul.
  75. var denyTags = { table: 1, tbody: 1, tr: 1, ol: 1, ul: 1 };
  76. while ( denyTags[ commonParent.getName() ] )
  77. commonParent = commonParent.getParent();
  78. // Reconstruct the block list to be processed such that all resulting blocks
  79. // satisfy parentNode.equals( commonParent ).
  80. var lastBlock = null;
  81. while ( paragraphs.length > 0 ) {
  82. block = paragraphs.shift();
  83. while ( !block.getParent().equals( commonParent ) )
  84. block = block.getParent();
  85. if ( !block.equals( lastBlock ) )
  86. tmp.push( block );
  87. lastBlock = block;
  88. }
  89. // If any of the selected blocks is a blockquote, remove it to prevent
  90. // nested blockquotes.
  91. while ( tmp.length > 0 ) {
  92. block = tmp.shift();
  93. if ( block.getName() == 'blockquote' ) {
  94. var docFrag = new CKEDITOR.dom.documentFragment( editor.document );
  95. while ( block.getFirst() ) {
  96. docFrag.append( block.getFirst().remove() );
  97. paragraphs.push( docFrag.getLast() );
  98. }
  99. docFrag.replace( block );
  100. } else {
  101. paragraphs.push( block );
  102. }
  103. }
  104. // Now we have all the blocks to be included in a new blockquote node.
  105. var bqBlock = editor.document.createElement( 'blockquote' );
  106. bqBlock.insertBefore( paragraphs[ 0 ] );
  107. while ( paragraphs.length > 0 ) {
  108. block = paragraphs.shift();
  109. bqBlock.append( block );
  110. }
  111. } else if ( state == CKEDITOR.TRISTATE_ON ) {
  112. var moveOutNodes = [],
  113. database = {};
  114. while ( ( block = iterator.getNextParagraph() ) ) {
  115. var bqParent = null,
  116. bqChild = null;
  117. while ( block.getParent() ) {
  118. if ( block.getParent().getName() == 'blockquote' ) {
  119. bqParent = block.getParent();
  120. bqChild = block;
  121. break;
  122. }
  123. block = block.getParent();
  124. }
  125. // Remember the blocks that were recorded down in the moveOutNodes array
  126. // to prevent duplicates.
  127. if ( bqParent && bqChild && !bqChild.getCustomData( 'blockquote_moveout' ) ) {
  128. moveOutNodes.push( bqChild );
  129. CKEDITOR.dom.element.setMarker( database, bqChild, 'blockquote_moveout', true );
  130. }
  131. }
  132. CKEDITOR.dom.element.clearAllMarkers( database );
  133. var movedNodes = [],
  134. processedBlockquoteBlocks = [];
  135. database = {};
  136. while ( moveOutNodes.length > 0 ) {
  137. var node = moveOutNodes.shift();
  138. bqBlock = node.getParent();
  139. // If the node is located at the beginning or the end, just take it out
  140. // without splitting. Otherwise, split the blockquote node and move the
  141. // paragraph in between the two blockquote nodes.
  142. if ( !node.getPrevious() )
  143. node.remove().insertBefore( bqBlock );
  144. else if ( !node.getNext() )
  145. node.remove().insertAfter( bqBlock );
  146. else {
  147. node.breakParent( node.getParent() );
  148. processedBlockquoteBlocks.push( node.getNext() );
  149. }
  150. // Remember the blockquote node so we can clear it later (if it becomes empty).
  151. if ( !bqBlock.getCustomData( 'blockquote_processed' ) ) {
  152. processedBlockquoteBlocks.push( bqBlock );
  153. CKEDITOR.dom.element.setMarker( database, bqBlock, 'blockquote_processed', true );
  154. }
  155. movedNodes.push( node );
  156. }
  157. CKEDITOR.dom.element.clearAllMarkers( database );
  158. // Clear blockquote nodes that have become empty.
  159. for ( i = processedBlockquoteBlocks.length - 1; i >= 0; i-- ) {
  160. bqBlock = processedBlockquoteBlocks[ i ];
  161. if ( noBlockLeft( bqBlock ) )
  162. bqBlock.remove();
  163. }
  164. if ( editor.config.enterMode == CKEDITOR.ENTER_BR ) {
  165. var firstTime = true;
  166. while ( movedNodes.length ) {
  167. node = movedNodes.shift();
  168. if ( node.getName() == 'div' ) {
  169. docFrag = new CKEDITOR.dom.documentFragment( editor.document );
  170. var needBeginBr = firstTime && node.getPrevious() && !( node.getPrevious().type == CKEDITOR.NODE_ELEMENT && node.getPrevious().isBlockBoundary() );
  171. if ( needBeginBr )
  172. docFrag.append( editor.document.createElement( 'br' ) );
  173. var needEndBr = node.getNext() && !( node.getNext().type == CKEDITOR.NODE_ELEMENT && node.getNext().isBlockBoundary() );
  174. while ( node.getFirst() )
  175. node.getFirst().remove().appendTo( docFrag );
  176. if ( needEndBr )
  177. docFrag.append( editor.document.createElement( 'br' ) );
  178. docFrag.replace( node );
  179. firstTime = false;
  180. }
  181. }
  182. }
  183. }
  184. selection.selectBookmarks( bookmarks );
  185. editor.focus();
  186. },
  187. refresh: function( editor, path ) {
  188. // Check if inside of blockquote.
  189. var firstBlock = path.block || path.blockLimit;
  190. this.setState( editor.elementPath( firstBlock ).contains( 'blockquote', 1 ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
  191. },
  192. context: 'blockquote',
  193. allowedContent: 'blockquote',
  194. requiredContent: 'blockquote'
  195. };
  196. CKEDITOR.plugins.add( 'blockquote', {
  197. // jscs:disable maximumLineLength
  198. lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
  199. // jscs:enable maximumLineLength
  200. icons: 'blockquote', // %REMOVE_LINE_CORE%
  201. hidpi: true, // %REMOVE_LINE_CORE%
  202. init: function( editor ) {
  203. if ( editor.blockless )
  204. return;
  205. editor.addCommand( 'blockquote', commandObject );
  206. editor.ui.addButton && editor.ui.addButton( 'Blockquote', {
  207. label: editor.lang.blockquote.toolbar,
  208. command: 'blockquote',
  209. toolbar: 'blocks,10'
  210. } );
  211. }
  212. } );
  213. } )();