refactor(editor): reorg code structure of store package (#9525)

This commit is contained in:
Saul-Mirone
2025-01-05 12:49:02 +00:00
parent 1180e9bc15
commit 3d168ba2d2
55 changed files with 618 additions and 635 deletions
@@ -3,11 +3,6 @@ import { nanoid as nanoidGenerator } from 'nanoid';
export type IdGenerator = () => string;
export function createAutoIncrementIdGenerator(): IdGenerator {
let i = 0;
return () => (i++).toString();
}
export const uuidv4: IdGenerator = () => {
return uuidv4IdGenerator();
};
@@ -1,47 +0,0 @@
import type { z } from 'zod';
import { SYS_KEYS } from '../consts.js';
import { native2Y } from '../reactive/index.js';
import type { BlockModel, BlockSchema } from '../schema/base.js';
import { internalPrimitives } from '../schema/base.js';
import type { YBlock } from '../store/doc/block/index.js';
import type { BlockProps } from '../store/workspace.js';
export function syncBlockProps(
schema: z.infer<typeof BlockSchema>,
model: BlockModel,
yBlock: YBlock,
props: Partial<BlockProps>
) {
const defaultProps = schema.model.props?.(internalPrimitives) ?? {};
Object.entries(props).forEach(([key, value]) => {
if (SYS_KEYS.has(key)) return;
if (value === undefined) return;
// @ts-expect-error allow props
model[key] = value;
});
// set default value
Object.entries(defaultProps).forEach(([key, value]) => {
const notExists =
!yBlock.has(`prop:${key}`) || yBlock.get(`prop:${key}`) === undefined;
if (!notExists) {
return;
}
// @ts-expect-error allow props
model[key] = native2Y(value);
});
}
export const hash = (str: string) => {
return str
.split('')
.reduce(
(prevHash, currVal) =>
((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0,
0
);
};