Use codeMirror editor for Custom CSS
[MAILPOET-2455]
This commit is contained in:
committed by
Jack Kitterhing
parent
a1b1f173d2
commit
0ba2e15a1c
@@ -0,0 +1,42 @@
|
|||||||
|
import React, { useEffect, useRef } from 'react';
|
||||||
|
import codemirror from 'codemirror';
|
||||||
|
import 'codemirror/mode/css/css'; // Side effect
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
|
const CodemirrorWrap = ({ options, value, onChange }) => {
|
||||||
|
const textArea = useRef(null);
|
||||||
|
const editor = useRef(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
editor.current = codemirror.fromTextArea(textArea.current, options);
|
||||||
|
editor.current.on('change', (doc) => onChange(doc.getValue()));
|
||||||
|
return () => {
|
||||||
|
if (editor.current) {
|
||||||
|
editor.current.toTextArea();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [options, onChange]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<textarea
|
||||||
|
ref={textArea}
|
||||||
|
name="name"
|
||||||
|
defaultValue={value}
|
||||||
|
autoComplete="off"
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
CodemirrorWrap.propTypes = {
|
||||||
|
value: PropTypes.string.isRequired,
|
||||||
|
onChange: PropTypes.func.isRequired,
|
||||||
|
options: PropTypes.shape({
|
||||||
|
lineNumbers: PropTypes.bool.isRequired,
|
||||||
|
tabMode: PropTypes.string.isRequired,
|
||||||
|
matchBrackets: PropTypes.bool.isRequired,
|
||||||
|
theme: PropTypes.string.isRequired,
|
||||||
|
mode: PropTypes.string.isRequired,
|
||||||
|
}).isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CodemirrorWrap;
|
@@ -1,24 +1,33 @@
|
|||||||
import React from 'react';
|
import React, { useRef } from 'react';
|
||||||
import {
|
import {
|
||||||
Panel,
|
Panel,
|
||||||
PanelBody,
|
PanelBody,
|
||||||
TextareaControl,
|
|
||||||
} from '@wordpress/components';
|
} from '@wordpress/components';
|
||||||
import MailPoet from 'mailpoet';
|
import MailPoet from 'mailpoet';
|
||||||
import { useSelect, useDispatch } from '@wordpress/data';
|
import { useSelect, useDispatch } from '@wordpress/data';
|
||||||
|
import CodeMirror from './codemirror_wrap.jsx';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const styles = useSelect(
|
const styles = useSelect(
|
||||||
(select) => select('mailpoet-form-editor').getFormStyles(),
|
(select) => select('mailpoet-form-editor').getFormStyles(),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
const options = useRef({
|
||||||
|
lineNumbers: true,
|
||||||
|
tabMode: 'indent',
|
||||||
|
matchBrackets: true,
|
||||||
|
theme: 'neo',
|
||||||
|
mode: 'css',
|
||||||
|
});
|
||||||
|
|
||||||
const { changeFormStyles } = useDispatch('mailpoet-form-editor');
|
const { changeFormStyles } = useDispatch('mailpoet-form-editor');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Panel>
|
<Panel>
|
||||||
<PanelBody title={MailPoet.I18n.t('customCss')} initialOpen={false}>
|
<PanelBody title={MailPoet.I18n.t('customCss')} initialOpen={false}>
|
||||||
<TextareaControl
|
<CodeMirror
|
||||||
value={styles}
|
value={styles}
|
||||||
rows={10}
|
options={options.current}
|
||||||
onChange={changeFormStyles}
|
onChange={changeFormStyles}
|
||||||
/>
|
/>
|
||||||
</PanelBody>
|
</PanelBody>
|
||||||
|
Reference in New Issue
Block a user