From d40d59d9eb6754a1be745a3c76e3e4718865ba1a Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 10 Apr 2021 03:46:29 -0400 Subject: [PATCH] AttributeManager: Simplify logic --- src/static/js/AttributeManager.js | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/static/js/AttributeManager.js b/src/static/js/AttributeManager.js index c89fc410f..0c9eb84e1 100644 --- a/src/static/js/AttributeManager.js +++ b/src/static/js/AttributeManager.js @@ -68,10 +68,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({ // see https://github.com/ether/etherpad-lite/issues/2772 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]; - + const [startCol, endCol] = this._findRowRange(row, start, end); const rowChangeset = this._setAttributesOnRangeByLine(row, startCol, endCol, attribs); // compose changesets of all rows into a single changeset @@ -89,26 +86,12 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({ }, _findRowRange(row, start, end) { - let startCol, endCol; - - 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" - } - + // Subtract 1 for the end-of-line '\n' (it is never selected). + const lineLength = + this.rep.lines.offsetOfIndex(row + 1) - this.rep.lines.offsetOfIndex(row) - 1; + const markerWidth = this.lineHasMarker(row) ? 1 : 0; + const startCol = row === start[0] ? start[1] : markerWidth; + const endCol = row === end[0] ? end[1] : lineLength; return [startCol, endCol]; },