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.
- package/.env.example +8 -0
- package/.firebaserc +8 -0
- package/.prettierignore +8 -0
- package/.prettierrc.json +5 -0
- package/_update-notifier-last-checked +0 -0
- package/firebase.json +46 -0
- package/firestore.indexes.json +4 -0
- package/firestore.rules +59 -0
- package/functions/.eslintrc.js +28 -0
- package/functions/package-lock.json +12595 -0
- package/functions/package.json +43 -0
- package/functions/src/index.ts +345 -0
- package/functions/tsconfig.dev.json +3 -0
- package/functions/tsconfig.json +15 -0
- package/package.json +53 -270
- package/scripts/claims.cjs +99 -0
- package/src/app.d.ts +9 -0
- package/src/app.html +24 -0
- package/src/lib/components/Auth.svelte +65 -0
- package/{components/Authentication.d.ts → src/lib/components/Authentication.ts} +2 -0
- package/{components → src/lib/components}/BookPreview.svelte +27 -22
- package/{components → src/lib/components}/External.svelte +2 -1
- package/src/lib/components/Link.svelte +58 -0
- package/{components → src/lib/components}/app/BookList.svelte +6 -3
- package/{components → src/lib/components}/app/Button.svelte +5 -4
- package/{components → src/lib/components}/app/Feedback.svelte +11 -7
- package/{components → src/lib/components}/app/Head.svelte +2 -1
- package/{components → src/lib/components}/app/Large.svelte +0 -0
- package/{components → src/lib/components}/app/Lead.svelte +0 -0
- package/{components → src/lib/components}/app/Link.svelte +4 -3
- package/{components → src/lib/components}/app/Options.svelte +5 -4
- package/{components → src/lib/components}/app/Paragraph.svelte +2 -1
- package/{components → src/lib/components}/app/Section.svelte +0 -0
- package/{components → src/lib/components}/app/Table.svelte +0 -0
- package/{components → src/lib/components}/app/TextInput.svelte +12 -9
- package/{components → src/lib/components}/chapter/Atom.svelte +18 -12
- package/src/lib/components/chapter/Block.svelte +42 -0
- package/{components → src/lib/components}/chapter/Callout.svelte +6 -3
- package/{components → src/lib/components}/chapter/CaptionedCode.svelte +21 -13
- package/{components → src/lib/components}/chapter/ChapterBody.svelte +13 -9
- package/{components → src/lib/components}/chapter/Citations.svelte +34 -33
- package/{components → src/lib/components}/chapter/Code.svelte +63 -58
- package/{components → src/lib/components}/chapter/Comment.svelte +30 -18
- package/{components → src/lib/components}/chapter/Definition.svelte +23 -17
- package/{components → src/lib/components}/chapter/Embed.svelte +97 -77
- package/src/lib/components/chapter/ErrorMessage.svelte +12 -0
- package/{components → src/lib/components}/chapter/ExtendedPrism.js +0 -0
- package/{components → src/lib/components}/chapter/Figure.svelte +34 -26
- package/{components → src/lib/components}/chapter/Footnote.svelte +32 -20
- package/{components → src/lib/components}/chapter/Format.svelte +39 -34
- package/{components → src/lib/components}/chapter/InlineCode.svelte +9 -5
- package/{components → src/lib/components}/chapter/Label.svelte +23 -14
- package/{components → src/lib/components}/chapter/Link.svelte +27 -21
- package/{components → src/lib/components}/chapter/List.svelte +5 -3
- package/{components → src/lib/components}/chapter/Marginal.svelte +54 -41
- package/{components → src/lib/components}/chapter/Paragraph.svelte +25 -20
- package/{components → src/lib/components}/chapter/Positioned.svelte +4 -1
- package/{components → src/lib/components}/chapter/Problem.svelte +0 -0
- package/src/lib/components/chapter/Python.svelte +144 -0
- package/{components → src/lib/components}/chapter/Quote.svelte +9 -5
- package/{components → src/lib/components}/chapter/Rule.svelte +4 -1
- package/{components → src/lib/components}/chapter/Table.svelte +11 -7
- package/src/lib/components/chapter/Text.svelte +109 -0
- package/src/lib/components/editor/BookishEditor.svelte +1101 -0
- package/src/lib/components/editor/CalloutEditor.svelte +14 -0
- package/{components → src/lib/components}/editor/CaptionedCodeEditor.svelte +10 -6
- package/src/lib/components/editor/CaretContext.ts +55 -0
- package/{components/editor/CaretState.d.ts → src/lib/components/editor/CaretState.ts} +6 -7
- package/{components/editor/CaretUtilities.d.ts → src/lib/components/editor/CaretUtilities.ts} +1 -0
- package/{components → src/lib/components}/editor/CaretView.svelte +25 -20
- package/src/lib/components/editor/CitationsEditor.svelte +64 -0
- package/{components/editor/Clipboard.d.ts → src/lib/components/editor/Clipboard.ts} +2 -1
- package/{components/editor/Command.d.ts → src/lib/components/editor/Command.ts} +8 -2
- package/{components/editor/Commands.js → src/lib/components/editor/Commands.ts} +730 -315
- package/src/lib/components/editor/ConfirmButton.svelte +51 -0
- package/src/lib/components/editor/DefinitionEditor.svelte +44 -0
- package/src/lib/components/editor/EmbedEditor.svelte +152 -0
- package/{components → src/lib/components}/editor/Icon.svelte +2 -1
- package/{components → src/lib/components}/editor/ImageChooser.svelte +24 -19
- package/src/lib/components/editor/InlineCodeEditor.svelte +16 -0
- package/{components → src/lib/components}/editor/LabelEditor.svelte +14 -9
- package/{components → src/lib/components}/editor/LanguageEditor.svelte +8 -5
- package/src/lib/components/editor/LinkEditor.svelte +110 -0
- package/{components → src/lib/components}/editor/Note.svelte +0 -0
- package/{components → src/lib/components}/editor/Permissions.svelte +43 -36
- package/src/lib/components/editor/PositionEditor.svelte +21 -0
- package/src/lib/components/editor/QuoteEditor.svelte +14 -0
- package/{components → src/lib/components}/editor/Switch.svelte +14 -11
- package/src/lib/components/editor/TableEditor.svelte +14 -0
- package/{components → src/lib/components}/editor/TextEditor.svelte +101 -89
- package/src/lib/components/editor/Toggle.svelte +47 -0
- package/src/lib/components/editor/Toolbar.svelte +297 -0
- package/{components → src/lib/components}/editor/ToolbarGroup.svelte +5 -3
- package/{components → src/lib/components}/editor/ToolbarSpacer.svelte +0 -0
- package/src/lib/components/editor/URLEditor.svelte +15 -0
- package/{components/editor/UndoState.d.ts → src/lib/components/editor/UndoState.ts} +3 -2
- package/{components → src/lib/components}/editor/icons/annotate.svg +0 -0
- package/{components → src/lib/components}/editor/icons/block.svg +0 -0
- package/{components → src/lib/components}/editor/icons/bold.svg +0 -0
- package/{components → src/lib/components}/editor/icons/bullets.svg +0 -0
- package/{components → src/lib/components}/editor/icons/callout.svg +0 -0
- package/{components → src/lib/components}/editor/icons/citation.svg +0 -0
- package/{components → src/lib/components}/editor/icons/clear.svg +0 -0
- package/{components → src/lib/components}/editor/icons/clipboard.svg +0 -0
- package/{components → src/lib/components}/editor/icons/code.svg +0 -0
- package/{components → src/lib/components}/editor/icons/columnafter.svg +0 -0
- package/{components → src/lib/components}/editor/icons/columnbefore.svg +0 -0
- package/{components → src/lib/components}/editor/icons/comment.svg +0 -0
- package/{components → src/lib/components}/editor/icons/copy.svg +0 -0
- package/{components → src/lib/components}/editor/icons/cut.svg +0 -0
- package/{components → src/lib/components}/editor/icons/define.svg +0 -0
- package/{components → src/lib/components}/editor/icons/deletecolumn.svg +0 -0
- package/{components → src/lib/components}/editor/icons/deleterow.svg +0 -0
- package/{components → src/lib/components}/editor/icons/footnote.svg +0 -0
- package/{components → src/lib/components}/editor/icons/header1.svg +0 -0
- package/{components → src/lib/components}/editor/icons/header2.svg +0 -0
- package/{components → src/lib/components}/editor/icons/header3.svg +0 -0
- package/{components → src/lib/components}/editor/icons/home.svg +0 -0
- package/{components → src/lib/components}/editor/icons/indent.svg +0 -0
- package/{components → src/lib/components}/editor/icons/italic.svg +0 -0
- package/{components → src/lib/components}/editor/icons/label.svg +0 -0
- package/{components → src/lib/components}/editor/icons/left.svg +0 -0
- package/{components → src/lib/components}/editor/icons/level.svg +0 -0
- package/{components → src/lib/components}/editor/icons/link.svg +0 -0
- package/{components → src/lib/components}/editor/icons/list.svg +0 -0
- package/{components → src/lib/components}/editor/icons/media.svg +0 -0
- package/{components → src/lib/components}/editor/icons/middle.svg +0 -0
- package/{components → src/lib/components}/editor/icons/numbers.svg +0 -0
- package/{components → src/lib/components}/editor/icons/paragraph.svg +0 -0
- package/{components → src/lib/components}/editor/icons/paste.svg +0 -0
- package/{components → src/lib/components}/editor/icons/quote.svg +0 -0
- package/{components → src/lib/components}/editor/icons/redo.svg +0 -0
- package/{components → src/lib/components}/editor/icons/right.svg +0 -0
- package/{components → src/lib/components}/editor/icons/rowabove.svg +0 -0
- package/{components → src/lib/components}/editor/icons/rowbelow.svg +0 -0
- package/{components → src/lib/components}/editor/icons/rule.svg +0 -0
- package/{components → src/lib/components}/editor/icons/sub.svg +0 -0
- package/{components → src/lib/components}/editor/icons/super.svg +0 -0
- package/{components → src/lib/components}/editor/icons/table.svg +0 -0
- package/{components → src/lib/components}/editor/icons/text.svg +0 -0
- package/{components → src/lib/components}/editor/icons/time.svg +0 -0
- package/{components → src/lib/components}/editor/icons/undo.svg +0 -0
- package/{components → src/lib/components}/editor/icons/unindent.svg +0 -0
- package/{components → src/lib/components}/editor/icons/unlink.svg +0 -0
- package/{components → src/lib/components}/page/Acknowledgements.svelte +19 -10
- package/src/lib/components/page/Authors.svelte +92 -0
- package/src/lib/components/page/BookLoader.svelte +77 -0
- package/src/lib/components/page/BulkReferenceEditor.svelte +86 -0
- package/src/lib/components/page/Chapter.svelte +449 -0
- package/src/lib/components/page/ChapterContext.ts +13 -0
- package/{components → src/lib/components}/page/ChapterNumber.svelte +0 -0
- package/{components → src/lib/components}/page/ChapterTitle.svelte +4 -2
- package/src/lib/components/page/Contexts.ts +176 -0
- package/{components → src/lib/components}/page/DefinitionView.svelte +52 -37
- package/{components → src/lib/components}/page/Description.svelte +15 -7
- package/src/lib/components/page/Edition.svelte +189 -0
- package/src/lib/components/page/EditionLoader.svelte +123 -0
- package/{components → src/lib/components}/page/Editions.svelte +22 -18
- package/{components → src/lib/components}/page/Glossary.svelte +33 -28
- package/{components → src/lib/components}/page/Header.svelte +59 -52
- package/{components → src/lib/components}/page/Index.svelte +48 -41
- package/{components → src/lib/components}/page/Instructions.svelte +2 -1
- package/{components → src/lib/components}/page/License.svelte +17 -9
- package/{components → src/lib/components}/page/Loading.svelte +0 -0
- package/{components → src/lib/components}/page/Media.svelte +44 -27
- package/{components → src/lib/components}/page/MediaPreview.svelte +3 -2
- package/{components → src/lib/components}/page/Muted.svelte +0 -0
- package/{components → src/lib/components}/page/Outline.svelte +140 -110
- package/src/lib/components/page/Page.svelte +94 -0
- package/{components → src/lib/components}/page/PageHeader.svelte +2 -1
- package/{components → src/lib/components}/page/PageParagraph.svelte +0 -0
- package/src/lib/components/page/PossibleChapter.svelte +16 -0
- package/src/lib/components/page/PossibleReference.svelte +14 -0
- package/{components → src/lib/components}/page/Print.svelte +5 -3
- package/{components → src/lib/components}/page/Publish.svelte +59 -52
- package/{components → src/lib/components}/page/Reference.svelte +39 -30
- package/{components → src/lib/components}/page/References.svelte +18 -14
- package/{components → src/lib/components}/page/Rows.svelte +0 -0
- package/src/lib/components/page/Search.svelte +153 -0
- package/{components → src/lib/components}/page/Status.svelte +5 -3
- package/{components → src/lib/components}/page/SubdomainEditor.svelte +16 -13
- package/{components → src/lib/components}/page/TableOfContents.svelte +79 -68
- package/src/lib/components/page/TableOfContentsImage.svelte +22 -0
- package/{components → src/lib/components}/page/TableOfContentsRow.svelte +34 -24
- package/{components → src/lib/components}/page/ThemeEditor.svelte +44 -33
- package/{components → src/lib/components}/page/ThemeEditorPreview.svelte +21 -12
- package/src/lib/components/page/ThemeSetEditor.svelte +21 -0
- package/{components → src/lib/components}/page/ThemeVariableEditor.svelte +18 -14
- package/{components → src/lib/components}/page/Title.svelte +0 -0
- package/{components → src/lib/components}/page/Unknown.svelte +10 -8
- package/{components/page/margins.js → src/lib/components/page/margins.ts} +79 -46
- package/{index.js → src/lib/index.js} +0 -0
- package/src/lib/models/CRUD.ts +469 -0
- package/{models/Firebase.js → src/lib/models/Firebase.ts} +26 -16
- package/src/lib/models/book/Book.ts +283 -0
- package/src/lib/models/book/BookMedia.ts +182 -0
- package/src/lib/models/book/BookSaveStatus.ts +8 -0
- package/src/lib/models/book/Chapter.ts +462 -0
- package/{models/book/ChapterID.js → src/lib/models/book/ChapterID.ts} +1 -0
- package/src/lib/models/book/Definition.ts +2 -0
- package/src/lib/models/book/Edition.ts +1216 -0
- package/src/lib/models/book/Reference.ts +171 -0
- package/{models/book/Schema.js → src/lib/models/book/Schema.ts} +1 -0
- package/{models/book/Theme.js → src/lib/models/book/Theme.ts} +111 -6
- package/{models/book/ThemeColors.d.ts → src/lib/models/book/ThemeColors.ts} +6 -0
- package/{models/book/Words.js → src/lib/models/book/Words.ts} +1 -0
- package/{models/chapter/AtomNode.js → src/lib/models/chapter/AtomNode.ts} +30 -10
- package/{models/chapter/BlockNode.js → src/lib/models/chapter/BlockNode.ts} +80 -47
- package/src/lib/models/chapter/BlocksNode.test.ts +867 -0
- package/{models/chapter/BlocksNode.js → src/lib/models/chapter/BlocksNode.ts} +571 -359
- package/{models/chapter/Bookkeeping.d.ts → src/lib/models/chapter/Bookkeeping.ts} +0 -0
- package/src/lib/models/chapter/CalloutNode.ts +79 -0
- package/{models/chapter/Caret.js → src/lib/models/chapter/Caret.ts} +50 -26
- package/src/lib/models/chapter/ChapterNode.ts +169 -0
- package/{models/chapter/CitationsNode.js → src/lib/models/chapter/CitationsNode.ts} +17 -10
- package/{models/chapter/CodeNode.js → src/lib/models/chapter/CodeNode.ts} +125 -62
- package/src/lib/models/chapter/CommentNode.ts +61 -0
- package/{models/chapter/DefinitionNode.js → src/lib/models/chapter/DefinitionNode.ts} +14 -8
- package/src/lib/models/chapter/Edit.ts +6 -0
- package/src/lib/models/chapter/EmbedNode.ts +257 -0
- package/{models/chapter/ErrorNode.js → src/lib/models/chapter/ErrorNode.ts} +15 -8
- package/src/lib/models/chapter/FootnoteNode.ts +59 -0
- package/src/lib/models/chapter/FormatNode.test.ts +137 -0
- package/{models/chapter/FormatNode.js → src/lib/models/chapter/FormatNode.ts} +443 -262
- package/{models/chapter/InlineCodeNode.js → src/lib/models/chapter/InlineCodeNode.ts} +17 -9
- package/{models/chapter/LabelNode.js → src/lib/models/chapter/LabelNode.ts} +26 -10
- package/{models/chapter/LinkNode.js → src/lib/models/chapter/LinkNode.ts} +12 -7
- package/{models/chapter/ListNode.test.js → src/lib/models/chapter/ListNode.test.ts} +57 -20
- package/{models/chapter/ListNode.js → src/lib/models/chapter/ListNode.ts} +277 -200
- package/src/lib/models/chapter/MetadataNode.ts +44 -0
- package/{models → src/lib/models}/chapter/Node.test.js +0 -0
- package/{models/chapter/Node.js → src/lib/models/chapter/Node.ts} +155 -71
- package/src/lib/models/chapter/ParagraphNode.test.ts +54 -0
- package/{models/chapter/ParagraphNode.js → src/lib/models/chapter/ParagraphNode.ts} +95 -58
- package/{models/chapter/Parser.test.js → src/lib/models/chapter/Parser.test.ts} +7 -0
- package/{models/chapter/Parser.js → src/lib/models/chapter/Parser.ts} +369 -184
- package/{models/chapter/Position.d.ts → src/lib/models/chapter/Position.ts} +0 -0
- package/src/lib/models/chapter/QuoteNode.ts +135 -0
- package/{models/chapter/RootNode.d.ts → src/lib/models/chapter/RootNode.ts} +2 -0
- package/{models/chapter/RuleNode.js → src/lib/models/chapter/RuleNode.ts} +9 -6
- package/{models/chapter/TableNode.test.js → src/lib/models/chapter/TableNode.test.ts} +27 -10
- package/{models/chapter/TableNode.js → src/lib/models/chapter/TableNode.ts} +141 -82
- package/{models/chapter/TextNode.test.js → src/lib/models/chapter/TextNode.test.ts} +2 -0
- package/{models/chapter/TextNode.js → src/lib/models/chapter/TextNode.ts} +117 -65
- package/{util/getSubdomain.js → src/lib/util/getSubdomain.ts} +14 -8
- package/{util/isMobile.js → src/lib/util/isMobile.ts} +1 -0
- package/{util/mineReference.js → src/lib/util/mineReference.ts} +45 -25
- package/{util/scrollToEyeLevel.js → src/lib/util/scrollToEyeLevel.ts} +4 -2
- package/src/params/edition.ts +3 -0
- package/src/routes/+error.svelte +12 -0
- package/src/routes/+layout.svelte +241 -0
- package/src/routes/+page.svelte +43 -0
- package/src/routes/Footer.svelte +0 -0
- package/src/routes/Header.svelte +112 -0
- package/src/routes/[bookid]/+layout.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/+error.svelte +14 -0
- package/src/routes/[bookid]/[[editionid=edition]]/+layout.svelte +7 -0
- package/src/routes/[bookid]/[[editionid=edition]]/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/[chapterid]/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/glossary/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/index/[[letter]]/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/media/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/print/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/references/+page.svelte +5 -0
- package/src/routes/[bookid]/[[editionid=edition]]/search/+page.svelte +5 -0
- package/src/routes/[bookid]/editions/+page.svelte +20 -0
- package/src/routes/about/+page.svelte +102 -0
- package/src/routes/confirm/+page.svelte +62 -0
- package/src/routes/email/+page.svelte +99 -0
- package/src/routes/login/+page.svelte +66 -0
- package/src/routes/read/+page.svelte +38 -0
- package/src/routes/write/+error.svelte +10 -0
- package/src/routes/write/+layout.svelte +11 -0
- package/src/routes/write/+page.svelte +114 -0
- package/src/routes/write/[bookid]/+layout.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/+layout.svelte +7 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/[chapterid]/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/glossary/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/index/[[letter]]/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/media/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/print/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/references/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/search/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/theme/+page.svelte +5 -0
- package/src/routes/write/[bookid]/[[editionid=edition]]/unknown/+page.svelte +5 -0
- package/src/routes/write/[bookid]/editions/+page.svelte +20 -0
- package/src/vite-env.d.ts +1 -0
- package/static/icons/favicon.png +0 -0
- package/static/icons/icon.png +0 -0
- package/static/icons/ios-icon.png +0 -0
- package/storage.rules +9 -0
- package/svelte.config.js +19 -0
- package/tsconfig.json +18 -0
- package/tsconfig.node.json +9 -0
- package/vite.config.ts +8 -0
- package/vite.reader.config.ts +32 -0
- package/components/Auth.svelte +0 -55
- package/components/Auth.svelte.d.ts +0 -16
- package/components/Authentication.js +0 -1
- package/components/BookPreview.svelte.d.ts +0 -18
- package/components/External.svelte.d.ts +0 -18
- package/components/Link.svelte +0 -51
- package/components/Link.svelte.d.ts +0 -18
- package/components/app/BookList.svelte.d.ts +0 -18
- package/components/app/Button.svelte.d.ts +0 -22
- package/components/app/Feedback.svelte.d.ts +0 -18
- package/components/app/Head.svelte.d.ts +0 -18
- package/components/app/Large.svelte.d.ts +0 -27
- package/components/app/Lead.svelte.d.ts +0 -27
- package/components/app/Link.svelte.d.ts +0 -20
- package/components/app/Options.svelte.d.ts +0 -19
- package/components/app/Paragraph.svelte.d.ts +0 -18
- package/components/app/Section.svelte.d.ts +0 -27
- package/components/app/Table.svelte.d.ts +0 -27
- package/components/app/TextInput.svelte.d.ts +0 -22
- package/components/chapter/Atom.svelte.d.ts +0 -19
- package/components/chapter/Block.svelte +0 -37
- package/components/chapter/Block.svelte.d.ts +0 -17
- package/components/chapter/Callout.svelte.d.ts +0 -17
- package/components/chapter/CaptionedCode.svelte.d.ts +0 -17
- package/components/chapter/ChapterBody.svelte.d.ts +0 -19
- package/components/chapter/Citations.svelte.d.ts +0 -17
- package/components/chapter/Code.svelte.d.ts +0 -22
- package/components/chapter/Comment.svelte.d.ts +0 -17
- package/components/chapter/Definition.svelte.d.ts +0 -17
- package/components/chapter/Embed.svelte.d.ts +0 -20
- package/components/chapter/ErrorMessage.svelte +0 -9
- package/components/chapter/ErrorMessage.svelte.d.ts +0 -17
- package/components/chapter/ExtendedPrism.d.ts +0 -2
- package/components/chapter/Figure.svelte.d.ts +0 -24
- package/components/chapter/Footnote.svelte.d.ts +0 -19
- package/components/chapter/Format.svelte.d.ts +0 -19
- package/components/chapter/InlineCode.svelte.d.ts +0 -17
- package/components/chapter/Label.svelte.d.ts +0 -17
- package/components/chapter/Link.svelte.d.ts +0 -17
- package/components/chapter/List.svelte.d.ts +0 -17
- package/components/chapter/Marginal.svelte.d.ts +0 -22
- package/components/chapter/Paragraph.svelte.d.ts +0 -18
- package/components/chapter/Positioned.svelte.d.ts +0 -19
- package/components/chapter/Problem.svelte.d.ts +0 -27
- package/components/chapter/Python.svelte +0 -128
- package/components/chapter/Python.svelte.d.ts +0 -18
- package/components/chapter/Quote.svelte.d.ts +0 -17
- package/components/chapter/Rule.svelte.d.ts +0 -17
- package/components/chapter/Table.svelte.d.ts +0 -17
- package/components/chapter/Text.svelte +0 -87
- package/components/chapter/Text.svelte.d.ts +0 -18
- package/components/editor/BookishEditor.svelte +0 -884
- package/components/editor/BookishEditor.svelte.d.ts +0 -25
- package/components/editor/CalloutEditor.svelte +0 -10
- package/components/editor/CalloutEditor.svelte.d.ts +0 -17
- package/components/editor/CaptionedCodeEditor.svelte.d.ts +0 -17
- package/components/editor/CaretContext.d.ts +0 -50
- package/components/editor/CaretContext.js +0 -1
- package/components/editor/CaretState.js +0 -1
- package/components/editor/CaretUtilities.js +0 -1
- package/components/editor/CaretView.svelte.d.ts +0 -25
- package/components/editor/CitationsEditor.svelte +0 -53
- package/components/editor/CitationsEditor.svelte.d.ts +0 -17
- package/components/editor/Clipboard.js +0 -1
- package/components/editor/Command.js +0 -1
- package/components/editor/Commands.d.ts +0 -3
- package/components/editor/ConfirmButton.svelte +0 -49
- package/components/editor/ConfirmButton.svelte.d.ts +0 -19
- package/components/editor/DefinitionEditor.svelte +0 -36
- package/components/editor/DefinitionEditor.svelte.d.ts +0 -17
- package/components/editor/EmbedEditor.svelte +0 -137
- package/components/editor/EmbedEditor.svelte.d.ts +0 -17
- package/components/editor/Icon.svelte.d.ts +0 -16
- package/components/editor/ImageChooser.svelte.d.ts +0 -18
- package/components/editor/InlineCodeEditor.svelte +0 -12
- package/components/editor/InlineCodeEditor.svelte.d.ts +0 -17
- package/components/editor/LabelEditor.svelte.d.ts +0 -17
- package/components/editor/LanguageEditor.svelte.d.ts +0 -17
- package/components/editor/LinkEditor.svelte +0 -100
- package/components/editor/LinkEditor.svelte.d.ts +0 -17
- package/components/editor/Note.svelte.d.ts +0 -27
- package/components/editor/Permissions.svelte.d.ts +0 -20
- package/components/editor/PositionEditor.svelte +0 -17
- package/components/editor/PositionEditor.svelte.d.ts +0 -18
- package/components/editor/QuoteEditor.svelte +0 -10
- package/components/editor/QuoteEditor.svelte.d.ts +0 -17
- package/components/editor/Switch.svelte.d.ts +0 -20
- package/components/editor/TableEditor.svelte +0 -10
- package/components/editor/TableEditor.svelte.d.ts +0 -17
- package/components/editor/TextEditor.svelte.d.ts +0 -24
- package/components/editor/Toggle.svelte +0 -41
- package/components/editor/Toggle.svelte.d.ts +0 -19
- package/components/editor/Toolbar.svelte +0 -249
- package/components/editor/Toolbar.svelte.d.ts +0 -17
- package/components/editor/ToolbarGroup.svelte.d.ts +0 -18
- package/components/editor/ToolbarSpacer.svelte.d.ts +0 -23
- package/components/editor/URLEditor.svelte +0 -13
- package/components/editor/URLEditor.svelte.d.ts +0 -18
- package/components/editor/UndoState.js +0 -1
- package/components/page/Acknowledgements.svelte.d.ts +0 -14
- package/components/page/Authors.svelte +0 -85
- package/components/page/Authors.svelte.d.ts +0 -21
- package/components/page/BookLoader.svelte +0 -59
- package/components/page/BookLoader.svelte.d.ts +0 -16
- package/components/page/BulkReferenceEditor.svelte +0 -63
- package/components/page/BulkReferenceEditor.svelte.d.ts +0 -14
- package/components/page/Chapter.svelte +0 -392
- package/components/page/Chapter.svelte.d.ts +0 -18
- package/components/page/ChapterContext.d.ts +0 -10
- package/components/page/ChapterContext.js +0 -1
- package/components/page/ChapterNumber.svelte.d.ts +0 -27
- package/components/page/ChapterTitle.svelte.d.ts +0 -18
- package/components/page/Contexts.d.ts +0 -47
- package/components/page/Contexts.js +0 -133
- package/components/page/DefinitionView.svelte.d.ts +0 -18
- package/components/page/Description.svelte.d.ts +0 -14
- package/components/page/Edition.svelte +0 -161
- package/components/page/Edition.svelte.d.ts +0 -20
- package/components/page/EditionLoader.svelte +0 -99
- package/components/page/EditionLoader.svelte.d.ts +0 -18
- package/components/page/Editions.svelte.d.ts +0 -14
- package/components/page/Glossary.svelte.d.ts +0 -14
- package/components/page/Header.svelte.d.ts +0 -29
- package/components/page/Index.svelte.d.ts +0 -14
- package/components/page/Instructions.svelte.d.ts +0 -18
- package/components/page/License.svelte.d.ts +0 -14
- package/components/page/Loading.svelte.d.ts +0 -23
- package/components/page/Media.svelte.d.ts +0 -14
- package/components/page/MediaPreview.svelte.d.ts +0 -19
- package/components/page/Muted.svelte.d.ts +0 -27
- package/components/page/Outline.svelte.d.ts +0 -19
- package/components/page/Page.svelte +0 -84
- package/components/page/Page.svelte.d.ts +0 -19
- package/components/page/PageHeader.svelte.d.ts +0 -18
- package/components/page/PageParagraph.svelte.d.ts +0 -27
- package/components/page/PossibleChapter.svelte +0 -14
- package/components/page/PossibleChapter.svelte.d.ts +0 -14
- package/components/page/PossibleReference.svelte +0 -11
- package/components/page/PossibleReference.svelte.d.ts +0 -18
- package/components/page/Print.svelte.d.ts +0 -14
- package/components/page/Publish.svelte.d.ts +0 -14
- package/components/page/Reference.svelte.d.ts +0 -17
- package/components/page/References.svelte.d.ts +0 -14
- package/components/page/Rows.svelte.d.ts +0 -27
- package/components/page/Search.svelte +0 -133
- package/components/page/Search.svelte.d.ts +0 -14
- package/components/page/Status.svelte.d.ts +0 -14
- package/components/page/SubdomainEditor.svelte.d.ts +0 -14
- package/components/page/TableOfContents.svelte.d.ts +0 -14
- package/components/page/TableOfContentsImage.svelte +0 -18
- package/components/page/TableOfContentsImage.svelte.d.ts +0 -16
- package/components/page/TableOfContentsRow.svelte.d.ts +0 -24
- package/components/page/ThemeEditor.svelte.d.ts +0 -14
- package/components/page/ThemeEditorPreview.svelte.d.ts +0 -17
- package/components/page/ThemeSetEditor.svelte +0 -19
- package/components/page/ThemeSetEditor.svelte.d.ts +0 -18
- package/components/page/ThemeVariableEditor.svelte.d.ts +0 -18
- package/components/page/Title.svelte.d.ts +0 -27
- package/components/page/Unknown.svelte.d.ts +0 -16
- package/components/page/margins.d.ts +0 -2
- package/index.d.ts +0 -15
- package/models/CRUD.d.ts +0 -27
- package/models/CRUD.js +0 -262
- package/models/Firebase.d.ts +0 -5
- package/models/book/Book.d.ts +0 -61
- package/models/book/Book.js +0 -135
- package/models/book/BookMedia.d.ts +0 -23
- package/models/book/BookMedia.js +0 -108
- package/models/book/BookSaveStatus.d.ts +0 -7
- package/models/book/BookSaveStatus.js +0 -8
- package/models/book/Chapter.d.ts +0 -81
- package/models/book/Chapter.js +0 -212
- package/models/book/ChapterID.d.ts +0 -10
- package/models/book/Definition.d.ts +0 -6
- package/models/book/Definition.js +0 -1
- package/models/book/Edition.d.ts +0 -139
- package/models/book/Edition.js +0 -620
- package/models/book/Reference.d.ts +0 -24
- package/models/book/Reference.js +0 -78
- package/models/book/Schema.d.ts +0 -174
- package/models/book/Theme.d.ts +0 -113
- package/models/book/ThemeColors.js +0 -1
- package/models/book/Words.d.ts +0 -2
- package/models/chapter/AtomNode.d.ts +0 -18
- package/models/chapter/BlockNode.d.ts +0 -20
- package/models/chapter/BlocksNode.d.ts +0 -50
- package/models/chapter/BlocksNode.test.d.ts +0 -1
- package/models/chapter/BlocksNode.test.js +0 -568
- package/models/chapter/Bookkeeping.js +0 -1
- package/models/chapter/CalloutNode.d.ts +0 -17
- package/models/chapter/CalloutNode.js +0 -58
- package/models/chapter/Caret.d.ts +0 -23
- package/models/chapter/ChapterNode.d.ts +0 -34
- package/models/chapter/ChapterNode.js +0 -117
- package/models/chapter/CitationsNode.d.ts +0 -16
- package/models/chapter/CodeNode.d.ts +0 -35
- package/models/chapter/CommentNode.d.ts +0 -18
- package/models/chapter/CommentNode.js +0 -48
- package/models/chapter/DefinitionNode.d.ts +0 -12
- package/models/chapter/Edit.d.ts +0 -7
- package/models/chapter/Edit.js +0 -1
- package/models/chapter/EmbedNode.d.ts +0 -45
- package/models/chapter/EmbedNode.js +0 -168
- package/models/chapter/ErrorNode.d.ts +0 -17
- package/models/chapter/FootnoteNode.d.ts +0 -18
- package/models/chapter/FootnoteNode.js +0 -47
- package/models/chapter/FormatNode.d.ts +0 -60
- package/models/chapter/FormatNode.test.d.ts +0 -1
- package/models/chapter/FormatNode.test.js +0 -96
- package/models/chapter/InlineCodeNode.d.ts +0 -12
- package/models/chapter/LabelNode.d.ts +0 -17
- package/models/chapter/LinkNode.d.ts +0 -12
- package/models/chapter/ListNode.d.ts +0 -51
- package/models/chapter/ListNode.test.d.ts +0 -1
- package/models/chapter/MetadataNode.d.ts +0 -17
- package/models/chapter/MetadataNode.js +0 -32
- package/models/chapter/Node.d.ts +0 -38
- package/models/chapter/Node.test.d.ts +0 -1
- package/models/chapter/ParagraphNode.d.ts +0 -33
- package/models/chapter/ParagraphNode.test.d.ts +0 -1
- package/models/chapter/ParagraphNode.test.js +0 -30
- package/models/chapter/Parser.d.ts +0 -75
- package/models/chapter/Parser.test.d.ts +0 -1
- package/models/chapter/Position.js +0 -1
- package/models/chapter/QuoteNode.d.ts +0 -25
- package/models/chapter/QuoteNode.js +0 -99
- package/models/chapter/RootNode.js +0 -1
- package/models/chapter/RuleNode.d.ts +0 -15
- package/models/chapter/TableNode.d.ts +0 -37
- package/models/chapter/TableNode.test.d.ts +0 -1
- package/models/chapter/TextNode.d.ts +0 -36
- package/models/chapter/TextNode.test.d.ts +0 -1
- package/util/getSubdomain.d.ts +0 -3
- package/util/isMobile.d.ts +0 -2
- package/util/mineReference.d.ts +0 -4
- 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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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 (
|
|
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(
|
|
56
|
+
return this.getBlocks().reduce(
|
|
57
|
+
(prev: FormatNode[], current) => prev.concat(current.getFormats()),
|
|
58
|
+
[]
|
|
59
|
+
);
|
|
47
60
|
}
|
|
48
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
80
|
-
if (
|
|
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
|
-
|
|
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 =
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
105
|
-
insideSelection
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
114
|
-
|
|
130
|
+
|
|
131
|
+
getBlockOfCaret(caret: Caret): BlockNode | undefined {
|
|
132
|
+
return caret.node.getClosestParentOfType<BlockNode>(this, BlockNode);
|
|
115
133
|
}
|
|
116
|
-
|
|
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
|
-
|
|
122
|
-
|
|
140
|
+
|
|
141
|
+
getTextNodes(): TextNode[] {
|
|
142
|
+
return this.getNodes().filter(
|
|
143
|
+
(node) => node instanceof TextNode
|
|
144
|
+
) as TextNode[];
|
|
123
145
|
}
|
|
124
|
-
|
|
125
|
-
|
|
146
|
+
|
|
147
|
+
getAtomNodes(): AtomNode<any>[] {
|
|
148
|
+
return this.getNodes().filter(
|
|
149
|
+
(node) => node instanceof AtomNode
|
|
150
|
+
) as AtomNode<any>[];
|
|
126
151
|
}
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
158
|
+
|
|
159
|
+
getIndexOfTextNode(node: TextNode): number | undefined {
|
|
131
160
|
const text = this.getTextNodes();
|
|
132
161
|
return text.indexOf(node);
|
|
133
162
|
}
|
|
134
|
-
|
|
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
|
-
|
|
168
|
+
|
|
169
|
+
getLastCaret(): Caret | undefined {
|
|
139
170
|
const text = this.getTextNodes();
|
|
140
171
|
return text.length === 0
|
|
141
172
|
? undefined
|
|
142
173
|
: {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
174
|
+
node: text[text.length - 1],
|
|
175
|
+
index: text[text.length - 1].getLength(),
|
|
176
|
+
};
|
|
146
177
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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(
|
|
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(
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
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 (
|
|
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 =
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
|
|
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(
|
|
200
|
-
|
|
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
|
-
|
|
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 =
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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
|
-
|
|
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
|
-
|
|
228
|
-
|
|
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
|
-
|
|
235
|
-
|
|
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
|
-
|
|
299
|
+
|
|
300
|
+
return this.create(newBlocks) as this;
|
|
246
301
|
}
|
|
247
|
-
|
|
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
|
-
|
|
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
|
-
|
|
320
|
+
|
|
321
|
+
withoutBlock(block: BlockNode): BlocksNode {
|
|
261
322
|
return this.create(this.#blocks.filter((n) => n !== block));
|
|
262
323
|
}
|
|
263
|
-
|
|
324
|
+
|
|
325
|
+
withBlockInsertedBefore(anchor: BlockNode, block: BlockNode) {
|
|
264
326
|
return this.withBlockInserted(anchor, block, true);
|
|
265
327
|
}
|
|
266
|
-
|
|
328
|
+
|
|
329
|
+
withBlockInsertedAfter(anchor: BlockNode, block: BlockNode) {
|
|
267
330
|
return this.withBlockInserted(anchor, block, false);
|
|
268
331
|
}
|
|
269
|
-
|
|
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 (
|
|
274
|
-
range.start.
|
|
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(
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
387
|
+
if (!(container instanceof BlocksNode)) return;
|
|
388
|
+
|
|
318
389
|
const newParagraph = new ParagraphNode();
|
|
319
390
|
const newCaret = newParagraph.getFirstCaret();
|
|
320
|
-
if (newCaret === undefined)
|
|
321
|
-
|
|
322
|
-
const newBlocks = container.withBlockInsertedAfter(
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
401
|
+
if (newRoot === undefined) return;
|
|
402
|
+
|
|
329
403
|
return { root: newRoot, range: { start: newCaret, end: newCaret } };
|
|
330
404
|
}
|
|
331
405
|
}
|
|
332
|
-
|
|
406
|
+
|
|
407
|
+
withoutRange(range: CaretRange): Edit {
|
|
333
408
|
return this.withRangeFormatted(range, undefined);
|
|
334
409
|
}
|
|
335
|
-
|
|
410
|
+
|
|
411
|
+
withoutAtom(atom: AtomNode<any>): Edit {
|
|
336
412
|
// Find the format that contains the atom.
|
|
337
|
-
const format = atom.getFarthestParentMatching(
|
|
338
|
-
|
|
339
|
-
|
|
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 =
|
|
346
|
-
|
|
347
|
-
|
|
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:
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
449
|
+
|
|
450
|
+
withRangeFormatted(range: CaretRange, format: Format | undefined): Edit {
|
|
366
451
|
// Only works on text nodes, atom nodes, and metadata nodes.
|
|
367
|
-
if (
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
range.
|
|
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 (
|
|
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 (
|
|
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
|
-
|
|
398
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
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 =
|
|
442
|
-
|
|
443
|
-
|
|
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 =
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
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
|
-
|
|
568
|
+
if (!newBlock.isEmpty()) formattedBlocks.push(newBlock);
|
|
569
|
+
|
|
459
570
|
// Replace the block, or delete it if it's empty.
|
|
460
|
-
newBlocks = newBlocks.withChildReplaced(
|
|
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 (
|
|
475
|
-
|
|
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(
|
|
478
|
-
|
|
479
|
-
|
|
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 =
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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
|
-
|
|
497
|
-
|
|
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
|
-
|
|
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(
|
|
504
|
-
|
|
505
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
528
|
-
|
|
529
|
-
|
|
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(
|
|
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(
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
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(
|
|
549
|
-
|
|
550
|
-
|
|
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(
|
|
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
|
-
|
|
561
|
-
|
|
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(
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
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(
|
|
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(
|
|
588
|
-
|
|
589
|
-
|
|
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
|
-
|
|
600
|
-
|
|
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(
|
|
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(
|
|
605
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
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
|
-
|
|
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(
|
|
662
|
-
|
|
663
|
-
|
|
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
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
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
|
-
|
|
690
|
-
|
|
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 (
|
|
714
|
-
parents
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
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(
|
|
754
|
-
|
|
755
|
-
|
|
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
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
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(
|
|
781
|
-
|
|
782
|
-
|
|
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(
|
|
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 (
|
|
807
|
-
|
|
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(
|
|
1016
|
+
const newBlocks = this.withNodeReplaced(
|
|
1017
|
+
blocks,
|
|
1018
|
+
blocks.withoutBlock(adjacentBlock)
|
|
1019
|
+
);
|
|
819
1020
|
return newBlocks === undefined
|
|
820
1021
|
? undefined
|
|
821
1022
|
: {
|
|
822
|
-
|
|
823
|
-
|
|
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
|
-
|
|
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 =
|
|
837
|
-
|
|
838
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
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 (
|
|
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(
|
|
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(
|
|
1095
|
+
const newParagraph = new ParagraphNode(
|
|
1096
|
+
0,
|
|
1097
|
+
new FormatNode('', [new TextNode()])
|
|
1098
|
+
);
|
|
889
1099
|
newCaret = newParagraph.getFirstCaret();
|
|
890
|
-
newBlocks = newBlocks.withBlockInsertedAfter(
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
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
|
}
|