Update editor, themes, menus, backgrounds, and release tooling
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import type { editor } from "monaco-editor";
|
||||
import { actionMenu, type ActionMenuItem } from "../ui/actionMenu";
|
||||
|
||||
/** Keep Monaco editing commands in the host's shared action menu. */
|
||||
export function codeMenu(view: editor.IStandaloneCodeEditor, report: (message: string) => void) {
|
||||
let root: HTMLElement | undefined;
|
||||
function close(focus = true) {
|
||||
root?.remove(); root = undefined;
|
||||
if (focus) view.focus();
|
||||
}
|
||||
const outside = (event: MouseEvent) => { if (root && !root.contains(event.target as Node)) close(false); };
|
||||
const scroll = (event: Event) => { if (root && event.target instanceof Node && !root.contains(event.target)) close(false); };
|
||||
const resize = () => close(false);
|
||||
document.addEventListener("mousedown", outside, true);
|
||||
document.addEventListener("scroll", scroll, true);
|
||||
window.addEventListener("resize", resize);
|
||||
function open(x: number, y: number) {
|
||||
close(false);
|
||||
const model = view.getModel(), selection = view.getSelection();
|
||||
if (!model || !selection) return;
|
||||
const version = model.getVersionId();
|
||||
const valid = () => view.getModel() === model && !model.isDisposed() && model.getVersionId() === version && !!view.getSelection()?.equalsRange(selection);
|
||||
const selected = model.getValueInRange(selection);
|
||||
const replace = (text: string) => {
|
||||
if (!valid()) return;
|
||||
view.pushUndoStop();
|
||||
view.executeEdits("code-menu", [{ range: selection, text }]);
|
||||
view.pushUndoStop(); view.focus();
|
||||
};
|
||||
const clipboard = async (kind: "cut" | "copy" | "paste") => {
|
||||
close();
|
||||
try {
|
||||
if (kind === "paste") replace(await navigator.clipboard.readText());
|
||||
else { await navigator.clipboard.writeText(selected); if (kind === "cut") replace(""); }
|
||||
} catch { report("Clipboard unavailable. Use the keyboard shortcut to copy, cut, or paste."); }
|
||||
};
|
||||
const run = (id: string) => { close(); void view.getAction(id)?.run(); };
|
||||
const items: ActionMenuItem[] = [
|
||||
{ label: "Undo", icon: "undo", shortcut: "Ctrl+Z", disabled: !model.canUndo(), run: () => { close(); view.trigger("code-menu", "undo", null); } },
|
||||
{ label: "Redo", icon: "redo", shortcut: "Ctrl+Shift+Z", disabled: !model.canRedo(), run: () => { close(); view.trigger("code-menu", "redo", null); } },
|
||||
{ separator: true },
|
||||
{ label: "Change all occurrences", icon: "select", shortcut: "Ctrl+F2", disabled: !view.getAction("editor.action.changeAll")?.isSupported(), run: () => run("editor.action.changeAll") },
|
||||
{ separator: true },
|
||||
{ label: "Cut", icon: "cut", shortcut: "Ctrl+X", disabled: !selected, run: () => void clipboard("cut") },
|
||||
{ label: "Copy", icon: "copy", shortcut: "Ctrl+C", disabled: !selected, run: () => void clipboard("copy") },
|
||||
{ label: "Paste", icon: "paste", shortcut: "Ctrl+V", run: () => void clipboard("paste") },
|
||||
{ label: "Select all", icon: "select", shortcut: "Ctrl+A", run: () => { close(); view.setSelection(model.getFullModelRange()); } },
|
||||
{ separator: true },
|
||||
{ label: "Command palette", icon: "terminal", shortcut: "F1", run: () => run("editor.action.quickCommand") },
|
||||
];
|
||||
root = actionMenu(x, y, items, close, "Code text actions");
|
||||
}
|
||||
return { open, close, destroy() {
|
||||
close(false);
|
||||
document.removeEventListener("mousedown", outside, true);
|
||||
document.removeEventListener("scroll", scroll, true);
|
||||
window.removeEventListener("resize", resize);
|
||||
} };
|
||||
}
|
||||
Reference in New Issue
Block a user