Skip to content

Commit fae5729

Browse files
committed
properly handle local image embeds
1 parent 4334dc8 commit fae5729

File tree

5 files changed

+47
-30
lines changed

5 files changed

+47
-30
lines changed
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
11
title: "ESP32 C3 Super Mini"
2+
3+
# dimensions counted in pins
24
width: 7
3-
height: 9
5+
height: 8
6+
7+
# images can be URLs, use adjustments to fit the pins
48
image:
59
front:
610
src: "esp32-c3-super-mini.front.png"
7-
top: -270
8-
left: -100
9-
right: -120
10-
bottom: -50
11+
top: -200
12+
left: 50
13+
right: 50
14+
bottom: -190
1115
back:
1216
src: "esp32-c3-super-mini.back.png"
13-
offsets:
14-
top: 2
15-
right: 1
16-
types: { }
17+
top: -150
18+
left: 50
19+
right: 50
20+
bottom: -170
21+
22+
# Define the pins (label:type), use empty entries when offsets are needed
1723
pins:
1824
left:
1925
- [ "GPIO5:gpio", "A5:analog", "MISO:spi" ]
20-
- [ "GPIO6:gpio", "MOSI:spi", "SCK:spi" ]
26+
- [ "GPIO6:gpio", "MOSI:spi" ]
2127
- [ "GPIO7:gpio", "SS:spi" ]
2228
- [ "GPIO8:gpio", "SDA:i2c" ]
2329
- [ "GPIO9:gpio", "SCL:i2c" ]
2430
- [ "GPIO10:gpio" ]
2531
- [ "GPIO20:gpio", "RX:uart" ]
2632
- [ "GPIO21:gpio", "TX:uart" ]
2733
right:
28-
-
2934
- [ "5V:power" ]
3035
- [ "GND:gnd" ]
3136
- [ "3V3:power" ]
3237
- [ "GPIO4:gpio", "A4:analog", "SCK:spi" ]
33-
- [ "GPIO3:gpio", "A3:analog", "MOSI:spi" ]
38+
- [ "GPIO3:gpio", "A3:analog" ]
3439
- [ "GPIO2:gpio", "A2:analog" ]
3540
- [ "GPIO1:gpio", "A1:analog" ]
3641
- [ "GPIO0:gpio", "A0:analog" ]
37-
top:
38-
-
39-
-
40-
- [ "GND:gnd", "A4:analog", "SCK:spi" ]
41-
- [ "5V:power" ]
42-
bottom:
43-
-
44-
- [ "GND:gnd" ]
45-
- [ "5V:power", "A4:analog", "SCK:spi" ]
42+
43+
# For custom types, top and bottom rows, row offsets, etc. read the docs

src/Builder.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class Builder {
4545
},
4646
gpio: {
4747
label: 'GPIO',
48-
bgcolor: '#8c49ae',
48+
bgcolor: '#79bc3c',
4949
fgcolor: '#ffffff',
5050
},
5151
power: {
@@ -60,17 +60,17 @@ export class Builder {
6060
},
6161
i2c: {
6262
label: 'I2C',
63-
bgcolor: '#485377',
63+
bgcolor: '#41b28c',
6464
fgcolor: '#ffffff',
6565
},
6666
uart: {
6767
label: 'UART',
68-
bgcolor: '#34CD71',
68+
bgcolor: '#637181',
6969
fgcolor: '#ffffff',
7070
},
7171
spi: {
7272
label: 'SPI',
73-
bgcolor: '#3399DD',
73+
bgcolor: '#775ee8',
7474
fgcolor: '#ffffff',
7575
},
7676
analog: {

src/Editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Editor {
7878
}
7979

8080
async onUpdate(setup) {
81-
const embed = new ImageEmbed();
81+
const embed = new ImageEmbed('pinouts');
8282
setup = await embed.embedImages(setup);
8383

8484
const builder = new Builder(setup);

src/ImageEmbed.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export class ImageEmbed {
1515
*/
1616
static SERVICE = 'https://imgdataurl.splitbrain.workers.dev/';
1717

18+
/**
19+
* @param {string} base The relative base to look for local images in
20+
*/
21+
constructor(base = '') {
22+
this.base = base;
23+
}
24+
1825
/**
1926
* Processes a setup object, attempting to embed images specified in
2027
* `setup.image.front.src` and `setup.image.back.src`.
@@ -30,10 +37,10 @@ export class ImageEmbed {
3037
const imageConfigsToProcess = [];
3138
// Collect image sources to process, storing references to the config objects
3239
if (setup?.image?.front?.src) {
33-
imageConfigsToProcess.push({ config: setup.image.front, originalSrc: setup.image.front.src });
40+
imageConfigsToProcess.push({config: setup.image.front, originalSrc: setup.image.front.src});
3441
}
3542
if (setup?.image?.back?.src) {
36-
imageConfigsToProcess.push({ config: setup.image.back, originalSrc: setup.image.back.src });
43+
imageConfigsToProcess.push({config: setup.image.back, originalSrc: setup.image.back.src});
3744
}
3845

3946
if (imageConfigsToProcess.length === 0) {
@@ -110,6 +117,8 @@ export class ImageEmbed {
110117
buffer = Buffer.from(await response.arrayBuffer());
111118
mimeType = response.headers.get('content-type')?.split(';')[0].toLowerCase();
112119
} else {
120+
source = this.base ? path.join(this.base, source) : source;
121+
113122
buffer = await fs.readFile(source);
114123
const extension = path.extname(source).toLowerCase();
115124
if (extension === '.jpg' || extension === '.jpeg') {
@@ -141,6 +150,16 @@ export class ImageEmbed {
141150
* @private
142151
*/
143152
async embedBrowser(source) {
153+
154+
// this is not a URL, so we assume it to be one of our own, locally served files
155+
if (!source.match(/^https?:\/\//)) {
156+
if (this.base) {
157+
source = `${this.base}/${source}`;
158+
}
159+
source = new URL(source, window.location.href).toString();
160+
if (source.match(/^http:\/\/(localhost|127.0.0.1)/)) return source; // no local embeds
161+
}
162+
144163
const embedServiceUrl = `${ImageEmbed.SERVICE}?url=${encodeURIComponent(source)}`;
145164
const response = await fetch(embedServiceUrl);
146165
if (!response.ok) {

src/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createWindow} from "svgdom";
22
import {Builder} from "./Builder.js";
33
import Yaml from 'yaml';
44
import { readdir, readFile, stat, writeFile } from 'fs/promises';
5-
import {join} from 'path';
5+
import {join, dirname} from 'path';
66
import {ImageEmbed} from "./ImageEmbed.js";
77

88
/**
@@ -66,7 +66,7 @@ async function processFile(file) {
6666
throw new Error(`Unsupported file type: ${file}`);
6767
}
6868

69-
const embed = new ImageEmbed();
69+
const embed = new ImageEmbed(dirname(file));
7070
setup = await embed.embedImages(setup);
7171

7272
const outputBase = file.replace(/\.(yaml|json)$/, '');

0 commit comments

Comments
 (0)