2011-12-04 15:33:56 +00:00
|
|
|
/**
|
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 13:10:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 17:59:34 +00:00
|
|
|
*
|
2011-03-26 13:10:41 +00: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 17:59:34 +00:00
|
|
|
*
|
2011-03-26 13:10:41 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 17:59:34 +00:00
|
|
|
*
|
2011-03-26 13:10:41 +00: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 17:59:34 +00:00
|
|
|
Ace2Editor.registry = {
|
|
|
|
nextId: 1
|
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2012-01-16 05:52:03 +00:00
|
|
|
var plugins = require('/plugins').plugins;
|
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
function Ace2Editor()
|
|
|
|
{
|
2011-03-26 13:10:41 +00:00
|
|
|
var ace2 = Ace2Editor;
|
|
|
|
|
|
|
|
var editor = {};
|
2011-07-07 17:59:34 +00:00
|
|
|
var info = {
|
|
|
|
editor: editor,
|
|
|
|
id: (ace2.registry.nextId++)
|
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
var loaded = false;
|
|
|
|
|
|
|
|
var actionsPendingInit = [];
|
2011-07-07 17:59:34 +00:00
|
|
|
|
|
|
|
function pendingInit(func, optDoNow)
|
|
|
|
{
|
|
|
|
return function()
|
|
|
|
{
|
2011-03-26 13:10:41 +00:00
|
|
|
var that = this;
|
|
|
|
var args = arguments;
|
2012-02-19 02:16:42 +00:00
|
|
|
var action = function()
|
2011-07-07 17:59:34 +00:00
|
|
|
{
|
|
|
|
func.apply(that, args);
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2011-07-07 17:59:34 +00:00
|
|
|
if (optDoNow)
|
|
|
|
{
|
|
|
|
optDoNow.apply(that, args);
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2011-07-07 17:59:34 +00:00
|
|
|
if (loaded)
|
|
|
|
{
|
|
|
|
action();
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2011-07-07 17:59:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
actionsPendingInit.push(action);
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-07-07 17:59:34 +00:00
|
|
|
|
|
|
|
function doActionsPendingInit()
|
|
|
|
{
|
2012-02-19 02:16:42 +00:00
|
|
|
$.each(actionsPendingInit, function(i,fn){
|
|
|
|
fn()
|
|
|
|
});
|
2011-03-26 13:10:41 +00:00
|
|
|
actionsPendingInit = [];
|
|
|
|
}
|
2012-02-19 02:16:42 +00:00
|
|
|
|
2011-03-26 13:10:41 +00:00
|
|
|
ace2.registry[info.id] = info;
|
|
|
|
|
2012-02-19 02:16:42 +00:00
|
|
|
// The following functions (prefixed by 'ace_') are exposed by editor, but
|
|
|
|
// execution is delayed until init is complete
|
|
|
|
var aceFunctionsPendingInit = ['importText', 'importAText', 'focus',
|
|
|
|
'setEditable', 'getFormattedCode', 'setOnKeyPress', 'setOnKeyDown',
|
|
|
|
'setNotifyDirty', 'setProperty', 'setBaseText', 'setBaseAttributedText',
|
|
|
|
'applyChangesToBase', 'applyPreparedChangesetToBase',
|
|
|
|
'setUserChangeNotificationCallback', 'setAuthorInfo',
|
|
|
|
'setAuthorSelectionRange', 'callWithAce', 'execCommand', 'replaceRange'];
|
|
|
|
|
|
|
|
$.each(aceFunctionsPendingInit, function(i,fnName){
|
|
|
|
var prefix = 'ace_';
|
|
|
|
var name = prefix + fnName;
|
|
|
|
editor[fnName] = pendingInit(function(){
|
|
|
|
info[prefix + fnName].apply(this, arguments);
|
|
|
|
});
|
2011-07-07 17:59:34 +00:00
|
|
|
});
|
2012-02-19 02:16:42 +00:00
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
editor.exportText = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return "(awaiting init)\n";
|
2011-03-26 13:10:41 +00:00
|
|
|
return info.ace_exportText();
|
|
|
|
};
|
2012-02-19 02:16:42 +00:00
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
editor.getFrame = function()
|
|
|
|
{
|
|
|
|
return info.frame || null;
|
|
|
|
};
|
2012-02-19 02:16:42 +00:00
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
editor.getDebugProperty = function(prop)
|
|
|
|
{
|
|
|
|
return info.ace_getDebugProperty(prop);
|
|
|
|
};
|
|
|
|
|
2011-03-26 13:10:41 +00: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 17:59:34 +00:00
|
|
|
editor.prepareUserChangeset = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return null;
|
2011-03-26 13:10:41 +00:00
|
|
|
return info.ace_prepareUserChangeset();
|
|
|
|
};
|
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
editor.getUnhandledErrors = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return [];
|
2011-03-26 13:10:41 +00:00
|
|
|
// returns array of {error: <browser Error object>, time: +new Date()}
|
|
|
|
return info.ace_getUnhandledErrors();
|
|
|
|
};
|
|
|
|
|
2012-02-26 15:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
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('<script type="text/javascript">');
|
|
|
|
buffer.push(Ace2Editor.EMBEDED[KERNEL_SOURCE]);
|
|
|
|
buffer.push(KERNEL_BOOT);
|
|
|
|
buffer.push('<\/script>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function pushScriptsTo(buffer) {
|
|
|
|
/* Folling is for packaging regular expression. */
|
|
|
|
/* $$INCLUDE_JS("../static/js/ace2_inner.js"); */
|
|
|
|
var ACE_SOURCE = '../static/js/ace2_inner.js';
|
|
|
|
if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[ACE_SOURCE]) {
|
|
|
|
buffer.push('<script type="text/javascript">');
|
|
|
|
buffer.push(Ace2Editor.EMBEDED[ACE_SOURCE]);
|
|
|
|
buffer.push('require("/ace2_inner");');
|
|
|
|
buffer.push('<\/script>');
|
|
|
|
} else {
|
|
|
|
file = ACE_SOURCE;
|
|
|
|
file = file.replace(/^\.\.\/static\/js\//, '../minified/');
|
|
|
|
buffer.push('<script type="application/javascript" src="' + file + '"><\/script>');
|
|
|
|
buffer.push('<script type="text/javascript">');
|
|
|
|
buffer.push('require("/ace2_inner");');
|
|
|
|
buffer.push('<\/script>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function pushStyleTagsFor(buffer, files) {
|
|
|
|
var sorted = sortFilesByEmbeded(files);
|
|
|
|
var embededFiles = sorted.embeded;
|
|
|
|
var remoteFiles = sorted.remote;
|
|
|
|
|
|
|
|
if (embededFiles.length > 0) {
|
|
|
|
buffer.push('<style type="text/css">');
|
|
|
|
for (var i = 0, ii = embededFiles.length; i < ii; i++) {
|
|
|
|
var file = embededFiles[i];
|
|
|
|
buffer.push(Ace2Editor.EMBEDED[file].replace(/<\//g, '<\\/'));
|
|
|
|
}
|
|
|
|
buffer.push('<\/style>');
|
|
|
|
}
|
|
|
|
for (var i = 0, ii = remoteFiles.length; i < ii; i++) {
|
|
|
|
var file = remoteFiles[i];
|
|
|
|
buffer.push('<link rel="stylesheet" type="text/css" href="' + file + '"\/>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
editor.destroy = pendingInit(function()
|
|
|
|
{
|
2011-03-26 13:10:41 +00: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 17:59:34 +00:00
|
|
|
editor.init = function(containerId, initialCode, doneFunc)
|
|
|
|
{
|
2011-03-26 13:10:41 +00:00
|
|
|
|
|
|
|
editor.importText(initialCode);
|
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
info.onEditorReady = function()
|
|
|
|
{
|
2011-03-26 13:10:41 +00:00
|
|
|
loaded = true;
|
|
|
|
doActionsPendingInit();
|
|
|
|
doneFunc();
|
|
|
|
};
|
2012-02-26 15:13:07 +00:00
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
(function()
|
|
|
|
{
|
2011-05-28 17:09:17 +00:00
|
|
|
var doctype = "<!doctype html>";
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2012-01-15 07:31:23 +00:00
|
|
|
var iframeHTML = [];
|
2011-07-07 17:59:34 +00:00
|
|
|
|
2012-01-15 07:31:23 +00:00
|
|
|
iframeHTML.push(doctype);
|
|
|
|
iframeHTML.push("<html><head>");
|
|
|
|
|
|
|
|
// For compatability's sake transform in and out.
|
|
|
|
for (var i = 0, ii = iframeHTML.length; i < ii; i++) {
|
|
|
|
iframeHTML[i] = JSON.stringify(iframeHTML[i]);
|
|
|
|
}
|
2011-07-07 17:59:34 +00:00
|
|
|
plugins.callHook("aceInitInnerdocbodyHead", {
|
|
|
|
iframeHTML: iframeHTML
|
|
|
|
});
|
2012-01-15 07:31:23 +00:00
|
|
|
for (var i = 0, ii = iframeHTML.length; i < ii; i++) {
|
|
|
|
iframeHTML[i] = JSON.parse(iframeHTML[i]);
|
|
|
|
}
|
2012-02-26 15:13:07 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
// these lines must conform to a specific format because they are passed by the build script:
|
|
|
|
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(iframeHTML, includedCSS);
|
|
|
|
|
|
|
|
var includedJS = [];
|
|
|
|
var $$INCLUDE_JS = function(filename) {includedJS.push(filename)};
|
|
|
|
pushRequireScriptTo(iframeHTML);
|
2012-02-05 19:13:43 +00:00
|
|
|
// Inject my plugins into my child.
|
2012-02-26 15:13:07 +00:00
|
|
|
iframeHTML.push('\
|
|
|
|
<script type="text/javascript">\
|
|
|
|
require.define("/plugins", null);\n\
|
|
|
|
require.define("/plugins.js", function (require, exports, module) {\
|
|
|
|
module.exports = parent.parent.require("/plugins");\
|
|
|
|
});\
|
|
|
|
</script>\
|
|
|
|
');
|
|
|
|
pushScriptsTo(iframeHTML);
|
|
|
|
|
2012-01-15 07:31:23 +00:00
|
|
|
iframeHTML.push('<style type="text/css" title="dynamicsyntax"></style>');
|
|
|
|
iframeHTML.push('</head><body id="innerdocbody" class="syntax" spellcheck="false"> </body></html>');
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2012-01-16 08:31:28 +00:00
|
|
|
// Expose myself to global for my child frame.
|
|
|
|
var thisFunctionsName = "ChildAccessibleAce2Editor";
|
|
|
|
(function () {return this}())[thisFunctionsName] = Ace2Editor;
|
|
|
|
|
2012-02-26 15:13:07 +00: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 = (' + JSON.stringify(iframeHTML.join('\n')) + ');doc.write(text); doc.close(); ' + '}, 0); }';
|
|
|
|
|
|
|
|
var outerHTML = [doctype, '<html><head>']
|
|
|
|
|
|
|
|
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);
|
2012-01-15 08:05:26 +00:00
|
|
|
|
2011-07-07 17:59:34 +00:00
|
|
|
// bizarrely, in FF2, a file with no "external" dependencies won't finish loading properly
|
|
|
|
// (throbs busy while typing)
|
2012-01-15 08:05:26 +00:00
|
|
|
outerHTML.push('<link rel="stylesheet" type="text/css" href="data:text/css,"/>', '\x3cscript>\n', outerScript.replace(/<\//g, '<\\/'), '\n\x3c/script>', '</head><body id="outerdocbody"><div id="sidediv"><!-- --></div><div id="linemetricsdiv">x</div><div id="overlaysdiv"><!-- --></div></body></html>');
|
2011-07-07 17:59:34 +00:00
|
|
|
|
2011-03-26 13:10:41 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-01-16 01:23:48 +00:00
|
|
|
|
|
|
|
exports.Ace2Editor = Ace2Editor;
|