@jsenv/navi 0.0.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.
Files changed (123) hide show
  1. package/index.js +51 -0
  2. package/package.json +38 -0
  3. package/src/action_private_properties.js +11 -0
  4. package/src/action_proxy_test.html +353 -0
  5. package/src/action_run_states.js +5 -0
  6. package/src/actions.js +1377 -0
  7. package/src/browser_integration/browser_integration.js +191 -0
  8. package/src/browser_integration/document_back_and_forward.js +17 -0
  9. package/src/browser_integration/document_loading_signal.js +100 -0
  10. package/src/browser_integration/document_state_signal.js +9 -0
  11. package/src/browser_integration/document_url_signal.js +9 -0
  12. package/src/browser_integration/use_is_visited.js +19 -0
  13. package/src/browser_integration/via_history.js +199 -0
  14. package/src/browser_integration/via_navigation.js +168 -0
  15. package/src/components/action_execution/form_context.js +8 -0
  16. package/src/components/action_execution/render_actionable_component.jsx +27 -0
  17. package/src/components/action_execution/use_action.js +330 -0
  18. package/src/components/action_execution/use_execute_action.js +161 -0
  19. package/src/components/action_renderer.jsx +136 -0
  20. package/src/components/collect_form_element_values.js +79 -0
  21. package/src/components/demos/0_button_demo.html +155 -0
  22. package/src/components/demos/1_checkbox_demo.html +257 -0
  23. package/src/components/demos/2_input_textual_demo.html +354 -0
  24. package/src/components/demos/3_radio_demo.html +222 -0
  25. package/src/components/demos/4_select_demo.html +104 -0
  26. package/src/components/demos/5_list_scrollable_demo.html +153 -0
  27. package/src/components/demos/action/0_button_demo.html +204 -0
  28. package/src/components/demos/action/10_shortcuts_demo.html +189 -0
  29. package/src/components/demos/action/11_nested_shortcuts_demo.html +401 -0
  30. package/src/components/demos/action/1_input_text_demo.html +461 -0
  31. package/src/components/demos/action/2_form_multiple.html +303 -0
  32. package/src/components/demos/action/3_details_demo.html +172 -0
  33. package/src/components/demos/action/4_input_checkbox_demo.html +611 -0
  34. package/src/components/demos/action/6_checkbox_list_demo.html +109 -0
  35. package/src/components/demos/action/7_radio_list_demo.html +217 -0
  36. package/src/components/demos/action/8_editable_text_demo.html +442 -0
  37. package/src/components/demos/action/9_link_demo.html +172 -0
  38. package/src/components/demos/demo.md +0 -0
  39. package/src/components/demos/route/basic/basic.html +14 -0
  40. package/src/components/demos/route/basic/basic_route_demo.jsx +224 -0
  41. package/src/components/demos/route/multi/multi.html +14 -0
  42. package/src/components/demos/route/multi/multi_route_demo.jsx +277 -0
  43. package/src/components/details/details.jsx +248 -0
  44. package/src/components/details/summary_marker.jsx +141 -0
  45. package/src/components/editable_text/editable_text.jsx +96 -0
  46. package/src/components/error_boundary_context.js +9 -0
  47. package/src/components/form.jsx +144 -0
  48. package/src/components/input/button.jsx +333 -0
  49. package/src/components/input/checkbox_list.jsx +294 -0
  50. package/src/components/input/field.jsx +61 -0
  51. package/src/components/input/field_css.js +118 -0
  52. package/src/components/input/input.jsx +15 -0
  53. package/src/components/input/input_checkbox.jsx +370 -0
  54. package/src/components/input/input_radio.jsx +299 -0
  55. package/src/components/input/input_textual.jsx +338 -0
  56. package/src/components/input/radio_list.jsx +283 -0
  57. package/src/components/input/select.jsx +273 -0
  58. package/src/components/input/use_form_event.js +20 -0
  59. package/src/components/input/use_on_change.js +12 -0
  60. package/src/components/link/link.jsx +291 -0
  61. package/src/components/loader/loader_background.jsx +324 -0
  62. package/src/components/loader/loading_spinner.jsx +68 -0
  63. package/src/components/loader/network_speed.js +83 -0
  64. package/src/components/loader/rectangle_loading.jsx +225 -0
  65. package/src/components/route.jsx +15 -0
  66. package/src/components/selection/selection.js +5 -0
  67. package/src/components/selection/selection_context.jsx +262 -0
  68. package/src/components/shortcut/os.js +9 -0
  69. package/src/components/shortcut/shortcut_context.jsx +390 -0
  70. package/src/components/use_action_events.js +37 -0
  71. package/src/components/use_auto_focus.js +43 -0
  72. package/src/components/use_debounce_true.js +31 -0
  73. package/src/components/use_focus_group.js +19 -0
  74. package/src/components/use_initial_value.js +104 -0
  75. package/src/components/use_is_visited.js +19 -0
  76. package/src/components/use_ref_array.js +38 -0
  77. package/src/components/use_signal_sync.js +50 -0
  78. package/src/components/use_state_array.js +40 -0
  79. package/src/docs/actions.md +228 -0
  80. package/src/docs/demos/resource/action_status.jsx +42 -0
  81. package/src/docs/demos/resource/demo.md +1 -0
  82. package/src/docs/demos/resource/resource_demo_0.html +84 -0
  83. package/src/docs/demos/resource/resource_demo_10_post_gc.html +364 -0
  84. package/src/docs/demos/resource/resource_demo_11_describe_many.html +362 -0
  85. package/src/docs/demos/resource/resource_demo_2.html +173 -0
  86. package/src/docs/demos/resource/resource_demo_3_filtered_users.html +415 -0
  87. package/src/docs/demos/resource/resource_demo_4_details.html +284 -0
  88. package/src/docs/demos/resource/resource_demo_5_renderer_lazy.html +115 -0
  89. package/src/docs/demos/resource/resource_demo_6_gc.html +217 -0
  90. package/src/docs/demos/resource/resource_demo_7_child_gc.html +240 -0
  91. package/src/docs/demos/resource/resource_demo_8_proxy_gc.html +319 -0
  92. package/src/docs/demos/resource/resource_demo_9_describe_one.html +472 -0
  93. package/src/docs/demos/resource/tata.jsx +3 -0
  94. package/src/docs/demos/resource/toto.jsx +3 -0
  95. package/src/docs/demos/user_nav/user_nav.html +12 -0
  96. package/src/docs/demos/user_nav/user_nav.jsx +330 -0
  97. package/src/docs/resource_dependencies.md +103 -0
  98. package/src/docs/resource_with_params.md +80 -0
  99. package/src/notes.md +13 -0
  100. package/src/route/route.js +518 -0
  101. package/src/route/route.test.html +228 -0
  102. package/src/store/array_signal_store.js +537 -0
  103. package/src/store/local_storage_signal.js +17 -0
  104. package/src/store/resource_graph.js +1303 -0
  105. package/src/store/tests/resource_graph_autoreload_demo.html +12 -0
  106. package/src/store/tests/resource_graph_autoreload_demo.jsx +964 -0
  107. package/src/store/tests/resource_graph_dependencies.test.js +95 -0
  108. package/src/store/value_in_local_storage.js +187 -0
  109. package/src/symbol_object_signal.js +1 -0
  110. package/src/use_action_data.js +10 -0
  111. package/src/use_action_status.js +47 -0
  112. package/src/utils/add_many_event_listeners.js +15 -0
  113. package/src/utils/array_add_remove.js +61 -0
  114. package/src/utils/array_signal.js +15 -0
  115. package/src/utils/compare_two_js_values.js +172 -0
  116. package/src/utils/execute_with_cleanup.js +21 -0
  117. package/src/utils/get_caller_info.js +85 -0
  118. package/src/utils/iterable_weak_set.js +62 -0
  119. package/src/utils/js_value_weak_map.js +162 -0
  120. package/src/utils/js_value_weak_map_demo.html +690 -0
  121. package/src/utils/merge_two_js_values.js +53 -0
  122. package/src/utils/stringify_for_display.js +150 -0
  123. package/src/utils/weak_effect.js +48 -0
