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

Improve BlockManager.update method #2602

Open
AricZhu opened this issue Feb 2, 2024 · 0 comments
Open

Improve BlockManager.update method #2602

AricZhu opened this issue Feb 2, 2024 · 0 comments

Comments

@AricZhu
Copy link

AricZhu commented Feb 2, 2024

The question

When calling update method, the original block will be deleted and a new block will be created. This will cause the editor to flicker. The problem will be more obvious during continuous updates. The code of the recurrence problem is as follows:

const str = 'when interval update this text, it will cause the editor to flicker.';
await intervalInvoke(
  async (curInvokeIndex) => {
    editor.blocks.update('your-block-id', {
      text: str.slice(0, curInvokeIndex),
    });
  },
  100,
  50
);

async function delay(ms) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(0);
    }, ms);
  });
}

async function intervalInvoke(
  fn,
  intervalTimeMs,
  invokeTimes
) {
  return new Promise((resolve) => {
    let count = 0;
    const invoke = async () => {
      const ret = await fn(count);
      if (ret === false) {
        return resolve(0);
      }
      count++;
      if (count >= invokeTimes) {
        return resolve(0);
      }
      await delay(intervalTimeMs);
      invoke();
    };

    invoke();
  });
}

Solution

To address the issues above, we can add support for the block.update method to avoid re creation. Of course, if ToolInstance does not provide an update method, it can still be downgraded to the original creation method to achieve updates.

So, I push a PR, please help review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant