block-widget.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  9. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  10. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  11. import List from '@ckeditor/ckeditor5-list/src/list';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  14. import { toWidget, toWidgetEditable } from '@ckeditor/ckeditor5-widget/src/utils';
  15. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  16. import Command from '@ckeditor/ckeditor5-core/src/command';
  17. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  18. class SimpleBox extends Plugin {
  19. static get requires() {
  20. return [ SimpleBoxEditing, SimpleBoxUI ];
  21. }
  22. }
  23. class SimpleBoxUI extends Plugin {
  24. init() {
  25. console.log( 'SimpleBoxUI#init() got called' );
  26. const editor = this.editor;
  27. const t = editor.t;
  28. // The "simpleBox" button must be registered among the UI components of the editor
  29. // to be displayed in the toolbar.
  30. editor.ui.componentFactory.add( 'simpleBox', locale => {
  31. // The state of the button will be bound to the widget command.
  32. const command = editor.commands.get( 'insertSimpleBox' );
  33. // The button will be an instance of ButtonView.
  34. const buttonView = new ButtonView( locale );
  35. buttonView.set( {
  36. // The t() function helps localize the editor. All strings enclosed in t() can be
  37. // translated and change when the language of the editor changes.
  38. label: t( 'Simple Box' ),
  39. withText: true,
  40. tooltip: true
  41. } );
  42. // Bind the state of the button to the command.
  43. buttonView.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
  44. // Execute the command when the button is clicked (executed).
  45. this.listenTo( buttonView, 'execute', () => editor.execute( 'insertSimpleBox' ) );
  46. return buttonView;
  47. } );
  48. }
  49. }
  50. class SimpleBoxEditing extends Plugin {
  51. static get requires() {
  52. return [ Widget ];
  53. }
  54. init() {
  55. console.log( 'SimpleBoxEditing#init() got called' );
  56. this._defineSchema();
  57. this._defineConverters();
  58. this.editor.commands.add( 'insertSimpleBox', new InsertSimpleBoxCommand( this.editor ) );
  59. }
  60. _defineSchema() {
  61. const schema = this.editor.model.schema;
  62. schema.register( 'simpleBox', {
  63. // Behaves like a self-contained object (e.g. an image).
  64. isObject: true,
  65. // Allow in places where other blocks are allowed (e.g. directly in the root).
  66. allowWhere: '$block'
  67. } );
  68. schema.register( 'simpleBoxTitle', {
  69. // Cannot be split or left by the caret.
  70. isLimit: true,
  71. allowIn: 'simpleBox',
  72. // Allow content which is allowed in blocks (i.e. text with attributes).
  73. allowContentOf: '$block'
  74. } );
  75. schema.register( 'simpleBoxDescription', {
  76. // Cannot be split or left by the caret.
  77. isLimit: true,
  78. allowIn: 'simpleBox',
  79. // Allow content which is allowed in the root (e.g. paragraphs).
  80. allowContentOf: '$root'
  81. } );
  82. schema.addChildCheck( ( context, childDefinition ) => {
  83. if ( context.endsWith( 'simpleBoxDescription' ) && childDefinition.name == 'simpleBox' ) {
  84. return false;
  85. }
  86. } );
  87. }
  88. _defineConverters() {
  89. const conversion = this.editor.conversion;
  90. // <simpleBox> converters
  91. conversion.for( 'upcast' ).elementToElement( {
  92. model: 'simpleBox',
  93. view: {
  94. name: 'section',
  95. classes: 'simple-box'
  96. }
  97. } );
  98. conversion.for( 'dataDowncast' ).elementToElement( {
  99. model: 'simpleBox',
  100. view: {
  101. name: 'section',
  102. classes: 'simple-box'
  103. }
  104. } );
  105. conversion.for( 'editingDowncast' ).elementToElement( {
  106. model: 'simpleBox',
  107. view: ( modelElement, viewWriter ) => {
  108. const section = viewWriter.createContainerElement( 'section', { class: 'simple-box' } );
  109. return toWidget( section, viewWriter, { label: 'simple box widget' } );
  110. }
  111. } );
  112. // <simpleBoxTitle> converters
  113. conversion.for( 'upcast' ).elementToElement( {
  114. model: 'simpleBoxTitle',
  115. view: {
  116. name: 'h1',
  117. classes: 'simple-box-title'
  118. }
  119. } );
  120. conversion.for( 'dataDowncast' ).elementToElement( {
  121. model: 'simpleBoxTitle',
  122. view: {
  123. name: 'h1',
  124. classes: 'simple-box-title'
  125. }
  126. } );
  127. conversion.for( 'editingDowncast' ).elementToElement( {
  128. model: 'simpleBoxTitle',
  129. view: ( modelElement, viewWriter ) => {
  130. // Note: You use a more specialized createEditableElement() method here.
  131. const h1 = viewWriter.createEditableElement( 'h1', { class: 'simple-box-title' } );
  132. return toWidgetEditable( h1, viewWriter );
  133. }
  134. } );
  135. // <simpleBoxDescription> converters
  136. conversion.for( 'upcast' ).elementToElement( {
  137. model: 'simpleBoxDescription',
  138. view: {
  139. name: 'div',
  140. classes: 'simple-box-description'
  141. }
  142. } );
  143. conversion.for( 'dataDowncast' ).elementToElement( {
  144. model: 'simpleBoxDescription',
  145. view: {
  146. name: 'div',
  147. classes: 'simple-box-description'
  148. }
  149. } );
  150. conversion.for( 'editingDowncast' ).elementToElement( {
  151. model: 'simpleBoxDescription',
  152. view: ( modelElement, viewWriter ) => {
  153. // Note: You use a more specialized createEditableElement() method here.
  154. const div = viewWriter.createEditableElement( 'div', { class: 'simple-box-description' } );
  155. return toWidgetEditable( div, viewWriter );
  156. }
  157. } );
  158. }
  159. }
  160. class InsertSimpleBoxCommand extends Command {
  161. execute() {
  162. this.editor.model.change( writer => {
  163. // Insert <simpleBox>*</simpleBox> at the current selection position
  164. // in a way that will result in creating a valid model structure.
  165. this.editor.model.insertContent( createSimpleBox( writer ) );
  166. } );
  167. }
  168. refresh() {
  169. const model = this.editor.model;
  170. const selection = model.document.selection;
  171. const allowedIn = model.schema.findAllowedParent( selection.getFirstPosition(), 'simpleBox' );
  172. this.isEnabled = allowedIn !== null;
  173. }
  174. }
  175. function createSimpleBox( writer ) {
  176. const simpleBox = writer.createElement( 'simpleBox' );
  177. const simpleBoxTitle = writer.createElement( 'simpleBoxTitle' );
  178. const simpleBoxDescription = writer.createElement( 'simpleBoxDescription' );
  179. writer.append( simpleBoxTitle, simpleBox );
  180. writer.append( simpleBoxDescription, simpleBox );
  181. // There must be at least one paragraph for the description to be editable.
  182. // See https://github.com/ckeditor/ckeditor5/issues/1464.
  183. writer.appendElement( 'paragraph', simpleBoxDescription );
  184. return simpleBox;
  185. }
  186. ClassicEditor
  187. .create( document.querySelector( '#snippet-block-widget' ), {
  188. plugins: [ Essentials, Bold, Italic, Heading, List, Paragraph, SimpleBox ],
  189. toolbar: {
  190. items: [ 'heading', '|', 'bold', 'italic', 'numberedList', 'bulletedList', 'simpleBox' ],
  191. viewportTopOffset: window.getViewportTopOffsetConfig()
  192. }
  193. } )
  194. .then( editor => {
  195. console.log( 'Editor was initialized', editor );
  196. window.editor = editor;
  197. } )
  198. .catch( err => {
  199. console.error( err.stack );
  200. } );