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