2011-12-04 15:33:56 +00:00
|
|
|
/**
|
2013-06-15 01:37:41 +08:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 15:33:56 +00:00
|
|
|
* 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 18:59:34 +01: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 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 18:59:34 +01: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
|
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
define(['ep_etherpad-lite/static/js/pluginfw/hooks', 'underscore'], function (hooks, _) {
|
|
|
|
Ace2Editor.registry = {
|
|
|
|
nextId: 1
|
2011-07-07 18:59:34 +01:00
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
function scriptTag(source) {
|
|
|
|
return (
|
|
|
|
'<script type="text/javascript">\n'
|
|
|
|
+ source.replace(/<\//g, '<\\/') +
|
|
|
|
'</script>'
|
|
|
|
)
|
|
|
|
}
|
2011-07-07 18:59:34 +01:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
function Ace2Editor()
|
2011-07-07 18:59:34 +01:00
|
|
|
{
|
2015-04-12 18:10:17 +02:00
|
|
|
var ace2 = Ace2Editor;
|
|
|
|
|
|
|
|
var editor = {};
|
|
|
|
var info = {
|
|
|
|
editor: editor,
|
|
|
|
id: (ace2.registry.nextId++)
|
|
|
|
};
|
|
|
|
var loaded = false;
|
|
|
|
|
|
|
|
var actionsPendingInit = [];
|
|
|
|
|
|
|
|
function pendingInit(func, optDoNow)
|
2011-07-07 18:59:34 +01:00
|
|
|
{
|
2015-04-12 18:10:17 +02:00
|
|
|
return function()
|
2011-07-07 18:59:34 +01:00
|
|
|
{
|
2015-04-12 18:10:17 +02:00
|
|
|
var that = this;
|
|
|
|
var args = arguments;
|
|
|
|
var action = function()
|
|
|
|
{
|
|
|
|
func.apply(that, args);
|
|
|
|
}
|
|
|
|
if (optDoNow)
|
|
|
|
{
|
|
|
|
optDoNow.apply(that, args);
|
|
|
|
}
|
|
|
|
if (loaded)
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
actionsPendingInit.push(action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-07-07 18:59:34 +01:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
function doActionsPendingInit()
|
|
|
|
{
|
|
|
|
_.each(actionsPendingInit, function(fn,i){
|
|
|
|
fn()
|
|
|
|
});
|
|
|
|
actionsPendingInit = [];
|
|
|
|
}
|
2013-06-15 01:37:41 +08:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
ace2.registry[info.id] = info;
|
|
|
|
|
|
|
|
// 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(fnName,i){
|
|
|
|
var prefix = 'ace_';
|
|
|
|
var name = prefix + fnName;
|
|
|
|
editor[fnName] = pendingInit(function(){
|
|
|
|
info[prefix + fnName].apply(this, arguments);
|
|
|
|
});
|
2012-02-19 03:16:42 +01:00
|
|
|
});
|
2013-06-15 01:37:41 +08:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.exportText = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return "(awaiting init)\n";
|
|
|
|
return info.ace_exportText();
|
|
|
|
};
|
2013-06-15 01:37:41 +08:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.getFrame = function()
|
|
|
|
{
|
|
|
|
return info.frame || null;
|
|
|
|
};
|
2013-06-15 01:37:41 +08:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.getDebugProperty = function(prop)
|
|
|
|
{
|
|
|
|
return info.ace_getDebugProperty(prop);
|
|
|
|
};
|
2011-07-07 18:59:34 +01:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.getInInternationalComposition = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return false;
|
|
|
|
return info.ace_getInInternationalComposition();
|
|
|
|
};
|
2012-10-11 23:39:01 +09:00
|
|
|
|
2015-04-12 18:10:17 +02: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.
|
|
|
|
editor.prepareUserChangeset = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return null;
|
|
|
|
return info.ace_prepareUserChangeset();
|
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.getUnhandledErrors = function()
|
|
|
|
{
|
|
|
|
if (!loaded) return [];
|
|
|
|
// returns array of {error: <browser Error object>, time: +new Date()}
|
|
|
|
return info.ace_getUnhandledErrors();
|
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
function pushStyleTagsFor(buffer, files) {
|
|
|
|
for (var i = 0, ii = files.length; i < ii; i++) {
|
|
|
|
var file = files[i];
|
|
|
|
buffer.push('<link rel="stylesheet" type="text/css" href="' + file + '"\/>');
|
|
|
|
}
|
2012-01-15 00:05:26 -08:00
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.destroy = pendingInit(function()
|
|
|
|
{
|
|
|
|
info.ace_dispose();
|
|
|
|
info.frame.parentNode.removeChild(info.frame);
|
|
|
|
delete ace2.registry[info.id];
|
|
|
|
info = null; // prevent IE 6 closure memory leaks
|
|
|
|
});
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
editor.init = function(containerId, initialCode, doneFunc)
|
|
|
|
{
|
|
|
|
editor.importText(initialCode);
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
info.onEditorReady = function()
|
|
|
|
{
|
|
|
|
loaded = true;
|
|
|
|
doActionsPendingInit();
|
|
|
|
doneFunc();
|
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-25 18:15:48 +02:00
|
|
|
requirejs(["ep_etherpad-lite/static/js/ace2_inner"], function (Ace2Inner) {
|
|
|
|
var editorId = info.id;
|
|
|
|
var editorInfo = Ace2Editor.registry[editorId];
|
|
|
|
Ace2Inner.init(editorInfo);
|
|
|
|
});
|
2015-04-12 18:10:17 +02:00
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
return editor;
|
|
|
|
}
|
2012-01-15 17:23:48 -08:00
|
|
|
|
2015-04-12 18:10:17 +02:00
|
|
|
return {Ace2Editor: Ace2Editor};
|
|
|
|
});
|