1.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals console, window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  10. import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  11. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  12. import AttributeContainer from '../../../../src/view/attributeelement';
  13. import ViewContainer from '../../../../src/view/containerelement';
  14. import { downcastElementToElement } from '../../../../src/conversion/downcast-converters';
  15. import { setData } from '../../../../src/dev-utils/model';
  16. import ViewEditable from '../../../../src/view/editableelement';
  17. ClassicEditor
  18. .create( document.querySelector( '#editor' ), {
  19. plugins: [ Essentials, Paragraph, Bold, Widget ],
  20. toolbar: [ 'undo', 'redo' ]
  21. } )
  22. .then( editor => {
  23. window.editor = editor;
  24. const model = editor.model;
  25. model.schema.register( 'widget', {
  26. inheritAllFrom: '$block',
  27. isObject: true
  28. } );
  29. model.schema.extend( '$text', {
  30. allowIn: 'nested'
  31. } );
  32. model.schema.register( 'nested', {
  33. allowIn: 'widget',
  34. isLimit: true
  35. } );
  36. editor.conversion.for( 'downcast' )
  37. .add( downcastElementToElement( {
  38. model: 'widget',
  39. view: () => {
  40. const b = new AttributeContainer( 'b' );
  41. const div = new ViewContainer( 'div', null, b );
  42. return toWidget( div, { label: 'element label' } );
  43. }
  44. } ) )
  45. .add( downcastElementToElement( {
  46. model: 'nested',
  47. view: () => new ViewEditable( 'figcaption', { contenteditable: true } )
  48. } ) );
  49. setData( editor.model,
  50. '<paragraph>foo[]</paragraph>' +
  51. '<widget><nested>bar</nested></widget>' +
  52. '<widget><nested>bom</nested></widget>'
  53. );
  54. } )
  55. .catch( err => {
  56. console.error( err.stack );
  57. } );