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

Other: Bootstrap highlight feature classes.

Maciej Gołaszewski 8 лет назад
Родитель
Сommit
8c33a4c54c

+ 4 - 0
packages/ckeditor5-highlight/package.json

@@ -7,8 +7,12 @@
     "ckeditor5-feature"
   ],
   "dependencies": {
+    "@ckeditor/ckeditor5-core": "^1.0.0-alpha.1",
+    "@ckeditor/ckeditor5-engine": "^1.0.0-alpha.1",
+    "@ckeditor/ckeditor5-ui": "^1.0.0-alpha.1"
   },
   "devDependencies": {
+    "@ckeditor/ckeditor5-paragraph": "^1.0.0-alpha.1",
     "eslint": "^4.8.0",
     "eslint-config-ckeditor5": "^1.0.6",
     "husky": "^0.14.3",

+ 36 - 0
packages/ckeditor5-highlight/src/highlight.js

@@ -0,0 +1,36 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module highlight/highlight
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+
+import HighlightEditing from './highlightediting';
+import HighlightUI from './highlightui';
+
+/**
+ * The highlight plugin.
+ *
+ * It requires {@link module:highlight/highlightediting~HighlightEditing} and {@link module:highlight/highlightui~HighlightUI} plugins.
+ *
+ * @extends module:core/plugin~Plugin
+ */
+export default class Highlight extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ HighlightEditing, HighlightUI ];
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'Highlight';
+	}
+}

+ 91 - 0
packages/ckeditor5-highlight/src/highlightcommand.js

@@ -0,0 +1,91 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module highlight/highlightcommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+
+/**
+ * The highlight command.
+ *
+ * @extends module:core/command~Command
+ */
+export default class HighlightCommand extends Command {
+	/**
+	 * Creates an instance of the command.
+	 *
+	 * @param {module:core/editor/editor~Editor} editor The editor instance.
+	 * @param {'left'|'right'|'center'|'justify'} type Highlight type to be handled by this command.
+	 */
+	constructor( editor, type ) {
+		super( editor );
+
+		/**
+		 * The type of the list created by the command.
+		 *
+		 * @readonly
+		 * @member {'left'|'right'|'center'|'justify'}
+		 */
+		this.type = type;
+
+		/**
+		 * A flag indicating whether the command is active, which means that the selection starts in a block
+		 * that has defined highlight of the same type.
+		 *
+		 * @observable
+		 * @readonly
+		 * @member {Boolean} #value
+		 */
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		this.isEnabled = this._checkEnabled();
+		this.value = this._getValue();
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @protected
+	 * @param {Object} [options] Options for the executed command.
+	 * @param {module:engine/model/batch~Batch} [options.batch] A batch to collect all the change steps.
+	 * A new batch will be created if this option is not set.
+	 */
+	execute() {
+		const editor = this.editor;
+		const document = editor.document;
+
+		document.enqueueChanges( () => {
+			// TODO
+		} );
+	}
+
+	/**
+	 * Checks whether the command can be enabled in the current context.
+	 *
+	 * @private
+	 * @param {module:engine/model/element~Element} firstBlock A first block in selection to be checked.
+	 * @returns {Boolean} Whether the command should be enabled.
+	 */
+	_checkEnabled() {
+		return true;
+	}
+
+	/**
+	 * Checks the command's {@link #value}.
+	 *
+	 * @private
+	 * @param {module:engine/model/element~Element} firstBlock A first block in selection to be checked.
+	 * @returns {Boolean} The current value.
+	 */
+	_getValue() {
+		return true;
+	}
+}

+ 26 - 0
packages/ckeditor5-highlight/src/highlightediting.js

@@ -0,0 +1,26 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module highlight/highlightediting
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+
+import HighlightCommand from './highlightcommand';
+
+/**
+ * @extends module:core/plugin~Plugin
+ */
+export default class HighlightEditing extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+
+		editor.commands.add( 'highlight', new HighlightCommand( editor ) );
+	}
+}

+ 40 - 0
packages/ckeditor5-highlight/src/highlightui.js

@@ -0,0 +1,40 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module highlight/highlightui
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+
+import HighlightEditing from './highlightediting';
+
+/**
+ * The default Highlight UI plugin.
+ *
+ * @extends module:core/plugin~Plugin
+ */
+export default class HighlightUI extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ HighlightEditing ];
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'HighlightUI';
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		// TODO
+	}
+}

+ 39 - 0
packages/ckeditor5-highlight/tests/highlight.js

