-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 0.16 New field type:
custom
for custom js field
- Loading branch information
Andrei Telteu
committed
Oct 14, 2024
1 parent
7965fcb
commit 28b4d54
Showing
7 changed files
with
1,509 additions
and
1,342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
{!! isset($content) ? $content : '' !!} |
Oops, something went wrong.