@inglorious/web 6.29.0 → 6.31.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 +31 -2255
- package/package.json +3 -3
- package/src/analytics/index.js +14 -5
- package/src/auth/index.js +23 -21
- package/src/decorators/limit.js +67 -0
- package/src/decorators/with-debounce/index.js +6 -57
- package/src/decorators/with-throttle/index.js +12 -67
- package/src/device/geolocation/index.js +5 -4
- package/src/device/keyboard/index.js +58 -21
- package/src/mount.js +7 -4
- package/src/native/app-links/index.js +44 -30
- package/src/native/back-button/index.js +40 -21
- package/src/native/live-update/index.js +104 -92
- package/src/native/system-bars/index.js +63 -33
- package/src/native/update/index.js +37 -38
- package/src/pwa/index.js +67 -3
- package/src/pwa/update.js +10 -20
- package/types/analytics.d.ts +2 -0
- package/types/auth.d.ts +1 -10
- package/types/decorators/with-live-reload.d.ts +9 -5
- package/types/device/geolocation.d.ts +21 -3
- package/types/device/keyboard.d.ts +26 -6
- package/types/mount.d.ts +11 -5
- package/types/native/app-links.d.ts +22 -14
- package/types/native/back-button.d.ts +22 -12
- package/types/native/live-update.d.ts +25 -32
- package/types/native/system-bars.d.ts +24 -8
- package/types/native/update.d.ts +12 -25
- package/types/pwa.d.ts +11 -20
- package/src/pwa/handlers.js +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.31.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": [
|
|
@@ -174,11 +174,11 @@
|
|
|
174
174
|
"lit-html": "^3.3.3"
|
|
175
175
|
},
|
|
176
176
|
"peerDependencies": {
|
|
177
|
-
"@inglorious/store": "^12.
|
|
177
|
+
"@inglorious/store": "^12.9.0"
|
|
178
178
|
},
|
|
179
179
|
"devDependencies": {
|
|
180
180
|
"@inglorious/eslint-config": "1.2.0",
|
|
181
|
-
"prettier": "^3.
|
|
181
|
+
"prettier": "^3.10.0",
|
|
182
182
|
"typescript": "~7.0.2",
|
|
183
183
|
"vitest": "^5.1.0"
|
|
184
184
|
},
|
package/src/analytics/index.js
CHANGED
|
@@ -18,6 +18,9 @@ const DENIED = "denied"
|
|
|
18
18
|
*
|
|
19
19
|
* Everything vendor-specific is the `adapter`'s:
|
|
20
20
|
*
|
|
21
|
+
* - `isSupported()`, optional: `false` when there is nothing to measure with,
|
|
22
|
+
* such as an analytics project not set up yet. Then the prompt never shows
|
|
23
|
+
* and nothing reaches the adapter.
|
|
21
24
|
* - `setConsent(isGranted)`: starts or stops measuring.
|
|
22
25
|
* - `pageView({ route, path })`: one page or screen. An adapter whose
|
|
23
26
|
* library already counts history changes as page views can do nothing.
|
|
@@ -35,15 +38,20 @@ const DENIED = "denied"
|
|
|
35
38
|
* api.notify("#analytics:analyticsTrack", { name: "sign_up" })
|
|
36
39
|
*/
|
|
37
40
|
export function createAnalytics({ adapter }) {
|
|
41
|
+
const isSupported = () => adapter.isSupported?.() !== false
|
|
42
|
+
|
|
38
43
|
return {
|
|
39
44
|
create(entity) {
|
|
40
45
|
entity.consent ??= null
|
|
41
|
-
entity.isAsking
|
|
42
|
-
|
|
46
|
+
entity.isAsking =
|
|
47
|
+
isSupported() && (entity.isAsking ?? entity.consent === null)
|
|
48
|
+
if (entity.consent === GRANTED && isSupported()) {
|
|
49
|
+
call(() => adapter.setConsent(true))
|
|
50
|
+
}
|
|
43
51
|
},
|
|
44
52
|
|
|
45
53
|
routeChange(entity, { route, path } = {}) {
|
|
46
|
-
if (entity.consent !== GRANTED) return
|
|
54
|
+
if (entity.consent !== GRANTED || !isSupported()) return
|
|
47
55
|
call(() => adapter.pageView({ route, path }))
|
|
48
56
|
},
|
|
49
57
|
|
|
@@ -51,6 +59,7 @@ export function createAnalytics({ adapter }) {
|
|
|
51
59
|
const consent = isGranted ? GRANTED : DENIED
|
|
52
60
|
entity.consent = consent
|
|
53
61
|
entity.isAsking = false
|
|
62
|
+
if (!isSupported()) return
|
|
54
63
|
call(() => adapter.setConsent(isGranted))
|
|
55
64
|
if (!isGranted) return
|
|
56
65
|
|
|
@@ -61,11 +70,11 @@ export function createAnalytics({ adapter }) {
|
|
|
61
70
|
},
|
|
62
71
|
|
|
63
72
|
analyticsAsk(entity) {
|
|
64
|
-
entity.isAsking =
|
|
73
|
+
entity.isAsking = isSupported()
|
|
65
74
|
},
|
|
66
75
|
|
|
67
76
|
analyticsTrack(entity, { name, params } = {}) {
|
|
68
|
-
if (entity.consent !== GRANTED || !name) return
|
|
77
|
+
if (entity.consent !== GRANTED || !name || !isSupported()) return
|
|
69
78
|
call(() => adapter.event(name, params))
|
|
70
79
|
},
|
|
71
80
|
}
|
package/src/auth/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { handleRequest } from "@inglorious/store/async"
|
|
2
2
|
|
|
3
|
-
const DEFAULT_ID = "auth"
|
|
4
3
|
const DEFAULT_METHODS = ["password"]
|
|
5
4
|
const DEFAULT_MIN_PASSWORD_LENGTH = 8
|
|
6
5
|
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
@@ -29,8 +28,12 @@ const CODE = /^\d{6,10}$/
|
|
|
29
28
|
* `linkSubmit({ tokenHash, purpose })` signs in with the link of an email
|
|
30
29
|
* instead of its code, for a page the link opens.
|
|
31
30
|
*
|
|
32
|
-
* The `
|
|
33
|
-
*
|
|
31
|
+
* The entity's `user` follows the backend's session from the moment the entity
|
|
32
|
+
* is created: signed in or out, from this tab or another, and when a saved
|
|
33
|
+
* session is restored at launch, which sets `isReady`.
|
|
34
|
+
*
|
|
35
|
+
* The `adapter` talks to the backend: `onSessionChange`, `signIn`, `signUp`,
|
|
36
|
+
* `sendCode`, `verifyCode`, `verifyLink`, `resendCode`, `sendResetCode`,
|
|
34
37
|
* `updatePassword`, `signOut`, `eraseAccount`, `errorCode`, and optionally
|
|
35
38
|
* `accountExists`, which skips straight to the password or the new account,
|
|
36
39
|
* and `prepare(action)`, called as soon as the next step is known, such as
|
|
@@ -44,7 +47,6 @@ const CODE = /^\d{6,10}$/
|
|
|
44
47
|
*
|
|
45
48
|
* @example
|
|
46
49
|
* const types = { Auth: [createAuth({ adapter: supabaseAuth(supabase) }), screen] }
|
|
47
|
-
* watchAuth(supabaseAuth(supabase), store)
|
|
48
50
|
*/
|
|
49
51
|
export function createAuth({
|
|
50
52
|
adapter,
|
|
@@ -53,13 +55,29 @@ export function createAuth({
|
|
|
53
55
|
}) {
|
|
54
56
|
const hasPassword = methods.includes("password")
|
|
55
57
|
const hasCode = methods.includes("code")
|
|
58
|
+
const sessions = new Map()
|
|
56
59
|
|
|
57
60
|
return {
|
|
58
|
-
create(entity) {
|
|
61
|
+
create(entity, _, api) {
|
|
59
62
|
entity.user ??= null
|
|
60
63
|
entity.isReady ??= false
|
|
61
64
|
entity.methods = methods
|
|
62
65
|
reset(entity)
|
|
66
|
+
|
|
67
|
+
if (!adapter.onSessionChange) return
|
|
68
|
+
const { id } = entity
|
|
69
|
+
sessions.get(id)?.()
|
|
70
|
+
sessions.set(
|
|
71
|
+
id,
|
|
72
|
+
adapter.onSessionChange((user) =>
|
|
73
|
+
api.notify(`#${id}:sessionChange`, user),
|
|
74
|
+
),
|
|
75
|
+
)
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
destroy(entity) {
|
|
79
|
+
sessions.get(entity.id)?.()
|
|
80
|
+
sessions.delete(entity.id)
|
|
63
81
|
},
|
|
64
82
|
|
|
65
83
|
sessionChange(entity, user) {
|
|
@@ -285,22 +303,6 @@ export function createAuth({
|
|
|
285
303
|
}
|
|
286
304
|
}
|
|
287
305
|
|
|
288
|
-
/**
|
|
289
|
-
* Keeps the entity's `user` in step with the backend's session: signed in or
|
|
290
|
-
* out, from this tab or another, and when a saved session is restored at
|
|
291
|
-
* launch, which sets `isReady`.
|
|
292
|
-
*
|
|
293
|
-
* @param {{ onSessionChange: (listener: (user: object | null) => void) => () => void }} adapter
|
|
294
|
-
* @param {{ notify: (type: string, payload?: unknown) => void }} store
|
|
295
|
-
* @param {{ id?: string }} [options]
|
|
296
|
-
* @returns {() => void} Stops watching.
|
|
297
|
-
*/
|
|
298
|
-
export function watchAuth(adapter, store, { id = DEFAULT_ID } = {}) {
|
|
299
|
-
return adapter.onSessionChange((user) =>
|
|
300
|
-
store.notify(`#${id}:sessionChange`, user),
|
|
301
|
-
)
|
|
302
|
-
}
|
|
303
|
-
|
|
304
306
|
function signedIn(entity, { user, isNew }, api) {
|
|
305
307
|
entity.user = user
|
|
306
308
|
reset(entity)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps selected handlers of a type so that each call goes through a limiter,
|
|
3
|
+
* one per entity and handler, which notifies `#id:<handler><Suffix>` when the
|
|
4
|
+
* call gets through. The original handler then runs in an update of its own,
|
|
5
|
+
* with a live draft: a call that fires later, on a timer, is outside the update
|
|
6
|
+
* that brought the event, when its draft is revoked. An entity destroyed in the
|
|
7
|
+
* meantime gets nothing.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object.<string, number>} delays - The handlers to wrap, by name, with their delay.
|
|
10
|
+
* @param {string} suffix - What the event the limiter notifies ends with.
|
|
11
|
+
* @param {(fn: Function, delay: number) => Function & { cancel(): void }} createLimiter
|
|
12
|
+
* @returns {(type: object) => object} The behavior.
|
|
13
|
+
*/
|
|
14
|
+
export function withLimiter(delays, suffix, createLimiter) {
|
|
15
|
+
return (type) => {
|
|
16
|
+
const limitersByEntity = new Map()
|
|
17
|
+
const behavior = {}
|
|
18
|
+
|
|
19
|
+
for (const handlerName of Object.keys(delays)) {
|
|
20
|
+
const originalHandler = type[handlerName]
|
|
21
|
+
if (!originalHandler) continue
|
|
22
|
+
|
|
23
|
+
const limitedEvent = `${handlerName}${suffix}`
|
|
24
|
+
behavior[limitedEvent] = originalHandler
|
|
25
|
+
|
|
26
|
+
behavior[handlerName] = (entity, payload, api) => {
|
|
27
|
+
const entityId = entity.id
|
|
28
|
+
|
|
29
|
+
let perEntity = limitersByEntity.get(entityId)
|
|
30
|
+
if (!perEntity) {
|
|
31
|
+
perEntity = {}
|
|
32
|
+
limitersByEntity.set(entityId, perEntity)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
perEntity[handlerName] ??= createLimiter((entityId, payload, api) => {
|
|
36
|
+
api.notify(`#${entityId}:${limitedEvent}`, payload)
|
|
37
|
+
}, delays[handlerName])
|
|
38
|
+
|
|
39
|
+
perEntity[handlerName](entityId, payload, api)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
behavior.destroy = (entity, payload, api) => {
|
|
44
|
+
const perEntity = limitersByEntity.get(entity.id)
|
|
45
|
+
if (perEntity) {
|
|
46
|
+
Object.values(perEntity).forEach((limiter) => limiter.cancel())
|
|
47
|
+
limitersByEntity.delete(entity.id)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type.destroy?.(entity, payload, api)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return behavior
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The delays by handler name, from either form of a decorator's arguments.
|
|
59
|
+
* @param {number|Object.<string, number>} config - One delay for every handler listed, or a map of handler names to delays.
|
|
60
|
+
* @param {string[]} [handlerNames] - The handlers, when `config` is one delay.
|
|
61
|
+
* @returns {Object.<string, number>}
|
|
62
|
+
*/
|
|
63
|
+
export function toDelays(config, handlerNames) {
|
|
64
|
+
return typeof config === "number"
|
|
65
|
+
? Object.fromEntries(handlerNames.map((name) => [name, config]))
|
|
66
|
+
: config
|
|
67
|
+
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { debounce } from "@inglorious/utils/functions"
|
|
2
2
|
|
|
3
|
+
import { toDelays, withLimiter } from "../limit.js"
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
|
-
* Wraps selected event handlers of a type with debouncing, scoped per
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* entity the event brought is a draft revoked by then. So the timer notifies
|
|
8
|
-
* `#id:<handler>Debounced`, which runs the original handler inside an update
|
|
9
|
-
* of its own, with a live draft whose changes reach subscribers. An entity
|
|
10
|
-
* destroyed in the meantime gets nothing.
|
|
6
|
+
* Wraps selected event handlers of a type with debouncing, scoped per entity.
|
|
7
|
+
* When the delay ends, the original handler runs in an update of its own,
|
|
8
|
+
* through the `<handler>Debounced` event.
|
|
11
9
|
*
|
|
12
10
|
* @param {number|Object} config - Either a single delay (ms) applied to all
|
|
13
11
|
* handlers listed in `handlerNames`, or a map of handlerName -> delay (ms).
|
|
@@ -28,54 +26,5 @@ import { debounce } from "@inglorious/utils/functions"
|
|
|
28
26
|
* }
|
|
29
27
|
*/
|
|
30
28
|
export function withDebounce(config, handlerNames) {
|
|
31
|
-
|
|
32
|
-
typeof config === "number"
|
|
33
|
-
? Object.fromEntries(handlerNames.map((name) => [name, config]))
|
|
34
|
-
: config
|
|
35
|
-
|
|
36
|
-
return (type) => {
|
|
37
|
-
const debouncedByEntity = new Map()
|
|
38
|
-
|
|
39
|
-
const behavior = {}
|
|
40
|
-
|
|
41
|
-
for (const handlerName of Object.keys(delays)) {
|
|
42
|
-
const originalHandler = type[handlerName]
|
|
43
|
-
if (!originalHandler) continue
|
|
44
|
-
|
|
45
|
-
const debouncedEvent = `${handlerName}Debounced`
|
|
46
|
-
behavior[debouncedEvent] = originalHandler
|
|
47
|
-
|
|
48
|
-
behavior[handlerName] = (entity, payload, api) => {
|
|
49
|
-
const entityId = entity.id
|
|
50
|
-
|
|
51
|
-
let perEntity = debouncedByEntity.get(entityId)
|
|
52
|
-
if (!perEntity) {
|
|
53
|
-
perEntity = {}
|
|
54
|
-
debouncedByEntity.set(entityId, perEntity)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!perEntity[handlerName]) {
|
|
58
|
-
perEntity[handlerName] = debounce((entityId, payload, api) => {
|
|
59
|
-
api.notify(`#${entityId}:${debouncedEvent}`, payload)
|
|
60
|
-
}, delays[handlerName])
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
perEntity[handlerName](entityId, payload, api)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
behavior.destroy = (entity, payload, api) => {
|
|
68
|
-
const entityId = entity.id
|
|
69
|
-
|
|
70
|
-
const perEntity = debouncedByEntity.get(entityId)
|
|
71
|
-
if (perEntity) {
|
|
72
|
-
Object.values(perEntity).forEach((d) => d.cancel())
|
|
73
|
-
debouncedByEntity.delete(entityId)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
type.destroy?.(entity, payload, api)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return behavior
|
|
80
|
-
}
|
|
29
|
+
return withLimiter(toDelays(config, handlerNames), "Debounced", debounce)
|
|
81
30
|
}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { throttle } from "@inglorious/utils/functions"
|
|
2
2
|
|
|
3
|
+
import { toDelays, withLimiter } from "../limit.js"
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
|
-
* Wraps selected event handlers of a type with throttling, scoped per
|
|
6
|
+
* Wraps selected event handlers of a type with throttling, scoped per entity.
|
|
5
7
|
*
|
|
6
8
|
* By default this is leading-edge only: the first call in a burst runs
|
|
7
|
-
* immediately, and further calls are dropped until `delay` has elapsed
|
|
9
|
+
* immediately, and further calls are dropped until `delay` has elapsed, so the
|
|
8
10
|
* last call of a burst may be lost. Pass `{ hasTrailing: true }` to also invoke
|
|
9
|
-
* the handler once more after `delay`,
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* Every call that gets through notifies `#id:<handler>Throttled`, which runs
|
|
13
|
-
* the original handler inside an update with a live draft: a trailing call
|
|
14
|
-
* fires on its own timer, outside any update, after the event's draft is
|
|
15
|
-
* revoked. An entity destroyed in the meantime gets nothing.
|
|
11
|
+
* the handler once more after `delay`, with the most recent suppressed call's
|
|
12
|
+
* arguments. Every call that gets through runs the original handler in an
|
|
13
|
+
* update of its own, through the `<handler>Throttled` event.
|
|
16
14
|
*
|
|
17
15
|
* @param {number|Object} config - Either a single delay (ms) applied to all
|
|
18
16
|
* handlers listed in `handlerNames`, or a map of handlerName -> delay (ms).
|
|
@@ -21,12 +19,10 @@ import { throttle } from "@inglorious/utils/functions"
|
|
|
21
19
|
* are used instead).
|
|
22
20
|
* @param {Object} [options]
|
|
23
21
|
* @param {boolean} [options.hasTrailing=false] - Whether throttled handlers
|
|
24
|
-
*
|
|
25
|
-
* call's arguments. Applies to every handler wrapped by this call.
|
|
22
|
+
* also fire once more after `delay`, with the last suppressed call's arguments.
|
|
26
23
|
*
|
|
27
24
|
* @example
|
|
28
|
-
* // leading-edge only (default)
|
|
29
|
-
* // a burst may be dropped
|
|
25
|
+
* // leading-edge only (default)
|
|
30
26
|
* const types = {
|
|
31
27
|
* ScrollTracker: [Base, withThrottle(200, ["scroll"])],
|
|
32
28
|
* }
|
|
@@ -42,58 +38,7 @@ export function withThrottle(
|
|
|
42
38
|
handlerNames,
|
|
43
39
|
{ hasTrailing = false } = {},
|
|
44
40
|
) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
: config
|
|
49
|
-
|
|
50
|
-
return (type) => {
|
|
51
|
-
const throttledByEntity = new Map()
|
|
52
|
-
|
|
53
|
-
const behavior = {}
|
|
54
|
-
|
|
55
|
-
for (const handlerName of Object.keys(delays)) {
|
|
56
|
-
const originalHandler = type[handlerName]
|
|
57
|
-
if (!originalHandler) continue
|
|
58
|
-
|
|
59
|
-
const throttledEvent = `${handlerName}Throttled`
|
|
60
|
-
behavior[throttledEvent] = originalHandler
|
|
61
|
-
|
|
62
|
-
behavior[handlerName] = (entity, payload, api) => {
|
|
63
|
-
const entityId = entity.id
|
|
64
|
-
|
|
65
|
-
let perEntity = throttledByEntity.get(entityId)
|
|
66
|
-
if (!perEntity) {
|
|
67
|
-
perEntity = {}
|
|
68
|
-
throttledByEntity.set(entityId, perEntity)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!perEntity[handlerName]) {
|
|
72
|
-
perEntity[handlerName] = throttle(
|
|
73
|
-
(entityId, payload, api) => {
|
|
74
|
-
api.notify(`#${entityId}:${throttledEvent}`, payload)
|
|
75
|
-
},
|
|
76
|
-
delays[handlerName],
|
|
77
|
-
{ hasTrailing },
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
perEntity[handlerName](entityId, payload, api)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
behavior.destroy = (entity, payload, api) => {
|
|
86
|
-
const entityId = entity.id
|
|
87
|
-
|
|
88
|
-
const perEntity = throttledByEntity.get(entityId)
|
|
89
|
-
if (perEntity) {
|
|
90
|
-
Object.values(perEntity).forEach((t) => t.cancel())
|
|
91
|
-
throttledByEntity.delete(entityId)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
type.destroy?.(entity, payload, api)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return behavior
|
|
98
|
-
}
|
|
41
|
+
return withLimiter(toDelays(config, handlerNames), "Throttled", (fn, delay) =>
|
|
42
|
+
throttle(fn, delay, { hasTrailing }),
|
|
43
|
+
)
|
|
99
44
|
}
|
|
@@ -62,14 +62,15 @@ export function createGeolocation({ plugin } = {}) {
|
|
|
62
62
|
entity.error = null
|
|
63
63
|
entity.isLoading = false
|
|
64
64
|
entity.position = {
|
|
65
|
+
// A plugin may leave the optional readings out: the entity says null.
|
|
65
66
|
coords: {
|
|
66
67
|
accuracy: position.coords.accuracy,
|
|
67
|
-
altitude: position.coords.altitude,
|
|
68
|
-
altitudeAccuracy: position.coords.altitudeAccuracy,
|
|
69
|
-
heading: position.coords.heading,
|
|
68
|
+
altitude: position.coords.altitude ?? null,
|
|
69
|
+
altitudeAccuracy: position.coords.altitudeAccuracy ?? null,
|
|
70
|
+
heading: position.coords.heading ?? null,
|
|
70
71
|
latitude: position.coords.latitude,
|
|
71
72
|
longitude: position.coords.longitude,
|
|
72
|
-
speed: position.coords.speed,
|
|
73
|
+
speed: position.coords.speed ?? null,
|
|
73
74
|
},
|
|
74
75
|
timestamp: position.timestamp,
|
|
75
76
|
}
|
|
@@ -17,28 +17,68 @@ const TEXT_INPUTS = new Set([
|
|
|
17
17
|
"week",
|
|
18
18
|
])
|
|
19
19
|
|
|
20
|
+
const watches = new Map()
|
|
21
|
+
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* `--iw-keyboard-height` on `<html>`
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* navigation hides.
|
|
31
|
-
*
|
|
32
|
-
* @returns {() => void} Stops following.
|
|
23
|
+
* The type that knows when the on-screen keyboard is up, and how much of the
|
|
24
|
+
* page it covers. Most phones don't shrink the page for the keyboard (iOS, and
|
|
25
|
+
* Chrome on Android): a bottom sheet or a snackbar ends up behind it. The entity
|
|
26
|
+
* holds `isOpen` and `height`, read from the visual viewport, and the type sets
|
|
27
|
+
* `--iw-keyboard-height` on `<html>` and the `iw-keyboard-open` class while the
|
|
28
|
+
* keyboard is up, which Inglorious UI's sheets, snackbars, toasts and bottom
|
|
29
|
+
* navigation follow. Where the page does shrink, as in an Android app, `isOpen`
|
|
30
|
+
* is still set but `height` stays 0, since nothing is covered. No plugin is
|
|
31
|
+
* needed, in a browser or a native app.
|
|
33
32
|
*
|
|
34
33
|
* @example
|
|
35
|
-
*
|
|
34
|
+
* const types = { Keyboard }
|
|
35
|
+
* const entities = { keyboard: { type: "Keyboard" } }
|
|
36
36
|
*/
|
|
37
|
-
export
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
export const Keyboard = {
|
|
38
|
+
create(entity, _, api) {
|
|
39
|
+
entity.isSupported = Boolean(globalThis.visualViewport)
|
|
40
|
+
entity.isOpen ??= false
|
|
41
|
+
entity.height ??= NONE
|
|
42
|
+
if (!entity.isSupported) return
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
const { id } = entity
|
|
45
|
+
watches.get(id)?.()
|
|
46
|
+
watches.set(
|
|
47
|
+
id,
|
|
48
|
+
watch((change) => api.notify(`#${id}:keyboardChange`, change)),
|
|
49
|
+
)
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
destroy(entity) {
|
|
53
|
+
watches.get(entity.id)?.()
|
|
54
|
+
watches.delete(entity.id)
|
|
55
|
+
const root = globalThis.document?.documentElement
|
|
56
|
+
root?.style.removeProperty(HEIGHT)
|
|
57
|
+
root?.classList.remove(OPEN)
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
keyboardChange(entity, { isOpen, height }) {
|
|
61
|
+
entity.isOpen = isOpen
|
|
62
|
+
entity.height = height
|
|
63
|
+
apply({ isOpen, height })
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function apply({ isOpen, height }) {
|
|
68
|
+
const root = globalThis.document?.documentElement
|
|
69
|
+
if (!root) return
|
|
70
|
+
root.style.setProperty(HEIGHT, `${height}px`)
|
|
71
|
+
root.classList.toggle(OPEN, isOpen)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Follows the visual viewport, calling `onChange` when the keyboard opens,
|
|
76
|
+
* closes or covers a different height.
|
|
77
|
+
* @param {(change: { isOpen: boolean, height: number }) => void} onChange
|
|
78
|
+
* @returns {() => void} Stops following.
|
|
79
|
+
*/
|
|
80
|
+
function watch(onChange) {
|
|
81
|
+
const viewport = window.visualViewport
|
|
42
82
|
let isOpen = false
|
|
43
83
|
let height = NONE
|
|
44
84
|
let fullWidth = window.innerWidth
|
|
@@ -64,8 +104,7 @@ export function watchKeyboard() {
|
|
|
64
104
|
const isOpening = isKeyboard && !isOpen
|
|
65
105
|
isOpen = isKeyboard
|
|
66
106
|
height = nextHeight
|
|
67
|
-
|
|
68
|
-
root.classList.toggle(OPEN, isOpen)
|
|
107
|
+
onChange({ isOpen, height })
|
|
69
108
|
if (isOpening) {
|
|
70
109
|
requestAnimationFrame(() =>
|
|
71
110
|
document.activeElement?.scrollIntoView?.({ block: "nearest" }),
|
|
@@ -82,8 +121,6 @@ export function watchKeyboard() {
|
|
|
82
121
|
viewport.removeEventListener("resize", update)
|
|
83
122
|
viewport.removeEventListener("scroll", update)
|
|
84
123
|
window.removeEventListener("resize", update)
|
|
85
|
-
root.style.removeProperty(HEIGHT)
|
|
86
|
-
root.classList.remove(OPEN)
|
|
87
124
|
}
|
|
88
125
|
}
|
|
89
126
|
|
package/src/mount.js
CHANGED
|
@@ -10,22 +10,25 @@ import { createAccessibleNavigation } from "./router/accessible-navigation.js"
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Mounts a lit-html template to the DOM and subscribes to a store for re-rendering.
|
|
13
|
+
* Markup already in the element, as a server-rendered page has, is hydrated rather
|
|
14
|
+
* than replaced. In an app with the `Router` type, each navigation is also announced
|
|
15
|
+
* to screen readers and moves focus back to the top of the new page, as a full page
|
|
16
|
+
* load would. Once mounted, it notifies `init`.
|
|
17
|
+
*
|
|
13
18
|
* @param {Store} store - The application state store.
|
|
14
19
|
* @param {(api: Api) => TemplateResult | null} renderFn - The root render function.
|
|
15
20
|
* @param {HTMLElement | DocumentFragment} element - The DOM element to mount the template to.
|
|
16
|
-
* In an app with the `Router` type, each navigation is also announced to screen readers
|
|
17
|
-
* and moves focus back to the top of the new page, as a full page load would.
|
|
18
|
-
*
|
|
19
21
|
* @param {Object} [options] - Root error-boundary and navigation options.
|
|
20
22
|
* @param {(error: unknown, api: Api) => void} [options.onError] - Called when the
|
|
21
23
|
* root render throws. Defaults to `console.error`.
|
|
22
24
|
* @param {(error: unknown, api: Api) => TemplateResult | null} [options.fallback] -
|
|
23
25
|
* Optional fallback template rendered in place of the failed root render.
|
|
26
|
+
* @param {boolean} [options.hydrate=true] - Whether existing markup is hydrated.
|
|
24
27
|
* @param {boolean} [options.hasAccessibleNavigation=true] - Whether navigations are
|
|
25
28
|
* announced and move focus. Only apps with the `Router` type navigate.
|
|
26
29
|
* @param {() => string | null} [options.getPageName] - The name to announce for the
|
|
27
30
|
* current page, or `null` while it is not known yet; by default the document title.
|
|
28
|
-
* @returns {() => void} An unsubscribe function
|
|
31
|
+
* @returns {Promise<() => void>} An unsubscribe function, once mounted.
|
|
29
32
|
*/
|
|
30
33
|
export async function mount(store, renderFn, element, options = {}) {
|
|
31
34
|
const {
|
|
@@ -1,46 +1,60 @@
|
|
|
1
1
|
import { isNative } from "../index.js"
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const DEFAULT_ROUTER = "router"
|
|
4
4
|
const URL_OPEN_EVENT = "appUrlOpen"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* app's domain, tapped while the app is installed, opens the app
|
|
9
|
-
* the browser, and the router goes to the link's path.
|
|
10
|
-
* starts the app as well as one that arrives while it runs.
|
|
11
|
-
* outside a native app.
|
|
7
|
+
* The type that opens Universal Links and App Links in the app's router: a
|
|
8
|
+
* link to the app's domain, tapped while the app is installed, opens the app
|
|
9
|
+
* instead of the browser, and the router goes to the link's path. It covers a
|
|
10
|
+
* link that starts the app as well as one that arrives while it runs. The
|
|
11
|
+
* entity's `url` is the last link opened. Does nothing outside a native app.
|
|
12
12
|
*
|
|
13
|
-
* @param {Object}
|
|
14
|
-
* @param {
|
|
15
|
-
* @param {
|
|
16
|
-
* @returns {() => Promise<void>} Stops listening.
|
|
13
|
+
* @param {Object} config
|
|
14
|
+
* @param {Object} config.plugin `App` from `@capacitor/app`.
|
|
15
|
+
* @param {string} [config.router] The router entity, `router` by default.
|
|
17
16
|
*
|
|
18
17
|
* @example
|
|
19
|
-
*
|
|
18
|
+
* import { App } from "@capacitor/app"
|
|
19
|
+
* const types = { AppLinks: createAppLinks({ plugin: App }) }
|
|
20
|
+
* const entities = { appLinks: { type: "AppLinks" } }
|
|
20
21
|
*/
|
|
21
|
-
export function
|
|
22
|
-
|
|
22
|
+
export function createAppLinks({ plugin, router = DEFAULT_ROUTER }) {
|
|
23
|
+
const listeners = new Map()
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
return {
|
|
26
|
+
create(entity, _, api) {
|
|
27
|
+
entity.url ??= null
|
|
28
|
+
if (!isNative()) return
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
await handle.remove()
|
|
30
|
-
}
|
|
30
|
+
const { id } = entity
|
|
31
|
+
const open = (url) => api.notify(`#${id}:appLinkOpen`, url)
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
plugin.getLaunchUrl().then(
|
|
34
|
+
(launch) => {
|
|
35
|
+
if (launch?.url) open(launch.url)
|
|
36
|
+
},
|
|
37
|
+
() => {
|
|
38
|
+
// No launch URL to read is the same as none.
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
listeners.set(
|
|
42
|
+
id,
|
|
43
|
+
plugin.addListener(URL_OPEN_EVENT, ({ url }) => open(url)),
|
|
44
|
+
)
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
destroy(entity) {
|
|
48
|
+
const listener = listeners.get(entity.id)
|
|
49
|
+
listeners.delete(entity.id)
|
|
50
|
+
listener?.then((handle) => handle.remove())
|
|
51
|
+
},
|
|
40
52
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
appLinkOpen(entity, url, api) {
|
|
54
|
+
entity.url = url
|
|
55
|
+
const path = toPath(url)
|
|
56
|
+
if (path) api.notify(`#${router}:navigate`, path)
|
|
57
|
+
},
|
|
44
58
|
}
|
|
45
59
|
}
|
|
46
60
|
|