resize.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, console, window */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import EnterPlugin from '@ckeditor/ckeditor5-enter/src/enter';
  8. import TypingPlugin from '@ckeditor/ckeditor5-typing/src/typing';
  9. import ParagraphPlugin from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import ImagePlugin from '../../src/image';
  11. import ImageStyle from '../../src/imagestyle';
  12. import ImageToolbar from '../../src/imagetoolbar';
  13. import ImageCaption from '../../src/imagecaption';
  14. import UndoPlugin from '@ckeditor/ckeditor5-undo/src/undo';
  15. import ClipboardPlugin from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  16. import List from '@ckeditor/ckeditor5-list/src/list';
  17. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  18. import Table from '@ckeditor/ckeditor5-table/src/table';
  19. import Indent from '@ckeditor/ckeditor5-indent/src/indent';
  20. import IndentBlock from '@ckeditor/ckeditor5-indent/src/indentblock';
  21. import ResizerCentral from '@ckeditor/ckeditor5-widget/src/resizercentral';
  22. import ResizerSide from '@ckeditor/ckeditor5-widget/src/resizerside';
  23. const commonConfig = {
  24. plugins: [ EnterPlugin, TypingPlugin, ParagraphPlugin, ImagePlugin, ImageStyle, ImageToolbar, ImageCaption,
  25. UndoPlugin, ClipboardPlugin, List, BlockQuote, Table, Indent, IndentBlock ],
  26. toolbar: [ 'undo', 'redo', 'bulletedList', 'numberedList', 'blockQuote', 'insertTable', 'indent' ],
  27. image: {
  28. toolbar: [ 'imageStyle:alignLeft', 'imageStyle:full', 'imageStyle:side' ],
  29. styles: [
  30. 'full',
  31. 'alignLeft',
  32. 'side' // Purposely using side image instead right aligned image to make sure it works well with both style types.
  33. ]
  34. }
  35. };
  36. ClassicEditor
  37. .create( document.querySelector( '#editor' ), commonConfig )
  38. .then( editor => {
  39. window.editor = editor;
  40. } )
  41. .catch( err => {
  42. console.error( err.stack );
  43. } );
  44. ClassicEditor
  45. .create( document.querySelector( '#fancyEditor' ), commonConfig )
  46. .then( editor => {
  47. window.fancyEditor = editor;
  48. } )
  49. .catch( err => {
  50. console.error( err.stack );
  51. } );
  52. ( function() {
  53. const optionsWrapper = document.getElementById( 'resizer-options' );
  54. const defaultConfig = {
  55. handler: [ 'top-left', 'top-right', 'bottom-right', 'bottom-left' ]
  56. };
  57. const dynamicStylesheet = document.createElement( 'style' );
  58. const cssRules = {};
  59. for ( const checkbox of optionsWrapper.querySelectorAll( 'input[name="handler[]"]' ) ) {
  60. checkbox.addEventListener( 'change', function() {
  61. const ruleValue = `.ck-widget__resizer.ck-widget__resizer-${ this.value } { display: none; }`;
  62. if ( this.value in cssRules ) {
  63. dynamicStylesheet.sheet.deleteRule( cssRules[ this.value ] );
  64. delete cssRules[ this.value ];
  65. }
  66. if ( !this.checked ) {
  67. cssRules[ this.value ] = dynamicStylesheet.sheet.insertRule( ruleValue );
  68. }
  69. } );
  70. if ( defaultConfig.handler.includes( checkbox.value ) ) {
  71. checkbox.checked = true;
  72. }
  73. }
  74. document.head.appendChild( dynamicStylesheet );
  75. // Resize using image
  76. document.getElementById( 'resize-image' ).addEventListener( 'change', function() {
  77. window.pocResizeUsingImage = this.checked;
  78. } );
  79. // Resize strategy
  80. document.getElementById( 'strategy' ).addEventListener( 'change', function() {
  81. const strategies = {
  82. 'center': ResizerCentral,
  83. 'side': ResizerSide
  84. };
  85. window.editor.plugins.get( 'WidgetResizer' ).set( 'resizerStrategy', strategies[ this.value ] );
  86. } );
  87. }() );