bookish-press 0.7.4 → 0.8.2

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