-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
use-site-editor-settings.js
86 lines (80 loc) · 2.83 KB
/
use-site-editor-settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { usePrevious } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import useNavigateToEntityRecord from './use-navigate-to-entity-record';
import { FOCUSABLE_ENTITIES } from '../../utils/constants';
const { useLocation, useHistory } = unlock( routerPrivateApis );
function useNavigateToPreviousEntityRecord() {
const location = useLocation();
const previousLocation = usePrevious( location );
const history = useHistory();
const goBack = useMemo( () => {
const isFocusMode =
location.params.focusMode ||
( location.params.postId &&
FOCUSABLE_ENTITIES.includes( location.params.postType ) );
const didComeFromEditorCanvas =
previousLocation?.params.canvas === 'edit';
const showBackButton = isFocusMode && didComeFromEditorCanvas;
return showBackButton ? () => history.back() : undefined;
// Disable reason: previousLocation changes when the component updates for any reason, not
// just when location changes. Until this is fixed we can't add it to deps. See
// https://github.com/WordPress/gutenberg/pull/58710#discussion_r1479219465.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ location, history ] );
return goBack;
}
export function useSpecificEditorSettings() {
const { params } = useLocation();
const { canvasMode = 'view' } = params;
const onNavigateToEntityRecord = useNavigateToEntityRecord();
const { settings, shouldUseTemplateAsDefaultRenderingMode } = useSelect(
( select ) => {
const { getEditedPostContext, getSettings } = unlock(
select( editSiteStore )
);
const _context = getEditedPostContext();
return {
settings: getSettings(),
// TODO: The `postType` check should be removed when the default rendering mode per post type is merged.
// @see https://github.com/WordPress/gutenberg/pull/62304/
shouldUseTemplateAsDefaultRenderingMode:
_context?.postId && _context?.postType !== 'post',
};
},
[]
);
const defaultRenderingMode = shouldUseTemplateAsDefaultRenderingMode
? 'template-locked'
: 'post-only';
const onNavigateToPreviousEntityRecord =
useNavigateToPreviousEntityRecord();
const defaultEditorSettings = useMemo( () => {
return {
...settings,
richEditingEnabled: true,
supportsTemplateMode: true,
focusMode: canvasMode !== 'view',
defaultRenderingMode,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
__unstableIsPreviewMode: canvasMode === 'view',
};
}, [
settings,
canvasMode,
defaultRenderingMode,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
] );
return defaultEditorSettings;
}