@tiptap/extension-ruby-text 3.30.2 → 3.30.4

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.
@@ -1 +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,sBAAsB;AAEvD,QAAM,iBAAiB,SAAS,MAAM,MAAM,OAAO,GAAG,EAAE,MAAM,KAAK,CAAC;AACpE,QAAM,iBAAiB,QAAQ,MAAM,QAAQ,CAAC;AAC9C,QAAM,iBAAiB,WAAW,WAAS;AACzC,QAAI,WAAW,KAAK,GAAG;AACrB;AAAA,IACF;AAEA,QAAI,MA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;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,aACE,gBACA,CAAC,EAAE,SAAS,MAAM;AAChB,eAAO,SAAS,QAAQ,KAAK,MAAM,UAAU;AAAA,MAC/C;AAAA,MACF,gBACE,gBACA,CAAC,EAAE,SAAS,MAAM;AAChB,eAAO,SAAS,WAAW,KAAK,MAAM,YAAY,EAAE,sBAAsB,KAAK,CAAC;AAAA,MAClF;AAAA,MACF,eACE,MACA,CAAC,EAAE,SAAS,MAAM;AAChB,eAAO,SAAS,UAAU,KAAK,MAAM,EAAE,sBAAsB,KAAK,CAAC;AAAA,MACrE;AAAA,IACJ;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,WAAO;AAAA,MACL,yBAAyB,KAAK,MAAM;AAAA,QAClC,QAAQ,KAAK;AAAA,QACb,kBAAkB,KAAK,QAAQ;AAAA,QAC/B,wBAAwB,KAAK,QAAQ;AAAA,MACvC,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEA,cAAc;AACZ,WAAO,CAAC,EAAE,eAAe,MAAM;AAC7B,YAAM,OAAO,SAAS,cAAc,MAAM;AAE1C,aAAO,YAAQ,6BAAgB,KAAK,QAAQ,gBAAgB,cAAc,CAAC,EAAE;AAAA,QAC3E,CAAC,CAAC,KAAK,KAAK,MAAM;AAChB,cAAI,SAAS,MAAM;AACjB,iBAAK,aAAa,KAAK,OAAO,KAAK,CAAC;AAAA,UACtC;AAAA,QACF;AAAA,MACF;AAEA,aAAO;AAAA,QACL,KAAK;AAAA,QACL,YAAY;AAAA,MACd;AAAA,IACF;AAAA,EACF;AACF,CAAC;;;ADzKD,IAAO,gBAAQ;","names":["_a"]}
1
+ {"version":3,"file":"index.cjs","names":["PluginKey","TextSelection","Decoration","DecorationSet","Plugin","Mark","mergeAttributes"],"sources":["../src/ruby-text-decoration-plugin.ts","../src/ruby-text.ts","../src/index.ts"],"sourcesContent":["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 { 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 { RubyText } from './ruby-text.js'\n\nexport * from './ruby-text.js'\n\nexport default RubyText\n"],"mappings":";;;;;;;;AAKA,MAAM,8BAA8B,IAAIA,iBAAAA,UAAU,oBAAoB;AAGtE,MAAM,+BAAe,IAAI,QAA0B;AAqCnD,SAAS,WAAW,OAAsB;CACxC,MAAM,gBAAiB,MAA+B;CACtD,OAAO,MAAM,eAAe,kBAAkB;AAChD;;AAGA,SAAS,8BAA8B,EACrC,YACA,QACA,WACgC;CAChC,MAAM,QAAQ,SAAS,cAAc,OAAO;CAC5C,MAAM,OAAO;CACb,MAAM,QAAQ;CACd,MAAM,OAAO,KAAK,IAAI,WAAW,QAAQ,CAAC;CAC1C,MAAM,aAAa,cAAc,sBAAsB;CAEvD,MAAM,iBAAiB,eAAe,MAAM,OAAO,GAAG,EAAE,MAAM,KAAK,CAAC;CACpE,MAAM,iBAAiB,cAAc,QAAQ,CAAC;CAC9C,MAAM,iBAAiB,YAAW,UAAS;EACzC,IAAI,WAAW,KAAK,GAClB;EAGF,IAAI,MAAM,QAAQ,UAAU;GAC1B,MAAM,eAAe;GACrB,QAAQ;EACV;EAEA,IAAI,MAAM,QAAQ,SAAS;GACzB,MAAM,eAAe;GACrB,OAAO,MAAM,KAAK;EACpB;CACF,CAAC;CAED,OAAO;AACT;AAEA,SAAS,gBACP,YACA,OACA,cACA,MACA,SACA;CACA,MAAM,KAAK,SAAS,cAAc,IAAI;CACtC,GAAG,kBAAkB;CACrB,GAAG,cAAc;CACjB,IAAI,UAAU;CAEd,MAAM,2BAA2B;EAC/B,KAAK,SAAS,KAAK,MAAM,GAAG,aAAaC,iBAAAA,cAAc,OAAO,KAAK,MAAM,KAAK,MAAM,EAAE,CAAC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BAAe,GAAG;GACpC;EACF,CACF;CACF;CAEA,WAAW,EAAE,gBAAgB,QAAQ;EACnC,OAAO;GACL;IACAC,GAAAA,aAAAA,gBAAAA,CAAgB,KAAK,QAAQ,gBAAgB,cAAc;GAC3D,CAAC,MAAM,CAAC;GACR,GAAI,KAAK,MAAM,MAAM,OAAO,CAAC,IAAI,CAAC;IAAC;IAAM,EAAE,iBAAiB,QAAQ;IAAG,KAAK,MAAM;GAAE,CAAC;EACvF;CACF;CAEA,cAAc;EACZ,OAAO;GACL,cACE,gBACC,EAAE,eAAe;IAChB,OAAO,SAAS,QAAQ,KAAK,MAAM,UAAU;GAC/C;GACF,iBACE,gBACC,EAAE,eAAe;IAChB,OAAO,SAAS,WAAW,KAAK,MAAM,YAAY,EAAE,sBAAsB,KAAK,CAAC;GAClF;GACF,sBAEG,EAAE,eAAe;IAChB,OAAO,SAAS,UAAU,KAAK,MAAM,EAAE,sBAAsB,KAAK,CAAC;GACrE;EACJ;CACF;CAEA,wBAAwB;EACtB,OAAO,CACL,yBAAyB,KAAK,MAAM;GAClC,QAAQ,KAAK;GACb,kBAAkB,KAAK,QAAQ;GAC/B,wBAAwB,KAAK,QAAQ;EACvC,CAAC,CACH;CACF;CAEA,cAAc;EACZ,QAAQ,EAAE,qBAAqB;GAC7B,MAAM,OAAO,SAAS,cAAc,MAAM;GAE1C,OAAO,SAAA,GAAQA,aAAAA,gBAAAA,CAAgB,KAAK,QAAQ,gBAAgB,cAAc,CAAC,CAAC,CAAC,SAC1E,CAAC,KAAK,WAAW;IAChB,IAAI,SAAS,MACX,KAAK,aAAa,KAAK,OAAO,KAAK,CAAC;GAExC,CACF;GAEA,OAAO;IACL,KAAK;IACL,YAAY;GACd;EACF;CACF;AACF,CAAC;;;ACzKD,IAAA,cAAe"}
package/dist/index.d.cts CHANGED
@@ -1,77 +1,82 @@
1
- import { Editor, Mark } from '@tiptap/core';
2
-
1
+ import { Editor, Mark } from "@tiptap/core";
2
+ import "@tiptap/pm/model";
3
+ import "@tiptap/pm/state";
4
+ import "@tiptap/pm/view";
5
+ //#region src/ruby-text-decoration-plugin.d.ts
3
6
  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;
