-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathuse-site-editor-settings.js
71 lines (64 loc) · 2.19 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
/**
* 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.query.focusMode ||
( location?.params?.postId &&
FOCUSABLE_ENTITIES.includes( location?.params?.postType ) );
const didComeFromEditorCanvas =
previousLocation?.query.canvas === 'edit';
const showBackButton = isFocusMode && didComeFromEditorCanvas;
return showBackButton ? () => history.back() : undefined;
// `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.
}, [ location, history ] );
return goBack;
}
export function useSpecificEditorSettings() {
const { query } = useLocation();
const { canvas = 'view' } = query;
const onNavigateToEntityRecord = useNavigateToEntityRecord();
const { settings } = useSelect( ( select ) => {
const { getSettings } = select( editSiteStore );
return {
settings: getSettings(),
};
}, [] );
const onNavigateToPreviousEntityRecord =
useNavigateToPreviousEntityRecord();
const defaultEditorSettings = useMemo( () => {
return {
...settings,
richEditingEnabled: true,
supportsTemplateMode: true,
focusMode: canvas !== 'view',
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
isPreviewMode: canvas === 'view',
};
}, [
settings,
canvas,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
] );
return defaultEditorSettings;
}