Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add webmanifest's shortcuts support #378

Merged
merged 3 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ var favicons = require("favicons"),
windows: true, // Create Windows 8 tile icons. `boolean` or `{ offset, background }` or an array of sources
yandex: true, // Create Yandex browser icon. `boolean` or `{ offset, background }` or an array of sources
},
shortcuts: [
// Your applications's Shortcuts (see: https://developer.mozilla.org/docs/Web/Manifest/shortcuts)
// Array of shortcut objects:
{
name: "View your Inbox", // The name of the shortcut. `string`
short_name: "inbox", // optionally, falls back to name. `string`
description: "View your inbox messages", // optionally, not used in any implemention yet. `string`
url: "/inbox", // The URL this shortcut should lead to. `string`
icon: "test/inbox_shortcut.png", // source image(s) for that shortcut. `string`, `buffer` or array of `string`
andy128k marked this conversation as resolved.
Show resolved Hide resolved
},
],
// more shortcuts objects
},
callback = function (error, response) {
if (error) {
Expand Down
11 changes: 10 additions & 1 deletion src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export interface FileOptions {
readonly manifestFileName?: string;
}

export interface ShortcutOptions {
readonly name: string;
readonly short_name?: string;
readonly description?: string;
readonly url: string;
readonly icon?: string | string[] | Buffer | Buffer[];
}

export interface Application {
readonly platform?: string;
readonly url?: string;
Expand Down Expand Up @@ -51,11 +59,12 @@ export interface FaviconOptions {
readonly pixel_art?: boolean;
readonly loadManifestWithCredentials?: boolean;
readonly manifestRelativePaths?: boolean;
readonly manifestMaskable?: boolean;
readonly manifestMaskable?: boolean | string | string[] | Buffer | Buffer[];
readonly preferRelatedApplications?: boolean;
readonly relatedApplications?: Application[];
readonly icons?: Dictionary<IconOptions | boolean | string[]>;
readonly files?: Dictionary<FileOptions>;
readonly shortcuts?: ShortcutOptions[];
readonly output?: OutputOptions;
}

Expand Down
83 changes: 83 additions & 0 deletions src/platforms/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import {
} from "../helpers.js";
import { Platform, uniformIconOptions } from "./base.js";

interface Icon {
readonly src: string;
readonly sizes: string;
readonly type: string;
}

interface Shortcut {
readonly name: string;
readonly short_name: string;
description?: string;
andy128k marked this conversation as resolved.
Show resolved Hide resolved
readonly url: string;
icons?: Icon[];
andy128k marked this conversation as resolved.
Show resolved Hide resolved
}

const ICONS_OPTIONS: Dictionary<IconOptions> = {
"android-chrome-36x36.png": transparentIcon(36),
"android-chrome-48x48.png": transparentIcon(48),
Expand All @@ -35,6 +49,15 @@ const ICONS_OPTIONS_MASKABLE: Dictionary<IconOptions> = {
"android-chrome-maskable-512x512.png": maskable(transparentIcon(512)),
};

const SHORTCUT_ICONS_OPTIONS: Dictionary<IconOptions> = {
"36x36.png": transparentIcon(36),
"48x48.png": transparentIcon(48),
"72x72.png": transparentIcon(72),
"96x96.png": transparentIcon(96),
"144x144.png": transparentIcon(144),
"192x192.png": transparentIcon(192),
};

export class AndroidPlatform extends Platform {
constructor(options: FaviconOptions) {
super(
Expand Down Expand Up @@ -69,6 +92,12 @@ export class AndroidPlatform extends Platform {

icons = [...icons, ...maskable];
}
if (
Array.isArray(this.options.shortcuts) &&
this.options.shortcuts.length > 0
) {
icons = [...icons, ...(await this.shortcutIcons())];
}

return icons;
}
Expand All @@ -91,6 +120,26 @@ export class AndroidPlatform extends Platform {
];
}

private async shortcutIcons(): Promise<FaviconImage[]> {
const images = new Images();
const icons = await Promise.all(
this.options.shortcuts.map(async (shortcut, index) => {
if (!shortcut.name || !shortcut.url || !shortcut.icon) return null;
const shortcutSourceset = await sourceImages(shortcut.icon);
return Promise.all(
Object.entries(SHORTCUT_ICONS_OPTIONS).map(([shortcutName, option]) =>
images.createFavicon(
shortcutSourceset,
`shortcut${index + 1}-${shortcutName}`,
option
)
)
);
})
);
return icons.flat();
andy128k marked this conversation as resolved.
Show resolved Hide resolved
}

private manifestFileName(): string {
return (
this.options.files?.android?.manifestFileName ?? "manifest.webmanifest"
Expand Down Expand Up @@ -157,9 +206,43 @@ export class AndroidPlatform extends Platform {
};
});

if (Array.isArray(options.shortcuts) && options.shortcuts.length > 0) {
properties.shortcuts = this.manifestShortcuts(basePath);
}

return {
name: this.manifestFileName(),
contents: JSON.stringify(properties, null, 2),
};
}

private manifestShortcuts(basePath: string): Shortcut[] {
return this.options.shortcuts
.map((shortcut, index) => {
if (!shortcut.name || !shortcut.url) return null; // skip if required name or url missing
const result: Shortcut = {
name: shortcut.name,
short_name: shortcut.short_name || shortcut.name, // fallback to name
url: shortcut.url,
};
if (shortcut.description) result.description = shortcut.description;
if (shortcut.icon) {
result.icons = Object.entries(SHORTCUT_ICONS_OPTIONS).map(
([shortcutName, option]) => {
const { width, height } = option.sizes[0];
return {
src: relativeTo(
basePath,
`shortcut${index + 1}-${shortcutName}`
),
sizes: `${width}x${height}`,
type: "image/png",
};
}
);
}
return result;
})
.filter((x) => x !== null);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading