fix(editor): playground init error (#12565)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **Bug Fixes**
	- Improved stability when observing document title changes by ensuring internal checks before updating.
	- Enhanced document initialization to reuse existing documents when available, reducing unnecessary duplication and improving performance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
L-Sun
2025-06-03 07:18:29 +00:00
parent 3fe2ac4e46
commit ab78b8e3ab
2 changed files with 39 additions and 25 deletions

View File

@@ -192,10 +192,14 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
this._updateTitleInMeta(); this._updateTitleInMeta();
this.requestUpdate(); this.requestUpdate();
}; };
this._rootModel?.props.title.yText.observe(updateMetaTitle);
this._disposables.add(() => { if (this._rootModel) {
this._rootModel?.props.title.yText.unobserve(updateMetaTitle); const rootModel = this._rootModel;
}); rootModel.props.title.yText.observe(updateMetaTitle);
this._disposables.add(() => {
rootModel.props.title.yText.unobserve(updateMetaTitle);
});
}
} }
override render() { override render() {

View File

@@ -7,30 +7,40 @@ import type { InitFn } from './utils.js';
const presetMarkdown = `Click the 🔁 button to switch between editors dynamically - they are fully compatible!`; const presetMarkdown = `Click the 🔁 button to switch between editors dynamically - they are fully compatible!`;
export const preset: InitFn = async (collection: Workspace, id: string) => { export const preset: InitFn = async (collection: Workspace, id: string) => {
const doc = collection.createDoc(id).getStore({ id }); let doc = collection.getDoc(id);
doc.load(); const hasDoc = !!doc;
// Add root block and surface block at root level if (!doc) {
const rootId = doc.addBlock('affine:page', { doc = collection.createDoc(id);
title: new Text('BlockSuite Playground'), }
});
doc.addBlock('affine:surface', {}, rootId);
// Add note block inside root block const store = doc.getStore({ id });
const noteId = doc.addBlock( store.load();
'affine:note',
{ xywh: '[0, 100, 800, 640]' },
rootId
);
// Import preset markdown content inside note block // Run only once on all clients.
await MarkdownTransformer.importMarkdownToBlock({ let noteId: string;
doc, if (!hasDoc) {
blockId: noteId, // Add root block and surface block at root level
markdown: presetMarkdown, const rootId = store.addBlock('affine:page', {
extensions: getTestStoreManager().get('store'), title: new Text('BlockSuite Playground'),
}); });
store.addBlock('affine:surface', {}, rootId);
doc.resetHistory(); // Add note block inside root block
noteId = store.addBlock(
'affine:note',
{ xywh: '[0, 100, 800, 640]' },
rootId
);
// Import preset markdown content inside note block
await MarkdownTransformer.importMarkdownToBlock({
doc: store,
blockId: noteId,
markdown: presetMarkdown,
extensions: getTestStoreManager().get('store'),
});
}
store.resetHistory();
}; };
preset.id = 'preset'; preset.id = 'preset';