@jsenv/navi 0.10.1 → 0.11.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/dist/jsenv_navi.js +13858 -23295
- package/dist/jsenv_navi.js.map +1281 -0
- package/package.json +5 -7
- package/index.js +0 -122
- package/src/action_private_properties.js +0 -11
- package/src/action_proxy_test.html +0 -353
- package/src/action_run_states.js +0 -5
- package/src/actions.js +0 -1401
- package/src/browser_integration/browser_integration.js +0 -216
- package/src/browser_integration/document_back_and_forward.js +0 -17
- package/src/browser_integration/document_loading_signal.js +0 -100
- package/src/browser_integration/document_state_signal.js +0 -9
- package/src/browser_integration/document_url_signal.js +0 -9
- package/src/browser_integration/use_is_visited.js +0 -19
- package/src/browser_integration/via_history.js +0 -232
- package/src/browser_integration/via_navigation.js +0 -168
- package/src/components/action_execution/form_context.js +0 -5
- package/src/components/action_execution/render_actionable_component.jsx +0 -29
- package/src/components/action_execution/use_action.js +0 -99
- package/src/components/action_execution/use_execute_action.js +0 -177
- package/src/components/action_execution/use_run_on_mount.js +0 -9
- package/src/components/action_renderer.jsx +0 -125
- package/src/components/callout/callout.js +0 -990
- package/src/components/callout/callout_demo.html +0 -201
- package/src/components/callout/test_dynamic_positioning.html +0 -161
- package/src/components/callout/test_html_document_iframe.html +0 -182
- package/src/components/demos/0_button_demo.html +0 -707
- package/src/components/demos/10_column_reordering_debug.html +0 -277
- package/src/components/demos/11_table_selection_debug.html +0 -432
- package/src/components/demos/1_checkbox_demo.html +0 -754
- package/src/components/demos/2_input_textual_demo.html +0 -286
- package/src/components/demos/3_radio_demo.html +0 -874
- package/src/components/demos/4_select_demo.html +0 -100
- package/src/components/demos/5_list_scrollable_demo.html +0 -153
- package/src/components/demos/6_tablist_demo.html +0 -77
- package/src/components/demos/7_table_selection_demo.html +0 -176
- package/src/components/demos/8_table_fixed_headers_demo.html +0 -584
- package/src/components/demos/9_table_column_drag_demo.html +0 -325
- package/src/components/demos/action/0_button_demo.html +0 -204
- package/src/components/demos/action/10_shortcuts_demo.html +0 -189
- package/src/components/demos/action/11_nested_shortcuts_demo.xhtml +0 -401
- package/src/components/demos/action/1_input_text_demo.html +0 -876
- package/src/components/demos/action/2_form_multiple.html +0 -303
- package/src/components/demos/action/3_details_demo.html +0 -203
- package/src/components/demos/action/4_input_checkbox_demo.html +0 -731
- package/src/components/demos/action/5_input_checkbox_state_demo.html +0 -270
- package/src/components/demos/action/6_checkbox_list_demo.html +0 -341
- package/src/components/demos/action/7_radio_list_demo.html +0 -357
- package/src/components/demos/action/8_editable_demo.html +0 -431
- package/src/components/demos/action/9_link_demo.html +0 -194
- package/src/components/demos/demo.md +0 -0
- package/src/components/demos/route/basic/basic.html +0 -14
- package/src/components/demos/route/basic/basic_route_demo.jsx +0 -224
- package/src/components/demos/route/multi/multi.html +0 -14
- package/src/components/demos/route/multi/multi_route_demo.jsx +0 -277
- package/src/components/demos/ui_transition/0_action_renderer_ui_transition_demo.html +0 -695
- package/src/components/demos/ui_transition/1_nested_ui_transition_demo.html +0 -429
- package/src/components/demos/ui_transition/2_height_transition_test.html +0 -295
- package/src/components/details/details.jsx +0 -245
- package/src/components/details/summary_marker.jsx +0 -141
- package/src/components/edition/editable.jsx +0 -186
- package/src/components/error_boundary_context.js +0 -9
- package/src/components/field/README.md +0 -247
- package/src/components/field/button.jsx +0 -429
- package/src/components/field/checkbox_list.jsx +0 -185
- package/src/components/field/collect_form_element_values.js +0 -82
- package/src/components/field/custom_field.js +0 -106
- package/src/components/field/form.jsx +0 -209
- package/src/components/field/input.jsx +0 -16
- package/src/components/field/input_checkbox.jsx +0 -434
- package/src/components/field/input_radio.jsx +0 -432
- package/src/components/field/input_textual.jsx +0 -389
- package/src/components/field/label.jsx +0 -46
- package/src/components/field/radio_list.jsx +0 -183
- package/src/components/field/select.jsx +0 -256
- package/src/components/field/use_action_events.js +0 -132
- package/src/components/field/use_form_events.js +0 -59
- package/src/components/field/use_ui_state_controller.js +0 -506
- package/src/components/item_tracker/README.md +0 -461
- package/src/components/item_tracker/use_isolated_item_tracker.jsx +0 -209
- package/src/components/item_tracker/use_isolated_item_tracker_demo.html +0 -148
- package/src/components/item_tracker/use_isolated_item_tracker_demo.jsx +0 -460
- package/src/components/item_tracker/use_item_tracker.jsx +0 -143
- package/src/components/item_tracker/use_item_tracker_demo.html +0 -207
- package/src/components/item_tracker/use_item_tracker_demo.jsx +0 -216
- package/src/components/keyboard_shortcuts/active_keyboard_shortcuts.jsx +0 -87
- package/src/components/keyboard_shortcuts/aria_key_shortcuts.js +0 -61
- package/src/components/keyboard_shortcuts/keyboard_key_meta.js +0 -17
- package/src/components/keyboard_shortcuts/keyboard_shortcuts.js +0 -371
- package/src/components/keyboard_shortcuts/os.js +0 -9
- package/src/components/layout/demos/demo_flex.html +0 -638
- package/src/components/layout/demos/demo_layout_style_buttons.html +0 -351
- package/src/components/layout/demos/demo_layout_style_input.html +0 -226
- package/src/components/layout/demos/demo_layout_style_text.html +0 -514
- package/src/components/layout/flex.jsx +0 -109
- package/src/components/layout/layout_context.jsx +0 -3
- package/src/components/layout/spacing.jsx +0 -20
- package/src/components/layout/use_layout_style.js +0 -249
- package/src/components/link/link.jsx +0 -267
- package/src/components/link/link_with_icon.jsx +0 -52
- package/src/components/loader/loader_background.jsx +0 -372
- package/src/components/loader/loading_spinner.jsx +0 -68
- package/src/components/loader/network_speed.js +0 -83
- package/src/components/loader/rectangle_loading.jsx +0 -244
- package/src/components/props_composition/demos/demo_with_props_style.html +0 -81
- package/src/components/props_composition/with_props_class_name.js +0 -37
- package/src/components/props_composition/with_props_style.js +0 -26
- package/src/components/route.jsx +0 -19
- package/src/components/selection/selection.jsx +0 -1583
- package/src/components/svg/font_sized_svg.jsx +0 -59
- package/src/components/svg/icon_and_text.jsx +0 -21
- package/src/components/svg/svg_mask_overlay.jsx +0 -105
- package/src/components/table/drag/table_drag.jsx +0 -506
- package/src/components/table/resize/table_resize.jsx +0 -650
- package/src/components/table/resize/table_size.js +0 -43
- package/src/components/table/selection/table_selection.js +0 -106
- package/src/components/table/selection/table_selection.jsx +0 -203
- package/src/components/table/sticky/sticky_group.js +0 -354
- package/src/components/table/sticky/table_sticky.js +0 -25
- package/src/components/table/sticky/table_sticky.jsx +0 -501
- package/src/components/table/table.jsx +0 -721
- package/src/components/table/table_css.js +0 -211
- package/src/components/table/table_ui.jsx +0 -49
- package/src/components/table/use_cells_and_columns.js +0 -90
- package/src/components/table/use_object_array_to_cells.js +0 -46
- package/src/components/table/z_indexes.js +0 -23
- package/src/components/tablist/tablist.jsx +0 -99
- package/src/components/text/demos/demo_text_and_icon.html +0 -421
- package/src/components/text/overflow.jsx +0 -15
- package/src/components/text/text.jsx +0 -83
- package/src/components/text/text_and_count.jsx +0 -28
- package/src/components/ui_transition.jsx +0 -128
- package/src/components/use_auto_focus.js +0 -94
- package/src/components/use_batch_during_render.js +0 -33
- package/src/components/use_debounce_true.js +0 -31
- package/src/components/use_dependencies_diff.js +0 -35
- package/src/components/use_focus_group.js +0 -20
- package/src/components/use_initial_value.js +0 -78
- package/src/components/use_is_visited.js +0 -19
- package/src/components/use_ref_array.js +0 -38
- package/src/components/use_signal_sync.js +0 -50
- package/src/components/use_stable_callback.js +0 -68
- package/src/components/use_state_array.js +0 -47
- package/src/docs/actions.md +0 -250
- package/src/docs/demos/resource/action_status.jsx +0 -42
- package/src/docs/demos/resource/demo.md +0 -1
- package/src/docs/demos/resource/resource_demo_0.html +0 -84
- package/src/docs/demos/resource/resource_demo_10_post_gc.html +0 -364
- package/src/docs/demos/resource/resource_demo_11_describe_many.html +0 -362
- package/src/docs/demos/resource/resource_demo_2.html +0 -173
- package/src/docs/demos/resource/resource_demo_3_filtered_users.html +0 -415
- package/src/docs/demos/resource/resource_demo_4_details.html +0 -284
- package/src/docs/demos/resource/resource_demo_5_renderer_lazy.html +0 -115
- package/src/docs/demos/resource/resource_demo_6_gc.html +0 -217
- package/src/docs/demos/resource/resource_demo_7_child_gc.html +0 -240
- package/src/docs/demos/resource/resource_demo_8_proxy_gc.html +0 -319
- package/src/docs/demos/resource/resource_demo_9_describe_one.html +0 -472
- package/src/docs/demos/resource/tata.jsx +0 -3
- package/src/docs/demos/resource/toto.jsx +0 -3
- package/src/docs/demos/user_nav/user_nav.html +0 -12
- package/src/docs/demos/user_nav/user_nav.jsx +0 -330
- package/src/docs/resource_dependencies.md +0 -103
- package/src/docs/resource_with_params.md +0 -80
- package/src/navi_css_vars.js +0 -14
- package/src/notes.md +0 -34
- package/src/route/route.js +0 -596
- package/src/route/route.xtest.html +0 -228
- package/src/store/array_signal_store.js +0 -537
- package/src/store/local_storage_signal.js +0 -17
- package/src/store/resource_graph.js +0 -1304
- package/src/store/tests/resource_graph_autoreload_demo.html +0 -12
- package/src/store/tests/resource_graph_autoreload_demo.jsx +0 -964
- package/src/store/tests/resource_graph_dependencies.test_manual.js +0 -95
- package/src/store/value_in_local_storage.js +0 -187
- package/src/symbol_object_signal.js +0 -1
- package/src/use_action_data.js +0 -10
- package/src/use_action_status.js +0 -47
- package/src/utils/add_many_event_listeners.js +0 -15
- package/src/utils/array_add_remove.js +0 -61
- package/src/utils/array_signal.js +0 -15
- package/src/utils/compare_two_js_values.js +0 -172
- package/src/utils/execute_with_cleanup.js +0 -21
- package/src/utils/get_caller_info.js +0 -85
- package/src/utils/is_signal.js +0 -20
- package/src/utils/js_value_weak_map.js +0 -162
- package/src/utils/js_value_weak_map_demo.html +0 -690
- package/src/utils/merge_two_js_values.js +0 -53
- package/src/utils/stringify_for_display.js +0 -131
- package/src/utils/weak_effect.js +0 -48
- package/src/validation/constraints/confirm_constraint.js +0 -14
- package/src/validation/constraints/create_unique_value_constraint.js +0 -27
- package/src/validation/constraints/native_constraints.js +0 -338
- package/src/validation/constraints/readonly_constraint.js +0 -41
- package/src/validation/constraints/same_as_constraint.js +0 -42
- package/src/validation/constraints/single_space_constraint.js +0 -13
- package/src/validation/custom_constraint_validation.js +0 -793
- package/src/validation/custom_message.js +0 -18
- package/src/validation/demos/browser_style.png +0 -0
- package/src/validation/demos/demo_same_as_constraint.html +0 -259
- package/src/validation/demos/form_validation_demo.html +0 -142
- package/src/validation/demos/form_validation_demo_preact.html +0 -87
- package/src/validation/demos/form_validation_native_popover_demo.html +0 -168
- package/src/validation/demos/form_validation_vs_native_demo.html +0 -172
- package/src/validation/hooks/use_constraints.js +0 -23
- package/src/validation/hooks/use_custom_validation_ref.js +0 -73
- package/src/validation/hooks/use_validation_message.js +0 -19
- package/src/validation/input_change_effect.js +0 -106
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { updateDocumentUrl } from "./document_url_signal.js";
|
|
2
|
-
|
|
3
|
-
let DEBUG = false;
|
|
4
|
-
|
|
5
|
-
export const setupRoutingViaNavigation = (handler) => {
|
|
6
|
-
updateDocumentUrl(navigation.currentEntry.url);
|
|
7
|
-
navigation.addEventListener("currententrychange", () => {
|
|
8
|
-
updateDocumentUrl(navigation.currentEntry.url);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
let isReloadFromNavigationAPI = false;
|
|
12
|
-
const navigationReload = navigation.reload;
|
|
13
|
-
navigation.reload = (...args) => {
|
|
14
|
-
isReloadFromNavigationAPI = true;
|
|
15
|
-
navigationReload.call(navigation, ...args);
|
|
16
|
-
isReloadFromNavigationAPI = false;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
navigation.addEventListener("navigate", (event) => {
|
|
20
|
-
if (!event.canIntercept) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (event.hashChange || event.downloadRequest !== null) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (
|
|
27
|
-
!event.userInitiated &&
|
|
28
|
-
event.navigationType === "reload" &&
|
|
29
|
-
event.isTrusted &&
|
|
30
|
-
!isReloadFromNavigationAPI
|
|
31
|
-
) {
|
|
32
|
-
// let window.location.reload() reload the whole document
|
|
33
|
-
// (used by jsenv hot reload)
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const { signal } = event;
|
|
37
|
-
const formAction = event.info?.formAction;
|
|
38
|
-
const isReload = event.navigationType === "reload";
|
|
39
|
-
const isReplace = event.navigationType === "replace";
|
|
40
|
-
const currentUrl = navigation.currentEntry.url;
|
|
41
|
-
const destinationUrl = event.destination.url;
|
|
42
|
-
const currentState = navigation.currentEntry.getState();
|
|
43
|
-
const destinationState = event.destination.getState();
|
|
44
|
-
const formData = event.formData || event.info?.formData;
|
|
45
|
-
const formUrl = event.info?.formUrl;
|
|
46
|
-
const stopAbortController = new AbortController();
|
|
47
|
-
const stopSignal = stopAbortController.signal;
|
|
48
|
-
const removeStopButtonClickDetector = detectBrowserStopButtonClick(
|
|
49
|
-
signal,
|
|
50
|
-
() => {
|
|
51
|
-
stopAbortController.abort("stop button clicked");
|
|
52
|
-
},
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
if (DEBUG) {
|
|
56
|
-
console.log("receive navigate event", {
|
|
57
|
-
destinationUrl,
|
|
58
|
-
destinationState,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
event.intercept({
|
|
63
|
-
handler: async () => {
|
|
64
|
-
if (event.info?.onStart) {
|
|
65
|
-
event.info.onStart();
|
|
66
|
-
}
|
|
67
|
-
try {
|
|
68
|
-
await handler(event, {
|
|
69
|
-
abortSignal: signal,
|
|
70
|
-
stopSignal,
|
|
71
|
-
formAction,
|
|
72
|
-
formData,
|
|
73
|
-
formUrl,
|
|
74
|
-
isReload,
|
|
75
|
-
isReplace,
|
|
76
|
-
currentUrl,
|
|
77
|
-
destinationUrl,
|
|
78
|
-
currentState,
|
|
79
|
-
destinationState,
|
|
80
|
-
});
|
|
81
|
-
} catch (e) {
|
|
82
|
-
console.error(e); // browser remains silent in case of error during handler so we explicitely log the error to the console
|
|
83
|
-
throw e;
|
|
84
|
-
} finally {
|
|
85
|
-
removeStopButtonClickDetector();
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
// https://github.com/WICG/navigation-api?tab=readme-ov-file#focus-management
|
|
89
|
-
// without this, after clicking <a href="...">, the focus does to document.body
|
|
90
|
-
// which is problematic for shortcuts for instance
|
|
91
|
-
focusReset: "manual",
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
// setupNavigateHandler(async (event, { stopSignal, formAction, formData }) => {
|
|
97
|
-
// if (formAction) {
|
|
98
|
-
// const result = await applyAction(formAction, {
|
|
99
|
-
// signal: stopSignal,
|
|
100
|
-
// formData,
|
|
101
|
-
// });
|
|
102
|
-
// event.info.formActionCallback(result);
|
|
103
|
-
// }
|
|
104
|
-
|
|
105
|
-
// await navMethods.applyRouting({
|
|
106
|
-
// sourceUrl: currentUrl,
|
|
107
|
-
// targetUrl: formUrl || destinationUrl,
|
|
108
|
-
// sourceState: currentState,
|
|
109
|
-
// targetState: destinationState || currentState,
|
|
110
|
-
// abortSignal,
|
|
111
|
-
// stopSignal,
|
|
112
|
-
// isReload,
|
|
113
|
-
// isReplace,
|
|
114
|
-
// info: event.info,
|
|
115
|
-
// });
|
|
116
|
-
// if (formUrl) {
|
|
117
|
-
// const finishedPromise = event.target.transition.finished;
|
|
118
|
-
// (async () => {
|
|
119
|
-
// try {
|
|
120
|
-
// await finishedPromise;
|
|
121
|
-
// } finally {
|
|
122
|
-
// navigation.navigate(window.location.href, {
|
|
123
|
-
// state: navigation.currentEntry.getState(),
|
|
124
|
-
// history: "replace",
|
|
125
|
-
// });
|
|
126
|
-
// }
|
|
127
|
-
// })();
|
|
128
|
-
// }
|
|
129
|
-
// });
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* There is 2 distinct reason to abort a navigation:
|
|
133
|
-
* - the user clicked the browser stop button
|
|
134
|
-
* - the user navigate to an other page
|
|
135
|
-
*
|
|
136
|
-
* When navigating to an other page we don't want to abort anything, the routing does that
|
|
137
|
-
* When clicking the stop button we want to cancel everything
|
|
138
|
-
*
|
|
139
|
-
* To detect that when aborted, we wait a setTimeout to see if we receive a new navigation
|
|
140
|
-
* If yes it means this is an abort due to a new navigation
|
|
141
|
-
* Otherwise it's an abort due to the stop button
|
|
142
|
-
*
|
|
143
|
-
* On top of that stop button must cancel X navigation so the last navigation detecting the stop click
|
|
144
|
-
* is notifying any current navigation that stop button was clicked
|
|
145
|
-
*/
|
|
146
|
-
let callEffect = () => {};
|
|
147
|
-
const browserStopButtonClickCallbackSet = new Set();
|
|
148
|
-
const detectBrowserStopButtonClick = (navigateEventSignal, callback) => {
|
|
149
|
-
callEffect();
|
|
150
|
-
browserStopButtonClickCallbackSet.add(callback);
|
|
151
|
-
navigateEventSignal.addEventListener("abort", async () => {
|
|
152
|
-
const timeout = setTimeout(() => {
|
|
153
|
-
callEffect = () => {};
|
|
154
|
-
|
|
155
|
-
for (const browserStopButtonClickCallback of browserStopButtonClickCallbackSet) {
|
|
156
|
-
browserStopButtonClickCallback();
|
|
157
|
-
}
|
|
158
|
-
browserStopButtonClickCallbackSet.clear();
|
|
159
|
-
});
|
|
160
|
-
callEffect = () => {
|
|
161
|
-
clearTimeout(timeout);
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
return () => {
|
|
166
|
-
browserStopButtonClickCallbackSet.delete(callback);
|
|
167
|
-
};
|
|
168
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { useContext } from "preact/hooks";
|
|
2
|
-
|
|
3
|
-
import { FormContext } from "./form_context.js";
|
|
4
|
-
|
|
5
|
-
export const renderActionableComponent = (
|
|
6
|
-
props,
|
|
7
|
-
ref,
|
|
8
|
-
{ Basic, WithAction, InsideForm, WithActionInsideForm },
|
|
9
|
-
) => {
|
|
10
|
-
const { action, shortcuts } = props;
|
|
11
|
-
const formContext = useContext(FormContext);
|
|
12
|
-
const hasActionProps = Boolean(action || (shortcuts && shortcuts.length > 0));
|
|
13
|
-
const considerInsideForm = Boolean(formContext);
|
|
14
|
-
|
|
15
|
-
if (hasActionProps && WithAction) {
|
|
16
|
-
if (considerInsideForm && WithActionInsideForm) {
|
|
17
|
-
return (
|
|
18
|
-
<WithActionInsideForm formContext={formContext} ref={ref} {...props} />
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
return <WithAction ref={ref} {...props} />;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (considerInsideForm && InsideForm) {
|
|
25
|
-
return <InsideForm formContext={formContext} ref={ref} {...props} />;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return <Basic ref={ref} {...props} />;
|
|
29
|
-
};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { useSignal } from "@preact/signals";
|
|
2
|
-
import { useCallback, useRef } from "preact/hooks";
|
|
3
|
-
|
|
4
|
-
import { createAction } from "../../actions.js";
|
|
5
|
-
import { addIntoArray, removeFromArray } from "../../utils/array_add_remove.js";
|
|
6
|
-
|
|
7
|
-
// used by form elements such as <input>, <select>, <textarea> to have their own action bound to a single parameter
|
|
8
|
-
// when inside a <form> the form params are updated when the form element single param is updated
|
|
9
|
-
export const useActionBoundToOneParam = (action, externalValue) => {
|
|
10
|
-
const actionFirstArgSignal = useSignal(externalValue);
|
|
11
|
-
const boundAction = useBoundAction(action, actionFirstArgSignal);
|
|
12
|
-
const getValue = useCallback(() => actionFirstArgSignal.value, []);
|
|
13
|
-
const setValue = useCallback((value) => {
|
|
14
|
-
actionFirstArgSignal.value = value;
|
|
15
|
-
}, []);
|
|
16
|
-
const externalValueRef = useRef(externalValue);
|
|
17
|
-
if (externalValue !== externalValueRef.current) {
|
|
18
|
-
externalValueRef.current = externalValue;
|
|
19
|
-
setValue(externalValue);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const value = getValue();
|
|
23
|
-
return [boundAction, value, setValue];
|
|
24
|
-
};
|
|
25
|
-
export const useActionBoundToOneArrayParam = (
|
|
26
|
-
action,
|
|
27
|
-
name,
|
|
28
|
-
externalValue,
|
|
29
|
-
fallbackValue,
|
|
30
|
-
defaultValue,
|
|
31
|
-
) => {
|
|
32
|
-
const [boundAction, value, setValue] = useActionBoundToOneParam(
|
|
33
|
-
action,
|
|
34
|
-
name,
|
|
35
|
-
externalValue,
|
|
36
|
-
fallbackValue,
|
|
37
|
-
defaultValue,
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const add = (valueToAdd, valueArray = value) => {
|
|
41
|
-
setValue(addIntoArray(valueArray, valueToAdd));
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const remove = (valueToRemove, valueArray = value) => {
|
|
45
|
-
setValue(removeFromArray(valueArray, valueToRemove));
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const result = [boundAction, value, setValue];
|
|
49
|
-
result.add = add;
|
|
50
|
-
result.remove = remove;
|
|
51
|
-
return result;
|
|
52
|
-
};
|
|
53
|
-
// used by <details> to just call their action
|
|
54
|
-
export const useAction = (action, paramsSignal) => {
|
|
55
|
-
return useBoundAction(action, paramsSignal);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const useBoundAction = (action, actionParamsSignal) => {
|
|
59
|
-
const actionRef = useRef();
|
|
60
|
-
const actionCallbackRef = useRef();
|
|
61
|
-
|
|
62
|
-
if (!action) {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
if (isFunctionButNotAnActionFunction(action)) {
|
|
66
|
-
actionCallbackRef.current = action;
|
|
67
|
-
const existingAction = actionRef.current;
|
|
68
|
-
if (existingAction) {
|
|
69
|
-
return existingAction;
|
|
70
|
-
}
|
|
71
|
-
const actionFromFunction = createAction(
|
|
72
|
-
(...args) => {
|
|
73
|
-
return actionCallbackRef.current?.(...args);
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
name: action.name,
|
|
77
|
-
// We don't want to give empty params by default
|
|
78
|
-
// we want to give undefined for regular functions
|
|
79
|
-
params: undefined,
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
if (!actionParamsSignal) {
|
|
83
|
-
actionRef.current = actionFromFunction;
|
|
84
|
-
return actionFromFunction;
|
|
85
|
-
}
|
|
86
|
-
const actionBoundToParams =
|
|
87
|
-
actionFromFunction.bindParams(actionParamsSignal);
|
|
88
|
-
actionRef.current = actionBoundToParams;
|
|
89
|
-
return actionBoundToParams;
|
|
90
|
-
}
|
|
91
|
-
if (actionParamsSignal) {
|
|
92
|
-
return action.bindParams(actionParamsSignal);
|
|
93
|
-
}
|
|
94
|
-
return action;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const isFunctionButNotAnActionFunction = (action) => {
|
|
98
|
-
return typeof action === "function" && !action.isAction;
|
|
99
|
-
};
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import { useCallback, useLayoutEffect, useRef, useState } from "preact/hooks";
|
|
2
|
-
import {
|
|
3
|
-
addCustomMessage,
|
|
4
|
-
removeCustomMessage,
|
|
5
|
-
} from "../../validation/custom_message.js";
|
|
6
|
-
import { useResetErrorBoundary } from "../error_boundary_context.js";
|
|
7
|
-
|
|
8
|
-
let debug = false;
|
|
9
|
-
|
|
10
|
-
export const useExecuteAction = (
|
|
11
|
-
elementRef,
|
|
12
|
-
{
|
|
13
|
-
errorEffect = "show_validation_message", // "show_validation_message" or "throw"
|
|
14
|
-
errorMapping,
|
|
15
|
-
} = {},
|
|
16
|
-
) => {
|
|
17
|
-
// see https://medium.com/trabe/catching-asynchronous-errors-in-react-using-error-boundaries-5e8a5fd7b971
|
|
18
|
-
// and https://codepen.io/dmail/pen/XJJqeGp?editors=0010
|
|
19
|
-
// To change if https://github.com/preactjs/preact/issues/4754 lands
|
|
20
|
-
const [error, setError] = useState(null);
|
|
21
|
-
const resetErrorBoundary = useResetErrorBoundary();
|
|
22
|
-
useLayoutEffect(() => {
|
|
23
|
-
if (error) {
|
|
24
|
-
error.__handled__ = true; // prevent jsenv from displaying it
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
}, [error]);
|
|
28
|
-
|
|
29
|
-
const validationMessageTargetRef = useRef(null);
|
|
30
|
-
const addErrorMessage = (error) => {
|
|
31
|
-
const validationMessageTarget = validationMessageTargetRef.current;
|
|
32
|
-
const errorMapped = errorMapping ? errorMapping(error) : error;
|
|
33
|
-
addCustomMessage(validationMessageTarget, "action_error", errorMapped, {
|
|
34
|
-
// This error should not prevent <form> submission
|
|
35
|
-
// so whenever user tries to submit the form the error is cleared
|
|
36
|
-
// (Hitting enter key, clicking on submit button, etc. would allow to re-submit the form in error state)
|
|
37
|
-
removeOnRequestAction: true,
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
const removeErrorMessage = () => {
|
|
41
|
-
const validationMessageTarget = validationMessageTargetRef.current;
|
|
42
|
-
if (validationMessageTarget) {
|
|
43
|
-
removeCustomMessage(validationMessageTarget, "action_error");
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
useLayoutEffect(() => {
|
|
48
|
-
const element = elementRef.current;
|
|
49
|
-
if (!element) {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
const form = element.tagName === "FORM" ? element : element.form;
|
|
53
|
-
if (!form) {
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
const onReset = () => {
|
|
57
|
-
removeErrorMessage();
|
|
58
|
-
};
|
|
59
|
-
form.addEventListener("reset", onReset);
|
|
60
|
-
return () => {
|
|
61
|
-
form.removeEventListener("reset", onReset);
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// const errorEffectRef = useRef();
|
|
66
|
-
// errorEffectRef.current = errorEffect;
|
|
67
|
-
const executeAction = useCallback(
|
|
68
|
-
(actionEvent) => {
|
|
69
|
-
const { action, actionOrigin, requester, event, method } =
|
|
70
|
-
actionEvent.detail;
|
|
71
|
-
const sharedActionEventDetail = {
|
|
72
|
-
action,
|
|
73
|
-
actionOrigin,
|
|
74
|
-
requester,
|
|
75
|
-
event,
|
|
76
|
-
method,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
if (debug) {
|
|
80
|
-
console.debug(
|
|
81
|
-
"executing action, requested by",
|
|
82
|
-
requester,
|
|
83
|
-
`(event: ${event?.type})`,
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const dispatchCustomEvent = (type, options) => {
|
|
88
|
-
const element = elementRef.current;
|
|
89
|
-
const customEvent = new CustomEvent(type, options);
|
|
90
|
-
element.dispatchEvent(customEvent);
|
|
91
|
-
};
|
|
92
|
-
if (resetErrorBoundary) {
|
|
93
|
-
resetErrorBoundary();
|
|
94
|
-
}
|
|
95
|
-
removeErrorMessage();
|
|
96
|
-
setError(null);
|
|
97
|
-
|
|
98
|
-
const validationMessageTarget = requester || elementRef.current;
|
|
99
|
-
validationMessageTargetRef.current = validationMessageTarget;
|
|
100
|
-
|
|
101
|
-
dispatchCustomEvent("actionstart", {
|
|
102
|
-
detail: sharedActionEventDetail,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
return action[method]({
|
|
106
|
-
reason: `"${event.type}" event on ${(() => {
|
|
107
|
-
const target = event.target;
|
|
108
|
-
const tagName = target.tagName.toLowerCase();
|
|
109
|
-
|
|
110
|
-
if (target.id) {
|
|
111
|
-
return `${tagName}#${target.id}`;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const uiName = target.getAttribute("data-ui-name");
|
|
115
|
-
if (uiName) {
|
|
116
|
-
return `${tagName}[data-ui-name="${uiName}"]`;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return `<${tagName}>`;
|
|
120
|
-
})()}`,
|
|
121
|
-
onAbort: (reason) => {
|
|
122
|
-
if (
|
|
123
|
-
// at this stage the action side effect might have removed the <element> from the DOM
|
|
124
|
-
// (in theory no because action side effect are batched to happen after)
|
|
125
|
-
// but other side effects might do this
|
|
126
|
-
elementRef.current
|
|
127
|
-
) {
|
|
128
|
-
dispatchCustomEvent("actionabort", {
|
|
129
|
-
detail: {
|
|
130
|
-
...sharedActionEventDetail,
|
|
131
|
-
reason,
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
onError: (error) => {
|
|
137
|
-
if (
|
|
138
|
-
// at this stage the action side effect might have removed the <element> from the DOM
|
|
139
|
-
// (in theory no because action side effect are batched to happen after)
|
|
140
|
-
// but other side effects might do this
|
|
141
|
-
elementRef.current
|
|
142
|
-
) {
|
|
143
|
-
dispatchCustomEvent("actionerror", {
|
|
144
|
-
detail: {
|
|
145
|
-
...sharedActionEventDetail,
|
|
146
|
-
error,
|
|
147
|
-
},
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
if (errorEffect === "show_validation_message") {
|
|
151
|
-
addErrorMessage(error);
|
|
152
|
-
} else if (errorEffect === "throw") {
|
|
153
|
-
setError(error);
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
onComplete: (data) => {
|
|
157
|
-
if (
|
|
158
|
-
// at this stage the action side effect might have removed the <element> from the DOM
|
|
159
|
-
// (in theory no because action side effect are batched to happen after)
|
|
160
|
-
// but other side effects might do this
|
|
161
|
-
elementRef.current
|
|
162
|
-
) {
|
|
163
|
-
dispatchCustomEvent("actionend", {
|
|
164
|
-
detail: {
|
|
165
|
-
...sharedActionEventDetail,
|
|
166
|
-
data,
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
},
|
|
173
|
-
[errorEffect],
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
return executeAction;
|
|
177
|
-
};
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { useErrorBoundary, useLayoutEffect } from "preact/hooks";
|
|
2
|
-
import { getActionPrivateProperties } from "../action_private_properties.js";
|
|
3
|
-
import { useActionStatus } from "../use_action_status.js";
|
|
4
|
-
|
|
5
|
-
import.meta.css = /* css */ `
|
|
6
|
-
.action_error {
|
|
7
|
-
padding: 20px;
|
|
8
|
-
background: #fdd;
|
|
9
|
-
border: 1px solid red;
|
|
10
|
-
margin-top: 0;
|
|
11
|
-
margin-bottom: 20px;
|
|
12
|
-
}
|
|
13
|
-
`;
|
|
14
|
-
|
|
15
|
-
const renderIdleDefault = () => null;
|
|
16
|
-
const renderLoadingDefault = () => null;
|
|
17
|
-
const renderAbortedDefault = () => null;
|
|
18
|
-
const renderErrorDefault = (error) => {
|
|
19
|
-
let routeErrorText = error && error.message ? error.message : error;
|
|
20
|
-
return <p className="action_error">An error occured: {routeErrorText}</p>;
|
|
21
|
-
};
|
|
22
|
-
const renderCompletedDefault = () => null;
|
|
23
|
-
|
|
24
|
-
export const ActionRenderer = ({ action, children, disabled }) => {
|
|
25
|
-
const {
|
|
26
|
-
idle: renderIdle = renderIdleDefault,
|
|
27
|
-
loading: renderLoading = renderLoadingDefault,
|
|
28
|
-
aborted: renderAborted = renderAbortedDefault,
|
|
29
|
-
error: renderError = renderErrorDefault,
|
|
30
|
-
completed: renderCompleted,
|
|
31
|
-
always: renderAlways,
|
|
32
|
-
} = typeof children === "function" ? { completed: children } : children || {};
|
|
33
|
-
|
|
34
|
-
if (disabled) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (action === undefined) {
|
|
39
|
-
throw new Error(
|
|
40
|
-
"ActionRenderer requires an action to render, but none was provided.",
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
const { idle, loading, aborted, error, data } = useActionStatus(action);
|
|
44
|
-
const UIRenderedPromise = useUIRenderedPromise(action);
|
|
45
|
-
const [errorBoundary, resetErrorBoundary] = useErrorBoundary();
|
|
46
|
-
|
|
47
|
-
// Mark this action as bound to UI components (has renderers)
|
|
48
|
-
// This tells the action system that errors should be caught and stored
|
|
49
|
-
// in the action's error state rather than bubbling up
|
|
50
|
-
useLayoutEffect(() => {
|
|
51
|
-
if (action) {
|
|
52
|
-
const { ui } = getActionPrivateProperties(action);
|
|
53
|
-
ui.hasRenderers = true;
|
|
54
|
-
}
|
|
55
|
-
}, [action]);
|
|
56
|
-
|
|
57
|
-
useLayoutEffect(() => {
|
|
58
|
-
resetErrorBoundary();
|
|
59
|
-
}, [action, loading, idle, resetErrorBoundary]);
|
|
60
|
-
|
|
61
|
-
useLayoutEffect(() => {
|
|
62
|
-
UIRenderedPromise.resolve();
|
|
63
|
-
return () => {
|
|
64
|
-
actionUIRenderedPromiseWeakMap.delete(action);
|
|
65
|
-
};
|
|
66
|
-
}, [action]);
|
|
67
|
-
|
|
68
|
-
// If renderAlways is provided, it wins and handles all rendering
|
|
69
|
-
if (renderAlways) {
|
|
70
|
-
return renderAlways({ idle, loading, aborted, error, data });
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (idle) {
|
|
74
|
-
return renderIdle(action);
|
|
75
|
-
}
|
|
76
|
-
if (errorBoundary) {
|
|
77
|
-
return renderError(errorBoundary, "ui_error", action);
|
|
78
|
-
}
|
|
79
|
-
if (error) {
|
|
80
|
-
return renderError(error, "action_error", action);
|
|
81
|
-
}
|
|
82
|
-
if (aborted) {
|
|
83
|
-
return renderAborted(action);
|
|
84
|
-
}
|
|
85
|
-
let renderCompletedSafe;
|
|
86
|
-
if (renderCompleted) {
|
|
87
|
-
renderCompletedSafe = renderCompleted;
|
|
88
|
-
} else {
|
|
89
|
-
const { ui } = getActionPrivateProperties(action);
|
|
90
|
-
if (ui.renderCompleted) {
|
|
91
|
-
renderCompletedSafe = ui.renderCompleted;
|
|
92
|
-
} else {
|
|
93
|
-
renderCompletedSafe = renderCompletedDefault;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (loading) {
|
|
97
|
-
if (action.canDisplayOldData && data !== undefined) {
|
|
98
|
-
return renderCompletedSafe(data, action);
|
|
99
|
-
}
|
|
100
|
-
return renderLoading(action);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return renderCompletedSafe(data, action);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const defaultPromise = Promise.resolve();
|
|
107
|
-
defaultPromise.resolve = () => {};
|
|
108
|
-
|
|
109
|
-
const actionUIRenderedPromiseWeakMap = new WeakMap();
|
|
110
|
-
const useUIRenderedPromise = (action) => {
|
|
111
|
-
if (!action) {
|
|
112
|
-
return defaultPromise;
|
|
113
|
-
}
|
|
114
|
-
const actionUIRenderedPromise = actionUIRenderedPromiseWeakMap.get(action);
|
|
115
|
-
if (actionUIRenderedPromise) {
|
|
116
|
-
return actionUIRenderedPromise;
|
|
117
|
-
}
|
|
118
|
-
let resolve;
|
|
119
|
-
const promise = new Promise((res) => {
|
|
120
|
-
resolve = res;
|
|
121
|
-
});
|
|
122
|
-
promise.resolve = resolve;
|
|
123
|
-
actionUIRenderedPromiseWeakMap.set(action, promise);
|
|
124
|
-
return promise;
|
|
125
|
-
};
|