linkui.js 35 KB

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