2025-10-18 16:10:08 +02:00
|
|
|
import { currentTool, Tool } from '$lib/components/toolbar/tools';
|
2026-10-31 18:08:22 +01:00
|
|
|
import { cuts, routeSegmentIds, splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
2025-02-02 11:17:22 +01:00
|
|
|
import { Scissors } from 'lucide-static';
|
2025-10-18 16:10:08 +02:00
|
|
|
import { get } from 'svelte/store';
|
2026-10-31 18:08:22 +01:00
|
|
|
import { mapCursor, MapCursorState } from '$lib/state/map-cursor';
|
|
|
|
|
import type { GeoJSONSource, Map, MapLayerMouseEvent } from 'maplibre-gl';
|
2026-01-30 21:01:24 +01:00
|
|
|
import { ANCHOR_LAYER_KEY } from '$lib/components/map/style';
|
2026-01-31 12:57:08 +01:00
|
|
|
import type { MapLayerEventManager } from '$lib/components/map/map-layer-event-manager';
|
2026-02-01 17:18:17 +01:00
|
|
|
import { loadSVGIcon } from '$lib/utils';
|
2026-10-31 18:08:22 +01:00
|
|
|
import { coversSegments } from '$lib/engine/selection-helpers';
|
|
|
|
|
import {
|
|
|
|
|
engine,
|
|
|
|
|
type Selection,
|
|
|
|
|
type SelectionStatistics,
|
|
|
|
|
type StatisticsRequest,
|
|
|
|
|
} from '$lib/engine';
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2026-10-31 18:08:22 +01:00
|
|
|
/**
|
|
|
|
|
* The anchors inside the segments of the selection, where a click splits the file, the route, the
|
|
|
|
|
* track or the segment: a command of the engine, which finds them by the index of their trackpoint
|
|
|
|
|
* in the selection. Only the segments that can be split as `splitAs` says have them.
|
|
|
|
|
*/
|
2024-07-25 16:15:44 +02:00
|
|
|
export class SplitControls {
|
2026-07-24 19:06:39 +02:00
|
|
|
map: Map;
|
2026-01-31 12:57:08 +01:00
|
|
|
layerEventManager: MapLayerEventManager;
|
2026-10-31 18:08:22 +01:00
|
|
|
unsubscribes: (() => void)[] = [];
|
|
|
|
|
/** What the tool needs from the engine, while it shows the anchors. */
|
|
|
|
|
request: StatisticsRequest = engine.requestStatistics();
|
|
|
|
|
selection: Selection = { type: 'empty' };
|
|
|
|
|
statistics: SelectionStatistics | undefined = undefined;
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2026-10-31 18:08:22 +01:00
|
|
|
layerOnMouseEnterBinded: () => void = this.layerOnMouseEnter.bind(this);
|
2025-11-16 16:46:31 +01:00
|
|
|
layerOnMouseLeaveBinded: () => void = this.layerOnMouseLeave.bind(this);
|
2026-10-31 18:08:22 +01:00
|
|
|
layerOnClickBinded: (e: MapLayerMouseEvent) => void = this.layerOnClick.bind(this);
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2026-07-24 19:06:39 +02:00
|
|
|
constructor(map: Map, layerEventManager: MapLayerEventManager) {
|
2024-07-25 16:15:44 +02:00
|
|
|
this.map = map;
|
2026-01-31 12:57:08 +01:00
|
|
|
this.layerEventManager = layerEventManager;
|
2026-02-01 17:18:17 +01:00
|
|
|
loadSVGIcon(
|
|
|
|
|
this.map,
|
|
|
|
|
'split-control',
|
|
|
|
|
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40">
|
|
|
|
|
<circle cx="20" cy="20" r="20" fill="white" />
|
|
|
|
|
<g transform="translate(8 8)">
|
|
|
|
|
${Scissors.replace('stroke="currentColor"', 'stroke="black"')}
|
|
|
|
|
</g>
|
|
|
|
|
</svg>`
|
|
|
|
|
);
|
2025-11-16 16:46:31 +01:00
|
|
|
|
2026-10-31 18:08:22 +01:00
|
|
|
this.unsubscribes.push(
|
|
|
|
|
engine.statistics.subscribe((statistics) => {
|
|
|
|
|
this.statistics = statistics;
|
|
|
|
|
this.addIfNeeded();
|
|
|
|
|
})
|
|
|
|
|
);
|
2025-10-18 16:10:08 +02:00
|
|
|
this.unsubscribes.push(currentTool.subscribe(this.addIfNeeded.bind(this)));
|
2026-10-31 18:08:22 +01:00
|
|
|
this.unsubscribes.push(splitAs.subscribe(this.addIfNeeded.bind(this)));
|
|
|
|
|
this.unsubscribes.push(
|
|
|
|
|
engine.selection.subscribe((selection) => {
|
|
|
|
|
this.selection = selection;
|
|
|
|
|
this.addIfNeeded();
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-07-25 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addIfNeeded() {
|
2026-10-31 18:08:22 +01:00
|
|
|
// the tool works on files, routes, tracks and segments, not on waypoints
|
|
|
|
|
const scissors = get(currentTool) === Tool.SCISSORS && coversSegments(this.selection);
|
|
|
|
|
this.request.set(scissors ? ['anchors'] : []);
|
2024-07-25 16:15:44 +02:00
|
|
|
if (!scissors) {
|
2025-11-16 16:46:31 +01:00
|
|
|
this.remove();
|
2024-07-25 16:15:44 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 16:46:31 +01:00
|
|
|
this.updateControls();
|
2024-07-25 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-31 18:08:22 +01:00
|
|
|
/** The anchors that are inside a segment, which is not where it starts or ends. */
|
|
|
|
|
inner(): GeoJSON.Feature[] {
|
|
|
|
|
const statistics = this.statistics;
|
|
|
|
|
const anchors = statistics?.anchors;
|
|
|
|
|
if (!statistics || !anchors) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const starts = anchors.segmentStarts;
|
|
|
|
|
const routeSegments = routeSegmentIds(this.selection);
|
|
|
|
|
const type = get(splitAs);
|
|
|
|
|
const features: GeoJSON.Feature[] = [];
|
|
|
|
|
let segment = 0;
|
|
|
|
|
for (let i = 0; i < anchors.indices.length; i++) {
|
|
|
|
|
const index = anchors.indices[i];
|
|
|
|
|
// the anchors are sorted: the segment can only be the same or a following one
|
|
|
|
|
while (segment + 1 < starts.length && starts[segment + 1] <= index) {
|
|
|
|
|
segment++;
|
|
|
|
|
}
|
|
|
|
|
const end = segment + 1 < starts.length ? starts[segment + 1] : statistics.length;
|
|
|
|
|
const inRoute = routeSegments.has(anchors.segmentIds[segment]);
|
|
|
|
|
if (index > starts[segment] && index < end - 1 && cuts(type, inRoute)) {
|
|
|
|
|
features.push({
|
|
|
|
|
type: 'Feature',
|
|
|
|
|
geometry: {
|
|
|
|
|
type: 'Point',
|
|
|
|
|
coordinates: [statistics.lng[index], statistics.lat[index]],
|
|
|
|
|
},
|
|
|
|
|
properties: { index, minZoom: anchors.zooms[i] },
|
2024-07-25 16:15:44 +02:00
|
|
|
});
|
|
|
|
|
}
|
2026-10-31 18:08:22 +01:00
|
|
|
}
|
|
|
|
|
return features;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateControls() {
|
|
|
|
|
const data: GeoJSON.FeatureCollection = {
|
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
|
features: this.inner(),
|
|
|
|
|
};
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2025-11-16 16:46:31 +01:00
|
|
|
try {
|
2026-10-31 18:08:22 +01:00
|
|
|
const source = this.map.getSource('split-controls') as GeoJSONSource | undefined;
|
2025-11-16 16:46:31 +01:00
|
|
|
if (source) {
|
|
|
|
|
source.setData(data);
|
|
|
|
|
} else {
|
|
|
|
|
this.map.addSource('split-controls', {
|
|
|
|
|
type: 'geojson',
|
|
|
|
|
data: data,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2025-11-16 16:46:31 +01:00
|
|
|
if (!this.map.getLayer('split-controls')) {
|
2026-01-30 21:01:24 +01:00
|
|
|
this.map.addLayer(
|
|
|
|
|
{
|
|
|
|
|
id: 'split-controls',
|
|
|
|
|
type: 'symbol',
|
|
|
|
|
source: 'split-controls',
|
|
|
|
|
layout: {
|
|
|
|
|
'icon-image': 'split-control',
|
|
|
|
|
'icon-size': 0.25,
|
|
|
|
|
'icon-padding': 0,
|
|
|
|
|
},
|
|
|
|
|
filter: ['<=', ['get', 'minZoom'], ['zoom']],
|
2025-11-16 16:46:31 +01:00
|
|
|
},
|
2026-01-30 21:01:24 +01:00
|
|
|
ANCHOR_LAYER_KEY.interactions
|
|
|
|
|
);
|
2025-11-16 16:46:31 +01:00
|
|
|
|
2026-01-31 12:57:08 +01:00
|
|
|
this.layerEventManager.on(
|
|
|
|
|
'mouseenter',
|
|
|
|
|
'split-controls',
|
|
|
|
|
this.layerOnMouseEnterBinded
|
|
|
|
|
);
|
|
|
|
|
this.layerEventManager.on(
|
|
|
|
|
'mouseleave',
|
|
|
|
|
'split-controls',
|
|
|
|
|
this.layerOnMouseLeaveBinded
|
|
|
|
|
);
|
|
|
|
|
this.layerEventManager.on('click', 'split-controls', this.layerOnClickBinded);
|
2025-11-16 16:46:31 +01:00
|
|
|
}
|
2026-10-31 18:08:22 +01:00
|
|
|
} catch {
|
2025-11-16 16:46:31 +01:00
|
|
|
// No reliable way to check if the map is ready to add sources and layers
|
|
|
|
|
}
|
2024-07-25 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove() {
|
2026-01-31 12:57:08 +01:00
|
|
|
this.layerEventManager.off('mouseenter', 'split-controls', this.layerOnMouseEnterBinded);
|
|
|
|
|
this.layerEventManager.off('mouseleave', 'split-controls', this.layerOnMouseLeaveBinded);
|
|
|
|
|
this.layerEventManager.off('click', 'split-controls', this.layerOnClickBinded);
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2025-11-16 16:46:31 +01:00
|
|
|
try {
|
|
|
|
|
if (this.map.getLayer('split-controls')) {
|
|
|
|
|
this.map.removeLayer('split-controls');
|
2024-07-25 16:15:44 +02:00
|
|
|
}
|
2025-11-16 16:46:31 +01:00
|
|
|
|
|
|
|
|
if (this.map.getSource('split-controls')) {
|
|
|
|
|
this.map.removeSource('split-controls');
|
|
|
|
|
}
|
2026-10-31 18:08:22 +01:00
|
|
|
} catch {
|
2025-11-16 16:46:31 +01:00
|
|
|
// No reliable way to check if the map is ready to remove sources and layers
|
|
|
|
|
}
|
2024-07-25 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-31 18:08:22 +01:00
|
|
|
layerOnMouseEnter() {
|
2025-11-16 16:46:31 +01:00
|
|
|
mapCursor.notify(MapCursorState.SPLIT_CONTROL, true);
|
|
|
|
|
}
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2025-11-16 16:46:31 +01:00
|
|
|
layerOnMouseLeave() {
|
|
|
|
|
mapCursor.notify(MapCursorState.SPLIT_CONTROL, false);
|
|
|
|
|
}
|
2024-07-25 16:15:44 +02:00
|
|
|
|
2026-07-24 19:06:39 +02:00
|
|
|
layerOnClick(e: MapLayerMouseEvent) {
|
2026-10-31 18:08:22 +01:00
|
|
|
const revision = this.statistics?.anchors?.revision;
|
|
|
|
|
if (revision !== undefined) {
|
|
|
|
|
engine.split(revision, e.features![0].properties!.index, get(splitAs));
|
|
|
|
|
}
|
2024-07-25 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
|
this.remove();
|
2026-10-31 18:08:22 +01:00
|
|
|
this.request.release();
|
2024-07-25 16:15:44 +02:00
|
|
|
this.unsubscribes.forEach((unsubscribe) => unsubscribe());
|
|
|
|
|
}
|
|
|
|
|
}
|