forked from adobe/helix-importer-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request adobe#2 from atopper/top/multiple-urls-in-localsto…
…arge Top/multiple urls in localstoarge
- Loading branch information
Showing
3 changed files
with
89 additions
and
32 deletions.
There are no files selected for viewing
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,78 @@ | ||
/** | ||
* Retrieve the Mapping for the provided URL from the local-storage. A mapping is an array with | ||
* objects that contain the attributes of the 'box' including: | ||
* - xpath | ||
* - boxId | ||
* - numCols | ||
* - numRows | ||
* - mapping (block name) | ||
* @param url The current URL | ||
* @returns {mapping|undefined} Return the mapping as an array (not string) or 'undefined' if it | ||
* doesn't exist for the provided URL. | ||
*/ | ||
function getImporterSectionsMapping(url) { | ||
try { | ||
const allMappings = JSON.parse(localStorage.getItem('helix-importer-sections-mapping')); | ||
if (allMappings) { | ||
if (Array.isArray(allMappings)) { | ||
const urlMapping = allMappings.find((sm) => sm.url === url); | ||
if (urlMapping) { | ||
return urlMapping.mapping; | ||
} | ||
} else if (allMappings.url && allMappings.url === url) { | ||
// Handle the old way (one single url saved at a time) | ||
return allMappings.mapping; | ||
} | ||
} | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error loading sections mapping data for url ${url}`, e); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Write (or overwrite) the Mapping for the provided (base?) URL to the local-storage while | ||
* maintaining the mappings for other URLs. | ||
* @param url The current URL | ||
* @param mapping the mapping to store (component id to block name) | ||
* @returns void | ||
*/ | ||
function saveImporterSectionsMapping(url, mapping) { | ||
try { | ||
let allMappings = JSON.parse(localStorage.getItem('helix-importer-sections-mapping')); | ||
if (allMappings && Array.isArray(allMappings)) { | ||
const index = allMappings.findIndex((sm) => sm.url === url); | ||
if (index >= 0) { | ||
if (mapping.length === 0) { | ||
allMappings.splice(index, 1); | ||
} else { | ||
allMappings[index].mapping = mapping; | ||
} | ||
} else { | ||
allMappings.push({ | ||
url, | ||
mapping, | ||
}); | ||
} | ||
} else { | ||
// Local-Storage was empty or contained the old one-url way, just write the whole mapping. | ||
allMappings = [{ | ||
url, | ||
mapping, | ||
}]; | ||
} | ||
|
||
// save mapping data | ||
localStorage.setItem('helix-importer-sections-mapping', JSON.stringify(allMappings)); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error saving sections mapping data for url ${url}`, e); | ||
} | ||
} | ||
|
||
export { | ||
getImporterSectionsMapping, | ||
saveImporterSectionsMapping, | ||
}; |