From 3cbdee65711da5b7d8123d3d64b3080a8da8d614 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Sun, 25 Sep 2016 23:09:13 +0800 Subject: [PATCH] Handle nested bookmarks folder fix #4291 Auditors: @bbondy Test Plan: 1. Create nested folder as #4291 shows 2. Nested folder should be imported correctly --- app/importer.js | 50 ++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/app/importer.js b/app/importer.js index b20aa326c44..b72db079793 100644 --- a/app/importer.js +++ b/app/importer.js @@ -56,9 +56,6 @@ importer.on('update-supported-browsers', (e, detail) => { } }) -importer.on('show-warning-dialog', (e) => { -}) - importer.on('add-password-form', (e, detail) => { }) @@ -78,13 +75,36 @@ importer.on('add-history-page', (e, history, visitSource) => { importer.on('add-homepage', (e, detail) => { }) +const getParentFolderId = (path, pathMap, sites, topLevelFolderId, nextFolderIdObject) => { + const pathLen = path.length + if (!pathLen) { + return topLevelFolderId + } + const parentFolder = path.pop() + let parentFolderId = pathMap[parentFolder] + if (parentFolderId === undefined) { + parentFolderId = nextFolderIdObject.id++ + pathMap[parentFolder] = parentFolderId + const folder = { + customTitle: parentFolder, + folderId: parentFolderId, + parentFolderId: getParentFolderId(path, pathMap, sites, topLevelFolderId, nextFolderIdObject), + lastAccessedTime: (new Date()).getTime(), + tags: [siteTags.BOOKMARK_FOLDER] + } + sites.push(folder) + } + return parentFolderId +} + importer.on('add-bookmarks', (e, bookmarks, topLevelFolder) => { let nextFolderId = siteUtil.getNextFolderId(AppStore.getState().get('sites')) + let nextFolderIdObject = { id: nextFolderId } let pathMap = {} let sites = [] let topLevelFolderId = 0 if (!isMergeFavorites) { - topLevelFolderId = nextFolderId++ + topLevelFolderId = nextFolderIdObject.id++ sites.push({ title: topLevelFolder, folderId: topLevelFolderId, @@ -104,26 +124,10 @@ importer.on('add-bookmarks', (e, bookmarks, topLevelFolder) => { pathMap['Links'] = 0 // Edge, IE } for (let i = 0; i < bookmarks.length; ++i) { - const pathLen = bookmarks[i].path.length - let parentFolderId = topLevelFolderId - if (pathLen) { - const parentFolder = bookmarks[i].path[pathLen - 1] - parentFolderId = pathMap[parentFolder] - if (parentFolderId === undefined) { - parentFolderId = nextFolderId++ - pathMap[parentFolder] = parentFolderId - const folder = { - customTitle: parentFolder, - folderId: parentFolderId, - parentFolderId: pathMap[bookmarks[i].path[pathLen - 2]] === undefined ? topLevelFolderId : pathMap[bookmarks[i].path[pathLen - 2]], - lastAccessedTime: (new Date()).getTime(), - tags: [siteTags.BOOKMARK_FOLDER] - } - sites.push(folder) - } - } + let path = bookmarks[i].path + let parentFolderId = getParentFolderId(path, pathMap, sites, topLevelFolderId, nextFolderIdObject) if (bookmarks[i].is_folder) { - const folderId = nextFolderId++ + const folderId = nextFolderIdObject.id++ pathMap[bookmarks[i].title] = folderId const folder = { customTitle: bookmarks[i].title,