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');
|
2021-03-20 01:50:01 +01:00
|
|
|
const makeCSSManager = require('./cssmanager').makeCSSManager;
|
2020-11-23 19:24:19 +01:00
|
|
|
const pluginUtils = require('./pluginfw/shared');
|
2012-01-16 06:52:03 +01:00
|
|
|
|
2021-03-01 01:37:14 +01:00
|
|
|
const debugLog = (...args) => {};
|
|
|
|
|
2021-03-01 01:59:11 +01:00
|
|
|
// The inner and outer iframe's locations are about:blank, so relative URLs are relative to that.
|
|
|
|
// Firefox and Chrome seem to do what the developer intends if given a relative URL, but Safari
|
|
|
|
// errors out unless given an absolute URL for a JavaScript-created element.
|
|
|
|
const absUrl = (url) => new URL(url, window.location.href).href;
|
|
|
|
|
2021-02-26 03:11:30 +01:00
|
|
|
const eventFired = async (obj, event, cleanups = [], predicate = () => true) => {
|
|
|
|
if (typeof cleanups === 'function') {
|
|
|
|
predicate = cleanups;
|
|
|
|
cleanups = [];
|
|
|
|
}
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
let cleanup;
|
|
|
|
const successCb = () => {
|
|
|
|
if (!predicate()) return;
|
|
|
|
debugLog(`Ace2Editor.init() ${event} event on`, obj);
|
|
|
|
cleanup();
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
const errorCb = () => {
|
|
|
|
const err = new Error(`Ace2Editor.init() error event while waiting for ${event} event`);
|
|
|
|
debugLog(`${err} on object`, obj);
|
|
|
|
cleanup();
|
|
|
|
reject(err);
|
|
|
|
};
|
|
|
|
cleanup = () => {
|
|
|
|
cleanup = () => {};
|
|
|
|
obj.removeEventListener(event, successCb);
|
|
|
|
obj.removeEventListener('error', errorCb);
|
|
|
|
};
|
|
|
|
cleanups.push(cleanup);
|
|
|
|
obj.addEventListener(event, successCb);
|
|
|
|
obj.addEventListener('error', errorCb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-19 09:35:43 +01:00
|
|
|
// Resolves when the frame's document is ready to be mutated. Browsers seem to be quirky about
|
|
|
|
// iframe ready events so this function throws the kitchen sink at the problem. Maybe one day we'll
|
|
|
|
// find a concise general solution.
|
2021-02-26 03:11:30 +01:00
|
|
|
const frameReady = async (frame) => {
|
|
|
|
// Can't do `const doc = frame.contentDocument;` because Firefox seems to asynchronously replace
|
|
|
|
// the document object after the frame is first created for some reason. ¯\_(ツ)_/¯
|
|
|
|
const doc = () => frame.contentDocument;
|
|
|
|
const cleanups = [];
|
|
|
|
try {
|
|
|
|
await Promise.race([
|
|
|
|
eventFired(frame, 'load', cleanups),
|
|
|
|
eventFired(frame.contentWindow, 'load', cleanups),
|
|
|
|
eventFired(doc(), 'load', cleanups),
|
|
|
|
eventFired(doc(), 'DOMContentLoaded', cleanups),
|
|
|
|
eventFired(doc(), 'readystatechange', cleanups, () => doc.readyState === 'complete'),
|
|
|
|
]);
|
|
|
|
} finally {
|
|
|
|
for (const cleanup of cleanups) cleanup();
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 05:59:50 +02:00
|
|
|
|
2021-01-29 06:43:53 +01:00
|
|
|
const Ace2Editor = function () {
|
2021-03-01 03:04:45 +01:00
|
|
|
let info = {editor: this};
|
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
|
|
|
|
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',
|
|
|
|
'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 =
|
2021-03-29 08:50:32 +02:00
|
|
|
() => loaded ? info.ace_getInInternationalComposition() : null;
|
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-02-26 03:11:30 +01:00
|
|
|
const addStyleTagsFor = (doc, files) => {
|
2021-03-01 08:21:22 +01:00
|
|
|
for (const file of files) {
|
2021-02-26 03:11:30 +01:00
|
|
|
const link = doc.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.type = 'text/css';
|
|
|
|
link.href = absUrl(encodeURI(file));
|
|
|
|
doc.head.appendChild(link);
|
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);
|
|
|
|
info = null; // prevent IE 6 closure memory leaks
|
|
|
|
});
|
|
|
|
|
2021-02-28 02:33:56 +01:00
|
|
|
this.init = async function (containerId, initialCode) {
|
2021-03-01 01:37:14 +01:00
|
|
|
debugLog('Ace2Editor.init()');
|
2021-01-29 06:43:53 +01:00
|
|
|
this.importText(initialCode);
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2021-03-01 08:21:22 +01:00
|
|
|
const includedCSS = [
|
2021-03-17 05:31:21 +01:00
|
|
|
`../static/css/iframe_editor.css?v=${clientVars.randomVersionString}`,
|
2021-03-01 08:21:22 +01:00
|
|
|
`../static/css/pad.css?v=${clientVars.randomVersionString}`,
|
|
|
|
...hooks.callAll('aceEditorCSS').map(
|
|
|
|
// Allow urls to external CSS - http(s):// and //some/path.css
|
|
|
|
(p) => /\/\//.test(p) ? p : `../static/plugins/${p}`),
|
|
|
|
`../static/skins/${clientVars.skinName}/pad.css?v=${clientVars.randomVersionString}`,
|
|
|
|
];
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2021-02-26 03:11:30 +01:00
|
|
|
const skinVariants = clientVars.skinVariants.split(' ').filter((x) => x !== '');
|
2021-02-26 07:37:33 +01:00
|
|
|
|
2021-02-26 03:11:30 +01:00
|
|
|
const outerFrame = document.createElement('iframe');
|
2021-02-26 07:37:33 +01:00
|
|
|
outerFrame.name = 'ace_outer';
|
|
|
|
outerFrame.frameBorder = 0; // for IE
|
|
|
|
outerFrame.title = 'Ether';
|
2021-03-19 09:35:43 +01:00
|
|
|
// Some browsers do strange things unless the iframe has a src or srcdoc property:
|
|
|
|
// - Firefox replaces the frame's contentWindow.document object with a different object after
|
|
|
|
// the frame is created. This can be worked around by waiting for the window's load event
|
|
|
|
// before continuing.
|
|
|
|
// - Chrome never fires any events on the frame or document. Eventually the document's
|
|
|
|
// readyState becomes 'complete' even though it never fires a readystatechange event.
|
|
|
|
// - Safari behaves like Chrome.
|
2021-07-20 18:50:18 +02:00
|
|
|
// srcdoc is avoided because Firefox's Content Security Policy engine does not properly handle
|
|
|
|
// 'self' with nested srcdoc iframes: https://bugzilla.mozilla.org/show_bug.cgi?id=1721296
|
|
|
|
outerFrame.src = '../static/empty.html';
|
2021-02-26 07:37:33 +01:00
|
|
|
info.frame = outerFrame;
|
|
|
|
document.getElementById(containerId).appendChild(outerFrame);
|
2021-02-26 03:11:30 +01:00
|
|
|
const outerWindow = outerFrame.contentWindow;
|
2021-02-26 03:11:30 +01:00
|
|
|
|
2021-03-01 01:37:14 +01:00
|
|
|
debugLog('Ace2Editor.init() waiting for outer frame');
|
2021-02-26 03:11:30 +01:00
|
|
|
await frameReady(outerFrame);
|
|
|
|
debugLog('Ace2Editor.init() outer frame ready');
|
|
|
|
|
2021-03-19 09:35:43 +01:00
|
|
|
// Firefox might replace the outerWindow.document object after iframe creation so this variable
|
|
|
|
// is assigned after the Window's load event.
|
2021-02-26 03:11:30 +01:00
|
|
|
const outerDocument = outerWindow.document;
|
|
|
|
|
|
|
|
// <html> tag
|
|
|
|
outerDocument.documentElement.classList.add('outer-editor', 'outerdoc', ...skinVariants);
|
|
|
|
|
|
|
|
// <head> tag
|
|
|
|
addStyleTagsFor(outerDocument, includedCSS);
|
|
|
|
const outerStyle = outerDocument.createElement('style');
|
|
|
|
outerStyle.type = 'text/css';
|
|
|
|
outerStyle.title = 'dynamicsyntax';
|
|
|
|
outerDocument.head.appendChild(outerStyle);
|
|
|
|
|
|
|
|
// <body> tag
|
|
|
|
outerDocument.body.id = 'outerdocbody';
|
|
|
|
outerDocument.body.classList.add('outerdocbody', ...pluginUtils.clientPluginNames());
|
|
|
|
const sideDiv = outerDocument.createElement('div');
|
|
|
|
sideDiv.id = 'sidediv';
|
|
|
|
sideDiv.classList.add('sidediv');
|
|
|
|
outerDocument.body.appendChild(sideDiv);
|
|
|
|
const lineMetricsDiv = outerDocument.createElement('div');
|
|
|
|
lineMetricsDiv.id = 'linemetricsdiv';
|
|
|
|
lineMetricsDiv.appendChild(outerDocument.createTextNode('x'));
|
|
|
|
outerDocument.body.appendChild(lineMetricsDiv);
|
|
|
|
|
|
|
|
const innerFrame = outerDocument.createElement('iframe');
|
|
|
|
innerFrame.name = 'ace_inner';
|
|
|
|
innerFrame.title = 'pad';
|
|
|
|
innerFrame.scrolling = 'no';
|
|
|
|
innerFrame.frameBorder = 0;
|
|
|
|
innerFrame.allowTransparency = true; // for IE
|
2021-03-19 09:35:43 +01:00
|
|
|
// The iframe MUST have a src or srcdoc property to avoid browser quirks. See the comment above
|
|
|
|
// outerFrame.srcdoc.
|
2021-07-20 18:50:18 +02:00
|
|
|
innerFrame.src = 'empty.html';
|
2021-02-26 03:11:30 +01:00
|
|
|
outerDocument.body.insertBefore(innerFrame, outerDocument.body.firstChild);
|
|
|
|
const innerWindow = innerFrame.contentWindow;
|
|
|
|
|
|
|
|
debugLog('Ace2Editor.init() waiting for inner frame');
|
|
|
|
await frameReady(innerFrame);
|
|
|
|
debugLog('Ace2Editor.init() inner frame ready');
|
|
|
|
|
2021-03-19 09:35:43 +01:00
|
|
|
// Firefox might replace the innerWindow.document object after iframe creation so this variable
|
|
|
|
// is assigned after the Window's load event.
|
2021-02-26 03:11:30 +01:00
|
|
|
const innerDocument = innerWindow.document;
|
|
|
|
|
|
|
|
// <html> tag
|
|
|
|
innerDocument.documentElement.classList.add('inner-editor', ...skinVariants);
|
|
|
|
|
|
|
|
// <head> tag
|
|
|
|
addStyleTagsFor(innerDocument, includedCSS);
|
|
|
|
const requireKernel = innerDocument.createElement('script');
|
|
|
|
requireKernel.type = 'text/javascript';
|
|
|
|
requireKernel.src =
|
|
|
|
absUrl(`../static/js/require-kernel.js?v=${clientVars.randomVersionString}`);
|
|
|
|
innerDocument.head.appendChild(requireKernel);
|
|
|
|
// Pre-fetch modules to improve load performance.
|
|
|
|
for (const module of ['ace2_inner', 'ace2_common']) {
|
|
|
|
const script = innerDocument.createElement('script');
|
|
|
|
script.type = 'text/javascript';
|
|
|
|
script.src = absUrl(`../javascripts/lib/ep_etherpad-lite/static/js/${module}.js` +
|
|
|
|
`?callback=require.define&v=${clientVars.randomVersionString}`);
|
|
|
|
innerDocument.head.appendChild(script);
|
|
|
|
}
|
|
|
|
const innerStyle = innerDocument.createElement('style');
|
|
|
|
innerStyle.type = 'text/css';
|
|
|
|
innerStyle.title = 'dynamicsyntax';
|
|
|
|
innerDocument.head.appendChild(innerStyle);
|
|
|
|
const headLines = [];
|
|
|
|
hooks.callAll('aceInitInnerdocbodyHead', {iframeHTML: headLines});
|
2021-06-18 06:52:34 +02:00
|
|
|
innerDocument.head.appendChild(
|
|
|
|
innerDocument.createRange().createContextualFragment(headLines.join('\n')));
|
2021-02-26 03:11:30 +01:00
|
|
|
|
|
|
|
// <body> tag
|
|
|
|
innerDocument.body.id = 'innerdocbody';
|
|
|
|
innerDocument.body.classList.add('innerdocbody');
|
|
|
|
innerDocument.body.setAttribute('spellcheck', 'false');
|
|
|
|
innerDocument.body.appendChild(innerDocument.createTextNode('\u00A0')); //
|
|
|
|
|
|
|
|
debugLog('Ace2Editor.init() waiting for require kernel load');
|
|
|
|
await eventFired(requireKernel, 'load');
|
|
|
|
debugLog('Ace2Editor.init() require kernel loaded');
|
|
|
|
const require = innerWindow.require;
|
|
|
|
require.setRootURI(absUrl('../javascripts/src'));
|
|
|
|
require.setLibraryURI(absUrl('../javascripts/lib'));
|
|
|
|
require.setGlobalKeyPath('require');
|
|
|
|
|
|
|
|
// intentially moved before requiring client_plugins to save a 307
|
|
|
|
innerWindow.Ace2Inner = require('ep_etherpad-lite/static/js/ace2_inner');
|
|
|
|
innerWindow.plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
|
|
|
|
innerWindow.plugins.adoptPluginsFromAncestorsOf(innerWindow);
|
|
|
|
|
|
|
|
innerWindow.$ = innerWindow.jQuery = require('ep_etherpad-lite/static/js/rjquery').jQuery;
|
|
|
|
|
|
|
|
debugLog('Ace2Editor.init() waiting for plugins');
|
|
|
|
await new Promise((resolve, reject) => innerWindow.plugins.ensure(
|
|
|
|
(err) => err != null ? reject(err) : resolve()));
|
|
|
|
debugLog('Ace2Editor.init() waiting for Ace2Inner.init()');
|
2021-03-20 01:50:01 +01:00
|
|
|
await innerWindow.Ace2Inner.init(info, {
|
|
|
|
inner: makeCSSManager(innerStyle.sheet),
|
|
|
|
outer: makeCSSManager(outerStyle.sheet),
|
|
|
|
parent: makeCSSManager(document.querySelector('style[title="dynamicsyntax"]').sheet),
|
|
|
|
});
|
2021-02-26 03:11:30 +01:00
|
|
|
debugLog('Ace2Editor.init() Ace2Inner.init() returned');
|
2021-02-28 02:33:56 +01:00
|
|
|
loaded = true;
|
|
|
|
doActionsPendingInit();
|
2021-03-01 01:37:14 +01:00
|
|
|
debugLog('Ace2Editor.init() done');
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
2021-01-29 06:43:53 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2012-01-16 02:23:48 +01:00
|
|
|
exports.Ace2Editor = Ace2Editor;
|