From 702acd0a7bba8e3972c29569de6eb963105fbc73 Mon Sep 17 00:00:00 2001 From: jokd Date: Fri, 31 Mar 2023 12:15:03 +0200 Subject: [PATCH 1/2] fix: update zoom level when scroll zooming --- src/controls/print/print-component.js | 26 ++++++++++++++++++++++++- src/controls/print/print-settings.js | 1 + src/controls/print/set-scale-control.js | 3 +++ src/maputils.js | 4 ++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/controls/print/print-component.js b/src/controls/print/print-component.js index 9b3bea125..3136a8444 100644 --- a/src/controls/print/print-component.js +++ b/src/controls/print/print-component.js @@ -79,12 +79,14 @@ const PrintComponent = function PrintComponent(options = {}) { let descriptionAlign = `text-align-${descriptionAlignment}`; let viewerMapTarget; let today = new Date(Date.now()); + let initZoom; let printScale = 0; let widthImage = 0; let heightImage = 0; const originalResolutions = viewer.getResolutions().map(item => item); const originalGrids = new Map(); const deviceOnIos = isOnIos(); + const view = map.getView(); // eslint-disable-next-line no-underscore-dangle const olPixelRatio = map.pixelRatio_; @@ -341,8 +343,28 @@ const PrintComponent = function PrintComponent(options = {}) { printToolbar.setDisabled(false); } + function updateScaleOnMove() { + if (initZoom !== view.getZoom()) { + initZoom = view.getZoom(); + const res = view.getResolution(); + const projection = view.getProjection(); + const scale = res * getPointResolution( + projection, + resolution / 25.4, + view.getCenter() + ); + let textContent = scale; + textContent = maputils.formatScale(scale * 1000); + printSettings.getScaleControl().setButtonText(textContent); + setScale(scale); + } + } + return Component({ name: 'printComponent', + getResolution() { + return resolution; + }, onInit() { this.on('render', this.onRender); this.addComponent(printSettings); @@ -469,6 +491,7 @@ const PrintComponent = function PrintComponent(options = {}) { } unByKey(mapLoadListenRefs[0]); unByKey(mapLoadListenRefs[1]); + unByKey(mapLoadListenRefs[2]); // GH-1537: remove layers temporarily added for print and unhide layers hidden for print viewer.getLayersByProperty('added-for-print', true).forEach((l) => viewer.removeLayer(l)); viewer.getLayersByProperty('hidden-for-print', true).forEach((l) => { @@ -571,7 +594,8 @@ const PrintComponent = function PrintComponent(options = {}) { function addMapLoadListeners() { const startEvRef = map.on('loadstart', disablePrintToolbar); const endEvRef = map.on('loadend', enablePrintToolbar); - return [startEvRef, endEvRef]; + const endMoveRef = map.on('moveend', updateScaleOnMove); + return [startEvRef, endEvRef, endMoveRef]; } mapLoadListenRefs = addMapLoadListeners(); printScale = 0; diff --git a/src/controls/print/print-settings.js b/src/controls/print/print-settings.js index 329d5da8b..80d1f4dc3 100644 --- a/src/controls/print/print-settings.js +++ b/src/controls/print/print-settings.js @@ -110,6 +110,7 @@ const PrintSettings = function PrintSettings(options = {}) { return Component({ close, + getScaleControl() { return setScaleControl; }, onInit() { openButton = Button({ cls: 'padding-small icon-smaller round light box-shadow', diff --git a/src/controls/print/set-scale-control.js b/src/controls/print/set-scale-control.js index df6441677..8ab8ede4c 100644 --- a/src/controls/print/set-scale-control.js +++ b/src/controls/print/set-scale-control.js @@ -57,6 +57,9 @@ export default function SetScaleControl(map, options = {}) {
${selectScale.render()}
`; + }, + setButtonText(buttonText) { + selectScale.setButtonText(buttonText); } }); } diff --git a/src/maputils.js b/src/maputils.js index 634985529..419be566b 100644 --- a/src/maputils.js +++ b/src/maputils.js @@ -128,6 +128,10 @@ const maputils = { const resolution = scale / (mpu * 39.37 * dpi); return resolution; }, + formatScale: function formatScale(scale) { + const roundedScale = this.roundScale(scale); + return `1:${numberFormatter(roundedScale)}`; + }, roundScale: function roundScale(scale) { let scaleValue = scale; const differens = scaleValue % 10; From 71eef737dc3b1f3ec223cd1f5b86159390425fee Mon Sep 17 00:00:00 2001 From: jokd Date: Fri, 31 Mar 2023 14:31:22 +0200 Subject: [PATCH 2/2] Update maputils.js --- src/maputils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/maputils.js b/src/maputils.js index 419be566b..886d6444c 100644 --- a/src/maputils.js +++ b/src/maputils.js @@ -129,7 +129,7 @@ const maputils = { return resolution; }, formatScale: function formatScale(scale) { - const roundedScale = this.roundScale(scale); + const roundedScale = Math.round(scale / 10) * 10; return `1:${numberFormatter(roundedScale)}`; }, roundScale: function roundScale(scale) {