Skip to content

Commit

Permalink
Enable copying of multiple blocks at the same level (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Dec 1, 2018
1 parent c068fe0 commit 6b15c75
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
63 changes: 63 additions & 0 deletions client/src/input/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,55 @@ export default Garnish.Base.extend({
return content
},

getParent(blocks)
{
const level = this.getLevel()
let index = blocks.indexOf(this)
let blockParent = null

if(index >= 0 && level > 0)
{
while(blockParent === null && index > 0)
{
let currentBlock = blocks[--index]
let currentLevel = currentBlock.getLevel()

if(currentLevel === level - 1)
{
blockParent = currentBlock
}
}
}

return blockParent
},

getChildren(blocks, descendants = null)
{
const level = this.getLevel()
let index = blocks.indexOf(this)
let childBlocks = []

if(index >= 0)
{
let currentBlock = blocks[++index]

while(currentBlock && currentBlock.getLevel() > level)
{
let currentLevel = currentBlock.getLevel()

if(descendants ? currentLevel > level : currentLevel === level + 1)
{
childBlocks.push(currentBlock)
}

currentBlock = blocks[++index]
}
}

return childBlocks
},

updatePreview(condensed=null)
{
condensed = typeof condensed === 'boolean' ? condensed : false
Expand Down Expand Up @@ -800,6 +849,20 @@ export default Garnish.Base.extend({
const pasteData = JSON.parse(localStorage.getItem('neo:copy') || '{}')
let pasteDisabled = (!pasteData.blocks || !pasteData.field || pasteData.field !== field)

// Test to see if pasting would exceed the parent's max child blocks
const parentBlock = this.getParent(blocks)
if(!pasteDisabled && parentBlock)
{
const maxChildBlocks = parentBlock.getBlockType().getMaxChildBlocks()

if(maxChildBlocks > 0)
{
const childBlockCount = parentBlock.getChildren(blocks).length
const pasteBlockCount = pasteData.blocks.length
pasteDisabled = pasteDisabled || childBlockCount + pasteBlockCount > maxChildBlocks
}
}

if(!pasteDisabled)
{
const currentBlockTypesById = blocks.reduce((m, b) =>
Expand Down
32 changes: 9 additions & 23 deletions client/src/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,24 +531,11 @@ export default Garnish.Base.extend({

const blocks = this._blocks
const block = blocks[index]
const childBlocks = []
let childBlocks = []

if(block)
{
const level = block.getLevel()

let currentBlock = blocks[++index]
while(currentBlock && currentBlock.getLevel() > level)
{
let currentLevel = currentBlock.getLevel()

if(descendants ? currentLevel > level : currentLevel === level + 1)
{
childBlocks.push(currentBlock)
}

currentBlock = blocks[++index]
}
childBlocks = block.getChildren(blocks, descendants)
}

return childBlocks
Expand Down Expand Up @@ -775,21 +762,20 @@ export default Garnish.Base.extend({

'@copyBlock'(e)
{
// TODO Allow copying of multiple blocks
// There are some issues with the pasting looking jittery, and the max child blocks check not functioning
// correctly. The following code supports copying of multiple blocks, just need to sort out these issues.
/* const blocks = []
// Get the selected blocks and their descendants
const blocks = []
this._blockBatch(e.block, (block) =>
{
// TODO: Allow copying blocks from multiple levels.
// Given that the blocks would be pasted to the same level, the block data collection will need to be
// amended to ensure the correct levels of all blocks, including descendants.
if(block.getLevel() === e.block.getLevel())
{
blocks.push(block, ...this._findChildBlocks(block, true))
}
}) */

const blocks = this._findChildBlocks(e.block, true)
blocks.unshift(e.block)
})

// Collect the relevant data from those blocks to be stored for pasting
const data = {
field: this._name,
blocks: []
Expand Down
Loading

0 comments on commit 6b15c75

Please sign in to comment.