mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 13:55:41 +01:00
Update Hover provider example
This commit is contained in:
parent
c7a3e42fe6
commit
543663b2f0
2 changed files with 35 additions and 11 deletions
|
|
@ -1011,23 +1011,18 @@ function getCode() {
|
||||||
|
|
||||||
== Hover provider example
|
== Hover provider example
|
||||||
=======================JS
|
=======================JS
|
||||||
var quoteOfTheDay = new monaco.Promise(function(c,e) {
|
|
||||||
window.parseResponse = function(rawQOD) {
|
|
||||||
c(rawQOD.data.contents.quotes[0].quote);
|
|
||||||
}
|
|
||||||
var script = document.createElement('script');
|
|
||||||
script.src='http://quotes.rest/qod.js';
|
|
||||||
document.head.appendChild(script);
|
|
||||||
});
|
|
||||||
|
|
||||||
monaco.languages.register({ id: 'mySpecialLanguage' });
|
monaco.languages.register({ id: 'mySpecialLanguage' });
|
||||||
|
|
||||||
monaco.languages.registerHoverProvider('mySpecialLanguage', {
|
monaco.languages.registerHoverProvider('mySpecialLanguage', {
|
||||||
provideHover: function(model, position) {
|
provideHover: function(model, position) {
|
||||||
return quoteOfTheDay.then(function(quoteText) {
|
return xhr('../playground.html').then(function(res) {
|
||||||
return {
|
return {
|
||||||
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
|
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
|
||||||
contents: ['**' + quoteText + '**']
|
contents: [
|
||||||
|
'**SOURCE**',
|
||||||
|
{ language: 'html', value: res.responseText.substring(0, 200) }
|
||||||
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -1038,6 +1033,35 @@ monaco.editor.create(document.getElementById("container"), {
|
||||||
language: 'mySpecialLanguage'
|
language: 'mySpecialLanguage'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function xhr(url) {
|
||||||
|
var req = null;
|
||||||
|
return new monaco.Promise(function(c,e,p) {
|
||||||
|
req = new XMLHttpRequest();
|
||||||
|
req.onreadystatechange = function () {
|
||||||
|
if (req._canceled) { return; }
|
||||||
|
|
||||||
|
if (req.readyState === 4) {
|
||||||
|
if ((req.status >= 200 && req.status < 300) || req.status === 1223) {
|
||||||
|
c(req);
|
||||||
|
} else {
|
||||||
|
e(req);
|
||||||
|
}
|
||||||
|
req.onreadystatechange = function () { };
|
||||||
|
} else {
|
||||||
|
p(req);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
req.open("GET", url, true );
|
||||||
|
req.responseType = "";
|
||||||
|
|
||||||
|
req.send(null);
|
||||||
|
}, function () {
|
||||||
|
req._canceled = true;
|
||||||
|
req.abort();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
=======================HTML
|
=======================HTML
|
||||||
<div id="container" style="height:100%;"></div>
|
<div id="container" style="height:100%;"></div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
// This is a generated file. Please do not edit directly.
|
// This is a generated file. Please do not edit directly.
|
||||||
var SAMPLES = this.SAMPLES || [];
|
var SAMPLES = this.SAMPLES || [];
|
||||||
SAMPLES.push({"id":"extending-language-services-hover-provider-example","js":"//---------------------------------------------------\n// Extending Language Services > Hover provider example\n//---------------------------------------------------\n\nvar quoteOfTheDay = new monaco.Promise(function(c,e) {\n\twindow.parseResponse = function(rawQOD) {\n\t\tc(rawQOD.data.contents.quotes[0].quote);\n\t}\n\tvar script = document.createElement('script');\n\tscript.src='http://quotes.rest/qod.js';\n\tdocument.head.appendChild(script);\n});\n\nmonaco.languages.register({ id: 'mySpecialLanguage' });\n\nmonaco.languages.registerHoverProvider('mySpecialLanguage', {\n\tprovideHover: function(model, position) {\n\t\treturn quoteOfTheDay.then(function(quoteText) {\n\t\t\treturn {\n\t\t\t\trange: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),\n\t\t\t\tcontents: ['**' + quoteText + '**']\n\t\t\t}\n\t\t});\n\t}\n});\n\nmonaco.editor.create(document.getElementById(\"container\"), {\n\tvalue: '\\n\\nHover over this text',\n\tlanguage: 'mySpecialLanguage'\n});\n","html":"<div id=\"container\" style=\"height:100%;\"></div>\n","css":""});
|
SAMPLES.push({"id":"extending-language-services-hover-provider-example","js":"//---------------------------------------------------\n// Extending Language Services > Hover provider example\n//---------------------------------------------------\n\n\nmonaco.languages.register({ id: 'mySpecialLanguage' });\n\nmonaco.languages.registerHoverProvider('mySpecialLanguage', {\n\tprovideHover: function(model, position) {\n\t\treturn xhr('../playground.html').then(function(res) {\n\t\t\treturn {\n\t\t\t\trange: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),\n\t\t\t\tcontents: [\n\t\t\t\t\t'**SOURCE**',\n\t\t\t\t\t{ language: 'html', value: res.responseText.substring(0, 200) }\n\t\t\t\t]\n\t\t\t}\n\t\t});\n\t}\n});\n\nmonaco.editor.create(document.getElementById(\"container\"), {\n\tvalue: '\\n\\nHover over this text',\n\tlanguage: 'mySpecialLanguage'\n});\n\nfunction xhr(url) {\n\tvar req = null;\n\treturn new monaco.Promise(function(c,e,p) {\n\t\treq = new XMLHttpRequest();\n\t\treq.onreadystatechange = function () {\n\t\t\tif (req._canceled) { return; }\n\n\t\t\tif (req.readyState === 4) {\n\t\t\t\tif ((req.status >= 200 && req.status < 300) || req.status === 1223) {\n\t\t\t\t\tc(req);\n\t\t\t\t} else {\n\t\t\t\t\te(req);\n\t\t\t\t}\n\t\t\t\treq.onreadystatechange = function () { };\n\t\t\t} else {\n\t\t\t\tp(req);\n\t\t\t}\n\t\t};\n\n\t\treq.open(\"GET\", url, true );\n\t\treq.responseType = \"\";\n\n\t\treq.send(null);\n\t}, function () {\n\t\treq._canceled = true;\n\t\treq.abort();\n\t});\n}\n","html":"<div id=\"container\" style=\"height:100%;\"></div>\n","css":""});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue