Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the way 2SCM support multiple attributes. Enable it for code. #7577

Merged
merged 17 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions packages/ckeditor5-basic-styles/docs/features/basic-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ By default, each feature can upcast more than one type of the content. Here's th
| {@link module:basic-styles/subscript~Subscript} | `<sub>`, `<* style="vertical-align: sub">` |
| {@link module:basic-styles/superscript~Superscript} | `<sup>`, `<* style="vertical-align: super">` |

## Typing around inline code

CKEditor 5 allows for typing both at inner and outer boundaries of code to make the editing easier for the users.

**To type inside a code element**, move the caret to its (start or end) boundary. As long as the code remains highlighted (by default: less transparent gray), typing and applying formatting will be done within its boundaries:

{@img assets/img/typing-inside-code.gif 770 The animation showing typing inside the code element in CKEditor 5 rich text editor.}

**To type before or after a code element**, move the caret to its boundary, then press the Arrow key (<kbd>→</kbd> or <kbd>←</kbd>) once. The code is no longer highlighted and whatever text you type or formatting you apply will not be enclosed by the code element:

{@img assets/img/typing-after-code.gif 770 The animation showing typing after the code element in CKEditor 5 rich text editor.}

## Installation

To add the basic styles features to your editor install the [`@ckeditor/ckeditor5-basic-styles`](https://www.npmjs.com/package/@ckeditor/ckeditor5-basic-styles) package:
Expand Down
1 change: 1 addition & 0 deletions packages/ckeditor5-basic-styles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"dependencies": {
"@ckeditor/ckeditor5-core": "^20.0.0",
"@ckeditor/ckeditor5-typing": "^20.0.0",
"@ckeditor/ckeditor5-ui": "^20.0.0"
},
"devDependencies": {
Expand Down
16 changes: 16 additions & 0 deletions packages/ckeditor5-basic-styles/src/code/codeediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import AttributeCommand from '../attributecommand';
import TwoStepCaretMovement from '@ckeditor/ckeditor5-typing/src/twostepcaretmovement';
import setupHighlight from '@ckeditor/ckeditor5-typing/src/utils/inlinehighlight';

const CODE = 'code';
const HIGHLIGHT_CLASS = 'ck-code_selected';

/**
* The code editing feature.
Expand All @@ -28,6 +31,13 @@ export default class CodeEditing extends Plugin {
return 'CodeEditing';
}

/**
* @inheritDoc
*/
static get requires() {
return [ TwoStepCaretMovement ];
}

/**
* @inheritDoc
*/
Expand All @@ -53,5 +63,11 @@ export default class CodeEditing extends Plugin {

// Create code command.
editor.commands.add( CODE, new AttributeCommand( editor, CODE ) );

// Enable two-step caret movement for `code` attribute.
editor.plugins.get( TwoStepCaretMovement ).registerAttribute( CODE );

// Setup highlight over selected element.
setupHighlight( editor, CODE, 'code', HIGHLIGHT_CLASS );
}
}
35 changes: 35 additions & 0 deletions packages/ckeditor5-basic-styles/tests/code/codeediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import AttributeCommand from '../../src/attributecommand';

import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';

/* global document */

describe( 'CodeEditing', () => {
let editor, model;
Expand Down Expand Up @@ -100,4 +103,36 @@ describe( 'CodeEditing', () => {
expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><code>foo</code>bar</p>' );
} );
} );

it( 'should add `ck-code_selected` class when caret enters the element', () => {
// Put selection before the link element.
setModelData( editor.model, '<paragraph>foo[]<$text code="true">ba</$text>r</paragraph>' );

// So let's simulate the `keydown` event.
editor.editing.view.document.fire( 'keydown', {
keyCode: keyCodes.arrowright,
preventDefault: () => {},
domTarget: document.body
} );

expect( getViewData( editor.editing.view ) ).to.equal(
'<p>foo<code class="ck-code_selected">{}ba</code>r</p>'
);
} );

it( 'should remove `ck-code_selected` class when caret leaves the element', () => {
// Put selection before the link element.
setModelData( editor.model, '<paragraph>foo<$text code="true">ba[]</$text>r</paragraph>' );

// So let's simulate the `keydown` event.
editor.editing.view.document.fire( 'keydown', {
keyCode: keyCodes.arrowright,
preventDefault: () => {},
domTarget: document.body
} );

expect( getViewData( editor.editing.view ) ).to.equal(
'<p>foo<code>ba</code>{}r</p>'
);
} );
} );
4 changes: 4 additions & 0 deletions packages/ckeditor5-basic-styles/theme/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
padding: .15em;
border-radius: 2px;
}

.ck .ck-code_selected {
background-color: hsla(0, 0%, 78%, 0.5);
}
4 changes: 2 additions & 2 deletions packages/ckeditor5-link/src/linkediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import MouseObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mouseobserver';
import TwoStepCaretMovement from '@ckeditor/ckeditor5-typing/src/twostepcaretmovement';
import setupLinkHighlight from '@ckeditor/ckeditor5-typing/src/utils/inlinehighlight';
import setupHighlight from '@ckeditor/ckeditor5-typing/src/utils/inlinehighlight';
import LinkCommand from './linkcommand';
import UnlinkCommand from './unlinkcommand';
import AutomaticDecorators from './utils/automaticdecorators';
Expand Down Expand Up @@ -104,7 +104,7 @@ export default class LinkEditing extends Plugin {
twoStepCaretMovementPlugin.registerAttribute( 'linkHref' );

// Setup highlight over selected link.
setupLinkHighlight( editor, 'linkHref', 'a', HIGHLIGHT_CLASS );
setupHighlight( editor, 'linkHref', 'a', HIGHLIGHT_CLASS );

// Change the attributes of the selection in certain situations after the link was inserted into the document.
this._enableInsertContentSelectionAttributesFixer();
Expand Down
Loading