Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Export file
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jan 15, 2017
1 parent 76e5c5d commit baa445d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
72 changes: 65 additions & 7 deletions app/bookmarksExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,89 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

'strict mode'
'use strict'

const path = require('path')
const moment = require('moment')
const fs = require('fs')
const electron = require('electron')
const dialog = electron.dialog
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const moment = require('moment')
const getSetting = require('../js/settings').getSetting
const settings = require('../js/constants/settings')
const siteTags = require('../js/constants/siteTags')
const siteUtil = require('../js/state/siteUtil')
const isWindows = process.platform === 'win32'
const indentLength = 2
const indentType = ' '

module.exports.dialog = () => {
module.exports.dialog = (sites) => {
const focusedWindow = BrowserWindow.getFocusedWindow()
const fileName = moment().format('DD_MM_YYYY') + '.html'
const defaultPath = path.join(getSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH) || app.getPath('downloads'), fileName)
let personal = []
let other = []

dialog.showSaveDialog(focusedWindow, {
defaultPath: defaultPath,
filters: [{
name: 'HTML',
extensions: ['html']
}]
}, (filePath) => {
if (filePath) {
console.log(filePath)
}, (fileName) => {
if (fileName) {
personal = createBookmarkArray(sites)
other = createBookmarkArray(sites, -1, false)
fs.writeFileSync(fileName, createStringHTML(personal, other))
}
})
}

function createBookmarkArray (sites, parentFolderId, first = true, depth = 1) {
const filteredBookmarks = parentFolderId
? sites.filter((site) => site.get('parentFolderId') === parentFolderId)
: sites.filter((site) => !site.get('parentFolderId'))
let payload = []
let title
let indentFirst = indentType.repeat(depth * indentLength)
let indentNext = (!first) ? indentFirst : indentType.repeat((depth + 1) * indentLength)

if (first) payload.push(`${indentFirst}<DL><p>`)

filteredBookmarks.forEach((site) => {
if (site.get('tags').includes(siteTags.BOOKMARK) && site.get('location')) {
title = site.get('customTitle') || site.get('title') || site.get('location')
payload.push(`${indentNext}<DT><A HREF="${site.get('location')}">${title}</A>`)
} else if (siteUtil.isFolder(site)) {
const folderId = site.get('folderId')
const submenuItems = sites.filter((bookmark) => bookmark.get('parentFolderId') === folderId)

if (submenuItems.count() > 0) {
title = site.get('customTitle') || site.get('title')
payload.push(`${indentNext}<DT><H3>${title}</H3>`)
payload = payload.concat(createBookmarkArray(sites, folderId, true, (depth + 1)))
}
}
})

if (first) payload.push(`${indentFirst}</DL><p>`)

return payload
}

function createStringHTML (personal, other) {
const breakTag = (isWindows) ? '\r\n' : '\n'
const title = 'Bookmarks'

return `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file. It will be read and overwritten. DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>${title}</TITLE>
<H1>${title}</H1>
<DL><p>
<DT><H3 PERSONAL_TOOLBAR_FOLDER="true">Bookmarks Bar</H3>
${personal.join(breakTag)}
${other.join(breakTag)}
</DL><p>`
}
1 change: 1 addition & 0 deletions app/extensions/brave/locales/en-US/bookmarks.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ importBrowserData.title=Import Browser Data
allFolders=All Folders
addBookmarkFolder.title=Add Folder
addBookmark.title=Add Bookmark
exportBookmarks.title=Export bookmarks
6 changes: 2 additions & 4 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,8 @@ app.on('ready', () => {
Importer.init()
})

// TODO check if needed
ipcMain.on(messages.EXPORT_BOOKMARKS, () => {
BookmarksExporter.dialog()
BookmarksExporter.dialog(AppStore.getState().get('sites'))
})

// This loads package.json into an object
Expand All @@ -751,9 +750,8 @@ app.on('ready', () => {
Importer.init()
})

// This is fired by a menu entry
process.on(messages.EXPORT_BOOKMARKS, () => {
BookmarksExporter.dialog()
BookmarksExporter.dialog(AppStore.getState().get('sites'))
})
})
ready = true
Expand Down
7 changes: 7 additions & 0 deletions js/about/aboutActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ const aboutActions = {
ipc.send(messages.IMPORT_BROWSER_DATA_NOW)
},

/**
* Export bookmarks
*/
exportBookmarks: function () {
ipc.send(messages.EXPORT_BOOKMARKS)
},

createWallet: function () {
ipc.send(messages.LEDGER_CREATE_WALLET)
},
Expand Down
7 changes: 6 additions & 1 deletion js/about/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ class AboutBookmarks extends React.Component {
this.onChangeSearch = this.onChangeSearch.bind(this)
this.onClearSearchText = this.onClearSearchText.bind(this)
this.importBrowserData = this.importBrowserData.bind(this)
this.exportBookmarks = this.exportBookmarks.bind(this)
this.addBookmarkFolder = this.addBookmarkFolder.bind(this)
this.onClick = this.onClick.bind(this)
this.clearSelection = this.clearSelection.bind(this)
Expand Down Expand Up @@ -428,6 +429,9 @@ class AboutBookmarks extends React.Component {
importBrowserData () {
aboutActions.importBrowserDataNow()
}
exportBookmarks () {
aboutActions.exportBookmarks()
}
addBookmarkFolder () {
const newFolder = Immutable.fromJS({
parentFolderId: this.state.selectedFolderId,
Expand Down Expand Up @@ -461,7 +465,8 @@ class AboutBookmarks extends React.Component {
<div className='folderView'>
<div className='columnHeader'>
<span data-l10n-id='folders' />
<span data-l10n-id='importBrowserData' className='fa fa-download importBrowserData' onClick={this.importBrowserData} />
<span data-l10n-id='importBrowserData' className='fa fa-upload importBrowserData' onClick={this.importBrowserData} />
<span data-l10n-id='exportBookmarks' className='fa fa-download exportBookmarks' onClick={this.exportBookmarks} />
<span data-l10n-id='addBookmarkFolder' className='addBookmarkFolder' onClick={this.addBookmarkFolder} />
</div>
<BookmarkFolderList
Expand Down
7 changes: 6 additions & 1 deletion less/about/bookmarks.less
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
-webkit-user-select: none;
cursor: default;

.importBrowserData {
.importBrowserData,
.exportBookmarks {
color: @buttonColor;
float: right;
font-size: 20px;
Expand All @@ -58,6 +59,10 @@
color: #000;
}
}

.exportBookmarks {
margin: 1px 7px 0 0;
}
}

> .bookmarkFolderList {
Expand Down

0 comments on commit baa445d

Please sign in to comment.