const loadMode = (editor, filename: string) => {
if (!editor) return;
const CodeMirror = window.CodeMirror;
// @ts-ignore
window.CodeMirror.modeURL = scriptCfg.baseUrl + "mode/%N/%N.min.js";
var val = filename,
m: any[],
mode: string,
spec: string | CodeMirror.ModeSpec<CodeMirror.ModeSpecOptions>;
try {
if ((m = /.+\.([^.]+)$/.exec(val))) {
// @ts-ignore
var info = CodeMirror.findModeByExtension(m[1]);
if (info) {
mode = info.mode;
spec = info.mime;
}
} else if (/\//.test(val)) {
// @ts-ignore
var info = CodeMirror.findModeByMIME(val);
if (info) {
mode = info.mode;
spec = val;
}
}
// else {
// mode = spec = val;
// }
if (mode) {
if (mode !== editor.getMode()?.name) {
editor.setOption("mode", spec);
// @ts-ignore
CodeMirror.autoLoadMode(editor, mode);
}
//console.log(mode, spec);
} else {
if (!!filename) alert("Could not find a mode corresponding to " + val);
editor.setOption("mode", "null");
}
} catch (e) {
console.error(e);
}
};
Last active
March 10, 2022 04:29
-
-
Save arisris/a2e075e415f3078895c540ac55b2e8c4 to your computer and use it in GitHub Desktop.
Detect detect file type in codemirror mode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment