/** * WordPress dependencies */ import { useState, useEffect } from '@safe-wordpress/element'; import type { CSSProperties } from 'react'; /** * External dependencies */ import { langs } from '@uiw/codemirror-extensions-langs'; import CodeMirror, { EditorView, Decoration, StateField, RangeSetBuilder, } from '@uiw/react-codemirror'; import { lintGutter } from '@codemirror/lint'; import { isDefined } from '@nab/utils'; /** * Internal dependencies */ import type { Props } from './props'; export const PhpEditor = ( { className, value, placeholder, before, after, readOnly, onBlur, onChange, onCreateEditor, onFocus, }: Props ): JSX.Element => { const [ buffer, setBuffer ] = useState( '' ); useEffect( () => { if ( buffer !== wrapPhp( value ) ) { setBuffer( wrapPhp( value ) ); } }, [ buffer, value ] ); return ( { setBuffer( wrapPhp( nextBuf ) ); onChange( unwrapPhp( nextBuf ) ); } } onFocus={ onFocus } onBlur={ onBlur } style={ { '--nab-code-before': JSON.stringify( before ), '--nab-code-after': JSON.stringify( after ), } as unknown as CSSProperties } /> ); }; // ======= // HELPERS // ======= function wrapPhp( raw: string ) { if ( ! raw || raw.trimStart().startsWith( '(); const doc = tr.state.doc; const text = doc.toString(); // Hide " EditorView.decorations.from( f ), } );