-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
166 lines (139 loc) · 4 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
/**
* External dependencies
*/
import { I18nManager } from 'react-native';
/**
* Internal dependencies
*/
import './globals';
import { getTranslation } from '../i18n-cache';
import initialHtml from './initial-html';
import setupApiFetch from './api-fetch-setup';
/**
* WordPress dependencies
*/
import {
validateThemeColors,
validateThemeGradients,
} from '@wordpress/block-editor';
import { dispatch } from '@wordpress/data';
import { unregisterBlockType } from '@wordpress/blocks';
const reactNativeSetup = () => {
// Disable warnings as they disrupt the user experience in dev mode
// eslint-disable-next-line no-console
console.disableYellowBox = true;
I18nManager.forceRTL( false ); // Change to `true` to debug RTL layout easily.
};
const gutenbergSetup = () => {
const wpData = require( '@wordpress/data' );
// wp-data
const userId = 1;
const storageKey = 'WP_DATA_USER_' + userId;
wpData.use( wpData.plugins.persistence, { storageKey } );
setupApiFetch();
const isHermes = () => global.HermesInternal !== null;
// eslint-disable-next-line no-console
console.log( 'Hermes is: ' + isHermes() );
setupInitHooks();
const initializeEditor = require( '@wordpress/edit-post' ).initializeEditor;
initializeEditor( 'gutenberg', 'post', 1 );
};
const setupInitHooks = () => {
const wpHooks = require( '@wordpress/hooks' );
wpHooks.addAction(
'native.pre-render',
'core/react-native-editor',
( props ) => {
setupLocale( props.locale, props.translations );
const capabilities = props.capabilities ?? {};
if ( capabilities.reusableBlock !== true ) {
unregisterBlockType( 'core/block' );
}
}
);
// Map native props to Editor props
// TODO: normalize props in the bridge (So we don't have to map initialData to initialHtml)
wpHooks.addFilter(
'native.block_editor_props',
'core/react-native-editor',
( props ) => {
const { capabilities = {} } = props;
let {
initialData,
initialTitle,
postType,
featuredImageId,
colors,
gradients,
} = props;
if ( initialData === undefined && __DEV__ ) {
initialData = initialHtml;
}
if ( initialTitle === undefined ) {
initialTitle = 'Welcome to Gutenberg!';
}
if ( postType === undefined ) {
postType = 'post';
}
colors = validateThemeColors( colors );
gradients = validateThemeGradients( gradients );
return {
initialHtml: initialData,
initialHtmlModeEnabled: props.initialHtmlModeEnabled,
initialTitle,
postType,
featuredImageId,
capabilities,
colors,
gradients,
};
}
);
wpHooks.addAction(
'native.render',
'core/react-native-editor',
( props ) => {
const isAudioBlockEnabled =
props.capabilities && props.capabilities.audioBlock;
if ( isAudioBlockEnabled === true ) {
dispatch( 'core/edit-post' ).showBlockTypes( [ 'core/audio' ] );
} else {
dispatch( 'core/edit-post' ).hideBlockTypes( [ 'core/audio' ] );
}
}
);
};
let blocksRegistered = false;
const setupLocale = ( locale, extraTranslations ) => {
const setLocaleData = require( '@wordpress/i18n' ).setLocaleData;
I18nManager.forceRTL( false ); // Change to `true` to debug RTL layout easily.
let gutenbergTranslations = getTranslation( locale );
if ( locale && ! gutenbergTranslations ) {
// Try stripping out the regional
locale = locale.replace( /[-_][A-Za-z]+$/, '' );
gutenbergTranslations = getTranslation( locale );
}
const translations = Object.assign(
{},
gutenbergTranslations,
extraTranslations
);
// eslint-disable-next-line no-console
console.log( 'locale', locale, translations );
// Only change the locale if it's supported by gutenberg
if ( gutenbergTranslations || extraTranslations ) {
setLocaleData( translations );
}
if ( blocksRegistered ) {
return;
}
const registerCoreBlocks = require( '@wordpress/block-library' )
.registerCoreBlocks;
registerCoreBlocks();
blocksRegistered = true;
};
export { initialHtml as initialHtmlGutenberg };
export function doGutenbergNativeSetup() {
reactNativeSetup();
gutenbergSetup();
}