|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import * as YAML from 'yaml'; |
| 3 | +import { CONFIG_SECTIONS, type ConfigField } from '../../editor/configSchema'; |
| 4 | + |
| 5 | +interface ConfigEditorProps { |
| 6 | + source: string; |
| 7 | + onChange: (source: string) => void; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * settings.yml as a form. |
| 12 | + * |
| 13 | + * Every change is written into the parsed document, so the long comment blocks |
| 14 | + * the plugin ships the file with are still there afterwards. |
| 15 | + */ |
| 16 | +export function ConfigEditor({ source, onChange }: ConfigEditorProps) { |
| 17 | + const document = useMemo(() => { |
| 18 | + try { |
| 19 | + return YAML.parseDocument(source); |
| 20 | + } catch { |
| 21 | + return null; |
| 22 | + } |
| 23 | + }, [source]); |
| 24 | + |
| 25 | + if (document === null || document.errors.length > 0) { |
| 26 | + return ( |
| 27 | + <div className="item-editor-content"> |
| 28 | + <p>This file does not parse as YAML.</p> |
| 29 | + <p>Fix it in the text editor before using the form.</p> |
| 30 | + </div> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + const write = (path: string[], value: unknown): void => { |
| 35 | + const next = YAML.parseDocument(source); |
| 36 | + |
| 37 | + if (value === null || value === '') { |
| 38 | + next.deleteIn(path); |
| 39 | + } else { |
| 40 | + next.setIn(path, value); |
| 41 | + } |
| 42 | + |
| 43 | + onChange(String(next)); |
| 44 | + }; |
| 45 | + |
| 46 | + const read = (field: ConfigField): unknown => document.getIn(field.path); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className="config-visual-editor"> |
| 50 | + <div className="config-visual-header"> |
| 51 | + <div> |
| 52 | + <div className="config-visual-title">Plugin Configuration</div> |
| 53 | + <div className="config-visual-subtitle"> |
| 54 | + Edit settings.yml visually. Comments are shown under each field. |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + <div className="config-visual-badge">settings.yml</div> |
| 58 | + </div> |
| 59 | + |
| 60 | + <div className="config-visual-content"> |
| 61 | + {CONFIG_SECTIONS.map(section => ( |
| 62 | + <div key={section.id} className="settings-section"> |
| 63 | + <div className="config-section-title">{section.title}</div> |
| 64 | + <div className="config-section-description">{section.description}</div> |
| 65 | + |
| 66 | + {section.fields.map(field => ( |
| 67 | + <div key={field.path.join('.')} className="setting-group"> |
| 68 | + <div className="config-field-label">{field.label}</div> |
| 69 | + <div className="config-field-description">{field.description}</div> |
| 70 | + <Control field={field} value={read(field)} onChange={value => write(field.path, value)} /> |
| 71 | + </div> |
| 72 | + ))} |
| 73 | + </div> |
| 74 | + ))} |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function Control({ |
| 81 | + field, |
| 82 | + value, |
| 83 | + onChange, |
| 84 | +}: { |
| 85 | + field: ConfigField; |
| 86 | + value: unknown; |
| 87 | + onChange: (value: unknown) => void; |
| 88 | +}) { |
| 89 | + switch (field.type) { |
| 90 | + case 'toggle': |
| 91 | + return ( |
| 92 | + <label className="editor-row"> |
| 93 | + <input type="checkbox" checked={value === true} onChange={event => onChange(event.target.checked)} /> |
| 94 | + <span>{value === true ? 'Enabled' : 'Disabled'}</span> |
| 95 | + </label> |
| 96 | + ); |
| 97 | + |
| 98 | + case 'select': |
| 99 | + return ( |
| 100 | + <select |
| 101 | + className="inline-input" |
| 102 | + value={String(value ?? '')} |
| 103 | + onChange={event => onChange(event.target.value)} |
| 104 | + aria-label={field.label} |
| 105 | + > |
| 106 | + {(field.options ?? []).map(option => ( |
| 107 | + <option key={option.value} value={option.value}> |
| 108 | + {option.label} |
| 109 | + </option> |
| 110 | + ))} |
| 111 | + </select> |
| 112 | + ); |
| 113 | + |
| 114 | + case 'number': |
| 115 | + return ( |
| 116 | + <input |
| 117 | + type="number" |
| 118 | + className="inline-input" |
| 119 | + min={field.min} |
| 120 | + max={field.max} |
| 121 | + value={typeof value === 'number' ? value : ''} |
| 122 | + onChange={event => onChange(event.target.value === '' ? null : Number(event.target.value))} |
| 123 | + aria-label={field.label} |
| 124 | + /> |
| 125 | + ); |
| 126 | + |
| 127 | + case 'list': |
| 128 | + return ( |
| 129 | + <textarea |
| 130 | + className="inline-input" |
| 131 | + rows={Math.min(10, Math.max(3, toLines(value).length + 1))} |
| 132 | + value={toLines(value).join('\n')} |
| 133 | + onChange={event => onChange(fromLines(event.target.value))} |
| 134 | + aria-label={field.label} |
| 135 | + /> |
| 136 | + ); |
| 137 | + |
| 138 | + default: |
| 139 | + return ( |
| 140 | + <input |
| 141 | + className="inline-input" |
| 142 | + value={typeof value === 'string' ? value : ''} |
| 143 | + placeholder={field.placeholder} |
| 144 | + onChange={event => onChange(event.target.value)} |
| 145 | + aria-label={field.label} |
| 146 | + /> |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +function toLines(value: unknown): string[] { |
| 152 | + if (Array.isArray(value)) { |
| 153 | + return value.map(String); |
| 154 | + } |
| 155 | + |
| 156 | + if (value !== null && typeof value === 'object' && 'toJSON' in value) { |
| 157 | + const plain = (value as { toJSON: () => unknown }).toJSON(); |
| 158 | + |
| 159 | + return Array.isArray(plain) ? plain.map(String) : []; |
| 160 | + } |
| 161 | + |
| 162 | + return []; |
| 163 | +} |
| 164 | + |
| 165 | +function fromLines(text: string): string[] | null { |
| 166 | + const lines = text.split('\n').filter(line => line.trim() !== ''); |
| 167 | + |
| 168 | + return lines.length === 0 ? null : lines; |
| 169 | +} |
0 commit comments