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

How do I update a block or add a block with content? #8455

Closed
mjp0 opened this issue Sep 25, 2024 · 1 comment
Closed

How do I update a block or add a block with content? #8455

mjp0 opened this issue Sep 25, 2024 · 1 comment

Comments

@mjp0
Copy link

mjp0 commented Sep 25, 2024

I have a simple question: how do I update a block, or even how do I add a block with text?

The API docs don't explain the logic behind methods and guides seem to be outdated, so I can't seem to find the right combination.

Here's one of the ways I tried. Guides use doc.getBlockById but that's deprecated and replaced by getBlock, so I guess paragraph.model is the right way because updateBlock expects BlockModel instead of Block that getBlock returns? In any case, this way doesn't work either.

const pageBlockId = doc.addBlock('affine:page', {});
doc.addBlock('affine:surface', {}, pageBlockId);
const noteId = doc.addBlock('affine:note', {}, pageBlockId);
const paragraphId = doc.addBlock('affine:paragraph', {}, noteId);
const paragraph = doc.getBlock(paragraphId);
doc.updateBlock(paragraph?.model, {
  text: 'some text',
});

This particular way crashes with rich-text need yText to init. error but since typings aren't giving me any errors and updateBlock is asking for text: string 🤷‍♂️

@doodlewind
Copy link
Member

Try this:

import { Text } from '@blocksuite/store';

doc.updateBlock(myBlock, {
  text: new Text('some text'),
})

This is designed since inline rich text is also supported.

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

No branches or pull requests

2 participants