8
0

restrictedediting.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import Table from '@ckeditor/ckeditor5-table/src/table';
  9. import StandardEditingMode from '../../src/standardeditingmode';
  10. import RestrictedEditingMode from '../../src/restrictededitingmode';
  11. import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
  12. const restrictedModeButton = document.getElementById( 'mode-restricted' );
  13. const standardModeButton = document.getElementById( 'mode-standard' );
  14. restrictedModeButton.addEventListener( 'change', handleModeChange );
  15. standardModeButton.addEventListener( 'change', handleModeChange );
  16. startMode( document.querySelector( 'input[name="mode"]:checked' ).value );
  17. async function handleModeChange( evt ) {
  18. await startMode( evt.target.value );
  19. }
  20. async function startMode( selectedMode ) {
  21. if ( selectedMode === 'standard' ) {
  22. await startStandardEditingMode();
  23. } else {
  24. await startRestrictedEditingMode();
  25. }
  26. }
  27. async function startStandardEditingMode() {
  28. await reloadEditor( {
  29. plugins: [ ArticlePluginSet, Table, StandardEditingMode ],
  30. toolbar: [
  31. 'heading', '|', 'bold', 'italic', 'link', '|',
  32. 'bulletedList', 'numberedList', 'blockQuote', 'insertTable', '|',
  33. 'restrictedEditingException', '|', 'undo', 'redo'
  34. ],
  35. image: {
  36. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  37. },
  38. table: {
  39. contentToolbar: [
  40. 'tableColumn',
  41. 'tableRow',
  42. 'mergeTableCells'
  43. ]
  44. }
  45. } );
  46. }
  47. // Functional form of a plugin - might be a class that extends `Plugin` as well.
  48. function MyPlugin( editor ) {
  49. // This action must be done in the `afterInit()` method of your plugin.
  50. this.afterInit = () => {
  51. const restrictedEditingModeEditing = editor.plugins.get( 'RestrictedEditingModeEditing' );
  52. const commandsToEnable = [
  53. 'insertTableRowAbove', 'insertTableRowBelow',
  54. 'insertTableColumnRight', 'insertTableColumnLeft',
  55. 'mergeTableCells'
  56. ];
  57. // Enable (always) some commands in restricted editing mode.
  58. commandsToEnable.forEach( commandName => restrictedEditingModeEditing.enableCommand( commandName ) );
  59. };
  60. }
  61. async function startRestrictedEditingMode() {
  62. await reloadEditor( {
  63. plugins: [ ArticlePluginSet, Table, TableToolbar, RestrictedEditingMode, MyPlugin ],
  64. toolbar: [ 'bold', 'italic', 'link', '|', 'restrictedEditing', '|', 'undo', 'redo' ],
  65. table: {
  66. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ],
  67. tableToolbar: [ 'bold', 'italic' ]
  68. }
  69. } );
  70. }
  71. async function reloadEditor( config ) {
  72. if ( window.editor ) {
  73. await window.editor.destroy();
  74. }
  75. window.editor = await ClassicEditor.create( document.querySelector( '#editor' ), config );
  76. }