From b4539362bc845ccf47bca3e2899d782f015bf6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kup=C5=9B?= Date: Mon, 15 May 2017 14:43:27 +0200 Subject: [PATCH 1/3] Removed features dependencies, autoformat is enabled when corresponding command exists. --- package.json | 9 ++--- src/autoformat.js | 97 ++++++++++++++++++++++++++++----------------- tests/autoformat.js | 82 +++++++++++++++++++++++++++++++++++++- 3 files changed, 145 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index 5785b86..e77b5af 100644 --- a/package.json +++ b/package.json @@ -4,17 +4,16 @@ "description": "Replaces predefined characters with corresponding structures.", "keywords": [], "dependencies": { - "@ckeditor/ckeditor5-basic-styles": "^0.8.1", "@ckeditor/ckeditor5-core": "^0.8.1", - "@ckeditor/ckeditor5-engine": "^0.10.0", - "@ckeditor/ckeditor5-heading": "^0.9.1", - "@ckeditor/ckeditor5-list": "^0.6.1" + "@ckeditor/ckeditor5-engine": "^0.10.0" }, "devDependencies": { + "@ckeditor/ckeditor5-basic-styles": "^0.8.1", "@ckeditor/ckeditor5-dev-lint": "^2.0.2", "@ckeditor/ckeditor5-editor-classic": "^0.7.3", "@ckeditor/ckeditor5-enter": "^0.9.1", - "@ckeditor/ckeditor5-list": "*", + "@ckeditor/ckeditor5-heading": "^0.9.1", + "@ckeditor/ckeditor5-list": "^0.6.1", "@ckeditor/ckeditor5-paragraph": "^0.8.0", "@ckeditor/ckeditor5-typing": "^0.9.1", "@ckeditor/ckeditor5-undo": "^0.8.1", diff --git a/src/autoformat.js b/src/autoformat.js index de4875b..232e263 100644 --- a/src/autoformat.js +++ b/src/autoformat.js @@ -10,10 +10,6 @@ import BlockAutoformatEngine from './blockautoformatengine'; import InlineAutoformatEngine from './inlineautoformatengine'; import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; -import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine'; -import ListEngine from '@ckeditor/ckeditor5-list/src/listengine'; -import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine'; -import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine'; /** * Includes a set of predefined autoformatting actions. @@ -47,16 +43,13 @@ import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine'; * * `**foo bar**` or `__foo bar__` – will bold the text, * * `*foo bar*` or `_foo bar_` – will italicize the text, * + * NOTE: Remember to add proper features to editor configuration. Autoformatting will be enabled only for those + * commands that are included in current configuration. For example: `bold` autoformatting will not work if there is no + * `bold` command registered in editor. + * * @extends module:core/plugin~Plugin */ export default class Autoformat extends Plugin { - /** - * @inheritDoc - */ - static get requires() { - return [ HeadingEngine, ListEngine, BoldEngine, ItalicEngine ]; - } - /** * @inheritDoc */ @@ -67,14 +60,14 @@ export default class Autoformat extends Plugin { /** * @inheritDoc */ - init() { + afterInit() { this._addListAutoformats(); + this._addBasicStylesAutoformats(); this._addHeadingAutoformats(); - this._addInlineAutoformats(); } /** - * Adds autoformatting related to ListEngine commands. + * Adds autoformatting related to `bulletedList` and `numberedList` commands. * * When typed: * - `* ` or `- ` - paragraph will be changed to a bulleted list, @@ -83,27 +76,19 @@ export default class Autoformat extends Plugin { * @private */ _addListAutoformats() { - new BlockAutoformatEngine( this.editor, /^[\*\-]\s$/, 'bulletedList' ); - new BlockAutoformatEngine( this.editor, /^\d+[\.|)]?\s$/, 'numberedList' ); - } + const commands = this.editor.commands; - /** - * Adds autoformatting related to HeadingEngine commands. - * When typed `# ` or `## ` or `### ` paragraph will be changed to a corresponding heading level. - * - * @private - */ - _addHeadingAutoformats() { - new BlockAutoformatEngine( this.editor, /^(#{1,3})\s$/, ( context ) => { - const { batch, match } = context; - const headingLevel = match[ 1 ].length; + if ( commands.has( 'bulletedList' ) ) { + new BlockAutoformatEngine( this.editor, /^[\*\-]\s$/, 'bulletedList' ); + } - this.editor.execute( `heading${ headingLevel }`, { batch } ); - } ); + if ( commands.has( 'numberedList' ) ) { + new BlockAutoformatEngine( this.editor, /^\d+[\.|)]?\s$/, 'numberedList' ); + } } /** - * Adds inline autoformatting capabilities to the editor. + *Adds autoformatting related to `bold` and `italic` commands. * * When typed: * - `**foobar**`: `**` characters are removed, and `foobar` is set to bold, @@ -113,13 +98,51 @@ export default class Autoformat extends Plugin { * * @private */ - _addInlineAutoformats() { - new InlineAutoformatEngine( this.editor, /(\*\*)([^\*]+)(\*\*)$/g, 'bold' ); - new InlineAutoformatEngine( this.editor, /(__)([^_]+)(__)$/g, 'bold' ); + _addBasicStylesAutoformats() { + const commands = this.editor.commands; + + if ( commands.has( 'bold' ) ) { + new InlineAutoformatEngine( this.editor, /(\*\*)([^\*]+)(\*\*)$/g, 'bold' ); + new InlineAutoformatEngine( this.editor, /(__)([^_]+)(__)$/g, 'bold' ); + } + + if ( commands.has( 'italic' ) ) { + // The italic autoformatter cannot be triggered by the bold markers, so we need to check the + // text before the pattern (e.g. `(?:^|[^\*])`). + new InlineAutoformatEngine( this.editor, /(?:^|[^\*])(\*)([^\*_]+)(\*)$/g, 'italic' ); + new InlineAutoformatEngine( this.editor, /(?:^|[^_])(_)([^_]+)(_)$/g, 'italic' ); + } + } + + /** + * Adds autoformatting related to heading commands. + * It is using number at the end of the command name to associate it with proper trigger: + * * `heading1` will be executed when typing `#`, + * * `heading2` will be executed when typing `##`, + * * `heading3` will be executed when typing `###`. + * + * @private + */ + _addHeadingAutoformats() { + const commands = this.editor.commands; + const options = this.editor.config.get( 'heading.options' ); + + if ( options ) { + for ( let option of options ) { + const commandName = option.modelElement; + let match; + + if ( commands.has( commandName ) && ( match = commandName.match( /\d+$/ ) ) ) { + const level = match[ 0 ]; + const regExp = new RegExp( `^(#{${ level }})\\s$` ); + + new BlockAutoformatEngine( this.editor, regExp, ( context ) => { + const { batch } = context; - // The italic autoformatter cannot be triggered by the bold markers, so we need to check the - // text before the pattern (e.g. `(?:^|[^\*])`). - new InlineAutoformatEngine( this.editor, /(?:^|[^\*])(\*)([^\*_]+)(\*)$/g, 'italic' ); - new InlineAutoformatEngine( this.editor, /(?:^|[^_])(_)([^_]+)(_)$/g, 'italic' ); + this.editor.execute( commandName, { batch } ); + } ); + } + } + } } } diff --git a/tests/autoformat.js b/tests/autoformat.js index 705bc57..7bc607b 100644 --- a/tests/autoformat.js +++ b/tests/autoformat.js @@ -5,6 +5,10 @@ import Autoformat from '../src/autoformat'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; +import ListEngine from '@ckeditor/ckeditor5-list/src/listengine'; +import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine'; +import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine'; +import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine'; import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import Enter from '@ckeditor/ckeditor5-enter/src/enter'; import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; @@ -17,7 +21,7 @@ describe( 'Autoformat', () => { beforeEach( () => { return VirtualTestEditor.create( { - plugins: [ Enter, Paragraph, Autoformat ] + plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine, BoldEngine, ItalicEngine ] } ) .then( newEditor => { editor = newEditor; @@ -141,4 +145,80 @@ describe( 'Autoformat', () => { expect( getData( doc ) ).to.equal( 'foo <$text bold="true">bar[] baz' ); } ); } ); + + describe( 'without commands', () => { + beforeEach( () => { + return VirtualTestEditor.create( { + plugins: [ Enter, Paragraph, Autoformat ] + } ) + .then( newEditor => { + editor = newEditor; + doc = editor.document; + batch = doc.batch(); + } ); + } ); + + it( 'should not replace asterisk with bulleted list item', () => { + setData( doc, '*[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( getData( doc ) ).to.equal( '* []' ); + } ); + + it( 'should not replace minus character with bulleted list item', () => { + setData( doc, '-[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( getData( doc ) ).to.equal( '- []' ); + } ); + + it( 'should not replace digit with numbered list item', () => { + setData( doc, '1.[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( getData( doc ) ).to.equal( '1. []' ); + } ); + + it( 'should not replace hash character with heading', () => { + setData( doc, '#[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( getData( doc ) ).to.equal( '# []' ); + } ); + + it( 'should not replace two hash characters with heading level 2', () => { + setData( doc, '##[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( getData( doc ) ).to.equal( '## []' ); + } ); + + it( 'should not replace both `**` with bold', () => { + setData( doc, '**foobar*[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), '*' ); + } ); + + expect( getData( doc ) ).to.equal( '**foobar**[]' ); + } ); + + it( 'should not replace both `*` with italic', () => { + setData( doc, '*foobar[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), '*' ); + } ); + + expect( getData( doc ) ).to.equal( '*foobar*[]' ); + } ); + } ); } ); From e4f403f5cd07b8182782550662621bb37d194fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kup=C5=9B?= Date: Mon, 15 May 2017 14:59:48 +0200 Subject: [PATCH 2/3] Test checking if autoformat is disabled for headings that are not configured. --- tests/autoformat.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/autoformat.js b/tests/autoformat.js index 7bc607b..63dcf44 100644 --- a/tests/autoformat.js +++ b/tests/autoformat.js @@ -220,5 +220,28 @@ describe( 'Autoformat', () => { expect( getData( doc ) ).to.equal( '*foobar*[]' ); } ); + + it( 'should use only configured headings', () => { + return VirtualTestEditor.create( { + plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine ], + heading: { + options: [ + { modelElement: 'paragraph' }, + { modelElement: 'heading1', viewElement: 'h2' } + ] + } + } ) + .then( editor => { + doc = editor.document; + batch = doc.batch(); + + setData( doc, '##[]' ); + doc.enqueueChanges( () => { + batch.insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( getData( doc ) ).to.equal( '## []' ); + } ); + } ); } ); } ); From e62d0ab4ece6feb1ce0819196a4989d092b2d461 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Thu, 18 May 2017 14:57:11 +0200 Subject: [PATCH 3/3] Docs: Improved documentation of the Plugin. --- src/autoformat.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/autoformat.js b/src/autoformat.js index aa44fcb..3f6fd00 100644 --- a/src/autoformat.js +++ b/src/autoformat.js @@ -43,9 +43,9 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; * * `**foo bar**` or `__foo bar__` – will bold the text, * * `*foo bar*` or `_foo bar_` – will italicize the text, * - * NOTE: Remember to add proper features to editor configuration. Autoformatting will be enabled only for those - * commands that are included in current configuration. For example: `bold` autoformatting will not work if there is no - * `bold` command registered in editor. + * NOTE: Remember to add proper features to the editor configuration. Autoformatting will be enabled only for those + * commands that are included in the actual configuration. For example: `bold` autoformatting will not work if there is no + * `bold` command registered in the editor. * * @extends module:core/plugin~Plugin */ @@ -67,11 +67,11 @@ export default class Autoformat extends Plugin { } /** - * Adds autoformatting related to `bulletedList` and `numberedList` commands. + * Adds autoformatting related to the {@link module:list/list~List}. * * When typed: - * - `* ` or `- ` - paragraph will be changed to a bulleted list, - * - `1. ` or `1) ` - paragraph will be changed to a numbered list (1 can be any digit or list of digits). + * - `* ` or `- ` - a paragraph will be changed to a bulleted list, + * - `1. ` or `1) ` - a paragraph will be changed to a numbered list ("1" can be any digit or list of digits). * * @private */ @@ -90,7 +90,8 @@ export default class Autoformat extends Plugin { } /** - *Adds autoformatting related to `bold` and `italic` commands. + * Adds autoformatting related to the {@link module:basic-styles/bold~Bold} and + * {@link module:basic-styles/italic~Italic}. * * When typed: * - `**foobar**`: `**` characters are removed, and `foobar` is set to bold, @@ -122,8 +123,9 @@ export default class Autoformat extends Plugin { } /** - * Adds autoformatting related to heading commands. - * It is using number at the end of the command name to associate it with proper trigger: + * Adds autoformatting related to {@link module:heading/heading~Heading}. + * + * It is using a number at the end of the command name to associate it with the proper trigger: * * `heading1` will be executed when typing `#`, * * `heading2` will be executed when typing `##`, * * `heading3` will be executed when typing `###`.