Check Terra Draw events behaviours
We are going to add scripts to listen Terra Draw events to monitor how it behaves.
Firstly, add the below script in script tag section.
let draw: TerraDraw | undefined = $state();
+let selectedFeature: string = $state('');
Secondly, add the following HTML in the last on sidebar class.
<hr />
<label for="selected-feature-geojson">Selected Feature GeoJSON:</label>
<textarea
id="selected-feature-geojson"
bind:value={selectedFeature}
style="width: 100%; resize: vertical;"
rows="10"
readonly
></textarea>
Lastly, add the below script in the last of once('load') function of maplibre.
map.once('load', () => {
draw?.start();
draw?.on('change', (ids, type) => {
if (!draw) return;
if (type === 'create') {
for (const id of ids) {
console.log(`change: Feature created: ${id}`);
}
} else if (type === 'update') {
for (const id of ids) {
console.log(`change: Feature updated: ${id}`);
}
} else if (type === 'delete') {
for (const id of ids) {
console.log(`change: Feature deleted: ${id}`);
}
} else if (type === 'styling') {
for (const id of ids) {
console.log(`change: Feature styling updated: ${id}`);
}
}
});
draw?.on(
'finish',
(id: TerraDrawExtend.FeatureId, context: { action: string; mode: string }) => {
if (context.action === 'draw') {
console.log(`finish: Feature drawn: ${id} in mode: ${context.mode}`);
} else if (context.action === 'dragFeature') {
console.log(`finish: Feature dragged: ${id} in mode: ${context.mode}`);
} else if (context.action === 'dragCoordinate') {
console.log(`finish: Coordinate dragged in feature: ${id} in mode: ${context.mode}`);
} else if (context.action === 'dragCoordinateResize') {
console.log(`finish: Coordinate resized in feature: ${id} in mode: ${context.mode}`);
}
}
);
draw?.on('select', (id: TerraDrawExtend.FeatureId) => {
console.log(`selected: ${id}`);
const feature = draw?.getSnapshotFeature(id);
selectedFeature = feature ? JSON.stringify(feature, null, 2) : '';
});
draw?.on('deselect', () => {
console.log(`deselected`);
selectedFeature = '';
});