import { BaseBoxShapeUtil, Editor, Geometry2d, HTMLContainer, Rectangle2d, TLBaseShape, TLOnResizeHandler, TLShape, TLShapeId, resizeBox, } from "tldraw"; import { getUserId } from "./storeUtils"; import { getEdge } from "./propagators/tlgraph"; export type ValueType = "SCALAR" | "BOOLEAN" | "STRING" | "RANK" | "NONE"; export type ISocialShape = TLBaseShape< "social", { w: number; h: number; text: string; selector: string; valueType: ValueType; values: Record; value: any; syntaxError: boolean; } >; export class SocialShapeUtil extends BaseBoxShapeUtil { static override type = "social" as const; private valueTypeRegex = (valueType: ValueType) => new RegExp(`${valueType}\\s*\\((.*?)\\)|${valueType}`); override canBind = () => true; override canEdit = () => false; override getDefaultProps(): ISocialShape["props"] { return { w: 160 * 2, h: 90 * 2, text: "", selector: "", valueType: "NONE", values: {}, value: null, syntaxError: false, }; } override onResize: TLOnResizeHandler = (shape, info) => { return resizeBox(shape, info); }; override getGeometry(shape: ISocialShape): Geometry2d { return new Rectangle2d({ width: shape.props.w, height: shape.props.h, isFilled: true, }); } indicator(shape: ISocialShape) { return ; } override component(shape: ISocialShape) { const currentUser = getUserId(this.editor); const defaultValues = { BOOLEAN: false, SCALAR: 0, DEFAULT: null, }; const handleOnChange = (newValue: boolean | number) => { console.log("NEW VALUE", newValue); this.updateProps(shape, { values: { ...shape.props.values, [currentUser]: newValue }, }); this.updateValue(shape.id); }; const handleTextChange = (text: string) => { let valueType: ValueType = "NONE"; const selector = text.match(/@([a-zA-Z]+)/)?.[1] || ""; if (text.includes("SCALAR")) { valueType = "SCALAR"; } else if (text.includes("BOOLEAN")) { valueType = "BOOLEAN"; } else if (text.includes("STRING")) { valueType = "STRING"; } else if (text.includes("RANK")) { valueType = "RANK"; } if (valueType !== shape.props.valueType) { this.updateProps(shape, { text, valueType, selector, values: {} }); } else { this.updateProps(shape, { text, selector }); } this.updateValue(shape.id); }; const args = this.getArgs(shape, shape.props.valueType); const inputMap = getInputMap(this.editor, shape); const usedInputs: any[] = []; for (const arg of args) { if (arg !== false && arg !== true && inputMap[arg]) { if (Array.isArray(inputMap[arg].value)) { usedInputs.push(...inputMap[arg].value); } else { usedInputs.push(inputMap[arg].value); } } } // console.log("USED INPUTS", usedInputs) return ( e.stopPropagation()} >