8
0
Просмотр исходного кода

Adjusted to changes made in engine view.

Szymon Kupś 8 лет назад
Родитель
Сommit
cac94ac513

+ 15 - 14
packages/ckeditor5-link/src/link.js

@@ -52,7 +52,7 @@ export default class Link extends Plugin {
 	init() {
 		const editor = this.editor;
 
-		editor.editing.view.document.addObserver( ClickObserver );
+		editor.editing.view.addObserver( ClickObserver );
 
 		/**
 		 * The form view displayed inside the balloon.
@@ -166,7 +166,7 @@ export default class Link extends Plugin {
 	 * @private
 	 */
 	_attachActions() {
-		const viewDocument = this.editor.editing.view;
+		const viewDocument = this.editor.editing.view.document;
 
 		// Handle click on view document and show panel when selection is placed inside the link element.
 		// Keep panel open until selection will be inside the same link element.
@@ -220,21 +220,21 @@ export default class Link extends Plugin {
 		const linkCommand = editor.commands.get( 'link' );
 		const unlinkCommand = editor.commands.get( 'unlink' );
 		const editing = editor.editing;
-		const showViewDocument = editing.view;
+		const showViewDocument = editing.view.document;
 		const showIsCollapsed = showViewDocument.selection.isCollapsed;
 		const showSelectedLink = this._getSelectedLinkElement();
 
-		this.listenTo( showViewDocument, 'render', () => {
+		this.listenTo( showViewDocument, 'change', () => {
 			const renderSelectedLink = this._getSelectedLinkElement();
 			const renderIsCollapsed = showViewDocument.selection.isCollapsed;
-			const hasSellectionExpanded = showIsCollapsed && !renderIsCollapsed;
+			const hasSelectionExpanded = showIsCollapsed && !renderIsCollapsed;
 
 			// Hide the panel if:
 			//   * the selection went out of the original link element
 			//     (e.g. paragraph containing the link was removed),
 			//   * the selection has expanded
-			// upon the #render event.
-			if ( hasSellectionExpanded || showSelectedLink !== renderSelectedLink ) {
+			// upon the #change event.
+			if ( hasSelectionExpanded || showSelectedLink !== renderSelectedLink ) {
 				this._hidePanel( true );
 			}
 			// Update the position of the panel when:
@@ -242,7 +242,7 @@ export default class Link extends Plugin {
 			//  * there was no link element in the first place, i.e. creating a new link
 			else {
 				// If still in a link element, simply update the position of the balloon.
-				// If there was no link, upon #render, the balloon must be moved
+				// If there was no link, upon #change, the balloon must be moved
 				// to the new position in the editing view (a new native DOM range).
 				this._balloon.updatePosition( this._getBalloonPositionData() );
 			}
@@ -285,7 +285,7 @@ export default class Link extends Plugin {
 	 * @param {Boolean} [focusEditable=false] When `true`, editable focus will be restored on panel hide.
 	 */
 	_hidePanel( focusEditable ) {
-		this.stopListening( this.editor.editing.view, 'render' );
+		this.stopListening( this.editor.editing.view.document, 'change' );
 
 		if ( !this._balloon.hasView( this.formView ) ) {
 			return;
@@ -295,7 +295,7 @@ export default class Link extends Plugin {
 			this.editor.editing.view.focus();
 		}
 
-		this.stopListening( this.editor.editing.view, 'render' );
+		this.stopListening( this.editor.editing.view.document, 'change' );
 		this._balloon.remove( this.formView );
 	}
 
@@ -310,14 +310,15 @@ export default class Link extends Plugin {
 	 * @returns {module:utils/dom/position~Options}
 	 */
 	_getBalloonPositionData() {
-		const viewDocument = this.editor.editing.view;
+		const view = this.editor.editing.view;
+		const viewDocument = view.document;
 		const targetLink = this._getSelectedLinkElement();
 
 		const target = targetLink ?
 			// When selection is inside link element, then attach panel to this element.
-			viewDocument.domConverter.mapViewToDom( targetLink ) :
+			view.domConverter.mapViewToDom( targetLink ) :
 			// Otherwise attach panel to the selection.
-			viewDocument.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() );
+			view.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() );
 
 		return { target };
 	}
@@ -334,7 +335,7 @@ export default class Link extends Plugin {
 	 * @returns {module:engine/view/attributeelement~AttributeElement|null}
 	 */
 	_getSelectedLinkElement() {
-		const selection = this.editor.editing.view.selection;
+		const selection = this.editor.editing.view.document.selection;
 
 		if ( selection.isCollapsed ) {
 			return findLinkElementAncestor( selection.getFirstPosition() );

+ 48 - 33
packages/ckeditor5-link/tests/link.js

@@ -75,11 +75,12 @@ describe( 'Link', () => {
 
 		beforeEach( () => {
 			balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
-			editor.editing.view.isFocused = true;
+			editor.editing.view.document.isFocused = true;
 		} );
 
 		it( 'should add #formView to the #_balloon and attach the #_balloon to the selection when text fragment is selected', () => {
 			setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
+
 			const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
 
 			linkFeature._showPanel();
@@ -229,27 +230,35 @@ describe( 'Link', () => {
 			expect( formView.urlInputView.inputView.element.value ).to.equal( '' );
 		} );
 
-		describe( 'when the document is rendering', () => {
-			it( 'should not duplicate #render listeners', () => {
-				const viewDocument = editor.editing.view;
+		describe( 'when the document is changing', () => {
+			let view, viewDocument;
+
+			beforeEach( () => {
+				view = editor.editing.view;
+				viewDocument = view.document;
+			} );
+
+			afterEach( () => {
+				view.destroy();
+			} );
 
+			it( 'should not duplicate #change listeners', () => {
 				setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
 
 				const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
 
 				linkFeature._showPanel();
-				viewDocument.render();
-				linkFeature._hidePanel();
+				view.change( () => {} );
 
+				linkFeature._hidePanel();
 				linkFeature._showPanel();
-				viewDocument.render();
+				view.change( () => {} );
+
 				sinon.assert.calledTwice( spy );
 			} );
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
-			it( 'updates the position of the panel – editing a link, then the selection remains in the link upon #render', () => {
-				const viewDocument = editor.editing.view;
-
+			it( 'updates the position of the panel – editing a link, then the selection remains in the link upon #change', () => {
 				setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
 
 				linkFeature._showPanel();
@@ -259,30 +268,32 @@ describe( 'Link', () => {
 				const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
 
 				// Move selection to foo[].
-				viewDocument.selection.setTo( Range.createFromParentsAndOffsets( text, 3, text, 3 ), true );
-				viewDocument.render();
+				view.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( text, 3, text, 3 ), true );
+				} );
 
 				sinon.assert.calledOnce( spy );
 				sinon.assert.calledWithExactly( spy, {
-					target: viewDocument.domConverter.mapViewToDom( root.getChild( 0 ).getChild( 0 ) )
+					target: view.domConverter.mapViewToDom( root.getChild( 0 ).getChild( 0 ) )
 				} );
 			} );
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
-			it( 'updates the position of the panel – creating a new link, then the selection moved upon #render', () => {
-				const viewDocument = editor.editing.view;
+			it( 'updates the position of the panel – creating a new link, then the selection moved upon #change', () => {
+				const viewDocument = editor.editing.view.document;
 
 				setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
 
 				linkFeature._showPanel();
 				const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
 
-				// Fires #render.
+				// Fires #change.
 				const root = viewDocument.getRoot();
 				const text = root.getChild( 0 ).getChild( 0 );
 
-				viewDocument.selection.setTo( Range.createFromParentsAndOffsets( text, 3, text, 3 ), true );
-				viewDocument.render();
+				view.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( text, 3, text, 3 ), true );
+				} );
 
 				sinon.assert.calledOnce( spy );
 				sinon.assert.calledWithExactly( spy, {
@@ -291,8 +302,8 @@ describe( 'Link', () => {
 			} );
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
-			it( 'hides of the panel – editing a link, then the selection moved out of the link upon #render', () => {
-				const viewDocument = editor.editing.view;
+			it( 'hides of the panel – editing a link, then the selection moved out of the link upon #change', () => {
+				const viewDocument = editor.editing.view.document;
 
 				setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
 
@@ -305,16 +316,17 @@ describe( 'Link', () => {
 				const text = root.getChild( 0 ).getChild( 1 );
 
 				// Move selection to b[]ar.
-				viewDocument.selection.setTo( Range.createFromParentsAndOffsets( text, 1, text, 1 ), true );
-				viewDocument.render();
+				view.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( text, 1, text, 1 ), true );
+				} );
 
 				sinon.assert.calledOnce( spyHide );
 				sinon.assert.notCalled( spyUpdate );
 			} );
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
-			it( 'hides of the panel – editing a link, then the selection moved to another link upon #render', () => {
-				const viewDocument = editor.editing.view;
+			it( 'hides of the panel – editing a link, then the selection moved to another link upon #change', () => {
+				const viewDocument = editor.editing.view.document;
 
 				setModelData(
 					editor.model,
@@ -330,16 +342,17 @@ describe( 'Link', () => {
 				const text = root.getChild( 0 ).getChild( 2 ).getChild( 0 );
 
 				// Move selection to b[]az.
-				viewDocument.selection.setTo( Range.createFromParentsAndOffsets( text, 1, text, 1 ), true );
-				viewDocument.render();
+				view.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( text, 1, text, 1 ), true );
+				} );
 
 				sinon.assert.calledOnce( spyHide );
 				sinon.assert.notCalled( spyUpdate );
 			} );
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
-			it( 'hides the panel – editing a link, then the selection expands upon #render', () => {
-				const viewDocument = editor.editing.view;
+			it( 'hides the panel – editing a link, then the selection expands upon #change', () => {
+				const viewDocument = editor.editing.view.document;
 
 				setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
 
@@ -352,8 +365,9 @@ describe( 'Link', () => {
 				const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
 
 				// Move selection to f[o]o.
-				viewDocument.selection.setTo( Range.createFromParentsAndOffsets( text, 1, text, 2 ), true );
-				viewDocument.render();
+				view.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( text, 1, text, 2 ), true );
+				} );
 
 				sinon.assert.calledOnce( spyHide );
 				sinon.assert.notCalled( spyUpdate );
@@ -400,12 +414,12 @@ describe( 'Link', () => {
 			} ).to.not.throw();
 		} );
 
-		it( 'should clear `render` listener from ViewDocument', () => {
+		it( 'should clear `change` listener from ViewDocument', () => {
 			const spy = sinon.spy();
 
-			linkFeature.listenTo( editor.editing.view, 'render', spy );
+			linkFeature.listenTo( editor.editing.view.document, 'change', spy );
 			linkFeature._hidePanel();
-			editor.editing.view.render();
+			editor.editing.view.change( () => {} );
 
 			sinon.assert.notCalled( spy );
 		} );
@@ -603,6 +617,7 @@ describe( 'Link', () => {
 				setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
 
 				observer.fire( 'click', { target: document.body } );
+				sinon.assert.calledOnce( spy );
 				sinon.assert.calledWithExactly( spy );
 			} );
 

+ 1 - 1
packages/ckeditor5-link/tests/linkengine.js

@@ -102,7 +102,7 @@ describe( 'LinkEngine', () => {
 		it( 'should convert to link element instance', () => {
 			setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
 
-			const element = editor.editing.view.getRoot().getChild( 0 ).getChild( 0 );
+			const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
 			expect( isLinkElement( element ) ).to.be.true;
 		} );