-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
206 lines (191 loc) · 6.96 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
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import { render, unmountComponentAtNode } from '@wordpress/element';
import { dispatch, select } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { store as preferencesStore } from '@wordpress/preferences';
/**
* Internal dependencies
*/
import './hooks';
import './plugins';
import Editor from './editor';
import { store as editPostStore } from './store';
/**
* Reinitializes the editor after the user chooses to reboot the editor after
* an unhandled error occurs, replacing previously mounted editor element using
* an initial state from prior to the crash.
*
* @param {Object} postType Post type of the post to edit.
* @param {Object} postId ID of the post to edit.
* @param {Element} target DOM node in which editor is rendered.
* @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 reinitializeEditor(
postType,
postId,
target,
settings,
initialEdits
) {
unmountComponentAtNode( target );
const reboot = reinitializeEditor.bind(
null,
postType,
postId,
target,
settings,
initialEdits
);
render(
<Editor
settings={ settings }
onError={ reboot }
postId={ postId }
postType={ postType }
initialEdits={ initialEdits }
recovery
/>,
target
);
}
/**
* 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
) {
// Prevent adding template part in the post editor.
// Only add the filter when the post editor is initialized, not imported.
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'removeTemplatePartsFromInserter',
( can, blockType ) => {
if (
! select( editPostStore ).isEditingTemplate() &&
blockType.name === 'core/template-part'
) {
return false;
}
return can;
}
);
const target = document.getElementById( id );
const reboot = reinitializeEditor.bind(
null,
postType,
postId,
target,
settings,
initialEdits
);
dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fixedToolbar: false,
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
isPublishSidebarEnabled: true,
openPanels: [ 'post-status' ],
preferredStyleVariations: {},
showBlockBreadcrumbs: true,
showIconLabels: false,
showListViewByDefault: false,
themeStyles: true,
welcomeGuide: true,
welcomeGuideTemplate: true,
} );
dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();
// Check if the block list view should be open by default.
if ( select( editPostStore ).isFeatureActive( 'showListViewByDefault' ) ) {
dispatch( editPostStore ).setIsListViewOpened( true );
}
registerCoreBlocks();
if ( process.env.IS_GUTENBERG_PLUGIN ) {
__experimentalRegisterExperimentalCoreBlocks( {
enableFSEBlocks: settings.__unstableEnableFullSiteEditingBlocks,
} );
}
// 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 );
render(
<Editor
settings={ settings }
onError={ reboot }
postId={ postId }
postType={ postType }
initialEdits={ initialEdits }
/>,
target
);
}
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 { store } from './store';