Skip to content

Commit 5cceeb7

Browse files
committed
feat: add built-in file viewer and Monaco editor to registry
feat: add F2 keybinding for saving in the editor
1 parent 3ad56a9 commit 5cceeb7

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

packages/ui/lib/features/commands/registerKeybindings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const appBuiltInKeybindings: Keybinding[] = [
115115
// Close viewer/editor commands
116116
{ command: CLOSE_VIEWER, key: "escape", when: "focusViewer" },
117117
{ command: CLOSE_EDITOR, key: "escape", when: "focusEditor" },
118+
{ command: DOTDIR_EDITOR_SAVE, key: "f2", when: "focusEditor" },
118119
{ command: DOTDIR_EDITOR_SAVE, key: "ctrl+s", mac: "cmd+s", when: "focusEditor" },
119120
{ command: CURSOR_UP, key: "up", when: "focusEditor" },
120121
{ command: CURSOR_DOWN, key: "down", when: "focusEditor" },

packages/ui/lib/features/extensions/builtins/MonacoEditorSurface.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ function createMonacoEditorExtensionApi(hostApi: DotDirGlobalApi, runtime?: Mona
217217
let unmountFn: (() => void) | null = null;
218218
const grammarJsonCache = new Map<string, object | null>();
219219
const activatedTokenProviders = new Set<string>();
220+
const reservedDotdirEditorKeys = new Set(["f1", "ctrl+f1", "cmd+f1", "f2", "ctrl+s", "cmd+s"]);
220221

221222
function publishEditorCommands(editor: Monaco.editor.IStandaloneCodeEditor | null): void {
222223
if (!runtime?.onCommandContributionsChange) return;
@@ -236,18 +237,22 @@ function createMonacoEditorExtensionApi(hostApi: DotDirGlobalApi, runtime?: Mona
236237
action.id === MONACO_QUICK_COMMAND_ACTION
237238
? undefined
238239
: toDotdirMonacoKeybinding(keybindingService?.lookupKeybinding(action.id));
240+
const sanitizedKeybinding =
241+
keybinding && !reservedDotdirEditorKeys.has(keybinding.key) && !(keybinding.mac && reservedDotdirEditorKeys.has(keybinding.mac))
242+
? keybinding
243+
: undefined;
239244
const displaySignature = [
240245
title.toLowerCase(),
241-
keybinding?.key ?? "",
242-
keybinding?.mac ?? "",
246+
sanitizedKeybinding?.key ?? "",
247+
sanitizedKeybinding?.mac ?? "",
243248
].join("\u0000");
244249
if (seenDisplaySignatures.has(displaySignature)) continue;
245250
seenDisplaySignatures.add(displaySignature);
246251
commands.push({
247252
command: action.id,
248253
title,
249254
palette: action.id !== MONACO_QUICK_COMMAND_ACTION,
250-
keybinding,
255+
keybinding: sanitizedKeybinding,
251256
});
252257
}
253258

packages/ui/lib/viewerEditorRegistry.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ import {
1717
} from "@/features/extensions/types";
1818
import { createContext, createElement, useContext, useRef, useSyncExternalStore, type ReactNode } from "react";
1919

20+
const BUILTIN_EXTENSION_DIR_PATH = "__dotdir_builtin__";
21+
22+
const BUILTIN_FILE_VIEWER: ExtensionViewerContribution = {
23+
id: "file-viewer",
24+
label: "File Viewer",
25+
patterns: ["*"],
26+
entry: "builtins/file-viewer",
27+
priority: -10_000,
28+
};
29+
30+
const BUILTIN_MONACO_EDITOR: ExtensionEditorContribution = {
31+
id: "monaco",
32+
label: "Monaco Editor",
33+
patterns: ["*"],
34+
entry: "builtins/monaco",
35+
priority: -10_000,
36+
};
37+
2038
export interface ResolvedViewer {
2139
contribution: ExtensionViewerContribution;
2240
extensionDirPath: string;
@@ -152,6 +170,10 @@ export class ViewerEditorRegistryManager {
152170
private version = 0;
153171
private listeners = new Set<RegistryListener>();
154172

173+
constructor() {
174+
this.registerBuiltIns();
175+
}
176+
155177
subscribe(listener: RegistryListener): () => void {
156178
this.listeners.add(listener);
157179
return () => {
@@ -163,10 +185,16 @@ export class ViewerEditorRegistryManager {
163185
return this.version;
164186
}
165187

188+
private registerBuiltIns(): void {
189+
this.viewerRegistry.register(BUILTIN_FILE_VIEWER, BUILTIN_EXTENSION_DIR_PATH);
190+
this.editorRegistry.register(BUILTIN_MONACO_EDITOR, BUILTIN_EXTENSION_DIR_PATH);
191+
}
192+
166193
replaceExtensions(extensions: LoadedExtension[]): void {
167194
this.viewerRegistry.clear();
168195
this.editorRegistry.clear();
169196
this.fsProviderRegistry.clear();
197+
this.registerBuiltIns();
170198

171199
for (const ext of extensions) {
172200
for (const viewer of extensionViewers(ext)) {

0 commit comments

Comments
 (0)