@@ -12,7 +12,6 @@ import {
1212} from "@tiptap/core" ;
1313import { ReactNodeViewRenderer , NodeViewWrapper , type NodeViewProps } from "@tiptap/react" ;
1414import StarterKit from "@tiptap/starter-kit" ;
15- import { type Node as ProseMirrorNode } from "@tiptap/pm/model" ;
1615import { newlineInCode , splitBlockKeepMarks } from "@tiptap/pm/commands" ;
1716import { isHistoryTransaction } from "@tiptap/pm/history" ;
1817import {
@@ -59,6 +58,11 @@ import {
5958} from "~/composer-logic" ;
6059import { useStableCallback } from "~/hooks/useStableCallback" ;
6160import { caretLineDirection , ComposerTextDirectionExtension } from "~/composer-text-direction" ;
61+ import {
62+ collapseShownMarkdownSource ,
63+ ComposerMarkdownSourceExtension ,
64+ } from "~/composer-markdown-source" ;
65+ import { MARKDOWN_SOURCE_MARK } from "~/composer-rich-text" ;
6266import {
6367 collectComposerPromptInlineTokens ,
6468 selectionTouchesMentionBoundary ,
@@ -69,7 +73,6 @@ import {
6973 ComposerCodeBlockExtension ,
7074 ComposerListExtensions ,
7175 buildTiptapContent ,
72- caretTakesMarksBefore ,
7376 ComposerBoldSelectionOnlyExtension ,
7477 convertBulletItemToTask ,
7578 ComposerCodeExtension ,
@@ -524,46 +527,6 @@ function ComposerContextReferenceNodeView({ node }: NodeViewProps) {
524527 ) ;
525528}
526529
527- type StyledRange = {
528- from : number ;
529- to : number ;
530- markers : { at : number ; side : number ; text : string } [ ] ;
531- } ;
532-
533- function collectStyledRanges ( doc : ProseMirrorNode ) : StyledRange [ ] {
534- const ranges : StyledRange [ ] = [ ] ;
535- const map = serializeEditorDoc ( doc ) ;
536- let range : StyledRange | null = null ;
537- let openLength = 0 ;
538- for ( const run of map . runs ) {
539- if ( run . nodeName === "codeBlock" ) continue ;
540- if ( run . openLen > 0 ) {
541- range ??= { from : run . pmPos , to : run . pmPos , markers : [ ] } ;
542- range . markers . push ( {
543- at : run . pmPos ,
544- side : - 1 ,
545- text : map . value . slice ( run . mdStart , run . mdStart + run . openLen ) ,
546- } ) ;
547- }
548- if ( range === null ) continue ;
549- range . to = run . pmPos + run . docLen ;
550- if ( run . closeLen > 0 ) {
551- const end = run . mdStart + run . mdLen ;
552- range . markers . push ( {
553- at : range . to ,
554- side : - 2 ,
555- text : map . value . slice ( end - run . closeLen , end ) ,
556- } ) ;
557- }
558- openLength += run . openLen - run . closeLen ;
559- if ( openLength === 0 ) {
560- ranges . push ( range ) ;
561- range = null ;
562- }
563- }
564- return ranges ;
565- }
566-
567530/** Wait for a character after '- ' so task markers reach their input rule before becoming bullets. */
568531function listMarkerInputRule ( find : RegExp , listType : "bulletList" | "orderedList" ) : InputRule {
569532 return new InputRule ( {
@@ -684,75 +647,41 @@ function isInCodeBlock(view: EditorView): boolean {
684647 return view . state . selection . $from . parent . type . spec . code === true ;
685648}
686649
687- const MarkerPluginKey = new PluginKey ( "composer-rich-markers " ) ;
650+ const ChipSelectionPluginKey = new PluginKey < DecorationSet > ( "composer-chip-range-selection " ) ;
688651
689- const ComposerMarkerPlugin = new Plugin ( {
690- key : MarkerPluginKey ,
691- state : {
692- init : ( _ , state ) => decorationsForSelection ( state ) ,
693- apply : ( tr , old , _ , state ) =>
694- tr . docChanged || tr . selectionSet || tr . storedMarksSet ? decorationsForSelection ( state ) : old ,
695- } ,
696- props : {
697- decorations ( state ) {
698- return MarkerPluginKey . getState ( state ) ;
699- } ,
652+ /** Highlights chips inside a text selection; their node views draw no native highlight. */
653+ const ComposerChipSelectionExtension = Extension . create ( {
654+ name : "composer-chip-range-selection" ,
655+ addProseMirrorPlugins ( ) {
656+ return [
657+ new Plugin < DecorationSet > ( {
658+ key : ChipSelectionPluginKey ,
659+ state : {
660+ init : ( _ , state ) => chipSelectionDecorations ( state ) ,
661+ apply : ( tr , old , _ , state ) =>
662+ tr . docChanged || tr . selectionSet ? chipSelectionDecorations ( state ) : old ,
663+ } ,
664+ props : {
665+ decorations : ( state ) => ChipSelectionPluginKey . getState ( state ) ,
666+ } ,
667+ } ) ,
668+ ] ;
700669 } ,
701670} ) ;
702671
703- function decorationsForSelection ( state : EditorState ) : DecorationSet {
704- const { doc, selection } = state ;
705- const caretSide = caretTakesMarksBefore ( state ) ? 3 : 0 ;
706- const rtl = caretLineDirection ( state ) === "rtl" ;
672+ function chipSelectionDecorations ( { doc, selection } : EditorState ) : DecorationSet {
673+ if ( selection . empty ) return DecorationSet . empty ;
707674 const decorations : Decoration [ ] = [ ] ;
708- if ( ! selection . empty ) {
709- doc . nodesBetween ( selection . from , selection . to , ( node , pos ) => {
710- if ( node . type . name . startsWith ( "composer-" ) ) {
711- decorations . push (
712- Decoration . node ( pos , pos + node . nodeSize , { class : "composer-chip-range-selected" } ) ,
713- ) ;
714- return false ;
715- }
716- return true ;
717- } ) ;
718- }
719- for ( const range of collectStyledRanges ( doc ) ) {
720- const active = selection . empty
721- ? selection . from >= range . from && selection . from <= range . to
722- : selection . from < range . to && selection . to > range . from ;
723- if ( ! active ) continue ;
724- for ( const { at, side : baseSide , text } of range . markers ) {
725- const side = selection . empty && at === selection . from ? baseSide + caretSide : baseSide ;
726- const marker = document . createElement ( "span" ) ;
727- marker . className = "composer-rich-marker" ;
728- marker . textContent = text ;
729- marker . setAttribute ( "aria-hidden" , "true" ) ;
730- // Inline code is isolated left to right. In a right-to-left line its
731- // opening backtick joins the closing one inside the isolate, or the line
732- // would put both on the same side of the code.
733- const codeMarks =
734- rtl && baseSide === - 1 && / ^ ` + $ / . test ( text )
735- ? doc . resolve ( at ) . nodeAfter ?. marks . filter ( ( mark ) => mark . type . spec . code )
736- : undefined ;
737- decorations . push (
738- Decoration . widget ( at , marker , {
739- side,
740- key : `marker-${ at } -${ side } -${ marker . textContent } ` ,
741- ...( codeMarks ?. length ? { marks : codeMarks } : { } ) ,
742- } ) ,
743- ) ;
744- }
745- }
675+ doc . nodesBetween ( selection . from , selection . to , ( node , pos ) => {
676+ if ( ! node . type . name . startsWith ( "composer-" ) ) return true ;
677+ decorations . push (
678+ Decoration . node ( pos , pos + node . nodeSize , { class : "composer-chip-range-selected" } ) ,
679+ ) ;
680+ return false ;
681+ } ) ;
746682 return DecorationSet . create ( doc , decorations ) ;
747683}
748684
749- const ComposerMarkersExtension = Extension . create ( {
750- name : "composer-rich-markers" ,
751- addProseMirrorPlugins ( ) {
752- return [ ComposerMarkerPlugin ] ;
753- } ,
754- } ) ;
755-
756685type TiptapEditor = NonNullable < ReturnType < typeof useEditor > > ;
757686
758687function replaceSkillChips (
@@ -842,9 +771,10 @@ function createComposerExtensions(richText: boolean) {
842771 ComposerSkillExtension ,
843772 ComposerCitationExtension ,
844773 ComposerContextReferenceExtension ,
845- ComposerMarkersExtension ,
774+ ComposerChipSelectionExtension ,
846775 ...( richText
847776 ? [
777+ ComposerMarkdownSourceExtension ,
848778 ComposerBoldSelectionOnlyExtension ,
849779 ComposerItalicExtension ,
850780 ComposerBoldExtension ,
@@ -1291,7 +1221,11 @@ function ComposerPromptEditorTiptapInner(
12911221 return true ;
12921222 }
12931223 event . preventDefault ( ) ;
1294- if ( ( isTaskItem || isListItem ) && instance && splitOrLiftListItem ( instance ) ) {
1224+ if (
1225+ ( isTaskItem || isListItem ) &&
1226+ instance &&
1227+ splitOrLiftListItem ( instance , collapseShownMarkdownSource )
1228+ ) {
12951229 return true ;
12961230 }
12971231 if (
@@ -1303,6 +1237,15 @@ function ComposerPromptEditorTiptapInner(
13031237 ) {
13041238 return true ;
13051239 }
1240+ // Parse shown Markdown first, or the split leaves each half with one delimiter.
1241+ if ( instance ) {
1242+ return instance
1243+ . chain ( )
1244+ . command ( collapseShownMarkdownSource )
1245+ . command ( ( { state, dispatch } ) => splitBlockKeepMarks ( state , dispatch ) )
1246+ . scrollIntoView ( )
1247+ . run ( ) ;
1248+ }
13061249 return splitBlockKeepMarks ( view . state , ( tr ) => {
13071250 view . dispatch ( tr . scrollIntoView ( ) ) ;
13081251 } ) ;
@@ -1330,11 +1273,12 @@ function ComposerPromptEditorTiptapInner(
13301273 return false ;
13311274 }
13321275 // Do not wrap chips, atoms, or styled text; default replacement preserves marks.
1276+ // Shown Markdown source is plain text, so typed delimiters wrap it.
13331277 let touchesSpecial = false ;
13341278 view . state . doc . nodesBetween ( from , to , ( node ) => {
13351279 if (
13361280 ( node . isAtom && node . isInline && ! node . isText ) ||
1337- ( node . isText && node . marks . length > 0 )
1281+ ( node . isText && node . marks . some ( ( mark ) => mark . type . name !== MARKDOWN_SOURCE_MARK ) )
13381282 ) {
13391283 touchesSpecial = true ;
13401284 return false ;
@@ -1368,6 +1312,22 @@ function ComposerPromptEditorTiptapInner(
13681312 ) ;
13691313 return true ;
13701314 }
1315+ // Shown Markdown source is parsed when the caret leaves, so paste one line into it as text.
1316+ const sourceType = view . state . schema . marks [ MARKDOWN_SOURCE_MARK ] ;
1317+ const { from : pasteFrom , to : pasteTo , $from : $pasteFrom } = view . state . selection ;
1318+ if (
1319+ sourceType ?. isInSet ( $pasteFrom . marks ( ) ) &&
1320+ ! pastedText . includes ( "\n" ) &&
1321+ collectComposerPromptInlineTokens ( `${ pastedText } \n` ) . length === 0
1322+ ) {
1323+ view . dispatch (
1324+ markAsClipboardEdit (
1325+ view . state . tr . insertText ( pastedText , pasteFrom , pasteTo ) ,
1326+ "paste" ,
1327+ ) . scrollIntoView ( ) ,
1328+ ) ;
1329+ return true ;
1330+ }
13711331 const importFragment = readCallbacks ( ) . importContextFragment ;
13721332 let text =
13731333 ! literalText && importFragment
@@ -1408,6 +1368,8 @@ function ComposerPromptEditorTiptapInner(
14081368 markAsClipboardEdit ( tr , "paste" ) ;
14091369 return true ;
14101370 } )
1371+ // Pasted chips and lines would split shown Markdown, so parse it first.
1372+ . command ( collapseShownMarkdownSource )
14111373 . insertContent ( content )
14121374 . command ( ( { tr } ) => {
14131375 // Move after a pasted rule so the next keystroke cannot replace it; add a trailing line
@@ -1562,9 +1524,8 @@ function ComposerPromptEditorTiptapInner(
15621524 } ,
15631525 ) ;
15641526 }
1527+ selectMarkdownOffset ( editor , normalizedExpandedCursor ) ;
15651528 const map = serializeEditorDoc ( editor . state . doc ) ;
1566- const flat = markdownToFlat ( map , normalizedExpandedCursor ) ;
1567- editor . commands . setTextSelection ( flatToPm ( map , flat ) ) ;
15681529 if ( isFocused ) scrollTiptapCaretIntoView ( editor ) ;
15691530 if ( pendingCitation ) {
15701531 citationRequestRef . current = null ;
@@ -1633,8 +1594,7 @@ function ComposerPromptEditorTiptapInner(
16331594 const boundedCursor = clampEditorCursor ( snapshotRef . current . value , nextCursor ) ;
16341595 const expandedCursor =
16351596 exactExpandedCursor ?? expandEditorCursor ( snapshotRef . current . value , boundedCursor ) ;
1636- const map = serializeEditorDoc ( editor . state . doc ) ;
1637- editor . commands . setTextSelection ( flatToPm ( map , markdownToFlat ( map , expandedCursor ) ) ) ;
1597+ selectMarkdownOffset ( editor , expandedCursor ) ;
16381598 scrollTiptapCaretIntoView ( editor ) ;
16391599 if ( boundedCursor === snapshotRef . current . cursor ) return ;
16401600 snapshotRef . current = {
@@ -1852,6 +1812,17 @@ function insertMarkdownParagraphs(
18521812 insertContent ( blocks as JSONContent [ ] ) ;
18531813}
18541814
1815+ /** Placing the caret can show Markdown markers, so place it again in the shown source. */
1816+ function selectMarkdownOffset ( editor : TiptapEditor , offset : number ) : void {
1817+ for ( let pass = 0 ; pass < 2 ; pass += 1 ) {
1818+ const map = serializeEditorDoc ( editor . state . doc ) ;
1819+ const pos = flatToPm ( map , markdownToFlat ( map , offset ) ) ;
1820+ const { selection } = editor . state ;
1821+ if ( pass > 0 && selection . empty && selection . from === pos ) return ;
1822+ editor . commands . setTextSelection ( pos ) ;
1823+ }
1824+ }
1825+
18551826/** Native scrolling follows only real input; explicitly reveal programmatically placed carets. */
18561827function scrollTiptapCaretIntoView ( editor : TiptapEditor ) : void {
18571828 editor . view . dispatch ( editor . state . tr . scrollIntoView ( ) ) ;
0 commit comments