Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Monaco] Add monaco.editor.registerLinkOpener method to be able to intercept opening links from the editor #177055

Merged
merged 1 commit into from
Mar 16, 2023

Conversation

spahnke
Copy link
Contributor

@spahnke spahnke commented Mar 14, 2023

Fixes microsoft/monaco-editor#3354

By default the Monaco editor opens links clicked in the editor in a new tab. As stated in my comment in the above issue this is a good thing! However, there are situations where you want to handle the links yourself. For example, we have types of links that should be opened in the same tab, and we have types of links that should use our own window open method that transfers some state to the new tab/window. Everything else should still use the default.

This PR addresses that need by providing a monaco.editor.registerLinkOpener function that can be used to intercept the openening of links from the embedded editor and handle them however the embedder wishes to. The function delegates to the IOpenerService and returns an IDisposable instance to unregister the handler again.

Monaco Playground Code:

const text = `function hello() {
	alert('Hello world!');
	// https://www.example.com
}`;

// Hover on each property to see its docs!
monaco.editor.create(document.getElementById("container"), {
	value: text,
	language: "javascript",
	automaticLayout: true,
});

monaco.editor.registerLinkOpener({
	open(resource) {
		// do something with the link
		console.log(`handled ${resource}`);
		return true;
	}
});

@hediet hediet enabled auto-merge March 16, 2023 11:30
@hediet hediet added this to the March 2023 milestone Mar 16, 2023
@hediet hediet merged commit 3cd16d0 into microsoft:main Mar 16, 2023
@spahnke spahnke deleted the boiling-turtle branch March 16, 2023 11:40
@github-actions github-actions bot locked and limited conversation to collaborators Apr 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Opening links in the same window
6 participants