svelte-dnd-action 0.9.16 → 0.9.17

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/index.js CHANGED
@@ -1054,6 +1054,54 @@
1054
1054
  resetScrolling$1();
1055
1055
  }
1056
1056
 
1057
+ /**
1058
+ * Fixes svelte issue when cloning node containing (or being) <select> which will loose it's value.
1059
+ * Since svelte manages select value internally.
1060
+ * @see https://github.com/sveltejs/svelte/issues/6717
1061
+ * @see https://github.com/isaacHagoel/svelte-dnd-action/issues/306
1062
+ *
1063
+ * @param {HTMLElement} el
1064
+ * @returns
1065
+ */
1066
+ function svelteNodeClone(el) {
1067
+ var cloned = el.cloneNode(true);
1068
+ var values = [];
1069
+ var elIsSelect = el.tagName === "SELECT";
1070
+ var selects = elIsSelect ? [el] : _toConsumableArray(el.querySelectorAll('select'));
1071
+
1072
+ var _iterator = _createForOfIteratorHelper(selects),
1073
+ _step;
1074
+
1075
+ try {
1076
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1077
+ var _select = _step.value;
1078
+ values.push(_select.value);
1079
+ }
1080
+ } catch (err) {
1081
+ _iterator.e(err);
1082
+ } finally {
1083
+ _iterator.f();
1084
+ }
1085
+
1086
+ if (selects.length <= 0) {
1087
+ return cloned;
1088
+ }
1089
+
1090
+ var clonedSelects = elIsSelect ? [cloned] : _toConsumableArray(cloned.querySelectorAll('select'));
1091
+
1092
+ for (var i = 0; i < clonedSelects.length; i++) {
1093
+ var select = clonedSelects[i];
1094
+ var value = values[i];
1095
+ var optionEl = select.querySelector("option[value=\"".concat(value, "\""));
1096
+
1097
+ if (optionEl) {
1098
+ optionEl.setAttribute('selected', true);
1099
+ }
1100
+ }
1101
+
1102
+ return cloned;
1103
+ }
1104
+
1057
1105
  var TRANSITION_DURATION_SECONDS = 0.2;
1058
1106
  /**
1059
1107
  * private helper function - creates a transition string for a property
@@ -1074,7 +1122,7 @@
1074
1122
 
1075
1123
  function createDraggedElementFrom(originalElement, positionCenterOnXY) {
1076
1124
  var rect = originalElement.getBoundingClientRect();
1077
- var draggedEl = originalElement.cloneNode(true);
1125
+ var draggedEl = svelteNodeClone(originalElement);
1078
1126
  copyStylesFromTo(originalElement, draggedEl);
1079
1127
  draggedEl.id = DRAGGED_ELEMENT_ID;
1080
1128
  draggedEl.style.position = "fixed";
package/dist/index.mjs CHANGED
@@ -1048,6 +1048,54 @@ function disarmWindowScroller() {
1048
1048
  resetScrolling$1();
1049
1049
  }
1050
1050
 
1051
+ /**
1052
+ * Fixes svelte issue when cloning node containing (or being) <select> which will loose it's value.
1053
+ * Since svelte manages select value internally.
1054
+ * @see https://github.com/sveltejs/svelte/issues/6717
1055
+ * @see https://github.com/isaacHagoel/svelte-dnd-action/issues/306
1056
+ *
1057
+ * @param {HTMLElement} el
1058
+ * @returns
1059
+ */
1060
+ function svelteNodeClone(el) {
1061
+ var cloned = el.cloneNode(true);
1062
+ var values = [];
1063
+ var elIsSelect = el.tagName === "SELECT";
1064
+ var selects = elIsSelect ? [el] : _toConsumableArray(el.querySelectorAll('select'));
1065
+
1066
+ var _iterator = _createForOfIteratorHelper(selects),
1067
+ _step;
1068
+
1069
+ try {
1070
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1071
+ var _select = _step.value;
1072
+ values.push(_select.value);
1073
+ }
1074
+ } catch (err) {
1075
+ _iterator.e(err);
1076
+ } finally {
1077
+ _iterator.f();
1078
+ }
1079
+
1080
+ if (selects.length <= 0) {
1081
+ return cloned;
1082
+ }
1083
+
1084
+ var clonedSelects = elIsSelect ? [cloned] : _toConsumableArray(cloned.querySelectorAll('select'));
1085
+
1086
+ for (var i = 0; i < clonedSelects.length; i++) {
1087
+ var select = clonedSelects[i];
1088
+ var value = values[i];
1089
+ var optionEl = select.querySelector("option[value=\"".concat(value, "\""));
1090
+
1091
+ if (optionEl) {
1092
+ optionEl.setAttribute('selected', true);
1093
+ }
1094
+ }
1095
+
1096
+ return cloned;
1097
+ }
1098
+
1051
1099
  var TRANSITION_DURATION_SECONDS = 0.2;
