-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
editor.js
108 lines (98 loc) · 2.65 KB
/
editor.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
ErrorBoundary,
PostLockedModal,
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { useMemo } from '@wordpress/element';
import { SlotFillProvider } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { CommandMenu } from '@wordpress/commands';
/**
* Internal dependencies
*/
import Layout from './components/layout';
import EditorInitialization from './components/editor-initialization';
import { store as editPostStore } from './store';
import { unlock } from './lock-unlock';
import useNavigateToEntityRecord from './hooks/use-navigate-to-entity-record';
const { ExperimentalEditorProvider } = unlock( editorPrivateApis );
function Editor( {
postId: initialPostId,
postType: initialPostType,
settings,
initialEdits,
...props
} ) {
const {
initialPost,
currentPost,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
} = useNavigateToEntityRecord( initialPostId, initialPostType );
const { post, template } = useSelect(
( select ) => {
const { getEditedPostTemplate } = select( editPostStore );
const { getEntityRecord, getPostType, canUser } =
select( coreStore );
const { getEditorSettings } = select( editorStore );
const postObject = getEntityRecord(
'postType',
currentPost.postType,
currentPost.postId
);
const supportsTemplateMode =
getEditorSettings().supportsTemplateMode;
const isViewable =
getPostType( currentPost.postType )?.viewable ?? false;
const canEditTemplate = canUser( 'create', 'templates' );
return {
template:
supportsTemplateMode &&
isViewable &&
canEditTemplate &&
currentPost.postType !== 'wp_template'
? getEditedPostTemplate()
: null,
post: postObject,
};
},
[ currentPost.postType, currentPost.postId ]
);
const editorSettings = useMemo(
() => ( {
...settings,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
defaultRenderingMode: 'post-only',
} ),
[ settings, onNavigateToEntityRecord, onNavigateToPreviousEntityRecord ]
);
if ( ! post ) {
return null;
}
return (
<SlotFillProvider>
<ExperimentalEditorProvider
settings={ editorSettings }
post={ post }
initialEdits={ initialEdits }
useSubRegistry={ false }
__unstableTemplate={ template }
{ ...props }
>
<ErrorBoundary>
<CommandMenu />
<EditorInitialization />
<Layout initialPost={ initialPost } />
</ErrorBoundary>
<PostLockedModal />
</ExperimentalEditorProvider>
</SlotFillProvider>
);
}
export default Editor;