loleaflet/src/geometry/Bounds.js | 32 +++++++++++++++- loleaflet/src/layer/tile/TileLayer.js | 68 +++++++++++++++++----------------- 2 files changed, 65 insertions(+), 35 deletions(-)
New commits: commit 2a4f781e06170dd9f7ab519ee44aac9f25dd98c3 Author: Dennis Francis <dennis.fran...@collabora.com> AuthorDate: Sat Jun 6 17:30:27 2020 +0530 Commit: Dennis Francis <dennis.fran...@collabora.com> CommitDate: Mon Jul 6 19:02:07 2020 +0200 introduce L.Bounds.parseArray()... and reuse this to parse all rectangles in text-selection (own/view) message. Change-Id: Ic6946b2f10d9f642232d540b5974d53edbb774d7 Reviewed-on: https://gerrit.libreoffice.org/c/online/+/98153 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Tested-by: Jenkins Reviewed-by: Dennis Francis <dennis.fran...@collabora.com> diff --git a/loleaflet/src/geometry/Bounds.js b/loleaflet/src/geometry/Bounds.js index 1c10ea6d4..957e03fe1 100644 --- a/loleaflet/src/geometry/Bounds.js +++ b/loleaflet/src/geometry/Bounds.js @@ -16,7 +16,7 @@ L.Bounds = function (a, b) { //(Point, Point) or Point[] L.Bounds.parse = function (rectString) { // (string) -> Bounds if (typeof rectString !== 'string') { - console.error('invalid rectangle string'); + console.error('invalid input type, expected string'); return undefined; } @@ -33,6 +33,29 @@ L.Bounds.parse = function (rectString) { // (string) -> Bounds return new L.Bounds(refPoint1, refPoint2); }; +L.Bounds.parseArray = function (rectListString) { // (string) -> Bounds[] + + if (typeof rectListString !== 'string') { + console.error('invalid input type, expected string'); + return undefined; + } + + var parts = rectListString.match(/\d+/g); + if (parts === null || parts.length < 4) { + return []; + } + + var rectangles = []; + for (var i = 0; (i + 3) < parts.length; i += 4) { + var refPoint1 = new L.Point(parseInt(parts[i]), parseInt(parts[i + 1])); + var offset = new L.Point(parseInt(parts[i + 2]), parseInt(parts[i + 3])); + var refPoint2 = refPoint1.add(offset); + rectangles.push(new L.Bounds(refPoint1, refPoint2)); + } + + return rectangles; +}; + L.Bounds.prototype = { // extend the bounds to contain the given point extend: function (point) { // (Point) @@ -127,6 +150,13 @@ L.Bounds.prototype = { return this; }, + getPointArray: function () { // -> Point[] + return [ + this.getBottomLeft(), this.getBottomRight(), + this.getTopLeft(), this.getTopRight() + ]; + }, + toString: function () { return '[' + this.min.toString() + ', ' + diff --git a/loleaflet/src/layer/tile/TileLayer.js b/loleaflet/src/layer/tile/TileLayer.js index 0cba2697c..f374c40a6 100644 --- a/loleaflet/src/layer/tile/TileLayer.js +++ b/loleaflet/src/layer/tile/TileLayer.js @@ -1665,19 +1665,15 @@ L.TileLayer = L.GridLayer.extend({ }, _onTextSelectionMsg: function (textMsg) { - var strTwips = textMsg.match(/\d+/g); + + var rectArray = this._getTextSelectionRectangles(textMsg); this._selections.clearLayers(); - if (strTwips != null) { - var rectangles = []; - for (var i = 0; i < strTwips.length; i += 4) { - var topLeftTwips = new L.Point(parseInt(strTwips[i]), parseInt(strTwips[i + 1])); - var offset = new L.Point(parseInt(strTwips[i + 2]), parseInt(strTwips[i + 3])); - var bottomRightTwips = topLeftTwips.add(offset); - var boundsTwips = this._convertToTileTwipsSheetArea( - new L.Bounds(topLeftTwips, bottomRightTwips)); - rectangles.push([boundsTwips.getBottomLeft(), boundsTwips.getBottomRight(), - boundsTwips.getTopLeft(), boundsTwips.getTopRight()]); - } + + if (rectArray.length) { + + var rectangles = rectArray.map(function (rect) { + return rect.getPointArray(); + }); var polygons = L.PolyUtil.rectanglesToPolygons(rectangles, this); var selection = new L.Polygon(polygons, { @@ -1706,19 +1702,14 @@ L.TileLayer = L.GridLayer.extend({ return; } - var strTwips = obj.selection.match(/\d+/g); + var rectArray = this._getTextSelectionRectangles(obj.selection); this._viewSelections[viewId] = this._viewSelections[viewId] || {}; - if (strTwips != null) { - var rectangles = []; - for (var i = 0; i < strTwips.length; i += 4) { - var topLeftTwips = new L.Point(parseInt(strTwips[i]), parseInt(strTwips[i + 1])); - var offset = new L.Point(parseInt(strTwips[i + 2]), parseInt(strTwips[i + 3])); - var bottomRightTwips = topLeftTwips.add(offset); - var boundsTwips = this._convertToTileTwipsSheetArea( - new L.Bounds(topLeftTwips, bottomRightTwips)); - rectangles.push([boundsTwips.getBottomLeft(), boundsTwips.getBottomRight(), - boundsTwips.getTopLeft(), boundsTwips.getTopRight()]); - } + + if (rectArray.length) { + + var rectangles = rectArray.map(function (rect) { + return rect.getPointArray(); + }); this._viewSelections[viewId].part = viewPart; this._viewSelections[viewId].polygons = L.PolyUtil.rectanglesToPolygons(rectangles, this); @@ -1821,12 +1812,11 @@ L.TileLayer = L.GridLayer.extend({ }, _onTextSelectionEndMsg: function (textMsg) { - var strTwips = textMsg.match(/\d+/g); - if (strTwips != null && this._map._permission === 'edit') { - var topLeftTwips = new L.Point(parseInt(strTwips[0]), parseInt(strTwips[1])); - var offset = new L.Point(parseInt(strTwips[2]), parseInt(strTwips[3])); - var bottomRightTwips = topLeftTwips.add(offset); + var rectangles = this._getTextSelectionRectangles(textMsg); + if (rectangles.length && this._map._permission === 'edit') { + var topLeftTwips = rectangles[0].getTopLeft(); + var bottomRightTwips = rectangles[0].getBottomRight(); var oldSelection = this._textSelectionEnd; this._textSelectionEnd = new L.LatLngBounds( this._twipsToLatLng(topLeftTwips, this._map.getZoom()), @@ -1840,11 +1830,11 @@ L.TileLayer = L.GridLayer.extend({ }, _onTextSelectionStartMsg: function (textMsg) { - var strTwips = textMsg.match(/\d+/g); - if (strTwips != null && this._map._permission === 'edit') { - var topLeftTwips = new L.Point(parseInt(strTwips[0]), parseInt(strTwips[1])); - var offset = new L.Point(parseInt(strTwips[2]), parseInt(strTwips[3])); - var bottomRightTwips = topLeftTwips.add(offset); + var rectangles = this._getTextSelectionRectangles(textMsg); + + if (rectangles.length && this._map._permission === 'edit') { + var topLeftTwips = rectangles[0].getTopLeft(); + var bottomRightTwips = rectangles[0].getBottomRight(); var oldSelection = this._textSelectionStart; //FIXME: The selection is really not two points, as they can be //FIXME: on top of each other, but on separate lines. We should @@ -3412,6 +3402,16 @@ L.TileLayer = L.GridLayer.extend({ return L.Bounds.parse(msgObj.rectangle); }, + _getTextSelectionRectangles: function (textMsg) { + + if (typeof textMsg !== 'string') { + console.error('invalid text selection message'); + return []; + } + + return L.Bounds.parseArray(textMsg); + }, + _debugGetTimeArray: function() { return {count: 0, ms: 0, best: Number.MAX_SAFE_INTEGER, worst: 0, date: 0}; }, _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits