The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { Editor, EditorEvents } from "@tiptap/core"; |
| 2 | import { |
| 3 | flatToMarkdown, |
| 4 | flatToPm, |
| 5 | markdownToFlat, |
| 6 | pmToFlat, |
| 7 | serializeEditorDoc, |
| 8 | } from "../composer-rich-text-doc"; |
| 9 | import { |
| 10 | type ComposerContextReference, |
| 11 | formatInlineContextReference, |
| 12 | inlineContextReferenceReplacement, |
| 13 | } from "../lib/composerContextReferences"; |
| 14 | import { markAsClipboardEdit } from "../composer-undo-grouping"; |
| 15 | |
| 16 | const INSERTION_ORDER_META = "composerAttachmentInsertionOrder"; |
| 17 | let nextInsertionOrder = 0; |
| 18 | |
| 19 | export function captureComposerAttachmentInsertion(editor: Editor) { |
| 20 | const order = nextInsertionOrder++; |
| 21 | let { from, to } = editor.state.selection; |
| 22 | let lost = false; |
| 23 | const track = ({ transaction, appendedTransactions }: EditorEvents["transaction"]) => { |
| 24 | for (const tr of [transaction, ...appendedTransactions]) { |
| 25 | // Stay before text typed at the paste point, but after an earlier capture's references. |
| 26 | const insertion: { order: number; from: number; end: number } | undefined = |
| 27 | tr.getMeta(INSERTION_ORDER_META); |
| 28 | const followsEarlier = insertion !== undefined && insertion.order < order; |
| 29 | const start = tr.mapping.mapResult(from, followsEarlier ? 1 : -1); |
| 30 | const end = tr.mapping.mapResult(to, followsEarlier ? 1 : -1); |
| 31 | // Replacing the following space maps its start left regardless of association. |
| 32 | if (followsEarlier && from === to && from === insertion.from) { |
| 33 | from = to = insertion.end; |
| 34 | continue; |
| 35 | } |
| 36 | // A replacement spanning the paste point (such as a controlled setContent) leaves no |
| 37 | // position to keep, so the caller falls back to the current caret. |
| 38 | lost ||= start.deletedAcross || end.deletedAcross; |
| 39 | from = start.pos; |
| 40 | to = start.deletedAfter || end.deletedBefore ? from : end.pos; |
| 41 | } |
| 42 | }; |
| 43 | editor.on("transaction", track); |
| 44 | const dispose = () => { |
| 45 | editor.off("transaction", track); |
| 46 | }; |
| 47 | return { |
| 48 | insert: (references: ReadonlyArray<ComposerContextReference>): boolean => { |
| 49 | dispose(); |
| 50 | if (lost || editor.isDestroyed || references.length === 0) return false; |
| 51 | const nodeType = editor.schema.nodes["composer-context-reference"]; |
| 52 | if (!nodeType) return false; |
| 53 | const map = serializeEditorDoc(editor.state.doc); |
| 54 | const edit = inlineContextReferenceReplacement( |
| 55 | map.value, |
| 56 | { |
| 57 | start: flatToMarkdown(map, pmToFlat(map, from)), |
| 58 | end: flatToMarkdown(map, pmToFlat(map, to)), |
| 59 | }, |
| 60 | references, |
| 61 | ); |
| 62 | const acceptsReference = |
| 63 | editor.state.doc.resolve(from).parent.type.contentMatch.matchType(nodeType) !== null; |
| 64 | const content = acceptsReference |
| 65 | ? references.flatMap((reference) => [ |
| 66 | nodeType.create({ ...reference, source: formatInlineContextReference(reference) }), |
| 67 | editor.schema.text(" "), |
| 68 | ]) |
| 69 | : [editor.schema.text(edit.text)]; |
| 70 | if (acceptsReference && edit.text.startsWith(" ")) content.unshift(editor.schema.text(" ")); |
| 71 | const replaceEnd = flatToPm(map, markdownToFlat(map, edit.end)); |
| 72 | const tr = editor.state.tr.replaceWith(from, replaceEnd, content); |
| 73 | editor.view.dispatch( |
| 74 | markAsClipboardEdit(tr, "paste").setMeta(INSERTION_ORDER_META, { |
| 75 | order, |
| 76 | from, |
| 77 | end: tr.mapping.map(replaceEnd, 1), |
| 78 | }), |
| 79 | ); |
| 80 | return true; |
| 81 | }, |
| 82 | dispose, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | export type ComposerAttachmentInsertion = ReturnType<typeof captureComposerAttachmentInsertion>; |