8
0

nestededitable.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /* global console */
  6. import { getData } from '../../src/dev-utils/model';
  7. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  8. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  14. import './nestededitable.css';
  15. class NestedEditable extends Plugin {
  16. init() {
  17. const editor = this.editor;
  18. const editing = editor.editing;
  19. const schema = editor.model.schema;
  20. schema.register( 'figure', {
  21. isObject: true
  22. } );
  23. schema.register( 'figcaption' );
  24. schema.extend( 'figure', { allowIn: '$root' } );
  25. schema.extend( 'figcaption', { allowIn: 'figure' } );
  26. schema.extend( '$text', {
  27. allowIn: [ 'figure', 'figcaption' ]
  28. } );
  29. editor.conversion.for( 'downcast' ).elementToElement( {
  30. model: 'figure',
  31. view: {
  32. name: 'figure',
  33. attribute: {
  34. contenteditable: 'false'
  35. }
  36. }
  37. } );
  38. editor.conversion.for( 'upcast' ).elementToElement( {
  39. model: 'figure',
  40. view: 'figure'
  41. } );
  42. editor.conversion.for( 'downcast' ).elementToElement( {
  43. model: 'figcaption',
  44. view: ( modelItem, { writer } ) => {
  45. const element = writer.createEditableElement( 'figcaption', { contenteditable: 'true' } );
  46. element.on( 'change:isFocused', ( evt, property, is ) => {
  47. if ( is ) {
  48. editing.view.change( writer => writer.addClass( 'focused', element ) );
  49. } else {
  50. editing.view.change( writer => writer.removeClass( 'focused', element ) );
  51. }
  52. } );
  53. return element;
  54. }
  55. } );
  56. editor.conversion.for( 'upcast' ).elementToElement( {
  57. model: 'figcaption',
  58. view: 'figcaption'
  59. } );
  60. }
  61. }
  62. ClassicEditor
  63. .create( global.document.querySelector( '#editor' ), {
  64. plugins: [ Enter, Typing, Paragraph, NestedEditable, Undo ],
  65. toolbar: [ 'undo', 'redo' ]
  66. } )
  67. .then( editor => {
  68. editor.model.document.on( 'change', () => {
  69. printModelContents( editor );
  70. } );
  71. printModelContents( editor );
  72. } )
  73. .catch( err => {
  74. console.error( err.stack );
  75. } );
  76. const modelDiv = global.document.querySelector( '#model' );
  77. function printModelContents( editor ) {
  78. modelDiv.innerText = getData( editor.model );
  79. }