linkui.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, Event */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  13. import LinkEditing from '../src/linkediting';
  14. import LinkUI from '../src/linkui';
  15. import LinkFormView from '../src/ui/linkformview';
  16. import LinkActionsView from '../src/ui/linkactionsview';
  17. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  18. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  19. import View from '@ckeditor/ckeditor5-ui/src/view';
  20. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  21. describe( 'LinkUI', () => {
  22. let editor, linkUIFeature, linkButton, balloon, formView, actionsView, editorElement;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. editorElement = document.createElement( 'div' );
  26. document.body.appendChild( editorElement );
  27. return ClassicTestEditor
  28. .create( editorElement, {
  29. plugins: [ LinkEditing, LinkUI, Paragraph, BlockQuote ]
  30. } )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. linkUIFeature = editor.plugins.get( LinkUI );
  34. linkButton = editor.ui.componentFactory.create( 'link' );
  35. balloon = editor.plugins.get( ContextualBalloon );
  36. formView = linkUIFeature.formView;
  37. actionsView = linkUIFeature.actionsView;
  38. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  39. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  40. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  41. formView.render();
  42. } );
  43. } );
  44. afterEach( () => {
  45. editorElement.remove();
  46. return editor.destroy();
  47. } );
  48. it( 'should be named', () => {
  49. expect( LinkUI.pluginName ).to.equal( 'LinkUI' );
  50. } );
  51. it( 'should load ContextualBalloon', () => {
  52. expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
  53. } );
  54. describe( 'init', () => {
  55. it( 'should register click observer', () => {
  56. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  57. } );
  58. it( 'should create #actionsView', () => {
  59. expect( actionsView ).to.be.instanceOf( LinkActionsView );
  60. } );
  61. it( 'should create #formView', () => {
  62. expect( formView ).to.be.instanceOf( LinkFormView );
  63. } );
  64. describe( 'link toolbar button', () => {
  65. it( 'should be registered', () => {
  66. expect( linkButton ).to.be.instanceOf( ButtonView );
  67. } );
  68. it( 'should be toggleable button', () => {
  69. expect( linkButton.isToggleable ).to.be.true;
  70. } );
  71. it( 'should be bound to the link command', () => {
  72. const command = editor.commands.get( 'link' );
  73. command.isEnabled = true;
  74. command.value = 'http://ckeditor.com';
  75. expect( linkButton.isOn ).to.be.true;
  76. expect( linkButton.isEnabled ).to.be.true;
  77. command.isEnabled = false;
  78. command.value = undefined;
  79. expect( linkButton.isOn ).to.be.false;
  80. expect( linkButton.isEnabled ).to.be.false;
  81. } );
  82. it( 'should call #_showUI upon #execute', () => {
  83. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  84. linkButton.fire( 'execute' );
  85. sinon.assert.calledWithExactly( spy, true );
  86. } );
  87. } );
  88. } );
  89. describe( '_showUI()', () => {
  90. let balloonAddSpy;
  91. beforeEach( () => {
  92. balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
  93. editor.editing.view.document.isFocused = true;
  94. } );
  95. it( 'should not throw if the UI is already visible', () => {
  96. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  97. linkUIFeature._showUI();
  98. expect( () => {
  99. linkUIFeature._showUI();
  100. } ).to.not.throw();
  101. } );
  102. it( 'should add #formView to the balloon and attach the balloon to the selection when text fragment is selected', () => {
  103. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  104. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  105. linkUIFeature._showUI();
  106. expect( balloon.visibleView ).to.equal( formView );
  107. sinon.assert.calledWithExactly( balloonAddSpy, {
  108. view: formView,
  109. position: {
  110. target: selectedRange
  111. }
  112. } );
  113. } );
  114. it( 'should add #formView to the balloon and attach the balloon to the selection when selection is collapsed', () => {
  115. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  116. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  117. linkUIFeature._showUI();
  118. expect( balloon.visibleView ).to.equal( formView );
  119. sinon.assert.calledWithExactly( balloonAddSpy, {
  120. view: formView,
  121. position: {
  122. target: selectedRange
  123. }
  124. } );
  125. } );
  126. it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
  127. 'that link',
  128. () => {
  129. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  130. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  131. linkUIFeature._showUI();
  132. expect( balloon.visibleView ).to.equal( actionsView );
  133. sinon.assert.calledWithExactly( balloonAddSpy, {
  134. view: actionsView,
  135. position: {
  136. target: linkElement
  137. }
  138. } );
  139. } );
  140. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  141. it( 'should add #formView to the balloon when collapsed selection is inside the link and #actionsView is already visible', () => {
  142. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  143. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  144. linkUIFeature._showUI();
  145. expect( balloon.visibleView ).to.equal( actionsView );
  146. sinon.assert.calledWithExactly( balloonAddSpy, {
  147. view: actionsView,
  148. position: {
  149. target: linkElement
  150. }
  151. } );
  152. linkUIFeature._showUI();
  153. expect( balloon.visibleView ).to.equal( formView );
  154. sinon.assert.calledWithExactly( balloonAddSpy, {
  155. view: formView,
  156. position: {
  157. target: linkElement
  158. }
  159. } );
  160. } );
  161. it( 'should disable #formView and #actionsView elements when link and unlink commands are disabled', () => {
  162. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  163. linkUIFeature._showUI();
  164. editor.commands.get( 'link' ).isEnabled = true;
  165. editor.commands.get( 'unlink' ).isEnabled = true;
  166. expect( formView.urlInputView.isReadOnly ).to.be.false;
  167. expect( formView.saveButtonView.isEnabled ).to.be.true;
  168. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  169. expect( actionsView.unlinkButtonView.isEnabled ).to.be.true;
  170. expect( actionsView.editButtonView.isEnabled ).to.be.true;
  171. editor.commands.get( 'link' ).isEnabled = false;
  172. editor.commands.get( 'unlink' ).isEnabled = false;
  173. expect( formView.urlInputView.isReadOnly ).to.be.true;
  174. expect( formView.saveButtonView.isEnabled ).to.be.false;
  175. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  176. expect( actionsView.unlinkButtonView.isEnabled ).to.be.false;
  177. expect( actionsView.editButtonView.isEnabled ).to.be.false;
  178. } );
  179. // https://github.com/ckeditor/ckeditor5-link/issues/78
  180. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (selected link)', () => {
  181. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  182. // Mock some leftover value **in DOM**, e.g. after previous editing.
  183. formView.urlInputView.fieldView.element.value = 'leftover';
  184. linkUIFeature._showUI();
  185. actionsView.fire( 'edit' );
  186. expect( formView.urlInputView.fieldView.element.value ).to.equal( 'url' );
  187. } );
  188. // https://github.com/ckeditor/ckeditor5-link/issues/123
  189. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (no link selected)', () => {
  190. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  191. linkUIFeature._showUI();
  192. expect( formView.urlInputView.fieldView.element.value ).to.equal( '' );
  193. } );
  194. it( 'should optionally force `main` stack to be visible', () => {
  195. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  196. balloon.add( {
  197. view: new View(),
  198. stackId: 'secondary'
  199. } );
  200. linkUIFeature._showUI( true );
  201. expect( balloon.visibleView ).to.equal( formView );
  202. } );
  203. // https://github.com/ckeditor/ckeditor5-link/issues/242.
  204. it( 'should update balloon position when is switched in rotator to a visible panel', () => {
  205. setModelData( editor.model, '<paragraph>fo<$text linkHref="foo">o[] b</$text>ar</paragraph>' );
  206. linkUIFeature._showUI();
  207. const customView = new View();
  208. const linkViewElement = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 1 );
  209. const linkDomElement = editor.editing.view.domConverter.mapViewToDom( linkViewElement );
  210. expect( balloon.visibleView ).to.equal( actionsView );
  211. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.equal( linkDomElement );
  212. balloon.add( {
  213. stackId: 'custom',
  214. view: customView,
  215. position: { target: {} }
  216. } );
  217. balloon.showStack( 'custom' );
  218. expect( balloon.visibleView ).to.equal( customView );
  219. expect( balloon.hasView( actionsView ) ).to.equal( true );
  220. editor.execute( 'blockQuote' );
  221. balloon.showStack( 'main' );
  222. expect( balloon.visibleView ).to.equal( actionsView );
  223. expect( balloon.hasView( customView ) ).to.equal( true );
  224. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.not.equal( linkDomElement );
  225. const newLinkViewElement = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 1 );
  226. const newLinkDomElement = editor.editing.view.domConverter.mapViewToDom( newLinkViewElement );
  227. expect( balloon.view.pin.lastCall.args[ 0 ].target ).to.equal( newLinkDomElement );
  228. } );
  229. describe( 'response to ui#update', () => {
  230. let view, viewDocument;
  231. beforeEach( () => {
  232. view = editor.editing.view;
  233. viewDocument = view.document;
  234. } );
  235. it( 'should not duplicate #update listeners', () => {
  236. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  237. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  238. linkUIFeature._showUI();
  239. editor.ui.fire( 'update' );
  240. linkUIFeature._hideUI();
  241. linkUIFeature._showUI();
  242. editor.ui.fire( 'update' );
  243. sinon.assert.calledTwice( spy );
  244. } );
  245. // https://github.com/ckeditor/ckeditor5-link/issues/113
  246. it( 'updates the position of the panel – editing a link, then the selection remains in the link', () => {
  247. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  248. linkUIFeature._showUI();
  249. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  250. expect( getViewData( view ) ).to.equal(
  251. '<p><a class="ck-link_selected" href="url">f{}oo</a></p>'
  252. );
  253. const root = viewDocument.getRoot();
  254. const linkElement = root.getChild( 0 ).getChild( 0 );
  255. const text = linkElement.getChild( 0 );
  256. // Move selection to foo[].
  257. view.change( writer => {
  258. writer.setSelection( text, 3, true );
  259. } );
  260. sinon.assert.calledOnce( spy );
  261. sinon.assert.calledWithExactly( spy, {
  262. target: view.domConverter.mapViewToDom( linkElement )
  263. } );
  264. } );
  265. // https://github.com/ckeditor/ckeditor5-link/issues/113
  266. it( 'updates the position of the panel – creating a new link, then the selection moved', () => {
  267. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  268. linkUIFeature._showUI();
  269. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  270. const root = viewDocument.getRoot();
  271. const text = root.getChild( 0 ).getChild( 0 );
  272. view.change( writer => {
  273. writer.setSelection( text, 3, true );
  274. } );
  275. sinon.assert.calledOnce( spy );
  276. sinon.assert.calledWithExactly( spy, {
  277. target: editorElement.ownerDocument.getSelection().getRangeAt( 0 )
  278. } );
  279. } );
  280. it( 'not update the position when is in not visible stack', () => {
  281. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  282. linkUIFeature._showUI();
  283. const customView = new View();
  284. balloon.add( {
  285. stackId: 'custom',
  286. view: customView,
  287. position: { target: {} }
  288. } );
  289. balloon.showStack( 'custom' );
  290. expect( balloon.visibleView ).to.equal( customView );
  291. expect( balloon.hasView( actionsView ) ).to.equal( true );
  292. const spy = testUtils.sinon.spy( balloon, 'updatePosition' );
  293. editor.ui.fire( 'update' );
  294. sinon.assert.notCalled( spy );
  295. } );
  296. // https://github.com/ckeditor/ckeditor5-link/issues/113
  297. it( 'hides of the panel – editing a link, then the selection moved out of the link', () => {
  298. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
  299. linkUIFeature._showUI();
  300. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  301. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  302. const root = viewDocument.getRoot();
  303. const text = root.getChild( 0 ).getChild( 1 );
  304. // Move selection to b[]ar.
  305. view.change( writer => {
  306. writer.setSelection( text, 1, true );
  307. } );
  308. sinon.assert.calledOnce( spyHide );
  309. sinon.assert.notCalled( spyUpdate );
  310. } );
  311. // https://github.com/ckeditor/ckeditor5-link/issues/113
  312. it( 'hides the panel – editing a link, then the selection expands', () => {
  313. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  314. linkUIFeature._showUI();
  315. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  316. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  317. expect( getViewData( view ) ).to.equal( '<p><a class="ck-link_selected" href="url">f{}oo</a></p>' );
  318. const root = viewDocument.getRoot();
  319. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  320. // Move selection to f[o]o.
  321. view.change( writer => {
  322. writer.setSelection( writer.createRange(
  323. writer.createPositionAt( text, 1 ),
  324. writer.createPositionAt( text, 2 )
  325. ), true );
  326. } );
  327. sinon.assert.calledOnce( spyHide );
  328. sinon.assert.notCalled( spyUpdate );
  329. } );
  330. // https://github.com/ckeditor/ckeditor5-link/issues/113
  331. it( 'hides the panel – creating a new link, then the selection moved to another parent', () => {
  332. setModelData( editor.model, '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  333. linkUIFeature._showUI();
  334. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  335. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  336. const root = viewDocument.getRoot();
  337. const text = root.getChild( 1 ).getChild( 0 );
  338. // Move selection to f[o]o.
  339. view.change( writer => {
  340. writer.setSelection( writer.createRange(
  341. writer.createPositionAt( text, 1 ),
  342. writer.createPositionAt( text, 2 )
  343. ), true );
  344. } );
  345. sinon.assert.calledOnce( spyHide );
  346. sinon.assert.notCalled( spyUpdate );
  347. } );
  348. } );
  349. } );
  350. describe( '_hideUI()', () => {
  351. beforeEach( () => {
  352. linkUIFeature._showUI();
  353. } );
  354. it( 'should remove the UI from the balloon', () => {
  355. expect( balloon.hasView( formView ) ).to.be.true;
  356. expect( balloon.hasView( actionsView ) ).to.be.true;
  357. linkUIFeature._hideUI();
  358. expect( balloon.hasView( formView ) ).to.be.false;
  359. expect( balloon.hasView( actionsView ) ).to.be.false;
  360. } );
  361. it( 'should focus the `editable` by default', () => {
  362. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  363. linkUIFeature._hideUI();
  364. // First call is from _removeFormView.
  365. sinon.assert.calledTwice( spy );
  366. } );
  367. // https://github.com/ckeditor/ckeditor5-link/issues/193
  368. it( 'should focus the `editable` before before removing elements from the balloon', () => {
  369. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  370. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  371. linkUIFeature._hideUI();
  372. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  373. } );
  374. it( 'should not throw an error when views are not in the `balloon`', () => {
  375. linkUIFeature._hideUI();
  376. expect( () => {
  377. linkUIFeature._hideUI();
  378. } ).to.not.throw();
  379. } );
  380. it( 'should clear ui#update listener from the ViewDocument', () => {
  381. const spy = sinon.spy();
  382. linkUIFeature.listenTo( editor.ui, 'update', spy );
  383. linkUIFeature._hideUI();
  384. editor.ui.fire( 'update' );
  385. sinon.assert.notCalled( spy );
  386. } );
  387. } );
  388. describe( 'keyboard support', () => {
  389. it( 'should show the UI on Ctrl+K keystroke', () => {
  390. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  391. editor.keystrokes.press( {
  392. keyCode: keyCodes.k,
  393. ctrlKey: true,
  394. preventDefault: sinon.spy(),
  395. stopPropagation: sinon.spy()
  396. } );
  397. sinon.assert.calledWithExactly( spy, true );
  398. } );
  399. it( 'should prevent default action on Ctrl+K keystroke', () => {
  400. const preventDefaultSpy = sinon.spy();
  401. const stopPropagationSpy = sinon.spy();
  402. editor.keystrokes.press( {
  403. keyCode: keyCodes.k,
  404. ctrlKey: true,
  405. preventDefault: preventDefaultSpy,
  406. stopPropagation: stopPropagationSpy
  407. } );
  408. sinon.assert.calledOnce( preventDefaultSpy );
  409. sinon.assert.calledOnce( stopPropagationSpy );
  410. } );
  411. it( 'should make stack with link visible on Ctrl+K keystroke - no link', () => {
  412. const command = editor.commands.get( 'link' );
  413. command.isEnabled = true;
  414. balloon.add( {
  415. view: new View(),
  416. stackId: 'custom'
  417. } );
  418. editor.keystrokes.press( {
  419. keyCode: keyCodes.k,
  420. ctrlKey: true,
  421. preventDefault: sinon.spy(),
  422. stopPropagation: sinon.spy()
  423. } );
  424. expect( balloon.visibleView ).to.equal( formView );
  425. } );
  426. it( 'should make stack with link visible on Ctrl+K keystroke - link', () => {
  427. setModelData( editor.model, '<paragraph><$text linkHref="foo.html">f[]oo</$text></paragraph>' );
  428. const customView = new View();
  429. balloon.add( {
  430. view: customView,
  431. stackId: 'custom'
  432. } );
  433. expect( balloon.visibleView ).to.equal( customView );
  434. editor.keystrokes.press( {
  435. keyCode: keyCodes.k,
  436. ctrlKey: true,
  437. preventDefault: sinon.spy(),
  438. stopPropagation: sinon.spy()
  439. } );
  440. expect( balloon.visibleView ).to.equal( actionsView );
  441. editor.keystrokes.press( {
  442. keyCode: keyCodes.k,
  443. ctrlKey: true,
  444. preventDefault: sinon.spy(),
  445. stopPropagation: sinon.spy()
  446. } );
  447. expect( balloon.visibleView ).to.equal( formView );
  448. } );
  449. it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
  450. const keyEvtData = {
  451. keyCode: keyCodes.tab,
  452. preventDefault: sinon.spy(),
  453. stopPropagation: sinon.spy()
  454. };
  455. const normalPriorityTabCallbackSpy = sinon.spy();
  456. const highestPriorityTabCallbackSpy = sinon.spy();
  457. editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
  458. editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
  459. // Balloon is invisible, form not focused.
  460. actionsView.focusTracker.isFocused = false;
  461. const spy = sinon.spy( actionsView, 'focus' );
  462. editor.keystrokes.press( keyEvtData );
  463. sinon.assert.notCalled( keyEvtData.preventDefault );
  464. sinon.assert.notCalled( keyEvtData.stopPropagation );
  465. sinon.assert.notCalled( spy );
  466. sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
  467. sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
  468. // Balloon is visible, form focused.
  469. linkUIFeature._showUI();
  470. testUtils.sinon.stub( linkUIFeature, '_areActionsVisible' ).value( true );
  471. actionsView.focusTracker.isFocused = true;
  472. editor.keystrokes.press( keyEvtData );
  473. sinon.assert.notCalled( keyEvtData.preventDefault );
  474. sinon.assert.notCalled( keyEvtData.stopPropagation );
  475. sinon.assert.notCalled( spy );
  476. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  477. sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
  478. // Balloon is still visible, form not focused.
  479. actionsView.focusTracker.isFocused = false;
  480. editor.keystrokes.press( keyEvtData );
  481. sinon.assert.calledOnce( keyEvtData.preventDefault );
  482. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  483. sinon.assert.calledOnce( spy );
  484. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  485. sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
  486. } );
  487. it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
  488. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  489. const keyEvtData = {
  490. keyCode: keyCodes.esc,
  491. preventDefault: sinon.spy(),
  492. stopPropagation: sinon.spy()
  493. };
  494. // Balloon is visible.
  495. linkUIFeature._showUI();
  496. editor.keystrokes.press( keyEvtData );
  497. sinon.assert.calledWithExactly( spy );
  498. } );
  499. it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
  500. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  501. const keyEvtData = {
  502. keyCode: keyCodes.esc,
  503. preventDefault: () => {},
  504. stopPropagation: () => {}
  505. };
  506. const viewMock = {
  507. ready: true,
  508. render: () => {},
  509. destroy: () => {}
  510. };
  511. linkUIFeature._showUI();
  512. // Some view precedes the link UI in the balloon.
  513. balloon.add( { view: viewMock } );
  514. editor.keystrokes.press( keyEvtData );
  515. sinon.assert.notCalled( spy );
  516. } );
  517. } );
  518. describe( 'mouse support', () => {
  519. it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
  520. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  521. linkUIFeature._showUI();
  522. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  523. sinon.assert.calledWithExactly( spy );
  524. } );
  525. it( 'should hide the UI when link is in not currently visible stack', () => {
  526. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  527. balloon.add( {
  528. view: new View(),
  529. stackId: 'secondary'
  530. } );
  531. linkUIFeature._showUI();
  532. // Be sure any of link view is not currently visible/
  533. expect( balloon.visibleView ).to.not.equal( actionsView );
  534. expect( balloon.visibleView ).to.not.equal( formView );
  535. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  536. sinon.assert.calledWithExactly( spy );
  537. } );
  538. it( 'should not hide the UI upon clicking inside the the UI', () => {
  539. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  540. linkUIFeature._showUI();
  541. balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  542. sinon.assert.notCalled( spy );
  543. } );
  544. describe( 'clicking on editable', () => {
  545. let observer, spy;
  546. beforeEach( () => {
  547. observer = editor.editing.view.getObserver( ClickObserver );
  548. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  549. spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  550. } );
  551. it( 'should show the UI when collapsed selection is inside link element', () => {
  552. setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
  553. observer.fire( 'click', { target: document.body } );
  554. sinon.assert.calledWithExactly( spy );
  555. } );
  556. it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
  557. setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
  558. observer.fire( 'click', { target: {} } );
  559. sinon.assert.calledWithExactly( spy );
  560. } );
  561. it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
  562. setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
  563. observer.fire( 'click', { target: {} } );
  564. sinon.assert.calledWithExactly( spy );
  565. } );
  566. it( 'should do nothing when selection is not inside link element', () => {
  567. setModelData( editor.model, '[]' );
  568. observer.fire( 'click', { target: {} } );
  569. sinon.assert.notCalled( spy );
  570. } );
  571. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
  572. setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
  573. observer.fire( 'click', { target: {} } );
  574. sinon.assert.notCalled( spy );
  575. } );
  576. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
  577. setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
  578. observer.fire( 'click', { target: {} } );
  579. sinon.assert.notCalled( spy );
  580. } );
  581. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
  582. setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
  583. observer.fire( 'click', { target: {} } );
  584. sinon.assert.notCalled( spy );
  585. } );
  586. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
  587. setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
  588. observer.fire( 'click', { target: {} } );
  589. sinon.assert.notCalled( spy );
  590. } );
  591. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
  592. setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
  593. observer.fire( 'click', { target: {} } );
  594. sinon.assert.notCalled( spy );
  595. } );
  596. } );
  597. } );
  598. describe( 'actions view', () => {
  599. let focusEditableSpy;
  600. beforeEach( () => {
  601. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  602. } );
  603. it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
  604. linkUIFeature._showUI();
  605. linkUIFeature._removeFormView();
  606. expect( balloon.visibleView ).to.equal( actionsView );
  607. editor.ui.focusTracker.isFocused = false;
  608. actionsView.element.dispatchEvent( new Event( 'focus' ) );
  609. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  610. } );
  611. describe( 'binding', () => {
  612. it( 'should show the #formView on #edit event and select the URL input field', () => {
  613. linkUIFeature._showUI();
  614. linkUIFeature._removeFormView();
  615. const selectSpy = testUtils.sinon.spy( formView.urlInputView.fieldView, 'select' );
  616. actionsView.fire( 'edit' );
  617. expect( balloon.visibleView ).to.equal( formView );
  618. sinon.assert.calledOnce( selectSpy );
  619. } );
  620. it( 'should execute unlink command on actionsView#unlink event', () => {
  621. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  622. actionsView.fire( 'unlink' );
  623. expect( executeSpy.calledOnce ).to.be.true;
  624. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
  625. } );
  626. it( 'should hide and focus editable on actionsView#unlink event', () => {
  627. linkUIFeature._showUI();
  628. linkUIFeature._removeFormView();
  629. // Removing the form would call the focus spy.
  630. focusEditableSpy.resetHistory();
  631. actionsView.fire( 'unlink' );
  632. expect( balloon.visibleView ).to.be.null;
  633. expect( focusEditableSpy.calledOnce ).to.be.true;
  634. } );
  635. it( 'should hide after Esc key press', () => {
  636. const keyEvtData = {
  637. keyCode: keyCodes.esc,
  638. preventDefault: sinon.spy(),
  639. stopPropagation: sinon.spy()
  640. };
  641. linkUIFeature._showUI();
  642. linkUIFeature._removeFormView();
  643. // Removing the form would call the focus spy.
  644. focusEditableSpy.resetHistory();
  645. actionsView.keystrokes.press( keyEvtData );
  646. expect( balloon.visibleView ).to.equal( null );
  647. expect( focusEditableSpy.calledOnce ).to.be.true;
  648. } );
  649. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  650. it( 'should add the #formView upon Ctrl+K keystroke press', () => {
  651. const keyEvtData = {
  652. keyCode: keyCodes.k,
  653. ctrlKey: true,
  654. preventDefault: sinon.spy(),
  655. stopPropagation: sinon.spy()
  656. };
  657. linkUIFeature._showUI();
  658. linkUIFeature._removeFormView();
  659. expect( balloon.visibleView ).to.equal( actionsView );
  660. actionsView.keystrokes.press( keyEvtData );
  661. expect( balloon.visibleView ).to.equal( formView );
  662. } );
  663. } );
  664. } );
  665. describe( 'link form view', () => {
  666. let focusEditableSpy;
  667. beforeEach( () => {
  668. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  669. } );
  670. it( 'should mark the editor UI as focused when the #formView is focused', () => {
  671. linkUIFeature._showUI();
  672. expect( balloon.visibleView ).to.equal( formView );
  673. editor.ui.focusTracker.isFocused = false;
  674. formView.element.dispatchEvent( new Event( 'focus' ) );
  675. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  676. } );
  677. describe( 'binding', () => {
  678. beforeEach( () => {
  679. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  680. } );
  681. it( 'should bind formView.urlInputView#value to link command value', () => {
  682. const command = editor.commands.get( 'link' );
  683. expect( formView.urlInputView.fieldView.value ).to.be.undefined;
  684. command.value = 'http://cksource.com';
  685. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://cksource.com' );
  686. } );
  687. it( 'should execute link command on formView#submit event', () => {
  688. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  689. formView.urlInputView.fieldView.value = 'http://ckeditor.com';
  690. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://ckeditor.com' );
  691. formView.urlInputView.fieldView.value = 'http://cksource.com';
  692. formView.fire( 'submit' );
  693. expect( executeSpy.calledOnce ).to.be.true;
  694. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com', {} ) ).to.be.true;
  695. } );
  696. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  697. linkUIFeature._showUI();
  698. formView.fire( 'submit' );
  699. expect( balloon.visibleView ).to.equal( actionsView );
  700. expect( focusEditableSpy.calledOnce ).to.be.true;
  701. } );
  702. it( 'should hide and reveal the #actionsView on formView#cancel event if link command has a value', () => {
  703. linkUIFeature._showUI();
  704. const command = editor.commands.get( 'link' );
  705. command.value = 'http://foo.com';
  706. formView.fire( 'cancel' );
  707. expect( balloon.visibleView ).to.equal( actionsView );
  708. expect( focusEditableSpy.calledOnce ).to.be.true;
  709. } );
  710. it( 'should hide the balloon on formView#cancel if link command does not have a value', () => {
  711. linkUIFeature._showUI();
  712. formView.fire( 'cancel' );
  713. expect( balloon.visibleView ).to.be.null;
  714. } );
  715. it( 'should hide and reveal the #actionsView after Esc key press if link command has a value', () => {
  716. const keyEvtData = {
  717. keyCode: keyCodes.esc,
  718. preventDefault: sinon.spy(),
  719. stopPropagation: sinon.spy()
  720. };
  721. linkUIFeature._showUI();
  722. const command = editor.commands.get( 'link' );
  723. command.value = 'http://foo.com';
  724. formView.keystrokes.press( keyEvtData );
  725. expect( balloon.visibleView ).to.equal( actionsView );
  726. expect( focusEditableSpy.calledOnce ).to.be.true;
  727. } );
  728. it( 'should hide the balloon after Esc key press if link command does not have a value', () => {
  729. const keyEvtData = {
  730. keyCode: keyCodes.esc,
  731. preventDefault: sinon.spy(),
  732. stopPropagation: sinon.spy()
  733. };
  734. linkUIFeature._showUI();
  735. formView.keystrokes.press( keyEvtData );
  736. expect( balloon.visibleView ).to.be.null;
  737. } );
  738. // https://github.com/ckeditor/ckeditor5/issues/1501
  739. it( 'should blur url input element before hiding the view', () => {
  740. linkUIFeature._showUI();
  741. const focusSpy = testUtils.sinon.spy( formView.saveButtonView, 'focus' );
  742. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  743. formView.fire( 'cancel' );
  744. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  745. } );
  746. describe( 'support manual decorators', () => {
  747. let editorElement, editor, model, formView, linkUIFeature;
  748. beforeEach( () => {
  749. editorElement = document.createElement( 'div' );
  750. document.body.appendChild( editorElement );
  751. return ClassicTestEditor
  752. .create( editorElement, {
  753. plugins: [ LinkEditing, LinkUI, Paragraph ],
  754. link: {
  755. decorators: {
  756. isFoo: {
  757. mode: 'manual',
  758. label: 'Foo',
  759. attributes: {
  760. foo: 'bar'
  761. }
  762. }
  763. }
  764. }
  765. } )
  766. .then( newEditor => {
  767. editor = newEditor;
  768. model = editor.model;
  769. model.schema.extend( '$text', {
  770. allowIn: '$root',
  771. allowAttributes: 'linkHref'
  772. } );
  773. linkUIFeature = editor.plugins.get( LinkUI );
  774. const balloon = editor.plugins.get( ContextualBalloon );
  775. formView = linkUIFeature.formView;
  776. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  777. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  778. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  779. formView.render();
  780. } );
  781. } );
  782. afterEach( () => {
  783. editorElement.remove();
  784. return editor.destroy();
  785. } );
  786. it( 'should gather information about manual decorators', () => {
  787. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  788. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  789. expect( formView.urlInputView.fieldView.element.value ).to.equal( 'url' );
  790. expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkIsFoo: true } );
  791. formView.fire( 'submit' );
  792. expect( executeSpy.calledOnce ).to.be.true;
  793. expect( executeSpy.calledWithExactly( 'link', 'url', { linkIsFoo: true } ) ).to.be.true;
  794. } );
  795. it( 'should reset switch state when form view is closed', () => {
  796. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  797. const manualDecorators = editor.commands.get( 'link' ).manualDecorators;
  798. const firstDecoratorModel = manualDecorators.first;
  799. const firstDecoratorSwitch = formView._manualDecoratorSwitches.first;
  800. expect( firstDecoratorModel.value, 'Initial value should be read from the model (true)' ).to.be.true;
  801. expect( firstDecoratorSwitch.isOn, 'Initial value should be read from the model (true)' ).to.be.true;
  802. firstDecoratorSwitch.fire( 'execute' );
  803. expect( firstDecoratorModel.value, 'Pressing button toggles value' ).to.be.false;
  804. expect( firstDecoratorSwitch.isOn, 'Pressing button toggles value' ).to.be.false;
  805. linkUIFeature._closeFormView();
  806. expect( firstDecoratorModel.value, 'Close form view without submit resets value to initial state' ).to.be.true;
  807. expect( firstDecoratorSwitch.isOn, 'Close form view without submit resets value to initial state' ).to.be.true;
  808. } );
  809. } );
  810. } );
  811. } );
  812. } );