-
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathuseBlockEditor.js
More file actions
92 lines (74 loc) · 2.7 KB
/
useBlockEditor.js
File metadata and controls
92 lines (74 loc) · 2.7 KB
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
/**
* WordPress dependencies
*/
import {__, sprintf} from '@wordpress/i18n';
const useBlockEditor = () => {
const editorSelect = wp.data?.select('core/editor');
const editorDispatch = wp.data?.dispatch('core/editor');
const notices = wp.data?.dispatch('core/notices');
// @todo Use hook instead of savePost override once stable.
if (!window.PodsBlockEditor && editorDispatch && editorDispatch.hasOwnProperty('savePost')) {
// First init.
window.PodsBlockEditor = {
// Store original.
savePost: editorDispatch.savePost,
messages: {},
callbacks: {},
};
// Override the current editor savePost function.
editorDispatch.savePost = async (options) => {
options = options || {};
const pbe = window.PodsBlockEditor;
if (!Object.values(pbe.messages).length) {
notices.removeNotice('pods-validation');
// eslint-disable-next-line no-undef
return pbe.savePost.apply(this, arguments);
}
return new Promise(function (resolve, reject) {
// Bail early if is autosave or preview.
if (options.isAutosave || options.isPreview) {
return resolve('Validation ignored (autosave).');
}
let validationFieldErrorList = [];
for (const fieldName in pbe.messages) {
editorDispatch?.lockPostSaving(fieldName);
let fieldRealName = fieldName.replace('pods-field-', '');
let fieldData = window.PodsDFVAPI.getField(fieldRealName);
validationFieldErrorList.push(fieldData?.fieldConfig?.label ?? realFieldName);
}
notices.createErrorNotice(
sprintf(__('Please complete these fields before saving: %s', 'pods'), validationFieldErrorList.join(', ')),
{id: 'pods-validation', isDismissible: true},
);
for (const fieldCallback in pbe.callbacks) {
if (pbe.callbacks.hasOwnProperty(fieldCallback) && 'function' === typeof pbe.callbacks[fieldCallback]) {
pbe.callbacks[fieldCallback]();
}
}
return reject('Pods validation failed');
});
};
}
return {
data: wp.data,
select: editorSelect,
dispatch: editorDispatch,
notices,
lockPostSaving: (name, messages, callback) => {
// @todo Use hook instead of savePost override once stable.
//wp.hooks.addFilter( 'editor.__unstablePreSavePost', 'editor', filter );
if (messages.length) {
window.PodsBlockEditor.messages[name] = messages;
window.PodsBlockEditor.callbacks[name] = callback;
}
},
unlockPostSaving: (name) => {
// @todo Use hook instead of savePost override once stable.
//wp.hooks.removeFilter( 'editor.__unstablePreSavePost', 'editor', filter );
delete window.PodsBlockEditor.messages[name];
delete window.PodsBlockEditor.callbacks[name];
editorDispatch?.unlockPostSaving(name);
},
};
};
export default useBlockEditor;