Skip to content

Commit

Permalink
feat: enable opening external URLs in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jan 5, 2024
1 parent ac78ad4 commit abf0bc7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ declare global {
pushOutputEntry(entry: OutputEntry): void;
readThemeFile(name?: string): Promise<LoadedFiddleTheme | null>;
reloadWindows(): void;
openExternal(url: string): Promise<void>;
removeAllListeners(type: FiddleEvent): void;
removeVersion(version: string): Promise<InstallState>;
saveFilesToTemp(files: Files): Promise<string>;
Expand Down
2 changes: 2 additions & 0 deletions src/ipc-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum IpcEvents {
TASK_DONE = 'TASK_DONE',
OUTPUT_ENTRY = 'OUTPUT_ENTRY',
RELOAD_WINDOW = 'RELOAD_WINDOW',
OPEN_EXTERNAL = 'OPEN_EXTERNAL',
SET_NATIVE_THEME = 'SET_NATIVE_THEME',
SHOW_WINDOW = 'SHOW_WINDOW',
GET_TEMPLATE_VALUES = 'GET_TEMPLATE_VALUES',
Expand Down Expand Up @@ -86,6 +87,7 @@ export const ipcMainEvents = [
IpcEvents.OUTPUT_ENTRY,
IpcEvents.TASK_DONE,
IpcEvents.RELOAD_WINDOW,
IpcEvents.OPEN_EXTERNAL,
IpcEvents.SET_NATIVE_THEME,
IpcEvents.SHOW_WINDOW,
IpcEvents.GET_TEMPLATE_VALUES,
Expand Down
4 changes: 4 additions & 0 deletions src/main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export function createMainWindow(): Electron.BrowserWindow {
browserWindow?.reload();
});

ipcMainManager.on(IpcEvents.OPEN_EXTERNAL, (_, url: string) => {
shell.openExternal(url);
});

browserWindows.push(browserWindow);

return browserWindow;
Expand Down
3 changes: 3 additions & 0 deletions src/preload/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ export async function setupFiddleGlobal() {
reloadWindows() {
ipcRenderer.send(IpcEvents.RELOAD_WINDOW);
},
openExternal(url: string) {
ipcRenderer.send(IpcEvents.OPEN_EXTERNAL, url);
},
readThemeFile(name?: string) {
return ipcRenderer.invoke(IpcEvents.READ_THEME_FILE, name);
},
Expand Down
50 changes: 40 additions & 10 deletions src/renderer/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ export class Editor extends React.Component<EditorProps> {
const { fontFamily, fontSize } = appState;

if (ref) {
this.editor = monaco.editor.create(ref, {
automaticLayout: true,
language: this.language,
theme: 'main',
fontFamily,
fontSize,
contextmenu: false,
model: null,
...monacoOptions,
});
this.editor = monaco.editor.create(
ref,
{
automaticLayout: true,
language: this.language,
theme: 'main',
fontFamily,
fontSize,
contextmenu: false,
model: null,
...monacoOptions,
},
{
openerService: this.openerService(),
},
);

// mark this editor as focused whenever it is
this.editor.onDidFocusEditorText(() => {
Expand All @@ -113,6 +119,30 @@ export class Editor extends React.Component<EditorProps> {
}
}

/**
* Implements external url handling for Monaco.
*
* @returns {MonacoType.editor.IOpenerService}
*/
private openerService() {
const { appState } = this.props;

return {
open: (url: string) => {
appState
.showConfirmDialog({
label: `Open ${url} in external browser?`,
ok: 'Open',
})
.then((open) => {
if (open) {
window.ElectronFiddle.openExternal(url);
}
});
},
};
}

public render() {
return <div className="editorContainer" ref={this.containerRef} />;
}
Expand Down
50 changes: 40 additions & 10 deletions src/renderer/components/output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ export const Output = observer(
const ref = this.outputRef.current;
if (ref) {
this.setupCustomOutputEditorLanguage(monaco);
this.editor = monaco.editor.create(ref, {
language: this.language,
theme: 'main',
readOnly: true,
contextmenu: false,
automaticLayout: true,
model: this.model,
...monacoOptions,
wordWrap: 'on',
});
this.editor = monaco.editor.create(
ref,
{
language: this.language,
theme: 'main',
readOnly: true,
contextmenu: false,
automaticLayout: true,
model: this.model,
...monacoOptions,
wordWrap: 'on',
},
{
openerService: this.openerService(),
},
);
}
}

Expand Down Expand Up @@ -193,6 +199,30 @@ export const Output = observer(
return lines;
}

/**
* Implements external url handling for Monaco.
*
* @returns {MonacoType.editor.IOpenerService}
*/
private openerService() {
const { appState } = this.props;

return {
open: (url: string) => {
appState
.showConfirmDialog({
label: `Open ${url} in external browser?`,
ok: 'Open',
})
.then((open) => {
if (open) {
window.ElectronFiddle.openExternal(url);
}
});
},
};
}

public render(): JSX.Element | null {
const { isConsoleShowing } = this.props.appState;

Expand Down

0 comments on commit abf0bc7

Please sign in to comment.