Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more hooks + some code refactoring #614

Merged
merged 13 commits into from
Apr 9, 2012
Merged
10 changes: 8 additions & 2 deletions src/node/handler/PadMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ var async = require("async");
var padManager = require("../db/PadManager");
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
var AttributeManager = require("ep_etherpad-lite/static/js/AttributeManager");
var authorManager = require("../db/AuthorManager");
var readOnlyManager = require("../db/ReadOnlyManager");
var settings = require('../utils/Settings');
var securityManager = require("../db/SecurityManager");
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins.js");
var log4js = require('log4js');
var messageLogger = log4js.getLogger("message");
var _ = require('underscore');

/**
* A associative array that translates a session to a pad
Expand Down Expand Up @@ -591,8 +593,12 @@ function _correctMarkersInPad(atext, apool) {
var offset = 0;
while (iter.hasNext()) {
var op = iter.next();
var listValue = Changeset.opAttributeValue(op, 'list', apool);
if (listValue) {

var hasMarker = _.find(AttributeManager.lineAttributes, function(attribute){
return Changeset.opAttributeValue(op, attribute, apool);
}) !== undefined;

if (hasMarker) {
for(var i=0;i<op.chars;i++) {
if (offset > 0 && text.charAt(offset-1) != '\n') {
badMarkers.push(offset);
Expand Down
8 changes: 8 additions & 0 deletions src/static/js/AttributeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var lineAttributes = [lineMarkerAttribute,'list'];
@param rep the document representation to be used
@param applyChangesetCallback this callback will be called
once a changeset has been built.


A document representation contains
- an array `alines` containing 1 attributes string for each line
- an Attribute pool `apool`
- a SkipList `lines` containing the text lines of the document.
*/

var AttributeManager = function(rep, applyChangesetCallback)
Expand All @@ -29,6 +35,8 @@ var AttributeManager = function(rep, applyChangesetCallback)
// it will be considered as a line marker
};

AttributeManager.lineAttributes = lineAttributes;

