widgettoolbarrepository.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. } );
  77. describe( 'integration tests', () => {
  78. beforeEach( () => {
  79. editor.ui.focusTracker.isFocused = true;
  80. } );
  81. it( 'toolbar should be visible when the `getRelatedElement` callback returns a selected widget element', () => {
  82. widgetToolbarRepository.register( 'fake', {
  83. items: editor.config.get( 'fake.toolbar' ),
  84. getRelatedElement: getSelectedFakeWidget
  85. } );
  86. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  87. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  88. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  89. } );
  90. it( 'toolbar should be hidden when the `getRelatedElement` callback returns null', () => {
  91. widgetToolbarRepository.register( 'fake', {
  92. items: editor.config.get( 'fake.toolbar' ),
  93. getRelatedElement: getSelectedFakeWidget
  94. } );
  95. setData( model, '[<paragraph>foo</paragraph>]<fake-widget></fake-widget>' );
  96. expect( balloon.visibleView ).to.equal( null );
  97. } );
  98. it( 'toolbar should be hidden when the `getRelatedElement` callback returns null #2', () => {
  99. widgetToolbarRepository.register( 'fake', {
  100. items: editor.config.get( 'fake.toolbar' ),
  101. getRelatedElement: getSelectedFakeWidget
  102. } );
  103. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  104. model.change( writer => {
  105. // Select the <paragraph>foo</paragraph>.
  106. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  107. } );
  108. expect( balloon.visibleView ).to.equal( null );
  109. } );
  110. it( 'toolbar should be removed from not visible balloon stack when the `getRelatedElement` callback returns null', () => {
  111. balloon.add( {
  112. view: new View(),
  113. stackId: 'secondary',
  114. position: {
  115. target: {}
  116. }
  117. } );
  118. widgetToolbarRepository.register( 'fake', {
  119. items: editor.config.get( 'fake.toolbar' ),
  120. getRelatedElement: getSelectedFakeWidget
  121. } );
  122. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  123. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  124. expect( balloon.hasView( fakeWidgetToolbarView ) );
  125. expect( balloon.visibleView ).to.not.equal( fakeWidgetToolbarView );
  126. model.change( writer => {
  127. // Select the <paragraph>foo</paragraph>.
  128. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  129. } );
  130. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( false );
  131. } );
  132. it( 'toolbar should be hidden when the editor ui lost focus', () => {
  133. widgetToolbarRepository.register( 'fake', {
  134. items: editor.config.get( 'fake.toolbar' ),
  135. getRelatedElement: getSelectedFakeWidget
  136. } );
  137. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  138. editor.ui.focusTracker.isFocused = false;
  139. expect( balloon.visibleView ).to.equal( null );
  140. } );
  141. it( 'toolbar should do nothing with toolbar when the editor ui lost focus but toolbar is not a visible view', () => {
  142. balloon.add( {
  143. view: new View(),
  144. stackId: 'secondary',
  145. position: {
  146. target: {}
  147. }
  148. } );
  149. widgetToolbarRepository.register( 'fake', {
  150. items: editor.config.get( 'fake.toolbar' ),
  151. getRelatedElement: getSelectedFakeWidget
  152. } );
  153. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  154. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  155. editor.ui.focusTracker.isFocused = false;
  156. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( true );
  157. } );
  158. it( 'toolbar should update its position when other widget is selected', () => {
  159. widgetToolbarRepository.register( 'fake', {
  160. items: editor.config.get( 'fake.toolbar' ),
  161. getRelatedElement: getSelectedFakeWidget
  162. } );
  163. setData( model, '[<fake-widget></fake-widget>]<fake-widget></fake-widget>' );
  164. model.change( writer => {
  165. // Select the second widget.
  166. writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
  167. } );
  168. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  169. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  170. } );
  171. it( 'it should be possible to create a widget toolbar for content inside the widget', () => {
  172. widgetToolbarRepository.register( 'fake', {
  173. items: editor.config.get( 'fake.toolbar' ),
  174. getRelatedElement: getSelectedFakeWidgetContent
  175. } );
  176. setData( model, '<fake-widget>[foo]</fake-widget>' );
  177. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  178. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  179. } );
  180. it( 'toolbar should not engage when is in the balloon yet invisible', () => {
  181. widgetToolbarRepository.register( 'fake', {
  182. items: editor.config.get( 'fake.toolbar' ),
  183. getRelatedElement: getSelectedFakeWidget
  184. } );
  185. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  186. setData( model, '[<fake-widget></fake-widget>]' );
  187. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  188. const lastView = new View();
  189. lastView.element = document.createElement( 'div' );
  190. balloon.add( {
  191. view: lastView,
  192. position: {
  193. target: document.body
  194. }
  195. } );
  196. expect( balloon.visibleView ).to.equal( lastView );
  197. editor.ui.fire( 'update' );
  198. expect( balloon.visibleView ).to.equal( lastView );
  199. } );
  200. // #60
  201. it( 'should show up only for the related element which is deepest in the view document', () => {
  202. // The point of this widget is to provide a getRelatedElement function that
  203. // returns a super–shallow related element which is ignored but satisfies code coverage.
  204. widgetToolbarRepository.register( 'dummy', {
  205. items: editor.config.get( 'fake.toolbar' ),
  206. getRelatedElement: () => editor.editing.view.document.getRoot()
  207. } );
  208. widgetToolbarRepository.register( 'fake', {
  209. items: editor.config.get( 'fake.toolbar' ),
  210. getRelatedElement: getSelectedFakeWidget
  211. } );
  212. widgetToolbarRepository.register( 'fake-child', {
  213. items: editor.config.get( 'fake.toolbar' ),
  214. getRelatedElement: getSelectedFakeChildWidget
  215. } );
  216. setData( model,
  217. '<paragraph>foo</paragraph>' +
  218. '<fake-widget>' +
  219. '<paragraph>foo</paragraph>' +
  220. '[<fake-child-widget></fake-child-widget>]' +
  221. '</fake-widget>' );
  222. const fakeChildWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake-child' ).view;
  223. expect( balloon.visibleView ).to.equal( fakeChildWidgetToolbarView );
  224. } );
  225. // #60
  226. it( 'should attach to the new related view element upon selecting another widget', () => {
  227. const view = editor.editing.view;
  228. widgetToolbarRepository.register( 'fake', {
  229. items: editor.config.get( 'fake.toolbar' ),
  230. getRelatedElement: getSelectedFakeWidget
  231. } );
  232. widgetToolbarRepository.register( 'fake-child', {
  233. items: editor.config.get( 'fake.toolbar' ),
  234. getRelatedElement: getSelectedFakeChildWidget
  235. } );
  236. setData( model,
  237. '<paragraph>foo</paragraph>' +
  238. '[<fake-widget>' +
  239. '<paragraph>foo</paragraph>' +
  240. '<fake-child-widget></fake-child-widget>' +
  241. '</fake-widget>]' );
  242. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  243. const fakeChildWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake-child' ).view;
  244. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  245. const fakeChildViewElement = view.document.getRoot().getChild( 1 ).getChild( 1 );
  246. const updatePositionSpy = sinon.spy( balloon, 'add' );
  247. view.change( writer => {
  248. // [<fake-child-widget></fake-child-widget>]
  249. writer.setSelection( fakeChildViewElement, 'on' );
  250. } );
  251. expect( balloon.visibleView ).to.equal( fakeChildWidgetToolbarView );
  252. expect( updatePositionSpy.firstCall.args[ 0 ].position.target ).to.equal(
  253. view.domConverter.viewToDom( fakeChildViewElement ) );
  254. } );
  255. it( 'should update position when is switched in rotator to a visible panel', () => {
  256. const view = editor.editing.view;
  257. const customView = new View();
  258. sinon.spy( balloon.view, 'pin' );
  259. widgetToolbarRepository.register( 'fake', {
  260. items: editor.config.get( 'fake.toolbar' ),
  261. getRelatedElement: getSelectedFakeWidget
  262. } );
  263. setData( model,
  264. '<paragraph>foo</paragraph>' +
  265. '[<fake-widget></fake-widget>]'
  266. );
  267. const fakeViewElement = view.document.getRoot().getChild( 1 );
  268. const fakeDomElement = editor.editing.view.domConverter.mapViewToDom( fakeViewElement );
  269. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  270. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.equal( fakeDomElement );
  271. balloon.add( {
  272. stackId: 'custom',
  273. view: customView,
  274. position: { target: {} }
  275. } );
  276. balloon.showStack( 'custom' );
  277. expect( balloon.visibleView ).to.equal( customView );
  278. expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( true );
  279. editor.execute( 'blockQuote' );
  280. balloon.showStack( 'main' );
  281. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  282. expect( balloon.hasView( customView ) ).to.equal( true );
  283. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.not.equal( fakeDomElement );
  284. const newFakeViewElement = view.document.getRoot().getChild( 1 ).getChild( 0 );
  285. const newFakeDomElement = editor.editing.view.domConverter.mapViewToDom( newFakeViewElement );
  286. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.equal( newFakeDomElement );
  287. } );
  288. } );
  289. } );
  290. describe( 'WidgetToolbarRepository - integration with the BalloonToolbar', () => {
  291. let clock, editor, model, balloon, balloonToolbar, widgetToolbarRepository, editorElement;
  292. testUtils.createSinonSandbox();
  293. beforeEach( () => {
  294. editorElement = document.createElement( 'div' );
  295. document.body.appendChild( editorElement );
  296. clock = testUtils.sinon.useFakeTimers();
  297. return BalloonEditor
  298. .create( editorElement, {
  299. plugins: [ Paragraph, FakeButton, WidgetToolbarRepository, FakeWidget, Bold ],
  300. balloonToolbar: [ 'bold' ],
  301. fake: {
  302. toolbar: [ 'fake_button' ]
  303. }
  304. } )
  305. .then( newEditor => {
  306. editor = newEditor;
  307. model = newEditor.model;
  308. widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  309. balloon = editor.plugins.get( 'ContextualBalloon' );
  310. balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
  311. editor.ui.focusTracker.isFocused = true;
  312. } );
  313. } );
  314. afterEach( () => {
  315. editorElement.remove();
  316. return editor.destroy();
  317. } );
  318. it( 'balloon toolbar should be hidden when the widget is selected', () => {
  319. widgetToolbarRepository.register( 'fake', {
  320. items: editor.config.get( 'fake.toolbar' ),
  321. getRelatedElement: getSelectedFakeWidget,
  322. } );
  323. const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view;
  324. editor.editing.view.document.isFocused = true;
  325. setData( model, '[<fake-widget></fake-widget>]<paragraph>foo</paragraph>' );
  326. clock.tick( 200 );
  327. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  328. } );
  329. it( 'balloon toolbar should be visible when the widget is not selected', () => {
  330. widgetToolbarRepository.register( 'fake', {
  331. items: editor.config.get( 'fake.toolbar' ),
  332. getRelatedElement: getSelectedFakeWidget
  333. } );
  334. editor.editing.view.document.isFocused = true;
  335. setData( model, '<fake-widget></fake-widget><paragraph>[foo]</paragraph>' );
  336. clock.tick( 200 );
  337. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  338. } );
  339. } );
  340. function getSelectedFakeWidget( selection ) {
  341. const viewElement = selection.getSelectedElement();
  342. if ( viewElement && isWidget( viewElement ) && !!viewElement.getCustomProperty( 'fakeWidget' ) ) {
  343. return viewElement;
  344. }
  345. return null;
  346. }
  347. function getSelectedFakeChildWidget( selection ) {
  348. const viewElement = selection.getSelectedElement();
  349. if ( viewElement && isWidget( viewElement ) && !!viewElement.getCustomProperty( 'fakeChildWidget' ) ) {
  350. return viewElement;
  351. }
  352. return null;
  353. }
  354. function getSelectedFakeWidgetContent( selection ) {
  355. const pos = selection.getFirstPosition();
  356. let node = pos.parent;
  357. while ( node ) {
  358. if ( node.is( 'element' ) && isWidget( node ) && node.getCustomProperty( 'fakeWidget' ) ) {
  359. return node;
  360. }
  361. node = node.parent;
  362. }
  363. return null;
  364. }
  365. // Plugin that adds fake_button to editor's component factory.
  366. class FakeButton extends Plugin {
  367. init() {
  368. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  369. const view = new ButtonView( locale );
  370. view.set( {
  371. label: 'fake button'
  372. } );
  373. return view;
  374. } );
  375. }
  376. }
  377. // Simple widget plugin
  378. // It registers `<fake-widget>` block in model and represents `div` in the view.
  379. // It allows having text inside self.
  380. class FakeWidget extends Plugin {
  381. static get requires() {
  382. return [ Widget ];
  383. }
  384. init() {
  385. const editor = this.editor;
  386. const schema = editor.model.schema;
  387. schema.register( 'fake-widget', {
  388. isObject: true,
  389. isBlock: true,
  390. allowWhere: '$block'
  391. } );
  392. schema.extend( '$text', { allowIn: 'fake-widget' } );
  393. schema.extend( 'paragraph', { allowIn: 'fake-widget' } );
  394. const conversion = editor.conversion;
  395. conversion.for( 'dataDowncast' ).elementToElement( {
  396. model: 'fake-widget',
  397. view: ( modelElement, viewWriter ) => {
  398. return viewWriter.createContainerElement( 'div' );
  399. }
  400. } );
  401. conversion.for( 'editingDowncast' ).elementToElement( {
  402. model: 'fake-widget',
  403. view: ( modelElement, viewWriter ) => {
  404. const fakeWidget = viewWriter.createContainerElement( 'div' );
  405. viewWriter.setCustomProperty( 'fakeWidget', true, fakeWidget );
  406. return toWidget( fakeWidget, viewWriter, { label: 'fake-widget' } );
  407. }
  408. } );
  409. conversion.for( 'upcast' ).elementToElement( {
  410. view: {
  411. name: 'div'
  412. },
  413. model: ( view, modelWriter ) => {
  414. return modelWriter.createElement( 'fake-widget' );
  415. }
  416. } );
  417. }
  418. }
  419. // A simple child widget plugin
  420. // It registers `<fake-child-widget>` block in model and represents `div` in the view.
  421. // It allows having text inside self.
  422. class FakeChildWidget extends Plugin {
  423. static get requires() {
  424. return [ Widget ];
  425. }
  426. init() {
  427. const editor = this.editor;
  428. const schema = editor.model.schema;
  429. schema.register( 'fake-child-widget', {
  430. isObject: true,
  431. isBlock: true,
  432. allowWhere: '$block',
  433. allowIn: 'fake-widget'
  434. } );
  435. schema.extend( '$text', { allowIn: 'fake-child-widget' } );
  436. schema.extend( 'paragraph', { allowIn: 'fake-child-widget' } );
  437. const conversion = editor.conversion;
  438. conversion.for( 'dataDowncast' ).elementToElement( {
  439. model: 'fake-child-widget',
  440. view: ( modelElement, viewWriter ) => {
  441. return viewWriter.createContainerElement( 'div' );
  442. }
  443. } );
  444. conversion.for( 'editingDowncast' ).elementToElement( {
  445. model: 'fake-child-widget',
  446. view: ( modelElement, viewWriter ) => {
  447. const fakeWidget = viewWriter.createContainerElement( 'div' );
  448. viewWriter.setCustomProperty( 'fakeChildWidget', true, fakeWidget );
  449. return toWidget( fakeWidget, viewWriter, { label: 'fake-child-widget' } );
  450. }
  451. } );
  452. conversion.for( 'upcast' ).elementToElement( {
  453. view: {
  454. name: 'div'
  455. },
  456. model: ( view, modelWriter ) => {
  457. return modelWriter.createElement( 'fake-child-widget' );
  458. }
  459. } );
  460. }
  461. }