@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.
Files changed (188) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +87 -2
  3. package/css/book.css +219 -0
  4. package/dist/exporter/bits/index.d.ts +42 -0
  5. package/dist/exporter/bits/index.d.ts.map +1 -0
  6. package/dist/exporter/bits/index.js +113 -0
  7. package/dist/exporter/bits/index.js.map +1 -0
  8. package/dist/exporter/bits/templates.d.ts +7 -0
  9. package/dist/exporter/bits/templates.d.ts.map +1 -0
  10. package/dist/exporter/bits/templates.js +78 -0
  11. package/dist/exporter/bits/templates.js.map +1 -0
  12. package/dist/exporter/docx/index.d.ts +33 -0
  13. package/dist/exporter/docx/index.d.ts.map +1 -0
  14. package/dist/exporter/docx/index.js +139 -0
  15. package/dist/exporter/docx/index.js.map +1 -0
  16. package/dist/exporter/docx/render.d.ts +19 -0
  17. package/dist/exporter/docx/render.d.ts.map +1 -0
  18. package/dist/exporter/docx/render.js +123 -0
  19. package/dist/exporter/docx/render.js.map +1 -0
  20. package/dist/exporter/epub/index.d.ts +70 -0
  21. package/dist/exporter/epub/index.d.ts.map +1 -0
  22. package/dist/exporter/epub/index.js +310 -0
  23. package/dist/exporter/epub/index.js.map +1 -0
  24. package/dist/exporter/epub/templates.d.ts +65 -0
  25. package/dist/exporter/epub/templates.d.ts.map +1 -0
  26. package/dist/exporter/epub/templates.js +224 -0
  27. package/dist/exporter/epub/templates.js.map +1 -0
  28. package/dist/exporter/epub/tools.d.ts +5 -0
  29. package/dist/exporter/epub/tools.d.ts.map +1 -0
  30. package/dist/exporter/epub/tools.js +59 -0
  31. package/dist/exporter/epub/tools.js.map +1 -0
  32. package/dist/exporter/html/index.d.ts +70 -0
  33. package/dist/exporter/html/index.d.ts.map +1 -0
  34. package/dist/exporter/html/index.js +320 -0
  35. package/dist/exporter/html/index.js.map +1 -0
  36. package/dist/exporter/html/templates.d.ts +40 -0
  37. package/dist/exporter/html/templates.d.ts.map +1 -0
  38. package/dist/exporter/html/templates.js +225 -0
  39. package/dist/exporter/html/templates.js.map +1 -0
  40. package/dist/exporter/html/tools.d.ts +2 -0
  41. package/dist/exporter/html/tools.d.ts.map +1 -0
  42. package/dist/exporter/html/tools.js +23 -0
  43. package/dist/exporter/html/tools.js.map +1 -0
  44. package/dist/exporter/latex/index.d.ts +36 -0
  45. package/dist/exporter/latex/index.d.ts.map +1 -0
  46. package/dist/exporter/latex/index.js +116 -0
  47. package/dist/exporter/latex/index.js.map +1 -0
  48. package/dist/exporter/latex/templates.d.ts +4 -0
  49. package/dist/exporter/latex/templates.d.ts.map +1 -0
  50. package/dist/exporter/latex/templates.js +26 -0
  51. package/dist/exporter/latex/templates.js.map +1 -0
  52. package/dist/exporter/native/index.d.ts +22 -0
  53. package/dist/exporter/native/index.d.ts.map +1 -0
  54. package/dist/exporter/native/index.js +141 -0
  55. package/dist/exporter/native/index.js.map +1 -0
  56. package/dist/exporter/odt/index.d.ts +33 -0
  57. package/dist/exporter/odt/index.d.ts.map +1 -0
  58. package/dist/exporter/odt/index.js +133 -0
  59. package/dist/exporter/odt/index.js.map +1 -0
  60. package/dist/exporter/odt/render.d.ts +21 -0
  61. package/dist/exporter/odt/render.d.ts.map +1 -0
  62. package/dist/exporter/odt/render.js +126 -0
  63. package/dist/exporter/odt/render.js.map +1 -0
  64. package/dist/exporter/pretty.d.ts +17 -0
  65. package/dist/exporter/pretty.d.ts.map +1 -0
  66. package/dist/exporter/pretty.js +55 -0
  67. package/dist/exporter/pretty.js.map +1 -0
  68. package/dist/exporter/print/index.d.ts +25 -0
  69. package/dist/exporter/print/index.d.ts.map +1 -0
  70. package/dist/exporter/print/index.js +46 -0
  71. package/dist/exporter/print/index.js.map +1 -0
  72. package/dist/exporter/print/templates.d.ts +2 -0
  73. package/dist/exporter/print/templates.d.ts.map +1 -0
  74. package/dist/exporter/print/templates.js +5 -0
  75. package/dist/exporter/print/templates.js.map +1 -0
  76. package/dist/exporter/tools.d.ts +31 -0
  77. package/dist/exporter/tools.d.ts.map +1 -0
  78. package/dist/exporter/tools.js +177 -0
  79. package/dist/exporter/tools.js.map +1 -0
  80. package/dist/i18n.d.ts +7 -0
  81. package/dist/i18n.d.ts.map +1 -0
  82. package/dist/i18n.js +918 -0
  83. package/dist/i18n.js.map +1 -0
  84. package/dist/ids.d.ts +19 -0
  85. package/dist/ids.d.ts.map +1 -0
  86. package/dist/ids.js +27 -0
  87. package/dist/ids.js.map +1 -0
  88. package/dist/importer/native/index.d.ts +45 -0
  89. package/dist/importer/native/index.d.ts.map +1 -0
  90. package/dist/importer/native/index.js +166 -0
  91. package/dist/importer/native/index.js.map +1 -0
  92. package/dist/importer/native/reader.d.ts +32 -0
  93. package/dist/importer/native/reader.d.ts.map +1 -0
  94. package/dist/importer/native/reader.js +170 -0
  95. package/dist/importer/native/reader.js.map +1 -0
  96. package/dist/index.d.ts +4 -0
  97. package/dist/index.d.ts.map +1 -0
  98. package/dist/index.js +3 -0
  99. package/dist/index.js.map +1 -0
  100. package/dist/schema/index.d.ts +7 -0
  101. package/dist/schema/index.d.ts.map +1 -0
  102. package/dist/schema/index.js +8 -0
  103. package/dist/schema/index.js.map +1 -0
  104. package/dist/types.d.ts +171 -0
  105. package/dist/types.d.ts.map +1 -0
  106. package/dist/types.js +5 -0
  107. package/dist/types.js.map +1 -0
  108. package/dist/ui/book_dialog/dialog.d.ts +161 -0
  109. package/dist/ui/book_dialog/dialog.d.ts.map +1 -0
  110. package/dist/ui/book_dialog/dialog.js +750 -0
  111. package/dist/ui/book_dialog/dialog.js.map +1 -0
  112. package/dist/ui/book_dialog/export.d.ts +95 -0
  113. package/dist/ui/book_dialog/export.d.ts.map +1 -0
  114. package/dist/ui/book_dialog/export.js +217 -0
  115. package/dist/ui/book_dialog/export.js.map +1 -0
  116. package/dist/ui/book_dialog/index.d.ts +38 -0
  117. package/dist/ui/book_dialog/index.d.ts.map +1 -0
  118. package/dist/ui/book_dialog/index.js +36 -0
  119. package/dist/ui/book_dialog/index.js.map +1 -0
  120. package/dist/ui/book_dialog/sanity_check.d.ts +22 -0
  121. package/dist/ui/book_dialog/sanity_check.d.ts.map +1 -0
  122. package/dist/ui/book_dialog/sanity_check.js +109 -0
  123. package/dist/ui/book_dialog/sanity_check.js.map +1 -0
  124. package/dist/ui/book_dialog/templates.d.ts +76 -0
  125. package/dist/ui/book_dialog/templates.d.ts.map +1 -0
  126. package/dist/ui/book_dialog/templates.js +542 -0
  127. package/dist/ui/book_dialog/templates.js.map +1 -0
  128. package/dist/ui/book_dialog/types.d.ts +258 -0
  129. package/dist/ui/book_dialog/types.d.ts.map +1 -0
  130. package/dist/ui/book_dialog/types.js +15 -0
  131. package/dist/ui/book_dialog/types.js.map +1 -0
  132. package/locale/ar/messages.json +49 -0
  133. package/locale/bg/messages.json +49 -0
  134. package/locale/cs/messages.json +49 -0
  135. package/locale/da/messages.json +49 -0
  136. package/locale/de/messages.json +49 -0
  137. package/locale/en/messages.json +49 -0
  138. package/locale/en_US/messages.json +49 -0
  139. package/locale/es/messages.json +49 -0
  140. package/locale/fr/messages.json +49 -0
  141. package/locale/it/messages.json +49 -0
  142. package/locale/ja/messages.json +49 -0
  143. package/locale/ko/messages.json +49 -0
  144. package/locale/nb/messages.json +49 -0
  145. package/locale/nl/messages.json +49 -0
  146. package/locale/pl/messages.json +49 -0
  147. package/locale/pt_BR/messages.json +49 -0
  148. package/locale/pt_PT/messages.json +49 -0
  149. package/locale/ru/messages.json +49 -0
  150. package/locale/sv/messages.json +49 -0
  151. package/locale/tr/messages.json +49 -0
  152. package/locale/zh_Hans/messages.json +49 -0
  153. package/logo.svg +10 -0
  154. package/package.json +150 -5
  155. package/src/exporter/bits/index.ts +184 -0
  156. package/src/exporter/bits/templates.ts +107 -0
  157. package/src/exporter/docx/index.ts +258 -0
  158. package/src/exporter/docx/render.ts +177 -0
  159. package/src/exporter/epub/index.ts +496 -0
  160. package/src/exporter/epub/templates.ts +403 -0
  161. package/src/exporter/epub/tools.ts +68 -0
  162. package/src/exporter/html/index.ts +479 -0
  163. package/src/exporter/html/templates.ts +362 -0
  164. package/src/exporter/html/tools.ts +26 -0
  165. package/src/exporter/latex/index.ts +190 -0
  166. package/src/exporter/latex/templates.ts +35 -0
  167. package/src/exporter/native/index.ts +238 -0
  168. package/src/exporter/odt/index.ts +230 -0
  169. package/src/exporter/odt/render.ts +181 -0
  170. package/src/exporter/pretty.ts +75 -0
  171. package/src/exporter/print/index.ts +76 -0
  172. package/src/exporter/print/templates.ts +8 -0
  173. package/src/exporter/tools.ts +270 -0
  174. package/src/global.d.ts +2 -0
  175. package/src/i18n.ts +936 -0
  176. package/src/ids.ts +27 -0
  177. package/src/importer/native/index.ts +269 -0
  178. package/src/importer/native/reader.ts +247 -0
  179. package/src/index.ts +20 -0
  180. package/src/modules.d.ts +7 -0
  181. package/src/schema/index.ts +8 -0
  182. package/src/types.ts +208 -0
  183. package/src/ui/book_dialog/dialog.ts +940 -0
  184. package/src/ui/book_dialog/export.ts +449 -0
  185. package/src/ui/book_dialog/index.ts +84 -0
  186. package/src/ui/book_dialog/sanity_check.ts +208 -0
  187. package/src/ui/book_dialog/templates.ts +736 -0
  188. package/src/ui/book_dialog/types.ts +283 -0
