-
-
Notifications
You must be signed in to change notification settings - Fork 34
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 #327 from penge/export
Export notes
- Loading branch information
Showing
13 changed files
with
157 additions
and
42 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
}, | ||
"dependencies": { | ||
"clsx": "^1.1.1", | ||
"fflate": "^0.7.1", | ||
"preact": "10.5.14" | ||
} | ||
} |
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,31 @@ | ||
import { zipSync, Zippable } from "fflate"; | ||
import { NotesObject } from "shared/storage/schema"; | ||
import { downloadBlob } from "shared/download"; | ||
|
||
export const exportNote = (noteName: string): void => chrome.storage.local.get("notes", (local) => { | ||
const { notes } = local as { notes: NotesObject }; | ||
if (!(noteName in notes)) { | ||
return; // there is no note to export | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(notes[noteName].content); | ||
|
||
downloadBlob(data, `${noteName}.html`, "text/html"); | ||
}); | ||
|
||
export const exportNotes = (): void => chrome.storage.local.get("notes", (local) => { | ||
const { notes } = local as { notes: NotesObject }; | ||
if (!Object.keys(notes).length) { | ||
return; // there are no notes to export | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
const data: Zippable = Object.keys(notes).reduce((acc, curr) => { | ||
acc[`${curr}.html`] = encoder.encode(notes[curr].content); | ||
return acc; | ||
}, {} as Zippable); | ||
|
||
const gzipped = zipSync(data, { level: 0 }); | ||
downloadBlob(gzipped, "notes.zip", "application/zip"); | ||
}); |
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,31 @@ | ||
import { h, Fragment } from "preact"; | ||
import clsx from "clsx"; | ||
import { exportNotes } from "notes/export"; | ||
|
||
let locked = false; | ||
|
||
interface ExportProps { | ||
canExport: boolean | ||
} | ||
|
||
const Export = ({ canExport }: ExportProps): h.JSX.Element => ( | ||
<Fragment> | ||
<h2>Export</h2> | ||
<input | ||
type="button" | ||
class={clsx("bold", "button", !canExport && "disabled")} | ||
value="Export all notes" | ||
onClick={() => { | ||
if (!canExport || locked) { | ||
return; | ||
} | ||
|
||
locked = true; | ||
exportNotes(); | ||
window.setTimeout(() => locked = false, 1000); | ||
}} | ||
/> | ||
</Fragment> | ||
); | ||
|
||
export default Export; |
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,17 @@ | ||
export const downloadURL = (data: string, fileName: string): void => { | ||
const a = document.createElement("a"); | ||
a.href = data; | ||
a.download = fileName; | ||
a.click(); | ||
a.remove(); | ||
}; | ||
|
||
export const downloadBlob = (data: Uint8Array, fileName: string, mimeType: string): void => { | ||
const blob = new Blob([data], { | ||
type: mimeType, | ||
}); | ||
|
||
const url = window.URL.createObjectURL(blob); | ||
downloadURL(url, fileName); | ||
window.setTimeout(() => window.URL.revokeObjectURL(url), 2000); | ||
}; |
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