leaferjs / leafer-ui

一款好用的 Canvas 渲染引擎,革新的体验。高效绘图 、UI 交互(小游戏、互动应用、组态)、图形编辑,前端开发必备
https://www.leaferjs.com
MIT License
2.44k stars 84 forks source link

[bug] leafer-editor 在选中元素的情况下销毁报错 #239

Open Ronbb opened 1 day ago

Ronbb commented 1 day ago

重现过程:

  1. 创建app,创建Rect
  2. 选中元素Rect
  3. 销毁app
  4. 出现报错

异常堆栈:

hook.js:608 Leafer TypeError: Cannot read properties of null (reading 'editBox')
    at get editBox (editor.esm.js:1842:40)
    at EditTool2.unload (editor.esm.js:1948:14)
    at Editor.updateEditTool (editor.esm.js:1531:18)
    at onTarget (editor.esm.js:1297:16)
    at Editor.set [as target] (editor.esm.js:296:47)
    at Editor.destroy (editor.esm.js:1824:18)
    at core.esm.js:5756:27
    at Array.forEach (<anonymous>)
    at Leafer2.removeAll (core.esm.js:5753:22)
    at Leafer2.destroy (core.esm.js:5645:22) Error
    at _Debug.error (core.esm.js:1358:19)
    at doDestory (draw.esm.js:1217:27)

可能的原因:

this.editTool 已经在 Editor.destroy 销毁 this.editToolList 时被销毁,但是 this.editTool 没有被修改为 null

   // /packages/editor/src/Editor.ts#L136
   public updateEditTool(): void {
        const tool = this.editTool
        if (tool) {
            this.editBox.unload()
            tool.unload()
            this.editTool = null
        }

        if (this.editing) {
            const tag = this.single ? this.list[0].editOuter as string : 'EditTool'
            this.editTool = this.editToolList[tag] = this.editToolList[tag] || EditToolCreator.get(tag, this)
            this.editBox.load()
            this.editTool.load()
        }
    }
Ronbb commented 1 day ago

临时解决方案:

  useEffect(() => {
    if ((!app.current || app.current.destroyed) && container.current) {
      const newApp = new App({
        view: container.current,
        editor: {},
      });

      app.current = newApp;
    }

    return () => {
      delete app.current?.editor.editTool; // <- 提前删除
      app.current?.destroy(true);
      app.current = null;
    };
  }, []);
leaferjs commented 1 day ago

感谢反馈~,v1.0.6修复过一个类似的bug,你使用的版本是之前的还是最新的?

Ronbb commented 1 day ago

感谢反馈~,v1.0.6修复过一个类似的bug,你使用的版本是之前的还是最新的?

@leaferjs 目前使用的是 leafer-editor": "^1.0.7

leaferjs commented 1 day ago

好的,我下个版本修复一下~

jdpoccorie commented 8 hours ago

Perhaps this solution will help you figure out the problem click