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

[Document list] GHS integration #11391

Merged
merged 27 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c894905
PoC WiP DocumentLists + GHS.
niegowski Feb 16, 2022
3ace1fe
WiP.
niegowski Feb 17, 2022
ad30c0b
WiP.
niegowski Feb 18, 2022
b89ad73
WiP.
niegowski Feb 20, 2022
4b60aaa
WiP.
niegowski Feb 21, 2022
1c105b7
WiP.
niegowski Feb 22, 2022
74260f8
Cleaning manual test.
niegowski Feb 23, 2022
8cfb837
Merge branch 'ck/11065-document-list-properties' into ck/11284-docume…
niegowski Feb 27, 2022
6095208
WiP.
niegowski Feb 28, 2022
209a4d4
Moved down-cast conversion of list attributes to the single converter…
niegowski Mar 1, 2022
e751b65
Code cleaning.
niegowski Mar 2, 2022
47cffde
Added post-fixer for Document Lists in GHS.
niegowski Mar 2, 2022
c609e16
Code cleaning.
niegowski Mar 2, 2022
5ad2f0f
Reverted handling of a list attributes (using attribute name prefix c…
niegowski Mar 3, 2022
829b34a
Reverted changes in downcast writer.
niegowski Mar 3, 2022
2face4c
Code cleaning.
niegowski Mar 3, 2022
4d1eda8
Revert "Reverted handling of a list attributes (using attribute name …
niegowski Mar 7, 2022
a174f29
Using registered strategies instead of attribute name prefix.
niegowski Mar 7, 2022
a3f22be
Merge branch 'ck/11065-document-list-properties' into ck/11284-docume…
niegowski Mar 7, 2022
e1973ff
Code cleaning.
niegowski Mar 7, 2022
406f00b
Updated docs.
niegowski Mar 7, 2022
58fe16d
Manual tests cleaning.
niegowski Mar 7, 2022
5a56385
Added basic tests for 100CC.
niegowski Mar 8, 2022
5dd4a9c
GHS: DocumentListSupport - tests
arkflpc Mar 16, 2022
299bb38
GHS: DocumentListSupport - indenting list
arkflpc Mar 17, 2022
ec7b4e6
Apply review comment.
niegowski Mar 22, 2022
c45f85d
Apply review comment.
niegowski Mar 22, 2022
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
4 changes: 3 additions & 1 deletion packages/ckeditor5-html-support/src/generalhtmlsupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import MediaEmbedElementSupport from './integrations/mediaembed';
import ScriptElementSupport from './integrations/script';
import TableElementSupport from './integrations/table';
import StyleElementSupport from './integrations/style';
import DocumentListElementSupport from './integrations/documentlist';

/**
* The General HTML Support feature.
Expand Down Expand Up @@ -48,7 +49,8 @@ export default class GeneralHtmlSupport extends Plugin {
MediaEmbedElementSupport,
ScriptElementSupport,
TableElementSupport,
StyleElementSupport
StyleElementSupport,
DocumentListElementSupport
];
}

Expand Down
189 changes: 189 additions & 0 deletions packages/ckeditor5-html-support/src/integrations/documentlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module html-support/integrations/documentlist
*/

import { isEqual } from 'lodash-es';
import { Plugin } from 'ckeditor5/src/core';
import { setViewAttributes } from '../conversionutils.js';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be conversion-utils.


import DataFilter from '../datafilter';

/**
* Provides the General HTML Support integration with {@link module:list/documentlist~DocumentList Document List} feature.
*
* @extends module:core/plugin~Plugin
*/
export default class DocumentListElementSupport extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ DataFilter ];
}

