cleanup
This commit is contained in:
parent
978d32eca3
commit
8a75cdca13
2 changed files with 88 additions and 102 deletions
|
|
@ -56,82 +56,82 @@ var config = require("../config");
|
|||
*/
|
||||
|
||||
exports.render = function(input, mode, theme, lineStart, disableGutter, callback) {
|
||||
lineStart = parseInt(lineStart || 1, 10);
|
||||
|
||||
// if theme and mode are both objects return the expected object.
|
||||
// preserves current behavior of giving mode and theme objects
|
||||
if(typeof theme !== 'string' && typeof mode !== 'string') {
|
||||
return renderer(mode, theme);
|
||||
}
|
||||
|
||||
var waiting = 0
|
||||
|
||||
// if either the theme or the mode were specified as objects
|
||||
// then we need to lazily load them.
|
||||
|
||||
// loads or passes the specified theme module then loads the mode.
|
||||
if (typeof theme == "string") {
|
||||
config.loadModule(['theme', theme], function (theme) {
|
||||
checkMode(theme);
|
||||
waiting++;
|
||||
config.loadModule(['theme', theme], function (m) {
|
||||
theme = m;
|
||||
--waiting || done();
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof mode == "string") {
|
||||
waiting++;
|
||||
config.loadModule(['mode', mode], function (m) {
|
||||
mode = new m.Mode()
|
||||
--waiting || done();
|
||||
});
|
||||
} else {
|
||||
// if theme was given as an object pass it through.
|
||||
checkMode(theme);
|
||||
}
|
||||
|
||||
// loads or passes the specified mode module then calls renderer
|
||||
function checkMode (theme) {
|
||||
if (typeof mode == "string") {
|
||||
config.loadModule(['mode', mode], function (mode) {
|
||||
callback(renderer(new mode.Mode(), theme));
|
||||
});
|
||||
} else {
|
||||
// if mode was given as an object pass it through.
|
||||
callback(renderer(mode, theme));
|
||||
}
|
||||
function done() {
|
||||
var result = exports.renderSync(input, mode, theme, lineStart, disableGutter);
|
||||
return callback ? callback(result) : result;
|
||||
}
|
||||
return waiting || done();
|
||||
};
|
||||
|
||||
/* Transforms a given input code snippet into HTML using the given mode
|
||||
*
|
||||
* @param {string} input Code snippet
|
||||
* @param {mode} mode Mode loaded from /ace/mode (use 'ServerSideHiglighter.getMode')
|
||||
* @param {string} r Code snippet
|
||||
* @returns {object} An object containing: html, css
|
||||
*/
|
||||
|
||||
exports.renderSync = function(input, mode, theme, lineStart, disableGutter) {
|
||||
lineStart = parseInt(lineStart || 1, 10);
|
||||
|
||||
var session = new EditSession("");
|
||||
session.setUseWorker(false);
|
||||
session.setMode(mode);
|
||||
|
||||
var textLayer = new TextLayer(document.createElement("div"));
|
||||
textLayer.setSession(session);
|
||||
textLayer.config = {
|
||||
characterWidth: 10,
|
||||
lineHeight: 20
|
||||
};
|
||||
|
||||
session.setValue(input);
|
||||
|
||||
var stringBuilder = [];
|
||||
var length = session.getLength();
|
||||
|
||||
for(var ix = 0; ix < length; ix++) {
|
||||
stringBuilder.push("<div class='ace_line'>");
|
||||
if (!disableGutter)
|
||||
stringBuilder.push("<span class='ace_gutter ace_gutter-cell' unselectable='on'>" + (ix + lineStart) + "</span>");
|
||||
textLayer.$renderLine(stringBuilder, ix, true, false);
|
||||
stringBuilder.push("</div>");
|
||||
}
|
||||
|
||||
// this was previously the body of exports.render
|
||||
// exports.render does the same thing it did before but now
|
||||
// if you give it mode or theme as strings you must specify a callback
|
||||
// which will receive the return value of renderer.
|
||||
// specifying the mode or theme as a string did not work before.
|
||||
function renderer (mode, theme) {
|
||||
var session = new EditSession("");
|
||||
session.setMode(mode);
|
||||
session.setUseWorker(false);
|
||||
var textLayer = new TextLayer(document.createElement("div"));
|
||||
textLayer.setSession(session);
|
||||
textLayer.config = {
|
||||
characterWidth: 10,
|
||||
lineHeight: 20
|
||||
};
|
||||
// let's prepare the whole html
|
||||
var html = "<div class=':cssClass'>\
|
||||
<div class='ace_editor ace_scroller ace_text-layer'>\
|
||||
:code\
|
||||
</div>\
|
||||
</div>".replace(/:cssClass/, theme.cssClass).replace(/:code/, stringBuilder.join(""));
|
||||
|
||||
session.setValue(input);
|
||||
|
||||
var stringBuilder = [];
|
||||
var length = session.getLength();
|
||||
|
||||
for(var ix = 0; ix < length; ix++) {
|
||||
stringBuilder.push("<div class='ace_line'>");
|
||||
if (!disableGutter)
|
||||
stringBuilder.push("<span class='ace_gutter ace_gutter-cell' unselectable='on'>" + (ix + lineStart) + "</span>");
|
||||
textLayer.$renderLine(stringBuilder, ix, true, false);
|
||||
stringBuilder.push("</div>");
|
||||
}
|
||||
|
||||
// let's prepare the whole html
|
||||
var html = "<div class=':cssClass'>\
|
||||
<div class='ace_editor ace_scroller ace_text-layer'>\
|
||||
:code\
|
||||
</div>\
|
||||
</div>".replace(/:cssClass/, theme.cssClass).replace(/:code/, stringBuilder.join(""));
|
||||
textLayer.destroy();
|
||||
|
||||
textLayer.destroy();
|
||||
|
||||
return {
|
||||
css: baseStyles + theme.cssText,
|
||||
html: html
|
||||
};
|
||||
}
|
||||
return {
|
||||
css: baseStyles + theme.cssText,
|
||||
html: html
|
||||
};
|
||||
};
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue