mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 11:42:57 +00:00

Former-commit-id: 18249e7b2f568d64429b4453a9507e64c5634d6a [formerly 1dca9ca367cdbe78fde1fbc4ffb191947050a885] [formerly 287797e28367eb5fb6de3af8b1c0b54c317013cd [formerly 9ad231c1b8ab0ba936d8be8a44e71b45d51c2f78]] Former-commit-id: 0c25a6a88a0cba03408be4d53c867eca910f3f97 [formerly b414f310c3f86e13f081c984355c6b06015f03d3] Former-commit-id: 7eebc725347308574085abd3d866d97085da141a
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
// Most of the code from this file comes from:
|
|
// https://github.com/codemirror/CodeMirror/blob/master/addon/mode/loadmode.js
|
|
import * as CodeMirror from 'codemirror'
|
|
|
|
// Make CodeMirror available globally so the modes' can register themselves.
|
|
window.CodeMirror = CodeMirror
|
|
|
|
if (!CodeMirror.modeURL) CodeMirror.modeURL = '../mode/%N/%N.js'
|
|
|
|
var loading = {}
|
|
|
|
function splitCallback (cont, n) {
|
|
var countDown = n
|
|
return function () {
|
|
if (--countDown === 0) cont()
|
|
}
|
|
}
|
|
|
|
function ensureDeps (mode, cont) {
|
|
var deps = CodeMirror.modes[mode].dependencies
|
|
if (!deps) return cont()
|
|
var missing = []
|
|
for (var i = 0; i < deps.length; ++i) {
|
|
if (!CodeMirror.modes.hasOwnProperty(deps[i])) missing.push(deps[i])
|
|
}
|
|
if (!missing.length) return cont()
|
|
var split = splitCallback(cont, missing.length)
|
|
for (i = 0; i < missing.length; ++i) CodeMirror.requireMode(missing[i], split)
|
|
}
|
|
|
|
CodeMirror.requireMode = function (mode, cont) {
|
|
if (typeof mode !== 'string') mode = mode.name
|
|
if (CodeMirror.modes.hasOwnProperty(mode)) return ensureDeps(mode, cont)
|
|
if (loading.hasOwnProperty(mode)) return loading[mode].push(cont)
|
|
|
|
var file = CodeMirror.modeURL.replace(/%N/g, mode)
|
|
|
|
var script = document.createElement('script')
|
|
script.src = file
|
|
var others = document.getElementsByTagName('script')[0]
|
|
var list = loading[mode] = [cont]
|
|
|
|
CodeMirror.on(script, 'load', function () {
|
|
ensureDeps(mode, function () {
|
|
for (var i = 0; i < list.length; ++i) list[i]()
|
|
})
|
|
})
|
|
|
|
others.parentNode.insertBefore(script, others)
|
|
}
|
|
|
|
CodeMirror.autoLoadMode = function (instance, mode) {
|
|
if (CodeMirror.modes.hasOwnProperty(mode)) return
|
|
|
|
CodeMirror.requireMode(mode, function () {
|
|
instance.setOption('mode', mode)
|
|
})
|
|
}
|
|
|
|
export default CodeMirror
|