@rogieking/figui3 1.0.98 → 1.1.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/example.html +3 -1
- package/fig.css +1 -0
- package/fig.js +98 -14
- package/package.json +1 -1
package/example.html
CHANGED
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
<label>Opacity slider</label>
|
|
81
81
|
<fig-slider type="opacity"
|
|
82
82
|
variant="minimal"
|
|
83
|
-
value="
|
|
83
|
+
value="75"
|
|
84
84
|
color="#ff0000"
|
|
85
85
|
units="%"
|
|
86
86
|
text="true"></fig-slider>
|
|
@@ -424,6 +424,8 @@
|
|
|
424
424
|
</fig-combo-input>
|
|
425
425
|
<br /><br />
|
|
426
426
|
<fig-input-text type="number"
|
|
427
|
+
min="0"
|
|
428
|
+
max="360"
|
|
427
429
|
value="90">
|
|
428
430
|
<span slot="append">°</span>
|
|
429
431
|
</fig-input-text>
|
package/fig.css
CHANGED
package/fig.js
CHANGED
|
@@ -842,9 +842,18 @@ class FigSlider extends HTMLElement {
|
|
|
842
842
|
window.customElements.define("fig-slider", FigSlider);
|
|
843
843
|
|
|
844
844
|
class FigInputText extends HTMLElement {
|
|
845
|
+
#boundMouseMove;
|
|
846
|
+
#boundMouseUp;
|
|
847
|
+
#boundMouseDown;
|
|
848
|
+
|
|
845
849
|
constructor() {
|
|
846
850
|
super();
|
|
851
|
+
// Pre-bind the event handlers once
|
|
852
|
+
this.#boundMouseMove = this.#handleMouseMove.bind(this);
|
|
853
|
+
this.#boundMouseUp = this.#handleMouseUp.bind(this);
|
|
854
|
+
this.#boundMouseDown = this.#handleMouseDown.bind(this);
|
|
847
855
|
}
|
|
856
|
+
|
|
848
857
|
connectedCallback() {
|
|
849
858
|
this.multiline = this.hasAttribute("multiline") || false;
|
|
850
859
|
this.value = this.getAttribute("value") || "";
|
|
@@ -904,6 +913,7 @@ class FigInputText extends HTMLElement {
|
|
|
904
913
|
if (this.getAttribute("step")) {
|
|
905
914
|
this.input.setAttribute("step", this.#transformNumber(this.step));
|
|
906
915
|
}
|
|
916
|
+
this.addEventListener("pointerdown", this.#boundMouseDown);
|
|
907
917
|
}
|
|
908
918
|
this.input.addEventListener("input", this.#handleInput.bind(this));
|
|
909
919
|
});
|
|
@@ -912,16 +922,58 @@ class FigInputText extends HTMLElement {
|
|
|
912
922
|
this.input.focus();
|
|
913
923
|
}
|
|
914
924
|
#transformNumber(value) {
|
|
915
|
-
return value * (this.transform || 1);
|
|
925
|
+
return Number(value) * (this.transform || 1);
|
|
916
926
|
}
|
|
917
927
|
#handleInput(e) {
|
|
918
928
|
let value = e.target.value;
|
|
919
929
|
if (this.type === "number") {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
930
|
+
value = this.#sanitizeInput(value);
|
|
931
|
+
value = value / (this.transform || 1);
|
|
932
|
+
}
|
|
933
|
+
this.setAttribute("value", value);
|
|
934
|
+
}
|
|
935
|
+
#handleMouseMove(e) {
|
|
936
|
+
if (e.altKey) {
|
|
937
|
+
const step = (this.step || 1) * e.movementX;
|
|
938
|
+
const value = this.#sanitizeInput(Number(this.value) + step).toFixed(2);
|
|
939
|
+
this.setAttribute("value", value);
|
|
923
940
|
}
|
|
924
941
|
}
|
|
942
|
+
#handleMouseDown(e) {
|
|
943
|
+
if (e.altKey) {
|
|
944
|
+
this.input.style.cursor =
|
|
945
|
+
this.style.cursor =
|
|
946
|
+
document.body.style.cursor =
|
|
947
|
+
"ew-resize";
|
|
948
|
+
this.style.userSelect = "none";
|
|
949
|
+
// Use the pre-bound handlers
|
|
950
|
+
window.addEventListener("pointermove", this.#boundMouseMove);
|
|
951
|
+
window.addEventListener("pointerup", this.#boundMouseUp);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
#handleMouseUp(e) {
|
|
955
|
+
this.input.style.cursor =
|
|
956
|
+
this.style.cursor =
|
|
957
|
+
document.body.style.cursor =
|
|
958
|
+
"";
|
|
959
|
+
this.style.userSelect = "all";
|
|
960
|
+
// Remove the pre-bound handlers
|
|
961
|
+
window.removeEventListener("pointermove", this.#boundMouseMove);
|
|
962
|
+
window.removeEventListener("pointerup", this.#boundMouseUp);
|
|
963
|
+
}
|
|
964
|
+
#sanitizeInput(value) {
|
|
965
|
+
let sanitized = value;
|
|
966
|
+
if (this.type === "number") {
|
|
967
|
+
sanitized = Number(sanitized);
|
|
968
|
+
if (typeof this.min === "number") {
|
|
969
|
+
sanitized = Math.max(this.min, sanitized);
|
|
970
|
+
}
|
|
971
|
+
if (typeof this.max === "number") {
|
|
972
|
+
sanitized = Math.min(this.max, sanitized);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
return sanitized;
|
|
976
|
+
}
|
|
925
977
|
|
|
926
978
|
static get observedAttributes() {
|
|
927
979
|
return [
|
|
@@ -941,7 +993,9 @@ class FigInputText extends HTMLElement {
|
|
|
941
993
|
if (this.input) {
|
|
942
994
|
switch (name) {
|
|
943
995
|
case "disabled":
|
|
944
|
-
this.disabled = this.input.disabled =
|
|
996
|
+
this.disabled = this.input.disabled =
|
|
997
|
+
newValue === "true" ||
|
|
998
|
+
(newValue === undefined && newValue !== null);
|
|
945
999
|
break;
|
|
946
1000
|
case "transform":
|
|
947
1001
|
if (this.type === "number") {
|
|
@@ -952,16 +1006,27 @@ class FigInputText extends HTMLElement {
|
|
|
952
1006
|
this.value = this.#transformNumber(this.value);
|
|
953
1007
|
}
|
|
954
1008
|
break;
|
|
955
|
-
|
|
1009
|
+
case "value":
|
|
956
1010
|
let value = newValue;
|
|
957
1011
|
if (this.type === "number") {
|
|
958
|
-
|
|
1012
|
+
let sanitized = this.#sanitizeInput(value);
|
|
1013
|
+
this.value = sanitized;
|
|
1014
|
+
this.input.value = this.#transformNumber(sanitized);
|
|
959
1015
|
}
|
|
960
|
-
this
|
|
1016
|
+
this.value = value;
|
|
1017
|
+
this.dispatchEvent(new CustomEvent("input", { bubbles: true }));
|
|
1018
|
+
break;
|
|
1019
|
+
case "min":
|
|
1020
|
+
case "max":
|
|
1021
|
+
case "step":
|
|
1022
|
+
this[name] = this.input[name] = Number(newValue);
|
|
961
1023
|
if (this.input) {
|
|
962
|
-
this.input.setAttribute(name,
|
|
1024
|
+
this.input.setAttribute(name, newValue);
|
|
963
1025
|
}
|
|
964
1026
|
break;
|
|
1027
|
+
default:
|
|
1028
|
+
this[name] = this.input[name] = value;
|
|
1029
|
+
break;
|
|
965
1030
|
}
|
|
966
1031
|
}
|
|
967
1032
|
}
|
|
@@ -1064,7 +1129,7 @@ class FigInputColor extends HTMLElement {
|
|
|
1064
1129
|
);
|
|
1065
1130
|
let html = ``;
|
|
1066
1131
|
if (this.getAttribute("text")) {
|
|
1067
|
-
let label = `<fig-input-text placeholder="Text" value="${this.getAttribute(
|
|
1132
|
+
let label = `<fig-input-text type="text" placeholder="Text" value="${this.getAttribute(
|
|
1068
1133
|
"value"
|
|
1069
1134
|
)}"></fig-input-text>`;
|
|
1070
1135
|
if (this.getAttribute("alpha") === "true") {
|
|
@@ -1092,8 +1157,8 @@ class FigInputColor extends HTMLElement {
|
|
|
1092
1157
|
|
|
1093
1158
|
requestAnimationFrame(() => {
|
|
1094
1159
|
this.#swatch = this.querySelector("input[type=color]");
|
|
1095
|
-
this.textInput = this.querySelector("input[type=
|
|
1096
|
-
this.#alphaInput = this.querySelector("input[type=number]");
|
|
1160
|
+
this.textInput = this.querySelector("fig-input-text:not([type=number])");
|
|
1161
|
+
this.#alphaInput = this.querySelector("fig-input-text[type=number]");
|
|
1097
1162
|
|
|
1098
1163
|
this.#swatch.disabled = this.hasAttribute("disabled");
|
|
1099
1164
|
this.#swatch.addEventListener("input", this.handleInput.bind(this));
|
|
@@ -1103,6 +1168,10 @@ class FigInputColor extends HTMLElement {
|
|
|
1103
1168
|
this.#rgba,
|
|
1104
1169
|
1
|
|
1105
1170
|
);
|
|
1171
|
+
this.textInput.addEventListener(
|
|
1172
|
+
"input",
|
|
1173
|
+
this.#handleTextInput.bind(this)
|
|
1174
|
+
);
|
|
1106
1175
|
}
|
|
1107
1176
|
|
|
1108
1177
|
if (this.#alphaInput) {
|
|
@@ -1113,6 +1182,12 @@ class FigInputColor extends HTMLElement {
|
|
|
1113
1182
|
}
|
|
1114
1183
|
});
|
|
1115
1184
|
}
|
|
1185
|
+
#handleTextInput(event) {
|
|
1186
|
+
//do not propagate to onInput handler for web component
|
|
1187
|
+
event.stopPropagation();
|
|
1188
|
+
this.value = this.#swatch.value = this.textInput.value;
|
|
1189
|
+
this.setAttribute("value", this.value);
|
|
1190
|
+
}
|
|
1116
1191
|
handleAlphaInput(event) {
|
|
1117
1192
|
//do not propagate to onInput handler for web component
|
|
1118
1193
|
event.stopPropagation();
|
|
@@ -1173,7 +1248,16 @@ class FigInputColor extends HTMLElement {
|
|
|
1173
1248
|
}
|
|
1174
1249
|
|
|
1175
1250
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
1176
|
-
|
|
1251
|
+
switch (name) {
|
|
1252
|
+
case "value":
|
|
1253
|
+
if (this.textInput) {
|
|
1254
|
+
this.value = this.#swatch.value = this.textInput.value = newValue;
|
|
1255
|
+
} else {
|
|
1256
|
+
this.value = newValue;
|
|
1257
|
+
}
|
|
1258
|
+
this.dispatchEvent(new CustomEvent("input", { bubbles: true }));
|
|
1259
|
+
break;
|
|
1260
|
+
}
|
|
1177
1261
|
}
|
|
1178
1262
|
|
|
1179
1263
|
rgbAlphaToHex({ r, g, b }, a = 1) {
|
|
@@ -1461,7 +1545,7 @@ class FigChit extends HTMLElement {
|
|
|
1461
1545
|
this.src = this.getAttribute("src") || "";
|
|
1462
1546
|
this.value = this.getAttribute("value") || "";
|
|
1463
1547
|
this.size = this.getAttribute("size") || "small";
|
|
1464
|
-
this.disabled = this.getAttribute("disabled") === "true"
|
|
1548
|
+
this.disabled = this.getAttribute("disabled") === "true";
|
|
1465
1549
|
this.innerHTML = `<input type="color" value="${this.value}" />`;
|
|
1466
1550
|
this.#updateSrc(this.src);
|
|
1467
1551
|
|