inline-widget.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  10. import List from '@ckeditor/ckeditor5-list/src/list';
  11. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  12. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  13. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  14. import { toWidget, viewToModelPositionOutsideModelElement } 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 { addListToDropdown, createDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import Model from '@ckeditor/ckeditor5-ui/src/model';
  20. class PlaceholderCommand extends Command {
  21. execute( { value } ) {
  22. const editor = this.editor;
  23. editor.model.change( writer => {
  24. // Create a <placeholder> elment with the "name" attribute...
  25. const placeholder = writer.createElement( 'placeholder', { name: value } );
  26. // ... and insert it into the document.
  27. editor.model.insertContent( placeholder );
  28. // Put the selection on the inserted element.
  29. writer.setSelection( placeholder, 'on' );
  30. } );
  31. }
  32. refresh() {
  33. const model = this.editor.model;
  34. const selection = model.document.selection;
  35. const isAllowed = model.schema.checkChild( selection.focus.parent, 'placeholder' );
  36. this.isEnabled = isAllowed;
  37. }
  38. }
  39. class Placeholder extends Plugin {
  40. static get requires() {
  41. return [ PlaceholderEditing, PlaceholderUI ];
  42. }
  43. }
  44. class PlaceholderUI extends Plugin {
  45. init() {
  46. const editor = this.editor;
  47. const t = editor.t;
  48. const placeholderNames = editor.config.get( 'placeholderConfig.types' );
  49. // The "placeholder" dropdown must be registered among the UI components of the editor
  50. // to be displayed in the toolbar.
  51. editor.ui.componentFactory.add( 'placeholder', locale => {
  52. const dropdownView = createDropdown( locale );
  53. // Populate the list in the dropdown with items.
  54. addListToDropdown( dropdownView, getDropdownItemsDefinitions( placeholderNames ) );
  55. dropdownView.buttonView.set( {
  56. // The t() function helps localize the editor. All strings enclosed in t() can be
  57. // translated and change when the language of the editor changes.
  58. label: t( 'Placeholder' ),
  59. tooltip: true,
  60. withText: true
  61. } );
  62. // Execute the command when the dropdown item is clicked (executed).
  63. this.listenTo( dropdownView, 'execute', evt => {
  64. editor.execute( 'placeholder', { value: evt.source.commandParam } );
  65. editor.editing.view.focus();
  66. } );
  67. return dropdownView;
  68. } );
  69. }
  70. }
  71. function getDropdownItemsDefinitions( placeholderNames ) {
  72. const itemDefinitions = new Collection();
  73. for ( const name of placeholderNames ) {
  74. const definition = {
  75. type: 'button',
  76. model: new Model( {
  77. commandParam: name,
  78. label: name,
  79. withText: true
  80. } )
  81. };
  82. // Add the item definition to the collection.
  83. itemDefinitions.add( definition );
  84. }
  85. return itemDefinitions;
  86. }
  87. class PlaceholderEditing extends Plugin {
  88. static get requires() {
  89. return [ Widget ];
  90. }
  91. init() {
  92. console.log( 'PlaceholderEditing#init() got called' );
  93. this._defineSchema();
  94. this._defineConverters();
  95. this.editor.commands.add( 'placeholder', new PlaceholderCommand( this.editor ) );
  96. this.editor.editing.mapper.on(
  97. 'viewToModelPosition',
  98. viewToModelPositionOutsideModelElement( this.editor.model, viewElement => viewElement.hasClass( 'placeholder' ) )
  99. );
  100. this.editor.config.define( 'placeholderConfig', {
  101. types: [ 'date', 'first name', 'surname' ]
  102. } );
  103. }
  104. _defineSchema() {
  105. const schema = this.editor.model.schema;
  106. schema.register( 'placeholder', {
  107. // Allow wherever text is allowed:
  108. allowWhere: '$text',
  109. // The placeholder will act as an inline node:
  110. isInline: true,
  111. // The inline widget is self-contained so it cannot be split by the caret and it can be selected:
  112. isObject: true,
  113. // The placeholder can have many types, like date, name, surname, etc:
  114. allowAttributes: [ 'name' ]
  115. } );
  116. }
  117. _defineConverters() {
  118. const conversion = this.editor.conversion;
  119. conversion.for( 'upcast' ).elementToElement( {
  120. view: {
  121. name: 'span',
  122. classes: [ 'placeholder' ]
  123. },
  124. model: ( viewElement, modelWriter ) => {
  125. // Extract the "name" from "{name}".
  126. const name = viewElement.getChild( 0 ).data.slice( 1, -1 );
  127. return modelWriter.createElement( 'placeholder', { name } );
  128. }
  129. } );
  130. conversion.for( 'editingDowncast' ).elementToElement( {
  131. model: 'placeholder',
  132. view: ( modelItem, viewWriter ) => {
  133. const widgetElement = createPlaceholderView( modelItem, viewWriter );
  134. // Enable widget handling on a placeholder element inside the editing view.
  135. return toWidget( widgetElement, viewWriter );
  136. }
  137. } );
  138. conversion.for( 'dataDowncast' ).elementToElement( {
  139. model: 'placeholder',
  140. view: createPlaceholderView
  141. } );
  142. // Helper method for both downcast converters.
  143. function createPlaceholderView( modelItem, viewWriter ) {
  144. const name = modelItem.getAttribute( 'name' );
  145. const placeholderView = viewWriter.createContainerElement( 'span', {
  146. class: 'placeholder'
  147. } );
  148. // Insert the placeholder name (as a text).
  149. const innerText = viewWriter.createText( '{' + name + '}' );
  150. viewWriter.insert( viewWriter.createPositionAt( placeholderView, 0 ), innerText );
  151. return placeholderView;
  152. }
  153. }
  154. }
  155. ClassicEditor
  156. .create( document.querySelector( '#snippet-inline-widget' ), {
  157. plugins: [ Essentials, Paragraph, Heading, List, Bold, Italic, Placeholder ],
  158. toolbar: [ 'heading', '|', 'bold', 'italic', 'numberedList', 'bulletedList', '|', 'placeholder' ],
  159. placeholderConfig: {
  160. types: [ 'date', 'color', 'first name', 'surname' ]
  161. }
  162. } )
  163. .then( editor => {
  164. console.log( 'Editor was initialized', editor );
  165. // Expose for playing in the console.
  166. window.editor = editor;
  167. } )
  168. .catch( error => {
  169. console.error( error.stack );
  170. } );