Bladeren bron

Merge branch t/ckeditor5-engine/1210

Internal: Align code to the new view API, introduced in engine t/1210.
Piotr Jasiun 7 jaren geleden
bovenliggende
commit
5b4ceba60a

+ 9 - 8
packages/ckeditor5-link/src/link.js

@@ -204,7 +204,7 @@ export default class Link extends Plugin {
 	 * @private
 	 */
 	_enableUserBalloonInteractions() {
-		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.
@@ -354,8 +354,8 @@ export default class Link extends Plugin {
 	}
 
 	/**
-	 * Makes the UI react to the {@link module:engine/view/document~Document#event:render} in the view
-	 * document to reposition itself as the document changes.
+	 * Makes the UI react to the {@link module:engine/view/view~View#event:render} in the view to
+	 * reposition itself as the document changes.
 	 *
 	 * See: {@link #_hideUI} to learn when the UI stops reacting to the `render` event.
 	 *
@@ -402,7 +402,7 @@ export default class Link extends Plugin {
 		} );
 
 		function getSelectionParent() {
-			return editingView.selection.focus.getAncestors()
+			return editingView.document.selection.focus.getAncestors()
 				.reverse()
 				.find( node => node.is( 'element' ) );
 		}
@@ -478,14 +478,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 };
 	}
@@ -502,7 +503,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() );

+ 3 - 1
packages/ckeditor5-link/src/linkengine.js

@@ -31,8 +31,10 @@ export default class LinkEngine extends Plugin {
 		// Allow link attribute on all inline nodes.
 		editor.model.schema.extend( '$text', { allowAttributes: 'linkHref' } );
 
+		const linkElementCreator = ( href, data, consumable, api ) => createLinkElement( href, api.writer );
+
 		editor.conversion.for( 'downcast' )
-			.add( downcastAttributeToElement( 'linkHref', { view: linkHref => createLinkElement( linkHref ) } ) );
+			.add( downcastAttributeToElement( 'linkHref', { view: linkElementCreator } ) );
 
 		editor.conversion.for( 'upcast' )
 			.add( upcastElementToAttribute( {

+ 4 - 8
packages/ckeditor5-link/src/utils.js

@@ -7,8 +7,6 @@
  * @module link/utils
  */
 
-import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
-
 const linkElementSymbol = Symbol( 'linkElement' );
 
 /**
@@ -27,12 +25,10 @@ export function isLinkElement( node ) {
  * @param {String} href
  * @return {module:engine/view/attributeelement~AttributeElement}
  */
-export function createLinkElement( href ) {
-	const linkElement = new AttributeElement( 'a', { href } );
-	linkElement.setCustomProperty( linkElementSymbol, true );
-
-	// https://github.com/ckeditor/ckeditor5-link/issues/121
-	linkElement.priority = 5;
+export function createLinkElement( href, writer ) {
+	// Priority 5 - https://github.com/ckeditor/ckeditor5-link/issues/121.
+	const linkElement = writer.createAttributeElement( 'a', { href }, 5 );
+	writer.setCustomProperty( linkElementSymbol, true, linkElement );
 
 	return linkElement;
 }

+ 28 - 30
packages/ckeditor5-link/tests/link.js

@@ -111,7 +111,7 @@ describe( 'Link', () => {
 
 		beforeEach( () => {
 			balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
-			editor.editing.view.isFocused = true;
+			editor.editing.view.document.isFocused = true;
 		} );
 
 		it( 'should not work if the link command is disabled', () => {
@@ -227,27 +227,30 @@ describe( 'Link', () => {
 			expect( formView.urlInputView.inputView.element.value ).to.equal( '' );
 		} );
 
-		describe( 'response to view#render', () => {
-			it( 'should not duplicate #render listeners', () => {
-				const viewDocument = editor.editing.view;
+		describe( 'response to view#change', () => {
+			let view, viewDocument;
 
+			beforeEach( () => {
+				view = editor.editing.view;
+				viewDocument = view.document;
+			} );
+
+			it( 'should not duplicate #change listeners', () => {
 				setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
 
 				const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
 
 				linkFeature._showUI();
-				viewDocument.render();
+				view.change( () => {} );
 				linkFeature._hideUI();
 
 				linkFeature._showUI();
-				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', () => {
-				const viewDocument = editor.editing.view;
-
 				setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
 
 				linkFeature._showUI();
@@ -257,30 +260,29 @@ 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', () => {
-				const viewDocument = editor.editing.view;
-
 				setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
 
 				linkFeature._showUI();
 				const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
 
-				// Fires #render.
 				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, {
@@ -290,8 +292,6 @@ 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', () => {
-				const viewDocument = editor.editing.view;
-
 				setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
 
 				linkFeature._showUI();
@@ -303,8 +303,9 @@ 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 );
@@ -312,8 +313,6 @@ describe( 'Link', () => {
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
 			it( 'hides the panel – editing a link, then the selection expands', () => {
-				const viewDocument = editor.editing.view;
-
 				setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
 
 				linkFeature._showUI();
@@ -325,8 +324,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 );
@@ -334,8 +334,6 @@ describe( 'Link', () => {
 
 			// https://github.com/ckeditor/ckeditor5-link/issues/113
 			it( 'hides the panel – creating a new link, then the selection moved to another parent', () => {
-				const viewDocument = editor.editing.view;
-
 				setModelData( editor.model, '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
 
 				linkFeature._showUI();
@@ -343,13 +341,13 @@ describe( 'Link', () => {
 				const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
 				const spyHide = testUtils.sinon.spy( linkFeature, '_hideUI' );
 
-				// Fires #render.
 				const root = viewDocument.getRoot();
 				const text = root.getChild( 1 ).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 );
@@ -394,7 +392,7 @@ describe( 'Link', () => {
 
 			linkFeature.listenTo( editor.editing.view, 'render', spy );
 			linkFeature._hideUI();
-			editor.editing.view.render();
+			editor.editing.view.change( () => {} );
 
 			sinon.assert.notCalled( 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;
 		} );
 

+ 4 - 2
packages/ckeditor5-link/tests/utils.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
+import ViewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
 import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
 import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
 import Text from '@ckeditor/ckeditor5-engine/src/view/text';
@@ -12,7 +14,7 @@ import { createLinkElement, isLinkElement } from '../src/utils';
 describe( 'utils', () => {
 	describe( 'isLinkElement', () => {
 		it( 'should return true for elements created by createLinkElement', () => {
-			const element = createLinkElement( 'http://ckeditor.com' );
+			const element = createLinkElement( 'http://ckeditor.com', new ViewWriter( new ViewDocument() ) );
 
 			expect( isLinkElement( element ) ).to.be.true;
 		} );
@@ -32,7 +34,7 @@ describe( 'utils', () => {
 
 	describe( 'createLinkElement', () => {
 		it( 'should create link AttributeElement', () => {
-			const element = createLinkElement( 'http://cksource.com' );
+			const element = createLinkElement( 'http://cksource.com', new ViewWriter( new ViewDocument() ) );
 
 			expect( isLinkElement( element ) ).to.be.true;
 			expect( element.priority ).to.equal( 5 );