Repository navigation
Editor: allow selecting which block styles to apply globally #79839
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
904f6f3
Editor: allow selecting which block styles to apply globally
aaronrobertshaw 203a344
Remove the directional chevron from the review modal
aaronrobertshaw 5773194
Extract useReviewRows into its own module to break circular import
aaronrobertshaw 23bbd96
Trim redundant JSDoc from format-style-value constants
aaronrobertshaw 9b28f3b
Replace inline change-row return type with a ChangeRow typedef
aaronrobertshaw f0ea57c
Add PR reference to editor changelog entry
aaronrobertshaw 74b1e13
Tidy review-modal comments and drop vestigial pushChanges default
aaronrobertshaw a089a45
Editor: trim verbose JSDoc in push-changes-to-global-styles
aaronrobertshaw e568969
Editor: consolidate the value-present check in format-style-value
aaronrobertshaw 06cdbe7
Editor: Fix mobile display of apply styles globally modal
aaronrobertshaw 321e274
Editor: fix apply-globally modal footer spacing and content overlap
aaronrobertshaw 36aa83d
Editor: simplify comments in push-to-global-styles
aaronrobertshaw da786c3
Editor: keep all four slots when formatting partial spacing
aaronrobertshaw 9f672f9
Editor: show current preset values by name in apply-globally modal
aaronrobertshaw 0961ac0
Editor: format axial block gap as a readable pair in apply-globally m…
aaronrobertshaw 664cffb
Editor: reflect Global Styles border style in apply-globally modal
aaronrobertshaw 911d47d
Editor: move apply-globally changelog entry to Unreleased
aaronrobertshaw f72c3e4
Editor: humanize CSS custom property preset values in apply-globally …
aaronrobertshaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
packages/editor/src/hooks/push-changes-to-global-styles/apply-globally-modal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Modal } from '@wordpress/components'; | ||
| import { DataViewsPicker } from '@wordpress/dataviews'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { getBlockType } from '@wordpress/blocks'; | ||
| import { useMemo, useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { useGlobalStyles } from '../../components/global-styles/hooks'; | ||
| import { useReviewRows } from './use-review-rows'; | ||
|
|
||
| // Show a simple table with a checkbox at the start of each row. | ||
| const DEFAULT_LAYOUTS = { pickerTable: {} }; | ||
|
|
||
| const getItemId = ( row ) => row.id; | ||
|
|
||
| /** | ||
| * Modal for reviewing a block's changed styles and choosing which ones to apply | ||
| * to every block of the same type. | ||
| * | ||
| * Each style is a row in the table and starts out selected. Apply pushes only | ||
| * the rows that are still selected, and is turned off when none are. Closing | ||
| * the modal or pressing Escape leaves the block untouched. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {string} props.name Block name. | ||
| * @param {Array} props.rows The block's changed styles, grouped into rows. | ||
| * @param {Function} props.onApply Called with the selected rows to apply. | ||
| * @param {Function} props.onRequestClose Called to close the modal. | ||
| */ | ||
| export default function ApplyGloballyModal( { | ||
| name, | ||
| rows, | ||
| onApply, | ||
| onRequestClose, | ||
| } ) { | ||
| const { merged } = useGlobalStyles(); | ||
| const reviewRows = useReviewRows( rows, merged, name ); | ||
|
|
||
| const [ selection, setSelection ] = useState( () => | ||
| reviewRows.map( ( row ) => row.id ) | ||
| ); | ||
| const [ view, setView ] = useState( () => ( { | ||
| type: 'pickerTable', | ||
| titleField: 'label', | ||
| fields: [ 'current', 'new' ], | ||
| page: 1, | ||
| perPage: reviewRows.length, | ||
| layout: { enableMoving: false }, | ||
| } ) ); | ||
|
|
||
| const fields = useMemo( | ||
| () => [ | ||
| { | ||
| id: 'label', | ||
| label: __( 'Style' ), | ||
| enableSorting: false, | ||
| enableHiding: false, | ||
| filterBy: false, | ||
| getValue: ( { item } ) => item.label, | ||
| render: ( { item } ) => item.label, | ||
| }, | ||
| { | ||
| id: 'current', | ||
| label: __( 'Current' ), | ||
| enableSorting: false, | ||
| enableHiding: false, | ||
| filterBy: false, | ||
| getValue: ( { item } ) => item.formattedCurrentValue, | ||
| render: ( { item } ) => ( | ||
| <span className="editor-push-changes-to-global-styles-modal__value"> | ||
| { item.formattedCurrentValue } | ||
| </span> | ||
| ), | ||
| }, | ||
| { | ||
| id: 'new', | ||
| label: __( 'New' ), | ||
| enableSorting: false, | ||
| enableHiding: false, | ||
| filterBy: false, | ||
| getValue: ( { item } ) => item.formattedNewValue, | ||
| render: ( { item } ) => ( | ||
| <span className="editor-push-changes-to-global-styles-modal__value"> | ||
| { item.formattedNewValue } | ||
| </span> | ||
| ), | ||
| }, | ||
| ], | ||
| [] | ||
| ); | ||
|
|
||
| const actions = useMemo( | ||
| () => [ | ||
| { | ||
| id: 'apply', | ||
| label: __( 'Apply' ), | ||
| isPrimary: true, | ||
| supportsBulk: true, | ||
| callback( items ) { | ||
| onApply( items ); | ||
| onRequestClose(); | ||
| }, | ||
| }, | ||
| ], | ||
| [ onApply, onRequestClose ] | ||
| ); | ||
|
|
||
| const blockTitle = getBlockType( name )?.title; | ||
|
|
||
| return ( | ||
| <Modal | ||
| title={ sprintf( | ||
| // translators: %s: Title of the block e.g. 'Heading'. | ||
| __( 'Apply %s styles globally' ), | ||
| blockTitle | ||
| ) } | ||
| onRequestClose={ onRequestClose } | ||
| size="large" | ||
| className="editor-push-changes-to-global-styles-modal" | ||
| > | ||
| <p> | ||
| { sprintf( | ||
| // translators: %s: Title of the block e.g. 'Heading'. | ||
| __( | ||
| 'Choose which styles to make default for all %s blocks.' | ||
| ), | ||
| blockTitle | ||
| ) } | ||
| </p> | ||
| <DataViewsPicker | ||
| data={ reviewRows } | ||
| fields={ fields } | ||
| view={ view } | ||
| onChangeView={ setView } | ||
| actions={ actions } | ||
| selection={ selection } | ||
| onChangeSelection={ setSelection } | ||
| getItemId={ getItemId } | ||
| paginationInfo={ { | ||
| totalItems: reviewRows.length, | ||
| totalPages: 1, | ||
| } } | ||
| defaultLayouts={ DEFAULT_LAYOUTS } | ||
| search={ false } | ||
| itemListLabel={ __( 'Styles to apply' ) } | ||
| > | ||
| <DataViewsPicker.Layout /> | ||
| <DataViewsPicker.Footer /> | ||
| </DataViewsPicker> | ||
| </Modal> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.