Skip to content

Commit

Permalink
Merge pull request adobe#2 from atopper/top/multiple-urls-in-localsto…
Browse files Browse the repository at this point in the history
…arge

Top/multiple urls in localstoarge
  • Loading branch information
atopper authored May 23, 2024
2 parents cb7e7ac + cdfd503 commit dc1cb9d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 32 deletions.
23 changes: 9 additions & 14 deletions js/import/import.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { asyncForEach, getElementByXpath } from '../shared/utils.js';
import PollImporter from '../shared/pollimporter.js';
import alert from '../shared/alert.js';
import { toggleLoadingButton } from '../shared/ui.js';
import { getImporterSectionsMapping, saveImporterSectionsMapping } from '../sections-mapping/utils.ui.js';

const PARENT_SELECTOR = '.import';
const CONFIG_PARENT_SELECTOR = `${PARENT_SELECTOR} form`;
Expand Down Expand Up @@ -482,10 +483,7 @@ const detectSections = async (src, frame) => {
});
}
// save sections mapping data
localStorage.setItem('helix-importer-sections-mapping', JSON.stringify({
url: originalURL,
mapping: mappingData,
}));
saveImporterSectionsMapping(originalURL, mappingData);
});

return blockPicker;
Expand Down Expand Up @@ -514,19 +512,16 @@ const detectSections = async (src, frame) => {
deleteBtn.innerHTML = '<sp-icon-delete></sp-icon-delete>';
row.appendChild(deleteBtn);
deleteBtn.addEventListener('click', (e) => {
console.log(e);
console.log('delete section', section.id);
// console.log(e);
// console.log('delete section', section.id);
// row
const rowEl = e.target.closest('.row');
if (rowEl) {
const id = rowEl.dataset.sectionId;
mappingData = mappingData.filter((m) => m.id !== id);

// save sections mapping data
localStorage.setItem('helix-importer-sections-mapping', JSON.stringify({
url: originalURL,
mapping: mappingData,
}));
saveImporterSectionsMapping(originalURL, mappingData);

rowEl.remove();
}
Expand All @@ -547,10 +542,10 @@ const detectSections = async (src, frame) => {

// look for existing mapping data
try {
const mapping = JSON.parse(localStorage.getItem('helix-importer-sections-mapping'));
if (mapping && mapping.url === originalURL) {
mappingData = mapping.mapping;
mapping.mapping.forEach((m) => {
const mapping = getImporterSectionsMapping(originalURL);
if (mapping) {
mappingData = mapping;
mapping.forEach((m) => {
const row = getMappingRow(m, MAPPING_EDITOR_SECTIONS.children.length);
MAPPING_EDITOR_SECTIONS.appendChild(row);
});
Expand Down
20 changes: 2 additions & 18 deletions js/sections-mapping/sections-mapping.import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import * as parsers from './parsers/parsers.js';
import { getImporterSectionsMapping } from './utils.ui.js';

/**
* functions
Expand All @@ -20,22 +21,6 @@ export function generateDocumentPath({ document, url }) {
return WebImporter.FileUtils.sanitizePath(p);
}

function getSectionsMappingData(url) {
const item = localStorage.getItem('helix-importer-sections-mapping');

if (item) {
const mData = JSON.parse(item);
return mData.mapping;
// TODO - support multiple mappings
// const found = mData.find((m) => m.url === url);
// if (found) {
// return found;
// }
}

return null;
}

function getElementByXpath(document, path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
Expand Down Expand Up @@ -96,7 +81,6 @@ export default {
}
}
});

},

transform: async ({ document, params }) => {
Expand All @@ -107,7 +91,7 @@ export default {
/**
* get sections mapping data
*/
const mapping = getSectionsMappingData(params.originalURL);
const mapping = getImporterSectionsMapping(params.originalURL);
if (!mapping) {
throw new Error('No sections mapping data found, aborting');
}
Expand Down
78 changes: 78 additions & 0 deletions js/sections-mapping/utils.ui.js
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,
};

0 comments on commit dc1cb9d

Please sign in to comment.