8
0

nestededitable.js 2.6 KB

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