Skip to content

Commit e517d16

Browse files
committed
feat: pininfo-change event
fired whenever the pinInfo changes (as a result of changing another attribute, e.g. `flip` for `wokwi-led`)
1 parent 778133f commit e517d16

10 files changed

+88
-13
lines changed

src/7segment-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ export class SevenSegmentElement extends LitElement {
143143
return this.pins === 'extend' ? 2 : 0;
144144
}
145145

146+
update(changedProperties: Map<string, unknown>) {
147+
if (changedProperties.has('digits') || changedProperties.has('pins')) {
148+
this.dispatchEvent(new CustomEvent('pininfo-change'));
149+
}
150+
super.update(changedProperties);
151+
}
152+
146153
renderDigit(x: number, startIndex: number) {
147154
const fill = (index: number) => (this.values[startIndex + index] ? this.color : this.offColor);
148155
return svg`

src/ks2e-m-dc5-element.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ const x4Pos = 71.8;
1111

1212
@customElement('wokwi-ks2e-m-dc5')
1313
export class KS2EMDC5Element extends LitElement {
14-
get pinInfo(): ElementPin[] {
15-
return [
16-
{ name: 'NO2', x: x1Pos, y: y1Pos, signals: [], number: 8 },
17-
{ name: 'NC2', x: x2Pos, y: y1Pos, signals: [], number: 6 },
18-
{ name: 'P2', x: x3Pos, y: y1Pos, signals: [], number: 4 },
19-
{ name: 'COIL2', x: x4Pos, y: y1Pos, signals: [{ type: 'power', signal: 'GND' }], number: 1 },
20-
{ name: 'NO1', x: x1Pos, y: y2Pos, signals: [], number: 9 },
21-
{ name: 'NC1', x: x2Pos, y: y2Pos, signals: [], number: 11 },
22-
{ name: 'P1', x: x3Pos, y: y2Pos, signals: [], number: 13 },
23-
{ name: 'COIL1', x: x4Pos, y: y2Pos, signals: [], number: 16 },
24-
];
25-
}
14+
readonly pinInfo: ElementPin[] = [
15+
{ name: 'NO2', x: x1Pos, y: y1Pos, signals: [], number: 8 },
16+
{ name: 'NC2', x: x2Pos, y: y1Pos, signals: [], number: 6 },
17+
{ name: 'P2', x: x3Pos, y: y1Pos, signals: [], number: 4 },
18+
{ name: 'COIL2', x: x4Pos, y: y1Pos, signals: [{ type: 'power', signal: 'GND' }], number: 1 },
19+
{ name: 'NO1', x: x1Pos, y: y2Pos, signals: [], number: 9 },
20+
{ name: 'NC1', x: x2Pos, y: y2Pos, signals: [], number: 11 },
21+
{ name: 'P1', x: x3Pos, y: y2Pos, signals: [], number: 13 },
22+
{ name: 'COIL1', x: x4Pos, y: y2Pos, signals: [], number: 16 },
23+
];
2624

2725
render() {
2826
return html`

src/lcd1602-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ export class LCD1602Element extends LitElement {
109109
return this.numRows;
110110
}
111111

112+
update(changedProperties: Map<string, unknown>) {
113+
if (changedProperties.has('pins')) {
114+
this.dispatchEvent(new CustomEvent('pininfo-change'));
115+
}
116+
super.update(changedProperties);
117+
}
118+
112119
path(characters: Uint8Array | number[]) {
113120
const xSpacing = 0.6;
114121
const ySpacing = 0.7;

src/led-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export class LEDElement extends LitElement {
5454
`;
5555
}
5656

57+
update(changedProperties: Map<string, unknown>) {
58+
if (changedProperties.has('flip')) {
59+
this.dispatchEvent(new CustomEvent('pininfo-change'));
60+
}
61+
super.update(changedProperties);
62+
}
63+
5764
render() {
5865
const { color, lightColor, flip } = this;
5966
const lightColorActual = lightColor || lightColors[color?.toLowerCase()] || color;

src/led-ring-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ export class LEDRingElement extends LitElement {
105105
this.animationFrame = requestAnimationFrame(this.animateStep);
106106
};
107107

108+
update(changedProperties: Map<string, unknown>) {
109+
if (changedProperties.has('pixels') || changedProperties.has('pixelSpacing')) {
110+
this.dispatchEvent(new CustomEvent('pininfo-change'));
111+
}
112+
super.update(changedProperties);
113+
}
114+
108115
updated() {
109116
if (this.animation && !this.animationFrame) {
110117
this.animationFrame = requestAnimationFrame(this.animateStep);

src/membrane-keypad-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export class MembraneKeypadElement extends LitElement {
6161
}
6262
}
6363

64+
update(changedProperties: Map<string, unknown>) {
65+
if (changedProperties.has('columns')) {
66+
this.dispatchEvent(new CustomEvent('pininfo-change'));
67+
}
68+
super.update(changedProperties);
69+
}
70+
6471
private pressedKeys = new Set<string>();
6572

6673
renderKey(row: number, column: number) {

src/neopixel-matrix-element.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ export class NeopixelMatrixElement extends LitElement {
168168
this.animationFrame = requestAnimationFrame(this.animateStep);
169169
};
170170

171+
update(changedProperties: Map<string, unknown>) {
172+
if (
173+
changedProperties.has('rows') ||
174+
changedProperties.has('cols') ||
175+
changedProperties.has('rowSpacing') ||
176+
changedProperties.has('colSpacing')
177+
) {
178+
this.dispatchEvent(new CustomEvent('pininfo-change'));
179+
}
180+
super.update(changedProperties);
181+
}
182+
171183
updated() {
172184
if (this.animation && !this.animationFrame) {
173185
this.animationFrame = requestAnimationFrame(this.animateStep);

src/slide-potentiometer-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ export class SlidePotentiometerElement extends LitElement {
4747
`;
4848
}
4949

50+
update(changedProperties: Map<string, unknown>) {
51+
if (changedProperties.has('travelLength')) {
52+
this.dispatchEvent(new CustomEvent('pininfo-change'));
53+
}
54+
super.update(changedProperties);
55+
}
56+
5057
render() {
5158
const { value, min: minValue, max: maxValue, travelLength } = this;
5259
// Tip is centered by default

src/stepper-motor-element.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ export class StepperMotorElement extends LitElement {
124124
},
125125
};
126126

127+
update(changedProperties: Map<string, unknown>) {
128+
if (changedProperties.has('size')) {
129+
this.dispatchEvent(new CustomEvent('pininfo-change'));
130+
}
131+
super.update(changedProperties);
132+
}
133+
127134
render() {
128135
const spec = this.nemaSpecMap[this.size] ?? this.nemaSpecMap[defaultSize];
129136

src/utils/show-pins-element.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,31 @@ export class ShowPinsElement extends LitElement {
3030
@property() pinColor = 'red';
3131
@query('#content') elementSlot!: HTMLSlotElement;
3232

33+
previousSlotChild?: ElementWithPinInfo;
34+
3335
get slotChild() {
3436
return this.elementSlot?.assignedElements()[0] as ElementWithPinInfo | undefined;
3537
}
3638

39+
private pinInfoChangeCallback = () => {
40+
this.requestUpdate();
41+
};
42+
43+
handleSlotChange() {
44+
const slotChild = this.slotChild;
45+
if (slotChild !== this.previousSlotChild) {
46+
this.previousSlotChild?.removeEventListener('pininfo-change', this.pinInfoChangeCallback);
47+
slotChild?.addEventListener('pininfo-change', this.pinInfoChangeCallback);
48+
this.previousSlotChild = slotChild;
49+
}
50+
this.requestUpdate();
51+
}
52+
3753
render() {
3854
const pinInfo = this.slotChild?.pinInfo ?? [];
3955
const { pinColor } = this;
4056
return html`<div style="position: relative">
41-
<slot id="content" @slotchange=${() => this.requestUpdate()}></slot>
57+
<slot id="content" @slotchange=${() => this.handleSlotChange()}></slot>
4258
4359
<svg style="position: absolute; top: 0; left: 0" width="100%" height="100%" fill=${pinColor}>
4460
${pinInfo.map(

0 commit comments

Comments
 (0)