Skip to content

Commit

Permalink
experimental propagator syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
OrionReed committed Nov 26, 2024
1 parent 79cc0fa commit ce2b058
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
5 changes: 3 additions & 2 deletions demo/event-propagator.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
source="#box1"
target="#box2"
triggers="click"
expression="$target.textContent += '!'"
expression="textContent: _ + '!'"
></event-propagator>

<fc-geometry id="box3" x="350" y="200" width="30" height="30"></fc-geometry>
Expand All @@ -47,7 +47,8 @@
source="#box3"
target="#box4"
triggers="move"
expression="$target.y = $source.x"
expression="y: from.x,
rotate: from.x"
></event-propagator>

<script type="module">
Expand Down
59 changes: 53 additions & 6 deletions src/arrows/event-propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,64 @@ export class EventPropagator extends FolkRope {
}

#expression = '';
#function = new Function();
#function: Function | null = null;
get expression() {
return this.#expression;
}
set expression(expression) {
this.mend();
this.#expression = expression;
try {
this.#function = new Function('$source', '$target', '$event', expression);
const processedExp = expression.trim();

// Process each line, looking for the first ':' to separate key from value
const processedProps = processedExp
.split('\n')
.map((line) => {
const line_trimmed = line.trim();
if (!line_trimmed || line_trimmed === '{' || line_trimmed === '}') return '';

// Remove trailing comma if it exists
const withoutComma = line_trimmed.replace(/,\s*$/, '');

const colonIndex = withoutComma.indexOf(':');
if (colonIndex === -1) return withoutComma;

const key = withoutComma.slice(0, colonIndex).trim();
const value = withoutComma.slice(colonIndex + 1).trim();

return `${key}: (function() { const _ = to[${JSON.stringify(key)}]; return ${value}; })()`;
})
.filter((line) => line)
.join(',\n');

this.#function = new Function(
'from',
'to',
'event',
`
return {
${processedProps}
};
`
);

console.log(processedProps);

this.#function = new Function(
'from',
'to',
'event',
`
return {
${processedProps}
};
`
);
} catch (error) {
console.warn('Failed to parse expression:', error);
// Use no-op function when parsing fails
this.cut();
this.#function = () => {};
this.#function = null;
}
}

Expand Down Expand Up @@ -133,13 +177,16 @@ export class EventPropagator extends FolkRope {
super.unobserveTarget();
}

// Do we need the event at all?
evaluateExpression = (event?: Event) => {
if (this.sourceElement === null || this.targetElement === null) return;
this.stroke = 'black';
if (!this.#function) return;

try {
this.#function(this.sourceElement, this.targetElement, event);
const assignments = this.#function(this.sourceElement, this.targetElement, event);
Object.assign(this.targetElement, assignments);
} catch (error) {
console.warn('Failed to parse expression:', error);
this.stroke = 'red';
}
};
Expand Down

0 comments on commit ce2b058

Please sign in to comment.