@pie-lib/text-select 4.0.2-next.20261007003844 → 4.0.2-next.20261007021359
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/token-select/index.js +31 -30
- package/dist/utils.js +15 -14
- package/package.json +3 -2
|
@@ -9,22 +9,23 @@ import { noSelect as d } from "@pie-lib/style-utils";
|
|
|
9
9
|
import f from "debug";
|
|
10
10
|
import { renderToString as p } from "react-dom/server";
|
|
11
11
|
import m from "@pie-lib/translator";
|
|
12
|
+
import { sanitizeModelHtml as h } from "@pie-element/shared-utils";
|
|
12
13
|
//#region src/token-select/index.tsx
|
|
13
|
-
var { translator:
|
|
14
|
+
var { translator: g } = m, _ = f("@pie-lib:text-select:token-select"), v = i("div")(() => ({
|
|
14
15
|
backgroundColor: "none",
|
|
15
16
|
whiteSpace: "pre",
|
|
16
17
|
...d(),
|
|
17
18
|
"& p": { whiteSpace: "break-spaces" }
|
|
18
|
-
})),
|
|
19
|
+
})), y = i("div")(() => ({
|
|
19
20
|
display: "none",
|
|
20
21
|
position: "absolute",
|
|
21
22
|
visibility: "hidden",
|
|
22
23
|
pointerEvents: "none"
|
|
23
|
-
})),
|
|
24
|
+
})), b = (e = "") => e.replace(/ /gi, "\xA0"), x = (e = "") => b(e).replace(/<\/p>\s*<p[^>]*>/gi, "\n\n").replace(/<br\s*\/?>/gi, "\n").replace(/<\/?(table|tbody|tr|td|p)[^>]*>/gi, ""), S = {
|
|
24
25
|
correct: "selectText.correctAnswerSelected",
|
|
25
26
|
incorrect: "selectText.incorrectSelection",
|
|
26
27
|
missing: "selectText.correctAnswerNotSelected"
|
|
27
|
-
},
|
|
28
|
+
}, C = (e) => e.correct === void 0 ? e.isMissing ? "missing" : void 0 : e.correct ? "correct" : "incorrect", w = (e) => e.text === "\n\n" || e.text === "\n", T = "[data-indexkey][tabindex]", E = (e) => e ? Number(e.dataset.indexkey) : void 0, D = class extends n.Component {
|
|
28
29
|
static propTypes = {
|
|
29
30
|
tokens: r.arrayOf(r.shape(t)).isRequired,
|
|
30
31
|
className: r.string,
|
|
@@ -48,7 +49,7 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
48
49
|
selectedCount = () => this.props.tokens.filter((e) => e.selected).length;
|
|
49
50
|
canSelectMore = (e) => {
|
|
50
51
|
let { maxNoOfSelections: t } = this.props;
|
|
51
|
-
return t === 1 ? !0 : (
|
|
52
|
+
return t === 1 ? !0 : (_("[canSelectMore] maxNoOfSelections: ", t, "selectedCount: ", e), t <= 0 || isFinite(t) && e < t);
|
|
52
53
|
};
|
|
53
54
|
toggleToken = (t) => {
|
|
54
55
|
let n = (t.target.closest?.(`.${e.rootClassName}`))?.dataset?.indexkey;
|
|
@@ -72,7 +73,7 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
72
73
|
s.splice(e, 1, c), n(s);
|
|
73
74
|
} else {
|
|
74
75
|
if (o && a > 0 && this.selectedCount() >= a) {
|
|
75
|
-
|
|
76
|
+
_("skip toggle max reached");
|
|
76
77
|
return;
|
|
77
78
|
}
|
|
78
79
|
let t = {
|
|
@@ -83,28 +84,28 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
};
|
|
86
|
-
sequence = () => Array.from(this.containerRef.current?.querySelectorAll(
|
|
87
|
+
sequence = () => Array.from(this.containerRef.current?.querySelectorAll(T) || []);
|
|
87
88
|
focusedToken = () => {
|
|
88
89
|
let e = this.containerRef.current, t = e?.getRootNode().activeElement;
|
|
89
|
-
return t && e.contains(t) ? t.closest(
|
|
90
|
+
return t && e.contains(t) ? t.closest(T) : null;
|
|
90
91
|
};
|
|
91
92
|
syncTabStop = () => {
|
|
92
|
-
let e = this.sequence(), t = e.find((e) =>
|
|
93
|
+
let e = this.sequence(), t = e.find((e) => E(e) === this.activeIndex) || e.find((e) => e.getAttribute("aria-pressed") === "true") || e[0];
|
|
93
94
|
return e.forEach((e) => {
|
|
94
95
|
let n = e === t ? "0" : "-1";
|
|
95
96
|
e.getAttribute("tabindex") !== n && e.setAttribute("tabindex", n);
|
|
96
97
|
}), t;
|
|
97
98
|
};
|
|
98
99
|
onFocus = (e) => {
|
|
99
|
-
let t = e.target.closest?.(
|
|
100
|
-
t && (this.activeIndex =
|
|
100
|
+
let t = e.target.closest?.(T);
|
|
101
|
+
t && (this.activeIndex = E(t), this.syncTabStop());
|
|
101
102
|
};
|
|
102
103
|
onKeyDown = (e) => {
|
|
103
104
|
if (!this.operable() || e.altKey || e.ctrlKey || e.metaKey) return;
|
|
104
|
-
let t = e.target.closest?.(
|
|
105
|
+
let t = e.target.closest?.(T);
|
|
105
106
|
if (!t) return;
|
|
106
107
|
if (e.key === " " || e.key === "Enter") {
|
|
107
|
-
e.preventDefault(), !e.repeat && t.getAttribute("aria-disabled") !== "true" && (this.keyboardToggle = !0, this.toggleAt(
|
|
108
|
+
e.preventDefault(), !e.repeat && t.getAttribute("aria-disabled") !== "true" && (this.keyboardToggle = !0, this.toggleAt(E(t)));
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
let n = this.sequence(), r = n.indexOf(t), i = n.length - 1, a = {
|
|
@@ -122,56 +123,56 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
122
123
|
}
|
|
123
124
|
getSnapshotBeforeUpdate() {
|
|
124
125
|
let e = this.focusedToken();
|
|
125
|
-
return e ?
|
|
126
|
+
return e ? E(e) : null;
|
|
126
127
|
}
|
|
127
128
|
componentDidUpdate(e, t, n) {
|
|
128
129
|
let r = this.syncTabStop();
|
|
129
|
-
n == null || this.focusedToken() || (this.sequence().find((e) =>
|
|
130
|
+
n == null || this.focusedToken() || (this.sequence().find((e) => E(e) === n) || r)?.focus({
|
|
130
131
|
preventScroll: !0,
|
|
131
132
|
focusVisible: this.keyboardToggle
|
|
132
133
|
});
|
|
133
134
|
}
|
|
134
135
|
groupName = () => {
|
|
135
136
|
let { maxNoOfSelections: e, language: t } = this.props;
|
|
136
|
-
return e > 0 ?
|
|
137
|
+
return e > 0 ? g.t("selectText.textGroupWithLimit", {
|
|
137
138
|
lng: t,
|
|
138
139
|
max: e
|
|
139
|
-
}) :
|
|
140
|
+
}) : g.t("selectText.textGroup", { lng: t });
|
|
140
141
|
};
|
|
141
142
|
generateTokensInHtml = () => {
|
|
142
|
-
let { tokens: t, disabled: n, highlightChoices: r, animationsDisabled: i, language: a } = this.props, o = this.selectedCount(), c = this.operable(), l = {}, u = (e) => c && !
|
|
143
|
+
let { tokens: t, disabled: n, highlightChoices: r, animationsDisabled: i, language: a } = this.props, o = this.selectedCount(), c = this.operable(), l = {}, u = (e) => c && !w(e) && (e.selected || e.selectable), d = (t || []).filter(u).length, f = 0, m = (e, t) => {
|
|
143
144
|
let n = `${this.idPrefix}-${e}`;
|
|
144
145
|
return l[n] = t, n;
|
|
145
146
|
};
|
|
146
147
|
return {
|
|
147
148
|
html: (t || []).reduce((t, c, l) => {
|
|
148
|
-
let
|
|
149
|
+
let h = c.selected || c.selectable && this.canSelectMore(o), _ = c.correct !== void 0 && (c.selectable || c.selected);
|
|
149
150
|
if (c.text === "\n\n") return `${t}</p><p>`;
|
|
150
151
|
if (c.text === "\n") return `${t}<br>`;
|
|
151
|
-
if (u(c) ||
|
|
152
|
+
if (u(c) || h && !n || _ || c.selected || c.isMissing || i && c.predefined) {
|
|
152
153
|
let o = u(c), _;
|
|
153
|
-
if (o) f += 1, _ = m(`position-${f}`,
|
|
154
|
+
if (o) f += 1, _ = m(`position-${f}`, g.t("selectText.tokenPosition", {
|
|
154
155
|
lng: a,
|
|
155
156
|
index: f,
|
|
156
157
|
total: d
|
|
157
158
|
}));
|
|
158
159
|
else {
|
|
159
|
-
let e =
|
|
160
|
-
e && (_ = m(e,
|
|
160
|
+
let e = C(c);
|
|
161
|
+
e && (_ = m(e, g.t(S[e], { lng: a })));
|
|
161
162
|
}
|
|
162
163
|
return t + p(/* @__PURE__ */ s(e, {
|
|
163
164
|
disabled: n,
|
|
164
165
|
index: l,
|
|
165
166
|
...c,
|
|
166
|
-
text:
|
|
167
|
-
selectable:
|
|
167
|
+
text: x(c.text),
|
|
168
|
+
selectable: h,
|
|
168
169
|
highlight: r,
|
|
169
170
|
animationsDisabled: i,
|
|
170
171
|
operable: o,
|
|
171
172
|
describedBy: _
|
|
172
173
|
}, l));
|
|
173
174
|
}
|
|
174
|
-
return t +
|
|
175
|
+
return t + b(c.text);
|
|
175
176
|
}, "<p>") + "</p>",
|
|
176
177
|
descriptions: l
|
|
177
178
|
};
|
|
@@ -179,7 +180,7 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
179
180
|
render() {
|
|
180
181
|
let { className: t } = this.props, { html: n, descriptions: r } = this.generateTokensInHtml();
|
|
181
182
|
return /* @__PURE__ */ c(o, { children: [
|
|
182
|
-
/* @__PURE__ */ c(
|
|
183
|
+
/* @__PURE__ */ c(y, {
|
|
183
184
|
"aria-hidden": "true",
|
|
184
185
|
children: [
|
|
185
186
|
/* @__PURE__ */ s(e, {
|
|
@@ -255,7 +256,7 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
255
256
|
})
|
|
256
257
|
]
|
|
257
258
|
}),
|
|
258
|
-
/* @__PURE__ */ s(
|
|
259
|
+
/* @__PURE__ */ s(v, {
|
|
259
260
|
ref: this.containerRef,
|
|
260
261
|
className: t,
|
|
261
262
|
role: "group",
|
|
@@ -263,7 +264,7 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
263
264
|
onClick: this.toggleToken,
|
|
264
265
|
onKeyDown: this.onKeyDown,
|
|
265
266
|
onFocus: this.onFocus,
|
|
266
|
-
dangerouslySetInnerHTML: { __html: n }
|
|
267
|
+
dangerouslySetInnerHTML: { __html: h(n) }
|
|
267
268
|
}),
|
|
268
269
|
Object.keys(r).length > 0 && /* @__PURE__ */ s("div", {
|
|
269
270
|
hidden: !0,
|
|
@@ -276,4 +277,4 @@ var { translator: h } = m, g = f("@pie-lib:text-select:token-select"), _ = i("di
|
|
|
276
277
|
}
|
|
277
278
|
};
|
|
278
279
|
//#endregion
|
|
279
|
-
export {
|
|
280
|
+
export { D as TokenSelect, D as default, t as TokenTypes };
|
package/dist/utils.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
+
import { sanitizeModelHtml as e } from "@pie-element/shared-utils";
|
|
1
2
|
//#region src/utils.tsx
|
|
2
|
-
var
|
|
3
|
-
let
|
|
4
|
-
return
|
|
5
|
-
},
|
|
3
|
+
var t = (t = "") => {
|
|
4
|
+
let n = document.createElement("div");
|
|
5
|
+
return n.innerHTML = e(t).trim(), n;
|
|
6
|
+
}, n = (e) => {
|
|
6
7
|
e.querySelectorAll("br").forEach((e) => e.replaceWith("\n")), e.innerHTML = e.innerHTML.replace(/\n\n/g, "\n");
|
|
7
|
-
},
|
|
8
|
-
let t = e.querySelectorAll("p"),
|
|
8
|
+
}, r = (e, t) => t ? e.innerHTML : `${e.innerHTML}\n\n`, i = (e) => {
|
|
9
|
+
let t = e.querySelectorAll("p"), n = "";
|
|
9
10
|
return t.forEach((e, i) => {
|
|
10
|
-
|
|
11
|
-
}),
|
|
12
|
-
},
|
|
13
|
-
let
|
|
14
|
-
if (
|
|
11
|
+
n += r(e, i === t.length - 1);
|
|
12
|
+
}), n || null;
|
|
13
|
+
}, a = (e) => {
|
|
14
|
+
let r = t(e), a = Array.from(r.querySelectorAll("*"));
|
|
15
|
+
if (r.querySelectorAll("p").length === 0) {
|
|
15
16
|
let e = document.createElement("div");
|
|
16
|
-
e.innerHTML = `<p>${
|
|
17
|
+
e.innerHTML = `<p>${r.innerHTML}</p>`, r = e;
|
|
17
18
|
}
|
|
18
|
-
return a.length === 0 ?
|
|
19
|
+
return a.length === 0 ? e : (n(r), i(r));
|
|
19
20
|
};
|
|
20
21
|
//#endregion
|
|
21
|
-
export {
|
|
22
|
+
export { n as parseBrs, r as parseParagraph, i as parseParagraphs, a as prepareText };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-lib/text-select",
|
|
3
|
-
"version": "4.0.2-next.
|
|
3
|
+
"version": "4.0.2-next.20261007021359",
|
|
4
4
|
"description": "Some react components for text selection",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,8 +12,9 @@
|
|
|
12
12
|
"@emotion/styled": "^11.14.1",
|
|
13
13
|
"@mui/icons-material": "^7.3.4",
|
|
14
14
|
"@mui/material": "^7.3.4",
|
|
15
|
+
"@pie-element/shared-utils": "0.2.0-next.20261007021359",
|
|
15
16
|
"@pie-framework/parse-english": "^1.0.0",
|
|
16
|
-
"@pie-lib/render-ui": "8.0.2-next.
|
|
17
|
+
"@pie-lib/render-ui": "8.0.2-next.20261007021359",
|
|
17
18
|
"@pie-lib/style-utils": "3.0.2-next.20261006160832",
|
|
18
19
|
"@pie-lib/translator": "5.0.2-next.20261006160832",
|
|
19
20
|
"debug": "^4.1.1",
|