-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
126 lines (111 loc) · 3.71 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
/**
* External dependencies
*/
import isPromise from 'is-promise';
/**
* WordPress dependencies
*/
import { Popover } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useEffect } from '@wordpress/element';
import { parse, rawHandler } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { BlockEditorProvider } from '@wordpress/block-editor';
import BlockEditorToolbar from '../block-editor-toolbar';
import BlockEditor from '../block-editor';
import getInitialEditorContent from './editor-content';
/** @typedef {import('../../store/editor/reducer').EditorMode} EditorMode */
/** @typedef {import('../../index').BlockEditorSettings} BlockEditorSettings */
/** @typedef {import('../../index').OnLoad} OnLoad */
/** @typedef {import('../../index').OnMore} OnMore */
/**
* Get editor selection
* @callback OnSelection
*/
/**
* Update callback
* @callback OnUpdate
* @param {object[]} blocks - Editor content to save
* @param {object} [options]
*/
async function getInitialContent( settings, loader ) {
const contentLoader = isPromise( loader )
? loader
: new Promise( ( resolve ) => {
resolve( loader ? loader( parse, rawHandler ) : [] );
} );
return contentLoader.then( ( content ) => {
return getInitialEditorContent(
settings.iso.patterns,
settings.iso.currentPattern,
settings.editor.template,
content
);
} );
}
/**
* The editor itself, including toolbar
*
* @param {object} props - Component props
* @param {object[]} props.blocks
* @param {OnUpdate} props.updateBlocksWithoutUndo - Callback to update blocks
* @param {OnUpdate} props.updateBlocksWithUndo - Callback to update blocks
* @param {boolean} props.isEditing - Are we editing in this editor?
* @param {EditorMode} props.editorMode - Visual or code?
* @param {object} props.children - Child components
* @param {BlockEditorSettings} props.settings - Settings
* @param {OnMore} props.renderMoreMenu - Callback to render additional items in the more menu
* @param {OnSelection} props.selection
* @param {OnLoad} props.onLoad - Load initial blocks
*/
function BlockEditorContents( props ) {
const { blocks, updateBlocksWithoutUndo, updateBlocksWithUndo, selection, isEditing, editorMode } = props;
const { children, settings, renderMoreMenu, onLoad } = props;
// Set initial content, if we have any, but only if there is no existing data in the editor (from elsewhere)
useEffect( () => {
const loadData = async () => {
const initialContent = await getInitialContent( settings, onLoad );
if ( initialContent.length > 0 && ( ! blocks || blocks.length === 0 ) ) {
updateBlocksWithoutUndo( initialContent );
}
};
loadData();
}, [] );
return (
<BlockEditorProvider
value={ blocks || [] }
onInput={ updateBlocksWithoutUndo }
onChange={ updateBlocksWithUndo }
useSubRegistry={ false }
selection={ selection }
settings={ settings.editor }
>
<BlockEditorToolbar editorMode={ editorMode } settings={ settings } renderMoreMenu={ renderMoreMenu } />
<BlockEditor isEditing={ isEditing } editorMode={ editorMode }>
{ children }
</BlockEditor>
<Popover.Slot />
</BlockEditorProvider>
);
}
export default compose( [
withSelect( ( select ) => {
const { getBlocks, getEditorSelection, getEditorMode, isEditing } = select( 'isolated/editor' );
return {
blocks: getBlocks(),
selection: getEditorSelection(),
isEditing: isEditing(),
editorMode: getEditorMode(),
};
} ),
withDispatch( ( dispatch ) => {
const { updateBlocksWithUndo, updateBlocksWithoutUndo } = dispatch( 'isolated/editor' );
return {
updateBlocksWithUndo,
updateBlocksWithoutUndo,
};
} ),
] )( BlockEditorContents );