mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Refs #2333 Editing json / jsonb / MySQL JSON cells showed one long plain text line, and invalid JSON only failed on Submit. The Value panel and the grid's ⇧Enter window now open the IDE's code editor for JSON cells, pretty-printed (big integers and duplicate keys preserved, large values formatted off the main thread) with highlighting, folding and find; validation as you type blocks Apply only for JSON columns; Reformat; Apply of an unchanged value creates no pending edit, and drafts survive opening the window. XML and bytea modes from #2333 remain. Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com>
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { registerActions } from "@/platform/actions";
|
|
import { setContext } from "@/platform/context";
|
|
|
|
interface ValueEditorActions {
|
|
reformat(): void;
|
|
find(): void;
|
|
openWindow?: () => void;
|
|
readOnly: boolean;
|
|
}
|
|
|
|
const editors = new WeakMap<Element, ValueEditorActions>();
|
|
let active: ValueEditorActions | null = null;
|
|
|
|
function update() {
|
|
setContext("dbJsonValueFocus", !!active);
|
|
setContext("dbJsonValueEditable", !!active && !active.readOnly);
|
|
setContext("dbJsonValueCanOpenWindow", !!active?.openWindow);
|
|
}
|
|
|
|
export function registerValueEditorActions(root: Element, actions: ValueEditorActions): () => void {
|
|
editors.set(root, actions);
|
|
return () => {
|
|
editors.delete(root);
|
|
if (active === actions) active = null;
|
|
update();
|
|
};
|
|
}
|
|
|
|
export function installValueEditorActions(): () => void {
|
|
const onFocus = (event: FocusEvent) => {
|
|
const root = event.target instanceof Element ? event.target.closest("[data-db-value-editor]") : null;
|
|
active = root ? (editors.get(root) ?? null) : null;
|
|
update();
|
|
};
|
|
document.addEventListener("focusin", onFocus);
|
|
const dispose = registerActions([
|
|
{
|
|
id: "db.reformatValue",
|
|
title: "Reformat",
|
|
category: "Database",
|
|
when: "dbJsonValueFocus && dbJsonValueEditable",
|
|
run: () => active?.reformat(),
|
|
},
|
|
{
|
|
id: "db.findInValue",
|
|
title: "Find",
|
|
category: "Database",
|
|
when: "dbJsonValueFocus",
|
|
run: () => active?.find(),
|
|
},
|
|
{
|
|
id: "db.openValueWindow",
|
|
title: "Open in Window",
|
|
category: "Database",
|
|
when: "dbJsonValueCanOpenWindow",
|
|
run: () => active?.openWindow?.(),
|
|
},
|
|
]);
|
|
return () => {
|
|
dispose();
|
|
document.removeEventListener("focusin", onFocus);
|
|
active = null;
|
|
update();
|
|
};
|
|
}
|