etherpad-lite/static/js/pad_editor.js

153 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* 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.
*/
2011-07-07 19:59:34 +02:00
var padeditor = (function()
{
2011-03-26 14:10:41 +01:00
var 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,
2011-07-07 19:59:34 +02:00
init: function(readyFunc, initialViewOptions)
{
2011-03-26 14:10:41 +01:00
2011-07-07 19:59:34 +02:00
function aceReady()
{
2011-03-26 14:10:41 +01:00
$("#editorloadingbox").hide();
2011-07-07 19:59:34 +02:00
if (readyFunc)
{
2011-03-26 14:10:41 +01:00
readyFunc();
}
}
self.ace = new Ace2Editor();
self.ace.init("editorcontainer", "", aceReady);
self.ace.setProperty("wraps", true);
2011-07-07 19:59:34 +02:00
if (pad.getIsDebugEnabled())
{
2011-03-26 14:10:41 +01:00
self.ace.setProperty("dmesg", pad.dmesg);
}
self.initViewOptions();
self.setViewOptions(initialViewOptions);
// view bar
self.initViewZoom();
$("#viewbarcontents").show();
},
2011-07-07 19:59:34 +02:00
initViewOptions: function()
{
padutils.bindCheckboxChange($("#options-linenoscheck"), function()
{
pad.changeViewOption('showLineNumbers', padutils.getCheckbox($("#options-linenoscheck")));
2011-03-26 14:10:41 +01:00
});
2011-07-07 19:59:34 +02:00
padutils.bindCheckboxChange($("#options-colorscheck"), function()
{
pad.changeViewOption('showAuthorColors', padutils.getCheckbox("#options-colorscheck"));
2011-03-26 14:10:41 +01:00
});
2011-07-07 19:59:34 +02:00
$("#viewfontmenu").change(function()
{
pad.changeViewOption('useMonospaceFont', $("#viewfontmenu").val() == 'monospace');
2011-03-26 14:10:41 +01:00
});
noColors = !noColors; // Inversed so we can pass it to showauthorcolors
2011-03-26 14:10:41 +01:00
},
2011-07-07 19:59:34 +02:00
setViewOptions: function(newOptions)
{
function getOption(key, defaultValue)
{
2011-03-26 14:10:41 +01:00
var value = String(newOptions[key]);
if (value == "true") return true;
if (value == "false") return false;
return defaultValue;
}
var v;
v = getOption('showLineNumbers', true);
self.ace.setProperty("showslinenumbers", v);
padutils.setCheckbox($("#options-linenoscheck"), v);
v = getOption('showAuthorColors', true);
self.ace.setProperty("showsauthorcolors", v);
padutils.setCheckbox($("#options-colorscheck"), v);
v = getOption('useMonospaceFont', false);
2011-07-07 19:59:34 +02:00
self.ace.setProperty("textface", (v ? "monospace" : "Arial, sans-serif"));
2011-03-26 14:10:41 +01:00
$("#viewfontmenu").val(v ? "monospace" : "normal");
self.ace.setProperty("showsauthorcolors", noColors);
2011-12-04 19:55:35 +01:00
self.ace.setProperty("rtlIsTrue", rtlIsTrue);
2011-03-26 14:10:41 +01:00
},
2011-07-07 19:59:34 +02:00
initViewZoom: function()
{
2011-03-26 14:10:41 +01:00
var viewZoom = Number(padcookie.getPref('viewZoom'));
2011-07-07 19:59:34 +02:00
if ((!viewZoom) || isNaN(viewZoom))
{
2011-03-26 14:10:41 +01:00
viewZoom = 100;
}
self.setViewZoom(viewZoom);
2011-07-07 19:59:34 +02:00
$("#viewzoommenu").change(function(evt)
{
2011-03-26 14:10:41 +01:00
// strip initial 'z' from val
self.setViewZoom(Number($("#viewzoommenu").val().substring(1)));
});
},
2011-07-07 19:59:34 +02:00
setViewZoom: function(percent)
{
if (!(percent >= 50 && percent <= 1000))
{
2011-03-26 14:10:41 +01:00
// percent is out of sane range or NaN (which fails comparisons)
return;
}
self.viewZoom = percent;
2011-07-07 19:59:34 +02:00
$("#viewzoommenu").val('z' + percent);
2011-03-26 14:10:41 +01:00
var baseSize = 13;
2011-07-07 19:59:34 +02:00
self.ace.setProperty('textsize', Math.round(baseSize * self.viewZoom / 100));
2011-03-26 14:10:41 +01:00
padcookie.setPref('viewZoom', percent);
},
2011-07-07 19:59:34 +02:00
dispose: function()
{
if (self.ace)
{
2011-03-26 14:10:41 +01:00
self.ace.destroy();
}
},
2011-07-07 19:59:34 +02:00
disable: function()
{
if (self.ace)
{
2011-03-26 14:10:41 +01:00
self.ace.setProperty("grayedOut", true);
self.ace.setEditable(false);
}
},
2011-07-07 19:59:34 +02:00
restoreRevisionText: function(dataFromServer)
{
2011-03-26 14:10:41 +01:00
pad.addHistoricalAuthors(dataFromServer.historicalAuthorData);
self.ace.importAText(dataFromServer.atext, dataFromServer.apool, true);
}
};
return self;
}());