@wingleeio/ori-react 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wing
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @wingleeio/ori-react
2
+
3
+ > **Alpha (`0.0.x`).** Experimental — APIs may change between releases and it is not yet production-ready.
4
+
5
+ React bindings for the Ori virtualized note editor.
6
+
7
+ ```tsx
8
+ import { useEditor, NoteEditor } from "@wingleeio/ori-react";
9
+ import "@wingleeio/ori-react/styles.css";
10
+
11
+ function Editor({ doc }) {
12
+ const editor = useEditor({ doc }); // owns an EditorController for this mount
13
+ return <NoteEditor editor={editor} autoFocus placeholder="Start writing…" maxWidth={720} />;
14
+ }
15
+ ```
16
+
17
+ `<NoteEditor>` renders only the blocks intersecting the viewport, draws the caret
18
+ and selection from logical state, and routes keyboard / IME / mouse input to the
19
+ controller. Pair with `useEditorSnapshot` / `useActiveMarks` to build toolbars.
20
+
21
+ To switch notes, give the host component a `key` (e.g. the note id) so it
22
+ remounts with a fresh controller for the new `Y.Doc`.
23
+
24
+ `react`, `react-dom` and `yjs` are peer dependencies.
package/dist/index.cjs ADDED
@@ -0,0 +1,553 @@
1
+ 'use strict';
2
+
3
+ var oriCore = require('@wingleeio/ori-core');
4
+ var react = require('react');
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+
7
+ // src/useEditor.ts
8
+ function useEditor(options = {}) {
9
+ const ref = react.useRef(null);
10
+ if (ref.current === null) {
11
+ ref.current = new oriCore.EditorController({
12
+ doc: options.doc,
13
+ measurer: options.measurer ?? oriCore.createCanvasMeasurer(),
14
+ typography: options.typography,
15
+ overscan: options.overscan,
16
+ blockSpacing: options.blockSpacing,
17
+ schema: options.schema
18
+ });
19
+ }
20
+ react.useEffect(() => {
21
+ const editor = ref.current;
22
+ return () => editor?.destroy();
23
+ }, []);
24
+ return ref.current;
25
+ }
26
+ function useEditorSnapshot(editor) {
27
+ return react.useSyncExternalStore(editor.subscribe, editor.getSnapshot, editor.getSnapshot);
28
+ }
29
+ function useActiveMarks(editor) {
30
+ const snapshot = useEditorSnapshot(editor);
31
+ void snapshot.revision;
32
+ return editor.getActiveMarks();
33
+ }
34
+ var EMPTY = { blocks: {}, atoms: {} };
35
+ var RenderersContext = react.createContext(EMPTY);
36
+ var RenderersProvider = RenderersContext.Provider;
37
+ var useRenderers = () => react.useContext(RenderersContext);
38
+ function fragmentStyle(frag) {
39
+ const f = frag.font;
40
+ const decoration = [];
41
+ if (frag.marks.underline) decoration.push("underline");
42
+ if (frag.marks.strike) decoration.push("line-through");
43
+ return {
44
+ fontFamily: f.fontFamily,
45
+ fontSize: f.fontSize,
46
+ fontWeight: f.fontWeight,
47
+ fontStyle: f.italic ? "italic" : "normal",
48
+ letterSpacing: f.letterSpacing || void 0,
49
+ textDecorationLine: decoration.length ? decoration.join(" ") : void 0
50
+ };
51
+ }
52
+ function AtomFragment({
53
+ editor,
54
+ atom,
55
+ render
56
+ }) {
57
+ return /* @__PURE__ */ jsxRuntime.jsx(
58
+ "span",
59
+ {
60
+ className: "ori-atom",
61
+ style: {
62
+ display: "inline-block",
63
+ width: atom.width,
64
+ verticalAlign: "middle",
65
+ whiteSpace: "normal"
66
+ },
67
+ children: render ? render({ editor, atom }) : null
68
+ }
69
+ );
70
+ }
71
+ function LineView({
72
+ line,
73
+ editor,
74
+ atoms
75
+ }) {
76
+ return /* @__PURE__ */ jsxRuntime.jsx(
77
+ "div",
78
+ {
79
+ className: "ori-line",
80
+ style: { height: line.height, lineHeight: `${line.height}px`, whiteSpace: "pre" },
81
+ children: line.fragments.map((frag) => {
82
+ if (frag.atom) {
83
+ return /* @__PURE__ */ jsxRuntime.jsx(
84
+ AtomFragment,
85
+ {
86
+ editor,
87
+ atom: frag.atom,
88
+ render: atoms[frag.atom.type]
89
+ },
90
+ frag.start
91
+ );
92
+ }
93
+ const className = ["ori-frag"];
94
+ if (frag.marks.code) className.push("ori-frag-code");
95
+ if (frag.marks.link) className.push("ori-frag-link");
96
+ return /* @__PURE__ */ jsxRuntime.jsx(
97
+ "span",
98
+ {
99
+ className: className.join(" "),
100
+ style: fragmentStyle(frag),
101
+ "data-start": frag.start,
102
+ children: frag.text
103
+ },
104
+ frag.start
105
+ );
106
+ })
107
+ }
108
+ );
109
+ }
110
+ function BlockView({ editor, block }) {
111
+ const { blocks, atoms } = useRenderers();
112
+ const layout = editor.getLayout(block.id);
113
+ if (!layout) return null;
114
+ const custom = blocks[block.type];
115
+ return /* @__PURE__ */ jsxRuntime.jsx(
116
+ "div",
117
+ {
118
+ className: `ori-block ori-block-${block.type}`,
119
+ "data-block-id": block.id,
120
+ style: {
121
+ position: "absolute",
122
+ top: block.top,
123
+ left: 0,
124
+ width: "100%",
125
+ height: block.height
126
+ },
127
+ children: custom ? custom({ editor, block, layout }) : layout.lines.map((line) => /* @__PURE__ */ jsxRuntime.jsx(LineView, { line, editor, atoms }, line.index))
128
+ }
129
+ );
130
+ }
131
+ function SelectionLayer({
132
+ editor,
133
+ snapshot
134
+ }) {
135
+ void snapshot.revision;
136
+ const rects = editor.selectionRectsForViewport();
137
+ if (rects.length === 0) return null;
138
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "ori-selection-layer", "aria-hidden": true, children: rects.map((r, i) => /* @__PURE__ */ jsxRuntime.jsx(
139
+ "div",
140
+ {
141
+ className: "ori-selection-rect",
142
+ style: { position: "absolute", left: r.x, top: r.y, width: r.width, height: r.height }
143
+ },
144
+ `${r.blockId}:${i}`
145
+ )) });
146
+ }
147
+ function CaretLayer({
148
+ editor,
149
+ snapshot,
150
+ focused
151
+ }) {
152
+ const sel = snapshot.selection;
153
+ if (!focused || !sel || !oriCore.isCollapsed(sel)) return null;
154
+ const rect = editor.caretRect();
155
+ if (!rect) return null;
156
+ return /* @__PURE__ */ jsxRuntime.jsx(
157
+ "div",
158
+ {
159
+ className: "ori-caret",
160
+ style: { position: "absolute", left: rect.x, top: rect.y, height: rect.height },
161
+ "aria-hidden": true
162
+ }
163
+ );
164
+ }
165
+ function useCallbackRef(fn) {
166
+ const ref = react.useRef(fn);
167
+ ref.current = fn;
168
+ return react.useMemo(() => (...args) => ref.current(...args), []);
169
+ }
170
+
171
+ // src/keymap.ts
172
+ function pasteText(editor, text) {
173
+ const parts = text.replace(/\r\n?/g, "\n").split("\n");
174
+ editor.insertText(parts[0]);
175
+ for (let i = 1; i < parts.length; i += 1) {
176
+ editor.insertParagraphBreak();
177
+ if (parts[i]) editor.insertText(parts[i]);
178
+ }
179
+ }
180
+ function handleKeyDown(editor, e, opts = {}) {
181
+ if (e.nativeEvent.isComposing) return false;
182
+ if (e.altKey) return false;
183
+ const mod = e.metaKey || e.ctrlKey;
184
+ const shift = e.shiftKey;
185
+ const ro = !!opts.readOnly;
186
+ if (mod) {
187
+ switch (e.key.toLowerCase()) {
188
+ case "b":
189
+ e.preventDefault();
190
+ if (!ro) editor.toggleMark("bold");
191
+ return true;
192
+ case "i":
193
+ e.preventDefault();
194
+ if (!ro) editor.toggleMark("italic");
195
+ return true;
196
+ case "u":
197
+ e.preventDefault();
198
+ if (!ro) editor.toggleMark("underline");
199
+ return true;
200
+ case "e":
201
+ e.preventDefault();
202
+ if (!ro) editor.toggleMark("code");
203
+ return true;
204
+ case "a":
205
+ e.preventDefault();
206
+ editor.selectAll();
207
+ return true;
208
+ case "z":
209
+ e.preventDefault();
210
+ if (!ro) shift ? editor.redo() : editor.undo();
211
+ return true;
212
+ case "y":
213
+ e.preventDefault();
214
+ if (!ro) editor.redo();
215
+ return true;
216
+ case "arrowleft":
217
+ e.preventDefault();
218
+ editor.moveCaret("lineStart", shift);
219
+ return true;
220
+ case "arrowright":
221
+ e.preventDefault();
222
+ editor.moveCaret("lineEnd", shift);
223
+ return true;
224
+ // Let the browser raise copy/cut/paste on the textarea.
225
+ case "c":
226
+ case "x":
227
+ case "v":
228
+ return false;
229
+ default:
230
+ return false;
231
+ }
232
+ }
233
+ switch (e.key) {
234
+ case "Backspace":
235
+ e.preventDefault();
236
+ if (!ro) editor.deleteBackward();
237
+ return true;
238
+ case "Delete":
239
+ e.preventDefault();
240
+ if (!ro) editor.deleteForward();
241
+ return true;
242
+ case "Enter":
243
+ e.preventDefault();
244
+ if (!ro) editor.insertParagraphBreak();
245
+ return true;
246
+ case "Tab":
247
+ e.preventDefault();
248
+ if (!ro) editor.insertText(" ");
249
+ return true;
250
+ case "ArrowLeft":
251
+ e.preventDefault();
252
+ editor.moveCaret("left", shift);
253
+ return true;
254
+ case "ArrowRight":
255
+ e.preventDefault();
256
+ editor.moveCaret("right", shift);
257
+ return true;
258
+ case "ArrowUp":
259
+ e.preventDefault();
260
+ editor.moveCaret("up", shift);
261
+ return true;
262
+ case "ArrowDown":
263
+ e.preventDefault();
264
+ editor.moveCaret("down", shift);
265
+ return true;
266
+ case "Home":
267
+ e.preventDefault();
268
+ editor.moveCaret("lineStart", shift);
269
+ return true;
270
+ case "End":
271
+ e.preventDefault();
272
+ editor.moveCaret("lineEnd", shift);
273
+ return true;
274
+ default:
275
+ return false;
276
+ }
277
+ }
278
+ var NoteEditor = react.forwardRef(function NoteEditor2({
279
+ editor,
280
+ className,
281
+ style,
282
+ maxWidth = 720,
283
+ placeholder = "Start writing\u2026",
284
+ autoFocus,
285
+ readOnly,
286
+ blockRenderers,
287
+ atomRenderers
288
+ }, ref) {
289
+ const snapshot = useEditorSnapshot(editor);
290
+ const renderers = react.useMemo(
291
+ () => ({ blocks: blockRenderers ?? {}, atoms: atomRenderers ?? {} }),
292
+ [blockRenderers, atomRenderers]
293
+ );
294
+ const scrollerRef = react.useRef(null);
295
+ const contentRef = react.useRef(null);
296
+ const inputRef = react.useRef(null);
297
+ const draggingRef = react.useRef(false);
298
+ const composingRef = react.useRef(false);
299
+ const [focused, setFocused] = react.useState(false);
300
+ react.useImperativeHandle(
301
+ ref,
302
+ () => ({
303
+ focus: () => inputRef.current?.focus(),
304
+ getCaretRect: () => {
305
+ const c = editor.caretRect();
306
+ const content = contentRef.current;
307
+ if (!c || !content) return null;
308
+ const r = content.getBoundingClientRect();
309
+ return { x: r.left + c.x, y: r.top + c.y, height: c.height };
310
+ },
311
+ getSelectionRect: () => {
312
+ const content = contentRef.current;
313
+ if (!content) return null;
314
+ const rects = editor.selectionRectsForViewport();
315
+ if (rects.length === 0) return null;
316
+ const r = content.getBoundingClientRect();
317
+ let top = Infinity;
318
+ let left = Infinity;
319
+ let bottom = -Infinity;
320
+ let right = -Infinity;
321
+ for (const rc of rects) {
322
+ top = Math.min(top, r.top + rc.y);
323
+ left = Math.min(left, r.left + rc.x);
324
+ bottom = Math.max(bottom, r.top + rc.y + rc.height);
325
+ right = Math.max(right, r.left + rc.x + rc.width);
326
+ }
327
+ return { top, left, right, bottom, width: right - left, height: bottom - top };
328
+ },
329
+ getScrollElement: () => scrollerRef.current
330
+ }),
331
+ [editor]
332
+ );
333
+ react.useLayoutEffect(() => {
334
+ const scroller = scrollerRef.current;
335
+ const content = contentRef.current;
336
+ if (!scroller || !content) return;
337
+ const sync = () => {
338
+ editor.setWidth(content.clientWidth);
339
+ editor.setViewport(scroller.scrollTop, scroller.clientHeight);
340
+ };
341
+ sync();
342
+ const ro = new ResizeObserver(sync);
343
+ ro.observe(scroller);
344
+ ro.observe(content);
345
+ return () => ro.disconnect();
346
+ }, [editor]);
347
+ react.useEffect(() => {
348
+ const fonts = document.fonts;
349
+ if (fonts?.ready) void fonts.ready.then(() => editor.invalidateMeasurements());
350
+ }, [editor]);
351
+ react.useEffect(() => {
352
+ if (autoFocus) inputRef.current?.focus();
353
+ }, [autoFocus]);
354
+ const pointToPosition = useCallbackRef((clientX, clientY) => {
355
+ const content = contentRef.current;
356
+ if (!content) return null;
357
+ const rect = content.getBoundingClientRect();
358
+ return editor.positionFromPoint(clientX - rect.left, clientY - rect.top);
359
+ });
360
+ react.useEffect(() => {
361
+ const onMove = (e) => {
362
+ if (!draggingRef.current) return;
363
+ const pos = pointToPosition(e.clientX, e.clientY);
364
+ const sel = editor.getSelection();
365
+ if (pos && sel) editor.setSelection({ anchor: sel.anchor, focus: pos });
366
+ };
367
+ const onUp = () => {
368
+ draggingRef.current = false;
369
+ };
370
+ window.addEventListener("mousemove", onMove);
371
+ window.addEventListener("mouseup", onUp);
372
+ return () => {
373
+ window.removeEventListener("mousemove", onMove);
374
+ window.removeEventListener("mouseup", onUp);
375
+ };
376
+ }, [editor, pointToPosition]);
377
+ const onScroll = () => {
378
+ const scroller = scrollerRef.current;
379
+ if (scroller) editor.setViewport(scroller.scrollTop, scroller.clientHeight);
380
+ };
381
+ const onMouseDown = (e) => {
382
+ if (e.button !== 0) return;
383
+ const scroller = scrollerRef.current;
384
+ if (scroller && e.clientX - scroller.getBoundingClientRect().left >= scroller.clientWidth) {
385
+ return;
386
+ }
387
+ inputRef.current?.focus();
388
+ const pos = pointToPosition(e.clientX, e.clientY);
389
+ if (!pos) return;
390
+ e.preventDefault();
391
+ const sel = editor.getSelection();
392
+ if (e.shiftKey && sel) {
393
+ editor.setSelection({ anchor: sel.anchor, focus: pos });
394
+ } else {
395
+ editor.collapse(pos);
396
+ }
397
+ draggingRef.current = true;
398
+ };
399
+ const onKeyDown = (e) => {
400
+ handleKeyDown(editor, e, { readOnly });
401
+ };
402
+ const commitInput = () => {
403
+ const el = inputRef.current;
404
+ if (!el) return;
405
+ const value = el.value;
406
+ if (value) {
407
+ if (!readOnly) editor.insertText(value);
408
+ el.value = "";
409
+ }
410
+ };
411
+ const onInput = (e) => {
412
+ if (composingRef.current || e.nativeEvent.isComposing) return;
413
+ commitInput();
414
+ };
415
+ const onCompositionStart = () => {
416
+ composingRef.current = true;
417
+ };
418
+ const onCompositionEnd = (e) => {
419
+ composingRef.current = false;
420
+ const el = inputRef.current;
421
+ if (el) {
422
+ if (e.data && !readOnly) editor.insertText(e.data);
423
+ el.value = "";
424
+ }
425
+ };
426
+ const caret = editor.caretRect();
427
+ const inputStyle = {
428
+ position: "absolute",
429
+ left: caret ? caret.x : 0,
430
+ top: caret ? caret.y : 0,
431
+ width: 1,
432
+ height: caret ? caret.height : 16,
433
+ opacity: 0,
434
+ padding: 0,
435
+ border: 0,
436
+ outline: "none",
437
+ resize: "none",
438
+ background: "transparent",
439
+ caretColor: "transparent",
440
+ color: "transparent",
441
+ overflow: "hidden",
442
+ whiteSpace: "pre",
443
+ zIndex: 1
444
+ };
445
+ return /* @__PURE__ */ jsxRuntime.jsx(RenderersProvider, { value: renderers, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: `ori-root${className ? ` ${className}` : ""}`, style, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "ori-scroller", ref: scrollerRef, onScroll, onMouseDown, children: /* @__PURE__ */ jsxRuntime.jsx(
446
+ "div",
447
+ {
448
+ className: "ori-content",
449
+ ref: contentRef,
450
+ style: { maxWidth, marginInline: "auto", position: "relative" },
451
+ children: /* @__PURE__ */ jsxRuntime.jsxs(
452
+ "div",
453
+ {
454
+ className: "ori-canvas",
455
+ style: { position: "relative", width: "100%", height: snapshot.totalHeight },
456
+ children: [
457
+ /* @__PURE__ */ jsxRuntime.jsx(SelectionLayer, { editor, snapshot }),
458
+ snapshot.visible.map((block) => /* @__PURE__ */ jsxRuntime.jsx(BlockView, { editor, block }, block.id)),
459
+ /* @__PURE__ */ jsxRuntime.jsx(CaretLayer, { editor, snapshot, focused }),
460
+ snapshot.empty && placeholder ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "ori-placeholder", "aria-hidden": true, children: placeholder }) : null,
461
+ !readOnly ? /* @__PURE__ */ jsxRuntime.jsx(
462
+ "textarea",
463
+ {
464
+ ref: inputRef,
465
+ className: "ori-input",
466
+ style: inputStyle,
467
+ spellCheck: false,
468
+ autoCapitalize: "off",
469
+ autoCorrect: "off",
470
+ onKeyDown,
471
+ onInput,
472
+ onCompositionStart,
473
+ onCompositionEnd,
474
+ onFocus: () => setFocused(true),
475
+ onBlur: () => setFocused(false),
476
+ onCopy: (e) => {
477
+ e.preventDefault();
478
+ e.clipboardData.setData("text/plain", editor.getSelectedText());
479
+ },
480
+ onCut: (e) => {
481
+ e.preventDefault();
482
+ e.clipboardData.setData("text/plain", editor.getSelectedText());
483
+ editor.deleteBackward();
484
+ },
485
+ onPaste: (e) => {
486
+ e.preventDefault();
487
+ pasteText(editor, e.clipboardData.getData("text/plain"));
488
+ }
489
+ }
490
+ ) : null
491
+ ]
492
+ }
493
+ )
494
+ }
495
+ ) }) }) });
496
+ });
497
+
498
+ Object.defineProperty(exports, "DEFAULT_TYPOGRAPHY", {
499
+ enumerable: true,
500
+ get: function () { return oriCore.DEFAULT_TYPOGRAPHY; }
501
+ });
502
+ Object.defineProperty(exports, "EditorController", {
503
+ enumerable: true,
504
+ get: function () { return oriCore.EditorController; }
505
+ });
506
+ Object.defineProperty(exports, "applyUpdate", {
507
+ enumerable: true,
508
+ get: function () { return oriCore.applyUpdate; }
509
+ });
510
+ Object.defineProperty(exports, "base64ToBytes", {
511
+ enumerable: true,
512
+ get: function () { return oriCore.base64ToBytes; }
513
+ });
514
+ Object.defineProperty(exports, "bytesToBase64", {
515
+ enumerable: true,
516
+ get: function () { return oriCore.bytesToBase64; }
517
+ });
518
+ Object.defineProperty(exports, "createCanvasMeasurer", {
519
+ enumerable: true,
520
+ get: function () { return oriCore.createCanvasMeasurer; }
521
+ });
522
+ Object.defineProperty(exports, "createNoteDoc", {
523
+ enumerable: true,
524
+ get: function () { return oriCore.createNoteDoc; }
525
+ });
526
+ Object.defineProperty(exports, "docFromUpdate", {
527
+ enumerable: true,
528
+ get: function () { return oriCore.docFromUpdate; }
529
+ });
530
+ Object.defineProperty(exports, "encodeDoc", {
531
+ enumerable: true,
532
+ get: function () { return oriCore.encodeDoc; }
533
+ });
534
+ Object.defineProperty(exports, "getBlocks", {
535
+ enumerable: true,
536
+ get: function () { return oriCore.getBlocks; }
537
+ });
538
+ Object.defineProperty(exports, "snapshotBlocks", {
539
+ enumerable: true,
540
+ get: function () { return oriCore.snapshotBlocks; }
541
+ });
542
+ exports.BlockView = BlockView;
543
+ exports.CaretLayer = CaretLayer;
544
+ exports.NoteEditor = NoteEditor;
545
+ exports.SelectionLayer = SelectionLayer;
546
+ exports.handleKeyDown = handleKeyDown;
547
+ exports.pasteText = pasteText;
548
+ exports.useActiveMarks = useActiveMarks;
549
+ exports.useEditor = useEditor;
550
+ exports.useEditorSnapshot = useEditorSnapshot;
551
+ exports.useRenderers = useRenderers;
552
+ //# sourceMappingURL=index.cjs.map
553
+ //# sourceMappingURL=index.cjs.map