Sfoglia il codice sorgente

Other: Bootstrap FontFamilyEditing and FontFamilyCommand.

Maciej Gołaszewski 8 anni fa
parent
commit
e0b71c73b5

+ 72 - 0
packages/ckeditor5-font/src/fontfamily/fontfamilycommand.js

@@ -0,0 +1,72 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module font/fontfamilycommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+
+/**
+ * The font family command. It is used by the {@link module:font/fontfamily/fontfamilyediting~FontFamilyEditing}
+ * to apply font family.
+ *
+ * @extends module:core/command~Command
+ */
+export default class FontFamilyCommand extends Command {
+	constructor( editor, fontFamily ) {
+		super( editor );
+
+		/**
+		 * Name of font family config.
+		 */
+		this.fontFamily = fontFamily;
+
+		/**
+		 * A flag indicating whether the command is active, which means that the selection has fontFamily attribute set.
+		 *
+		 * @observable
+		 * @readonly
+		 * @member {Boolean} module:font/fontfamilycommand~FontFamilyCommand#value
+		 */
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const doc = this.editor.model.document;
+
+		this.value = doc.selection.getAttribute( 'fontFamily' ) === this.fontFamily;
+		this.isEnabled = this.editor.model.schema.checkAttributeInSelection( doc.selection, 'fontFamily' );
+	}
+
+	/**
+	 * 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 model = this.editor.model;
+		const document = model.document;
+		const selection = document.selection;
+
+		// Do not apply fontFamily on collapsed selection.
+		if ( selection.isCollapsed ) {
+			return;
+		}
+
+		model.change( writer => {
+			const ranges = model.schema.getValidRanges( selection.getRanges(), 'fontFamily' );
+
+			for ( const range of ranges ) {
+				writer.setAttribute( 'fontFamily', this.fontFamily, range );
+			}
+		} );
+	}
+}

+ 159 - 0
packages/ckeditor5-font/src/fontfamily/fontfamilyediting.js

@@ -0,0 +1,159 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module font/fontfamily/fontfamilyediting
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import {
+	modelAttributeToViewAttributeElement,
+	viewToModelAttribute
+} from '@ckeditor/ckeditor5-engine/src/conversion/configurationdefinedconverters';
+
+/**
+ * The Font Family Editing feature.
+ *
+ * @extends module:core/plugin~Plugin
+ */
+export default class FontFamilyEditing extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	constructor( editor ) {
+		super( editor );
+
+		// Define default configuration using named presets
+		editor.config.define( 'fontFamily', {
+			items: [
+				'default',
+				'Arial, Helvetica, sans-serif',
+				'Courier New, Courier, monospace'
+			]
+		} );
+
+		// Get configuration
+		const data = editor.data;
+		const editing = editor.editing;
+
+		for ( const item of this.configuredItems ) {
+			// Covert view to model.
+			viewToModelAttribute( 'fontFamily', item, [ data.viewToModel ] );
+
+			// Covert model to view.
+			modelAttributeToViewAttributeElement( 'fontFamily', item, [ data.modelToView, editing.modelToView ] );
+
+			// Add command.
+		}
+	}
+
+	get configuredItems() {
+		// Cache value
+		if ( this._cachedItems ) {
+			return this._cachedItems;
+		}
+
+		const items = [];
+		const editor = this.editor;
+		const config = editor.config;
+
+		const configuredItems = config.get( 'fontFamily.items' );
+
+		for ( const item of configuredItems ) {
+			const itemDefinition = getItemDefinition( item );
+
+			if ( itemDefinition ) {
+				items.push( itemDefinition );
+			}
+		}
+
+		return ( this._cachedItems = items );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+
+		// Allow highlight attribute on all elements
+		editor.model.schema.allow( { name: '$inline', attributes: 'fontFamily', inside: '$block' } );
+		// Temporary workaround. See https://github.com/ckeditor/ckeditor5/issues/477.
+		editor.model.schema.allow( { name: '$inline', attributes: 'fontFamily', inside: '$clipboardHolder' } );
+	}
+}
+
+// Returns item definition from preset
+function getItemDefinition( item ) {
+	// Probably it is full item definition so return it
+	if ( typeof item === 'object' ) {
+		return item;
+	}
+
+	return generateFontPreset( item );
+}
+
+// Creates a predefined preset for pixel size.
+function generateFontPreset( font ) {
+	const fontNames = font.split( ',' );
+
+	const cssFontNames = fontNames.join( fontNames );
+
+	return {
+		label: fontNames[ 0 ],
+		model: fontNames[ 0 ],
+		view: {
+			name: 'span',
+			styles: {
+				'font-family': cssFontNames
+			}
+		}
+	};
+}
+
+/**
+ * Font family option descriptor.
+ *
+ * @typedef {Object} module:font/fontfamily/fontfamilyediting~FontFamilyOption
+ *
+ * @property {String} label TODO me
+ * @property {String} model TODO me
+ * @property {module:engine/view/viewelementdefinition~ViewElementDefinition} view View element configuration.
+ */
+
+/**
+ * The configuration of the heading feature. Introduced by the {@link module:font/fontfamily/fontfamilyediting~FontSizeEditing} feature.
+ *
+ * Read more in {@link module:font/fontfamily/fontfamilyediting~FontFamilyConfig}.
+ *
+ * @member {module:font/fontfamily/fontfamilyediting~FontFamilyConfig} module:core/editor/editorconfig~EditorConfig#fontFamily
+ */
+
+/**
+ * The configuration of the font family feature.
+ * The option is used by the {@link module:font/fontfamily/fontfamilyediting~FontSizeEditing} feature.
+ *
+ *        ClassicEditor
+ *            .create( {
+ * 				fontFamily: ... // Font family feature config.
+ *			} )
+ *            .then( ... )
+ *            .catch( ... );
+ *
+ * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
+ *
+ * @interface module:font/fontfamily/fontfamilyediting~FontFamilyConfig
+ */
+
+/**
+ * Available font family options. Defined either as array of strings.
+ *
+ * The default value is
+ * TODO code
+ * which configures
+ *
+ * @member {Array.<String|Number|module:font/fontfamily/fontfamilyediting~FontFamilyOption>}
+ *  module:font/fontfamily/fontfamilyediting~FontFamilyConfig#items
+ */

