import { createSignal, onMount, onCleanup, createEffect, splitProps, mergeProps } from 'solid-js'; export default function CodeEditor(props) { const merged = mergeProps( { value: '', language: 'javascript', readonly: false, placeholder: '', lines: 4, }, props ); const [local, others] = splitProps(merged, [ 'value', 'language', 'readonly', 'placeholder', 'lines', 'onInput', 'onChange', 'onClick', 'class', ]); let textareaRef; let editorInstance; const initializeEditor = () => { if (!textareaRef || !window.wp || !window.wp.codeEditor) { return; } const settings = { codemirror: { mode: local.language, lineNumbers: true, indentUnit: 2, tabSize: 2, indentWithTabs: false, lineWrapping: true, readOnly: local.readonly ? 'nocursor' : false, theme: 'default', extraKeys: { 'Ctrl-Space': 'autocomplete', }, }, }; try { editorInstance = window.wp.codeEditor.initialize(textareaRef, settings); if (editorInstance && editorInstance.codemirror) { // Calculate height based on lines prop (~25px per line) const height = local.lines * 25; editorInstance.codemirror.setSize(null, height); // Make the wrapper resizable const wrapper = editorInstance.codemirror.getWrapperElement(); if (wrapper) { wrapper.style.resize = 'vertical'; wrapper.style.overflow = 'auto'; wrapper.style.minHeight = height + 'px'; } editorInstance.codemirror.on('change', (cm) => { const value = cm.getValue(); local.onInput?.(value); local.onChange?.(value); }); } } catch (error) { console.error('Failed to initialize code editor:', error); } }; createEffect(() => { if (editorInstance && editorInstance.codemirror) { const currentValue = editorInstance.codemirror.getValue(); if (currentValue !== local.value) { editorInstance.codemirror.setValue(local.value || ''); } } }); createEffect(() => { const isReadonly = props.readonly; if (editorInstance && editorInstance.codemirror) { editorInstance.codemirror.setOption('readOnly', isReadonly ? 'nocursor' : false); } }); onMount(() => { if (window.wp && window.wp.codeEditor) { initializeEditor(); } else { const checkCodeEditor = setInterval(() => { if (window.wp && window.wp.codeEditor) { clearInterval(checkCodeEditor); initializeEditor(); } }, 100); setTimeout(() => clearInterval(checkCodeEditor), 5000); } }); onCleanup(() => { if (editorInstance && editorInstance.codemirror) { editorInstance.codemirror.toTextArea(); } }); return (