-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
content-functions.js
71 lines (62 loc) · 1.82 KB
/
content-functions.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
window.getBlockEditorStore = () => {
return {
blockEditorSelect:
window.wp.data.select( 'core/block-editor' ) ||
window.wp.data.select( 'core/editor' ), // For WP v5.0 and v5.1.
blockEditorDispatch:
window.wp.data.dispatch( 'core/block-editor' ) ||
window.wp.data.dispatch( 'core/editor' ), // For WP v5.0 and v5.1.
};
};
window.getHTMLPostContent = () => {
const { blockEditorSelect } = window.getBlockEditorStore();
const blocks = blockEditorSelect.getBlocks();
const HTML = window.wp.blocks.serialize( blocks );
// Check if platform is iOS
if ( window.webkit ) {
window.webkit.messageHandlers.htmlPostContent.postMessage( HTML );
// Otherwise it\'s Android
} else {
window.wpwebkit.postMessage( HTML );
}
};
window.insertBlock = ( blockHTML ) => {
// Setup the editor with the inserted block.
const post = window.wp.data.select( 'core/editor' ).getCurrentPost();
window.wp.data
.dispatch( 'core/editor' )
.setupEditor( post, { content: blockHTML } );
window.contentIncerted = true;
};
window.sendGutenbergReadyMessage = () => {
if ( window.webkit ) {
// iOS
window.webkit.messageHandlers.gutenbergReady.postMessage( '' );
} else {
// Android
window.wpwebkit.gutenbergReady();
}
window.readyMessageSent = true;
};
window.isGutenbergReady = () => {
const currentPost = window.wp.data.select( 'core/editor' ).getCurrentPost();
return currentPost.id !== undefined;
};
window.startObservingGutenberg = () => {
if ( window.wp.data && window.subscribed !== true ) {
if ( window.isGutenbergReady() ) {
window.sendGutenbergReadyMessage();
return;
}
const unsubscribe = window.wp.data.subscribe( () => {
if (
window.isGutenbergReady() &&
window.readyMessageSent !== true
) {
unsubscribe();
window.sendGutenbergReadyMessage();
}
} );
window.subscribed = true;
}
};