linkui.js 35 KB

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