@kujoforall/paper-ui 0.0.0-stage → 0.1.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/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +252 -2
- package/dist/components/editor/block-text.d.ts +8 -0
- package/dist/components/editor/block-text.d.ts.map +1 -0
- package/dist/components/editor/block-text.js +213 -0
- package/dist/components/editor/block-text.js.map +1 -0
- package/dist/components/editor/blocks.d.ts +33 -0
- package/dist/components/editor/blocks.d.ts.map +1 -0
- package/dist/components/editor/blocks.js +187 -0
- package/dist/components/editor/blocks.js.map +1 -0
- package/dist/components/editor/document-editor.d.ts +27 -0
- package/dist/components/editor/document-editor.d.ts.map +1 -0
- package/dist/components/editor/document-editor.js +40 -0
- package/dist/components/editor/document-editor.js.map +1 -0
- package/dist/components/editor/document-view.d.ts +22 -0
- package/dist/components/editor/document-view.d.ts.map +1 -0
- package/dist/components/editor/document-view.js +105 -0
- package/dist/components/editor/document-view.js.map +1 -0
- package/dist/components/editor/editor-state.d.ts +100 -0
- package/dist/components/editor/editor-state.d.ts.map +1 -0
- package/dist/components/editor/editor-state.js +192 -0
- package/dist/components/editor/editor-state.js.map +1 -0
- package/dist/components/editor/extensions/spoiler.d.ts +8 -0
- package/dist/components/editor/extensions/spoiler.d.ts.map +1 -0
- package/dist/components/editor/extensions/spoiler.js +36 -0
- package/dist/components/editor/extensions/spoiler.js.map +1 -0
- package/dist/components/editor/index.d.ts +9 -0
- package/dist/components/editor/index.d.ts.map +1 -0
- package/dist/components/editor/index.js +9 -0
- package/dist/components/editor/index.js.map +1 -0
- package/dist/components/editor/kinds.d.ts +99 -0
- package/dist/components/editor/kinds.d.ts.map +1 -0
- package/dist/components/editor/kinds.js +74 -0
- package/dist/components/editor/kinds.js.map +1 -0
- package/dist/components/sketch/rough.d.ts +38 -0
- package/dist/components/sketch/rough.d.ts.map +1 -0
- package/dist/components/sketch/rough.js +57 -0
- package/dist/components/sketch/rough.js.map +1 -0
- package/dist/components/ui/accent-picker.d.ts +2 -0
- package/dist/components/ui/accent-picker.d.ts.map +1 -0
- package/dist/components/ui/accent-picker.js +44 -0
- package/dist/components/ui/accent-picker.js.map +1 -0
- package/dist/components/ui/address-field.d.ts +10 -0
- package/dist/components/ui/address-field.d.ts.map +1 -0
- package/dist/components/ui/address-field.js +24 -0
- package/dist/components/ui/address-field.js.map +1 -0
- package/dist/components/ui/app-shell.d.ts +25 -0
- package/dist/components/ui/app-shell.d.ts.map +1 -0
- package/dist/components/ui/app-shell.js +69 -0
- package/dist/components/ui/app-shell.js.map +1 -0
- package/dist/components/ui/badge.d.ts +19 -0
- package/dist/components/ui/badge.d.ts.map +1 -0
- package/dist/components/ui/badge.js +62 -0
- package/dist/components/ui/badge.js.map +1 -0
- package/dist/components/ui/breadcrumbs.d.ts +10 -0
- package/dist/components/ui/breadcrumbs.d.ts.map +1 -0
- package/dist/components/ui/breadcrumbs.js +9 -0
- package/dist/components/ui/breadcrumbs.js.map +1 -0
- package/dist/components/ui/button.d.ts +13 -0
- package/dist/components/ui/button.d.ts.map +1 -0
- package/dist/components/ui/button.js +45 -0
- package/dist/components/ui/button.js.map +1 -0
- package/dist/components/ui/calendar.d.ts +5 -0
- package/dist/components/ui/calendar.d.ts.map +1 -0
- package/dist/components/ui/calendar.js +64 -0
- package/dist/components/ui/calendar.js.map +1 -0
- package/dist/components/ui/callout.d.ts +13 -0
- package/dist/components/ui/callout.d.ts.map +1 -0
- package/dist/components/ui/callout.js +27 -0
- package/dist/components/ui/callout.js.map +1 -0
- package/dist/components/ui/card.d.ts +13 -0
- package/dist/components/ui/card.d.ts.map +1 -0
- package/dist/components/ui/card.js +27 -0
- package/dist/components/ui/card.js.map +1 -0
- package/dist/components/ui/checkbox.d.ts +6 -0
- package/dist/components/ui/checkbox.d.ts.map +1 -0
- package/dist/components/ui/checkbox.js +10 -0
- package/dist/components/ui/checkbox.js.map +1 -0
- package/dist/components/ui/command-menu.d.ts +15 -0
- package/dist/components/ui/command-menu.d.ts.map +1 -0
- package/dist/components/ui/command-menu.js +24 -0
- package/dist/components/ui/command-menu.js.map +1 -0
- package/dist/components/ui/date-picker.d.ts +35 -0
- package/dist/components/ui/date-picker.d.ts.map +1 -0
- package/dist/components/ui/date-picker.js +56 -0
- package/dist/components/ui/date-picker.js.map +1 -0
- package/dist/components/ui/date-time-picker.d.ts +19 -0
- package/dist/components/ui/date-time-picker.d.ts.map +1 -0
- package/dist/components/ui/date-time-picker.js +32 -0
- package/dist/components/ui/date-time-picker.js.map +1 -0
- package/dist/components/ui/dialog.d.ts +17 -0
- package/dist/components/ui/dialog.d.ts.map +1 -0
- package/dist/components/ui/dialog.js +33 -0
- package/dist/components/ui/dialog.js.map +1 -0
- package/dist/components/ui/dropdown-menu.d.ts +20 -0
- package/dist/components/ui/dropdown-menu.d.ts.map +1 -0
- package/dist/components/ui/dropdown-menu.js +56 -0
- package/dist/components/ui/dropdown-menu.js.map +1 -0
- package/dist/components/ui/editable-text.d.ts +17 -0
- package/dist/components/ui/editable-text.d.ts.map +1 -0
- package/dist/components/ui/editable-text.js +68 -0
- package/dist/components/ui/editable-text.js.map +1 -0
- package/dist/components/ui/field-button.d.ts +14 -0
- package/dist/components/ui/field-button.d.ts.map +1 -0
- package/dist/components/ui/field-button.js +16 -0
- package/dist/components/ui/field-button.js.map +1 -0
- package/dist/components/ui/file-preview.d.ts +5 -0
- package/dist/components/ui/file-preview.d.ts.map +1 -0
- package/dist/components/ui/file-preview.js +66 -0
- package/dist/components/ui/file-preview.js.map +1 -0
- package/dist/components/ui/file-uploader.d.ts +39 -0
- package/dist/components/ui/file-uploader.d.ts.map +1 -0
- package/dist/components/ui/file-uploader.js +110 -0
- package/dist/components/ui/file-uploader.js.map +1 -0
- package/dist/components/ui/handle-row.d.ts +15 -0
- package/dist/components/ui/handle-row.d.ts.map +1 -0
- package/dist/components/ui/handle-row.js +41 -0
- package/dist/components/ui/handle-row.js.map +1 -0
- package/dist/components/ui/image-block.d.ts +31 -0
- package/dist/components/ui/image-block.d.ts.map +1 -0
- package/dist/components/ui/image-block.js +103 -0
- package/dist/components/ui/image-block.js.map +1 -0
- package/dist/components/ui/image-card.d.ts +17 -0
- package/dist/components/ui/image-card.d.ts.map +1 -0
- package/dist/components/ui/image-card.js +38 -0
- package/dist/components/ui/image-card.js.map +1 -0
- package/dist/components/ui/image-viewer.d.ts +31 -0
- package/dist/components/ui/image-viewer.d.ts.map +1 -0
- package/dist/components/ui/image-viewer.js +81 -0
- package/dist/components/ui/image-viewer.js.map +1 -0
- package/dist/components/ui/image.d.ts +11 -0
- package/dist/components/ui/image.d.ts.map +1 -0
- package/dist/components/ui/image.js +32 -0
- package/dist/components/ui/image.js.map +1 -0
- package/dist/components/ui/index.d.ts +49 -0
- package/dist/components/ui/index.d.ts.map +1 -0
- package/dist/components/ui/index.js +49 -0
- package/dist/components/ui/index.js.map +1 -0
- package/dist/components/ui/input.d.ts +16 -0
- package/dist/components/ui/input.d.ts.map +1 -0
- package/dist/components/ui/input.js +32 -0
- package/dist/components/ui/input.js.map +1 -0
- package/dist/components/ui/link-block.d.ts +13 -0
- package/dist/components/ui/link-block.d.ts.map +1 -0
- package/dist/components/ui/link-block.js +21 -0
- package/dist/components/ui/link-block.js.map +1 -0
- package/dist/components/ui/mac-card.d.ts +15 -0
- package/dist/components/ui/mac-card.d.ts.map +1 -0
- package/dist/components/ui/mac-card.js +9 -0
- package/dist/components/ui/mac-card.js.map +1 -0
- package/dist/components/ui/nav-tree.d.ts +16 -0
- package/dist/components/ui/nav-tree.d.ts.map +1 -0
- package/dist/components/ui/nav-tree.js +34 -0
- package/dist/components/ui/nav-tree.js.map +1 -0
- package/dist/components/ui/page.d.ts +12 -0
- package/dist/components/ui/page.d.ts.map +1 -0
- package/dist/components/ui/page.js +15 -0
- package/dist/components/ui/page.js.map +1 -0
- package/dist/components/ui/popover.d.ts +8 -0
- package/dist/components/ui/popover.d.ts.map +1 -0
- package/dist/components/ui/popover.js +18 -0
- package/dist/components/ui/popover.js.map +1 -0
- package/dist/components/ui/progress.d.ts +9 -0
- package/dist/components/ui/progress.d.ts.map +1 -0
- package/dist/components/ui/progress.js +9 -0
- package/dist/components/ui/progress.js.map +1 -0
- package/dist/components/ui/property.d.ts +15 -0
- package/dist/components/ui/property.d.ts.map +1 -0
- package/dist/components/ui/property.js +34 -0
- package/dist/components/ui/property.js.map +1 -0
- package/dist/components/ui/segmented.d.ts +13 -0
- package/dist/components/ui/segmented.d.ts.map +1 -0
- package/dist/components/ui/segmented.js +10 -0
- package/dist/components/ui/segmented.js.map +1 -0
- package/dist/components/ui/select-menu.d.ts +10 -0
- package/dist/components/ui/select-menu.d.ts.map +1 -0
- package/dist/components/ui/select-menu.js +10 -0
- package/dist/components/ui/select-menu.js.map +1 -0
- package/dist/components/ui/select.d.ts +11 -0
- package/dist/components/ui/select.d.ts.map +1 -0
- package/dist/components/ui/select.js +31 -0
- package/dist/components/ui/select.js.map +1 -0
- package/dist/components/ui/selection-bar.d.ts +12 -0
- package/dist/components/ui/selection-bar.d.ts.map +1 -0
- package/dist/components/ui/selection-bar.js +15 -0
- package/dist/components/ui/selection-bar.js.map +1 -0
- package/dist/components/ui/separator.d.ts +4 -0
- package/dist/components/ui/separator.d.ts.map +1 -0
- package/dist/components/ui/separator.js +12 -0
- package/dist/components/ui/separator.js.map +1 -0
- package/dist/components/ui/spotlight.d.ts +23 -0
- package/dist/components/ui/spotlight.d.ts.map +1 -0
- package/dist/components/ui/spotlight.js +107 -0
- package/dist/components/ui/spotlight.js.map +1 -0
- package/dist/components/ui/stat-tile.d.ts +8 -0
- package/dist/components/ui/stat-tile.d.ts.map +1 -0
- package/dist/components/ui/stat-tile.js +9 -0
- package/dist/components/ui/stat-tile.js.map +1 -0
- package/dist/components/ui/step-dialog.d.ts +20 -0
- package/dist/components/ui/step-dialog.d.ts.map +1 -0
- package/dist/components/ui/step-dialog.js +36 -0
- package/dist/components/ui/step-dialog.js.map +1 -0
- package/dist/components/ui/table.d.ts +8 -0
- package/dist/components/ui/table.d.ts.map +1 -0
- package/dist/components/ui/table.js +23 -0
- package/dist/components/ui/table.js.map +1 -0
- package/dist/components/ui/theme-toggle.d.ts +2 -0
- package/dist/components/ui/theme-toggle.d.ts.map +1 -0
- package/dist/components/ui/theme-toggle.js +49 -0
- package/dist/components/ui/theme-toggle.js.map +1 -0
- package/dist/components/ui/time-picker.d.ts +21 -0
- package/dist/components/ui/time-picker.d.ts.map +1 -0
- package/dist/components/ui/time-picker.js +63 -0
- package/dist/components/ui/time-picker.js.map +1 -0
- package/dist/components/ui/tooltip.d.ts +15 -0
- package/dist/components/ui/tooltip.d.ts.map +1 -0
- package/dist/components/ui/tooltip.js +21 -0
- package/dist/components/ui/tooltip.js.map +1 -0
- package/dist/components/ui/typography.d.ts +34 -0
- package/dist/components/ui/typography.d.ts.map +1 -0
- package/dist/components/ui/typography.js +72 -0
- package/dist/components/ui/typography.js.map +1 -0
- package/dist/components/ui/video-block.d.ts +14 -0
- package/dist/components/ui/video-block.d.ts.map +1 -0
- package/dist/components/ui/video-block.js +18 -0
- package/dist/components/ui/video-block.js.map +1 -0
- package/dist/hooks/use-save-queue.d.ts +13 -0
- package/dist/hooks/use-save-queue.d.ts.map +1 -0
- package/dist/hooks/use-save-queue.js +45 -0
- package/dist/hooks/use-save-queue.js.map +1 -0
- package/dist/hooks/use-selection.d.ts +10 -0
- package/dist/hooks/use-selection.d.ts.map +1 -0
- package/dist/hooks/use-selection.js +43 -0
- package/dist/hooks/use-selection.js.map +1 -0
- package/dist/hooks/use-shared-transition.d.ts +3 -0
- package/dist/hooks/use-shared-transition.d.ts.map +1 -0
- package/dist/hooks/use-shared-transition.js +38 -0
- package/dist/hooks/use-shared-transition.js.map +1 -0
- package/dist/hooks/use-upload-queue.d.ts +22 -0
- package/dist/hooks/use-upload-queue.d.ts.map +1 -0
- package/dist/hooks/use-upload-queue.js +78 -0
- package/dist/hooks/use-upload-queue.js.map +1 -0
- package/dist/lib/accent.d.ts +5 -0
- package/dist/lib/accent.d.ts.map +1 -0
- package/dist/lib/accent.js +10 -0
- package/dist/lib/accent.js.map +1 -0
- package/dist/lib/bento.d.ts +17 -0
- package/dist/lib/bento.d.ts.map +1 -0
- package/dist/lib/bento.js +92 -0
- package/dist/lib/bento.js.map +1 -0
- package/dist/lib/dates.d.ts +14 -0
- package/dist/lib/dates.d.ts.map +1 -0
- package/dist/lib/dates.js +34 -0
- package/dist/lib/dates.js.map +1 -0
- package/dist/lib/document.d.ts +79 -0
- package/dist/lib/document.d.ts.map +1 -0
- package/dist/lib/document.js +11 -0
- package/dist/lib/document.js.map +1 -0
- package/dist/lib/embed.d.ts +14 -0
- package/dist/lib/embed.d.ts.map +1 -0
- package/dist/lib/embed.js +63 -0
- package/dist/lib/embed.js.map +1 -0
- package/dist/lib/files.d.ts +23 -0
- package/dist/lib/files.d.ts.map +1 -0
- package/dist/lib/files.js +69 -0
- package/dist/lib/files.js.map +1 -0
- package/dist/lib/justified.d.ts +50 -0
- package/dist/lib/justified.d.ts.map +1 -0
- package/dist/lib/justified.js +78 -0
- package/dist/lib/justified.js.map +1 -0
- package/dist/lib/rich-text-dom.d.ts +14 -0
- package/dist/lib/rich-text-dom.d.ts.map +1 -0
- package/dist/lib/rich-text-dom.js +143 -0
- package/dist/lib/rich-text-dom.js.map +1 -0
- package/dist/lib/rich-text.d.ts +15 -0
- package/dist/lib/rich-text.d.ts.map +1 -0
- package/dist/lib/rich-text.js +35 -0
- package/dist/lib/rich-text.js.map +1 -0
- package/dist/lib/spotlight.d.ts +22 -0
- package/dist/lib/spotlight.d.ts.map +1 -0
- package/dist/lib/spotlight.js +66 -0
- package/dist/lib/spotlight.js.map +1 -0
- package/dist/lib/tree.d.ts +17 -0
- package/dist/lib/tree.d.ts.map +1 -0
- package/dist/lib/tree.js +35 -0
- package/dist/lib/tree.js.map +1 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +6 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/styles/notion.css +526 -0
- package/package.json +106 -5
- package/src/components/editor/block-text.tsx +230 -0
- package/src/components/editor/blocks.tsx +368 -0
- package/src/components/editor/document-editor.tsx +101 -0
- package/src/components/editor/document-view.tsx +192 -0
- package/src/components/editor/editor-state.tsx +270 -0
- package/src/components/editor/extensions/spoiler.tsx +76 -0
- package/src/components/editor/index.ts +9 -0
- package/src/components/editor/kinds.ts +158 -0
- package/src/components/sketch/rough.tsx +90 -0
- package/src/components/ui/accent-picker.tsx +72 -0
- package/src/components/ui/address-field.tsx +57 -0
- package/src/components/ui/app-shell.tsx +138 -0
- package/src/components/ui/badge.tsx +81 -0
- package/src/components/ui/breadcrumbs.tsx +25 -0
- package/src/components/ui/button.tsx +73 -0
- package/src/components/ui/calendar.tsx +100 -0
- package/src/components/ui/callout.tsx +62 -0
- package/src/components/ui/card.tsx +90 -0
- package/src/components/ui/checkbox.tsx +32 -0
- package/src/components/ui/command-menu.tsx +72 -0
- package/src/components/ui/date-picker.tsx +170 -0
- package/src/components/ui/date-time-picker.tsx +117 -0
- package/src/components/ui/dialog.tsx +124 -0
- package/src/components/ui/dropdown-menu.tsx +183 -0
- package/src/components/ui/editable-text.tsx +109 -0
- package/src/components/ui/field-button.tsx +68 -0
- package/src/components/ui/file-preview.tsx +80 -0
- package/src/components/ui/file-uploader.tsx +376 -0
- package/src/components/ui/handle-row.tsx +116 -0
- package/src/components/ui/image-block.tsx +238 -0
- package/src/components/ui/image-card.tsx +102 -0
- package/src/components/ui/image-viewer.tsx +187 -0
- package/src/components/ui/image.tsx +96 -0
- package/src/components/ui/index.ts +83 -0
- package/src/components/ui/input.tsx +99 -0
- package/src/components/ui/link-block.tsx +82 -0
- package/src/components/ui/mac-card.tsx +46 -0
- package/src/components/ui/nav-tree.tsx +86 -0
- package/src/components/ui/page.tsx +25 -0
- package/src/components/ui/popover.tsx +42 -0
- package/src/components/ui/progress.tsx +36 -0
- package/src/components/ui/property.tsx +64 -0
- package/src/components/ui/segmented.tsx +42 -0
- package/src/components/ui/select-menu.tsx +46 -0
- package/src/components/ui/select.tsx +124 -0
- package/src/components/ui/selection-bar.tsx +50 -0
- package/src/components/ui/separator.tsx +30 -0
- package/src/components/ui/spotlight.tsx +222 -0
- package/src/components/ui/stat-tile.tsx +25 -0
- package/src/components/ui/step-dialog.tsx +104 -0
- package/src/components/ui/table.tsx +42 -0
- package/src/components/ui/theme-toggle.tsx +101 -0
- package/src/components/ui/time-picker.tsx +173 -0
- package/src/components/ui/tooltip.tsx +64 -0
- package/src/components/ui/typography.tsx +168 -0
- package/src/components/ui/video-block.tsx +60 -0
- package/src/hooks/use-save-queue.ts +66 -0
- package/src/hooks/use-selection.ts +44 -0
- package/src/hooks/use-shared-transition.ts +40 -0
- package/src/hooks/use-upload-queue.ts +111 -0
- package/src/lib/accent.ts +11 -0
- package/src/lib/bento.ts +87 -0
- package/src/lib/dates.ts +39 -0
- package/src/lib/document.ts +55 -0
- package/src/lib/embed.ts +62 -0
- package/src/lib/files.ts +73 -0
- package/src/lib/justified.ts +106 -0
- package/src/lib/rich-text-dom.ts +136 -0
- package/src/lib/rich-text.ts +50 -0
- package/src/lib/spotlight.ts +78 -0
- package/src/lib/tree.ts +41 -0
- package/src/lib/utils.ts +6 -0
- package/src/styles/notion.css +526 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, type KeyboardEvent } from "react";
|
|
4
|
+
import type { Text } from "../../lib/rich-text.ts";
|
|
5
|
+
import { CommandMenu } from "../ui/command-menu.tsx";
|
|
6
|
+
import { caretOffset, caretOnEdgeLine, caretRect } from "../../lib/rich-text-dom.ts";
|
|
7
|
+
import { EditableText } from "../ui/editable-text.tsx";
|
|
8
|
+
import { useEditor } from "./editor-state.tsx";
|
|
9
|
+
import { hasText, isListType, type BlockKind, type Draft, type Node, type TextNode } from "./kinds.ts";
|
|
10
|
+
import { cut, normalize, plain, split } from "../../lib/rich-text.ts";
|
|
11
|
+
import { flat, locate } from "../../lib/tree.ts";
|
|
12
|
+
|
|
13
|
+
type Slash = { from: number; query: string; index: number; rect: DOMRect };
|
|
14
|
+
|
|
15
|
+
/* The text of one block, with the keyboard behaviour that makes a column of
|
|
16
|
+
separate elements feel like one document: Enter splits, Backspace at the start
|
|
17
|
+
joins, Tab nests, arrows cross block edges, "/" opens the list of blocks.
|
|
18
|
+
Pictures, videos, cards and dividers above or below are selected by Backspace,
|
|
19
|
+
Delete and the arrows, as in Notion (see editor-state). */
|
|
20
|
+
export function BlockText({
|
|
21
|
+
block,
|
|
22
|
+
placeholder,
|
|
23
|
+
alwaysShowPlaceholder,
|
|
24
|
+
className,
|
|
25
|
+
}: {
|
|
26
|
+
block: TextNode;
|
|
27
|
+
placeholder?: string;
|
|
28
|
+
alwaysShowPlaceholder?: boolean;
|
|
29
|
+
className?: string;
|
|
30
|
+
}) {
|
|
31
|
+
const ed = useEditor();
|
|
32
|
+
const { schema } = ed;
|
|
33
|
+
const kind = (key: string) => schema.kind(key)!;
|
|
34
|
+
const [slash, setSlash] = useState<Slash | null>(null);
|
|
35
|
+
const matches = slash ? schema.kinds.filter((k) => k.label.toLowerCase().includes(slash.query.toLowerCase())) : [];
|
|
36
|
+
|
|
37
|
+
const place = locate(ed.blocks, block.id)!;
|
|
38
|
+
const parentId = place.parent?.id ?? null;
|
|
39
|
+
const setText = (text: Text) => ed.edit(block.id, (b) => ({ ...b, text }) as Node, { rewrite: true });
|
|
40
|
+
|
|
41
|
+
// Makes this block a `to` block holding `text`, or, where that is impossible,
|
|
42
|
+
// keeps it and adds the new block below.
|
|
43
|
+
const turnInto = (to: BlockKind, text: Text) => {
|
|
44
|
+
const made = to.make(text);
|
|
45
|
+
const empty = plain(text) === "" && block.children.length === 0;
|
|
46
|
+
if (!("text" in made)) {
|
|
47
|
+
setText(text);
|
|
48
|
+
// An extension that can take the caret (a table's first cell) gets it; the kit's own
|
|
49
|
+
// pictures, videos and cards keep it in their address field instead.
|
|
50
|
+
const id = ed.create(parentId, block.id, made, { focus: schema.extension(made.type)?.focus !== undefined });
|
|
51
|
+
if (empty) ed.remove(block.id);
|
|
52
|
+
// A divider leaves the writing to go on below it.
|
|
53
|
+
if (made.type === "divider") ed.create(parentId, id, { type: "paragraph", text: [] }, { focus: true });
|
|
54
|
+
} else if (block.children.some((child) => !schema.holds(made.type, child.type))) {
|
|
55
|
+
setText(text);
|
|
56
|
+
ed.create(parentId, block.id, to.make([]), { focus: true });
|
|
57
|
+
} else {
|
|
58
|
+
ed.edit(block.id, (b) => ({ ...made, id: b.id, children: b.children }) as Node, { rewrite: true });
|
|
59
|
+
ed.focus(block.id, "end");
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const onChange = (text: Text, el: HTMLElement) => {
|
|
64
|
+
const typed = plain(text);
|
|
65
|
+
const shortcut = schema.shortcuts[typed];
|
|
66
|
+
if (shortcut && schema.kind(shortcut) && schema.kindOf(block)?.key !== shortcut) return turnInto(kind(shortcut), []);
|
|
67
|
+
|
|
68
|
+
ed.edit(block.id, (b) => ({ ...b, text }) as Node, { typing: true });
|
|
69
|
+
|
|
70
|
+
const caret = caretOffset(el);
|
|
71
|
+
if (caret === null) return setSlash(null);
|
|
72
|
+
if (slash) {
|
|
73
|
+
const query = typed.slice(slash.from + 1, caret);
|
|
74
|
+
setSlash(caret <= slash.from || typed[slash.from] !== "/" || /\s/.test(query) ? null : { ...slash, query, index: 0 });
|
|
75
|
+
} else if (typed[caret - 1] === "/" && (caret === 1 || /\s/.test(typed[caret - 2] ?? ""))) {
|
|
76
|
+
const rect = caretRect();
|
|
77
|
+
if (rect) setSlash({ from: caret - 1, query: "", index: 0, rect });
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const pick = (to: BlockKind) => {
|
|
82
|
+
if (!slash) return;
|
|
83
|
+
const rest = cut(block.text, slash.from, slash.from + 1 + slash.query.length);
|
|
84
|
+
setSlash(null);
|
|
85
|
+
if (plain(rest) === "") turnInto(to, []);
|
|
86
|
+
else {
|
|
87
|
+
setText(rest);
|
|
88
|
+
ed.create(parentId, block.id, to.make([]), { focus: true });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function outdent(caret: number | "end") {
|
|
93
|
+
if (!place.parent) return;
|
|
94
|
+
const grand = locate(ed.blocks, place.parent.id)!.parent;
|
|
95
|
+
ed.move(block.id, grand?.id ?? null, place.parent.id);
|
|
96
|
+
ed.focus(block.id, caret);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
100
|
+
if (slash && matches.length > 0) {
|
|
101
|
+
const step = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0;
|
|
102
|
+
if (step) {
|
|
103
|
+
e.preventDefault();
|
|
104
|
+
setSlash({ ...slash, index: (slash.index + step + matches.length) % matches.length });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (e.key === "Enter") {
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
pick(matches[slash.index]!);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (e.key === "Escape") return setSlash(null);
|
|
114
|
+
|
|
115
|
+
const caret = caretOffset(e.currentTarget);
|
|
116
|
+
const length = plain(block.text).length;
|
|
117
|
+
|
|
118
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
if (caret === null) return;
|
|
121
|
+
// Enter on an empty list item, heading or quote backs out of it, one level at a time.
|
|
122
|
+
if (length === 0 && block.type !== "paragraph" && block.children.length === 0) {
|
|
123
|
+
if (place.parent) outdent(0);
|
|
124
|
+
else turnInto(kind("text"), []);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// An empty last line inside a toggle steps back out of it, as Notion's do.
|
|
128
|
+
if (length === 0 && block.type === "paragraph" && place.parent !== null && schema.toggle(place.parent.type) && place.index === place.siblings.length - 1) {
|
|
129
|
+
outdent(0);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const [before, after] = split(block.text, caret);
|
|
133
|
+
const next: Draft =
|
|
134
|
+
block.type === "check"
|
|
135
|
+
? { type: "check", checked: false, text: after }
|
|
136
|
+
: isListType(block.type)
|
|
137
|
+
? { type: block.type as "bullet" | "numbered", text: after }
|
|
138
|
+
: { type: "paragraph", text: after };
|
|
139
|
+
setText(before);
|
|
140
|
+
// A toggle's own line names it; what Enter starts goes inside it.
|
|
141
|
+
if (schema.toggle(block.type)) ed.create(block.id, null, next, { focus: true });
|
|
142
|
+
// With children, the new item goes first among them, where the caret appears to be.
|
|
143
|
+
else if (block.children.length > 0) ed.create(block.id, null, next, { focus: true });
|
|
144
|
+
else ed.create(parentId, block.id, next, { focus: true });
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (e.key === "Backspace" && caret === 0 && window.getSelection()?.isCollapsed) {
|
|
149
|
+
if (block.type !== "paragraph") {
|
|
150
|
+
e.preventDefault();
|
|
151
|
+
if (block.children.length === 0) turnInto(kind("text"), block.text);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const order = flat(ed.blocks);
|
|
155
|
+
const prev = order[order.findIndex((b) => b.id === block.id) - 1];
|
|
156
|
+
if (!prev) return;
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
if (!hasText(prev)) {
|
|
159
|
+
// As in Notion: an empty line goes, and the picture or card above is
|
|
160
|
+
// selected; a second Backspace removes that. A line with words keeps them.
|
|
161
|
+
if (length === 0) ed.remove(block.id);
|
|
162
|
+
ed.select(prev.id);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const joinAt = plain(prev.text).length;
|
|
166
|
+
ed.edit(prev.id, (b) => ({ ...b, text: normalize([...prev.text, ...block.text]) }) as Node, { rewrite: true });
|
|
167
|
+
ed.remove(block.id);
|
|
168
|
+
ed.focus(prev.id, joinAt);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (e.key === "Tab") {
|
|
173
|
+
e.preventDefault();
|
|
174
|
+
if (e.shiftKey) outdent(caret ?? "end");
|
|
175
|
+
else {
|
|
176
|
+
const above = place.siblings[place.index - 1];
|
|
177
|
+
if (!above || !schema.holds(above.type, block.type)) return;
|
|
178
|
+
ed.move(block.id, above.id, above.children.at(-1)?.id ?? null);
|
|
179
|
+
ed.focus(block.id, caret ?? "end");
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Delete at the end of a line takes in what follows: the next line's words,
|
|
185
|
+
// or, when a picture or card is next, selects it to be deleted.
|
|
186
|
+
if (e.key === "Delete" && caret === length && window.getSelection()?.isCollapsed) {
|
|
187
|
+
const order = flat(ed.blocks);
|
|
188
|
+
const next = order[order.findIndex((b) => b.id === block.id) + 1];
|
|
189
|
+
if (next === undefined || block.children.length > 0) return;
|
|
190
|
+
e.preventDefault();
|
|
191
|
+
if (!hasText(next)) return ed.select(next.id);
|
|
192
|
+
if (next.children.length > 0) return;
|
|
193
|
+
ed.edit(block.id, (b) => ({ ...b, text: normalize([...block.text, ...next.text]) }) as Node, { rewrite: true });
|
|
194
|
+
ed.remove(next.id);
|
|
195
|
+
ed.focus(block.id, length);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// The arrows move through every block, landing on pictures and cards too. They
|
|
200
|
+
// leave from anywhere on a block's first or last line, not only its very ends.
|
|
201
|
+
if (
|
|
202
|
+
(e.key === "ArrowUp" && (caret === 0 || caretOnEdgeLine(e.currentTarget, "first"))) ||
|
|
203
|
+
(e.key === "ArrowDown" && (caret === length || caretOnEdgeLine(e.currentTarget, "last")))
|
|
204
|
+
) {
|
|
205
|
+
const order = flat(ed.blocks);
|
|
206
|
+
const to = order[order.findIndex((b) => b.id === block.id) + (e.key === "ArrowUp" ? -1 : 1)];
|
|
207
|
+
if (!to) return;
|
|
208
|
+
e.preventDefault();
|
|
209
|
+
if (hasText(to)) ed.focus(to.id, e.key === "ArrowUp" ? "end" : 0);
|
|
210
|
+
else ed.select(to.id);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
return (
|
|
215
|
+
<>
|
|
216
|
+
<EditableText
|
|
217
|
+
id={String(block.id)}
|
|
218
|
+
text={block.text}
|
|
219
|
+
rev={ed.rev(block.id)}
|
|
220
|
+
onChange={onChange}
|
|
221
|
+
onKeyDown={onKeyDown}
|
|
222
|
+
onLink={ed.onLink}
|
|
223
|
+
placeholder={placeholder}
|
|
224
|
+
alwaysShowPlaceholder={alwaysShowPlaceholder}
|
|
225
|
+
className={className}
|
|
226
|
+
/>
|
|
227
|
+
{slash && matches.length > 0 && <CommandMenu title="Blocks" rect={slash.rect} commands={matches} index={slash.index} onPick={(c) => pick(c as BlockKind)} />}
|
|
228
|
+
</>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, type ReactNode } from "react";
|
|
4
|
+
import { Maximize2, Trash2 } from "lucide-react";
|
|
5
|
+
import type { Text } from "../../lib/rich-text.ts";
|
|
6
|
+
import { cn } from "../../lib/utils.ts";
|
|
7
|
+
import { Checkbox } from "../ui/checkbox.tsx";
|
|
8
|
+
import { DropdownMenuCheckboxItem, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator } from "../ui/dropdown-menu.tsx";
|
|
9
|
+
import { EditableText } from "../ui/editable-text.tsx";
|
|
10
|
+
import { HandleRow } from "../ui/handle-row.tsx";
|
|
11
|
+
import { ImageBlock } from "../ui/image-block.tsx";
|
|
12
|
+
import { LinkBlock } from "../ui/link-block.tsx";
|
|
13
|
+
import { VideoBlock } from "../ui/video-block.tsx";
|
|
14
|
+
import { locate } from "../../lib/tree.ts";
|
|
15
|
+
import { BlockText } from "./block-text.tsx";
|
|
16
|
+
import { useEditor } from "./editor-state.tsx";
|
|
17
|
+
import { hasText, isListType, type BuiltinNode, type Node } from "./kinds.ts";
|
|
18
|
+
|
|
19
|
+
type Of<T extends BuiltinNode["type"]> = Extract<BuiltinNode, { type: T }>;
|
|
20
|
+
|
|
21
|
+
/** What a press on a selectable block leaves to the control it landed on. An extension marks
|
|
22
|
+
any other area that handles its own presses (rows that open a dialog) with `data-own-press`. */
|
|
23
|
+
const OWN_CLICK = "input, textarea, a, iframe, button:not(:disabled), [contenteditable=true], [role=slider], [data-own-press]";
|
|
24
|
+
|
|
25
|
+
/* One block's own line, with Notion's margin controls from HandleRow: "+" adds a
|
|
26
|
+
block below, and the grip drags it and, on a click, opens its menu. Blocks
|
|
27
|
+
without words — pictures, videos, cards, dividers — are picked as a whole by a
|
|
28
|
+
click and show it with an outline; see editor-state for what keys then do.
|
|
29
|
+
A list item's children sit outside the row, so hovering a child never lights
|
|
30
|
+
up its parent's controls. `menu` adds an extension's own entries
|
|
31
|
+
(DropdownMenuItem elements) to the grip's menu, above Delete. */
|
|
32
|
+
export function BlockRow({ block, className, children, open, menu }: { block: Node; className?: string; children: ReactNode; open?: () => void; menu?: ReactNode }) {
|
|
33
|
+
const ed = useEditor();
|
|
34
|
+
const place = locate(ed.blocks, block.id)!;
|
|
35
|
+
const parentId = place.parent?.id ?? null;
|
|
36
|
+
const { drag } = ed;
|
|
37
|
+
const current = ed.schema.kindOf(block);
|
|
38
|
+
const selectable = !hasText(block);
|
|
39
|
+
const selected = ed.selected === block.id;
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<HandleRow
|
|
43
|
+
className={className}
|
|
44
|
+
onAdd={() => ed.create(parentId, block.id, { type: "paragraph", text: [] }, { focus: true })}
|
|
45
|
+
dragging={drag.id !== null}
|
|
46
|
+
onDragStart={() => drag.start(block.id)}
|
|
47
|
+
onDragEnd={drag.end}
|
|
48
|
+
onDragOverSide={(side) => (drag.drop?.id !== block.id || drag.drop.side !== side) && drag.over({ id: block.id, side })}
|
|
49
|
+
onDrop={() => {
|
|
50
|
+
if (drag.id !== null && drag.drop) {
|
|
51
|
+
const target = locate(ed.blocks, drag.drop.id)!;
|
|
52
|
+
const afterId = drag.drop.side === "after" ? target.node.id : (target.siblings[target.index - 1]?.id ?? null);
|
|
53
|
+
ed.move(drag.id, target.parent?.id ?? null, afterId);
|
|
54
|
+
}
|
|
55
|
+
drag.end();
|
|
56
|
+
}}
|
|
57
|
+
drop={drag.drop?.id === block.id && drag.id !== block.id ? drag.drop.side : null}
|
|
58
|
+
menu={
|
|
59
|
+
<>
|
|
60
|
+
{hasText(block) && (
|
|
61
|
+
<>
|
|
62
|
+
<DropdownMenuLabel>Turn into</DropdownMenuLabel>
|
|
63
|
+
{ed.schema.textKinds.map((k) => (
|
|
64
|
+
<DropdownMenuCheckboxItem
|
|
65
|
+
key={k.key}
|
|
66
|
+
checked={k.key === current?.key}
|
|
67
|
+
// A block with children can only become something that can hold them.
|
|
68
|
+
disabled={block.children.some((child) => !ed.schema.holds(k.make([]).type, child.type))}
|
|
69
|
+
onSelect={() => ed.edit(block.id, (b) => ({ ...k.make(hasText(b) ? b.text : []), id: b.id, children: b.children }) as Node, { rewrite: true })}
|
|
70
|
+
>
|
|
71
|
+
{k.label}
|
|
72
|
+
</DropdownMenuCheckboxItem>
|
|
73
|
+
))}
|
|
74
|
+
<DropdownMenuSeparator />
|
|
75
|
+
</>
|
|
76
|
+
)}
|
|
77
|
+
{open !== undefined && (
|
|
78
|
+
<DropdownMenuItem onSelect={open}>
|
|
79
|
+
<Maximize2 />
|
|
80
|
+
Open picture
|
|
81
|
+
</DropdownMenuItem>
|
|
82
|
+
)}
|
|
83
|
+
{menu !== undefined && (
|
|
84
|
+
<>
|
|
85
|
+
{menu}
|
|
86
|
+
<DropdownMenuSeparator />
|
|
87
|
+
</>
|
|
88
|
+
)}
|
|
89
|
+
<DropdownMenuItem danger onSelect={() => ed.remove(block.id)}>
|
|
90
|
+
<Trash2 />
|
|
91
|
+
Delete
|
|
92
|
+
</DropdownMenuItem>
|
|
93
|
+
</>
|
|
94
|
+
}
|
|
95
|
+
>
|
|
96
|
+
{selectable ? (
|
|
97
|
+
<div
|
|
98
|
+
data-block={block.id}
|
|
99
|
+
aria-selected={selected}
|
|
100
|
+
onPointerDown={(e) => {
|
|
101
|
+
if (!(e.target as HTMLElement).closest(OWN_CLICK)) ed.select(block.id);
|
|
102
|
+
}}
|
|
103
|
+
className={cn("rounded-surface transition-shadow duration-100", selected && "shadow-[0_0_0_2px_var(--accent),0_0_0_5px_var(--accent-tint)]")}
|
|
104
|
+
>
|
|
105
|
+
{children}
|
|
106
|
+
</div>
|
|
107
|
+
) : (
|
|
108
|
+
children
|
|
109
|
+
)}
|
|
110
|
+
</HandleRow>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type ListNode = Extract<BuiltinNode, { type: "bullet" | "numbered" | "check" }>;
|
|
115
|
+
const isList = (b: Node): b is ListNode => isListType(b.type);
|
|
116
|
+
|
|
117
|
+
/** Consecutive list items of one kind form one list, so numbering restarts where a run is interrupted. */
|
|
118
|
+
function runs(blocks: Node[]): (Node | ListNode[])[] {
|
|
119
|
+
const out: (Node | ListNode[])[] = [];
|
|
120
|
+
for (const block of blocks) {
|
|
121
|
+
const last = out.at(-1);
|
|
122
|
+
if (isList(block) && Array.isArray(last) && last[0]!.type === block.type) last.push(block);
|
|
123
|
+
else out.push(isList(block) ? [block] : block);
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function BlockList({ blocks, depth = 0 }: { blocks: Node[]; depth?: number }) {
|
|
129
|
+
const ed = useEditor();
|
|
130
|
+
// A page that is one empty paragraph still says what it is for, focused or not.
|
|
131
|
+
const alone = depth === 0 && blocks.length === 1;
|
|
132
|
+
return runs(blocks).map((entry) => {
|
|
133
|
+
// An app's own block draws itself.
|
|
134
|
+
const extension = Array.isArray(entry) ? undefined : ed.schema.extension(entry.type);
|
|
135
|
+
if (extension !== undefined && !Array.isArray(entry)) return <extension.Edit key={entry.id} block={entry} depth={depth} />;
|
|
136
|
+
return builtin(entry as BuiltinNode | ListNode[], depth, alone);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The kit's own kinds, drawn. */
|
|
141
|
+
function builtin(entry: BuiltinNode | ListNode[], depth: number, alone: boolean) {
|
|
142
|
+
if (Array.isArray(entry)) {
|
|
143
|
+
const List = entry[0]!.type === "numbered" ? "ol" : "ul";
|
|
144
|
+
return (
|
|
145
|
+
<List key={entry[0]!.id}>
|
|
146
|
+
{entry.map((block, index) => (
|
|
147
|
+
<ListItem key={block.id} block={block} depth={depth} index={index} />
|
|
148
|
+
))}
|
|
149
|
+
</List>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
switch (entry.type) {
|
|
153
|
+
case "paragraph":
|
|
154
|
+
return (
|
|
155
|
+
<BlockRow key={entry.id} block={entry} className="my-px">
|
|
156
|
+
<BlockText
|
|
157
|
+
block={entry}
|
|
158
|
+
placeholder="Write, or type '/' for pictures, video and more"
|
|
159
|
+
alwaysShowPlaceholder={alone}
|
|
160
|
+
className="min-h-[1.5em] px-0.5 py-[3px]"
|
|
161
|
+
/>
|
|
162
|
+
</BlockRow>
|
|
163
|
+
);
|
|
164
|
+
case "heading":
|
|
165
|
+
return <Heading key={entry.id} block={entry} />;
|
|
166
|
+
case "quote":
|
|
167
|
+
return (
|
|
168
|
+
<BlockRow key={entry.id} block={entry} className="my-1">
|
|
169
|
+
<BlockText block={entry} placeholder="Quote" alwaysShowPlaceholder className="border-l-[3px] border-ink px-3.5 py-[3px] text-lg" />
|
|
170
|
+
</BlockRow>
|
|
171
|
+
);
|
|
172
|
+
case "divider":
|
|
173
|
+
return (
|
|
174
|
+
<BlockRow key={entry.id} block={entry}>
|
|
175
|
+
{/* The space around the line is part of what a click selects. */}
|
|
176
|
+
<div className="cursor-pointer py-2.5">
|
|
177
|
+
<hr className="border-hairline-strong" />
|
|
178
|
+
</div>
|
|
179
|
+
</BlockRow>
|
|
180
|
+
);
|
|
181
|
+
case "image":
|
|
182
|
+
return <PictureBlock key={entry.id} block={entry} />;
|
|
183
|
+
case "video":
|
|
184
|
+
return <VideoEmbed key={entry.id} block={entry} />;
|
|
185
|
+
case "bookmark":
|
|
186
|
+
return <Bookmark key={entry.id} block={entry} />;
|
|
187
|
+
default:
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/* Rank comes from size and the space above, nothing else. A heading sits much
|
|
193
|
+
closer to what follows it than to what came before. */
|
|
194
|
+
const HEADINGS = { 1: "text-h1 mt-8 mb-1", 2: "text-h2 mt-6 mb-1", 3: "text-h3 mt-4 mb-0.5" } as const;
|
|
195
|
+
|
|
196
|
+
function Heading({ block }: { block: Of<"heading"> }) {
|
|
197
|
+
return (
|
|
198
|
+
<BlockRow block={block} className={cn("first:mt-0", HEADINGS[block.level])}>
|
|
199
|
+
{/* Colour sits apart from the size class: tailwind-merge takes both for text colours and keeps one. */}
|
|
200
|
+
<div role="heading" aria-level={block.level + 1} className="text-accent-text">
|
|
201
|
+
<BlockText block={block} placeholder={`Heading ${block.level}`} alwaysShowPlaceholder className="px-0.5 py-[3px]" />
|
|
202
|
+
</div>
|
|
203
|
+
</BlockRow>
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Notion cycles the bullet by depth: disc, ring, square. Drawn as shapes, since the glyphs fall back to emoji on some platforms. */
|
|
208
|
+
const BULLETS = ["rounded-full bg-current", "rounded-full border border-current", "bg-current"];
|
|
209
|
+
|
|
210
|
+
function ListItem({ block, depth, index }: { block: ListNode; depth: number; index: number }) {
|
|
211
|
+
const ed = useEditor();
|
|
212
|
+
const marker =
|
|
213
|
+
block.type === "check" ? (
|
|
214
|
+
<Checkbox checked={block.checked} onCheckedChange={(checked) => ed.edit(block.id, (b) => (b.type === "check" ? { ...b, checked: checked === true } : b))} />
|
|
215
|
+
) : block.type === "numbered" ? (
|
|
216
|
+
<span className="tabular-nums">{index + 1}.</span>
|
|
217
|
+
) : (
|
|
218
|
+
<span className={cn("size-[5px]", BULLETS[depth % BULLETS.length])} />
|
|
219
|
+
);
|
|
220
|
+
const done = block.type === "check" && block.checked;
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
<li className="my-px">
|
|
224
|
+
<BlockRow block={block}>
|
|
225
|
+
<div className="flex items-start px-0.5 py-[3px]">
|
|
226
|
+
<span contentEditable={false} className="flex h-[1.5em] w-6 shrink-0 items-center justify-center text-accent-text select-none">
|
|
227
|
+
{marker}
|
|
228
|
+
</span>
|
|
229
|
+
<BlockText
|
|
230
|
+
block={block}
|
|
231
|
+
placeholder={block.type === "check" ? "To-do" : "List"}
|
|
232
|
+
alwaysShowPlaceholder
|
|
233
|
+
className={cn("flex-1", done && "text-ink-secondary line-through decoration-ink-tertiary")}
|
|
234
|
+
/>
|
|
235
|
+
</div>
|
|
236
|
+
</BlockRow>
|
|
237
|
+
{block.children.length > 0 && (
|
|
238
|
+
<div className="pl-6">
|
|
239
|
+
<BlockList blocks={block.children} depth={depth + 1} />
|
|
240
|
+
</div>
|
|
241
|
+
)}
|
|
242
|
+
</li>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** A caption under a picture or a video: rich text of its own, saved with the block. Enter goes on below. */
|
|
247
|
+
export function Caption({ block, text }: { block: Node; text: Text }) {
|
|
248
|
+
const ed = useEditor();
|
|
249
|
+
return (
|
|
250
|
+
<EditableText
|
|
251
|
+
text={text}
|
|
252
|
+
rev={ed.rev(block.id)}
|
|
253
|
+
placeholder="Add a caption"
|
|
254
|
+
onLink={ed.onLink}
|
|
255
|
+
className="px-0.5 text-center"
|
|
256
|
+
onChange={(caption) => ed.edit(block.id, (b) => ({ ...b, caption }) as Node, { typing: true })}
|
|
257
|
+
onKeyDown={(e) => {
|
|
258
|
+
if (e.key === "Enter") {
|
|
259
|
+
e.preventDefault();
|
|
260
|
+
const place = locate(ed.blocks, block.id)!;
|
|
261
|
+
ed.create(place.parent?.id ?? null, block.id, { type: "paragraph", text: [] }, { focus: true });
|
|
262
|
+
}
|
|
263
|
+
}}
|
|
264
|
+
/>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* ImageBlock in a document. A file is handed to the owner's `uploadImage`, which
|
|
270
|
+
* says where it can be loaded from; without one, only links are taken. Once it
|
|
271
|
+
* has a picture the block is selected, so Enter goes on writing below it. A click
|
|
272
|
+
* selects it; opening it larger is in its menu.
|
|
273
|
+
*/
|
|
274
|
+
export function PictureBlock({ block }: { block: Of<"image"> }) {
|
|
275
|
+
const ed = useEditor();
|
|
276
|
+
const [busy, setBusy] = useState<number | false>(false);
|
|
277
|
+
const [error, setError] = useState<string>();
|
|
278
|
+
const set = (patch: Partial<Of<"image">>) => ed.edit(block.id, (b) => ({ ...b, ...patch }) as Node);
|
|
279
|
+
const upload = ed.uploadImage;
|
|
280
|
+
|
|
281
|
+
const onFile = async (file: File) => {
|
|
282
|
+
if (!upload) return;
|
|
283
|
+
setError(undefined);
|
|
284
|
+
setBusy(0);
|
|
285
|
+
try {
|
|
286
|
+
const image = await upload(file, setBusy);
|
|
287
|
+
set({ src: image.src, ref: image.ref, aspect: image.aspect });
|
|
288
|
+
ed.select(block.id);
|
|
289
|
+
} catch (err) {
|
|
290
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
291
|
+
} finally {
|
|
292
|
+
setBusy(false);
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// Which addresses are fine to show is the owner's rule; the kit takes what it is given.
|
|
297
|
+
const src = block.src;
|
|
298
|
+
// A picture an app's extra supplies is selected like one uploaded, so Enter writes below it.
|
|
299
|
+
const extras =
|
|
300
|
+
ed.imageExtras?.(block, (patch) => {
|
|
301
|
+
set(patch);
|
|
302
|
+
if (patch.src !== undefined) ed.select(block.id);
|
|
303
|
+
}) ?? {};
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<BlockRow block={block} className="my-2" open={src === undefined ? undefined : () => window.open(src, "_blank", "noreferrer")}>
|
|
307
|
+
<ImageBlock
|
|
308
|
+
src={src}
|
|
309
|
+
aspect={block.aspect}
|
|
310
|
+
width={block.width}
|
|
311
|
+
onWidthChange={(width) => set({ width })}
|
|
312
|
+
onFile={upload ? (file) => void onFile(file) : undefined}
|
|
313
|
+
onLink={(url) => {
|
|
314
|
+
const refused = ed.checkLink?.(url, "image") ?? null;
|
|
315
|
+
if (refused !== null) return setError(refused);
|
|
316
|
+
setError(undefined);
|
|
317
|
+
set({ src: url, ref: undefined, aspect: undefined });
|
|
318
|
+
ed.select(block.id);
|
|
319
|
+
}}
|
|
320
|
+
busy={busy === false ? false : busy}
|
|
321
|
+
error={error}
|
|
322
|
+
more={extras.more}
|
|
323
|
+
actions={extras.actions}
|
|
324
|
+
placeholder={extras.placeholder}
|
|
325
|
+
caption={src === undefined ? undefined : <Caption block={block} text={block.caption} />}
|
|
326
|
+
/>
|
|
327
|
+
</BlockRow>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function VideoEmbed({ block }: { block: Of<"video"> }) {
|
|
332
|
+
const ed = useEditor();
|
|
333
|
+
return (
|
|
334
|
+
<BlockRow block={block} className="my-2">
|
|
335
|
+
<VideoBlock
|
|
336
|
+
url={block.url}
|
|
337
|
+
check={ed.checkLink === undefined ? undefined : (url) => ed.checkLink!(url, "video")}
|
|
338
|
+
onLink={(url) => {
|
|
339
|
+
ed.edit(block.id, (b) => ({ ...b, url }) as Node);
|
|
340
|
+
ed.select(block.id);
|
|
341
|
+
}}
|
|
342
|
+
caption={<Caption block={block} text={block.caption} />}
|
|
343
|
+
/>
|
|
344
|
+
</BlockRow>
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function Bookmark({ block }: { block: Of<"bookmark"> }) {
|
|
349
|
+
const ed = useEditor();
|
|
350
|
+
return (
|
|
351
|
+
<BlockRow block={block} className="my-2">
|
|
352
|
+
<LinkBlock
|
|
353
|
+
url={block.url}
|
|
354
|
+
title={block.title}
|
|
355
|
+
check={ed.checkLink === undefined ? undefined : (url) => ed.checkLink!(url, "bookmark")}
|
|
356
|
+
onTitleChange={(title) => ed.edit(block.id, (b) => ({ ...b, title }) as Node, { typing: true })}
|
|
357
|
+
onTitleEnter={() => {
|
|
358
|
+
const place = locate(ed.blocks, block.id)!;
|
|
359
|
+
ed.create(place.parent?.id ?? null, block.id, { type: "paragraph", text: [] }, { focus: true });
|
|
360
|
+
}}
|
|
361
|
+
onLink={(url) => {
|
|
362
|
+
ed.edit(block.id, (b) => ({ ...b, url }) as Node);
|
|
363
|
+
ed.select(block.id);
|
|
364
|
+
}}
|
|
365
|
+
/>
|
|
366
|
+
</BlockRow>
|
|
367
|
+
);
|
|
368
|
+
}
|