@@ -0,0 +1,39 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global document */
+
+import Highlight from './../src/highlight';
+import HighlightEditing from './../src/highlightediting';
+import HighlightUI from './../src/highlightui';
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+
+describe( 'Highlight', () => {
+	let editor, element;
+
+	beforeEach( () => {
+		element = document.createElement( 'div' );
+		document.body.appendChild( element );
+
+		return ClassicTestEditor
+			.create( element, {
+				plugins: [ Highlight ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+			} );
+	} );
+
+	afterEach( () => {
+		element.remove();
+
+		return editor.destroy();
+	} );
+
+	it( 'requires HighlightEditing and HighlightUI', () => {
+		expect( Highlight.requires ).to.deep.equal( [ HighlightEditing, HighlightUI ] );
+	} );
+} );

+ 67 - 0
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -0,0 +1,67 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import HighlightCommand from './../src/highlightcommand';
+
+import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+
+describe( 'HighlightCommand', () => {
+	let editor, doc, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				doc = newEditor.document;
+				command = new HighlightCommand( newEditor );
+				editor = newEditor;
+
+				editor.commands.add( 'highlight', command );
+
+				doc.schema.registerItem( 'paragraph', '$block' );
+
+				doc.schema.allow( { name: '$block', inside: '$root', attributes: 'highlight' } );
+			} );
+	} );
+
+	afterEach( () => {
+		editor.destroy();
+	} );
+
+	it( 'is a command', () => {
+		expect( HighlightCommand.prototype ).to.be.instanceOf( Command );
+		expect( command ).to.be.instanceOf( Command );
+	} );
+
+	describe( 'value', () => {
+		it( 'is true when selection is in block with commend type highlight', () => {
+			setModelData( doc, '<paragraph highlight="center"><$text highlight="true">fo[]o</$text></paragraph>' );
+
+			expect( command ).to.have.property( 'value', true );
+		} );
+	} );
+
+	describe( 'isEnabled', () => {
+		it( 'is true when selection is in a block which can have added highlight', () => {
+			setModelData( doc, '<paragraph highlight="center"><$text highlight="true">fo[]o</$text></paragraph>' );
+
+			expect( command ).to.have.property( 'isEnabled', true );
+		} );
+	} );
+
+	describe.skip( 'execute()', () => {
+		describe( 'applying highlight', () => {
+			it( 'adds highlight to selected text element', () => {
+				setModelData( doc, '<paragraph>f[o]o</paragraph>' );
+
+				editor.execute( 'highlight' );
+
+				expect( getModelData( doc ) ).to.equal( '<paragraph>f<$text highlight="true">[o]</$text>o</paragraph>' );
+			} );
+		} );
+	} );
+} );

+ 91 - 0
packages/ckeditor5-highlight/tests/highlightediting.js

@@ -0,0 +1,91 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import HighlightEditing from './../src/highlightediting';
+import HighlightCommand from './../src/highlightcommand';
+
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
+describe( 'HighlightEditing', () => {
+	let editor, doc;
+
+	beforeEach( () => {
+		return VirtualTestEditor
+			.create( {
+				plugins: [ HighlightEditing, Paragraph ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				doc = editor.document;
+			} );
+	} );
+
+	afterEach( () => {
+		editor.destroy();
+	} );
+
+	it( 'adds highlight commands', () => {
+		expect( editor.commands.get( 'highlight' ) ).to.be.instanceOf( HighlightCommand );
+	} );
+
+	it.skip( 'allows for highlight in $blocks', () => {
+		expect( doc.schema.check( { name: '$text', inside: '$root', attributes: 'highlight' } ) ).to.be.true;
+	} );
+
+	describe.skip( 'integration', () => {
+		beforeEach( () => {
+			return VirtualTestEditor
+				.create( {
+					plugins: [ HighlightEditing, Paragraph ]
+				} )
+				.then( newEditor => {
+					editor = newEditor;
+
+					doc = editor.document;
+				} );
+		} );
+
+		it( 'is allowed inside paragraph', () => {
+			expect( doc.schema.check( { name: 'paragraph', attributes: 'highlight' } ) ).to.be.true;
+		} );
+	} );
+
+	describe.skip( 'highlight', () => {
+		it( 'adds converters to the data pipeline', () => {
+			const data = '<p>f<mark>o</mark>o</p>';
+
+			editor.setData( data );
+
+			expect( getModelData( doc ) ).to.equal( '<paragraph>f<$text highlight="">o</$text>o</paragraph>' );
+			expect( editor.getData() ).to.equal( '<p>x</p>' );
+		} );
+
+		it( 'adds a converter to the view pipeline for removing attribute', () => {
+			setModelData( doc, '<paragraph>f<$text highlight="">o</$text>o</paragraph>' );
+
+			expect( editor.getData() ).to.equal( '<p>f<mark>o</mark>o</p>' );
+
+			const command = editor.commands.get( 'highlight' );
+
+			command.execute();
+
+			expect( editor.getData() ).to.equal( '<p>x</p>' );
+		} );
+	} );
+
+	describe.skip( 'config', () => {
+		describe( 'styles', () => {
+			describe( 'default value', () => {
+				it( 'should be set', () => {
+					expect( editor.config.get( 'highlight.styles' ) ).to.deep.equal( {} );
+				} );
+			} );
+		} );
+	} );
+} );