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

Global Styles JSON data inspector #697

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/editor-sidebar/global-styles-json-editor-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* External dependencies
*/
import CodeMirror from '@uiw/react-codemirror';
import { json } from '@codemirror/lang-json';

/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Modal } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

const GlobalStylesJsonEditorModal = ( { onRequestClose } ) => {
const themeName = useSelect( ( select ) =>
select( 'core' ).getCurrentTheme()
)?.name?.raw;

const { record: globalStylesRecord } = useSelect( ( select ) => {
const {
__experimentalGetCurrentGlobalStylesId,
getEditedEntityRecord,
} = select( coreStore );
const globalStylesId = __experimentalGetCurrentGlobalStylesId();
const record = getEditedEntityRecord(
'root',
'globalStyles',
globalStylesId
);
return {
record,
};
} );

const globalStyles = {
...( globalStylesRecord?.styles && {
styles: globalStylesRecord.styles,
} ),
...( globalStylesRecord?.settings && {
settings: globalStylesRecord.settings,
} ),
};

const globalStylesAsString = globalStyles
? JSON.stringify( globalStyles, null, 4 )
: '';

const handleSave = () => {};

return (
<Modal
size="large"
title={ sprintf(
// translators: %s: theme name.
__( 'Custom Styles for %s', 'create-block-theme' ),
themeName
) }
onRequestClose={ onRequestClose }
className="create-block-theme__theme-json-modal"
>
<CodeMirror
extensions={ [ json() ] }
value={ globalStylesAsString }
onChange={ handleSave }
readOnly
/>
</Modal>
);
};

export default GlobalStylesJsonEditorModal;
22 changes: 22 additions & 0 deletions src/plugin-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
*/
import { CreateThemePanel } from './editor-sidebar/create-panel';
import ThemeJsonEditorModal from './editor-sidebar/json-editor-modal';
import GlobalStylesJsonEditorModal from './editor-sidebar/global-styles-json-editor-modal';
import { SaveThemePanel } from './editor-sidebar/save-panel';
import { CreateVariationPanel } from './editor-sidebar/create-variation-panel';
import { ThemeMetadataEditorModal } from './editor-sidebar/metadata-editor-modal';
Expand All @@ -59,6 +60,8 @@ import './plugin-styles.scss';

const CreateBlockThemePlugin = () => {
const [ isEditorOpen, setIsEditorOpen ] = useState( false );
const [ isGlobalStylesEditorOpen, setIsGlobalStylesEditorOpen ] =
useState( false );

const [ isMetadataEditorOpen, setIsMetadataEditorOpen ] = useState( false );

Expand Down Expand Up @@ -155,6 +158,17 @@ const CreateBlockThemePlugin = () => {
'create-block-theme'
) }
</Button>
<Button
icon={ code }
onClick={ () =>
setIsGlobalStylesEditorOpen( true )
}
>
{ __(
'View Custom Styles',
'create-block-theme'
) }
</Button>
<Button
icon={ download }
onClick={ () => handleExportClick() }
Expand Down Expand Up @@ -329,6 +343,14 @@ const CreateBlockThemePlugin = () => {
/>
) }

{ isGlobalStylesEditorOpen && (
<GlobalStylesJsonEditorModal
onRequestClose={ () =>
setIsGlobalStylesEditorOpen( false )
}
/>
) }

{ isMetadataEditorOpen && (
<ThemeMetadataEditorModal
onRequestClose={ () => setIsMetadataEditorOpen( false ) }
Expand Down
Loading