/**
* @inheritDoc
*/
init() {
const editor = this.editor;

if ( !editor.plugins.has( 'DocumentListEditing' ) ) {
return;
}

const schema = editor.model.schema;
const conversion = editor.conversion;
const dataFilter = editor.plugins.get( DataFilter );
const documentListEditing = editor.plugins.get( 'DocumentListEditing' );

// Register downcast strategy.
// Note that this must be done before document list editing registers conversion in afterInit.
documentListEditing.registerDowncastStrategy( {
scope: 'item',
attributeName: 'htmlLiAttributes',

setAttributeOnDowncast( writer, attributeValue, viewElement ) {
setViewAttributes( writer, attributeValue, viewElement );
}
} );

documentListEditing.registerDowncastStrategy( {
scope: 'list',
attributeName: 'htmlListAttributes',

setAttributeOnDowncast( writer, viewAttributes, viewElement ) {
setViewAttributes( writer, viewAttributes, viewElement );
}
} );

dataFilter.on( 'register', ( evt, definition ) => {
if ( ![ 'ul', 'ol', 'li' ].includes( definition.view ) ) {
return;
}

evt.stop();

// Do not register same converters twice.
if ( schema.checkAttribute( '$block', 'htmlListAttributes' ) ) {
return;
}

schema.extend( '$block', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
schema.extend( '$blockObject', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
schema.extend( '$container', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );

conversion.for( 'upcast' ).add( dispatcher => {
dispatcher.on( 'element:ul', viewToModelListAttributeConverter( 'htmlListAttributes', dataFilter ), { priority: 'low' } );
dispatcher.on( 'element:ol', viewToModelListAttributeConverter( 'htmlListAttributes', dataFilter ), { priority: 'low' } );
dispatcher.on( 'element:li', viewToModelListAttributeConverter( 'htmlLiAttributes', dataFilter ), { priority: 'low' } );
} );
} );

// Reset list attributes after indenting list items.
this.listenTo( editor.commands.get( 'indentList' ), 'afterExecute', ( evt, changedBlocks ) => {
editor.model.change( writer => {
for ( const node of changedBlocks ) {
// Just reset the attribute.
// If there is a previous indented list that this node should be merged into,
// the postfixer will unify all the attributes of both sub-lists.
writer.setAttribute( 'htmlListAttributes', {}, node );
}
} );
} );

// Make sure that all items in a single list (items at the same level & listType) have the same properties.
// Note: This is almost exact copy from DocumentListPropertiesEditing.
documentListEditing.on( 'postFixer', ( evt, { listNodes, writer } ) => {
const previousNodesByIndent = []; // Last seen nodes of lower indented lists.

for ( const { node, previous } of listNodes ) {
// For the first list block there is nothing to compare with.
if ( !previous ) {
continue;
}

const nodeIndent = node.getAttribute( 'listIndent' );
const previousNodeIndent = previous.getAttribute( 'listIndent' );

let previousNodeInList = null; // It's like `previous` but has the same indent as current node.

// Let's find previous node for the same indent.
// We're going to need that when we get back to previous indent.
if ( nodeIndent > previousNodeIndent ) {
previousNodesByIndent[ previousNodeIndent ] = previous;
}
// Restore the one for given indent.
else if ( nodeIndent < previousNodeIndent ) {
previousNodeInList = previousNodesByIndent[ nodeIndent ];
previousNodesByIndent.length = nodeIndent;
}
// Same indent.
else {
previousNodeInList = previous;
}

// This is a first item of a nested list.
if ( !previousNodeInList ) {
continue;
}

if ( previousNodeInList.getAttribute( 'listType' ) == node.getAttribute( 'listType' ) ) {
const value = previousNodeInList.getAttribute( 'htmlListAttributes' );

if ( !isEqual( node.getAttribute( 'htmlListAttributes' ), value ) ) {
writer.setAttribute( 'htmlListAttributes', value, node );
evt.return = true;
}
}

if ( previousNodeInList.getAttribute( 'listItemId' ) == node.getAttribute( 'listItemId' ) ) {
const value = previousNodeInList.getAttribute( 'htmlLiAttributes' );

if ( !isEqual( node.getAttribute( 'htmlLiAttributes' ), value ) ) {
writer.setAttribute( 'htmlLiAttributes', value, node );
evt.return = true;
}
}
}
} );
}
}

// View-to-model conversion helper preserving allowed attributes on {@link TODO}
// feature model element.
//
// @private
// @param {String} attributeName
// @param {module:html-support/datafilter~DataFilter} dataFilter
// @returns {Function} Returns a conversion callback.
function viewToModelListAttributeConverter( attributeName, dataFilter ) {
return ( evt, data, conversionApi ) => {
const viewElement = data.viewItem;

if ( !data.modelRange ) {
Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
}

const viewAttributes = dataFilter._consumeAllowedAttributes( viewElement, conversionApi );

for ( const item of data.modelRange.getItems( { shallow: true } ) ) {
// Apply only to list item blocks.
if ( !item.hasAttribute( 'listItemId' ) ) {
continue;
}

// Set list attributes only on same level items, those nested deeper are already handled
// by the recursive conversion.
if ( item.hasAttribute( attributeName ) ) {
continue;
}

conversionApi.writer.setAttribute( attributeName, viewAttributes || {}, item );
}
};
}
Loading