Skip to content

Commit cfb5511

Browse files
committed
feat(json-crdt-extensions): 🎸 add ability to attach custom data to slice definitions
1 parent fda1abd commit cfb5511

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎src/json-crdt-extensions/peritext/registry/SliceRegistry.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
99

1010
export type TagType = SliceTypeCon | number | string;
1111

12+
const sliceCustomData = new WeakMap<SliceRegistryEntry<any, any, any>, Record<string, unknown>>();
13+
1214
export class SliceRegistryEntry<
1315
Behavior extends SliceBehavior = SliceBehavior,
1416
Tag extends TagType = TagType,
@@ -18,6 +20,14 @@ export class SliceRegistryEntry<
1820
return this.behavior !== SliceBehavior.Marker;
1921
}
2022

23+
public data(): Record<string, unknown> {
24+
const data = sliceCustomData.get(this);
25+
if (data) return data;
26+
const newData = {};
27+
sliceCustomData.set(this, newData);
28+
return newData;
29+
}
30+
2131
constructor(
2232
/**
2333
* Specifies whether the slice is an inline or block element. And if it is
@@ -125,6 +135,10 @@ export class SliceRegistry {
125135
if (tagStr && typeof tagStr === 'string') _fromHtml.set(tagStr, [[entry, () => [tag, null]]]);
126136
}
127137

138+
public get(tag: TagType): SliceRegistryEntry | undefined {
139+
return this.map.get(tag);
140+
}
141+
128142
public isContainer(tag: TagType): boolean {
129143
const entry = this.map.get(tag);
130144
return entry?.container ?? false;

0 commit comments

Comments
 (0)