Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #36 from ckeditor/t/35
Browse files Browse the repository at this point in the history
Feature: Added support for backticks which apply `<code>` to the wrapped fragment of text. Closes #35.
  • Loading branch information
Reinmar authored Sep 12, 2017
2 parents b9df37f + bbd2456 commit 3e93bf6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
5 changes: 3 additions & 2 deletions docs/features/autoformat.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ The following block formatting options are available:

The following inline formatting options are available:

* Bold &ndash; Type `**text**` or `__text__`.
* Italic &ndash; Type `*text*` or `_text_`.
* Bold &ndash; Type `**text**` or `__text__`,
* Italic &ndash; Type `*text*` or `_text_`,
* Code &ndash; Type ``` `text` ```.

## Autoformatting sample

Expand Down
19 changes: 13 additions & 6 deletions src/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ export default class Autoformat extends Plugin {
}

/**
* Adds autoformatting related to the {@link module:basic-styles/bold~Bold} and
* {@link module:basic-styles/italic~Italic}.
* Adds autoformatting related to the {@link module:basic-styles/bold~Bold},
* {@link module:basic-styles/italic~Italic} and {@link module:basic-styles/code~Code}.
*
* When typed:
* - `**foobar**` &ndash; `**` characters are removed and `foobar` is set to bold.
* - `__foobar__` &ndash; `__` characters are removed and `foobar` is set to bold.
* - `*foobar*` &ndash; `*` characters are removed and `foobar` is set to italic.
* - `_foobar_` &ndash; `_` characters are removed and `foobar` is set to italic.
* - `**foobar**` &ndash; `**` characters are removed and `foobar` is set to bold,
* - `__foobar__` &ndash; `__` characters are removed and `foobar` is set to bold,
* - `*foobar*` &ndash; `*` characters are removed and `foobar` is set to italic,
* - `_foobar_` &ndash; `_` characters are removed and `foobar` is set to italic,
* - ``` `foobar` &ndash; ``` ` ``` characters are removed and `foobar` is set to code.
*
* @private
*/
Expand All @@ -89,6 +90,12 @@ export default class Autoformat extends Plugin {
new InlineAutoformatEngine( this.editor, /(?:^|[^_])(_)([^_]+)(_)$/g, 'italic' );
/* eslint-enable no-new */
}

if ( commands.get( 'code' ) ) {
/* eslint-disable no-new */
new InlineAutoformatEngine( this.editor, /(`)([^`]+)(`)$/g, 'code' );
/* eslint-enable no-new */
}
}

/**
Expand Down
43 changes: 36 additions & 7 deletions tests/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 CodeEngine from '@ckeditor/ckeditor5-basic-styles/src/codeengine';
import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
import BlockQuoteEngine from '@ckeditor/ckeditor5-block-quote/src/blockquoteengine';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
Expand All @@ -28,7 +29,17 @@ describe( 'Autoformat', () => {
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine, BoldEngine, ItalicEngine, BlockQuoteEngine ]
plugins: [
Enter,
Paragraph,
Autoformat,
ListEngine,
HeadingEngine,
BoldEngine,
ItalicEngine,
CodeEngine,
BlockQuoteEngine
]
} )
.then( newEditor => {
editor = newEditor;
Expand Down Expand Up @@ -205,7 +216,7 @@ describe( 'Autoformat', () => {
} );

describe( 'Inline autoformat', () => {
it( 'should replace both `**` with bold', () => {
it( 'should replace both "**" with bold', () => {
setData( doc, '<paragraph>**foobar*[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
Expand All @@ -214,7 +225,7 @@ describe( 'Autoformat', () => {
expect( getData( doc ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
} );

it( 'should replace both `*` with italic', () => {
it( 'should replace both "*" with italic', () => {
setData( doc, '<paragraph>*foobar[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
Expand All @@ -223,7 +234,16 @@ describe( 'Autoformat', () => {
expect( getData( doc ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
} );

it( 'nothing should be replaces when typing `*`', () => {
it( 'should replace both "`" with code', () => {
setData( doc, '<paragraph>`foobar[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '`' );
} );

expect( getData( doc ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
} );

it( 'nothing should be replaces when typing "*"', () => {
setData( doc, '<paragraph>foobar[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
Expand Down Expand Up @@ -300,7 +320,7 @@ describe( 'Autoformat', () => {
expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
} );

it( 'should not replace both `**` with bold', () => {
it( 'should not replace both "**" with bold', () => {
setData( doc, '<paragraph>**foobar*[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
Expand All @@ -309,7 +329,7 @@ describe( 'Autoformat', () => {
expect( getData( doc ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
} );

it( 'should not replace both `*` with italic', () => {
it( 'should not replace both "*" with italic', () => {
setData( doc, '<paragraph>*foobar[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
Expand All @@ -318,7 +338,16 @@ describe( 'Autoformat', () => {
expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
} );

it( 'should not replace `>` with block quote', () => {
it( 'should not replace both "`" with code', () => {
setData( doc, '<paragraph>`foobar[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '`' );
} );

expect( getData( doc ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
} );

it( 'should not replace ">" with block quote', () => {
setData( doc, '<paragraph>>[]</paragraph>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
Expand Down
5 changes: 3 additions & 2 deletions tests/manual/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Code from '@ckeditor/ckeditor5-basic-styles/src/code';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';

ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading, List, Autoformat, BlockQuote ],
toolbar: [ 'headings', 'numberedList', 'bulletedList', 'blockQuote', 'bold', 'italic', 'undo', 'redo' ]
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Code, Heading, List, Autoformat, BlockQuote ],
toolbar: [ 'headings', 'numberedList', 'bulletedList', 'blockQuote', 'bold', 'italic', 'code', 'undo', 'redo' ]
} )
.then( editor => {
window.editor = editor;
Expand Down
4 changes: 3 additions & 1 deletion tests/manual/autoformat.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

1. Type `*` or `-` and the press space in an empty paragraph to replace it with a list item.

1. Type `> ` and press the space in an empty paragraph to replace it with a block quote.
1. Type `>` and press the space in an empty paragraph to replace it with a block quote.

1. Type a number from the range **1-3** to replace an empty paragraph with a numbered list item.

1. Type `*foobar*`/`_foobar_` to italicize `foobar`. `*`/`_` should be removed.

1. Type `**foobar**`/`__foobar__` to bold `foobar`. `**`/`__` should be removed.

1. Type ``` `foobar` ``` to mark as code `foobar`. ``` ` ``` should be removed.

1. For every autoformat pattern: Undo until you'll see just the pattern (e.g. `- `). Typing should be then possible without triggering the autoformatting again.

1. Typing a different pattern in an already converted block **must not** trigger the autoformatting. For example, typing `- ` in a heading should not convert a heading to a list.
Expand Down

0 comments on commit 3e93bf6

Please sign in to comment.