When working on our current project, we where trying to build a preset mechanism (which is working fine but too dev to be shared).
One thing we had in mind is being able to add entry in context menus.
As an example, at block level, having a custom entry in context menu that could then allow us to create popin, do copy action, or whatever… could be something magic!
In our case, being able to add an entry in Block context menu saying “Copy block structure to clipboard” would be something perfect.
Regarding this feature, do you think there is a way to get the Modular Block JSON structure just like you do with “Copy to clipboard” in localstorage ?
What I would like to do is pretty simple:
The user selects 2 Modular blocks he’s created
Then, I would like to add a new button in the global context menu bar “Copy selection”:
In executeFieldDropDownAction(), you can use ctx.formValues to get the block values:
switch (actionId) {
case "copyAsJson": {
// This only works on HTTPS, so won't work on localhost during dev unless you add HTTPS to the vite server
const stringifiedCtx = JSON.stringify(ctx, null, 2)
await navigator.clipboard.writeText(stringifiedCtx)
console.log(ctx.formValues['modular_content_field'])
await ctx.notice("Copied to clipboard")
break;
}
default: {
}
}
As far as I can tell, it has no way to access the selection state of the multi-block selector
Even if you could get that from the clientside somehow, cross-frame security restrictions won’t let you easily pass it to the plugin =/
If this functionality has to be in a plugin, you have to render your own UI for filtering/selecting the blocks, separate from the native multi-block selector. It could list (jankily) some of the block fields in a multi-select field, for example, and copy only those… https://www.datocms.com/docs/plugin-sdk/form
Alternative idea… instead of a plugin, might be easier for your editors to just use a bookmarklet? Like: