@fiduswriter/books-document 0.1.7 → 0.1.8

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.
Files changed (55) hide show
  1. package/README.md +69 -10
  2. package/dist/exporter/bits/index.d.ts +2 -2
  3. package/dist/exporter/bits/index.d.ts.map +1 -1
  4. package/dist/exporter/bits/index.js +1 -1
  5. package/dist/exporter/bits/index.js.map +1 -1
  6. package/dist/exporter/docx/index.d.ts +9 -5
  7. package/dist/exporter/docx/index.d.ts.map +1 -1
  8. package/dist/exporter/docx/index.js +2 -1
  9. package/dist/exporter/docx/index.js.map +1 -1
  10. package/dist/exporter/docx/render.d.ts +12 -7
  11. package/dist/exporter/docx/render.d.ts.map +1 -1
  12. package/dist/exporter/docx/render.js +18 -11
  13. package/dist/exporter/docx/render.js.map +1 -1
  14. package/dist/exporter/epub/index.d.ts.map +1 -1
  15. package/dist/exporter/epub/index.js +3 -3
  16. package/dist/exporter/epub/index.js.map +1 -1
  17. package/dist/exporter/html/index.d.ts +6 -6
  18. package/dist/exporter/html/index.d.ts.map +1 -1
  19. package/dist/exporter/html/index.js +4 -4
  20. package/dist/exporter/html/index.js.map +1 -1
  21. package/dist/exporter/latex/index.d.ts +2 -2
  22. package/dist/exporter/latex/index.d.ts.map +1 -1
  23. package/dist/exporter/latex/index.js +1 -1
  24. package/dist/exporter/latex/index.js.map +1 -1
  25. package/dist/exporter/native/index.d.ts +2 -2
  26. package/dist/exporter/native/index.d.ts.map +1 -1
  27. package/dist/exporter/native/index.js +1 -1
  28. package/dist/exporter/native/index.js.map +1 -1
  29. package/dist/exporter/odt/index.d.ts +9 -5
  30. package/dist/exporter/odt/index.d.ts.map +1 -1
  31. package/dist/exporter/odt/index.js +2 -1
  32. package/dist/exporter/odt/index.js.map +1 -1
  33. package/dist/exporter/odt/render.d.ts +14 -8
  34. package/dist/exporter/odt/render.d.ts.map +1 -1
  35. package/dist/exporter/odt/render.js +22 -15
  36. package/dist/exporter/odt/render.js.map +1 -1
  37. package/dist/exporter/print/index.d.ts.map +1 -1
  38. package/dist/exporter/print/index.js +3 -1
  39. package/dist/exporter/print/index.js.map +1 -1
  40. package/dist/exporter/tools.d.ts.map +1 -1
  41. package/dist/exporter/tools.js.map +1 -1
  42. package/dist/importer/native/index.js.map +1 -1
  43. package/package.json +9 -1
  44. package/src/exporter/bits/index.ts +3 -3
  45. package/src/exporter/docx/index.ts +26 -22
  46. package/src/exporter/docx/render.ts +51 -29
  47. package/src/exporter/epub/index.ts +25 -16
  48. package/src/exporter/html/index.ts +28 -16
  49. package/src/exporter/latex/index.ts +12 -11
  50. package/src/exporter/native/index.ts +5 -4
  51. package/src/exporter/odt/index.ts +23 -19
  52. package/src/exporter/odt/render.ts +54 -31
  53. package/src/exporter/print/index.ts +3 -1
  54. package/src/exporter/tools.ts +5 -4
  55. package/src/importer/native/index.ts +6 -6
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import type {Schema} from "prosemirror-model"
6
- import type {CSL, User} from "@fiduswriter/document"
6
+ import type {CSL, ExportMetadata, FidusNode, User} from "@fiduswriter/document"
7
7
  import type {ProgressCallback} from "@fiduswriter/document/exporter/tools/progress"
