mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-12-27 05:30:01 +00:00
revert clone function switch
This commit is contained in:
@@ -16,6 +16,16 @@ import {
|
||||
} from './types';
|
||||
import { immerable, isDraft, original, freeze } from 'immer';
|
||||
|
||||
function cloneJSON<T>(obj: T): T {
|
||||
if (obj === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (obj === null || typeof obj !== 'object') {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
// An abstract class that groups functions that need to be computed recursively in the GPX file hierarchy
|
||||
export abstract class GPXTreeElement<T extends GPXTreeElement<any>> {
|
||||
_data: { [key: string]: any } = {};
|
||||
@@ -238,12 +248,12 @@ export class GPXFile extends GPXTreeNode<Track> {
|
||||
|
||||
clone(): GPXFile {
|
||||
return new GPXFile({
|
||||
attributes: structuredClone(this.attributes),
|
||||
metadata: structuredClone(this.metadata),
|
||||
attributes: cloneJSON(this.attributes),
|
||||
metadata: cloneJSON(this.metadata),
|
||||
wpt: this.wpt.map((waypoint) => waypoint.clone()),
|
||||
trk: this.trk.map((track) => track.clone()),
|
||||
rte: [],
|
||||
_data: structuredClone(this._data),
|
||||
_data: cloneJSON(this._data),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -256,7 +266,7 @@ export class GPXFile extends GPXTreeNode<Track> {
|
||||
|
||||
toGPXFileType(exclude: string[] = []): GPXFileType {
|
||||
let file: GPXFileType = {
|
||||
attributes: structuredClone(this.attributes),
|
||||
attributes: cloneJSON(this.attributes),
|
||||
metadata: {},
|
||||
wpt: this.wpt.map((wpt) => wpt.toWaypointType(exclude)),
|
||||
trk: this.trk.map((track) => track.toTrackType(exclude)),
|
||||
@@ -270,10 +280,10 @@ export class GPXFile extends GPXTreeNode<Track> {
|
||||
file.metadata.desc = this.metadata.desc;
|
||||
}
|
||||
if (this.metadata.author) {
|
||||
file.metadata.author = structuredClone(this.metadata.author);
|
||||
file.metadata.author = cloneJSON(this.metadata.author);
|
||||
}
|
||||
if (this.metadata.link) {
|
||||
file.metadata.link = structuredClone(this.metadata.link);
|
||||
file.metadata.link = cloneJSON(this.metadata.link);
|
||||
}
|
||||
if (this.metadata.time && !exclude.includes('time')) {
|
||||
file.metadata.time = this.metadata.time;
|
||||
@@ -566,11 +576,11 @@ export class Track extends GPXTreeNode<TrackSegment> {
|
||||
cmt: this.cmt,
|
||||
desc: this.desc,
|
||||
src: this.src,
|
||||
link: structuredClone(this.link),
|
||||
link: cloneJSON(this.link),
|
||||
type: this.type,
|
||||
extensions: structuredClone(this.extensions),
|
||||
extensions: cloneJSON(this.extensions),
|
||||
trkseg: this.trkseg.map((seg) => seg.clone()),
|
||||
_data: structuredClone(this._data),
|
||||
_data: cloneJSON(this._data),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1087,7 +1097,7 @@ export class TrackSegment extends GPXTreeLeaf {
|
||||
clone(): TrackSegment {
|
||||
return new TrackSegment({
|
||||
trkpt: this.trkpt.map((point) => point.clone()),
|
||||
_data: structuredClone(this._data),
|
||||
_data: cloneJSON(this._data),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1213,14 +1223,14 @@ export class TrackSegment extends GPXTreeLeaf {
|
||||
let trkpt = og.trkpt.map(
|
||||
(point, i) =>
|
||||
new TrackPoint({
|
||||
attributes: structuredClone(point.attributes),
|
||||
attributes: cloneJSON(point.attributes),
|
||||
ele: point.ele,
|
||||
time: new Date(
|
||||
newStartTimestamp.getTime() +
|
||||
(originalEndTimestamp.getTime() - og.trkpt[i].time.getTime())
|
||||
),
|
||||
extensions: structuredClone(point.extensions),
|
||||
_data: structuredClone(point._data),
|
||||
extensions: cloneJSON(point.extensions),
|
||||
_data: cloneJSON(point._data),
|
||||
})
|
||||
);
|
||||
|
||||
@@ -1464,7 +1474,7 @@ export class TrackPoint {
|
||||
},
|
||||
ele: this.ele,
|
||||
time: this.time ? new Date(this.time.getTime()) : undefined,
|
||||
extensions: this.extensions ? structuredClone(this.extensions) : undefined,
|
||||
extensions: this.extensions ? cloneJSON(this.extensions) : undefined,
|
||||
_data: {
|
||||
index: this._data?.index,
|
||||
anchor: this._data?.anchor,
|
||||
@@ -1566,7 +1576,7 @@ export class Waypoint {
|
||||
name: this.name,
|
||||
cmt: this.cmt,
|
||||
desc: this.desc,
|
||||
link: structuredClone(this.link),
|
||||
link: cloneJSON(this.link),
|
||||
sym: this.sym,
|
||||
type: this.type,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user