Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 57x 975x 975x 1x 974x 1x 973x 975x 975x 15453x 15453x 15453x 1x 15452x | import type { Slice } from "./slice";
export class EditorContext {
private slices = new Map<string, Slice>();
register<T extends Slice>(SliceClass: { sliceKey: string; new (ctx: EditorContext): T }): T;
register<T extends Slice>(
SliceClass: { sliceKey: string },
factory: (ctx: EditorContext) => T,
): T;
register<T extends Slice>(
SliceClass: { sliceKey: string; new (ctx: EditorContext): T },
factory?: (ctx: EditorContext) => T,
): T {
const key = SliceClass.sliceKey;
if (key === "unknown") {
throw new Error(
`Slice class must define a static "sliceKey" property. ` +
`Example: static readonly sliceKey = "my-slice";`,
);
}
if (this.slices.has(key)) {
throw new Error(
`Slice "${key}" is already registered. ` +
`Available slices: ${[...this.slices.keys()].join(", ")}`,
);
}
const slice = factory ? factory(this) : new SliceClass(this);
this.slices.set(key, slice);
return slice;
}
get<T extends Slice>(SliceClass: { sliceKey: string; new (...args: never[]): T }): T {
const key = SliceClass.sliceKey;
const slice = this.slices.get(key);
if (!slice) {
throw new Error(
`Slice "${key}" is not registered. ` +
`Available slices: ${[...this.slices.keys()].join(", ")}`,
);
}
return slice as T;
}
}
|