-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomField.php
83 lines (77 loc) · 3.98 KB
/
CustomField.php
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
<?php
namespace App\Blocks;
class CustomField
{
public static function render()
{
return [
'name' => 'CustomField',
'slug' => 'cb-custom-field',
'icon' => 'block-default', // https://developer.wordpress.org/resource/dashicons/
'fields' => [
[
'name' => 'content',
'label' => 'Content',
'type' => 'custom',
'multilanguage' => true,
'js' => <<<'JS'
const element = React.useRef(null);
const instance = React.useRef(null);
const old = React.useRef(props.activeLang);
const doubleChange = React.useRef();
function loadLibrary() {
return new Promise((resolve, reject) => {
if ($.fn.summernote) {
resolve();
} else {
$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', '/packages/summernote/dist/summernote-bs4.css'));
$('head').append($('<style type="text/css"> .note-editor.note-frame .note-status-output, .note-editor.note-airframe .note-status-output { height: auto; } </style>'));
$.getScript('/packages/summernote/dist/summernote-bs4.min.js', resolve);
}
});
}
React.useEffect(() => {
var summernoteOptions = {
dialogsInBody: true,
tooltip: false,
callbacks: {
onChange: function(contents, $editable) {
if (doubleChange.current) {
doubleChange.current = null;
return;
}
props.onChange(contents);
},
},
};
loadLibrary().then(() => {
$(element.current).summernote(summernoteOptions);
instance.current = $(element.current);
});
return () => {
if (instance.current) instance.current.summernote('destroy');
}
}, [element]);
React.useEffect(() => {
console.log('useeffect old', old.current, 'new', props.activeLang);
if (instance.current && old.current !== props.activeLang) {
let newVal = props.field.multilanguage ? props.value[props.activeLang] : props.value;
doubleChange.current = true; // the next line trigger onchange. this disables it;
instance.current.summernote('code', newVal);
}
old.current = props.activeLang;
}, [props.value, props.activeLang]);
return (
React.createElement('div', {},
React.createElement('textarea', {
ref: element,
value: props.field.multilanguage ? props.value[props.activeLang] : props.value,
})
)
);
JS,
],
],
];
}
}