From 21ed04ed65ddbaf36d233c6ecc6ad29194b09056 Mon Sep 17 00:00:00 2001 From: Quincy Morgan Date: Fri, 15 May 2020 09:51:50 -0400 Subject: [PATCH] Limit Copy operation by visible extent (close #7603) --- data/core.yaml | 3 +++ dist/locales/en.json | 4 ++++ modules/operations/copy.js | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/data/core.yaml b/data/core.yaml index bd98e79a4..53704d40c 100644 --- a/data/core.yaml +++ b/data/core.yaml @@ -88,6 +88,9 @@ en: annotation: single: Copied a feature. multiple: "Copied {n} features." + too_large: + single: This can't be copied because not enough of it is currently visible. + multiple: These can't be copied because not enough of them are currently visible. paste: title: Paste description: diff --git a/dist/locales/en.json b/dist/locales/en.json index e1aa33cb7..f50319afe 100644 --- a/dist/locales/en.json +++ b/dist/locales/en.json @@ -112,6 +112,10 @@ "annotation": { "single": "Copied a feature.", "multiple": "Copied {n} features." + }, + "too_large": { + "single": "This can't be copied because not enough of it is currently visible.", + "multiple": "These can't be copied because not enough of them are currently visible." } }, "paste": { diff --git a/modules/operations/copy.js b/modules/operations/copy.js index 270ddf732..bd81c8943 100644 --- a/modules/operations/copy.js +++ b/modules/operations/copy.js @@ -3,10 +3,13 @@ import { event as d3_event } from 'd3-selection'; import { t } from '../core/localizer'; import { behaviorOperation } from '../behavior/operation'; import { uiCmd } from '../ui/cmd'; -import { utilArrayGroupBy } from '../util'; +import { geoExtent } from '../geo'; +import { utilArrayGroupBy, utilGetAllNodes } from '../util'; export function operationCopy(context, selectedIDs) { + var _multi = selectedIDs.length === 1 ? 'single' : 'multiple'; + function getFilteredIdsToCopy() { return selectedIDs.filter(function(selectedID) { var entity = context.graph().hasEntity(selectedID); @@ -98,14 +101,22 @@ export function operationCopy(context, selectedIDs) { operation.disabled = function() { + var nodes = utilGetAllNodes(getFilteredIdsToCopy(), context.graph()); + var extent = nodes.reduce(function(extent, node) { + return extent.extend(node.extent(context.graph())); + }, geoExtent()); + if (extent.area() && extent.percentContainedIn(context.map().extent()) < 0.8) { + return 'too_large'; + } return false; }; operation.tooltip = function() { - return selectedIDs.length === 1 ? - t('operations.copy.description.single') : - t('operations.copy.description.multiple'); + var disable = operation.disabled(); + return disable ? + t('operations.copy.' + disable + '.' + _multi) : + t('operations.copy.description' + '.' + _multi); };