2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// requires: top
|
|
|
|
// requires: plugins
|
|
|
|
// requires: undefined
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
Ace2Editor.registry = {
|
|
|
|
nextId: 1
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function Ace2Editor()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var thisFunctionsName = "Ace2Editor";
|
|
|
|
var ace2 = Ace2Editor;
|
|
|
|
|
|
|
|
var editor = {};
|
2011-07-07 19:59:34 +02:00
|
|
|
var info = {
|
|
|
|
editor: editor,
|
|
|
|
id: (ace2.registry.nextId++)
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
var loaded = false;
|
|
|
|
|
|
|
|
var actionsPendingInit = [];
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function pendingInit(func, optDoNow)
|
|
|
|
{
|
|
|
|
return function()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var that = this;
|
|
|
|
var args = arguments;
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function action()
|
|
|
|
{
|
|
|
|
func.apply(that, args);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
if (optDoNow)
|
|
|
|
{
|
|
|
|
optDoNow.apply(that, args);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
if (loaded)
|
|
|
|
{
|
|
|
|
action();
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
actionsPendingInit.push(action);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function doActionsPendingInit()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < actionsPendingInit.length; i++)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
actionsPendingInit[i]();
|
|
|
|
}
|
|
|
|
actionsPendingInit = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
ace2.registry[info.id] = info;
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.importText = pendingInit(function(newCode, undoable)
|
|
|
|
{
|
|
|
|
info.ace_importText(newCode, undoable);
|
|
|
|
});
|
|
|
|
editor.importAText = pendingInit(function(newCode, apoolJsonObj, undoable)
|
|
|
|
{
|
|
|
|
info.ace_importAText(newCode, apoolJsonObj, undoable);
|
|
|
|
});
|
|
|
|
editor.exportText = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return "(awaiting init)\n";
|
2011-03-26 14:10:41 +01:00
|
|
|
return info.ace_exportText();
|
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.getFrame = function()
|
|
|
|
{
|
|
|
|
return info.frame || null;
|
|
|
|
};
|
|
|
|
editor.focus = pendingInit(function()
|
|
|
|
{
|
|
|
|
info.ace_focus();
|
|
|
|
});
|
|
|
|
editor.setEditable = pendingInit(function(newVal)
|
|
|
|
{
|
|
|
|
info.ace_setEditable(newVal);
|
|
|
|
});
|
|
|
|
editor.getFormattedCode = function()
|
|
|
|
{
|
|
|
|
return info.ace_getFormattedCode();
|
|
|
|
};
|
|
|
|
editor.setOnKeyPress = pendingInit(function(handler)
|
|
|
|
{
|
|
|
|
info.ace_setOnKeyPress(handler);
|
|
|
|
});
|
|
|
|
editor.setOnKeyDown = pendingInit(function(handler)
|
|
|
|
{
|
|
|
|
info.ace_setOnKeyDown(handler);
|
|
|
|
});
|
|
|
|
editor.setNotifyDirty = pendingInit(function(handler)
|
|
|
|
{
|
|
|
|
info.ace_setNotifyDirty(handler);
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.setProperty = pendingInit(function(key, value)
|
|
|
|
{
|
|
|
|
info.ace_setProperty(key, value);
|
|
|
|
});
|
|
|
|
editor.getDebugProperty = function(prop)
|
|
|
|
{
|
|
|
|
return info.ace_getDebugProperty(prop);
|
|
|
|
};
|
|
|
|
|
|
|
|
editor.setBaseText = pendingInit(function(txt)
|
|
|
|
{
|
|
|
|
info.ace_setBaseText(txt);
|
|
|
|
});
|
|
|
|
editor.setBaseAttributedText = pendingInit(function(atxt, apoolJsonObj)
|
|
|
|
{
|
|
|
|
info.ace_setBaseAttributedText(atxt, apoolJsonObj);
|
|
|
|
});
|
|
|
|
editor.applyChangesToBase = pendingInit(function(changes, optAuthor, apoolJsonObj)
|
|
|
|
{
|
|
|
|
info.ace_applyChangesToBase(changes, optAuthor, apoolJsonObj);
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
// prepareUserChangeset:
|
|
|
|
// Returns null if no new changes or ACE not ready. Otherwise, bundles up all user changes
|
|
|
|
// to the latest base text into a Changeset, which is returned (as a string if encodeAsString).
|
|
|
|
// If this method returns a truthy value, then applyPreparedChangesetToBase can be called
|
|
|
|
// at some later point to consider these changes part of the base, after which prepareUserChangeset
|
|
|
|
// must be called again before applyPreparedChangesetToBase. Multiple consecutive calls
|
|
|
|
// to prepareUserChangeset will return an updated changeset that takes into account the
|
|
|
|
// latest user changes, and modify the changeset to be applied by applyPreparedChangesetToBase
|
|
|
|
// accordingly.
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.prepareUserChangeset = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return null;
|
2011-03-26 14:10:41 +01:00
|
|
|
return info.ace_prepareUserChangeset();
|
|
|
|
};
|
|
|
|
editor.applyPreparedChangesetToBase = pendingInit(
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function()
|
|
|
|
{
|
|
|
|
info.ace_applyPreparedChangesetToBase();
|
|
|
|
});
|
|
|
|
editor.setUserChangeNotificationCallback = pendingInit(function(callback)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
info.ace_setUserChangeNotificationCallback(callback);
|
|
|
|
});
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.setAuthorInfo = pendingInit(function(author, authorInfo)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
info.ace_setAuthorInfo(author, authorInfo);
|
|
|
|
});
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.setAuthorSelectionRange = pendingInit(function(author, start, end)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
info.ace_setAuthorSelectionRange(author, start, end);
|
|
|
|
});
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.getUnhandledErrors = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return [];
|
2011-03-26 14:10:41 +01:00
|
|
|
// returns array of {error: <browser Error object>, time: +new Date()}
|
|
|
|
return info.ace_getUnhandledErrors();
|
|
|
|
};
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.callWithAce = pendingInit(function(fn, callStack, normalize)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return info.ace_callWithAce(fn, callStack, normalize);
|
|
|
|
});
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.execCommand = pendingInit(function(cmd, arg1)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
info.ace_execCommand(cmd, arg1);
|
|
|
|
});
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.replaceRange = pendingInit(function(start, end, text)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
info.ace_replaceRange(start, end, text);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// calls to these functions ($$INCLUDE_...) are replaced when this file is processed
|
|
|
|
// and compressed, putting the compressed code from the named file directly into the
|
|
|
|
// source here.
|
2011-07-07 19:59:34 +02:00
|
|
|
var $$INCLUDE_CSS = function(fileName)
|
|
|
|
{
|
|
|
|
return '<link rel="stylesheet" type="text/css" href="' + fileName + '"/>';
|
|
|
|
};
|
|
|
|
var $$INCLUDE_JS = function(fileName)
|
|
|
|
{
|
|
|
|
return '\x3cscript type="text/javascript" src="' + fileName + '">\x3c/script>';
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
var $$INCLUDE_JS_DEV = $$INCLUDE_JS;
|
|
|
|
var $$INCLUDE_CSS_DEV = $$INCLUDE_CSS;
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
var $$INCLUDE_CSS_Q = function(fileName)
|
|
|
|
{
|
|
|
|
return '\'<link rel="stylesheet" type="text/css" href="' + fileName + '"/>\'';
|
|
|
|
};
|
|
|
|
var $$INCLUDE_JS_Q = function(fileName)
|
|
|
|
{
|
|
|
|
return '\'\\x3cscript type="text/javascript" src="' + fileName + '">\\x3c/script>\'';
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
var $$INCLUDE_JS_Q_DEV = $$INCLUDE_JS_Q;
|
|
|
|
var $$INCLUDE_CSS_Q_DEV = $$INCLUDE_CSS_Q;
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.destroy = pendingInit(function()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
info.ace_dispose();
|
|
|
|
info.frame.parentNode.removeChild(info.frame);
|
|
|
|
delete ace2.registry[info.id];
|
|
|
|
info = null; // prevent IE 6 closure memory leaks
|
|
|
|
});
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
editor.init = function(containerId, initialCode, doneFunc)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
editor.importText(initialCode);
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
info.onEditorReady = function()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
loaded = true;
|
|
|
|
doActionsPendingInit();
|
|
|
|
doneFunc();
|
|
|
|
};
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
(function()
|
|
|
|
{
|
2011-05-28 19:09:17 +02:00
|
|
|
var doctype = "<!doctype html>";
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
var iframeHTML = ["'" + doctype + "<html><head>'"];
|
|
|
|
|
|
|
|
plugins.callHook("aceInitInnerdocbodyHead", {
|
|
|
|
iframeHTML: iframeHTML
|
|
|
|
});
|
|
|
|
|
2011-05-28 19:09:17 +02:00
|
|
|
// these lines must conform to a specific format because they are passed by the build script:
|
2011-07-25 15:38:54 +02:00
|
|
|
iframeHTML.push($$INCLUDE_CSS_Q("../static/css/iframe_editor.css"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/ace2_common.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/skiplist.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/virtual_lines.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/easysync2.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/cssmanager.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/colorutils.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/undomodule.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/contentcollector.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/changesettracker.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/linestylefilter.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/domline.js"));
|
|
|
|
iframeHTML.push($$INCLUDE_JS_Q("../static/js/ace2_inner.js"));
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
iframeHTML.push('\'\\n<style type="text/css" title="dynamicsyntax"></style>\\n\'');
|
|
|
|
iframeHTML.push('\'</head><body id="innerdocbody" class="syntax" spellcheck="false"> </body></html>\'');
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
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; ' + 'readyFunc = function() { editorInfo.onEditorReady(); readyFunc = null; editorInfo = null; }; ' + 'var doc = iframe.contentWindow.document; doc.open(); var text = (' + iframeHTML.join('+') + ').replace(/\\\\x3c/g, \'<\');doc.write(text); doc.close(); ' + '}, 0); }';
|
|
|
|
|
2011-07-25 15:38:54 +02:00
|
|
|
var outerHTML = [doctype, '<html><head>', $$INCLUDE_CSS("../static/css/iframe_editor.css"),
|
2011-07-07 19:59:34 +02:00
|
|
|
// bizarrely, in FF2, a file with no "external" dependencies won't finish loading properly
|
|
|
|
// (throbs busy while typing)
|
|
|
|
'<link rel="stylesheet" type="text/css" href="data:text/css,"/>', '\x3cscript>\n', outerScript, '\n\x3c/script>', '</head><body id="outerdocbody"><div id="sidediv"><!-- --></div><div id="linemetricsdiv">x</div><div id="overlaysdiv"><!-- --></div></body></html>'];
|
|
|
|
|
|
|
|
if (!Array.prototype.map) Array.prototype.map = function(fun)
|
|
|
|
{ //needed for IE
|
2011-03-26 14:10:41 +01:00
|
|
|
if (typeof fun != "function") throw new TypeError();
|
|
|
|
var len = this.length;
|
|
|
|
var res = new Array(len);
|
|
|
|
var thisp = arguments[1];
|
2011-07-07 19:59:34 +02:00
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
if (i in this) res[i] = fun.call(thisp, this[i], i, this);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
var outerFrame = document.createElement("IFRAME");
|
|
|
|
outerFrame.frameBorder = 0; // for IE
|
|
|
|
info.frame = outerFrame;
|
|
|
|
document.getElementById(containerId).appendChild(outerFrame);
|
|
|
|
|
|
|
|
var editorDocument = outerFrame.contentWindow.document;
|
|
|
|
|
|
|
|
editorDocument.open();
|
|
|
|
editorDocument.write(outerHTML.join(''));
|
|
|
|
editorDocument.close();
|
|
|
|
})();
|
|
|
|
};
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|