mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 04:48:53 +00:00
feat(core): editor setting service (#7956)
define editor setting schema in `packages/frontend/core/src/modules/editor-settting/schema.ts`
e.g.
```ts
const BSEditorSettingSchema = z.object({
connector: z.object({
stroke: z
.union([
z.string(),
z.object({
dark: z.string(),
light: z.string(),
}),
])
.default('#000000'), // default is necessary
}),
});
```
schema can be defined in a nested way. EditorSetting api is in flat way:
editorSetting api:
```ts
editorSetting.settings$ === {
'connector.stroke': '#000000'
}
editorSetting.set('connector.stroke', '#000')
```
and use `expandFlattenObject` function can restore the flattened structure to a nested structure. nested structure is required by blocksuite
```ts
editorSetting.settings$.map(expandFlattenObject) === {
connector: {
stroke: '#000000'
}
}
```
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { expect, test } from 'vitest';
|
||||
|
||||
import { unflattenObject } from '../unflatten-object';
|
||||
|
||||
test('unflattenObject', () => {
|
||||
const ob = {
|
||||
'a.b.c': 1,
|
||||
d: 2,
|
||||
};
|
||||
const result = unflattenObject(ob);
|
||||
expect(result).toEqual({
|
||||
a: {
|
||||
b: {
|
||||
c: 1,
|
||||
},
|
||||
},
|
||||
d: 2,
|
||||
});
|
||||
});
|
||||
@@ -5,3 +5,4 @@ export * from './fractional-indexing';
|
||||
export * from './popup';
|
||||
export * from './string2color';
|
||||
export * from './toast';
|
||||
export * from './unflatten-object';
|
||||
|
||||
22
packages/frontend/core/src/utils/unflatten-object.ts
Normal file
22
packages/frontend/core/src/utils/unflatten-object.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export function unflattenObject(ob: any) {
|
||||
const result: any = {};
|
||||
|
||||
for (const key in ob) {
|
||||
if (!Object.prototype.hasOwnProperty.call(ob, key)) continue;
|
||||
|
||||
const keys = key.split('.');
|
||||
let current = result;
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const k = keys[i];
|
||||
if (i === keys.length - 1) {
|
||||
current[k] = ob[key];
|
||||
} else {
|
||||
current[k] = current[k] || {};
|
||||
current = current[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user