@emberbox/ui 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/vite/logo.d.ts +4 -3
  2. package/dist/vite/logo.js +13 -26
  3. package/dist/vite/manifest.d.ts +18 -0
  4. package/dist/vite/manifest.js +53 -0
  5. package/dist/vite/root-files.d.ts +14 -0
  6. package/dist/vite/root-files.js +28 -0
  7. package/dist/vite.d.ts +4 -0
  8. package/dist/vite.js +6 -1
  9. package/logos/alarmzettel-192.png +0 -0
  10. package/logos/alarmzettel-maskable-192.png +0 -0
  11. package/logos/alarmzettel-maskable-512.png +0 -0
  12. package/logos/calltaker-192.png +0 -0
  13. package/logos/calltaker-maskable-192.png +0 -0
  14. package/logos/calltaker-maskable-512.png +0 -0
  15. package/logos/emberbox-192.png +0 -0
  16. package/logos/emberbox-maskable-192.png +0 -0
  17. package/logos/emberbox-maskable-512.png +0 -0
  18. package/logos/planspiel-180.png +0 -0
  19. package/logos/planspiel-192.png +0 -0
  20. package/logos/planspiel-512.png +0 -0
  21. package/logos/planspiel-maskable-192.png +0 -0
  22. package/logos/planspiel-maskable-512.png +0 -0
  23. package/logos/planspiel.svg +1 -0
  24. package/logos/statussim-192.png +0 -0
  25. package/logos/statussim-maskable-192.png +0 -0
  26. package/logos/statussim-maskable-512.png +0 -0
  27. package/package.json +3 -1
  28. package/src/components/AppFooter.vue +2 -1
  29. package/src/components/AppHeader.vue +2 -2
  30. package/src/components/CodeDisplay.vue +6 -4
  31. package/src/components/CollapsibleSection.vue +1 -1
  32. package/src/components/ComboboxField.vue +5 -1
  33. package/src/components/YesNoField.vue +2 -0
  34. package/src/vite/logo.ts +15 -27
  35. package/src/vite/manifest.ts +69 -0
  36. package/src/vite/root-files.ts +34 -0
  37. package/src/vite.ts +9 -1
@@ -1,8 +1,9 @@
1
1
  import type { Plugin } from 'vite';
2
2
  /** The tools for which `@emberbox/ui` ships a logo. */
3
- export type Logo = 'emberbox' | 'alarmzettel' | 'statussim' | 'calltaker';
3
+ export type Logo = 'emberbox' | 'alarmzettel' | 'statussim' | 'calltaker' | 'planspiel';
4
4
  /**
5
- * Serves a tool's logo as `favicon.svg`, `apple-touch-icon.png` and `logo-512.png` at the root, in
6
- * the dev server as in the build, and links the first two in the head of every page.
5
+ * Serves a tool's logo as `favicon.svg`, `apple-touch-icon.png`, `logo-192.png`, `logo-512.png` and the
6
+ * maskable `logo-maskable-192.png` and `logo-maskable-512.png` at the root, in the dev server as in
7
+ * the build, and links the first two in the head of every page; the rest are the app icons.
7
8
  */
8
9
  export declare function logoPlugin(logo: Logo): Plugin;
package/dist/vite/logo.js CHANGED
@@ -1,40 +1,27 @@
1
1
  import { readFileSync } from 'node:fs';
2
2
  import { fileURLToPath } from 'node:url';
3
+ import { rootFilesPlugin } from './root-files.js';
3
4
  const LOGOS = fileURLToPath(new URL('../../logos/', import.meta.url));
4
- const CONTENT_TYPES = { svg: 'image/svg+xml', png: 'image/png' };
5
- /** Which file is served under which name at the tool's root. */
5
+ /** Which file of the logo is served under which name at the tool's root. */
6
6
  function logoFiles(logo) {
7
+ const file = (source, type) => ({ content: () => readFileSync(`${LOGOS}${source}`), type });
7
8
  return {
8
- 'favicon.svg': `${LOGOS}${logo}.svg`,
9
- 'apple-touch-icon.png': `${LOGOS}${logo}-180.png`,
10
- 'logo-512.png': `${LOGOS}${logo}-512.png`,
9
+ 'favicon.svg': file(`${logo}.svg`, 'image/svg+xml'),
10
+ 'apple-touch-icon.png': file(`${logo}-180.png`, 'image/png'),
11
+ 'logo-192.png': file(`${logo}-192.png`, 'image/png'),
12
+ 'logo-512.png': file(`${logo}-512.png`, 'image/png'),
13
+ 'logo-maskable-192.png': file(`${logo}-maskable-192.png`, 'image/png'),
14
+ 'logo-maskable-512.png': file(`${logo}-maskable-512.png`, 'image/png'),
11
15
  };
12
16
  }
