plugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /**
  6. * @fileOverview The "div" plugin. It wraps the selected block level elements with a 'div' element with specified styles and attributes.
  7. *
  8. */
  9. ( function() {
  10. CKEDITOR.plugins.add( 'div', {
  11. requires: 'dialog',
  12. // jscs:disable maximumLineLength
  13. 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%
  14. // jscs:enable maximumLineLength
  15. icons: 'creatediv', // %REMOVE_LINE_CORE%
  16. hidpi: true, // %REMOVE_LINE_CORE%
  17. init: function( editor ) {
  18. if ( editor.blockless )
  19. return;
  20. var lang = editor.lang.div,
  21. allowed = 'div(*)';
  22. if ( CKEDITOR.dialog.isTabEnabled( editor, 'editdiv', 'advanced' ) )
  23. allowed += ';div[dir,id,lang,title]{*}';
  24. editor.addCommand( 'creatediv', new CKEDITOR.dialogCommand( 'creatediv', {
  25. allowedContent: allowed,
  26. requiredContent: 'div',
  27. contextSensitive: true,
  28. refresh: function( editor, path ) {
  29. var context = editor.config.div_wrapTable ? path.root : path.blockLimit;
  30. this.setState( 'div' in context.getDtd() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
  31. }
  32. } ) );
  33. editor.addCommand( 'editdiv', new CKEDITOR.dialogCommand( 'editdiv', { requiredContent: 'div' } ) );
  34. editor.addCommand( 'removediv', {
  35. requiredContent: 'div',
  36. exec: function( editor ) {
  37. var selection = editor.getSelection(),
  38. ranges = selection && selection.getRanges(),
  39. range,
  40. bookmarks = selection.createBookmarks(),
  41. walker,
  42. toRemove = [];
  43. function findDiv( node ) {
  44. var div = CKEDITOR.plugins.div.getSurroundDiv( editor, node );
  45. if ( div && !div.data( 'cke-div-added' ) ) {
  46. toRemove.push( div );
  47. div.data( 'cke-div-added' );
  48. }
  49. }
  50. for ( var i = 0; i < ranges.length; i++ ) {
  51. range = ranges[ i ];
  52. if ( range.collapsed )
  53. findDiv( selection.getStartElement() );
  54. else {
  55. walker = new CKEDITOR.dom.walker( range );
  56. walker.evaluator = findDiv;
  57. walker.lastForward();
  58. }
  59. }
  60. for ( i = 0; i < toRemove.length; i++ )
  61. toRemove[ i ].remove( true );
  62. selection.selectBookmarks( bookmarks );
  63. }
  64. } );
  65. editor.ui.addButton && editor.ui.addButton( 'CreateDiv', {
  66. label: lang.toolbar,
  67. command: 'creatediv',
  68. toolbar: 'blocks,50'
  69. } );
  70. if ( editor.addMenuItems ) {
  71. editor.addMenuItems( {
  72. editdiv: {
  73. label: lang.edit,
  74. command: 'editdiv',
  75. group: 'div',
  76. order: 1
  77. },
  78. removediv: {
  79. label: lang.remove,
  80. command: 'removediv',
  81. group: 'div',
  82. order: 5
  83. }
  84. } );
  85. if ( editor.contextMenu ) {
  86. editor.contextMenu.addListener( function( element ) {
  87. if ( !element || element.isReadOnly() )
  88. return null;
  89. if ( CKEDITOR.plugins.div.getSurroundDiv( editor ) ) {
  90. return {
  91. editdiv: CKEDITOR.TRISTATE_OFF,
  92. removediv: CKEDITOR.TRISTATE_OFF
  93. };
  94. }
  95. return null;
  96. } );
  97. }
  98. }
  99. CKEDITOR.dialog.add( 'creatediv', this.path + 'dialogs/div.js' );
  100. CKEDITOR.dialog.add( 'editdiv', this.path + 'dialogs/div.js' );
  101. }
  102. } );
  103. CKEDITOR.plugins.div = {
  104. getSurroundDiv: function( editor, start ) {
  105. var path = editor.elementPath( start );
  106. return editor.elementPath( path.blockLimit ).contains( function( node ) {
  107. // Avoid read-only (i.e. contenteditable="false") divs (#11083).
  108. return node.is( 'div' ) && !node.isReadOnly();
  109. }, 1 );
  110. }
  111. };
  112. } )();