nestededitable.js 2.7 KB

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