A browser based code editor that powers Visual Studio Code
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core editor creation, configuration, and management functionality for Monaco Editor instances.
monaco.editor.create(
domElement: HTMLElement,
options?: IStandaloneEditorConstructionOptions,
override?: IEditorOverrideServices
): IStandaloneCodeEditorCreates a standalone code editor instance.
Parameters:
domElement: HTML element to attach the editor tooptions: Editor configuration options (optional)override: Service overrides (optional)Returns: IStandaloneCodeEditor instance
const editor = monaco.editor.create(document.getElementById('container'), {
value: 'function hello() {\n\tconsole.log("Hello world!");\n}',
language: 'javascript',
theme: 'vs-dark',
readOnly: false,
automaticLayout: true
});monaco.editor.createDiffEditor(
domElement: HTMLElement,
options?: IStandaloneDiffEditorConstructionOptions,
override?: IEditorOverrideServices
): IStandaloneDiffEditorCreates a diff editor for comparing two models.
Parameters:
domElement: HTML element to attach the editor tooptions: Diff editor configuration options (optional)override: Service overrides (optional)Returns: IStandaloneDiffEditor instance
const diffEditor = monaco.editor.createDiffEditor(document.getElementById('container'), {
theme: 'vs-dark',
readOnly: true
});
diffEditor.setModel({
original: originalModel,
modified: modifiedModel
});interface IStandaloneEditorConstructionOptions {
value?: string;
language?: string;
theme?: string;
readOnly?: boolean;
automaticLayout?: boolean;
wordWrap?: 'off' | 'on' | 'wordWrapColumn' | 'bounded';
minimap?: IEditorMinimapOptions;
scrollbar?: IEditorScrollbarOptions;
lineNumbers?: 'on' | 'off' | 'relative' | 'interval';
fontSize?: number;
fontFamily?: string;
tabSize?: number;
insertSpaces?: boolean;
// ... many more options
}interface IEditorMinimapOptions {
enabled?: boolean;
side?: 'right' | 'left';
size?: 'proportional' | 'fill' | 'fit';
showSlider?: 'always' | 'mouseover';
autohide?: boolean;
}interface IEditorScrollbarOptions {
vertical?: 'auto' | 'visible' | 'hidden';
horizontal?: 'auto' | 'visible' | 'hidden';
arrowSize?: number;
useShadows?: boolean;
verticalHasArrows?: boolean;
horizontalHasArrows?: boolean;
handleMouseWheel?: boolean;
alwaysConsumeMouseWheel?: boolean;
}monaco.editor.getEditors(): ICodeEditor[]Returns all editor instances.
monaco.editor.getDiffEditors(): IDiffEditor[]Returns all diff editor instances.
editor.focus(): voidFocuses the editor.
editor.hasTextFocus(): booleanReturns true if the editor has text focus.
editor.getPosition(): Position | nullGets the current cursor position.
editor.setPosition(position: IPosition): voidSets the cursor position.
editor.getSelection(): Selection | nullGets the current selection.
editor.setSelection(selection: IRange | ISelection): voidSets the selection.
editor.getValue(options?: IEditorStateOptions): stringGets the editor content.
editor.setValue(newValue: string): voidSets the editor content.
editor.getModel(): ITextModel | nullGets the current model.
editor.setModel(model: ITextModel | null): voidSets the current model.
editor.trigger(source: string | null | undefined, handlerId: string, payload: any): voidTriggers an editor action.
Common action IDs:
'editor.action.formatDocument' - Format document'editor.action.commentLine' - Toggle line comment'editor.action.copyLinesDown' - Copy lines down'editor.action.selectAll' - Select all content'undo' - Undo last change'redo' - Redo last undone change// Format the document
editor.trigger('keyboard', 'editor.action.formatDocument', null);
// Toggle line comment
editor.trigger('keyboard', 'editor.action.commentLine', null);editor.addAction(descriptor: IActionDescriptor): IDisposableAdds a custom action to the editor.
interface IActionDescriptor {
id: string;
label: string;
alias?: string;
precondition?: string;
keybindings?: number[];
keybindingContext?: string;
contextMenuGroupId?: string;
contextMenuOrder?: number;
run(editor: ICodeEditor, ...args: any[]): void | Promise<void>;
}const disposable = editor.addAction({
id: 'my-unique-id',
label: 'My Custom Action',
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.F10],
contextMenuGroupId: 'navigation',
contextMenuOrder: 1.5,
run: function(ed) {
alert('Custom action executed!');
}
});enum KeyMod {
CtrlCmd = 2048,
Shift = 1024,
Alt = 512,
WinCtrl = 256
}Key modifier constants for combining with key codes in actions.
enum KeyCode {
F1 = 59, F2 = 60, F3 = 61, F4 = 62, F5 = 63, F6 = 64, F7 = 65, F8 = 66, F9 = 67, F10 = 68,
F11 = 69, F12 = 70,
Enter = 3, Escape = 9, Space = 10, Tab = 2,
KEY_A = 31, KEY_B = 32, KEY_C = 33, KEY_D = 34, KEY_E = 35, KEY_F = 36, KEY_G = 37,
KEY_H = 38, KEY_I = 39, KEY_J = 40, KEY_K = 41, KEY_L = 42, KEY_M = 43, KEY_N = 44,
KEY_O = 45, KEY_P = 46, KEY_Q = 47, KEY_R = 48, KEY_S = 49, KEY_T = 50, KEY_U = 51,
KEY_V = 52, KEY_W = 53, KEY_X = 54, KEY_Y = 55, KEY_Z = 56,
KEY_0 = 21, KEY_1 = 22, KEY_2 = 23, KEY_3 = 24, KEY_4 = 25, KEY_5 = 26, KEY_6 = 27,
KEY_7 = 28, KEY_8 = 29, KEY_9 = 30,
Backspace = 1, Delete = 20, Insert = 19, Home = 14, End = 13, PageUp = 15, PageDown = 16,
LeftArrow = 17, RightArrow = 18, UpArrow = 11, DownArrow = 12
}Keyboard key codes for use in keybinding definitions.
// Common keybinding combinations
const saveKeybinding = monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S;
const formatKeybinding = monaco.KeyMod.Shift | monaco.KeyMod.Alt | monaco.KeyCode.KEY_F;editor.onDidChangeModelContent(listener: (e: IModelContentChangedEvent) => void): IDisposableFired when the model content changes.
const disposable = editor.onDidChangeModelContent((e) => {
console.log('Content changed:', e);
});editor.onDidChangeCursorPosition(listener: (e: ICursorPositionChangedEvent) => void): IDisposableFired when the cursor position changes.
editor.onDidChangeCursorSelection(listener: (e: ICursorSelectionChangedEvent) => void): IDisposableFired when the selection changes.
editor.onDidFocusEditorText(listener: () => void): IDisposableFired when the editor gains focus.
editor.onDidBlurEditorText(listener: () => void): IDisposableFired when the editor loses focus.
editor.layout(dimension?: IDimension): voidInstructs the editor to remeasure its container.
interface IDimension {
width: number;
height: number;
}// Automatic layout on window resize
window.addEventListener('resize', () => {
editor.layout();
});
// Manual layout with specific dimensions
editor.layout({ width: 800, height: 600 });monaco.editor.setTheme(themeName: string): voidSets the global theme.
Built-in themes:
'vs' - Light theme'vs-dark' - Dark theme'hc-black' - High contrast black theme'hc-light' - High contrast light thememonaco.editor.setTheme('vs-dark');monaco.editor.defineTheme(themeName: string, themeData: IStandaloneThemeData): voidDefines a custom theme.
interface IStandaloneThemeData {
base: 'vs' | 'vs-dark' | 'hc-black' | 'hc-light';
inherit: boolean;
rules: ITokenThemeRule[];
encodedTokensColors?: string[];
colors: IColors;
}editor.dispose(): voidDisposes the editor and frees resources.
// Always dispose editors when done
editor.dispose();interface IDisposable {
dispose(): void;
}Many Monaco operations return disposables that should be cleaned up:
const disposables: IDisposable[] = [];
disposables.push(editor.onDidChangeModelContent(() => {
// Handle content change
}));
disposables.push(editor.addAction({
// Action descriptor
}));
// Clean up all disposables
disposables.forEach(d => d.dispose());