* toJSON * Save the edit history to JSON. * @return {string?} A String containing the JSON, or `undefined` if nothing to save
()
| 896 | * @return {string?} A String containing the JSON, or `undefined` if nothing to save |
| 897 | */ |
| 898 | toJSON() { |
| 899 | if (!this.hasChanges()) return; |
| 900 | |
| 901 | const OSM_PRECISION = 7; |
| 902 | const baseGraph = this.base.graph; // The initial unedited graph |
| 903 | const modifiedEntities = new Map(); // Map(Entity.key -> Entity) |
| 904 | const baseEntities = new Map(); // Map(entityID -> Entity) |
| 905 | const historyData = []; |
| 906 | |
| 907 | // Preserve the users history of edits.. |
| 908 | for (const edit of this._history) { |
| 909 | const modified = []; |
| 910 | const deleted = []; |
| 911 | |
| 912 | // watch out: for modified entities we index on "key" - e.g. "n1v1" |
| 913 | for (const [entityID, entity] of edit.graph.local.entities) { |
| 914 | if (entity) { |
| 915 | const key = osmEntity.key(entity); |
| 916 | modifiedEntities.set(key, _copyEntity(entity)); |
| 917 | modified.push(key); |
| 918 | } else { |
| 919 | deleted.push(entityID); |
| 920 | } |
| 921 | |
| 922 | // Collect the original versions of edited Entities. |
| 923 | const original = baseGraph.hasEntity(entityID); |
| 924 | if (original && !baseEntities.has(entityID)) { |
| 925 | baseEntities.set(entityID, _copyEntity(original)); |
| 926 | } |
| 927 | |
| 928 | // For modified ways, collect originals of child nodes also. - iD#4108 |
| 929 | // (This is needed for situations where we connect a way to an existing node) |
| 930 | if (entity && entity.nodes) { |
| 931 | for (const childID of entity.nodes) { |
| 932 | const child = baseGraph.hasEntity(childID); |
| 933 | if (child && !baseEntities.has(child.id)) { |
| 934 | baseEntities.set(child.id, _copyEntity(child)); |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | // Collect original parent ways also. |
| 940 | // (This is needed for situations where we reshape or move a way - |
| 941 | // behind the scenes, only the nodes were really modified) |
| 942 | if (original) { |
| 943 | for (const parent of baseGraph.parentWays(original)) { |
| 944 | if (!baseEntities.has(parent.id)) { |
| 945 | baseEntities.set(parent.id, _copyEntity(parent)); |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | const sources = edit.sources ?? {}; |
| 952 | |
| 953 | const item = {}; |
| 954 | if (modified.length) item.modified = modified; |
| 955 | if (deleted.length) item.deleted = deleted; |
no test coverage detected