13
17
  /**
14
- * Serves a tool's logo as `favicon.svg`, `apple-touch-icon.png` and `logo-512.png` at the root, in
15
- * the dev server as in the build, and links the first two in the head of every page.
18
+ * Serves a tool's logo as `favicon.svg`, `apple-touch-icon.png`, `logo-192.png`, `logo-512.png` and the
19
+ * maskable `logo-maskable-192.png` and `logo-maskable-512.png` at the root, in the dev server as in
20
+ * the build, and links the first two in the head of every page; the rest are the app icons.
16
21
  */
17
22
  export function logoPlugin(logo) {
18
- const files = logoFiles(logo);
19
23
  return {
20
- name: 'emberbox:logo',
21
- configureServer(server) {
22
- server.middlewares.use((request, response, next) => {
23
- const name = request.url?.split('?')[0]?.slice(1) ?? '';
24
- const file = files[name];
25
- if (!file)
26
- return next();
27
- response.setHeader('Content-Type', CONTENT_TYPES[name.split('.').pop() ?? ''] ?? 'application/octet-stream');
28
- response.end(readFileSync(file));
29
- });
30
- },
31
- generateBundle() {
32
- if (this.environment?.config.build.ssr)
33
- return;
34
- for (const [name, file] of Object.entries(files)) {
35
- this.emitFile({ type: 'asset', fileName: name, source: readFileSync(file) });
36
- }
37
- },
24
+ ...rootFilesPlugin('emberbox:logo', logoFiles(logo)),
38
25
  transformIndexHtml: () => [
39
26
  { tag: 'link', attrs: { rel: 'icon', type: 'image/svg+xml', href: '/favicon.svg' }, injectTo: 'head' },
40
27
  { tag: 'link', attrs: { rel: 'apple-touch-icon', href: '/apple-touch-icon.png' }, injectTo: 'head' },
@@ -0,0 +1,18 @@
1
+ import type { Plugin } from 'vite';
2
+ /** What a tool calls itself once it is installed as an app. */
3
+ export interface AppOptions {
4
+ /** The full name, shown when the app is installed and in its window title. */
5
+ name: string;
6
+ /** A short name for the home screen, the full name by default. */
7
+ shortName?: string;
8
+ /** One sentence about what the app does. */
9
+ description?: string;
10
+ }
11
+ /**
12
+ * Serves `manifest.webmanifest` at the root, in the dev server as in the build, and links it with
13
+ * the theme colour in the head of every page.
14
+ *
15
+ * @param app the app's names and description
16
+ * @returns the plugin
17
+ */
18
+ export declare function manifestPlugin(app: AppOptions): Plugin;
@@ -0,0 +1,53 @@
1
+ import { rootFilesPlugin } from './root-files.js';
2
+ /** The colour of the app's title bar: the signal red of the logos. */
3
+ const THEME_COLOR = '#d5271b';
4
+ /** The splash screen behind the starting app: the logo's red, so it looks the same in light and dark mode. */
5
+ const BACKGROUND_COLOR = THEME_COLOR;
6
+ const MANIFEST = 'manifest.webmanifest';
7
+ /**
8
+ * The web app manifest of a tool, which makes it installable on phones and desktops. It starts at
9
+ * the root, runs without browser chrome and uses the icons the logo plugin serves. There is no
10
+ * service worker: the app always loads from the server, and the new-build banner remains the way
11
+ * updates arrive.
12
+ *
13
+ * @param app the app's names and description
14
+ * @returns the manifest as JSON
15
+ */
16
+ function manifest(app) {
17
+ return JSON.stringify({
18
+ id: '/',
19
+ name: app.name,
20
+ short_name: app.shortName ?? app.name,
21
+ description: app.description,
22
+ lang: 'de',
23
+ start_url: '/',
24
+ scope: '/',
25
+ display: 'standalone',
26
+ theme_color: THEME_COLOR,
27
+ background_color: BACKGROUND_COLOR,
28
+ icons: [
29
+ { src: '/logo-192.png', sizes: '192x192', type: 'image/png', purpose: 'any' },
30
+ { src: '/logo-512.png', sizes: '512x512', type: 'image/png', purpose: 'any' },
31
+ { src: '/logo-maskable-192.png', sizes: '192x192', type: 'image/png', purpose: 'maskable' },
32
+ { src: '/logo-maskable-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' },
33
+ { src: '/favicon.svg', sizes: 'any', type: 'image/svg+xml', purpose: 'any' },
34
+ ],
35
+ }, null, 2);
36
+ }
37
+ /**
38
+ * Serves `manifest.webmanifest` at the root, in the dev server as in the build, and links it with
39
+ * the theme colour in the head of every page.
40
+ *
41
+ * @param app the app's names and description
42
+ * @returns the plugin
43
+ */
44
+ export function manifestPlugin(app) {
45
+ const source = manifest(app);
46
+ return {
47
+ ...rootFilesPlugin('emberbox:manifest', { [MANIFEST]: { content: () => source, type: 'application/manifest+json' } }),
48
+ transformIndexHtml: () => [
49
+ { tag: 'link', attrs: { rel: 'manifest', href: `/${MANIFEST}` }, injectTo: 'head' },
50
+ { tag: 'meta', attrs: { name: 'theme-color', content: THEME_COLOR }, injectTo: 'head' },
51
+ ],
52
+ };
53
+ }
@@ -0,0 +1,14 @@
1
+ import type { Plugin } from 'vite';
2
+ /** A file served at the tool's root: its content and its media type. */
3
+ export interface RootFile {
4
+ content: () => string | Uint8Array;
5
+ type: string;
6
+ }
7
+ /**
8
+ * A plugin that serves fixed files at the root of a tool, in the dev server as in the client build.
9
+ *
10
+ * @param name the plugin's name
11
+ * @param files the files by their name at the root
12
+ * @returns the plugin, to be extended with further hooks
13
+ */
14
+ export declare function rootFilesPlugin(name: string, files: Record<string, RootFile>): Plugin;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * A plugin that serves fixed files at the root of a tool, in the dev server as in the client build.
3
+ *
4
+ * @param name the plugin's name
5
+ * @param files the files by their name at the root
6
+ * @returns the plugin, to be extended with further hooks
7
+ */
8
+ export function rootFilesPlugin(name, files) {
9
+ return {
10
+ name,
11
+ configureServer(server) {
12
+ server.middlewares.use((request, response, next) => {
13
+ const file = files[request.url?.split('?')[0]?.slice(1) ?? ''];
14
+ if (!file)
15
+ return next();
16
+ response.setHeader('Content-Type', file.type);
17
+ response.end(file.content());
18
+ });
19
+ },
20
+ generateBundle() {
21
+ if (this.environment?.config.build.ssr)
22
+ return;
23
+ for (const [fileName, file] of Object.entries(files)) {
24
+ this.emitFile({ type: 'asset', fileName, source: file.content() });
25
+ }
26
+ },
27
+ };
28
+ }
package/dist/vite.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import type { NuxtUIOptions } from '@nuxt/ui/unplugin';
2
2
  import type { PluginOption } from 'vite';
3
3
  import { type Logo } from './vite/logo.js';
4
+ import { type AppOptions } from './vite/manifest.js';
4
5
  import { type PrerenderOptions } from './vite/prerender.js';
5
6
  export { PAGES_DIR, type PrerenderOptions } from './vite/prerender.js';
6
7
  export type { Logo } from './vite/logo.js';
8
+ export type { AppOptions } from './vite/manifest.js';
7
9
  /** Settings a tool passes to `emberbox()`. */
8
10
  export interface EmberboxOptions {
9
11
  /** `false` for tools without vue-router; links are then rendered as plain anchors. */
@@ -16,6 +18,8 @@ export interface EmberboxOptions {
16
18
  logo?: Logo;
17
19
  /** Which public pages are prerendered at build time; needs `url`. */
18
20
  prerender?: PrerenderOptions;
21
+ /** Makes the tool installable as an app under this name; needs `logo` for the app icons. */
22
+ app?: AppOptions;
19
23
  }
20
24
  /**
21
25
  * The icons of the `@emberbox/ui` components. The scan does not walk `node_modules`, so they are
package/dist/vite.js CHANGED
@@ -2,6 +2,7 @@ import ui from '@nuxt/ui/vite';
2
2
  import { defu } from 'defu';
3
3
  import { execSync } from 'node:child_process';
4
4
  import { logoPlugin } from './vite/logo.js';
5
+ import { manifestPlugin } from './vite/manifest.js';
5
6
  import { prerenderPlugin } from './vite/prerender.js';
6
7
  export { PAGES_DIR } from './vite/prerender.js';
7
8
  const COLORS = ['primary', 'signal', 'success', 'info', 'warning', 'error'];
@@ -87,6 +88,9 @@ export function emberbox(options = {}) {
87
88
  if (options.prerender && !url) {
88
89
  throw new Error('emberbox(): prerendering needs a url.');
89
90
  }
91
+ if (options.app && !options.logo) {
92
+ throw new Error('emberbox(): an installable app needs a logo.');
93
+ }
90
94
  return [
91
95
  ui({
92
96
  router: options.router ?? true,
@@ -108,7 +112,7 @@ export function emberbox(options = {}) {
108
112
  },
109
113
  optimizeDeps: { exclude: ['@emberbox/ui', '@iconify/vue'] },
110
114
  resolve: { dedupe: ['vue', 'vue-router', '@nuxt/ui'] },
111
- ssr: { noExternal: ['@emberbox/ui', '@nuxt/ui'] },
115
+ ssr: { noExternal: ['@emberbox/ui', '@nuxt/ui', 'vue-router'] },
112
116
  }),
113
117
  transformIndexHtml: () => [
114
118
  { tag: 'script', children: COLOR_MODE_SCRIPT, injectTo: 'head' },
@@ -120,6 +124,7 @@ export function emberbox(options = {}) {
120
124
  },
121
125
  },
122
126
  options.logo ? logoPlugin(options.logo) : false,
127
+ options.app && options.logo ? manifestPlugin(options.app) : false,
123
128
  options.prerender && url ? prerenderPlugin(options.prerender, url) : false,
124
129
  ];
125
130
  }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" rx="8" fill="#d5271b"/><g transform="translate(14 14) scale(1.5)" color="#fff"><g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"><path d="M18 8c0 3.613-3.869 7.429-5.393 8.795a1 1 0 0 1-1.214 0C9.87 15.429 6 11.613 6 8a6 6 0 0 1 12 0"/><path d="M4.474 15h-.197a1 1 0 0 0-.969.753l-1.097 4.35a1.5 1.5 0 0 0 1.444 1.898L20.344 22a1.5 1.5 0 0 0 1.446-1.897l-1.098-4.35a1 1 0 0 0-.969-.753h-.197"/><circle cx="12" cy="8" r="2"/></g></g></svg>
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emberbox/ui",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Shared look and building blocks of the Emberbox tools, built on Nuxt UI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -53,8 +53,10 @@
53
53
  "vue": "^3.5.0"
54
54
  },
55
55
  "devDependencies": {
56
+ "@emberbox/lint": "^0.2.0",
56
57
  "@types/node": "^24.0.0",
57
58
  "@vue/tsconfig": "^0.9.1",
59
+ "eslint": "^10.15.0",
58
60
  "typescript": "~6.0.3",
59
61
  "vite": "^8.3.8",
60
62
  "vue": "^3.5.46",
@@ -19,6 +19,7 @@ const props = withDefaults(defineProps<{
19
19
  notice?: string
20
20
  backlink?: boolean
21
21
  }>(), {
22
+ help: undefined,
22
23
  notice: 'Kein offizielles Angebot einer Feuerwehr oder Leitstelle.',
23
24
  backlink: true,
24
25
  })
@@ -42,7 +43,7 @@ const links = [
42
43
  <div class="flex flex-col items-center gap-1 text-center">
43
44
  <ULink v-if="backlink" :to="HUB_URL"
44
45
  class="mb-1 flex items-center gap-2 text-sm text-default hover:text-highlighted hover:underline">
45
- <img :src="emberboxLogo" alt="" width="20" height="20" class="size-5 rounded-[3px]">
46
+ <img :src="emberboxLogo" alt="" width="20" height="20" class="size-5 rounded-[3px]" />
46
47
  <span>Ein Werkzeug der <strong class="font-semibold">Emberbox</strong></span>
47
48
  </ULink>
48
49
  <p class="m-0 flex flex-wrap items-center justify-center gap-x-3 gap-y-1">
@@ -14,7 +14,7 @@ withDefaults(defineProps<{
14
14
  subtitle?: string
15
15
  home?: string
16
16
  logo?: string
17
- }>(), {home: '/'})
17
+ }>(), {subtitle: undefined, home: '/', logo: undefined})
18
18
  </script>
19
19
 
20
20
  <template>
@@ -23,7 +23,7 @@ withDefaults(defineProps<{
23
23
  <div class="flex shrink-0 items-center gap-3">
24
24
  <ULink :to="HUB_URL" class="flex size-8 items-center justify-center rounded-sm text-muted hover:text-highlighted"
25
25
  aria-label="Alle Werkzeuge auf emberbox.app">
26
- <img v-if="logo" :src="logo" alt="" class="size-8 rounded-sm">
26
+ <img v-if="logo" :src="logo" alt="" class="size-8 rounded-sm" />
27
27
  <UIcon v-else name="i-lucide-layout-grid" class="size-5"/>
28
28
  </ULink>
29
29
  <ULink :to="home" class="flex items-baseline gap-2 text-highlighted hover:text-highlighted">
@@ -22,8 +22,11 @@ const props = defineProps<{
22
22
  const FEEDBACK_MS = 1500
23
23
 
24
24
  const revealed = ref(false)
25
- const result = ref<{ what: 'code' | 'link', succeeded: boolean } | null>(null)
26
- const qrImage = computed(() => renderSVG(props.link ?? props.code, {border: 1}))
25
+ /** What the last copy was and whether it worked. */
26
+ type CopyResult = { what: 'code' | 'link', succeeded: boolean }
27
+
28
+ const result = ref<CopyResult | null>(null)
29
+ const qrImage = computed(() => `data:image/svg+xml;charset=utf-8,${encodeURIComponent(renderSVG(props.link ?? props.code, {border: 1}))}`)
27
30
  let timer: ReturnType<typeof setTimeout> | undefined
28
31
 
29
32
  const message = computed(() => result.value === null
@@ -68,8 +71,7 @@ onBeforeUnmount(() => clearTimeout(timer))
68
71
  <UPopover v-if="qr">
69
72
  <UButton icon="i-lucide-qr-code" color="neutral" variant="ghost" size="sm" :aria-label="`QR-Code für ${title}`"/>
70
73
  <template #content>
71
- <div class="size-56 bg-white p-2 text-black [&>svg]:size-full" role="img" :aria-label="`QR-Code für ${title}`"
72
- v-html="qrImage"/>
74
+ <img :src="qrImage" :alt="`QR-Code für ${title}`" width="224" height="224" class="size-56 bg-white p-2"/>
73
75
  </template>
74
76
  </UPopover>
75
77
  <span class="sr-only" role="status">{{ message }}</span>
@@ -10,7 +10,7 @@ import {onMounted, ref, useId, watch} from 'vue'
10
10
  * mounting, so prerendered pages hydrate with `defaultOpen` first.
11
11
  */
12
12
  const {
13
- name, title, count, icon, defaultOpen = false, storageKey = 'emberbox_sections', level = 2,
13
+ name, title, count = undefined, icon = undefined, defaultOpen = false, storageKey = 'emberbox_sections', level = 2,
14
14
  } = defineProps<{
15
15
  /** Key under which the state is remembered. */
16
16
  name: string
@@ -22,7 +22,10 @@ const emit = defineEmits<{ change: [] }>()
22
22
  const model = defineModel<string>({default: ''})
23
23
  const id = useId()
24
24
  const labelId = `${id}-label`
25
- const input = ref<{ inputRef: HTMLInputElement | null }>()
25
+ /** The part of Nuxt UI's input that exposes the native element. */
26
+ type InputHandle = { inputRef: HTMLInputElement | null }
27
+
28
+ const input = ref<InputHandle>()
26
29
  const open = ref(false)
27
30
 
28
31
  /** Which entry the arrow keys are on, or -1 while the typing stands on its own. */
@@ -121,6 +124,7 @@ watch(filtered, () => (active.value = -1))
121
124
  <ul v-if="visible" :id="`${id}-list`" role="listbox" :aria-labelledby="labelId"
122
125
  class="absolute inset-x-0 top-full z-30 mt-1 max-h-64 overflow-auto rounded-sm border border-default bg-default py-1 shadow-lg">
123
126
  <li v-for="(value, index) in filtered" :id="optionId(index)" :key="`${index}-${value}`" role="option"
127
+ tabindex="-1"
124
128
  :aria-selected="index === active"
125
129
  class="cursor-pointer px-3 py-1.5 text-sm"
126
130
  :class="[index === active ? 'bg-elevated' : '', value === model ? 'font-semibold' : '']"
@@ -14,6 +14,8 @@ const {
14
14
  noValue = 'nein',
15
15
  yesLabel = 'Ja',
16
16
  noLabel = 'Nein',
17
+ yesTitle = undefined,
18
+ noTitle = undefined,
17
19
  } = defineProps<{
18
20
  label: string
19
21
  yesValue?: string
package/src/vite/logo.ts CHANGED
@@ -1,46 +1,34 @@
1
1
  import {readFileSync} from 'node:fs'
2
2
  import {fileURLToPath} from 'node:url'
3
3
  import type {Plugin} from 'vite'
4
+ import {type RootFile, rootFilesPlugin} from './root-files.js'
4
5
 
5
6
  /** The tools for which `@emberbox/ui` ships a logo. */
6
- export type Logo = 'emberbox' | 'alarmzettel' | 'statussim' | 'calltaker'
7
+ export type Logo = 'emberbox' | 'alarmzettel' | 'statussim' | 'calltaker' | 'planspiel'
7
8
 
8
9
  const LOGOS = fileURLToPath(new URL('../../logos/', import.meta.url))
9
10
 
10
- const CONTENT_TYPES: Record<string, string> = {svg: 'image/svg+xml', png: 'image/png'}
11
-
12
- /** Which file is served under which name at the tool's root. */
13
- function logoFiles(logo: Logo): Record<string, string> {
11
+ /** Which file of the logo is served under which name at the tool's root. */
12
+ function logoFiles(logo: Logo): Record<string, RootFile> {
13
+ const file = (source: string, type: string): RootFile => ({content: () => readFileSync(`${LOGOS}${source}`), type})
14
14
  return {
15
- 'favicon.svg': `${LOGOS}${logo}.svg`,
16
- 'apple-touch-icon.png': `${LOGOS}${logo}-180.png`,
17
- 'logo-512.png': `${LOGOS}${logo}-512.png`,
15
+ 'favicon.svg': file(`${logo}.svg`, 'image/svg+xml'),
16
+ 'apple-touch-icon.png': file(`${logo}-180.png`, 'image/png'),
17
+ 'logo-192.png': file(`${logo}-192.png`, 'image/png'),
18
+ 'logo-512.png': file(`${logo}-512.png`, 'image/png'),
19
+ 'logo-maskable-192.png': file(`${logo}-maskable-192.png`, 'image/png'),
20
+ 'logo-maskable-512.png': file(`${logo}-maskable-512.png`, 'image/png'),
18
21
  }
19
22
  }
20
23
 
21
24
  /**
22
- * Serves a tool's logo as `favicon.svg`, `apple-touch-icon.png` and `logo-512.png` at the root, in
23
- * the dev server as in the build, and links the first two in the head of every page.
25
+ * Serves a tool's logo as `favicon.svg`, `apple-touch-icon.png`, `logo-192.png`, `logo-512.png` and the
26
+ * maskable `logo-maskable-192.png` and `logo-maskable-512.png` at the root, in the dev server as in
27
+ * the build, and links the first two in the head of every page; the rest are the app icons.
24
28
  */
25
29
  export function logoPlugin(logo: Logo): Plugin {
26
- const files = logoFiles(logo)
27
30
  return {
28
- name: 'emberbox:logo',
29
- configureServer(server) {
30
- server.middlewares.use((request, response, next) => {
31
- const name = request.url?.split('?')[0]?.slice(1) ?? ''
32
- const file = files[name]
33
- if (!file) return next()
34
- response.setHeader('Content-Type', CONTENT_TYPES[name.split('.').pop() ?? ''] ?? 'application/octet-stream')
35
- response.end(readFileSync(file))
36
- })
37
- },
38
- generateBundle() {
39
- if (this.environment?.config.build.ssr) return
40
- for (const [name, file] of Object.entries(files)) {
41
- this.emitFile({type: 'asset', fileName: name, source: readFileSync(file)})
42
- }
43
- },
31
+ ...rootFilesPlugin('emberbox:logo', logoFiles(logo)),
44
32
  transformIndexHtml: () => [
45
33
  {tag: 'link', attrs: {rel: 'icon', type: 'image/svg+xml', href: '/favicon.svg'}, injectTo: 'head'},
46
34
  {tag: 'link', attrs: {rel: 'apple-touch-icon', href: '/apple-touch-icon.png'}, injectTo: 'head'},
@@ -0,0 +1,69 @@
1
+ import type {Plugin} from 'vite'
2
+ import {rootFilesPlugin} from './root-files.js'
3
+
4
+ /** What a tool calls itself once it is installed as an app. */
5
+ export interface AppOptions {
6
+ /** The full name, shown when the app is installed and in its window title. */
7
+ name: string
8
+ /** A short name for the home screen, the full name by default. */
9
+ shortName?: string
10
+ /** One sentence about what the app does. */
11
+ description?: string
12
+ }
13
+
14
+ /** The colour of the app's title bar: the signal red of the logos. */
15
+ const THEME_COLOR = '#d5271b'
16
+
17
+ /** The splash screen behind the starting app: the logo's red, so it looks the same in light and dark mode. */
18
+ const BACKGROUND_COLOR = THEME_COLOR
19
+
20
+ const MANIFEST = 'manifest.webmanifest'
21
+
22
+ /**
23
+ * The web app manifest of a tool, which makes it installable on phones and desktops. It starts at
24
+ * the root, runs without browser chrome and uses the icons the logo plugin serves. There is no
25
+ * service worker: the app always loads from the server, and the new-build banner remains the way
26
+ * updates arrive.
27
+ *
28
+ * @param app the app's names and description
29
+ * @returns the manifest as JSON
30
+ */
31
+ function manifest(app: AppOptions): string {
32
+ return JSON.stringify({
33
+ id: '/',
34
+ name: app.name,
35
+ short_name: app.shortName ?? app.name,
36
+ description: app.description,
37
+ lang: 'de',
38
+ start_url: '/',
39
+ scope: '/',
40
+ display: 'standalone',
41
+ theme_color: THEME_COLOR,
42
+ background_color: BACKGROUND_COLOR,
43
+ icons: [
44
+ {src: '/logo-192.png', sizes: '192x192', type: 'image/png', purpose: 'any'},
45
+ {src: '/logo-512.png', sizes: '512x512', type: 'image/png', purpose: 'any'},
46
+ {src: '/logo-maskable-192.png', sizes: '192x192', type: 'image/png', purpose: 'maskable'},
47
+ {src: '/logo-maskable-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable'},
48
+ {src: '/favicon.svg', sizes: 'any', type: 'image/svg+xml', purpose: 'any'},
49
+ ],
50
+ }, null, 2)
51
+ }
52
+
53
+ /**
54
+ * Serves `manifest.webmanifest` at the root, in the dev server as in the build, and links it with
55
+ * the theme colour in the head of every page.
56
+ *
57
+ * @param app the app's names and description
58
+ * @returns the plugin
59
+ */
60
+ export function manifestPlugin(app: AppOptions): Plugin {
61
+ const source = manifest(app)
62
+ return {
63
+ ...rootFilesPlugin('emberbox:manifest', {[MANIFEST]: {content: () => source, type: 'application/manifest+json'}}),
64
+ transformIndexHtml: () => [
65
+ {tag: 'link', attrs: {rel: 'manifest', href: `/${MANIFEST}`}, injectTo: 'head'},
66
+ {tag: 'meta', attrs: {name: 'theme-color', content: THEME_COLOR}, injectTo: 'head'},
67
+ ],
68
+ }
69
+ }
@@ -0,0 +1,34 @@
1
+ import type {Plugin} from 'vite'
2
+
3
+ /** A file served at the tool's root: its content and its media type. */
4
+ export interface RootFile {
5
+ content: () => string | Uint8Array
6
+ type: string
7
+ }
8
+
9
+ /**
10
+ * A plugin that serves fixed files at the root of a tool, in the dev server as in the client build.
11
+ *
12
+ * @param name the plugin's name
13
+ * @param files the files by their name at the root
14
+ * @returns the plugin, to be extended with further hooks
15
+ */
16
+ export function rootFilesPlugin(name: string, files: Record<string, RootFile>): Plugin {
17
+ return {
18
+ name,
19
+ configureServer(server) {
20
+ server.middlewares.use((request, response, next) => {
21
+ const file = files[request.url?.split('?')[0]?.slice(1) ?? '']
22
+ if (!file) return next()
23
+ response.setHeader('Content-Type', file.type)
24
+ response.end(file.content())
25
+ })
26
+ },
27
+ generateBundle() {
28
+ if (this.environment?.config.build.ssr) return
29
+ for (const [fileName, file] of Object.entries(files)) {
30
+ this.emitFile({type: 'asset', fileName, source: file.content()})
31
+ }
32
+ },
33
+ }
34
+ }
package/src/vite.ts CHANGED
@@ -4,10 +4,12 @@ import {defu} from 'defu'
4
4
  import {execSync} from 'node:child_process'
5
5
  import type {PluginOption} from 'vite'
6
6
  import {type Logo, logoPlugin} from './vite/logo.js'
7
+ import {type AppOptions, manifestPlugin} from './vite/manifest.js'
7
8
  import {prerenderPlugin, type PrerenderOptions} from './vite/prerender.js'
8
9
 
9
10
  export {PAGES_DIR, type PrerenderOptions} from './vite/prerender.js'
10
11
  export type {Logo} from './vite/logo.js'
12
+ export type {AppOptions} from './vite/manifest.js'
11
13
 
12
14
  /** Settings a tool passes to `emberbox()`. */
13
15
  export interface EmberboxOptions {
@@ -21,6 +23,8 @@ export interface EmberboxOptions {
21
23
  logo?: Logo
22
24
  /** Which public pages are prerendered at build time; needs `url`. */
23
25
  prerender?: PrerenderOptions
26
+ /** Makes the tool installable as an app under this name; needs `logo` for the app icons. */
27
+ app?: AppOptions
24
28
  }
25
29
 
26
30
  const COLORS = ['primary', 'signal', 'success', 'info', 'warning', 'error']
@@ -111,6 +115,9 @@ export function emberbox(options: EmberboxOptions = {}): PluginOption[] {
111
115
  if (options.prerender && !url) {
112
116
  throw new Error('emberbox(): prerendering needs a url.')
113
117
  }
118
+ if (options.app && !options.logo) {
119
+ throw new Error('emberbox(): an installable app needs a logo.')
120
+ }
114
121
  return [
115
122
  ui({
116
123
  router: options.router ?? true,
@@ -132,7 +139,7 @@ export function emberbox(options: EmberboxOptions = {}): PluginOption[] {
132
139
  },
133
140
  optimizeDeps: {exclude: ['@emberbox/ui', '@iconify/vue']},
134
141
  resolve: {dedupe: ['vue', 'vue-router', '@nuxt/ui']},
135
- ssr: {noExternal: ['@emberbox/ui', '@nuxt/ui']},
142
+ ssr: {noExternal: ['@emberbox/ui', '@nuxt/ui', 'vue-router']},
136
143
  }),
137
144
  transformIndexHtml: () => [
138
145
  {tag: 'script', children: COLOR_MODE_SCRIPT, injectTo: 'head'},
@@ -144,6 +151,7 @@ export function emberbox(options: EmberboxOptions = {}): PluginOption[] {
144
151
  },
145
152
  },
146
153
  options.logo ? logoPlugin(options.logo) : false,
154
+ options.app && options.logo ? manifestPlugin(options.app) : false,
147
155
  options.prerender && url ? prerenderPlugin(options.prerender, url) : false,
148
156
  ]
149
157
  }