|
|
@@ -10,7 +10,7 @@ import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
|
|
|
|
|
|
const getTypeFromViewElement = viewElement => {
|
|
|
for ( const type of [ 'info', 'warning' ] ) {
|
|
|
- if ( viewElement.hasClass( `info-box-${ type }` ) ) {
|
|
|
+ if ( viewElement.hasClass( `side-card-${ type }` ) ) {
|
|
|
return type;
|
|
|
}
|
|
|
}
|
|
|
@@ -18,21 +18,21 @@ const getTypeFromViewElement = viewElement => {
|
|
|
return 'info';
|
|
|
};
|
|
|
|
|
|
-const upcastInfoBox = ( viewElement, { writer } ) => {
|
|
|
- const complexInfoBox = writer.createElement( 'complexInfoBox' );
|
|
|
+const upcastCard = ( viewElement, { writer } ) => {
|
|
|
+ const sideCard = writer.createElement( 'sideCard' );
|
|
|
|
|
|
const type = getTypeFromViewElement( viewElement );
|
|
|
- writer.setAttribute( 'infoBoxType', type, complexInfoBox );
|
|
|
+ writer.setAttribute( 'cardType', type, sideCard );
|
|
|
|
|
|
const urlWrapper = [ ...viewElement.getChildren() ].find( child => {
|
|
|
- return child.is( 'element', 'div' ) && child.hasClass( 'info-box-url' );
|
|
|
+ return child.is( 'element', 'div' ) && child.hasClass( 'side-card-url' );
|
|
|
} );
|
|
|
|
|
|
if ( urlWrapper ) {
|
|
|
- writer.setAttribute( 'infoBoxURL', urlWrapper.getChild( 0 ).data, complexInfoBox );
|
|
|
+ writer.setAttribute( 'cardURL', urlWrapper.getChild( 0 ).data, sideCard );
|
|
|
}
|
|
|
|
|
|
- return complexInfoBox;
|
|
|
+ return sideCard;
|
|
|
};
|
|
|
|
|
|
function addActionButton( text, callback, domElement, editor ) {
|
|
|
@@ -52,29 +52,29 @@ function addActionButton( text, callback, domElement, editor ) {
|
|
|
const renderActionsView = ( editor, modelElement ) => function( domElement ) {
|
|
|
addActionButton( 'Set URL', writer => {
|
|
|
// eslint-disable-next-line no-alert
|
|
|
- const newURL = prompt( 'Set URL', modelElement.getAttribute( 'infoBoxURL' ) || '' );
|
|
|
+ const newURL = prompt( 'Set URL', modelElement.getAttribute( 'cardURL' ) || '' );
|
|
|
|
|
|
- writer.setAttribute( 'infoBoxURL', newURL, modelElement );
|
|
|
+ writer.setAttribute( 'cardURL', newURL, modelElement );
|
|
|
}, domElement, editor );
|
|
|
|
|
|
- const currentType = modelElement.getAttribute( 'infoBoxType' );
|
|
|
+ const currentType = modelElement.getAttribute( 'cardType' );
|
|
|
const newType = currentType === 'info' ? 'warning' : 'info';
|
|
|
|
|
|
- addActionButton( `Change to ${ newType }`, writer => {
|
|
|
- writer.setAttribute( 'infoBoxType', newType, modelElement );
|
|
|
+ addActionButton( 'Change type', writer => {
|
|
|
+ writer.setAttribute( 'cardType', newType, modelElement );
|
|
|
}, domElement, editor );
|
|
|
|
|
|
const childCount = modelElement.childCount;
|
|
|
|
|
|
- const addButton = addActionButton( 'Add content box', writer => {
|
|
|
- writer.insertElement( 'complexInfoBoxContent', modelElement, 'end' );
|
|
|
+ const addButton = addActionButton( 'Add section', writer => {
|
|
|
+ writer.insertElement( 'sideCardSection', modelElement, 'end' );
|
|
|
}, domElement, editor );
|
|
|
|
|
|
if ( childCount > 4 ) {
|
|
|
addButton.setAttribute( 'disabled', 'disabled' );
|
|
|
}
|
|
|
|
|
|
- const removeButton = addActionButton( 'Remove content box', writer => {
|
|
|
+ const removeButton = addActionButton( 'Remove section', writer => {
|
|
|
writer.remove( modelElement.getChild( childCount - 1 ) );
|
|
|
}, domElement, editor );
|
|
|
|
|
|
@@ -83,61 +83,62 @@ const renderActionsView = ( editor, modelElement ) => function( domElement ) {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const downcastInfoBox = editor => {
|
|
|
+const downcastSideCard = editor => {
|
|
|
return ( modelElement, { writer, consumable, mapper } ) => {
|
|
|
- const complexInfoBoxView = writer.createContainerElement( 'div', {
|
|
|
- class: 'info-box'
|
|
|
- } );
|
|
|
-
|
|
|
- const type = modelElement.getAttribute( 'infoBoxType' ) || 'info';
|
|
|
+ const type = modelElement.getAttribute( 'cardType' ) || 'info';
|
|
|
|
|
|
- writer.addClass( `info-box-${ type }`, complexInfoBoxView );
|
|
|
+ const sideCardView = writer.createContainerElement( 'aside', {
|
|
|
+ class: `side-card side-card-${ type }`
|
|
|
+ } );
|
|
|
|
|
|
+ // Create inner views from side card children.
|
|
|
for ( const child of modelElement.getChildren() ) {
|
|
|
const childView = writer.createEditableElement( 'div' );
|
|
|
|
|
|
- if ( child.is( 'element', 'complexInfoBoxTitle' ) ) {
|
|
|
- writer.addClass( 'info-box-title', childView );
|
|
|
+ // Child is either a "title" or "section".
|
|
|
+ if ( child.is( 'element', 'sideCardTitle' ) ) {
|
|
|
+ writer.addClass( 'side-card-title', childView );
|
|
|
} else {
|
|
|
- writer.addClass( 'info-box-content', childView );
|
|
|
+ writer.addClass( 'side-card-section', childView );
|
|
|
}
|
|
|
|
|
|
+ // It is important to consume & bind converted elements.
|
|
|
consumable.consume( child, 'insert' );
|
|
|
mapper.bindElements( child, childView );
|
|
|
|
|
|
+ // Make it an editable part of the widget.
|
|
|
toWidgetEditable( childView, writer );
|
|
|
|
|
|
- writer.insert( writer.createPositionAt( complexInfoBoxView, 'end' ), childView );
|
|
|
+ writer.insert( writer.createPositionAt( sideCardView, 'end' ), childView );
|
|
|
}
|
|
|
|
|
|
- const urlAttribute = modelElement.getAttribute( 'infoBoxURL' );
|
|
|
+ const urlAttribute = modelElement.getAttribute( 'cardURL' );
|
|
|
|
|
|
// Do not render empty URL field
|
|
|
if ( urlAttribute ) {
|
|
|
const urlBox = writer.createRawElement( 'div', {
|
|
|
- class: 'info-box-url'
|
|
|
+ class: 'side-card-url'
|
|
|
}, function( domElement ) {
|
|
|
domElement.innerText = `URL: "${ urlAttribute }"`;
|
|
|
} );
|
|
|
|
|
|
- writer.insert( writer.createPositionAt( complexInfoBoxView, 'end' ), urlBox );
|
|
|
+ writer.insert( writer.createPositionAt( sideCardView, 'end' ), urlBox );
|
|
|
}
|
|
|
|
|
|
+ // Inner element used to render simple UI that allows to change side card's attributes.
|
|
|
const actionsView = writer.createRawElement( 'div', {
|
|
|
- class: 'info-box-actions',
|
|
|
+ class: 'side-card-actions',
|
|
|
contenteditable: 'false', // Prevent editing of the element:
|
|
|
'data-cke-ignore-events': 'true' // Allows using custom UI elements inside editing view.
|
|
|
- }, renderActionsView( editor, modelElement ) );
|
|
|
+ }, renderActionsView( editor, modelElement ) ); // See the full code for details.
|
|
|
|
|
|
- writer.insert( writer.createPositionAt( complexInfoBoxView, 'end' ), actionsView );
|
|
|
+ writer.insert( writer.createPositionAt( sideCardView, 'end' ), actionsView );
|
|
|
|
|
|
- return toWidget( complexInfoBoxView, writer, {
|
|
|
- widgetLabel: 'Complex Info Box'
|
|
|
- } );
|
|
|
+ return toWidget( sideCardView, writer, { widgetLabel: 'Side card' } );
|
|
|
};
|
|
|
};
|
|
|
|
|
|
-class ComplexInfoBox {
|
|
|
+class ComplexBox {
|
|
|
constructor( editor ) {
|
|
|
this._defineSchema( editor );
|
|
|
this._defineConversion( editor );
|
|
|
@@ -148,25 +149,25 @@ class ComplexInfoBox {
|
|
|
|
|
|
conversion.for( 'upcast' )
|
|
|
.elementToElement( {
|
|
|
- view: { name: 'div', classes: [ 'info-box' ] },
|
|
|
- model: upcastInfoBox
|
|
|
+ view: { name: 'aside', classes: [ 'side-card' ] },
|
|
|
+ model: upcastCard
|
|
|
} )
|
|
|
.elementToElement( {
|
|
|
- view: { name: 'div', classes: [ 'info-box-title' ] },
|
|
|
- model: 'complexInfoBoxTitle'
|
|
|
+ view: { name: 'div', classes: [ 'side-card-title' ] },
|
|
|
+ model: 'sideCardTitle'
|
|
|
} )
|
|
|
.elementToElement( {
|
|
|
- view: { name: 'div', classes: [ 'info-box-content' ] },
|
|
|
- model: 'complexInfoBoxContent'
|
|
|
+ view: { name: 'div', classes: [ 'side-card-section' ] },
|
|
|
+ model: 'sideCardSection'
|
|
|
} );
|
|
|
|
|
|
// The downcast conversion must be split as you need a widget in the editing pipeline.
|
|
|
conversion.for( 'downcast' ).elementToElement( {
|
|
|
- model: 'complexInfoBox',
|
|
|
- view: downcastInfoBox( editor ),
|
|
|
+ model: 'sideCard',
|
|
|
+ view: downcastSideCard( editor ),
|
|
|
triggerBy: {
|
|
|
- attributes: [ 'infoBoxType', 'infoBoxURL' ],
|
|
|
- children: [ 'complexInfoBoxContent' ]
|
|
|
+ attributes: [ 'cardType', 'cardURL' ],
|
|
|
+ children: [ 'sideCardSection' ]
|
|
|
}
|
|
|
} );
|
|
|
}
|
|
|
@@ -175,30 +176,30 @@ class ComplexInfoBox {
|
|
|
const schema = editor.model.schema;
|
|
|
|
|
|
// The main element with attributes for type and URL:
|
|
|
- schema.register( 'complexInfoBox', {
|
|
|
+ schema.register( 'sideCard', {
|
|
|
allowWhere: '$block',
|
|
|
isObject: true,
|
|
|
- allowAttributes: [ 'infoBoxType', 'infoBoxURL' ]
|
|
|
+ allowAttributes: [ 'cardType', 'cardURL' ]
|
|
|
} );
|
|
|
|
|
|
// A text-only title.
|
|
|
- schema.register( 'complexInfoBoxTitle', {
|
|
|
+ schema.register( 'sideCardTitle', {
|
|
|
isLimit: true,
|
|
|
- allowIn: 'complexInfoBox'
|
|
|
+ allowIn: 'sideCard'
|
|
|
} );
|
|
|
// Allow text in title...
|
|
|
- schema.extend( '$text', { allowIn: 'complexInfoBoxTitle' } );
|
|
|
+ schema.extend( '$text', { allowIn: 'sideCardTitle' } );
|
|
|
// ...but disallow any text attribute inside.
|
|
|
schema.addAttributeCheck( context => {
|
|
|
- if ( context.endsWith( 'complexInfoBoxTitle $text' ) ) {
|
|
|
+ if ( context.endsWith( 'sideCardTitle $text' ) ) {
|
|
|
return false;
|
|
|
}
|
|
|
} );
|
|
|
|
|
|
// A content block which can have any content allowed in $root.
|
|
|
- schema.register( 'complexInfoBoxContent', {
|
|
|
+ schema.register( 'sideCardSection', {
|
|
|
isLimit: true,
|
|
|
- allowIn: 'complexInfoBox',
|
|
|
+ allowIn: 'sideCard',
|
|
|
allowContentOf: '$root'
|
|
|
} );
|
|
|
}
|
|
|
@@ -207,7 +208,7 @@ class ComplexInfoBox {
|
|
|
ClassicEditor
|
|
|
.create( document.querySelector( '#editor-element-reconversion' ), {
|
|
|
cloudServices: CS_CONFIG,
|
|
|
- extraPlugins: [ ComplexInfoBox ],
|
|
|
+ extraPlugins: [ ComplexBox ],
|
|
|
image: {
|
|
|
toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
|
|
|
},
|