7
+ /**
8
+ * The current annotation value (`''` when the mark has an empty annotation).
9
+ */
10
+ annotation: string;
11
+ /**
12
+ * Applies the given value as the new annotation and closes the editor.
13
+ * Calling it more than once (or after `dismiss`) is a no-op.
14
+ */
15
+ submit: (value: string) => void;
16
+ /**
17
+ * Closes the editor without changing the annotation.
18
+ * Calling it more than once (or after `submit`) is a no-op.
19
+ */
20
+ dismiss: () => void;
21
+ /**
22
+ * The editor instance.
23
+ */
24
+ editor: Editor;
22
25
  }
23
-
26
+ //#endregion
27
+ //#region src/ruby-text.d.ts
24
28
  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;
29
+ /**
30
+ * HTML attributes to add to the ruby element.
31
+ * @default {}
32
+ */
33
+ HTMLAttributes: Record<string, any>;
34
+ /**
35
+ * Whether clicking an annotation opens an inline editor.
36
+ * @default true
37
+ */
38
+ allowClickToEdit: boolean;
39
+ /**
40
+ * Renders a custom element used as the inline annotation editor.
41
+ * The returned element is mounted inside the `rt` element. A descendant
42
+ * with the `autofocus` attribute is focused, otherwise the element itself.
43
+ * When not set, a plain text input is rendered.
44
+ * @default undefined
45
+ */
46
+ renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement;
43
47
  }
