Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.

Changing insertion point to always insert at root block #13

@ehausen

Description

@ehausen

I trying to build a system where all patterns are pre-built as elements that should be inserted at page root level. They are basically top level page rows with content.

So I would like to always insert elements at BlockRootClient even if user has selected/clicked somewhere deeper inside than one the outermost one rows.

When the Pattern Explorer modal is open I can run following in the console and it select the insertion point I want:

const ed = wp.data.select('core/block-editor');
wp.data.dispatch('core/block-editor').selectBlock( ed.getBlockHierarchyRootClientId( ed.getSelectedBlockClientIds() ) );

And a selection moved "out" for example:
Page > Row > Section > Paragraph --> Page > Row

But when I tried to hack it to run on a subscribe, like so, the modal closes:

Not working. Modal closes.

wp.data.subscribe( () => { 
    const PatternModal = document.getElementsByClassName("components-modal__frame block-pattern-explorer__modal is-full-screen")[0];    
    //const spinner = document.getElementsByClassName("components-modal__frame block-pattern-explorer__modal is-full-screen .components-spinner")[0]
    const ed = wp.data.select('core/block-editor');
    const numOfParentsBlocks = ed.getBlockParents(ed.getSelectedBlockClientId()).length;
    if(PatternModal && numOfParentsBlocks > 0 ) {  
        console.log("Pattern Modal exist! And number of parents is > 1");
        wp.data.dispatch('core/block-editor').selectBlock( ed.getBlockHierarchyRootClientId( ed.getSelectedBlockClientIds() ) );
    } 

});

I also tried modifying use-insertion-point.js slightly, like so:
const selectedBlockClientId = getBlockRootClientId( getSelectedBlockClientId() );

But got some strange results.

I clearly don't grasp the principles very well.
Could you perhaps point me in the right direction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions