feat: update data on control point remove

This commit is contained in:
Azgaar 2024-06-23 14:18:57 +02:00
parent b30358859c
commit cd45c3a01b

View file

@ -47,8 +47,7 @@ function editRoute(id) {
function getRoute() { function getRoute() {
const routeId = +elSelected.attr("id").slice(5); const routeId = +elSelected.attr("id").slice(5);
const route = pack.routes.find(r => r.i === routeId); return pack.routes.find(route => route.i === routeId);
return route;
} }
function updateRouteData() { function updateRouteData() {
@ -145,7 +144,6 @@ function editRoute(id) {
function addControlPoint() { function addControlPoint() {
const [x, y] = d3.mouse(this); const [x, y] = d3.mouse(this);
const route = getRoute(); const route = getRoute();
if (!route.points) route.points = debug.selectAll("#controlPoints > *").data(); if (!route.points) route.points = debug.selectAll("#controlPoints > *").data();
@ -172,6 +170,32 @@ function editRoute(id) {
redrawRoute(); redrawRoute();
} }
function removeControlPoint() {
const route = getRoute();
if (!route.points) route.points = debug.selectAll("#controlPoints > *").data();
const [x, y] = d3.select(this).datum();
const cellId = findCell(x, y);
const routeAllCells = route.points.map(([x, y]) => findCell(x, y));
const isOnlyPointInCell = routeAllCells.filter(cell => cell === cellId).length === 1;
if (isOnlyPointInCell) {
const index = route.cells.indexOf(cellId);
const prev = route.cells[index - 1];
const next = route.cells[index + 1];
if (prev) removeConnection(prev, cellId);
if (next) removeConnection(cellId, next);
if (prev && next) addConnection(prev, next, route.i);
}
this.remove();
route.points = debug.selectAll("#controlPoints > *").data();
route.cells = unique(route.points.map(([x, y]) => findCell(x, y)));
drawCells();
redrawRoute();
}
function showCreationDialog() { function showCreationDialog() {
const route = getRoute(); const route = getRoute();
createRoute(route.group); createRoute(route.group);
@ -193,12 +217,6 @@ function editRoute(id) {
routes[to][from] = routeId; routes[to][from] = routeId;
} }
function removeControlPoint() {
this.remove();
redrawRoute();
drawCells();
}
function changeName() { function changeName() {
getRoute().name = this.value; getRoute().name = this.value;
} }