44
48
  interface RubyTextAttributes {
45
- /**
46
- * The ruby text annotation rendered in the HTML `rt` element.
47
- */
48
- rt: string | null;
49
+ /**
50
+ * The ruby text annotation rendered in the HTML `rt` element.
51
+ */
52
+ rt: string | null;
49
53
  }
50
54
  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
- }
55
+ interface Commands<ReturnType> {
56
+ rubyText: {
57
+ /**
58
+ * Set a ruby text mark with an annotation on the current selection.
59
+ * @example editor.commands.setRubyText({ rt: 'かんじ' })
60
+ */
61
+ setRubyText: (attributes: RubyTextAttributes) => ReturnType;
62
+ /**
63
+ * Toggle a ruby text mark on the current selection.
64
+ * @example editor.commands.toggleRubyText({ rt: 'かんじ' })
65
+ */
66
+ toggleRubyText: (attributes: RubyTextAttributes) => ReturnType;
67
+ /**
68
+ * Remove the ruby text mark from the current selection.
69
+ * @example editor.commands.unsetRubyText()
70
+ */
71
+ unsetRubyText: () => ReturnType;
72
+ };
73
+ }
70
74
  }
71
75
  /**
72
76
  * This extension adds support for HTML ruby text annotations.
73
77
  * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
74
78
  */
75
79
  declare const RubyText: Mark<RubyTextOptions, any>;
76
-
77
- export { RubyText, type RubyTextAnnotationEditorProps, type RubyTextAttributes, type RubyTextOptions, RubyText as default };
80
+ //#endregion
81
+ export { RubyText, RubyText as default, type RubyTextAnnotationEditorProps, RubyTextAttributes, RubyTextOptions };
82
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/ruby-text-decoration-plugin.ts","../src/ruby-text.ts"],"mappings":";;;;;UAgBiB;;;;EAIf;;;;;EAKA,SAAS;;;;;EAKT;;;;EAIA,QAAQ;;;;UC3BO;;;;;EAKf,gBAAgB;;;;;EAKhB;;;;;;;;EAQA,0BAA0B,OAAO,kCAAkC;;UAGpD;;;;EAIf;;;YAIU,SAAS;IACjB;;;;;MAKE,cAAc,YAAY,uBAAuB;;;;;MAKjD,iBAAiB,YAAY,uBAAuB;;;;;MAKpD,qBAAqB;;;;;;;;cASd,UAAQ,KAAA"}
package/dist/index.d.ts CHANGED
@@ -1,77 +1,82 @@
1
- import { Editor, Mark } from '@tiptap/core';
2
-
1
+ import { Editor, Mark } from "@tiptap/core";
2
+ import { Plugin } from "@tiptap/pm/state";
3
+ import { DecorationSet } from "@tiptap/pm/view";
4
+ import "@tiptap/pm/model";
5
+ //#region src/ruby-text-decoration-plugin.d.ts
3
6
  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;
