bookish-press 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/components/Auth.svelte +52 -0
- package/components/Auth.svelte.d.ts +16 -0
- package/components/Authentication.d.ts +8 -0
- package/components/Authentication.js +1 -0
- package/components/BookPreview.svelte +87 -0
- package/components/BookPreview.svelte.d.ts +18 -0
- package/components/External.svelte +4 -0
- package/components/External.svelte.d.ts +18 -0
- package/components/Link.svelte +5 -0
- package/components/Link.svelte.d.ts +19 -0
- package/components/chapter/Atom.svelte +36 -0
- package/components/chapter/Atom.svelte.d.ts +19 -0
- package/components/chapter/Block.svelte +32 -0
- package/components/chapter/Block.svelte.d.ts +17 -0
- package/components/chapter/Callout.svelte +10 -0
- package/components/chapter/Callout.svelte.d.ts +17 -0
- package/components/chapter/CaptionedCode.svelte +34 -0
- package/components/chapter/CaptionedCode.svelte.d.ts +17 -0
- package/components/chapter/ChapterBody.svelte +23 -0
- package/components/chapter/ChapterBody.svelte.d.ts +18 -0
- package/components/chapter/Citations.svelte +66 -0
- package/components/chapter/Citations.svelte.d.ts +17 -0
- package/components/chapter/Code.svelte +35 -0
- package/components/chapter/Code.svelte.d.ts +22 -0
- package/components/chapter/Comment.svelte +46 -0
- package/components/chapter/Comment.svelte.d.ts +17 -0
- package/components/chapter/Definition.svelte +38 -0
- package/components/chapter/Definition.svelte.d.ts +20 -0
- package/components/chapter/Embed.svelte +122 -0
- package/components/chapter/Embed.svelte.d.ts +18 -0
- package/components/chapter/ErrorMessage.svelte +4 -0
- package/components/chapter/ErrorMessage.svelte.d.ts +17 -0
- package/components/chapter/Footnote.svelte +37 -0
- package/components/chapter/Footnote.svelte.d.ts +19 -0
- package/components/chapter/Format.svelte +57 -0
- package/components/chapter/Format.svelte.d.ts +18 -0
- package/components/chapter/InlineCode.svelte +12 -0
- package/components/chapter/InlineCode.svelte.d.ts +17 -0
- package/components/chapter/Label.svelte +20 -0
- package/components/chapter/Label.svelte.d.ts +17 -0
- package/components/chapter/Link.svelte +51 -0
- package/components/chapter/Link.svelte.d.ts +17 -0
- package/components/chapter/List.svelte +14 -0
- package/components/chapter/List.svelte.d.ts +17 -0
- package/components/chapter/Marginal.svelte +48 -0
- package/components/chapter/Marginal.svelte.d.ts +19 -0
- package/components/chapter/Paragraph.svelte +15 -0
- package/components/chapter/Paragraph.svelte.d.ts +18 -0
- package/components/chapter/Python.svelte +81 -0
- package/components/chapter/Python.svelte.d.ts +18 -0
- package/components/chapter/Quote.svelte +16 -0
- package/components/chapter/Quote.svelte.d.ts +17 -0
- package/components/chapter/Rule.svelte +4 -0
- package/components/chapter/Rule.svelte.d.ts +17 -0
- package/components/chapter/Table.svelte +29 -0
- package/components/chapter/Table.svelte.d.ts +17 -0
- package/components/chapter/Text.svelte +62 -0
- package/components/chapter/Text.svelte.d.ts +17 -0
- package/components/chapter/renderPosition.d.ts +3 -0
- package/components/chapter/renderPosition.js +4 -0
- package/components/editor/BookishEditor.svelte +755 -0
- package/components/editor/BookishEditor.svelte.d.ts +22 -0
- package/components/editor/CalloutEditor.svelte +10 -0
- package/components/editor/CalloutEditor.svelte.d.ts +17 -0
- package/components/editor/CaptionedCodeEditor.svelte +22 -0
- package/components/editor/CaptionedCodeEditor.svelte.d.ts +17 -0
- package/components/editor/CaretContext.d.ts +18 -0
- package/components/editor/CaretContext.js +1 -0
- package/components/editor/CaretState.d.ts +47 -0
- package/components/editor/CaretState.js +1 -0
- package/components/editor/CaretUtilities.d.ts +5 -0
- package/components/editor/CaretUtilities.js +1 -0
- package/components/editor/CitationsEditor.svelte +42 -0
- package/components/editor/CitationsEditor.svelte.d.ts +17 -0
- package/components/editor/Clipboard.d.ts +3 -0
- package/components/editor/Clipboard.js +1 -0
- package/components/editor/Command.d.ts +18 -0
- package/components/editor/Command.js +1 -0
- package/components/editor/Commands.d.ts +3 -0
- package/components/editor/Commands.js +951 -0
- package/components/editor/ConfirmButton.svelte +43 -0
- package/components/editor/ConfirmButton.svelte.d.ts +18 -0
- package/components/editor/DefinitionEditor.svelte +28 -0
- package/components/editor/DefinitionEditor.svelte.d.ts +17 -0
- package/components/editor/EmbedEditor.svelte +87 -0
- package/components/editor/EmbedEditor.svelte.d.ts +17 -0
- package/components/editor/ImageChooser.svelte +70 -0
- package/components/editor/ImageChooser.svelte.d.ts +18 -0
- package/components/editor/InlineCodeEditor.svelte +9 -0
- package/components/editor/InlineCodeEditor.svelte.d.ts +17 -0
- package/components/editor/LabelEditor.svelte +23 -0
- package/components/editor/LabelEditor.svelte.d.ts +17 -0
- package/components/editor/LanguageEditor.svelte +11 -0
- package/components/editor/LanguageEditor.svelte.d.ts +17 -0
- package/components/editor/LinkEditor.svelte +75 -0
- package/components/editor/LinkEditor.svelte.d.ts +17 -0
- package/components/editor/Note.svelte +9 -0
- package/components/editor/Note.svelte.d.ts +23 -0
- package/components/editor/PositionEditor.svelte +14 -0
- package/components/editor/PositionEditor.svelte.d.ts +18 -0
- package/components/editor/QuoteEditor.svelte +5 -0
- package/components/editor/QuoteEditor.svelte.d.ts +17 -0
- package/components/editor/Switch.svelte +56 -0
- package/components/editor/Switch.svelte.d.ts +20 -0
- package/components/editor/TextEditor.svelte +193 -0
- package/components/editor/TextEditor.svelte.d.ts +23 -0
- package/components/editor/Toggle.svelte +38 -0
- package/components/editor/Toggle.svelte.d.ts +19 -0
- package/components/editor/Toolbar.svelte +202 -0
- package/components/editor/Toolbar.svelte.d.ts +20 -0
- package/components/editor/ToolbarGroup.svelte +41 -0
- package/components/editor/ToolbarGroup.svelte.d.ts +19 -0
- package/components/editor/ToolbarIcon.svelte +22 -0
- package/components/editor/ToolbarIcon.svelte.d.ts +16 -0
- package/components/editor/ToolbarSpacer.svelte +1 -0
- package/components/editor/ToolbarSpacer.svelte.d.ts +19 -0
- package/components/editor/URLEditor.svelte +15 -0
- package/components/editor/URLEditor.svelte.d.ts +18 -0
- package/components/editor/UndoState.d.ts +8 -0
- package/components/editor/UndoState.js +1 -0
- package/components/page/Acknowledgements.svelte +29 -0
- package/components/page/Acknowledgements.svelte.d.ts +14 -0
- package/components/page/Alert.svelte +11 -0
- package/components/page/Alert.svelte.d.ts +23 -0
- package/components/page/Authors.svelte +62 -0
- package/components/page/Authors.svelte.d.ts +20 -0
- package/components/page/BulkReferenceEditor.svelte +32 -0
- package/components/page/BulkReferenceEditor.svelte.d.ts +14 -0
- package/components/page/Chapter.svelte +301 -0
- package/components/page/Chapter.svelte.d.ts +18 -0
- package/components/page/ChapterContext.d.ts +10 -0
- package/components/page/ChapterContext.js +1 -0
- package/components/page/Contexts.d.ts +27 -0
- package/components/page/Contexts.js +21 -0
- package/components/page/DefinitionView.svelte +129 -0
- package/components/page/DefinitionView.svelte.d.ts +18 -0
- package/components/page/Description.svelte +22 -0
- package/components/page/Description.svelte.d.ts +14 -0
- package/components/page/Edition.svelte +106 -0
- package/components/page/Edition.svelte.d.ts +21 -0
- package/components/page/Glossary.svelte +61 -0
- package/components/page/Glossary.svelte.d.ts +14 -0
- package/components/page/Header.svelte +105 -0
- package/components/page/Header.svelte.d.ts +27 -0
- package/components/page/Index.svelte +117 -0
- package/components/page/Index.svelte.d.ts +14 -0
- package/components/page/Instructions.svelte +30 -0
- package/components/page/Instructions.svelte.d.ts +16 -0
- package/components/page/License.svelte +29 -0
- package/components/page/License.svelte.d.ts +14 -0
- package/components/page/Loading.svelte +13 -0
- package/components/page/Loading.svelte.d.ts +19 -0
- package/components/page/Media.svelte +76 -0
- package/components/page/Media.svelte.d.ts +14 -0
- package/components/page/MediaPreview.svelte +11 -0
- package/components/page/MediaPreview.svelte.d.ts +19 -0
- package/components/page/Outline.svelte +173 -0
- package/components/page/Outline.svelte.d.ts +19 -0
- package/components/page/Page.svelte +53 -0
- package/components/page/Page.svelte.d.ts +19 -0
- package/components/page/PossibleChapter.svelte +14 -0
- package/components/page/PossibleChapter.svelte.d.ts +14 -0
- package/components/page/PossibleReference.svelte +16 -0
- package/components/page/PossibleReference.svelte.d.ts +19 -0
- package/components/page/Print.svelte +8 -0
- package/components/page/Print.svelte.d.ts +14 -0
- package/components/page/Reference.svelte +145 -0
- package/components/page/Reference.svelte.d.ts +17 -0
- package/components/page/References.svelte +58 -0
- package/components/page/References.svelte.d.ts +14 -0
- package/components/page/Revisions.svelte +124 -0
- package/components/page/Revisions.svelte.d.ts +14 -0
- package/components/page/Search.svelte +108 -0
- package/components/page/Search.svelte.d.ts +14 -0
- package/components/page/Status.svelte +49 -0
- package/components/page/Status.svelte.d.ts +19 -0
- package/components/page/SubdomainEditor.svelte +30 -0
- package/components/page/SubdomainEditor.svelte.d.ts +14 -0
- package/components/page/TableOfContents.svelte +234 -0
- package/components/page/TableOfContents.svelte.d.ts +14 -0
- package/components/page/TableOfContentsImage.svelte +15 -0
- package/components/page/TableOfContentsImage.svelte.d.ts +16 -0
- package/components/page/TableOfContentsRow.svelte +57 -0
- package/components/page/TableOfContentsRow.svelte.d.ts +24 -0
- package/components/page/ThemeEditor.svelte +52 -0
- package/components/page/ThemeEditor.svelte.d.ts +14 -0
- package/components/page/ThemeEditorPreview.svelte +37 -0
- package/components/page/ThemeEditorPreview.svelte.d.ts +17 -0
- package/components/page/ThemeSetEditor.svelte +17 -0
- package/components/page/ThemeSetEditor.svelte.d.ts +18 -0
- package/components/page/ThemeVariableEditor.svelte +28 -0
- package/components/page/ThemeVariableEditor.svelte.d.ts +18 -0
- package/components/page/Unknown.svelte +29 -0
- package/components/page/Unknown.svelte.d.ts +16 -0
- package/components/page/margins.d.ts +2 -0
- package/components/page/margins.js +132 -0
- package/models/BookLoader.d.ts +2 -0
- package/models/BookLoader.js +67 -0
- package/models/Firebase.d.ts +4 -0
- package/models/Firebase.js +26 -0
- package/models/Firestore.d.ts +22 -0
- package/models/Firestore.js +194 -0
- package/models/book/Book.d.ts +60 -0
- package/models/book/Book.js +135 -0
- package/models/book/BookMedia.d.ts +23 -0
- package/models/book/BookMedia.js +102 -0
- package/models/book/Chapter.d.ts +70 -0
- package/models/book/Chapter.js +221 -0
- package/models/book/ChapterID.d.ts +10 -0
- package/models/book/ChapterID.js +10 -0
- package/models/book/Definition.d.ts +6 -0
- package/models/book/Definition.js +1 -0
- package/models/book/Edition.d.ts +111 -0
- package/models/book/Edition.js +466 -0
- package/models/book/Theme.d.ts +106 -0
- package/models/book/Theme.js +76 -0
- package/models/book/ThemeColors.d.ts +15 -0
- package/models/book/ThemeColors.js +1 -0
- package/models/chapter/AtomNode.d.ts +18 -0
- package/models/chapter/AtomNode.js +31 -0
- package/models/chapter/BlockNode.d.ts +21 -0
- package/models/chapter/BlockNode.js +117 -0
- package/models/chapter/BlocksNode.d.ts +50 -0
- package/models/chapter/BlocksNode.js +709 -0
- package/models/chapter/BlocksNode.test.d.ts +1 -0
- package/models/chapter/BlocksNode.test.js +302 -0
- package/models/chapter/Bookkeeping.d.ts +4 -0
- package/models/chapter/Bookkeeping.js +1 -0
- package/models/chapter/CalloutNode.d.ts +17 -0
- package/models/chapter/CalloutNode.js +36 -0
- package/models/chapter/Caret.d.ts +21 -0
- package/models/chapter/Caret.js +69 -0
- package/models/chapter/ChapterNode.d.ts +34 -0
- package/models/chapter/ChapterNode.js +80 -0
- package/models/chapter/CitationsNode.d.ts +16 -0
- package/models/chapter/CitationsNode.js +17 -0
- package/models/chapter/CodeNode.d.ts +30 -0
- package/models/chapter/CodeNode.js +64 -0
- package/models/chapter/CommentNode.d.ts +18 -0
- package/models/chapter/CommentNode.js +32 -0
- package/models/chapter/DefinitionNode.d.ts +12 -0
- package/models/chapter/DefinitionNode.js +20 -0
- package/models/chapter/Edit.d.ts +7 -0
- package/models/chapter/Edit.js +1 -0
- package/models/chapter/EmbedNode.d.ts +40 -0
- package/models/chapter/EmbedNode.js +89 -0
- package/models/chapter/ErrorNode.d.ts +17 -0
- package/models/chapter/ErrorNode.js +21 -0
- package/models/chapter/FootnoteNode.d.ts +18 -0
- package/models/chapter/FootnoteNode.js +29 -0
- package/models/chapter/FormatNode.d.ts +60 -0
- package/models/chapter/FormatNode.js +614 -0
- package/models/chapter/FormatNode.test.d.ts +1 -0
- package/models/chapter/FormatNode.test.js +54 -0
- package/models/chapter/InlineCodeNode.d.ts +12 -0
- package/models/chapter/InlineCodeNode.js +18 -0
- package/models/chapter/LabelNode.d.ts +17 -0
- package/models/chapter/LabelNode.js +22 -0
- package/models/chapter/LinkNode.d.ts +12 -0
- package/models/chapter/LinkNode.js +16 -0
- package/models/chapter/ListNode.d.ts +51 -0
- package/models/chapter/ListNode.js +370 -0
- package/models/chapter/ListNode.test.d.ts +1 -0
- package/models/chapter/ListNode.test.js +49 -0
- package/models/chapter/MetadataNode.d.ts +17 -0
- package/models/chapter/MetadataNode.js +24 -0
- package/models/chapter/Node.d.ts +36 -0
- package/models/chapter/Node.js +173 -0
- package/models/chapter/Node.test.d.ts +1 -0
- package/models/chapter/Node.test.js +27 -0
- package/models/chapter/ParagraphNode.d.ts +33 -0
- package/models/chapter/ParagraphNode.js +93 -0
- package/models/chapter/ParagraphNode.test.d.ts +1 -0
- package/models/chapter/ParagraphNode.test.js +21 -0
- package/models/chapter/Parser.d.ts +75 -0
- package/models/chapter/Parser.js +824 -0
- package/models/chapter/Parser.test.d.ts +1 -0
- package/models/chapter/Parser.test.js +25 -0
- package/models/chapter/Position.d.ts +2 -0
- package/models/chapter/Position.js +1 -0
- package/models/chapter/QuoteNode.d.ts +25 -0
- package/models/chapter/QuoteNode.js +61 -0
- package/models/chapter/ReferenceNode.d.ts +29 -0
- package/models/chapter/ReferenceNode.js +48 -0
- package/models/chapter/RootNode.d.ts +5 -0
- package/models/chapter/RootNode.js +1 -0
- package/models/chapter/RuleNode.d.ts +15 -0
- package/models/chapter/RuleNode.js +16 -0
- package/models/chapter/TableNode.d.ts +35 -0
- package/models/chapter/TableNode.js +176 -0
- package/models/chapter/TableNode.test.d.ts +1 -0
- package/models/chapter/TableNode.test.js +31 -0
- package/models/chapter/TextNode.d.ts +36 -0
- package/models/chapter/TextNode.js +136 -0
- package/models/chapter/TextNode.test.d.ts +1 -0
- package/models/chapter/TextNode.test.js +6 -0
- package/models/loadEdition.d.ts +5 -0
- package/models/loadEdition.js +16 -0
- package/package.json +189 -0
- package/util/getSubdomain.d.ts +3 -0
- package/util/getSubdomain.js +22 -0
- package/util/isMobile.d.ts +2 -0
- package/util/isMobile.js +6 -0
- package/util/mineReference.d.ts +3 -0
- package/util/mineReference.js +62 -0
- package/util/scrollToEyeLevel.d.ts +2 -0
- package/util/scrollToEyeLevel.js +5 -0
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
<script>import ChapterNode from "../../models/chapter/ChapterNode";
|
|
2
|
+
import Parser from "../../models/chapter/Parser";
|
|
3
|
+
import LinkNode from "../../models/chapter/LinkNode";
|
|
4
|
+
import ParagraphNode from "../../models/chapter/ParagraphNode";
|
|
5
|
+
import TextNode from "../../models/chapter/TextNode";
|
|
6
|
+
import AtomNode from "../../models/chapter/AtomNode";
|
|
7
|
+
import FormatNode from "../../models/chapter/FormatNode";
|
|
8
|
+
import BlocksNode from "../../models/chapter/BlocksNode";
|
|
9
|
+
import ListNode from "../../models/chapter/ListNode";
|
|
10
|
+
import TableNode from "../../models/chapter/TableNode";
|
|
11
|
+
import MetadataNode from "../../models/chapter/MetadataNode";
|
|
12
|
+
import CodeNode from "../../models/chapter/CodeNode";
|
|
13
|
+
import BlockNode from "../../models/chapter/BlockNode";
|
|
14
|
+
import EmbedNode from "../../models/chapter/EmbedNode";
|
|
15
|
+
import ErrorNode from "../../models/chapter/ErrorNode";
|
|
16
|
+
import { caretRangeToIndexRange, indexRangeToCaretRange } from "../../models/chapter/Caret";
|
|
17
|
+
import commands from "./Commands";
|
|
18
|
+
import Toolbar from "./Toolbar.svelte";
|
|
19
|
+
import { CARET, getEdition } from "../page/Contexts";
|
|
20
|
+
import { afterUpdate, onMount, setContext } from "svelte";
|
|
21
|
+
import { writable } from "svelte/store";
|
|
22
|
+
const IDLE_TIME = 500;
|
|
23
|
+
export let ast;
|
|
24
|
+
export let save;
|
|
25
|
+
export let chapter;
|
|
26
|
+
export let autofocus = false;
|
|
27
|
+
export let component;
|
|
28
|
+
export let placeholder;
|
|
29
|
+
let element = null;
|
|
30
|
+
let caretRange = undefined;
|
|
31
|
+
let caretCoordinate = undefined;
|
|
32
|
+
let lastInputTime = 0;
|
|
33
|
+
let keyboardIdle = true;
|
|
34
|
+
let editorFocused = false;
|
|
35
|
+
let undoStack = [];
|
|
36
|
+
let undoPosition = -1;
|
|
37
|
+
let clipboard = undefined;
|
|
38
|
+
let editedNode = ast;
|
|
39
|
+
let ignoredInput = false;
|
|
40
|
+
let edition = getEdition();
|
|
41
|
+
onMount(() => {
|
|
42
|
+
const caret = editedNode.getFirstCaret();
|
|
43
|
+
if (caret)
|
|
44
|
+
caretRange = { start: caret, end: caret };
|
|
45
|
+
// Focus the editor on load.
|
|
46
|
+
if (element && autofocus === true) {
|
|
47
|
+
element.focus();
|
|
48
|
+
}
|
|
49
|
+
// Listen to selection changes
|
|
50
|
+
document.addEventListener("selectionchange", handleSelectionChange);
|
|
51
|
+
// Stop listening to selection changes
|
|
52
|
+
return () => {
|
|
53
|
+
document.removeEventListener("selectionchange", handleSelectionChange);
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
onMount(() => {
|
|
57
|
+
// When input happens, set a timer and then see if idle time has elapsed. If it has, track time since last keystroke to control caret blinking behavior.
|
|
58
|
+
const keystrokeTimer = setTimeout(() => {
|
|
59
|
+
// Remember that we're idle so we can render things accordingly.
|
|
60
|
+
const isIdle = Date.now() - lastInputTime > IDLE_TIME;
|
|
61
|
+
if (isIdle) {
|
|
62
|
+
keyboardIdle = true;
|
|
63
|
+
ignoredInput = false;
|
|
64
|
+
}
|
|
65
|
+
}, IDLE_TIME);
|
|
66
|
+
return () => {
|
|
67
|
+
// Clear the timer if we're unmounting or re-rendering.
|
|
68
|
+
clearTimeout(keystrokeTimer);
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
// When the selection changes and is rendered, set the browser's selection to correspond. This helps with two things:
|
|
72
|
+
// 1) we can rely on the browser to render selections rather than rendering it ourselves.
|
|
73
|
+
// 2) We can measure the caret position, so we can render our own fancy one rather than relying on inconsistent cross browser behavior.
|
|
74
|
+
afterUpdate(() => {
|
|
75
|
+
if (!editorFocused)
|
|
76
|
+
return;
|
|
77
|
+
// Measure the location of the selection using the browser's selection.
|
|
78
|
+
let docSelection = document.getSelection();
|
|
79
|
+
let newCaretPosition = undefined;
|
|
80
|
+
if (docSelection) {
|
|
81
|
+
if (caretRange) {
|
|
82
|
+
// Browsers don't render backwards ranges, so we sort this before measuring.
|
|
83
|
+
const sortedRange = caretRange;
|
|
84
|
+
let startNode = document.querySelector(`[data-nodeid='${sortedRange.start.node.nodeID}`);
|
|
85
|
+
let endNode = document.querySelector(`[data-nodeid='${sortedRange.end.node.nodeID}`);
|
|
86
|
+
// If we found both selected nodes and we have a reference to the DOM, update the
|
|
87
|
+
// browser's selection if necessary and measure it's position so we can draw our own caret.
|
|
88
|
+
if (startNode && endNode && element) {
|
|
89
|
+
// If the current selection isn't different from the new one, then we don't do any of this.
|
|
90
|
+
// This prevents an infinite loop when, for example, the user clicks, the browser selection changes,
|
|
91
|
+
// and we update the caret range, which updates the browser selection, ad infinitum.
|
|
92
|
+
let rangeChanged = true;
|
|
93
|
+
let currentRange = undefined;
|
|
94
|
+
if (docSelection.rangeCount > 0) {
|
|
95
|
+
currentRange = docSelection.getRangeAt(0);
|
|
96
|
+
// If our caret range is non-empty TextNodes at a particular index and equal to the browser's caret range, then nothing's changed, so we shouldn't update the selection.
|
|
97
|
+
const startIsEqual = (startNode.childNodes.length > 0 && currentRange.startContainer === startNode.childNodes[0] && currentRange.startOffset === sortedRange.start.index);
|
|
98
|
+
const endIsEqual = (endNode.childNodes.length > 0 && currentRange.endContainer === endNode.childNodes[0] && currentRange.endOffset === sortedRange.end.index);
|
|
99
|
+
if (startIsEqual && endIsEqual)
|
|
100
|
+
rangeChanged = false;
|
|
101
|
+
}
|
|
102
|
+
// We always aim for the DOM text nodes inside of bookish-text, but when the text nodes are the empty string, React doesn't
|
|
103
|
+
// render a DOM text node, and browsers often render an empty bounding box in this case. To measure the caret position,
|
|
104
|
+
// we insert a zero-width space in the empty span, then remove it after measurement.
|
|
105
|
+
// We only do this to the end node if it's different from the start node.
|
|
106
|
+
// We leave them in there so that the "rangeChanged" guard below can track them.
|
|
107
|
+
const emptyStart = startNode.childNodes.length === 0;
|
|
108
|
+
const emptyEnd = startNode !== endNode && endNode.childNodes.length === 0;
|
|
109
|
+
if (emptyStart)
|
|
110
|
+
startNode.appendChild(document.createTextNode('\ufeff'));
|
|
111
|
+
if (emptyEnd)
|
|
112
|
+
endNode.appendChild(document.createTextNode('\ufeff'));
|
|
113
|
+
// If we're just after a new line, set the selection to the character after.
|
|
114
|
+
const afterNewLine = sortedRange.start.node instanceof TextNode &&
|
|
115
|
+
sortedRange.start.index > 0 &&
|
|
116
|
+
sortedRange.start.node.getText().charAt(sortedRange.start.index - 1) === "\n";
|
|
117
|
+
// If the range has changed, set the selection so we can measure it's location.
|
|
118
|
+
// We guard this to avoid triggering unnecessary selection change events that don't actually modify the selection.
|
|
119
|
+
if (rangeChanged) {
|
|
120
|
+
try {
|
|
121
|
+
docSelection.setBaseAndExtent(startNode.childNodes[0], sortedRange.start.index, endNode.childNodes[0], sortedRange.end.index);
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
console.error(e);
|
|
125
|
+
throw Error(`Error setting caret range was set to ${startNode.childNodes[0]}:${sortedRange.start.index} - ${endNode.childNodes[0]}:${sortedRange.end.index}`);
|
|
126
|
+
}
|
|
127
|
+
currentRange = docSelection.getRangeAt(0);
|
|
128
|
+
}
|
|
129
|
+
// Measure and remember caret position
|
|
130
|
+
if (currentRange) {
|
|
131
|
+
const rangeRect = currentRange.getBoundingClientRect();
|
|
132
|
+
const textRect = startNode.getBoundingClientRect();
|
|
133
|
+
const lineHeightString = window.getComputedStyle(startNode).getPropertyValue("line-height");
|
|
134
|
+
const lineHeight = lineHeightString.endsWith("px") ? parseInt(lineHeightString.substring(0, lineHeightString.length - 2)) : undefined;
|
|
135
|
+
// Is the caret in relatively positioned ancestors above the editor? Undo their offsets.
|
|
136
|
+
let caretAncestor = startNode.closest(".bookish-editor");
|
|
137
|
+
let relativeX = 0;
|
|
138
|
+
let relativeY = 0;
|
|
139
|
+
while (caretAncestor != null) {
|
|
140
|
+
if (window.getComputedStyle(caretAncestor).position === "relative") {
|
|
141
|
+
relativeX += caretAncestor.offsetLeft;
|
|
142
|
+
relativeY += caretAncestor.offsetTop;
|
|
143
|
+
}
|
|
144
|
+
caretAncestor = caretAncestor.parentElement;
|
|
145
|
+
}
|
|
146
|
+
// If we're after a new line, calculate the correct position, since selections don't actually render to the next line.
|
|
147
|
+
const left = afterNewLine ? textRect.left : rangeRect.left;
|
|
148
|
+
const top = afterNewLine && lineHeight ? rangeRect.top + lineHeight : rangeRect.top;
|
|
149
|
+
const position = {
|
|
150
|
+
x: left + window.scrollX - relativeX,
|
|
151
|
+
y: top + window.scrollY - relativeY,
|
|
152
|
+
height: rangeRect.height
|
|
153
|
+
};
|
|
154
|
+
newCaretPosition = position;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// If the range is empty, remove the browser's selection too.
|
|
159
|
+
else if (editorFocused)
|
|
160
|
+
document.getSelection()?.empty();
|
|
161
|
+
}
|
|
162
|
+
// Save the new caret position so we render it.
|
|
163
|
+
caretCoordinate = newCaretPosition;
|
|
164
|
+
// If the caret position is outside the window, and the editor is focused, scroll to make the caret visible.
|
|
165
|
+
if (newCaretPosition && editorFocused) {
|
|
166
|
+
const caretTop = newCaretPosition.y;
|
|
167
|
+
const caretBottom = caretTop + newCaretPosition.height;
|
|
168
|
+
const windowHeight = window.innerHeight;
|
|
169
|
+
const toolbar = element?.querySelector(".bookish-editor-toolbar");
|
|
170
|
+
const toolbarHeight = toolbar ? toolbar.clientHeight : 0;
|
|
171
|
+
const buffer = newCaretPosition.height * 5;
|
|
172
|
+
if (caretTop < window.scrollY + buffer + toolbarHeight) {
|
|
173
|
+
window.scrollTo({ top: caretTop - buffer, behavior: 'smooth' });
|
|
174
|
+
}
|
|
175
|
+
else if (caretBottom > window.scrollY + windowHeight - buffer) {
|
|
176
|
+
window.scrollTo({ top: caretBottom - (windowHeight - buffer), behavior: 'smooth' });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
function rangeToCaret(domNode, rangeIndex) {
|
|
181
|
+
// If it's a text node, find the closest TextNode parent
|
|
182
|
+
if (domNode.nodeType === Node.TEXT_NODE) {
|
|
183
|
+
let parent = domNode.parentNode;
|
|
184
|
+
// Keep looking up until we find the TextNode span with the node ID.
|
|
185
|
+
while (parent && !(parent instanceof HTMLElement && parent.classList.contains("bookish-text")))
|
|
186
|
+
parent = parent.parentNode;
|
|
187
|
+
if (parent && parent.dataset.nodeid) {
|
|
188
|
+
const node = editedNode.getNode(parseInt(parent.dataset.nodeid));
|
|
189
|
+
if (node instanceof TextNode)
|
|
190
|
+
// Account for the zero-width spaces that we insert in order to make selections possible on empty text nodes.
|
|
191
|
+
return { node: node, index: Math.min(rangeIndex, node.getLength()) };
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// If it's an element, see if it has a nodeID and handle it accordingly.
|
|
195
|
+
else if (domNode.nodeType === Node.ELEMENT_NODE && domNode instanceof HTMLElement && domNode.dataset.nodeid) {
|
|
196
|
+
const node = editedNode.getNode(parseInt(domNode.dataset.nodeid));
|
|
197
|
+
// These assume that triple clicks on paragraphs in the browser choose text nodes for the selection start
|
|
198
|
+
// and spans or paragraph nodes for the end.
|
|
199
|
+
if (node instanceof ParagraphNode) {
|
|
200
|
+
const first = node.getFirstTextNode();
|
|
201
|
+
return { node: first, index: 0 };
|
|
202
|
+
}
|
|
203
|
+
else if (node instanceof TextNode) {
|
|
204
|
+
return { node: node, index: 0 };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Return nothing by default.
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
function handleSelectionChange() {
|
|
211
|
+
// Make sure we're showing the caret.
|
|
212
|
+
lastInputTime = Date.now();
|
|
213
|
+
keyboardIdle = false;
|
|
214
|
+
// Map the browser selection to our caret range model.
|
|
215
|
+
const selection = document.getSelection();
|
|
216
|
+
if (selection && selection.anchorNode && selection.anchorOffset && selection.focusNode && selection.focusOffset) {
|
|
217
|
+
const start = rangeToCaret(selection.anchorNode, selection.anchorOffset);
|
|
218
|
+
const end = rangeToCaret(selection.focusNode, selection.focusOffset);
|
|
219
|
+
// If we found to text nodes, set the selection
|
|
220
|
+
if (start && end) {
|
|
221
|
+
caretRange = { start: start, end: end };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function getCaretCoordinate(caret) {
|
|
226
|
+
if (!(caret.node instanceof TextNode || caret.node instanceof AtomNode))
|
|
227
|
+
return undefined;
|
|
228
|
+
const domNode = document.querySelector(`[data-nodeid='${caret.node.nodeID}`);
|
|
229
|
+
// If we didn't find the node or there's no text node inside it, then there's no caret position.
|
|
230
|
+
// This happens temporarily before the useEffect above has a chance to insert zero-width non-breaking space.
|
|
231
|
+
if (domNode === null || domNode.childNodes.length === 0)
|
|
232
|
+
return undefined;
|
|
233
|
+
const range = document.createRange();
|
|
234
|
+
range.setStart(domNode.childNodes.length === 0 ? domNode : domNode.childNodes[0], domNode.childNodes.length === 0 ? 0 : caret.index);
|
|
235
|
+
const characterRect = range.getBoundingClientRect();
|
|
236
|
+
if (characterRect.left > 0)
|
|
237
|
+
return { top: characterRect.top, left: characterRect.left, height: characterRect.height };
|
|
238
|
+
const elementRect = domNode.getBoundingClientRect();
|
|
239
|
+
return { top: elementRect.top, left: elementRect.left, height: elementRect.height };
|
|
240
|
+
}
|
|
241
|
+
function getCaretOnLine(caret, below) {
|
|
242
|
+
if (caret.node instanceof TextNode || caret.node instanceof AtomNode) {
|
|
243
|
+
// Find the position of the current start node.
|
|
244
|
+
const startCoordinate = getCaretCoordinate(caret);
|
|
245
|
+
let candidate = editedNode.getAdjacentCaret(caret, below);
|
|
246
|
+
if (candidate === undefined)
|
|
247
|
+
return caret;
|
|
248
|
+
let previousCandidate = undefined;
|
|
249
|
+
let previousCoordinate = undefined;
|
|
250
|
+
if (startCoordinate) {
|
|
251
|
+
while (candidate.node instanceof TextNode || candidate.node instanceof AtomNode) {
|
|
252
|
+
const candidateCoordinate = getCaretCoordinate(candidate);
|
|
253
|
+
// Did we make it to the next line and the character before/after? If we're going down,
|
|
254
|
+
// we're looking for a top that's below the bottom. If we're going up, we're looking for a bottom
|
|
255
|
+
// that's below the top.
|
|
256
|
+
if (candidateCoordinate) {
|
|
257
|
+
const startTop = startCoordinate.top;
|
|
258
|
+
const candidateTop = candidateCoordinate.top;
|
|
259
|
+
const closerCandidate = previousCoordinate && previousCandidate && Math.abs(startCoordinate.left - previousCoordinate.left) < Math.abs(startCoordinate.left - candidateCoordinate.left) ? previousCandidate : candidate;
|
|
260
|
+
if (below) {
|
|
261
|
+
// If this candidate is on the line below the start line, then return the last candidate on the line below the start line.
|
|
262
|
+
if (previousCandidate && previousCoordinate && previousCoordinate.top > startTop + startCoordinate.height && candidateTop > previousCoordinate.top + previousCoordinate.height)
|
|
263
|
+
return previousCandidate;
|
|
264
|
+
// If this is just below the start position, stop and return the closer of the current and previous candidates.
|
|
265
|
+
else if (candidateTop > startTop + startCoordinate.height && candidateCoordinate.left >= startCoordinate.left)
|
|
266
|
+
return closerCandidate;
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
// If this candidate is on the live above the line above the start line, then return the last candidate on the line above the start line.
|
|
270
|
+
if (previousCandidate && previousCoordinate && previousCoordinate.top + previousCoordinate.height < startTop && candidateTop + candidateCoordinate.height < previousCoordinate.top)
|
|
271
|
+
return previousCandidate;
|
|
272
|
+
// If this is just above the start position, stop and return the closer of the current and previous candidates.
|
|
273
|
+
if (candidateTop + candidateCoordinate.height < startTop && candidateCoordinate.left <= startCoordinate.left)
|
|
274
|
+
return closerCandidate;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// Get the next candidate to consider.
|
|
278
|
+
const nextCandidate = editedNode.getAdjacentCaret(candidate, below);
|
|
279
|
+
if (nextCandidate === undefined)
|
|
280
|
+
break;
|
|
281
|
+
// If the caret didn't move, we stop searching, something is wrong.
|
|
282
|
+
if (nextCandidate.node === candidate.node && nextCandidate.index === candidate.index) {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
// Otherwise, we advance to the next candidate.
|
|
286
|
+
previousCandidate = candidate;
|
|
287
|
+
previousCoordinate = candidateCoordinate;
|
|
288
|
+
candidate = nextCandidate;
|
|
289
|
+
if (candidate === undefined)
|
|
290
|
+
return caret;
|
|
291
|
+
}
|
|
292
|
+
return candidate;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// If we didn't find one, just return what we were given.
|
|
296
|
+
return caret;
|
|
297
|
+
}
|
|
298
|
+
function atParagraphStart() {
|
|
299
|
+
if (caretRange === undefined ||
|
|
300
|
+
caretRange.start.node !== caretRange.end.node ||
|
|
301
|
+
caretRange.start.index !== caretRange.end.index ||
|
|
302
|
+
!(caretRange.start.node instanceof TextNode))
|
|
303
|
+
return false;
|
|
304
|
+
const firstCaret = caretRange.start.node.getFormatRoot(editedNode)?.getFirstCaret();
|
|
305
|
+
return firstCaret !== undefined && firstCaret.node === caretRange.start.node && firstCaret.index === caretRange.start.index;
|
|
306
|
+
}
|
|
307
|
+
function undo() {
|
|
308
|
+
// Grab the next state
|
|
309
|
+
let undoState = undoStack[undoPosition + 1];
|
|
310
|
+
// Restore the content of the chapter.
|
|
311
|
+
let node = ast instanceof ChapterNode ? Parser.parseChapter($edition, undoState.bookdown) :
|
|
312
|
+
ast instanceof FormatNode ? Parser.parseFormat($edition, undoState.bookdown) :
|
|
313
|
+
ast instanceof EmbedNode ? Parser.parseEmbed($edition, undoState.bookdown) :
|
|
314
|
+
undefined;
|
|
315
|
+
if (node === undefined || node instanceof ErrorNode)
|
|
316
|
+
return;
|
|
317
|
+
// Restore the view
|
|
318
|
+
editedNode = node;
|
|
319
|
+
// Save the undo.
|
|
320
|
+
save(node);
|
|
321
|
+
// Move the undo state down a position.
|
|
322
|
+
if (undoPosition < undoStack.length)
|
|
323
|
+
undoPosition = undoPosition + 1;
|
|
324
|
+
// Return the original caret.
|
|
325
|
+
const range = indexRangeToCaretRange(node, undoState.range);
|
|
326
|
+
if (range === undefined)
|
|
327
|
+
return;
|
|
328
|
+
return { root: node, range: range };
|
|
329
|
+
}
|
|
330
|
+
function redo() {
|
|
331
|
+
// Grab the next state
|
|
332
|
+
let undoState = undoStack[undoPosition - 1];
|
|
333
|
+
// Restore the content of the chapter.
|
|
334
|
+
let node = ast instanceof ChapterNode ? Parser.parseChapter($edition, undoState.bookdown) :
|
|
335
|
+
ast instanceof FormatNode ? Parser.parseFormat($edition, undoState.bookdown) :
|
|
336
|
+
ast instanceof EmbedNode ? Parser.parseEmbed($edition, undoState.bookdown) :
|
|
337
|
+
undefined;
|
|
338
|
+
if (node === undefined)
|
|
339
|
+
return;
|
|
340
|
+
editedNode = node;
|
|
341
|
+
// Move the undo state down a position.
|
|
342
|
+
if (undoPosition > 0)
|
|
343
|
+
undoPosition = undoPosition - 1;
|
|
344
|
+
// Return the original caret.
|
|
345
|
+
const range = indexRangeToCaretRange(node, undoState.range);
|
|
346
|
+
if (range === undefined)
|
|
347
|
+
return;
|
|
348
|
+
return { root: node, range: range };
|
|
349
|
+
}
|
|
350
|
+
function getCaretContext() {
|
|
351
|
+
if (caretRange === undefined)
|
|
352
|
+
return undefined;
|
|
353
|
+
// Determine whether the range contains a list
|
|
354
|
+
const nodes = editedNode.getNodes();
|
|
355
|
+
let inside = false;
|
|
356
|
+
let includesList = false;
|
|
357
|
+
nodes.forEach(n => {
|
|
358
|
+
if (caretRange === undefined)
|
|
359
|
+
return;
|
|
360
|
+
if (n === caretRange.start.node)
|
|
361
|
+
inside = true;
|
|
362
|
+
if (inside && n.getClosestParentMatching(editedNode, p => p instanceof ListNode) !== undefined)
|
|
363
|
+
includesList = true;
|
|
364
|
+
if (n === caretRange.end.node)
|
|
365
|
+
inside = false;
|
|
366
|
+
});
|
|
367
|
+
const parents = editedNode.getParentsOf(caretRange.start.node)?.reverse();
|
|
368
|
+
return {
|
|
369
|
+
// We make a new range so that setCaretRange always causes a re-render
|
|
370
|
+
chapter: chapter,
|
|
371
|
+
range: { start: caretRange.start, end: caretRange.end },
|
|
372
|
+
start: caretRange.start,
|
|
373
|
+
end: caretRange.end,
|
|
374
|
+
isSelection: caretRange.start.node !== caretRange.end.node || caretRange.start.index !== caretRange.end.index,
|
|
375
|
+
root: editedNode,
|
|
376
|
+
blocks: parents?.find(n => n instanceof BlocksNode),
|
|
377
|
+
paragraph: parents?.find(n => n instanceof ParagraphNode),
|
|
378
|
+
block: parents?.find(n => n instanceof BlockNode),
|
|
379
|
+
code: parents?.find(n => n instanceof CodeNode),
|
|
380
|
+
list: parents?.find(n => n instanceof ListNode),
|
|
381
|
+
atom: parents?.find(n => n instanceof AtomNode),
|
|
382
|
+
meta: parents?.find(n => n instanceof MetadataNode),
|
|
383
|
+
includesList: includesList,
|
|
384
|
+
table: parents?.find(n => n instanceof TableNode),
|
|
385
|
+
format: (caretRange.end.node instanceof TextNode || caretRange.end.node instanceof AtomNode) ? caretRange.end.node.getFormatRoot(editedNode) : undefined,
|
|
386
|
+
startIsText: caretRange.start.node instanceof TextNode,
|
|
387
|
+
endIsText: caretRange.end.node instanceof TextNode,
|
|
388
|
+
startIsTextOrAtom: caretRange.start.node instanceof TextNode || caretRange.start.node instanceof AtomNode,
|
|
389
|
+
endIsTextOrAtom: caretRange.end.node instanceof TextNode || caretRange.end.node instanceof AtomNode,
|
|
390
|
+
atParagraphStart: atParagraphStart(),
|
|
391
|
+
undoStack: undoStack,
|
|
392
|
+
undoPosition: undoPosition,
|
|
393
|
+
undo: undo,
|
|
394
|
+
redo: redo,
|
|
395
|
+
clipboard: clipboard,
|
|
396
|
+
handleCopy: handleCopy,
|
|
397
|
+
handlePaste: handlePaste
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function getUtilities() {
|
|
401
|
+
return {
|
|
402
|
+
getCaretOnLine: getCaretOnLine
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function handleKeyDown(event) {
|
|
406
|
+
// Only handle keystrokes when this is focused.
|
|
407
|
+
// Otherwise, we let any focusable elements in this editor handle them.
|
|
408
|
+
if (!editorFocused)
|
|
409
|
+
return;
|
|
410
|
+
// Remember the time of this keystroke.
|
|
411
|
+
lastInputTime = Date.now();
|
|
412
|
+
keyboardIdle = false;
|
|
413
|
+
if (caretRange === undefined)
|
|
414
|
+
return;
|
|
415
|
+
// Build some context
|
|
416
|
+
const context = getCaretContext();
|
|
417
|
+
if (context === undefined)
|
|
418
|
+
return;
|
|
419
|
+
// Loop through the commands to see if there's a match.
|
|
420
|
+
const unmatched = commands.every(command => {
|
|
421
|
+
// If the keystroke and caret position matches the command signature, execute the command and update the caret range.
|
|
422
|
+
if ((command.shift === undefined || command.shift === event.shiftKey) &&
|
|
423
|
+
command.alt === event.altKey &&
|
|
424
|
+
command.control === (event.ctrlKey || event.metaKey) &&
|
|
425
|
+
(command.key === undefined || command.key === event.key || (Array.isArray(command.key) && command.key.includes(event.key))) &&
|
|
426
|
+
(command.code === undefined || command.code === event.code) &&
|
|
427
|
+
(command.active === true || (command.active instanceof Function && command.active.call(undefined, context, event.key)))) {
|
|
428
|
+
event.preventDefault();
|
|
429
|
+
event.stopPropagation();
|
|
430
|
+
// Execute the command
|
|
431
|
+
executeCommand(command, event.key);
|
|
432
|
+
// Stop searching for a matching command.
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
// Continue searching for a matching command.
|
|
436
|
+
return true;
|
|
437
|
+
});
|
|
438
|
+
if (unmatched) {
|
|
439
|
+
// Toolbar navigation
|
|
440
|
+
if (event.key === "Tab") {
|
|
441
|
+
// If we've selected a FootnoteNode or Comment, navigate to the footnote or comment text.
|
|
442
|
+
if (caretRange.start.node instanceof AtomNode && caretRange.start.node.getMeta() instanceof FormatNode) {
|
|
443
|
+
const firstCaret = caretRange.start.node.getMeta().getFirstCaret();
|
|
444
|
+
caretRange = { start: firstCaret, end: firstCaret };
|
|
445
|
+
event.preventDefault();
|
|
446
|
+
event.stopPropagation();
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
else if (caretRange.start.node.isInside(editedNode, AtomNode)) {
|
|
450
|
+
const atom = caretRange.start.node.getClosestParentOfType(editedNode, AtomNode);
|
|
451
|
+
if (atom) {
|
|
452
|
+
const atomCaret = { node: atom, index: 0 };
|
|
453
|
+
caretRange = { start: atomCaret, end: atomCaret };
|
|
454
|
+
event.preventDefault();
|
|
455
|
+
event.stopPropagation();
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (element) {
|
|
460
|
+
const controls = [
|
|
461
|
+
element.querySelector(".bookish-editor-toolbar input"),
|
|
462
|
+
element.querySelector(".bookish-editor-toolbar select"),
|
|
463
|
+
element.querySelector(".bookish-editor-toolbar button")
|
|
464
|
+
];
|
|
465
|
+
const match = controls.find(control => control && control instanceof HTMLElement);
|
|
466
|
+
if (match && match instanceof HTMLElement) {
|
|
467
|
+
match.focus();
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
event.stopPropagation();
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function executeCommand(command, key) {
|
|
477
|
+
// Assume we process it, then flip it if we don't.
|
|
478
|
+
ignoredInput = false;
|
|
479
|
+
const context = getCaretContext();
|
|
480
|
+
if (context && caretRange) {
|
|
481
|
+
const results = command.handler.call(undefined, context, getUtilities(), key);
|
|
482
|
+
// If the command invoked produced a new range
|
|
483
|
+
if (results !== undefined) {
|
|
484
|
+
const { root, range } = results;
|
|
485
|
+
const newRange = { start: range.start, end: range.end };
|
|
486
|
+
if (root === editedNode && command.category !== "navigation" && command.category !== "selection")
|
|
487
|
+
console.error(`Warning: immutability violation on ${command.description}`);
|
|
488
|
+
// Set the range to force a rerender, assuming something in the document changed.
|
|
489
|
+
caretRange = newRange;
|
|
490
|
+
// Save the copy in the undo stack if this isn't a navigation or selection state.
|
|
491
|
+
if (command.category !== "navigation" && command.category !== "selection" && command.category !== "history") {
|
|
492
|
+
saveEdit(root, newRange, command);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
else
|
|
496
|
+
ignoredInput = true;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
function saveEdit(newRoot, newRange, command) {
|
|
500
|
+
// Call the save callback.
|
|
501
|
+
save(newRoot);
|
|
502
|
+
// Initialize a new history if we don't have one.
|
|
503
|
+
let newStack = undoStack.length > 0 ? undoStack : [];
|
|
504
|
+
// If there's nothing on the stack, save the initial state before we add something to the stack.
|
|
505
|
+
if (undoStack.length === 0 && caretRange !== undefined) {
|
|
506
|
+
const currentRange = caretRangeToIndexRange(editedNode, caretRange);
|
|
507
|
+
if (currentRange !== undefined)
|
|
508
|
+
newStack = [{
|
|
509
|
+
bookdown: editedNode.toBookdown(),
|
|
510
|
+
command: undefined,
|
|
511
|
+
range: currentRange
|
|
512
|
+
}];
|
|
513
|
+
}
|
|
514
|
+
// Change the chapter's AST.
|
|
515
|
+
editedNode = newRoot;
|
|
516
|
+
// Set the new undo stack, pre-pending the new command to the front.
|
|
517
|
+
const newTextRange = caretRangeToIndexRange(newRoot, newRange);
|
|
518
|
+
if (newTextRange !== undefined)
|
|
519
|
+
undoStack = [{
|
|
520
|
+
bookdown: newRoot.toBookdown(),
|
|
521
|
+
command: command,
|
|
522
|
+
range: newTextRange
|
|
523
|
+
// If the undo position is beyond the front, clear everything before it, because we're changing history.
|
|
524
|
+
}, ...(undoPosition > 0 ? newStack.slice(undoPosition) : newStack)];
|
|
525
|
+
// Set the undo position to the last index.
|
|
526
|
+
undoPosition = 0;
|
|
527
|
+
}
|
|
528
|
+
function editNode(previous, edited) {
|
|
529
|
+
if (caretRange) {
|
|
530
|
+
const newRoot = editedNode.withNodeReplaced(previous, edited);
|
|
531
|
+
if (newRoot === undefined)
|
|
532
|
+
return;
|
|
533
|
+
saveEdit(newRoot, { start: caretRange.start, end: caretRange.end });
|
|
534
|
+
// Update the range if the current range contains the previous node. This will generally
|
|
535
|
+
// be true any time a selected atom node is edited.
|
|
536
|
+
if (caretRange.start.node === previous && (edited instanceof AtomNode || edited instanceof TextNode))
|
|
537
|
+
caretRange = { start: { node: edited, index: 0 }, end: { node: edited, index: 0 } };
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function handleMouseDown() {
|
|
541
|
+
if (!element)
|
|
542
|
+
return;
|
|
543
|
+
// Grab focus.
|
|
544
|
+
element.focus();
|
|
545
|
+
// Update the caret range to ensure it's positioned correctly, since it depends on relative positioned parents.
|
|
546
|
+
if (caretRange)
|
|
547
|
+
caretRange = { start: caretRange.start, end: caretRange.end };
|
|
548
|
+
// If we've selected a non-TextNode, release it, so the browser is free to select a text node.
|
|
549
|
+
if (caretRange && !(caretRange.start.node instanceof TextNode || caretRange.start.node instanceof AtomNode)) {
|
|
550
|
+
caretRange = undefined;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function handleKeyUp(event) {
|
|
554
|
+
event.preventDefault();
|
|
555
|
+
}
|
|
556
|
+
function forceUpdate() {
|
|
557
|
+
if (caretRange !== undefined) {
|
|
558
|
+
caretRange = { start: caretRange.start, end: caretRange.end };
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
function updateFocus() {
|
|
562
|
+
editorFocused = document.activeElement === element || (element !== null && element.contains(document.activeElement));
|
|
563
|
+
}
|
|
564
|
+
function handleCopy(node) {
|
|
565
|
+
// Set the editor's clipboard.
|
|
566
|
+
clipboard = node;
|
|
567
|
+
// Set the OS clipboard.
|
|
568
|
+
if (navigator.clipboard) {
|
|
569
|
+
navigator.clipboard.write([
|
|
570
|
+
new ClipboardItem({
|
|
571
|
+
"text/plain": new Blob([node.toBookdown()], { type: "text/plain" }),
|
|
572
|
+
"text/html": new Blob([node.toHTML()], { type: "text/html" })
|
|
573
|
+
})
|
|
574
|
+
]);
|
|
575
|
+
}
|
|
576
|
+
else
|
|
577
|
+
window.alert("Your browser doesn't support copying to the clipboard :(");
|
|
578
|
+
}
|
|
579
|
+
function handlePaste(context, node, save) {
|
|
580
|
+
// Track revisions in these two variables.
|
|
581
|
+
let newRoot = context.root;
|
|
582
|
+
let newCaret = context.start;
|
|
583
|
+
// Is there any text to remove? Remove it first.
|
|
584
|
+
if (context.isSelection) {
|
|
585
|
+
const edit = newRoot.withRangeFormatted(context.range, undefined);
|
|
586
|
+
if (edit === undefined || !(edit.root instanceof ChapterNode))
|
|
587
|
+
return;
|
|
588
|
+
newRoot = edit.root;
|
|
589
|
+
newCaret = edit.range.start;
|
|
590
|
+
}
|
|
591
|
+
// Find the parents of the caret node, from nearest to furthest
|
|
592
|
+
const parents = context.root.getParentsOf(newCaret.node);
|
|
593
|
+
if (parents === undefined)
|
|
594
|
+
return;
|
|
595
|
+
// Pop parents, nearest to farthest, searching for one that handles an insertion.
|
|
596
|
+
let edit = undefined;
|
|
597
|
+
let parent = undefined;
|
|
598
|
+
while (parents.length > 0) {
|
|
599
|
+
parent = parents.pop();
|
|
600
|
+
if (parent === undefined)
|
|
601
|
+
break;
|
|
602
|
+
// It's critical that we insert a copy and not the node in case people copy multiple times. A node can only appear once in a tree.
|
|
603
|
+
edit = parent.withNodeInserted(newCaret, node.copy());
|
|
604
|
+
if (edit !== undefined)
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
// Insert the node at the caret.
|
|
608
|
+
if (parent !== undefined && edit !== undefined) {
|
|
609
|
+
const newChapter = context.root.withNodeReplaced(parent, edit.root);
|
|
610
|
+
if (newChapter === undefined)
|
|
611
|
+
return;
|
|
612
|
+
if (save) {
|
|
613
|
+
// Set the range to force a rerender, assuming something in the document changed.
|
|
614
|
+
caretRange = edit.range;
|
|
615
|
+
// Save the copy in the undo stack if this isn't a navigation or selection state.
|
|
616
|
+
saveEdit(newChapter, edit.range);
|
|
617
|
+
}
|
|
618
|
+
else
|
|
619
|
+
return { root: newChapter, range: edit.range };
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
$: isAtom = caretRange && caretRange.start.node instanceof AtomNode;
|
|
623
|
+
$: inAtom = caretRange && caretRange.start.node.isInside(editedNode, AtomNode);
|
|
624
|
+
$: isSelection = caretRange && (caretRange.start.node !== caretRange.end.node || caretRange.start.index !== caretRange.end.index);
|
|
625
|
+
$: isItalic = caretRange && !isSelection && caretRange.start.node instanceof TextNode && caretRange.start.node.isItalic(editedNode);
|
|
626
|
+
$: isBold = caretRange && !isSelection && caretRange.start.node instanceof TextNode && caretRange.start.node.isBold(editedNode);
|
|
627
|
+
$: isLink = caretRange && !isSelection && caretRange.start.node.getClosestParentMatching(editedNode, p => p instanceof LinkNode) !== undefined;
|
|
628
|
+
let context = undefined;
|
|
629
|
+
$: {
|
|
630
|
+
caretRange = caretRange;
|
|
631
|
+
editedNode = editedNode;
|
|
632
|
+
context = getCaretContext();
|
|
633
|
+
}
|
|
634
|
+
// Create a caret store for the editor and make it available to children.
|
|
635
|
+
let caretStore = setContext(CARET, writable());
|
|
636
|
+
// Update the caret store it whenever any of the below change.
|
|
637
|
+
$: caretStore.set({
|
|
638
|
+
range: caretRange,
|
|
639
|
+
coordinate: caretCoordinate,
|
|
640
|
+
forceUpdate: forceUpdate,
|
|
641
|
+
setCaret: range => { caretRange = range; element?.focus(); },
|
|
642
|
+
edit: editNode,
|
|
643
|
+
context: context,
|
|
644
|
+
root: editedNode,
|
|
645
|
+
focused: editorFocused
|
|
646
|
+
});
|
|
647
|
+
</script>
|
|
648
|
+
|
|
649
|
+
<div
|
|
650
|
+
class={`bookish-editor ${inAtom ? "bookish-editor-atom-focused" : ""}`}
|
|
651
|
+
bind:this={element}
|
|
652
|
+
on:keydown={handleKeyDown}
|
|
653
|
+
on:keyup={handleKeyUp}
|
|
654
|
+
on:mousedown|stopPropagation={handleMouseDown}
|
|
655
|
+
on:focus={updateFocus}
|
|
656
|
+
on:blur={updateFocus}
|
|
657
|
+
role="textbox"
|
|
658
|
+
tabIndex=0
|
|
659
|
+
>
|
|
660
|
+
{#if context && caretCoordinate }
|
|
661
|
+
<Toolbar context={context} executor={executeCommand} visible={editorFocused} />
|
|
662
|
+
{/if}
|
|
663
|
+
<!-- Draw a caret. We draw our own since this view isn't contentEditable and we can't show a caret.
|
|
664
|
+
Customize the rendering based on the formatting applied to the text node. -->
|
|
665
|
+
{#if caretCoordinate && caretRange && !isAtom && !isSelection && editorFocused }
|
|
666
|
+
<div
|
|
667
|
+
class={`bookish-editor-caret ${isLink ? "bookish-editor-caret-linked" : isItalic ? "bookish-editor-caret-italic" :""} ${isBold ? "bookish-editor-caret-bold" : ""} ${editorFocused && keyboardIdle ? "bookish-editor-caret-blink" : ""} ${!editorFocused ? "bookish-editor-caret-disabled" : ""} ${ignoredInput ? "bookish-editor-caret-ignored" : ""}`}
|
|
668
|
+
style={`left: ${caretCoordinate.x}px; top: ${caretCoordinate.y}px; height: ${caretCoordinate.height}px;`}>
|
|
669
|
+
</div>
|
|
670
|
+
{/if}
|
|
671
|
+
<svelte:component this={component} node={editedNode} placeholder={placeholder} />
|
|
672
|
+
</div>
|
|
673
|
+
|
|
674
|
+
<style>
|
|
675
|
+
.bookish-editor:focus {
|
|
676
|
+
outline: 2px solid var(--bookish-highlight-color);
|
|
677
|
+
outline-offset: var(--bookish-app-chrome-padding);
|
|
678
|
+
border-radius: var(--bookish-app-chrome-roundedness);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
.bookish-editor.bookish-editor-atom-focused:focus {
|
|
682
|
+
outline: none;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
.bookish-editor-caret {
|
|
686
|
+
left: 0;
|
|
687
|
+
top: 0;
|
|
688
|
+
width: 0px;
|
|
689
|
+
background: none;
|
|
690
|
+
outline: 1px solid var(--bookish-paragraph-color);
|
|
691
|
+
display: inline-block;
|
|
692
|
+
box-sizing: border-box;
|
|
693
|
+
position: absolute;
|
|
694
|
+
z-index: 1;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
@keyframes bookish-editor-caret-blink {
|
|
698
|
+
100% {
|
|
699
|
+
outline: none;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
.bookish-editor-caret-disabled {
|
|
704
|
+
outline-color: var(--bookish-app-chrome-muted);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
.bookish-editor-caret-blink {
|
|
708
|
+
animation: bookish-editor-caret-blink 1s steps(2) infinite;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
.bookish-editor-caret-italic {
|
|
712
|
+
transform: skew(-10deg);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
.bookish-editor-caret-bold {
|
|
716
|
+
outline-width: 2px;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
.bookish-editor-caret-ignored {
|
|
720
|
+
animation: failure 100ms 10;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
.bookish-editor-caret-linked {
|
|
724
|
+
outline-width: 1px;
|
|
725
|
+
outline-color: var(--bookish-link-color);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
.bookish-editor-inline-editor {
|
|
729
|
+
display: inline-block;
|
|
730
|
+
position: relative;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
.bookish-editor-inline-form {
|
|
734
|
+
position: absolute;
|
|
735
|
+
left: 0;
|
|
736
|
+
top: 0;
|
|
737
|
+
min-width: 7em;
|
|
738
|
+
width: auto;
|
|
739
|
+
z-index: 2;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
.bookish-link-active {
|
|
743
|
+
font-weight: bold;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
:global(.bookish-editor .bookish-table table td) {
|
|
747
|
+
border: var(--bookish-app-chrome-border-width) dashed var(--bookish-app-chrome-border-color);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
.bookish-editor :global(hr) {
|
|
751
|
+
margin-top: 1em;
|
|
752
|
+
margin-bottom: 1em;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
</style>
|