generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c54b74
commit cdbc0c4
Showing
2 changed files
with
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { v4 as uuid } from 'uuid'; | ||
import { | ||
getBlocksFieldname, | ||
getBlocksLayoutFieldname, | ||
getBlocks, | ||
} from '@plone/volto/helpers'; | ||
import { blocks } from '~/config'; | ||
|
||
const columnConfig = { | ||
cloneData(blockData) { | ||
// console.log('column', blockData); | ||
return cloneFormData(blockData); | ||
}, | ||
}; | ||
|
||
function cloneFormData(formData) { | ||
const formBlocks = getBlocks(formData); | ||
|
||
const cloneWithIds = formBlocks | ||
.filter(([id, blockData]) => { | ||
return blockData.blocks ? true : !!blockData['@type']; // support "columns" | ||
}) | ||
.map(([id, blockData]) => { | ||
const blockConfig = | ||
blocks.blocksConfig[blockData['@type']] || columnConfig; | ||
return blockConfig.cloneData | ||
? blockConfig.cloneData(blockData) | ||
: [uuid(), blockData]; | ||
}) | ||
.filter((info) => !!info); // some blocks may refuse to be copied | ||
|
||
const blocksFieldname = getBlocksFieldname(formData); | ||
const blocksLayoutFieldname = getBlocksLayoutFieldname(formData); | ||
|
||
const newBlockData = { | ||
[blocksFieldname]: { | ||
...formData[blocksFieldname], | ||
...Object.assign( | ||
{}, | ||
...cloneWithIds.map(([id, data]) => ({ [id]: data })), | ||
), | ||
}, | ||
[blocksLayoutFieldname]: { | ||
...formData[blocksLayoutFieldname], | ||
items: [...cloneWithIds.map(([id]) => id)], | ||
}, | ||
}; | ||
return [uuid(), newBlockData]; | ||
} | ||
|
||
export function cloneColumnsBlockData(blockData) { | ||
const columnsData = blockData.data; | ||
const cloneWithIds = cloneFormData(columnsData); | ||
|
||
const [id, newBlockData] = cloneWithIds; | ||
return [ | ||
id, | ||
{ | ||
...blockData, | ||
data: newBlockData, | ||
}, | ||
]; | ||
} |