bookish-press 0.4.45 → 0.4.47

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 (533) hide show
  1. package/.env.example +8 -0
  2. package/.firebaserc +8 -0
  3. package/.prettierignore +8 -0
  4. package/.prettierrc.json +5 -0
  5. package/_update-notifier-last-checked +0 -0
  6. package/firebase.json +46 -0
  7. package/firestore.indexes.json +4 -0
  8. package/firestore.rules +59 -0
  9. package/functions/.eslintrc.js +28 -0
  10. package/functions/package-lock.json +12595 -0
  11. package/functions/package.json +43 -0
  12. package/functions/src/index.ts +345 -0
  13. package/functions/tsconfig.dev.json +3 -0
  14. package/functions/tsconfig.json +15 -0
  15. package/package.json +53 -270
  16. package/scripts/claims.cjs +99 -0
  17. package/src/app.d.ts +9 -0
  18. package/src/app.html +24 -0
  19. package/src/lib/components/Auth.svelte +65 -0
  20. package/{components/Authentication.d.ts → src/lib/components/Authentication.ts} +2 -0
  21. package/{components → src/lib/components}/BookPreview.svelte +27 -22
  22. package/{components → src/lib/components}/External.svelte +2 -1
  23. package/src/lib/components/Link.svelte +58 -0
  24. package/{components → src/lib/components}/app/BookList.svelte +6 -3
  25. package/{components → src/lib/components}/app/Button.svelte +5 -4
  26. package/{components → src/lib/components}/app/Feedback.svelte +11 -7
  27. package/{components → src/lib/components}/app/Head.svelte +2 -1
  28. package/{components → src/lib/components}/app/Large.svelte +0 -0
  29. package/{components → src/lib/components}/app/Lead.svelte +0 -0
  30. package/{components → src/lib/components}/app/Link.svelte +4 -3
  31. package/{components → src/lib/components}/app/Options.svelte +5 -4
  32. package/{components → src/lib/components}/app/Paragraph.svelte +2 -1
  33. package/{components → src/lib/components}/app/Section.svelte +0 -0
  34. package/{components → src/lib/components}/app/Table.svelte +0 -0
  35. package/{components → src/lib/components}/app/TextInput.svelte +12 -9
  36. package/{components → src/lib/components}/chapter/Atom.svelte +18 -12
  37. package/src/lib/components/chapter/Block.svelte +42 -0
  38. package/{components → src/lib/components}/chapter/Callout.svelte +6 -3
  39. package/{components → src/lib/components}/chapter/CaptionedCode.svelte +21 -13
  40. package/{components → src/lib/components}/chapter/ChapterBody.svelte +13 -9
  41. package/{components → src/lib/components}/chapter/Citations.svelte +34 -33
  42. package/{components → src/lib/components}/chapter/Code.svelte +63 -58
  43. package/{components → src/lib/components}/chapter/Comment.svelte +30 -18
  44. package/{components → src/lib/components}/chapter/Definition.svelte +23 -17
  45. package/{components → src/lib/components}/chapter/Embed.svelte +97 -77
  46. package/src/lib/components/chapter/ErrorMessage.svelte +12 -0
  47. package/{components → src/lib/components}/chapter/ExtendedPrism.js +0 -0
  48. package/{components → src/lib/components}/chapter/Figure.svelte +34 -26
  49. package/{components → src/lib/components}/chapter/Footnote.svelte +32 -20
  50. package/{components → src/lib/components}/chapter/Format.svelte +39 -34
  51. package/{components → src/lib/components}/chapter/InlineCode.svelte +9 -5
  52. package/{components → src/lib/components}/chapter/Label.svelte +23 -14
  53. package/{components → src/lib/components}/chapter/Link.svelte +27 -21
  54. package/{components → src/lib/components}/chapter/List.svelte +5 -3
  55. package/{components → src/lib/components}/chapter/Marginal.svelte +54 -41
  56. package/{components → src/lib/components}/chapter/Paragraph.svelte +25 -20
  57. package/{components → src/lib/components}/chapter/Positioned.svelte +4 -1
  58. package/{components → src/lib/components}/chapter/Problem.svelte +0 -0
  59. package/src/lib/components/chapter/Python.svelte +144 -0
  60. package/{components → src/lib/components}/chapter/Quote.svelte +9 -5
  61. package/{components → src/lib/components}/chapter/Rule.svelte +4 -1
  62. package/{components → src/lib/components}/chapter/Table.svelte +11 -7
  63. package/src/lib/components/chapter/Text.svelte +109 -0
  64. package/src/lib/components/editor/BookishEditor.svelte +1101 -0
  65. package/src/lib/components/editor/CalloutEditor.svelte +14 -0
  66. package/{components → src/lib/components}/editor/CaptionedCodeEditor.svelte +10 -6
  67. package/src/lib/components/editor/CaretContext.ts +55 -0
  68. package/{components/editor/CaretState.d.ts → src/lib/components/editor/CaretState.ts} +6 -7
  69. package/{components/editor/CaretUtilities.d.ts → src/lib/components/editor/CaretUtilities.ts} +1 -0
  70. package/{components → src/lib/components}/editor/CaretView.svelte +25 -20
  71. package/src/lib/components/editor/CitationsEditor.svelte +64 -0
  72. package/{components/editor/Clipboard.d.ts → src/lib/components/editor/Clipboard.ts} +2 -1
  73. package/{components/editor/Command.d.ts → src/lib/components/editor/Command.ts} +8 -2
  74. package/{components/editor/Commands.js → src/lib/components/editor/Commands.ts} +730 -315
  75. package/src/lib/components/editor/ConfirmButton.svelte +51 -0
  76. package/src/lib/components/editor/DefinitionEditor.svelte +44 -0
  77. package/src/lib/components/editor/EmbedEditor.svelte +152 -0
  78. package/{components → src/lib/components}/editor/Icon.svelte +2 -1
  79. package/{components → src/lib/components}/editor/ImageChooser.svelte +24 -19
  80. package/src/lib/components/editor/InlineCodeEditor.svelte +16 -0
  81. package/{components → src/lib/components}/editor/LabelEditor.svelte +14 -9
  82. package/{components → src/lib/components}/editor/LanguageEditor.svelte +8 -5
  83. package/src/lib/components/editor/LinkEditor.svelte +110 -0
  84. package/{components → src/lib/components}/editor/Note.svelte +0 -0
  85. package/{components → src/lib/components}/editor/Permissions.svelte +43 -36
  86. package/src/lib/components/editor/PositionEditor.svelte +21 -0
  87. package/src/lib/components/editor/QuoteEditor.svelte +14 -0
  88. package/{components → src/lib/components}/editor/Switch.svelte +14 -11
  89. package/src/lib/components/editor/TableEditor.svelte +14 -0
  90. package/{components → src/lib/components}/editor/TextEditor.svelte +101 -89
  91. package/src/lib/components/editor/Toggle.svelte +47 -0
  92. package/src/lib/components/editor/Toolbar.svelte +297 -0
  93. package/{components → src/lib/components}/editor/ToolbarGroup.svelte +5 -3
  94. package/{components → src/lib/components}/editor/ToolbarSpacer.svelte +0 -0
  95. package/src/lib/components/editor/URLEditor.svelte +15 -0
  96. package/{components/editor/UndoState.d.ts → src/lib/components/editor/UndoState.ts} +3 -2
  97. package/{components → src/lib/components}/editor/icons/annotate.svg +0 -0
  98. package/{components → src/lib/components}/editor/icons/block.svg +0 -0
  99. package/{components → src/lib/components}/editor/icons/bold.svg +0 -0
  100. package/{components → src/lib/components}/editor/icons/bullets.svg +0 -0
  101. package/{components → src/lib/components}/editor/icons/callout.svg +0 -0
  102. package/{components → src/lib/components}/editor/icons/citation.svg +0 -0
  103. package/{components → src/lib/components}/editor/icons/clear.svg +0 -0
  104. package/{components → src/lib/components}/editor/icons/clipboard.svg +0 -0
  105. package/{components → src/lib/components}/editor/icons/code.svg +0 -0
  106. package/{components → src/lib/components}/editor/icons/columnafter.svg +0 -0
  107. package/{components → src/lib/components}/editor/icons/columnbefore.svg +0 -0
  108. package/{components → src/lib/components}/editor/icons/comment.svg +0 -0
  109. package/{components → src/lib/components}/editor/icons/copy.svg +0 -0
  110. package/{components → src/lib/components}/editor/icons/cut.svg +0 -0
  111. package/{components → src/lib/components}/editor/icons/define.svg +0 -0
  112. package/{components → src/lib/components}/editor/icons/deletecolumn.svg +0 -0
  113. package/{components → src/lib/components}/editor/icons/deleterow.svg +0 -0
  114. package/{components → src/lib/components}/editor/icons/footnote.svg +0 -0
  115. package/{components → src/lib/components}/editor/icons/header1.svg +0 -0
  116. package/{components → src/lib/components}/editor/icons/header2.svg +0 -0
  117. package/{components → src/lib/components}/editor/icons/header3.svg +0 -0
  118. package/{components → src/lib/components}/editor/icons/home.svg +0 -0
  119. package/{components → src/lib/components}/editor/icons/indent.svg +0 -0
  120. package/{components → src/lib/components}/editor/icons/italic.svg +0 -0
  121. package/{components → src/lib/components}/editor/icons/label.svg +0 -0
  122. package/{components → src/lib/components}/editor/icons/left.svg +0 -0
  123. package/{components → src/lib/components}/editor/icons/level.svg +0 -0
  124. package/{components → src/lib/components}/editor/icons/link.svg +0 -0
  125. package/{components → src/lib/components}/editor/icons/list.svg +0 -0
  126. package/{components → src/lib/components}/editor/icons/media.svg +0 -0
  127. package/{components → src/lib/components}/editor/icons/middle.svg +0 -0
  128. package/{components → src/lib/components}/editor/icons/numbers.svg +0 -0
  129. package/{components → src/lib/components}/editor/icons/paragraph.svg +0 -0
  130. package/{components → src/lib/components}/editor/icons/paste.svg +0 -0
  131. package/{components → src/lib/components}/editor/icons/quote.svg +0 -0
  132. package/{components → src/lib/components}/editor/icons/redo.svg +0 -0
  133. package/{components → src/lib/components}/editor/icons/right.svg +0 -0
  134. package/{components → src/lib/components}/editor/icons/rowabove.svg +0 -0
  135. package/{components → src/lib/components}/editor/icons/rowbelow.svg +0 -0
  136. package/{components → src/lib/components}/editor/icons/rule.svg +0 -0
  137. package/{components → src/lib/components}/editor/icons/sub.svg +0 -0
  138. package/{components → src/lib/components}/editor/icons/super.svg +0 -0
  139. package/{components → src/lib/components}/editor/icons/table.svg +0 -0
  140. package/{components → src/lib/components}/editor/icons/text.svg +0 -0
  141. package/{components → src/lib/components}/editor/icons/time.svg +0 -0
  142. package/{components → src/lib/components}/editor/icons/undo.svg +0 -0
  143. package/{components → src/lib/components}/editor/icons/unindent.svg +0 -0
  144. package/{components → src/lib/components}/editor/icons/unlink.svg +0 -0
  145. package/{components → src/lib/components}/page/Acknowledgements.svelte +19 -10
  146. package/src/lib/components/page/Authors.svelte +92 -0
  147. package/src/lib/components/page/BookLoader.svelte +77 -0
  148. package/src/lib/components/page/BulkReferenceEditor.svelte +86 -0
  149. package/src/lib/components/page/Chapter.svelte +449 -0
  150. package/src/lib/components/page/ChapterContext.ts +13 -0
  151. package/{components → src/lib/components}/page/ChapterNumber.svelte +0 -0
  152. package/{components → src/lib/components}/page/ChapterTitle.svelte +4 -2
  153. package/src/lib/components/page/Contexts.ts +176 -0
  154. package/{components → src/lib/components}/page/DefinitionView.svelte +52 -37
  155. package/{components → src/lib/components}/page/Description.svelte +15 -7
  156. package/src/lib/components/page/Edition.svelte +189 -0
  157. package/src/lib/components/page/EditionLoader.svelte +123 -0
  158. package/{components → src/lib/components}/page/Editions.svelte +22 -18
  159. package/{components → src/lib/components}/page/Glossary.svelte +33 -28
  160. package/{components → src/lib/components}/page/Header.svelte +59 -52
  161. package/{components → src/lib/components}/page/Index.svelte +48 -41
  162. package/{components → src/lib/components}/page/Instructions.svelte +2 -1
  163. package/{components → src/lib/components}/page/License.svelte +17 -9
  164. package/{components → src/lib/components}/page/Loading.svelte +0 -0
  165. package/{components → src/lib/components}/page/Media.svelte +44 -27
  166. package/{components → src/lib/components}/page/MediaPreview.svelte +3 -2
  167. package/{components → src/lib/components}/page/Muted.svelte +0 -0
  168. package/{components → src/lib/components}/page/Outline.svelte +140 -110
  169. package/src/lib/components/page/Page.svelte +94 -0
  170. package/{components → src/lib/components}/page/PageHeader.svelte +2 -1
  171. package/{components → src/lib/components}/page/PageParagraph.svelte +0 -0
  172. package/src/lib/components/page/PossibleChapter.svelte +16 -0
  173. package/src/lib/components/page/PossibleReference.svelte +14 -0
  174. package/{components → src/lib/components}/page/Print.svelte +5 -3
  175. package/{components → src/lib/components}/page/Publish.svelte +59 -52
  176. package/{components → src/lib/components}/page/Reference.svelte +39 -30
  177. package/{components → src/lib/components}/page/References.svelte +18 -14
  178. package/{components → src/lib/components}/page/Rows.svelte +0 -0
  179. package/src/lib/components/page/Search.svelte +153 -0
  180. package/{components → src/lib/components}/page/Status.svelte +5 -3
  181. package/{components → src/lib/components}/page/SubdomainEditor.svelte +16 -13
  182. package/{components → src/lib/components}/page/TableOfContents.svelte +79 -68
  183. package/src/lib/components/page/TableOfContentsImage.svelte +22 -0
  184. package/{components → src/lib/components}/page/TableOfContentsRow.svelte +34 -24
  185. package/{components → src/lib/components}/page/ThemeEditor.svelte +44 -33
  186. package/{components → src/lib/components}/page/ThemeEditorPreview.svelte +21 -12
  187. package/src/lib/components/page/ThemeSetEditor.svelte +21 -0
  188. package/{components → src/lib/components}/page/ThemeVariableEditor.svelte +18 -14
  189. package/{components → src/lib/components}/page/Title.svelte +0 -0
  190. package/{components → src/lib/components}/page/Unknown.svelte +10 -8
  191. package/{components/page/margins.js → src/lib/components/page/margins.ts} +79 -46
  192. package/{index.js → src/lib/index.js} +0 -0
  193. package/src/lib/models/CRUD.ts +469 -0
  194. package/{models/Firebase.js → src/lib/models/Firebase.ts} +26 -16
  195. package/src/lib/models/book/Book.ts +283 -0
  196. package/src/lib/models/book/BookMedia.ts +182 -0
  197. package/src/lib/models/book/BookSaveStatus.ts +8 -0
  198. package/src/lib/models/book/Chapter.ts +462 -0
  199. package/{models/book/ChapterID.js → src/lib/models/book/ChapterID.ts} +1 -0
  200. package/src/lib/models/book/Definition.ts +2 -0
  201. package/src/lib/models/book/Edition.ts +1216 -0
  202. package/src/lib/models/book/Reference.ts +171 -0
  203. package/{models/book/Schema.js → src/lib/models/book/Schema.ts} +1 -0
  204. package/{models/book/Theme.js → src/lib/models/book/Theme.ts} +111 -6
  205. package/{models/book/ThemeColors.d.ts → src/lib/models/book/ThemeColors.ts} +6 -0
  206. package/{models/book/Words.js → src/lib/models/book/Words.ts} +1 -0
  207. package/{models/chapter/AtomNode.js → src/lib/models/chapter/AtomNode.ts} +30 -10
  208. package/{models/chapter/BlockNode.js → src/lib/models/chapter/BlockNode.ts} +80 -47
  209. package/src/lib/models/chapter/BlocksNode.test.ts +867 -0
  210. package/{models/chapter/BlocksNode.js → src/lib/models/chapter/BlocksNode.ts} +571 -359
  211. package/{models/chapter/Bookkeeping.d.ts → src/lib/models/chapter/Bookkeeping.ts} +0 -0
  212. package/src/lib/models/chapter/CalloutNode.ts +79 -0
  213. package/{models/chapter/Caret.js → src/lib/models/chapter/Caret.ts} +50 -26
  214. package/src/lib/models/chapter/ChapterNode.ts +169 -0
  215. package/{models/chapter/CitationsNode.js → src/lib/models/chapter/CitationsNode.ts} +17 -10
  216. package/{models/chapter/CodeNode.js → src/lib/models/chapter/CodeNode.ts} +125 -62
  217. package/src/lib/models/chapter/CommentNode.ts +61 -0
  218. package/{models/chapter/DefinitionNode.js → src/lib/models/chapter/DefinitionNode.ts} +14 -8
  219. package/src/lib/models/chapter/Edit.ts +6 -0
  220. package/src/lib/models/chapter/EmbedNode.ts +257 -0
  221. package/{models/chapter/ErrorNode.js → src/lib/models/chapter/ErrorNode.ts} +15 -8
  222. package/src/lib/models/chapter/FootnoteNode.ts +59 -0
  223. package/src/lib/models/chapter/FormatNode.test.ts +137 -0
  224. package/{models/chapter/FormatNode.js → src/lib/models/chapter/FormatNode.ts} +443 -262
  225. package/{models/chapter/InlineCodeNode.js → src/lib/models/chapter/InlineCodeNode.ts} +17 -9
  226. package/{models/chapter/LabelNode.js → src/lib/models/chapter/LabelNode.ts} +26 -10
  227. package/{models/chapter/LinkNode.js → src/lib/models/chapter/LinkNode.ts} +12 -7
  228. package/{models/chapter/ListNode.test.js → src/lib/models/chapter/ListNode.test.ts} +57 -20
  229. package/{models/chapter/ListNode.js → src/lib/models/chapter/ListNode.ts} +277 -200
  230. package/src/lib/models/chapter/MetadataNode.ts +44 -0
  231. package/{models → src/lib/models}/chapter/Node.test.js +0 -0
  232. package/{models/chapter/Node.js → src/lib/models/chapter/Node.ts} +155 -71
  233. package/src/lib/models/chapter/ParagraphNode.test.ts +54 -0
  234. package/{models/chapter/ParagraphNode.js → src/lib/models/chapter/ParagraphNode.ts} +95 -58
  235. package/{models/chapter/Parser.test.js → src/lib/models/chapter/Parser.test.ts} +7 -0
  236. package/{models/chapter/Parser.js → src/lib/models/chapter/Parser.ts} +369 -184
  237. package/{models/chapter/Position.d.ts → src/lib/models/chapter/Position.ts} +0 -0
  238. package/src/lib/models/chapter/QuoteNode.ts +135 -0
  239. package/{models/chapter/RootNode.d.ts → src/lib/models/chapter/RootNode.ts} +2 -0
  240. package/{models/chapter/RuleNode.js → src/lib/models/chapter/RuleNode.ts} +9 -6
  241. package/{models/chapter/TableNode.test.js → src/lib/models/chapter/TableNode.test.ts} +27 -10
  242. package/{models/chapter/TableNode.js → src/lib/models/chapter/TableNode.ts} +141 -82
  243. package/{models/chapter/TextNode.test.js → src/lib/models/chapter/TextNode.test.ts} +2 -0
  244. package/{models/chapter/TextNode.js → src/lib/models/chapter/TextNode.ts} +117 -65
  245. package/{util/getSubdomain.js → src/lib/util/getSubdomain.ts} +14 -8
  246. package/{util/isMobile.js → src/lib/util/isMobile.ts} +1 -0
  247. package/{util/mineReference.js → src/lib/util/mineReference.ts} +45 -25
  248. package/{util/scrollToEyeLevel.js → src/lib/util/scrollToEyeLevel.ts} +4 -2
  249. package/src/params/edition.ts +3 -0
  250. package/src/routes/+error.svelte +12 -0
  251. package/src/routes/+layout.svelte +241 -0
  252. package/src/routes/+page.svelte +43 -0
  253. package/src/routes/Footer.svelte +0 -0
  254. package/src/routes/Header.svelte +112 -0
  255. package/src/routes/[bookid]/+layout.svelte +5 -0
  256. package/src/routes/[bookid]/[[editionid=edition]]/+error.svelte +14 -0
  257. package/src/routes/[bookid]/[[editionid=edition]]/+layout.svelte +7 -0
  258. package/src/routes/[bookid]/[[editionid=edition]]/+page.svelte +5 -0
  259. package/src/routes/[bookid]/[[editionid=edition]]/[chapterid]/+page.svelte +5 -0
  260. package/src/routes/[bookid]/[[editionid=edition]]/glossary/+page.svelte +5 -0
  261. package/src/routes/[bookid]/[[editionid=edition]]/index/[[letter]]/+page.svelte +5 -0
  262. package/src/routes/[bookid]/[[editionid=edition]]/media/+page.svelte +5 -0
  263. package/src/routes/[bookid]/[[editionid=edition]]/print/+page.svelte +5 -0
  264. package/src/routes/[bookid]/[[editionid=edition]]/references/+page.svelte +5 -0
  265. package/src/routes/[bookid]/[[editionid=edition]]/search/+page.svelte +5 -0
  266. package/src/routes/[bookid]/editions/+page.svelte +20 -0
  267. package/src/routes/about/+page.svelte +102 -0
  268. package/src/routes/confirm/+page.svelte +62 -0
  269. package/src/routes/email/+page.svelte +99 -0
  270. package/src/routes/login/+page.svelte +66 -0
  271. package/src/routes/read/+page.svelte +38 -0
  272. package/src/routes/write/+error.svelte +10 -0
  273. package/src/routes/write/+layout.svelte +11 -0
  274. package/src/routes/write/+page.svelte +114 -0
  275. package/src/routes/write/[bookid]/+layout.svelte +5 -0
  276. package/src/routes/write/[bookid]/[[editionid=edition]]/+layout.svelte +7 -0
  277. package/src/routes/write/[bookid]/[[editionid=edition]]/+page.svelte +5 -0
  278. package/src/routes/write/[bookid]/[[editionid=edition]]/[chapterid]/+page.svelte +5 -0
  279. package/src/routes/write/[bookid]/[[editionid=edition]]/glossary/+page.svelte +5 -0
  280. package/src/routes/write/[bookid]/[[editionid=edition]]/index/[[letter]]/+page.svelte +5 -0
  281. package/src/routes/write/[bookid]/[[editionid=edition]]/media/+page.svelte +5 -0
  282. package/src/routes/write/[bookid]/[[editionid=edition]]/print/+page.svelte +5 -0
  283. package/src/routes/write/[bookid]/[[editionid=edition]]/references/+page.svelte +5 -0
  284. package/src/routes/write/[bookid]/[[editionid=edition]]/search/+page.svelte +5 -0
  285. package/src/routes/write/[bookid]/[[editionid=edition]]/theme/+page.svelte +5 -0
  286. package/src/routes/write/[bookid]/[[editionid=edition]]/unknown/+page.svelte +5 -0
  287. package/src/routes/write/[bookid]/editions/+page.svelte +20 -0
  288. package/src/vite-env.d.ts +1 -0
  289. package/static/icons/favicon.png +0 -0
  290. package/static/icons/icon.png +0 -0
  291. package/static/icons/ios-icon.png +0 -0
  292. package/storage.rules +9 -0
  293. package/svelte.config.js +19 -0
  294. package/tsconfig.json +18 -0
  295. package/tsconfig.node.json +9 -0
  296. package/vite.config.ts +8 -0
  297. package/vite.reader.config.ts +32 -0
  298. package/components/Auth.svelte +0 -55
  299. package/components/Auth.svelte.d.ts +0 -16
  300. package/components/Authentication.js +0 -1
  301. package/components/BookPreview.svelte.d.ts +0 -18
  302. package/components/External.svelte.d.ts +0 -18
  303. package/components/Link.svelte +0 -51
  304. package/components/Link.svelte.d.ts +0 -18
  305. package/components/app/BookList.svelte.d.ts +0 -18
  306. package/components/app/Button.svelte.d.ts +0 -22
  307. package/components/app/Feedback.svelte.d.ts +0 -18
  308. package/components/app/Head.svelte.d.ts +0 -18
  309. package/components/app/Large.svelte.d.ts +0 -27
  310. package/components/app/Lead.svelte.d.ts +0 -27
  311. package/components/app/Link.svelte.d.ts +0 -20
  312. package/components/app/Options.svelte.d.ts +0 -19
  313. package/components/app/Paragraph.svelte.d.ts +0 -18
  314. package/components/app/Section.svelte.d.ts +0 -27
  315. package/components/app/Table.svelte.d.ts +0 -27
  316. package/components/app/TextInput.svelte.d.ts +0 -22
  317. package/components/chapter/Atom.svelte.d.ts +0 -19
  318. package/components/chapter/Block.svelte +0 -37
  319. package/components/chapter/Block.svelte.d.ts +0 -17
  320. package/components/chapter/Callout.svelte.d.ts +0 -17
  321. package/components/chapter/CaptionedCode.svelte.d.ts +0 -17
  322. package/components/chapter/ChapterBody.svelte.d.ts +0 -19
  323. package/components/chapter/Citations.svelte.d.ts +0 -17
  324. package/components/chapter/Code.svelte.d.ts +0 -22
  325. package/components/chapter/Comment.svelte.d.ts +0 -17
  326. package/components/chapter/Definition.svelte.d.ts +0 -17
  327. package/components/chapter/Embed.svelte.d.ts +0 -20
  328. package/components/chapter/ErrorMessage.svelte +0 -9
  329. package/components/chapter/ErrorMessage.svelte.d.ts +0 -17
  330. package/components/chapter/ExtendedPrism.d.ts +0 -2
  331. package/components/chapter/Figure.svelte.d.ts +0 -24
  332. package/components/chapter/Footnote.svelte.d.ts +0 -19
  333. package/components/chapter/Format.svelte.d.ts +0 -19
  334. package/components/chapter/InlineCode.svelte.d.ts +0 -17
  335. package/components/chapter/Label.svelte.d.ts +0 -17
  336. package/components/chapter/Link.svelte.d.ts +0 -17
  337. package/components/chapter/List.svelte.d.ts +0 -17
  338. package/components/chapter/Marginal.svelte.d.ts +0 -22
  339. package/components/chapter/Paragraph.svelte.d.ts +0 -18
  340. package/components/chapter/Positioned.svelte.d.ts +0 -19
  341. package/components/chapter/Problem.svelte.d.ts +0 -27
  342. package/components/chapter/Python.svelte +0 -128
  343. package/components/chapter/Python.svelte.d.ts +0 -18
  344. package/components/chapter/Quote.svelte.d.ts +0 -17
  345. package/components/chapter/Rule.svelte.d.ts +0 -17
  346. package/components/chapter/Table.svelte.d.ts +0 -17
  347. package/components/chapter/Text.svelte +0 -87
  348. package/components/chapter/Text.svelte.d.ts +0 -18
  349. package/components/editor/BookishEditor.svelte +0 -884
  350. package/components/editor/BookishEditor.svelte.d.ts +0 -25
  351. package/components/editor/CalloutEditor.svelte +0 -10
  352. package/components/editor/CalloutEditor.svelte.d.ts +0 -17
  353. package/components/editor/CaptionedCodeEditor.svelte.d.ts +0 -17
  354. package/components/editor/CaretContext.d.ts +0 -50
  355. package/components/editor/CaretContext.js +0 -1
  356. package/components/editor/CaretState.js +0 -1
  357. package/components/editor/CaretUtilities.js +0 -1
  358. package/components/editor/CaretView.svelte.d.ts +0 -25
  359. package/components/editor/CitationsEditor.svelte +0 -53
  360. package/components/editor/CitationsEditor.svelte.d.ts +0 -17
  361. package/components/editor/Clipboard.js +0 -1
  362. package/components/editor/Command.js +0 -1
  363. package/components/editor/Commands.d.ts +0 -3
  364. package/components/editor/ConfirmButton.svelte +0 -49
  365. package/components/editor/ConfirmButton.svelte.d.ts +0 -19
  366. package/components/editor/DefinitionEditor.svelte +0 -36
  367. package/components/editor/DefinitionEditor.svelte.d.ts +0 -17
  368. package/components/editor/EmbedEditor.svelte +0 -137
  369. package/components/editor/EmbedEditor.svelte.d.ts +0 -17
  370. package/components/editor/Icon.svelte.d.ts +0 -16
  371. package/components/editor/ImageChooser.svelte.d.ts +0 -18
  372. package/components/editor/InlineCodeEditor.svelte +0 -12
  373. package/components/editor/InlineCodeEditor.svelte.d.ts +0 -17
  374. package/components/editor/LabelEditor.svelte.d.ts +0 -17
  375. package/components/editor/LanguageEditor.svelte.d.ts +0 -17
  376. package/components/editor/LinkEditor.svelte +0 -100
  377. package/components/editor/LinkEditor.svelte.d.ts +0 -17
  378. package/components/editor/Note.svelte.d.ts +0 -27
  379. package/components/editor/Permissions.svelte.d.ts +0 -20
  380. package/components/editor/PositionEditor.svelte +0 -17
  381. package/components/editor/PositionEditor.svelte.d.ts +0 -18
  382. package/components/editor/QuoteEditor.svelte +0 -10
  383. package/components/editor/QuoteEditor.svelte.d.ts +0 -17
  384. package/components/editor/Switch.svelte.d.ts +0 -20
  385. package/components/editor/TableEditor.svelte +0 -10
  386. package/components/editor/TableEditor.svelte.d.ts +0 -17
  387. package/components/editor/TextEditor.svelte.d.ts +0 -24
  388. package/components/editor/Toggle.svelte +0 -41
  389. package/components/editor/Toggle.svelte.d.ts +0 -19
  390. package/components/editor/Toolbar.svelte +0 -249
  391. package/components/editor/Toolbar.svelte.d.ts +0 -17
  392. package/components/editor/ToolbarGroup.svelte.d.ts +0 -18
  393. package/components/editor/ToolbarSpacer.svelte.d.ts +0 -23
  394. package/components/editor/URLEditor.svelte +0 -13
  395. package/components/editor/URLEditor.svelte.d.ts +0 -18
  396. package/components/editor/UndoState.js +0 -1
  397. package/components/page/Acknowledgements.svelte.d.ts +0 -14
  398. package/components/page/Authors.svelte +0 -85
  399. package/components/page/Authors.svelte.d.ts +0 -21
  400. package/components/page/BookLoader.svelte +0 -59
  401. package/components/page/BookLoader.svelte.d.ts +0 -16
  402. package/components/page/BulkReferenceEditor.svelte +0 -63
  403. package/components/page/BulkReferenceEditor.svelte.d.ts +0 -14
  404. package/components/page/Chapter.svelte +0 -392
  405. package/components/page/Chapter.svelte.d.ts +0 -18
  406. package/components/page/ChapterContext.d.ts +0 -10
  407. package/components/page/ChapterContext.js +0 -1
  408. package/components/page/ChapterNumber.svelte.d.ts +0 -27
  409. package/components/page/ChapterTitle.svelte.d.ts +0 -18
  410. package/components/page/Contexts.d.ts +0 -47
  411. package/components/page/Contexts.js +0 -133
  412. package/components/page/DefinitionView.svelte.d.ts +0 -18
  413. package/components/page/Description.svelte.d.ts +0 -14
  414. package/components/page/Edition.svelte +0 -161
  415. package/components/page/Edition.svelte.d.ts +0 -20
  416. package/components/page/EditionLoader.svelte +0 -99
  417. package/components/page/EditionLoader.svelte.d.ts +0 -18
  418. package/components/page/Editions.svelte.d.ts +0 -14
  419. package/components/page/Glossary.svelte.d.ts +0 -14
  420. package/components/page/Header.svelte.d.ts +0 -29
  421. package/components/page/Index.svelte.d.ts +0 -14
  422. package/components/page/Instructions.svelte.d.ts +0 -18
  423. package/components/page/License.svelte.d.ts +0 -14
  424. package/components/page/Loading.svelte.d.ts +0 -23
  425. package/components/page/Media.svelte.d.ts +0 -14
  426. package/components/page/MediaPreview.svelte.d.ts +0 -19
  427. package/components/page/Muted.svelte.d.ts +0 -27
  428. package/components/page/Outline.svelte.d.ts +0 -19
  429. package/components/page/Page.svelte +0 -84
  430. package/components/page/Page.svelte.d.ts +0 -19
  431. package/components/page/PageHeader.svelte.d.ts +0 -18
  432. package/components/page/PageParagraph.svelte.d.ts +0 -27
  433. package/components/page/PossibleChapter.svelte +0 -14
  434. package/components/page/PossibleChapter.svelte.d.ts +0 -14
  435. package/components/page/PossibleReference.svelte +0 -11
  436. package/components/page/PossibleReference.svelte.d.ts +0 -18
  437. package/components/page/Print.svelte.d.ts +0 -14
  438. package/components/page/Publish.svelte.d.ts +0 -14
  439. package/components/page/Reference.svelte.d.ts +0 -17
  440. package/components/page/References.svelte.d.ts +0 -14
  441. package/components/page/Rows.svelte.d.ts +0 -27
  442. package/components/page/Search.svelte +0 -133
  443. package/components/page/Search.svelte.d.ts +0 -14
  444. package/components/page/Status.svelte.d.ts +0 -14
  445. package/components/page/SubdomainEditor.svelte.d.ts +0 -14
  446. package/components/page/TableOfContents.svelte.d.ts +0 -14
  447. package/components/page/TableOfContentsImage.svelte +0 -18
  448. package/components/page/TableOfContentsImage.svelte.d.ts +0 -16
  449. package/components/page/TableOfContentsRow.svelte.d.ts +0 -24
  450. package/components/page/ThemeEditor.svelte.d.ts +0 -14
  451. package/components/page/ThemeEditorPreview.svelte.d.ts +0 -17
  452. package/components/page/ThemeSetEditor.svelte +0 -19
  453. package/components/page/ThemeSetEditor.svelte.d.ts +0 -18
  454. package/components/page/ThemeVariableEditor.svelte.d.ts +0 -18
  455. package/components/page/Title.svelte.d.ts +0 -27
  456. package/components/page/Unknown.svelte.d.ts +0 -16
  457. package/components/page/margins.d.ts +0 -2
  458. package/index.d.ts +0 -15
  459. package/models/CRUD.d.ts +0 -27
  460. package/models/CRUD.js +0 -262
  461. package/models/Firebase.d.ts +0 -5
  462. package/models/book/Book.d.ts +0 -61
  463. package/models/book/Book.js +0 -135
  464. package/models/book/BookMedia.d.ts +0 -23
  465. package/models/book/BookMedia.js +0 -108
  466. package/models/book/BookSaveStatus.d.ts +0 -7
  467. package/models/book/BookSaveStatus.js +0 -8
  468. package/models/book/Chapter.d.ts +0 -81
  469. package/models/book/Chapter.js +0 -212
  470. package/models/book/ChapterID.d.ts +0 -10
  471. package/models/book/Definition.d.ts +0 -6
  472. package/models/book/Definition.js +0 -1
  473. package/models/book/Edition.d.ts +0 -139
  474. package/models/book/Edition.js +0 -620
  475. package/models/book/Reference.d.ts +0 -24
  476. package/models/book/Reference.js +0 -78
  477. package/models/book/Schema.d.ts +0 -174
  478. package/models/book/Theme.d.ts +0 -113
  479. package/models/book/ThemeColors.js +0 -1
  480. package/models/book/Words.d.ts +0 -2
  481. package/models/chapter/AtomNode.d.ts +0 -18
  482. package/models/chapter/BlockNode.d.ts +0 -20
  483. package/models/chapter/BlocksNode.d.ts +0 -50
  484. package/models/chapter/BlocksNode.test.d.ts +0 -1
  485. package/models/chapter/BlocksNode.test.js +0 -568
  486. package/models/chapter/Bookkeeping.js +0 -1
  487. package/models/chapter/CalloutNode.d.ts +0 -17
  488. package/models/chapter/CalloutNode.js +0 -58
  489. package/models/chapter/Caret.d.ts +0 -23
  490. package/models/chapter/ChapterNode.d.ts +0 -34
  491. package/models/chapter/ChapterNode.js +0 -117
  492. package/models/chapter/CitationsNode.d.ts +0 -16
  493. package/models/chapter/CodeNode.d.ts +0 -35
  494. package/models/chapter/CommentNode.d.ts +0 -18
  495. package/models/chapter/CommentNode.js +0 -48
  496. package/models/chapter/DefinitionNode.d.ts +0 -12
  497. package/models/chapter/Edit.d.ts +0 -7
  498. package/models/chapter/Edit.js +0 -1
  499. package/models/chapter/EmbedNode.d.ts +0 -45
  500. package/models/chapter/EmbedNode.js +0 -168
  501. package/models/chapter/ErrorNode.d.ts +0 -17
  502. package/models/chapter/FootnoteNode.d.ts +0 -18
  503. package/models/chapter/FootnoteNode.js +0 -47
  504. package/models/chapter/FormatNode.d.ts +0 -60
  505. package/models/chapter/FormatNode.test.d.ts +0 -1
  506. package/models/chapter/FormatNode.test.js +0 -96
  507. package/models/chapter/InlineCodeNode.d.ts +0 -12
  508. package/models/chapter/LabelNode.d.ts +0 -17
  509. package/models/chapter/LinkNode.d.ts +0 -12
  510. package/models/chapter/ListNode.d.ts +0 -51
  511. package/models/chapter/ListNode.test.d.ts +0 -1
  512. package/models/chapter/MetadataNode.d.ts +0 -17
  513. package/models/chapter/MetadataNode.js +0 -32
  514. package/models/chapter/Node.d.ts +0 -38
  515. package/models/chapter/Node.test.d.ts +0 -1
  516. package/models/chapter/ParagraphNode.d.ts +0 -33
  517. package/models/chapter/ParagraphNode.test.d.ts +0 -1
  518. package/models/chapter/ParagraphNode.test.js +0 -30
  519. package/models/chapter/Parser.d.ts +0 -75
  520. package/models/chapter/Parser.test.d.ts +0 -1
  521. package/models/chapter/Position.js +0 -1
  522. package/models/chapter/QuoteNode.d.ts +0 -25
  523. package/models/chapter/QuoteNode.js +0 -99
  524. package/models/chapter/RootNode.js +0 -1
  525. package/models/chapter/RuleNode.d.ts +0 -15
  526. package/models/chapter/TableNode.d.ts +0 -37
  527. package/models/chapter/TableNode.test.d.ts +0 -1
  528. package/models/chapter/TextNode.d.ts +0 -36
  529. package/models/chapter/TextNode.test.d.ts +0 -1
  530. package/util/getSubdomain.d.ts +0 -3
  531. package/util/isMobile.d.ts +0 -2
  532. package/util/mineReference.d.ts +0 -4
  533. package/util/scrollToEyeLevel.d.ts +0 -2
