Skip to content

Commit

Permalink
feat: add type for addBlock and updateBlock (toeverything#6301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Feb 26, 2024
1 parent 74df974 commit f8f6883
Show file tree
Hide file tree
Showing 55 changed files with 316 additions and 183 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
]
},
"overrides": {
"typescript": "5.3.3",
"array-includes": "npm:@nolyfill/array-includes@latest",
"array.prototype.flat": "npm:@nolyfill/array.prototype.flat@latest",
"array.prototype.flatmap": "npm:@nolyfill/array.prototype.flatmap@latest",
Expand Down
9 changes: 2 additions & 7 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@types/hast": "^3.0.3",
"@types/mdast": "^4.0.3",
"@types/webfontloader": "^1.6.38",
"@types/sortablejs": "^1.15.7",
"buffer": "^6.0.3",
"date-fns": "^3.3.0",
"file-type": "^16.5.4",
Expand Down Expand Up @@ -83,11 +84,5 @@
"!dist/__tests__",
"models.d.ts",
"models.js"
],
"devDependencies": {
"@blocksuite/block-std": "workspace:*",
"@blocksuite/lit": "workspace:*",
"@blocksuite/store": "workspace:*",
"@types/sortablejs": "^1.15.7"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('DatabaseManager', () => {
noteBlockId = page.addBlock('affine:note', {}, pageBlockId);

databaseBlockId = page.addBlock(
'affine:database',
'affine:database' as BlockSuite.ModelKeys,
{
columns: [],
titleColumn: 'Title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ export class EmbedCardMoreMenu extends WithDisposable(LitElement) {
const { page } = model;
const parent = page.getParent(model);
const index = parent?.children.indexOf(model);
page.addBlock(model.flavour, duplicateProps, parent, index);
page.addBlock(
model.flavour as BlockSuite.ModelKeys,
duplicateProps,
parent,
index
);
this.abortController.abort();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class EmbedCardToolbar extends WithDisposable(LitElement) {
const index = parent.children.indexOf(this._model);

page.addBlock(
targetFlavour,
targetFlavour as never,
{ url, style: targetStyle, caption },
parent,
index
Expand Down Expand Up @@ -392,7 +392,12 @@ export class EmbedCardToolbar extends WithDisposable(LitElement) {
assertExists(parent);
const index = parent.children.indexOf(this._model);

page.addBlock(flavour, { url, style: targetStyle, caption }, parent, index);
page.addBlock(
flavour as never,
{ url, style: targetStyle, caption },
parent,
index
);

this._std.selection.setGroup('note', []);
page.deleteBlock(this._model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class EmbedCardCreateModal extends WithDisposable(ShadowlessElement) {
}

this.host.page.addBlock(
flavour,
flavour as never,
{
url,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Page } from '@blocksuite/store';
import { type BlockModel } from '@blocksuite/store';
import { Text } from '@blocksuite/store';

import type { BlockModelProps } from '../../../_common/utils/model.js';
import {
isInsideBlockByFlavour,
matchFlavours,
Expand Down Expand Up @@ -62,8 +61,14 @@ export function handleBlockEndEnter(
}

const getProps = ():
| ['affine:list', Partial<BlockModelProps['affine:list']>]
| ['affine:paragraph', Partial<BlockModelProps['affine:paragraph']>] => {
| [
'affine:list',
BlockSuite.ModelProps<BlockSuite.BlockModels['affine:list']>,
]
| [
'affine:paragraph',
BlockSuite.ModelProps<BlockSuite.BlockModels['affine:paragraph']>,
] => {
const shouldInheritFlavour = matchFlavours(model, ['affine:list']);
if (shouldInheritFlavour) {
return [model.flavour, { type: model.type }];
Expand Down Expand Up @@ -170,7 +175,7 @@ export function handleBlockSplit(
const children = [...model.children];
page.updateBlock(model, { children: [] });
const id = page.addBlock(
model.flavour,
model.flavour as never,
{
text: right,
type: model.type,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/configs/quick-action/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const quickActionConfig: QuickActionConfig[] = [
const blockProps = Object.fromEntries(
keys.map((key, i) => [key, values[i]])
);
linkedPage.addBlock(model.flavour, blockProps, noteId);
linkedPage.addBlock(model.flavour as never, blockProps, noteId);
page.deleteBlock(model);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ import {
import { Bound } from '../../surface-block/index.js';
import { EMBED_CARD_HEIGHT, EMBED_CARD_WIDTH } from '../consts.js';
import type { EdgelessSelectableProps } from '../edgeless/mixin/index.js';
import {
type BlockModels,
type EmbedCardStyle,
matchFlavours,
} from '../utils/index.js';
import { type EmbedCardStyle, matchFlavours } from '../utils/index.js';

export class EmbedBlockElement<
Model extends
Expand Down Expand Up @@ -71,7 +67,7 @@ export class EmbedBlockElement<
if (
!anchorComponent ||
!matchFlavours(anchorComponent.model, [
this.flavour as keyof BlockModels,
this.flavour as keyof BlockSuite.BlockModels,
])
)
return false;
Expand Down Expand Up @@ -117,7 +113,7 @@ export class EmbedBlockElement<
if (
draggingElements.length !== 1 ||
!matchFlavours(draggingElements[0].model, [
this.flavour as keyof BlockModels,
this.flavour as keyof BlockSuite.BlockModels,
])
)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export class LinkPopup extends WithDisposable(LitElement) {
const parent = page.getParent(blockElement.model);
assertExists(parent);
const index = parent.children.indexOf(blockElement.model);
page.addBlock(targetFlavour, props, parent, index + 1);
page.addBlock(targetFlavour as never, props, parent, index + 1);

const totalTextLength = this.inlineEditor.yTextLength;
const inlineTextLength = this.targetInlineRange.length;
Expand All @@ -296,7 +296,7 @@ export class LinkPopup extends WithDisposable(LitElement) {
assertExists(parent);
const index = parent.children.indexOf(blockElement.model);

page.addBlock(flavour, { url }, parent, index + 1);
page.addBlock(flavour as never, { url }, parent, index + 1);

const totalTextLength = this.inlineEditor.yTextLength;
const inlineTextLength = this.targetInlineRange.length;
Expand Down
89 changes: 5 additions & 84 deletions packages/blocks/src/_common/utils/model.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,18 @@
import type { BlockModel, Page } from '@blocksuite/store';

import type {
AttachmentBlockModel,
AttachmentBlockSchema,
BookmarkBlockModel,
BookmarkBlockSchema,
CodeBlockModel,
CodeBlockSchema,
DatabaseBlockModel,
DatabaseBlockSchema,
DataViewBlockModel,
DataViewBlockSchema,
DividerBlockModel,
DividerBlockSchema,
EmbedFigmaModel,
EmbedGithubModel,
EmbedHtmlModel,
EmbedLinkedDocModel,
EmbedLoomModel,
EmbedSyncedDocModel,
EmbedYoutubeModel,
FrameBlockModel,
FrameBlockSchema,
ImageBlockModel,
ImageBlockSchema,
ListBlockModel,
ListBlockSchema,
NoteBlockModel,
NoteBlockSchema,
PageBlockModel,
PageBlockSchema,
ParagraphBlockModel,
ParagraphBlockSchema,
SurfaceBlockModel,
SurfaceBlockSchema,
SurfaceRefBlockModel,
} from '../../index.js';

export type BlockModels = {
'affine:paragraph': ParagraphBlockModel;
'affine:page': PageBlockModel;
'affine:list': ListBlockModel;
'affine:note': NoteBlockModel;
'affine:code': CodeBlockModel;
'affine:divider': DividerBlockModel;
'affine:image': ImageBlockModel;
'affine:surface': SurfaceBlockModel;
'affine:frame': FrameBlockModel;
'affine:database': DatabaseBlockModel;
'affine:data-view': DataViewBlockModel;
'affine:bookmark': BookmarkBlockModel;
'affine:attachment': AttachmentBlockModel;
'affine:surface-ref': SurfaceRefBlockModel;
'affine:embed-github': EmbedGithubModel;
'affine:embed-youtube': EmbedYoutubeModel;
'affine:embed-figma': EmbedFigmaModel;
'affine:embed-linked-doc': EmbedLinkedDocModel;
'affine:embed-synced-doc': EmbedSyncedDocModel;
'affine:embed-html': EmbedHtmlModel;
'affine:embed-loom': EmbedLoomModel;
};

export type BlockSchemas = {
'affine:paragraph': typeof ParagraphBlockSchema;
'affine:page': typeof PageBlockSchema;
'affine:list': typeof ListBlockSchema;
'affine:note': typeof NoteBlockSchema;
'affine:code': typeof CodeBlockSchema;
'affine:divider': typeof DividerBlockSchema;
'affine:image': typeof ImageBlockSchema;
'affine:surface': typeof SurfaceBlockSchema;
'affine:frame': typeof FrameBlockSchema;
'affine:database': typeof DatabaseBlockSchema;
'affine:data-view': typeof DataViewBlockSchema;
'affine:bookmark': typeof BookmarkBlockSchema;
'affine:attachment': typeof AttachmentBlockSchema;
};

export type BlockModelProps = {
[K in keyof BlockSchemas]: ReturnType<BlockSchemas[K]['model']['props']>;
};

export function assertFlavours(model: { flavour: string }, allowed: string[]) {
if (!allowed.includes(model.flavour)) {
throw new Error(`model flavour ${model.flavour} is not allowed`);
}
}

export function matchFlavours<Key extends (keyof BlockModels)[]>(
export function matchFlavours<Key extends (keyof BlockSuite.BlockModels)[]>(
model: BlockModel | null,
expected: Key
): model is BlockModels[Key[number]] {
return !!model && expected.includes(model.flavour as keyof BlockModels);
): model is BlockSuite.BlockModels[Key[number]] {
return (
!!model && expected.includes(model.flavour as keyof BlockSuite.BlockModels)
);
}

export function isInsideBlockByFlavour(
Expand Down
5 changes: 5 additions & 0 deletions packages/blocks/src/attachment-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AttachmentBlockModel } from './attachment-model.js';
import type { AttachmentService } from './attachment-service.js';

export * from './attachment-block.js';
Expand All @@ -13,5 +14,9 @@ declare global {
interface BlockServices {
'affine:attachment': AttachmentService;
}

interface BlockModels {
'affine:attachment': AttachmentBlockModel;
}
}
}
4 changes: 4 additions & 0 deletions packages/blocks/src/bookmark-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BookmarkBlockModel } from './bookmark-model.js';
import type { BookmarkService } from './bookmark-service.js';

export * from './bookmark-block.js';
Expand All @@ -9,5 +10,8 @@ declare global {
interface BlockServices {
'affine:bookmark': BookmarkService;
}
interface BlockModels {
'affine:bookmark': BookmarkBlockModel;
}
}
}
10 changes: 10 additions & 0 deletions packages/blocks/src/code-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type { CodeBlockModel } from './code-model.js';

export * from './code-block.js';
export * from './code-model.js';
export * from './components/index.js';

declare global {
namespace BlockSuite {
interface BlockModels {
'affine:code': CodeBlockModel;
}
}
}
10 changes: 10 additions & 0 deletions packages/blocks/src/data-view-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import type { DataViewBlockModel } from './data-view-model.js';

export * from './data-view-block.js';
export * from './data-view-model.js';

declare global {
namespace BlockSuite {
interface BlockModels {
'affine:data-view': DataViewBlockModel;
}
}
}
4 changes: 4 additions & 0 deletions packages/blocks/src/database-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DatabaseBlockModel } from './database-model.js';
import type { DatabaseService } from './database-service.js';

export { checkboxPureColumnConfig } from './common/columns/checkbox/define.js';
Expand All @@ -19,5 +20,8 @@ declare global {
interface BlockServices {
'affine:database': DatabaseService;
}
interface BlockModels {
'affine:database': DatabaseBlockModel;
}
}
}
10 changes: 10 additions & 0 deletions packages/blocks/src/divider-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import type { DividerBlockModel } from './divider-model.js';

export * from './divider-block.js';
export * from './divider-model.js';

declare global {
namespace BlockSuite {
interface BlockModels {
'affine:divider': DividerBlockModel;
}
}
}
4 changes: 4 additions & 0 deletions packages/blocks/src/embed-figma-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { noop } from '@blocksuite/global/utils';

import { EmbedFigmaBlockComponent } from './embed-figma-block.js';
import type { EmbedFigmaModel } from './embed-figma-model.js';
import type { EmbedFigmaService } from './embed-figma-service.js';
noop(EmbedFigmaBlockComponent);

Expand All @@ -13,5 +14,8 @@ declare global {
interface BlockServices {
'affine:embed-figma': EmbedFigmaService;
}
interface BlockModels {
'affine:embed-figma': EmbedFigmaModel;
}
}
}
4 changes: 4 additions & 0 deletions packages/blocks/src/embed-github-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { noop } from '@blocksuite/global/utils';

import { EmbedGithubBlockComponent } from './embed-github-block.js';
import type { EmbedGithubModel } from './embed-github-model.js';
import type { EmbedGithubService } from './embed-github-service.js';
noop(EmbedGithubBlockComponent);

Expand All @@ -11,6 +12,9 @@ export * from './embed-github-spec.js';

declare global {
namespace BlockSuite {
interface BlockModels {
'affine:embed-github': EmbedGithubModel;
}
interface BlockServices {
'affine:embed-github': EmbedGithubService;
}
Expand Down
Loading

0 comments on commit f8f6883

Please sign in to comment.