@@ -21,7 +21,7 @@ import {Editor} from "../../editor";
2121import { writeText , isInAndroid , isInHarmony } from "../../protyle/util/compatibility" ;
2222import { mathRender } from "../../protyle/render/mathRender" ;
2323import { genEmptyElement } from "../../block/util" ;
24- import { focusBlock } from "../../protyle/util/selection" ;
24+ import { focusBlock , focusByWbr } from "../../protyle/util/selection" ;
2525
2626export class Outline extends Model {
2727 public tree : Tree ;
@@ -952,17 +952,22 @@ export class Outline extends Model {
952952 icon : "iconBefore" ,
953953 label : window . siyuan . languages . insertSameLevelHeadingBefore ,
954954 click : ( ) => {
955- fetchPost ( "/api/block/insertBlock" , {
956- data : "#" . repeat ( currentLevel ) + " " ,
957- dataType : "markdown" ,
958- nextID : id
959- } , ( response ) => {
960- openFileById ( {
961- app : this . app ,
962- id : response . data [ 0 ] . doOperations [ 0 ] . id ,
963- action : [ Constants . CB_GET_FOCUS , Constants . CB_GET_OUTLINE , Constants . CB_GET_SETID , Constants . CB_GET_CONTEXT , Constants . CB_GET_HTML ]
964- } ) ;
965- } ) ;
955+ const data = this . getProtyleAndBlockElement ( element ) ;
956+ const newId = Lute . NewNodeID ( ) ;
957+ const html = `<div data-subtype="h${ currentLevel } " data-node-id="${ newId } " data-type="NodeHeading" class="h${ currentLevel } "><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${ Constants . ZWSP } </div></div>` ;
958+ transaction ( data . protyle , [ {
959+ action : "insert" ,
960+ data : html ,
961+ id : newId ,
962+ previousID : data . blockElement . previousElementSibling ?. getAttribute ( "data-node-id" ) ,
963+ parentID : data . blockElement . parentElement . getAttribute ( "data-node-id" ) || data . protyle . block . parentID ,
964+ } ] , [ {
965+ action : "delete" ,
966+ id : newId
967+ } ] ) ;
968+ data . blockElement . insertAdjacentHTML ( "beforebegin" , html ) ;
969+ data . blockElement . previousElementSibling . scrollIntoView ( ) ;
970+ focusByWbr ( data . blockElement . previousElementSibling , document . createRange ( ) ) ;
966971 }
967972 } ) . element ) ;
968973
@@ -974,17 +979,26 @@ export class Outline extends Model {
974979 fetchPost ( "/api/block/getHeadingDeleteTransaction" , {
975980 id,
976981 } , ( deleteResponse ) => {
977- fetchPost ( "/api/block/insertBlock" , {
978- data : "#" . repeat ( currentLevel ) + " " ,
979- dataType : "markdown" ,
980- previousID : deleteResponse . data . doOperations [ deleteResponse . data . doOperations . length - 1 ] . id
981- } , ( response ) => {
982- openFileById ( {
983- app : this . app ,
984- id : response . data [ 0 ] . doOperations [ 0 ] . id ,
985- action : [ Constants . CB_GET_FOCUS , Constants . CB_GET_OUTLINE , Constants . CB_GET_SETID , Constants . CB_GET_CONTEXT , Constants . CB_GET_HTML ]
986- } ) ;
987- } ) ;
982+ const data = this . getProtyleAndBlockElement ( element ) ;
983+ const previousID = deleteResponse . data . doOperations [ deleteResponse . data . doOperations . length - 1 ] . id ;
984+
985+ const newId = Lute . NewNodeID ( ) ;
986+ const html = `<div data-subtype="h${ currentLevel } " data-node-id="${ newId } " data-type="NodeHeading" class="h${ currentLevel } "><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${ Constants . ZWSP } </div></div>` ;
987+ transaction ( data . protyle , [ {
988+ action : "insert" ,
989+ data : html ,
990+ id : newId ,
991+ previousID,
992+ } ] , [ {
993+ action : "delete" ,
994+ id : newId
995+ } ] ) ;
996+ const previousElement = data . protyle . wysiwyg . element . querySelector ( `[data-node-id="${ previousID } "]` ) ;
997+ if ( previousElement ) {
998+ previousElement . insertAdjacentHTML ( "afterend" , html ) ;
999+ previousElement . nextElementSibling . scrollIntoView ( ) ;
1000+ focusByWbr ( previousElement . nextElementSibling , document . createRange ( ) ) ;
1001+ }
9881002 } ) ;
9891003 }
9901004 } ) . element ) ;
@@ -1006,19 +1020,26 @@ export class Outline extends Model {
10061020 return true ;
10071021 }
10081022 } ) ;
1009- fetchPost ( "/api/block/insertBlock" , {
1010- data : "#" . repeat ( Math . min ( currentLevel + 1 , 6 ) ) + " " ,
1011- dataType : "markdown" ,
1023+
1024+
1025+ const data = this . getProtyleAndBlockElement ( element ) ;
1026+ const newId = Lute . NewNodeID ( ) ;
1027+ const html = `<div data-subtype="h${ currentLevel + 1 } " data-node-id="${ newId } " data-type="NodeHeading" class="h${ currentLevel + 1 } "><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${ Constants . ZWSP } </div></div>` ;
1028+ transaction ( data . protyle , [ {
1029+ action : "insert" ,
1030+ data : html ,
1031+ id : newId ,
10121032 previousID,
1013- } , ( response ) => {
1014- if ( response . code === 0 && response . data && response . data . length > 0 ) {
1015- openFileById ( {
1016- app : this . app ,
1017- id : response . data [ 0 ] . doOperations [ 0 ] . id ,
1018- action : [ Constants . CB_GET_FOCUS , Constants . CB_GET_OUTLINE ]
1019- } ) ;
1020- }
1021- } ) ;
1033+ } ] , [ {
1034+ action : "delete" ,
1035+ id : newId
1036+ } ] ) ;
1037+ const previousElement = data . protyle . wysiwyg . element . querySelector ( `[data-node-id="${ previousID } "]` ) ;
1038+ if ( previousElement ) {
1039+ previousElement . insertAdjacentHTML ( "afterend" , html ) ;
1040+ previousElement . nextElementSibling . scrollIntoView ( ) ;
1041+ focusByWbr ( previousElement . nextElementSibling , document . createRange ( ) ) ;
1042+ }
10221043 } ) ;
10231044 }
10241045 } ) . element ) ;
0 commit comments