From c3f728b2ed8592973bfca992485bb11e1678f089 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Sun, 26 Feb 2012 16:13:07 +0100 Subject: [PATCH] Revert "drop embedding of JS/CSS files in ace.js. May result in a little performance drop but makes code much smaller" This reverts commit 9ede14a546731e692640a408c2b5bdb07d2c2c59. --- node/utils/Minify.js | 74 +++++++++++++++++++- static/js/ace.js | 158 ++++++++++++++++++++++++++++--------------- 2 files changed, 176 insertions(+), 56 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index baeaee688..859ee07ae 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -213,6 +213,72 @@ function _handle(req, res, jsFilename, jsFiles) { } } +// find all includes in ace.js and embed them. +function getAceFile(callback) { + fs.readFile(JS_DIR + 'ace.js', "utf8", function(err, data) { + if(ERR(err, callback)) return; + + // Find all includes in ace.js and embed them + var founds = data.match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"-]+\)/gi); + if (!settings.minify) { + founds = []; + } + founds.push('$$INCLUDE_JS("../static/js/require-kernel.js")'); + + data += ';\n'; + data += 'Ace2Editor.EMBEDED = Ace2Editor.EMBEDED || {};\n'; + + //go trough all includes + async.forEach(founds, function (item, callback) { + var filename = item.match(/"([^"]*)"/)[1]; + var type = item.match(/INCLUDE_([A-Z]+)/)[1]; + var shortFilename = (filename.match(/^..\/static\/js\/(.*)$/, '')||[])[1]; + + //read the included files + if (shortFilename) { + if (shortFilename == 'require-kernel.js') { + // the kernel isn’t actually on the file system. + handleEmbed(null, requireDefinition()); + } else { + var contents = ''; + tarCode(tar[shortFilename] || shortFilename + , function (content) { + contents += content; + } + , function () { + handleEmbed(null, contents); + } + ); + } + } else { + fs.readFile(ROOT_DIR + filename, "utf8", handleEmbed); + } + + function handleEmbed(error, data_) { + if (error) { + return; // Don't bother to include it. + } + if (settings.minify) { + if (type == "JS") { + try { + data_ = compressJS([data_]); + } catch (e) { + // Ignore, include uncompresseed, which will break in browser. + } + } else { + data_ = compressCSS([data_]); + } + } + data += 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = ' + + JSON.stringify(data_) + ';\n'; + callback(); + } + }, function(error) { + callback(error, data); + }); + }); +} + exports.requireDefinition = requireDefinition; function requireDefinition() { return 'var require = ' + RequireKernel.kernelSource + ';\n'; @@ -222,8 +288,12 @@ function tarCode(jsFiles, write, callback) { write('require.define({'); var initialEntry = true; async.forEach(jsFiles, function (filename, callback){ - fs.readFile(JS_DIR + filename, "utf8", handleFile); - + if (filename == 'ace.js') { + getAceFile(handleFile); + } else { + fs.readFile(JS_DIR + filename, "utf8", handleFile); + } + function handleFile(err, data) { if(ERR(err, callback)) return; var srcPath = JSON.stringify('/' + filename); diff --git a/static/js/ace.js b/static/js/ace.js index 63b1e61fe..6c7bb84e2 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -133,6 +133,74 @@ function Ace2Editor() return info.ace_getUnhandledErrors(); }; + + + function sortFilesByEmbeded(files) { + var embededFiles = []; + var remoteFiles = []; + + if (Ace2Editor.EMBEDED) { + for (var i = 0, ii = files.length; i < ii; i++) { + var file = files[i]; + if (Object.prototype.hasOwnProperty.call(Ace2Editor.EMBEDED, file)) { + embededFiles.push(file); + } else { + remoteFiles.push(file); + } + } + } else { + remoteFiles = files; + } + + return {embeded: embededFiles, remote: remoteFiles}; + } + function pushRequireScriptTo(buffer) { + var KERNEL_SOURCE = '../static/js/require-kernel.js'; + var KERNEL_BOOT = 'require.setRootURI("../minified/");\nrequire.setGlobalKeyPath("require");' + if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[KERNEL_SOURCE]) { + buffer.push(''); - iframeHTML.push(''); - iframeHTML.push('\ +'); + pushScriptsTo(iframeHTML); + iframeHTML.push(''); iframeHTML.push(' '); - editor.iframeHTML = iframeHTML.join('\n'); - // Expose myself to global for my child frame. var thisFunctionsName = "ChildAccessibleAce2Editor"; (function () {return this}())[thisFunctionsName] = Ace2Editor; - - var outerScript = 'editorId = "' + info.id + '"; editorInfo = parent.' + thisFunctionsName + '.registry[editorId]; ' + - 'window.onload = function()' + - '{ window.onload = null; setTimeout' + '(function() ' + - '{ var iframe = document.createElement("IFRAME"); ' + - 'iframe.scrolling = "no"; var outerdocbody = document.getElementById("outerdocbody"); ' + - 'iframe.frameBorder = 0; iframe.allowTransparency = true; ' + // for IE - 'outerdocbody.insertBefore(iframe, outerdocbody.firstChild); ' + - 'iframe.ace_outerWin = window; ' + - 'var doc = iframe.contentWindow.document; doc.open();doc.write(editorInfo.editor.iframeHTML); doc.close();'+ - 'readyFunc = function() { editorInfo.onEditorReady(); readyFunc = null; editorInfo = null; };' + - '}, 0); }'; - - - // Build HTML of editor iFrame; - var outerHTML = [doctype, '']; - outerHTML.push(''); - outerHTML.push(''); - outerHTML.push(''] + + var includedCSS = []; + var $$INCLUDE_CSS = function(filename) {includedCSS.push(filename)}; + $$INCLUDE_CSS("../static/css/iframe_editor.css"); + $$INCLUDE_CSS("../static/css/pad.css"); + $$INCLUDE_CSS("../static/custom/pad.css"); + pushStyleTagsFor(outerHTML, includedCSS); // bizarrely, in FF2, a file with no "external" dependencies won't finish loading properly // (throbs busy while typing) @@ -244,7 +295,6 @@ function Ace2Editor() document.getElementById(containerId).appendChild(outerFrame); var editorDocument = outerFrame.contentWindow.document; - editorDocument.open(); editorDocument.write(outerHTML.join(''));