refactor(editor): move zod schema with model for non surface blocks (#9876)

This commit is contained in:
doodlewind
2025-01-23 10:57:49 +00:00
parent d6cfbec5c9
commit 872a578bf7
6 changed files with 85 additions and 66 deletions

View File

@@ -4,20 +4,43 @@ import type {
} from '@blocksuite/block-std/gfx'; } from '@blocksuite/block-std/gfx';
import { GfxCompatible } from '@blocksuite/block-std/gfx'; import { GfxCompatible } from '@blocksuite/block-std/gfx';
import { BlockModel, defineBlockSchema } from '@blocksuite/store'; import { BlockModel, defineBlockSchema } from '@blocksuite/store';
import { z } from 'zod';
import { import {
FontFamily, FontFamily,
FontFamilySchema,
FontStyle, FontStyle,
FontStyleSchema,
FontWeight, FontWeight,
FontWeightSchema,
TextAlign, TextAlign,
TextAlignSchema,
type TextStyleProps, type TextStyleProps,
} from '../../consts/index.js'; } from '../../consts/index.js';
import { ColorSchema } from '../../themes/color.js';
import { DefaultTheme } from '../../themes/default.js';
type EdgelessTextProps = { type EdgelessTextProps = {
hasMaxWidth: boolean; hasMaxWidth: boolean;
} & Omit<TextStyleProps, 'fontSize'> & } & Omit<TextStyleProps, 'fontSize'> &
GfxCommonBlockProps; GfxCommonBlockProps;
export const EdgelessTextZodSchema = z
.object({
color: ColorSchema,
fontFamily: FontFamilySchema,
fontWeight: FontWeightSchema,
fontStyle: FontStyleSchema,
textAlign: TextAlignSchema,
})
.default({
color: DefaultTheme.textColor,
fontFamily: FontFamily.Inter,
fontWeight: FontWeight.Regular,
fontStyle: FontStyle.Normal,
textAlign: TextAlign.Left,
});
export const EdgelessTextBlockSchema = defineBlockSchema({ export const EdgelessTextBlockSchema = defineBlockSchema({
flavour: 'affine:edgeless-text', flavour: 'affine:edgeless-text',
props: (): EdgelessTextProps => ({ props: (): EdgelessTextProps => ({

View File

@@ -16,8 +16,9 @@ import {
} from '@blocksuite/block-std/gfx'; } from '@blocksuite/block-std/gfx';
import { Bound } from '@blocksuite/global/utils'; import { Bound } from '@blocksuite/global/utils';
import { BlockModel, defineBlockSchema, type Text } from '@blocksuite/store'; import { BlockModel, defineBlockSchema, type Text } from '@blocksuite/store';
import { z } from 'zod';
import type { Color } from '../../themes/index.js'; import { type Color, ColorSchema } from '../../themes/index.js';
export type FrameBlockProps = { export type FrameBlockProps = {
title: Text; title: Text;
@@ -26,6 +27,12 @@ export type FrameBlockProps = {
presentationIndex?: string; presentationIndex?: string;
} & GfxCompatibleProps; } & GfxCompatibleProps;
export const FrameZodSchema = z
.object({
background: ColorSchema.optional(),
})
.default({});
export const FrameBlockSchema = defineBlockSchema({ export const FrameBlockSchema = defineBlockSchema({
flavour: 'affine:frame', flavour: 'affine:frame',
props: (internal): FrameBlockProps => ({ props: (internal): FrameBlockProps => ({

View File

@@ -5,6 +5,7 @@ import type {
import { GfxCompatible } from '@blocksuite/block-std/gfx'; import { GfxCompatible } from '@blocksuite/block-std/gfx';
import { Bound } from '@blocksuite/global/utils'; import { Bound } from '@blocksuite/global/utils';
import { BlockModel, defineBlockSchema } from '@blocksuite/store'; import { BlockModel, defineBlockSchema } from '@blocksuite/store';
import { z } from 'zod';
import { import {
DEFAULT_NOTE_BORDER_SIZE, DEFAULT_NOTE_BORDER_SIZE,
@@ -14,9 +15,38 @@ import {
DEFAULT_NOTE_SHADOW, DEFAULT_NOTE_SHADOW,
DEFAULT_NOTE_WIDTH, DEFAULT_NOTE_WIDTH,
NoteDisplayMode, NoteDisplayMode,
NoteDisplayModeSchema,
NoteShadowsSchema,
type StrokeStyle, type StrokeStyle,
} from '../../consts/index.js'; StrokeStyleSchema,
import { type Color, DefaultTheme } from '../../themes/index.js'; } from '../../consts/note';
import { type Color, ColorSchema, DefaultTheme } from '../../themes';
export const NoteZodSchema = z
.object({
background: ColorSchema,
displayMode: NoteDisplayModeSchema,
edgeless: z.object({
style: z.object({
borderRadius: z.number(),
borderSize: z.number(),
borderStyle: StrokeStyleSchema,
shadowType: NoteShadowsSchema,
}),
}),
})
.default({
background: DefaultTheme.noteBackgrounColor,
displayMode: NoteDisplayMode.EdgelessOnly,
edgeless: {
style: {
borderRadius: DEFAULT_NOTE_CORNER,
borderSize: DEFAULT_NOTE_BORDER_SIZE,
borderStyle: DEFAULT_NOTE_BORDER_STYLE,
shadowType: DEFAULT_NOTE_SHADOW,
},
},
});
export const NoteBlockSchema = defineBlockSchema({ export const NoteBlockSchema = defineBlockSchema({
flavour: 'affine:note', flavour: 'affine:note',

View File

@@ -44,6 +44,10 @@ export enum StrokeStyle {
Solid = 'solid', Solid = 'solid',
} }
export const StrokeStyleSchema = z.nativeEnum(StrokeStyle);
export const NoteDisplayModeSchema = z.nativeEnum(NoteDisplayMode);
export const DEFAULT_NOTE_BORDER_STYLE = StrokeStyle.None; export const DEFAULT_NOTE_BORDER_STYLE = StrokeStyle.None;
export const StrokeStyleMap = createEnumMap(StrokeStyle); export const StrokeStyleMap = createEnumMap(StrokeStyle);

View File

@@ -1,3 +1,5 @@
import { z } from 'zod';
import type { Color } from '../themes/color.js'; import type { Color } from '../themes/color.js';
import { createEnumMap } from '../utils/enum.js'; import { createEnumMap } from '../utils/enum.js';
@@ -59,3 +61,8 @@ export enum TextResizing {
AUTO_WIDTH_AND_HEIGHT, AUTO_WIDTH_AND_HEIGHT,
AUTO_HEIGHT, AUTO_HEIGHT,
} }
export const FontFamilySchema = z.nativeEnum(FontFamily);
export const FontWeightSchema = z.nativeEnum(FontWeight);
export const FontStyleSchema = z.nativeEnum(FontStyle);
export const TextAlignSchema = z.nativeEnum(TextAlign);

View File

@@ -2,39 +2,35 @@ import {
ColorSchema, ColorSchema,
ConnectorMode, ConnectorMode,
DEFAULT_FRONT_END_POINT_STYLE, DEFAULT_FRONT_END_POINT_STYLE,
DEFAULT_NOTE_BORDER_SIZE,
DEFAULT_NOTE_BORDER_STYLE,
DEFAULT_NOTE_CORNER,
DEFAULT_NOTE_SHADOW,
DEFAULT_REAR_END_POINT_STYLE, DEFAULT_REAR_END_POINT_STYLE,
DEFAULT_ROUGHNESS, DEFAULT_ROUGHNESS,
DefaultTheme, DefaultTheme,
EdgelessTextZodSchema,
FontFamily, FontFamily,
FontFamilySchema,
FontStyle, FontStyle,
FontStyleSchema,
FontWeight, FontWeight,
FontWeightSchema,
FrameZodSchema,
LayoutType, LayoutType,
LineWidth, LineWidth,
MindmapStyle, MindmapStyle,
NoteDisplayMode, NoteZodSchema,
NoteShadowsSchema,
PointStyle, PointStyle,
ShapeStyle, ShapeStyle,
StrokeStyle, StrokeStyle,
StrokeStyleSchema,
TextAlign, TextAlign,
TextAlignSchema,
TextVerticalAlign, TextVerticalAlign,
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { z, ZodDefault, ZodObject, type ZodTypeAny, ZodUnion } from 'zod'; import { z, ZodDefault, ZodObject, type ZodTypeAny, ZodUnion } from 'zod';
const ConnectorEndpointSchema = z.nativeEnum(PointStyle); const ConnectorEndpointSchema = z.nativeEnum(PointStyle);
const StrokeStyleSchema = z.nativeEnum(StrokeStyle);
const LineWidthSchema = z.nativeEnum(LineWidth); const LineWidthSchema = z.nativeEnum(LineWidth);
const ShapeStyleSchema = z.nativeEnum(ShapeStyle); const ShapeStyleSchema = z.nativeEnum(ShapeStyle);
const FontFamilySchema = z.nativeEnum(FontFamily);
const FontWeightSchema = z.nativeEnum(FontWeight);
const FontStyleSchema = z.nativeEnum(FontStyle);
const TextAlignSchema = z.nativeEnum(TextAlign);
const TextVerticalAlignSchema = z.nativeEnum(TextVerticalAlign); const TextVerticalAlignSchema = z.nativeEnum(TextVerticalAlign);
const NoteDisplayModeSchema = z.nativeEnum(NoteDisplayMode);
const ConnectorModeSchema = z.nativeEnum(ConnectorMode); const ConnectorModeSchema = z.nativeEnum(ConnectorMode);
const LayoutTypeSchema = z.nativeEnum(LayoutType); const LayoutTypeSchema = z.nativeEnum(LayoutType);
const MindmapStyleSchema = z.nativeEnum(MindmapStyle); const MindmapStyleSchema = z.nativeEnum(MindmapStyle);
@@ -145,48 +141,6 @@ export const TextSchema = z
textAlign: TextAlign.Left, textAlign: TextAlign.Left,
}); });
export const EdgelessTextSchema = z
.object({
color: ColorSchema,
fontFamily: FontFamilySchema,
fontWeight: FontWeightSchema,
fontStyle: FontStyleSchema,
textAlign: TextAlignSchema,
})
.default({
color: DefaultTheme.textColor,
fontFamily: FontFamily.Inter,
fontWeight: FontWeight.Regular,
fontStyle: FontStyle.Normal,
textAlign: TextAlign.Left,
});
export const NoteSchema = z
.object({
background: ColorSchema,
displayMode: NoteDisplayModeSchema,
edgeless: z.object({
style: z.object({
borderRadius: z.number(),
borderSize: z.number(),
borderStyle: StrokeStyleSchema,
shadowType: NoteShadowsSchema,
}),
}),
})
.default({
background: DefaultTheme.noteBackgrounColor,
displayMode: NoteDisplayMode.EdgelessOnly,
edgeless: {
style: {
borderRadius: DEFAULT_NOTE_CORNER,
borderSize: DEFAULT_NOTE_BORDER_SIZE,
borderStyle: DEFAULT_NOTE_BORDER_STYLE,
shadowType: DEFAULT_NOTE_SHADOW,
},
},
});
export const MindmapSchema = z export const MindmapSchema = z
.object({ .object({
layoutType: LayoutTypeSchema, layoutType: LayoutTypeSchema,
@@ -197,20 +151,14 @@ export const MindmapSchema = z
style: MindmapStyle.ONE, style: MindmapStyle.ONE,
}); });
export const FrameSchema = z
.object({
background: ColorSchema.optional(),
})
.default({});
export const NodePropsSchema = z.object({ export const NodePropsSchema = z.object({
connector: ConnectorSchema, connector: ConnectorSchema,
brush: BrushSchema, brush: BrushSchema,
text: TextSchema, text: TextSchema,
mindmap: MindmapSchema, mindmap: MindmapSchema,
'affine:edgeless-text': EdgelessTextSchema, 'affine:edgeless-text': EdgelessTextZodSchema,
'affine:note': NoteSchema, 'affine:note': NoteZodSchema,
'affine:frame': FrameSchema, 'affine:frame': FrameZodSchema,
// shapes // shapes
'shape:diamond': ShapeSchema, 'shape:diamond': ShapeSchema,
'shape:ellipse': ShapeSchema, 'shape:ellipse': ShapeSchema,