-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Share the editor settings between the post and site editors #55970
Changes from all commits
57c47cd
e278a66
85daca8
b2fe1e3
336e8b3
af5b956
d554abc
6e5af82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,16 @@ | |
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { privateApis as editorPrivateApis } from '@wordpress/editor'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
import inserterMediaCategories from './inserter-media-categories'; | ||
|
||
const { useBlockEditorSettings } = unlock( editorPrivateApis ); | ||
|
||
function useArchiveLabel( templateSlug ) { | ||
const taxonomyMatches = templateSlug?.match( | ||
|
@@ -85,40 +89,25 @@ function useArchiveLabel( templateSlug ) { | |
|
||
export default function useSiteEditorSettings() { | ||
const { setIsInserterOpened } = useDispatch( editSiteStore ); | ||
const { storedSettings, canvasMode, templateType, siteSettings } = | ||
useSelect( | ||
( select ) => { | ||
const { canUser, getEntityRecord } = select( coreStore ); | ||
const { getSettings, getCanvasMode, getEditedPostType } = | ||
unlock( select( editSiteStore ) ); | ||
return { | ||
storedSettings: getSettings( setIsInserterOpened ), | ||
canvasMode: getCanvasMode(), | ||
templateType: getEditedPostType(), | ||
siteSettings: canUser( 'read', 'settings' ) | ||
? getEntityRecord( 'root', 'site' ) | ||
: undefined, | ||
}; | ||
}, | ||
[ setIsInserterOpened ] | ||
); | ||
|
||
const settingsBlockPatterns = | ||
storedSettings.__experimentalAdditionalBlockPatterns ?? // WP 6.0 | ||
storedSettings.__experimentalBlockPatterns; // WP 5.9 | ||
const settingsBlockPatternCategories = | ||
storedSettings.__experimentalAdditionalBlockPatternCategories ?? // WP 6.0 | ||
storedSettings.__experimentalBlockPatternCategories; // WP 5.9 | ||
|
||
const { | ||
restBlockPatterns, | ||
restBlockPatternCategories, | ||
templateSlug, | ||
userPatternCategories, | ||
focusMode, | ||
isDistractionFree, | ||
hasFixedToolbar, | ||
keepCaretInsideBlock, | ||
canvasMode, | ||
settings, | ||
postType, | ||
postId, | ||
} = useSelect( ( select ) => { | ||
const { getEditedPostType, getEditedPostId } = select( editSiteStore ); | ||
const { getEditedEntityRecord, getUserPatternCategories } = | ||
select( coreStore ); | ||
const { | ||
getEditedPostType, | ||
getEditedPostId, | ||
getCanvasMode, | ||
getSettings, | ||
} = unlock( select( editSiteStore ) ); | ||
const { get: getPreference } = select( preferencesStore ); | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const usedPostType = getEditedPostType(); | ||
const usedPostId = getEditedPostId(); | ||
const _record = getEditedEntityRecord( | ||
|
@@ -127,75 +116,53 @@ export default function useSiteEditorSettings() { | |
usedPostId | ||
); | ||
return { | ||
restBlockPatterns: select( coreStore ).getBlockPatterns(), | ||
restBlockPatternCategories: | ||
select( coreStore ).getBlockPatternCategories(), | ||
templateSlug: _record.slug, | ||
userPatternCategories: getUserPatternCategories(), | ||
focusMode: !! getPreference( 'core/edit-site', 'focusMode' ), | ||
isDistractionFree: !! getPreference( | ||
'core/edit-site', | ||
'distractionFree' | ||
), | ||
hasFixedToolbar: !! getPreference( | ||
'core/edit-site', | ||
'fixedToolbar' | ||
), | ||
keepCaretInsideBlock: !! getPreference( | ||
'core/edit-site', | ||
'keepCaretInsideBlock' | ||
), | ||
canvasMode: getCanvasMode(), | ||
settings: getSettings(), | ||
postType: usedPostType, | ||
postId: usedPostId, | ||
}; | ||
}, [] ); | ||
const archiveLabels = useArchiveLabel( templateSlug ); | ||
|
||
const blockPatterns = useMemo( | ||
() => | ||
[ | ||
...( settingsBlockPatterns || [] ), | ||
...( restBlockPatterns || [] ), | ||
] | ||
.filter( | ||
( x, index, arr ) => | ||
index === arr.findIndex( ( y ) => x.name === y.name ) | ||
) | ||
.filter( ( { postTypes } ) => { | ||
return ( | ||
! postTypes || | ||
( Array.isArray( postTypes ) && | ||
postTypes.includes( templateType ) ) | ||
); | ||
} ), | ||
[ settingsBlockPatterns, restBlockPatterns, templateType ] | ||
); | ||
|
||
const blockPatternCategories = useMemo( | ||
() => | ||
[ | ||
...( settingsBlockPatternCategories || [] ), | ||
...( restBlockPatternCategories || [] ), | ||
].filter( | ||
( x, index, arr ) => | ||
index === arr.findIndex( ( y ) => x.name === y.name ) | ||
), | ||
[ settingsBlockPatternCategories, restBlockPatternCategories ] | ||
); | ||
return useMemo( () => { | ||
const { | ||
__experimentalAdditionalBlockPatterns, | ||
__experimentalAdditionalBlockPatternCategories, | ||
focusMode, | ||
...restStoredSettings | ||
} = storedSettings; | ||
|
||
const defaultEditorSettings = useMemo( () => { | ||
return { | ||
...restStoredSettings, | ||
inserterMediaCategories, | ||
__experimentalBlockPatterns: blockPatterns, | ||
__experimentalBlockPatternCategories: blockPatternCategories, | ||
__experimentalUserPatternCategories: userPatternCategories, | ||
...settings, | ||
|
||
__experimentalSetIsInserterOpened: setIsInserterOpened, | ||
focusMode: canvasMode === 'view' && focusMode ? false : focusMode, | ||
isDistractionFree, | ||
hasFixedToolbar, | ||
keepCaretInsideBlock, | ||
|
||
// I wonder if they should be set in the post editor too | ||
__experimentalArchiveTitleTypeLabel: archiveLabels.archiveTypeLabel, | ||
__experimentalArchiveTitleNameLabel: archiveLabels.archiveNameLabel, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two settings are added by the site editor but I wonder if they make sense in the post editor. (Imagine you're using the post editor to edit the archive template, which is possible if you switch the I think also regardless of whether it's possible or not to actually access the page right now, we just have to think about this setting, why it's only defined in site editor but not post editor. cc @ntsekouras There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't added these, but it seems they are used by
I wasn't aware of that, and I thought there is no way you can edit such a template in post editor. I think that's the reasoning it was only defined in site editor. |
||
pageOnFront: siteSettings?.page_on_front, | ||
pageForPosts: siteSettings?.page_for_posts, | ||
}; | ||
}, [ | ||
storedSettings, | ||
blockPatterns, | ||
blockPatternCategories, | ||
userPatternCategories, | ||
settings, | ||
setIsInserterOpened, | ||
focusMode, | ||
isDistractionFree, | ||
hasFixedToolbar, | ||
keepCaretInsideBlock, | ||
canvasMode, | ||
archiveLabels.archiveTypeLabel, | ||
archiveLabels.archiveNameLabel, | ||
siteSettings?.page_on_front, | ||
siteSettings?.page_for_posts, | ||
] ); | ||
|
||
return useBlockEditorSettings( defaultEditorSettings, postType, postId ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I'm trying to mimic exactly what the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a lot of moving parts here. All the above already exists in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I checked all the settings one by one. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the existing hook that was already present in the "editor" package that I exposed temporarily to share the logic. (later I'll remove it once the site editor is able to use EditorProvider directly).