Skip to content

Commit 5229634

Browse files
feat(indiekit): shortcut icons
1 parent 8d77a53 commit 5229634

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

docs/plugins/api/add-endpoint.md

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ Used to register the plug-in. Accepts an `Indiekit` instance to allow its modifi
7676
`name`
7777
: A string representing the text shown in the shortcut item. **Required**.
7878

79+
`iconName`
80+
: A string representing the name of the icon to use for the shortcut item.
81+
7982
`requiresDatabase`
8083
: A boolean for whether the item should only be displayed if a database has been configured.
8184

packages/indiekit/lib/controllers/assets.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { appIcon, scripts, styles } from "@indiekit/frontend";
1+
import { appIcon, shortcutIcon, scripts, styles } from "@indiekit/frontend";
22

33
export const getAppIcon = async (request, response, next) => {
44
const { purpose, size } = request.params;
@@ -12,6 +12,17 @@ export const getAppIcon = async (request, response, next) => {
1212
}
1313
};
1414

15+
export const getShortcutIcon = async (request, response, next) => {
16+
const { name, size } = request.params;
17+
18+
try {
19+
const png = await shortcutIcon(size, name);
20+
return response.type("image/png").send(png).end();
21+
} catch {
22+
next();
23+
}
24+
};
25+
1526
export const getScripts = async (request, response) => {
1627
const js = await scripts();
1728

packages/indiekit/lib/routes.js

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export const routes = (indiekitConfig) => {
4242
"/assets/app-icon-:size-:purpose.png",
4343
assetsController.getAppIcon,
4444
);
45+
router.get(
46+
"/assets/shortcut-icon-:size-:name.png",
47+
assetsController.getShortcutIcon,
48+
);
4549

4650
// Plug-in assets
4751
for (const plugin of application.installedPlugins) {

packages/indiekit/lib/shortcuts.js

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ export const getShortcuts = (application, response) => {
2020
for (const item of shortcuts) {
2121
// Translate text strings
2222
item.name = response.locals.__(item.name);
23+
24+
// Add shortcut icon
25+
if (item.iconName) {
26+
item.icons = [
27+
{
28+
src: `/assets/shortcut-icon-96-${item.iconName}.png`,
29+
sizes: "96x96",
30+
},
31+
];
32+
delete item.iconName;
33+
}
34+
2335
// Remove database requirement check
2436
delete item.requiresDatabase;
2537
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { strict as assert } from "node:assert";
2+
import { after, describe, it } from "node:test";
3+
import supertest from "supertest";
4+
import { testServer } from "@indiekit-test/server";
5+
6+
const server = await testServer();
7+
const request = supertest.agent(server);
8+
9+
describe("indiekit GET /assets/shortcut-icon-96-tick.png", () => {
10+
it("Returns JavaScript", async () => {
11+
const result = await request.get("/assets/shortcut-icon-96-tick.png");
12+
13+
assert.equal(result.status, 200);
14+
assert.equal(result.type, "image/png");
15+
});
16+
17+
after(() => {
18+
server.close(() => process.exit(0));
19+
});
20+
});

0 commit comments

Comments
 (0)