pad.libre-service.eu-etherpad/src/static/js/pad_editor.js

169 lines
5.6 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* 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 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.
*/
const Cookies = require('./pad_utils').Cookies;
2020-11-23 19:24:19 +01:00
const padcookie = require('./pad_cookie').padcookie;
const padutils = require('./pad_utils').padutils;
2011-03-26 14:10:41 +01:00
const padeditor = (() => {
2020-11-23 19:24:19 +01:00
let Ace2Editor = undefined;
let pad = undefined;
let settings = undefined;
const self = {
2011-07-07 19:59:34 +02:00
ace: null,
// this is accessed directly from other files
2011-03-26 14:10:41 +01:00
viewZoom: 100,
init: (readyFunc, initialViewOptions, _pad) => {
2012-03-07 02:27:03 +01:00
Ace2Editor = require('./ace').Ace2Editor;
pad = _pad;
settings = pad.settings;
2011-03-26 14:10:41 +01:00
const aceReady = () => {
2020-11-23 19:24:19 +01:00
$('#editorloadingbox').hide();
if (readyFunc) {
2011-03-26 14:10:41 +01:00
readyFunc();
}
};
2011-03-26 14:10:41 +01:00
self.ace = new Ace2Editor();
2020-11-23 19:24:19 +01:00
self.ace.init('editorcontainer', '', aceReady);
self.ace.setProperty('wraps', true);
if (pad.getIsDebugEnabled()) {
self.ace.setProperty('dmesg', pad.dmesg);
2011-03-26 14:10:41 +01:00
}
self.initViewOptions();
self.setViewOptions(initialViewOptions);
// view bar
2020-11-23 19:24:19 +01:00
$('#viewbarcontents').show();
2011-03-26 14:10:41 +01:00
},
initViewOptions: () => {
// Line numbers
2020-11-23 19:24:19 +01:00
padutils.bindCheckboxChange($('#options-linenoscheck'), () => {
pad.changeViewOption('showLineNumbers', padutils.getCheckbox($('#options-linenoscheck')));
2011-03-26 14:10:41 +01:00
});
// Author colors
2020-11-23 19:24:19 +01:00
padutils.bindCheckboxChange($('#options-colorscheck'), () => {
padcookie.setPref('showAuthorshipColors', padutils.getCheckbox('#options-colorscheck'));
pad.changeViewOption('showAuthorColors', padutils.getCheckbox('#options-colorscheck'));
2011-03-26 14:10:41 +01:00
});
// Right to left
2020-11-23 19:24:19 +01:00
padutils.bindCheckboxChange($('#options-rtlcheck'), () => {
pad.changeViewOption('rtlIsTrue', padutils.getCheckbox($('#options-rtlcheck')));
});
2020-11-23 19:24:19 +01:00
html10n.bind('localized', () => {
pad.changeViewOption('rtlIsTrue', ('rtl' === html10n.getDirection()));
padutils.setCheckbox($('#options-rtlcheck'), ('rtl' === html10n.getDirection()));
2020-11-23 19:24:19 +01:00
});
// font family change
2020-11-23 19:24:19 +01:00
$('#viewfontmenu').change(() => {
pad.changeViewOption('padFontFamily', $('#viewfontmenu').val());
2011-03-26 14:10:41 +01:00
});
// Language
2020-11-23 19:24:19 +01:00
html10n.bind('localized', () => {
$('#languagemenu').val(html10n.getLanguage());
// translate the value of 'unnamed' and 'Enter your name' textboxes in the userlist
// this does not interfere with html10n's normal value-setting because
// html10n just ingores <input>s
// also, a value which has been set by the user will be not overwritten
// since a user-edited <input> does *not* have the editempty-class
2020-11-23 19:24:19 +01:00
$('input[data-l10n-id]').each((key, input) => {
input = $(input);
2020-11-23 19:24:19 +01:00
if (input.hasClass('editempty')) {
input.val(html10n.get(input.attr('data-l10n-id')));
}
});
2020-11-23 19:24:19 +01:00
});
$('#languagemenu').val(html10n.getLanguage());
$('#languagemenu').change(() => {
Cookies.set('language', $('#languagemenu').val());
2020-12-16 22:51:43 +01:00
window.html10n.localize([$('#languagemenu').val(), 'en']);
if ($('select').niceSelect) {
$('select').niceSelect('update');
}
2012-11-12 16:49:15 +01:00
});
2011-03-26 14:10:41 +01:00
},
setViewOptions: (newOptions) => {
const getOption = (key, defaultValue) => {
2020-11-23 19:24:19 +01:00
const value = String(newOptions[key]);
if (value === 'true') return true;
if (value === 'false') return false;
2011-03-26 14:10:41 +01:00
return defaultValue;
};
2020-11-23 19:24:19 +01:00
let v;
2011-03-26 14:10:41 +01:00
v = getOption('rtlIsTrue', ('rtl' === html10n.getDirection()));
2020-11-23 19:24:19 +01:00
self.ace.setProperty('rtlIsTrue', v);
padutils.setCheckbox($('#options-rtlcheck'), v);
2012-10-29 22:55:57 +01:00
2011-03-26 14:10:41 +01:00
v = getOption('showLineNumbers', true);
2020-11-23 19:24:19 +01:00
self.ace.setProperty('showslinenumbers', v);
padutils.setCheckbox($('#options-linenoscheck'), v);
2011-03-26 14:10:41 +01:00
v = getOption('showAuthorColors', true);
2020-11-23 19:24:19 +01:00
self.ace.setProperty('showsauthorcolors', v);
$('#chattext').toggleClass('authorColors', v);
$('iframe[name="ace_outer"]').contents().find('#sidedivinner').toggleClass('authorColors', v);
2020-11-23 19:24:19 +01:00
padutils.setCheckbox($('#options-colorscheck'), v);
// Override from parameters if true
2020-11-23 19:24:19 +01:00
if (settings.noColors !== false) {
self.ace.setProperty('showsauthorcolors', !settings.noColors);
}
2020-11-23 19:24:19 +01:00
self.ace.setProperty('textface', newOptions.padFontFamily || '');
2011-03-26 14:10:41 +01:00
},
dispose: () => {
2020-11-23 19:24:19 +01:00
if (self.ace) {
2011-03-26 14:10:41 +01:00
self.ace.destroy();
self.ace = null;
2011-03-26 14:10:41 +01:00
}
},
enable: () => {
2020-11-23 19:24:19 +01:00
if (self.ace) {
self.ace.setEditable(true);
}
},
disable: () => {
2020-11-23 19:24:19 +01:00
if (self.ace) {
self.ace.setProperty('grayedOut', true);
2011-03-26 14:10:41 +01:00
self.ace.setEditable(false);
}
},
restoreRevisionText: (dataFromServer) => {
2011-03-26 14:10:41 +01:00
pad.addHistoricalAuthors(dataFromServer.historicalAuthorData);
self.ace.importAText(dataFromServer.atext, dataFromServer.apool, true);
2020-11-23 19:24:19 +01:00
},
2011-03-26 14:10:41 +01:00
};
return self;
})();
exports.padeditor = padeditor;