Skip to content

Commit de8ab4b

Browse files
committed
feat: Implement Env service
1 parent 813b28f commit de8ab4b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/services.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { RenameFile, CreateFile, WorkspaceEdit, Disposable } from 'vscode-langua
77
import WatchableConsoleWindow from './services/WatchableConsoleWindow'
88
import CodinGameMonacoWorkspace from './services/CodinGameMonacoWorkspace'
99
import { Infrastructure } from './infrastructure'
10+
import CodinGameMonacoEnv from './services/CodinGameMonacoEnv'
1011

1112
interface CgMonacoServices extends Services {
1213
commands: MonacoCommands
@@ -35,7 +36,8 @@ const services = {
3536
commands: new MonacoCommands(monaco),
3637
languages: new MonacoLanguages(monaco, p2m, m2p),
3738
workspace: new CodinGameMonacoWorkspace(p2m, m2p, 'file:///tmp/project'),
38-
window: new WatchableConsoleWindow()
39+
window: new WatchableConsoleWindow(),
40+
env: new CodinGameMonacoEnv()
3941
}
4042

4143
installCommands(services)

src/services/CodinGameMonacoEnv.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
Env
3+
} from 'monaco-languageclient'
4+
5+
/**
6+
* Attempts to open a window and returns whether it succeeded. This technique is
7+
* not appropriate in certain contexts, like for example when the JS context is
8+
* executing inside a sandboxed iframe. If it is not necessary to know if the
9+
* browser blocked the new window, use {@link windowOpenNoOpener}.
10+
*
11+
* See https://github.com/microsoft/monaco-editor/issues/601
12+
* See https://github.com/microsoft/monaco-editor/issues/2474
13+
* See https://mathiasbynens.github.io/rel-noopener/
14+
*
15+
* @param url the url to open
16+
* @param noOpener whether or not to set the {@link window.opener} to null. You should leave the default
17+
* (true) unless you trust the url that is being opened.
18+
* @returns boolean indicating if the {@link window.open} call succeeded
19+
*/
20+
export function windowOpenWithSuccess (url: string, noOpener = true): boolean {
21+
const newTab = window.open()
22+
if (newTab != null) {
23+
if (noOpener) {
24+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25+
(newTab as any).opener = null
26+
}
27+
newTab.location.href = url
28+
return true
29+
}
30+
return false
31+
}
32+
33+
export default class CodinGameMonacoEnv implements Env {
34+
openExternal: Env['openExternal'] = async (uri) => {
35+
return windowOpenWithSuccess(uri.toString())
36+
}
37+
}

0 commit comments

Comments
 (0)