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

#8959 DowncastWriter should handle UIElements consistently while wrapping and inserting #8998

Merged
merged 16 commits into from
Feb 22, 2021
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
13 changes: 11 additions & 2 deletions docs/_snippets/framework/tutorials/inline-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import Model from '@ckeditor/ckeditor5-ui/src/model';
class PlaceholderCommand extends Command {
execute( { value } ) {
const editor = this.editor;
const selection = editor.model.document.selection;

editor.model.change( writer => {
// Create a <placeholder> elment with the "name" attribute...
const placeholder = writer.createElement( 'placeholder', { name: value } );
// Create a <placeholder> element with the "name" attribute (and all the selection attributes)...
const placeholder = writer.createElement( 'placeholder', {
...Object.fromEntries( selection.getAttributes() ),
name: value
} );

// ... and insert it into the document.
editor.model.insertContent( placeholder );
Expand Down Expand Up @@ -145,6 +149,9 @@ class PlaceholderEditing extends Plugin {
// The inline widget is self-contained so it cannot be split by the caret and it can be selected:
isObject: true,

// The inline widget can have the same attributes as text (for example linkHref, bold).
allowAttributesOf: '$text',

// The placeholder can have many types, like date, name, surname, etc:
allowAttributes: [ 'name' ]
} );
Expand Down Expand Up @@ -187,6 +194,8 @@ class PlaceholderEditing extends Plugin {

const placeholderView = viewWriter.createContainerElement( 'span', {
class: 'placeholder'
}, {
isAllowedInsideAttributeElement: true
} );

// Insert the placeholder name (as a text).
Expand Down
25 changes: 21 additions & 4 deletions docs/framework/guides/tutorials/implementing-an-inline-widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ export default class PlaceholderEditing extends Plugin {
// The inline widget is self-contained so it cannot be split by the caret and can be selected:
isObject: true,

// The inline widget can have the same attributes as text (for example linkHref, bold).
allowAttributesOf: '$text',

// The placeholder can have many types, like date, name, surname, etc:
allowAttributes: [ 'name' ]
} );
Expand Down Expand Up @@ -354,6 +357,8 @@ export default class PlaceholderEditing extends Plugin {

const placeholderView = viewWriter.createContainerElement( 'span', {
class: 'placeholder'
}, {
isAllowedInsideAttributeElement: true
} );

// Insert the placeholder name (as a text).
Expand Down Expand Up @@ -398,10 +403,14 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
export default class PlaceholderCommand extends Command {
execute( { value } ) {
const editor = this.editor;
const selection = editor.model.document.selection;

editor.model.change( writer => {
// Create a <placeholder> elment with the "name" attribute...
const placeholder = writer.createElement( 'placeholder', { name: value } );
// Create a <placeholder> elment with the "name" attribute (and all the selection attributes)...
const placeholder = writer.createElement( 'placeholder', {
...Object.fromEntries( selection.getAttributes() ),
name: value
} );

// ... and insert it into the document.
editor.model.insertContent( placeholder );
Expand Down Expand Up @@ -769,8 +778,11 @@ class PlaceholderCommand extends Command {
const editor = this.editor;

editor.model.change( writer => {
// Create a <placeholder> elment with the "name" attribute...
const placeholder = writer.createElement( 'placeholder', { name: value } );
// Create a <placeholder> elment with the "name" attribute (and all the selection attributes)...
const placeholder = writer.createElement( 'placeholder', {
...Object.fromEntries( selection.getAttributes() ),
name: value
} );

// ... and insert it into the document.
editor.model.insertContent( placeholder );
Expand Down Expand Up @@ -882,6 +894,9 @@ class PlaceholderEditing extends Plugin {
// The inline widget is self-contained so it cannot be split by the caret and it can be selected:
isObject: true,

// The inline widget can have the same attributes as text (for example linkHref, bold).
allowAttributesOf: '$text',

// The placeholder can have many types, like date, name, surname, etc:
allowAttributes: [ 'name' ]
} );
Expand Down Expand Up @@ -924,6 +939,8 @@ class PlaceholderEditing extends Plugin {

const placeholderView = viewWriter.createContainerElement( 'span', {
class: 'placeholder'
}, {
isInline: true
} );

// Insert the placeholder name (as a text).
Expand Down
10 changes: 10 additions & 0 deletions packages/ckeditor5-engine/src/view/attributeelement.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const DEFAULT_PRIORITY = 10;
* To create a new attribute element instance use the
* {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement `DowncastWriter#createAttributeElement()`} method.
*
* **Note:** Attribute elements by default can wrap {@link module:engine/view/text~Text},
* {@link module:engine/view/emptyelement~EmptyElement}, {@link module:engine/view/uielement~UIElement},
* {@link module:engine/view/rawelement~RawElement} and other attribute elements with higher priority. Other elements while placed inside
* an attribute element will split it (or nest in case of an `AttributeElement`). This behavior can be modified by changing
* the `isAllowedInsideAttributeElement` option while creating
* {@link module:engine/view/downcastwriter~DowncastWriter#createContainerElement},
* {@link module:engine/view/downcastwriter~DowncastWriter#createEmptyElement},
* {@link module:engine/view/downcastwriter~DowncastWriter#createUIElement} or
* {@link module:engine/view/downcastwriter~DowncastWriter#createRawElement}.
*
* @extends module:engine/view/element~Element
*/
export default class AttributeElement extends Element {
Expand Down
Loading