@@ -0,0 +1,191 @@
1
+ import { useEffect, useRef } from "preact/hooks";
2
+ import { updateActions } from "../actions.js";
3
+ import {
4
+ setBrowserIntegration,
5
+ setOnRouteDefined,
6
+ updateRoutes,
7
+ } from "../route/route.js";
8
+ import {
9
+ documentIsBusySignal,
10
+ routingWhile,
11
+ windowIsLoadingSignal,
12
+ workingWhile,
13
+ } from "./document_loading_signal.js";
14
+ import { setupBrowserIntegrationViaHistory } from "./via_history.js";
15
+
16
+ const applyActions = (params) => {
17
+ const updateActionsResult = updateActions(params);
18
+ const { allResult, runningActionSet } = updateActionsResult;
19
+ const pendingTaskNameArray = [];
20
+ for (const runningAction of runningActionSet) {
21
+ pendingTaskNameArray.push(runningAction.name);
22
+ }
23
+ workingWhile(() => allResult, pendingTaskNameArray);
24
+ return updateActionsResult;
25
+ };
26
+
27
+ const applyRouting = (
28
+ url,
29
+ { globalAbortSignal, abortSignal, state, replace, isVisited },
30
+ ) => {
31
+ const {
32
+ loadSet,
33
+ reloadSet,
34
+ abortSignalMap,
35
+ routeLoadRequestedMap,
36
+ activeRouteSet,
37
+ } = updateRoutes(url, { state, replace, isVisited });
38
+ if (loadSet.size === 0 && reloadSet.size === 0) {
39
+ return {
40
+ allResult: undefined,
41
+ requestedResult: undefined,
42
+ activeRouteSet: new Set(),
43
+ };
44
+ }
45
+ const updateActionsResult = updateActions({
46
+ globalAbortSignal,
47
+ abortSignal,
48
+ runSet: loadSet,
49
+ rerunSet: reloadSet,
50
+ abortSignalMap,
51
+ reason: `Document navigating to ${url}`,
52
+ });
53
+ const { allResult, runningActionSet } = updateActionsResult;
54
+ const pendingTaskNameArray = [];
55
+ for (const [route, routeAction] of routeLoadRequestedMap) {
56
+ if (runningActionSet.has(routeAction)) {
57
+ pendingTaskNameArray.push(`${route.relativeUrl} -> ${routeAction.name}`);
58
+ }
59
+ }
60
+ routingWhile(() => allResult, pendingTaskNameArray);
61
+ return { ...updateActionsResult, activeRouteSet };
62
+ };
63
+
64
+ const browserIntegration = setupBrowserIntegrationViaHistory({
65
+ applyActions,
66
+ applyRouting,
67
+ });
68
+
69
+ setOnRouteDefined(() => {
70
+ browserIntegration.init();
71
+ });
72
+ setBrowserIntegration(browserIntegration);
73
+
74
+ export const actionIntegratedVia = browserIntegration.integration;
75
+ export const goTo = browserIntegration.goTo;
76
+ export const stopLoad = (reason = "stopLoad() called") => {
77
+ const windowIsLoading = windowIsLoadingSignal.value;
78
+ if (windowIsLoading) {
79
+ window.stop();
80
+ }
81
+ const documentIsBusy = documentIsBusySignal.value;
82
+ if (documentIsBusy) {
83
+ browserIntegration.stop(reason);
84
+ }
85
+ };
86
+ export const reload = browserIntegration.reload;
87
+ export const goBack = browserIntegration.goBack;
88
+ export const goForward = browserIntegration.goForward;
89
+ export const isVisited = browserIntegration.isVisited;
90
+ export const visitedUrlsSignal = browserIntegration.visitedUrlsSignal;
91
+ export const handleActionTask = browserIntegration.handleActionTask;
92
+
93
+ const idUsageMap = new Map();
94
+ const useNavStateWithWarnings = (id, initialValue, options) => {
95
+ const idRef = useRef(undefined);
96
+ if (idRef.current !== id) {
97
+ const oldId = idRef.current;
98
+ idUsageMap.delete(oldId);
99
+ idRef.current = id;
100
+
101
+ const usage = idUsageMap.get(id);
102
+ if (!usage) {
103
+ idUsageMap.set(id, {
104
+ stackTrace: new Error().stack,
105
+ });
106
+ } else {
107
+ console.warn(
108
+ `useNavState ID conflict detected!
109
+ ID "${id}" is already in use by another component.
110
+ This can cause UI state conflicts and unexpected behavior.
111
+ Consider using unique IDs for each component instance.`,
112
+ );
113
+ }
114
+ }
115
+
116
+ useEffect(() => {
117
+ return () => {
118
+ idUsageMap.delete(id);
119
+ };
120
+ }, [id]);
121
+
122
+ return useNavStateBasic(id, initialValue, options);
123
+ };
124
+
125
+ const NOT_SET = {};
126
+ const NO_OP = () => {};
127
+ const NO_ID_GIVEN = [undefined, NO_OP, NO_OP];
128
+ const useNavStateBasic = (id, initialValue, { debug } = {}) => {
129
+ const navStateRef = useRef(NOT_SET);
130
+ if (!id) {
131
+ return NO_ID_GIVEN;
132
+ }
133
+
134
+ if (navStateRef.current === NOT_SET) {
135
+ const documentState = browserIntegration.getDocumentState();
136
+ const valueInDocumentState = documentState ? documentState[id] : undefined;
137
+ if (valueInDocumentState === undefined) {
138
+ navStateRef.current = initialValue;
139
+ if (initialValue !== undefined) {
140
+ console.debug(
141
+ `useNavState(${id}) initial value is ${initialValue} (from initialValue passed in as argument)`,
142
+ );
143
+ }
144
+ } else {
145
+ navStateRef.current = valueInDocumentState;
146
+ if (debug) {
147
+ console.debug(
148
+ `useNavState(${id}) initial value is ${initialValue} (from nav state)`,
149
+ );
150
+ }
151
+ }
152
+ }
153
+
154
+ const set = (value) => {
155
+ const currentValue = navStateRef.current;
156
+ if (typeof value === "function") {
157
+ value = value(currentValue);
158
+ }
159
+ if (debug) {
160
+ console.debug(
161
+ `useNavState(${id}) set ${value} (previous was ${currentValue})`,
162
+ );
163
+ }
164
+
165
+ const currentState = browserIntegration.getDocumentState() || {};
166
+ if (value === undefined) {
167
+ delete currentState[id];
168
+ } else {
169
+ currentState[id] = value;
170
+ }
171
+ browserIntegration.replaceDocumentState(currentState);
172
+ };
173
+
174
+ return [
175
+ navStateRef.current,
176
+ set,
177
+ () => {
178
+ set(undefined);
179
+ },
180
+ ];
181
+ };
182
+
183
+ export const useNavState = import.meta.dev
184
+ ? useNavStateWithWarnings
185
+ : useNavStateBasic;
186
+
187
+ if (import.meta.hot) {
188
+ import.meta.hot.dispose(() => {
189
+ idUsageMap.clear();
190
+ });
191
+ }
@@ -0,0 +1,17 @@
1
+ import { signal } from "@preact/signals";
2
+
3
+ export const canGoBackSignal = signal(false);
4
+ export const updateCanGoBack = (can) => {
5
+ canGoBackSignal.value = can;
6
+ };
7
+ export const useCanGoBack = () => {
8
+ return canGoBackSignal.value;
9
+ };
10
+
11
+ export const canGoForwardSignal = signal(false);
12
+ export const updateCanGoForward = (can) => {
13
+ canGoBackSignal.value = can;
14
+ };
15
+ export const useCanGoForward = () => {
16
+ return canGoForwardSignal.value;
17
+ };
@@ -0,0 +1,100 @@
1
+ import { computed, signal } from "@preact/signals";
2
+ import { arraySignal } from "../utils/array_signal.js";
3
+ import { executeWithCleanup } from "../utils/execute_with_cleanup.js";
4
+
5
+ let DEBUG = false;
6
+ export const enableDebugOnDocumentLoading = () => {
7
+ DEBUG = true;
8
+ };
9
+
10
+ export const windowIsLoadingSignal = signal(true);
11
+ if (document.readyState === "complete") {
12
+ windowIsLoadingSignal.value = false;
13
+ } else {
14
+ document.addEventListener("readystatechange", () => {
15
+ if (document.readyState === "complete") {
16
+ windowIsLoadingSignal.value = false;
17
+ }
18
+ });
19
+ }
20
+
21
+ const [
22
+ documentLoadingRouteArraySignal,
23
+ addToDocumentLoadingRouteArraySignal,
24
+ removeFromDocumentLoadingRouteArraySignal,
25
+ ] = arraySignal([]);
26
+ export { documentLoadingRouteArraySignal };
27
+ export const routingWhile = (fn, routeNames = []) => {
28
+ if (DEBUG && routeNames.length > 0) {
29
+ console.debug(`routingWhile: Adding routes to loading state:`, routeNames);
30
+ }
31
+ addToDocumentLoadingRouteArraySignal(...routeNames);
32
+ return executeWithCleanup(fn, () => {
33
+ removeFromDocumentLoadingRouteArraySignal(...routeNames);
34
+ if (DEBUG && routeNames.length > 0) {
35
+ console.debug(
36
+ `routingWhile: Removed routes from loading state:`,
37
+ routeNames,
38
+ "state after removing:",
39
+ documentLoadingRouteArraySignal.peek(),
40
+ );
41
+ }
42
+ });
43
+ };
44
+
45
+ const [
46
+ documentLoadingActionArraySignal,
47
+ addToDocumentLoadingActionArraySignal,
48
+ removeFromDocumentLoadingActionArraySignal,
49
+ ] = arraySignal([]);
50
+ export { documentLoadingActionArraySignal };
51
+ export const workingWhile = (fn, actionNames = []) => {
52
+ if (DEBUG && actionNames.length > 0) {
53
+ console.debug(
54
+ `workingWhile: Adding actions to loading state:`,
55
+ actionNames,
56
+ );
57
+ }
58
+ addToDocumentLoadingActionArraySignal(...actionNames);
59
+ return executeWithCleanup(fn, () => {
60
+ removeFromDocumentLoadingActionArraySignal(...actionNames);
61
+ if (DEBUG && actionNames.length > 0) {
62
+ console.debug(
63
+ `routingWhile: Removed action from loading state:`,
64
+ actionNames,
65
+ "start after removing:",
66
+ documentLoadingActionArraySignal.peek(),
67
+ );
68
+ }
69
+ });
70
+ };
71
+
72
+ export const documentIsBusySignal = computed(() => {
73
+ return (
74
+ documentLoadingRouteArraySignal.value.length > 0 ||
75
+ documentLoadingActionArraySignal.value.length > 0
76
+ );
77
+ });
78
+
79
+ const documentLoadingReasonArraySignal = computed(() => {
80
+ const windowIsLoading = windowIsLoadingSignal.value;
81
+ const routesLoading = documentLoadingRouteArraySignal.value;
82
+ const actionsLoading = documentLoadingActionArraySignal.value;
83
+ const reasonArray = [];
84
+ if (windowIsLoading) {
85
+ reasonArray.push("window_loading");
86
+ }
87
+ if (routesLoading.length > 0) {
88
+ reasonArray.push("document_routing");
89
+ }
90
+ if (actionsLoading.length > 0) {
91
+ reasonArray.push("document_working");
92
+ }
93
+ return reasonArray;
94
+ });
95
+ export const useDocumentLoadingReasonArray = () => {
96
+ return documentLoadingReasonArraySignal.value;
97
+ };
98
+ export const useDocumentIsLoading = () => {
99
+ return documentLoadingReasonArraySignal.value.length > 0;
100
+ };
@@ -0,0 +1,9 @@
1
+ import { signal } from "@preact/signals";
2
+
3
+ export const documentStateSignal = signal(null);
4
+ export const useDocumentState = () => {
5
+ return documentStateSignal.value;
6
+ };
7
+ export const updateDocumentState = (value) => {
8
+ documentStateSignal.value = value;
9
+ };
@@ -0,0 +1,9 @@
1
+ import { signal } from "@preact/signals";
2
+
3
+ export const documentUrlSignal = signal(window.location.href);
4
+ export const useDocumentUrl = () => {
5
+ return documentUrlSignal.value;
6
+ };
7
+ export const updateDocumentUrl = (value) => {
8
+ documentUrlSignal.value = value;
9
+ };
@@ -0,0 +1,19 @@
1
+ import { useMemo } from "preact/hooks";
2
+ import { isVisited, visitedUrlsSignal } from "./browser_integration.js";
3
+
4
+ /**
5
+ * Hook that reactively checks if a URL is visited.
6
+ * Re-renders when the visited URL set changes.
7
+ *
8
+ * @param {string} url - The URL to check
9
+ * @returns {boolean} Whether the URL has been visited
10
+ */
11
+ export const useIsVisited = (url) => {
12
+ return useMemo(() => {
13
+ // Access the signal to create reactive dependency
14
+ // eslint-disable-next-line no-unused-expressions
15
+ visitedUrlsSignal.value;
16
+
17
+ return isVisited(url);
18
+ }, [url, visitedUrlsSignal.value]);
19
+ };
@@ -0,0 +1,199 @@
1
+ import { signal } from "@preact/signals";
2
+ import { setActionDispatcher } from "../actions.js";
3
+ import { executeWithCleanup } from "../utils/execute_with_cleanup.js";
4
+ import { updateDocumentState } from "./document_state_signal.js";
5
+ import { documentUrlSignal, updateDocumentUrl } from "./document_url_signal.js";
6
+
7
+ export const setupBrowserIntegrationViaHistory = ({
8
+ applyActions,
9
+ applyRouting,
10
+ }) => {
11
+ const { history } = window;
12
+
13
+ let globalAbortController = new AbortController();
14
+ const triggerGlobalAbort = (reason) => {
15
+ globalAbortController.abort(reason);
16
+ globalAbortController = new AbortController();
17
+ };
18
+
19
+ const dispatchActions = (params) => {
20
+ const { requestedResult } = applyActions({
21
+ globalAbortSignal: globalAbortController.signal,
22
+ abortSignal: new AbortController().signal,
23
+ ...params,
24
+ });
25
+ return requestedResult;
26
+ };
27
+ setActionDispatcher(dispatchActions);
28
+
29
+ const getDocumentState = () => {
30
+ return window.history.state ? { ...window.history.state } : null;
31
+ };
32
+
33
+ const replaceDocumentState = (newState) => {
34
+ const url = window.location.href;
35
+ window.history.replaceState(newState, null, url);
36
+ handleRoutingTask(url, { state: newState });
37
+ };
38
+
39
+ const historyStartAtStart = getDocumentState();
40
+ const visitedUrlSet = historyStartAtStart
41
+ ? new Set(historyStartAtStart.jsenv_visited_urls || [])
42
+ : new Set();
43
+
44
+ // Create a signal that tracks visited URLs for reactive updates
45
+ // Using a counter instead of the Set directly for better performance
46
+ // Links will check isVisited() when this signal changes
47
+ const visitedUrlsSignal = signal(0);
48
+
49
+ const isVisited = (url) => {
50
+ url = new URL(url, window.location.href).href;
51
+ return visitedUrlSet.has(url);
52
+ };
53
+ const markUrlAsVisited = (url) => {
54
+ if (visitedUrlSet.has(url)) {
55
+ return;
56
+ }
57
+ visitedUrlSet.add(url);
58
+
59
+ // Increment signal to notify subscribers that visited URLs changed
60
+ visitedUrlsSignal.value++;
61
+
62
+ const historyState = getDocumentState() || {};
63
+ const hsitoryStateWithVisitedUrls = {
64
+ ...historyState,
65
+ jsenv_visited_urls: Array.from(visitedUrlSet),
66
+ };
67
+ window.history.replaceState(
68
+ hsitoryStateWithVisitedUrls,
69
+ null,
70
+ window.location.href,
71
+ );
72
+ updateDocumentState(hsitoryStateWithVisitedUrls);
73
+ };
74
+
75
+ let abortController = null;
76
+ const handleRoutingTask = (url, { state, replace }) => {
77
+ markUrlAsVisited(url);
78
+ updateDocumentUrl(url);
79
+ updateDocumentState(state);
80
+ if (abortController) {
81
+ abortController.abort(`navigating to ${url}`);
82
+ }
83
+ abortController = new AbortController();
84
+
85
+ const { allResult, requestedResult } = applyRouting(url, {
86
+ globalAbortSignal: globalAbortController.signal,
87
+ abortSignal: abortController.signal,
88
+ state,
89
+ replace,
90
+ isVisited,
91
+ });
92
+
93
+ executeWithCleanup(
94
+ () => allResult,
95
+ () => {
96
+ abortController = undefined;
97
+ },
98
+ );
99
+ return requestedResult;
100
+ };
101
+
102
+ // Browser event handlers
103
+ window.addEventListener(
104
+ "click",
105
+ (e) => {
106
+ if (e.button !== 0) {
107
+ // Ignore non-left clicks
108
+ return;
109
+ }
110
+ if (e.metaKey) {
111
+ // Ignore clicks with meta key (e.g. open in new tab)
112
+ return;
113
+ }
114
+ const linkElement = e.target.closest("a");
115
+ if (!linkElement) {
116
+ return;
117
+ }
118
+ const href = linkElement.href;
119
+ if (!href || !href.startsWith(window.location.origin)) {
120
+ return;
121
+ }
122
+ if (linkElement.hasAttribute("data-readonly")) {
123
+ return;
124
+ }
125
+ // TODO: ignore anchor navigation
126
+ e.preventDefault();
127
+ const state = null;
128
+ history.pushState(state, null, href);
129
+ handleRoutingTask(href, { state });
130
+ },
131
+ { capture: true },
132
+ );
133
+
134
+ window.addEventListener(
135
+ "submit",
136
+ () => {
137
+ // TODO: Handle form submissions
138
+ },
139
+ { capture: true },
140
+ );
141
+
142
+ window.addEventListener("popstate", (popstateEvent) => {
143
+ const url = window.location.href;
144
+ const state = popstateEvent.state;
145
+ handleRoutingTask(url, { state });
146
+ });
147
+
148
+ const goTo = async (url, { state = null, replace } = {}) => {
149
+ const currentUrl = documentUrlSignal.peek();
150
+ if (url === currentUrl) {
151
+ return;
152
+ }
153
+ if (replace) {
154
+ window.history.replaceState(state, null, url);
155
+ } else {
156
+ window.history.pushState(state, null, url);
157
+ }
158
+ handleRoutingTask(url, { state, replace });
159
+ };
160
+
161
+ const stop = (reason = "stop called") => {
162
+ triggerGlobalAbort(reason);
163
+ };
164
+
165
+ const reload = () => {
166
+ const url = window.location.href;
167
+ const state = history.state;
168
+ handleRoutingTask(url, { state });
169
+ };
170
+
171
+ const goBack = () => {
172
+ window.history.back();
173
+ };
174
+
175
+ const goForward = () => {
176
+ window.history.forward();
177
+ };
178
+
179
+ const init = () => {
180
+ const url = window.location.href;
181
+ const state = history.state;
182
+ history.replaceState(state, null, url);
183
+ handleRoutingTask(url, { state, replace: true });
184
+ };
185
+
186
+ return {
187
+ integration: "browser_history_api",
188
+ init,
189
+ goTo,
190
+ stop,
191
+ reload,
192
+ goBack,
193
+ goForward,
194
+ getDocumentState,
195
+ replaceDocumentState,
196
+ isVisited,
197
+ visitedUrlsSignal,
198
+ };
199
+ };