(CodeMirror: CodeMirrorLike)
| 11 | type CodeMirrorLike = any |
| 12 | |
| 13 | const loadMore = (CodeMirror: CodeMirrorLike): void => { |
| 14 | if (!CodeMirror.modeURL) { |
| 15 | CodeMirror.modeURL = '../../../../node_modules/codemirror/mode/%N/%N.js' |
| 16 | } |
| 17 | |
| 18 | const loading: Record<string, Array<() => void>> = {} |
| 19 | function splitCallback(cont: () => void, n: number): () => void { |
| 20 | let countDown = n |
| 21 | return function() { |
| 22 | if (--countDown === 0) cont() |
| 23 | } |
| 24 | } |
| 25 | function ensureDeps(mode: string, cont: () => void): void { |
| 26 | const deps: string[] | undefined = CodeMirror.modes[mode].dependencies |
| 27 | if (!deps) return cont() |
| 28 | const missing: string[] = [] |
| 29 | for (let i = 0; i < deps.length; ++i) { |
| 30 | if (!Object.prototype.hasOwnProperty.call(CodeMirror.modes, deps[i])) { |
| 31 | missing.push(deps[i]) |
| 32 | } |
| 33 | } |
| 34 | if (!missing.length) return cont() |
| 35 | const split = splitCallback(cont, missing.length) |
| 36 | for (let i = 0; i < missing.length; ++i) { |
| 37 | CodeMirror.requireMode(missing[i], split) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | CodeMirror.requireMode = function(mode: string | { name: string }, cont: () => void): void { |
| 42 | if (typeof mode !== 'string') { |
| 43 | mode = mode.name |
| 44 | } |
| 45 | if (Object.prototype.hasOwnProperty.call(CodeMirror.modes, mode)) return ensureDeps(mode, cont) |
| 46 | if (Object.prototype.hasOwnProperty.call(loading, mode)) { |
| 47 | loading[mode].push(cont) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | const list = (loading[mode] = [cont]) |
| 52 | |
| 53 | const pathKey: string = CodeMirror.modeURL.replace(/%N/g, mode) |
| 54 | |
| 55 | if (!pathKey) { |
| 56 | delete loading[mode] |
| 57 | console.error(`Cannot find path for CodeMirror mode ${mode}`) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | const loader = codeMirrorMode[pathKey] |
| 62 | if (typeof loader !== 'function') { |
| 63 | delete loading[mode] |
| 64 | console.error(`Invalid loader for CodeMirror mode ${mode}`) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | loader() |
| 69 | .then(() => { |
| 70 | ensureDeps(mode as string, function() { |
nothing calls this directly
no test coverage detected