@skathi-native/cli 0.0.0 → 0.1.0-beta.2

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 (81) hide show
  1. package/aot-inputs.ts +294 -0
  2. package/ascii.ts +397 -0
  3. package/build.ts +1236 -0
  4. package/check/builtins.json +1219 -0
  5. package/check/builtins.ts +112 -0
  6. package/check/codes.ts +178 -0
  7. package/check/command.ts +105 -0
  8. package/check/config.ts +76 -0
  9. package/check/finding.ts +30 -0
  10. package/check/graph.ts +69 -0
  11. package/check/highlight.ts +40 -0
  12. package/check/index.ts +308 -0
  13. package/check/packages.ts +54 -0
  14. package/check/program.ts +209 -0
  15. package/check/report.ts +219 -0
  16. package/check/rules.ts +307 -0
  17. package/check/schema.ts +69 -0
  18. package/check/typing.ts +703 -0
  19. package/cli/assets.ts +114 -0
  20. package/cli/build-app.ts +587 -0
  21. package/cli/config.ts +646 -0
  22. package/cli/create.ts +327 -0
  23. package/cli/dev.ts +92 -0
  24. package/cli/elf.ts +142 -0
  25. package/cli/help.ts +160 -0
  26. package/cli/host-fetch.ts +178 -0
  27. package/cli/host.ts +378 -0
  28. package/cli/icon.ts +303 -0
  29. package/cli/macos-bundle.ts +451 -0
  30. package/cli/pe.ts +429 -0
  31. package/cli/platform.ts +74 -0
  32. package/cli/sk.ts +290 -0
  33. package/cli/skpk.ts +268 -0
  34. package/cli/stamp.ts +254 -0
  35. package/cli/term.ts +176 -0
  36. package/cli/versioninfo.ts +123 -0
  37. package/css-prefixes.ts +118 -0
  38. package/devtools/apps.ts +68 -0
  39. package/devtools/commands.ts +860 -0
  40. package/devtools/ctl.ts +166 -0
  41. package/package.json +52 -3
  42. package/prod.ts +701 -0
  43. package/runtime-path.ts +24 -0
  44. package/tailwind.ts +76 -0
  45. package/templates/minimal/assets/hello.txt +1 -0
  46. package/templates/minimal/src/App.vue +142 -0
  47. package/templates/minimal/src/main.ts +4 -0
  48. package/templates/showcase/assets/hello.txt +1 -0
  49. package/templates/showcase/assets/media/aurora.png +0 -0
  50. package/templates/showcase/assets/media/clip.mp4 +0 -0
  51. package/templates/showcase/assets/media/sticker.tgs +0 -0
  52. package/templates/showcase/src/App.vue +128 -0
  53. package/templates/showcase/src/components/Card.vue +41 -0
  54. package/templates/showcase/src/components/Icon.vue +29 -0
  55. package/templates/showcase/src/components/Missing.vue +26 -0
  56. package/templates/showcase/src/components/Page.vue +41 -0
  57. package/templates/showcase/src/components/Titlebar.vue +128 -0
  58. package/templates/showcase/src/engine.ts +33 -0
  59. package/templates/showcase/src/icons.ts +22 -0
  60. package/templates/showcase/src/main.ts +7 -0
  61. package/templates/showcase/src/pages/AudioPage.vue +124 -0
  62. package/templates/showcase/src/pages/FilesPage.vue +91 -0
  63. package/templates/showcase/src/pages/FormsPage.vue +139 -0
  64. package/templates/showcase/src/pages/GraphicsPage.vue +156 -0
  65. package/templates/showcase/src/pages/HomePage.vue +141 -0
  66. package/templates/showcase/src/pages/LocalePage.vue +71 -0
  67. package/templates/showcase/src/pages/MediaPage.vue +85 -0
  68. package/templates/showcase/src/pages/MotionPage.vue +199 -0
  69. package/templates/showcase/src/pages/NetworkPage.vue +103 -0
  70. package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
  71. package/templates/showcase/src/pages/StoragePage.vue +117 -0
  72. package/templates/showcase/src/pages/StylesPage.vue +199 -0
  73. package/templates/showcase/src/pages/SystemPage.vue +89 -0
  74. package/templates/showcase/src/pages/TrayPage.vue +66 -0
  75. package/templates/showcase/src/pages/WindowPage.vue +128 -0
  76. package/templates/showcase/src/product.ts +2 -0
  77. package/templates/showcase/src/router.ts +149 -0
  78. package/templates/showcase/src/stores/counter.ts +9 -0
  79. package/templates/showcase/src/stores/desktop.ts +115 -0
  80. package/templates/showcase/src/styles.css +140 -0
  81. package/vitecompat.ts +1151 -0
