foldkit 0.8.0-canary.0 → 0.9.0-canary.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"addNavigationEventListeners.d.ts","sourceRoot":"","sources":["../../src/runtime/addNavigationEventListeners.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,EAAgB,MAAM,QAAQ,CAAA;AAI3D,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAGzC,eAAO,MAAM,2BAA2B,GAAI,OAAO,EACjD,cAAc,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,EAClC,eAAe,aAAa,CAAC,OAAO,CAAC,SAKtC,CAAA"}
1
+ {"version":3,"file":"addNavigationEventListeners.d.ts","sourceRoot":"","sources":["../../src/runtime/addNavigationEventListeners.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,EAAU,MAAM,QAAQ,CAAA;AAI9C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAGzC,eAAO,MAAM,2BAA2B,GAAI,OAAO,EACjD,cAAc,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,EAClC,eAAe,aAAa,CAAC,OAAO,CAAC,SAKtC,CAAA"}
@@ -1,4 +1,4 @@
1
- import { Array, Option, Queue, String, pipe } from 'effect';
1
+ import { Option, Queue, String } from 'effect';
2
2
  import { OptionExt, StringExt } from '../effectExtensions';
3
3
  import { External, Internal } from './urlRequest';
4
4
  export const addNavigationEventListeners = (messageQueue, browserConfig) => {
@@ -8,73 +8,51 @@ export const addNavigationEventListeners = (messageQueue, browserConfig) => {
8
8
  };
9
9
  const addPopStateListener = (messageQueue, browserConfig) => {
10
10
  const onPopState = () => {
11
- const search = StringExt.stripPrefixNonEmpty('?')(window.location.search);
12
- const hash = StringExt.stripPrefixNonEmpty('#')(window.location.hash);
13
- const newUrl = {
14
- protocol: window.location.protocol,
15
- host: window.location.host,
16
- port: OptionExt.fromString(window.location.port),
17
- pathname: window.location.pathname,
18
- search,
19
- hash,
20
- };
21
- Queue.unsafeOffer(messageQueue, browserConfig.onUrlChange(newUrl));
11
+ Queue.unsafeOffer(messageQueue, browserConfig.onUrlChange(locationToUrl()));
22
12
  };
23
13
  window.addEventListener('popstate', onPopState);
24
14
  };
25
15
  const addLinkClickListener = (messageQueue, browserConfig) => {
26
16
  const onLinkClick = (event) => {
27
17
  const target = event.target;
28
- if (!target || !('closest' in target))
18
+ if (!target || !('closest' in target)) {
29
19
  return;
20
+ }
30
21
  /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions */
31
- const link = target.closest('a');
32
- if (!link)
22
+ const maybeLink = Option.fromNullable(target.closest('a'));
23
+ if (Option.isNone(maybeLink)) {
33
24
  return;
34
- const href = link.getAttribute('href');
35
- if (!href)
25
+ }
26
+ const { href } = maybeLink.value;
27
+ if (String.isEmpty(href)) {
36
28
  return;
37
- if (href.startsWith('http://') || href.startsWith('https://') || href.startsWith('//')) {
29
+ }
30
+ event.preventDefault();
31
+ const linkUrl = new URL(href);
32
+ const currentUrl = new URL(window.location.href);
33
+ if (linkUrl.origin !== currentUrl.origin) {
38
34
  Queue.unsafeOffer(messageQueue, browserConfig.onUrlRequest(External.make({ href })));
39
35
  return;
40
36
  }
41
- const url = pipe(href, String.split('?'), (parts) => {
42
- event.preventDefault();
43
- const path = Array.headNonEmpty(parts);
44
- const searchAndHash = Array.tailNonEmpty(parts);
45
- const pathname = pipe(path, String.split('#'), Array.headNonEmpty) || '/';
46
- const searchPart = Array.head(searchAndHash).pipe(Option.match({
47
- onNone: () => '',
48
- onSome: (s) => s,
49
- }));
50
- const search = pipe(searchPart, String.split('#'), Array.headNonEmpty, OptionExt.fromString);
51
- const hash = pipe(href, String.split('#'), (hashParts) => Array.isNonEmptyArray(hashParts) && hashParts.length > 1 ? hashParts[1] : '', OptionExt.fromString);
52
- return {
53
- protocol: window.location.protocol,
54
- host: window.location.host,
55
- port: OptionExt.fromString(window.location.port),
56
- pathname,
57
- search,
58
- hash,
59
- };
60
- });
61
- Queue.unsafeOffer(messageQueue, browserConfig.onUrlRequest(Internal.make({ url })));
37
+ Queue.unsafeOffer(messageQueue, browserConfig.onUrlRequest(Internal.make({ url: urlToFoldkitUrl(linkUrl) })));
62
38
  };
63
39
  document.addEventListener('click', onLinkClick);
64
40
  };
65
41
  const addProgrammaticNavigationListener = (messageQueue, browserConfig) => {
66
42
  const onProgrammaticNavigation = () => {
67
- const search = StringExt.stripPrefixNonEmpty('?')(window.location.search);
68
- const hash = StringExt.stripPrefixNonEmpty('#')(window.location.hash);
69
- const newUrl = {
70
- protocol: window.location.protocol,
71
- host: window.location.host,
72
- port: OptionExt.fromString(window.location.port),
73
- pathname: window.location.pathname,
74
- search,
75
- hash,
76
- };
77
- Queue.unsafeOffer(messageQueue, browserConfig.onUrlChange(newUrl));
43
+ Queue.unsafeOffer(messageQueue, browserConfig.onUrlChange(locationToUrl()));
78
44
  };
79
45
  window.addEventListener('foldkit:urlchange', onProgrammaticNavigation);
80
46
  };
47
+ const urlToFoldkitUrl = (url) => {
48
+ const { protocol, hostname, port, pathname, search, hash } = url;
49
+ return {
50
+ protocol,
51
+ host: hostname,
52
+ port: OptionExt.fromString(port),
53
+ pathname,
54
+ search: StringExt.stripPrefixNonEmpty('?')(search),
55
+ hash: StringExt.stripPrefixNonEmpty('#')(hash),
56
+ };
57
+ };
58
+ const locationToUrl = () => urlToFoldkitUrl(new URL(window.location.href));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldkit",
3
- "version": "0.8.0-canary.0",
3
+ "version": "0.9.0-canary.1",
4
4
  "description": "Elm-inspired UI framework powered by Effect",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",