1052
1100
  /**
1053
1101
  * private helper function - creates a transition string for a property
@@ -1068,7 +1116,7 @@ function trs(property) {
1068
1116
 
1069
1117
  function createDraggedElementFrom(originalElement, positionCenterOnXY) {
1070
1118
  var rect = originalElement.getBoundingClientRect();
1071
- var draggedEl = originalElement.cloneNode(true);
1119
+ var draggedEl = svelteNodeClone(originalElement);
1072
1120
  copyStylesFromTo(originalElement, draggedEl);
1073
1121
  draggedEl.id = DRAGGED_ELEMENT_ID;
1074
1122
  draggedEl.style.position = "fixed";
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "dist"
41
41
  ],
42
42
  "description": "*An awesome drag and drop library for Svelte 3 (not using the browser's built-in dnd, thanks god): Rich animations, nested containers, touch support and more *",
43
- "version": "0.9.16",
43
+ "version": "0.9.17",
44
44
  "repository": {
45
45
  "type": "git",
46
46
  "url": "git+https://github.com/isaacHagoel/svelte-dnd-action.git"
@@ -1,5 +1,6 @@
1
1
  import {SHADOW_ELEMENT_ATTRIBUTE_NAME, DRAGGED_ELEMENT_ID} from "../constants";
2
2
  import {findCenter} from "./intersection";
3
+ import { svelteNodeClone } from "./svelteNodeClone";
3
4
 
4
5
  const TRANSITION_DURATION_SECONDS = 0.2;
5
6
 
@@ -19,7 +20,7 @@ function trs(property) {
19
20
  */
20
21
  export function createDraggedElementFrom(originalElement, positionCenterOnXY) {
21
22
  const rect = originalElement.getBoundingClientRect();
22
- const draggedEl = originalElement.cloneNode(true);
23
+ const draggedEl = svelteNodeClone(originalElement);
23
24
  copyStylesFromTo(originalElement, draggedEl);
24
25
  draggedEl.id = DRAGGED_ELEMENT_ID;
25
26
  draggedEl.style.position = "fixed";
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Fixes svelte issue when cloning node containing (or being) <select> which will loose it's value.
3
+ * Since svelte manages select value internally.
4
+ * @see https://github.com/sveltejs/svelte/issues/6717
5
+ * @see https://github.com/isaacHagoel/svelte-dnd-action/issues/306
6
+ *
7
+ * @param {HTMLElement} el
8
+ * @returns
9
+ */
10
+ export function svelteNodeClone(el) {
11
+ const cloned = el.cloneNode(true);
12
+
13
+ const values = [];
14
+ const elIsSelect = el.tagName === "SELECT";
15
+ const selects = elIsSelect ? [el] : [...el.querySelectorAll('select')];
16
+ for (const select of selects) {
17
+ values.push(select.value);
18
+ }
19
+
20
+ if (selects.length <= 0) {
21
+ return cloned;
22
+ }
23
+
24
+ const clonedSelects = elIsSelect ? [cloned] : [...cloned.querySelectorAll('select')];
25
+ for (let i = 0; i < clonedSelects.length; i++) {
26
+ const select = clonedSelects[i];
27
+ const value = values[i];
28
+ const optionEl = select.querySelector(`option[value="${value}"`);
29
+ if (optionEl) {
30
+ optionEl.setAttribute('selected', true);
31
+ }
32
+ }
33
+
34
+ return cloned;
35
+ }