|
| 1 | +import { assertParamsObject, defineBlockKind } from "@platforma-sdk/block-kind"; |
| 2 | +import { invariant, isPlainObject } from "es-toolkit"; |
| 3 | +import { |
| 4 | + isColumnUniversalId, |
| 5 | + type AnnotationSpecUi as SdkAnnotationSpecUi, |
| 6 | + type ColumnUniversalId, |
| 7 | + type FilterSpec as SdkFilterSpec, |
| 8 | + type FilterSpecLeaf, |
| 9 | + type FilterSpecUi as SdkFilterSpecUi, |
| 10 | +} from "@platforma-sdk/model"; |
| 11 | +import { name, version } from "../package.json" with { type: "json" }; |
| 12 | + |
| 13 | +export type FilterSpec = SdkFilterSpec< |
| 14 | + FilterSpecLeaf, |
| 15 | + { id: number; name?: string; isExpanded?: boolean } |
| 16 | +>; |
| 17 | + |
| 18 | +export type FilterSpecUI = SdkFilterSpecUi<Extract<FilterSpec, { type: "and" | "or" }>> & { |
| 19 | + id: number; |
| 20 | +}; |
| 21 | + |
| 22 | +export type AnnotationSpecUi = SdkAnnotationSpecUi<FilterSpecUI> & { defaultValue?: string }; |
| 23 | + |
| 24 | +export type BlockParams = { |
| 25 | + inputAnchor?: ColumnUniversalId; |
| 26 | + annotationSpecUi?: AnnotationSpecUi; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Both fields are optional, so a params object that sets neither is valid — a block |
| 31 | + * seeded with nothing to browse and nothing annotated is a state the UI reaches too. |
| 32 | + * Only the two declared fields are read; anything else in the object is dropped here |
| 33 | + * rather than refused, so the returned value is the whole of what the block receives. |
| 34 | + */ |
| 35 | +function parseInitializationParams(value: unknown): BlockParams { |
| 36 | + assertParamsObject(value); |
| 37 | + |
| 38 | + const { inputAnchor, annotationSpecUi } = value; |
| 39 | + |
| 40 | + if (inputAnchor !== undefined && !isColumnUniversalId(inputAnchor)) { |
| 41 | + throw new Error("'inputAnchor' must be a column id."); |
| 42 | + } |
| 43 | + if (annotationSpecUi !== undefined) assertAnnotationSpec(annotationSpecUi); |
| 44 | + |
| 45 | + return { inputAnchor, annotationSpecUi }; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * The shape of an annotation script, checked only as far as the editor's own states go: |
| 50 | + * a step carries a label and a filter from the moment it is added, both still empty |
| 51 | + * until the user fills them in. Rejecting an empty label here would refuse a script |
| 52 | + * the block itself can produce and export. |
| 53 | + */ |
| 54 | +function assertAnnotationSpec(spec: unknown): asserts spec is AnnotationSpecUi { |
| 55 | + invariant(isPlainObject(spec), "'annotationSpecUi' must be an object."); |
| 56 | + invariant(typeof spec.title === "string", "'annotationSpecUi.title' must be a string."); |
| 57 | + invariant( |
| 58 | + spec.defaultValue === undefined || typeof spec.defaultValue === "string", |
| 59 | + "'annotationSpecUi.defaultValue' must be a string.", |
| 60 | + ); |
| 61 | + invariant(Array.isArray(spec.steps), "'annotationSpecUi.steps' must be an array."); |
| 62 | + |
| 63 | + spec.steps.forEach((step: unknown, i: number) => { |
| 64 | + invariant(isPlainObject(step), `Annotation step ${i} must be an object.`); |
| 65 | + invariant(typeof step.label === "string", `Annotation step ${i} must have a string label.`); |
| 66 | + invariant(isPlainObject(step.filter), `Annotation step ${i} must have a filter object.`); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +export const kind = defineBlockKind<BlockParams>({ name, version, parseInitializationParams }); |
0 commit comments