|
|
@@ -15,6 +15,7 @@ import LinkEngine from '/ckeditor5/link/linkengine.js';
|
|
|
import Button from '/ckeditor5/ui/button/button.js';
|
|
|
import LinkBalloonPanel from '/ckeditor5/link/ui/linkballoonpanel.js';
|
|
|
|
|
|
+import Range from '/ckeditor5/engine/view/range.js';
|
|
|
import ClickObserver from '/ckeditor5/engine/view/observer/clickobserver.js';
|
|
|
|
|
|
testUtils.createSinonSandbox();
|
|
|
@@ -72,36 +73,14 @@ describe( 'Link', () => {
|
|
|
expect( model.isEnabled ).to.be.false;
|
|
|
} );
|
|
|
|
|
|
- it( 'should open panel on linkButton#model execute event, when editor is focused', () => {
|
|
|
- editor.editing.view.isFocused = true;
|
|
|
-
|
|
|
+ it( 'should open panel on linkButton#model execute event', () => {
|
|
|
linkButton.model.fire( 'execute' );
|
|
|
|
|
|
expect( linkFeature.balloonPanel.view.isVisible ).to.true;
|
|
|
} );
|
|
|
|
|
|
- it( 'should select panel input value when panel is opened', () => {
|
|
|
- const selectSpy = sinon.spy( linkFeature.balloonPanel.urlInput.view, 'select' );
|
|
|
-
|
|
|
- editor.editing.view.isFocused = true;
|
|
|
-
|
|
|
- linkButton.model.fire( 'execute' );
|
|
|
-
|
|
|
- expect( selectSpy.calledOnce ).to.true;
|
|
|
-
|
|
|
- selectSpy.restore();
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should not open panel on linkButton#model execute event, when editor is not focused', () => {
|
|
|
- editor.editing.view.isFocused = false;
|
|
|
-
|
|
|
- linkButton.model.fire( 'execute' );
|
|
|
-
|
|
|
- expect( linkFeature.balloonPanel.view.isVisible ).to.false;
|
|
|
- } );
|
|
|
-
|
|
|
it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
|
|
|
- const attachToSpy = sinon.spy( balloonPanel.view, 'attachTo' );
|
|
|
+ const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
|
|
|
|
|
|
editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
|
|
|
@@ -115,7 +94,7 @@ describe( 'Link', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
|
|
|
- const attachToSpy = sinon.spy( balloonPanel.view, 'attachTo' );
|
|
|
+ const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
|
|
|
|
|
|
editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
setModelData( editor.document, 'so[me ur]l' );
|
|
|
@@ -127,6 +106,16 @@ describe( 'Link', () => {
|
|
|
|
|
|
expect( attachToSpy.calledWithExactly( selectedRange, editorElement ) ).to.true;
|
|
|
} );
|
|
|
+
|
|
|
+ it( 'should select panel input value when panel is opened', () => {
|
|
|
+ const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
|
|
|
+
|
|
|
+ editor.editing.view.isFocused = true;
|
|
|
+
|
|
|
+ linkButton.model.fire( 'execute' );
|
|
|
+
|
|
|
+ expect( selectUrlInputSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
describe( 'unlink toolbar button', () => {
|
|
|
@@ -152,114 +141,300 @@ describe( 'Link', () => {
|
|
|
expect( executeSpy.calledOnce ).to.true;
|
|
|
expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
|
|
|
} );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should hide panel on unlinkButton#model execute event', () => {
|
|
|
- balloonPanel.view.model.isVisible = true;
|
|
|
-
|
|
|
- unlinkButton.model.fire( 'execute' );
|
|
|
+ describe( 'link balloon panel', () => {
|
|
|
+ let hidePanelSpy, focusEditableSpy;
|
|
|
|
|
|
- expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ beforeEach( () => {
|
|
|
+ hidePanelSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
|
|
|
+ focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
|
|
|
} );
|
|
|
- } );
|
|
|
|
|
|
- describe( 'link balloon panel', () => {
|
|
|
- it( 'should create LinkBalloonPanel component', () => {
|
|
|
+ it( 'should be created', () => {
|
|
|
expect( balloonPanel ).to.instanceOf( LinkBalloonPanel );
|
|
|
} );
|
|
|
|
|
|
- it( 'should bind balloonPanel#model to link command', () => {
|
|
|
- const model = balloonPanel.model;
|
|
|
- const command = editor.commands.get( 'link' );
|
|
|
+ it( 'should be appended to the document body', () => {
|
|
|
+ expect( document.body.contains( balloonPanel.view.element ) );
|
|
|
+ } );
|
|
|
|
|
|
- expect( model.url ).to.undefined;
|
|
|
+ it( 'should open with selected url input on `CTRL+L` keystroke', () => {
|
|
|
+ const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
|
|
|
|
|
|
- command.value = 'http://cksource.com';
|
|
|
+ editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
|
|
|
|
|
|
- expect( model.url ).to.equal( 'http://cksource.com' );
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+ expect( selectUrlInputSpy.calledOnce ).to.true;
|
|
|
} );
|
|
|
|
|
|
- it( 'should execute link command on balloonPanel#model executeLink event', () => {
|
|
|
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
|
|
|
+ describe( 'binding', () => {
|
|
|
+ it( 'should bind balloonPanel#model to link command', () => {
|
|
|
+ const model = balloonPanel.model;
|
|
|
+ const command = editor.commands.get( 'link' );
|
|
|
|
|
|
- balloonPanel.model.url = 'http://cksource.com';
|
|
|
- balloonPanel.model.fire( 'executeLink' );
|
|
|
+ expect( model.url ).to.undefined;
|
|
|
|
|
|
- expect( executeSpy.calledOnce ).to.true;
|
|
|
- expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
|
|
|
- } );
|
|
|
+ command.value = 'http://cksource.com';
|
|
|
|
|
|
- it( 'should hide balloon panel on balloonPanel#model execute event', () => {
|
|
|
- const hideSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
|
|
|
+ expect( model.url ).to.equal( 'http://cksource.com' );
|
|
|
+ } );
|
|
|
|
|
|
- balloonPanel.model.fire( 'executeLink' );
|
|
|
+ it( 'should execute link command on balloonPanel#model executeLink event', () => {
|
|
|
+ const executeSpy = testUtils.sinon.spy( editor, 'execute' );
|
|
|
|
|
|
- expect( hideSpy.calledOnce ).to.true;
|
|
|
- } );
|
|
|
+ balloonPanel.model.url = 'http://cksource.com';
|
|
|
+ balloonPanel.model.fire( 'executeLink' );
|
|
|
|
|
|
- it( 'should execute unlink command on balloonPanel#model executeUnlink event', () => {
|
|
|
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
|
|
|
+ expect( executeSpy.calledOnce ).to.true;
|
|
|
+ expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
|
|
|
+ } );
|
|
|
|
|
|
- balloonPanel.model.fire( 'executeUnlink' );
|
|
|
+ it( 'should hide and focus editable on balloonPanel#model executeLink event', () => {
|
|
|
+ balloonPanel.model.fire( 'executeLink' );
|
|
|
|
|
|
- expect( executeSpy.calledOnce ).to.true;
|
|
|
- expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
|
|
|
- } );
|
|
|
+ expect( hidePanelSpy.calledOnce ).to.true;
|
|
|
+ expect( focusEditableSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should hide balloon panel on balloonPanel#model executeUnlink event', () => {
|
|
|
- const hideSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
|
|
|
+ it( 'should execute unlink command on balloonPanel#model executeUnlink event', () => {
|
|
|
+ const executeSpy = testUtils.sinon.spy( editor, 'execute' );
|
|
|
|
|
|
- balloonPanel.model.fire( 'executeUnlink' );
|
|
|
+ balloonPanel.model.fire( 'executeUnlink' );
|
|
|
|
|
|
- expect( hideSpy.calledOnce ).to.true;
|
|
|
- } );
|
|
|
+ expect( executeSpy.calledOnce ).to.true;
|
|
|
+ expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should append panel element to the body', () => {
|
|
|
- expect( document.body.contains( balloonPanel.view.element ) );
|
|
|
- } );
|
|
|
+ it( 'should hide and focus editable on balloonPanel#model executeUnlink event', () => {
|
|
|
+ balloonPanel.model.fire( 'executeUnlink' );
|
|
|
|
|
|
- it( 'should open panel on `CTRL+L` keystroke', () => {
|
|
|
- editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
|
|
|
+ expect( hidePanelSpy.calledOnce ).to.true;
|
|
|
+ expect( focusEditableSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
|
|
|
- expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+ it( 'should hide and focus editable on balloonPanel#model executeCancel event', () => {
|
|
|
+ balloonPanel.model.fire( 'executeCancel' );
|
|
|
+
|
|
|
+ expect( hidePanelSpy.calledOnce ).to.true;
|
|
|
+ expect( focusEditableSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- it( 'should focus editor on balloonPanel hide', () => {
|
|
|
- const focusSpy = sinon.spy( editor.editing.view, 'focus' );
|
|
|
+ describe( 'close listeners', () => {
|
|
|
+ describe( 'keyboard', () => {
|
|
|
+ it( 'should listen keyboard events when is open', () => {
|
|
|
+ const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnEsc' );
|
|
|
|
|
|
- balloonPanel.view.model.isVisible = true;
|
|
|
+ balloonPanel.view.model.isVisible = true;
|
|
|
+ document.dispatchEvent( new Event( 'keydown' ) );
|
|
|
|
|
|
- balloonPanel.view.model.isVisible = false;
|
|
|
+ expect( escCloseSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
|
|
|
- expect( focusSpy.calledOnce ).to.true;
|
|
|
- } );
|
|
|
+ it( 'should not listen keyboard events when is closed', () => {
|
|
|
+ const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnEsc' );
|
|
|
|
|
|
- it( 'should hide panel on editor focus event', () => {
|
|
|
- balloonPanel.view.model.isVisible = true;
|
|
|
+ balloonPanel.view.model.isVisible = false;
|
|
|
+ document.dispatchEvent( new Event( 'keydown' ) );
|
|
|
|
|
|
- editor.editing.view.fire( 'focus' );
|
|
|
+ expect( escCloseSpy.calledOnce ).to.false;
|
|
|
+ } );
|
|
|
|
|
|
- expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
- } );
|
|
|
+ it( 'should stop listening keyboard events after close', () => {
|
|
|
+ const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnEsc' );
|
|
|
|
|
|
- it( 'should open panel on editor click, when selection is inside link element', () => {
|
|
|
- const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+ balloonPanel.view.model.isVisible = true;
|
|
|
+ balloonPanel.view.model.isVisible = false;
|
|
|
|
|
|
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
- setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
|
|
|
+ document.dispatchEvent( new Event( 'keydown' ) );
|
|
|
|
|
|
- observer.fire( 'click', { target: document.body } );
|
|
|
+ expect( escCloseSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
|
|
|
- expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+ describe( 'mouse', () => {
|
|
|
+ it( 'should not close on click inside the panel', () => {
|
|
|
+ balloonPanel.view.model.isVisible = true;
|
|
|
+ balloonPanel.view.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
|
|
|
+
|
|
|
+ expect( hidePanelSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should close and not focus editable on click outside the panel', () => {
|
|
|
+ balloonPanel.view.model.isVisible = true;
|
|
|
+ document.dispatchEvent( new Event( 'mouseup' ) );
|
|
|
+
|
|
|
+ expect( hidePanelSpy.calledOnce ).to.true;
|
|
|
+ expect( focusEditableSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not listen mouse events before open', () => {
|
|
|
+ balloonPanel.view.model.isVisible = false;
|
|
|
+ document.dispatchEvent( new Event( 'mouseup' ) );
|
|
|
+
|
|
|
+ expect( hidePanelSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should stop listening mouse events after closed', () => {
|
|
|
+ balloonPanel.view.model.isVisible = true;
|
|
|
+ balloonPanel.view.model.isVisible = false;
|
|
|
+
|
|
|
+ document.dispatchEvent( new Event( 'mouseup' ) );
|
|
|
+
|
|
|
+ expect( hidePanelSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- it( 'should not open panel on editor click, when selection is not inside link element', () => {
|
|
|
- const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+ describe( 'click on editable', () => {
|
|
|
+ it( 'should open with not selected url input when collapsed selection is inside link element', () => {
|
|
|
+ const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: document.body } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+ expect( selectUrlInputSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
|
|
|
+
|
|
|
+ const root = editor.editing.view.getRoot();
|
|
|
+ const text = root.getChild( 0 ).getChild( 0 );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: document.body } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+
|
|
|
+ const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
|
|
|
+
|
|
|
+ editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
|
|
|
+ editor.editing.view.render();
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+ expect( attachToSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should close when selection goes outside the link element', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, 'foo <$text linkHref="url">b[]ar</$text>' );
|
|
|
+
|
|
|
+ const root = editor.editing.view.getRoot();
|
|
|
+ const text = root.getChild( 0 );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: document.body } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+
|
|
|
+ editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
|
|
|
+ editor.editing.view.render();
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should close when selection goes to the other link element with the same href', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, '<$text linkHref="url">f[]oo</$text> bar <$text linkHref="url">biz</$text>' );
|
|
|
+
|
|
|
+ const root = editor.editing.view.getRoot();
|
|
|
+ const text = root.getChild( 2 ).getChild( 0 );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: document.body } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+
|
|
|
+ editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
|
|
|
+ editor.editing.view.render();
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should close when selection becomes non-collapsed', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, '<$text linkHref="url">f[]oo</$text>' );
|
|
|
+
|
|
|
+ const root = editor.editing.view.getRoot();
|
|
|
+ const text = root.getChild( 0 ).getChild( 0 );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: {} } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+
|
|
|
+ editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
|
|
|
+ editor.editing.view.render();
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should stop updating position after close', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
|
|
|
+
|
|
|
+ const root = editor.editing.view.getRoot();
|
|
|
+ const text = root.getChild( 0 ).getChild( 0 );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: {} } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.true;
|
|
|
+
|
|
|
+ balloonPanel.view.model.isVisible = false;
|
|
|
+
|
|
|
+ const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
|
|
|
+
|
|
|
+ editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 2, text, 2 ) ], true );
|
|
|
+ editor.editing.view.render();
|
|
|
+
|
|
|
+ expect( attachToSpy.notCalled ).to.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not open when selection is not inside link element', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ setModelData( editor.document, '[]' );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: {} } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not open when selection is non-collapsed', () => {
|
|
|
+ const observer = editor.editing.view.getObserver( ClickObserver );
|
|
|
+
|
|
|
+ editor.document.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
|
|
|
+
|
|
|
+ observer.fire( 'click', { target: document.body } );
|
|
|
+
|
|
|
+ expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
|
|
|
- setModelData( editor.document, '[]' );
|
|
|
+ describe( '_closePanelOnEsc', () => {
|
|
|
+ it( 'should hide panel and focus editable on ESC press', () => {
|
|
|
+ const eventInfo = {};
|
|
|
+ const domEvent = { keyCode: 27 };
|
|
|
|
|
|
- observer.fire( 'click', { target: document.body } );
|
|
|
+ linkFeature._closePanelOnEsc( eventInfo, domEvent );
|
|
|
|
|
|
- expect( balloonPanel.view.model.isVisible ).to.false;
|
|
|
+ expect( hidePanelSpy.calledOnce ).to.true;
|
|
|
+ expect( focusEditableSpy.calledOnce ).to.true;
|
|
|
+ } );
|
|
|
} );
|
|
|
} );
|
|
|
} );
|