nitro_kit 0.9.0 → 2.0.0.alpha.2
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.
- checksums.yaml +4 -4
- data/.agents/plugins/marketplace.json +20 -0
- data/CHANGELOG.md +172 -0
- data/LICENSE +28 -0
- data/README.md +22 -15
- data/STYLE_GUIDE.md +425 -0
- data/app/assets/stylesheets/nitro_kit-tailwind-v4.css +38 -0
- data/app/assets/stylesheets/nitro_kit.css +7239 -0
- data/app/components/nitro_kit/accordion.rb +125 -65
- data/app/components/nitro_kit/alert.rb +80 -43
- data/app/components/nitro_kit/app_navigation.rb +307 -0
- data/app/components/nitro_kit/app_shell.rb +227 -0
- data/app/components/nitro_kit/appearance_bootstrap.rb +146 -0
- data/app/components/nitro_kit/appearance_picker.rb +156 -0
- data/app/components/nitro_kit/auth_shell.rb +34 -0
- data/app/components/nitro_kit/avatar.rb +85 -29
- data/app/components/nitro_kit/avatar_stack.rb +155 -9
- data/app/components/nitro_kit/badge.rb +43 -81
- data/app/components/nitro_kit/button.rb +180 -103
- data/app/components/nitro_kit/button_group.rb +63 -11
- data/app/components/nitro_kit/button_to.rb +69 -0
- data/app/components/nitro_kit/card.rb +42 -45
- data/app/components/nitro_kit/checkbox.rb +115 -62
- data/app/components/nitro_kit/checkbox_group.rb +131 -22
- data/app/components/nitro_kit/choice.rb +55 -0
- data/app/components/nitro_kit/combobox.rb +292 -105
- data/app/components/nitro_kit/command_palette.rb +412 -0
- data/app/components/nitro_kit/component.rb +322 -46
- data/app/components/nitro_kit/container.rb +32 -0
- data/app/components/nitro_kit/control_group.rb +45 -0
- data/app/components/nitro_kit/danger_zone.rb +78 -0
- data/app/components/nitro_kit/data_section.rb +106 -0
- data/app/components/nitro_kit/details_table.rb +229 -0
- data/app/components/nitro_kit/dialog.rb +257 -83
- data/app/components/nitro_kit/dropdown.rb +335 -122
- data/app/components/nitro_kit/dropzone.rb +326 -0
- data/app/components/nitro_kit/empty_state.rb +99 -0
- data/app/components/nitro_kit/field.rb +461 -236
- data/app/components/nitro_kit/field_group.rb +15 -8
- data/app/components/nitro_kit/fieldset.rb +37 -39
- data/app/components/nitro_kit/flex.rb +49 -0
- data/app/components/nitro_kit/form_builder.rb +313 -72
- data/app/components/nitro_kit/form_section.rb +79 -0
- data/app/components/nitro_kit/grid.rb +34 -0
- data/app/components/nitro_kit/icon.rb +54 -25
- data/app/components/nitro_kit/input.rb +97 -10
- data/app/components/nitro_kit/label.rb +22 -6
- data/app/components/nitro_kit/layout_options.rb +7 -0
- data/app/components/nitro_kit/page_header.rb +76 -0
- data/app/components/nitro_kit/pagination.rb +355 -69
- data/app/components/nitro_kit/pagination_bar.rb +90 -0
- data/app/components/nitro_kit/progressive_image.rb +181 -0
- data/app/components/nitro_kit/radio_button.rb +77 -55
- data/app/components/nitro_kit/radio_button_group.rb +107 -36
- data/app/components/nitro_kit/responsive_value.rb +94 -0
- data/app/components/nitro_kit/rich_text_area.rb +31 -0
- data/app/components/nitro_kit/select.rb +129 -64
- data/app/components/nitro_kit/settings_layout.rb +152 -0
- data/app/components/nitro_kit/sheet.rb +202 -0
- data/app/components/nitro_kit/stat_grid.rb +75 -0
- data/app/components/nitro_kit/switch.rb +108 -51
- data/app/components/nitro_kit/table.rb +237 -45
- data/app/components/nitro_kit/tabs.rb +168 -63
- data/app/components/nitro_kit/textarea.rb +81 -10
- data/app/components/nitro_kit/toast.rb +211 -80
- data/app/components/nitro_kit/toolbar.rb +75 -0
- data/app/components/nitro_kit/tooltip.rb +235 -34
- data/app/components/nitro_kit/typeset.rb +28 -0
- data/app/javascript/controllers/nk/app_shell_controller.js +224 -0
- data/app/javascript/controllers/nk/appearance_controller.js +73 -0
- data/app/javascript/controllers/nk/avatar_controller.js +15 -0
- data/app/javascript/controllers/nk/button_controller.js +56 -0
- data/app/javascript/controllers/nk/checkable_controller.js +41 -0
- data/app/javascript/controllers/nk/combobox_controller.js +314 -82
- data/app/javascript/controllers/nk/command_palette_controller.js +249 -0
- data/app/javascript/controllers/nk/dialog_controller.js +14 -10
- data/app/javascript/controllers/nk/dropdown_controller.js +106 -58
- data/app/javascript/controllers/nk/dropzone/direct_upload.js +61 -0
- data/app/javascript/controllers/nk/dropzone/file_rules.js +77 -0
- data/app/javascript/controllers/nk/dropzone/form_submit_lock.js +30 -0
- data/app/javascript/controllers/nk/dropzone_controller.js +482 -0
- data/app/javascript/controllers/nk/overlay_position.js +37 -0
- data/app/javascript/controllers/nk/progressive_image_controller.js +116 -0
- data/app/javascript/controllers/nk/tabs_controller.js +135 -23
- data/app/javascript/controllers/nk/toast_controller.js +112 -45
- data/app/javascript/controllers/nk/tooltip_controller.js +12 -47
- data/config/importmap.rb +2 -0
- data/config/locales/en.yml +98 -0
- data/docs/agent_guide.md +139 -0
- data/docs/agent_native_spec.md +422 -0
- data/docs/component_contracts.md +263 -0
- data/docs/customization.md +595 -0
- data/docs/hotwire.md +94 -0
- data/docs/initialization_prompt.md +36 -0
- data/docs/migration_1_to_2.md +318 -0
- data/docs/new_app_strategy.md +22 -0
- data/docs/patterns/application_foundation.md +136 -0
- data/docs/patterns/crud_resource.md +144 -0
- data/docs/patterns/destructive_action.md +105 -0
- data/docs/patterns/flash_and_toast.md +57 -0
- data/docs/patterns/inline_edit.md +59 -0
- data/docs/patterns/queryable_collection.md +187 -0
- data/docs/patterns/resource_form.md +126 -0
- data/docs/rails_conventions.md +95 -0
- data/docs/rails_integration.md +514 -0
- data/lib/generators/nitro_kit/install_generator.rb +48 -0
- data/lib/generators/nitro_kit/upgrade_smoke_tests_generator.rb +67 -0
- data/lib/nitro_kit/engine.rb +20 -0
- data/lib/nitro_kit/installation.rb +671 -0
- data/lib/nitro_kit/migration_inventory.rb +345 -0
- data/lib/nitro_kit/upgrade_smoke_test.rb +403 -0
- data/lib/nitro_kit/version.rb +1 -1
- data/lib/nitro_kit.rb +5 -42
- data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +42 -0
- data/lib/tasks/nitro_kit_tasks.rake +79 -4
- data/plugins/nitro-kit/.codex-plugin/plugin.json +28 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +67 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +60 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +62 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +4 -0
- data/src/stylesheets/nitro_kit/components/accordion.css +119 -0
- data/src/stylesheets/nitro_kit/components/alert.css +82 -0
- data/src/stylesheets/nitro_kit/components/app_navigation.css +156 -0
- data/src/stylesheets/nitro_kit/components/app_shell.css +605 -0
- data/src/stylesheets/nitro_kit/components/appearance_picker.css +117 -0
- data/src/stylesheets/nitro_kit/components/auth_shell.css +8 -0
- data/src/stylesheets/nitro_kit/components/avatar.css +81 -0
- data/src/stylesheets/nitro_kit/components/avatar_stack.css +80 -0
- data/src/stylesheets/nitro_kit/components/badge.css +76 -0
- data/src/stylesheets/nitro_kit/components/button.css +283 -0
- data/src/stylesheets/nitro_kit/components/button_group.css +53 -0
- data/src/stylesheets/nitro_kit/components/button_to.css +6 -0
- data/src/stylesheets/nitro_kit/components/card.css +70 -0
- data/src/stylesheets/nitro_kit/components/checkbox.css +160 -0
- data/src/stylesheets/nitro_kit/components/checkbox_group.css +61 -0
- data/src/stylesheets/nitro_kit/components/combobox.css +148 -0
- data/src/stylesheets/nitro_kit/components/command_palette.css +255 -0
- data/src/stylesheets/nitro_kit/components/container.css +25 -0
- data/src/stylesheets/nitro_kit/components/control_group.css +149 -0
- data/src/stylesheets/nitro_kit/components/danger_zone.css +54 -0
- data/src/stylesheets/nitro_kit/components/data_section.css +40 -0
- data/src/stylesheets/nitro_kit/components/details_table.css +53 -0
- data/src/stylesheets/nitro_kit/components/dialog.css +80 -0
- data/src/stylesheets/nitro_kit/components/dropdown.css +152 -0
- data/src/stylesheets/nitro_kit/components/dropzone.css +198 -0
- data/src/stylesheets/nitro_kit/components/empty_state.css +53 -0
- data/src/stylesheets/nitro_kit/components/field.css +79 -0
- data/src/stylesheets/nitro_kit/components/field_group.css +6 -0
- data/src/stylesheets/nitro_kit/components/fieldset.css +28 -0
- data/src/stylesheets/nitro_kit/components/flex.css +455 -0
- data/src/stylesheets/nitro_kit/components/form_section.css +71 -0
- data/src/stylesheets/nitro_kit/components/grid.css +308 -0
- data/src/stylesheets/nitro_kit/components/icon.css +26 -0
- data/src/stylesheets/nitro_kit/components/input.css +80 -0
- data/src/stylesheets/nitro_kit/components/label.css +9 -0
- data/src/stylesheets/nitro_kit/components/layout.css +440 -0
- data/src/stylesheets/nitro_kit/components/page_header.css +61 -0
- data/src/stylesheets/nitro_kit/components/pagination.css +119 -0
- data/src/stylesheets/nitro_kit/components/pagination_bar.css +41 -0
- data/src/stylesheets/nitro_kit/components/palette.css +113 -0
- data/src/stylesheets/nitro_kit/components/progressive_image.css +139 -0
- data/src/stylesheets/nitro_kit/components/radio_button.css +127 -0
- data/src/stylesheets/nitro_kit/components/radio_button_group.css +143 -0
- data/src/stylesheets/nitro_kit/components/rich_text_area.css +21 -0
- data/src/stylesheets/nitro_kit/components/select.css +70 -0
- data/src/stylesheets/nitro_kit/components/settings_layout.css +100 -0
- data/src/stylesheets/nitro_kit/components/sheet.css +100 -0
- data/src/stylesheets/nitro_kit/components/stat_grid.css +51 -0
- data/src/stylesheets/nitro_kit/components/switch.css +152 -0
- data/src/stylesheets/nitro_kit/components/table.css +116 -0
- data/src/stylesheets/nitro_kit/components/tabs.css +138 -0
- data/src/stylesheets/nitro_kit/components/textarea.css +39 -0
- data/src/stylesheets/nitro_kit/components/toast.css +131 -0
- data/src/stylesheets/nitro_kit/components/toolbar.css +78 -0
- data/src/stylesheets/nitro_kit/components/tooltip.css +124 -0
- data/src/stylesheets/nitro_kit/components/typeset.css +191 -0
- data/src/stylesheets/nitro_kit/layers.css +7 -0
- data/src/stylesheets/nitro_kit/reset.css +192 -0
- data/src/stylesheets/nitro_kit/tokens.css +198 -0
- metadata +182 -51
- data/Rakefile +0 -8
- data/app/assets/tailwind/application.css +0 -49
- data/app/components/nitro_kit/datepicker.rb +0 -9
- data/app/helpers/nitro_kit/accordion_helper.rb +0 -9
- data/app/helpers/nitro_kit/alert_helper.rb +0 -11
- data/app/helpers/nitro_kit/avatar_helper.rb +0 -13
- data/app/helpers/nitro_kit/badge_helper.rb +0 -13
- data/app/helpers/nitro_kit/button_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/button_helper.rb +0 -49
- data/app/helpers/nitro_kit/card_helper.rb +0 -9
- data/app/helpers/nitro_kit/checkbox_helper.rb +0 -50
- data/app/helpers/nitro_kit/combobox_helper.rb +0 -9
- data/app/helpers/nitro_kit/datepicker_helper.rb +0 -9
- data/app/helpers/nitro_kit/dialog_helper.rb +0 -9
- data/app/helpers/nitro_kit/dropdown_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_helper.rb +0 -9
- data/app/helpers/nitro_kit/fieldset_helper.rb +0 -9
- data/app/helpers/nitro_kit/form_helper.rb +0 -13
- data/app/helpers/nitro_kit/icon_helper.rb +0 -9
- data/app/helpers/nitro_kit/input_helper.rb +0 -37
- data/app/helpers/nitro_kit/label_helper.rb +0 -19
- data/app/helpers/nitro_kit/pagination_helper.rb +0 -46
- data/app/helpers/nitro_kit/radio_button_helper.rb +0 -23
- data/app/helpers/nitro_kit/select_helper.rb +0 -24
- data/app/helpers/nitro_kit/switch_helper.rb +0 -9
- data/app/helpers/nitro_kit/table_helper.rb +0 -9
- data/app/helpers/nitro_kit/tabs_helper.rb +0 -9
- data/app/helpers/nitro_kit/textarea_helper.rb +0 -9
- data/app/helpers/nitro_kit/toast_helper.rb +0 -25
- data/app/helpers/nitro_kit/tooltip_helper.rb +0 -9
- data/app/javascript/controllers/nk/accordion_controller.js +0 -18
- data/app/javascript/controllers/nk/datepicker_controller.js +0 -5
- data/app/javascript/controllers/nk/switch_controller.js +0 -32
- data/lib/generators/nitro_kit/component_generator.rb +0 -84
- data/lib/nitro_kit/schema_builder.rb +0 -129
- data/lib/nitro_kit/variants.rb +0 -21
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { Controller } from "@hotwired/stimulus";
|
|
2
2
|
|
|
3
3
|
export default class extends Controller {
|
|
4
|
-
static targets = ["
|
|
4
|
+
static targets = ["panel"];
|
|
5
|
+
static values = { dismissible: Boolean };
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
this.
|
|
8
|
-
|
|
7
|
+
dismiss(event) {
|
|
8
|
+
if (!this.dismissibleValue || event.target !== this.panelTarget) return;
|
|
9
|
+
|
|
10
|
+
const rect = this.panelTarget.getBoundingClientRect();
|
|
11
|
+
const inside =
|
|
12
|
+
event.clientX >= rect.left &&
|
|
13
|
+
event.clientX <= rect.right &&
|
|
14
|
+
event.clientY >= rect.top &&
|
|
15
|
+
event.clientY <= rect.bottom;
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
this.dialogTarget.close();
|
|
17
|
+
if (!inside) this.panelTarget.close();
|
|
12
18
|
}
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
this.close();
|
|
17
|
-
}
|
|
20
|
+
cancel(event) {
|
|
21
|
+
if (!this.dismissibleValue) event.preventDefault();
|
|
18
22
|
}
|
|
19
23
|
}
|
|
@@ -1,78 +1,126 @@
|
|
|
1
1
|
import { Controller } from "@hotwired/stimulus";
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
shift,
|
|
7
|
-
autoUpdate,
|
|
8
|
-
} from "@floating-ui/dom";
|
|
3
|
+
observeOverlayPosition,
|
|
4
|
+
positionOverlay,
|
|
5
|
+
} from "controllers/nk/overlay_position";
|
|
9
6
|
|
|
10
7
|
export default class extends Controller {
|
|
11
|
-
static targets = ["trigger", "content"];
|
|
12
|
-
static values = {
|
|
13
|
-
placement: { type: String, default: "bottom" },
|
|
14
|
-
};
|
|
8
|
+
static targets = ["trigger", "content", "item"];
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
this.
|
|
10
|
+
disconnect() {
|
|
11
|
+
this.stopPositioning?.();
|
|
18
12
|
}
|
|
19
13
|
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
openFromKeyboard(event) {
|
|
15
|
+
if (!["ArrowDown", "ArrowUp"].includes(event.key)) return;
|
|
16
|
+
if (this.triggerTarget.disabled) return;
|
|
17
|
+
|
|
18
|
+
event.preventDefault();
|
|
19
|
+
this.focusLast = event.key === "ArrowUp";
|
|
20
|
+
|
|
21
|
+
if (this.contentTarget.matches(":popover-open")) {
|
|
22
|
+
this.focusInitialItem();
|
|
23
|
+
} else {
|
|
24
|
+
this.contentTarget.showPopover();
|
|
25
|
+
}
|
|
22
26
|
}
|
|
23
27
|
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
rememberFocus(event) {
|
|
29
|
+
if (event.newState === "open") return;
|
|
30
|
+
|
|
31
|
+
this.hadFocus =
|
|
32
|
+
this.restoreOnClose !== false &&
|
|
33
|
+
this.contentTarget.contains(document.activeElement);
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
this.
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.triggerTarget.setAttribute("aria-expanded", "true");
|
|
40
|
-
this.contentTarget.setAttribute("aria-hidden", "false");
|
|
41
|
-
|
|
42
|
-
document.addEventListener("click", this.clickOutside);
|
|
43
|
-
|
|
44
|
-
this.clearAutoUpdate = autoUpdate(
|
|
45
|
-
this.triggerTarget,
|
|
46
|
-
this.contentTarget,
|
|
47
|
-
this.updatePosition,
|
|
48
|
-
);
|
|
49
|
-
};
|
|
36
|
+
focusOpened(event) {
|
|
37
|
+
if (event.newState === "open") {
|
|
38
|
+
this.startPositioning();
|
|
39
|
+
this.focusInitialItem();
|
|
40
|
+
} else {
|
|
41
|
+
this.stopPositioning?.();
|
|
42
|
+
this.stopPositioning = null;
|
|
43
|
+
this.focusLast = false;
|
|
44
|
+
this.restoreFocus();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
this.
|
|
53
|
-
this.contentTarget.setAttribute("aria-hidden", "true");
|
|
48
|
+
restoreFocus() {
|
|
49
|
+
if (!this.hadFocus) return;
|
|
54
50
|
|
|
55
|
-
|
|
51
|
+
this.hadFocus = false;
|
|
52
|
+
this.triggerTarget.focus();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
startPositioning() {
|
|
56
|
+
this.stopPositioning?.();
|
|
57
|
+
const update = () =>
|
|
58
|
+
positionOverlay(
|
|
59
|
+
this.triggerTarget,
|
|
60
|
+
this.contentTarget,
|
|
61
|
+
this.element.dataset.placement,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
update();
|
|
65
|
+
this.stopPositioning = observeOverlayPosition(update);
|
|
66
|
+
}
|
|
56
67
|
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
navigate(event) {
|
|
69
|
+
switch (event.key) {
|
|
70
|
+
case "Escape":
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
this.hide();
|
|
73
|
+
return;
|
|
74
|
+
case "Tab":
|
|
75
|
+
this.hide({ restoreFocus: false });
|
|
76
|
+
return;
|
|
59
77
|
}
|
|
60
|
-
};
|
|
61
78
|
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
79
|
+
const items = this.enabledItems;
|
|
80
|
+
if (items.length === 0) return;
|
|
81
|
+
|
|
82
|
+
const currentIndex = items.indexOf(document.activeElement);
|
|
83
|
+
const nextIndex = {
|
|
84
|
+
ArrowDown: (currentIndex + 1) % items.length,
|
|
85
|
+
ArrowUp: (currentIndex - 1 + items.length) % items.length,
|
|
86
|
+
Home: 0,
|
|
87
|
+
End: items.length - 1,
|
|
88
|
+
}[event.key];
|
|
89
|
+
|
|
90
|
+
if (nextIndex === undefined) return;
|
|
91
|
+
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
items[nextIndex].focus();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
select() {
|
|
97
|
+
this.hide();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
focusInitialItem() {
|
|
101
|
+
const item = this.focusLast
|
|
102
|
+
? this.enabledItems.at(-1)
|
|
103
|
+
: this.enabledItems[0];
|
|
104
|
+
|
|
105
|
+
this.focusLast = false;
|
|
106
|
+
if (item) {
|
|
107
|
+
item.focus();
|
|
65
108
|
} else {
|
|
66
|
-
this.
|
|
109
|
+
this.contentTarget.focus();
|
|
67
110
|
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
) {
|
|
75
|
-
this.close();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
hide({ restoreFocus = true } = {}) {
|
|
114
|
+
this.restoreOnClose = restoreFocus;
|
|
115
|
+
if (this.contentTarget.matches(":popover-open")) {
|
|
116
|
+
this.contentTarget.hidePopover();
|
|
76
117
|
}
|
|
77
|
-
|
|
118
|
+
this.restoreOnClose = true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get enabledItems() {
|
|
122
|
+
return this.itemTargets.filter(
|
|
123
|
+
(item) => !item.disabled && item.getAttribute("aria-disabled") !== "true",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
78
126
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { DirectUpload } from "@rails/activestorage";
|
|
2
|
+
|
|
3
|
+
export default class DirectUploadSession {
|
|
4
|
+
constructor(file, url, { onProgress, onComplete }) {
|
|
5
|
+
this.file = file;
|
|
6
|
+
this.url = url;
|
|
7
|
+
this.onProgress = onProgress;
|
|
8
|
+
this.onComplete = onComplete;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
start() {
|
|
12
|
+
this.upload = new DirectUpload(this.file, this.url, {
|
|
13
|
+
directUploadWillCreateBlobWithXHR: (request) => {
|
|
14
|
+
this.bindRequest(request);
|
|
15
|
+
},
|
|
16
|
+
directUploadWillStoreFileWithXHR: (request) => {
|
|
17
|
+
this.bindRequest(request, { tracksProgress: true });
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
this.upload.create((error, attributes) => {
|
|
22
|
+
if (this.cancelled) return;
|
|
23
|
+
|
|
24
|
+
this.releaseRequest();
|
|
25
|
+
this.onComplete(error, attributes);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
cancel() {
|
|
30
|
+
this.cancelled = true;
|
|
31
|
+
this.request?.abort();
|
|
32
|
+
this.releaseRequest();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
bindRequest(request, { tracksProgress = false } = {}) {
|
|
36
|
+
if (this.cancelled) {
|
|
37
|
+
queueMicrotask(() => request.abort());
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.releaseRequest();
|
|
42
|
+
this.request = request;
|
|
43
|
+
|
|
44
|
+
if (tracksProgress) {
|
|
45
|
+
this.progressListener = (event) => this.onProgress(event);
|
|
46
|
+
request.upload.addEventListener("progress", this.progressListener);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
releaseRequest() {
|
|
51
|
+
if (this.request && this.progressListener) {
|
|
52
|
+
this.request.upload.removeEventListener(
|
|
53
|
+
"progress",
|
|
54
|
+
this.progressListener,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.request = null;
|
|
59
|
+
this.progressListener = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Every reason is a caller-supplied template so the locale layer, not this
|
|
2
|
+
// module, owns user-facing text.
|
|
3
|
+
export function interpolate(template, replacements = {}) {
|
|
4
|
+
return template.replace(/%\{(\w+)\}/g, (match, name) =>
|
|
5
|
+
Object.hasOwn(replacements, name) ? String(replacements[name]) : match,
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function validateFiles(files, { maxFiles, maxBytes, accept, messages }) {
|
|
10
|
+
let acceptedCount = 0;
|
|
11
|
+
const selections = files.map((file) => {
|
|
12
|
+
const rejection = fileRejection(file, { maxBytes, accept, messages });
|
|
13
|
+
if (rejection) return rejection;
|
|
14
|
+
|
|
15
|
+
if (acceptedCount >= maxFiles) {
|
|
16
|
+
return {
|
|
17
|
+
file,
|
|
18
|
+
reason: interpolate(
|
|
19
|
+
maxFiles === 1 ? messages.maxFilesOne : messages.maxFilesOther,
|
|
20
|
+
{ count: maxFiles },
|
|
21
|
+
),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
acceptedCount += 1;
|
|
26
|
+
return { file, reason: null };
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
accepted: selections
|
|
31
|
+
.filter(({ reason }) => !reason)
|
|
32
|
+
.map(({ file }) => file),
|
|
33
|
+
rejected: selections.filter(({ reason }) => reason),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function rejectionMessage(rejected) {
|
|
38
|
+
return Array.from(new Set(rejected.map(({ reason }) => reason))).join(" ");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function formatBytes(bytes) {
|
|
42
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
43
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
44
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fileRejection(file, { maxBytes, accept, messages }) {
|
|
48
|
+
if (maxBytes && file.size > maxBytes) {
|
|
49
|
+
return {
|
|
50
|
+
file,
|
|
51
|
+
reason: interpolate(messages.tooLarge, {
|
|
52
|
+
name: file.name,
|
|
53
|
+
size: formatBytes(maxBytes),
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (accept && !accepts(file, accept)) {
|
|
58
|
+
return {
|
|
59
|
+
file,
|
|
60
|
+
reason: interpolate(messages.notAccepted, { name: file.name }),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function accepts(file, accept) {
|
|
68
|
+
return accept.split(",").some((rawRule) => {
|
|
69
|
+
const rule = rawRule.trim().toLowerCase();
|
|
70
|
+
const type = file.type.toLowerCase();
|
|
71
|
+
const name = file.name.toLowerCase();
|
|
72
|
+
|
|
73
|
+
if (rule.startsWith(".")) return name.endsWith(rule);
|
|
74
|
+
if (rule.endsWith("/*")) return type.startsWith(rule.slice(0, -1));
|
|
75
|
+
return type === rule;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const submitControlSelector = "button[type='submit'], input[type='submit']";
|
|
2
|
+
const locks = new WeakMap();
|
|
3
|
+
|
|
4
|
+
export function acquireFormSubmitLock(form, owner) {
|
|
5
|
+
if (!form) return;
|
|
6
|
+
|
|
7
|
+
let lock = locks.get(form);
|
|
8
|
+
if (!lock) {
|
|
9
|
+
lock = { owners: new Set(), controls: new Map() };
|
|
10
|
+
locks.set(form, lock);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
form.querySelectorAll(submitControlSelector).forEach((control) => {
|
|
14
|
+
if (!lock.controls.has(control)) {
|
|
15
|
+
lock.controls.set(control, control.disabled);
|
|
16
|
+
}
|
|
17
|
+
control.disabled = true;
|
|
18
|
+
});
|
|
19
|
+
lock.owners.add(owner);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function releaseFormSubmitLock(form, owner) {
|
|
23
|
+
const lock = form && locks.get(form);
|
|
24
|
+
if (!lock || !lock.owners.delete(owner) || lock.owners.size > 0) return;
|
|
25
|
+
|
|
26
|
+
lock.controls.forEach((initiallyDisabled, control) => {
|
|
27
|
+
if (control.isConnected) control.disabled = initiallyDisabled;
|
|
28
|
+
});
|
|
29
|
+
locks.delete(form);
|
|
30
|
+
}
|