Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing insertion point to always insert at root block #13

Open
ehausen opened this issue May 12, 2022 · 0 comments
Open

Changing insertion point to always insert at root block #13

ehausen opened this issue May 12, 2022 · 0 comments

Comments

@ehausen
Copy link

ehausen commented May 12, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant