@@ -12,7 +12,7 @@ import {Tab} from "./Tab";
1212import { Model } from "./Model" ;
1313import { Editor } from "../editor" ;
1414import { Graph } from "./dock/Graph" ;
15- import { hasClosestByAttribute , hasClosestByClassName } from "../protyle/util/hasClosest" ;
15+ import { hasClosestBlock , hasClosestByAttribute , hasClosestByClassName } from "../protyle/util/hasClosest" ;
1616import { Constants } from "../constants" ;
1717/// #if !BROWSER
1818import { webFrame , ipcRenderer } from "electron" ;
@@ -32,6 +32,7 @@ import {MenuItem} from "../menus/Menu";
3232import { escapeHtml } from "../util/escape" ;
3333import { isWindow } from "../util/functions" ;
3434import { hideAllElements } from "../protyle/ui/hideElements" ;
35+ import { focusByOffset , getSelectionOffset } from "../protyle/util/selection" ;
3536
3637export class Wnd {
3738 public id : string ;
@@ -369,6 +370,7 @@ export class Wnd {
369370 switchWnd ( newWnd , targetWnd ) ;
370371 }
371372 }
373+ resizeTabs ( ) ;
372374 /// #if !BROWSER
373375 setTabPosition ( ) ;
374376 /// #endif
@@ -754,10 +756,29 @@ export class Wnd {
754756 }
755757
756758 public moveTab ( tab : Tab , nextId ?: string ) {
759+ let rangeData : {
760+ id : string ,
761+ start : number ,
762+ end : number
763+ } ;
764+ if ( tab . model instanceof Editor && tab . model . editor . protyle . toolbar . range ) {
765+ const blockElement = hasClosestBlock ( tab . model . editor . protyle . toolbar . range . startContainer ) ;
766+ if ( blockElement ) {
767+ const startEnd = getSelectionOffset ( blockElement , undefined , tab . model . editor . protyle . toolbar . range ) ;
768+ rangeData = {
769+ id : blockElement . getAttribute ( "data-node-id" ) ,
770+ start : startEnd . start ,
771+ end : startEnd . end
772+ } ;
773+ }
774+ }
757775 this . element . querySelector ( ".layout-tab-container" ) . append ( tab . panelElement ) ;
758- if ( tab . model instanceof Editor ) {
759- // DOM 移动后 range 会变化,因此置空
760- tab . model . editor . protyle . toolbar . range = null ;
776+ if ( rangeData && tab . model instanceof Editor ) {
777+ // DOM 移动后 range 会变化
778+ const range = focusByOffset ( tab . model . editor . protyle . wysiwyg . element . querySelector ( `[data-node-id="${ rangeData . id } "]` ) , rangeData . start , rangeData . end ) ;
779+ if ( range ) {
780+ tab . model . editor . protyle . toolbar . range = range ;
781+ }
761782 }
762783 if ( nextId ) {
763784 // 只能用 find https://github.com/siyuan-note/siyuan/issues/3455
@@ -802,7 +823,6 @@ export class Wnd {
802823 }
803824 }
804825 tab . parent = this ;
805- resizeTabs ( ) ;
806826 hideAllElements ( [ "toolbar" ] ) ;
807827 }
808828
0 commit comments