Allow squaring multiple features at once (close #6565)

This commit is contained in:
Quincy Morgan
2019-06-21 15:21:58 -04:00
parent 21e6c27f8e
commit 960d0d58a9
3 changed files with 132 additions and 49 deletions
+57 -25
View File
@@ -5,33 +5,41 @@ import { utilGetAllNodes } from '../util';
export function operationOrthogonalize(selectedIDs, context) {
var _entityID;
var _entity;
var _geometry;
var action = chooseAction();
var _extent;
var type;
var actions = selectedIDs.map(chooseAction).filter(Boolean);
var amount = actions.length === 1 ? 'single' : 'multiple';
var nodes = utilGetAllNodes(selectedIDs, context.graph());
var coords = nodes.map(function(n) { return n.loc; });
function chooseAction() {
if (selectedIDs.length !== 1) return null;
function chooseAction(entityID) {
_entityID = selectedIDs[0];
_entity = context.entity(_entityID);
_geometry = context.geometry(_entityID);
var entity = context.entity(entityID);
var geometry = context.geometry(entityID);
if (!_extent) {
_extent = entity.extent(context.graph());
} else {
_extent = _extent.extend(entity.extent(context.graph()));
}
// square a line/area
if (_entity.type === 'way' && new Set(_entity.nodes).size > 2 ) {
return actionOrthogonalize(_entityID, context.projection);
if (entity.type === 'way' && new Set(entity.nodes).size > 2 ) {
if (type && type !== 'feature') return null;
type = 'feature';
return actionOrthogonalize(entityID, context.projection);
// square a single vertex
} else if (_geometry === 'vertex') {
} else if (geometry === 'vertex') {
if (type && type !== 'corner') return null;
type = 'corner';
var graph = context.graph();
var parents = graph.parentWays(_entity);
var parents = graph.parentWays(entity);
if (parents.length === 1) {
var way = parents[0];
if (way.nodes.indexOf(_entityID) !== -1) {
return actionOrthogonalize(way.id, context.projection, _entityID);
if (way.nodes.indexOf(entityID) !== -1) {
return actionOrthogonalize(way.id, context.projection, entityID);
}
}
}
@@ -41,9 +49,19 @@ export function operationOrthogonalize(selectedIDs, context) {
var operation = function() {
if (!action) return;
if (!actions.length) return;
context.perform(action, operation.annotation());
var combinedAction = function(graph, t) {
actions.forEach(function(action) {
if (!action.disabled(graph)) {
graph = action(graph, t);
}
});
return graph;
};
combinedAction.transitionable = true;
context.perform(combinedAction, operation.annotation());
window.setTimeout(function() {
context.validator().validate();
@@ -52,19 +70,33 @@ export function operationOrthogonalize(selectedIDs, context) {
operation.available = function() {
return Boolean(action);
return actions.length && selectedIDs.length === actions.length;
};
// don't cache this because the visible extent could change
operation.disabled = function() {
if (!action) return '';
if (!actions.length) return '';
var actionDisabled;
var actionDisableds = {};
if (actions.every(function(action) {
var disabled = action.disabled(context.graph());
if (disabled) actionDisableds[disabled] = true;
return disabled;
})) {
actionDisabled = actions[0].disabled(context.graph());
}
var actionDisabled = action.disabled(context.graph());
if (actionDisabled) {
if (Object.keys(actionDisableds).length > 1) {
return 'multiple_blockers';
}
return actionDisabled;
} else if (_geometry !== 'vertex' &&
_entity.extent(context.graph()).percentContainedIn(context.extent()) < 0.8) {
} else if (type !== 'corner' &&
_extent.percentContainedIn(context.extent()) < 0.8) {
return 'too_large';
} else if (someMissing()) {
return 'not_downloaded';
@@ -93,13 +125,13 @@ export function operationOrthogonalize(selectedIDs, context) {
operation.tooltip = function() {
var disable = operation.disabled();
return disable ?
t('operations.orthogonalize.' + disable) :
t('operations.orthogonalize.description.' + _geometry);
t('operations.orthogonalize.' + disable + '.' + amount) :
t('operations.orthogonalize.description.' + type + '.' + amount);
};
operation.annotation = function() {
return t('operations.orthogonalize.annotation.' + _geometry);
return t('operations.orthogonalize.annotation.' + type + '.' + amount);
};