@@ -192,6 +192,9 @@ export class GraphModel {
192192 ( ( entries ) => {
193193 for ( const entry of entries ) {
194194 if ( entry . target === this . rootEl ) {
195+ // 检查元素是否仍在DOM中
196+ const isElementInDOM = document . body . contains ( this . rootEl )
197+ if ( ! isElementInDOM ) return
195198 this . resize ( )
196199 this . eventCenter . emit ( 'graph:resize' , {
197200 target : this . rootEl ,
@@ -1584,15 +1587,32 @@ export class GraphModel {
15841587 * 重新设置画布的宽高
15851588 */
15861589 @action resize ( width ?: number , height ?: number ) : void {
1587- this . width = width ?? this . rootEl . getBoundingClientRect ( ) . width
1588- this . isContainerWidth = isNil ( width )
1589- this . height = height ?? this . rootEl . getBoundingClientRect ( ) . height
1590- this . isContainerHeight = isNil ( height )
1591-
1592- if ( ! this . width || ! this . height ) {
1593- console . warn (
1594- '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675' ,
1595- )
1590+ // 检查当前实例是否已被销毁或rootEl不存在
1591+ if ( ! this . rootEl ) return
1592+
1593+ // 检查元素是否仍在DOM中
1594+ const isElementInDOM = document . body . contains ( this . rootEl )
1595+ if ( ! isElementInDOM ) return
1596+
1597+ // 检查元素是否可见
1598+ const isVisible = this . rootEl . offsetParent !== null
1599+ if ( ! isVisible ) return
1600+
1601+ try {
1602+ this . width = width ?? this . rootEl . getBoundingClientRect ( ) . width
1603+ this . isContainerWidth = isNil ( width )
1604+ this . height = height ?? this . rootEl . getBoundingClientRect ( ) . height
1605+ this . isContainerHeight = isNil ( height )
1606+
1607+ // 只有在元素可见且应该有宽高的情况下才显示警告
1608+ if ( isVisible && ( ! this . width || ! this . height ) ) {
1609+ console . warn (
1610+ '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675' ,
1611+ )
1612+ }
1613+ } catch ( error ) {
1614+ // 捕获可能的DOM操作错误
1615+ console . warn ( '获取画布宽高时发生错误:' , error )
15961616 }
15971617 }
15981618
0 commit comments