mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-31 00:59:57 +08:00
17bf75e843
Closes: [BS-2216](https://linear.app/affine-design/issue/BS-2216/remove-global-types-in-command)
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import type { LatexProps } from '@blocksuite/affine-model';
|
|
import type { Command } from '@blocksuite/block-std';
|
|
import { assertInstanceOf } from '@blocksuite/global/utils';
|
|
import type { BlockModel } from '@blocksuite/store';
|
|
|
|
import { LatexBlockComponent } from './latex-block.js';
|
|
|
|
export const insertLatexBlockCommand: Command<
|
|
{
|
|
latex?: string;
|
|
place?: 'after' | 'before';
|
|
removeEmptyLine?: boolean;
|
|
selectedModels?: BlockModel[];
|
|
},
|
|
{
|
|
insertedLatexBlockId: Promise<string>;
|
|
}
|
|
> = (ctx, next) => {
|
|
const { selectedModels, latex, place, removeEmptyLine, std } = ctx;
|
|
if (!selectedModels?.length) return;
|
|
|
|
const targetModel =
|
|
place === 'before'
|
|
? selectedModels[0]
|
|
: selectedModels[selectedModels.length - 1];
|
|
|
|
const latexBlockProps: Partial<LatexProps> & {
|
|
flavour: 'affine:latex';
|
|
} = {
|
|
flavour: 'affine:latex',
|
|
latex: latex ?? '',
|
|
};
|
|
|
|
const result = std.store.addSiblingBlocks(
|
|
targetModel,
|
|
[latexBlockProps],
|
|
place
|
|
);
|
|
if (result.length === 0) return;
|
|
|
|
if (removeEmptyLine && targetModel.text?.length === 0) {
|
|
std.store.deleteBlock(targetModel);
|
|
}
|
|
|
|
next({
|
|
insertedLatexBlockId: std.host.updateComplete.then(async () => {
|
|
if (!latex) {
|
|
const blockComponent = std.view.getBlock(result[0]);
|
|
assertInstanceOf(blockComponent, LatexBlockComponent);
|
|
await blockComponent.updateComplete;
|
|
blockComponent.toggleEditor();
|
|
}
|
|
return result[0];
|
|
}),
|
|
});
|
|
};
|