-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
arkflpc
merged 27 commits into
ck/epic/2973-document-lists
from
ck/11284-document-list-ghs
Mar 30, 2022
Merged
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 3ace1fe
WiP.
niegowski ad30c0b
WiP.
niegowski b89ad73
WiP.
niegowski 4b60aaa
WiP.
niegowski 1c105b7
WiP.
niegowski 74260f8
Cleaning manual test.
niegowski 8cfb837
Merge branch 'ck/11065-document-list-properties' into ck/11284-docume…
niegowski 6095208
WiP.
niegowski 209a4d4
Moved down-cast conversion of list attributes to the single converter…
niegowski e751b65
Code cleaning.
niegowski 47cffde
Added post-fixer for Document Lists in GHS.
niegowski c609e16
Code cleaning.
niegowski 5ad2f0f
Reverted handling of a list attributes (using attribute name prefix c…
niegowski 829b34a
Reverted changes in downcast writer.
niegowski 2face4c
Code cleaning.
niegowski 4d1eda8
Revert "Reverted handling of a list attributes (using attribute name …
niegowski a174f29
Using registered strategies instead of attribute name prefix.
niegowski a3f22be
Merge branch 'ck/11065-document-list-properties' into ck/11284-docume…
niegowski e1973ff
Code cleaning.
niegowski 406f00b
Updated docs.
niegowski 58fe16d
Manual tests cleaning.
niegowski 5a56385
Added basic tests for 100CC.
niegowski 5dd4a9c
GHS: DocumentListSupport - tests
arkflpc 299bb38
GHS: DocumentListSupport - indenting list
arkflpc ec7b4e6
Apply review comment.
niegowski c45f85d
Apply review comment.
niegowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
packages/ckeditor5-html-support/src/integrations/documentlist.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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 ); | ||
} | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
conversion-utils
.