@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,101 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { AnyBlock } from "../../lib/document.ts";
|
|
4
|
+
import { plain } from "../../lib/rich-text.ts";
|
|
5
|
+
import { BlockList } from "./blocks.tsx";
|
|
6
|
+
import { useImperativeHandle, useRef, type Ref } from "react";
|
|
7
|
+
import { DocumentProvider, useEditor, type CheckLink, type ImageExtras, type OnLink, type ProviderProps, type UploadImage } from "./editor-state.tsx";
|
|
8
|
+
import { hasText, toBlocks } from "./kinds.ts";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A Notion-style page of blocks, edited in place: text, headings, lists, to-dos,
|
|
12
|
+
* quotes, dividers, pictures, video and link cards. Enter splits, Backspace at the
|
|
13
|
+
* start joins, Tab nests, "/" opens the list of blocks, and blocks without words
|
|
14
|
+
* are selected and removed like Notion's. It holds the document while it is
|
|
15
|
+
* edited and reports every change; storing it is the owner's.
|
|
16
|
+
*
|
|
17
|
+
* Remount it (with a `key`) to replace the document from outside, for example
|
|
18
|
+
* with a newer version saved elsewhere.
|
|
19
|
+
*/
|
|
20
|
+
/** What an owner can do to an editor from outside it: `ref={handle}`. */
|
|
21
|
+
export type DocumentEditorHandle = {
|
|
22
|
+
/** Puts the caret in a block, the first when no id is given (from a title, on Enter). */
|
|
23
|
+
focus: (id?: number, at?: number | "end") => void;
|
|
24
|
+
/** The document as it stands, in its stored shape. */
|
|
25
|
+
blocks: () => AnyBlock[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function DocumentEditor({
|
|
29
|
+
initial,
|
|
30
|
+
onChange,
|
|
31
|
+
uploadImage,
|
|
32
|
+
checkLink,
|
|
33
|
+
onLink,
|
|
34
|
+
imageExtras,
|
|
35
|
+
extensions,
|
|
36
|
+
kinds,
|
|
37
|
+
newId,
|
|
38
|
+
ref,
|
|
39
|
+
placeholder = "Write, or type “/” for headings, pictures, video and links",
|
|
40
|
+
className,
|
|
41
|
+
}: ProviderProps & {
|
|
42
|
+
ref?: Ref<DocumentEditorHandle>;
|
|
43
|
+
placeholder?: string;
|
|
44
|
+
className?: string;
|
|
45
|
+
}) {
|
|
46
|
+
return (
|
|
47
|
+
<DocumentProvider
|
|
48
|
+
initial={initial}
|
|
49
|
+
onChange={onChange}
|
|
50
|
+
uploadImage={uploadImage}
|
|
51
|
+
checkLink={checkLink}
|
|
52
|
+
onLink={onLink}
|
|
53
|
+
imageExtras={imageExtras}
|
|
54
|
+
extensions={extensions}
|
|
55
|
+
kinds={kinds}
|
|
56
|
+
newId={newId}
|
|
57
|
+
>
|
|
58
|
+
<Body placeholder={placeholder} className={className} handle={ref} />
|
|
59
|
+
</DocumentProvider>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function Body({ placeholder, className, handle }: { placeholder: string; className?: string; handle?: Ref<DocumentEditorHandle> }) {
|
|
64
|
+
const ed = useEditor();
|
|
65
|
+
const latest = useRef(ed);
|
|
66
|
+
latest.current = ed;
|
|
67
|
+
useImperativeHandle(
|
|
68
|
+
handle,
|
|
69
|
+
() => ({
|
|
70
|
+
focus: (id, at = 0) => {
|
|
71
|
+
const target = id ?? latest.current.blocks[0]?.id;
|
|
72
|
+
if (target !== undefined) latest.current.focus(target, at);
|
|
73
|
+
else latest.current.create(null, null, { type: "paragraph", text: [] }, { focus: true });
|
|
74
|
+
},
|
|
75
|
+
blocks: () => toBlocks(latest.current.blocks, latest.current.schema),
|
|
76
|
+
}),
|
|
77
|
+
[],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// Clicking the empty space under the last block continues the page, as in Notion.
|
|
81
|
+
const continuePage = () => {
|
|
82
|
+
const last = ed.blocks.at(-1);
|
|
83
|
+
if (last?.type === "paragraph" && hasText(last) && plain(last.text) === "") ed.focus(last.id);
|
|
84
|
+
else ed.create(null, last?.id ?? null, { type: "paragraph", text: [] }, { focus: true });
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
// Room on the left for the margin controls, which hang outside each block.
|
|
89
|
+
// The gutter is the margin controls' (HandleRow, 46px) and is always 3rem. A page may pull the
|
|
90
|
+
// editor left to line its text up with a title, but by no more than its own padding, or the
|
|
91
|
+
// controls land on whatever is beside the page. Phones have neither gutter nor controls.
|
|
92
|
+
<div ref={ed.root} className={`relative flex min-h-24 flex-col pl-12 max-sm:pl-0 ${className ?? ""}`}>
|
|
93
|
+
<BlockList blocks={ed.blocks} />
|
|
94
|
+
<button type="button" aria-label="Continue writing" className="flex min-h-16 w-full flex-1 cursor-text items-start" onClick={continuePage}>
|
|
95
|
+
{ed.blocks.length === 0 && <span className="mt-px px-0.5 py-[3px] text-left text-ink-tertiary">{placeholder}</span>}
|
|
96
|
+
</button>
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type { CheckLink, ImageExtras, OnLink, UploadImage };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { createContext, useContext, type ReactNode } from "react";
|
|
4
|
+
import { ExternalLink, Globe } from "lucide-react";
|
|
5
|
+
import type { AnyBlock, Block } from "../../lib/document.ts";
|
|
6
|
+
import { hostOf, isSafeHref, videoEmbed } from "../../lib/embed.ts";
|
|
7
|
+
import type { Span, Text } from "../../lib/rich-text.ts";
|
|
8
|
+
import { cn } from "../../lib/utils.ts";
|
|
9
|
+
import { Image } from "../ui/image.tsx";
|
|
10
|
+
import type { AnyExtension } from "./kinds.ts";
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* A document read-only, for readers: what DocumentEditor writes, drawn as React
|
|
14
|
+
* elements, never as HTML. A link that is not a web or mail address is dropped
|
|
15
|
+
* to its words. An app's own blocks are drawn by their extension's `View`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const Extensions = createContext<Map<string, AnyExtension>>(new Map());
|
|
19
|
+
|
|
20
|
+
/** How much room a document takes: a page of its own, or `compact`, inside a list or card. */
|
|
21
|
+
export type Density = "page" | "compact";
|
|
22
|
+
const Dense = createContext<Density>("page");
|
|
23
|
+
|
|
24
|
+
const safe = isSafeHref;
|
|
25
|
+
|
|
26
|
+
export function Spans({ text }: { text: Text }) {
|
|
27
|
+
return text.map((span, i) => <SpanView key={i} span={span} />);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function SpanView({ span }: { span: Span }) {
|
|
31
|
+
let node: ReactNode = span.text;
|
|
32
|
+
if (span.code) node = <code className="rounded-[3px] bg-tint-hover px-1 py-0.5 font-mono text-[0.9em] text-danger">{node}</code>;
|
|
33
|
+
if (span.bold) node = <strong className="font-semibold">{node}</strong>;
|
|
34
|
+
if (span.italic) node = <em>{node}</em>;
|
|
35
|
+
if (span.strike) node = <s>{node}</s>;
|
|
36
|
+
if (span.page !== undefined) node = <span className="font-medium text-accent-text">{node}</span>;
|
|
37
|
+
else if (span.href !== undefined && safe(span.href)) {
|
|
38
|
+
node = (
|
|
39
|
+
<a href={span.href} target="_blank" rel="noreferrer nofollow ugc" className="underline decoration-ink-tertiary underline-offset-2 hover:decoration-ink">
|
|
40
|
+
{node}
|
|
41
|
+
</a>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return node;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const HEADINGS = {
|
|
48
|
+
page: { 1: "text-h1 mt-10 mb-1", 2: "text-h2 mt-8 mb-1", 3: "text-h3 mt-6 mb-0.5" },
|
|
49
|
+
// Inside a list, a heading ranks by weight more than size, so it never outgrows what holds it.
|
|
50
|
+
compact: { 1: "text-lg font-semibold mt-4 mb-0.5", 2: "text-base font-semibold mt-3 mb-0.5", 3: "text-sm font-semibold mt-2" },
|
|
51
|
+
} as const;
|
|
52
|
+
const SPACE = {
|
|
53
|
+
page: { p: "my-2 leading-relaxed", quote: "my-4 px-4 text-lg", figure: "my-6", divider: "my-6", list: "my-1" },
|
|
54
|
+
compact: { p: "my-1", quote: "my-2 px-3", figure: "my-3", divider: "my-3", list: "my-0.5" },
|
|
55
|
+
} as const;
|
|
56
|
+
const BULLETS = ["list-disc", "list-[circle]", "list-[square]"];
|
|
57
|
+
|
|
58
|
+
/** Draws `blocks`. Pass the same `extensions` the editor was given, for an app's own blocks. */
|
|
59
|
+
export function DocumentView({ blocks, extensions, density = "page", className }: { blocks: AnyBlock[]; extensions?: AnyExtension[]; density?: Density; className?: string }) {
|
|
60
|
+
return (
|
|
61
|
+
<Extensions value={new Map((extensions ?? []).map((x) => [x.type, x]))}>
|
|
62
|
+
<Dense value={density}>
|
|
63
|
+
<div className={cn("text-ink", className)}>
|
|
64
|
+
<Blocks blocks={blocks} />
|
|
65
|
+
</div>
|
|
66
|
+
</Dense>
|
|
67
|
+
</Extensions>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The blocks of one level, lists run together. An extension's `View` uses this for its children. */
|
|
72
|
+
export function Blocks({ blocks, depth = 0 }: { blocks: AnyBlock[]; depth?: number }) {
|
|
73
|
+
const extensions = useContext(Extensions);
|
|
74
|
+
const space = SPACE[useContext(Dense)];
|
|
75
|
+
const out: ReactNode[] = [];
|
|
76
|
+
for (let i = 0; i < blocks.length; i += 1) {
|
|
77
|
+
const block = blocks[i]!;
|
|
78
|
+
const extension = extensions.get(block.type);
|
|
79
|
+
if (extension !== undefined) {
|
|
80
|
+
if (extension.View !== undefined) out.push(<extension.View key={block.id} block={block} depth={depth} />);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const b = block as Block;
|
|
84
|
+
if (b.type === "bullet" || b.type === "numbered" || b.type === "check") {
|
|
85
|
+
const run: Extract<Block, { type: "bullet" | "numbered" | "check" }>[] = [];
|
|
86
|
+
while (i < blocks.length && blocks[i]!.type === b.type) run.push(blocks[i++] as (typeof run)[number]);
|
|
87
|
+
i -= 1;
|
|
88
|
+
const List = b.type === "numbered" ? "ol" : "ul";
|
|
89
|
+
out.push(
|
|
90
|
+
<List key={b.id} className={cn(space.list, "pl-6", b.type === "numbered" ? "list-decimal" : b.type === "bullet" ? BULLETS[depth % 3] : "list-none pl-1")}>
|
|
91
|
+
{run.map((item) => (
|
|
92
|
+
<li key={item.id} className="py-0.5 pl-1">
|
|
93
|
+
{item.type === "check" ? (
|
|
94
|
+
<span className="flex items-start gap-2">
|
|
95
|
+
<span aria-hidden className={cn("mt-1 flex size-3.5 shrink-0 items-center justify-center rounded-[3px] text-[10px] shadow-[inset_0_0_0_1px_var(--hairline-strong)]", item.checked && "bg-accent text-accent-ink")}>
|
|
96
|
+
{item.checked ? "✓" : ""}
|
|
97
|
+
</span>
|
|
98
|
+
<span className={cn(item.checked && "text-ink-secondary line-through")}>
|
|
99
|
+
<Spans text={item.text} />
|
|
100
|
+
</span>
|
|
101
|
+
</span>
|
|
102
|
+
) : (
|
|
103
|
+
<Spans text={item.text} />
|
|
104
|
+
)}
|
|
105
|
+
{item.children.length > 0 && <Blocks blocks={item.children} depth={depth + 1} />}
|
|
106
|
+
</li>
|
|
107
|
+
))}
|
|
108
|
+
</List>,
|
|
109
|
+
);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
out.push(<BlockView key={b.id} block={b} />);
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function BlockView({ block }: { block: Block }) {
|
|
118
|
+
const density = useContext(Dense);
|
|
119
|
+
const space = SPACE[density];
|
|
120
|
+
switch (block.type) {
|
|
121
|
+
case "paragraph":
|
|
122
|
+
return (
|
|
123
|
+
<p className={cn("min-h-[1.5em]", space.p)}>
|
|
124
|
+
<Spans text={block.text} />
|
|
125
|
+
</p>
|
|
126
|
+
);
|
|
127
|
+
case "heading":
|
|
128
|
+
return (
|
|
129
|
+
<div role="heading" aria-level={block.level + 1} className={cn("text-accent-text first:mt-0", HEADINGS[density][block.level])}>
|
|
130
|
+
<Spans text={block.text} />
|
|
131
|
+
</div>
|
|
132
|
+
);
|
|
133
|
+
case "quote":
|
|
134
|
+
return (
|
|
135
|
+
<blockquote className={cn("border-l-[3px] border-ink leading-relaxed", space.quote)}>
|
|
136
|
+
<Spans text={block.text} />
|
|
137
|
+
</blockquote>
|
|
138
|
+
);
|
|
139
|
+
case "divider":
|
|
140
|
+
return <hr className={cn("border-hairline-strong", space.divider)} />;
|
|
141
|
+
case "image":
|
|
142
|
+
if (block.src === undefined) return null;
|
|
143
|
+
return (
|
|
144
|
+
<figure className={cn("mx-auto", space.figure)} style={{ width: `${block.width * 100}%` }}>
|
|
145
|
+
<Image src={block.src} aspect={block.aspect} alt="" loading="lazy" className="rounded-surface" />
|
|
146
|
+
{block.caption.length > 0 && (
|
|
147
|
+
<figcaption className="mt-2 text-center text-sm text-ink-secondary">
|
|
148
|
+
<Spans text={block.caption} />
|
|
149
|
+
</figcaption>
|
|
150
|
+
)}
|
|
151
|
+
</figure>
|
|
152
|
+
);
|
|
153
|
+
case "video": {
|
|
154
|
+
const embed = block.url === "" ? null : videoEmbed(block.url);
|
|
155
|
+
if (embed === null) return null;
|
|
156
|
+
return (
|
|
157
|
+
<figure className={space.figure}>
|
|
158
|
+
<div className="aspect-video overflow-hidden rounded-surface bg-surface-sunken">
|
|
159
|
+
<iframe
|
|
160
|
+
src={embed}
|
|
161
|
+
title={`Video from ${hostOf(embed)}`}
|
|
162
|
+
className="size-full"
|
|
163
|
+
allow="fullscreen; picture-in-picture; encrypted-media"
|
|
164
|
+
sandbox="allow-scripts allow-same-origin allow-presentation allow-popups"
|
|
165
|
+
referrerPolicy="strict-origin-when-cross-origin"
|
|
166
|
+
loading="lazy"
|
|
167
|
+
/>
|
|
168
|
+
</div>
|
|
169
|
+
{block.caption.length > 0 && (
|
|
170
|
+
<figcaption className="mt-2 text-sm text-ink-secondary">
|
|
171
|
+
<Spans text={block.caption} />
|
|
172
|
+
</figcaption>
|
|
173
|
+
)}
|
|
174
|
+
</figure>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
case "bookmark":
|
|
178
|
+
if (block.url === "" || !safe(block.url)) return null;
|
|
179
|
+
return (
|
|
180
|
+
<a href={block.url} target="_blank" rel="noreferrer nofollow ugc" className={cn(density === "compact" ? "my-2" : "my-4", "flex items-center gap-3 rounded-surface px-3.5 py-3 shadow-[inset_0_0_0_1px_var(--hairline)] transition-colors duration-100 hover:bg-tint-hover")}>
|
|
181
|
+
<span className="flex size-9 shrink-0 items-center justify-center rounded-control bg-accent-tint text-accent-text">
|
|
182
|
+
<Globe className="size-4" />
|
|
183
|
+
</span>
|
|
184
|
+
<span className="min-w-0 flex-1">
|
|
185
|
+
<span className="block truncate font-medium text-ink">{block.title || hostOf(block.url)}</span>
|
|
186
|
+
<span className="block truncate text-sm text-ink-tertiary">{block.url}</span>
|
|
187
|
+
</span>
|
|
188
|
+
<ExternalLink className="size-4 shrink-0 text-ink-secondary" />
|
|
189
|
+
</a>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode, type RefObject } from "react";
|
|
4
|
+
import type { AnyBlock } from "../../lib/document.ts";
|
|
5
|
+
import { contains, flat, insert, locate, remove, update } from "../../lib/tree.ts";
|
|
6
|
+
import { setCaret } from "../../lib/rich-text-dom.ts";
|
|
7
|
+
import { hasText, schemaFor, toBlocks, toNodes, type AnyExtension, type Draft, type BuiltinNode, type KindOptions, type Node, type Schema } from "./kinds.ts";
|
|
8
|
+
|
|
9
|
+
type FocusAt = number | "end";
|
|
10
|
+
type Drop = { id: number; side: "before" | "after" };
|
|
11
|
+
|
|
12
|
+
/** Uploads a picture and says where it can be loaded from; see DocumentEditor. */
|
|
13
|
+
export type UploadImage = (file: File, progress: (share: number) => void) => Promise<{ src: string; ref?: string; aspect?: number }>;
|
|
14
|
+
|
|
15
|
+
/** Refuses a link a reader's browser would load or open: why, or null to take it. */
|
|
16
|
+
export type CheckLink = (url: string, kind: "image" | "video" | "bookmark") => string | null;
|
|
17
|
+
|
|
18
|
+
/** A click on a link or page mention in the text. Without it, links open in a new tab and mentions do nothing. */
|
|
19
|
+
export type OnLink = (link: { href?: string; page?: string }) => void;
|
|
20
|
+
|
|
21
|
+
export type ImageNode = Extract<BuiltinNode, { type: "image" }>;
|
|
22
|
+
/**
|
|
23
|
+
* Extras for the kit's picture block, per block: `more` beside its file and link
|
|
24
|
+
* fields (a picker of the app's own pictures), `actions` over a picture once it
|
|
25
|
+
* has one (Replace), and `placeholder`, a tiny version shown blurred while it
|
|
26
|
+
* loads. `set` changes the block, as the picture block itself does.
|
|
27
|
+
*/
|
|
28
|
+
export type ImageExtras = (block: ImageNode, set: (patch: Partial<ImageNode>) => void) => { more?: ReactNode; actions?: ReactNode; placeholder?: string };
|
|
29
|
+
|
|
30
|
+
type Editor = {
|
|
31
|
+
schema: Schema;
|
|
32
|
+
uploadImage?: UploadImage;
|
|
33
|
+
checkLink?: CheckLink;
|
|
34
|
+
onLink?: OnLink;
|
|
35
|
+
imageExtras?: ImageExtras;
|
|
36
|
+
/** The editor's own element: keys and focus are looked for inside it only. */
|
|
37
|
+
root: RefObject<HTMLDivElement | null>;
|
|
38
|
+
blocks: Node[];
|
|
39
|
+
/** Bumped when a block's text is replaced from outside its own element. */
|
|
40
|
+
rev: (id: number) => number;
|
|
41
|
+
/** Changes a block and tells the owner, saying whether it was typing. */
|
|
42
|
+
edit: (id: number, fn: (node: Node) => Node, opts?: { typing?: boolean; rewrite?: boolean }) => void;
|
|
43
|
+
create: (parentId: number | null, afterId: number | null, draft: Draft, opts?: { focus?: boolean }) => number;
|
|
44
|
+
remove: (id: number) => void;
|
|
45
|
+
move: (id: number, parentId: number | null, afterId: number | null) => void;
|
|
46
|
+
/** The caret into a block: its text, an extension's `focus`, or else the block selected whole. */
|
|
47
|
+
focus: (id: number, at?: FocusAt) => void;
|
|
48
|
+
/**
|
|
49
|
+
* The block picked as a whole — a picture, video, link card or divider, which
|
|
50
|
+
* have no caret of their own. While one is, Backspace or Delete removes it,
|
|
51
|
+
* Enter starts a line below it and the arrows move on, as in Notion.
|
|
52
|
+
*/
|
|
53
|
+
selected: number | null;
|
|
54
|
+
select: (id: number | null) => void;
|
|
55
|
+
drag: { id: number | null; drop: Drop | null; start: (id: number) => void; over: (drop: Drop | null) => void; end: () => void };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const Ctx = createContext<Editor | null>(null);
|
|
59
|
+
|
|
60
|
+
export function useEditor(): Editor {
|
|
61
|
+
const editor = useContext(Ctx);
|
|
62
|
+
if (!editor) throw new Error("useEditor outside DocumentProvider");
|
|
63
|
+
return editor;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const TYPING = 1;
|
|
67
|
+
const CHANGE = 0;
|
|
68
|
+
|
|
69
|
+
/** New blocks are numbered here, above any the document already has, so one can be focused before it is saved. */
|
|
70
|
+
const nextId = (nodes: Node[]): number => nodes.reduce((max, n) => Math.max(max, n.id, nextId(n.children) - 1), 0) + 1;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Holds a document's tree. Edits apply here, and `onChange` hears every one, with
|
|
74
|
+
* the document in its stored shape and whether it was typing (save after a
|
|
75
|
+
* pause) or a change of structure (save soon). Saving is the owner's.
|
|
76
|
+
*/
|
|
77
|
+
export type ProviderProps = {
|
|
78
|
+
initial: AnyBlock[];
|
|
79
|
+
onChange?: (blocks: AnyBlock[], cause: "typing" | "change") => void;
|
|
80
|
+
uploadImage?: UploadImage;
|
|
81
|
+
checkLink?: CheckLink;
|
|
82
|
+
onLink?: OnLink;
|
|
83
|
+
imageExtras?: ImageExtras;
|
|
84
|
+
extensions?: AnyExtension[];
|
|
85
|
+
kinds?: KindOptions;
|
|
86
|
+
/**
|
|
87
|
+
* The id for a new block. By default one above the highest in the document,
|
|
88
|
+
* which is unique within it; give your own where ids must be unique more
|
|
89
|
+
* widely, for example a store that keeps every page's blocks in one table.
|
|
90
|
+
*/
|
|
91
|
+
newId?: () => number;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export function DocumentProvider({
|
|
95
|
+
initial,
|
|
96
|
+
onChange,
|
|
97
|
+
uploadImage,
|
|
98
|
+
checkLink,
|
|
99
|
+
onLink,
|
|
100
|
+
imageExtras,
|
|
101
|
+
extensions,
|
|
102
|
+
kinds,
|
|
103
|
+
newId,
|
|
104
|
+
children,
|
|
105
|
+
}: ProviderProps & { children: ReactNode }) {
|
|
106
|
+
// Built once: an app's extensions and kinds are fixed for the life of an editor.
|
|
107
|
+
const [schema] = useState(() => schemaFor(extensions, kinds));
|
|
108
|
+
const root = useRef<HTMLDivElement>(null);
|
|
109
|
+
const [blocks, setBlocks] = useState(() => toNodes(initial));
|
|
110
|
+
const [revs, setRevs] = useState<Record<number, number>>({});
|
|
111
|
+
const [dragId, setDragId] = useState<number | null>(null);
|
|
112
|
+
const [drop, setDrop] = useState<Drop | null>(null);
|
|
113
|
+
const [pendingFocus, setPendingFocus] = useState<{ id: number; at: FocusAt } | null>(null);
|
|
114
|
+
const [selected, setSelected] = useState<number | null>(null);
|
|
115
|
+
|
|
116
|
+
const tree = useRef(blocks);
|
|
117
|
+
const counter = useRef(nextId(blocks));
|
|
118
|
+
const mint = useRef(newId);
|
|
119
|
+
mint.current = newId;
|
|
120
|
+
const fresh = useCallback(() => mint.current?.() ?? counter.current++, []);
|
|
121
|
+
const listener = useRef(onChange);
|
|
122
|
+
listener.current = onChange;
|
|
123
|
+
const schedule = useCallback(
|
|
124
|
+
(cause: number) => {
|
|
125
|
+
listener.current?.(toBlocks(tree.current, schema), cause === TYPING ? "typing" : "change");
|
|
126
|
+
},
|
|
127
|
+
[schema],
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
if (!pendingFocus) return;
|
|
132
|
+
setPendingFocus(null);
|
|
133
|
+
const { id, at } = pendingFocus;
|
|
134
|
+
const el = root.current?.querySelector<HTMLElement>(`[data-text="${id}"]`);
|
|
135
|
+
if (el) return setCaret(el, at);
|
|
136
|
+
// A block without words: its extension may take the caret, or it is selected.
|
|
137
|
+
const node = locate(tree.current, id)?.node;
|
|
138
|
+
const block = root.current?.querySelector<HTMLElement>(`[data-block="${id}"]`);
|
|
139
|
+
const extension = node && schema.extension(node.type);
|
|
140
|
+
if (block && extension?.focus?.(block, at)) return;
|
|
141
|
+
if (node) setSelected(id);
|
|
142
|
+
}, [pendingFocus, schema]);
|
|
143
|
+
|
|
144
|
+
const commit = useCallback((next: Node[]) => {
|
|
145
|
+
tree.current = next;
|
|
146
|
+
setBlocks(next);
|
|
147
|
+
}, []);
|
|
148
|
+
|
|
149
|
+
/** Puts the caret in a block with text, or selects one without. */
|
|
150
|
+
const land = useCallback((node: Node, at: FocusAt) => {
|
|
151
|
+
if (hasText(node)) {
|
|
152
|
+
setSelected(null);
|
|
153
|
+
setPendingFocus({ id: node.id, at });
|
|
154
|
+
} else setSelected(node.id);
|
|
155
|
+
}, []);
|
|
156
|
+
|
|
157
|
+
// The keys of a selected block. On the window, because a selected block holds
|
|
158
|
+
// no caret: the text that had one is blurred, so typing cannot edit it unseen.
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
if (selected === null) return;
|
|
161
|
+
(document.activeElement as HTMLElement | null)?.blur?.();
|
|
162
|
+
const onKey = (e: KeyboardEvent) => {
|
|
163
|
+
if (e.defaultPrevented) return;
|
|
164
|
+
// The target is an element when focus is somewhere, the window or document otherwise.
|
|
165
|
+
// Keys meant for anything outside this editor, such as a dialog opened from the
|
|
166
|
+
// selected block, are not the selection's: only those on the page itself are.
|
|
167
|
+
if (e.target instanceof Element && e.target !== document.body && !root.current?.contains(e.target)) return;
|
|
168
|
+
if (e.target instanceof Element && e.target.closest("input, textarea, select, [contenteditable=true], [role=dialog], [role=menu]")) return;
|
|
169
|
+
const order = flat(tree.current);
|
|
170
|
+
const index = order.findIndex((n) => n.id === selected);
|
|
171
|
+
const node = order[index];
|
|
172
|
+
if (node === undefined) return setSelected(null);
|
|
173
|
+
const place = locate(tree.current, selected)!;
|
|
174
|
+
|
|
175
|
+
if (e.key === "Backspace" || e.key === "Delete") {
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
const before = order[index - 1];
|
|
178
|
+
const after = order.slice(index + 1).find((n) => !contains(node, n.id));
|
|
179
|
+
commit(remove(tree.current, selected));
|
|
180
|
+
schedule(CHANGE);
|
|
181
|
+
setSelected(null);
|
|
182
|
+
// Where the caret goes next: the block above, else below, else a fresh line.
|
|
183
|
+
if (before !== undefined) land(before, "end");
|
|
184
|
+
else if (after !== undefined) land(after, 0);
|
|
185
|
+
else {
|
|
186
|
+
const id = fresh();
|
|
187
|
+
commit(insert(tree.current, null, null, { type: "paragraph", text: [], id, children: [] } as Node));
|
|
188
|
+
schedule(CHANGE);
|
|
189
|
+
setPendingFocus({ id, at: 0 });
|
|
190
|
+
}
|
|
191
|
+
} else if (e.key === "Enter") {
|
|
192
|
+
e.preventDefault();
|
|
193
|
+
const id = fresh();
|
|
194
|
+
commit(insert(tree.current, place.parent?.id ?? null, selected, { type: "paragraph", text: [], id, children: [] } as Node));
|
|
195
|
+
schedule(CHANGE);
|
|
196
|
+
setSelected(null);
|
|
197
|
+
setPendingFocus({ id, at: 0 });
|
|
198
|
+
} else if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
199
|
+
const to = order[index + (e.key === "ArrowUp" ? -1 : 1)];
|
|
200
|
+
if (to === undefined) return;
|
|
201
|
+
e.preventDefault();
|
|
202
|
+
land(to, e.key === "ArrowUp" ? "end" : 0);
|
|
203
|
+
} else if (e.key === "Escape") setSelected(null);
|
|
204
|
+
};
|
|
205
|
+
// A press anywhere but on the selected block lets it go.
|
|
206
|
+
const onDown = (e: PointerEvent) => {
|
|
207
|
+
if (!(e.target instanceof Element && e.target.closest(`[data-block="${selected}"]`))) setSelected(null);
|
|
208
|
+
};
|
|
209
|
+
window.addEventListener("keydown", onKey);
|
|
210
|
+
window.addEventListener("pointerdown", onDown);
|
|
211
|
+
return () => {
|
|
212
|
+
window.removeEventListener("keydown", onKey);
|
|
213
|
+
window.removeEventListener("pointerdown", onDown);
|
|
214
|
+
};
|
|
215
|
+
}, [selected, commit, land, fresh]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
216
|
+
|
|
217
|
+
const editor = useMemo<Editor>(
|
|
218
|
+
() => ({
|
|
219
|
+
schema,
|
|
220
|
+
uploadImage,
|
|
221
|
+
checkLink,
|
|
222
|
+
onLink,
|
|
223
|
+
imageExtras,
|
|
224
|
+
root,
|
|
225
|
+
blocks,
|
|
226
|
+
rev: (id) => revs[id] ?? 0,
|
|
227
|
+
edit: (id, fn, opts) => {
|
|
228
|
+
commit(update(tree.current, id, fn));
|
|
229
|
+
if (opts?.rewrite) setRevs((r) => ({ ...r, [id]: (r[id] ?? 0) + 1 }));
|
|
230
|
+
schedule(opts?.typing ? TYPING : CHANGE);
|
|
231
|
+
},
|
|
232
|
+
create: (parentId, afterId, draft, opts) => {
|
|
233
|
+
const id = fresh();
|
|
234
|
+
commit(insert(tree.current, parentId, afterId, { ...draft, id, children: [] } as Node));
|
|
235
|
+
if (opts?.focus) setPendingFocus({ id, at: 0 });
|
|
236
|
+
schedule(CHANGE);
|
|
237
|
+
return id;
|
|
238
|
+
},
|
|
239
|
+
remove: (id) => {
|
|
240
|
+
commit(remove(tree.current, id));
|
|
241
|
+
schedule(CHANGE);
|
|
242
|
+
},
|
|
243
|
+
move: (id, parentId, afterId) => {
|
|
244
|
+
const place = locate(tree.current, id);
|
|
245
|
+
if (!place || id === afterId || id === parentId || contains(place.node, parentId ?? -1)) return;
|
|
246
|
+
commit(insert(remove(tree.current, id), parentId, afterId, place.node));
|
|
247
|
+
schedule(CHANGE);
|
|
248
|
+
},
|
|
249
|
+
focus: (id, at = "end") => {
|
|
250
|
+
setSelected(null);
|
|
251
|
+
setPendingFocus({ id, at });
|
|
252
|
+
},
|
|
253
|
+
selected,
|
|
254
|
+
select: setSelected,
|
|
255
|
+
drag: {
|
|
256
|
+
id: dragId,
|
|
257
|
+
drop,
|
|
258
|
+
start: setDragId,
|
|
259
|
+
over: setDrop,
|
|
260
|
+
end: () => {
|
|
261
|
+
setDragId(null);
|
|
262
|
+
setDrop(null);
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
}),
|
|
266
|
+
[schema, uploadImage, checkLink, onLink, imageExtras, blocks, revs, dragId, drop, commit, schedule, selected, fresh],
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return <Ctx.Provider value={editor}>{children}</Ctx.Provider>;
|
|
270
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { EyeOff } from "lucide-react";
|
|
4
|
+
import type { AnyBlock } from "../../../lib/document.ts";
|
|
5
|
+
import type { Text } from "../../../lib/rich-text.ts";
|
|
6
|
+
import { BlockList, BlockRow } from "../blocks.tsx";
|
|
7
|
+
import { BlockText } from "../block-text.tsx";
|
|
8
|
+
import { Blocks, Spans } from "../document-view.tsx";
|
|
9
|
+
import { useEditor } from "../editor-state.tsx";
|
|
10
|
+
import type { BlockExtension, Node, TextNode } from "../kinds.ts";
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* A spoiler: its own line says what it hides, and the blocks inside it are the
|
|
14
|
+
* spoiler. Written open, read closed until pressed. Built only from what the kit
|
|
15
|
+
* exports to extensions, so it doubles as the example of writing one.
|
|
16
|
+
*
|
|
17
|
+
* Stored as { type: "spoiler", text, children }. It hides text in the interface
|
|
18
|
+
* only: whoever stores a document must keep a spoiler's children out of any
|
|
19
|
+
* summary or excerpt shown beside it.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
function SpoilerEdit({ block, depth }: { block: Node; depth: number }) {
|
|
23
|
+
const ed = useEditor();
|
|
24
|
+
return (
|
|
25
|
+
<div className="my-1.5 rounded-surface bg-tint-hover/70 py-1 shadow-[inset_0_0_0_1px_var(--hairline)]">
|
|
26
|
+
<BlockRow block={block}>
|
|
27
|
+
<div className="flex items-start gap-2 px-2.5 py-[3px]">
|
|
28
|
+
<span contentEditable={false} className="flex h-[1.5em] shrink-0 items-center text-ink-tertiary select-none" title="Spoiler: hidden from readers until opened">
|
|
29
|
+
<EyeOff className="size-4" />
|
|
30
|
+
</span>
|
|
31
|
+
<BlockText block={block as TextNode} placeholder="Spoiler — say what it hides, e.g. “The ending”" alwaysShowPlaceholder className="flex-1 font-medium" />
|
|
32
|
+
</div>
|
|
33
|
+
</BlockRow>
|
|
34
|
+
<div className="pr-2.5 pl-8">
|
|
35
|
+
{block.children.length > 0 ? (
|
|
36
|
+
<BlockList blocks={block.children} depth={depth + 1} />
|
|
37
|
+
) : (
|
|
38
|
+
<button type="button" onClick={() => ed.create(block.id, null, { type: "paragraph", text: [] }, { focus: true })} className="px-0.5 py-[3px] text-left text-sm text-ink-tertiary hover:text-ink-secondary">
|
|
39
|
+
Write what it hides
|
|
40
|
+
</button>
|
|
41
|
+
)}
|
|
42
|
+
</div>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function SpoilerView({ block, depth }: { block: AnyBlock; depth: number }) {
|
|
48
|
+
// A View receives any block; this one is always a spoiler's shape.
|
|
49
|
+
const { text = [], children = [] } = block as { text?: Text; children?: AnyBlock[] };
|
|
50
|
+
return (
|
|
51
|
+
<details className="group my-4 rounded-surface bg-tint-hover/70 shadow-[inset_0_0_0_1px_var(--hairline)]">
|
|
52
|
+
<summary className="flex cursor-pointer list-none items-center gap-2 px-4 py-3 font-medium select-none [&::-webkit-details-marker]:hidden">
|
|
53
|
+
<EyeOff className="size-4 shrink-0 text-ink-tertiary" />
|
|
54
|
+
<span className="min-w-0 flex-1">
|
|
55
|
+
<Spans text={text.length > 0 ? text : [{ text: "Spoiler" }]} />
|
|
56
|
+
</span>
|
|
57
|
+
<span className="text-caption font-normal text-ink-tertiary group-open:hidden">Spoiler · show</span>
|
|
58
|
+
<span className="hidden text-caption font-normal text-ink-tertiary group-open:inline">Hide</span>
|
|
59
|
+
</summary>
|
|
60
|
+
<div className="px-4 pb-3">
|
|
61
|
+
<Blocks blocks={children} depth={depth + 1} />
|
|
62
|
+
</div>
|
|
63
|
+
</details>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const spoilerBlock: BlockExtension<{ text: Text; children?: AnyBlock[] }> = {
|
|
68
|
+
type: "spoiler",
|
|
69
|
+
kinds: [{ key: "spoiler", label: "Spoiler", hint: "Hidden until a reader opens it", icon: EyeOff, make: (text) => ({ type: "spoiler", text }) }],
|
|
70
|
+
shortcuts: { "!! ": "spoiler" },
|
|
71
|
+
// Anything can be hidden; a spoiler inside a spoiler is allowed but rarely wanted.
|
|
72
|
+
holds: () => true,
|
|
73
|
+
toggle: true,
|
|
74
|
+
Edit: SpoilerEdit,
|
|
75
|
+
View: SpoilerView,
|
|
76
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { DocumentEditor, type CheckLink, type DocumentEditorHandle, type ImageExtras, type OnLink, type UploadImage } from "./document-editor.tsx";
|
|
2
|
+
export { DocumentView, Blocks, Spans, type Density } from "./document-view.tsx";
|
|
3
|
+
export { spoilerBlock } from "./extensions/spoiler.tsx";
|
|
4
|
+
// What an app needs to write its own block (see BlockExtension and extensions/spoiler).
|
|
5
|
+
export { BlockList, BlockRow, Caption, PictureBlock } from "./blocks.tsx";
|
|
6
|
+
export { BlockText } from "./block-text.tsx";
|
|
7
|
+
export { useEditor } from "./editor-state.tsx";
|
|
8
|
+
export { schemaFor, type AnyExtension, type BlockExtension, type BlockKind, type KindOptions, type Node, type TextNode } from "./kinds.ts";
|
|
9
|
+
export type { AnyBlock, Block, CustomBlock } from "../../lib/document.ts";
|