@@ -0,0 +1,750 @@
1
+ /**
2
+ * The book edit dialog.
3
+ *
4
+ * A tabbed dialog that edits a book's metadata and settings, manages its
5
+ * chapter list (add/remove/reorder, part grouping and per-chapter part
6
+ * titles), picks the cover image, triggers a sanity check and starts an
7
+ * export.
8
+ *
9
+ * Everything host-specific is injected: see `./types.js`. The book *overview*
10
+ * page -- its datatable, bulk actions, sharing, copy and delete -- is not
11
+ * part of this component and stays with the host.
12
+ */
13
+ import { ContentMenu, Dialog, FileSelector, addAlert, findTarget, gettext } from "fwtoolkit";
14
+ import { bookExportMenuModel } from "./export.js";
15
+ import { bookSanityCheck } from "./sanity_check.js";
16
+ import { bookBasicInfoTemplate, bookBibliographyDataTemplate, bookChapterDialogTemplate, bookChapterListTemplate, bookDOCXDataRowTemplate, bookDOCXDataTemplate, bookDialogChaptersTemplate, bookDialogTemplate, bookEpubDataCoverTemplate, bookEpubDataTemplate, bookODTDataRowTemplate, bookODTDataTemplate, bookPrintDataTemplate, bookSanityCheckTemplate } from "./templates.js";
17
+ /** The metadata a freshly created book starts out with. */
18
+ function emptyMetadata() {
19
+ return {
20
+ author: "",
21
+ subtitle: "",
22
+ version: "",
23
+ publisher: "",
24
+ copyright: "",
25
+ keywords: "",
26
+ description: "",
27
+ isbn: "",
28
+ publication_date: "",
29
+ series_title: "",
30
+ series_position: ""
31
+ };
32
+ }
33
+ /**
34
+ * Read a value out of one of the book dialog's form fields. The dialog markup
35
+ * comes from ./templates.js, so the ids are fixed.
36
+ */
37
+ function fieldValue(root, id) {
38
+ return root.querySelector(`#${id}`).value;
39
+ }
40
+ /** The tabbed book editor. */
41
+ export class BookDialog {
42
+ options;
43
+ /** Whether this dialog creates a new book or edits an existing one. */
44
+ isNew;
45
+ /** The book being edited. A working copy, not the host's own record. */
46
+ book;
47
+ documentList;
48
+ exportMenu;
49
+ /** Callbacks invoked after the book has been saved. */
50
+ onSave;
51
+ dialogParts;
52
+ /** Cover images that are not part of the host's image database. */
53
+ bookImageDB;
54
+ dialog;
55
+ /** The chapters tab's file selector, once the dialog is open. */
56
+ fileSelector;
57
+ constructor(options) {
58
+ this.options = options;
59
+ this.documentList = options.documentList;
60
+ this.exportMenu = bookExportMenuModel();
61
+ this.onSave = options.onSave ? [...options.onSave] : [];
62
+ this.dialog = null;
63
+ this.fileSelector = null;
64
+ this.isNew = !options.book;
65
+ this.book = this.isNew
66
+ ? this.createEmptyBook()
67
+ : this.workingCopy(options.book);
68
+ this.bookImageDB = { db: {} };
69
+ if (this.book.cover_image && !this.imageDB().db[this.book.cover_image]) {
70
+ // The cover image is not in the current user's image DB -- it was
71
+ // either deleted or another user originally added it. As we don't
72
+ // do anything fancy with it, we simply add the current cover image
73
+ // to the DB locally so that image selection works as expected.
74
+ this.bookImageDB.db[this.book.cover_image] =
75
+ this.book.cover_image_data ?? {};
76
+ }
77
+ this.dialogParts = [
78
+ {
79
+ title: gettext("Basic info"),
80
+ description: gettext("Basic book information"),
81
+ template: bookBasicInfoTemplate
82
+ },
83
+ {
84
+ title: gettext("Chapters"),
85
+ description: gettext("Documents assigned as chapters"),
86
+ template: bookDialogChaptersTemplate
87
+ },
88
+ {
89
+ title: gettext("Bibliography"),
90
+ description: gettext("Bibliography related settings"),
91
+ template: bookBibliographyDataTemplate
92
+ },
93
+ {
94
+ title: gettext("Epub"),
95
+ description: gettext("Epub related settings"),
96
+ template: bookEpubDataTemplate
97
+ },
98
+ ...(this.options.backend.saveDocxTemplate
99
+ ? [
100
+ {
101
+ title: gettext("DOCX"),
102
+ description: gettext("DOCX related settings"),
103
+ template: bookDOCXDataTemplate
104
+ }
105
+ ]
106
+ : []),
107
+ ...(this.options.backend.saveOdtTemplate
108
+ ? [
109
+ {
110
+ title: gettext("ODT"),
111
+ description: gettext("ODT related settings"),
112
+ template: bookODTDataTemplate
113
+ }
114
+ ]
115
+ : []),
116
+ {
117
+ title: gettext("Print/PDF"),
118
+ description: gettext("Print related settings"),
119
+ template: bookPrintDataTemplate
120
+ },
121
+ {
122
+ title: gettext("Sanity check"),
123
+ description: gettext("Perform sanity check on book"),
124
+ template: bookSanityCheckTemplate
125
+ }
126
+ ];
127
+ }
128
+ /** The image database the dialog renders cover previews from. */
129
+ imageDB() {
130
+ return {
131
+ db: Object.assign({}, this.options.imageDB?.db ?? {}, this.bookImageDB.db)
132
+ };
133
+ }
134
+ /** The dialog's root element, or the document before it is opened. */
135
+ root() {
136
+ return this.dialog?.dialogEl ?? document;
137
+ }
138
+ /** The bundle the tab templates are rendered from. */
139
+ bookInfo() {
140
+ return {
141
+ book: this.book,
142
+ documentList: this.documentList,
143
+ citationStyles: this.options.citationStyles ?? {},
144
+ bookStyleList: this.options.exportContext?.bookStyles ?? [],
145
+ imageDB: this.imageDB(),
146
+ canPickChapters: Boolean(this.options.backend.pickChapters),
147
+ canOpenChapters: Boolean(this.options.backend.openChapter),
148
+ chapterCatalog: this.options.chapterCatalog !== false
149
+ };
150
+ }
151
+ /** A brand new book with the dialog's default metadata and settings. */
152
+ createEmptyBook() {
153
+ const { path = "/", exportContext } = this.options;
154
+ return {
155
+ title: "",
156
+ id: 0,
157
+ chapters: [],
158
+ added: 0,
159
+ updated: 0,
160
+ path,
161
+ metadata: emptyMetadata(),
162
+ settings: {
163
+ bibliography_header: gettext("Bibliography"),
164
+ citationstyle: "apa",
165
+ book_style: exportContext?.bookStyles[0]
166
+ ? exportContext.bookStyles[0].slug
167
+ : "",
168
+ papersize: "octavo",
169
+ language: "en-US"
170
+ }
171
+ };
172
+ }
173
+ /**
174
+ * The dialog's working copy of an existing book.
175
+ *
176
+ * The record is copied shallowly -- the `chapters` array stays shared with
177
+ * the host's record, exactly as the Django backend's dialog behaved -- and
178
+ * the metadata block is merged onto a complete one so every form field
179
+ * has something to read back.
180
+ */
181
+ workingCopy(book) {
182
+ return Object.assign({}, book, {
183
+ metadata: Object.assign(emptyMetadata(), book.metadata)
184
+ });
185
+ }
186
+ /**
187
+ * The dialog's buttons, from the host's `buttons` option or the classic
188
+ * default row (Export dropdown, Save, Cancel -- or Close for a read-only
189
+ * book).
190
+ */
191
+ buildButtons() {
192
+ const specs = this.options.buttons ?? this.defaultButtons();
193
+ return specs.map(spec => {
194
+ if (spec.type) {
195
+ return { type: spec.type };
196
+ }
197
+ return {
198
+ text: spec.text,
199
+ classes: spec.classes ?? "fw-dark",
200
+ icon: spec.icon,
201
+ dropdown: spec.dropdown,
202
+ click: (event) => this.runButton(spec, event)
203
+ };
204
+ });
205
+ }
206
+ /** The classic button row, used when the host supplies none. */
207
+ defaultButtons() {
208
+ if (this.readOnly()) {
209
+ return [{ type: "close" }];
210
+ }
211
+ const specs = [];
212
+ if (this.options.exportContext) {
213
+ specs.push({
214
+ text: gettext("Export"),
215
+ dropdown: true,
216
+ keepOpen: true,
217
+ click: (dialog, event) => dialog.openExportMenu(event)
218
+ });
219
+ }
220
+ specs.push({ text: gettext("Save"), click: dialog => dialog.saveBook() });
221
+ specs.push({ type: "cancel" });
222
+ return specs;
223
+ }
224
+ /** Run a button's action, closing the dialog unless it wants to stay. */
225
+ runButton(spec, event) {
226
+ const result = spec.click?.(this, event);
227
+ const close = () => {
228
+ if (!spec.keepOpen) {
229
+ this.dialog?.close();
230
+ }
231
+ };
232
+ if (result instanceof Promise) {
233
+ return result.then(close);
234
+ }
235
+ close();
236
+ return result;
237
+ }
238
+ /**
239
+ * Open the export dropdown menu at the position of the triggering event.
240
+ *
241
+ * Exposed for hosts that build their own button row: the Export button
242
+ * is just a dropdown-styled button whose click calls this. Does nothing
243
+ * when no export context was given.
244
+ */
245
+ openExportMenu(event) {
246
+ if (!this.options.exportContext) {
247
+ return undefined;
248
+ }
249
+ const mouseEvent = event;
250
+ const contentMenu = new ContentMenu({
251
+ page: {
252
+ saveBook: (exportId) => this.saveBook(exportId),
253
+ book: this.book,
254
+ documentList: this.documentList,
255
+ context: this.options.exportContext
256
+ },
257
+ menu: this.exportMenu,
258
+ menuPos: { X: mouseEvent.pageX, Y: mouseEvent.pageY },
259
+ width: 250
260
+ });
261
+ return contentMenu.open();
262
+ }
263
+ /** Render and open the dialog. */
264
+ open() {
265
+ const title = this.isNew ? gettext("Create Book") : gettext("Edit Book");
266
+ const body = bookDialogTemplate({
267
+ title,
268
+ dialogParts: this.dialogParts,
269
+ bookInfo: this.bookInfo()
270
+ });
271
+ const dialog = new Dialog({
272
+ width: 840,
273
+ height: 520,
274
+ title,
275
+ body,
276
+ buttons: this.buildButtons(),
277
+ fullScreen: this.options.fullScreen,
278
+ canClose: this.options.canClose,
279
+ canEscape: this.options.canEscape,
280
+ ...(this.options.onClose ? { onClose: this.options.onClose } : {})
281
+ });
282
+ dialog.open();
283
+ this.dialog = dialog;
284
+ dialog.dialogEl
285
+ .querySelectorAll("#bookoptions-tab .tab-content")
286
+ .forEach((el, index) => {
287
+ if (index) {
288
+ el.style.display = "none";
289
+ }
290
+ });
291
+ if (!this.readOnly()) {
292
+ this.refreshFileSelector();
293
+ this.bindTemplateInput(dialog, "input-docx-template", "docx-template-row", (book, template) => {
294
+ book.docx_template = template;
295
+ }, this.options.backend.saveDocxTemplate?.bind(this.options.backend));
296
+ this.bindTemplateInput(dialog, "input-odt-template", "odt-template-row", (book, template) => {
297
+ book.odt_template = template;
298
+ }, this.options.backend.saveOdtTemplate?.bind(this.options.backend));
299
+ }
300
+ // Handle tab link clicking
301
+ dialog.dialogEl
302
+ .querySelectorAll("#bookoptions-tab .fw-tab-link a")
303
+ .forEach(el => {
304
+ const tab = el.parentElement;
305
+ const tabList = tab.parentElement;
306
+ el.addEventListener("click", event => {
307
+ event.preventDefault();
308
+ tabList
309
+ .querySelectorAll(".fw-tab-link.fw-current-tab")
310
+ .forEach(other => other.classList.remove("fw-current-tab"));
311
+ tab.classList.add("fw-current-tab");
312
+ const link = el.getAttribute("href");
313
+ dialog.dialogEl
314
+ .querySelectorAll("#bookoptions-tab .tab-content")
315
+ .forEach(panel => {
316
+ if (panel.matches(link)) {
317
+ panel.style.display = "";
318
+ }
319
+ else {
320
+ panel.style.display = "none";
321
+ }
322
+ });
323
+ });
324
+ });
325
+ dialog.dialogEl.addEventListener("click", event => this.handleDialogClick(event));
326
+ const citationStyleSelect = dialog.dialogEl.querySelector("#book-settings-citationstyle");
327
+ if (citationStyleSelect) {
328
+ citationStyleSelect.addEventListener("change", event => {
329
+ this.book.settings.citationstyle = event.target.value;
330
+ });
331
+ }
332
+ const bookStyleSelect = dialog.dialogEl.querySelector("#book-settings-bookstyle");
333
+ if (bookStyleSelect) {
334
+ bookStyleSelect.addEventListener("change", event => {
335
+ this.book.settings.book_style = event.target.value;
336
+ });
337
+ }
338
+ const paperSizeSelect = dialog.dialogEl.querySelector("#book-settings-papersize");
339
+ if (paperSizeSelect) {
340
+ paperSizeSelect.addEventListener("change", event => {
341
+ this.book.settings.papersize = event.target.value;
342
+ });
343
+ }
344
+ }
345
+ /** Whether the book is only readable, i.e. not editable. */
346
+ readOnly() {
347
+ return this.book.rights === "read";
348
+ }
349
+ /**
350
+ * Let the user pick a DOCX/ODT template file and hand it to the host.
351
+ *
352
+ * The book is saved first, because a freshly created book has no id the
353
+ * host could attach a template to.
354
+ */
355
+ bindTemplateInput(dialog, inputId, rowId, applyTemplate, saveTemplate) {
356
+ const input = dialog.dialogEl.querySelector(`#${inputId}`);
357
+ if (!input || !saveTemplate) {
358
+ return;
359
+ }
360
+ input.addEventListener("change", event => {
361
+ const file = event.target.files[0];
362
+ return this.saveBook()
363
+ .then(() => saveTemplate(this.book, file))
364
+ .then(template => {
365
+ if (!template) {
366
+ return;
367
+ }
368
+ applyTemplate(this.book, template);
369
+ this.refreshRow(rowId, rowId === "docx-template-row"
370
+ ? bookDOCXDataRowTemplate({ book: this.book })
371
+ : bookODTDataRowTemplate({ book: this.book }));
372
+ });
373
+ });
374
+ }
375
+ /** Replace the markup of one of the dialog's rows. */
376
+ refreshRow(rowId, html) {
377
+ const row = this.root().querySelector(`#${rowId}`);
378
+ if (row) {
379
+ row.innerHTML = html;
380
+ }
381
+ }
382
+ /**
383
+ * (Re-)build the chapters tab's file selector from the document list.
384
+ *
385
+ * Hosts can extend the list after the dialog opened -- the desktop adds
386
+ * the files the user picked through its native file dialog -- so the
387
+ * selector is rebuilt rather than created only in `open()`.
388
+ */
389
+ refreshFileSelector() {
390
+ if (this.readOnly() || !this.dialog) {
391
+ return;
392
+ }
393
+ const dom = this.dialog.dialogEl.querySelector("#book-document-list");
394
+ if (!dom) {
395
+ return;
396
+ }
397
+ this.fileSelector = new FileSelector({
398
+ dom,
399
+ files: this.documentList,
400
+ multiSelect: true,
401
+ selectFolders: false
402
+ });
403
+ this.fileSelector.init();
404
+ }
405
+ /**
406
+ * Replace the document list the dialog's catalog and chapter rows read
407
+ * from, and refresh the affected parts of the dialog.
408
+ */
409
+ setDocumentList(documentList) {
410
+ this.documentList = documentList;
411
+ this.refreshFileSelector();
412
+ this.refreshChapterList();
413
+ this.updateChapterNotice();
414
+ }
415
+ /** Re-render the chapter list. */
416
+ refreshChapterList() {
417
+ this.refreshRow("book-chapter-list", bookChapterListTemplate({
418
+ book: this.book,
419
+ documentList: this.documentList
420
+ }));
421
+ }
422
+ /**
423
+ * Add or remove the encrypted-chapters notice that lives alongside the
424
+ * chapter list. Called after any operation that changes book.chapters so
425
+ * the notice stays in sync.
426
+ */
427
+ updateChapterNotice() {
428
+ const chapterListEl = this.root().querySelector("#book-chapter-list");
429
+ if (!chapterListEl) {
430
+ return;
431
+ }
432
+ const container = chapterListEl.closest(".fw-ar-container");
433
+ if (!container) {
434
+ return;
435
+ }
436
+ const existingNotice = container.querySelector(".e2ee-chapter-notice");
437
+ const hasEncrypted = this.book.chapters.some(ch => this.documentList.find(doc => doc.id === ch.text)?.e2ee);
438
+ if (hasEncrypted && !existingNotice) {
439
+ container.insertAdjacentHTML("beforeend", `<p class="fw-note e2ee-chapter-notice">
440
+ <i class="fas fa-lock"></i>
441
+ ${gettext("This book contains encrypted chapters. A personal passphrase is required to export or run a sanity check on this book.")}
442
+ </p>`);
443
+ }
444
+ else if (!hasEncrypted && existingNotice) {
445
+ existingNotice.remove();
446
+ }
447
+ }
448
+ /** Open a sub-dialog to edit a chapter's part title. */
449
+ editChapterDialog(chapter) {
450
+ const doc = this.documentList.find(doc => doc.id === chapter.text);
451
+ let docTitle = doc?.title ?? "";
452
+ if (!docTitle.length) {
453
+ docTitle = gettext("Untitled");
454
+ }
455
+ const buttons = [
456
+ {
457
+ type: "cancel"
458
+ },
459
+ {
460
+ text: gettext("Submit"),
461
+ classes: "fw-dark",
462
+ click: () => {
463
+ // The part-title field lives in this sub-dialog, not in
464
+ // the book dialog's own root.
465
+ chapter.part = fieldValue(dialog.dialogEl, "book-chapter-part");
466
+ this.refreshChapterList();
467
+ dialog.close();
468
+ }
469
+ }
470
+ ];
471
+ const dialog = new Dialog({
472
+ title: `${gettext("Edit Chapter")}: ${chapter.number}. ${docTitle}`,
473
+ body: bookChapterDialogTemplate({ chapter }),
474
+ width: 300,
475
+ height: 100,
476
+ buttons
477
+ });
478
+ dialog.open();
479
+ }
480
+ /** Let the user pick the cover image through the host's image picker. */
481
+ selectCoverImage() {
482
+ return Promise.resolve(this.options.backend.selectCoverImage?.(this.book.cover_image ?? false)).then(selected => {
483
+ if (!selected) {
484
+ delete this.book.cover_image;
485
+ }
486
+ else {
487
+ this.book.cover_image = selected.id;
488
+ this.book.cover_image_data = selected.data;
489
+ this.bookImageDB.db[selected.id] = selected.data;
490
+ }
491
+ this.refreshRow("cover-preview-row", bookEpubDataCoverTemplate({
492
+ book: this.book,
493
+ imageDB: this.imageDB()
494
+ }));
495
+ });
496
+ }
497
+ /**
498
+ * Read the form fields into the book record without persisting.
499
+ *
500
+ * Chapter and part edits live in `this.book` directly, but the metadata
501
+ * and settings fields are only read back on save. Hosts that write the
502
+ * book from outside the dialog -- a window-close flush, a Save-as in the
503
+ * host UI -- call this first so the edited state is complete. A no-op
504
+ * when the dialog is closed or the book is read-only.
505
+ *
506
+ * @returns The dialog's book record with the form fields applied.
507
+ */
508
+ syncEdits() {
509
+ const book = this.book;
510
+ if (this.readOnly() || !this.dialog) {
511
+ return book;
512
+ }
513
+ const root = this.root();
514
+ book.title = fieldValue(root, "book-title");
515
+ book.metadata.author = fieldValue(root, "book-metadata-author");
516
+ book.metadata.subtitle = fieldValue(root, "book-metadata-subtitle");
517
+ book.metadata.version = fieldValue(root, "book-metadata-version");
518
+ book.metadata.copyright = fieldValue(root, "book-metadata-copyright");
519
+ book.metadata.publisher = fieldValue(root, "book-metadata-publisher");
520
+ book.metadata.keywords = fieldValue(root, "book-metadata-keywords");
521
+ book.metadata.description = fieldValue(root, "book-metadata-description");
522
+ book.metadata.isbn = fieldValue(root, "book-metadata-isbn");
523
+ book.metadata.publication_date = fieldValue(root, "book-metadata-publication-date");
524
+ book.metadata.series_title = fieldValue(root, "book-metadata-series-title");
525
+ book.metadata.series_position = fieldValue(root, "book-metadata-series-position");
526
+ book.settings.language = fieldValue(root, "book-settings-language");
527
+ book.path = (this.isNew ? "" : (book.path ?? "")) || this.options.path || "";
528
+ return book;
529
+ }
530
+ /**
531
+ * Persist the edited book through the host and notify the save callbacks.
532
+ *
533
+ * Must only be called while the dialog is open.
534
+ */
535
+ saveBook(exportId) {
536
+ const book = this.book;
537
+ if (this.readOnly()) {
538
+ return Promise.resolve();
539
+ }
540
+ this.syncEdits();
541
+ return this.options.backend
542
+ .saveBook(book, exportId)
543
+ .catch((error) => {
544
+ // Include the cause: a bare "could not be saved" leaves hosts
545
+ // (and users) with no way to tell a cancelled picker from a
546
+ // failed write.
547
+ addAlert("error", `${gettext("The book could not be saved")}: ${error instanceof Error ? error.message : String(error)}`);
548
+ throw error;
549
+ })
550
+ .then(async (saved) => {
551
+ if (!saved) {
552
+ // The host handed the session to a new window (an
553
+ // untitled book promoted to its real path) and resumes
554
+ // the triggering export there; nothing to update here.
555
+ return false;
556
+ }
557
+ this.book = Object.assign(book, {
558
+ id: saved.id ?? book.id,
559
+ added: saved.added ?? book.added,
560
+ updated: saved.updated ?? book.updated
561
+ });
562
+ await Promise.all(this.onSave.map(method => method(this.book)));
563
+ return true;
564
+ });
565
+ }
566
+ /**
567
+ * Move a chapter one position up or down the list.
568
+ *
569
+ * @param docId - Id of the chapter document to move.
570
+ * @param offset - `-1` to move up, `1` to move down.
571
+ */
572
+ moveChapter(docId, offset) {
573
+ const chapter = this.book.chapters.find(chapter => chapter.text === docId);
574
+ const neighbour = this.book.chapters.find(other => other.number === chapter.number + offset);
575
+ if (!neighbour) {
576
+ return;
577
+ }
578
+ chapter.number += offset;
579
+ neighbour.number -= offset;
580
+ this.refreshChapterList();
581
+ this.updateChapterNotice();
582
+ }
583
+ /** Remove a chapter from the book and close the numbering gap. */
584
+ removeChapter(docId) {
585
+ const chapter = this.book.chapters.find(chapter => chapter.text === docId);
586
+ if (!chapter) {
587
+ return;
588
+ }
589
+ this.book.chapters.forEach(other => {
590
+ if (other.number > chapter.number) {
591
+ other.number--;
592
+ }
593
+ });
594
+ this.book.chapters = this.book.chapters.filter(other => other !== chapter);
595
+ this.refreshChapterList();
596
+ this.updateChapterNotice();
597
+ }
598
+ /**
599
+ * Add a document as the book's next chapter.
600
+ *
601
+ * External documents (standalone files the host picked) carry their file
602
+ * location onto the chapter, so file-based hosts can re-resolve the live
603
+ * document on the next load.
604
+ */
605
+ addChapter(doc) {
606
+ if (this.book.chapters.some(chapter => chapter.text === doc.id)) {
607
+ return;
608
+ }
609
+ const chapNums = this.book.chapters.map(chapter => chapter.number);
610
+ const number = chapNums.length ? Math.max(...chapNums) + 1 : 1;
611
+ this.book.chapters.push({
612
+ text: doc.id,
613
+ number,
614
+ part: "",
615
+ ...(doc.external && doc.path ? { path: doc.path } : {})
616
+ });
617
+ }
618
+ /** Append the documents selected in the file selector as new chapters. */
619
+ addChapters(fileSelector) {
620
+ fileSelector.selected.forEach(entry => {
621
+ if (entry.type !== "file") {
622
+ return;
623
+ }
624
+ this.addChapter(entry.file);
625
+ });
626
+ fileSelector.deselectAll();
627
+ this.refreshChapterList();
628
+ this.updateChapterNotice();
629
+ }
630
+ /**
631
+ * Let the user pick standalone documents through the host and add them as
632
+ * chapters.
633
+ *
634
+ * The host returns stub entries for the picked files; the dialog merges
635
+ * them into the catalog so they show up in the file selector, and adds
636
+ * each as a chapter. The chapter content itself is resolved by the host
637
+ * when the book is saved or exported.
638
+ */
639
+ addPickedChapters() {
640
+ if (!this.options.backend.pickChapters) {
641
+ return Promise.resolve();
642
+ }
643
+ return this.options.backend.pickChapters().then(picked => {
644
+ if (!picked.length) {
645
+ return;
646
+ }
647
+ picked.forEach(doc => {
648
+ if (!this.documentList.some(existing => existing.id === doc.id)) {
649
+ this.documentList.push(doc);
650
+ }
651
+ this.addChapter(doc);
652
+ });
653
+ this.refreshFileSelector();
654
+ this.refreshChapterList();
655
+ this.updateChapterNotice();
656
+ });
657
+ }
658
+ /** Handle a click anywhere inside the dialog. */
659
+ handleDialogClick(event) {
660
+ const el = {};
661
+ // `findTarget` fills `el.target` with the matching ancestor; all the
662
+ // chapter/template rows carry their document id in a data attribute.
663
+ const clickedId = () => Number.parseInt(el.target.dataset.id);
664
+ switch (true) {
665
+ case findTarget(event, ".book-sort-up", el):
666
+ this.moveChapter(clickedId(), -1);
667
+ break;
668
+ case findTarget(event, ".book-sort-down", el):
669
+ this.moveChapter(clickedId(), 1);
670
+ break;
671
+ case findTarget(event, ".delete-chapter", el):
672
+ this.removeChapter(clickedId());
673
+ break;
674
+ case findTarget(event, "#add-chapter", el):
675
+ if (this.fileSelector) {
676
+ this.addChapters(this.fileSelector);
677
+ }
678
+ break;
679
+ case findTarget(event, "#add-chapter-file", el):
680
+ void this.addPickedChapters();
681
+ break;
682
+ case findTarget(event, ".open-chapter", el): {
683
+ const chapter = this.book.chapters.find(candidate => candidate.text === clickedId());
684
+ if (chapter) {
685
+ this.options.backend.openChapter?.(chapter);
686
+ }
687
+ break;
688
+ }
689
+ case findTarget(event, ".edit-chapter", el):
690
+ this.editChapterDialog(this.book.chapters.find(chapter => chapter.text === clickedId()));
691
+ break;
692
+ case findTarget(event, "#select-cover-image-button", el):
693
+ this.selectCoverImage();
694
+ break;
695
+ case findTarget(event, "#select-docx-template", el): {
696
+ const input = this.root().querySelector("#input-docx-template");
697
+ input.click();
698
+ break;
699
+ }
700
+ case findTarget(event, "#select-odt-template", el): {
701
+ const input = this.root().querySelector("#input-odt-template");
702
+ input.click();
703
+ break;
704
+ }
705
+ case findTarget(event, "#remove-cover-image-button", el): {
706
+ delete this.book.cover_image;
707
+ this.refreshRow("cover-preview-row", bookEpubDataCoverTemplate({
708
+ book: this.book,
709
+ // We just deleted the cover image, so we don't need a
710
+ // full DB.
711
+ imageDB: { db: {} }
712
+ }));
713
+ break;
714
+ }
715
+ case findTarget(event, "#remove-docx-template-button", el): {
716
+ delete this.book.docx_template;
717
+ this.refreshRow("docx-template-row", bookDOCXDataRowTemplate({ book: this.book }));
718
+ break;
719
+ }
720
+ case findTarget(event, "#remove-odt-template-button", el): {
721
+ delete this.book.odt_template;
722
+ this.refreshRow("odt-template-row", bookODTDataRowTemplate({ book: this.book }));
723
+ break;
724
+ }
725
+ case findTarget(event, "#perform-sanity-check-button", el): {
726
+ this.performSanityCheck();
727
+ break;
728
+ }
729
+ default:
730
+ break;
731
+ }
732
+ }
733
+ /** Save the book and render its sanity check report in place. */
734
+ performSanityCheck() {
735
+ const { schema, chapterLoader, e2ee } = this.options;
736
+ return this.saveBook()
737
+ .then(() => bookSanityCheck(this.book, this.documentList, {
738
+ schema,
739
+ chapterLoader,
740
+ e2ee
741
+ }))
742
+ .then(sanityCheckOutputHTML => {
743
+ const sanityCheckOutput = this.root().querySelector("#sanity-check-output");
744
+ if (sanityCheckOutput) {
745
+ sanityCheckOutput.innerHTML = sanityCheckOutputHTML;
746
+ }
747
+ });
748
+ }
749
+ }
750
+ //# sourceMappingURL=dialog.js.map