Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
josephfusco committed May 14, 2024
1 parent 3fc123b commit f061bb3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@wordpress/data": "^9.22.0",
"@wordpress/element": "^5.23.0",
"@wordpress/hooks": "^3.49.0",
"@wordpress/i18n": "^4.57.0",
"@wordpress/icons": "^9.43.0",
"copy-to-clipboard": "^3.3.3",
"graphiql": "^3.0.10",
Expand Down
28 changes: 28 additions & 0 deletions src/regions/document-editor/components/EditorToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,38 @@ export const EditorToolbar = () => {
{ Object.entries( buttons ).map( ( [ key, button ] ) => {
const props = button.config();

const buttonName = buttons[key].name ?? key;

if ( ! isValidButton(props, buttonName) ) {
return null;
}

// If a component is provided, use it, otherwise use the default ToolbarButton
const Component = props.component || ToolbarButton;
return <Component key={ key } { ...props } />;
} ) }
</>
);
};

const isValidButton = ( config, name ) => {
let hasError = false;
if ( undefined === config.label ) {
console.warn( `Button "${name}" needs a "label" defined`, { config } );
hasError = true;
}
if ( undefined === config.children ) {
console.warn( `Button "${name}" needs "children" defined`, { config } );
hasError = true;
}
if ( undefined === config.onClick ) {
console.warn( `Button "${name}" needs "onClick" defined`, { config } );
hasError = true;
}

if ( hasError ) {
return false;
}

return true;
}
9 changes: 9 additions & 0 deletions src/registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ export const init = () => {
mergeFragmentsButton
);
registerDocumentEditorToolbarButton( 'copy-query', copyQueryButton );
registerDocumentEditorToolbarButton( 'invalid-button', () => {
return {
title: 'MyButton',
notOnClick: () => {
console.log( 'I meant to type onClick!' );
},
child: <h2>I meant to type children</h2>,
};
} );
};

0 comments on commit f061bb3

Please sign in to comment.