|
| 1 | +import type React from 'react'; |
| 2 | + |
| 3 | +import type {Transaction} from 'prosemirror-state'; |
| 4 | +import {TextSelection} from 'prosemirror-state'; |
| 5 | +import type {EditorView} from 'prosemirror-view'; |
| 6 | + |
| 7 | +import type {ExtensionDeps} from '#core'; |
| 8 | +import { |
| 9 | + LinkAttr, |
| 10 | + ReactWidgetDescriptor, |
| 11 | + linkType, |
| 12 | + normalizeUrlFactory, |
| 13 | + pType, |
| 14 | + removeDecoration, |
| 15 | +} from 'src/extensions'; |
| 16 | +import { |
| 17 | + LinkPlaceholderWidget, |
| 18 | + type LinkPlaceholderWidgetProps, |
| 19 | +} from 'src/extensions/markdown/Link/PlaceholderWidget/widget'; |
| 20 | +import {isTextSelection} from 'src/utils'; |
| 21 | + |
| 22 | +export class QuoteLinkWidgetDescriptor extends ReactWidgetDescriptor { |
| 23 | + #domElem; |
| 24 | + #view?: EditorView; |
| 25 | + #getPos?: () => number; |
| 26 | + #schema?: ExtensionDeps['schema']; |
| 27 | + |
| 28 | + private normalizeUrl; |
| 29 | + |
| 30 | + constructor(initPos: number, deps: ExtensionDeps) { |
| 31 | + super(initPos, 'link-empty'); |
| 32 | + this.#domElem = document.createElement('span'); |
| 33 | + this.#schema = deps.schema; |
| 34 | + this.normalizeUrl = normalizeUrlFactory(deps); |
| 35 | + } |
| 36 | + |
| 37 | + getDomElem(): HTMLElement { |
| 38 | + return this.#domElem; |
| 39 | + } |
| 40 | + |
| 41 | + renderReactElement(view: EditorView, getPos: () => number): React.ReactElement { |
| 42 | + this.#view = view; |
| 43 | + this.#getPos = getPos; |
| 44 | + return <LinkPlaceholderWidget onCancel={this.onCancel} onSubmit={this.onSubmit} />; |
| 45 | + } |
| 46 | + |
| 47 | + onCancel: LinkPlaceholderWidgetProps['onCancel'] = () => { |
| 48 | + if (!this.#view) return; |
| 49 | + |
| 50 | + this.#view.dispatch(removeDecoration(this.#view.state.tr, this.id)); |
| 51 | + this.#view.focus(); |
| 52 | + }; |
| 53 | + |
| 54 | + onSubmit: LinkPlaceholderWidgetProps['onSubmit'] = (params) => { |
| 55 | + const normalizeResult = this.normalizeUrl(params.url); |
| 56 | + if (!normalizeResult || !this.#view || !this.#getPos) return; |
| 57 | + |
| 58 | + let tr = this.#view.state.tr; |
| 59 | + |
| 60 | + const {url} = normalizeResult; |
| 61 | + const text = params.text.trim() || normalizeResult.text; |
| 62 | + |
| 63 | + const from = this.#getPos(); |
| 64 | + const isAllSelected = |
| 65 | + from === 1 && (!isTextSelection(tr.selection) || !tr.selection.$cursor); |
| 66 | + const to = from + text.length + (isAllSelected ? 1 : 0); |
| 67 | + |
| 68 | + tr = tr.insertText(text, from); |
| 69 | + tr = tr.addMark( |
| 70 | + from, |
| 71 | + to, |
| 72 | + linkType(this.#view.state.schema).create({ |
| 73 | + [LinkAttr.Href]: url, |
| 74 | + [LinkAttr.DataQuoteLink]: true, |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + tr = removeDecoration(tr, this.id); |
| 79 | + |
| 80 | + tr = tr.insert( |
| 81 | + to, |
| 82 | + pType(this.#view.state.schema).create(null, text ? this.#schema?.text(text) : null), |
| 83 | + ); |
| 84 | + tr.setSelection(TextSelection.create(tr.doc, to + 1 + text.length + 1)); |
| 85 | + |
| 86 | + this.#view.dispatch(tr); |
| 87 | + this.#view.focus(); |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +export const addPlaceholder = (tr: Transaction, deps: ExtensionDeps) => { |
| 92 | + const isAllSelected = |
| 93 | + tr.selection.from === 0 && (!isTextSelection(tr.selection) || !tr.selection.$cursor); |
| 94 | + return new QuoteLinkWidgetDescriptor(tr.selection.from + (isAllSelected ? 1 : 0), deps).applyTo( |
| 95 | + tr, |
| 96 | + ); |
| 97 | +}; |
0 commit comments