Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第二次调用fromJSON 报错 Cannot read properties of null (reading 'getZIndex') #2210

Closed
thorin8 opened this issue Jun 10, 2022 · 4 comments
Labels
resolution: needs demos 需要复现 Issues that need minimal demos to reproduce any problems

Comments

@thorin8
Copy link

thorin8 commented Jun 10, 2022

问题描述

经调试发现是renderer.js 828行,如下代码问题
this.sortElements(elems, (a, b) => {
// 重写代码
// let indexA = a.zIndex;
// let indexB = b.zIndex;
// return indexA==indexB?0:indexA<indexB?-1:1;

const cellA = model.getCell(a.getAttribute('data-cell-id') || '');
const cellB = model.getCell(b.getAttribute('data-cell-id') || '');
//报错问题是cellA、cellB为null
if(cellA && cellB){
    const z1 = cellA.getZIndex() || 0;
    const z2 = cellB.getZIndex() || 0;
    return z1 === z2 ? 0 : z1 < z2 ? -1 : 1;
}
return 0;

});

重现链接

重现步骤

  1. 第一次调用fromJSON 正常
  2. clearCells
  3. 再次调用fromJSON报错

预期行为

我期望不报错

平台

  • 操作系统: [macOS, Windows, Linux, React Native ...]
  • 网页浏览器: [Google Chrome, Safari, Firefox]
  • X6 版本: [1.28.2 ... ]

屏幕截图或视频(可选)

No response

补充说明(可选)

No response

@x6-bot
Copy link
Contributor

x6-bot bot commented Jun 10, 2022

👋 @thorin8

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

@thorin8
Copy link
Author

thorin8 commented Jun 10, 2022

已通过重写方法解决

let renderer = this.g.renderer
renderer.sortViewsExact = () => {
  const elems = renderer.view
    .$(renderer.view.stage)
    .children('[data-cell-id]')
    .toArray()
  const placements = elems.map((elem) => {
    const parentNode = elem.parentNode
    const nextSibling = parentNode.insertBefore(
      document.createTextNode(''),
      elem.nextSibling
    )
    return (targetNode) => {
      if (parentNode === targetNode) {
        throw new Error(
          "You can't sort elements if any one is a descendant of another."
        )
      }
      parentNode.insertBefore(targetNode, nextSibling)
      parentNode.removeChild(nextSibling)
    }
  })
  elems
    .sort((a: any, b: any) => {
      // 重写代码
      let indexA = a.zIndex
      let indexB = b.zIndex
      return indexA == indexB ? 0 : indexA < indexB ? -1 : 1
    })
    .forEach((elem, index) => placements[index](elem))
}

@tonywu6
Copy link
Collaborator

tonywu6 commented Jun 10, 2022

Hello! 可以提供一个代码重现吗?可以使用这个 CodeSandbox 模版

@tonywu6 tonywu6 added the resolution: needs demos 需要复现 Issues that need minimal demos to reproduce any problems label Jun 10, 2022
@x6-bot
Copy link
Contributor

x6-bot bot commented Jul 10, 2023

This thread has been automatically locked because it has not had recent activity.

Please open a new issue for related bugs and link to relevant comments in this thread.

@x6-bot x6-bot bot locked as resolved and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolution: needs demos 需要复现 Issues that need minimal demos to reproduce any problems
Projects
None yet
Development

No branches or pull requests

3 participants