github.com/DerekStrickland/consul@v1.4.5/ui-v2/app/utils/editor/lint.js (about) 1 /*global CodeMirror*/ 2 3 // CodeMirror doesn't seem to have anyway to hook into whether a mode 4 // has already loaded, or when a mode has finished loading 5 // follow more or less what CodeMirror does but doesn't expose 6 // see codemirror/addon/mode/loadmode.js 7 8 export const createLoader = function( 9 $$ = document.getElementsByTagName.bind(document), 10 CM = CodeMirror 11 ) { 12 CM.registerHelper('lint', 'ruby', function(text) { 13 return []; 14 }); 15 return function(editor, mode, cb) { 16 let scripts = [...$$('script')]; 17 const loaded = scripts.find(function(item) { 18 return item.src.indexOf(`/codemirror/mode/${mode}/${mode}.js`) !== -1; 19 }); 20 CM.autoLoadMode(editor, mode); 21 if (loaded) { 22 cb(); 23 } else { 24 scripts = [...$$('script')]; 25 CM.on(scripts[0], 'load', function() { 26 cb(); 27 }); 28 } 29 }; 30 }; 31 const load = createLoader(); 32 export default function(editor, mode) { 33 load(editor, mode, function() { 34 if (editor.getValue().trim().length) { 35 editor.performLint(); 36 } 37 }); 38 }