refactor(editor): remove re-exports (#9406)

This commit is contained in:
Saul-Mirone
2024-12-28 06:51:41 +00:00
parent 1deed602c8
commit dc92d78895
28 changed files with 62 additions and 90 deletions
@@ -1,16 +1,2 @@
// Compat with SSR
export * from '../types.js';
export * from './query.js';
export {
createButtonPopper,
getBlockProps,
getImageFilesFromLocal,
isMiddleButtonPressed,
isRightButtonPressed,
isValidUrl,
matchFlavours,
on,
once,
openFileOrFiles,
requestThrottledConnectedFrame,
} from '@blocksuite/affine-shared/utils';
@@ -1,119 +0,0 @@
import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import type { FrameBlockModel, NoteBlockModel } from '@blocksuite/affine-model';
import { NoteDisplayMode } from '@blocksuite/affine-model';
import { DocModeProvider } from '@blocksuite/affine-shared/services';
import { getBlockProps } from '@blocksuite/affine-shared/utils';
import type { EditorHost } from '@blocksuite/block-std';
import { type BlockModel, type Doc } from '@blocksuite/store';
import { GfxBlockModel } from '../../root-block/edgeless/block-model.js';
import {
getElementProps,
mapFrameIds,
sortEdgelessElements,
} from '../../root-block/edgeless/utils/clone-utils.js';
import {
isFrameBlock,
isNoteBlock,
} from '../../root-block/edgeless/utils/query.js';
export function addBlocksToDoc(
targetDoc: Doc,
model: BlockModel,
parentId: string
) {
// Add current block to linked doc
const blockProps = getBlockProps(model);
const newModelId = targetDoc.addBlock(
model.flavour as BlockSuite.Flavour,
blockProps,
parentId
);
// Add children to linked doc, parent is the new model
const children = model.children;
if (children.length > 0) {
children.forEach(child => {
addBlocksToDoc(targetDoc, child, newModelId);
});
}
}
export function createLinkedDocFromNote(
doc: Doc,
note: NoteBlockModel,
docTitle?: string
) {
const linkedDoc = doc.collection.createDoc({});
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new doc.Text(docTitle),
});
linkedDoc.addBlock('affine:surface', {}, rootId);
const blockProps = getBlockProps(note);
// keep note props & show in both mode
const noteId = linkedDoc.addBlock(
'affine:note',
{
...blockProps,
hidden: false,
displayMode: NoteDisplayMode.DocAndEdgeless,
},
rootId
);
// Add note to linked doc recursively
note.children.forEach(model => {
addBlocksToDoc(linkedDoc, model, noteId);
});
});
return linkedDoc;
}
export function createLinkedDocFromEdgelessElements(
host: EditorHost,
elements: BlockSuite.EdgelessModel[],
docTitle?: string
) {
const linkedDoc = host.doc.collection.createDoc({});
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new host.doc.Text(docTitle),
});
const surfaceId = linkedDoc.addBlock('affine:surface', {}, rootId);
const surface = getSurfaceBlock(linkedDoc);
if (!surface) return;
const sortedElements = sortEdgelessElements(elements);
const ids = new Map<string, string>();
sortedElements.forEach(model => {
let newId = model.id;
if (model instanceof GfxBlockModel) {
const blockProps = getBlockProps(model);
if (isNoteBlock(model)) {
newId = linkedDoc.addBlock('affine:note', blockProps, rootId);
// Add note children to linked doc recursively
model.children.forEach(model => {
addBlocksToDoc(linkedDoc, model, newId);
});
} else {
if (isFrameBlock(model)) {
mapFrameIds(blockProps as unknown as FrameBlockModel, ids);
}
newId = linkedDoc.addBlock(
model.flavour as BlockSuite.Flavour,
blockProps,
surfaceId
);
}
} else {
const props = getElementProps(model, ids);
newId = surface.addElement(props);
}
ids.set(model.id, newId);
});
});
host.std.get(DocModeProvider).setPrimaryMode('edgeless', linkedDoc.id);
return linkedDoc;
}