Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Move getTopMostBlocks() function to Selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Dec 21, 2018
1 parent 64abfeb commit 79b0f40
Showing 1 changed file with 2 additions and 48 deletions.
50 changes: 2 additions & 48 deletions src/blockquotecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class BlockQuoteCommand extends Command {
const schema = model.schema;
const selection = model.document.selection;

const blocks = getTopMostBlocks( selection, schema );
const blocks = Array.from( selection.getTopMostBlocks() );

model.change( writer => {
if ( this.value ) {
Expand All @@ -69,10 +69,9 @@ export default class BlockQuoteCommand extends Command {
* @returns {Boolean} The current value.
*/
_getValue() {
const schema = this.editor.model.schema;
const selection = this.editor.model.document.selection;

const firstBlock = getTopMostBlocks( selection, schema ).shift();
const firstBlock = first( selection.getTopMostBlocks() );

// In the current implementation, the block quote must be an immediate parent of a block element.
return !!( firstBlock && findQuote( firstBlock ) );
Expand Down Expand Up @@ -227,48 +226,3 @@ function checkCanBeQuoted( schema, block ) {

return isBQAllowed && isBlockAllowedInBQ;
}

// Returns top-level block elements from selected blocks.
//
// For given selection:
//
// [<blockA></blockA>
// <blockB>
// <blockC></blockC>
// <blockD></blockD>
// </blockB>
// <blockE></blockE>]
//
// This method will only return blocks: A, B & E.
//
// @param {module:engine/model/selection~Selection} selection
// @param {module:engine/model/schema~Schema} schema
// @returns {module:engine/model/element~Element[]}
function getTopMostBlocks( selection, schema ) {
const topMostBlocks = Array.from( selection.getSelectedBlocks() )
.filter( block => {
const parentBlock = findAncestorBlock( block, schema );

// Filter out blocks that are nested in other selected blocks (like paragraphs in tables).
return !parentBlock || !Array.from( selection.getSelectedBlocks() ).includes( parentBlock );
} );

return topMostBlocks;
}

// Returns first ancestor block of a node.
//
// @param {module:engine/model/node~Node} node
// @param {module:engine/model/schema~Schema} schema
// @returns {module:engine/model/node~Node|undefined}
function findAncestorBlock( node, schema ) {
let parent = node.parent;

while ( parent ) {
if ( schema.isBlock( parent ) ) {
return parent;
}

parent = parent.parent;
}
}

0 comments on commit 79b0f40

Please sign in to comment.