-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export and import layers (closes #18)
- Loading branch information
1 parent
22fea06
commit 7f6a2a5
Showing
9 changed files
with
141 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { dialog, Notification } from 'electron' | ||
import sanitizeFilename from 'sanitize-filename' | ||
import fs from 'fs' | ||
|
||
export const exportLayer = async (event, layerName, content) => { | ||
|
||
const dialogOptions = { | ||
title: 'Export layer', | ||
defaultPath: sanitizeFilename(`${layerName}.json`), | ||
filters: [{ name: 'Layer', extensions: ['json'] }] | ||
} | ||
|
||
const interaction = await dialog.showSaveDialog(event.sender.getOwnerBrowserWindow(), dialogOptions) | ||
if (interaction.canceled) return | ||
try { | ||
await fs.promises.writeFile(interaction.filePath, JSON.stringify(content)) | ||
} catch (error) { | ||
const n = new Notification({ | ||
title: 'Export failed', | ||
body: error.message | ||
}) | ||
n.show() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as R from 'ramda' | ||
import * as ID from '../ids' | ||
|
||
export const clone = async (defaultLayerId, unsorted) => { | ||
const entries = unsorted.sort(([a], [b]) => ID.ord(a) - ID.ord(b)) | ||
const entrymap = Object.fromEntries(entries) | ||
|
||
// Collect all features which are not included in a layer | ||
// which itself is also part of selection. | ||
// These (uncovered) features will be assigned to default layer. | ||
|
||
const uncoveredIds = entries.reduce((acc, [key]) => { | ||
if (!ID.isFeatureId(key)) return acc | ||
if (entrymap[ID.layerId(key)]) return acc | ||
acc.push(key) | ||
return acc | ||
}, []) | ||
|
||
// Create default layer if necessary. | ||
|
||
const tuples = [] | ||
if (uncoveredIds.length && !defaultLayerId) { | ||
defaultLayerId = ID.layerId() | ||
tuples.push([defaultLayerId, { name: 'Default Layer' }]) | ||
tuples.push([ID.defaultId(defaultLayerId), ['default']]) | ||
} | ||
|
||
// All (uncovered) features where layer is not explicitly included | ||
// in selection are assigned to default layer. | ||
|
||
const keymap = uncoveredIds.reduce((acc, featureId) => { | ||
// Map old (uncovered) layer to default layer: | ||
acc[ID.layerId(featureId)] = defaultLayerId | ||
return acc | ||
}, {}) | ||
|
||
|
||
// Add new replacement to keymap (old key -> new key): | ||
const replace = key => R.tap(replacement => (keymap[key] = replacement)) | ||
|
||
const rewrite = R.cond([ | ||
[ID.isLayerId, key => replace(key)(ID.layerId())], | ||
[ID.isFeatureId, key => replace(key)(ID.featureId(keymap[ID.layerId(key)]))], | ||
[ID.isLinkId, key => replace(key)(ID.linkId(keymap[ID.containerId(key)]))], | ||
[ID.isTagsId, key => replace(key)(ID.tagsId(keymap[ID.associatedId(key)]))], | ||
[ID.isMarkerId, key => replace(key)(ID.markerId())], | ||
[ID.isTileServiceId, key => replace(key)(ID.tileServiceId())], | ||
[ID.isStyleId, key => replace(key)(ID.styleId(keymap[ID.associatedId(key)]))], | ||
[R.T, key => key] | ||
]) | ||
|
||
// Nasty side-effect: adds tuples (aka acc): | ||
entries.reduce((acc, [key, value]) => { | ||
acc.push([rewrite(key), value]) | ||
return acc | ||
}, tuples) | ||
|
||
return tuples | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters