@folijs/react 0.0.0-stage → 0.2.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/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,317 @@
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
+ --foli-error-color: #b3261e;
23
+ --foli-link-color: #1f5fbf;
24
+ --foli-marker-color: currentColor;
25
+ --foli-rule-color: #8c8c87;
26
+ --foli-rule-thickness: 1px;
27
+ }
28
+
29
+ .foli-canvas {
30
+ padding: 32px 16px;
31
+ background: var(--foli-canvas-background);
32
+ }
33
+
34
+ /* Pages are rendered by the view (size and margins inline, from the document settings). */
35
+ .foli-page {
36
+ margin: 0 auto;
37
+ background: var(--foli-page-background);
38
+ box-shadow: var(--foli-page-shadow);
39
+ color: var(--foli-text-color);
40
+ font-family: var(--foli-font-family);
41
+ font-size: var(--foli-font-size);
42
+ line-height: var(--foli-line-height);
43
+ }
44
+
45
+ .foli-page + .foli-page {
46
+ margin-top: var(--foli-page-gap);
47
+ }
48
+
49
+ .foli-content {
50
+ outline: none;
51
+ }
52
+
53
+ .foli-content ::selection {
54
+ background: var(--foli-selection-color);
55
+ }
56
+
57
+ /* Links and list markers in the document. */
58
+ .foli-page a {
59
+ color: var(--foli-link-color);
60
+ }
61
+
62
+ .foli-page li::marker {
63
+ color: var(--foli-marker-color);
64
+ }
65
+
66
+ /*
67
+ * A horizontal rule: a line in the middle of a taller box, so it is easy to click (a click
68
+ * selects it). Drawn with a border, which prints (backgrounds may not).
69
+ */
70
+ .foli-page hr {
71
+ position: relative;
72
+ height: 12px;
73
+ margin: 6px 0;
74
+ border: 0;
75
+ overflow: visible;
76
+ }
77
+
78
+ .foli-page hr::after {
79
+ content: "";
80
+ position: absolute;
81
+ inset: 50% 0 auto;
82
+ border-top: var(--foli-rule-thickness) solid var(--foli-rule-color);
83
+ }
84
+
85
+ /* A node-selected block (a horizontal rule): the view adds the class. */
86
+ .foli-content .foli-selected-node {
87
+ outline: 2px solid var(--foli-focus-ring);
88
+ outline-offset: 2px;
89
+ }
90
+
91
+ /* Toolbar (FoliToolbar). */
92
+ .foli-toolbar {
93
+ display: flex;
94
+ flex-wrap: wrap;
95
+ align-items: center;
96
+ gap: 2px;
97
+ padding: 4px 8px;
98
+ border-bottom: 1px solid var(--foli-border-color);
99
+ background: var(--foli-toolbar-background);
100
+ color: var(--foli-toolbar-color);
101
+ font:
102
+ 14px / 1 system-ui,
103
+ sans-serif;
104
+ }
105
+
106
+ .foli-toolbar-button {
107
+ min-width: 32px;
108
+ height: 32px;
109
+ padding: 0 8px;
110
+ border: 1px solid transparent;
111
+ border-radius: 4px;
112
+ background: none;
113
+ color: inherit;
114
+ font: inherit;
115
+ cursor: pointer;
116
+ }
117
+
118
+ .foli-toolbar-button:not([aria-disabled="true"]):hover {
119
+ background: var(--foli-toolbar-hover);
120
+ }
121
+
122
+ .foli-toolbar-button[aria-pressed="true"] {
123
+ border-color: var(--foli-focus-ring);
124
+ background: var(--foli-toolbar-pressed);
125
+ }
126
+
127
+ .foli-toolbar-button[aria-disabled="true"] {
128
+ opacity: 0.45;
129
+ cursor: default;
130
+ }
131
+
132
+ .foli-toolbar-button:focus-visible {
133
+ outline: 2px solid var(--foli-focus-ring);
134
+ outline-offset: 1px;
135
+ }
136
+
137
+ .foli-toolbar-bold {
138
+ font-weight: 700;
139
+ }
140
+ .foli-toolbar-italic {
141
+ font-style: italic;
142
+ }
143
+ .foli-toolbar-underline {
144
+ text-decoration: underline;
145
+ }
146
+ .foli-toolbar-strike {
147
+ text-decoration: line-through;
148
+ }
149
+
150
+ /* Text color and highlight menu buttons: the letter over the current color. */
151
+ .foli-toolbar-menu {
152
+ position: relative;
153
+ display: inline-flex;
154
+ }
155
+
156
+ .foli-toolbar-textColor,
157
+ .foli-toolbar-highlight {
158
+ display: inline-flex;
159
+ flex-direction: column;
160
+ align-items: center;
161
+ justify-content: center;
162
+ gap: 2px;
163
+ }
164
+
165
+ .foli-toolbar-swatch {
166
+ width: 16px;
167
+ height: 4px;
168
+ border-radius: 1px;
169
+ box-shadow: inset 0 0 0 1px var(--foli-border-color);
170
+ }
171
+
172
+ .foli-menu {
173
+ position: absolute;
174
+ top: 100%;
175
+ left: 0;
176
+ z-index: 10;
177
+ display: flex;
178
+ flex-direction: column;
179
+ min-width: 10em;
180
+ margin-top: 2px;
181
+ padding: 4px;
182
+ border: 1px solid var(--foli-border-color);
183
+ border-radius: 4px;
184
+ background: var(--foli-toolbar-background);
185
+ box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
186
+ }
187
+
188
+ .foli-menu-item {
189
+ display: flex;
190
+ align-items: center;
191
+ gap: 8px;
192
+ height: 28px;
193
+ padding: 0 8px;
194
+ border: 0;
195
+ border-radius: 3px;
196
+ background: none;
197
+ color: inherit;
198
+ font: inherit;
199
+ text-align: start;
200
+ cursor: pointer;
201
+ }
202
+
203
+ .foli-menu-item:hover,
204
+ .foli-menu-item:focus-visible {
205
+ background: var(--foli-toolbar-hover);
206
+ }
207
+
208
+ .foli-menu-item:focus-visible {
209
+ outline: 2px solid var(--foli-focus-ring);
210
+ outline-offset: -2px;
211
+ }
212
+
213
+ .foli-menu-swatch {
214
+ width: 16px;
215
+ height: 16px;
216
+ border-radius: 3px;
217
+ box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.2);
218
+ }
219
+
220
+ /* The checked color: a ring around its swatch (an outline also shows in forced colors). */
221
+ .foli-menu-item[aria-checked="true"] .foli-menu-swatch {
222
+ outline: 2px solid var(--foli-focus-ring);
223
+ outline-offset: 1px;
224
+ }
225
+
226
+ /* Link dialog (FoliToolbar): under the toolbar. */
227
+ .foli-link-dialog {
228
+ padding: 8px;
229
+ border-bottom: 1px solid var(--foli-border-color);
230
+ background: var(--foli-toolbar-background);
231
+ color: var(--foli-toolbar-color);
232
+ font:
233
+ 14px / 1.4 system-ui,
234
+ sans-serif;
235
+ }
236
+
237
+ .foli-link-dialog form {
238
+ display: flex;
239
+ flex-wrap: wrap;
240
+ align-items: center;
241
+ gap: 8px;
242
+ }
243
+
244
+ .foli-link-dialog input {
245
+ flex: 1 1 16em;
246
+ min-width: 0;
247
+ height: 32px;
248
+ padding: 0 8px;
249
+ border: 1px solid var(--foli-border-color);
250
+ border-radius: 4px;
251
+ font: inherit;
252
+ }
253
+
254
+ .foli-link-dialog input[aria-invalid="true"] {
255
+ border-color: var(--foli-error-color);
256
+ }
257
+
258
+ .foli-link-dialog button {
259
+ height: 32px;
260
+ padding: 0 12px;
261
+ border: 1px solid var(--foli-border-color);
262
+ border-radius: 4px;
263
+ background: none;
264
+ color: inherit;
265
+ font: inherit;
266
+ cursor: pointer;
267
+ }
268
+
269
+ .foli-link-dialog :focus-visible {
270
+ outline: 2px solid var(--foli-focus-ring);
271
+ outline-offset: 1px;
272
+ }
273
+
274
+ .foli-link-error {
275
+ flex-basis: 100%;
276
+ margin: 0;
277
+ color: var(--foli-error-color);
278
+ }
279
+
280
+ /* Forced colors (Windows High Contrast) drop backgrounds: mark pressed buttons another way. */
281
+ @media (forced-colors: active) {
282
+ .foli-toolbar-button[aria-pressed="true"] {
283
+ outline: 2px solid Highlight;
284
+ outline-offset: -2px;
285
+ }
286
+ /* Swatches show their color (the names say it too). */
287
+ .foli-toolbar-swatch,
288
+ .foli-menu-swatch {
289
+ forced-color-adjust: none;
290
+ }
291
+ }
292
+
293
+ .foli-toolbar-separator {
294
+ align-self: stretch;
295
+ width: 1px;
296
+ margin: 4px 4px;
297
+ border: none;
298
+ background: var(--foli-border-color);
299
+ }
300
+
301
+ /*
302
+ * The view prints one sheet per page (size from the document settings); this drops the chrome.
303
+ * Hide the rest of your page in print: anything printed before the pages adds sheets.
304
+ */
305
+ @media print {
306
+ .foli-canvas {
307
+ padding: 0;
308
+ background: none;
309
+ }
310
+ .foli-page {
311
+ box-shadow: none;
312
+ }
313
+ .foli-toolbar,
314
+ .foli-link-dialog {
315
+ display: none;
316
+ }
317
+ }