Explorar o código

Changed: feature is using `engine.model.Selection#getSelectedBlocks` instead of it's own algorithm. Removed src/utils.js.

Szymon Cofalik %!s(int64=8) %!d(string=hai) anos
pai
achega
45593fa50f

+ 2 - 2
packages/ckeditor5-list/src/indentcommand.js

@@ -8,7 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command/command';
-import { getClosestListItem } from './utils';
+import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
  * The list indent command. It is used by the {@link module:list/list~List list feature}.
@@ -97,7 +97,7 @@ export default class IndentCommand extends Command {
 	 */
 	_checkEnabled() {
 		// Check whether any of position's ancestor is a list item.
-		const listItem = getClosestListItem( this.editor.document.selection.getFirstPosition() );
+		const listItem = first( this.editor.document.selection.getSelectedBlocks() );
 
 		// If selection is not in a list item, the command is disabled.
 		if ( !listItem ) {

+ 2 - 4
packages/ckeditor5-list/src/listcommand.js

@@ -9,7 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command/command';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
-import { getClosestListItem } from './utils';
+import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
  * The list command. It is used by the {@link module:list/list~List list feature}.
@@ -56,10 +56,8 @@ export default class ListCommand extends Command {
 	 * Sets command's value based on the document selection.
 	 */
 	refreshValue() {
-		const position = this.editor.document.selection.getFirstPosition();
-
 		// Check whether closest `listItem` ancestor of the position has a correct type.
-		const listItem = getClosestListItem( position );
+		const listItem = first( this.editor.document.selection.getSelectedBlocks() );
 		this.value = listItem !== null && listItem.getAttribute( 'type' ) == this.type;
 	}
 

+ 3 - 0
packages/ckeditor5-list/src/listengine.js

@@ -40,6 +40,9 @@ export default class ListEngine extends Plugin {
 		const editor = this.editor;
 
 		// Schema.
+		// Note: in case `$block` will be ever allowed in `listItem`, keep in mind that this feature
+		// uses `Selection#getSelectedBlocks()` without any additional processing to obtain all selected list items.
+		// If there are blocks allowed inside list item, algorithms using `getSelectedBlocks()` will have to be modified.
 		const schema = editor.document.schema;
 		schema.registerItem( 'listItem', '$block' );
 		schema.allow( {

+ 0 - 22
packages/ckeditor5-list/src/utils.js

@@ -1,22 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/**
- * Utilities used in modules from {@link module:list/list list} package.
- *
- * @module list/utils
- */
-
-/**
- * For given {@link module:engine/model/position~Position position}, returns the closest ancestor of that position which is a
- * `listItem` element.
- *
- * @param {module:engine/model/position~Position} position Position which ancestor should be check looking for `listItem` element.
- * @returns {module:engine/model/element~Element|null} Element with `listItem` name that is a closest ancestor of given `position`, or
- * `null` if neither of `position` ancestors is a `listItem`.
- */
-export function getClosestListItem( position ) {
-	return Array.from( position.getAncestors() ).find( ( parent ) => parent.name == 'listItem' ) || null;
-}

+ 2 - 0
packages/ckeditor5-list/tests/indentcommand.js

@@ -21,9 +21,11 @@ describe( 'IndentCommand', () => {
 		root = doc.createRoot();
 
 		doc.schema.registerItem( 'listItem', '$block' );
+		doc.schema.registerItem( 'paragraph', '$block' );
 
 		doc.schema.allow( { name: '$block', inside: '$root' } );
 		doc.schema.allow( { name: 'listItem', attributes: [ 'type', 'indent' ], inside: '$root' } );
+		doc.schema.allow( { name: 'paragraph', inside: '$root' } );
 
 		setData(
 			doc,

+ 0 - 22
packages/ckeditor5-list/tests/utils.js

@@ -1,22 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-import { getClosestListItem } from '../src/utils';
-
-import Element from '@ckeditor/ckeditor5-engine/src/model/element';
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
-
-describe( 'getClosestListItem', () => {
-	const item = new Element( 'listItem', null, 'foobar' );
-	const root = new Element( '$root', null, [ item ] );
-
-	it( 'should return model listItem element if given position is in such element', () => {
-		expect( getClosestListItem( Position.createAt( item ) ) ).to.equal( item );
-	} );
-
-	it( 'should return null if position is not in listItem', () => {
-		expect( getClosestListItem( Position.createAt( root ) ) ).to.be.null;
-	} );
-} );