@tiptap/extension-ruby-text 3.29.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.md +21 -0
- package/README.md +20 -0
- package/dist/index.cjs +317 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +290 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
- package/src/index.ts +5 -0
- package/src/ruby-text-decoration-plugin.ts +295 -0
- package/src/ruby-text.ts +174 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// src/ruby-text.ts
|
|
2
|
+
import { Mark, mergeAttributes } from "@tiptap/core";
|
|
3
|
+
|
|
4
|
+
// src/ruby-text-decoration-plugin.ts
|
|
5
|
+
import { Plugin, PluginKey, TextSelection } from "@tiptap/pm/state";
|
|
6
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view";
|
|
7
|
+
var rubyTextDecorationPluginKey = new PluginKey("rubyTextDecoration");
|
|
8
|
+
var editSessions = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
function isImeEvent(event) {
|
|
10
|
+
const legacyKeyCode = event.keyCode;
|
|
11
|
+
return event.isComposing || legacyKeyCode === 229;
|
|
12
|
+
}
|
|
13
|
+
function defaultRenderAnnotationEditor({
|
|
14
|
+
annotation,
|
|
15
|
+
submit,
|
|
16
|
+
dismiss
|
|
17
|
+
}) {
|
|
18
|
+
const input = document.createElement("input");
|
|
19
|
+
input.type = "text";
|
|
20
|
+
input.value = annotation;
|
|
21
|
+
input.size = Math.max(annotation.length, 1);
|
|
22
|
+
input.setAttribute("aria-label", "Ruby text annotation");
|
|
23
|
+
input.addEventListener("focus", () => input.select(), { once: true });
|
|
24
|
+
input.addEventListener("blur", () => dismiss());
|
|
25
|
+
input.addEventListener("keydown", (event) => {
|
|
26
|
+
if (isImeEvent(event)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (event.key === "Escape") {
|
|
30
|
+
event.preventDefault();
|
|
31
|
+
dismiss();
|
|
32
|
+
}
|
|
33
|
+
if (event.key === "Enter") {
|
|
34
|
+
event.preventDefault();
|
|
35
|
+
submit(input.value);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return input;
|
|
39
|
+
}
|
|
40
|
+
function createRtElement(annotation, range, rubyTextType, view, options) {
|
|
41
|
+
const rt = document.createElement("rt");
|
|
42
|
+
rt.contentEditable = "false";
|
|
43
|
+
rt.textContent = annotation;
|
|
44
|
+
let editing = false;
|
|
45
|
+
const restoreEditorFocus = () => {
|
|
46
|
+
view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, range.to)));
|
|
47
|
+
view.focus();
|
|
48
|
+
};
|
|
49
|
+
if (!options.allowClickToEdit || !view.editable) {
|
|
50
|
+
return rt;
|
|
51
|
+
}
|
|
52
|
+
editSessions.set(rt, () => {
|
|
53
|
+
editing = false;
|
|
54
|
+
});
|
|
55
|
+
const dismiss = () => {
|
|
56
|
+
if (!editing) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
editing = false;
|
|
60
|
+
rt.textContent = annotation;
|
|
61
|
+
restoreEditorFocus();
|
|
62
|
+
};
|
|
63
|
+
const submit = (value) => {
|
|
64
|
+
if (!editing) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (!view.editable || value === annotation) {
|
|
68
|
+
dismiss();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
editing = false;
|
|
72
|
+
const transaction = view.state.tr.addMark(
|
|
73
|
+
range.from,
|
|
74
|
+
range.to,
|
|
75
|
+
rubyTextType.create({ ...range.mark.attrs, rt: value })
|
|
76
|
+
);
|
|
77
|
+
view.dispatch(transaction.setSelection(TextSelection.create(transaction.doc, range.to)));
|
|
78
|
+
view.focus();
|
|
79
|
+
};
|
|
80
|
+
const openEditor = () => {
|
|
81
|
+
var _a, _b;
|
|
82
|
+
const renderEditor = (_a = options.renderAnnotationEditor) != null ? _a : defaultRenderAnnotationEditor;
|
|
83
|
+
const element = renderEditor({ annotation, submit, dismiss, editor: options.editor });
|
|
84
|
+
rt.replaceChildren(element);
|
|
85
|
+
const focusTarget = (_b = element.querySelector("[autofocus]")) != null ? _b : element;
|
|
86
|
+
focusTarget.focus();
|
|
87
|
+
};
|
|
88
|
+
rt.addEventListener("click", () => {
|
|
89
|
+
if (editing || !view.editable) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
editing = true;
|
|
93
|
+
openEditor();
|
|
94
|
+
});
|
|
95
|
+
return rt;
|
|
96
|
+
}
|
|
97
|
+
function getRubyTextMark(node, rubyTextType) {
|
|
98
|
+
return node.isText ? node.marks.find((candidate) => candidate.type === rubyTextType) : void 0;
|
|
99
|
+
}
|
|
100
|
+
function addRange(ranges, range) {
|
|
101
|
+
if (range) {
|
|
102
|
+
ranges.push(range);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function canExtendRange(range, mark, pos) {
|
|
106
|
+
return (range == null ? void 0 : range.to) === pos && range.mark.eq(mark);
|
|
107
|
+
}
|
|
108
|
+
function getNextRange(ranges, range, node, pos, rubyTextType) {
|
|
109
|
+
const mark = getRubyTextMark(node, rubyTextType);
|
|
110
|
+
if (!mark) {
|
|
111
|
+
addRange(ranges, range);
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
if (range && canExtendRange(range, mark, pos)) {
|
|
115
|
+
range.to += node.nodeSize;
|
|
116
|
+
return range;
|
|
117
|
+
}
|
|
118
|
+
addRange(ranges, range);
|
|
119
|
+
return { from: pos, to: pos + node.nodeSize, mark };
|
|
120
|
+
}
|
|
121
|
+
function getRubyTextRanges(doc, rubyTextType) {
|
|
122
|
+
const ranges = [];
|
|
123
|
+
let range = null;
|
|
124
|
+
doc.descendants((node, pos) => {
|
|
125
|
+
range = getNextRange(ranges, range, node, pos, rubyTextType);
|
|
126
|
+
});
|
|
127
|
+
addRange(ranges, range);
|
|
128
|
+
return ranges;
|
|
129
|
+
}
|
|
130
|
+
var STOPPED_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
131
|
+
"click",
|
|
132
|
+
"dblclick",
|
|
133
|
+
"contextmenu",
|
|
134
|
+
"mousedown",
|
|
135
|
+
"mouseup",
|
|
136
|
+
"pointerdown",
|
|
137
|
+
"pointerup",
|
|
138
|
+
"touchstart",
|
|
139
|
+
"touchend",
|
|
140
|
+
"keydown",
|
|
141
|
+
"keypress",
|
|
142
|
+
"keyup",
|
|
143
|
+
"input",
|
|
144
|
+
"beforeinput",
|
|
145
|
+
"compositionstart",
|
|
146
|
+
"compositionupdate",
|
|
147
|
+
"compositionend",
|
|
148
|
+
"paste",
|
|
149
|
+
"cut",
|
|
150
|
+
"copy",
|
|
151
|
+
"focus",
|
|
152
|
+
"blur",
|
|
153
|
+
"focusin",
|
|
154
|
+
"focusout"
|
|
155
|
+
]);
|
|
156
|
+
function createDecorations(doc, rubyTextType, options) {
|
|
157
|
+
const decorations = getRubyTextRanges(doc, rubyTextType).map((range) => {
|
|
158
|
+
var _a;
|
|
159
|
+
const annotation = (_a = range.mark.attrs.rt) != null ? _a : "";
|
|
160
|
+
return Decoration.widget(
|
|
161
|
+
range.to,
|
|
162
|
+
(view) => createRtElement(annotation, range, rubyTextType, view, options),
|
|
163
|
+
{
|
|
164
|
+
key: `ruby-text-${range.from}-${range.to}-${annotation}`,
|
|
165
|
+
marks: [range.mark],
|
|
166
|
+
side: 1,
|
|
167
|
+
stopEvent: (event) => options.allowClickToEdit && STOPPED_EVENT_TYPES.has(event.type),
|
|
168
|
+
destroy: (node) => {
|
|
169
|
+
var _a2;
|
|
170
|
+
return (_a2 = editSessions.get(node)) == null ? void 0 : _a2();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
return DecorationSet.create(doc, decorations);
|
|
176
|
+
}
|
|
177
|
+
function RubyTextDecorationPlugin(rubyTextType, options) {
|
|
178
|
+
return new Plugin({
|
|
179
|
+
key: rubyTextDecorationPluginKey,
|
|
180
|
+
state: {
|
|
181
|
+
init: (_, state) => createDecorations(state.doc, rubyTextType, options),
|
|
182
|
+
apply: (transaction, decorations) => transaction.docChanged ? createDecorations(transaction.doc, rubyTextType, options) : decorations.map(transaction.mapping, transaction.doc)
|
|
183
|
+
},
|
|
184
|
+
props: {
|
|
185
|
+
decorations: (state) => rubyTextDecorationPluginKey.getState(state)
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/ruby-text.ts
|
|
191
|
+
var RubyText = Mark.create({
|
|
192
|
+
name: "rubyText",
|
|
193
|
+
inclusive: false,
|
|
194
|
+
addOptions() {
|
|
195
|
+
return {
|
|
196
|
+
HTMLAttributes: {},
|
|
197
|
+
allowClickToEdit: true,
|
|
198
|
+
renderAnnotationEditor: void 0
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
addAttributes() {
|
|
202
|
+
return {
|
|
203
|
+
rt: {
|
|
204
|
+
default: null
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
parseHTML() {
|
|
209
|
+
return [
|
|
210
|
+
{
|
|
211
|
+
tag: "ruby",
|
|
212
|
+
contentElement: (node) => {
|
|
213
|
+
const rb = node.querySelector("rb");
|
|
214
|
+
if (rb) {
|
|
215
|
+
return rb;
|
|
216
|
+
}
|
|
217
|
+
const surrogate = document.createElement("span");
|
|
218
|
+
Array.from(node.childNodes).forEach((child) => {
|
|
219
|
+
if (child.nodeName !== "RT" && child.nodeName !== "RP") {
|
|
220
|
+
surrogate.appendChild(child.cloneNode(true));
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
return surrogate;
|
|
224
|
+
},
|
|
225
|
+
getAttrs: (node) => {
|
|
226
|
+
var _a;
|
|
227
|
+
const rt = node.querySelector("rt");
|
|
228
|
+
if (!rt) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
return { rt: (_a = rt.textContent) != null ? _a : "" };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
];
|
|
235
|
+
},
|
|
236
|
+
renderHTML({ HTMLAttributes, mark }) {
|
|
237
|
+
return [
|
|
238
|
+
"ruby",
|
|
239
|
+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
|
|
240
|
+
["rb", 0],
|
|
241
|
+
...mark.attrs.rt == null ? [] : [["rt", { contenteditable: "false" }, mark.attrs.rt]]
|
|
242
|
+
];
|
|
243
|
+
},
|
|
244
|
+
addCommands() {
|
|
245
|
+
return {
|
|
246
|
+
setRubyText: (attributes) => ({ commands }) => {
|
|
247
|
+
return commands.setMark(this.name, attributes);
|
|
248
|
+
},
|
|
249
|
+
toggleRubyText: (attributes) => ({ commands }) => {
|
|
250
|
+
return commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true });
|
|
251
|
+
},
|
|
252
|
+
unsetRubyText: () => ({ commands }) => {
|
|
253
|
+
return commands.unsetMark(this.name, { extendEmptyMarkRange: true });
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
},
|
|
257
|
+
addProseMirrorPlugins() {
|
|
258
|
+
return [
|
|
259
|
+
RubyTextDecorationPlugin(this.type, {
|
|
260
|
+
editor: this.editor,
|
|
261
|
+
allowClickToEdit: this.options.allowClickToEdit,
|
|
262
|
+
renderAnnotationEditor: this.options.renderAnnotationEditor
|
|
263
|
+
})
|
|
264
|
+
];
|
|
265
|
+
},
|
|
266
|
+
addMarkView() {
|
|
267
|
+
return ({ HTMLAttributes }) => {
|
|
268
|
+
const ruby = document.createElement("ruby");
|
|
269
|
+
Object.entries(mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)).forEach(
|
|
270
|
+
([key, value]) => {
|
|
271
|
+
if (value != null) {
|
|
272
|
+
ruby.setAttribute(key, String(value));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
);
|
|
276
|
+
return {
|
|
277
|
+
dom: ruby,
|
|
278
|
+
contentDOM: ruby
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// src/index.ts
|
|
285
|
+
var index_default = RubyText;
|
|
286
|
+
export {
|
|
287
|
+
RubyText,
|
|
288
|
+
index_default as default
|
|
289
|
+
};
|
|
290
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ruby-text.ts","../src/ruby-text-decoration-plugin.ts","../src/index.ts"],"sourcesContent":["import { Mark, mergeAttributes } from '@tiptap/core'\n\nimport type { RubyTextAnnotationEditorProps } from './ruby-text-decoration-plugin.js'\nimport { RubyTextDecorationPlugin } from './ruby-text-decoration-plugin.js'\n\nexport type { RubyTextAnnotationEditorProps }\n\nexport interface RubyTextOptions {\n /**\n * HTML attributes to add to the ruby element.\n * @default {}\n */\n HTMLAttributes: Record<string, any>\n /**\n * Whether clicking an annotation opens an inline editor.\n * @default true\n */\n allowClickToEdit: boolean\n /**\n * Renders a custom element used as the inline annotation editor.\n * The returned element is mounted inside the `rt` element. A descendant\n * with the `autofocus` attribute is focused, otherwise the element itself.\n * When not set, a plain text input is rendered.\n * @default undefined\n */\n renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement\n}\n\nexport interface RubyTextAttributes {\n /**\n * The ruby text annotation rendered in the HTML `rt` element.\n */\n rt: string | null\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n rubyText: {\n /**\n * Set a ruby text mark with an annotation on the current selection.\n * @example editor.commands.setRubyText({ rt: 'かんじ' })\n */\n setRubyText: (attributes: RubyTextAttributes) => ReturnType\n /**\n * Toggle a ruby text mark on the current selection.\n * @example editor.commands.toggleRubyText({ rt: 'かんじ' })\n */\n toggleRubyText: (attributes: RubyTextAttributes) => ReturnType\n /**\n * Remove the ruby text mark from the current selection.\n * @example editor.commands.unsetRubyText()\n */\n unsetRubyText: () => ReturnType\n }\n }\n}\n\n/**\n * This extension adds support for HTML ruby text annotations.\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby\n */\nexport const RubyText = Mark.create<RubyTextOptions>({\n name: 'rubyText',\n\n inclusive: false,\n\n addOptions() {\n return {\n HTMLAttributes: {},\n allowClickToEdit: true,\n renderAnnotationEditor: undefined,\n }\n },\n\n addAttributes() {\n return {\n rt: {\n default: null,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'ruby',\n contentElement: (node: HTMLElement) => {\n const rb = node.querySelector('rb')\n\n if (rb) {\n return rb\n }\n\n const surrogate = document.createElement('span')\n\n Array.from(node.childNodes).forEach(child => {\n if (child.nodeName !== 'RT' && child.nodeName !== 'RP') {\n surrogate.appendChild(child.cloneNode(true))\n }\n })\n\n return surrogate\n },\n getAttrs: (node: HTMLElement) => {\n const rt = node.querySelector('rt')\n\n if (!rt) {\n return false\n }\n\n return { rt: rt.textContent ?? '' }\n },\n },\n ]\n },\n\n renderHTML({ HTMLAttributes, mark }) {\n return [\n 'ruby',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n ['rb', 0],\n ...(mark.attrs.rt == null ? [] : [['rt', { contenteditable: 'false' }, mark.attrs.rt]]),\n ]\n },\n\n addCommands() {\n return {\n setRubyText:\n attributes =>\n ({ commands }) => {\n return commands.setMark(this.name, attributes)\n },\n toggleRubyText:\n attributes =>\n ({ commands }) => {\n return commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n },\n unsetRubyText:\n () =>\n ({ commands }) => {\n return commands.unsetMark(this.name, { extendEmptyMarkRange: true })\n },\n }\n },\n\n addProseMirrorPlugins() {\n return [\n RubyTextDecorationPlugin(this.type, {\n editor: this.editor,\n allowClickToEdit: this.options.allowClickToEdit,\n renderAnnotationEditor: this.options.renderAnnotationEditor,\n }),\n ]\n },\n\n addMarkView() {\n return ({ HTMLAttributes }) => {\n const ruby = document.createElement('ruby')\n\n Object.entries(mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)).forEach(\n ([key, value]) => {\n if (value != null) {\n ruby.setAttribute(key, String(value))\n }\n },\n )\n\n return {\n dom: ruby,\n contentDOM: ruby,\n }\n }\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport type { Mark, MarkType, Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey, TextSelection } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet, type EditorView } from '@tiptap/pm/view'\n\nconst rubyTextDecorationPluginKey = new PluginKey('rubyTextDecoration')\n\n// Ends a widget's edit session on destroy, so stale submit/dismiss calls do nothing.\nconst editSessions = new WeakMap<Node, () => void>()\n\ninterface RubyTextRange {\n from: number\n to: number\n mark: Mark\n}\n\nexport interface RubyTextAnnotationEditorProps {\n /**\n * The current annotation value (`''` when the mark has an empty annotation).\n */\n annotation: string\n /**\n * Applies the given value as the new annotation and closes the editor.\n * Calling it more than once (or after `dismiss`) is a no-op.\n */\n submit: (value: string) => void\n /**\n * Closes the editor without changing the annotation.\n * Calling it more than once (or after `submit`) is a no-op.\n */\n dismiss: () => void\n /**\n * The editor instance.\n */\n editor: Editor\n}\n\nexport interface RubyTextDecorationPluginOptions {\n editor: Editor\n allowClickToEdit: boolean\n renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement\n}\n\n// Enter and Escape during IME composition must not close the editor.\n// Safari reports some IME keydowns only via the deprecated keyCode 229.\nfunction isImeEvent(event: KeyboardEvent) {\n const legacyKeyCode = (event as { keyCode?: number }).keyCode\n return event.isComposing || legacyKeyCode === 229\n}\n\n/** Default annotation editor: a plain text input. Enter submits, Escape or blur dismisses. */\nfunction defaultRenderAnnotationEditor({\n annotation,\n submit,\n dismiss,\n}: RubyTextAnnotationEditorProps) {\n const input = document.createElement('input')\n input.type = 'text'\n input.value = annotation\n input.size = Math.max(annotation.length, 1)\n input.setAttribute('aria-label', 'Ruby text annotation')\n\n input.addEventListener('focus', () => input.select(), { once: true })\n input.addEventListener('blur', () => dismiss())\n input.addEventListener('keydown', event => {\n if (isImeEvent(event)) {\n return\n }\n\n if (event.key === 'Escape') {\n event.preventDefault()\n dismiss()\n }\n\n if (event.key === 'Enter') {\n event.preventDefault()\n submit(input.value)\n }\n })\n\n return input\n}\n\nfunction createRtElement(\n annotation: string,\n range: RubyTextRange,\n rubyTextType: MarkType,\n view: EditorView,\n options: RubyTextDecorationPluginOptions,\n) {\n const rt = document.createElement('rt')\n rt.contentEditable = 'false'\n rt.textContent = annotation\n let editing = false\n\n const restoreEditorFocus = () => {\n view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, range.to)))\n view.focus()\n }\n\n if (!options.allowClickToEdit || !view.editable) {\n return rt\n }\n\n editSessions.set(rt, () => {\n editing = false\n })\n\n const dismiss = () => {\n if (!editing) {\n return\n }\n\n editing = false\n rt.textContent = annotation\n restoreEditorFocus()\n }\n\n const submit = (value: string) => {\n if (!editing) {\n return\n }\n\n // An unchanged value produces no doc change, so the widget would not\n // re-render and the editor element would stay mounted.\n if (!view.editable || value === annotation) {\n dismiss()\n return\n }\n\n editing = false\n const transaction = view.state.tr.addMark(\n range.from,\n range.to,\n rubyTextType.create({ ...range.mark.attrs, rt: value }),\n )\n\n view.dispatch(transaction.setSelection(TextSelection.create(transaction.doc, range.to)))\n view.focus()\n }\n\n const openEditor = () => {\n const renderEditor = options.renderAnnotationEditor ?? defaultRenderAnnotationEditor\n const element = renderEditor({ annotation, submit, dismiss, editor: options.editor })\n\n rt.replaceChildren(element)\n\n // Browsers ignore autofocus on inserted nodes, so we focus it manually.\n const focusTarget = element.querySelector<HTMLElement>('[autofocus]') ?? element\n focusTarget.focus()\n }\n\n rt.addEventListener('click', () => {\n if (editing || !view.editable) {\n return\n }\n\n editing = true\n openEditor()\n })\n\n return rt\n}\n\n/** Gets the mark of ruby text from the given node, if it exists. */\nfunction getRubyTextMark(node: ProseMirrorNode, rubyTextType: MarkType) {\n return node.isText ? node.marks.find(candidate => candidate.type === rubyTextType) : undefined\n}\n\n/** Adds a ruby text range to an existing array of ranges. */\nfunction addRange(ranges: RubyTextRange[], range: RubyTextRange | null) {\n if (range) {\n ranges.push(range)\n }\n}\n\n/** Can the current range be extended with the given mark at the given position? */\nfunction canExtendRange(range: RubyTextRange | null, mark: Mark, pos: number) {\n return range?.to === pos && range.mark.eq(mark)\n}\n\n/**\n * Gets the next ruby text range based on the current node and position.\n * @param ranges The array of ruby text ranges to add to.\n * @param range The current ruby text range being built.\n * @param node The current ProseMirror node being processed.\n * @param pos The position of the current node in the document.\n * @param rubyTextType The MarkType for ruby text.\n * @returns The updated ruby text range, or null if the current node does not have a ruby text mark.\n */\nfunction getNextRange(\n ranges: RubyTextRange[],\n range: RubyTextRange | null,\n node: ProseMirrorNode,\n pos: number,\n rubyTextType: MarkType,\n) {\n const mark = getRubyTextMark(node, rubyTextType)\n\n if (!mark) {\n addRange(ranges, range)\n return null\n }\n\n if (range && canExtendRange(range, mark, pos)) {\n range.to += node.nodeSize\n return range\n }\n\n addRange(ranges, range)\n return { from: pos, to: pos + node.nodeSize, mark }\n}\n\nfunction getRubyTextRanges(doc: ProseMirrorNode, rubyTextType: MarkType) {\n const ranges: RubyTextRange[] = []\n let range: RubyTextRange | null = null\n\n doc.descendants((node, pos) => {\n range = getNextRange(ranges, range, node, pos, rubyTextType)\n })\n addRange(ranges, range)\n\n return ranges\n}\n\n// Events from the annotation editor that ProseMirror must not handle itself.\nconst STOPPED_EVENT_TYPES = new Set([\n 'click',\n 'dblclick',\n 'contextmenu',\n 'mousedown',\n 'mouseup',\n 'pointerdown',\n 'pointerup',\n 'touchstart',\n 'touchend',\n 'keydown',\n 'keypress',\n 'keyup',\n 'input',\n 'beforeinput',\n 'compositionstart',\n 'compositionupdate',\n 'compositionend',\n 'paste',\n 'cut',\n 'copy',\n 'focus',\n 'blur',\n 'focusin',\n 'focusout',\n])\n\nfunction createDecorations(\n doc: ProseMirrorNode,\n rubyTextType: MarkType,\n options: RubyTextDecorationPluginOptions,\n) {\n const decorations = getRubyTextRanges(doc, rubyTextType).map(range => {\n const annotation = range.mark.attrs.rt ?? ''\n\n return Decoration.widget(\n range.to,\n view => createRtElement(annotation, range, rubyTextType, view, options),\n {\n key: `ruby-text-${range.from}-${range.to}-${annotation}`,\n marks: [range.mark],\n side: 1,\n stopEvent: event => options.allowClickToEdit && STOPPED_EVENT_TYPES.has(event.type),\n destroy: node => editSessions.get(node)?.(),\n },\n )\n })\n\n return DecorationSet.create(doc, decorations)\n}\n\nexport function RubyTextDecorationPlugin(\n rubyTextType: MarkType,\n options: RubyTextDecorationPluginOptions,\n) {\n return new Plugin({\n key: rubyTextDecorationPluginKey,\n state: {\n init: (_, state) => createDecorations(state.doc, rubyTextType, options),\n apply: (transaction, decorations) =>\n transaction.docChanged\n ? createDecorations(transaction.doc, rubyTextType, options)\n : decorations.map(transaction.mapping, transaction.doc),\n },\n props: {\n decorations: state => rubyTextDecorationPluginKey.getState(state),\n },\n })\n}\n","import { RubyText } from './ruby-text.js'\n\nexport * from './ruby-text.js'\n\nexport default RubyText\n"],"mappings":";AAAA,SAAS,MAAM,uBAAuB;;;ACEtC,SAAS,QAAQ,WAAW,qBAAqB;AACjD,SAAS,YAAY,qBAAsC;AAE3D,IAAM,8BAA8B,IAAI,UAAU,oBAAoB;AAGtE,IAAM,eAAe,oBAAI,QAA0B;AAqCnD,SAAS,WAAW,OAAsB;AACxC,QAAM,gBAAiB,MAA+B;AACtD,SAAO,MAAM,eAAe,kBAAkB;AAChD;AAGA,SAAS,8BAA8B;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AACF,GAAkC;AAChC,QAAM,QAAQ,SAAS,cAAc,OAAO;AAC5C,QAAM,OAAO;AACb,QAAM,QAAQ;AACd,QAAM,OAAO,KAAK,IAAI,WAAW,QAAQ,CAAC;AAC1C,QAAM,aAAa,cAAc,sBAAsB;AAEvD,QAAM,iBAAiB,SAAS,MAAM,MAAM,OAAO,GAAG,EAAE,MAAM,KAAK,CAAC;AACp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tiptap/extension-ruby-text",
|
|
3
|
+
"version": "3.29.0",
|
|
4
|
+
"description": "HTML ruby text annotation extension for tiptap",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tiptap",
|
|
7
|
+
"tiptap extension",
|
|
8
|
+
"ruby-text",
|
|
9
|
+
"furigana",
|
|
10
|
+
"cjk"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://tiptap.dev",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/ueberdosis/tiptap/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/ueberdosis/tiptap",
|
|
20
|
+
"directory": "packages/extension-ruby-text"
|
|
21
|
+
},
|
|
22
|
+
"funding": {
|
|
23
|
+
"type": "github",
|
|
24
|
+
"url": "https://github.com/sponsors/ueberdosis"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "dist/index.cjs",
|
|
32
|
+
"module": "dist/index.js",
|
|
33
|
+
"types": "dist/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": {
|
|
37
|
+
"import": "./dist/index.d.ts",
|
|
38
|
+
"require": "./dist/index.d.cts"
|
|
39
|
+
},
|
|
40
|
+
"import": "./dist/index.js",
|
|
41
|
+
"require": "./dist/index.cjs"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@tiptap/core": "^3.29.0",
|
|
46
|
+
"@tiptap/pm": "^3.29.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@tiptap/core": "3.29.0",
|
|
50
|
+
"@tiptap/pm": "3.29.0"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core'
|
|
2
|
+
import type { Mark, MarkType, Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
3
|
+
import { Plugin, PluginKey, TextSelection } from '@tiptap/pm/state'
|
|
4
|
+
import { Decoration, DecorationSet, type EditorView } from '@tiptap/pm/view'
|
|
5
|
+
|
|
6
|
+
const rubyTextDecorationPluginKey = new PluginKey('rubyTextDecoration')
|
|
7
|
+
|
|
8
|
+
// Ends a widget's edit session on destroy, so stale submit/dismiss calls do nothing.
|
|
9
|
+
const editSessions = new WeakMap<Node, () => void>()
|
|
10
|
+
|
|
11
|
+
interface RubyTextRange {
|
|
12
|
+
from: number
|
|
13
|
+
to: number
|
|
14
|
+
mark: Mark
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RubyTextAnnotationEditorProps {
|
|
18
|
+
/**
|
|
19
|
+
* The current annotation value (`''` when the mark has an empty annotation).
|
|
20
|
+
*/
|
|
21
|
+
annotation: string
|
|
22
|
+
/**
|
|
23
|
+
* Applies the given value as the new annotation and closes the editor.
|
|
24
|
+
* Calling it more than once (or after `dismiss`) is a no-op.
|
|
25
|
+
*/
|
|
26
|
+
submit: (value: string) => void
|
|
27
|
+
/**
|
|
28
|
+
* Closes the editor without changing the annotation.
|
|
29
|
+
* Calling it more than once (or after `submit`) is a no-op.
|
|
30
|
+
*/
|
|
31
|
+
dismiss: () => void
|
|
32
|
+
/**
|
|
33
|
+
* The editor instance.
|
|
34
|
+
*/
|
|
35
|
+
editor: Editor
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RubyTextDecorationPluginOptions {
|
|
39
|
+
editor: Editor
|
|
40
|
+
allowClickToEdit: boolean
|
|
41
|
+
renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Enter and Escape during IME composition must not close the editor.
|
|
45
|
+
// Safari reports some IME keydowns only via the deprecated keyCode 229.
|
|
46
|
+
function isImeEvent(event: KeyboardEvent) {
|
|
47
|
+
const legacyKeyCode = (event as { keyCode?: number }).keyCode
|
|
48
|
+
return event.isComposing || legacyKeyCode === 229
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Default annotation editor: a plain text input. Enter submits, Escape or blur dismisses. */
|
|
52
|
+
function defaultRenderAnnotationEditor({
|
|
53
|
+
annotation,
|
|
54
|
+
submit,
|
|
55
|
+
dismiss,
|
|
56
|
+
}: RubyTextAnnotationEditorProps) {
|
|
57
|
+
const input = document.createElement('input')
|
|
58
|
+
input.type = 'text'
|
|
59
|
+
input.value = annotation
|
|
60
|
+
input.size = Math.max(annotation.length, 1)
|
|
61
|
+
input.setAttribute('aria-label', 'Ruby text annotation')
|
|
62
|
+
|
|
63
|
+
input.addEventListener('focus', () => input.select(), { once: true })
|
|
64
|
+
input.addEventListener('blur', () => dismiss())
|
|
65
|
+
input.addEventListener('keydown', event => {
|
|
66
|
+
if (isImeEvent(event)) {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (event.key === 'Escape') {
|
|
71
|
+
event.preventDefault()
|
|
72
|
+
dismiss()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (event.key === 'Enter') {
|
|
76
|
+
event.preventDefault()
|
|
77
|
+
submit(input.value)
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
return input
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function createRtElement(
|
|
85
|
+
annotation: string,
|
|
86
|
+
range: RubyTextRange,
|
|
87
|
+
rubyTextType: MarkType,
|
|
88
|
+
view: EditorView,
|
|
89
|
+
options: RubyTextDecorationPluginOptions,
|
|
90
|
+
) {
|
|
91
|
+
const rt = document.createElement('rt')
|
|
92
|
+
rt.contentEditable = 'false'
|
|
93
|
+
rt.textContent = annotation
|
|
94
|
+
let editing = false
|
|
95
|
+
|
|
96
|
+
const restoreEditorFocus = () => {
|
|
97
|
+
view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, range.to)))
|
|
98
|
+
view.focus()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!options.allowClickToEdit || !view.editable) {
|
|
102
|
+
return rt
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
editSessions.set(rt, () => {
|
|
106
|
+
editing = false
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const dismiss = () => {
|
|
110
|
+
if (!editing) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
editing = false
|
|
115
|
+
rt.textContent = annotation
|
|
116
|
+
restoreEditorFocus()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const submit = (value: string) => {
|
|
120
|
+
if (!editing) {
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// An unchanged value produces no doc change, so the widget would not
|
|
125
|
+
// re-render and the editor element would stay mounted.
|
|
126
|
+
if (!view.editable || value === annotation) {
|
|
127
|
+
dismiss()
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
editing = false
|
|
132
|
+
const transaction = view.state.tr.addMark(
|
|
133
|
+
range.from,
|
|
134
|
+
range.to,
|
|
135
|
+
rubyTextType.create({ ...range.mark.attrs, rt: value }),
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
view.dispatch(transaction.setSelection(TextSelection.create(transaction.doc, range.to)))
|
|
139
|
+
view.focus()
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const openEditor = () => {
|
|
143
|
+
const renderEditor = options.renderAnnotationEditor ?? defaultRenderAnnotationEditor
|
|
144
|
+
const element = renderEditor({ annotation, submit, dismiss, editor: options.editor })
|
|
145
|
+
|
|
146
|
+
rt.replaceChildren(element)
|
|
147
|
+
|
|
148
|
+
// Browsers ignore autofocus on inserted nodes, so we focus it manually.
|
|
149
|
+
const focusTarget = element.querySelector<HTMLElement>('[autofocus]') ?? element
|
|
150
|
+
focusTarget.focus()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
rt.addEventListener('click', () => {
|
|
154
|
+
if (editing || !view.editable) {
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
editing = true
|
|
159
|
+
openEditor()
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
return rt
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Gets the mark of ruby text from the given node, if it exists. */
|
|
166
|
+
function getRubyTextMark(node: ProseMirrorNode, rubyTextType: MarkType) {
|
|
167
|
+
return node.isText ? node.marks.find(candidate => candidate.type === rubyTextType) : undefined
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Adds a ruby text range to an existing array of ranges. */
|
|
171
|
+
function addRange(ranges: RubyTextRange[], range: RubyTextRange | null) {
|
|
172
|
+
if (range) {
|
|
173
|
+
ranges.push(range)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Can the current range be extended with the given mark at the given position? */
|
|
178
|
+
function canExtendRange(range: RubyTextRange | null, mark: Mark, pos: number) {
|
|
179
|
+
return range?.to === pos && range.mark.eq(mark)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Gets the next ruby text range based on the current node and position.
|
|
184
|
+
* @param ranges The array of ruby text ranges to add to.
|
|
185
|
+
* @param range The current ruby text range being built.
|
|
186
|
+
* @param node The current ProseMirror node being processed.
|
|
187
|
+
* @param pos The position of the current node in the document.
|
|
188
|
+
* @param rubyTextType The MarkType for ruby text.
|
|
189
|
+
* @returns The updated ruby text range, or null if the current node does not have a ruby text mark.
|
|
190
|
+
*/
|
|
191
|
+
function getNextRange(
|
|
192
|
+
ranges: RubyTextRange[],
|
|
193
|
+
range: RubyTextRange | null,
|
|
194
|
+
node: ProseMirrorNode,
|
|
195
|
+
pos: number,
|
|
196
|
+
rubyTextType: MarkType,
|
|
197
|
+
) {
|
|
198
|
+
const mark = getRubyTextMark(node, rubyTextType)
|
|
199
|
+
|
|
200
|
+
if (!mark) {
|
|
201
|
+
addRange(ranges, range)
|
|
202
|
+
return null
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (range && canExtendRange(range, mark, pos)) {
|
|
206
|
+
range.to += node.nodeSize
|
|
207
|
+
return range
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
addRange(ranges, range)
|
|
211
|
+
return { from: pos, to: pos + node.nodeSize, mark }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function getRubyTextRanges(doc: ProseMirrorNode, rubyTextType: MarkType) {
|
|
215
|
+
const ranges: RubyTextRange[] = []
|
|
216
|
+
let range: RubyTextRange | null = null
|
|
217
|
+
|
|
218
|
+
doc.descendants((node, pos) => {
|
|
219
|
+
range = getNextRange(ranges, range, node, pos, rubyTextType)
|
|
220
|
+
})
|
|
221
|
+
addRange(ranges, range)
|
|
222
|
+
|
|
223
|
+
return ranges
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Events from the annotation editor that ProseMirror must not handle itself.
|
|
227
|
+
const STOPPED_EVENT_TYPES = new Set([
|
|
228
|
+
'click',
|
|
229
|
+
'dblclick',
|
|
230
|
+
'contextmenu',
|
|
231
|
+
'mousedown',
|
|
232
|
+
'mouseup',
|
|
233
|
+
'pointerdown',
|
|
234
|
+
'pointerup',
|
|
235
|
+
'touchstart',
|
|
236
|
+
'touchend',
|
|
237
|
+
'keydown',
|
|
238
|
+
'keypress',
|
|
239
|
+
'keyup',
|
|
240
|
+
'input',
|
|
241
|
+
'beforeinput',
|
|
242
|
+
'compositionstart',
|
|
243
|
+
'compositionupdate',
|
|
244
|
+
'compositionend',
|
|
245
|
+
'paste',
|
|
246
|
+
'cut',
|
|
247
|
+
'copy',
|
|
248
|
+
'focus',
|
|
249
|
+
'blur',
|
|
250
|
+
'focusin',
|
|
251
|
+
'focusout',
|
|
252
|
+
])
|
|
253
|
+
|
|
254
|
+
function createDecorations(
|
|
255
|
+
doc: ProseMirrorNode,
|
|
256
|
+
rubyTextType: MarkType,
|
|
257
|
+
options: RubyTextDecorationPluginOptions,
|
|
258
|
+
) {
|
|
259
|
+
const decorations = getRubyTextRanges(doc, rubyTextType).map(range => {
|
|
260
|
+
const annotation = range.mark.attrs.rt ?? ''
|
|
261
|
+
|
|
262
|
+
return Decoration.widget(
|
|
263
|
+
range.to,
|
|
264
|
+
view => createRtElement(annotation, range, rubyTextType, view, options),
|
|
265
|
+
{
|
|
266
|
+
key: `ruby-text-${range.from}-${range.to}-${annotation}`,
|
|
267
|
+
marks: [range.mark],
|
|
268
|
+
side: 1,
|
|
269
|
+
stopEvent: event => options.allowClickToEdit && STOPPED_EVENT_TYPES.has(event.type),
|
|
270
|
+
destroy: node => editSessions.get(node)?.(),
|
|
271
|
+
},
|
|
272
|
+
)
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
return DecorationSet.create(doc, decorations)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function RubyTextDecorationPlugin(
|
|
279
|
+
rubyTextType: MarkType,
|
|
280
|
+
options: RubyTextDecorationPluginOptions,
|
|
281
|
+
) {
|
|
282
|
+
return new Plugin({
|
|
283
|
+
key: rubyTextDecorationPluginKey,
|
|
284
|
+
state: {
|
|
285
|
+
init: (_, state) => createDecorations(state.doc, rubyTextType, options),
|
|
286
|
+
apply: (transaction, decorations) =>
|
|
287
|
+
transaction.docChanged
|
|
288
|
+
? createDecorations(transaction.doc, rubyTextType, options)
|
|
289
|
+
: decorations.map(transaction.mapping, transaction.doc),
|
|
290
|
+
},
|
|
291
|
+
props: {
|
|
292
|
+
decorations: state => rubyTextDecorationPluginKey.getState(state),
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
}
|