mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
Close [BS-3068](https://linear.app/affine-design/issue/BS-3068/在edgeless-的embed-doc-block上,增加-insert-into-page的选项,类似frame) Close [BS-3069](https://linear.app/affine-design/issue/BS-3069/edgeless下的-embed,覆盖现在的edit行为-,暂定叫做duplicate-into-a-note,复制一个-note)
119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import type {
|
|
EmbedSyncedDocBlockProps,
|
|
NoteBlockModel,
|
|
ParagraphProps,
|
|
RootBlockProps,
|
|
} from '@blocksuite/affine-model';
|
|
import type { Store } from '@blocksuite/store';
|
|
import type { Page } from '@playwright/test';
|
|
/**
|
|
* using page.evaluate to init the embed synced doc state
|
|
* @param page - playwright page
|
|
* @param data.title - the title of the doc
|
|
* @param data.content - the content of the doc
|
|
* @param data.inEdgeless - whether this doc is in parent doc's canvas, default is in page
|
|
* @param option.chain - doc1 -> doc2 -> doc3 -> ..., if chain is false, doc1 will be the parent of remaining docs
|
|
* @returns the ids of created docs
|
|
*/
|
|
export async function initEmbedSyncedDocState(
|
|
page: Page,
|
|
data: { title: string; content: string; inEdgeless?: boolean }[],
|
|
option?: {
|
|
chain?: boolean;
|
|
}
|
|
) {
|
|
if (data.length === 0) {
|
|
throw new Error('Data is empty');
|
|
}
|
|
|
|
return await page.evaluate(
|
|
({ data, option }) => {
|
|
const createDoc = async (
|
|
docId: string,
|
|
title: string,
|
|
content: string
|
|
) => {
|
|
const { collection, $blocksuite } = window;
|
|
const Text = $blocksuite.store.Text;
|
|
|
|
const store = (
|
|
collection.getDoc(docId) ?? collection.createDoc(docId)
|
|
).getStore();
|
|
store.load();
|
|
|
|
const rootId = store.addBlock('affine:page', {
|
|
title: new Text(title),
|
|
} satisfies Partial<RootBlockProps>);
|
|
|
|
store.addBlock('affine:surface', {}, rootId);
|
|
|
|
const noteId = store.addBlock('affine:note', {}, rootId);
|
|
store.addBlock(
|
|
'affine:paragraph',
|
|
{
|
|
text: new Text(content),
|
|
} satisfies Partial<ParagraphProps>,
|
|
noteId
|
|
);
|
|
};
|
|
|
|
const getVisibleNote = (store: Store) => {
|
|
const note = store
|
|
.getModelsByFlavour('affine:note')
|
|
.filter((note): note is NoteBlockModel => {
|
|
return (
|
|
note instanceof NoteBlockModel &&
|
|
note.props.displayMode === NoteDisplayMode.DocAndEdgeless
|
|
);
|
|
})[0];
|
|
return note ?? null;
|
|
};
|
|
|
|
const docIds = data.map(({ title, content }, index) => {
|
|
const id = index === 0 ? window.doc.id : `embed-doc-${index}`;
|
|
createDoc(id, title, content);
|
|
return id;
|
|
});
|
|
|
|
const { NoteBlockModel, NoteDisplayMode } =
|
|
window.$blocksuite.affineModel;
|
|
|
|
let prevId = window.doc.id;
|
|
for (let index = 1; index < docIds.length; index++) {
|
|
const docId = docIds[index];
|
|
|
|
const store = window.collection
|
|
.getDoc(option?.chain ? prevId : docIds[0])
|
|
?.getStore();
|
|
if (!store) {
|
|
throw new Error('Store not found');
|
|
}
|
|
|
|
const note = getVisibleNote(store);
|
|
if (!note) {
|
|
throw new Error(`Note not found in ${docId}`);
|
|
}
|
|
|
|
const surface = store.getModelsByFlavour('affine:surface')[0];
|
|
if (!surface) {
|
|
throw new Error(`Surface not found in ${docId}`);
|
|
}
|
|
|
|
store.addBlock(
|
|
'affine:embed-synced-doc',
|
|
{
|
|
pageId: docId,
|
|
xywh: '[0, 100, 370, 100]',
|
|
} satisfies Partial<EmbedSyncedDocBlockProps>,
|
|
data[index].inEdgeless ? surface.id : note.id
|
|
);
|
|
|
|
prevId = docId;
|
|
}
|
|
|
|
return docIds;
|
|
},
|
|
{ data, option }
|
|
);
|
|
}
|