+ 124 - 0
packages/ckeditor5-font/tests/fontfamily/fontfamilycommand.js

@@ -0,0 +1,124 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import FontFamilyCommand from '../../src/fontfamily/fontfamilycommand';
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
+describe( 'FontFamilyCommand', () => {
+	let editor, model, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				command = new FontFamilyCommand( editor, 'arial' );
+				model = editor.model;
+
+				editor.commands.add( 'fontFamily', command );
+
+				model.schema.registerItem( 'paragraph', '$block' );
+
+				model.schema.allow( { name: '$inline', attributes: 'fontFamily', inside: '$block' } );
+			} );
+	} );
+
+	afterEach( () => {
+		editor.destroy();
+	} );
+
+	it( 'is a command', () => {
+		expect( FontFamilyCommand.prototype ).to.be.instanceOf( Command );
+		expect( command ).to.be.instanceOf( Command );
+	} );
+
+	describe( 'value', () => {
+		it( 'is set to true when selection is in text with fontFamily attribute', () => {
+			setData( model, '<paragraph><$text fontFamily="arial">fo[]o</$text></paragraph>' );
+
+			expect( command ).to.have.property( 'value', true );
+		} );
+
+		it( 'is undefined when selection is not in text with fontFamily attribute', () => {
+			setData( model, '<paragraph>fo[]o</paragraph>' );
+
+			expect( command ).to.have.property( 'value', false );
+		} );
+	} );
+
+	describe( 'isEnabled', () => {
+		it( 'is true when selection is on text which can have fontFamily added', () => {
+			setData( model, '<paragraph>fo[]o</paragraph>' );
+
+			expect( command ).to.have.property( 'isEnabled', true );
+		} );
+	} );
+
+	describe( 'execute()', () => {
+		it( 'should add fontFamily attribute on selected text', () => {
+			setData( model, '<paragraph>a[bc<$text fontFamily="arial">fo]obar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.be.false;
+
+			command.execute();
+
+			expect( command.value ).to.be.true;
+
+			expect( getData( model ) ).to.equal( '<paragraph>a[<$text fontFamily="arial">bcfo]obar</$text>xyz</paragraph>' );
+		} );
+
+		it( 'should add fontFamily attribute on selected nodes (multiple nodes)', () => {
+			setData(
+				model,
+				'<paragraph>abcabc[abc</paragraph>' +
+				'<paragraph>foofoofoo</paragraph>' +
+				'<paragraph>barbar]bar</paragraph>'
+			);
+
+			command.execute();
+
+			expect( command.value ).to.be.true;
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>abcabc[<$text fontFamily="arial">abc</$text></paragraph>' +
+				'<paragraph><$text fontFamily="arial">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text fontFamily="arial">barbar</$text>]bar</paragraph>'
+			);
+		} );
+
+		it( 'should change fontFamily attribute on selected nodes', () => {
+			setData(
+				model,
+				'<paragraph>abc[abc<$text fontFamily="text-small">abc</$text></paragraph>' +
+				'<paragraph><$text fontFamily="text-small">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text fontFamily="text-small">bar]bar</$text>bar</paragraph>'
+			);
+
+			command.execute();
+
+			expect( command.value ).to.be.true;
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>abc[<$text fontFamily="arial">abcabc</$text></paragraph>' +
+				'<paragraph><$text fontFamily="arial">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text fontFamily="arial">bar</$text>]<$text fontFamily="text-small">bar</$text>bar</paragraph>'
+			);
+		} );
+
+		it( 'should do nothing on collapsed range', () => {
+			setData( model, '<paragraph>abc<$text fontFamily="arial">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.be.true;
+
+			command.execute();
+
+			expect( getData( model ) ).to.equal( '<paragraph>abc<$text fontFamily="arial">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.be.true;
+		} );
+	} );
+} );

+ 45 - 0
packages/ckeditor5-font/tests/fontfamily/fontfamilyediting.js

@@ -0,0 +1,45 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import FontFamilyEditing from './../../src/fontfamily/fontfamilyediting';
+
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+
+describe( 'FontFamilyEditing', () => {
+	let editor;
+
+	beforeEach( () => {
+		return VirtualTestEditor
+			.create( {
+				plugins: [ FontFamilyEditing, Paragraph ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+			} );
+	} );
+
+	afterEach( () => {
+		editor.destroy();
+	} );
+
+	it( 'should set proper schema rules', () => {
+		expect( editor.model.schema.check( { name: '$inline', attributes: 'fontFamily', inside: '$block' } ) ).to.be.true;
+		expect( editor.model.schema.check( { name: '$inline', attributes: 'fontFamily', inside: '$clipboardHolder' } ) ).to.be.true;
+	} );
+
+	describe( 'config', () => {
+		describe( 'default value', () => {
+			it( 'should be set', () => {
+				expect( editor.config.get( 'fontFamily.items' ) ).to.deep.equal( [
+					'default',
+					'Arial, Helvetica, sans-serif',
+					'Courier New, Courier, monospace'
+				] );
+			} );
+		} );
+	} );
+} );