mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-25 23:35:00 +03:00
fix(inbounds): preserve formatting in the advanced JSON editor (#6857)
In the inbound form's Advanced → All editor, pressing Enter or adding any whitespace while the JSON stayed valid made the panel replace the operator's text with its own formatting, dropping the new line and moving the caret. Edits that only change formatting are now left alone, while changes made elsewhere in the form still reach the editor, a tag change patches only the tag, and an edit the form rejects (a non-numeric port, a removed section) is replaced by what the form will actually save, so the editor never shows JSON the panel will not submit.
This commit is contained in:
@@ -157,8 +157,18 @@ const JsonEditor = forwardRef<JsonEditorHandle, JsonEditorProps>(function JsonEd
|
||||
if (!view) return;
|
||||
const current = view.state.doc.toString();
|
||||
if (value === current) return;
|
||||
let from = 0;
|
||||
while (from < current.length && from < value.length && current[from] === value[from]) {
|
||||
from += 1;
|
||||
}
|
||||
let oldEnd = current.length;
|
||||
let newEnd = value.length;
|
||||
while (oldEnd > from && newEnd > from && current[oldEnd - 1] === value[newEnd - 1]) {
|
||||
oldEnd -= 1;
|
||||
newEnd -= 1;
|
||||
}
|
||||
valueRef.current = value;
|
||||
view.dispatch({ changes: { from: 0, to: current.length, insert: value } });
|
||||
view.dispatch({ changes: { from, to: oldEnd, insert: value.slice(from, newEnd) } });
|
||||
}, [value]);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user