Skip to content

Commit 8719ac7

Browse files
committed
feat(utils): add wokwi-show-pins element for debugging
1 parent d1043a2 commit 8719ac7

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

CONTRIBUTING.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Contributing to Wokwi Elements
22

3-
First of all, thank you for considering contributing to Wokwi Elements!
3+
First of all, thank you for considering contributing to Wokwi Elements!
44
Please go over these guidelines to ensure that your contribution lands
55
successfully.
66

77
## Creating a New Element
88

9-
Before starting to work on a new element, please
9+
Before starting to work on a new element, please
1010
[file an issue](https://github.com/wokwi/wokwi-elements/issues/new/choose)
11-
to discuss the implementation.
11+
to discuss the implementation.
1212

1313
Also, please make sure that any 3d-party graphics that you use
1414
for the element is released under a permissive license (such as
15-
MIT, BSD, Apache, or CC-BY), and that you give credits to the
15+
MIT, BSD, Apache, or CC-BY), and that you give credits to the
1616
original authors as required.
1717

1818
To create the element, use our hygen generator, by running:
@@ -45,14 +45,50 @@ Please make sure to follow these guidelines when working on your new element:
4545
3. Add docstring to the element class and any public properties/methods. These
4646
doc strings will appear in the Docs tag in storybook. Check out the [Resistor
4747
Element](src/resistor-element.ts) for an example.
48-
4. Your commit messages should follow the [conventional commits
48+
4. Include a `pinInfo` property in your elements. This property defines the pins
49+
of the element, specifying at least the name and x/y position of each pin.
50+
51+
Example (from the [LED element](src/led-element.ts)):
52+
53+
```typescript
54+
readonly pinInfo: ElementPin[] = [
55+
{ name: 'A', x: 24, y: 42, signals: [], description: 'Anode' },
56+
{ name: 'C', x: 16, y: 42, signals: [], description: 'Cathode' },
57+
];
58+
```
59+
60+
5. Create a storybook story for each element configuration. For example, check the [LCD1602
61+
stories](src/lcd1602-element.stories.ts)
62+
6. Your commit messages should follow the [conventional commits
4963
standard](https://www.conventionalcommits.org/), e.g.:
5064
`feat: add neopixel element`
5165

66+
## Debugging Pin Info
67+
68+
When working on the `pinInfo` property, it is often useful to visually see the
69+
pins that you define. To see the pin locations, add the `<wokwi-show-pins>`
70+
utility element to your story:
71+
72+
1. Import the element definition into your story, by adding this line at the top
73+
of the story file:
74+
```
75+
import './utils/show-pins-element';
76+
```
77+
2. Wrap your element with the <wokwi-show-pins> element, e.g.
78+
```
79+
export const HCSR04 = () => html`
80+
<wokwi-show-pins>
81+
<wokwi-hc-sr04></wokwi-hc-sr04>
82+
</wokwi-show-pins>
83+
`;
84+
```
85+
3. When you are happy with the pin definition, please remove the `<wokwi-show-pins>`
86+
from your story. It is only a debugging tool, and shouldn't be present in the final story.
87+
5288
## Video Tutorial and Blog Post
5389

5490
The [Membrane keypad element live-coding video tutorial](https://www.youtube.com/watch?v=gh27icNatwA) walks
5591
through the complete process behind creating an element: research, drawing, and writing the code /
56-
stories.
92+
stories.
5793

5894
If you prefer reading over watching a video, please check out [Recreating The Arduino Pushbutton Using SVG And &lt;lit-element&gt;](https://www.smashingmagazine.com/2020/01/recreating-arduino-pushbutton-svg/).

src/utils/show-pins-element.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { customElement, html, LitElement, property, query, svg } from 'lit-element';
2+
import { ElementPin } from '../pin';
3+
4+
export interface ElementWithPinInfo extends Element {
5+
pinInfo?: ElementPin[];
6+
}
7+
8+
/**
9+
* Utility element to debug the pinInfo property. Pin locations are displayed using red dots.
10+
* Moving your mouse over one of the red dots shows a tooltip with the pin's name.
11+
*
12+
*
13+
* Usage:
14+
* 1. Import this file in your element's story file, e.g.
15+
* ```
16+
* import './utils/show-pins-element';
17+
* ```
18+
* 2. Wrap your element with the <wokwi-show-pins> element, e.g.
19+
* ```
20+
* export const HCSR04 = () => html`
21+
* <wokwi-show-pins>
22+
* <wokwi-hc-sr04></wokwi-hc-sr04>
23+
* </wokwi-show-pins>
24+
* `;
25+
* ```
26+
*/
27+
@customElement('wokwi-show-pins')
28+
export class ShowPinsElement extends LitElement {
29+
@property() pinColor = 'red';
30+
@query('#content') elementSlot!: HTMLSlotElement;
31+
32+
get slotChild() {
33+
return this.elementSlot?.assignedElements()[0] as ElementWithPinInfo | undefined;
34+
}
35+
36+
render() {
37+
const pinInfo = this.slotChild?.pinInfo ?? [];
38+
const { pinColor } = this;
39+
return html`<div style="position: relative">
40+
<slot id="content" @slotchange=${() => this.requestUpdate()}></slot>
41+
42+
<svg style="position: absolute; top: 0; left: 0" width="100%" height="100%" fill=${pinColor}>
43+
${pinInfo.map(
44+
(pin) => svg`<circle cx=${pin.x} cy=${pin.y} r=2><title>${pin.name}</title></circle>`
45+
)}
46+
</svg>
47+
</div>`;
48+
}
49+
}

0 commit comments

Comments
 (0)