/**
* 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
*/
/**
* Copyright 2009 Google Inc.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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: undefined
const KERNEL_SOURCE = '../static/js/require-kernel.js';
Ace2Editor.registry = {
nextId: 1,
};
const hooks = require('./pluginfw/hooks');
const pluginUtils = require('./pluginfw/shared');
const _ = require('./underscore');
function scriptTag(source) {
return (
``
);
}
function Ace2Editor() {
const ace2 = Ace2Editor;
const editor = {};
let info = {
editor,
id: (ace2.registry.nextId++),
};
let loaded = false;
let actionsPendingInit = [];
function pendingInit(func, optDoNow) {
return function () {
const that = this;
const args = arguments;
const action = function () {
func.apply(that, args);
};
if (optDoNow) {
optDoNow.apply(that, args);
}
if (loaded) {
action();
} else {
actionsPendingInit.push(action);
}
};
}
function doActionsPendingInit() {
_.each(actionsPendingInit, (fn, i) => {
fn();
});
actionsPendingInit = [];
}
ace2.registry[info.id] = info;
// The following functions (prefixed by 'ace_') are exposed by editor, but
// execution is delayed until init is complete
const aceFunctionsPendingInit = ['importText',
'importAText',
'focus',
'setEditable',
'getFormattedCode',
'setOnKeyPress',
'setOnKeyDown',
'setNotifyDirty',
'setProperty',
'setBaseText',
'setBaseAttributedText',
'applyChangesToBase',
'applyPreparedChangesetToBase',
'setUserChangeNotificationCallback',
'setAuthorInfo',
'setAuthorSelectionRange',
'callWithAce',
'execCommand',
'replaceRange'];
_.each(aceFunctionsPendingInit, (fnName, i) => {
const prefix = 'ace_';
const name = prefix + fnName;
editor[fnName] = pendingInit(function () {
if (fnName === 'setAuthorInfo') {
if (!arguments[0]) {
// setAuthorInfo AuthorId not set for some reason
} else {
info[prefix + fnName].apply(this, arguments);
}
} else {
info[prefix + fnName].apply(this, arguments);
}
});
});
editor.exportText = function () {
if (!loaded) return '(awaiting init)\n';
return info.ace_exportText();
};
editor.getFrame = function () {
return info.frame || null;
};
editor.getDebugProperty = function (prop) {
return info.ace_getDebugProperty(prop);
};
editor.getInInternationalComposition = function () {
if (!loaded) return false;
return info.ace_getInInternationalComposition();
};
// 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();
};
editor.getUnhandledErrors = function () {
if (!loaded) return [];
// returns array of {error: , time: +new Date()}
return info.ace_getUnhandledErrors();
};
function sortFilesByEmbeded(files) {
const embededFiles = [];
let remoteFiles = [];
if (Ace2Editor.EMBEDED) {
for (let i = 0, ii = files.length; i < ii; i++) {
const 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 pushStyleTagsFor(buffer, files) {
const sorted = sortFilesByEmbeded(files);
const embededFiles = sorted.embeded;
const remoteFiles = sorted.remote;
if (embededFiles.length > 0) {
buffer.push('');
hooks.callAll('aceInitInnerdocbodyHead', {
iframeHTML,
});
iframeHTML.push('