2020</template >
2121
2222<script setup lang="ts">
23- import { TIdentityGeoJSONFeature , Tools } from ' ../../../../packages/maplugin-core' ;
23+ import { TIdentityGeoJSONFeature } from ' ../../../../packages/maplugin-core' ;
24+ import FileProcesses from ' ../../services/file-processes' ;
2425
2526const props = defineProps <{
2627 onUpload(features : Array <TIdentityGeoJSONFeature >): void ,
2728 onDownload(): GeoJSON .FeatureCollection
2829}>();
2930
30- const file_process: Array <{
31- extension: string ,
32- description: string ,
33- contentType: string
34- decode? (file : File , onFinish : (features : Array <TIdentityGeoJSONFeature >) => void ): void ,
35- encode? (geojson : GeoJSON .FeatureCollection ): string | Blob
36- }> = [
37- {
38- extension: " .geojson" ,
39- description: " geojson" ,
40- contentType: " application/json" ,
41- decode(file , onFinish ) {
42- const reader = new FileReader ();
43-
44- reader .onload = e => {
45- try {
46- const geojson = JSON .parse (e .target ?.result as string ) as GeoJSON .FeatureCollection ;
47- geojson .features .forEach (f => {
48- f .properties ?? = {};
49- if (! f .properties [' id' ]) {
50- f .properties [' id' ] = Tools .uuid ();
51- }
52- });
53- onFinish (geojson .features as any );
54- } catch (e ) {
55- alert (" geojson文件格式不正确" );
56- }
57- };
58-
59- reader .readAsText (file );
60- },
61-
62- encode(geojson ) {
63- return JSON .stringify (geojson , null , 4 );
64- }
65- }
66- ]
67-
6831function handleUpload() {
6932 const input = document .createElement (' input' );
7033 input .type = ' file' ;
71- input .accept = file_process .filter (x => x .encode ).map (x => x .extension ).join (" | " );
72- input .onchange = function (e : any ) {
34+ input .accept = FileProcesses .filter (x => x .decode ).map (x => x .extension ).join (" , " );
35+ input .onchange = async function (e : any ) {
7336 const file: File = e .target .files [0 ];
7437 const extension = file .name .split (' .' ).pop ()! ;
75- file_process .find (x => x .extension === ` .${extension } ` )?.decode ?.(file , features => props .onUpload (features ));
38+ const features = await FileProcesses .find (x => x .extension === ` .${extension } ` )! .decode !(file );
39+ props .onUpload (features );
7640 input .remove ();
7741 }
7842 input .click ();
@@ -83,7 +47,7 @@ async function handleDownload() {
8347
8448 if ((window as any )[' showSaveFilePicker' ]) {
8549 const opts = {
86- types: file_process .filter (x => x .encode ).map (x => ({
50+ types: FileProcesses .filter (x => x .encode ).map (x => ({
8751 description: x .description ,
8852 accept: {
8953 [x .contentType ]: [x .extension ]
@@ -97,8 +61,8 @@ async function handleDownload() {
9761 writable = await handle .createWritable (); // 创建可写入的文件对象
9862
9963 const extension = handle .name .split (' .' ).pop ()! ;
100- let encode = file_process .find (x => x .extension === ` .${extension } ` )?.encode ;
101- if (! encode ) encode = file_process .find (x => x .extension === ' .geojson' )! .encode ! ;
64+ let encode = FileProcesses .find (x => x .extension === ` .${extension } ` )?.encode ;
65+ if (! encode ) encode = FileProcesses .find (x => x .extension === ' .geojson' )! .encode ! ;
10266
10367 writable .write (encode (geojson )).then (() => {
10468 writable ?.close ();
0 commit comments