@fiduswriter/books 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +87 -2
- package/css/book.css +219 -0
- package/dist/exporter/bits/index.d.ts +42 -0
- package/dist/exporter/bits/index.d.ts.map +1 -0
- package/dist/exporter/bits/index.js +113 -0
- package/dist/exporter/bits/index.js.map +1 -0
- package/dist/exporter/bits/templates.d.ts +7 -0
- package/dist/exporter/bits/templates.d.ts.map +1 -0
- package/dist/exporter/bits/templates.js +78 -0
- package/dist/exporter/bits/templates.js.map +1 -0
- package/dist/exporter/docx/index.d.ts +33 -0
- package/dist/exporter/docx/index.d.ts.map +1 -0
- package/dist/exporter/docx/index.js +139 -0
- package/dist/exporter/docx/index.js.map +1 -0
- package/dist/exporter/docx/render.d.ts +19 -0
- package/dist/exporter/docx/render.d.ts.map +1 -0
- package/dist/exporter/docx/render.js +123 -0
- package/dist/exporter/docx/render.js.map +1 -0
- package/dist/exporter/epub/index.d.ts +70 -0
- package/dist/exporter/epub/index.d.ts.map +1 -0
- package/dist/exporter/epub/index.js +310 -0
- package/dist/exporter/epub/index.js.map +1 -0
- package/dist/exporter/epub/templates.d.ts +65 -0
- package/dist/exporter/epub/templates.d.ts.map +1 -0
- package/dist/exporter/epub/templates.js +224 -0
- package/dist/exporter/epub/templates.js.map +1 -0
- package/dist/exporter/epub/tools.d.ts +5 -0
- package/dist/exporter/epub/tools.d.ts.map +1 -0
- package/dist/exporter/epub/tools.js +59 -0
- package/dist/exporter/epub/tools.js.map +1 -0
- package/dist/exporter/html/index.d.ts +70 -0
- package/dist/exporter/html/index.d.ts.map +1 -0
- package/dist/exporter/html/index.js +320 -0
- package/dist/exporter/html/index.js.map +1 -0
- package/dist/exporter/html/templates.d.ts +40 -0
- package/dist/exporter/html/templates.d.ts.map +1 -0
- package/dist/exporter/html/templates.js +225 -0
- package/dist/exporter/html/templates.js.map +1 -0
- package/dist/exporter/html/tools.d.ts +2 -0
- package/dist/exporter/html/tools.d.ts.map +1 -0
- package/dist/exporter/html/tools.js +23 -0
- package/dist/exporter/html/tools.js.map +1 -0
- package/dist/exporter/latex/index.d.ts +36 -0
- package/dist/exporter/latex/index.d.ts.map +1 -0
- package/dist/exporter/latex/index.js +116 -0
- package/dist/exporter/latex/index.js.map +1 -0
- package/dist/exporter/latex/templates.d.ts +4 -0
- package/dist/exporter/latex/templates.d.ts.map +1 -0
- package/dist/exporter/latex/templates.js +26 -0
- package/dist/exporter/latex/templates.js.map +1 -0
- package/dist/exporter/native/index.d.ts +22 -0
- package/dist/exporter/native/index.d.ts.map +1 -0
- package/dist/exporter/native/index.js +141 -0
- package/dist/exporter/native/index.js.map +1 -0
- package/dist/exporter/odt/index.d.ts +33 -0
- package/dist/exporter/odt/index.d.ts.map +1 -0
- package/dist/exporter/odt/index.js +133 -0
- package/dist/exporter/odt/index.js.map +1 -0
- package/dist/exporter/odt/render.d.ts +21 -0
- package/dist/exporter/odt/render.d.ts.map +1 -0
- package/dist/exporter/odt/render.js +126 -0
- package/dist/exporter/odt/render.js.map +1 -0
- package/dist/exporter/pretty.d.ts +17 -0
- package/dist/exporter/pretty.d.ts.map +1 -0
- package/dist/exporter/pretty.js +55 -0
- package/dist/exporter/pretty.js.map +1 -0
- package/dist/exporter/print/index.d.ts +25 -0
- package/dist/exporter/print/index.d.ts.map +1 -0
- package/dist/exporter/print/index.js +46 -0
- package/dist/exporter/print/index.js.map +1 -0
- package/dist/exporter/print/templates.d.ts +2 -0
- package/dist/exporter/print/templates.d.ts.map +1 -0
- package/dist/exporter/print/templates.js +5 -0
- package/dist/exporter/print/templates.js.map +1 -0
- package/dist/exporter/tools.d.ts +31 -0
- package/dist/exporter/tools.d.ts.map +1 -0
- package/dist/exporter/tools.js +177 -0
- package/dist/exporter/tools.js.map +1 -0
- package/dist/i18n.d.ts +7 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +918 -0
- package/dist/i18n.js.map +1 -0
- package/dist/ids.d.ts +19 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +27 -0
- package/dist/ids.js.map +1 -0
- package/dist/importer/native/index.d.ts +45 -0
- package/dist/importer/native/index.d.ts.map +1 -0
- package/dist/importer/native/index.js +166 -0
- package/dist/importer/native/index.js.map +1 -0
- package/dist/importer/native/reader.d.ts +32 -0
- package/dist/importer/native/reader.d.ts.map +1 -0
- package/dist/importer/native/reader.js +170 -0
- package/dist/importer/native/reader.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +8 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/types.d.ts +171 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/book_dialog/dialog.d.ts +161 -0
- package/dist/ui/book_dialog/dialog.d.ts.map +1 -0
- package/dist/ui/book_dialog/dialog.js +750 -0
- package/dist/ui/book_dialog/dialog.js.map +1 -0
- package/dist/ui/book_dialog/export.d.ts +95 -0
- package/dist/ui/book_dialog/export.d.ts.map +1 -0
- package/dist/ui/book_dialog/export.js +217 -0
- package/dist/ui/book_dialog/export.js.map +1 -0
- package/dist/ui/book_dialog/index.d.ts +38 -0
- package/dist/ui/book_dialog/index.d.ts.map +1 -0
- package/dist/ui/book_dialog/index.js +36 -0
- package/dist/ui/book_dialog/index.js.map +1 -0
- package/dist/ui/book_dialog/sanity_check.d.ts +22 -0
- package/dist/ui/book_dialog/sanity_check.d.ts.map +1 -0
- package/dist/ui/book_dialog/sanity_check.js +109 -0
- package/dist/ui/book_dialog/sanity_check.js.map +1 -0
- package/dist/ui/book_dialog/templates.d.ts +76 -0
- package/dist/ui/book_dialog/templates.d.ts.map +1 -0
- package/dist/ui/book_dialog/templates.js +542 -0
- package/dist/ui/book_dialog/templates.js.map +1 -0
- package/dist/ui/book_dialog/types.d.ts +258 -0
- package/dist/ui/book_dialog/types.d.ts.map +1 -0
- package/dist/ui/book_dialog/types.js +15 -0
- package/dist/ui/book_dialog/types.js.map +1 -0
- package/locale/ar/messages.json +49 -0
- package/locale/bg/messages.json +49 -0
- package/locale/cs/messages.json +49 -0
- package/locale/da/messages.json +49 -0
- package/locale/de/messages.json +49 -0
- package/locale/en/messages.json +49 -0
- package/locale/en_US/messages.json +49 -0
- package/locale/es/messages.json +49 -0
- package/locale/fr/messages.json +49 -0
- package/locale/it/messages.json +49 -0
- package/locale/ja/messages.json +49 -0
- package/locale/ko/messages.json +49 -0
- package/locale/nb/messages.json +49 -0
- package/locale/nl/messages.json +49 -0
- package/locale/pl/messages.json +49 -0
- package/locale/pt_BR/messages.json +49 -0
- package/locale/pt_PT/messages.json +49 -0
- package/locale/ru/messages.json +49 -0
- package/locale/sv/messages.json +49 -0
- package/locale/tr/messages.json +49 -0
- package/locale/zh_Hans/messages.json +49 -0
- package/logo.svg +10 -0
- package/package.json +150 -5
- package/src/exporter/bits/index.ts +184 -0
- package/src/exporter/bits/templates.ts +107 -0
- package/src/exporter/docx/index.ts +258 -0
- package/src/exporter/docx/render.ts +177 -0
- package/src/exporter/epub/index.ts +496 -0
- package/src/exporter/epub/templates.ts +403 -0
- package/src/exporter/epub/tools.ts +68 -0
- package/src/exporter/html/index.ts +479 -0
- package/src/exporter/html/templates.ts +362 -0
- package/src/exporter/html/tools.ts +26 -0
- package/src/exporter/latex/index.ts +190 -0
- package/src/exporter/latex/templates.ts +35 -0
- package/src/exporter/native/index.ts +238 -0
- package/src/exporter/odt/index.ts +230 -0
- package/src/exporter/odt/render.ts +181 -0
- package/src/exporter/pretty.ts +75 -0
- package/src/exporter/print/index.ts +76 -0
- package/src/exporter/print/templates.ts +8 -0
- package/src/exporter/tools.ts +270 -0
- package/src/global.d.ts +2 -0
- package/src/i18n.ts +936 -0
- package/src/ids.ts +27 -0
- package/src/importer/native/index.ts +269 -0
- package/src/importer/native/reader.ts +247 -0
- package/src/index.ts +20 -0
- package/src/modules.d.ts +7 -0
- package/src/schema/index.ts +8 -0
- package/src/types.ts +208 -0
- package/src/ui/book_dialog/dialog.ts +940 -0
- package/src/ui/book_dialog/export.ts +449 -0
- package/src/ui/book_dialog/index.ts +84 -0
- package/src/ui/book_dialog/sanity_check.ts +208 -0
- package/src/ui/book_dialog/templates.ts +736 -0
- package/src/ui/book_dialog/types.ts +283 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local replacement for the unmaintained `pretty` package (v2.0.0), backed by
|
|
3
|
+
* js-beautify 2. It replicates pretty's behavior exactly: HTML beautification
|
|
4
|
+
* with fixed defaults, plus the "ocd" post-processing that condenses newlines
|
|
5
|
+
* and spaces out comments.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import jsBeautify from "js-beautify"
|
|
9
|
+
|
|
10
|
+
const beautify = jsBeautify as unknown as {
|
|
11
|
+
html: (source: string, options?: object) => string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const DEFAULTS = {
|
|
15
|
+
unformatted: ["code", "pre", "em", "strong", "span"],
|
|
16
|
+
indent_inner_html: true,
|
|
17
|
+
indent_char: " ",
|
|
18
|
+
indent_size: 2,
|
|
19
|
+
sep: "\n"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PrettyOptions {
|
|
23
|
+
ocd?: boolean
|
|
24
|
+
newlines?: string
|
|
25
|
+
indent_size?: number
|
|
26
|
+
indent_char?: string
|
|
27
|
+
indent_inner_html?: boolean
|
|
28
|
+
unformatted?: string[]
|
|
29
|
+
sep?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default function pretty(str: string, options: PrettyOptions = {}): string {
|
|
33
|
+
const opts: PrettyOptions & {sep: string} = {...DEFAULTS, ...options} as PrettyOptions & {sep: string}
|
|
34
|
+
str = beautify.html(str, opts)
|
|
35
|
+
|
|
36
|
+
if (opts.ocd === true) {
|
|
37
|
+
if (opts.newlines) {
|
|
38
|
+
opts.sep = opts.newlines
|
|
39
|
+
}
|
|
40
|
+
return ocd(str, opts)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return str
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function ocd(str: string, options: PrettyOptions & {sep?: string}): string {
|
|
47
|
+
return condenseNewlines(str, options)
|
|
48
|
+
// Remove empty whitespace from the top of the file.
|
|
49
|
+
.replace(/^\s+/g, "")
|
|
50
|
+
// Remove extra whitespace from eof.
|
|
51
|
+
.replace(/\s+$/g, "\n")
|
|
52
|
+
// Add a space above each comment.
|
|
53
|
+
.replace(/(\s*<!--)/g, "\n$1")
|
|
54
|
+
// Bring closing comments up to the same line as closing tag.
|
|
55
|
+
.replace(/>(\s*)(?=<!--\s*\/)/g, "> ")
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Inline replacement for the condense-newlines package (v0.2.1) as pretty used
|
|
59
|
+
// it: collapse runs of 2+ newlines into options.sep, with all-whitespace lines
|
|
60
|
+
// emptied first. \u2424 is the historic "symbol for newline" that the package
|
|
61
|
+
// also treated as a line break.
|
|
62
|
+
function condenseNewlines(str: string, options: {sep?: string}): string {
|
|
63
|
+
const sep = options.sep || "\n\n"
|
|
64
|
+
str = str
|
|
65
|
+
.split("\n")
|
|
66
|
+
.map(line => (isWhitespace(line) ? line.trim() : line))
|
|
67
|
+
.join("\n")
|
|
68
|
+
str = str.replace(/\s+$/, "\n")
|
|
69
|
+
return str.replace(/(\r\n|\n|\u2424){2,}/g, sep)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isWhitespace(str: string): boolean {
|
|
73
|
+
// Regex copied from is-whitespace@0.3.0 for exact parity.
|
|
74
|
+
return /^[\s\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF"]+$/.test(str)
|
|
75
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Print book exporter.
|
|
3
|
+
*
|
|
4
|
+
* Renders the book in the browser using the selected pagination engine (see
|
|
5
|
+
* `@fiduswriter/document/exporter/print`).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {Schema} from "prosemirror-model"
|
|
9
|
+
import type {CSL, User} from "@fiduswriter/document"
|
|
10
|
+
import {getPrintEngine} from "@fiduswriter/document/exporter/print/index"
|
|
11
|
+
|
|
12
|
+
import type {Book, BookStyles, DocumentListEntry} from "../../types.js"
|
|
13
|
+
import {HTMLBookExporter} from "../html/index.js"
|
|
14
|
+
import {chapterTemplate} from "./templates.js"
|
|
15
|
+
|
|
16
|
+
export class PrintBookExporter extends HTMLBookExporter {
|
|
17
|
+
constructor(
|
|
18
|
+
schema: Schema,
|
|
19
|
+
csl: CSL,
|
|
20
|
+
documentStyles: BookStyles,
|
|
21
|
+
book: Book,
|
|
22
|
+
user: User,
|
|
23
|
+
docList: DocumentListEntry[]
|
|
24
|
+
) {
|
|
25
|
+
super(schema, csl, documentStyles, book, user, docList, 0, false, {
|
|
26
|
+
relativeUrls: false
|
|
27
|
+
})
|
|
28
|
+
this.chapterTemplate = chapterTemplate
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
addBookStyle(): boolean {
|
|
32
|
+
const bookStyle = this.bookStyles.find(
|
|
33
|
+
style => style.slug === this.book.settings.book_style
|
|
34
|
+
)
|
|
35
|
+
if (!bookStyle) {
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
let contents = bookStyle.contents
|
|
39
|
+
bookStyle.bookstylefile_set.forEach(
|
|
40
|
+
([url, filename]: [string, string]) =>
|
|
41
|
+
(contents = contents.replace(
|
|
42
|
+
new RegExp(filename, "g"),
|
|
43
|
+
url
|
|
44
|
+
))
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
this.styleSheets.push({contents})
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async createZip(): Promise<void> {
|
|
52
|
+
const htmlDoc = this.textFiles.find(
|
|
53
|
+
file => file.filename === "index.html"
|
|
54
|
+
)?.contents
|
|
55
|
+
if (!htmlDoc) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
const engine = getPrintEngine()
|
|
59
|
+
;(window as unknown as {printInstance?: unknown}).printInstance = {
|
|
60
|
+
engine: engine.name
|
|
61
|
+
}
|
|
62
|
+
await engine.print({html: htmlDoc, title: this.book.title})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async loadStyle(sheet: {
|
|
66
|
+
url?: string
|
|
67
|
+
filename?: string
|
|
68
|
+
contents?: string
|
|
69
|
+
}): Promise<{url?: string; filename?: string; contents?: string}> {
|
|
70
|
+
if (sheet.url) {
|
|
71
|
+
sheet.filename = sheet.url
|
|
72
|
+
delete sheet.url
|
|
73
|
+
}
|
|
74
|
+
return sheet
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared tools for book exporters.
|
|
3
|
+
*
|
|
4
|
+
* `getMissingChapterData` is fully dependency-injected: it no longer imports
|
|
5
|
+
* browser/server-specific helpers from the Fidus Writer core. Callers supply
|
|
6
|
+
* a `ChapterLoader` to fetch missing chapter data and an `E2EEStrategy` to
|
|
7
|
+
* decrypt end-to-end encrypted chapters.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {Node, Schema} from "prosemirror-model"
|
|
11
|
+
import {getSettings} from "@fiduswriter/document/schema/convert"
|
|
12
|
+
import {acceptAllNoInsertions} from "@fiduswriter/document/transform"
|
|
13
|
+
import {gettext} from "fwtoolkit"
|
|
14
|
+
import type {ProgressCallback} from "@fiduswriter/document/exporter/tools/progress"
|
|
15
|
+
import type {FidusDoc} from "@fiduswriter/document"
|
|
16
|
+
|
|
17
|
+
import type {
|
|
18
|
+
ChapterLoader,
|
|
19
|
+
DocumentListEntry,
|
|
20
|
+
E2EEStrategy,
|
|
21
|
+
Book
|
|
22
|
+
} from "../types.js"
|
|
23
|
+
|
|
24
|
+
/** Default no-op chapter loader. */
|
|
25
|
+
export const noopChapterLoader: ChapterLoader = {
|
|
26
|
+
loadChapters: async () => {
|
|
27
|
+
// noop
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Default no-op E2EE strategy. */
|
|
32
|
+
export const noopE2EEStrategy: E2EEStrategy = {
|
|
33
|
+
ensurePassphraseUnlocked: async () => false,
|
|
34
|
+
getDocumentPassword: async () => null,
|
|
35
|
+
resolvePasswordToKey: async () => {
|
|
36
|
+
throw new Error("No E2EE strategy provided")
|
|
37
|
+
},
|
|
38
|
+
decryptObject: async () => {
|
|
39
|
+
throw new Error("No E2EE strategy provided")
|
|
40
|
+
},
|
|
41
|
+
decryptImageToUrl: async () => {
|
|
42
|
+
throw new Error("No E2EE strategy provided")
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Ensure all chapter data required for export is present.
|
|
48
|
+
*
|
|
49
|
+
* @param book - The book being exported.
|
|
50
|
+
* @param documentList - Document entries for the book's chapters.
|
|
51
|
+
* @param schema - ProseMirror schema used when parsing decrypted content.
|
|
52
|
+
* @param options - Optional loader and E2EE strategy.
|
|
53
|
+
* @returns Promise that resolves when data is ready.
|
|
54
|
+
*/
|
|
55
|
+
export async function getMissingChapterData(
|
|
56
|
+
book: Book,
|
|
57
|
+
documentList: DocumentListEntry[],
|
|
58
|
+
schema: Schema,
|
|
59
|
+
options: {
|
|
60
|
+
rawContent?: boolean
|
|
61
|
+
loader?: ChapterLoader
|
|
62
|
+
e2ee?: E2EEStrategy
|
|
63
|
+
progressCallback?: ProgressCallback
|
|
64
|
+
} = {}
|
|
65
|
+
): Promise<void> {
|
|
66
|
+
const {
|
|
67
|
+
rawContent = false,
|
|
68
|
+
loader = noopChapterLoader,
|
|
69
|
+
e2ee = noopE2EEStrategy,
|
|
70
|
+
progressCallback
|
|
71
|
+
} = options
|
|
72
|
+
|
|
73
|
+
progressCallback?.(gettext("Loading chapter data..."), 0)
|
|
74
|
+
|
|
75
|
+
const bookDocuments = book.chapters.map(chapter =>
|
|
76
|
+
documentList.find(doc => doc.id === chapter.text)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if (bookDocuments.some(doc => doc === undefined)) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
gettext(
|
|
82
|
+
"Cannot produce book as you lack access rights to its chapters."
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const docIds = book.chapters.map(chapter => chapter.text)
|
|
88
|
+
await loader.loadChapters(docIds, documentList, schema, rawContent)
|
|
89
|
+
await decryptE2EEChapters(book, documentList, schema, rawContent, e2ee)
|
|
90
|
+
|
|
91
|
+
// Chapter files read straight from disk (WebDAV/desktop hosts) or
|
|
92
|
+
// produced by importers may carry nodes without every schema attr filled
|
|
93
|
+
// in. Round-trip every chapter through the schema so exporters see
|
|
94
|
+
// exactly what the editor would have saved. Best-effort: a chapter whose
|
|
95
|
+
// content does not parse is left untouched, matching what exporters did
|
|
96
|
+
// with it before.
|
|
97
|
+
book.chapters.forEach(chapter => {
|
|
98
|
+
const doc = documentList.find(
|
|
99
|
+
doc => Number(doc.id) === Number(chapter.text)
|
|
100
|
+
)
|
|
101
|
+
if (
|
|
102
|
+
!doc ||
|
|
103
|
+
doc.missing ||
|
|
104
|
+
typeof doc.content !== "object" ||
|
|
105
|
+
!doc.content
|
|
106
|
+
) {
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
doc.content = schema.nodeFromJSON(
|
|
111
|
+
doc.content as Parameters<Schema["nodeFromJSON"]>[0]
|
|
112
|
+
).toJSON()
|
|
113
|
+
if (doc.rawContent && typeof doc.rawContent === "object") {
|
|
114
|
+
doc.rawContent = schema.nodeFromJSON(
|
|
115
|
+
doc.rawContent as Parameters<Schema["nodeFromJSON"]>[0]
|
|
116
|
+
).toJSON()
|
|
117
|
+
}
|
|
118
|
+
doc.settings = {
|
|
119
|
+
...doc.settings,
|
|
120
|
+
...getSettings(doc.content as unknown as FidusDoc)
|
|
121
|
+
}
|
|
122
|
+
} catch {
|
|
123
|
+
// Leave the chapter as stored and let the exporter handle it.
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
progressCallback?.(gettext("Chapter data loaded."), 50)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Decrypt all E2EE chapters in the book whose content is still an encrypted
|
|
132
|
+
* string.
|
|
133
|
+
*/
|
|
134
|
+
async function decryptE2EEChapters(
|
|
135
|
+
book: Book,
|
|
136
|
+
documentList: DocumentListEntry[],
|
|
137
|
+
schema: Schema,
|
|
138
|
+
rawContent: boolean,
|
|
139
|
+
e2ee: E2EEStrategy
|
|
140
|
+
): Promise<void> {
|
|
141
|
+
const e2eeChapters = book.chapters.filter(chapter => {
|
|
142
|
+
const doc = documentList.find(doc => doc.id === chapter.text)
|
|
143
|
+
return doc && doc.e2ee && typeof doc.content === "string"
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
if (!e2eeChapters.length) {
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const unlocked = await e2ee.ensurePassphraseUnlocked()
|
|
151
|
+
if (!unlocked) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
gettext(
|
|
154
|
+
"A personal passphrase is required to work with books that contain encrypted chapters. Please set up or unlock your personal passphrase in your profile settings."
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
await Promise.all(
|
|
160
|
+
e2eeChapters.map(async chapter => {
|
|
161
|
+
const doc = documentList.find(d => d.id === chapter.text)
|
|
162
|
+
if (!doc) {
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
const chapterLabel = doc.title
|
|
166
|
+
? `"${doc.title}"`
|
|
167
|
+
: gettext("Untitled")
|
|
168
|
+
|
|
169
|
+
const password = await e2ee.getDocumentPassword(Number(doc.id))
|
|
170
|
+
if (!password) {
|
|
171
|
+
throw new Error(
|
|
172
|
+
`${gettext("No encryption key found for chapter:")} ${chapterLabel}. ${gettext("The key may not have been shared with you.")}`
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!doc.e2ee_salt) {
|
|
177
|
+
// No salt means the document has no encrypted snapshot yet.
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const salt = base64ToUint8Array(doc.e2ee_salt)
|
|
182
|
+
const iterations = doc.e2ee_iterations || 600000
|
|
183
|
+
|
|
184
|
+
const key = await e2ee.resolvePasswordToKey(password, salt, iterations)
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
const decryptedContent = (await e2ee.decryptObject(
|
|
188
|
+
doc.content as unknown as string,
|
|
189
|
+
key
|
|
190
|
+
)) as Record<string, unknown>
|
|
191
|
+
|
|
192
|
+
// Update the plaintext title.
|
|
193
|
+
const titleNode = (decryptedContent.content as Record<string, unknown>[] | undefined)?.[0]
|
|
194
|
+
const titleContent = (titleNode?.content as Record<string, unknown>[] | undefined) || []
|
|
195
|
+
let title = ""
|
|
196
|
+
titleContent.forEach(child => {
|
|
197
|
+
const marks = (child.marks as Record<string, unknown>[] | undefined) || []
|
|
198
|
+
if (!marks.some(m => (m.type as string) === "deletion")) {
|
|
199
|
+
title += (child.text as string) || ""
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
if (title) {
|
|
203
|
+
doc.title = title.substring(0, 255)
|
|
204
|
+
if (typeof sessionStorage !== "undefined") {
|
|
205
|
+
sessionStorage.setItem(`e2ee_title_${String(doc.id)}`, doc.title)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Parse ProseMirror content.
|
|
210
|
+
if (rawContent) {
|
|
211
|
+
doc.rawContent = JSON.parse(
|
|
212
|
+
JSON.stringify(schema.nodeFromJSON(decryptedContent as unknown as Record<string, unknown>).toJSON())
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
doc.content = acceptAllNoInsertions(
|
|
216
|
+
schema.nodeFromJSON(decryptedContent as unknown as Record<string, unknown>) as Node
|
|
217
|
+
).toJSON()
|
|
218
|
+
doc.settings = getSettings(doc.content as unknown as FidusDoc)
|
|
219
|
+
|
|
220
|
+
// Decrypt encrypted images.
|
|
221
|
+
const encryptedImageEntries = Object.entries(doc.images || {}).filter(
|
|
222
|
+
([, entry]) =>
|
|
223
|
+
entry.file_type === "application/octet-stream" &&
|
|
224
|
+
typeof entry.image === "string"
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
if (!encryptedImageEntries.length) {
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
await Promise.all(
|
|
232
|
+
encryptedImageEntries.map(async ([id, entry]) => {
|
|
233
|
+
try {
|
|
234
|
+
const blobUrl = await e2ee.decryptImageToUrl(
|
|
235
|
+
entry.image as string,
|
|
236
|
+
key
|
|
237
|
+
)
|
|
238
|
+
doc.images![id] = {
|
|
239
|
+
...entry,
|
|
240
|
+
image: blobUrl,
|
|
241
|
+
file_type: "image/png"
|
|
242
|
+
}
|
|
243
|
+
} catch {
|
|
244
|
+
delete doc.images![id]
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
)
|
|
248
|
+
} catch (err) {
|
|
249
|
+
if (
|
|
250
|
+
err instanceof Error &&
|
|
251
|
+
err.message.startsWith("No encryption key")
|
|
252
|
+
) {
|
|
253
|
+
throw err
|
|
254
|
+
}
|
|
255
|
+
throw new Error(
|
|
256
|
+
`${gettext("Could not decrypt chapter:")} ${chapterLabel}. ${gettext("The document may have been re-encrypted with a different password.")}`
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
})
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function base64ToUint8Array(base64: string): Uint8Array {
|
|
264
|
+
const binary = atob(base64)
|
|
265
|
+
const bytes = new Uint8Array(binary.length)
|
|
266
|
+
for (let i = 0; i < binary.length; i++) {
|
|
267
|
+
bytes[i] = binary.charCodeAt(i)
|
|
268
|
+
}
|
|
269
|
+
return bytes
|
|
270
|
+
}
|
package/src/global.d.ts
ADDED