@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/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Tiptap GmbH
|
|
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,20 @@
|
|
|
1
|
+
# @tiptap/extension-ruby-text
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@tiptap/extension-ruby-text)
|
|
4
|
+
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
|
5
|
+
[](https://www.npmjs.com/package/@tiptap/extension-ruby-text)
|
|
6
|
+
[](https://github.com/sponsors/ueberdosis)
|
|
7
|
+
|
|
8
|
+
## Introduction
|
|
9
|
+
|
|
10
|
+
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
|
|
11
|
+
|
|
12
|
+
This package adds support for [HTML ruby text](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby) (reading guides for CJK text). It is not related to the Ruby programming language.
|
|
13
|
+
|
|
14
|
+
## Official Documentation
|
|
15
|
+
|
|
16
|
+
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
|
17
|
+
|
|
18
|
+
## License
|
|
19
|
+
|
|
20
|
+
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
RubyText: () => RubyText,
|
|
24
|
+
default: () => index_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/ruby-text.ts
|
|
29
|
+
var import_core = require("@tiptap/core");
|
|
30
|
+
|
|
31
|
+
// src/ruby-text-decoration-plugin.ts
|
|
32
|
+
var import_state = require("@tiptap/pm/state");
|
|
33
|
+
var import_view = require("@tiptap/pm/view");
|
|
34
|
+
var rubyTextDecorationPluginKey = new import_state.PluginKey("rubyTextDecoration");
|
|
35
|
+
var editSessions = /* @__PURE__ */ new WeakMap();
|
|
36
|
+
function isImeEvent(event) {
|
|
37
|
+
const legacyKeyCode = event.keyCode;
|
|
38
|
+
return event.isComposing || legacyKeyCode === 229;
|
|
39
|
+
}
|
|
40
|
+
function defaultRenderAnnotationEditor({
|
|
41
|
+
annotation,
|
|
42
|
+
submit,
|
|
43
|
+
dismiss
|
|
44
|
+
}) {
|
|
45
|
+
const input = document.createElement("input");
|
|
46
|
+
input.type = "text";
|
|
47
|
+
input.value = annotation;
|
|
48
|
+
input.size = Math.max(annotation.length, 1);
|
|
49
|
+
input.setAttribute("aria-label", "Ruby text annotation");
|
|
50
|
+
input.addEventListener("focus", () => input.select(), { once: true });
|
|
51
|
+
input.addEventListener("blur", () => dismiss());
|
|
52
|
+
input.addEventListener("keydown", (event) => {
|
|
53
|
+
if (isImeEvent(event)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (event.key === "Escape") {
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
dismiss();
|
|
59
|
+
}
|
|
60
|
+
if (event.key === "Enter") {
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
submit(input.value);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return input;
|
|
66
|
+
}
|
|
67
|
+
function createRtElement(annotation, range, rubyTextType, view, options) {
|
|
68
|
+
const rt = document.createElement("rt");
|
|
69
|
+
rt.contentEditable = "false";
|
|
70
|
+
rt.textContent = annotation;
|
|
71
|
+
let editing = false;
|
|
72
|
+
const restoreEditorFocus = () => {
|
|
73
|
+
view.dispatch(view.state.tr.setSelection(import_state.TextSelection.create(view.state.doc, range.to)));
|
|
74
|
+
view.focus();
|
|
75
|
+
};
|
|
76
|
+
if (!options.allowClickToEdit || !view.editable) {
|
|
77
|
+
return rt;
|
|
78
|
+
}
|
|
79
|
+
editSessions.set(rt, () => {
|
|
80
|
+
editing = false;
|
|
81
|
+
});
|
|
82
|
+
const dismiss = () => {
|
|
83
|
+
if (!editing) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
editing = false;
|
|
87
|
+
rt.textContent = annotation;
|
|
88
|
+
restoreEditorFocus();
|
|
89
|
+
};
|
|
90
|
+
const submit = (value) => {
|
|
91
|
+
if (!editing) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!view.editable || value === annotation) {
|
|
95
|
+
dismiss();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
editing = false;
|
|
99
|
+
const transaction = view.state.tr.addMark(
|
|
100
|
+
range.from,
|
|
101
|
+
range.to,
|
|
102
|
+
rubyTextType.create({ ...range.mark.attrs, rt: value })
|
|
103
|
+
);
|
|
104
|
+
view.dispatch(transaction.setSelection(import_state.TextSelection.create(transaction.doc, range.to)));
|
|
105
|
+
view.focus();
|
|
106
|
+
};
|
|
107
|
+
const openEditor = () => {
|
|
108
|
+
var _a, _b;
|
|
109
|
+
const renderEditor = (_a = options.renderAnnotationEditor) != null ? _a : defaultRenderAnnotationEditor;
|
|
110
|
+
const element = renderEditor({ annotation, submit, dismiss, editor: options.editor });
|
|
111
|
+
rt.replaceChildren(element);
|
|
112
|
+
const focusTarget = (_b = element.querySelector("[autofocus]")) != null ? _b : element;
|
|
113
|
+
focusTarget.focus();
|
|
114
|
+
};
|
|
115
|
+
rt.addEventListener("click", () => {
|
|
116
|
+
if (editing || !view.editable) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
editing = true;
|
|
120
|
+
openEditor();
|
|
121
|
+
});
|
|
122
|
+
return rt;
|
|
123
|
+
}
|
|
124
|
+
function getRubyTextMark(node, rubyTextType) {
|
|
125
|
+
return node.isText ? node.marks.find((candidate) => candidate.type === rubyTextType) : void 0;
|
|
126
|
+
}
|
|
127
|
+
function addRange(ranges, range) {
|
|
128
|
+
if (range) {
|
|
129
|
+
ranges.push(range);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function canExtendRange(range, mark, pos) {
|
|
133
|
+
return (range == null ? void 0 : range.to) === pos && range.mark.eq(mark);
|
|
134
|
+
}
|
|
135
|
+
function getNextRange(ranges, range, node, pos, rubyTextType) {
|
|
136
|
+
const mark = getRubyTextMark(node, rubyTextType);
|
|
137
|
+
if (!mark) {
|
|
138
|
+
addRange(ranges, range);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
if (range && canExtendRange(range, mark, pos)) {
|
|
142
|
+
range.to += node.nodeSize;
|
|
143
|
+
return range;
|
|
144
|
+
}
|
|
145
|
+
addRange(ranges, range);
|
|
146
|
+
return { from: pos, to: pos + node.nodeSize, mark };
|
|
147
|
+
}
|
|
148
|
+
function getRubyTextRanges(doc, rubyTextType) {
|
|
149
|
+
const ranges = [];
|
|
150
|
+
let range = null;
|
|
151
|
+
doc.descendants((node, pos) => {
|
|
152
|
+
range = getNextRange(ranges, range, node, pos, rubyTextType);
|
|
153
|
+
});
|
|
154
|
+
addRange(ranges, range);
|
|
155
|
+
return ranges;
|
|
156
|
+
}
|
|
157
|
+
var STOPPED_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
158
|
+
"click",
|
|
159
|
+
"dblclick",
|
|
160
|
+
"contextmenu",
|
|
161
|
+
"mousedown",
|
|
162
|
+
"mouseup",
|
|
163
|
+
"pointerdown",
|
|
164
|
+
"pointerup",
|
|
165
|
+
"touchstart",
|
|
166
|
+
"touchend",
|
|
167
|
+
"keydown",
|
|
168
|
+
"keypress",
|
|
169
|
+
"keyup",
|
|
170
|
+
"input",
|
|
171
|
+
"beforeinput",
|
|
172
|
+
"compositionstart",
|
|
173
|
+
"compositionupdate",
|
|
174
|
+
"compositionend",
|
|
175
|
+
"paste",
|
|
176
|
+
"cut",
|
|
177
|
+
"copy",
|
|
178
|
+
"focus",
|
|
179
|
+
"blur",
|
|
180
|
+
"focusin",
|
|
181
|
+
"focusout"
|
|
182
|
+
]);
|
|
183
|
+
function createDecorations(doc, rubyTextType, options) {
|
|
184
|
+
const decorations = getRubyTextRanges(doc, rubyTextType).map((range) => {
|
|
185
|
+
var _a;
|
|
186
|
+
const annotation = (_a = range.mark.attrs.rt) != null ? _a : "";
|
|
187
|
+
return import_view.Decoration.widget(
|
|
188
|
+
range.to,
|
|
189
|
+
(view) => createRtElement(annotation, range, rubyTextType, view, options),
|
|
190
|
+
{
|
|
191
|
+
key: `ruby-text-${range.from}-${range.to}-${annotation}`,
|
|
192
|
+
marks: [range.mark],
|
|
193
|
+
side: 1,
|
|
194
|
+
stopEvent: (event) => options.allowClickToEdit && STOPPED_EVENT_TYPES.has(event.type),
|
|
195
|
+
destroy: (node) => {
|
|
196
|
+
var _a2;
|
|
197
|
+
return (_a2 = editSessions.get(node)) == null ? void 0 : _a2();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
return import_view.DecorationSet.create(doc, decorations);
|
|
203
|
+
}
|
|
204
|
+
function RubyTextDecorationPlugin(rubyTextType, options) {
|
|
205
|
+
return new import_state.Plugin({
|
|
206
|
+
key: rubyTextDecorationPluginKey,
|
|
207
|
+
state: {
|
|
208
|
+
init: (_, state) => createDecorations(state.doc, rubyTextType, options),
|
|
209
|
+
apply: (transaction, decorations) => transaction.docChanged ? createDecorations(transaction.doc, rubyTextType, options) : decorations.map(transaction.mapping, transaction.doc)
|
|
210
|
+
},
|
|
211
|
+
props: {
|
|
212
|
+
decorations: (state) => rubyTextDecorationPluginKey.getState(state)
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/ruby-text.ts
|
|
218
|
+
var RubyText = import_core.Mark.create({
|
|
219
|
+
name: "rubyText",
|
|
220
|
+
inclusive: false,
|
|
221
|
+
addOptions() {
|
|
222
|
+
return {
|
|
223
|
+
HTMLAttributes: {},
|
|
224
|
+
allowClickToEdit: true,
|
|
225
|
+
renderAnnotationEditor: void 0
|
|
226
|
+
};
|
|
227
|
+
},
|
|
228
|
+
addAttributes() {
|
|
229
|
+
return {
|
|
230
|
+
rt: {
|
|
231
|
+
default: null
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
},
|
|
235
|
+
parseHTML() {
|
|
236
|
+
return [
|
|
237
|
+
{
|
|
238
|
+
tag: "ruby",
|
|
239
|
+
contentElement: (node) => {
|
|
240
|
+
const rb = node.querySelector("rb");
|
|
241
|
+
if (rb) {
|
|
242
|
+
return rb;
|
|
243
|
+
}
|
|
244
|
+
const surrogate = document.createElement("span");
|
|
245
|
+
Array.from(node.childNodes).forEach((child) => {
|
|
246
|
+
if (child.nodeName !== "RT" && child.nodeName !== "RP") {
|
|
247
|
+
surrogate.appendChild(child.cloneNode(true));
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
return surrogate;
|
|
251
|
+
},
|
|
252
|
+
getAttrs: (node) => {
|
|
253
|
+
var _a;
|
|
254
|
+
const rt = node.querySelector("rt");
|
|
255
|
+
if (!rt) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
return { rt: (_a = rt.textContent) != null ? _a : "" };
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
];
|
|
262
|
+
},
|
|
263
|
+
renderHTML({ HTMLAttributes, mark }) {
|
|
264
|
+
return [
|
|
265
|
+
"ruby",
|
|
266
|
+
(0, import_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes),
|
|
267
|
+
["rb", 0],
|
|
268
|
+
...mark.attrs.rt == null ? [] : [["rt", { contenteditable: "false" }, mark.attrs.rt]]
|
|
269
|
+
];
|
|
270
|
+
},
|
|
271
|
+
addCommands() {
|
|
272
|
+
return {
|
|
273
|
+
setRubyText: (attributes) => ({ commands }) => {
|
|
274
|
+
return commands.setMark(this.name, attributes);
|
|
275
|
+
},
|
|
276
|
+
toggleRubyText: (attributes) => ({ commands }) => {
|
|
277
|
+
return commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true });
|
|
278
|
+
},
|
|
279
|
+
unsetRubyText: () => ({ commands }) => {
|
|
280
|
+
return commands.unsetMark(this.name, { extendEmptyMarkRange: true });
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
},
|
|
284
|
+
addProseMirrorPlugins() {
|
|
285
|
+
return [
|
|
286
|
+
RubyTextDecorationPlugin(this.type, {
|
|
287
|
+
editor: this.editor,
|
|
288
|
+
allowClickToEdit: this.options.allowClickToEdit,
|
|
289
|
+
renderAnnotationEditor: this.options.renderAnnotationEditor
|
|
290
|
+
})
|
|
291
|
+
];
|
|
292
|
+
},
|
|
293
|
+
addMarkView() {
|
|
294
|
+
return ({ HTMLAttributes }) => {
|
|
295
|
+
const ruby = document.createElement("ruby");
|
|
296
|
+
Object.entries((0, import_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes)).forEach(
|
|
297
|
+
([key, value]) => {
|
|
298
|
+
if (value != null) {
|
|
299
|
+
ruby.setAttribute(key, String(value));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
);
|
|
303
|
+
return {
|
|
304
|
+
dom: ruby,
|
|
305
|
+
contentDOM: ruby
|
|
306
|
+
};
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
// src/index.ts
|
|
312
|
+
var index_default = RubyText;
|
|
313
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
314
|
+
0 && (module.exports = {
|
|
315
|
+
RubyText
|
|
316
|
+
});
|
|
317
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/ruby-text.ts","../src/ruby-text-decoration-plugin.ts"],"sourcesContent":["import { RubyText } from './ruby-text.js'\n\nexport * from './ruby-text.js'\n\nexport default RubyText\n","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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAsC;;;ACEtC,mBAAiD;AACjD,kBAA2D;AAE3D,IAAM,8BAA8B,IAAI,uBAAU,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,sBAA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AAQ;","names":["_a"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Editor, Mark } from '@tiptap/core';
|
|
2
|
+
|
|
3
|
+
interface RubyTextAnnotationEditorProps {
|
|
4
|
+
/**
|
|
5
|
+
* The current annotation value (`''` when the mark has an empty annotation).
|
|
6
|
+
*/
|
|
7
|
+
annotation: string;
|
|
8
|
+
/**
|
|
9
|
+
* Applies the given value as the new annotation and closes the editor.
|
|
10
|
+
* Calling it more than once (or after `dismiss`) is a no-op.
|
|
11
|
+
*/
|
|
12
|
+
submit: (value: string) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Closes the editor without changing the annotation.
|
|
15
|
+
* Calling it more than once (or after `submit`) is a no-op.
|
|
16
|
+
*/
|
|
17
|
+
dismiss: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* The editor instance.
|
|
20
|
+
*/
|
|
21
|
+
editor: Editor;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface RubyTextOptions {
|
|
25
|
+
/**
|
|
26
|
+
* HTML attributes to add to the ruby element.
|
|
27
|
+
* @default {}
|
|
28
|
+
*/
|
|
29
|
+
HTMLAttributes: Record<string, any>;
|
|
30
|
+
/**
|
|
31
|
+
* Whether clicking an annotation opens an inline editor.
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
34
|
+
allowClickToEdit: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Renders a custom element used as the inline annotation editor.
|
|
37
|
+
* The returned element is mounted inside the `rt` element. A descendant
|
|
38
|
+
* with the `autofocus` attribute is focused, otherwise the element itself.
|
|
39
|
+
* When not set, a plain text input is rendered.
|
|
40
|
+
* @default undefined
|
|
41
|
+
*/
|
|
42
|
+
renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement;
|
|
43
|
+
}
|
|
44
|
+
interface RubyTextAttributes {
|
|
45
|
+
/**
|
|
46
|
+
* The ruby text annotation rendered in the HTML `rt` element.
|
|
47
|
+
*/
|
|
48
|
+
rt: string | null;
|
|
49
|
+
}
|
|
50
|
+
declare module '@tiptap/core' {
|
|
51
|
+
interface Commands<ReturnType> {
|
|
52
|
+
rubyText: {
|
|
53
|
+
/**
|
|
54
|
+
* Set a ruby text mark with an annotation on the current selection.
|
|
55
|
+
* @example editor.commands.setRubyText({ rt: 'かんじ' })
|
|
56
|
+
*/
|
|
57
|
+
setRubyText: (attributes: RubyTextAttributes) => ReturnType;
|
|
58
|
+
/**
|
|
59
|
+
* Toggle a ruby text mark on the current selection.
|
|
60
|
+
* @example editor.commands.toggleRubyText({ rt: 'かんじ' })
|
|
61
|
+
*/
|
|
62
|
+
toggleRubyText: (attributes: RubyTextAttributes) => ReturnType;
|
|
63
|
+
/**
|
|
64
|
+
* Remove the ruby text mark from the current selection.
|
|
65
|
+
* @example editor.commands.unsetRubyText()
|
|
66
|
+
*/
|
|
67
|
+
unsetRubyText: () => ReturnType;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* This extension adds support for HTML ruby text annotations.
|
|
73
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
|
|
74
|
+
*/
|
|
75
|
+
declare const RubyText: Mark<RubyTextOptions, any>;
|
|
76
|
+
|
|
77
|
+
export { RubyText, type RubyTextAnnotationEditorProps, type RubyTextAttributes, type RubyTextOptions, RubyText as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Editor, Mark } from '@tiptap/core';
|
|
2
|
+
|
|
3
|
+
interface RubyTextAnnotationEditorProps {
|
|
4
|
+
/**
|
|
5
|
+
* The current annotation value (`''` when the mark has an empty annotation).
|
|
6
|
+
*/
|
|
7
|
+
annotation: string;
|
|
8
|
+
/**
|
|
9
|
+
* Applies the given value as the new annotation and closes the editor.
|
|
10
|
+
* Calling it more than once (or after `dismiss`) is a no-op.
|
|
11
|
+
*/
|
|
12
|
+
submit: (value: string) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Closes the editor without changing the annotation.
|
|
15
|
+
* Calling it more than once (or after `submit`) is a no-op.
|
|
16
|
+
*/
|
|
17
|
+
dismiss: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* The editor instance.
|
|
20
|
+
*/
|
|
21
|
+
editor: Editor;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface RubyTextOptions {
|
|
25
|
+
/**
|
|
26
|
+
* HTML attributes to add to the ruby element.
|
|
27
|
+
* @default {}
|
|
28
|
+
*/
|
|
29
|
+
HTMLAttributes: Record<string, any>;
|
|
30
|
+
/**
|
|
31
|
+
* Whether clicking an annotation opens an inline editor.
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
34
|
+
allowClickToEdit: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Renders a custom element used as the inline annotation editor.
|
|
37
|
+
* The returned element is mounted inside the `rt` element. A descendant
|
|
38
|
+
* with the `autofocus` attribute is focused, otherwise the element itself.
|
|
39
|
+
* When not set, a plain text input is rendered.
|
|
40
|
+
* @default undefined
|
|
41
|
+
*/
|
|
42
|
+
renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement;
|
|
43
|
+
}
|
|
44
|
+
interface RubyTextAttributes {
|
|
45
|
+
/**
|
|
46
|
+
* The ruby text annotation rendered in the HTML `rt` element.
|
|
47
|
+
*/
|
|
48
|
+
rt: string | null;
|
|
49
|
+
}
|
|
50
|
+
declare module '@tiptap/core' {
|
|
51
|
+
interface Commands<ReturnType> {
|
|
52
|
+
rubyText: {
|
|
53
|
+
/**
|
|
54
|
+
* Set a ruby text mark with an annotation on the current selection.
|
|
55
|
+
* @example editor.commands.setRubyText({ rt: 'かんじ' })
|
|
56
|
+
*/
|
|
57
|
+
setRubyText: (attributes: RubyTextAttributes) => ReturnType;
|
|
58
|
+
/**
|
|
59
|
+
* Toggle a ruby text mark on the current selection.
|
|
60
|
+
* @example editor.commands.toggleRubyText({ rt: 'かんじ' })
|
|
61
|
+
*/
|
|
62
|
+
toggleRubyText: (attributes: RubyTextAttributes) => ReturnType;
|
|
63
|
+
/**
|
|
64
|
+
* Remove the ruby text mark from the current selection.
|
|
65
|
+
* @example editor.commands.unsetRubyText()
|
|
66
|
+
*/
|
|
67
|
+
unsetRubyText: () => ReturnType;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* This extension adds support for HTML ruby text annotations.
|
|
73
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
|
|
74
|
+
*/
|
|
75
|
+
declare const RubyText: Mark<RubyTextOptions, any>;
|
|
76
|
+
|
|
77
|
+
export { RubyText, type RubyTextAnnotationEditorProps, type RubyTextAttributes, type RubyTextOptions, RubyText as default };
|