add inputs
This commit is contained in:
parent
987902c3be
commit
5a58ed4ebc
|
|
@ -1,7 +1,6 @@
|
||||||
import {
|
import {
|
||||||
BaseBoxShapeUtil,
|
BaseBoxShapeUtil,
|
||||||
HTMLContainer,
|
HTMLContainer,
|
||||||
// TLArrowShape,
|
|
||||||
TLBaseShape,
|
TLBaseShape,
|
||||||
TLGeoShape,
|
TLGeoShape,
|
||||||
TLOnResizeHandler,
|
TLOnResizeHandler,
|
||||||
|
|
@ -11,8 +10,6 @@ import { getEdge } from "./propagators/tlgraph"
|
||||||
import { llm } from "./llm"
|
import { llm } from "./llm"
|
||||||
import { isShapeOfType } from "./propagators/utils"
|
import { isShapeOfType } from "./propagators/utils"
|
||||||
import { ISocialShape } from "./SocialShapeUtil"
|
import { ISocialShape } from "./SocialShapeUtil"
|
||||||
// import TextInput from "react-autocomplete-input"
|
|
||||||
// import "react-autocomplete-input/dist/bundle.css"
|
|
||||||
|
|
||||||
type IPrompt = TLBaseShape<
|
type IPrompt = TLBaseShape<
|
||||||
"prompt",
|
"prompt",
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,19 @@
|
||||||
import {
|
import {
|
||||||
BaseBoxShapeUtil,
|
BaseBoxShapeUtil,
|
||||||
|
Editor,
|
||||||
Geometry2d,
|
Geometry2d,
|
||||||
HTMLContainer,
|
HTMLContainer,
|
||||||
Rectangle2d,
|
Rectangle2d,
|
||||||
TLBaseShape,
|
TLBaseShape,
|
||||||
TLOnResizeHandler,
|
TLOnResizeHandler,
|
||||||
|
TLShape,
|
||||||
TLShapeId,
|
TLShapeId,
|
||||||
resizeBox,
|
resizeBox,
|
||||||
} from 'tldraw'
|
} from 'tldraw'
|
||||||
import { getUserId } from './storeUtils'
|
import { getUserId } from './storeUtils'
|
||||||
|
import { getEdge } from './propagators/tlgraph'
|
||||||
|
|
||||||
export type ValueType = "SCALAR" | "BOOLEAN" | "STRING" | "NONE"
|
export type ValueType = "SCALAR" | "BOOLEAN" | "STRING" | "RANK" | "NONE"
|
||||||
|
|
||||||
export type ISocialShape = TLBaseShape<
|
export type ISocialShape = TLBaseShape<
|
||||||
"social",
|
"social",
|
||||||
|
|
@ -78,6 +81,8 @@ export class SocialShapeUtil extends BaseBoxShapeUtil<ISocialShape> {
|
||||||
valueType = 'BOOLEAN'
|
valueType = 'BOOLEAN'
|
||||||
} else if (text.includes('STRING')) {
|
} else if (text.includes('STRING')) {
|
||||||
valueType = 'STRING'
|
valueType = 'STRING'
|
||||||
|
} else if (text.includes('RANK')) {
|
||||||
|
valueType = 'RANK'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valueType !== shape.props.valueType) {
|
if (valueType !== shape.props.valueType) {
|
||||||
|
|
@ -103,21 +108,10 @@ export class SocialShapeUtil extends BaseBoxShapeUtil<ISocialShape> {
|
||||||
private updateValue(shapeId: TLShapeId) {
|
private updateValue(shapeId: TLShapeId) {
|
||||||
const shape = this.editor.getShape(shapeId) as ISocialShape
|
const shape = this.editor.getShape(shapeId) as ISocialShape
|
||||||
const valueType = shape.props.valueType
|
const valueType = shape.props.valueType
|
||||||
// console.log("SHAPE", shape)
|
|
||||||
const vals = Array.from(Object.values(shape.props.values))
|
const vals = Array.from(Object.values(shape.props.values))
|
||||||
// console.log("VALS", vals)
|
|
||||||
|
|
||||||
// Check for "sum" or "average" followed by whitespace or end of string
|
|
||||||
// const invalidFunctionUsage = /\b(sum|average)(\s|$)/.test(shape.props.text)
|
|
||||||
|
|
||||||
// if (invalidFunctionUsage) {
|
|
||||||
// this.updateProps(shape, { syntaxError: true })
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
const functionBody = `return ${shape.props.text.replace(valueType, 'VALUES')};`
|
const functionBody = `return ${shape.props.text.replace(valueType, 'VALUES')};`
|
||||||
|
|
||||||
// console.log("FUNCTION BODY", functionBody)
|
|
||||||
const sum = (vals: number[] | boolean[]) => {
|
const sum = (vals: number[] | boolean[]) => {
|
||||||
if (valueType === 'SCALAR') {
|
if (valueType === 'SCALAR') {
|
||||||
return (vals as number[]).reduce((acc, val) => acc + val, 0)
|
return (vals as number[]).reduce((acc, val) => acc + val, 0)
|
||||||
|
|
@ -137,11 +131,15 @@ export class SocialShapeUtil extends BaseBoxShapeUtil<ISocialShape> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const inputMap = getInputMap(this.editor, shape)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const func = new Function('sum', 'average', 'VALUES', functionBody)
|
const paramNames = ['sum', 'average', 'VALUES', ...Object.keys(inputMap)]
|
||||||
const result = func(sum, average, vals)
|
const paramValues = [sum, average, vals, ...Object.values(inputMap).map(s => s.value)]
|
||||||
const resultIsFunction = typeof result === 'function'
|
const func = new Function(...paramNames, functionBody)
|
||||||
if (resultIsFunction) {
|
const result = func(...paramValues)
|
||||||
|
|
||||||
|
if (typeof result === 'function') {
|
||||||
this.updateProps({ ...shape, props: { ...shape.props, value: null } }, { syntaxError: true })
|
this.updateProps({ ...shape, props: { ...shape.props, value: null } }, { syntaxError: true })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -241,4 +239,32 @@ function ValueInterface({ type, value, values, onChange }: { type: ValueType; va
|
||||||
default:
|
default:
|
||||||
return <div style={{ marginTop: 10, textAlign: 'center' }}>No Interface...</div>
|
return <div style={{ marginTop: 10, textAlign: 'center' }}>No Interface...</div>
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInputMap(editor: Editor, shape: TLShape) {
|
||||||
|
const arrowBindings = editor.getBindingsInvolvingShape(
|
||||||
|
shape.id,
|
||||||
|
"arrow",
|
||||||
|
)
|
||||||
|
const arrows = arrowBindings
|
||||||
|
.map((binding) => editor.getShape(binding.fromId))
|
||||||
|
|
||||||
|
return arrows.reduce((acc, arrow) => {
|
||||||
|
const edge = getEdge(arrow, editor);
|
||||||
|
if (edge && edge.to === shape.id) {
|
||||||
|
const sourceShape = editor.getShape(edge.from);
|
||||||
|
if (sourceShape && edge.text) {
|
||||||
|
acc[edge.text] = { value: sourceShape.props.value || sourceShape.props.text || null, shapeId: sourceShape.id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, { value: any, shapeId: TLShapeId }>);
|
||||||
|
}
|
||||||
|
|
||||||
|
function listenToShape(editor: Editor, shapeId: TLShapeId, callback: (prev: TLShape, next: TLShape) => void) {
|
||||||
|
return editor.sideEffects.registerAfterChangeHandler<'shape'>('shape', (prev, next) => {
|
||||||
|
if (next.id === shapeId) {
|
||||||
|
callback(prev, next)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue