plugin1.js 4.0 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( 'figure', {
  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: 'createfigure', // %REMOVE_LINE_CORE%
  16. // hidpi: true, // %REMOVE_LINE_CORE%
  17. init: function( editor ) {
  18. if ( editor.blockless )
  19. return;
  20. var lang = editor.lang.figure,
  21. allowed = 'figure(*)';
  22. if ( CKEDITOR.dialog.isTabEnabled( editor, 'editfigure', 'advanced' ) )
  23. allowed += ';figure[dir,id,lang,title]{*}';
  24. editor.addCommand( 'createfigure', new CKEDITOR.dialogCommand( 'createfigure', {
  25. allowedContent: allowed,
  26. requiredContent: 'figure',
  27. contextSensitive: true,
  28. refresh: function( editor, path ) {
  29. var context = editor.config.figure_wrapTable ? path.root : path.blockLimit;
  30. this.setState( 'figure' in context.getDtd() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
  31. }
  32. } ) );
  33. editor.addCommand( 'editfigure', new CKEDITOR.dialogCommand( 'editfigure', { requiredContent: 'figure' } ) );
  34. editor.addCommand( 'removefigure', {
  35. requiredContent: 'figure',
  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 figure = CKEDITOR.plugins.figure.getSurroundDiv( editor, node );
  45. if ( figure && !figure.data( 'cke-figure-added' ) ) {
  46. toRemove.push( figure );
  47. figure.data( 'cke-figure-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( 'CreateFigure', {
  66. label: lang.toolbar,
  67. command: 'createfigure',
  68. toolbar: 'figure,50'
  69. } );
  70. if ( editor.addMenuItems ) {
  71. editor.addMenuItems( {
  72. editfigure: {
  73. label: lang.edit,
  74. command: 'editfigure',
  75. group: 'figure',
  76. order: 1
  77. },
  78. removefigure: {
  79. label: lang.remove,
  80. command: 'removefigure',
  81. group: 'figure',
  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.figure.getSurroundDiv( editor ) ) {
  90. return {
  91. editfigure: CKEDITOR.TRISTATE_OFF,
  92. removefigure: CKEDITOR.TRISTATE_OFF
  93. };
  94. }
  95. return null;
  96. } );
  97. }
  98. }
  99. CKEDITOR.dialog.add( 'createfigure', this.path + 'dialogs/figure.js' );
  100. CKEDITOR.dialog.add( 'editfigure', this.path + 'dialogs/figure.js' );
  101. }
  102. } );
  103. CKEDITOR.plugins.figure = {
  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") figures (#11083).
  108. return node.is( 'figure' ) && !node.isReadOnly();
  109. }, 1 );
  110. }
  111. };
  112. } )();