forked from ckeditor/ckeditor5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ckeditor#14174 from ckeditor/ck/12738-add-document…
…listseparator-plugin Feature (list): Add the `AdjacentListsSupport` plugin to separate lists of the same type. Closes ckeditor#12738.
- Loading branch information
Showing
8 changed files
with
618 additions
and
0 deletions.
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
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
110 changes: 110 additions & 0 deletions
110
packages/ckeditor5-list/src/documentlist/adjacentlistssupport.ts
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,110 @@ | ||
/** | ||
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module list/documentlist/adjacentlistssupport | ||
*/ | ||
|
||
import type { GetCallback } from 'ckeditor5/src/utils'; | ||
import { Plugin } from 'ckeditor5/src/core'; | ||
|
||
import type { UpcastElementEvent, ViewElement } from 'ckeditor5/src/engine'; | ||
|
||
export default class AdjacentListsSupport extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static get pluginName(): 'AdjacentListsSupport' { | ||
return 'AdjacentListsSupport'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public init(): void { | ||
const editor = this.editor; | ||
const model = editor.model; | ||
|
||
model.schema.register( 'listSeparator', { | ||
allowWhere: '$block', | ||
isBlock: true | ||
} ); | ||
|
||
editor.conversion.for( 'upcast' ) | ||
// Add `listSeparator` element between similar list elements on upcast. | ||
.add( dispatcher => { | ||
dispatcher.on<UpcastElementEvent>( 'element:ol', listSeparatorUpcastConverter() ); | ||
dispatcher.on<UpcastElementEvent>( 'element:ul', listSeparatorUpcastConverter() ); | ||
} ) | ||
// View to model transformation. | ||
.elementToElement( { | ||
model: 'listSeparator', | ||
view: 'ck-list-separator' | ||
} ); | ||
|
||
// The `listSeparator` element should exist in the view, but should be invisible (hidden). | ||
editor.conversion.for( 'editingDowncast' ).elementToElement( { | ||
model: 'listSeparator', | ||
view: { | ||
name: 'div', | ||
classes: [ 'ck-list-separator', 'ck-hidden' ] | ||
} | ||
} ); | ||
|
||
// The `listSeparator` element should not exist in output data. | ||
editor.conversion.for( 'dataDowncast' ).elementToElement( { | ||
model: 'listSeparator', | ||
view: ( modelElement, conversionApi ) => { | ||
const viewElement = conversionApi.writer.createContainerElement( 'ck-list-separator' ); | ||
|
||
conversionApi.writer.setCustomProperty( 'dataPipeline:transparentRendering', true, viewElement ); | ||
|
||
viewElement.getFillerOffset = () => null; | ||
|
||
return viewElement; | ||
} | ||
} ); | ||
} | ||
} | ||
|
||
/** | ||
* Inserts a `listSeparator` element between two lists of the same type (`ol` + `ol` or `ul` + `ul`). | ||
*/ | ||
function listSeparatorUpcastConverter(): GetCallback<UpcastElementEvent> { | ||
return ( evt, data, conversionApi ) => { | ||
const element: ViewElement = data.viewItem; | ||
const nextSibling = element.nextSibling as ViewElement | null; | ||
|
||
if ( !nextSibling ) { | ||
return; | ||
} | ||
|
||
if ( element.name !== nextSibling.name ) { | ||
return; | ||
} | ||
|
||
if ( !data.modelRange ) { | ||
Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) ); | ||
} | ||
|
||
const writer = conversionApi.writer; | ||
const modelElement = writer.createElement( 'listSeparator' ); | ||
|
||
// Try to insert `listSeparator` element on the current model cursor position. | ||
if ( !conversionApi.safeInsert( modelElement, data.modelCursor ) ) { | ||
return; | ||
} | ||
|
||
const parts = conversionApi.getSplitParts( modelElement ); | ||
|
||
// Extend model range with the range of the created listSeparator element. | ||
data.modelRange = writer.createRange( | ||
data.modelRange!.start, | ||
writer.createPositionAfter( parts[ parts.length - 1 ] ) | ||
); | ||
|
||
conversionApi.updateConversionResult( modelElement, data ); | ||
}; | ||
} |
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
Oops, something went wrong.