You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monaco-editor/website/static/monarch/monarch.js

179 lines
4.3 KiB
JavaScript

2 years ago
/// <reference path="../../node_modules/monaco-editor/monaco.d.ts" />
3 years ago
2 years ago
"use strict";
3 years ago
/*-----------------------------------------
General helpers
------------------------------------------*/
function clearInnerText(elem) {
2 years ago
elem.innerHTML = "";
3 years ago
}
function getInnerText(elem) {
var text = elem.innerText;
if (!text) text = elem.textContent;
return text;
}
function escapeToHTML(text) {
2 years ago
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
3 years ago
}
function appendInnerText(elem, txt) {
txt = escapeToHTML(txt);
elem.innerHTML += txt;
}
function setInnerText(elem, txt) {
clearInnerText(elem);
appendInnerText(elem, txt);
}
function getTextFromId(id) {
var elem = document.getElementById(id);
if (elem == null) return null;
return getInnerText(elem);
}
/* -----------------------------------------
The main loader for the workbench UI
------------------------------------------*/
2 years ago
var outputPane = document.getElementById("monarchConsole");
3 years ago
var isDirty = false;
window.onbeforeunload = function (ev) {
if (isDirty) {
2 years ago
return "If you leave this page any unsaved work will be lost.";
3 years ago
}
};
function createLangModel(languageId, text) {
monaco.languages.register({ id: languageId });
2 years ago
var langModel = monaco.editor.createModel(text, "javascript");
3 years ago
var update = function () {
var def = null;
try {
2 years ago
def = eval("(function(){ " + langModel.getValue() + "; })()");
3 years ago
} catch (err) {
2 years ago
setInnerText(outputPane, err + "\n");
3 years ago
return;
}
monaco.languages.setMonarchTokensProvider(languageId, def);
2 years ago
setInnerText(outputPane, "up-to-date\n");
3 years ago
};
langModel.onDidChangeContent(function () {
isDirty = true;
update();
});
update();
return langModel;
}
function readSamples(sampleSelect) {
var samples = {};
for (var i = 0; i < sampleSelect.options.length; i++) {
var id = sampleSelect.options[i].value;
if (!id || sampleSelect.options[i].disabled) {
continue;
}
2 years ago
var languageId = "monarch-language-" + id;
3 years ago
2 years ago
var sampleText = getTextFromId(id + "-sample");
3 years ago
samples[id] = {
languageId: languageId,
langModel: createLangModel(languageId, getTextFromId(id)),
langViewState: null,
sampleModel: monaco.editor.createModel(sampleText, languageId),
2 years ago
sampleViewState: null,
3 years ago
};
}
return samples;
}
2 years ago
require(["vs/editor/editor.main"], function () {
var sampleSelect = document.getElementById("sampleselect");
var langPane = document.getElementById("langPane");
var editorPane = document.getElementById("editor");
3 years ago
// Adjust height of editors
var screenHeight = window.innerHeight;
if (screenHeight) {
var paneHeight = 0.76 * screenHeight;
2 years ago
langPane.style.height = paneHeight + "px";
editorPane.style.height = paneHeight - 112 + "px"; // 100px + margin 10px + borders 2px
3 years ago
}
var SAMPLES = readSamples(sampleSelect);
var CURRENT_SAMPLE = null;
var langEditor = monaco.editor.create(langPane, {
model: null,
2 years ago
scrollBeyondLastLine: false,
3 years ago
});
var sampleEditor = monaco.editor.create(editorPane, {
model: null,
2 years ago
scrollBeyondLastLine: false,
3 years ago
});
2 years ago
var select = document.getElementById("themeselect");
var currentTheme = "vs";
3 years ago
select.onchange = function () {
currentTheme = select.options[select.selectedIndex].value;
monaco.editor.setTheme(currentTheme);
};
// on resize
function refreshLayout() {
langEditor.layout();
sampleEditor.layout();
}
window.onresize = refreshLayout;
// Switch to another sample
function setEditorState(name) {
if (!name || CURRENT_SAMPLE === name || !SAMPLES[name]) {
return;
}
// Save previous sample's view state
if (CURRENT_SAMPLE) {
SAMPLES[CURRENT_SAMPLE].langViewState = langEditor.saveViewState();
2 years ago
SAMPLES[CURRENT_SAMPLE].sampleViewState =
sampleEditor.saveViewState();
3 years ago
}
CURRENT_SAMPLE = name;
// Apply new sample
langEditor.setModel(SAMPLES[CURRENT_SAMPLE].langModel);
if (SAMPLES[CURRENT_SAMPLE].langViewState) {
langEditor.restoreViewState(SAMPLES[CURRENT_SAMPLE].langViewState);
}
sampleEditor.setModel(SAMPLES[CURRENT_SAMPLE].sampleModel);
if (SAMPLES[CURRENT_SAMPLE].sampleViewState) {
2 years ago
sampleEditor.restoreViewState(
SAMPLES[CURRENT_SAMPLE].sampleViewState
);
3 years ago
}
}
// Refresh the sample text
function refreshSample() {
var name = sampleSelect.options[sampleSelect.selectedIndex].value;
setEditorState(name);
}
sampleSelect.onchange = refreshSample;
refreshSample(); // initialize initial text
}); // require