/
index.js
222 lines (204 loc) · 7.6 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import deprecated from '@wordpress/deprecated';
import { createRoot } from '@wordpress/element';
import { dispatch, select } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { store as preferencesStore } from '@wordpress/preferences';
import {
registerLegacyWidgetBlock,
registerWidgetGroupBlock,
} from '@wordpress/widgets';
import {
privateApis as editorPrivateApis,
store as editorStore,
} from '@wordpress/editor';
/**
* Internal dependencies
*/
import './hooks';
import './plugins';
import Editor from './editor';
import { store as editPostStore } from './store';
import { unlock } from './lock-unlock';
const { PluginPostExcerpt: __experimentalPluginPostExcerpt } =
unlock( editorPrivateApis );
/**
* Initializes and returns an instance of Editor.
*
* @param {string} id Unique identifier for editor instance.
* @param {string} postType Post type of the post to edit.
* @param {Object} postId ID of the post to edit.
* @param {?Object} settings Editor settings object.
* @param {Object} initialEdits Programmatic edits to apply initially, to be
* considered as non-user-initiated (bypass for
* unsaved changes prompt).
*/
export function initializeEditor(
id,
postType,
postId,
settings,
initialEdits
) {
const target = document.getElementById( id );
const root = createRoot( target );
dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
isPublishSidebarEnabled: true,
openPanels: [ 'post-status' ],
preferredStyleVariations: {},
themeStyles: true,
welcomeGuide: true,
welcomeGuideTemplate: true,
} );
dispatch( preferencesStore ).setDefaults( 'core', {
allowRightClickOverrides: true,
fixedToolbar: false,
showBlockBreadcrumbs: true,
showIconLabels: false,
showListViewByDefault: false,
} );
dispatch( blocksStore ).reapplyBlockTypeFilters();
// Check if the block list view should be open by default.
// If `distractionFree` mode is enabled, the block list view should not be open.
if (
select( preferencesStore ).get( 'core', 'showListViewByDefault' ) &&
! select( editPostStore ).isFeatureActive( 'distractionFree' )
) {
dispatch( editorStore ).setIsListViewOpened( true );
}
registerCoreBlocks();
registerLegacyWidgetBlock( { inserter: false } );
registerWidgetGroupBlock( { inserter: false } );
if ( process.env.IS_GUTENBERG_PLUGIN ) {
__experimentalRegisterExperimentalCoreBlocks( {
enableFSEBlocks: settings.__unstableEnableFullSiteEditingBlocks,
} );
}
/*
* Prevent adding template part in the post editor.
* Only add the filter when the post editor is initialized, not imported.
* Also only add the filter(s) after registerCoreBlocks()
* so that common filters in the block library are not overwritten.
*/
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'removeTemplatePartsFromInserter',
( canInsert, blockType ) => {
if (
select( editorStore ).getRenderingMode() === 'post-only' &&
blockType.name === 'core/template-part'
) {
return false;
}
return canInsert;
}
);
/*
* Prevent adding post content block (except in query block) in the post editor.
* Only add the filter when the post editor is initialized, not imported.
* Also only add the filter(s) after registerCoreBlocks()
* so that common filters in the block library are not overwritten.
*/
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'removePostContentFromInserter',
(
canInsert,
blockType,
rootClientId,
{ getBlockParentsByBlockName }
) => {
if (
select( editorStore ).getRenderingMode() === 'post-only' &&
blockType.name === 'core/post-content'
) {
return (
getBlockParentsByBlockName( rootClientId, 'core/query' )
.length > 0
);
}
return canInsert;
}
);
// Show a console log warning if the browser is not in Standards rendering mode.
const documentMode =
document.compatMode === 'CSS1Compat' ? 'Standards' : 'Quirks';
if ( documentMode !== 'Standards' ) {
// eslint-disable-next-line no-console
console.warn(
"Your browser is using Quirks Mode. \nThis can cause rendering issues such as blocks overlaying meta boxes in the editor. Quirks Mode can be triggered by PHP errors or HTML code appearing before the opening <!DOCTYPE html>. Try checking the raw page source or your site's PHP error log and resolving errors there, removing any HTML before the doctype, or disabling plugins."
);
}
// This is a temporary fix for a couple of issues specific to Webkit on iOS.
// Without this hack the browser scrolls the mobile toolbar off-screen.
// Once supported in Safari we can replace this in favor of preventScroll.
// For details see issue #18632 and PR #18686
// Specifically, we scroll `interface-interface-skeleton__body` to enable a fixed top toolbar.
// But Mobile Safari forces the `html` element to scroll upwards, hiding the toolbar.
const isIphone = window.navigator.userAgent.indexOf( 'iPhone' ) !== -1;
if ( isIphone ) {
window.addEventListener( 'scroll', ( event ) => {
const editorScrollContainer = document.getElementsByClassName(
'interface-interface-skeleton__body'
)[ 0 ];
if ( event.target === document ) {
// Scroll element into view by scrolling the editor container by the same amount
// that Mobile Safari tried to scroll the html element upwards.
if ( window.scrollY > 100 ) {
editorScrollContainer.scrollTop =
editorScrollContainer.scrollTop + window.scrollY;
}
// Undo unwanted scroll on html element, but only in the visual editor.
if (
document.getElementsByClassName( 'is-mode-visual' )[ 0 ]
) {
window.scrollTo( 0, 0 );
}
}
} );
}
// Prevent the default browser action for files dropped outside of dropzones.
window.addEventListener( 'dragover', ( e ) => e.preventDefault(), false );
window.addEventListener( 'drop', ( e ) => e.preventDefault(), false );
root.render(
<Editor
settings={ settings }
postId={ postId }
postType={ postType }
initialEdits={ initialEdits }
/>
);
return root;
}
/**
* Used to reinitialize the editor after an error. Now it's a deprecated noop function.
*/
export function reinitializeEditor() {
deprecated( 'wp.editPost.reinitializeEditor', {
since: '6.2',
version: '6.3',
} );
}
export { default as PluginBlockSettingsMenuItem } from './components/block-settings-menu/plugin-block-settings-menu-item';
export { default as PluginDocumentSettingPanel } from './components/sidebar/plugin-document-setting-panel';
export { default as PluginMoreMenuItem } from './components/header/plugin-more-menu-item';
export { default as PluginPostPublishPanel } from './components/sidebar/plugin-post-publish-panel';
export { default as PluginPostStatusInfo } from './components/sidebar/plugin-post-status-info';
export { default as PluginPrePublishPanel } from './components/sidebar/plugin-pre-publish-panel';
export { default as PluginSidebar } from './components/sidebar/plugin-sidebar';
export { default as PluginSidebarMoreMenuItem } from './components/header/plugin-sidebar-more-menu-item';
export { default as __experimentalFullscreenModeClose } from './components/header/fullscreen-mode-close';
export { default as __experimentalMainDashboardButton } from './components/header/main-dashboard-button';
export { __experimentalPluginPostExcerpt };
export { store } from './store';