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,11 +192,15 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
this._updateTitleInMeta(); this._updateTitleInMeta();
this.requestUpdate(); this.requestUpdate();
}; };
this._rootModel?.props.title.yText.observe(updateMetaTitle);
if (this._rootModel) {
const rootModel = this._rootModel;
rootModel.props.title.yText.observe(updateMetaTitle);
this._disposables.add(() => { this._disposables.add(() => {
this._rootModel?.props.title.yText.unobserve(updateMetaTitle); rootModel.props.title.yText.unobserve(updateMetaTitle);
}); });
} }
}
override render() { override render() {
const isEmpty = !this._rootModel?.props.title.length && !this._isComposing; const isEmpty = !this._rootModel?.props.title.length && !this._isComposing;

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;
if (!doc) {
doc = collection.createDoc(id);
}
const store = doc.getStore({ id });
store.load();
// Run only once on all clients.
let noteId: string;
if (!hasDoc) {
// Add root block and surface block at root level // Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', { const rootId = store.addBlock('affine:page', {
title: new Text('BlockSuite Playground'), title: new Text('BlockSuite Playground'),
}); });
doc.addBlock('affine:surface', {}, rootId); store.addBlock('affine:surface', {}, rootId);
// Add note block inside root block // Add note block inside root block
const noteId = doc.addBlock( noteId = store.addBlock(
'affine:note', 'affine:note',
{ xywh: '[0, 100, 800, 640]' }, { xywh: '[0, 100, 800, 640]' },
rootId rootId
); );
// Import preset markdown content inside note block // Import preset markdown content inside note block
await MarkdownTransformer.importMarkdownToBlock({ await MarkdownTransformer.importMarkdownToBlock({
doc, doc: store,
blockId: noteId, blockId: noteId,
markdown: presetMarkdown, markdown: presetMarkdown,
extensions: getTestStoreManager().get('store'), extensions: getTestStoreManager().get('store'),
}); });
}
doc.resetHistory(); store.resetHistory();
}; };
preset.id = 'preset'; preset.id = 'preset';