|
|
@ -1,23 +1,38 @@
|
|
|
|
/*@flow*/
|
|
|
|
/*@flow*/
|
|
|
|
/*:: const define = () => {}; */
|
|
|
|
/*:: const define = () => {}; */
|
|
|
|
define([
|
|
|
|
define([
|
|
|
|
'/api/config',
|
|
|
|
'/api/config'
|
|
|
|
'/bower_components/less/dist/less.min.js'
|
|
|
|
], function (Config) { /*::});module.exports = (function() {
|
|
|
|
], function (Config, Less) { /*::});module.exports = (function() {
|
|
|
|
|
|
|
|
const Config = (undefined:any);
|
|
|
|
const Config = (undefined:any);
|
|
|
|
const Less = (undefined:any);
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
var module = { exports: {} };
|
|
|
|
var module = { exports: {} };
|
|
|
|
var key = Config.requireConf.urlArgs;
|
|
|
|
var key = Config.requireConf.urlArgs;
|
|
|
|
var localStorage;
|
|
|
|
var localStorage = {};
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
localStorage = window.localStorage || {};
|
|
|
|
localStorage = window.localStorage || {};
|
|
|
|
|
|
|
|
if (localStorage['LESS_CACHE'] !== key) {
|
|
|
|
|
|
|
|
Object.keys(localStorage).forEach(function (k) {
|
|
|
|
|
|
|
|
if (k.indexOf('LESS_CACHE|') !== 0) { return; }
|
|
|
|
|
|
|
|
delete localStorage[k];
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage['LESS_CACHE'] = key;
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e);
|
|
|
|
localStorage = {};
|
|
|
|
localStorage = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cacheGet = function (k, cb) {
|
|
|
|
|
|
|
|
if (window.cryptpadCache) { return void window.cryptpadCache.get(k, cb); }
|
|
|
|
|
|
|
|
setTimeout(function () { cb(localStorage['LESS_CACHE|' + key + '|' + k]); })
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var cachePut = function (k, v, cb) {
|
|
|
|
|
|
|
|
if (window.cryptpadCache) { return void window.cryptpadCache.put(k, v, cb); }
|
|
|
|
|
|
|
|
cb = cb || function () { };
|
|
|
|
|
|
|
|
setTimeout(function () { localStorage['LESS_CACHE|' + key + '|' + k] = v; cb(); });
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var fixURL = function (url, parent) {
|
|
|
|
var fixURL = function (url, parent) {
|
|
|
|
// data: blob: etc
|
|
|
|
// data: blob: etc
|
|
|
|
if (/^[a-zA-Z0-9]*:/.test(url)) { return url; }
|
|
|
|
if (/^[a-zA-Z0-9]*:/.test(url)) { return url; }
|
|
|
@ -32,13 +47,6 @@ define([
|
|
|
|
return out;
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var doXHR = Less.FileManager.prototype.doXHR;
|
|
|
|
|
|
|
|
Less.FileManager.prototype.doXHR = function (url, type, callback, errback) {
|
|
|
|
|
|
|
|
url = fixURL(url);
|
|
|
|
|
|
|
|
//console.log("xhr: " + url);
|
|
|
|
|
|
|
|
return doXHR(url, type, callback, errback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var inject = function (cssText, url) {
|
|
|
|
var inject = function (cssText, url) {
|
|
|
|
var curStyle = document.createElement('style');
|
|
|
|
var curStyle = document.createElement('style');
|
|
|
|
curStyle.setAttribute('data-original-src', url);
|
|
|
|
curStyle.setAttribute('data-original-src', url);
|
|
|
@ -48,15 +56,6 @@ define([
|
|
|
|
document.head.appendChild(curStyle);
|
|
|
|
document.head.appendChild(curStyle);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var checkCache = function () {
|
|
|
|
|
|
|
|
if (localStorage['LESS_CACHE'] === key) { return; }
|
|
|
|
|
|
|
|
Object.keys(localStorage).forEach(function (k) {
|
|
|
|
|
|
|
|
if (k.indexOf('LESS_CACHE|') !== 0) { return; }
|
|
|
|
|
|
|
|
delete localStorage[k];
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage['LESS_CACHE'] = key;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fixAllURLs = function (source, parent) {
|
|
|
|
var fixAllURLs = function (source, parent) {
|
|
|
|
var urlRegEx = /@import\s*("([^"]*)"|'([^']*)')|url\s*\(\s*(\s*"([^"]*)"|'([^']*)'|[^\)]*\s*)\s*\)/ig;
|
|
|
|
var urlRegEx = /@import\s*("([^"]*)"|'([^']*)')|url\s*\(\s*(\s*"([^"]*)"|'([^']*)'|[^\)]*\s*)\s*\)/ig;
|
|
|
|
var result, url;
|
|
|
|
var result, url;
|
|
|
@ -84,25 +83,46 @@ define([
|
|
|
|
xhr.send(null);
|
|
|
|
xhr.send(null);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var lessEngine;
|
|
|
|
|
|
|
|
var getLessEngine = function (cb) {
|
|
|
|
|
|
|
|
if (lessEngine) {
|
|
|
|
|
|
|
|
cb(lessEngine);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
require(['/bower_components/less/dist/less.min.js'], function (Less) {
|
|
|
|
|
|
|
|
lessEngine = Less;
|
|
|
|
|
|
|
|
var doXHR = lessEngine.FileManager.prototype.doXHR;
|
|
|
|
|
|
|
|
lessEngine.FileManager.prototype.doXHR = function (url, type, callback, errback) {
|
|
|
|
|
|
|
|
url = fixURL(url);
|
|
|
|
|
|
|
|
//console.log("xhr: " + url);
|
|
|
|
|
|
|
|
return doXHR(url, type, callback, errback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
cb(lessEngine);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var loadLess = function (url, cb) {
|
|
|
|
var loadLess = function (url, cb) {
|
|
|
|
Less.render('@import (multiple) "' + url + '";', {}, function(err, css) {
|
|
|
|
getLessEngine(function (less) {
|
|
|
|
if (err) { return void cb(err); }
|
|
|
|
less.render('@import (multiple) "' + url + '";', {}, function(err, css) {
|
|
|
|
cb(undefined, css.css);
|
|
|
|
if (err) { return void cb(err); }
|
|
|
|
}, window.less);
|
|
|
|
cb(undefined, css.css);
|
|
|
|
|
|
|
|
}, window.less);
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.load = function (url /*:string*/, cb /*:()=>void*/) {
|
|
|
|
module.exports.load = function (url /*:string*/, cb /*:()=>void*/) {
|
|
|
|
checkCache();
|
|
|
|
cacheGet(url, function (css) {
|
|
|
|
if (localStorage['LESS_CACHE|' + key + '|' + url]) {
|
|
|
|
if (css) {
|
|
|
|
inject(localStorage['LESS_CACHE|' + key + '|' + url], url);
|
|
|
|
inject(css, url);
|
|
|
|
cb();
|
|
|
|
return void cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('CACHE MISS ' + url);
|
|
|
|
((/\.less([\?\#].*)?$/.test(url)) ? loadLess : loadCSS)(url, function (err, css) {
|
|
|
|
((/\.less([\?\#].*)?$/.test(url)) ? loadLess : loadCSS)(url, function (err, css) {
|
|
|
|
var output = fixAllURLs(css, url);
|
|
|
|
var output = fixAllURLs(css, url);
|
|
|
|
localStorage['LESS_CACHE|' + key + '|' + url] = output;
|
|
|
|
cachePut(url, output);
|
|
|
|
inject(output, url);
|
|
|
|
inject(output, url);
|
|
|
|
cb();
|
|
|
|
cb();
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|