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

410 lines
14 KiB
JavaScript
Raw Normal View History

2020-11-23 19:24:19 +01:00
const Changeset = require('./Changeset');
const ChangesetUtils = require('./ChangesetUtils');
const _ = require('./underscore');
2012-04-05 00:50:04 +02:00
2020-11-23 19:24:19 +01:00
const lineMarkerAttribute = 'lmkr';
// Some of these attributes are kept for compatibility purposes.
// Not sure if we need all of them
2020-11-23 19:24:19 +01:00
const DEFAULT_LINE_ATTRIBUTES = ['author', 'lmkr', 'insertorder', 'start'];
// If one of these attributes are set to the first character of a
// line it is considered as a line attribute marker i.e. attributes
// set on this marker are applied to the whole line.
// The list attribute is only maintained for compatibility reasons
2020-11-23 19:24:19 +01:00
const lineAttributes = [lineMarkerAttribute, 'list'];
/*
The Attribute manager builds changesets based on a document
representation for setting and removing range or line-based attributes.
@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
2012-04-08 21:21:05 +02:00
- an Attribute pool `apool`
- a SkipList `lines` containing the text lines of the document.
*/
2020-11-23 19:24:19 +01:00
const AttributeManager = function (rep, applyChangesetCallback) {
2012-04-05 00:50:04 +02:00
this.rep = rep;
this.applyChangesetCallback = applyChangesetCallback;
this.author = '';
2012-04-05 01:07:47 +02:00
// If the first char in a line has one of the following attributes
// it will be considered as a line marker
2012-04-05 00:50:04 +02:00
};
AttributeManager.DEFAULT_LINE_ATTRIBUTES = DEFAULT_LINE_ATTRIBUTES;
AttributeManager.lineAttributes = lineAttributes;
2012-04-05 00:50:04 +02:00
AttributeManager.prototype = _(AttributeManager.prototype).extend({
2020-11-23 19:24:19 +01:00
applyChangeset(changeset) {
if (!this.applyChangesetCallback) return changeset;
2020-11-23 19:24:19 +01:00
const cs = changeset.toString();
if (!Changeset.isIdentity(cs)) {
2012-04-05 00:50:04 +02:00
this.applyChangesetCallback(cs);
}
return changeset;
2012-04-05 00:50:04 +02:00
},
/*
Sets attributes on a range
@param start [row, col] tuple pointing to the start of the range
@param end [row, col] tuple pointing to the end of the range
@param attribs: an array of attributes
*/
2020-11-23 19:24:19 +01:00
setAttributesOnRange(start, end, attribs) {
// instead of applying the attributes to the whole range at once, we need to apply them
// line by line, to be able to disregard the "*" used as line marker. For more details,
// see https://github.com/ether/etherpad-lite/issues/2772
2020-11-23 19:24:19 +01:00
let allChangesets;
for (let row = start[0]; row <= end[0]; row++) {
const rowRange = this._findRowRange(row, start, end);
const startCol = rowRange[0];
const endCol = rowRange[1];
2020-11-23 19:24:19 +01:00
const rowChangeset = this._setAttributesOnRangeByLine(row, startCol, endCol, attribs);
// compose changesets of all rows into a single changeset, as the range might not be continuous
// due to the presence of line markers on the rows
if (allChangesets) {
allChangesets = Changeset.compose(allChangesets.toString(), rowChangeset.toString(), this.rep.apool);
} else {
allChangesets = rowChangeset;
}
}
return this.applyChangeset(allChangesets);
},
2020-11-23 19:24:19 +01:00
_findRowRange(row, start, end) {
let startCol, endCol;
2020-11-23 19:24:19 +01:00
const startLineOffset = this.rep.lines.offsetOfIndex(row);
const endLineOffset = this.rep.lines.offsetOfIndex(row + 1);
const lineLength = endLineOffset - startLineOffset;
// find column where range on this row starts
if (row === start[0]) { // are we on the first row of range?
startCol = start[1];
} else {
startCol = this.lineHasMarker(row) ? 1 : 0; // remove "*" used as line marker
}
// find column where range on this row ends
if (row === end[0]) { // are we on the last row of range?
endCol = end[1]; // if so, get the end of range, not end of row
} else {
endCol = lineLength - 1; // remove "\n"
}
return [startCol, endCol];
},
/*
Sets attributes on a range, by line
@param row the row where range is
@param startCol column where range starts
@param endCol column where range ends
@param attribs: an array of attributes
*/
2020-11-23 19:24:19 +01:00
_setAttributesOnRangeByLine(row, startCol, endCol, attribs) {
const builder = Changeset.builder(this.rep.lines.totalWidth());
ChangesetUtils.buildKeepToStartOfRange(this.rep, builder, [row, startCol]);
ChangesetUtils.buildKeepRange(this.rep, builder, [row, startCol], [row, endCol], attribs, this.rep.apool);
return builder;
},
/*
Returns if the line already has a line marker
@param lineNum: the number of the line
*/
2020-11-23 19:24:19 +01:00
lineHasMarker(lineNum) {
const that = this;
2020-11-23 19:24:19 +01:00
return _.find(lineAttributes, (attribute) => that.getAttributeOnLine(lineNum, attribute) != '') !== undefined;
2012-04-05 00:50:04 +02:00
},
/*
Gets a specified attribute on a line
@param lineNum: the number of the line to set the attribute for
@param attributeKey: the name of the attribute to get, e.g. list
*/
2020-11-23 19:24:19 +01:00
getAttributeOnLine(lineNum, attributeName) {
2012-04-05 00:50:04 +02:00
// get `attributeName` attribute of first char of line
2020-11-23 19:24:19 +01:00
const aline = this.rep.alines[lineNum];
if (aline) {
const opIter = Changeset.opIterator(aline);
if (opIter.hasNext()) {
2012-04-05 00:50:04 +02:00
return Changeset.opAttributeValue(opIter.next(), attributeName, this.rep.apool) || '';
}
}
return '';
},
/*
Gets all attributes on a line
@param lineNum: the number of the line to get the attribute for
*/
2020-11-23 19:24:19 +01:00
getAttributesOnLine(lineNum) {
// get attributes of first char of line
2020-11-23 19:24:19 +01:00
const aline = this.rep.alines[lineNum];
const attributes = [];
if (aline) {
const opIter = Changeset.opIterator(aline);
let op;
if (opIter.hasNext()) {
op = opIter.next();
if (!op.attribs) return [];
Changeset.eachAttribNumber(op.attribs, (n) => {
attributes.push([this.rep.apool.getAttribKey(n), this.rep.apool.getAttribValue(n)]);
});
return attributes;
}
}
return [];
},
2020-11-23 19:24:19 +01:00
/*
2016-03-26 15:00:34 +01:00
Gets a given attribute on a selection
@param attributeName
@param prevChar
returns true or false if an attribute is visible in range
*/
2020-11-23 19:24:19 +01:00
getAttributeOnSelection(attributeName, prevChar) {
const rep = this.rep;
if (!(rep.selStart && rep.selEnd)) return;
2016-03-26 15:00:34 +01:00
// If we're looking for the caret attribute not the selection
// has the user already got a selection or is this purely a caret location?
2020-11-23 19:24:19 +01:00
const isNotSelection = (rep.selStart[0] == rep.selEnd[0] && rep.selEnd[1] === rep.selStart[1]);
if (isNotSelection) {
if (prevChar) {
2016-03-26 15:00:34 +01:00
// If it's not the start of the line
2020-11-23 19:24:19 +01:00
if (rep.selStart[1] !== 0) {
2016-03-26 15:00:34 +01:00
rep.selStart[1]--;
}
}
}
2020-11-23 19:24:19 +01:00
const withIt = Changeset.makeAttribsString('+', [
[attributeName, 'true'],
2016-03-26 15:00:34 +01:00
], rep.apool);
2020-11-23 19:24:19 +01:00
const withItRegex = new RegExp(`${withIt.replace(/\*/g, '\\*')}(\\*|$)`);
function hasIt(attribs) {
2016-03-26 15:00:34 +01:00
return withItRegex.test(attribs);
}
2020-11-23 19:24:19 +01:00
return rangeHasAttrib(rep.selStart, rep.selEnd);
2016-03-26 15:00:34 +01:00
function rangeHasAttrib(selStart, selEnd) {
// if range is collapsed -> no attribs in range
2020-11-23 19:24:19 +01:00
if (selStart[1] == selEnd[1] && selStart[0] == selEnd[0]) return false;
2016-03-26 15:00:34 +01:00
2020-11-23 19:24:19 +01:00
if (selStart[0] != selEnd[0]) { // -> More than one line selected
var hasAttrib = true;
2016-03-26 15:00:34 +01:00
// from selStart to the end of the first line
2020-11-23 19:24:19 +01:00
hasAttrib = hasAttrib && rangeHasAttrib(selStart, [selStart[0], rep.lines.atIndex(selStart[0]).text.length]);
2016-03-26 15:00:34 +01:00
// for all lines in between
2020-11-23 19:24:19 +01:00
for (let n = selStart[0] + 1; n < selEnd[0]; n++) {
hasAttrib = hasAttrib && rangeHasAttrib([n, 0], [n, rep.lines.atIndex(n).text.length]);
2016-03-26 15:00:34 +01:00
}
// for the last, potentially partial, line
2020-11-23 19:24:19 +01:00
hasAttrib = hasAttrib && rangeHasAttrib([selEnd[0], 0], [selEnd[0], selEnd[1]]);
2016-03-26 15:00:34 +01:00
2020-11-23 19:24:19 +01:00
return hasAttrib;
2016-03-26 15:00:34 +01:00
}
// Logic tells us we now have a range on a single line
2020-11-23 19:24:19 +01:00
const lineNum = selStart[0];
const start = selStart[1];
const end = selEnd[1];
var hasAttrib = true;
2016-03-26 15:00:34 +01:00
// Iterate over attribs on this line
2020-11-23 19:24:19 +01:00
const opIter = Changeset.opIterator(rep.alines[lineNum]);
let indexIntoLine = 0;
2016-03-26 15:00:34 +01:00
while (opIter.hasNext()) {
2020-11-23 19:24:19 +01:00
const op = opIter.next();
const opStartInLine = indexIntoLine;
const opEndInLine = opStartInLine + op.chars;
2016-03-26 15:00:34 +01:00
if (!hasIt(op.attribs)) {
// does op overlap selection?
if (!(opEndInLine <= start || opStartInLine >= end)) {
hasAttrib = false; // since it's overlapping but hasn't got the attrib -> range hasn't got it
break;
}
}
indexIntoLine = opEndInLine;
}
2020-11-23 19:24:19 +01:00
return hasAttrib;
2016-03-26 15:00:34 +01:00
}
},
/*
Gets all attributes at a position containing line number and column
@param lineNumber starting with zero
@param column starting with zero
returns a list of attributes in the format
[ ["key","value"], ["key","value"], ... ]
*/
2020-11-23 19:24:19 +01:00
getAttributesOnPosition(lineNumber, column) {
// get all attributes of the line
2020-11-23 19:24:19 +01:00
const aline = this.rep.alines[lineNumber];
if (!aline) {
2020-11-23 19:24:19 +01:00
return [];
}
// iterate through all operations of a line
2020-11-23 19:24:19 +01:00
const opIter = Changeset.opIterator(aline);
// we need to sum up how much characters each operations take until the wanted position
2020-11-23 19:24:19 +01:00
let currentPointer = 0;
const attributes = [];
let currentOperation;
while (opIter.hasNext()) {
currentOperation = opIter.next();
2020-11-23 19:24:19 +01:00
currentPointer += currentOperation.chars;
if (currentPointer > column) {
// we got the operation of the wanted position, now collect all its attributes
2020-11-23 19:24:19 +01:00
Changeset.eachAttribNumber(currentOperation.attribs, (n) => {
attributes.push([
this.rep.apool.getAttribKey(n),
2020-11-23 19:24:19 +01:00
this.rep.apool.getAttribValue(n),
]);
2020-11-23 19:24:19 +01:00
});
// skip the loop
return attributes;
}
}
return attributes;
},
/*
Gets all attributes at caret position
if the user selected a range, the start of the selection is taken
returns a list of attributes in the format
2015-03-31 10:58:47 +02:00
[ ["key","value"], ["key","value"], ... ]
*/
2020-11-23 19:24:19 +01:00
getAttributesOnCaret() {
return this.getAttributesOnPosition(this.rep.selStart[0], this.rep.selStart[1]);
},
2012-04-05 00:50:04 +02:00
/*
Sets a specified attribute on a line
@param lineNum: the number of the line to set the attribute for
@param attributeKey: the name of the attribute to set, e.g. list
@param attributeValue: an optional parameter to pass to the attribute (e.g. indention level)
2012-04-05 00:50:04 +02:00
*/
2020-11-23 19:24:19 +01:00
setAttributeOnLine(lineNum, attributeName, attributeValue) {
let loc = [0, 0];
const builder = Changeset.builder(this.rep.lines.totalWidth());
const hasMarker = this.lineHasMarker(lineNum);
2012-04-05 00:50:04 +02:00
ChangesetUtils.buildKeepRange(this.rep, builder, loc, (loc = [lineNum, 0]));
2020-11-23 19:24:19 +01:00
if (hasMarker) {
2012-04-05 00:50:04 +02:00
ChangesetUtils.buildKeepRange(this.rep, builder, loc, (loc = [lineNum, 1]), [
2020-11-23 19:24:19 +01:00
[attributeName, attributeValue],
], this.rep.apool);
} else {
// add a line marker
builder.insert('*', [
['author', this.author],
['insertorder', 'first'],
[lineMarkerAttribute, '1'],
[attributeName, attributeValue],
2012-04-05 00:50:04 +02:00
], this.rep.apool);
}
2012-04-05 00:50:04 +02:00
return this.applyChangeset(builder);
},
2020-11-23 19:24:19 +01:00
/**
2015-03-06 23:02:31 +01:00
* Removes a specified attribute on a line
* @param lineNum the number of the affected line
* @param attributeName the name of the attribute to remove, e.g. list
* @param attributeValue if given only attributes with equal value will be removed
2012-04-05 00:50:04 +02:00
*/
2020-11-23 19:24:19 +01:00
removeAttributeOnLine(lineNum, attributeName, attributeValue) {
const builder = Changeset.builder(this.rep.lines.totalWidth());
const hasMarker = this.lineHasMarker(lineNum);
let found = false;
const attribs = _(this.getAttributesOnLine(lineNum)).map((attrib) => {
2020-11-23 19:24:19 +01:00
if (attrib[0] === attributeName && (!attributeValue || attrib[0] === attributeValue)) {
found = true;
return [attributeName, ''];
} else if (attrib[0] === 'author') {
// update last author to make changes to line attributes on this line
return [attributeName, this.author];
}
return attrib;
});
if (!found) {
return;
}
ChangesetUtils.buildKeepToStartOfRange(this.rep, builder, [lineNum, 0]);
const countAttribsWithMarker = _.chain(attribs).filter((a) => !!a[1])
.map((a) => a[0]).difference(DEFAULT_LINE_ATTRIBUTES).size().value();
// if we have marker and any of attributes don't need to have marker. we need delete it
if (hasMarker && !countAttribsWithMarker) {
ChangesetUtils.buildRemoveRange(this.rep, builder, [lineNum, 0], [lineNum, 1]);
} else {
ChangesetUtils.buildKeepRange(this.rep, builder, [lineNum, 0], [lineNum, 1], attribs, this.rep.apool);
}
return this.applyChangeset(builder);
},
/*
Toggles a line attribute for the specified line number
If a line attribute with the specified name exists with any value it will be removed
Otherwise it will be set to the given value
@param lineNum: the number of the line to toggle the attribute for
@param attributeKey: the name of the attribute to toggle, e.g. list
@param attributeValue: the value to pass to the attribute (e.g. indention level)
2012-04-05 00:50:04 +02:00
*/
2020-11-23 19:24:19 +01:00
toggleAttributeOnLine(lineNum, attributeName, attributeValue) {
return this.getAttributeOnLine(lineNum, attributeName)
? this.removeAttributeOnLine(lineNum, attributeName)
: this.setAttributeOnLine(lineNum, attributeName, attributeValue);
},
2020-11-23 19:24:19 +01:00
hasAttributeOnSelectionOrCaretPosition(attributeName) {
const hasSelection = ((this.rep.selStart[0] !== this.rep.selEnd[0]) || (this.rep.selEnd[1] !== this.rep.selStart[1]));
let hasAttrib;
if (hasSelection) {
hasAttrib = this.getAttributeOnSelection(attributeName);
2020-11-23 19:24:19 +01:00
} else {
const attributesOnCaretPosition = this.getAttributesOnCaret();
hasAttrib = _.contains(_.flatten(attributesOnCaretPosition), attributeName);
}
return hasAttrib;
},
2012-04-05 00:50:04 +02:00
});
2015-03-06 23:02:31 +01:00
module.exports = AttributeManager;