@rubyfield/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/LICENSE +21 -0
- package/README.md +217 -2
- package/dist/index.d.ts +10 -0
- package/dist/index.js +55 -0
- package/package.json +42 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 graphemecluster
|
|
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
CHANGED
|
@@ -1,3 +1,218 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Rubyfield
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@rubyfield/react)
|
|
4
|
+
[](https://jsr.io/@rubyfield/react)
|
|
5
|
+
[](https://www.npmjs.com/package/@rubyfield/react?activeTab=code)
|
|
6
|
+
[](https://github.com/graphemecluster/rubyfield/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
Rubyfield turns readings, glosses, and other text annotations into an input field you can see and try immediately. Type and edit the base text while the annotations update live above it.
|
|
9
|
+
|
|
10
|
+
Use it as a Pinyin, Jyutping, or Furigana input field with the [recipes](#recipes) below—or plug in your own annotation function. NLP library authors can use the same component as a playground: change the function, type a sentence, and see its output live.
|
|
11
|
+
|
|
12
|
+
- [Mandarin Pinyin](#mandarin-pinyin) with `pinyin-pro`.
|
|
13
|
+
- [Cantonese Jyutping](#cantonese-jyutping) with `to-jyutping`.
|
|
14
|
+
- [Japanese Furigana](#japanese-furigana) with `kuromoji` and `wanakana`.
|
|
15
|
+
- Custom readings, glosses, and other annotations through one `annotate` function.
|
|
16
|
+
|
|
17
|
+
Try the [live demo](https://graphemecluster.github.io/rubyfield/).
|
|
18
|
+
|
|
19
|
+
| For | Use Package |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| Framework-agnostic JavaScript | [rubyfield][core] |
|
|
22
|
+
| React 18+ | [@rubyfield/react][react] [current] |
|
|
23
|
+
| Vue 3+ | [@rubyfield/vue][vue] |
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
Install `@rubyfield/react` alongside React 18+ and React DOM.
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { useRef, useState } from "react";
|
|
31
|
+
import { Rubyfield, type RubyfieldHandle } from "@rubyfield/react";
|
|
32
|
+
|
|
33
|
+
export function Example() {
|
|
34
|
+
const [content, setContent] = useState("你好");
|
|
35
|
+
const ref = useRef<RubyfieldHandle>(null);
|
|
36
|
+
return <Rubyfield className="your-wrapper-class" ref={ref} content={content} onUpdate={setContent}
|
|
37
|
+
placeholder="Start typing…"
|
|
38
|
+
annotate={text => text === "你好" ? [[0, 1, "nei5"], [1, 2, "hou2"]] : []}
|
|
39
|
+
attributes={{ class: "your-input-field-class" }} />;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Recipes
|
|
44
|
+
|
|
45
|
+
### Mandarin Pinyin
|
|
46
|
+
|
|
47
|
+
Install `pinyin-pro`.
|
|
48
|
+
|
|
49
|
+
```jsx
|
|
50
|
+
import { pinyin } from "pinyin-pro";
|
|
51
|
+
import { Rubyfield, segmentsToRanges } from "@rubyfield/react";
|
|
52
|
+
|
|
53
|
+
const annotate = text => segmentsToRanges(pinyin(text, { type: "all" }).map(({ origin, pinyin, isZh }) => [origin, isZh ? pinyin : null]));
|
|
54
|
+
|
|
55
|
+
<Rubyfield lang="zh" annotate={annotate}
|
|
56
|
+
ruby={{ rt: { attributes: { lang: "zh-Latn-pinyin" } } }} />;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Cantonese Jyutping
|
|
60
|
+
|
|
61
|
+
Install `to-jyutping`.
|
|
62
|
+
|
|
63
|
+
```jsx
|
|
64
|
+
import { getJyutpingList } from "to-jyutping";
|
|
65
|
+
import { Rubyfield, segmentsToRanges } from "@rubyfield/react";
|
|
66
|
+
|
|
67
|
+
const annotate = text => segmentsToRanges(getJyutpingList(text));
|
|
68
|
+
|
|
69
|
+
<Rubyfield lang="yue" annotate={annotate}
|
|
70
|
+
ruby={{ rt: { attributes: { lang: "yue-Latn-jyutping" } } }} />;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Japanese Furigana
|
|
74
|
+
|
|
75
|
+
Install `kuromoji` and `wanakana`.
|
|
76
|
+
|
|
77
|
+
Serve Kuromoji's `dict` directory at `/dict/` as binary files without a `Content-Encoding` header. Load the tokenizer once before mounting the field; this example uses its browser bundle.
|
|
78
|
+
|
|
79
|
+
```jsx
|
|
80
|
+
import kuromoji from "kuromoji/build/kuromoji.js";
|
|
81
|
+
import { toHiragana } from "wanakana";
|
|
82
|
+
import { Rubyfield, segmentsToRanges } from "@rubyfield/react";
|
|
83
|
+
|
|
84
|
+
const tokenizer = await new Promise((resolve, reject) => kuromoji.builder({ dicPath: "/dict/" }).build((error, tokenizer) => error ? reject(error) : resolve(tokenizer)));
|
|
85
|
+
const annotate = text => segmentsToRanges(tokenizer.tokenize(text).map(({ surface_form, reading }) => [surface_form, /\p{Script=Han}/u.test(surface_form) && reading ? toHiragana(reading) : null]));
|
|
86
|
+
|
|
87
|
+
<Rubyfield lang="ja" annotate={annotate}
|
|
88
|
+
ruby={{ rt: { attributes: { lang: "ja-Hira" } } }} />;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### An unreliable narrator
|
|
92
|
+
|
|
93
|
+
This example demonstrates custom annotation with context-sensitive handling. `有趣` reads **yǒu qù** (“interesting”), but adding `真` (“really”) before it changes the reading to **wú liáo** (“boring”). `生命` gets the Japanese reading **いのち**: a narrator who finds life less interesting the more you insist otherwise.
|
|
94
|
+
|
|
95
|
+
```jsx
|
|
96
|
+
import { Rubyfield } from "@rubyfield/react";
|
|
97
|
+
|
|
98
|
+
const annotate = text => [...text.matchAll(/(?<とき>時間|瞬間)|(?<いのち>生命)|(?<yǒu>有)(?<qù>趣)|真(?<wú>有)(?<liáo>趣)/gd)].flatMap(match => Object.entries(match.indices.groups).flatMap(([groupName, indices]) => indices ? [[...indices, groupName]] : []));
|
|
99
|
+
|
|
100
|
+
<Rubyfield defaultContent="生命真有趣" annotate={annotate} />;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Plain-text annotation separators
|
|
104
|
+
|
|
105
|
+
`<rp>` supplies separators for plain-text representations, such as search-engine rendering and programmatically copying content with annotations. Without it, `textContent` sticks base text and annotation together. Browsers with ruby support normally hide these separators.
|
|
106
|
+
|
|
107
|
+
```jsx
|
|
108
|
+
import { Rubyfield } from "@rubyfield/react";
|
|
109
|
+
|
|
110
|
+
<Rubyfield annotate={() => [[0, 1, "nei5"]]}
|
|
111
|
+
ruby={{ rp: { before: { text: "(" }, after: { text: ")" } } }} />;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
For base text `你` and reading `nei5`, the editable element (available through the handle's `element` property) has a `textContent` of `你(nei5)`. The same annotated text is available through `new DOMParser().parseFromString(html, "text/html").body.textContent`, where `html` is the string returned by the handle's `getHTML()` method. Normal copying from the field copies base text only.
|
|
115
|
+
|
|
116
|
+
## API
|
|
117
|
+
|
|
118
|
+
### Options
|
|
119
|
+
|
|
120
|
+
<table>
|
|
121
|
+
<thead><tr><th colspan="4">Option</th><th>Type</th><th>Default</th><th>Description</th></tr></thead>
|
|
122
|
+
<tbody>
|
|
123
|
+
<tr><td colspan="4"><code>annotate</code></td><td><code>(text: string) => readonly (readonly [startIndex: number, endIndex: number, rubyText: string])[]</code></td><td><strong>Required</strong></td><td>Pure function returning annotation ranges. Indices are zero-based UTF-16 offsets with an exclusive end, matching JavaScript string slicing. Empty ruby text leaves the base text unannotated.</td></tr>
|
|
124
|
+
<tr><td colspan="4"><code>annotationBehavior</code></td><td><code>AnnotationBehaviorOptions</code></td><td><code>undefined</code></td><td>Control annotation scope, invalid results, and errors.</td></tr>
|
|
125
|
+
<tr><td></td><td colspan="3"><code>scope</code></td><td><code>"line" | "text"</code></td><td><code>"line"</code></td><td><ul><li><code>line</code>: pass only new or changed newline-delimited lines to the annotator; unchanged lines retain their marks and reuse results. Useful when annotation is expensive.</li><li><code>text</code>: pass the whole base text so the annotator can use context across paragraphs; ranges crossing line breaks repeat their ruby text on each line. Changing the annotator, scope, or validation resets cached results.</li></ul></td></tr>
|
|
126
|
+
<tr><td></td><td colspan="3"><code>validation</code></td><td><code>"lenient" | "strict"</code></td><td><code>"lenient"</code></td><td><ul><li><code>lenient</code>: intended for integrating third-party annotation providers whose output you do not control. Discard non-array entries, non-number/non-finite/fractional indices, non-string ruby text, and reversed or empty ranges; clamp bounds. Preserve array order, overlaps, and surrogate/grapheme splits. Later ranges replace earlier marks where they overlap.</li><li><code>strict</code>: intended for annotator authors developing or debugging their functions. Discard out-of-bounds ranges instead of clamping; also discard overlaps and surrogate/grapheme splits. Process ranges in starting-index order; retain the previously accepted range on overlap. Report discarded entries in an <code>AggregateError</code>.</li></ul>A non-array result is treated as empty; strict mode also reports an error.</td></tr>
|
|
127
|
+
<tr><td></td><td colspan="3"><code>onError</code></td><td><code>(error: unknown) => void</code></td><td><code>undefined</code></td><td>Receive errors after the edit commits; without a handler, errors are thrown asynchronously. A throwing annotator leaves the affected text unannotated; strict validation retains valid entries. Annotation errors do not reject text edits.</td></tr>
|
|
128
|
+
<tr><td colspan="4"><code>content</code></td><td><code>string</code></td><td><code>undefined</code></td><td>Externally managed base text. Changes replace the current text; keep it in sync with <code>onUpdate</code>.</td></tr>
|
|
129
|
+
<tr><td colspan="4"><code>defaultContent</code></td><td><code>string</code></td><td><code>""</code></td><td>Initial base text for an uncontrolled field. Do not combine with <code>content</code>.</td></tr>
|
|
130
|
+
<tr><td colspan="4"><code>onUpdate</code></td><td><code>(content: string) => void</code></td><td><code>undefined</code></td><td>Called with the base text after an edit.</td></tr>
|
|
131
|
+
<tr><td colspan="4"><code>ruby</code></td><td><code>RubyOptions</code></td><td><code>undefined</code></td><td>Configure ruby elements in both the live field and HTML export.</td></tr>
|
|
132
|
+
<tr><td></td><td colspan="3"><code>attributes</code></td><td><code>Attributes</code></td><td><code>{}</code></td><td>Raw HTML attributes on <code><ruby></code>.</td></tr>
|
|
133
|
+
<tr><td></td><td colspan="3"><code>rt</code></td><td><code>ElementOptions</code></td><td><code>undefined</code></td><td>Configure <code><rt></code>. Omitting this option still creates the annotation element.</td></tr>
|
|
134
|
+
<tr><td></td><td></td><td colspan="2"><code>attributes</code></td><td><code>Attributes</code></td><td><code>{}</code></td><td>Raw HTML attributes on <code><rt></code>, such as the annotation language.</td></tr>
|
|
135
|
+
<tr><td></td><td colspan="3"><code>rp</code></td><td><code>{ before?: RPElementOptions; after?: RPElementOptions }</code></td><td><code>undefined</code></td><td>Configure plain-text separators around annotations. Omitting this option creates neither <code><rp></code> element.</td></tr>
|
|
136
|
+
<tr><td></td><td></td><td colspan="2"><code>before</code></td><td><code>RPElementOptions</code></td><td><code>undefined</code></td><td>Create an <code><rp></code> before the annotation only when this object is supplied.</td></tr>
|
|
137
|
+
<tr><td></td><td></td><td></td><td colspan="1"><code>text</code></td><td><code>string</code></td><td><code>"("</code></td><td>Plain text inside the preceding <code><rp></code>; HTML is not interpreted.</td></tr>
|
|
138
|
+
<tr><td></td><td></td><td></td><td colspan="1"><code>attributes</code></td><td><code>Attributes</code></td><td><code>{}</code></td><td>Raw HTML attributes on the preceding <code><rp></code>.</td></tr>
|
|
139
|
+
<tr><td></td><td></td><td colspan="2"><code>after</code></td><td><code>RPElementOptions</code></td><td><code>undefined</code></td><td>Create an <code><rp></code> after the annotation only when this object is supplied.</td></tr>
|
|
140
|
+
<tr><td></td><td></td><td></td><td colspan="1"><code>text</code></td><td><code>string</code></td><td><code>")"</code></td><td>Plain text inside the following <code><rp></code>; HTML is not interpreted.</td></tr>
|
|
141
|
+
<tr><td></td><td></td><td></td><td colspan="1"><code>attributes</code></td><td><code>Attributes</code></td><td><code>{}</code></td><td>Raw HTML attributes on the following <code><rp></code>.</td></tr>
|
|
142
|
+
<tr><td colspan="4"><code>attributes</code></td><td><code>Attributes</code></td><td><code>{}</code></td><td>Raw HTML attributes on the editable element, such as <code>lang</code>, <code>class</code>, or <code>aria-*</code>.</td></tr>
|
|
143
|
+
<tr><td colspan="4"><code>multiline</code></td><td><code>boolean</code></td><td><code>true</code></td><td>Allow line breaks. When false, the field stays on one line; supplied or pasted/dropped line breaks become spaces. Set at creation; recreate or remount to change it.</td></tr>
|
|
144
|
+
<tr><td colspan="4"><code>readOnly</code></td><td><code>boolean</code></td><td><code>false</code></td><td>Prevent editing while allowing focus, selection, and copying.</td></tr>
|
|
145
|
+
<tr><td colspan="4"><code>disabled</code></td><td><code>boolean</code></td><td><code>false</code></td><td>Prevent editing and remove the field from keyboard tab navigation.</td></tr>
|
|
146
|
+
<tr><td colspan="4"><code>placeholder</code></td><td><code>string</code></td><td><code>""</code></td><td>Text displayed while the base text is empty.</td></tr>
|
|
147
|
+
<tr><td colspan="4"><code>autoFocus</code></td><td><code>boolean</code></td><td><code>false</code></td><td>Focus the field when it mounts.</td></tr>
|
|
148
|
+
</tbody>
|
|
149
|
+
</table>
|
|
150
|
+
|
|
151
|
+
### Usage
|
|
152
|
+
|
|
153
|
+
Rubyfield installs no CSS apart from the minimal styles needed for placeholders and single-line fields. Supply your own styles.
|
|
154
|
+
|
|
155
|
+
Input is plain text: pasted formatting and images are not retained, and line endings become `\n`. Base text is editable; annotations are read-only.
|
|
156
|
+
|
|
157
|
+
Each Rubyfield package exports the optional utility `segmentsToRanges(pairs)`, which converts `[baseText, rubyText]` pairs into UTF-16 annotation ranges. The `baseText` strings, joined in order, must reproduce the original text, including spaces and punctuation. A pair such as `["你好", "nei5 hou2"]` adds an annotation; `["!", null]` leaves that segment unannotated while counting its length for subsequent ranges.
|
|
158
|
+
|
|
159
|
+
Raw `Attributes` objects use HTML names (`class`, not `className`):
|
|
160
|
+
|
|
161
|
+
- Null/undefined omit an attribute.
|
|
162
|
+
- Other values are stringified. Use CSS strings for `style`; functions are not event listeners.
|
|
163
|
+
|
|
164
|
+
For example, use `hidden: ""` to enable `hidden` and omit it to disable it; `hidden: false` becomes `hidden="false"`, which still hides the element.
|
|
165
|
+
|
|
166
|
+
The field's handle provides access to its properties and methods through React's `ref.current`, Vue's template-ref `.value`, or the object returned by `createRubyfield(...)`. Its TypeScript type is `RubyfieldHandle`. The handle exposes:
|
|
167
|
+
|
|
168
|
+
- Properties:
|
|
169
|
+
- `element`: the editable `HTMLElement`, or null.
|
|
170
|
+
- `value`: the current base text, read-only.
|
|
171
|
+
- Methods:
|
|
172
|
+
- `focus()` / `blur()`: focus or blur the field.
|
|
173
|
+
- `select()`: select all base text.
|
|
174
|
+
- `setSelectionRange(start, end)`: select a range using zero-based UTF-16 offsets.
|
|
175
|
+
- `getHTML()`: return an annotated HTML string. Normal copying copies base text only.
|
|
176
|
+
|
|
177
|
+
Native DOM props, including classes, styles, and event callbacks, apply to the outer container with framework semantics. Use `attributes` for the editable element. Capture handlers run before field handling; `onUpdate` (core/React) or `update:content` (Vue) reports base-text edits. Children, rich HTML, `contentEditable`, and textarea-style `onChange` are not supported.
|
|
178
|
+
|
|
179
|
+
In React Server Component apps, place the field inside a client component.
|
|
180
|
+
|
|
181
|
+
## Launching the Demo
|
|
182
|
+
|
|
183
|
+
Try the [hosted demo](https://graphemecluster.github.io/rubyfield/), or run it locally from the repository root:
|
|
184
|
+
|
|
185
|
+
```sh
|
|
186
|
+
bun install
|
|
187
|
+
bun run dev
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Open the URL printed by Vite. The demo includes Pinyin, Jyutping, and Furigana; the Japanese dictionary loads when Furigana is selected.
|
|
191
|
+
|
|
192
|
+
## Development
|
|
193
|
+
|
|
194
|
+
From the repository root:
|
|
195
|
+
|
|
196
|
+
- Build: `bun run build`.
|
|
197
|
+
- Check types: `bun run typecheck`.
|
|
198
|
+
- Format: `bun run format`.
|
|
199
|
+
- Check formatting: `bun run format:check`.
|
|
200
|
+
- Run tests:
|
|
201
|
+
- Install managed Chromium once: `bunx --no-install playwright-core install chromium`.
|
|
202
|
+
- All tests: `bun test`.
|
|
203
|
+
- Browser tests only: `bun run test:browser`.
|
|
204
|
+
- Unit tests only: `bun test tests/text.test.ts`.
|
|
205
|
+
|
|
206
|
+
Tests start and stop their own servers.
|
|
207
|
+
|
|
208
|
+
JSR uses the `deno.json` workspace configuration and publishes TypeScript source as `@rubyfield/core`, `@rubyfield/react`, and `@rubyfield/vue`. The JSR CLI downloads Deno internally; development uses Bun.
|
|
209
|
+
|
|
210
|
+
To release, set the same version in all three packages' `package.json` and `deno.json` files, update the core import versions in the root `deno.json` to match, then push a matching `vX.Y.Z` tag. The `publish.yml` workflow checks, builds, tests, and publishes to npm and JSR before creating a GitHub release. Prereleases use npm's `next` tag. Both registries require repository linking; npm also requires trusted publisher configuration for `publish.yml`. Failed runs can be retried; already published versions are skipped.
|
|
211
|
+
|
|
212
|
+
## License
|
|
213
|
+
|
|
214
|
+
[MIT](https://github.com/graphemecluster/rubyfield/blob/main/LICENSE).
|
|
215
|
+
|
|
216
|
+
[core]: https://github.com/graphemecluster/rubyfield/tree/main/packages/core#readme
|
|
217
|
+
[react]: https://github.com/graphemecluster/rubyfield/tree/main/packages/react#readme
|
|
218
|
+
[vue]: https://github.com/graphemecluster/rubyfield/tree/main/packages/vue#readme
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RubyfieldOptions, RubyfieldHandle } from 'rubyfield';
|
|
2
|
+
export { Annotate, Annotation, AnnotationBehaviorOptions, Attributes, ElementOptions, RPElementOptions, RubyOptions, RubyfieldHandle, RubyfieldOptions, segmentsToRanges } from 'rubyfield';
|
|
3
|
+
import { ForwardRefExoticComponent, HTMLAttributes, RefAttributes } from 'react';
|
|
4
|
+
|
|
5
|
+
type DOMProps = Omit<HTMLAttributes<HTMLDivElement>, "children" | "dangerouslySetInnerHTML" | "contentEditable" | "defaultValue" | "onChange" | "onChangeCapture">;
|
|
6
|
+
type RubyfieldProps = RubyfieldOptions & DOMProps;
|
|
7
|
+
/** Plain-text field with live, read-only ruby annotations and a ref exposing RubyfieldHandle. */
|
|
8
|
+
declare const Rubyfield: ForwardRefExoticComponent<RubyfieldProps & RefAttributes<RubyfieldHandle>>;
|
|
9
|
+
|
|
10
|
+
export { Rubyfield, type RubyfieldProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/index.tsx
|
|
2
|
+
import { EditorContent, useEditor } from "@tiptap/react";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
import { BindingSession } from "rubyfield/session";
|
|
5
|
+
import { segmentsToRanges } from "rubyfield";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
var Rubyfield = forwardRef(function Rubyfield2(props, ref) {
|
|
8
|
+
const {
|
|
9
|
+
annotate,
|
|
10
|
+
annotationBehavior,
|
|
11
|
+
content,
|
|
12
|
+
defaultContent,
|
|
13
|
+
onUpdate,
|
|
14
|
+
ruby,
|
|
15
|
+
attributes,
|
|
16
|
+
multiline,
|
|
17
|
+
readOnly,
|
|
18
|
+
disabled,
|
|
19
|
+
placeholder,
|
|
20
|
+
autoFocus,
|
|
21
|
+
...domProps
|
|
22
|
+
} = props;
|
|
23
|
+
const options = {
|
|
24
|
+
annotate,
|
|
25
|
+
annotationBehavior,
|
|
26
|
+
content,
|
|
27
|
+
defaultContent,
|
|
28
|
+
onUpdate,
|
|
29
|
+
ruby,
|
|
30
|
+
attributes,
|
|
31
|
+
multiline,
|
|
32
|
+
readOnly,
|
|
33
|
+
disabled,
|
|
34
|
+
placeholder,
|
|
35
|
+
autoFocus
|
|
36
|
+
};
|
|
37
|
+
const sessionRef = useRef(null);
|
|
38
|
+
if (!sessionRef.current) sessionRef.current = new BindingSession(options);
|
|
39
|
+
const session = sessionRef.current;
|
|
40
|
+
const initialOptions = useRef(null);
|
|
41
|
+
if (!initialOptions.current) initialOptions.current = session.createOptions();
|
|
42
|
+
const editor = useEditor({ ...initialOptions.current, immediatelyRender: false });
|
|
43
|
+
useImperativeHandle(ref, () => session.handle, [session]);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (editor) {
|
|
46
|
+
session.attach(editor);
|
|
47
|
+
session.setOptions(options);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return /* @__PURE__ */ jsx(EditorContent, { editor, ...domProps });
|
|
51
|
+
});
|
|
52
|
+
export {
|
|
53
|
+
Rubyfield,
|
|
54
|
+
segmentsToRanges
|
|
55
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@rubyfield/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Rubyfield React component.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/graphemecluster/rubyfield.git",
|
|
8
|
+
"directory": "packages/react"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"main": "./dist/index.js",
|
|
20
|
+
"module": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup src/index.tsx --format esm --dts --clean"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@tiptap/core": "3.35.0",
|
|
32
|
+
"@tiptap/pm": "3.35.0",
|
|
33
|
+
"@tiptap/react": "3.35.0",
|
|
34
|
+
"rubyfield": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
38
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|