@@ -1,24 +1,33 @@
1
+ import type Node from './Node';
1
2
  import BlockNode from './BlockNode';
2
3
  import ListNode from './ListNode';
4
+ import type Caret from './Caret';
5
+ import type { CaretRange } from './Caret';
3
6
  import { caretToIndex, indexToCaret } from './Caret';
4
7
  import ParagraphNode from './ParagraphNode';
8
+ import type { Format, FormatNodeSegmentType } from './FormatNode';
5
9
  import FormatNode from './FormatNode';
6
10
  import TextNode from './TextNode';
7
11
  import AtomNode from './AtomNode';
12
+ import type Edit from './Edit';
8
13
  import CodeNode from './CodeNode';
9
14
  import EmbedNode from './EmbedNode';
10
- export default class BlocksNode extends BlockNode {
11
- #blocks;
12
- constructor(elements) {
15
+
16
+ export default abstract class BlocksNode extends BlockNode {
17
+ readonly #blocks: BlockNode[];
18
+
19
+ constructor(elements: BlockNode[]) {
13
20
  super();
14
21
  const adjustedElements = [];
15
22
  for (let i = 0; i < elements.length; i++) {
16
23
  const block = elements[i];
17
24
  const previousBlock = i === 0 ? undefined : elements[i - 1];
18
25
  // If this block and the previous one are lists of the same kind, merge them.
19
- if (block instanceof ListNode &&
26
+ if (
27
+ block instanceof ListNode &&
20
28
  previousBlock instanceof ListNode &&
21
- block.isNumbered() === previousBlock.isNumbered()) {
29
+ block.isNumbered() === previousBlock.isNumbered()
30
+ ) {
22
31
  adjustedElements[adjustedElements.length - 1] =
23
32
  previousBlock.withListAppended(block);
24
33
  }
@@ -28,40 +37,49 @@ export default class BlocksNode extends BlockNode {
28
37
  // if(!(block instanceof ParagraphNode) && (i === 0 || !(previousBlock instanceof ParagraphNode)))
29
38
  // adjustedElements.push(new ParagraphNode());
30
39
  // }
31
- else
32
- adjustedElements.push(block);
40
+ else adjustedElements.push(block);
33
41
  }
34
42
  // Check the last one.
35
43
  // if(!(adjustedElements[adjustedElements.length - 1] instanceof ParagraphNode))
36
44
  // adjustedElements.push(new ParagraphNode());
45
+
37
46
  this.#blocks = adjustedElements;
47
+
38
48
  // After all that, always ensure there's at least one empty paragraph block.
39
- if (this.#blocks.length === 0)
40
- this.#blocks.push(new ParagraphNode());
49
+ if (this.#blocks.length === 0) this.#blocks.push(new ParagraphNode());
41
50
  }
51
+
42
52
  getBlocks() {
43
53
  return this.#blocks;
44
54
  }
45
55
  getFormats() {
46
- return this.getBlocks().reduce((prev, current) => prev.concat(current.getFormats()), []);
56
+ return this.getBlocks().reduce(
57
+ (prev: FormatNode[], current) => prev.concat(current.getFormats()),
58
+ []
59
+ );
47
60
  }
48
- getIndexOf(block) {
61
+
62
+ getIndexOf(block: BlockNode): number | undefined {
49
63
  const index = this.#blocks.indexOf(block);
50
64
  return index < 0 ? undefined : index;
51
65
  }
52
- getBlockBefore(anchor) {
66
+
67
+ getBlockBefore(anchor: BlockNode): BlockNode | undefined {
53
68
  const index = this.#blocks.indexOf(anchor);
54
- if (index <= 0)
55
- return undefined;
69
+ if (index <= 0) return undefined;
56
70
  return this.#blocks[index - 1];
57
71
  }
58
- getBlockAfter(anchor) {
72
+
73
+ getBlockAfter(anchor: BlockNode): BlockNode | undefined {
59
74
  const index = this.#blocks.indexOf(anchor);
60
- if (index < 0 || index > this.#blocks.length - 2)
61
- return undefined;
75
+ if (index < 0 || index > this.#blocks.length - 2) return undefined;
62
76
  return this.#blocks[index + 1];
63
77
  }
64
- getBlocksBetween(first, last) {
78
+
79
+ getBlocksBetween(
80
+ first: BlockNode,
81
+ last: BlockNode
82
+ ): BlockNode[] | undefined {
65
83
  const firstIndex = this.getIndexOf(first);
66
84
  const lastIndex = this.getIndexOf(last);
67
85
  if (firstIndex !== undefined && lastIndex !== undefined) {
@@ -75,133 +93,164 @@ export default class BlocksNode extends BlockNode {
75
93
  let inside = false;
76
94
  for (let i = 0; i < this.#blocks.length; i++) {
77
95
  let block = this.#blocks[i];
78
- if (block === first)
79
- inside = true;
80
- if (inside)
81
- blocks.push(block);
82
- if (block === last)
83
- break;
96
+ if (block === first) inside = true;
97
+ if (inside) blocks.push(block);
98
+ if (block === last) break;
84
99
  }
85
100
  return blocks;
86
101
  }
87
102
  return undefined;
88
103
  }
89
- getBlocksInRange(range) {
104
+
105
+ getBlocksInRange(range: CaretRange): BlockNode[] | undefined {
90
106
  // Find the start and end block the carets reside in. Bail on fail.
91
107
  const startBlock = this.getBlockOfCaret(range.start);
92
- const endBlock = range.end === range.start
93
- ? startBlock
94
- : this.getBlockOfCaret(range.end);
95
- if (startBlock === undefined || endBlock === undefined)
96
- return;
108
+ const endBlock =
109
+ range.end === range.start
110
+ ? startBlock
111
+ : this.getBlockOfCaret(range.end);
112
+ if (startBlock === undefined || endBlock === undefined) return;
113
+
97
114
  // Find all of the blocks between the start and end blocks.
98
115
  // Start tracking when we hit the start node. Stop tracking when we hit the end node.
99
- const blocksToEdit = [];
116
+ const blocksToEdit: BlockNode[] = [];
100
117
  let insideSelection = false;
101
118
  this.getNodes()
102
119
  .filter((n) => n instanceof BlockNode)
103
120
  .forEach((node) => {
104
- if (node === startBlock)
105
- insideSelection = true;
106
- if (insideSelection && node instanceof BlockNode)
107
- blocksToEdit.push(node);
108
- if (insideSelection && node === endBlock)
109
- insideSelection = false;
110
- });
121
+ if (node === startBlock) insideSelection = true;
122
+ if (insideSelection && node instanceof BlockNode)
123
+ blocksToEdit.push(node);
124
+ if (insideSelection && node === endBlock)
125
+ insideSelection = false;
126
+ });
127
+
111
128
  return blocksToEdit;
112
129
  }
113
- getBlockOfCaret(caret) {
114
- return caret.node.getClosestParentOfType(this, BlockNode);
130
+
131
+ getBlockOfCaret(caret: Caret): BlockNode | undefined {
132
+ return caret.node.getClosestParentOfType<BlockNode>(this, BlockNode);
115
133
  }
116
- getParentOf(node) {
134
+
135
+ getParentOf(node: Node): Node | undefined {
117
136
  return this.#blocks
118
137
  .map((b) => (b === node ? this : b.getParentOf(node)))
119
138
  .find((b) => b !== undefined);
120
139
  }
121
- getTextNodes() {
122
- return this.getNodes().filter((node) => node instanceof TextNode);
140
+
141
+ getTextNodes(): TextNode[] {
142
+ return this.getNodes().filter(
143
+ (node) => node instanceof TextNode
144
+ ) as TextNode[];
123
145
  }
124
- getAtomNodes() {
125
- return this.getNodes().filter((node) => node instanceof AtomNode);
146
+
147
+ getAtomNodes(): AtomNode<any>[] {
148
+ return this.getNodes().filter(
149
+ (node) => node instanceof AtomNode
150
+ ) as AtomNode<any>[];
126
151
  }
127
- getTextAndAtomNodes() {
128
- return this.getNodes().filter((node) => node instanceof TextNode || node instanceof AtomNode);
152
+
153
+ getTextAndAtomNodes(): (TextNode | AtomNode<any>)[] {
154
+ return this.getNodes().filter(
155
+ (node) => node instanceof TextNode || node instanceof AtomNode
156
+ ) as (TextNode | AtomNode<any>)[];
129
157
  }
130
- getIndexOfTextNode(node) {
158
+
159
+ getIndexOfTextNode(node: TextNode): number | undefined {
131
160
  const text = this.getTextNodes();
132
161
  return text.indexOf(node);
133
162
  }
134
- getFirstCaret() {
163
+
164
+ getFirstCaret(): Caret | undefined {
135
165
  const text = this.getTextNodes();
136
166
  return text.length === 0 ? undefined : { node: text[0], index: 0 };
137
167
  }
138
- getLastCaret() {
168
+
169
+ getLastCaret(): Caret | undefined {
139
170
  const text = this.getTextNodes();
140
171
  return text.length === 0
141
172
  ? undefined
142
173
  : {
143
- node: text[text.length - 1],
144
- index: text[text.length - 1].getLength(),
145
- };
174
+ node: text[text.length - 1],
175
+ index: text[text.length - 1].getLength(),
176
+ };
146
177
  }
147
- getAdjacentCaret(caret, next) {
148
- if (!this.contains(caret.node))
149
- return;
178
+
179
+ getAdjacentCaret(caret: Caret, next: boolean): Caret | undefined {
180
+ if (!this.contains(caret.node)) return;
181
+
150
182
  // Navigate from code node.
151
- const code = caret.node.getFarthestParentMatching(this, (n) => n instanceof CodeNode);
183
+ const code = caret.node.getFarthestParentMatching(
184
+ this,
185
+ (n) => n instanceof CodeNode
186
+ ) as CodeNode;
152
187
  if (code && caret.node === code.getCodeNode()) {
153
188
  // Get the adjacent text node.
154
189
  const newCaret = next
155
190
  ? caret.node.getNextCaret(caret.index)
156
191
  : caret.node.getPreviousCaret(caret.index);
157
- if (newCaret !== undefined)
158
- return newCaret;
192
+ if (newCaret !== undefined) return newCaret;
159
193
  // Otherwise, get the adjacent format's first/last caret.
160
- if (next)
161
- return code.getCaption().getFirstCaret();
194
+ if (next) return code.getCaption().getFirstCaret();
162
195
  // If there's no adjacent caret, find the adjacent root format.
163
- const blocks = this.getNodes().filter((n) => n instanceof BlockNode);
196
+ const blocks = this.getNodes().filter(
197
+ (n) => n instanceof BlockNode
198
+ ) as BlockNode[];
164
199
  const codeIndex = blocks.indexOf(code);
165
- if (codeIndex <= 0)
166
- return;
200
+ if (codeIndex <= 0) return;
167
201
  const adjacentBlock = blocks[codeIndex - 1];
168
202
  const adjacentFormats = adjacentBlock.getFormats();
169
203
  const adjacentFormat = adjacentFormats[adjacentFormats.length - 1];
170
204
  return adjacentFormat.getLastCaret();
171
205
  }
172
- const format = caret.node.getFarthestParentMatching(this, (n) => n instanceof FormatNode);
173
- if (format === undefined)
174
- return;
206
+
207
+ const format = caret.node.getFarthestParentMatching(
208
+ this,
209
+ (n) => n instanceof FormatNode
210
+ ) as FormatNode;
211
+ if (format === undefined) return;
212
+
175
213
  // Find the format that contains the caret and return the adjacent caret if there is one.
176
214
  const adjacentCaretInFormat = format.getAdjacentCaret(caret, next);
177
- if (adjacentCaretInFormat !== undefined)
178
- return adjacentCaretInFormat;
215
+ if (adjacentCaretInFormat !== undefined) return adjacentCaretInFormat;
216
+
179
217
  // If there's no adjacent caret in the root format, find the adjacent format or code.
180
218
  const formats = this.getNodes().filter((n) => {
181
219
  const parent = n.getParent(this);
182
- return ((n instanceof TextNode && parent instanceof CodeNode) ||
220
+ return (
221
+ (n instanceof TextNode && parent instanceof CodeNode) ||
183
222
  (n instanceof FormatNode &&
184
223
  !(parent instanceof AtomNode) &&
185
- !(parent instanceof FormatNode)));
186
- });
224
+ !(parent instanceof FormatNode))
225
+ );
226
+ }) as (TextNode | FormatNode)[];
227
+
187
228
  const index = formats.indexOf(format);
188
- const adjacentFormat = index < 0 || formats.length === 0
189
- ? undefined
190
- : formats[index + (next ? 1 : -1)];
191
- if (adjacentFormat === undefined)
192
- return;
229
+ const adjacentFormat =
230
+ index < 0 || formats.length === 0
231
+ ? undefined
232
+ : formats[index + (next ? 1 : -1)];
233
+ if (adjacentFormat === undefined) return;
193
234
  return next
194
235
  ? adjacentFormat.getFirstCaret()
195
236
  : adjacentFormat.getLastCaret();
196
237
  }
197
- getSelectedFormatRange(format, range) {
238
+
239
+ getSelectedFormatRange(
240
+ format: FormatNode,
241
+ range: CaretRange
242
+ ): CaretRange | undefined {
198
243
  // Find all of the formats.
199
- const formats = this.getNodes().filter((n) => n instanceof FormatNode &&
200
- !(this.getParentOf(n) instanceof FormatNode));
244
+ const formats = this.getNodes().filter(
245
+ (n) =>
246
+ n instanceof FormatNode &&
247
+ !(this.getParentOf(n) instanceof FormatNode)
248
+ ) as FormatNode[];
249
+
201
250
  // Find the given format index.
202
251
  const formatIndex = formats.indexOf(format);
203
- if (formatIndex < 0)
204
- return undefined;
252
+ if (formatIndex < 0) return undefined;
253
+
205
254
  // Find the range start and end formats.
206
255
  const startFormat = formats.find((f) => f.contains(range.start.node));
207
256
  const endFormat = formats.find((f) => f.contains(range.end.node));
@@ -209,196 +258,247 @@ export default class BlocksNode extends BlockNode {
209
258
  return undefined;
210
259
  const startIndex = formats.indexOf(startFormat);
211
260
  const endIndex = formats.indexOf(endFormat);
261
+
212
262
  // No range if out of bounds.
213
- if (startIndex < 0 || endIndex < 0)
214
- return undefined;
263
+ if (startIndex < 0 || endIndex < 0) return undefined;
215
264
  if (formatIndex < startIndex || formatIndex > endIndex)
216
265
  return undefined;
266
+
217
267
  // Choose the portion of the format that is selected.
218
- const startCaret = formatIndex === startIndex ? range.start : format.getFirstCaret();
219
- const endCaret = formatIndex === endIndex ? range.end : format.getLastCaret();
220
- if (startCaret === undefined || endCaret === undefined)
221
- return;
268
+ const startCaret =
269
+ formatIndex === startIndex ? range.start : format.getFirstCaret();
270
+ const endCaret =
271
+ formatIndex === endIndex ? range.end : format.getLastCaret();
272
+ if (startCaret === undefined || endCaret === undefined) return;
222
273
  return { start: startCaret, end: endCaret };
223
274
  }
224
- withContentInRange(range) {
275
+
276
+ abstract create(blocks: BlockNode[]): BlocksNode;
277
+
278
+ withContentInRange(range: CaretRange): this | undefined {
225
279
  const containsStart = this.contains(range.start.node);
226
280
  const containsEnd = this.contains(range.end.node);
227
- if (!containsStart && !containsEnd)
228
- return this.copy();
281
+
282
+ if (!containsStart && !containsEnd) return this.copy();
283
+
229
284
  let foundStart = false;
230
285
  let foundEnd = false;
231
- const newBlocks = [];
286
+ const newBlocks: BlockNode[] = [];
232
287
  for (let i = 0; i < this.#blocks.length; i++) {
233
- if (this.#blocks[i].contains(range.start.node))
234
- foundStart = true;
235
- if (((containsStart && foundStart) || !containsStart) &&
236
- ((containsEnd && !foundEnd) || !containsEnd)) {
288
+ if (this.#blocks[i].contains(range.start.node)) foundStart = true;
289
+ if (
290
+ ((containsStart && foundStart) || !containsStart) &&
291
+ ((containsEnd && !foundEnd) || !containsEnd)
292
+ ) {
237
293
  const newBlock = this.#blocks[i].withContentInRange(range);
238
- if (newBlock === undefined)
239
- return;
294
+ if (newBlock === undefined) return;
240
295
  newBlocks.push(newBlock);
241
296
  }
242
- if (this.#blocks[i].contains(range.end.node))
243
- foundEnd = true;
297
+ if (this.#blocks[i].contains(range.end.node)) foundEnd = true;
244
298
  }
245
- return this.create(newBlocks);
299
+
300
+ return this.create(newBlocks) as this;
246
301
  }
247
- withBlockInserted(anchor, block, before) {
302
+
303
+ withBlockInserted(
304
+ anchor: BlockNode,
305
+ block: BlockNode,
306
+ before: boolean
307
+ ): BlocksNode | undefined {
248
308
  const index = this.#blocks.indexOf(anchor);
249
- if (index < 0)
250
- return;
309
+ if (index < 0) return;
251
310
  const newBlocks = this.#blocks.slice();
252
311
  newBlocks.splice(index + (before ? 0 : 1), 0, block);
253
312
  return this.create(newBlocks);
254
313
  }
255
- withBlockAppended(block) {
314
+
315
+ withBlockAppended(block: BlockNode): BlocksNode {
256
316
  let newBlocks = this.#blocks.slice();
257
317
  newBlocks.push(block);
258
318
  return this.create(newBlocks);
259
319
  }
260
- withoutBlock(block) {
320
+
321
+ withoutBlock(block: BlockNode): BlocksNode {
261
322
  return this.create(this.#blocks.filter((n) => n !== block));
262
323
  }
263
- withBlockInsertedBefore(anchor, block) {
324
+
325
+ withBlockInsertedBefore(anchor: BlockNode, block: BlockNode) {
264
326
  return this.withBlockInserted(anchor, block, true);
265
327
  }
266
- withBlockInsertedAfter(anchor, block) {
328
+
329
+ withBlockInsertedAfter(anchor: BlockNode, block: BlockNode) {
267
330
  return this.withBlockInserted(anchor, block, false);
268
331
  }
269
- withSelectionSplit(range) {
332
+
333
+ withSelectionSplit(range: CaretRange): Edit {
270
334
  let caret = range.start;
271
- let blocks = this;
335
+ let blocks: BlocksNode = this;
336
+
272
337
  // If there's a selection, remove it before inserting.
273
- if (range.start.node !== range.end.node ||
274
- range.start.index !== range.end.index) {
338
+ if (
339
+ range.start.node !== range.end.node ||
340
+ range.start.index !== range.end.index
341
+ ) {
275
342
  const edit = blocks.withoutRange(range);
276
343
  if (edit === undefined || !(edit.root instanceof BlocksNode))
277
344
  return;
278
345
  blocks = edit.root;
279
346
  caret = edit.range.start;
280
347
  }
348
+
281
349
  // Find what block the caret is in.
282
- const block = caret.node.getClosestParentOfType(blocks, BlockNode);
283
- if (block === undefined)
284
- return;
350
+ const block = caret.node.getClosestParentOfType<BlockNode>(
351
+ blocks,
352
+ BlockNode
353
+ );
354
+
355
+ if (block === undefined) return;
356
+
285
357
  // If it's a paragraph, split it.
286
358
  if (block instanceof ParagraphNode) {
287
359
  const paragraph = block;
288
360
  // Split the paragraph in two.
289
361
  const split = paragraph.split(caret);
290
- if (split === undefined)
291
- return;
362
+ if (split === undefined) return;
292
363
  const [first, last] = split;
293
364
  const newCaret = last.getFirstCaret();
294
- if (newCaret === undefined)
295
- return;
365
+ if (newCaret === undefined) return;
366
+
296
367
  // Find the paragraph's blocks node.
297
368
  const paragraphBlocks = paragraph.getParent(blocks);
298
- if (!(paragraphBlocks instanceof BlocksNode))
299
- return;
369
+ if (!(paragraphBlocks instanceof BlocksNode)) return;
370
+
300
371
  const newBlocks = paragraphBlocks
301
372
  .withBlockInsertedBefore(paragraph, first)
302
373
  ?.withBlockInsertedAfter(paragraph, last)
303
374
  ?.withoutBlock(paragraph);
304
- if (newBlocks === undefined)
305
- return;
375
+ if (newBlocks === undefined) return;
376
+
306
377
  // Replace the paragraphs blocks in these blocks.
307
378
  const newRoot = blocks.withNodeReplaced(paragraphBlocks, newBlocks);
308
- if (newRoot === undefined)
309
- return;
379
+ if (newRoot === undefined) return;
380
+
310
381
  return { root: newRoot, range: { start: newCaret, end: newCaret } };
311
382
  }
312
383
  // If any other kind of block, and an empty paragraph after the block.
313
384
  else {
314
385
  // Find the paragraph's blocks node.
315
386
  const container = block.getParent(blocks);
316
- if (!(container instanceof BlocksNode))
317
- return;
387
+ if (!(container instanceof BlocksNode)) return;
388
+
318
389
  const newParagraph = new ParagraphNode();
319
390
  const newCaret = newParagraph.getFirstCaret();
320
- if (newCaret === undefined)
321
- return;
322
- const newBlocks = container.withBlockInsertedAfter(block, newParagraph);
323
- if (newBlocks === undefined)
324
- return;
391
+ if (newCaret === undefined) return;
392
+
393
+ const newBlocks = container.withBlockInsertedAfter(
394
+ block,
395
+ newParagraph
396
+ );
397
+ if (newBlocks === undefined) return;
398
+
325
399
  // Replace the paragraphs blocks in these blocks.
326
400
  const newRoot = blocks.withNodeReplaced(container, newBlocks);
327
- if (newRoot === undefined)
328
- return;
401
+ if (newRoot === undefined) return;
402
+
329
403
  return { root: newRoot, range: { start: newCaret, end: newCaret } };
330
404
  }
331
405
  }
332
- withoutRange(range) {
406
+
407
+ withoutRange(range: CaretRange): Edit {
333
408
  return this.withRangeFormatted(range, undefined);
334
409
  }
335
- withoutAtom(atom) {
410
+
411
+ withoutAtom(atom: AtomNode<any>): Edit {
336
412
  // Find the format that contains the atom.
337
- const format = atom.getFarthestParentMatching(this, (p) => p instanceof FormatNode);
338
- if (format === undefined)
339
- return;
413
+ const format = atom.getFarthestParentMatching(
414
+ this,
415
+ (p) => p instanceof FormatNode
416
+ ) as FormatNode | undefined;
417
+ if (format === undefined) return;
418
+
340
419
  // Find the text or atom node to the left and right of the atom to determine the new caret position.
341
420
  const previous = format.getPreviousTextOrAtom(atom);
342
421
  const next = format.getNextTextOrAtom(atom);
343
422
  const newText = new TextNode();
423
+
344
424
  // If the format is otherwise empty, replace the atom with an empty text node
345
- const newFormat = previous === undefined && next == undefined
346
- ? format.withSegmentReplaced(atom, newText)
347
- : format.withoutSegment(atom);
425
+ const newFormat =
426
+ previous === undefined && next == undefined
427
+ ? format.withSegmentReplaced(atom, newText)
428
+ : format.withoutSegment(atom);
429
+
348
430
  // New caret is left, or right, or empty text.
349
431
  const newCaret = {
350
432
  node: previous ? previous : next ? next : newText,
351
- index: previous && previous instanceof TextNode
352
- ? previous.getLength()
353
- : 0,
433
+ index:
434
+ previous && previous instanceof TextNode
435
+ ? previous.getLength()
436
+ : 0,
354
437
  };
438
+
355
439
  // No format? Fail.
356
- if (newFormat === undefined)
357
- return;
440
+ if (newFormat === undefined) return;
441
+
358
442
  // Create a new chapter with the revised format.
359
443
  const newRoot = this.withNodeReplaced(format, newFormat);
360
- if (newRoot === undefined)
361
- return;
444
+ if (newRoot === undefined) return;
445
+
362
446
  // Return the edited tree!
363
447
  return { root: newRoot, range: { start: newCaret, end: newCaret } };
364
448
  }
365
- withRangeFormatted(range, format) {
449
+
450
+ withRangeFormatted(range: CaretRange, format: Format | undefined): Edit {
366
451
  // Only works on text nodes, atom nodes, and metadata nodes.
367
- if (!(range.start.node instanceof TextNode ||
368
- range.start.node instanceof AtomNode) ||
369
- !(range.end.node instanceof TextNode ||
370
- range.end.node instanceof AtomNode))
452
+ if (
453
+ !(
454
+ range.start.node instanceof TextNode ||
455
+ range.start.node instanceof AtomNode
456
+ ) ||
457
+ !(
458
+ range.end.node instanceof TextNode ||
459
+ range.end.node instanceof AtomNode
460
+ )
461
+ )
371
462
  return;
463
+
372
464
  // Have we selected a single atom node for removal? Remove it and place the caret in the adjacent word.
373
- if (format === undefined &&
465
+ if (
466
+ format === undefined &&
374
467
  range.start.node instanceof AtomNode &&
375
468
  range.end.node instanceof AtomNode &&
376
- range.start.node === range.end.node) {
469
+ range.start.node === range.end.node
470
+ ) {
377
471
  return this.withoutAtom(range.start.node);
378
472
  }
473
+
379
474
  // Preserve the original range, since there are a few cases where we adjust it.
380
475
  let adjustedRange = range;
476
+
381
477
  // If this a single point in format, and we're clearing formatting, adjust the selection to the whole format.
382
- if (format === '' &&
478
+ if (
479
+ format === '' &&
383
480
  range.start.node === range.end.node &&
384
- range.start.index === range.end.index) {
481
+ range.start.index === range.end.index
482
+ ) {
385
483
  const selection = range.start.node
386
484
  .getFormatRoot(this)
387
485
  ?.getSelection();
388
- if (selection === undefined)
389
- return;
486
+ if (selection === undefined) return;
390
487
  adjustedRange = selection;
391
488
  }
489
+
392
490
  // Sort the range if it's out of order, since the algorithm below assumes that it's ordered.
393
491
  const sortedRange = this.sortRange(adjustedRange);
492
+
394
493
  // Remember the start and end text index so we can map it back after editing.
395
494
  const startTextIndex = caretToIndex(this, sortedRange.start);
396
495
  const endTextIndex = caretToIndex(this, sortedRange.end);
397
- if (startTextIndex === undefined || endTextIndex === undefined)
398
- return;
496
+
497
+ if (startTextIndex === undefined || endTextIndex === undefined) return;
498
+
399
499
  // Ask all of the blocks to format themselves, replacing them as we do.
400
- let newBlocks = this;
401
- let formattedBlocks = [];
500
+ let newBlocks: this | undefined = this;
501
+ let formattedBlocks: BlockNode[] = [];
402
502
  let inRange = false;
403
503
  for (let i = 0; i < this.#blocks.length; i++) {
404
504
  const block = this.#blocks[i];
@@ -413,243 +513,321 @@ export default class BlocksNode extends BlockNode {
413
513
  const blockEnd = containsEnd
414
514
  ? sortedRange.end
415
515
  : block.getLastCaret();
416
- if (blockStart === undefined || blockEnd === undefined)
417
- return;
516
+ if (blockStart === undefined || blockEnd === undefined) return;
517
+
418
518
  // If we're just going to delete it, then don't bother formatting it.
419
519
  if (!containsStart && !containsEnd && format === undefined) {
420
520
  newBlocks = newBlocks.withChildReplaced(block, undefined);
421
- if (newBlocks === undefined)
422
- return;
521
+ if (newBlocks === undefined) return;
423
522
  }
424
523
  // Otherwise, format it.
425
524
  else {
426
- const edit = block.withRangeFormatted({ start: blockStart, end: blockEnd }, format);
525
+ const edit = block.withRangeFormatted(
526
+ { start: blockStart, end: blockEnd },
527
+ format
528
+ );
529
+
427
530
  // If we failed to format it, remove it.
428
- if (edit === undefined)
429
- return;
531
+ if (edit === undefined) return;
532
+
430
533
  // If we succeeded in formatting it, replace it, otherwise skip it.
431
- let newBlock = edit.root;
534
+ let newBlock = edit.root as BlockNode;
535
+
432
536
  // Finally, if the selection started before a list node, and we were deleting in the middle of some item,
433
537
  // convert that item (which should now be the first item in the list) into a paragraph, since it's bullet was deleted, conceptually.
434
- if (!newBlock.isEmpty() &&
538
+ if (
539
+ !newBlock.isEmpty() &&
435
540
  newBlock instanceof ListNode &&
436
541
  !containsStart &&
437
- containsEnd) {
542
+ containsEnd
543
+ ) {
438
544
  const firstItem = newBlock.getItem(0);
439
545
  if (firstItem instanceof FormatNode) {
440
546
  // Remove the item from the list.
441
- const newListWithoutItem = newBlock.withoutItem(firstItem);
442
- if (newListWithoutItem === undefined)
443
- return;
547
+ const newListWithoutItem =
548
+ newBlock.withoutItem(firstItem);
549
+ if (newListWithoutItem === undefined) return;
444
550
  newBlock = newListWithoutItem;
551
+
445
552
  // Replace the revised list
446
- const blocksWithList = newBlocks
447
- .withChildReplaced(block, newBlock)
448
- ?.withBlockInsertedBefore(newBlock, new ParagraphNode(0, firstItem));
449
- if (blocksWithList === undefined)
450
- return;
451
- newBlocks = blocksWithList;
553
+ const blocksWithList: BlocksNode | undefined =
554
+ newBlocks
555
+ .withChildReplaced(block, newBlock)
556
+ ?.withBlockInsertedBefore(
557
+ newBlock,
558
+ new ParagraphNode(0, firstItem)
559
+ );
560
+
561
+ if (blocksWithList === undefined) return;
562
+ newBlocks = blocksWithList as this;
452
563
  }
453
564
  }
454
565
  // Otherwise, just handle the new block in isolation.
455
566
  else {
456
567
  // Remember non-empty revised blocks so we can merge things after.
457
- if (!newBlock.isEmpty())
458
- formattedBlocks.push(newBlock);
568
+ if (!newBlock.isEmpty()) formattedBlocks.push(newBlock);
569
+
459
570
  // Replace the block, or delete it if it's empty.
460
- newBlocks = newBlocks.withChildReplaced(block, newBlock.isEmpty() ? undefined : newBlock);
571
+ newBlocks = newBlocks.withChildReplaced(
572
+ block,
573
+ newBlock.isEmpty() ? undefined : newBlock
574
+ );
461
575
  }
576
+
462
577
  // If we failed to replace it, bail.
463
- if (newBlocks === undefined)
464
- return;
578
+ if (newBlocks === undefined) return;
465
579
  }
466
580
  }
467
- if (block.contains(sortedRange.end.node))
468
- inRange = false;
581
+ if (block.contains(sortedRange.end.node)) inRange = false;
469
582
  }
583
+
470
584
  // If we're deleting and there are two non-empty paragraphs left, merge them.
471
585
  if (format === undefined && formattedBlocks.length >= 2) {
472
586
  const first = formattedBlocks[0];
473
587
  const last = formattedBlocks[formattedBlocks.length - 1];
474
- if (first instanceof ParagraphNode &&
475
- last instanceof ParagraphNode) {
588
+ if (
589
+ first instanceof ParagraphNode &&
590
+ last instanceof ParagraphNode
591
+ ) {
476
592
  // Replace the first paragraph with the second merged.
477
- newBlocks = newBlocks.withNodeReplaced(first, first.withContent(first.getFormat().withSegmentsAppended(last.getFormat())));
478
- if (newBlocks === undefined)
479
- return;
593
+ newBlocks = newBlocks.withNodeReplaced(
594
+ first,
595
+ first.withContent(
596
+ first.getFormat().withSegmentsAppended(last.getFormat())
597
+ )
598
+ );
599
+ if (newBlocks === undefined) return;
480
600
  // Remove the last paragraph.
481
601
  newBlocks = newBlocks.withNodeReplaced(last, undefined);
482
- if (newBlocks === undefined)
483
- return;
602
+ if (newBlocks === undefined) return;
484
603
  }
485
604
  }
605
+
486
606
  const startCaret = indexToCaret(newBlocks, startTextIndex);
487
607
  // If we deleted, then the end caret should be the same as the start.
488
- const endCaret = format === undefined
489
- ? startCaret
490
- : indexToCaret(newBlocks, endTextIndex);
491
- if (startCaret === undefined || endCaret === undefined)
492
- return;
608
+ const endCaret =
609
+ format === undefined
610
+ ? startCaret
611
+ : indexToCaret(newBlocks, endTextIndex);
612
+
613
+ if (startCaret === undefined || endCaret === undefined) return;
614
+
493
615
  // Return the new root and the start and end of the range.
494
616
  return { root: newBlocks, range: { start: startCaret, end: endCaret } };
495
617
  }
496
- withSegmentAtSelection(range, nodeCreator) {
497
- let blocks = this;
618
+
619
+ withSegmentAtSelection(
620
+ range: CaretRange,
621
+ nodeCreator: (text: string) => FormatNodeSegmentType
622
+ ): Edit {
623
+ let blocks: BlocksNode = this;
498
624
  let caret = range.start;
625
+
499
626
  // Only works at a text node.
500
- if (!(caret.node instanceof TextNode))
501
- return;
627
+ if (!(caret.node instanceof TextNode)) return;
628
+
502
629
  // Get the nearest FormatNode parent of the selected text. Bail on fail.
503
- const formatted = caret.node.getClosestParentOfType(blocks, FormatNode);
504
- if (formatted === undefined)
505
- return;
630
+ const formatted = caret.node.getClosestParentOfType<FormatNode>(
631
+ blocks,
632
+ FormatNode
633
+ );
634
+ if (formatted === undefined) return;
635
+
506
636
  // Update the format and replace it in the blocks.
507
637
  const edit = formatted.withSegmentAtSelection(range, nodeCreator);
508
- if (edit === undefined || !(edit.root instanceof FormatNode))
509
- return;
638
+ if (edit === undefined || !(edit.root instanceof FormatNode)) return;
510
639
  const newBlocks = blocks.withNodeReplaced(formatted, edit.root);
511
- if (newBlocks === undefined)
512
- return;
640
+ if (newBlocks === undefined) return;
513
641
  return { root: newBlocks, range: edit.range };
514
642
  }
515
- withParagraphsAsLists(range, numbered) {
643
+
644
+ withParagraphsAsLists(
645
+ range: CaretRange,
646
+ numbered: boolean
647
+ ): BlocksNode | undefined {
516
648
  const sortedRange = this.sortRange(range);
517
649
  const blocksInRange = this.getBlocksInRange(sortedRange);
518
- if (blocksInRange === undefined)
519
- return;
650
+ if (blocksInRange === undefined) return;
520
651
  // Only format if every block selected is a paragraph node. Non-paragraphs can't be in lists.
521
- if (!blocksInRange.every((b) => b instanceof ParagraphNode))
522
- return;
652
+ if (!blocksInRange.every((b) => b instanceof ParagraphNode)) return;
653
+
523
654
  // Split the blocks into groups with shared parents.
524
- const sequences = [[]];
525
- let previous = undefined;
655
+ const sequences: ParagraphNode[][] = [[]];
656
+ let previous: ParagraphNode | undefined = undefined;
526
657
  blocksInRange.forEach((block) => {
527
- if (previous === undefined ||
528
- block.getParent(this) === previous.getParent(this))
529
- sequences[sequences.length - 1].push(block);
658
+ if (
659
+ previous === undefined ||
660
+ block.getParent(this) === previous.getParent(this)
661
+ )
662
+ sequences[sequences.length - 1].push(block as ParagraphNode);
530
663
  else {
531
- sequences.push([block]);
664
+ sequences.push([block as ParagraphNode]);
532
665
  }
533
- previous = block;
666
+ previous = block as ParagraphNode;
534
667
  });
668
+
535
669
  // For each sequence, create a list of all of the paragraphs in it.
536
- let newBlocks = this;
670
+ let newBlocks: BlocksNode = this;
537
671
  for (let i = 0; i < sequences.length; i++) {
538
672
  const sequence = sequences[i];
539
673
  // Make the new list
540
- const newList = new ListNode(sequence.map((p) => p.getFormat()), numbered);
674
+ const newList = new ListNode(
675
+ sequence.map((p) => p.getFormat()),
676
+ numbered
677
+ );
541
678
  // Insert the new list
542
- const blocksParent = sequence[0].getParent(newBlocks);
543
- if (blocksParent === undefined)
544
- return;
545
- let newSequenceBlocks = blocksParent.withBlockInsertedBefore(sequence[0], newList);
679
+ const blocksParent: BlocksNode | undefined = sequence[0].getParent(
680
+ newBlocks
681
+ ) as BlocksNode;
682
+ if (blocksParent === undefined) return;
683
+ let newSequenceBlocks = blocksParent.withBlockInsertedBefore(
684
+ sequence[0],
685
+ newList
686
+ );
546
687
  // Remove all of the old paragraphs
547
688
  for (let j = 0; j < sequence.length; j++) {
548
- newSequenceBlocks = newSequenceBlocks?.withoutBlock(sequence[j]);
549
- if (newSequenceBlocks === undefined)
550
- return;
689
+ newSequenceBlocks = newSequenceBlocks?.withoutBlock(
690
+ sequence[j]
691
+ );
692
+ if (newSequenceBlocks === undefined) return;
551
693
  }
552
694
  // Replace the blocks parent with the new blocks.
553
- const newRoot = newBlocks.withNodeReplaced(blocksParent, newSequenceBlocks);
695
+ const newRoot = newBlocks.withNodeReplaced(
696
+ blocksParent,
697
+ newSequenceBlocks
698
+ );
554
699
  if (newRoot === undefined || !(newRoot instanceof BlocksNode))
555
700
  return;
556
701
  newBlocks = newRoot;
557
702
  }
703
+
558
704
  return newBlocks;
559
705
  }
560
- withListsAsParagraphs(range) {
561
- let newBlocks = this;
706
+
707
+ withListsAsParagraphs(range: CaretRange): BlocksNode | undefined {
708
+ let newBlocks: BlocksNode = this;
562
709
  const sortedRange = this.sortRange(range);
563
710
  const blocksInRange = this.getBlocksInRange(sortedRange);
564
- if (blocksInRange === undefined)
565
- return;
711
+ if (blocksInRange === undefined) return;
566
712
  // Convert all of the lists into their root lists, since we only convert entire lists, not parts of lists.
567
713
  const listsInRange = blocksInRange.filter((l) => l instanceof ListNode);
568
714
  const listRootsInRange = [
569
- ...new Set(listsInRange
570
- .map((l) => l.getParent(this) instanceof ListNode
571
- ? l.getFarthestParentMatching(this, (m) => m instanceof ListNode)
572
- : l)
573
- .filter((n) => n !== undefined)),
574
- ];
575
- if (listRootsInRange.length === 0)
576
- return undefined;
715
+ ...new Set(
716
+ listsInRange
717
+ .map((l) =>
718
+ l.getParent(this) instanceof ListNode
719
+ ? l.getFarthestParentMatching(
720
+ this,
721
+ (m) => m instanceof ListNode
722
+ )
723
+ : l
724
+ )
725
+ .filter((n) => n !== undefined)
726
+ ),
727
+ ] as ListNode[];
728
+ if (listRootsInRange.length === 0) return undefined;
729
+
577
730
  // For each list in the range, convert all items in range to paragraphs.
578
731
  for (let i = 0; i < listRootsInRange.length; i++) {
579
732
  const list = listRootsInRange[i];
580
733
  const paragraphs = list
581
734
  .getNodes()
582
735
  .filter((n) => n instanceof FormatNode)
583
- .map((f) => new ParagraphNode(0, f));
736
+ .map(
737
+ (f) => new ParagraphNode(0, f as FormatNode)
738
+ ) as ParagraphNode[];
584
739
  // Insert in reverse order to keep them in order, since the list is our anchor.
585
740
  for (let j = paragraphs.length - 1; j >= 0; j--) {
586
741
  const paragraph = paragraphs[j];
587
- const insertedBlocks = newBlocks.withBlockInsertedAfter(list, paragraph);
588
- if (insertedBlocks === undefined)
589
- return;
742
+ const insertedBlocks = newBlocks.withBlockInsertedAfter(
743
+ list,
744
+ paragraph
745
+ );
746
+ if (insertedBlocks === undefined) return;
590
747
  newBlocks = insertedBlocks;
591
748
  }
592
749
  const withoutList = newBlocks.withoutBlock(list);
593
- if (withoutList === undefined)
594
- return;
750
+ if (withoutList === undefined) return;
595
751
  newBlocks = withoutList;
596
752
  }
753
+
597
754
  return newBlocks;
598
755
  }
599
- withListsIndented(range, indent) {
600
- let newBlocks = this;
756
+
757
+ withListsIndented(
758
+ range: CaretRange,
759
+ indent: boolean
760
+ ): BlocksNode | undefined {
761
+ let newBlocks: BlocksNode = this;
762
+
601
763
  // Find all of the formats in list nodes in the range and indent them.
602
- const ancestor = range.start.node.getCommonAncestor(newBlocks, range.end.node);
764
+ const ancestor = range.start.node.getCommonAncestor(
765
+ newBlocks,
766
+ range.end.node
767
+ );
603
768
  const nodes = ancestor?.getNodes();
604
- const formats = nodes?.filter((n) => n instanceof FormatNode &&
605
- newBlocks.getParentOf(n) instanceof ListNode);
769
+ const formats = nodes?.filter(
770
+ (n) =>
771
+ n instanceof FormatNode &&
772
+ newBlocks.getParentOf(n) instanceof ListNode
773
+ ) as FormatNode[];
606
774
  const startIndex = nodes?.indexOf(range.start.node);
607
775
  const endIndex = nodes?.indexOf(range.end.node);
608
- if (formats === undefined ||
776
+
777
+ if (
778
+ formats === undefined ||
609
779
  startIndex === undefined ||
610
- endIndex === undefined)
780
+ endIndex === undefined
781
+ )
611
782
  return;
783
+
612
784
  // Loop through the formats in the range and indent or dedent them, constructing a new root.
613
785
  const first = startIndex < endIndex ? range.start.node : range.end.node;
614
786
  const last = startIndex < endIndex ? range.end.node : range.start.node;
615
787
  let inside = false;
616
788
  for (let i = 0; i < formats.length; i++) {
617
789
  const format = formats[i];
618
- if (first.hasAncestor(newBlocks, format))
619
- inside = true;
790
+ if (first.hasAncestor(newBlocks, format)) inside = true;
620
791
  // If we're in the selection and the format is in a list, restructure it's list to indent/unindent it.
621
792
  if (inside) {
622
793
  // Find the root list node of the format.
623
- const list = format.getFarthestParentMatching(newBlocks, (n) => n instanceof ListNode);
794
+ const list = format.getFarthestParentMatching(
795
+ newBlocks,
796
+ (n) => n instanceof ListNode
797
+ );
624
798
  if (list instanceof ListNode) {
625
799
  const newList = indent
626
800
  ? list.withItemIndented(format)
627
801
  : list.withItemUnindented(format);
628
- if (newList === undefined)
629
- return;
630
- const blocksWithIndent = newBlocks.withChildReplaced(list, newList);
631
- if (blocksWithIndent === undefined)
632
- return;
802
+ if (newList === undefined) return;
803
+ const blocksWithIndent = newBlocks.withChildReplaced(
804
+ list,
805
+ newList
806
+ );
807
+ if (blocksWithIndent === undefined) return;
633
808
  newBlocks = blocksWithIndent;
634
809
  }
635
810
  }
636
- if (last.hasAncestor(newBlocks, format))
637
- inside = false;
811
+ if (last.hasAncestor(newBlocks, format)) inside = false;
638
812
  }
813
+
639
814
  // We shouldn't need to update the range because we haven't modified the text nodes, just their position.
640
815
  return newBlocks;
641
816
  }
817
+
642
818
  // This accounts for adjascent lists that end up with the same style.
643
- withListAsStyle(list, numbered) {
819
+ withListAsStyle(list: ListNode, numbered: boolean): BlocksNode | undefined {
644
820
  return this.withNodeReplaced(list, list.withStyle(numbered));
645
821
  }
822
+
646
823
  // Removes the character before or after the current caret
647
- withoutAdjacentContent(caret, next) {
824
+ withoutAdjacentContent(caret: Caret, next: boolean): Edit | undefined {
648
825
  // Confirm that this caret is in this tree. Bail on fail.
649
- if (!this.contains(caret.node))
650
- return;
826
+ if (!this.contains(caret.node)) return;
827
+
651
828
  // Find the parents of the node.
652
829
  const parents = this.getParentsOf(caret.node);
830
+
653
831
  // Iterate through the ancestors, seeing if they'd like to handle the request for a deletion.
654
832
  while (parents && parents.length > 0) {
655
833
  const parent = parents.pop();
@@ -658,9 +836,11 @@ export default class BlocksNode extends BlockNode {
658
836
  const editedFormat = parent.withoutCharacter(caret, next);
659
837
  // If it failed, let a parent try. Otherwise, just update the format.
660
838
  if (editedFormat !== undefined) {
661
- const newBlocks = this.withNodeReplaced(parent, editedFormat.root);
662
- if (newBlocks === undefined)
663
- return;
839
+ const newBlocks = this.withNodeReplaced(
840
+ parent,
841
+ editedFormat.root
842
+ );
843
+ if (newBlocks === undefined) return;
664
844
  return { root: newBlocks, range: editedFormat.range };
665
845
  }
666
846
  }
@@ -673,12 +853,16 @@ export default class BlocksNode extends BlockNode {
673
853
  index: caret.index + (next ? 1 : -1),
674
854
  },
675
855
  });
676
- if (revisedCode === undefined)
677
- return;
678
- const revisedCodeNode = parent.withChildReplaced(parent.getCodeNode(), revisedCode);
679
- if (revisedCodeNode === undefined)
680
- return;
681
- const newBlocks = this.withNodeReplaced(parent, revisedCodeNode);
856
+ if (revisedCode === undefined) return;
857
+ const revisedCodeNode = parent.withChildReplaced(
858
+ parent.getCodeNode(),
859
+ revisedCode
860
+ );
861
+ if (revisedCodeNode === undefined) return;
862
+ const newBlocks = this.withNodeReplaced(
863
+ parent,
864
+ revisedCodeNode
865
+ );
682
866
  const newCaret = {
683
867
  node: revisedCode,
684
868
  index: Math.max(0, caret.index + (next ? 0 : -1)),
@@ -686,37 +870,38 @@ export default class BlocksNode extends BlockNode {
686
870
  return newBlocks === undefined
687
871
  ? undefined
688
872
  : {
689
- root: newBlocks,
690
- range: { start: newCaret, end: newCaret },
691
- };
873
+ root: newBlocks,
874
+ range: { start: newCaret, end: newCaret },
875
+ };
692
876
  }
693
877
  // If it's a format in the list node, merge with the previous list item
694
878
  else if (parent instanceof ListNode) {
695
879
  const index = parent.getItemContaining(caret);
696
- if (index === undefined)
697
- break;
880
+ if (index === undefined) break;
698
881
  // Merge the item backwards.
699
882
  if (parent.getLength() > 1) {
700
883
  const edit = parent.withItemMergedBackwards(index);
701
- if (edit === undefined)
702
- return;
884
+ if (edit === undefined) return;
703
885
  const [newList, newCaret] = edit;
704
886
  const newBlocks = this.withNodeReplaced(parent, newList);
705
- if (newBlocks === undefined)
706
- return;
887
+ if (newBlocks === undefined) return;
707
888
  return {
708
889
  root: newBlocks,
709
890
  range: { start: newCaret, end: newCaret },
710
891
  };
711
892
  }
712
893
  // Convert the remaining list item into a paragraph.
713
- else if (parents.length > 0 &&
714
- parents[parents.length - 1] instanceof BlocksNode) {
894
+ else if (
895
+ parents.length > 0 &&
896
+ parents[parents.length - 1] instanceof BlocksNode
897
+ ) {
715
898
  const item = parent.getItem(0);
716
- if (!(item instanceof FormatNode))
717
- return;
899
+ if (!(item instanceof FormatNode)) return;
718
900
  const newParagraph = new ParagraphNode(0, item);
719
- const newBlocks = this.withNodeReplaced(parent, newParagraph);
901
+ const newBlocks = this.withNodeReplaced(
902
+ parent,
903
+ newParagraph
904
+ );
720
905
  const newCaret = newParagraph.getFirstCaret();
721
906
  if (newBlocks === undefined || newCaret === undefined)
722
907
  return;
@@ -731,41 +916,49 @@ export default class BlocksNode extends BlockNode {
731
916
  const firstCaret = next
732
917
  ? parent.getLastCaret()
733
918
  : parent.getFirstCaret();
734
- if (firstCaret === undefined)
735
- return;
736
- const blocks = parents[parents.length - 1];
919
+ if (firstCaret === undefined) return;
920
+ const blocks = parents[parents.length - 1] as BlocksNode;
737
921
  const adjacentBlock = next
738
922
  ? blocks.getBlockAfter(parent)
739
923
  : blocks.getBlockBefore(parent);
740
924
  // Is this the first/last caret of the paragraph and there's an adjascent block? Try deleting something.
741
- if (firstCaret.node === caret.node &&
925
+ if (
926
+ firstCaret.node === caret.node &&
742
927
  firstCaret.index === caret.index &&
743
- adjacentBlock !== undefined) {
928
+ adjacentBlock !== undefined
929
+ ) {
744
930
  // If the block adjacent this paragraph is a paragraph, merge the paragraph into the adjacent paragraph.
745
931
  if (adjacentBlock instanceof ParagraphNode) {
746
932
  const currentCaret = next
747
933
  ? parent.getLastCaret()
748
934
  : adjacentBlock.getLastCaret();
749
- if (currentCaret === undefined)
750
- return;
935
+ if (currentCaret === undefined) return;
751
936
  const textIndex = next
752
937
  ? caretToIndex(parent.getFormat(), currentCaret)
753
- : caretToIndex(adjacentBlock.getFormat(), currentCaret);
754
- if (textIndex === undefined)
755
- return;
938
+ : caretToIndex(
939
+ adjacentBlock.getFormat(),
940
+ currentCaret
941
+ );
942
+ if (textIndex === undefined) return;
756
943
  const mergedParagraph = next
757
944
  ? adjacentBlock.withParagraphPrepended(parent)
758
945
  : adjacentBlock.withParagraphAppended(parent);
759
- if (mergedParagraph === undefined)
760
- return;
761
- const newBlocks = this.withNodeReplaced(blocks, blocks
762
- .withChildReplaced(adjacentBlock, mergedParagraph)
763
- ?.withoutBlock(parent));
764
- if (newBlocks === undefined)
765
- return;
766
- const newCaret = indexToCaret(mergedParagraph.getFormat(), textIndex);
767
- if (newCaret === undefined)
768
- return;
946
+ if (mergedParagraph === undefined) return;
947
+ const newBlocks = this.withNodeReplaced(
948
+ blocks,
949
+ blocks
950
+ .withChildReplaced(
951
+ adjacentBlock,
952
+ mergedParagraph
953
+ )
954
+ ?.withoutBlock(parent)
955
+ );
956
+ if (newBlocks === undefined) return;
957
+ const newCaret = indexToCaret(
958
+ mergedParagraph.getFormat(),
959
+ textIndex
960
+ );
961
+ if (newCaret === undefined) return;
769
962
  return {
770
963
  root: newBlocks,
771
964
  range: { start: newCaret, end: newCaret },
@@ -777,14 +970,15 @@ export default class BlocksNode extends BlockNode {
777
970
  if (next) {
778
971
  const format = adjacentBlock.getFirstItem();
779
972
  if (format !== undefined) {
780
- const newParagraph = parent.withContent(parent
781
- .getFormat()
782
- .withSegmentsAppended(format));
973
+ const newParagraph = parent.withContent(
974
+ parent
975
+ .getFormat()
976
+ .withSegmentsAppended(format)
977
+ );
783
978
  const newBlocks = blocks
784
979
  .withNodeReplaced(parent, newParagraph)
785
980
  ?.withNodeReplaced(format, undefined);
786
- if (newBlocks === undefined)
787
- return;
981
+ if (newBlocks === undefined) return;
788
982
  return {
789
983
  root: newBlocks,
790
984
  range: { start: caret, end: caret },
@@ -796,15 +990,19 @@ export default class BlocksNode extends BlockNode {
796
990
  else {
797
991
  const format = adjacentBlock.getLastItem();
798
992
  if (format !== undefined) {
799
- const newFormat = format.withSegmentAppended(parent.getFormat());
993
+ const newFormat = format.withSegmentAppended(
994
+ parent.getFormat()
995
+ );
800
996
  const newBlocks = blocks
801
997
  .withNodeReplaced(format, newFormat)
802
998
  ?.withoutBlock(parent);
803
999
  const newCaret = parent
804
1000
  .getFormat()
805
1001
  .getFirstCaret();
806
- if (newBlocks === undefined ||
807
- newCaret === undefined)
1002
+ if (
1003
+ newBlocks === undefined ||
1004
+ newCaret === undefined
1005
+ )
808
1006
  return;
809
1007
  return {
810
1008
  root: newBlocks,
@@ -815,56 +1013,62 @@ export default class BlocksNode extends BlockNode {
815
1013
  }
816
1014
  // Otherwise, return a root without the previous block, deleting it.
817
1015
  else {
818
- const newBlocks = this.withNodeReplaced(blocks, blocks.withoutBlock(adjacentBlock));
1016
+ const newBlocks = this.withNodeReplaced(
1017
+ blocks,
1018
+ blocks.withoutBlock(adjacentBlock)
1019
+ );
819
1020
  return newBlocks === undefined
820
1021
  ? undefined
821
1022
  : {
822
- root: newBlocks,
823
- range: { start: caret, end: caret },
824
- };
1023
+ root: newBlocks,
1024
+ range: { start: caret, end: caret },
1025
+ };
825
1026
  }
826
1027
  }
827
1028
  }
828
1029
  }
829
1030
  // No parent could handle it. Fail!
830
1031
  }
831
- withNodeInserted(caret, node) {
1032
+
1033
+ withNodeInserted(caret: Caret, node: Node): Edit {
832
1034
  // The caret's node must be in this tree.
833
- if (!this.contains(caret.node))
834
- return;
1035
+ if (!this.contains(caret.node)) return;
835
1036
  // Get the blocks to insert.
836
- const blocks = node instanceof BlocksNode
837
- ? node.getBlocks()
838
- : node instanceof BlockNode
1037
+ const blocks =
1038
+ node instanceof BlocksNode
1039
+ ? node.getBlocks()
1040
+ : node instanceof BlockNode
839
1041
  ? [node]
840
1042
  : [];
841
1043
  // If there are none, do nothing.
842
- if (blocks.length === 0)
843
- return;
1044
+ if (blocks.length === 0) return;
844
1045
  // Find the block in this blocks node that contains the caret.
845
1046
  const anchor = this.#blocks.find((b) => b.contains(caret.node));
846
- if (anchor === undefined)
847
- return;
1047
+ if (anchor === undefined) return;
1048
+
848
1049
  let lastInsert = anchor;
849
- let newBlocks = this;
1050
+ let newBlocks: BlocksNode | undefined = this;
850
1051
  while (blocks.length > 0) {
851
1052
  // Get the block to insert
852
1053
  let newBlock = blocks.shift();
853
- if (newBlock === undefined)
854
- return;
1054
+ if (newBlock === undefined) return;
1055
+
855
1056
  // If we're inserting after the anchor and the anchor is a paragraph and the block to insert is a paragraph, merge them.
856
- if (lastInsert === anchor &&
1057
+ if (
1058
+ lastInsert === anchor &&
857
1059
  anchor instanceof ParagraphNode &&
858
- newBlock instanceof ParagraphNode) {
1060
+ newBlock instanceof ParagraphNode
1061
+ ) {
859
1062
  newBlock = anchor.withParagraphAppended(newBlock);
860
- if (newBlock === undefined)
861
- return;
1063
+ if (newBlock === undefined) return;
862
1064
  newBlocks = newBlocks.withChildReplaced(anchor, newBlock);
863
1065
  }
864
1066
  // If we're inserting after the anchor and the anchor is a list and the block to insert is a list, merge them.
865
- else if (lastInsert === anchor &&
1067
+ else if (
1068
+ lastInsert === anchor &&
866
1069
  anchor instanceof ListNode &&
867
- newBlock instanceof ListNode) {
1070
+ newBlock instanceof ListNode
1071
+ ) {
868
1072
  const edit = anchor.withNodeInserted(caret, newBlock);
869
1073
  if (edit === undefined || !(edit.root instanceof BlockNode))
870
1074
  return;
@@ -873,31 +1077,39 @@ export default class BlocksNode extends BlockNode {
873
1077
  }
874
1078
  // Otherwise just insert the block after the last insert, including if it was the anchor block.
875
1079
  else {
876
- newBlocks = newBlocks.withBlockInsertedAfter(lastInsert, newBlock);
1080
+ newBlocks = newBlocks.withBlockInsertedAfter(
1081
+ lastInsert,
1082
+ newBlock
1083
+ );
877
1084
  }
878
1085
  // Bail on fail.
879
- if (newBlocks === undefined)
880
- return;
1086
+ if (newBlocks === undefined) return;
881
1087
  // Remember what we inserted so we can insert after it.
882
1088
  lastInsert = newBlock;
883
1089
  }
1090
+
884
1091
  // Set the caret to the last caret of the last insert, and if there isn't one, insert an empty paragraph so we can.
885
1092
  const formats = lastInsert.getFormats();
886
1093
  let newCaret = undefined;
887
1094
  if (formats.length === 0) {
888
- const newParagraph = new ParagraphNode(0, new FormatNode('', [new TextNode()]));
1095
+ const newParagraph = new ParagraphNode(
1096
+ 0,
1097
+ new FormatNode('', [new TextNode()])
1098
+ );
889
1099
  newCaret = newParagraph.getFirstCaret();
890
- newBlocks = newBlocks.withBlockInsertedAfter(lastInsert, newParagraph);
891
- if (newCaret === undefined || newBlocks === undefined)
892
- return;
893
- }
894
- else {
1100
+ newBlocks = newBlocks.withBlockInsertedAfter(
1101
+ lastInsert,
1102
+ newParagraph
1103
+ );
1104
+ if (newCaret === undefined || newBlocks === undefined) return;
1105
+ } else {
895
1106
  newCaret = formats[formats.length - 1].getLastCaret();
896
- if (newCaret === undefined)
897
- return;
1107
+ if (newCaret === undefined) return;
898
1108
  }
1109
+
899
1110
  return { root: newBlocks, range: { start: newCaret, end: newCaret } };
900
1111
  }
1112
+
901
1113
  toHTML() {
902
1114
  return this.#blocks.map((b) => b.toHTML()).join('');
903
1115
  }