@inglorious/web 6.18.0 → 6.19.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.
package/README.md CHANGED
@@ -1544,6 +1544,23 @@ A failed check keeps the last answer, so nobody is blocked by being offline. Ren
1544
1544
 
1545
1545
  ---
1546
1546
 
1547
+ ## App Links
1548
+
1549
+ With Universal Links (iOS) and App Links (Android), a link to the app's domain opens the installed app instead of the browser. `@inglorious/web/app-links` sends the router to that link's path, both for the link that started the app and for links that arrive while it runs. It does nothing outside a native app.
1550
+
1551
+ ```javascript
1552
+ import { App } from "@capacitor/app"
1553
+ import { watchAppLinks } from "@inglorious/web/app-links"
1554
+
1555
+ watchAppLinks(App, store)
1556
+ ```
1557
+
1558
+ `{ id }` as a third argument notifies a router with another id; it returns a function that stops listening.
1559
+
1560
+ The operating system opens the app only when the domain vouches for it: iOS reads `/.well-known/apple-app-site-association` and Android `/.well-known/assetlinks.json`, and the app declares the domain (Associated Domains on iOS, an intent filter on Android). `@inglorious/create-app --mobile` generates the iOS file and documents the rest.
1561
+
1562
+ ---
1563
+
1547
1564
  ## Forms
1548
1565
 
1549
1566
  `@inglorious/web` includes a small but powerful `form` type for managing form state inside your entity store. It offers:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.18.0",
3
+ "version": "6.19.0",
4
4
  "description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "contributors": [
@@ -37,6 +37,10 @@
37
37
  "types": "./types/analytics.d.ts",
38
38
  "import": "./src/analytics/index.js"
39
39
  },
40
+ "./app-links": {
41
+ "types": "./types/app-links.d.ts",
42
+ "import": "./src/app-links/index.js"
43
+ },
40
44
  "./decorators/with-debounce": {
41
45
  "types": "./types/decorators/with-debounce.d.ts",
42
46
  "import": "./src/decorators/with-debounce/index.js"
@@ -0,0 +1,54 @@
1
+ import { isNative } from "../mobile/index.js"
2
+
3
+ const DEFAULT_ID = "router"
4
+ const URL_OPEN_EVENT = "appUrlOpen"
5
+
6
+ /**
7
+ * Opens Universal Links and App Links in the app's router: a link to the
8
+ * app's domain, tapped while the app is installed, opens the app instead of
9
+ * the browser, and the router goes to the link's path. Covers a link that
10
+ * starts the app as well as one that arrives while it runs. Does nothing
11
+ * outside a native app.
12
+ *
13
+ * @param {Object} app `App` from `@capacitor/app`.
14
+ * @param {{ notify: (type: string, payload?: unknown) => void }} store
15
+ * @param {{ id?: string }} [options] The router entity, `router` by default.
16
+ * @returns {() => Promise<void>} Stops listening.
17
+ *
18
+ * @example
19
+ * watchAppLinks(App, store)
20
+ */
21
+ export function watchAppLinks(app, store, { id = DEFAULT_ID } = {}) {
22
+ if (!isNative()) return async () => {}
23
+
24
+ openLaunchUrl()
25
+ const listener = app.addListener(URL_OPEN_EVENT, ({ url }) => open(url))
26
+
27
+ return async function stop() {
28
+ const handle = await listener
29
+ await handle.remove()
30
+ }
31
+
32
+ async function openLaunchUrl() {
33
+ try {
34
+ const launch = await app.getLaunchUrl()
35
+ if (launch?.url) open(launch.url)
36
+ } catch {
37
+ // No launch URL to read is the same as none.
38
+ }
39
+ }
40
+
41
+ function open(url) {
42
+ const path = toPath(url)
43
+ if (path) store.notify(`#${id}:navigate`, path)
44
+ }
45
+ }
46
+
47
+ function toPath(url) {
48
+ try {
49
+ const { pathname, search, hash } = new URL(url)
50
+ return `${pathname}${search}${hash}`
51
+ } catch {
52
+ return null
53
+ }
54
+ }
@@ -32,7 +32,7 @@ export function init() {
32
32
  if (areListenersInitialized) return
33
33
 
34
34
  // Only a press that would submit or reset a form without saying so: any
35
- // other button's default is its own, such as a `commandfor` opening a menu.
35
+ // other button's default is its own, such as a `popovertarget` opening a menu.
36
36
  document.addEventListener("click", (event) => {
37
37
  const button = event.target.closest("button")
38
38
 
@@ -0,0 +1,27 @@
1
+ import { Api } from "./mount"
2
+
3
+ /** What `watchAppLinks` needs of `App` from `@capacitor/app`. */
4
+ export interface AppLinksApp {
5
+ getLaunchUrl(): Promise<{ url: string } | undefined>
6
+ addListener(
7
+ event: "appUrlOpen",
8
+ listener: (event: { url: string }) => void,
9
+ ): Promise<{ remove(): Promise<void> }>
10
+ }
11
+
12
+ export interface WatchAppLinksOptions {
13
+ /** The router entity to notify. Defaults to `router`. */
14
+ id?: string
15
+ }
16
+
17
+ /**
18
+ * Opens Universal Links and App Links in the router, both the link that
19
+ * started the app and those that arrive while it runs, notifying
20
+ * `#${id}:navigate`. Does nothing outside a native app.
21
+ * @returns A function that stops listening.
22
+ */
23
+ export declare function watchAppLinks(
24
+ app: AppLinksApp,
25
+ store: Pick<Api, "notify">,
26
+ options?: WatchAppLinksOptions,
27
+ ): () => Promise<void>