8
0

widgettoolbarrepository.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  12. import Widget from '../src/widget';
  13. import WidgetToolbarRepository from '../src/widgettoolbarrepository';
  14. import { isWidget, toWidget } from '../src/utils';
  15. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  16. import View from '@ckeditor/ckeditor5-ui/src/view';
  17. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  19. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  20. describe( 'WidgetToolbarRepository', () => {
  21. let editor, model, balloon, widgetToolbarRepository, editorElement;
  22. testUtils.createSinonSandbox();
  23. beforeEach( () => {
  24. editorElement = document.createElement( 'div' );
  25. document.body.appendChild( editorElement );
  26. return ClassicTestEditor
  27. .create( editorElement, {
  28. plugins: [ Paragraph, FakeButton, WidgetToolbarRepository, FakeWidget, FakeChildWidget, BlockQuote ],
  29. fake: {
  30. toolbar: [ 'fake_button' ]
  31. }
  32. } )
  33. .then( newEditor => {
  34. editor = newEditor;
  35. model = newEditor.model;
  36. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  37. balloon = editor.plugins.get( 'ContextualBalloon' );
  38. } );
  39. } );
  40. afterEach( () => {
  41. editorElement.remove();
  42. return editor.destroy();
  43. } );
  44. it( 'should be loaded', () => {
  45. expect( editor.plugins.get( WidgetToolbarRepository ) ).to.be.instanceOf( WidgetToolbarRepository );
  46. } );
  47. it( 'should work if balloon toolbar is not available', () => {
  48. editorElement.remove();
  49. editor.destroy();
  50. editorElement = document.createElement( 'div' );
  51. document.body.appendChild( editorElement );
  52. expect( editor.plugins.has( 'BalloonToolbar' ) ).to.be.false;
  53. expect( editor.plugins.has( WidgetToolbarRepository ) ).to.be.true;
  54. } );
  55. describe( 'register()', () => {
  56. it( 'should create a widget toolbar and add it to the collection', () => {
  57. widgetToolbarRepository.register( 'fake', {
  58. items: editor.config.get( 'fake.toolbar' ),
  59. getRelatedElement: () => null,
  60. } );
  61. expect( widgetToolbarRepository._toolbarDefinitions.size ).to.equal( 1 );
  62. expect( widgetToolbarRepository._toolbarDefinitions.get( 'fake' ) ).to.be.an( 'object' );
  63. } );
  64. it( 'should throw when adding two times widget with the same id', () => {
  65. widgetToolbarRepository.register( 'fake', {
  66. items: editor.config.get( 'fake.toolbar' ),
  67. getRelatedElement: () => null
  68. } );
  69. expectToThrowCKEditorError( () => {
  70. widgetToolbarRepository.register( 'fake', {
  71. items: editor.config.get( 'fake.toolbar' ),
  72. getRelatedElement: () => null
  73. } );
  74. }, /^widget-toolbar-duplicated/, editor );
  75. } );
  76. it( 'should use a pre–defined aria-label for the toolbar', () => {
  77. widgetToolbarRepository.register( 'fake', {
  78. items: editor.config.get( 'fake.toolbar' ),
  79. getRelatedElement: () => null
  80. } );
  81. const toolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  82. toolbarView.render();
  83. expect( toolbarView.element.getAttribute( 'aria-label' ) ).to.equal( 'Widget toolbar' );
  84. toolbarView.destroy();
  85. } );
  86. it( 'should use a custom aria-label when provided', () => {
  87. widgetToolbarRepository.register( 'fake', {
  88. items: editor.config.get( 'fake.toolbar' ),
  89. getRelatedElement: () => null,
  90. ariaLabel: 'Custom label'
  91. } );
  92. const toolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  93. toolbarView.render();
  94. expect( toolbarView.element.getAttribute( 'aria-label' ) ).to.equal( 'Custom label' );
  95. toolbarView.destroy();
  96. } );
  97. } );
  98. describe( 'integration tests', () => {
  99. beforeEach( () => {
  100. editor.ui.focusTracker.isFocused = true;
  101. } );
  102. it( 'toolbar should be visible when the `getRelatedElement` callback returns a selected widget element', () => {
  103. widgetToolbarRepository.register( 'fake', {
  104. items: editor.config.get( 'fake.toolbar' ),
  105. getRelatedElement: getSelectedFakeWidget
  106. } );
  107. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  108. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  109. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  110. } );
  111. it( 'toolbar should be hidden when the `getRelatedElement` callback returns null', () => {
  112. widgetToolbarRepository.register( 'fake', {
  113. items: editor.config.get( 'fake.toolbar' ),
  114. getRelatedElement: getSelectedFakeWidget
  115. } );
  116. setData( model, '[<paragraph>foo</paragraph>]<fake-widget></fake-widget>' );
  117. expect( balloon.visibleView ).to.equal( null );
  118. } );
  119. it( 'toolbar should be hidden when the `getRelatedElement` callback returns null #2', () => {
  120. widgetToolbarRepository.register( 'fake', {
  121. items: editor.config.get( 'fake.toolbar' ),
  122. getRelatedElement: getSelectedFakeWidget
  123. } );
  124. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  125. model.change( writer => {
  126. // Select the <paragraph>foo</paragraph>.
  127. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  128. } );
  129. expect( balloon.visibleView ).to.equal( null );
  130. } );
  131. it( 'toolbar should be removed from not visible balloon stack when the `getRelatedElement` callback returns null', () => {
  132. balloon.add( {
  133. view: new View(),
  134. stackId: 'secondary',
  135. position: {
  136. target: {}
  137. }
  138. } );
  139. widgetToolbarRepository.register( 'fake', {
  140. items: editor.config.get( 'fake.toolbar' ),
  141. getRelatedElement: getSelectedFakeWidget
  142. } );
  143. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  144. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  145. expect( balloon.hasView( fakeWidgetToolbarView ) );
  146. expect( balloon.visibleView ).to.not.equal( fakeWidgetToolbarView );
  147. model.change( writer => {
  148. // Select the <paragraph>foo</paragraph>.
  149. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  150. } );
  151. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( false );
  152. } );
  153. it( 'toolbar should be hidden when the editor ui lost focus', () => {
  154. widgetToolbarRepository.register( 'fake', {
  155. items: editor.config.get( 'fake.toolbar' ),
  156. getRelatedElement: getSelectedFakeWidget
  157. } );
  158. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  159. editor.ui.focusTracker.isFocused = false;
  160. expect( balloon.visibleView ).to.equal( null );
  161. } );
  162. it( 'toolbar should do nothing with toolbar when the editor ui lost focus but toolbar is not a visible view', () => {
  163. balloon.add( {
  164. view: new View(),
  165. stackId: 'secondary',
  166. position: {
  167. target: {}
  168. }
  169. } );
  170. widgetToolbarRepository.register( 'fake', {
  171. items: editor.config.get( 'fake.toolbar' ),
  172. getRelatedElement: getSelectedFakeWidget
  173. } );
  174. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  175. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  176. editor.ui.focusTracker.isFocused = false;
  177. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( true );
  178. } );
  179. it( 'toolbar should update its position when other widget is selected', () => {
  180. widgetToolbarRepository.register( 'fake', {
  181. items: editor.config.get( 'fake.toolbar' ),
  182. getRelatedElement: getSelectedFakeWidget
  183. } );
  184. setData( model, '[<fake-widget></fake-widget>]<fake-widget></fake-widget>' );
  185. model.change( writer => {
  186. // Select the second widget.
  187. writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
  188. } );
  189. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  190. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  191. } );
  192. it( 'it should be possible to create a widget toolbar for content inside the widget', () => {
  193. widgetToolbarRepository.register( 'fake', {
  194. items: editor.config.get( 'fake.toolbar' ),
  195. getRelatedElement: getSelectedFakeWidgetContent
  196. } );
  197. setData( model, '<fake-widget>[foo]</fake-widget>' );
  198. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  199. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  200. } );
  201. it( 'toolbar should not engage when is in the balloon yet invisible', () => {
  202. widgetToolbarRepository.register( 'fake', {
  203. items: editor.config.get( 'fake.toolbar' ),
  204. getRelatedElement: getSelectedFakeWidget
  205. } );
  206. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  207. setData( model, '[<fake-widget></fake-widget>]' );
  208. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  209. const lastView = new View();
  210. lastView.element = document.createElement( 'div' );
  211. balloon.add( {
  212. view: lastView,
  213. position: {
  214. target: document.body
  215. }
  216. } );
  217. expect( balloon.visibleView ).to.equal( lastView );
  218. editor.ui.fire( 'update' );
  219. expect( balloon.visibleView ).to.equal( lastView );
  220. } );
  221. // #60
  222. it( 'should show up only for the related element which is deepest in the view document', () => {
  223. // The point of this widget is to provide a getRelatedElement function that
  224. // returns a super–shallow related element which is ignored but satisfies code coverage.
  225. widgetToolbarRepository.register( 'dummy', {
  226. items: editor.config.get( 'fake.toolbar' ),
  227. getRelatedElement: () => editor.editing.view.document.getRoot()
  228. } );
  229. widgetToolbarRepository.register( 'fake', {
  230. items: editor.config.get( 'fake.toolbar' ),
  231. getRelatedElement: getSelectedFakeWidget
  232. } );
  233. widgetToolbarRepository.register( 'fake-child', {
  234. items: editor.config.get( 'fake.toolbar' ),
  235. getRelatedElement: getSelectedFakeChildWidget
  236. } );
  237. setData( model,
  238. '<paragraph>foo</paragraph>' +
  239. '<fake-widget>' +
  240. '<paragraph>foo</paragraph>' +
  241. '[<fake-child-widget></fake-child-widget>]' +
  242. '</fake-widget>' );
  243. const fakeChildWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake-child' ).view;
  244. expect( balloon.visibleView ).to.equal( fakeChildWidgetToolbarView );
  245. } );
  246. // #60
  247. it( 'should attach to the new related view element upon selecting another widget', () => {
  248. const view = editor.editing.view;
  249. widgetToolbarRepository.register( 'fake', {
  250. items: editor.config.get( 'fake.toolbar' ),
  251. getRelatedElement: getSelectedFakeWidget
  252. } );
  253. widgetToolbarRepository.register( 'fake-child', {
  254. items: editor.config.get( 'fake.toolbar' ),
  255. getRelatedElement: getSelectedFakeChildWidget
  256. } );
  257. setData( model,
  258. '<paragraph>foo</paragraph>' +
  259. '[<fake-widget>' +
  260. '<paragraph>foo</paragraph>' +
  261. '<fake-child-widget></fake-child-widget>' +
  262. '</fake-widget>]' );
  263. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  264. const fakeChildWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake-child' ).view;
  265. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  266. const fakeChildViewElement = view.document.getRoot().getChild( 1 ).getChild( 1 );
  267. const updatePositionSpy = sinon.spy( balloon, 'add' );
  268. view.change( writer => {
  269. // [<fake-child-widget></fake-child-widget>]
  270. writer.setSelection( fakeChildViewElement, 'on' );
  271. } );
  272. expect( balloon.visibleView ).to.equal( fakeChildWidgetToolbarView );
  273. expect( updatePositionSpy.firstCall.args[ 0 ].position.target ).to.equal(
  274. view.domConverter.viewToDom( fakeChildViewElement ) );
  275. } );
  276. it( 'should not update balloon position when toolbar is in not visible stack', () => {
  277. const customView = new View();
  278. sinon.spy( balloon.view, 'pin' );
  279. widgetToolbarRepository.register( 'fake', {
  280. items: editor.config.get( 'fake.toolbar' ),
  281. getRelatedElement: getSelectedFakeWidget
  282. } );
  283. setData( model,
  284. '<paragraph>foo</paragraph>' +
  285. '[<fake-widget></fake-widget>]'
  286. );
  287. balloon.add( {
  288. stackId: 'custom',
  289. view: customView,
  290. position: { target: {} }
  291. } );
  292. balloon.showStack( 'custom' );
  293. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  294. expect( balloon.visibleView ).to.equal( customView );
  295. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( true );
  296. const spy = testUtils.sinon.spy( balloon, 'updatePosition' );
  297. editor.ui.fire( 'update' );
  298. sinon.assert.notCalled( spy );
  299. } );
  300. it( 'should update balloon position when stack with toolbar is switched in rotator to visible', () => {
  301. const view = editor.editing.view;
  302. const customView = new View();
  303. sinon.spy( balloon.view, 'pin' );
  304. widgetToolbarRepository.register( 'fake', {
  305. items: editor.config.get( 'fake.toolbar' ),
  306. getRelatedElement: getSelectedFakeWidget
  307. } );
  308. setData( model,
  309. '<paragraph>foo</paragraph>' +
  310. '[<fake-widget></fake-widget>]'
  311. );
  312. const fakeViewElement = view.document.getRoot().getChild( 1 );
  313. const fakeDomElement = editor.editing.view.domConverter.mapViewToDom( fakeViewElement );
  314. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  315. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.equal( fakeDomElement );
  316. balloon.add( {
  317. stackId: 'custom',
  318. view: customView,
  319. position: { target: {} }
  320. } );
  321. balloon.showStack( 'custom' );
  322. expect( balloon.visibleView ).to.equal( customView );
  323. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( true );
  324. editor.execute( 'blockQuote' );
  325. balloon.showStack( 'main' );
  326. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  327. expect( balloon.hasView( customView ) ).to.equal( true );
  328. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.not.equal( fakeDomElement );
  329. const newFakeViewElement = view.document.getRoot().getChild( 1 ).getChild( 0 );
  330. const newFakeDomElement = editor.editing.view.domConverter.mapViewToDom( newFakeViewElement );
  331. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.equal( newFakeDomElement );
  332. } );
  333. } );
  334. } );
  335. describe( 'WidgetToolbarRepository - integration with the BalloonToolbar', () => {
  336. let clock, editor, model, balloon, balloonToolbar, widgetToolbarRepository, editorElement;
  337. testUtils.createSinonSandbox();
  338. beforeEach( () => {
  339. editorElement = document.createElement( 'div' );
  340. document.body.appendChild( editorElement );
  341. clock = testUtils.sinon.useFakeTimers();
  342. return BalloonEditor
  343. .create( editorElement, {
  344. plugins: [ Paragraph, FakeButton, WidgetToolbarRepository, FakeWidget, Bold ],
  345. balloonToolbar: [ 'bold' ],
  346. fake: {
  347. toolbar: [ 'fake_button' ]
  348. }
  349. } )
  350. .then( newEditor => {
  351. editor = newEditor;
  352. model = newEditor.model;
  353. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  354. balloon = editor.plugins.get( 'ContextualBalloon' );
  355. balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
  356. editor.ui.focusTracker.isFocused = true;
  357. } );
  358. } );
  359. afterEach( () => {
  360. editorElement.remove();
  361. return editor.destroy();
  362. } );
  363. it( 'balloon toolbar should be hidden when the widget is selected', () => {
  364. widgetToolbarRepository.register( 'fake', {
  365. items: editor.config.get( 'fake.toolbar' ),
  366. getRelatedElement: getSelectedFakeWidget,
  367. } );
  368. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  369. editor.editing.view.document.isFocused = true;
  370. setData( model, '[<fake-widget></fake-widget>]<paragraph>foo</paragraph>' );
  371. clock.tick( 200 );
  372. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  373. } );
  374. it( 'balloon toolbar should be visible when the widget is not selected', () => {
  375. widgetToolbarRepository.register( 'fake', {
  376. items: editor.config.get( 'fake.toolbar' ),
  377. getRelatedElement: getSelectedFakeWidget
  378. } );
  379. editor.editing.view.document.isFocused = true;
  380. setData( model, '<fake-widget></fake-widget><paragraph>[foo]</paragraph>' );
  381. clock.tick( 200 );
  382. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  383. } );
  384. } );
  385. function getSelectedFakeWidget( selection ) {
  386. const viewElement = selection.getSelectedElement();
  387. if ( viewElement && isWidget( viewElement ) && !!viewElement.getCustomProperty( 'fakeWidget' ) ) {
  388. return viewElement;
  389. }
  390. return null;
  391. }
  392. function getSelectedFakeChildWidget( selection ) {
  393. const viewElement = selection.getSelectedElement();
  394. if ( viewElement && isWidget( viewElement ) && !!viewElement.getCustomProperty( 'fakeChildWidget' ) ) {
  395. return viewElement;
  396. }
  397. return null;
  398. }
  399. function getSelectedFakeWidgetContent( selection ) {
  400. const pos = selection.getFirstPosition();
  401. let node = pos.parent;
  402. while ( node ) {
  403. if ( node.is( 'element' ) && isWidget( node ) && node.getCustomProperty( 'fakeWidget' ) ) {
  404. return node;
  405. }
  406. node = node.parent;
  407. }
  408. return null;
  409. }
  410. // Plugin that adds fake_button to editor's component factory.
  411. class FakeButton extends Plugin {
  412. init() {
  413. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  414. const view = new ButtonView( locale );
  415. view.set( {
  416. label: 'fake button'
  417. } );
  418. return view;
  419. } );
  420. }
  421. }
  422. // Simple widget plugin
  423. // It registers `<fake-widget>` block in model and represents `div` in the view.
  424. // It allows having text inside self.
  425. class FakeWidget extends Plugin {
  426. static get requires() {
  427. return [ Widget ];
  428. }
  429. init() {
  430. const editor = this.editor;
  431. const schema = editor.model.schema;
  432. schema.register( 'fake-widget', {
  433. isObject: true,
  434. isBlock: true,
  435. allowWhere: '$block'
  436. } );
  437. schema.extend( '$text', { allowIn: 'fake-widget' } );
  438. schema.extend( 'paragraph', { allowIn: 'fake-widget' } );
  439. const conversion = editor.conversion;
  440. conversion.for( 'dataDowncast' ).elementToElement( {
  441. model: 'fake-widget',
  442. view: ( modelElement, viewWriter ) => {
  443. return viewWriter.createContainerElement( 'div' );
  444. }
  445. } );
  446. conversion.for( 'editingDowncast' ).elementToElement( {
  447. model: 'fake-widget',
  448. view: ( modelElement, viewWriter ) => {
  449. const fakeWidget = viewWriter.createContainerElement( 'div' );
  450. viewWriter.setCustomProperty( 'fakeWidget', true, fakeWidget );
  451. return toWidget( fakeWidget, viewWriter, { label: 'fake-widget' } );
  452. }
  453. } );
  454. conversion.for( 'upcast' ).elementToElement( {
  455. view: {
  456. name: 'div'
  457. },
  458. model: ( view, modelWriter ) => {
  459. return modelWriter.createElement( 'fake-widget' );
  460. }
  461. } );
  462. }
  463. }
  464. // A simple child widget plugin
  465. // It registers `<fake-child-widget>` block in model and represents `div` in the view.
  466. // It allows having text inside self.
  467. class FakeChildWidget extends Plugin {
  468. static get requires() {
  469. return [ Widget ];
  470. }
  471. init() {
  472. const editor = this.editor;
  473. const schema = editor.model.schema;
  474. schema.register( 'fake-child-widget', {
  475. isObject: true,
  476. isBlock: true,
  477. allowWhere: '$block',
  478. allowIn: 'fake-widget'
  479. } );
  480. schema.extend( '$text', { allowIn: 'fake-child-widget' } );
  481. schema.extend( 'paragraph', { allowIn: 'fake-child-widget' } );
  482. const conversion = editor.conversion;
  483. conversion.for( 'dataDowncast' ).elementToElement( {
  484. model: 'fake-child-widget',
  485. view: ( modelElement, viewWriter ) => {
  486. return viewWriter.createContainerElement( 'div' );
  487. }
  488. } );
  489. conversion.for( 'editingDowncast' ).elementToElement( {
  490. model: 'fake-child-widget',
  491. view: ( modelElement, viewWriter ) => {
  492. const fakeWidget = viewWriter.createContainerElement( 'div' );
  493. viewWriter.setCustomProperty( 'fakeChildWidget', true, fakeWidget );
  494. return toWidget( fakeWidget, viewWriter, { label: 'fake-child-widget' } );
  495. }
  496. } );
  497. conversion.for( 'upcast' ).elementToElement( {
  498. view: {
  499. name: 'div'
  500. },
  501. model: ( view, modelWriter ) => {
  502. return modelWriter.createElement( 'fake-child-widget' );
  503. }
  504. } );
  505. }
  506. }