mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
refactor(editor): support dynamic text attribute key (#12947)
#### PR Dependency Tree * **PR #12946** * **PR #12947** 👈 * **PR #12948** This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal)
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
type DeltaInsert,
|
||||
type ExtensionType,
|
||||
} from '@blocksuite/store';
|
||||
import { z, type ZodObject, type ZodTypeAny } from 'zod';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { StdIdentifier } from '../../identifier.js';
|
||||
import type { BlockStdScope } from '../../scope/index.js';
|
||||
@@ -42,20 +42,10 @@ export class InlineManager<TextAttributes extends BaseTextAttributes> {
|
||||
return renderer;
|
||||
};
|
||||
|
||||
getSchema = (): ZodObject<Record<keyof TextAttributes, ZodTypeAny>> => {
|
||||
const defaultSchema = baseTextAttributes as unknown as ZodObject<
|
||||
Record<keyof TextAttributes, ZodTypeAny>
|
||||
>;
|
||||
|
||||
const schema: ZodObject<Record<keyof TextAttributes, ZodTypeAny>> =
|
||||
this.specs.reduce((acc, cur) => {
|
||||
const currentSchema = z.object({
|
||||
[cur.name]: cur.schema,
|
||||
}) as ZodObject<Record<keyof TextAttributes, ZodTypeAny>>;
|
||||
return acc.merge(currentSchema) as ZodObject<
|
||||
Record<keyof TextAttributes, ZodTypeAny>
|
||||
>;
|
||||
}, defaultSchema);
|
||||
getSchema = (): z.ZodSchema => {
|
||||
const schema = this.specs.reduce<z.ZodSchema>((acc, cur) => {
|
||||
return z.intersection(acc, cur.schema);
|
||||
}, baseTextAttributes);
|
||||
return schema;
|
||||
};
|
||||
|
||||
|
||||
@@ -5,13 +5,19 @@ import type {
|
||||
} from '@blocksuite/std/inline';
|
||||
import type { BaseTextAttributes, DeltaInsert } from '@blocksuite/store';
|
||||
import type * as Y from 'yjs';
|
||||
import type { ZodTypeAny } from 'zod';
|
||||
import type { AnyZodObject, KeySchema, ZodEffects, ZodRecord } from 'zod';
|
||||
|
||||
export type InlineSpecs<
|
||||
TextAttributes extends BaseTextAttributes = BaseTextAttributes,
|
||||
> = {
|
||||
name: keyof TextAttributes | string;
|
||||
schema: ZodTypeAny;
|
||||
schema:
|
||||
| AnyZodObject
|
||||
| ZodEffects<
|
||||
ZodRecord<KeySchema>,
|
||||
Partial<Record<string, unknown>>,
|
||||
unknown
|
||||
>;
|
||||
match: (delta: DeltaInsert<TextAttributes>) => boolean;
|
||||
renderer: AttributeRenderer<TextAttributes>;
|
||||
embed?: boolean;
|
||||
|
||||
26
blocksuite/framework/std/src/inline/utils/dynamic-schema.ts
Normal file
26
blocksuite/framework/std/src/inline/utils/dynamic-schema.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { z, type ZodTypeAny } from 'zod';
|
||||
|
||||
export function dynamicSchema<Key extends string, Value extends ZodTypeAny>(
|
||||
keyValidator: (key: string) => key is Key,
|
||||
valueType: Value
|
||||
) {
|
||||
return z.preprocess(
|
||||
record => {
|
||||
// check it is a record
|
||||
if (typeof record !== 'object' || record === null) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return Object.entries(record)
|
||||
.filter((data): data is [Key, unknown] => keyValidator(data[0]))
|
||||
.reduce(
|
||||
(acc, [key, value]) => {
|
||||
acc[key] = value;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<Key, unknown>
|
||||
);
|
||||
},
|
||||
z.record(z.custom<Key>(keyValidator), valueType)
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './attribute-renderer.js';
|
||||
export * from './delta-convert.js';
|
||||
export * from './dynamic-schema.js';
|
||||
export * from './embed.js';
|
||||
export * from './guard.js';
|
||||
export * from './point-conversion.js';
|
||||
|
||||
Reference in New Issue
Block a user