@pie-lib/text-select 0.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.
Files changed (45) hide show
  1. package/dist/index.d.ts +16 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +7 -0
  4. package/dist/legend.d.ts +13 -0
  5. package/dist/legend.d.ts.map +1 -0
  6. package/dist/legend.js +64 -0
  7. package/dist/text-select.d.ts +35 -0
  8. package/dist/text-select.d.ts.map +1 -0
  9. package/dist/text-select.js +53 -0
  10. package/dist/token-select/index.d.ts +39 -0
  11. package/dist/token-select/index.d.ts.map +1 -0
  12. package/dist/token-select/index.js +102 -0
  13. package/dist/token-select/token.d.ts +33 -0
  14. package/dist/token-select/token.d.ts.map +1 -0
  15. package/dist/token-select/token.js +134 -0
  16. package/dist/tokenizer/builder.d.ts +28 -0
  17. package/dist/tokenizer/builder.d.ts.map +1 -0
  18. package/dist/tokenizer/builder.js +124 -0
  19. package/dist/tokenizer/controls.d.ts +24 -0
  20. package/dist/tokenizer/controls.d.ts.map +1 -0
  21. package/dist/tokenizer/controls.js +68 -0
  22. package/dist/tokenizer/index.d.ts +36 -0
  23. package/dist/tokenizer/index.d.ts.map +1 -0
  24. package/dist/tokenizer/index.js +91 -0
  25. package/dist/tokenizer/selection-utils.d.ts +11 -0
  26. package/dist/tokenizer/selection-utils.d.ts.map +1 -0
  27. package/dist/tokenizer/selection-utils.js +18 -0
  28. package/dist/tokenizer/token-text.d.ts +28 -0
  29. package/dist/tokenizer/token-text.d.ts.map +1 -0
  30. package/dist/tokenizer/token-text.js +85 -0
  31. package/dist/utils.d.ts +13 -0
  32. package/dist/utils.d.ts.map +1 -0
  33. package/dist/utils.js +21 -0
  34. package/package.json +39 -0
  35. package/src/index.ts +18 -0
  36. package/src/legend.tsx +112 -0
  37. package/src/text-select.tsx +89 -0
  38. package/src/token-select/index.tsx +181 -0
  39. package/src/token-select/token.tsx +233 -0
  40. package/src/tokenizer/builder.ts +268 -0
  41. package/src/tokenizer/controls.tsx +81 -0
  42. package/src/tokenizer/index.tsx +154 -0
  43. package/src/tokenizer/selection-utils.ts +59 -0
  44. package/src/tokenizer/token-text.tsx +145 -0
  45. package/src/utils.tsx +66 -0
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/text-select/src/tokenizer/controls.jsx
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ import React from 'react';
10
+ import PropTypes from 'prop-types';
11
+ export declare class Controls extends React.Component {
12
+ static propTypes: {
13
+ onClear: PropTypes.Validator<(...args: any[]) => any>;
14
+ onWords: PropTypes.Validator<(...args: any[]) => any>;
15
+ onSentences: PropTypes.Validator<(...args: any[]) => any>;
16
+ onParagraphs: PropTypes.Validator<(...args: any[]) => any>;
17
+ setCorrectMode: PropTypes.Validator<boolean>;
18
+ onToggleCorrectMode: PropTypes.Validator<(...args: any[]) => any>;
19
+ };
20
+ static defaultProps: {};
21
+ render(): JSX.Element;
22
+ }
23
+ export default Controls;
24
+ //# sourceMappingURL=controls.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controls.d.ts","sourceRoot":"","sources":["../../src/tokenizer/controls.tsx"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,SAAS,MAAM,YAAY,CAAC;AA8BnC,qBAAa,QAAS,SAAQ,KAAK,CAAC,SAAS;IAC3C,MAAM,CAAC,SAAS;;;;;;;MAOd;IAEF,MAAM,CAAC,YAAY,KAAM;IAEzB,MAAM;CA0BP;AACD,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,68 @@
1
+ import e from "react";
2
+ import t from "prop-types";
3
+ import n from "@mui/material/Button";
4
+ import { styled as r } from "@mui/material/styles";
5
+ import i from "@mui/material/Switch";
6
+ import a from "@mui/material/FormControlLabel";
7
+ import { color as o } from "@pie-lib/render-ui";
8
+ import { jsx as s, jsxs as c } from "react/jsx-runtime";
9
+ //#region src/tokenizer/controls.tsx
10
+ var l = r("div")(() => ({
11
+ display: "flex",
12
+ alignItems: "center",
13
+ justifyContent: "space-between"
14
+ })), u = r(n)(({ theme: e }) => ({ marginRight: e.spacing(1) })), d = r(i)(() => ({
15
+ "& .MuiSwitch-thumb": { "&.Mui-checked": { color: `${o.tertiary()} !important` } },
16
+ "& .MuiSwitch-track": { "&.Mui-checked": { backgroundColor: `${o.tertiaryLight()} !important` } }
17
+ })), f = class extends e.Component {
18
+ static propTypes = {
19
+ onClear: t.func.isRequired,
20
+ onWords: t.func.isRequired,
21
+ onSentences: t.func.isRequired,
22
+ onParagraphs: t.func.isRequired,
23
+ setCorrectMode: t.bool.isRequired,
24
+ onToggleCorrectMode: t.func.isRequired
25
+ };
26
+ static defaultProps = {};
27
+ render() {
28
+ let { onClear: e, onWords: t, onSentences: n, onParagraphs: r, setCorrectMode: i, onToggleCorrectMode: o } = this.props;
29
+ return /* @__PURE__ */ c(l, { children: [/* @__PURE__ */ c("div", { children: [
30
+ /* @__PURE__ */ s(u, {
31
+ onClick: t,
32
+ size: "small",
33
+ color: "primary",
34
+ disabled: i,
35
+ children: "Words"
36
+ }),
37
+ /* @__PURE__ */ s(u, {
38
+ onClick: n,
39
+ size: "small",
40
+ color: "primary",
41
+ disabled: i,
42
+ children: "Sentences"
43
+ }),
44
+ /* @__PURE__ */ s(u, {
45
+ onClick: r,
46
+ size: "small",
47
+ color: "primary",
48
+ disabled: i,
49
+ children: "Paragraphs"
50
+ }),
51
+ /* @__PURE__ */ s(u, {
52
+ size: "small",
53
+ color: "secondary",
54
+ onClick: e,
55
+ disabled: i,
56
+ children: "Clear"
57
+ })
58
+ ] }), /* @__PURE__ */ s(a, {
59
+ control: /* @__PURE__ */ s(d, {
60
+ checked: i,
61
+ onChange: o
62
+ }),
63
+ label: "Set correct answers"
64
+ })] });
65
+ }
66
+ };
67
+ //#endregion
68
+ export { f as default };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/text-select/src/tokenizer/index.jsx
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ import React from 'react';
10
+ import PropTypes from 'prop-types';
11
+ export declare class Tokenizer extends React.Component {
12
+ static propTypes: {
13
+ text: PropTypes.Validator<string>;
14
+ tokens: PropTypes.Requireable<(PropTypes.InferProps<{
15
+ text: PropTypes.Requireable<string>;
16
+ correct: PropTypes.Requireable<boolean>;
17
+ start: PropTypes.Requireable<number>;
18
+ end: PropTypes.Requireable<number>;
19
+ }> | null | undefined)[]>;
20
+ onChange: PropTypes.Validator<(...args: any[]) => any>;
21
+ };
22
+ static defaultProps: {};
23
+ constructor(props: any);
24
+ onChangeHandler: any;
25
+ toggleCorrectMode: () => any;
26
+ clear: any;
27
+ buildTokens: any;
28
+ selectToken: any;
29
+ tokenClick: any;
30
+ tokenIndex: any;
31
+ setCorrect: any;
32
+ removeToken: any;
33
+ render(): JSX.Element;
34
+ }
35
+ export default Tokenizer;
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tokenizer/index.tsx"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,SAAS,MAAM,YAAY,CAAC;AAgBnC,qBAAa,SAAU,SAAQ,KAAK,CAAC,SAAS;IAC5C,MAAM,CAAC,SAAS;;;;;;;;;MAWd;IAEF,MAAM,CAAC,YAAY,KAAM;gBAEb,KAAK,KAAA;IAQjB,eAAe,EAAE,GAAG,CAMlB;IAEF,iBAAiB,YAAuE;IAExF,KAAK,EAAE,GAAG,CAER;IAEF,WAAW,EAAE,GAAG,CAKd;IAEF,WAAW,EAAE,GAAG,CAMd;IAEF,UAAU,EAAE,GAAG,CAQb;IAEF,UAAU,EAAE,GAAG,CAMb;IAEF,UAAU,EAAE,GAAG,CAab;IAEF,WAAW,EAAE,GAAG,CAWd;IAEF,MAAM;CAyBP;AAED,eAAe,SAAS,CAAC"}
@@ -0,0 +1,91 @@
1
+ import e from "./controls.js";
2
+ import { paragraphs as t, sentences as n, words as r } from "./builder.js";
3
+ import i from "./token-text.js";
4
+ import a from "react";
5
+ import o from "prop-types";
6
+ import { styled as s } from "@mui/material/styles";
7
+ import { jsx as c, jsxs as l } from "react/jsx-runtime";
8
+ import { clone as u, differenceWith as d, isEqual as f } from "lodash-es";
9
+ import { noSelect as p } from "@pie-lib/style-utils";
10
+ //#region src/tokenizer/index.tsx
11
+ var m = s("div")(({ disabled: e }) => ({
12
+ whiteSpace: "pre-wrap",
13
+ ...e && { ...p() }
14
+ })), h = class extends a.Component {
15
+ static propTypes = {
16
+ text: o.string.isRequired,
17
+ tokens: o.arrayOf(o.shape({
18
+ text: o.string,
19
+ correct: o.bool,
20
+ start: o.number,
21
+ end: o.number
22
+ })),
23
+ onChange: o.func.isRequired
24
+ };
25
+ static defaultProps = {};
26
+ constructor(e) {
27
+ super(e), this.state = {
28
+ setCorrectMode: !1,
29
+ mode: ""
30
+ };
31
+ }
32
+ onChangeHandler = (e, t) => {
33
+ this.props.onChange(e, t), this.setState({ mode: t });
34
+ };
35
+ toggleCorrectMode = () => this.setState({ setCorrectMode: !this.state.setCorrectMode });
36
+ clear = () => {
37
+ this.onChangeHandler([], "");
38
+ };
39
+ buildTokens = (e, t) => {
40
+ let { text: n } = this.props, r = t(n);
41
+ this.onChangeHandler(r, e);
42
+ };
43
+ selectToken = (e, t) => {
44
+ let { tokens: n } = this.props, r = d(u(n), t, f);
45
+ r.push(e), this.onChangeHandler(r, this.state.mode);
46
+ };
47
+ tokenClick = (e) => {
48
+ let { setCorrectMode: t } = this.state;
49
+ t ? this.setCorrect(e) : this.removeToken(e);
50
+ };
51
+ tokenIndex = (e) => {
52
+ let { tokens: t } = this.props;
53
+ return t.findIndex((t) => t.text == e.text && t.start == e.start && t.end == e.end);
54
+ };
55
+ setCorrect = (e) => {
56
+ let { tokens: t } = this.props, n = this.tokenIndex(e);
57
+ if (n !== -1) {
58
+ let e = t[n];
59
+ e.correct = !e.correct;
60
+ let r = u(t);
61
+ r.splice(n, 1, e), this.onChangeHandler(r, this.state.mode);
62
+ }
63
+ };
64
+ removeToken = (e) => {
65
+ let { tokens: t } = this.props, n = this.tokenIndex(e);
66
+ if (n !== -1) {
67
+ let e = u(t);
68
+ e.splice(n, 1), this.onChangeHandler(e, this.state.mode);
69
+ }
70
+ };
71
+ render() {
72
+ let { text: a, tokens: o } = this.props, { setCorrectMode: s } = this.state;
73
+ return /* @__PURE__ */ l("div", { children: [/* @__PURE__ */ c(e, {
74
+ onClear: this.clear,
75
+ onWords: () => this.buildTokens("words", r),
76
+ onSentences: () => this.buildTokens("sentence", n),
77
+ onParagraphs: () => this.buildTokens("paragraphs", t),
78
+ setCorrectMode: s,
79
+ onToggleCorrectMode: this.toggleCorrectMode
80
+ }), /* @__PURE__ */ c(m, {
81
+ disabled: s,
82
+ as: i,
83
+ text: a,
84
+ tokens: o,
85
+ onTokenClick: this.tokenClick,
86
+ onSelectToken: this.selectToken
87
+ })] });
88
+ }
89
+ };
90
+ //#endregion
91
+ export { h as default };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/text-select/src/tokenizer/selection-utils.js
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ export declare const clearSelection: () => void;
10
+ export declare const getCaretCharacterOffsetWithin: (element: any) => number;
11
+ //# sourceMappingURL=selection-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selection-utils.d.ts","sourceRoot":"","sources":["../../src/tokenizer/selection-utils.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,eAAO,MAAM,cAAc,YAmB1B,CAAC;AAEF,eAAO,MAAM,6BAA6B,GAAI,YAAO,WA2BpD,CAAC"}
@@ -0,0 +1,18 @@
1
+ //#region src/tokenizer/selection-utils.ts
2
+ var e = () => {
3
+ document.getSelection ? (document.getSelection().removeAllRanges(), document.getSelection().addRange(document.createRange())) : window.getSelection ? window.getSelection().removeAllRanges ? (window.getSelection().removeAllRanges(), window.getSelection().addRange(document.createRange())) : window.getSelection().empty && window.getSelection().empty() : document.selection && document.selection.empty();
4
+ }, t = (e) => {
5
+ var t = 0, n = e.ownerDocument || e.document, r = n.defaultView || n.parentWindow, i;
6
+ if (r.getSelection !== void 0) {
7
+ if (i = r.getSelection(), i.rangeCount > 0) {
8
+ var a = r.getSelection().getRangeAt(0), o = a.toString().length, s = a.cloneRange();
9
+ s.selectNodeContents(e), s.setEnd(a.endContainer, a.endOffset), t = o ? s.toString().length - o : s.toString().length;
10
+ }
11
+ } else if ((i = n.selection) && i.type !== "Control") {
12
+ var c = i.createRange(), l = n.body.createTextRange();
13
+ l.moveToElementText(e), l.setEndPoint("EndToEnd", c), t = l.text.length;
14
+ }
15
+ return t;
16
+ };
17
+ //#endregion
18
+ export { e as clearSelection, t as getCaretCharacterOffsetWithin };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/text-select/src/tokenizer/token-text.jsx
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ import React from 'react';
10
+ import PropTypes from 'prop-types';
11
+ export declare const Text: ({ text, predefined, onClick, correct }: {
12
+ text: any;
13
+ predefined: any;
14
+ onClick: any;
15
+ correct: any;
16
+ }) => JSX.Element;
17
+ export default class TokenText extends React.Component {
18
+ static propTypes: {
19
+ text: PropTypes.Validator<string>;
20
+ tokens: PropTypes.Validator<any[]>;
21
+ onTokenClick: PropTypes.Validator<(...args: any[]) => any>;
22
+ onSelectToken: PropTypes.Validator<(...args: any[]) => any>;
23
+ className: PropTypes.Requireable<string>;
24
+ };
25
+ onClick: any;
26
+ render(): JSX.Element;
27
+ }
28
+ //# sourceMappingURL=token-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-text.d.ts","sourceRoot":"","sources":["../../src/tokenizer/token-text.tsx"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,SAAS,MAAM,YAAY,CAAC;AAgCnC,eAAO,MAAM,IAAI,GAAI;;;;;CAAsC,gBAU1D,CAAC;AAIF,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,KAAK,CAAC,SAAS;IACpD,MAAM,CAAC,SAAS;;;;;;MAMd;IAeF,OAAO,EAAE,GAAG,CAmDV;IAEF,MAAM;CAYP"}
@@ -0,0 +1,85 @@
1
+ import { intersection as e, normalize as t } from "./builder.js";
2
+ import { clearSelection as n, getCaretCharacterOffsetWithin as r } from "./selection-utils.js";
3
+ import i from "react";
4
+ import a from "prop-types";
5
+ import { styled as o } from "@mui/material/styles";
6
+ import { jsx as s } from "react/jsx-runtime";
7
+ import c from "debug";
8
+ import l from "classnames";
9
+ import { green as u, yellow as d } from "@mui/material/colors";
10
+ //#region src/tokenizer/token-text.tsx
11
+ var f = c("@pie-lib:text-select:token-text"), p = o("span")(() => ({
12
+ "&.predefined": {
13
+ cursor: "pointer",
14
+ backgroundColor: d[100],
15
+ border: `dashed 0px ${d[700]}`,
16
+ "& *": {
17
+ cursor: "pointer",
18
+ backgroundColor: d[100],
19
+ border: `dashed 0px ${d[700]}`
20
+ }
21
+ },
22
+ "&.correct": {
23
+ backgroundColor: u[500],
24
+ "& *": { backgroundColor: u[500] }
25
+ }
26
+ })), m = ({ text: e, predefined: t, onClick: n, correct: r }) => {
27
+ let i = (e || "").replace(/\n/g, "<br>");
28
+ return t ? /* @__PURE__ */ s(p, {
29
+ onClick: n,
30
+ className: l("predefined", r && "correct"),
31
+ dangerouslySetInnerHTML: { __html: i }
32
+ }) : /* @__PURE__ */ s("span", { dangerouslySetInnerHTML: { __html: i } });
33
+ }, h = [
34
+ "\n",
35
+ " ",
36
+ " "
37
+ ], g = class extends i.Component {
38
+ static propTypes = {
39
+ text: a.string.isRequired,
40
+ tokens: a.array.isRequired,
41
+ onTokenClick: a.func.isRequired,
42
+ onSelectToken: a.func.isRequired,
43
+ className: a.string
44
+ };
45
+ onClick = (t) => {
46
+ let { onSelectToken: i, text: a, tokens: o } = this.props;
47
+ if (t.preventDefault(), typeof window > "u") return;
48
+ let s = window.getSelection().toString();
49
+ if (s.length > 0 && h.indexOf(s) < 0 && this.root) {
50
+ let t = r(this.root), c = a.slice(t).indexOf(s);
51
+ if (t += c, t !== void 0) {
52
+ let r = t + s.length;
53
+ if (r <= a.length) {
54
+ let a = e({
55
+ start: t,
56
+ end: r
57
+ }, o);
58
+ if (a.hasOverlap) f("hasOverlap - do nothing"), n();
59
+ else {
60
+ let e = a.surroundedTokens;
61
+ i({
62
+ text: s,
63
+ start: t,
64
+ end: r
65
+ }, e), n();
66
+ }
67
+ }
68
+ }
69
+ }
70
+ };
71
+ render() {
72
+ let { text: e, tokens: n, className: r, onTokenClick: i } = this.props, a = t(e, n);
73
+ return /* @__PURE__ */ s("div", {
74
+ className: r,
75
+ ref: (e) => this.root = e,
76
+ onClick: this.onClick,
77
+ children: a.map((e, t) => /* @__PURE__ */ s(m, {
78
+ ...e,
79
+ onClick: () => i(e)
80
+ }, t))
81
+ });
82
+ }
83
+ };
84
+ //#endregion
85
+ export { g as default };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/text-select/src/utils.js
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ export declare const parseBrs: (dom: any) => void;
10
+ export declare const parseParagraph: (paragraph: any, end: any) => any;
11
+ export declare const parseParagraphs: (dom: any) => string | null;
12
+ export declare const prepareText: (text: any) => any;
13
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.tsx"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAUH,eAAO,MAAM,QAAQ,GAAI,QAAG,SAM3B,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,cAAS,EAAE,QAAG,QAM5C,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,QAAG,kBAUlC,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,SAAI,QAmB/B,CAAC"}
package/dist/utils.js ADDED
@@ -0,0 +1,21 @@
1
+ //#region src/utils.tsx
2
+ var e = (e = "") => {
3
+ let t = document.createElement("div");
4
+ return t.innerHTML = e.trim(), t;
5
+ }, t = (e) => {
6
+ e.querySelectorAll("br").forEach((e) => e.replaceWith("\n")), e.innerHTML = e.innerHTML.replace(/\n\n/g, "\n");
7
+ }, n = (e, t) => t ? e.innerHTML : `${e.innerHTML}\n\n`, r = (e) => {
8
+ let t = e.querySelectorAll("p"), r = "";
9
+ return t.forEach((e, i) => {
10
+ r += n(e, i === t.length - 1);
11
+ }), r || null;
12
+ }, i = (n) => {
13
+ let i = e(n), a = Array.from(i.querySelectorAll("*"));
14
+ if (i.querySelectorAll("p").length === 0) {
15
+ let e = document.createElement("div");
16
+ e.innerHTML = `<p>${i.innerHTML}</p>`, i = e;
17
+ }
18
+ return a.length === 0 ? n : (t(i), r(i));
19
+ };
20
+ //#endregion
21
+ export { i as prepareText };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@pie-lib/text-select",
3
+ "version": "0.1.0",
4
+ "description": "Some react components for text selection",
5
+ "dependencies": {
6
+ "@emotion/react": "^11.14.0",
7
+ "@emotion/style": "^0.8.0",
8
+ "@mui/icons-material": "^7.3.4",
9
+ "@mui/material": "^7.3.4",
10
+ "@pie-framework/parse-english": "^1.0.0",
11
+ "@pie-lib/render-ui": "0.1.0",
12
+ "@pie-lib/style-utils": "0.1.0",
13
+ "@pie-lib/translator": "0.1.0",
14
+ "classnames": "^2.2.6",
15
+ "debug": "^4.1.1",
16
+ "lodash-es": "^4.17.23",
17
+ "prop-types": "^15.6.2",
18
+ "react": "^18.2.0"
19
+ },
20
+ "type": "module",
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "src"
32
+ ],
33
+ "sideEffects": false,
34
+ "scripts": {
35
+ "build": "bun x vite build && bun x tsc --emitDeclarationOnly",
36
+ "dev": "bun x vite",
37
+ "test": "bun x vitest run"
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-lib/packages/text-select/src/index.js
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import Tokenizer from './tokenizer/index.js';
12
+ import TokenSelect, { TokenTypes } from './token-select/index.js';
13
+ import TextSelect from './text-select.js';
14
+ import { Legend } from './legend.js';
15
+ import Token from './token-select/token.js';
16
+ import { prepareText } from './utils.js';
17
+
18
+ export { TextSelect, TokenTypes, Tokenizer, TokenSelect, Token, prepareText, Legend };
package/src/legend.tsx ADDED
@@ -0,0 +1,112 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-lib/packages/text-select/src/legend.js
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import React from 'react';
12
+ import { styled } from '@mui/material/styles';
13
+ import Check from '@mui/icons-material/Check';
14
+ import Close from '@mui/icons-material/Close';
15
+ import { color } from '@pie-lib/render-ui';
16
+ import Translator from '@pie-lib/translator';
17
+
18
+ const { translator } = Translator;
19
+
20
+ const StyledFlexContainer: any = styled('div')(({ theme }) => ({
21
+ display: 'flex',
22
+ flexDirection: 'row',
23
+ alignItems: 'center',
24
+ gap: theme.spacing(2),
25
+ borderBottom: '1px solid lightgrey',
26
+ borderTop: '1px solid lightgrey',
27
+ paddingBottom: theme.spacing(1),
28
+ paddingTop: theme.spacing(1),
29
+ marginBottom: theme.spacing(1),
30
+ }));
31
+
32
+ const StyledKey: any = styled('span')(({ theme }) => ({
33
+ fontSize: '14px',
34
+ fontWeight: 'bold',
35
+ color: color.black(),
36
+ marginLeft: theme.spacing(1),
37
+ }));
38
+
39
+ const StyledContainer: any = styled('div')(() => ({
40
+ position: 'relative',
41
+ padding: '4px',
42
+ fontSize: '14px',
43
+ borderRadius: '4px',
44
+ }));
45
+
46
+ const StyledCorrectContainer: any = styled(StyledContainer)(() => ({
47
+ border: `${color.correctTertiary()} solid 2px`,
48
+ }));
49
+
50
+ const StyledIncorrectContainer: any = styled(StyledContainer)(() => ({
51
+ border: `${color.incorrectWithIcon()} solid 2px`,
52
+ }));
53
+
54
+ const StyledMissingContainer: any = styled(StyledContainer)(() => ({
55
+ border: `${color.incorrectWithIcon()} dashed 2px`,
56
+ }));
57
+
58
+ const baseIconStyles = {
59
+ color: color.white(),
60
+ position: 'absolute',
61
+ top: '-8px',
62
+ left: '-8px',
63
+ borderRadius: '50%',
64
+ fontSize: '12px',
65
+ padding: '2px',
66
+ };
67
+
68
+ const StyledCorrectCheckIcon: any = styled(Check)(() => ({
69
+ ...baseIconStyles,
70
+ backgroundColor: color.correctTertiary(),
71
+ }));
72
+
73
+ const StyledIncorrectCloseIcon: any = styled(Close)(() => ({
74
+ ...baseIconStyles,
75
+ backgroundColor: color.incorrectWithIcon(),
76
+ }));
77
+
78
+ export const Legend = ({ language, showOnlyCorrect }) => {
79
+ const legendItems = [
80
+ {
81
+ Icon: StyledCorrectCheckIcon,
82
+ label: translator.t('selectText.correctAnswerSelected', { lng: language }),
83
+ Container: StyledCorrectContainer,
84
+ },
85
+ {
86
+ Icon: StyledIncorrectCloseIcon,
87
+ label: translator.t('selectText.incorrectSelection', { lng: language }),
88
+ Container: StyledIncorrectContainer,
89
+ },
90
+ {
91
+ Icon: StyledIncorrectCloseIcon,
92
+ label: translator.t('selectText.correctAnswerNotSelected', { lng: language }),
93
+ Container: StyledMissingContainer,
94
+ },
95
+ ];
96
+
97
+ if (showOnlyCorrect) {
98
+ legendItems.splice(1, 2);
99
+ }
100
+
101
+ return (
102
+ <StyledFlexContainer>
103
+ <StyledKey>{translator.t('selectText.key', { lng: language })}</StyledKey>
104
+ {legendItems.map(({ Icon, label, Container }, idx) => (
105
+ <Container key={idx}>
106
+ <Icon />
107
+ <span>{label}</span>
108
+ </Container>
109
+ ))}
110
+ </StyledFlexContainer>
111
+ );
112
+ };