@@ -252,6 +252,31 @@ Delta _lineContentToDelta(String lineContent) {
252252 return delta;
253253}
254254
255+ void _pasteMarkdown (EditorState editorState, String markdown) {
256+ final selection =
257+ editorState.service.selectionService.currentSelection.value? .normalized;
258+ if (selection == null ) {
259+ return ;
260+ }
261+
262+ final lines = markdown.split ('\n ' );
263+
264+ if (lines.length == 1 ) {
265+ _pasteSingleLine (editorState, selection, lines[0 ]);
266+ return ;
267+ }
268+
269+ var path = selection.end.path.next;
270+ final node = editorState.document.nodeAtPath (selection.end.path);
271+ if (node is TextNode && node.toPlainText ().isEmpty) {
272+ path = selection.end.path;
273+ }
274+ final document = markdownToDocument (markdown);
275+ final transaction = editorState.transaction;
276+ transaction.insertNodes (path, document.root.children);
277+ editorState.apply (transaction);
278+ }
279+
255280void _handlePastePlainText (EditorState editorState, String plainText) {
256281 final selection = editorState.cursorSelection? .normalized;
257282 if (selection == null ) {
@@ -269,45 +294,7 @@ void _handlePastePlainText(EditorState editorState, String plainText) {
269294 // single line
270295 _pasteSingleLine (editorState, selection, lines.first);
271296 } else {
272- final firstLine = lines[0 ];
273- final beginOffset = selection.end.offset;
274- final remains = lines.sublist (1 );
275-
276- final path = [...selection.end.path];
277- if (path.isEmpty) {
278- return ;
279- }
280-
281- final node =
282- editorState.document.nodeAtPath (selection.end.path)! as TextNode ;
283- final insertedLineSuffix = node.delta.slice (beginOffset);
284-
285- path[path.length - 1 ]++ ;
286- final tb = editorState.transaction;
287- final List <TextNode > nodes =
288- remains.map ((e) => TextNode (delta: _lineContentToDelta (e))).toList ();
289-
290- final afterSelection =
291- _computeSelectionAfterPasteMultipleNodes (editorState, nodes);
292-
293- // append remain text to the last line
294- if (nodes.isNotEmpty) {
295- final last = nodes.last;
296- nodes[nodes.length - 1 ] =
297- TextNode (delta: last.delta..addAll (insertedLineSuffix));
298- }
299-
300- // insert first line
301- tb.updateText (
302- node,
303- Delta ()
304- ..retain (beginOffset)
305- ..insert (firstLine)
306- ..delete (node.delta.length - beginOffset));
307- // insert remains
308- tb.insertNodes (path, nodes);
309- tb.afterSelection = afterSelection;
310- editorState.apply (tb);
297+ _pasteMarkdown (editorState, plainText);
311298 }
312299}
313300
0 commit comments