diff --git a/src/SocialShapeUtil.tsx b/src/SocialShapeUtil.tsx index 9143e41..ad23f22 100644 --- a/src/SocialShapeUtil.tsx +++ b/src/SocialShapeUtil.tsx @@ -5,12 +5,13 @@ import { Rectangle2d, TLBaseShape, TLOnResizeHandler, + TLShapeId, resizeBox, toDomPrecision, } from 'tldraw' import { getUserId } from './storeUtils' -export type ValueType = "scalar" | "boolean" | null +export type ValueType = "SCALAR" | "BOOLEAN" | "NONE" export type ISocialShape = TLBaseShape< "social", @@ -21,15 +22,17 @@ export type ISocialShape = TLBaseShape< selector: string valueType: ValueType values: Record + value: any + syntaxError: boolean } > export class SocialShapeUtil extends BaseBoxShapeUtil { static override type = 'social' as const - override canBind = () => false + override canBind = () => true override canEdit = () => false override getDefaultProps(): ISocialShape['props'] { - return { w: 160 * 2, h: 90 * 2, text: '', selector: '', valueType: null, values: {} } + 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) @@ -43,11 +46,10 @@ export class SocialShapeUtil extends BaseBoxShapeUtil { } indicator(shape: ISocialShape) { - const bounds = this.editor.getShapeGeometry(shape).bounds return ( ) @@ -56,19 +58,82 @@ export class SocialShapeUtil extends BaseBoxShapeUtil { override component(shape: ISocialShape) { const currentUser = getUserId(this.editor) - const handleOnChange = (newValue: any) => { + const defaultValues = { + BOOLEAN: false, + SCALAR: 0, + DEFAULT: null + } + + const handleOnChange = (newValue: boolean | number) => { this.updateProps(shape, { values: { ...shape.props.values, [currentUser]: newValue } }) - console.log(shape.props.values) + 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' + } + + if (valueType !== shape.props.valueType) { + this.updateProps(shape, { text, valueType, selector, values: {} }) + } else { + this.updateProps(shape, { text, selector }) + } + this.updateValue(shape.id) } return ( - e.stopPropagation()}> -