Parcourir la source

Renamed Scope to Context.

Oskar Wróbel il y a 6 ans
Parent
commit
7d35e9da97

+ 11 - 11
packages/ckeditor5-core/src/scope.js → packages/ckeditor5-core/src/context.js

@@ -4,7 +4,7 @@
  */
 
 /**
- * @module core/scope
+ * @module core/context
  */
 
 import Config from '@ckeditor/ckeditor5-utils/src/config';
@@ -12,10 +12,10 @@ import PluginCollection from './plugincollection';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
-export default class Scope {
+export default class Context {
 	constructor( config ) {
 		/**
-		 * Holds all configurations specific to this scope instance.
+		 * Holds all configurations specific to this context instance.
 		 *
 		 * @readonly
 		 * @type {module:utils/config~Config}
@@ -23,7 +23,7 @@ export default class Scope {
 		this.config = new Config( config );
 
 		/**
-		 * The plugins loaded and in use by this scope instance.
+		 * The plugins loaded and in use by this context instance.
 		 *
 		 * @readonly
 		 * @type {module:core/plugincollection~PluginCollection}
@@ -42,7 +42,7 @@ export default class Scope {
 		} );
 
 		/**
-		 * List of editors used with this scope.
+		 * List of editors used with this context.
 		 *
 		 * @private
 		 * @type {Set<module:core/editor/editor~Editor>}
@@ -69,7 +69,7 @@ export default class Scope {
 
 		for ( const plugin of plugins ) {
 			if ( typeof plugin != 'function' ) {
-				throw new CKEditorError( 'scope-initplugins: Only constructor is allowed as a Scope plugin' );
+				throw new CKEditorError( 'context-initplugins: Only constructor is allowed as a Context plugin' );
 			}
 		}
 
@@ -77,15 +77,15 @@ export default class Scope {
 	}
 
 	/**
-	 * Destroys the scope instance, releasing all resources used by it.
+	 * Destroys the context instance, releasing all resources used by it.
 	 *
-	 * @returns {Promise} A promise that resolves once the scope instance is fully destroyed.
+	 * @returns {Promise} A promise that resolves once the context instance is fully destroyed.
 	 */
 	destroy() {
 		const promises = [];
 
 		for ( const editor of Array.from( this._editors ) ) {
-			editor.scope = null;
+			editor.context = null;
 			promises.push( editor.destroy() );
 		}
 
@@ -95,9 +95,9 @@ export default class Scope {
 
 	static create( config ) {
 		return new Promise( resolve => {
-			const scope = new this( config );
+			const context = new this( config );
 
-			resolve( scope.initPlugins().then( () => scope ) );
+			resolve( context.initPlugins().then( () => context ) );
 		} );
 	}
 }

+ 12 - 12
packages/ckeditor5-core/src/editor/editor.js

@@ -7,7 +7,7 @@
  * @module core/editor/editor
  */
 
-import Scope from '@ckeditor/ckeditor5-core/src/scope';
+import Context from '@ckeditor/ckeditor5-core/src/context';
 import Config from '@ckeditor/ckeditor5-utils/src/config';
 import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
 import PluginCollection from '../plugincollection';
@@ -53,10 +53,10 @@ export default class Editor {
 	constructor( config = {} ) {
 		/**
 		 * @readonly
-		 * @type {module:core/scope~Scope}
+		 * @type {module:core/context~Context}
 		 */
-		this.scope = config.scope || new Scope( config );
-		this.scope.addEditor( this );
+		this.context = config.context || new Context( config );
+		this.context.addEditor( this );
 
 		const availablePlugins = this.constructor.builtinPlugins;
 
@@ -80,7 +80,7 @@ export default class Editor {
 		 * @readonly
 		 * @member {module:core/plugincollection~PluginCollection}
 		 */
-		this.plugins = new PluginCollection( this, availablePlugins, this.scope.plugins );
+		this.plugins = new PluginCollection( this, availablePlugins, this.context.plugins );
 
 		/**
 		 * Commands registered to the editor.
@@ -206,7 +206,7 @@ export default class Editor {
 	 * @type {module:utils/locale~Locale}
 	 */
 	get locale() {
-		return this.scope.locale;
+		return this.context.locale;
 	}
 
 	/**
@@ -244,14 +244,14 @@ export default class Editor {
 	 * @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
 	 */
 	destroy() {
-		// If editor scope is created by this editor
-		// it is enough to destroy the scope, editor will be destroyed along with it.
-		if ( this.scope ) {
-			if ( this.config.scope ) {
-				return this.scope.destroy();
+		// If editor context is created by this editor
+		// it is enough to destroy the context, editor will be destroyed along with it.
+		if ( this.context ) {
+			if ( this.config.context ) {
+				return this.context.destroy();
 			}
 
-			this.scope.removeEditor( this );
+			this.context.removeEditor( this );
 		}
 
 		let readyPromise = Promise.resolve();

+ 115 - 0
packages/ckeditor5-core/tests/context.js

@@ -0,0 +1,115 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import Context from '../src/context';
+import Plugin from '../src/plugin';
+import VirtualTestEditor from './_utils/virtualtesteditor';
+
+describe( 'Context', () => {
+	it( 'should share the same instance of plugin within editors using the same context', async () => {
+		class ContextPluginA extends Plugin {}
+		class ContextPluginB extends Plugin {}
+		class EditorPluginA extends Plugin {}
+
+		const context = await Context.create( { plugins: [ ContextPluginA, ContextPluginB ] } );
+		const editorA = await VirtualTestEditor.create( { context, plugins: [ ContextPluginA, EditorPluginA ] } );
+		const editorB = await VirtualTestEditor.create( { context, plugins: [ ContextPluginB, EditorPluginA ] } );
+
+		expect( editorA.plugins.get( ContextPluginA ) ).to.equal( context.plugins.get( ContextPluginA ) );
+		expect( editorA.plugins.has( ContextPluginB ) ).to.equal( false );
+		expect( editorB.plugins.get( ContextPluginB ) ).to.equal( context.plugins.get( ContextPluginB ) );
+		expect( editorB.plugins.has( ContextPluginA ) ).to.equal( false );
+
+		expect( context.plugins.has( EditorPluginA ) ).to.equal( false );
+		expect( editorA.plugins.get( EditorPluginA ) ).to.not.equal( editorB.plugins.get( EditorPluginA ) );
+
+		await context.destroy();
+	} );
+
+	it( 'should share the same instance of plugin (dependencies) within editors using the same context', async () => {
+		class ContextPluginA extends Plugin {}
+		class ContextPluginB extends Plugin {}
+		class EditorPluginA extends Plugin {
+			static get requires() {
+				return [ ContextPluginA ];
+			}
+		}
+		class EditorPluginB extends Plugin {
+			static get requires() {
+				return [ ContextPluginB ];
+			}
+		}
+
+		const context = await Context.create( { plugins: [ ContextPluginA, ContextPluginB ] } );
+		const editorA = await VirtualTestEditor.create( { context, plugins: [ EditorPluginA ] } );
+		const editorB = await VirtualTestEditor.create( { context, plugins: [ EditorPluginB ] } );
+
+		expect( context.plugins.get( ContextPluginA ) ).to.equal( editorA.plugins.get( ContextPluginA ) );
+		expect( context.plugins.get( ContextPluginB ) ).to.equal( editorB.plugins.get( ContextPluginB ) );
+
+		await context.destroy();
+	} );
+
+	it( 'should not initialize twice plugin added to the context and the editor', async () => {
+		const initSpy = sinon.spy();
+		const afterInitSpy = sinon.spy();
+
+		class ContextPluginA extends Plugin {
+			init() {
+				initSpy();
+			}
+
+			afterInit() {
+				afterInitSpy();
+			}
+		}
+
+		const context = await Context.create( { plugins: [ ContextPluginA ] } );
+		const editor = await VirtualTestEditor.create( { context, plugins: [ ContextPluginA ] } );
+
+		expect( context.plugins.get( ContextPluginA ) ).to.equal( editor.plugins.get( ContextPluginA ) );
+		sinon.assert.calledOnce( initSpy );
+		sinon.assert.calledOnce( afterInitSpy );
+
+		await context.destroy();
+	} );
+
+	it( 'should not destroy context plugin when only the editor is destroyed', async () => {
+		const contextPluginDestroySpy = sinon.spy();
+		const editorPluginDestroySpy = sinon.spy();
+
+		class ContextPlugin extends Plugin {
+			destroy() {
+				contextPluginDestroySpy();
+			}
+		}
+
+		class EditorPlugin extends Plugin {
+			destroy() {
+				editorPluginDestroySpy();
+			}
+		}
+
+		const context = await Context.create( { plugins: [ ContextPlugin ] } );
+		const editor = await VirtualTestEditor.create( { context, plugins: [ ContextPlugin, EditorPlugin ] } );
+
+		await editor.destroy();
+
+		sinon.assert.calledOnce( editorPluginDestroySpy );
+		sinon.assert.notCalled( contextPluginDestroySpy );
+	} );
+
+	it( 'should not destroy context along with editor when context is injected to the editor', () => {
+
+	} );
+
+	it( 'should destroy all editors along with the context when context is injected to them', () => {
+
+	} );
+
+	it( 'should destroy context along with editor when context is created by the editor', () => {
+
+	} );
+} );

+ 0 - 115
packages/ckeditor5-core/tests/scope.js

@@ -1,115 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-import Scope from '../src/scope';
-import Plugin from '../src/plugin';
-import VirtualTestEditor from './_utils/virtualtesteditor';
-
-describe( 'Scope', () => {
-	it( 'should share the same instance of plugin within editors using scope', async () => {
-		class ScopePluginA extends Plugin {}
-		class ScopePluginB extends Plugin {}
-		class EditorPluginA extends Plugin {}
-
-		const scope = await Scope.create( { plugins: [ ScopePluginA, ScopePluginB ] } );
-		const editorA = await VirtualTestEditor.create( { scope, plugins: [ ScopePluginA, EditorPluginA ] } );
-		const editorB = await VirtualTestEditor.create( { scope, plugins: [ ScopePluginB, EditorPluginA ] } );
-
-		expect( editorA.plugins.get( ScopePluginA ) ).to.equal( scope.plugins.get( ScopePluginA ) );
-		expect( editorA.plugins.has( ScopePluginB ) ).to.equal( false );
-		expect( editorB.plugins.get( ScopePluginB ) ).to.equal( scope.plugins.get( ScopePluginB ) );
-		expect( editorB.plugins.has( ScopePluginA ) ).to.equal( false );
-
-		expect( scope.plugins.has( EditorPluginA ) ).to.equal( false );
-		expect( editorA.plugins.get( EditorPluginA ) ).to.not.equal( editorB.plugins.get( EditorPluginA ) );
-
-		await scope.destroy();
-	} );
-
-	it( 'should share the same instance of plugin within editors using scope (deeps)', async () => {
-		class ScopePluginA extends Plugin {}
-		class ScopePluginB extends Plugin {}
-		class EditorPluginA extends Plugin {
-			static get requires() {
-				return [ ScopePluginA ];
-			}
-		}
-		class EditorPluginB extends Plugin {
-			static get requires() {
-				return [ ScopePluginB ];
-			}
-		}
-
-		const scope = await Scope.create( { plugins: [ ScopePluginA, ScopePluginB ] } );
-		const editorA = await VirtualTestEditor.create( { scope, plugins: [ EditorPluginA ] } );
-		const editorB = await VirtualTestEditor.create( { scope, plugins: [ EditorPluginB ] } );
-
-		expect( scope.plugins.get( ScopePluginA ) ).to.equal( editorA.plugins.get( ScopePluginA ) );
-		expect( scope.plugins.get( ScopePluginB ) ).to.equal( editorB.plugins.get( ScopePluginB ) );
-
-		await scope.destroy();
-	} );
-
-	it( 'should not initialize twice plugin added to the scope and the editor', async () => {
-		const initSpy = sinon.spy();
-		const afterInitSpy = sinon.spy();
-
-		class ScopePluginA extends Plugin {
-			init() {
-				initSpy();
-			}
-
-			afterInit() {
-				afterInitSpy();
-			}
-		}
-
-		const scope = await Scope.create( { plugins: [ ScopePluginA ] } );
-		const editor = await VirtualTestEditor.create( { scope, plugins: [ ScopePluginA ] } );
-
-		expect( scope.plugins.get( ScopePluginA ) ).to.equal( editor.plugins.get( ScopePluginA ) );
-		sinon.assert.calledOnce( initSpy );
-		sinon.assert.calledOnce( afterInitSpy );
-
-		await scope.destroy();
-	} );
-
-	it( 'should not destroy scope plugin when only the editor is destroyed', async () => {
-		const scopePluginDestroySpy = sinon.spy();
-		const editorPluginDestroySpy = sinon.spy();
-
-		class ScopePlugin extends Plugin {
-			destroy() {
-				scopePluginDestroySpy();
-			}
-		}
-
-		class EditorPlugin extends Plugin {
-			destroy() {
-				editorPluginDestroySpy();
-			}
-		}
-
-		const scope = await Scope.create( { plugins: [ ScopePlugin ] } );
-		const editor = await VirtualTestEditor.create( { scope, plugins: [ ScopePlugin, EditorPlugin ] } );
-
-		await editor.destroy();
-
-		sinon.assert.calledOnce( editorPluginDestroySpy );
-		sinon.assert.notCalled( scopePluginDestroySpy );
-	} );
-
-	it( 'should not destroy scope along with editor when scope is injected to the editor', () => {
-
-	} );
-
-	it( 'should destroy all editors along with the scope when scope is injected to them', () => {
-
-	} );
-
-	it( 'should destroy scope along with editor when scope is created by the editor', () => {
-
-	} );
-} );