8
0

linkui.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 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. return editor.destroy();
  801. } );
  802. it( 'should gather information about manual decorators', () => {
  803. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  804. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  805. expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
  806. expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkIsFoo: true } );
  807. formView.fire( 'submit' );
  808. expect( executeSpy.calledOnce ).to.be.true;
  809. expect( executeSpy.calledWithExactly( 'link', 'url', { linkIsFoo: true } ) ).to.be.true;
  810. } );
  811. it( 'should reset switch state when form view is closed', () => {
  812. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  813. const manualDecorators = editor.commands.get( 'link' ).manualDecorators;
  814. const firstDecoratorModel = manualDecorators.first;
  815. const firstDecoratorSwitch = formView._manualDecoratorSwitches.first;
  816. expect( firstDecoratorModel.value, 'Initial value should be read from the model (true)' ).to.be.true;
  817. expect( firstDecoratorSwitch.isOn, 'Initial value should be read from the model (true)' ).to.be.true;
  818. firstDecoratorSwitch.fire( 'execute' );
  819. expect( firstDecoratorModel.value, 'Pressing button toggles value' ).to.be.false;
  820. expect( firstDecoratorSwitch.isOn, 'Pressing button toggles value' ).to.be.false;
  821. linkUIFeature._closeFormView();
  822. expect( firstDecoratorModel.value, 'Close form view without submit resets value to initial state' ).to.be.true;
  823. expect( firstDecoratorSwitch.isOn, 'Close form view without submit resets value to initial state' ).to.be.true;
  824. } );
  825. } );
  826. } );
  827. } );
  828. } );