@folijs/react 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 +19 -0
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/FoliCanvas.d.ts +23 -0
- package/dist/FoliCanvas.d.ts.map +1 -0
- package/dist/FoliCanvas.js +32 -0
- package/dist/FoliCanvas.js.map +1 -0
- package/dist/FoliEditor.d.ts +44 -0
- package/dist/FoliEditor.d.ts.map +1 -0
- package/dist/FoliEditor.js +38 -0
- package/dist/FoliEditor.js.map +1 -0
- package/dist/FoliToolbar.d.ts +26 -0
- package/dist/FoliToolbar.d.ts.map +1 -0
- package/dist/FoliToolbar.js +136 -0
- package/dist/FoliToolbar.js.map +1 -0
- package/dist/editor.d.ts +40 -0
- package/dist/editor.d.ts.map +1 -0
- package/dist/editor.js +63 -0
- package/dist/editor.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -3
- package/src/FoliCanvas.tsx +68 -0
- package/src/FoliEditor.tsx +103 -0
- package/src/FoliToolbar.tsx +231 -0
- package/src/editor.tsx +105 -0
- package/src/index.ts +16 -0
- package/styles.css +142 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Editor } from "@folijs/core";
|
|
2
|
+
import { EditorView } from "@folijs/view";
|
|
3
|
+
import { forwardRef, useImperativeHandle, useRef } from "react";
|
|
4
|
+
import { useEditorOrContext, useIsomorphicLayoutEffect } from "./editor.js";
|
|
5
|
+
|
|
6
|
+
export interface FoliCanvasProps {
|
|
7
|
+
/** Default: the editor of the nearest `FoliEditorProvider`. */
|
|
8
|
+
editor?: Editor | undefined;
|
|
9
|
+
/** The user can read, select and copy, but not change the document. */
|
|
10
|
+
readOnly?: boolean | undefined;
|
|
11
|
+
/** Shown while the document is empty. */
|
|
12
|
+
placeholder?: string | undefined;
|
|
13
|
+
/** Accessible name of the editable area (default "Document"). */
|
|
14
|
+
label?: string | undefined;
|
|
15
|
+
className?: string | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** What a `ref` on `FoliCanvas` gives. */
|
|
19
|
+
export interface FoliCanvasHandle {
|
|
20
|
+
/** Focuses the document and shows the editor's selection in it. */
|
|
21
|
+
focus(): void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The editable document on real pages (size and margins from the document settings). The view
|
|
26
|
+
* owns the DOM inside it; React never renders children there.
|
|
27
|
+
*/
|
|
28
|
+
export const FoliCanvas = forwardRef<FoliCanvasHandle, FoliCanvasProps>(function FoliCanvas(
|
|
29
|
+
{ editor: given, readOnly = false, placeholder = "", label = "Document", className },
|
|
30
|
+
ref,
|
|
31
|
+
) {
|
|
32
|
+
const editor = useEditorOrContext(given, "FoliCanvas");
|
|
33
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
34
|
+
const viewRef = useRef<EditorView | null>(null);
|
|
35
|
+
|
|
36
|
+
useImperativeHandle(ref, () => ({ focus: () => viewRef.current?.focus() }), []);
|
|
37
|
+
|
|
38
|
+
useIsomorphicLayoutEffect(() => {
|
|
39
|
+
const host = contentRef.current;
|
|
40
|
+
if (!host) return;
|
|
41
|
+
const view = new EditorView(host, editor);
|
|
42
|
+
viewRef.current = view;
|
|
43
|
+
return () => {
|
|
44
|
+
view.destroy();
|
|
45
|
+
viewRef.current = null;
|
|
46
|
+
};
|
|
47
|
+
}, [editor]);
|
|
48
|
+
|
|
49
|
+
// Runs after the view is created (effects run in order): for a new view, and when the props change.
|
|
50
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: a new editor means a new view, which needs the options too.
|
|
51
|
+
useIsomorphicLayoutEffect(() => {
|
|
52
|
+
viewRef.current?.setOptions({ editable: !readOnly, placeholder });
|
|
53
|
+
}, [editor, readOnly, placeholder]);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div className={className ? `foli-canvas ${className}` : "foli-canvas"}>
|
|
57
|
+
{/* biome-ignore lint/a11y/useSemanticElements: EditorView makes this div a contenteditable rich-text box. */}
|
|
58
|
+
<div
|
|
59
|
+
ref={contentRef}
|
|
60
|
+
className="foli-content"
|
|
61
|
+
role="textbox"
|
|
62
|
+
aria-multiline="true"
|
|
63
|
+
aria-label={label}
|
|
64
|
+
tabIndex={0}
|
|
65
|
+
/>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDocument,
|
|
3
|
+
type DocJSON,
|
|
4
|
+
type Editor,
|
|
5
|
+
type FoliDocument,
|
|
6
|
+
type Plugin,
|
|
7
|
+
type Transaction,
|
|
8
|
+
} from "@folijs/core";
|
|
9
|
+
import { forwardRef, type ReactNode, useImperativeHandle, useRef } from "react";
|
|
10
|
+
import { FoliEditorProvider, useFoliEditor, useIsomorphicLayoutEffect } from "./editor.js";
|
|
11
|
+
import { FoliCanvas, type FoliCanvasHandle, type FoliCanvasProps } from "./FoliCanvas.js";
|
|
12
|
+
import { FoliToolbar } from "./FoliToolbar.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A change to the document's content, as `FoliEditor` reports it. `document` is a getter:
|
|
16
|
+
* spreading the object or `JSON.stringify(change)` serializes the document (and walks `editor`).
|
|
17
|
+
*/
|
|
18
|
+
export interface FoliChange {
|
|
19
|
+
readonly editor: Editor;
|
|
20
|
+
/** The transactions of this change (several when plugins append their own). */
|
|
21
|
+
readonly transactions: readonly Transaction[];
|
|
22
|
+
/**
|
|
23
|
+
* The whole document right after this change, serialized on first read (keep reads out of
|
|
24
|
+
* the hot path of typing: a debounced save reads only the last change's).
|
|
25
|
+
*/
|
|
26
|
+
readonly document: FoliDocument;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FoliEditorProps extends Omit<FoliCanvasProps, "editor"> {
|
|
30
|
+
/** Read once on mount. To load another document later: `editor.setDocument()` or a new `key`. */
|
|
31
|
+
initialContent?: FoliDocument | undefined;
|
|
32
|
+
/** Read once on mount. */
|
|
33
|
+
plugins?: readonly Plugin[] | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Called after every change to the document's content made through transactions (typing,
|
|
36
|
+
* commands, undo). Not for selection changes, and not for `editor.setDocument()`, whose
|
|
37
|
+
* caller already has the document.
|
|
38
|
+
*/
|
|
39
|
+
onChange?: ((change: FoliChange) => void) | undefined;
|
|
40
|
+
/** Show the default `FoliToolbar` above the pages (default true). */
|
|
41
|
+
toolbar?: boolean | undefined;
|
|
42
|
+
/** Rendered between the toolbar and the pages, inside the editor's context. */
|
|
43
|
+
children?: ReactNode | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What a `ref` on `FoliEditor` gives: its editor (commands, `setDocument`…) and focus. */
|
|
47
|
+
export interface FoliEditorHandle extends FoliCanvasHandle {
|
|
48
|
+
readonly editor: Editor;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* An editable document on real pages with a toolbar: creates its editor, provides it to
|
|
53
|
+
* `children` and shows it. Uncontrolled: the editor owns the document; React observes it
|
|
54
|
+
* through `onChange`, a parent reaches it through a `ref`.
|
|
55
|
+
*/
|
|
56
|
+
export const FoliEditor = forwardRef<FoliEditorHandle, FoliEditorProps>(function FoliEditor(
|
|
57
|
+
{ initialContent, plugins, onChange, toolbar = true, children, ...canvas },
|
|
58
|
+
ref,
|
|
59
|
+
) {
|
|
60
|
+
const editor = useFoliEditor({ initialContent, plugins });
|
|
61
|
+
const onChangeRef = useRef(onChange);
|
|
62
|
+
const canvasRef = useRef<FoliCanvasHandle>(null);
|
|
63
|
+
|
|
64
|
+
useImperativeHandle(ref, () => ({ editor, focus: () => canvasRef.current?.focus() }), [editor]);
|
|
65
|
+
|
|
66
|
+
useIsomorphicLayoutEffect(() => {
|
|
67
|
+
onChangeRef.current = onChange;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
useIsomorphicLayoutEffect(
|
|
71
|
+
() =>
|
|
72
|
+
editor.subscribe((state, transactions) => {
|
|
73
|
+
const listener = onChangeRef.current;
|
|
74
|
+
if (!listener || !transactions.some((tr) => tr.docChanged)) return;
|
|
75
|
+
// The state is immutable: keeping it makes a later read give this change's document.
|
|
76
|
+
const { settings } = editor;
|
|
77
|
+
let document: FoliDocument | undefined;
|
|
78
|
+
listener({
|
|
79
|
+
editor,
|
|
80
|
+
transactions,
|
|
81
|
+
get document() {
|
|
82
|
+
document ??= createDocument(state.doc.toJSON() as DocJSON, settings);
|
|
83
|
+
return document;
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}),
|
|
87
|
+
[editor],
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<FoliEditorProvider editor={editor}>
|
|
92
|
+
{toolbar && (
|
|
93
|
+
<FoliToolbar
|
|
94
|
+
editor={editor}
|
|
95
|
+
disabled={canvas.readOnly}
|
|
96
|
+
onClickCommand={() => canvasRef.current?.focus()}
|
|
97
|
+
/>
|
|
98
|
+
)}
|
|
99
|
+
{children}
|
|
100
|
+
<FoliCanvas ref={canvasRef} editor={editor} {...canvas} />
|
|
101
|
+
</FoliEditorProvider>
|
|
102
|
+
);
|
|
103
|
+
});
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Command,
|
|
3
|
+
type Editor,
|
|
4
|
+
type EditorState,
|
|
5
|
+
insertPageBreak,
|
|
6
|
+
redo,
|
|
7
|
+
redoDepth,
|
|
8
|
+
setBlockType,
|
|
9
|
+
toggleMark,
|
|
10
|
+
undo,
|
|
11
|
+
undoDepth,
|
|
12
|
+
} from "@folijs/core";
|
|
13
|
+
import { Fragment, type KeyboardEvent, type MouseEvent, useRef, useState } from "react";
|
|
14
|
+
import { useEditorOrContext, useEditorState } from "./editor.js";
|
|
15
|
+
|
|
16
|
+
export interface FoliToolbarProps {
|
|
17
|
+
/** Default: the editor of the nearest `FoliEditorProvider`. */
|
|
18
|
+
editor?: Editor | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Every button is disabled. Pass it whenever the canvas is `readOnly`: commands run through
|
|
21
|
+
* the editor, which code may always change (`FoliEditor` does this for you).
|
|
22
|
+
*/
|
|
23
|
+
disabled?: boolean | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Called after a click ran a command, to show its result in the document (`FoliEditor`
|
|
26
|
+
* focuses its canvas). Keyboard activation keeps the focus in the toolbar.
|
|
27
|
+
*/
|
|
28
|
+
onClickCommand?: (() => void) | undefined;
|
|
29
|
+
/** Accessible name of the toolbar (default "Formatting"). */
|
|
30
|
+
label?: string | undefined;
|
|
31
|
+
className?: string | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Item {
|
|
35
|
+
key: string;
|
|
36
|
+
/** Tooltip: what the button does. */
|
|
37
|
+
label: string;
|
|
38
|
+
/** Visible text: the accessible name, unless it is a symbol (then the label is). */
|
|
39
|
+
text: string;
|
|
40
|
+
symbol?: boolean;
|
|
41
|
+
/** Whether the button shows as pressed (toggle buttons only). */
|
|
42
|
+
pressed?: (state: EditorState, editor: Editor) => boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Whether the command can run now (default: always; in schema v1 every selection is in
|
|
45
|
+
* textblocks, which take every mark, block type and page break). A cheap check, not a dry
|
|
46
|
+
* run: it runs after every transaction, and a dry run builds the whole transaction (for a
|
|
47
|
+
* block type over a large selection, quadratic in the number of blocks).
|
|
48
|
+
*/
|
|
49
|
+
enabled?: (state: EditorState) => boolean;
|
|
50
|
+
command: (state: EditorState) => Command;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether the selection holds textblocks and all of them are `type` with `level` (if given). */
|
|
54
|
+
function blockIs(state: EditorState, type: string, level?: number): boolean {
|
|
55
|
+
const { from, to } = state.selection;
|
|
56
|
+
let all = true;
|
|
57
|
+
let any = false;
|
|
58
|
+
state.doc.nodesBetween(from, to, (node) => {
|
|
59
|
+
if (!all) return false;
|
|
60
|
+
if (node.isTextblock) {
|
|
61
|
+
any = true;
|
|
62
|
+
if (node.type.name !== type || (level && node.attrs.level !== level)) all = false;
|
|
63
|
+
}
|
|
64
|
+
return !node.isTextblock;
|
|
65
|
+
});
|
|
66
|
+
return any && all;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const mark = (key: string, label: string, text: string): Item => ({
|
|
70
|
+
key,
|
|
71
|
+
label,
|
|
72
|
+
text,
|
|
73
|
+
symbol: true,
|
|
74
|
+
pressed: (_state, editor) => editor.isMarkActive(key),
|
|
75
|
+
command: () => toggleMark(key),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const heading = (level: number): Item => ({
|
|
79
|
+
key: `h${level}`,
|
|
80
|
+
label: `Heading ${level}`,
|
|
81
|
+
text: `H${level}`,
|
|
82
|
+
pressed: (state) => blockIs(state, "heading", level),
|
|
83
|
+
// Pressing an active heading turns it back into a paragraph.
|
|
84
|
+
command: (state) =>
|
|
85
|
+
blockIs(state, "heading", level)
|
|
86
|
+
? setBlockType("paragraph")
|
|
87
|
+
: setBlockType("heading", { level }),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const GROUPS: Item[][] = [
|
|
91
|
+
[
|
|
92
|
+
mark("bold", "Bold", "B"),
|
|
93
|
+
mark("italic", "Italic", "I"),
|
|
94
|
+
mark("underline", "Underline", "U"),
|
|
95
|
+
mark("strike", "Strikethrough", "S"),
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
{
|
|
99
|
+
key: "paragraph",
|
|
100
|
+
label: "Paragraph",
|
|
101
|
+
text: "¶",
|
|
102
|
+
symbol: true,
|
|
103
|
+
pressed: (state) => blockIs(state, "paragraph"),
|
|
104
|
+
command: () => setBlockType("paragraph"),
|
|
105
|
+
},
|
|
106
|
+
heading(1),
|
|
107
|
+
heading(2),
|
|
108
|
+
heading(3),
|
|
109
|
+
],
|
|
110
|
+
[
|
|
111
|
+
{
|
|
112
|
+
key: "undo",
|
|
113
|
+
label: "Undo",
|
|
114
|
+
text: "Undo",
|
|
115
|
+
enabled: (state) => undoDepth(state) > 0,
|
|
116
|
+
command: () => undo,
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
key: "redo",
|
|
120
|
+
label: "Redo",
|
|
121
|
+
text: "Redo",
|
|
122
|
+
enabled: (state) => redoDepth(state) > 0,
|
|
123
|
+
command: () => redo,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
[{ key: "pageBreak", label: "Page break", text: "Page break", command: () => insertPageBreak }],
|
|
127
|
+
];
|
|
128
|
+
const ITEMS = GROUPS.flat();
|
|
129
|
+
|
|
130
|
+
interface ItemState {
|
|
131
|
+
pressed: boolean | undefined;
|
|
132
|
+
enabled: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const sameStates = (a: ItemState[], b: ItemState[]) =>
|
|
136
|
+
a.length === b.length &&
|
|
137
|
+
a.every((item, i) => item.pressed === b[i]?.pressed && item.enabled === b[i]?.enabled);
|
|
138
|
+
|
|
139
|
+
/** Runs after every transaction: O(selection), no transactions built. */
|
|
140
|
+
const selectStates = (state: EditorState, editor: Editor): ItemState[] =>
|
|
141
|
+
ITEMS.map((item) => ({
|
|
142
|
+
pressed: item.pressed?.(state, editor),
|
|
143
|
+
enabled: item.enabled?.(state) ?? true,
|
|
144
|
+
}));
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Formatting buttons for an editor: marks, paragraph and heading levels, undo, redo, page
|
|
148
|
+
* break. One tab stop (arrow keys, Home and End move between buttons); toggles carry
|
|
149
|
+
* `aria-pressed`; a button whose command cannot run now is `aria-disabled` (still focusable).
|
|
150
|
+
* Clicks leave the focus and selection in the document.
|
|
151
|
+
*/
|
|
152
|
+
export function FoliToolbar({
|
|
153
|
+
editor: given,
|
|
154
|
+
disabled = false,
|
|
155
|
+
onClickCommand,
|
|
156
|
+
label = "Formatting",
|
|
157
|
+
className,
|
|
158
|
+
}: FoliToolbarProps) {
|
|
159
|
+
const editor = useEditorOrContext(given, "FoliToolbar");
|
|
160
|
+
const states = useEditorState(selectStates, { editor, isEqual: sameStates });
|
|
161
|
+
const [active, setActive] = useState(0);
|
|
162
|
+
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
|
|
163
|
+
|
|
164
|
+
const run = (index: number, event: MouseEvent) => {
|
|
165
|
+
const item = ITEMS[index];
|
|
166
|
+
if (!item || disabled || !states[index]?.enabled) return;
|
|
167
|
+
editor.run(item.command(editor.state));
|
|
168
|
+
// `detail` counts pointer clicks; Enter and Space on a button give 0.
|
|
169
|
+
if (event.detail > 0) onClickCommand?.();
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
173
|
+
// Alt+Left (back) and other shortcuts are the browser's.
|
|
174
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
175
|
+
const count = ITEMS.length;
|
|
176
|
+
const moves: Record<string, number> = {
|
|
177
|
+
ArrowRight: (active + 1) % count,
|
|
178
|
+
ArrowLeft: (active - 1 + count) % count,
|
|
179
|
+
Home: 0,
|
|
180
|
+
End: count - 1,
|
|
181
|
+
};
|
|
182
|
+
const next = moves[event.key];
|
|
183
|
+
if (next === undefined) return;
|
|
184
|
+
event.preventDefault();
|
|
185
|
+
setActive(next);
|
|
186
|
+
buttons.current[next]?.focus();
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
let index = 0;
|
|
190
|
+
return (
|
|
191
|
+
<div
|
|
192
|
+
role="toolbar"
|
|
193
|
+
aria-label={label}
|
|
194
|
+
className={className ? `foli-toolbar ${className}` : "foli-toolbar"}
|
|
195
|
+
onKeyDown={onKeyDown}
|
|
196
|
+
>
|
|
197
|
+
{GROUPS.map((group, g) => (
|
|
198
|
+
<Fragment key={group[0]?.key}>
|
|
199
|
+
{g > 0 && <hr className="foli-toolbar-separator" aria-orientation="vertical" />}
|
|
200
|
+
{group.map((item) => {
|
|
201
|
+
const i = index++;
|
|
202
|
+
const state = states[i];
|
|
203
|
+
const off = disabled || !state?.enabled;
|
|
204
|
+
return (
|
|
205
|
+
<button
|
|
206
|
+
key={item.key}
|
|
207
|
+
ref={(element) => {
|
|
208
|
+
buttons.current[i] = element;
|
|
209
|
+
}}
|
|
210
|
+
type="button"
|
|
211
|
+
className={`foli-toolbar-button foli-toolbar-${item.key}`}
|
|
212
|
+
// Speech input users say what they see: "H1", "Undo"; symbols get a name.
|
|
213
|
+
aria-label={item.symbol ? item.label : undefined}
|
|
214
|
+
title={item.label}
|
|
215
|
+
aria-pressed={item.pressed ? Boolean(state?.pressed) : undefined}
|
|
216
|
+
aria-disabled={off || undefined}
|
|
217
|
+
tabIndex={i === active ? 0 : -1}
|
|
218
|
+
// Keep the focus (and the selection) in the document on click.
|
|
219
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
220
|
+
onFocus={() => setActive(i)}
|
|
221
|
+
onClick={(event) => run(i, event)}
|
|
222
|
+
>
|
|
223
|
+
{item.text}
|
|
224
|
+
</button>
|
|
225
|
+
);
|
|
226
|
+
})}
|
|
227
|
+
</Fragment>
|
|
228
|
+
))}
|
|
229
|
+
</div>
|
|
230
|
+
);
|
|
231
|
+
}
|
package/src/editor.tsx
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createEditor,
|
|
3
|
+
type Editor,
|
|
4
|
+
type EditorState,
|
|
5
|
+
type FoliDocument,
|
|
6
|
+
type Plugin,
|
|
7
|
+
} from "@folijs/core";
|
|
8
|
+
import {
|
|
9
|
+
createContext,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
useCallback,
|
|
12
|
+
useContext,
|
|
13
|
+
useEffect,
|
|
14
|
+
useLayoutEffect,
|
|
15
|
+
useRef,
|
|
16
|
+
useState,
|
|
17
|
+
useSyncExternalStore,
|
|
18
|
+
} from "react";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `useLayoutEffect` in the browser, `useEffect` on the server, where neither runs: React 18
|
|
22
|
+
* warns about a layout effect in server rendering.
|
|
23
|
+
*/
|
|
24
|
+
export const useIsomorphicLayoutEffect =
|
|
25
|
+
typeof document === "undefined" ? useEffect : useLayoutEffect;
|
|
26
|
+
|
|
27
|
+
export interface UseFoliEditorOptions {
|
|
28
|
+
/** Read once, when the editor is created. To load another document, `editor.setDocument()`. */
|
|
29
|
+
initialContent?: FoliDocument | undefined;
|
|
30
|
+
/** Read once, when the editor is created. */
|
|
31
|
+
plugins?: readonly Plugin[] | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A headless `Editor` (state, commands, history) owned by the calling component: created on the
|
|
36
|
+
* first render and kept for its lifetime. Later changes to `options` are ignored.
|
|
37
|
+
*/
|
|
38
|
+
export function useFoliEditor(options: UseFoliEditorOptions = {}): Editor {
|
|
39
|
+
const [editor] = useState(() => {
|
|
40
|
+
const { initialContent: document, plugins } = options;
|
|
41
|
+
return createEditor({ ...(document && { document }), ...(plugins && { plugins }) });
|
|
42
|
+
});
|
|
43
|
+
return editor;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const EditorContext = createContext<Editor | null>(null);
|
|
47
|
+
|
|
48
|
+
/** Makes `editor` the one `useCurrentEditor`, `useEditorState` and `FoliCanvas` use below it. */
|
|
49
|
+
export function FoliEditorProvider({
|
|
50
|
+
editor,
|
|
51
|
+
children,
|
|
52
|
+
}: {
|
|
53
|
+
editor: Editor;
|
|
54
|
+
children?: ReactNode | undefined;
|
|
55
|
+
}) {
|
|
56
|
+
return <EditorContext.Provider value={editor}>{children}</EditorContext.Provider>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The editor of the nearest `FoliEditorProvider` (or `FoliEditor`). Throws outside one. */
|
|
60
|
+
export function useCurrentEditor(): Editor {
|
|
61
|
+
const editor = useContext(EditorContext);
|
|
62
|
+
if (!editor) throw new Error("useCurrentEditor: no FoliEditorProvider or FoliEditor above");
|
|
63
|
+
return editor;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** `editor` if given, else the context's. */
|
|
67
|
+
export function useEditorOrContext(editor: Editor | undefined, caller: string): Editor {
|
|
68
|
+
const context = useContext(EditorContext);
|
|
69
|
+
const found = editor ?? context;
|
|
70
|
+
if (!found) throw new Error(`${caller}: pass an editor or render inside a FoliEditorProvider`);
|
|
71
|
+
return found;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface UseEditorStateOptions<T> {
|
|
75
|
+
/** Default: the context's editor. */
|
|
76
|
+
editor?: Editor | undefined;
|
|
77
|
+
/** When the selected value equals the previous one, the component does not re-render (default `Object.is`). */
|
|
78
|
+
isEqual?: ((a: T, b: T) => boolean) | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A slice of the editor state that re-renders the component only when it changes, e.g.
|
|
83
|
+
* `useEditorState((state, editor) => editor.isMarkActive("bold"))`. The selector runs after
|
|
84
|
+
* every transaction: keep it cheap.
|
|
85
|
+
*/
|
|
86
|
+
export function useEditorState<T>(
|
|
87
|
+
selector: (state: EditorState, editor: Editor) => T,
|
|
88
|
+
{ editor: given, isEqual = Object.is }: UseEditorStateOptions<T> = {},
|
|
89
|
+
): T {
|
|
90
|
+
const editor = useEditorOrContext(given, "useEditorState");
|
|
91
|
+
// The last selected value, kept across renders so an equal one keeps its identity (an inline
|
|
92
|
+
// selector is a new function every render: it runs again, the result stays the same object).
|
|
93
|
+
const last = useRef<{ state: EditorState; selector: unknown; value: T } | null>(null);
|
|
94
|
+
const subscribe = useCallback((notify: () => void) => editor.subscribe(notify), [editor]);
|
|
95
|
+
const getSnapshot = () => {
|
|
96
|
+
const state = editor.state;
|
|
97
|
+
const previous = last.current;
|
|
98
|
+
if (previous?.state === state && previous.selector === selector) return previous.value;
|
|
99
|
+
const value = selector(state, editor);
|
|
100
|
+
const kept = previous && isEqual(previous.value, value) ? previous.value : value;
|
|
101
|
+
last.current = { state, selector, value: kept };
|
|
102
|
+
return kept;
|
|
103
|
+
};
|
|
104
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
105
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
FoliEditorProvider,
|
|
3
|
+
type UseEditorStateOptions,
|
|
4
|
+
type UseFoliEditorOptions,
|
|
5
|
+
useCurrentEditor,
|
|
6
|
+
useEditorState,
|
|
7
|
+
useFoliEditor,
|
|
8
|
+
} from "./editor.js";
|
|
9
|
+
export { FoliCanvas, type FoliCanvasHandle, type FoliCanvasProps } from "./FoliCanvas.js";
|
|
10
|
+
export {
|
|
11
|
+
type FoliChange,
|
|
12
|
+
FoliEditor,
|
|
13
|
+
type FoliEditorHandle,
|
|
14
|
+
type FoliEditorProps,
|
|
15
|
+
} from "./FoliEditor.js";
|
|
16
|
+
export { FoliToolbar, type FoliToolbarProps } from "./FoliToolbar.js";
|
package/styles.css
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Foli default theme. The tokens' defaults live on the root (and a shadow host), so a value set
|
|
3
|
+
* on any ancestor of the editor, or on .foli-canvas / .foli-toolbar themselves, overrides them.
|
|
4
|
+
*/
|
|
5
|
+
:root,
|
|
6
|
+
:host {
|
|
7
|
+
--foli-canvas-background: #f3f3f1;
|
|
8
|
+
--foli-page-background: #ffffff;
|
|
9
|
+
--foli-page-shadow: 0 1px 2px rgb(0 0 0 / 0.08), 0 2px 8px rgb(0 0 0 / 0.06);
|
|
10
|
+
--foli-border-color: #e2e2df;
|
|
11
|
+
--foli-text-color: #1d1d1b;
|
|
12
|
+
--foli-selection-color: rgb(35 103 220 / 0.22);
|
|
13
|
+
--foli-font-family: Georgia, "Times New Roman", serif;
|
|
14
|
+
--foli-font-size: 11pt;
|
|
15
|
+
--foli-line-height: 1.5;
|
|
16
|
+
--foli-page-gap: 24px;
|
|
17
|
+
--foli-toolbar-background: #ffffff;
|
|
18
|
+
--foli-toolbar-color: #1d1d1b;
|
|
19
|
+
--foli-toolbar-pressed: #dfe8f8;
|
|
20
|
+
--foli-toolbar-hover: #f0f0ee;
|
|
21
|
+
--foli-focus-ring: #2367dc;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.foli-canvas {
|
|
25
|
+
padding: 32px 16px;
|
|
26
|
+
background: var(--foli-canvas-background);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* Pages are rendered by the view (size and margins inline, from the document settings). */
|
|
30
|
+
.foli-page {
|
|
31
|
+
margin: 0 auto;
|
|
32
|
+
background: var(--foli-page-background);
|
|
33
|
+
box-shadow: var(--foli-page-shadow);
|
|
34
|
+
color: var(--foli-text-color);
|
|
35
|
+
font-family: var(--foli-font-family);
|
|
36
|
+
font-size: var(--foli-font-size);
|
|
37
|
+
line-height: var(--foli-line-height);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.foli-page + .foli-page {
|
|
41
|
+
margin-top: var(--foli-page-gap);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.foli-content {
|
|
45
|
+
outline: none;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.foli-content ::selection {
|
|
49
|
+
background: var(--foli-selection-color);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* Toolbar (FoliToolbar). */
|
|
53
|
+
.foli-toolbar {
|
|
54
|
+
display: flex;
|
|
55
|
+
flex-wrap: wrap;
|
|
56
|
+
align-items: center;
|
|
57
|
+
gap: 2px;
|
|
58
|
+
padding: 4px 8px;
|
|
59
|
+
border-bottom: 1px solid var(--foli-border-color);
|
|
60
|
+
background: var(--foli-toolbar-background);
|
|
61
|
+
color: var(--foli-toolbar-color);
|
|
62
|
+
font:
|
|
63
|
+
14px / 1 system-ui,
|
|
64
|
+
sans-serif;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.foli-toolbar-button {
|
|
68
|
+
min-width: 32px;
|
|
69
|
+
height: 32px;
|
|
70
|
+
padding: 0 8px;
|
|
71
|
+
border: 1px solid transparent;
|
|
72
|
+
border-radius: 4px;
|
|
73
|
+
background: none;
|
|
74
|
+
color: inherit;
|
|
75
|
+
font: inherit;
|
|
76
|
+
cursor: pointer;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.foli-toolbar-button:not([aria-disabled="true"]):hover {
|
|
80
|
+
background: var(--foli-toolbar-hover);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.foli-toolbar-button[aria-pressed="true"] {
|
|
84
|
+
border-color: var(--foli-focus-ring);
|
|
85
|
+
background: var(--foli-toolbar-pressed);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.foli-toolbar-button[aria-disabled="true"] {
|
|
89
|
+
opacity: 0.45;
|
|
90
|
+
cursor: default;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.foli-toolbar-button:focus-visible {
|
|
94
|
+
outline: 2px solid var(--foli-focus-ring);
|
|
95
|
+
outline-offset: 1px;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.foli-toolbar-bold {
|
|
99
|
+
font-weight: 700;
|
|
100
|
+
}
|
|
101
|
+
.foli-toolbar-italic {
|
|
102
|
+
font-style: italic;
|
|
103
|
+
}
|
|
104
|
+
.foli-toolbar-underline {
|
|
105
|
+
text-decoration: underline;
|
|
106
|
+
}
|
|
107
|
+
.foli-toolbar-strike {
|
|
108
|
+
text-decoration: line-through;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* Forced colors (Windows High Contrast) drop backgrounds: mark pressed buttons another way. */
|
|
112
|
+
@media (forced-colors: active) {
|
|
113
|
+
.foli-toolbar-button[aria-pressed="true"] {
|
|
114
|
+
outline: 2px solid Highlight;
|
|
115
|
+
outline-offset: -2px;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
.foli-toolbar-separator {
|
|
120
|
+
align-self: stretch;
|
|
121
|
+
width: 1px;
|
|
122
|
+
margin: 4px 4px;
|
|
123
|
+
border: none;
|
|
124
|
+
background: var(--foli-border-color);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/*
|
|
128
|
+
* The view prints one sheet per page (size from the document settings); this drops the chrome.
|
|
129
|
+
* Hide the rest of your page in print: anything printed before the pages adds sheets.
|
|
130
|
+
*/
|
|
131
|
+
@media print {
|
|
132
|
+
.foli-canvas {
|
|
133
|
+
padding: 0;
|
|
134
|
+
background: none;
|
|
135
|
+
}
|
|
136
|
+
.foli-page {
|
|
137
|
+
box-shadow: none;
|
|
138
|
+
}
|
|
139
|
+
.foli-toolbar {
|
|
140
|
+
display: none;
|
|
141
|
+
}
|
|
142
|
+
}
|