@@ -0,0 +1,71 @@
1
+ <script setup lang="ts">
2
+ import { appLocale } from "@skathi-native/runtime";
3
+ import { computed, ref } from "vue";
4
+ import Card from "../components/Card.vue";
5
+ import Page from "../components/Page.vue";
6
+ import { nav } from "../engine";
7
+ import { productName } from "../product";
8
+
9
+ // `{name}` is filled from the arguments to t(). `sk.*` keys reword the SDK's own text (its dialogs and prompts).
10
+ appLocale.define({
11
+ en: { greeting: "Welcome to {name}", files: "{count} files selected", today: "Today is a good day to ship." },
12
+ de: { greeting: "Willkommen bei {name}", files: "{count} Dateien ausgewählt", today: "Heute ist ein guter Tag." },
13
+ es: { greeting: "Bienvenido a {name}", files: "{count} archivos seleccionados", today: "Hoy es un buen día." },
14
+ ja: { greeting: "{name} へようこそ", files: "{count} 個のファイルを選択", today: "今日はリリース日和です。" },
15
+ });
16
+
17
+ const LANGUAGES = [null, "en", "de", "es", "ja"] as const;
18
+ const language = ref(appLocale.language);
19
+ const count = ref(3);
20
+
21
+ function choose(tag: (typeof LANGUAGES)[number]) {
22
+ appLocale.language = tag;
23
+ language.value = tag;
24
+ }
25
+
26
+ // appLocale is not reactive: reading `language` is what makes this recompute after a switch.
27
+ const text = computed(() => {
28
+ void language.value;
29
+ return {
30
+ resolved: appLocale.resolved,
31
+ greeting: appLocale.t("greeting", { name: productName }),
32
+ files: appLocale.t("files", { count: count.value }),
33
+ today: appLocale.t("today"),
34
+ };
35
+ });
36
+ </script>
37
+
38
+ <template>
39
+ <Page title="Locale" lead="A message catalog per language; t() picks the best match for the user's languages.">
40
+ <Card title="Language" :api="`resolved: ${text.resolved}`">
41
+ <div class="row">
42
+ <button
43
+ v-for="tag in LANGUAGES"
44
+ :key="tag ?? 'auto'"
45
+ class="btn"
46
+ :class="{ on: language === tag }"
47
+ @click="choose(tag)"
48
+ >
49
+ {{ tag ?? "System" }}
50
+ </button>
51
+ </div>
52
+ <p class="faint">System follows navigator.languages: {{ nav.languages?.join(", ") }}</p>
53
+ </Card>
54
+
55
+ <Card title="Messages" api="appLocale.t()">
56
+ <div class="greeting">{{ text.greeting }}</div>
57
+ <div class="row">
58
+ <button class="btn" @click="count++">+1</button>
59
+ <span class="muted">{{ text.files }}</span>
60
+ </div>
61
+ <p class="muted">{{ text.today }}</p>
62
+ </Card>
63
+ </Page>
64
+ </template>
65
+
66
+ <style scoped>
67
+ .greeting {
68
+ font-size: 22px;
69
+ font-weight: 700;
70
+ }
71
+ </style>
@@ -0,0 +1,85 @@
1
+ <script setup lang="ts">
2
+ import { ref } from "vue";
3
+ import Card from "../components/Card.vue";
4
+ import Page from "../components/Page.vue";
5
+
6
+ const video = ref<SkMediaElement | null>(null);
7
+ const playing = ref(true);
8
+ const toggleVideo = () => {
9
+ if (!video.value) return;
10
+ if (video.value.paused) video.value.play();
11
+ else video.value.pause();
12
+ playing.value = !video.value.paused;
13
+ };
14
+
15
+ const stickerPlaying = ref(true);
16
+ const speed = ref(1);
17
+ </script>
18
+
19
+ <template>
20
+ <Page
21
+ title="Media"
22
+ lead="Images, video and Lottie animations, loaded from assets/media. sk.config.json puts that folder in its own pack, media.skpk, beside the exe."
23
+ >
24
+ <div class="tiles">
25
+ <Card class="tile" title="Image" api="<img>">
26
+ <img class="shot zoom" src="assets/media/aurora.png" alt="Aurora" />
27
+ </Card>
28
+
29
+ <Card class="tile" title="Video" api="<video>">
30
+ <video ref="video" class="shot" src="assets/media/clip.mp4" autoplay loop muted />
31
+ <div class="row">
32
+ <button class="btn" @click="toggleVideo">{{ playing ? "Pause" : "Play" }}</button>
33
+ <span class="faint">Decoded by the OS media stack.</span>
34
+ </div>
35
+ </Card>
36
+
37
+ <Card class="tile" title="Lottie sticker" api="<lottie>">
38
+ <div class="stage">
39
+ <lottie class="sticker" src="assets/media/sticker.tgs" loop :autoplay="stickerPlaying" :speed="speed" />
40
+ </div>
41
+ <div class="row">
42
+ <button class="btn" @click="stickerPlaying = !stickerPlaying">{{ stickerPlaying ? "Pause" : "Play" }}</button>
43
+ <button v-for="s in [0.5, 1, 2]" :key="s" class="btn" :class="{ on: speed === s }" @click="speed = s">
44
+ {{ s }}×
45
+ </button>
46
+ </div>
47
+ </Card>
48
+ </div>
49
+ </Page>
50
+ </template>
51
+
52
+ <style scoped>
53
+ .tiles {
54
+ display: flex;
55
+ flex-wrap: wrap;
56
+ gap: 16px;
57
+ }
58
+ .tile {
59
+ width: 320px;
60
+ }
61
+ .shot {
62
+ width: 100%;
63
+ height: 160px;
64
+ border-radius: 10px;
65
+ object-fit: cover;
66
+ }
67
+ .zoom {
68
+ transition: transform 300ms ease;
69
+ }
70
+ .zoom:hover {
71
+ transform: scale(1.04);
72
+ }
73
+ .stage {
74
+ height: 160px;
75
+ display: flex;
76
+ align-items: center;
77
+ justify-content: center;
78
+ border-radius: 10px;
79
+ background: radial-gradient(circle, #8b9dff30, #00000000);
80
+ }
81
+ .sticker {
82
+ width: 120px;
83
+ height: 120px;
84
+ }
85
+ </style>
@@ -0,0 +1,199 @@
1
+ <script setup lang="ts">
2
+ import { ref, shallowRef } from "vue";
3
+ import Card from "../components/Card.vue";
4
+ import Page from "../components/Page.vue";
5
+
6
+ const expanded = ref(false);
7
+
8
+ const target = shallowRef<SkElement | null>(null);
9
+ const shake = () =>
10
+ target.value?.animate(
11
+ [
12
+ { transform: "translateX(0px)" },
13
+ { transform: "translateX(-10px)" },
14
+ { transform: "translateX(10px)" },
15
+ { transform: "translateX(0px)" },
16
+ ],
17
+ { duration: 360, easing: "ease-in-out", iterations: 2 },
18
+ );
19
+ const spin = () =>
20
+ target.value?.animate([{ transform: "rotate(0deg) scale(1)" }, { transform: "rotate(360deg) scale(1.15)" }], {
21
+ duration: 700,
22
+ easing: "cubic-bezier(0.34, 1.56, 0.64, 1)",
23
+ });
24
+
25
+ const shown = ref(true);
26
+ const words = ["Native", "Fast", "Small", "Vue"];
27
+ const word = ref(0);
28
+ const nextWord = () => (word.value = (word.value + 1) % words.length);
29
+ </script>
30
+
31
+ <template>
32
+ <Page
33
+ title="Motion"
34
+ lead="CSS transitions and keyframes run in the renderer; the Web Animations API drives them from script."
35
+ >
36
+ <Card title="Transition" api="transition">
37
+ <div class="track" @click="expanded = !expanded">
38
+ <div class="thumb" :class="{ expanded }">{{ expanded ? "wide" : "narrow" }} · click</div>
39
+ </div>
40
+ </Card>
41
+
42
+ <Card title="Keyframes" api="@keyframes">
43
+ <div class="row gap">
44
+ <div class="pulse" />
45
+ <div class="orbit"><div class="moon" /></div>
46
+ <div class="shimmer">Loading…</div>
47
+ </div>
48
+ </Card>
49
+
50
+ <Card title="Web Animations" api="element.animate()">
51
+ <div class="row gap">
52
+ <div ref="target" class="box">✦</div>
53
+ <button class="btn" @click="shake">Shake</button>
54
+ <button class="btn" @click="spin">Spin</button>
55
+ </div>
56
+ </Card>
57
+
58
+ <Card title="Vue transitions" api="<Transition>">
59
+ <div class="row">
60
+ <button class="btn" @click="shown = !shown">{{ shown ? "Hide" : "Show" }} the panel</button>
61
+ <button class="btn" @click="nextWord">Next word</button>
62
+ </div>
63
+ <Transition name="fade">
64
+ <div v-if="shown" class="panel">Enter and leave classes, as in a browser.</div>
65
+ </Transition>
66
+ <Transition name="swap" mode="out-in">
67
+ <div :key="word" class="word">{{ words[word] }}</div>
68
+ </Transition>
69
+ </Card>
70
+ </Page>
71
+ </template>
72
+
73
+ <style scoped>
74
+ .gap {
75
+ gap: 20px;
76
+ }
77
+ .track {
78
+ height: 44px;
79
+ padding: 4px;
80
+ border-radius: 12px;
81
+ background: #00000040;
82
+ cursor: pointer;
83
+ }
84
+ .thumb {
85
+ width: 160px;
86
+ height: 36px;
87
+ display: flex;
88
+ align-items: center;
89
+ justify-content: center;
90
+ border-radius: 9px;
91
+ background: var(--surface-strong);
92
+ font-size: 12px;
93
+ color: var(--text-muted);
94
+ transition:
95
+ width 420ms cubic-bezier(0.34, 1.56, 0.64, 1),
96
+ background-color 420ms ease,
97
+ color 420ms ease;
98
+ }
99
+ .thumb.expanded {
100
+ width: 420px;
101
+ background: var(--accent-strong);
102
+ color: #ffffff;
103
+ }
104
+ @keyframes pulse {
105
+ 0% {
106
+ transform: scale(0.8);
107
+ opacity: 1;
108
+ }
109
+ 100% {
110
+ transform: scale(1.6);
111
+ opacity: 0;
112
+ }
113
+ }
114
+ .pulse {
115
+ width: 28px;
116
+ height: 28px;
117
+ border-radius: 14px;
118
+ background: var(--accent);
119
+ animation: pulse 1.4s ease-out infinite;
120
+ }
121
+ @keyframes spin {
122
+ to {
123
+ transform: rotate(360deg);
124
+ }
125
+ }
126
+ .orbit {
127
+ width: 48px;
128
+ height: 48px;
129
+ border-radius: 24px;
130
+ border: 1px dashed var(--line-strong);
131
+ animation: spin 3s linear infinite;
132
+ }
133
+ .moon {
134
+ width: 12px;
135
+ height: 12px;
136
+ margin-left: 18px;
137
+ margin-top: -6px;
138
+ border-radius: 6px;
139
+ background: #b48cff;
140
+ }
141
+ @keyframes shimmer {
142
+ 50% {
143
+ opacity: 0.35;
144
+ }
145
+ }
146
+ .shimmer {
147
+ padding: 8px 14px;
148
+ border-radius: 8px;
149
+ background: var(--surface-strong);
150
+ font-size: 12px;
151
+ animation: shimmer 1.6s ease-in-out infinite;
152
+ }
153
+ .box {
154
+ width: 52px;
155
+ height: 52px;
156
+ display: flex;
157
+ align-items: center;
158
+ justify-content: center;
159
+ border-radius: 14px;
160
+ background: linear-gradient(135deg, var(--accent), #b48cff);
161
+ color: #ffffff;
162
+ font-size: 22px;
163
+ }
164
+ .panel {
165
+ padding: 14px;
166
+ border-radius: 10px;
167
+ background: var(--accent-soft);
168
+ font-size: 13px;
169
+ }
170
+ .fade-enter-active,
171
+ .fade-leave-active {
172
+ transition:
173
+ opacity 250ms ease,
174
+ transform 250ms ease;
175
+ }
176
+ .fade-enter-from,
177
+ .fade-leave-to {
178
+ opacity: 0;
179
+ transform: translateY(-6px);
180
+ }
181
+ .word {
182
+ font-size: 28px;
183
+ font-weight: 700;
184
+ }
185
+ .swap-enter-active,
186
+ .swap-leave-active {
187
+ transition:
188
+ opacity 180ms ease,
189
+ transform 180ms ease;
190
+ }
191
+ .swap-enter-from {
192
+ opacity: 0;
193
+ transform: translateY(10px);
194
+ }
195
+ .swap-leave-to {
196
+ opacity: 0;
197
+ transform: translateY(-10px);
198
+ }
199
+ </style>
@@ -0,0 +1,103 @@
1
+ <script setup lang="ts">
2
+ import { onUnmounted, ref, shallowRef } from "vue";
3
+ import Card from "../components/Card.vue";
4
+ import Page from "../components/Page.vue";
5
+ import { describe, http, nav } from "../engine";
6
+
7
+ const quote = ref("");
8
+ const took = ref("");
9
+ async function load() {
10
+ const start = performance.now();
11
+ try {
12
+ const res = await http("https://api.github.com/zen");
13
+ quote.value = res.ok ? await res.text() : `HTTP ${res.status}`;
14
+ } catch (e) {
15
+ quote.value = describe(e);
16
+ }
17
+ took.value = `${Math.round(performance.now() - start)} ms`;
18
+ }
19
+
20
+ const ECHO = "wss://echo.websocket.org";
21
+ const socket = shallowRef<SkWebSocket | null>(null);
22
+ const message = ref("Hello over a socket");
23
+ const log = ref<string[]>([]);
24
+ const note = (line: string) => (log.value = [line, ...log.value].slice(0, 6));
25
+
26
+ function connect() {
27
+ const ws = new WebSocket(ECHO);
28
+ ws.onopen = () => note("open");
29
+ ws.onmessage = (e) => note(`← ${String(e.data)}`);
30
+ ws.onclose = (e) => {
31
+ note(`closed (${e.code})`);
32
+ socket.value = null;
33
+ };
34
+ socket.value = ws;
35
+ }
36
+ function send(ws: SkWebSocket) {
37
+ ws.send(message.value);
38
+ note(`→ ${message.value}`);
39
+ }
40
+ onUnmounted(() => socket.value?.close());
41
+
42
+ const webView = skFeatures.iframe;
43
+ const embedded = ref(false);
44
+ </script>
45
+
46
+ <template>
47
+ <Page
48
+ title="Network"
49
+ lead='fetch and WebSocket as in a browser; responses are cached on disk (sk.config.json "httpCache").'
50
+ >
51
+ <Card title="fetch" :api="nav.onLine ? 'online' : 'offline'">
52
+ <div class="row">
53
+ <button class="btn primary" @click="load">GET api.github.com/zen</button>
54
+ <span class="faint">{{ took }}</span>
55
+ </div>
56
+ <p v-if="quote" class="quote">{{ quote }}</p>
57
+ </Card>
58
+
59
+ <Card title="WebSocket" :api="ECHO">
60
+ <div class="row">
61
+ <button v-if="!socket" class="btn" @click="connect">Connect</button>
62
+ <template v-else>
63
+ <input v-model="message" class="input grow" @keyup.enter="send(socket)" />
64
+ <button class="btn primary" @click="send(socket)">Send</button>
65
+ <button class="btn" @click="socket.close()">Close</button>
66
+ </template>
67
+ </div>
68
+ <div v-for="(line, i) in log" :key="i" class="mono line">{{ line }}</div>
69
+ </Card>
70
+
71
+ <Card title="Embedded web page" api="<iframe>">
72
+ <template v-if="webView">
73
+ <div class="row">
74
+ <button class="btn" @click="embedded = !embedded">{{ embedded ? "Remove it" : "Embed example.com" }}</button>
75
+ </div>
76
+ <iframe v-if="embedded" class="frame" src="https://example.com" />
77
+ </template>
78
+ <p v-else class="muted">This platform has no web view for &lt;iframe&gt; (it needs WebView2 on Windows).</p>
79
+ <p class="faint">The page runs in the system web view, isolated from the app; postMessage talks across.</p>
80
+ </Card>
81
+ </Page>
82
+ </template>
83
+
84
+ <style scoped>
85
+ .grow {
86
+ flex: 1;
87
+ }
88
+ .quote {
89
+ margin: 0;
90
+ font-size: 16px;
91
+ font-style: italic;
92
+ color: var(--text);
93
+ }
94
+ .line {
95
+ color: var(--text-muted);
96
+ }
97
+ .frame {
98
+ width: 100%;
99
+ height: 260px;
100
+ border-radius: 10px;
101
+ border: 1px solid var(--line);
102
+ }
103
+ </style>
@@ -0,0 +1,80 @@
1
+ <script setup lang="ts">
2
+ import { appPermissions } from "@skathi-native/runtime";
3
+ import { onMounted, onUnmounted, ref } from "vue";
4
+ import Card from "../components/Card.vue";
5
+ import Page from "../components/Page.vue";
6
+ import { describe, nav } from "../engine";
7
+
8
+ const rows = ref(appPermissions.list());
9
+ const asking = ref("");
10
+ const status = ref("");
11
+
12
+ const read = () => (rows.value = appPermissions.list());
13
+ onMounted(() => appPermissions.addEventListener("change", read));
14
+ onUnmounted(() => appPermissions.removeEventListener("change", read));
15
+
16
+ const tone = (state: string) => (state === "granted" ? "ok" : state === "prompt" ? "warn" : "bad");
17
+
18
+ // Resolves with a fresh read from the OS once the user answers (on Windows, once the Settings toggle moves).
19
+ async function request(name: string) {
20
+ asking.value = name;
21
+ try {
22
+ await appPermissions.request(name);
23
+ } catch (e) {
24
+ status.value = describe(e);
25
+ } finally {
26
+ asking.value = "";
27
+ read();
28
+ }
29
+ }
30
+
31
+ async function tryMicrophone() {
32
+ try {
33
+ const stream = await nav.mediaDevices.getUserMedia({ audio: true });
34
+ status.value = stream.getAudioTracks()[0]?.label ?? "a microphone";
35
+ for (const track of stream.getTracks()) track.stop();
36
+ } catch (e) {
37
+ status.value = describe(e);
38
+ }
39
+ }
40
+ </script>
41
+
42
+ <template>
43
+ <Page
44
+ title="Permissions"
45
+ lead="What the OS lets this app use, read live. Unlike a browser, an app can ask before it needs the device."
46
+ >
47
+ <Card :title="`On ${appPermissions.platform}`" api="appPermissions">
48
+ <div v-for="row in rows" :key="row.name" class="perm">
49
+ <span class="name">{{ row.name }}</span>
50
+ <span class="pill" :class="tone(row.state)">{{ row.state }}</span>
51
+ <button v-if="row.canRequest" class="btn" :disabled="asking === row.name" @click="request(row.name)">
52
+ {{ asking === row.name ? "Asking…" : "Ask" }}
53
+ </button>
54
+ </div>
55
+ </Card>
56
+
57
+ <Card title="Use it" api="getUserMedia()">
58
+ <div class="row">
59
+ <button class="btn" @click="tryMicrophone">Open the microphone</button>
60
+ <span class="faint">{{ status }}</span>
61
+ </div>
62
+ <p class="faint">
63
+ Opens and closes the device at once; the first use asks for consent if nothing was granted yet.
64
+ </p>
65
+ </Card>
66
+ </Page>
67
+ </template>
68
+
69
+ <style scoped>
70
+ .perm {
71
+ display: flex;
72
+ align-items: center;
73
+ gap: 12px;
74
+ height: 34px;
75
+ }
76
+ .name {
77
+ width: 160px;
78
+ font-size: 13px;
79
+ }
80
+ </style>
@@ -0,0 +1,117 @@
1
+ <script setup lang="ts">
2
+ import { appStorage } from "@skathi-native/runtime";
3
+ import { onMounted, ref } from "vue";
4
+ import Card from "../components/Card.vue";
5
+ import Missing from "../components/Missing.vue";
6
+ import Page from "../components/Page.vue";
7
+ import { describe, idb, storage } from "../engine";
8
+ import { appName } from "../product";
9
+
10
+ const visits = ref(0);
11
+ const note = ref("");
12
+ onMounted(() => {
13
+ if (!storage) return;
14
+ visits.value = Number(storage.getItem("visits") ?? 0) + 1;
15
+ storage.setItem("visits", String(visits.value));
16
+ note.value = storage.getItem("note") ?? "";
17
+ });
18
+
19
+ interface Todo {
20
+ id: number;
21
+ text: string;
22
+ }
23
+ const todos = ref<Todo[]>([]);
24
+ const draft = ref("");
25
+ const status = ref("");
26
+
27
+ const done = <T,>(request: SkIDBRequest<T>) =>
28
+ new Promise<T>((resolve, reject) => {
29
+ request.onsuccess = () => resolve(request.result);
30
+ request.onerror = () => reject(request.error);
31
+ });
32
+
33
+ let db: SkIDBDatabase | null = null;
34
+ async function store(mode: "readonly" | "readwrite") {
35
+ if (!idb) throw new Error("no IndexedDB");
36
+ if (!db) {
37
+ const open = idb.open(appName, 1);
38
+ open.onupgradeneeded = () => open.result.createObjectStore("todos", { keyPath: "id", autoIncrement: true });
39
+ db = await done(open);
40
+ }
41
+ return db.transaction("todos", mode).objectStore("todos");
42
+ }
43
+
44
+ async function update(change?: (todos: SkIDBObjectStore) => SkIDBRequest) {
45
+ try {
46
+ if (change) await done(change(await store("readwrite")));
47
+ todos.value = await done((await store("readonly")).getAll() as SkIDBRequest<Todo[]>);
48
+ } catch (e) {
49
+ status.value = describe(e);
50
+ }
51
+ }
52
+ onMounted(() => idb && update());
53
+
54
+ function add() {
55
+ const text = draft.value.trim();
56
+ if (!text) return;
57
+ draft.value = "";
58
+ update((s) => s.add({ text }));
59
+ }
60
+ </script>
61
+
62
+ <template>
63
+ <Page title="Storage" lead="The web's storage APIs, kept on disk in the app's own data folder between runs.">
64
+ <Card title="localStorage" api="localStorage">
65
+ <template v-if="storage">
66
+ <p class="muted">This page was opened {{ visits }} times, counting earlier runs.</p>
67
+ <input
68
+ v-model="note"
69
+ class="input"
70
+ placeholder="A note that survives a restart"
71
+ @input="storage.setItem('note', note)"
72
+ />
73
+ </template>
74
+ <Missing v-else part="storages" />
75
+ </Card>
76
+
77
+ <Card title="IndexedDB" api="indexedDB">
78
+ <template v-if="idb">
79
+ <div class="row">
80
+ <input v-model="draft" class="input grow" placeholder="Something to do" @keyup.enter="add" />
81
+ <button class="btn primary" @click="add">Add</button>
82
+ </div>
83
+ <div v-for="todo in todos" :key="todo.id" class="todo">
84
+ <span>{{ todo.text }}</span>
85
+ <button class="btn" @click="update((s) => s.delete(todo.id))">Done</button>
86
+ </div>
87
+ <p v-if="!todos.length" class="faint">Nothing yet. Items are rows in a SQLite file under the data folder.</p>
88
+ <p v-if="status" class="faint">{{ status }}</p>
89
+ </template>
90
+ <Missing v-else part="indexeddb" />
91
+ </Card>
92
+
93
+ <Card title="Where it goes" api="appStorage">
94
+ <div class="kv">
95
+ <span class="k">folder</span><span class="v mono">{{ appStorage.folder || "in memory" }}</span>
96
+ </div>
97
+ <div class="kv">
98
+ <span class="k">encrypted</span><span class="v">{{ appStorage.encrypted ? "yes" : "no" }}</span>
99
+ </div>
100
+ </Card>
101
+ </Page>
102
+ </template>
103
+
104
+ <style scoped>
105
+ .grow {
106
+ flex: 1;
107
+ }
108
+ .todo {
109
+ display: flex;
110
+ align-items: center;
111
+ justify-content: space-between;
112
+ padding: 8px 8px 8px 12px;
113
+ border-radius: 9px;
114
+ background: var(--surface-hover);
115
+ font-size: 13px;
116
+ }
117
+ </style>