Skip to content

Commit

Permalink
Fix/when no render mode (#74)
Browse files Browse the repository at this point in the history
* fix: fixed bug when render button is removed. add default hidden mode if no render buttom.

* doc: delete selected feature control if no feature is selected.
  • Loading branch information
JinIgarashi authored Nov 15, 2024
1 parent cdf5c1d commit 37a9836
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-wasps-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@watergis/maplibre-gl-terradraw': patch
---

fix: fixed bug when render button is removed. add default hidden mode if no render buttom.
20 changes: 17 additions & 3 deletions src/lib/MaplibreTerradrawControl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ControlPosition, IControl, Map } from 'maplibre-gl';
import { TerraDraw, TerraDrawMapLibreGLAdapter } from 'terra-draw';
import { TerraDraw, TerraDrawMapLibreGLAdapter, TerraDrawRenderMode } from 'terra-draw';
import type { ControlOptions, TerradrawMode, TerradrawModeClass } from './interfaces/index.js';
import { defaultControlOptions } from './constants/defaultControlOptions.js';
import { getDefaultModeOptions } from './constants/getDefaultModeOptions.js';
Expand All @@ -16,6 +16,8 @@ export class MaplibreTerradrawControl implements IControl {
private terradraw?: TerraDraw;
private options: ControlOptions = defaultControlOptions;

private defaultMode = 'render';

/**
* Constructor
* @param options Plugin control options
Expand Down Expand Up @@ -69,6 +71,17 @@ export class MaplibreTerradrawControl implements IControl {
}
});

// if no render button is specified, it add hidden render mode
if (!this.options?.modes?.includes('render')) {
modes.push(
new TerraDrawRenderMode({
modeName: 'default',
styles: {}
})
);
this.defaultMode = 'default';
}

this.isExpanded = this.options.open === true;

this.terradraw = new TerraDraw({
Expand All @@ -83,6 +96,7 @@ export class MaplibreTerradrawControl implements IControl {
this.controlContainer.classList.add(`maplibregl-ctrl-group`);

modes.forEach((m: TerradrawModeClass) => {
if (m.mode === 'default') return;
this.addTerradrawButton(m.mode as TerradrawMode);
});

Expand Down Expand Up @@ -175,7 +189,7 @@ export class MaplibreTerradrawControl implements IControl {
if (!item) continue;
item.classList.remove('active');
}
this.terradraw?.setMode('render');
this.terradraw?.setMode(this.defaultMode);
}

/**
Expand Down Expand Up @@ -228,7 +242,7 @@ export class MaplibreTerradrawControl implements IControl {
if (selected.length > 0) {
// if feature is selected, delete only selected feature
const currentMode = this.terradraw.getMode();
this.terradraw.setMode('render');
this.terradraw.setMode(this.defaultMode);
const ids = selected.map((f) => f.id);
this.terradraw.removeFeatures(ids);
this.terradraw.setMode(currentMode);
Expand Down
8 changes: 8 additions & 0 deletions src/routes/demo/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
const feature = snapshot?.find((feature) => feature.id === id);
selectedFeature = JSON.stringify(feature, null, 4);
});
drawInstance?.on('change', () => {
const snapshot = drawInstance.getSnapshot();
const selectedFeatures = snapshot.filter((f) => f.properties.selected === true);
if (selectedFeatures.length === 0) {
selectedFeature = '';
}
});
});
</script>

Expand Down

0 comments on commit 37a9836

Please sign in to comment.