8
0

nestededitable.js 2.7 KB

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