parse FileSource from dropped file for protocol.add

# Conflicts:
#	src/components/MapMaplibreGl.tsx
This commit is contained in:
prusswan
2025-01-20 23:13:02 +08:00
parent 979137eb9c
commit 7ee8e44ca1
2 changed files with 20 additions and 3 deletions
+13 -1
View File
@@ -8,7 +8,7 @@ import get from 'lodash.get'
import {unset} from 'lodash' import {unset} from 'lodash'
import {arrayMoveMutable} from 'array-move' import {arrayMoveMutable} from 'array-move'
import hash from "string-hash"; import hash from "string-hash";
import { PMTiles } from "pmtiles"; import { FileSource, PMTiles } from 'pmtiles';
import {Map, LayerSpecification, StyleSpecification, ValidationError, SourceSpecification} from 'maplibre-gl' import {Map, LayerSpecification, StyleSpecification, ValidationError, SourceSpecification} from 'maplibre-gl'
import {latest, validateStyleMin} from '@maplibre/maplibre-gl-style-spec' import {latest, validateStyleMin} from '@maplibre/maplibre-gl-style-spec'
@@ -741,6 +741,7 @@ export default class App extends React.Component<any, AppState> {
onChange={this.onMapChange} onChange={this.onMapChange}
options={this.state.maplibreGlDebugOptions} options={this.state.maplibreGlDebugOptions}
inspectModeEnabled={this.state.mapState === "inspect"} inspectModeEnabled={this.state.mapState === "inspect"}
file={this.state.file}
highlightedLayer={this.state.mapStyle.layers[this.state.selectedLayerIndex]} highlightedLayer={this.state.mapStyle.layers[this.state.selectedLayerIndex]}
onLayerSelect={this.onLayerSelect} /> onLayerSelect={this.onLayerSelect} />
} }
@@ -881,6 +882,15 @@ export default class App extends React.Component<any, AppState> {
}); });
} }
onFileSelected = (e) => {
var file = e[0];
var pmt = new PMTiles(new FileSource(file));
console.log("App.onFileSelected", pmt);
this.setState({
file: pmt
})
}
render() { render() {
const layers = this.state.mapStyle.layers || [] const layers = this.state.mapStyle.layers || []
const selectedLayer = layers.length > 0 ? layers[this.state.selectedLayerIndex] : undefined const selectedLayer = layers.length > 0 ? layers[this.state.selectedLayerIndex] : undefined
@@ -895,6 +905,8 @@ export default class App extends React.Component<any, AppState> {
onStyleOpen={this.onStyleChanged} onStyleOpen={this.onStyleChanged}
onSetMapState={this.setMapState} onSetMapState={this.setMapState}
onToggleModal={this.toggleModal.bind(this)} onToggleModal={this.toggleModal.bind(this)}
files={this.state.files}
onFileSelected={this.onFileSelected}
/> />
const layerList = <LayerList const layerList = <LayerList
+7 -2
View File
@@ -93,6 +93,7 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,
inspect: null, inspect: null,
geocoder: null, geocoder: null,
zoomControl: null, zoomControl: null,
protocol: new Protocol({metadata: true})
} }
i18next.on('languageChanged', () => { i18next.on('languageChanged', () => {
this.forceUpdate(); this.forceUpdate();
@@ -134,7 +135,10 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,
this.state.inspect!.render(); this.state.inspect!.render();
}, 500); }, 500);
} }
if (this.props.file) {
this.state.protocol.add(this.props.file); // this is necessary for non-HTTP sources
}
} }
componentDidMount() { componentDidMount() {
@@ -149,8 +153,9 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,
localIdeographFontFamily: false localIdeographFontFamily: false
} satisfies MapOptions; } satisfies MapOptions;
const protocol = new Protocol({metadata: true}); let protocol = this.state.protocol;
MapLibreGl.addProtocol("pmtiles",protocol.tile); MapLibreGl.addProtocol("pmtiles",protocol.tile);
const map = new MapLibreGl.Map(mapOpts); const map = new MapLibreGl.Map(mapOpts);
const mapViewChange = () => { const mapViewChange = () => {