2021-01-29 06:43:53 +01:00
|
|
|
'use strict';
|
2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2013-06-14 19:37:41 +02:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01: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 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: undefined
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const hooks = require('./pluginfw/hooks');
|
|
|
|
const pluginUtils = require('./pluginfw/shared');
|
2012-01-16 06:52:03 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
const scriptTag =
|
|
|
|
(source) => `<script type="text/javascript">\n${source.replace(/<\//g, '<\\/')}</script>`;
|
2012-09-12 05:59:50 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
const Ace2Editor = function () {
|
2020-11-23 19:24:19 +01:00
|
|
|
const ace2 = Ace2Editor;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let info = {
|
2021-01-29 06:43:53 +01:00
|
|
|
editor: this,
|
2020-11-23 19:24:19 +01:00
|
|
|
id: (ace2.registry.nextId++),
|
2011-07-07 19:59:34 +02:00
|
|
|
};
|
2020-11-23 19:24:19 +01:00
|
|
|
let loaded = false;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let actionsPendingInit = [];
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2021-01-28 21:23:26 +01:00
|
|
|
const pendingInit = (func) => function (...args) {
|
|
|
|
const action = () => func.apply(this, args);
|
|
|
|
if (loaded) return action();
|
|
|
|
actionsPendingInit.push(action);
|
|
|
|
};
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
const doActionsPendingInit = () => {
|
|
|
|
for (const fn of actionsPendingInit) fn();
|
2011-03-26 14:10:41 +01:00
|
|
|
actionsPendingInit = [];
|
2021-01-29 06:43:53 +01:00
|
|
|
};
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
ace2.registry[info.id] = info;
|
|
|
|
|
2012-02-19 03:16:42 +01:00
|
|
|
// The following functions (prefixed by 'ace_') are exposed by editor, but
|
|
|
|
// execution is delayed until init is complete
|
2021-01-29 06:43:53 +01:00
|
|
|
const aceFunctionsPendingInit = [
|
|
|
|
'importText',
|
2020-11-23 19:24:19 +01:00
|
|
|
'importAText',
|
|
|
|
'focus',
|
|
|
|
'setEditable',
|
|
|
|
'getFormattedCode',
|
|
|
|
'setOnKeyPress',
|
|
|
|
'setOnKeyDown',
|
|
|
|
'setNotifyDirty',
|
|
|
|
'setProperty',
|
|
|
|
'setBaseText',
|
|
|
|
'setBaseAttributedText',
|
|
|
|
'applyChangesToBase',
|
|
|
|
'applyPreparedChangesetToBase',
|
|
|
|
'setUserChangeNotificationCallback',
|
|
|
|
'setAuthorInfo',
|
|
|
|
'setAuthorSelectionRange',
|
|
|
|
'callWithAce',
|
|
|
|
'execCommand',
|
2021-01-29 06:43:53 +01:00
|
|
|
'replaceRange',
|
|
|
|
];
|
2020-11-23 19:24:19 +01:00
|
|
|
|
2021-01-28 21:23:26 +01:00
|
|
|
for (const fnName of aceFunctionsPendingInit) {
|
|
|
|
// Note: info[`ace_${fnName}`] does not exist yet, so it can't be passed directly to
|
|
|
|
// pendingInit(). A simple wrapper is used to defer the info[`ace_${fnName}`] lookup until
|
|
|
|
// method invocation.
|
2021-01-29 06:43:53 +01:00
|
|
|
this[fnName] = pendingInit(function (...args) {
|
2021-01-28 21:23:26 +01:00
|
|
|
info[`ace_${fnName}`].apply(this, args);
|
2012-02-19 03:16:42 +01:00
|
|
|
});
|
2021-01-28 21:23:26 +01:00
|
|
|
}
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
this.exportText = () => loaded ? info.ace_exportText() : '(awaiting init)\n';
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
this.getDebugProperty = (prop) => info.ace_getDebugProperty(prop);
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
this.getInInternationalComposition =
|
|
|
|
() => loaded ? info.ace_getInInternationalComposition() : false;
|
2012-10-11 16:39:01 +02:00
|
|
|
|
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).
|
2021-01-29 06:43:53 +01:00
|
|
|
// 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.
|
|
|
|
this.prepareUserChangeset = () => loaded ? info.ace_prepareUserChangeset() : null;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
// returns array of {error: <browser Error object>, time: +new Date()}
|
|
|
|
this.getUnhandledErrors = () => loaded ? info.ace_getUnhandledErrors() : [];
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
const sortFilesByEmbeded = (files) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const embededFiles = [];
|
|
|
|
let remoteFiles = [];
|
2012-01-15 09:05:26 +01:00
|
|
|
|
|
|
|
if (Ace2Editor.EMBEDED) {
|
2020-11-23 19:24:19 +01:00
|
|
|
for (let i = 0, ii = files.length; i < ii; i++) {
|
|
|
|
const file = files[i];
|
2012-01-15 09:05:26 +01:00
|
|
|
if (Object.prototype.hasOwnProperty.call(Ace2Editor.EMBEDED, file)) {
|
|
|
|
embededFiles.push(file);
|
|
|
|
} else {
|
|
|
|
remoteFiles.push(file);
|
|
|
|
}
|
|
|
|
}
|
2012-01-15 06:42:47 +01:00
|
|
|
} else {
|
2012-01-15 09:05:26 +01:00
|
|
|
remoteFiles = files;
|
2012-01-15 06:42:47 +01:00
|
|
|
}
|
2012-01-15 09:05:26 +01:00
|
|
|
|
|
|
|
return {embeded: embededFiles, remote: remoteFiles};
|
2021-01-29 06:43:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const pushStyleTagsFor = (buffer, files) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const sorted = sortFilesByEmbeded(files);
|
|
|
|
const embededFiles = sorted.embeded;
|
|
|
|
const remoteFiles = sorted.remote;
|
2012-01-15 09:05:26 +01:00
|
|
|
|
|
|
|
if (embededFiles.length > 0) {
|
|
|
|
buffer.push('<style type="text/css">');
|
2021-01-29 06:43:53 +01:00
|
|
|
for (const file of embededFiles) {
|
2012-09-09 23:32:47 +02:00
|
|
|
buffer.push((Ace2Editor.EMBEDED[file] || '').replace(/<\//g, '<\\/'));
|
2012-01-15 09:05:26 +01:00
|
|
|
}
|
2021-01-29 06:43:53 +01:00
|
|
|
buffer.push('</style>');
|
2012-01-15 09:05:26 +01:00
|
|
|
}
|
2021-01-29 06:43:53 +01:00
|
|
|
for (const file of remoteFiles) {
|
|
|
|
buffer.push(`<link rel="stylesheet" type="text/css" href="${encodeURI(file)}"/>`);
|
2012-01-15 09:05:26 +01:00
|
|
|
}
|
2021-01-29 06:43:53 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
this.destroy = pendingInit(() => {
|
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
|
|
|
|
});
|
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
this.init = function (containerId, initialCode, doneFunc) {
|
|
|
|
this.importText(initialCode);
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
info.onEditorReady = () => {
|
2011-03-26 14:10:41 +01:00
|
|
|
loaded = true;
|
|
|
|
doActionsPendingInit();
|
|
|
|
doneFunc();
|
|
|
|
};
|
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
(() => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const doctype = '<!doctype html>';
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const iframeHTML = [];
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2012-01-15 08:31:23 +01:00
|
|
|
iframeHTML.push(doctype);
|
2020-11-23 19:24:19 +01:00
|
|
|
iframeHTML.push(`<html class='inner-editor ${clientVars.skinVariants}'><head>`);
|
2012-01-15 08:31:23 +01:00
|
|
|
|
2012-01-15 09:05:26 +01: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.
|
2013-06-14 19:37:41 +02:00
|
|
|
// these lines must conform to a specific format because they are passed by the build script:
|
2021-01-29 06:43:53 +01:00
|
|
|
let includedCSS = [];
|
|
|
|
let $$INCLUDE_CSS = (filename) => { includedCSS.push(filename); };
|
2020-11-23 19:24:19 +01:00
|
|
|
$$INCLUDE_CSS('../static/css/iframe_editor.css');
|
2016-05-20 15:42:05 +02:00
|
|
|
|
|
|
|
// disableCustomScriptsAndStyles can be used to disable loading of custom scripts
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!clientVars.disableCustomScriptsAndStyles) {
|
|
|
|
$$INCLUDE_CSS(`../static/css/pad.css?v=${clientVars.randomVersionString}`);
|
2016-05-20 15:42:05 +02:00
|
|
|
}
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
let additionalCSS = hooks.callAll('aceEditorCSS').map((path) => {
|
2015-12-01 13:16:44 +01:00
|
|
|
if (path.match(/\/\//)) { // Allow urls to external CSS - http(s):// and //some/path.css
|
|
|
|
return path;
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
return `../static/plugins/${path}`;
|
2015-12-01 13:16:44 +01:00
|
|
|
});
|
2012-04-07 01:40:13 +02:00
|
|
|
includedCSS = includedCSS.concat(additionalCSS);
|
2021-01-29 06:43:53 +01:00
|
|
|
$$INCLUDE_CSS(
|
|
|
|
`../static/skins/${clientVars.skinName}/pad.css?v=${clientVars.randomVersionString}`);
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2012-01-15 09:05:26 +01:00
|
|
|
pushStyleTagsFor(iframeHTML, includedCSS);
|
2021-02-25 14:17:36 +01:00
|
|
|
iframeHTML.push(`<script type="text/javascript" src="../static/js/require-kernel.js?v=${clientVars.randomVersionString}"></script>`);
|
2021-02-27 15:52:32 +01:00
|
|
|
// fill the cache
|
|
|
|
iframeHTML.push(`<script type="text/javascript" src="../javascripts/lib/ep_etherpad-lite/static/js/ace2_inner.js?callback=require.define&v=${clientVars.randomVersionString}"></script>`);
|
|
|
|
iframeHTML.push(`<script type="text/javascript" src="../javascripts/lib/ep_etherpad-lite/static/js/ace2_common.js?callback=require.define&v=${clientVars.randomVersionString}"></script>`);
|
2012-05-15 04:01:00 +02:00
|
|
|
|
2021-02-26 07:09:19 +01:00
|
|
|
iframeHTML.push(scriptTag(`(() => {
|
|
|
|
const require = window.require;
|
|
|
|
require.setRootURI('../javascripts/src');
|
|
|
|
require.setLibraryURI('../javascripts/lib');
|
|
|
|
require.setGlobalKeyPath('require');
|
2021-02-26 04:32:17 +01:00
|
|
|
|
|
|
|
// intentially moved before requiring client_plugins to save a 307
|
2021-02-26 07:09:19 +01:00
|
|
|
window.Ace2Inner = require('ep_etherpad-lite/static/js/ace2_inner');
|
|
|
|
window.plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
|
|
|
|
window.plugins.adoptPluginsFromAncestorsOf(window);
|
2021-02-26 04:32:17 +01:00
|
|
|
|
2021-02-26 07:09:19 +01:00
|
|
|
window.$ = window.jQuery = require('ep_etherpad-lite/static/js/rjquery').jQuery;
|
2021-02-26 04:32:17 +01:00
|
|
|
|
2021-02-26 07:09:19 +01:00
|
|
|
window.plugins.ensure(() => { window.Ace2Inner.init(); });
|
|
|
|
})();`));
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2012-01-15 08:31:23 +01:00
|
|
|
iframeHTML.push('<style type="text/css" title="dynamicsyntax"></style>');
|
2012-05-30 10:00:36 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
hooks.callAll('aceInitInnerdocbodyHead', {
|
|
|
|
iframeHTML,
|
2012-05-30 10:00:36 +02:00
|
|
|
});
|
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
iframeHTML.push('</head><body id="innerdocbody" class="innerdocbody" role="application" ' +
|
2021-02-26 04:29:11 +01:00
|
|
|
'spellcheck="false"> </body></html>');
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-01-29 02:53:15 +01:00
|
|
|
// eslint-disable-next-line node/no-unsupported-features/es-builtins
|
|
|
|
const gt = typeof globalThis === 'object' ? globalThis : window;
|
|
|
|
gt.ChildAccessibleAce2Editor = Ace2Editor;
|
2012-01-16 09:31:28 +01:00
|
|
|
|
2021-02-26 07:09:19 +01:00
|
|
|
const outerScript = `(() => {
|
|
|
|
window.editorInfo = parent.ChildAccessibleAce2Editor.registry[${JSON.stringify(info.id)}];
|
|
|
|
window.onload = () => {
|
2021-02-26 04:32:17 +01:00
|
|
|
window.onload = null;
|
2021-02-26 07:09:19 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
iframe.name = 'ace_inner';
|
|
|
|
iframe.title = 'pad';
|
|
|
|
iframe.scrolling = 'no';
|
2021-02-26 04:32:17 +01:00
|
|
|
iframe.frameBorder = 0;
|
|
|
|
iframe.allowTransparency = true; // for IE
|
|
|
|
iframe.ace_outerWin = window;
|
2021-02-26 07:09:19 +01:00
|
|
|
document.body.insertBefore(iframe, document.body.firstChild);
|
|
|
|
window.readyFunc = () => {
|
|
|
|
delete window.readyFunc;
|
|
|
|
window.editorInfo.onEditorReady();
|
|
|
|
delete window.editorInfo;
|
2021-02-26 04:32:17 +01:00
|
|
|
};
|
2021-02-26 07:09:19 +01:00
|
|
|
const doc = iframe.contentWindow.document;
|
2021-02-26 04:32:17 +01:00
|
|
|
doc.open();
|
2021-02-26 07:09:19 +01:00
|
|
|
doc.write(${JSON.stringify(iframeHTML.join('\n'))});
|
2021-02-26 04:32:17 +01:00
|
|
|
doc.close();
|
|
|
|
}, 0);
|
|
|
|
}
|
2021-02-26 07:09:19 +01:00
|
|
|
})();`;
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
const outerHTML =
|
|
|
|
[doctype, `<html class="inner-editor outerdoc ${clientVars.skinVariants}"><head>`];
|
2012-01-15 09:05:26 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
includedCSS = [];
|
|
|
|
$$INCLUDE_CSS = (filename) => { includedCSS.push(filename); };
|
2020-11-23 19:24:19 +01:00
|
|
|
$$INCLUDE_CSS('../static/css/iframe_editor.css');
|
|
|
|
$$INCLUDE_CSS(`../static/css/pad.css?v=${clientVars.randomVersionString}`);
|
2013-06-14 19:37:41 +02:00
|
|
|
|
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
additionalCSS = hooks.callAll('aceEditorCSS').map((path) => {
|
2015-12-01 13:16:44 +01:00
|
|
|
if (path.match(/\/\//)) { // Allow urls to external CSS - http(s):// and //some/path.css
|
|
|
|
return path;
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
return `../static/plugins/${path}`;
|
2021-01-29 06:43:53 +01:00
|
|
|
});
|
2012-04-07 01:40:13 +02:00
|
|
|
includedCSS = includedCSS.concat(additionalCSS);
|
2021-01-29 06:43:53 +01:00
|
|
|
$$INCLUDE_CSS(
|
|
|
|
`../static/skins/${clientVars.skinName}/pad.css?v=${clientVars.randomVersionString}`);
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2012-01-15 09:05:26 +01:00
|
|
|
pushStyleTagsFor(outerHTML, includedCSS);
|
|
|
|
|
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)
|
2020-11-23 19:24:19 +01:00
|
|
|
const pluginNames = pluginUtils.clientPluginNames();
|
2020-09-06 21:27:18 +02:00
|
|
|
outerHTML.push(
|
|
|
|
'<style type="text/css" title="dynamicsyntax"></style>',
|
|
|
|
'<link rel="stylesheet" type="text/css" href="data:text/css,"/>',
|
|
|
|
scriptTag(outerScript),
|
|
|
|
'</head>',
|
|
|
|
'<body id="outerdocbody" class="outerdocbody ', pluginNames.join(' '), '">',
|
|
|
|
'<div id="sidediv" class="sidediv"><!-- --></div>',
|
|
|
|
'<div id="linemetricsdiv">x</div>',
|
|
|
|
'</body></html>');
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const outerFrame = document.createElement('IFRAME');
|
|
|
|
outerFrame.name = 'ace_outer';
|
2011-03-26 14:10:41 +01:00
|
|
|
outerFrame.frameBorder = 0; // for IE
|
2020-11-23 19:24:19 +01:00
|
|
|
outerFrame.title = 'Ether';
|
2011-03-26 14:10:41 +01:00
|
|
|
info.frame = outerFrame;
|
|
|
|
document.getElementById(containerId).appendChild(outerFrame);
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const editorDocument = outerFrame.contentWindow.document;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
editorDocument.open();
|
|
|
|
editorDocument.write(outerHTML.join(''));
|
|
|
|
editorDocument.close();
|
|
|
|
})();
|
|
|
|
};
|
2021-01-29 06:43:53 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
Ace2Editor.registry = {
|
|
|
|
nextId: 1,
|
|
|
|
};
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
exports.Ace2Editor = Ace2Editor;
|