AttributeManager.prototype = _(AttributeManager.prototype).extend({

applyChangeset: function(changeset){
Expand Down
9 changes: 9 additions & 0 deletions src/static/js/ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ require.setGlobalKeyPath("require");\n\
$$INCLUDE_CSS("../static/css/iframe_editor.css");
$$INCLUDE_CSS("../static/css/pad.css");
$$INCLUDE_CSS("../static/custom/pad.css");

var additionalCSS = _(hooks.callAll("aceEditorCSS")).map(function(path){ return '../static/plugins/' + path });
includedCSS = includedCSS.concat(additionalCSS);

pushStyleTagsFor(iframeHTML, includedCSS);

var includedJS = [];
Expand Down Expand Up @@ -294,6 +298,11 @@ require.setGlobalKeyPath("require");\n\
$$INCLUDE_CSS("../static/css/iframe_editor.css");
$$INCLUDE_CSS("../static/css/pad.css");
$$INCLUDE_CSS("../static/custom/pad.css");


var additionalCSS = _(hooks.callAll("aceEditorCSS")).map(function(path){ return '../static/plugins/' + path });
includedCSS = includedCSS.concat(additionalCSS);

pushStyleTagsFor(outerHTML, includedCSS);

// bizarrely, in FF2, a file with no "external" dependencies won't finish loading properly
Expand Down
8 changes: 7 additions & 1 deletion src/static/js/ace2_inner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,7 @@ function Ace2Inner(){

_.each(hooks.callAll('aceRegisterBlockElements'), function(element){
_blockElems[element] = 1;
})
});

function isBlockElement(n)
{
Expand Down Expand Up @@ -3365,6 +3365,9 @@ function Ace2Inner(){
var thisLineListType = getLineListType(theLine);
var prevLineEntry = (theLine > 0 && rep.lines.atIndex(theLine - 1));
var prevLineBlank = (prevLineEntry && prevLineEntry.text.length == prevLineEntry.lineMarker);

var thisLineHasMarker = documentAttributeManager.lineHasMarker(theLine);

if (thisLineListType)
{
// this line is a list
Expand All @@ -3378,6 +3381,9 @@ function Ace2Inner(){
// delistify
performDocumentReplaceRange([theLine, 0], [theLine, lineEntry.lineMarker], '');
}
}else if (thisLineHasMarker && prevLineEntry){
// If the line has any attributes assigned, remove them by removing the marker '*'
performDocumentReplaceRange([theLine -1 , prevLineEntry.text.length], [theLine, lineEntry.lineMarker], '');
}
else if (theLine > 0)
{
Expand Down
59 changes: 37 additions & 22 deletions src/static/js/contentcollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _MAX_LIST_LEVEL = 8;

var Changeset = require('./Changeset');
var hooks = require('./pluginfw/hooks');
var _ = require('./underscore');

function sanitizeUnicode(s)
{
Expand Down Expand Up @@ -161,12 +162,6 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
var selection, startPoint, endPoint;
var selStart = [-1, -1],
selEnd = [-1, -1];
var blockElems = {
"div": 1,
"p": 1,
"pre": 1
};

function _isEmpty(node, state)
{
// consider clean blank lines pasted in IE to be empty
Expand All @@ -188,7 +183,7 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
{
var ln = lines.length() - 1;
var chr = lines.textOfLine(ln).length;
if (chr == 0 && state.listType && state.listType != 'none')
if (chr == 0 && !_.isEmpty(state.lineAttributes))
{
chr += 1; // listMarker
}
Expand Down Expand Up @@ -240,25 +235,30 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class

function _enterList(state, listType)
{
var oldListType = state.listType;
state.listLevel = (state.listLevel || 0) + 1;
var oldListType = state.lineAttributes['list'];
if (listType != 'none')
{
state.listNesting = (state.listNesting || 0) + 1;
}
state.listType = listType;

if(listType === 'none' || !listType ){
delete state.lineAttributes['list'];
}
else{
state.lineAttributes['list'] = listType;
}

_recalcAttribString(state);
return oldListType;
}

function _exitList(state, oldListType)
{
state.listLevel--;
if (state.listType != 'none')
if (state.lineAttributes['list'])
{
state.listNesting--;
}
state.listType = oldListType;
if(oldListType) state.lineAttributes['list'] = oldListType;
_recalcAttribString(state);
}

Expand Down Expand Up @@ -301,21 +301,29 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
state.attribString = Changeset.makeAttribsString('+', lst, apool);
}

function _produceListMarker(state)
function _produceLineAttributesMarker(state)
{
lines.appendText('*', Changeset.makeAttribsString('+', [
['list', state.listType],
// TODO: This has to go to AttributeManager.
var attributes = [
['lmkr', '1'],
['insertorder', 'first']
], apool));
].concat(
_.map(state.lineAttributes,function(value,key){
console.log([key, value])
return [key, value];
})
);

lines.appendText('*', Changeset.makeAttribsString('+', attributes , apool));
}
cc.startNewLine = function(state)
{
if (state)
{
var atBeginningOfLine = lines.textOfLine(lines.length() - 1).length == 0;
if (atBeginningOfLine && state.listType && state.listType != 'none')
if (atBeginningOfLine && !_.isEmpty(state.lineAttributes))
{
_produceListMarker(state);
_produceLineAttributesMarker(state);
}
}
lines.startNew();
Expand Down Expand Up @@ -345,7 +353,14 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
localAttribs: null,
attribs: { /*name -> nesting counter*/
},
attribString: ''
attribString: '',
// lineAttributes maintain a map from attributes to attribute values set on a line
lineAttributes: {
/*
example:
'list': 'bullet1',
*/
}
};
}
var localAttribs = state.localAttribs;
Expand Down Expand Up @@ -407,9 +422,9 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
// newlines in the source mustn't become spaces at beginning of line box
txt2 = txt2.replace(/^\n*/, '');
}
if (atBeginningOfLine && state.listType && state.listType != 'none')
if (atBeginningOfLine && !_.isEmpty(state.lineAttributes))
{
_produceListMarker(state);
_produceLineAttributesMarker(state);
}
lines.appendText(textify(txt2), state.attribString);
x += consumed;
Expand Down
31 changes: 25 additions & 6 deletions src/static/js/domline.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
var Security = require('./security');
var hooks = require('./pluginfw/hooks');
var _ = require('./underscore');

var lineAttributeMarker = require('./linestylefilter').lineAttributeMarker;
var Ace2Common = require('./ace2_common');
var noop = Ace2Common.noop;

Expand Down Expand Up @@ -82,7 +82,8 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
}

var html = [];
var preHtml, postHtml;
var preHtml = '',
postHtml = '';
var curHTML = null;

function processSpaces(s)
Expand All @@ -95,7 +96,9 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
var lineClass = 'ace-line';
result.appendSpan = function(txt, cls)
{
if (cls.indexOf('list') >= 0)
var processedMarker = false;
// Handle lineAttributeMarker, if present
if (cls.indexOf(lineAttributeMarker) >= 0)
{
var listType = /(?:^| )list:(\S+)/.exec(cls);
var start = /(?:^| )start:(\S+)/.exec(cls);
Expand All @@ -115,10 +118,26 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
preHtml = '<ol '+start+' class="list-' + Security.escapeHTMLAttribute(listType) + '"><li>';
postHtml = '</li></ol>';
}
}
}
processedMarker = true;
}

_.map(hooks.callAll("aceDomLineProcessLineAttributes", {
domline: domline,
cls: cls
}), function(modifier)
{
preHtml += modifier.preHtml;
postHtml += modifier.postHtml;
processedMarker |= modifier.processedMarker;
});

if( processedMarker ){
result.lineMarker += txt.length;
return; // don't append any text
}
}


}
var href = null;
var simpleTags = null;
Expand Down Expand Up @@ -203,7 +222,7 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
{
newHTML = (preHtml || '') + newHTML + (postHtml || '');
}
html = preHtml = postHtml = null; // free memory
html = preHtml = postHtml = ''; // free memory
if (newHTML !== curHTML)
{
curHTML = newHTML;
Expand Down
16 changes: 14 additions & 2 deletions src/static/js/linestylefilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var Changeset = require('./Changeset');
var hooks = require('./pluginfw/hooks');
var linestylefilter = {};
var _ = require('./underscore');
var AttributeManager = require('./AttributeManager');


linestylefilter.ATTRIB_CLASSES = {
'bold': 'tag:b',
Expand All @@ -40,6 +42,9 @@ linestylefilter.ATTRIB_CLASSES = {
'strikethrough': 'tag:s'
};

var lineAttributeMarker = 'lineAttribMarker';
exports.lineAttributeMarker = lineAttributeMarker;

linestylefilter.getAuthorClassName = function(author)
{
return "author-" + author.replace(/[^a-y0-9]/g, function(c)
Expand Down Expand Up @@ -68,14 +73,19 @@ linestylefilter.getLineStyleFilter = function(lineLength, aline, textAndClassFun
function attribsToClasses(attribs)
{
var classes = '';
var isLineAttribMarker = false;

Changeset.eachAttribNumber(attribs, function(n)
{
var key = apool.getAttribKey(n);
var key = apool.getAttribKey(n);
if (key)
{
var value = apool.getAttribValue(n);
if (value)
{
if (!isLineAttribMarker && _.indexOf(AttributeManager.lineAttributes, key) >= 0){
isLineAttribMarker = true;
}
if (key == 'author')
{
classes += ' ' + linestylefilter.getAuthorClassName(value);
Expand All @@ -99,10 +109,12 @@ linestylefilter.getLineStyleFilter = function(lineLength, aline, textAndClassFun
key: key,
value: value
}, " ", " ", "");
}
}
}
}
});

if(isLineAttribMarker) classes += ' ' + lineAttributeMarker;
return classes.substring(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/static/js/pad.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ var pad = {
if(padcookie.getPref("showAuthorshipColors") == false){
pad.changeViewOption('showAuthorColors', false);
}
hooks.aCallAll("postAceInit");
hooks.aCallAll("postAceInit", {ace: padeditor.ace});
}
},
dispose: function()
Expand Down
3 changes: 3 additions & 0 deletions src/static/js/skiplist.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ function SkipList()
var widthLoc = point.widthSkips[0] + point.nodes[0].downSkipWidths[0];
var newWidth = _entryWidth(entry);
p.mark("loop1");

// The new node will have at least level 1
// With a proability of 0.01^(n-1) the nodes level will be >= n
while (newNode.levels == 0 || Math.random() < 0.01)
{
var lvl = newNode.levels;
Expand Down