8
8
  import {
9
9
  fixTables,
@@ -33,7 +33,7 @@ export class ODTBookExporter {
33
33
  user: User
34
34
  docList: DocumentListEntry[]
35
35
  templateUrl: string
36
- updated: any
36
+ updated: number
37
37
  textFiles: Array<Record<string, unknown>>
38
38
  httpFiles: Array<Record<string, unknown>>
39
39
  mimeType: string
@@ -45,7 +45,7 @@ export class ODTBookExporter {
45
45
  book: Book,
46
46
  user: User,
47
47
  docList: DocumentListEntry[],
48
- updated: any
48
+ updated: number
49
49
  ) {
50
50
  this.schema = schema
51
51
  this.csl = csl
@@ -83,14 +83,15 @@ export class ODTBookExporter {
83
83
  this.progressCallback?.(gettext("Preparing ODT book..."), 10)
84
84
  this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1))
85
85
  const xml = new XmlZip(this.templateUrl, this.mimeType)
86
- const styles = new ODTExporterStyles(xml as any)
87
- const math = new ODTExporterMath(xml as any)
88
- const tracks = new ODTExporterTracks(xml as any)
89
- const render = new ODTBookExporterRender(xml as any, styles)
86
+ const styles = new ODTExporterStyles(xml)
87
+ const math = new ODTExporterMath(xml)
88
+ const tracks = new ODTExporterTracks(xml)
89
+ const render = new ODTBookExporterRender(xml, styles)
90
90
  const metadata = new ODTExporterMetadata(
91
- xml as any,
91
+ xml,
92
92
  styles,
93
- this.getBaseMetadata() as any
93
+ this.getBaseMetadata(),
94
+ this.csl
94
95
  )
95
96
 
96
97
  return xml
@@ -100,7 +101,7 @@ export class ODTBookExporter {
100
101
  .then(() => render.init())
101
102
  .then(() => metadata.init())
102
103
  .then(() => math.init())
103
- .then(() => this.exportChapters(xml as any, render, styles, math, tracks))
104
+ .then(() => this.exportChapters(xml, render, styles, math, tracks))
104
105
  .then(() => {
105
106
  this.progressCallback?.(gettext("Finalizing ODT book..."), 90)
106
107
  return xml.prepareBlob()
@@ -115,11 +116,11 @@ export class ODTBookExporter {
115
116
  }
116
117
 
117
118
  exportChapters(
118
- xml: any,
119
+ xml: XmlZip,
119
120
  render: ODTBookExporterRender,
120
- styles: any,
121
- math: any,
122
- tracks: any
121
+ styles: ODTExporterStyles,
122
+ math: ODTExporterMath,
123
+ tracks: ODTExporterTracks
123
124
  ): Promise<void> {
124
125
  return this.book.chapters
125
126
  .map((chapter, chapterIndex) => {
@@ -129,10 +130,12 @@ export class ODTBookExporter {
129
130
  return Promise.resolve()
130
131
  }
131
132
  const docContent = fixTables(
132
- removeHidden(doc.rawContent as any) as any
133
- ) as any
133
+ removeHidden(
134
+ doc.rawContent as unknown as FidusNode
135
+ ) as FidusNode
136
+ )
134
137
  const citations = new ODTExporterCitations(
135
- docContent as any,
138
+ docContent,
136
139
  doc.settings,
137
140
  styles,
138
141
  {db: doc.bibliography || {}},
@@ -169,7 +172,7 @@ export class ODTBookExporter {
169
172
  .then(() => {
170
173
  const pmBib = footnotes.pmBib || citations.pmBib
171
174
  render.render(
172
- docContent as any,
175
+ docContent,
173
176
  pmBib,
174
177
  doc.settings,
175
178
  richtext,
@@ -199,7 +202,7 @@ export class ODTBookExporter {
199
202
  .then(() => render.assemble())
200
203
  }
201
204
 
202
- getBaseMetadata(): Record<string, unknown> {
205
+ getBaseMetadata(): ExportMetadata {
203
206
  const authors = this.book.metadata.author?.length
204
207
  ? [{institution: this.book.metadata.author}]
205
208
  : []
@@ -210,6 +213,7 @@ export class ODTBookExporter {
210
213
  : []
211
214
  return {
212
215
  authors,
216
+ contributors: [],
213
217
  keywords,
214
218
  title: this.book.title,
215
219
  language: this.book.settings.language
@@ -1,14 +1,20 @@
1
+ import type {DocSettings, FidusNode} from "@fiduswriter/document"
2
+ import {XMLElement} from "@fiduswriter/document/exporter/tools/xml"
3
+ import type {XmlZip} from "@fiduswriter/document/exporter/tools/xml_zip"
4
+ import {ODTExporterCitations} from "@fiduswriter/document/exporter/odt/citations"
1
5
  import {ODTExporterRender} from "@fiduswriter/document/exporter/odt/render"
6
+ import {ODTExporterRichtext} from "@fiduswriter/document/exporter/odt/richtext"
7
+ import type {ODTExporterStyles} from "@fiduswriter/document/exporter/odt/styles"
2
8
 
3
9
  export class ODTBookExporterRender extends ODTExporterRender {
4
- styles: any
5
- preamble: any
6
- bodyTemplate: any
7
- postamble: any
8
- bodyParts: any[]
9
- fileXml: any
10
+ styles: ODTExporterStyles
11
+ preamble: XMLElement | null
12
+ bodyTemplate: XMLElement | null
13
+ postamble: XMLElement | null
14
+ bodyParts: XMLElement[]
15
+ fileXml: XMLElement | null
10
16
 
11
- constructor(xml: any, styles: any) {
17
+ constructor(xml: XmlZip, styles: ODTExporterStyles) {
12
18
  super(xml)
13
19
 
14
20
  this.styles = styles
@@ -22,16 +28,18 @@ export class ODTBookExporterRender extends ODTExporterRender {
22
28
  }
23
29
 
24
30
  init(): Promise<void> {
25
- return this.xml.getXml(this.filePath).then((xml: any) => {
31
+ return this.xml.getXml(this.filePath).then(xml => {
26
32
  this.fileXml = xml
27
- const text = xml.query("office:text")
33
+ const text = xml.query("office:text") as XMLElement
28
34
  this.preamble = text.cloneNode(false)
29
35
  this.bodyTemplate = text.cloneNode(false)
30
36
  this.postamble = text.cloneNode(false)
31
- let currentSection: any
32
- const textChildren = Array.from(text.children)
33
- textChildren.forEach((node: any) => {
34
- if (["text:p", "text:h"].includes(node.tagName)) {
37
+ let currentSection: XMLElement | null = null
38
+ const textChildren = Array.from(text.children).filter(
39
+ (node): node is XMLElement => node instanceof XMLElement
40
+ )
41
+ textChildren.forEach(node => {
42
+ if (node.tagName && ["text:p", "text:h"].includes(node.tagName)) {
35
43
  const bookmark = node.query("text:bookmark")
36
44
  if (bookmark) {
37
45
  const sectionName = String(
@@ -58,10 +66,17 @@ export class ODTBookExporterRender extends ODTExporterRender {
58
66
  })
59
67
  }
60
68
 
61
- render(...args: any[]): void {
62
- const [docContent, pmBib, settings, richtext, citations, chapterIndex] = args
63
- this.text = this.bodyTemplate.cloneNode(true)
64
- const bodyBookmark = (this.text as any).query("text:bookmark", {
69
+ render(
70
+ docContent: FidusNode,
71
+ pmBib: unknown,
72
+ settings: DocSettings,
73
+ richtext: ODTExporterRichtext,
74
+ citations: ODTExporterCitations,
75
+ chapterIndex = 0
76
+ ): void {
77
+ this.text = this.bodyTemplate!.cloneNode(true)
78
+ const textEl = this.text as XMLElement
79
+ const bodyBookmark = textEl.query("text:bookmark", {
65
80
  "text:name": "body"
66
81
  })
67
82
  if (bodyBookmark) {
@@ -104,12 +119,18 @@ export class ODTBookExporterRender extends ODTExporterRender {
104
119
  {title: "book.series_title", content: series_title},
105
120
  {title: "book.series_position", content: series_position}
106
121
  ]
107
- const usedTags: any[] = []
108
- const ambles = [this.preamble, this.postamble]
122
+ const usedTags: Array<{
123
+ title: string
124
+ content?: unknown
125
+ block: XMLElement
126
+ }> = []
127
+ const ambles = [this.preamble, this.postamble].filter(
128
+ (amble): amble is XMLElement => amble !== null
129
+ )
109
130
  ambles.forEach(amble => {
110
131
  const blocks = amble.queryAll(["text:p", "text:h"])
111
- blocks.forEach((block: any) => {
112
- if (block.parentElement.nodeName === "text:deletion") {
132
+ blocks.forEach(block => {
133
+ if (block.parentElement?.tagName === "text:deletion") {
113
134
  return
114
135
  }
115
136
  const text = block.textContent
@@ -126,21 +147,23 @@ export class ODTBookExporterRender extends ODTExporterRender {
126
147
 
127
148
  assemble(): void {
128
149
  this.styles.addPageBreakStyle()
129
- const text = this.fileXml.query("office:text")
130
- Array.from(this.preamble.children).forEach((node: any) =>
131
- text.appendChild(node)
132
- )
133
- this.bodyParts.forEach((bodyPart: any, index: number) => {
134
- const children = bodyPart.children.slice()
135
- children.forEach((node: any) => {
150
+ const text = this.fileXml!.query("office:text") as XMLElement
151
+ Array.from(this.preamble!.children)
152
+ .filter((node): node is XMLElement => node instanceof XMLElement)
153
+ .forEach(node => text.appendChild(node))
154
+ this.bodyParts.forEach((bodyPart, index) => {
155
+ const children = bodyPart.children
156
+ .slice()
157
+ .filter((node): node is XMLElement => node instanceof XMLElement)
158
+ children.forEach(node => {
136
159
  text.appendChild(node)
137
160
  })
138
161
  if (index < this.bodyParts.length - 1) {
139
162
  text.appendXML('<text:p text:style-name="PageBreak"/>')
140
163
  }
141
164
  })
142
- Array.from(this.postamble.children).forEach((node: any) =>
143
- text.appendChild(node)
144
- )
165
+ Array.from(this.postamble!.children)
166
+ .filter((node): node is XMLElement => node instanceof XMLElement)
167
+ .forEach(node => text.appendChild(node))
145
168
  }
146
169
  }
@@ -54,7 +54,9 @@ export class PrintBookExporter extends HTMLBookExporter {
54
54
  if (!htmlDoc) {
55
55
  return
56
56
  }
57
- const config: any = {title: this.book.title}
57
+ const config: {title: string; printCallback?: (iframeWin: Window) => void} = {
58
+ title: this.book.title
59
+ }
58
60
 
59
61
  if (navigator.userAgent.includes("Gecko/")) {
60
62
  config.printCallback = (iframeWin: Window) => {
@@ -7,11 +7,12 @@
7
7
  * decrypt end-to-end encrypted chapters.
8
8
  */
9
9
 
10
- import type {Schema} from "prosemirror-model"
10
+ import type {Node, Schema} from "prosemirror-model"
11
11
  import {getSettings} from "@fiduswriter/document/schema/convert"
12
12
  import {acceptAllNoInsertions} from "@fiduswriter/document/transform"
13
13
  import {gettext} from "fwtoolkit"
14
14
  import type {ProgressCallback} from "@fiduswriter/document/exporter/tools/progress"
15
+ import type {FidusDoc} from "@fiduswriter/document"
15
16
 
16
17
  import type {
17
18
  ChapterLoader,
@@ -172,13 +173,13 @@ async function decryptE2EEChapters(
172
173
  // Parse ProseMirror content.
173
174
  if (rawContent) {
174
175
  doc.rawContent = JSON.parse(
175
- JSON.stringify(schema.nodeFromJSON(decryptedContent as Record<string, unknown>).toJSON())
176
+ JSON.stringify(schema.nodeFromJSON(decryptedContent as unknown as Record<string, unknown>).toJSON())
176
177
  )
177
178
  }
178
179
  doc.content = acceptAllNoInsertions(
179
- schema.nodeFromJSON(decryptedContent as Record<string, unknown>)
180
+ schema.nodeFromJSON(decryptedContent as unknown as Record<string, unknown>) as Node
180
181
  ).toJSON()
181
- doc.settings = getSettings(doc.content)
182
+ doc.settings = getSettings(doc.content as unknown as FidusDoc)
182
183
 
183
184
  // Decrypt encrypted images.
184
185
  const encryptedImageEntries = Object.entries(doc.images || {}).filter(
@@ -167,7 +167,7 @@ export class NativeBookImporter {
167
167
 
168
168
  const bookData = JSON.parse(
169
169
  textFiles.find(f => f.filename === "book.json")?.content || "{}"
170
- ) as Record<string, any>
170
+ ) as Record<string, unknown>
171
171
 
172
172
  const sortedChapters = [...(bookData.chapters as Array<{chapter_index: number; number: number; part?: string}>)].sort(
173
173
  (a, b) => a.chapter_index - b.chapter_index
@@ -197,9 +197,9 @@ export class NativeBookImporter {
197
197
  throw new Error(`Missing chapter data for index ${ci}`)
198
198
  }
199
199
 
200
- const docJson = JSON.parse(docFile.content) as Record<string, any>
201
- const imagesJson = JSON.parse(imagesFile.content) as Record<string, any>
202
- const bibJson = JSON.parse(bibFile.content) as Record<string, any>
200
+ const docJson = JSON.parse(docFile.content) as Record<string, unknown>
201
+ const imagesJson = JSON.parse(imagesFile.content) as Record<string, unknown>
202
+ const bibJson = JSON.parse(bibFile.content) as Record<string, unknown>
203
203
 
204
204
  const chapterPrefix = `chapters/${ci}/images/`
205
205
  const chapterOtherFiles = binaryFiles
@@ -220,7 +220,7 @@ export class NativeBookImporter {
220
220
  const importer = new NativeImporter(
221
221
  docJson,
222
222
  bibJson,
223
- {db: imagesJson},
223
+ {db: imagesJson as unknown as import("@fiduswriter/document").ImageDB["db"]},
224
224
  chapterOtherFiles,
225
225
  this.user,
226
226
  this.nativeBackend,
@@ -256,7 +256,7 @@ export class NativeBookImporter {
256
256
  part: chapter.part || ""
257
257
  }))
258
258
 
259
- const book = await this.bookBackend.createBook(bookData as any, chapters, false)
259
+ const book = await this.bookBackend.createBook(bookData, chapters, false)
260
260
  this.bookId = book.id || null
261
261
  this.ok = true
262
262
  this.statusText = `"${bookData.title}" ${gettext("successfully imported.")}`