7
+ /**
8
+ * The current annotation value (`''` when the mark has an empty annotation).
9
+ */
10
+ annotation: string;
11
+ /**
12
+ * Applies the given value as the new annotation and closes the editor.
13
+ * Calling it more than once (or after `dismiss`) is a no-op.
14
+ */
15
+ submit: (value: string) => void;
16
+ /**
17
+ * Closes the editor without changing the annotation.
18
+ * Calling it more than once (or after `submit`) is a no-op.
19
+ */
20
+ dismiss: () => void;
21
+ /**
22
+ * The editor instance.
23
+ */
24
+ editor: Editor;
22
25
  }
23
-
26
+ //#endregion
27
+ //#region src/ruby-text.d.ts
24
28
  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;
29
+ /**
30
+ * HTML attributes to add to the ruby element.
31
+ * @default {}
32
+ */
33
+ HTMLAttributes: Record<string, any>;
34
+ /**
35
+ * Whether clicking an annotation opens an inline editor.
36
+ * @default true
37
+ */
38
+ allowClickToEdit: boolean;
39
+ /**
40
+ * Renders a custom element used as the inline annotation editor.
41
+ * The returned element is mounted inside the `rt` element. A descendant
42
+ * with the `autofocus` attribute is focused, otherwise the element itself.
43
+ * When not set, a plain text input is rendered.
44
+ * @default undefined
45
+ */
46
+ renderAnnotationEditor?: (props: RubyTextAnnotationEditorProps) => HTMLElement;
43
47
  }
44
48
  interface RubyTextAttributes {
45
- /**
46
- * The ruby text annotation rendered in the HTML `rt` element.
47
- */
48
- rt: string | null;
49
+ /**
50
+ * The ruby text annotation rendered in the HTML `rt` element.
51
+ */
52
+ rt: string | null;
49
53
  }
50
54
  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
- }
55
+ interface Commands<ReturnType> {
56
+ rubyText: {
57
+ /**
58
+ * Set a ruby text mark with an annotation on the current selection.
59
+ * @example editor.commands.setRubyText({ rt: 'かんじ' })
60
+ */
61
+ setRubyText: (attributes: RubyTextAttributes) => ReturnType;
62
+ /**
63
+ * Toggle a ruby text mark on the current selection.
64
+ * @example editor.commands.toggleRubyText({ rt: 'かんじ' })
65
+ */
66
+ toggleRubyText: (attributes: RubyTextAttributes) => ReturnType;
67
+ /**
68
+ * Remove the ruby text mark from the current selection.
69
+ * @example editor.commands.unsetRubyText()
70
+ */
71
+ unsetRubyText: () => ReturnType;
72
+ };
73
+ }
70
74
  }
71
75
  /**
72
76
  * This extension adds support for HTML ruby text annotations.
73
77
  * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
74
78
  */
75
79
  declare const RubyText: Mark<RubyTextOptions, any>;
76
-
77
- export { RubyText, type RubyTextAnnotationEditorProps, type RubyTextAttributes, type RubyTextOptions, RubyText as default };
80
+ //#endregion
81
+ export { RubyText, RubyText as default, type RubyTextAnnotationEditorProps, RubyTextAttributes, RubyTextOptions };
82
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/ruby-text-decoration-plugin.ts","../src/ruby-text.ts"],"mappings":";;;;;UAgBiB;;;;EAIf;;;;;EAKA,SAAS;;;;;EAKT;;;;EAIA,QAAQ;;;;UC3BO;;;;;EAKf,gBAAgB;;;;;EAKhB;;;;;;;;EAQA,0BAA0B,OAAO,kCAAkC;;UAGpD;;;;EAIf;;;YAIU,SAAS;IACjB;;;;;MAKE,cAAc,YAAY,uBAAuB;;;;;MAKjD,iBAAiB,YAAY,uBAAuB;;;;;MAKpD,qBAAqB;;;;;;;;cASd,UAAQ,KAAA"}