浏览代码

Multi command proof of concept.

Maciej Gołaszewski 6 年之前
父节点
当前提交
14381521d7
共有 2 个文件被更改,包括 63 次插入1 次删除
  1. 46 0
      packages/ckeditor5-core/src/multicommand.js
  2. 17 1
      packages/ckeditor5-core/tests/manual/article.js

+ 46 - 0
packages/ckeditor5-core/src/multicommand.js

@@ -0,0 +1,46 @@
+/**
+ * @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 Command from './command';
+import Collection from '@ckeditor/ckeditor5-utils/src/collection';
+
+/**
+ * @module core/multicommand
+ */
+
+export default class MultiCommand extends Command {
+	constructor( editor ) {
+		super( editor );
+
+		this._childCommands = new Collection();
+	}
+
+	refresh() {
+		// The command listens to child commands rather then acts on refresh.
+		// TODO: check with track changes.
+	}
+
+	execute() {
+		const { command } = this._getEnabled();
+
+		command.execute();
+	}
+
+	registerChildCommand( command ) {
+		this._childCommands.add( { command } );
+
+		command.on( 'change:isEnabled', () => this._checkEnabled() );
+
+		this._checkEnabled();
+	}
+
+	_checkEnabled() {
+		this.isEnabled = !!this._getEnabled();
+	}
+
+	_getEnabled() {
+		return this._childCommands.find( ( { command } ) => command.isEnabled );
+	}
+}

+ 17 - 1
packages/ckeditor5-core/tests/manual/article.js

@@ -8,10 +8,26 @@
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 
 
 import ArticlePluginSet from '../_utils/articlepluginset';
 import ArticlePluginSet from '../_utils/articlepluginset';
+import MultiCommand from '../../src/multicommand';
+import Plugin from '../../src/plugin';
+
+class IndentOutdent extends Plugin {
+	afterInit() {
+		const editor = this.editor;
+
+		const indentCommand = new MultiCommand( editor );
+		indentCommand.registerChildCommand( editor.commands.get( 'indentList' ) );
+		editor.commands.add( 'indent', indentCommand );
+
+		const outdentCommand = new MultiCommand( editor );
+		outdentCommand.registerChildCommand( editor.commands.get( 'outdentList' ) );
+		editor.commands.add( 'outdent', outdentCommand );
+	}
+}
 
 
 ClassicEditor
 ClassicEditor
 	.create( document.querySelector( '#editor' ), {
 	.create( document.querySelector( '#editor' ), {
-		plugins: [ ArticlePluginSet ],
+		plugins: [ ArticlePluginSet, IndentOutdent ],
 		toolbar: [
 		toolbar: [
 			'heading',
 			'heading',
 			'|',
 			'|',