@webwriter/quiz 1.1.3 → 2.0.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 (167) hide show
  1. package/.github/workflows/publish.yml +8 -0
  2. package/LICENSE +6 -6
  3. package/README.md +778 -0
  4. package/assets/icons/grip-horizontal.svg +8 -0
  5. package/assets/icons/grip-vertical.svg +8 -0
  6. package/assets/icons/truefalse.svg +6 -0
  7. package/custom-elements.json +2779 -0
  8. package/custom.d.ts +180 -176
  9. package/dist/api/index.d.ts +27 -0
  10. package/dist/api/index.js +40 -0
  11. package/dist/migrate.js +1434 -0
  12. package/dist/widgets/webwriter-choice-item.js +4036 -3895
  13. package/dist/widgets/webwriter-choice.js +2269 -5821
  14. package/dist/widgets/webwriter-gap-item.js +3987 -0
  15. package/dist/widgets/webwriter-gap.js +2908 -0
  16. package/dist/widgets/webwriter-mark.js +4335 -3489
  17. package/dist/widgets/webwriter-order-item.js +4014 -3306
  18. package/dist/widgets/webwriter-order.js +2944 -4386
  19. package/dist/widgets/webwriter-pairing-item.js +1058 -2421
  20. package/dist/widgets/webwriter-pairing.js +2993 -4383
  21. package/dist/widgets/webwriter-quiz-hint.js +2469 -0
  22. package/dist/widgets/webwriter-quiz.js +4933 -6099
  23. package/dist/widgets/webwriter-speech.js +6077 -5230
  24. package/dist/widgets/webwriter-task-prompt.js +4775 -2165
  25. package/dist/widgets/webwriter-task.js +2364 -8382
  26. package/dist/widgets/webwriter-text.js +4169 -4337
  27. package/dist/widgets/webwriter-true-false.js +4674 -0
  28. package/editing-config.json +106 -0
  29. package/lit-localize.json +21 -15
  30. package/localization/de.xlf +407 -74
  31. package/localization/es.xlf +407 -69
  32. package/localization/fr.xlf +407 -69
  33. package/localization/generated/de.ts +112 -29
  34. package/localization/generated/es.ts +112 -29
  35. package/localization/generated/fr.ts +112 -29
  36. package/localization/generated/index.d.ts +5 -0
  37. package/localization/generated/index.js +2 -2
  38. package/localization/generated/it.ts +112 -29
  39. package/localization/generated/locale-codes.js +31 -79
  40. package/localization/generated/nl.ts +112 -29
  41. package/localization/it.xlf +407 -69
  42. package/localization/nl.xlf +407 -69
  43. package/package.json +119 -252
  44. package/scripts/apply-translations.mjs +97 -0
  45. package/src/api/index.ts +72 -0
  46. package/src/lib/artboard.ts +686 -0
  47. package/src/lib/draggable-grid.ts +432 -0
  48. package/src/lib/encrypted-property.ts +202 -0
  49. package/src/lib/hint-popup.ts +152 -0
  50. package/src/lib/utils.ts +97 -0
  51. package/src/lib/webwriter-quirks.ts +121 -0
  52. package/src/migrate/index.ts +622 -0
  53. package/src/migrate/legacy-solution.ts +362 -0
  54. package/src/snippets/order.html +9 -8
  55. package/src/widgets/choice/webwriter-choice-item.ts +316 -0
  56. package/src/widgets/choice/webwriter-choice.ts +328 -0
  57. package/src/widgets/choice/webwriter-true-false.ts +113 -0
  58. package/src/widgets/dnd/webwriter-dnd-draggable.ts +28 -0
  59. package/src/widgets/dnd/webwriter-dnd-dropzone.ts +9 -0
  60. package/src/widgets/dnd/webwriter-dnd.ts +36 -0
  61. package/src/widgets/gap/webwriter-gap-item.ts +250 -0
  62. package/src/widgets/gap/webwriter-gap.ts +447 -0
  63. package/src/widgets/order/webwriter-order-item.ts +243 -0
  64. package/src/widgets/order/webwriter-order.ts +282 -0
  65. package/src/widgets/pairing/webwriter-pairing-item.ts +46 -0
  66. package/src/widgets/pairing/webwriter-pairing.ts +934 -0
  67. package/src/widgets/speech/audio-player.ts +325 -0
  68. package/src/widgets/speech/audio-recorder.ts +285 -0
  69. package/src/widgets/speech/waveform.ts +113 -0
  70. package/src/widgets/speech/webwriter-speech.ts +87 -0
  71. package/src/widgets/webwriter-mark.ts +374 -432
  72. package/src/widgets/webwriter-quiz-hint.ts +36 -0
  73. package/src/widgets/webwriter-quiz.ts +320 -282
  74. package/src/widgets/webwriter-task-prompt.ts +49 -24
  75. package/src/widgets/webwriter-task.ts +216 -547
  76. package/src/widgets/webwriter-text.ts +291 -156
  77. package/tsconfig.api.json +12 -0
  78. package/tsconfig.json +8 -7
  79. package/dist/widgets/webwriter-choice.css +0 -444
  80. package/dist/widgets/webwriter-cloze-gap.css +0 -444
  81. package/dist/widgets/webwriter-cloze-gap.js +0 -5866
  82. package/dist/widgets/webwriter-cloze.css +0 -444
  83. package/dist/widgets/webwriter-cloze.js +0 -3786
  84. package/dist/widgets/webwriter-mark.css +0 -444
  85. package/dist/widgets/webwriter-order.css +0 -444
  86. package/dist/widgets/webwriter-pairing-item.css +0 -444
  87. package/dist/widgets/webwriter-pairing.css +0 -444
  88. package/dist/widgets/webwriter-quiz.css +0 -444
  89. package/dist/widgets/webwriter-task-explainer.css +0 -444
  90. package/dist/widgets/webwriter-task-explainer.js +0 -2872
  91. package/dist/widgets/webwriter-task-hint.css +0 -444
  92. package/dist/widgets/webwriter-task-hint.js +0 -2811
  93. package/dist/widgets/webwriter-task.css +0 -444
  94. package/dist/widgets/webwriter-text.css +0 -444
  95. package/localization/bg.xlf +0 -69
  96. package/localization/cs.xlf +0 -69
  97. package/localization/da.xlf +0 -69
  98. package/localization/el.xlf +0 -69
  99. package/localization/et.xlf +0 -69
  100. package/localization/fi.xlf +0 -69
  101. package/localization/generated/bg.ts +0 -30
  102. package/localization/generated/cs.ts +0 -30
  103. package/localization/generated/da.ts +0 -30
  104. package/localization/generated/el.ts +0 -30
  105. package/localization/generated/et.ts +0 -30
  106. package/localization/generated/fi.ts +0 -30
  107. package/localization/generated/hu.ts +0 -30
  108. package/localization/generated/id.ts +0 -30
  109. package/localization/generated/ja.ts +0 -30
  110. package/localization/generated/ko.ts +0 -30
  111. package/localization/generated/lt.ts +0 -30
  112. package/localization/generated/lv.ts +0 -30
  113. package/localization/generated/nb.ts +0 -30
  114. package/localization/generated/pl.ts +0 -30
  115. package/localization/generated/pt-BR.ts +0 -30
  116. package/localization/generated/pt-PT.ts +0 -30
  117. package/localization/generated/ro.ts +0 -30
  118. package/localization/generated/ru.ts +0 -30
  119. package/localization/generated/sk.ts +0 -30
  120. package/localization/generated/sl.ts +0 -30
  121. package/localization/generated/sv.ts +0 -30
  122. package/localization/generated/tr.ts +0 -30
  123. package/localization/generated/uk.ts +0 -30
  124. package/localization/generated/zh-hans.ts +0 -30
  125. package/localization/hu.xlf +0 -69
  126. package/localization/id.xlf +0 -69
  127. package/localization/ja.xlf +0 -69
  128. package/localization/ko.xlf +0 -69
  129. package/localization/lt.xlf +0 -69
  130. package/localization/lv.xlf +0 -69
  131. package/localization/nb.xlf +0 -69
  132. package/localization/pl.xlf +0 -69
  133. package/localization/pt-BR.xlf +0 -69
  134. package/localization/pt-PT.xlf +0 -69
  135. package/localization/ro.xlf +0 -69
  136. package/localization/ru.xlf +0 -69
  137. package/localization/sk.xlf +0 -69
  138. package/localization/sl.xlf +0 -69
  139. package/localization/sv.xlf +0 -69
  140. package/localization/tr.xlf +0 -69
  141. package/localization/uk.xlf +0 -69
  142. package/localization/zh-hans.xlf +0 -69
  143. package/src/lib/combobox.ts +0 -456
  144. package/src/lib/highlighter-fill.svg +0 -6
  145. package/src/snippets/Beispiel-Choice.html +0 -57
  146. package/src/snippets/Beispiel-Mark.html +0 -14
  147. package/src/snippets/Beispiel-Order.html +0 -52
  148. package/src/snippets/Beispiel-Text.html +0 -20
  149. package/src/snippets/blank.html +0 -1
  150. package/src/snippets/choice.html +0 -8
  151. package/src/snippets/cloze.html +0 -4
  152. package/src/snippets/mark.html +0 -4
  153. package/src/snippets/pairing.html +0 -10
  154. package/src/snippets/speech.html +0 -4
  155. package/src/snippets/text.html +0 -4
  156. package/src/snippets/wordsearch.html +0 -4
  157. package/src/widgets/webwriter-choice-item.ts +0 -254
  158. package/src/widgets/webwriter-choice.ts +0 -353
  159. package/src/widgets/webwriter-cloze-gap.ts +0 -216
  160. package/src/widgets/webwriter-cloze.ts +0 -136
  161. package/src/widgets/webwriter-order-item.ts +0 -441
  162. package/src/widgets/webwriter-order.ts +0 -327
  163. package/src/widgets/webwriter-pairing-item.ts +0 -161
  164. package/src/widgets/webwriter-pairing.ts +0 -193
  165. package/src/widgets/webwriter-speech.ts +0 -273
  166. package/src/widgets/webwriter-task-explainer.ts +0 -43
  167. package/src/widgets/webwriter-task-hint.ts +0 -23
@@ -1,156 +1,291 @@
1
- import {html, css, PropertyValues} from "lit"
2
- import {LitElementWw, option} from "@webwriter/lit"
3
- import {customElement, property, query} from "lit/decorators.js"
4
- import {ifDefined} from "lit/directives/if-defined.js"
5
-
6
- import SlTextarea from "@shoelace-style/shoelace/dist/components/textarea/textarea.component.js"
7
- import SlInput from "@shoelace-style/shoelace/dist/components/input/input.component.js"
8
- import "@shoelace-style/shoelace/dist/themes/light.css"
9
-
10
- import LOCALIZE from "../../localization/generated"
11
- import {msg} from "@lit/localize"
12
-
13
- declare global {interface HTMLElementTagNameMap {
14
- "webwriter-text": WebwriterText;
15
- }}
16
-
17
- @customElement("webwriter-text")
18
- export class WebwriterText extends LitElementWw {
19
-
20
- localize = LOCALIZE
21
-
22
- @property({type: String, attribute: true, reflect: true})
23
- @option({
24
- type: "select",
25
- label: {"en": "Type", "de": "Typ"},
26
- options: [
27
- {value: "long-text", label: {"en": "Long Text", "de": "Langer Text"}},
28
- {value: "text", label: {"en": "Short Text", "de": "Kurzer Text"}},
29
- {value: "number", label: {"en": "Number", "de": "Zahl"}},
30
- {value: "date", label: {"en": "Date", "de": "Datum"}},
31
- {value: "time", label: {"en": "Time", "de": "Uhrzeit"}},
32
- {value: "datetime-local", label: {"en": "Date & Time", "de": "Datum & Uhrzeit"}}
33
- ]
34
- })
35
- accessor type: "date" | "datetime-local" | "number" | "text" | "time" | "long-text" = "long-text"
36
-
37
- @property({type: String, attribute: true, reflect: true})
38
- @option()
39
- accessor placeholder: string
40
-
41
- @property({type: Boolean, attribute: true, reflect: true})
42
- @option({
43
- type: Boolean,
44
- label: {"en": "Ignore capitalization", "de": "Großschreibung ignorieren"},
45
- })
46
- accessor ignoreCase = false
47
-
48
- @property({type: Boolean, attribute: true, reflect: true})
49
- @option({
50
- type: Boolean,
51
- label: {"en": "show Solution", "de": "Lösung anzeigen"},
52
- })
53
- accessor showSolution = false
54
-
55
- @property({type: String, attribute: true, reflect: true})
56
- @option({
57
- label: {"en": "Message for wrong solution", "de": "Nachricht bei falscher Lösung"},
58
- })
59
- accessor wrongMessage: string
60
-
61
- @property({type: String, attribute: true, reflect: true})
62
- accessor value: string
63
-
64
- /*
65
- @property({type: Number, attribute: true, reflect: true})
66
- @option({type: Number})
67
- min: number
68
-
69
- @property({type: Number, attribute: true, reflect: true})
70
- @option({type: Number})
71
- max: number
72
-
73
- @property({type: Number, attribute: true, reflect: true})
74
- @option({type: Number})
75
- step: number
76
- */
77
-
78
- static scopedElements = {
79
- "sl-textarea": SlTextarea,
80
- "sl-input": SlInput
81
- }
82
-
83
- static styles = css`
84
- :host(:is([contenteditable=true], [contenteditable=""])) sl-textarea::part(textarea) {
85
- color: var(--sl-color-success-700);
86
- }
87
-
88
- sl-textarea {
89
- resize: vertical;
90
- overflow: hidden;
91
- min-height: 40px;
92
-
93
- &::part(form-control), &::part(form-control-input), &::part(base), &::part(textarea) {
94
- height: 100%;
95
- }
96
- }
97
-
98
- :is(sl-textarea, sl-input)[data-correct]::part(base) {
99
- background: var(--sl-color-success-200);
100
- }
101
-
102
- #solution {
103
- padding: 1rem;
104
- }
105
-
106
- #solution[data-correct] {
107
- background-color: var(--sl-color-success-200);
108
- }
109
-
110
- #solution:not([data-correct]) {
111
- background-color: var(--sl-color-danger-200);
112
- }
113
- `
114
-
115
- handleChange = (e: CustomEvent) => {
116
- const target = e.target as SlTextarea | SlInput
117
- if(this.isContentEditable) {
118
- this.solution = target.value?.trim()
119
- }
120
- else {
121
- this.value = target.value?.trim()
122
- }
123
- this.dispatchEvent(new CustomEvent("ww-answer-change", {
124
- detail: {value: target.value},
125
- bubbles: true,
126
- composed: true
127
- }))
128
- }
129
-
130
- @query("sl-textarea, sl-input")
131
- accessor input: SlTextarea | SlInput
132
-
133
- focus() {
134
- this.input.focus()
135
- }
136
-
137
- reset() {
138
- this.solution = undefined
139
- this.input.value = ""
140
- }
141
-
142
- reportSolution() {}
143
-
144
- @property({type: String, attribute: false, reflect: false})
145
- accessor solution: string
146
-
147
- render() {
148
- const correct = !this.ignoreCase ? this.solution && this.value?.trim() === this.solution : this.solution && this.value?.trim().toLowerCase() === this.solution.toLowerCase()
149
- const textarea = html`<sl-textarea ?data-correct=${correct} value=${this.isContentEditable? this.solution: this.value} placeholder=${this.placeholder} resize="none" @sl-change=${this.handleChange}></sl-textarea>`
150
- const input = html`<sl-input ?data-correct=${correct} value=${this.isContentEditable? this.solution: this.value} placeholder=${this.placeholder} type=${this.type} @sl-change=${this.handleChange}></sl-input>`
151
- return html`
152
- ${this.type === "long-text"? textarea: input}
153
- ${this.solution && !this.isContentEditable && !correct? html`<div id="solution" ?data-correct=${correct}>${this.showSolution?this.solution:html`<i>${this.wrongMessage}</i>`}</div>`: undefined}
154
- `
155
- }
156
- }
1
+ import { msg } from "@lit/localize";
2
+ import SlInput from "@shoelace-style/shoelace/dist/components/input/input.component.js";
3
+ import SlTextarea from "@shoelace-style/shoelace/dist/components/textarea/textarea.component.js";
4
+ import { LitElementWw, type OptionDeclaration } from "@webwriter/lit";
5
+ import TextParagraphIcon from "bootstrap-icons/icons/text-paragraph.svg";
6
+ import { css, html, nothing, type PropertyValues } from "lit";
7
+ import { customElement, property, query, state } from "lit/decorators.js";
8
+ import LOCALIZE from "../../localization/generated";
9
+ import { name as packageId } from "../../package.json";
10
+ import { registerQuizType, type IWebWriterQuizType } from "../api";
11
+ import { encryptedProperty, encryptPlaintextAttributes } from "../lib/encrypted-property";
12
+
13
+ declare global {
14
+ interface HTMLElementTagNameMap {
15
+ "webwriter-text": WebwriterText;
16
+ }
17
+ }
18
+
19
+ export type TextType = "long-text" | "text" | "number" | "date" | "time" | "datetime-local";
20
+
21
+ registerQuizType({
22
+ packageId,
23
+ widgetPosition: 30,
24
+ id: "webwriter-text",
25
+ getName: () => msg("Text"),
26
+ icon: TextParagraphIcon,
27
+ createInstance: () => document.createElement("webwriter-text"),
28
+ });
29
+
30
+ /**
31
+ * An answer where learners type a value into an input field.
32
+ *
33
+ * It must be assigned to the default slot of a `<webwriter-task>`.
34
+ */
35
+ @customElement("webwriter-text")
36
+ export class WebwriterText extends LitElementWw implements IWebWriterQuizType {
37
+ /** @internal */
38
+ localize = LOCALIZE;
39
+
40
+ /** @internal */
41
+ static scopedElements = {
42
+ "sl-textarea": SlTextarea,
43
+ "sl-input": SlInput,
44
+ };
45
+
46
+ static styles = css`
47
+ :host {
48
+ display: block;
49
+ width: 100%;
50
+ }
51
+
52
+ /* Highlight the author's solution while editing. */
53
+ :host(:is([contenteditable="true"], [contenteditable=""])) :is(sl-textarea::part(textarea), sl-input::part(input)) {
54
+ color: var(--sl-color-success-700);
55
+ }
56
+
57
+ sl-textarea::part(base),
58
+ sl-input::part(base) {
59
+ font-size: 1em !important;
60
+ }
61
+
62
+ sl-textarea {
63
+ resize: vertical;
64
+ overflow: hidden;
65
+ min-height: 40px;
66
+
67
+ &::part(form-control),
68
+ &::part(form-control-input),
69
+ &::part(base),
70
+ &::part(textarea) {
71
+ height: 100%;
72
+ }
73
+ }
74
+
75
+ :is(sl-textarea, sl-input):disabled::part(base) {
76
+ background-color: var(--sl-input-background-color);
77
+ border-color: var(--sl-input-border-color);
78
+ opacity: 1;
79
+ cursor: inherit;
80
+ }
81
+
82
+ :is(sl-textarea, sl-input)[data-state="correct"]::part(base) {
83
+ background: var(--sl-color-success-50);
84
+ border-color: var(--sl-color-success-400);
85
+ }
86
+
87
+ :is(sl-textarea, sl-input)[data-state="incorrect"]::part(base) {
88
+ background: var(--sl-color-danger-50);
89
+ border-color: var(--sl-color-danger-400);
90
+ }
91
+
92
+ .feedback {
93
+ margin-top: var(--sl-spacing-x-small);
94
+ color: var(--sl-color-neutral-500);
95
+ font-size: var(--sl-font-size-small);
96
+ overflow-wrap: anywhere;
97
+ }
98
+
99
+ .solution {
100
+ margin-top: var(--sl-spacing-x-small);
101
+ }
102
+ `;
103
+
104
+ /**
105
+ * The kind of input field learners answer in.
106
+ *
107
+ * - `long-text`: A multi-line text area.
108
+ * - `text`: A single-line text field.
109
+ * - `number`: A number field.
110
+ * - `date`: A date picker.
111
+ * - `time`: A time picker.
112
+ * - `datetime-local`: A combined date and time picker.
113
+ */
114
+ @property({ type: String, attribute: true, reflect: true })
115
+ accessor type: TextType = "long-text";
116
+
117
+ /**
118
+ * The placeholder shown while the field is empty.
119
+ */
120
+ @property({ type: String, attribute: true, reflect: true })
121
+ accessor placeholder: string = "";
122
+
123
+ /**
124
+ * Whether any non-empty answer counts as correct, without comparing it to the solution.
125
+ */
126
+ @property({ type: Boolean, attribute: "free-text", reflect: true })
127
+ accessor freeText = false;
128
+
129
+ /**
130
+ * Whether the answer is compared to the solution case-insensitively.
131
+ */
132
+ @property({ type: Boolean, attribute: "ignore-case", reflect: true })
133
+ accessor ignoreCase = false;
134
+
135
+ /**
136
+ * The feedback shown after submitting if the answer is correct.
137
+ */
138
+ @property({ type: String, attribute: "correct-message", reflect: true })
139
+ accessor correctMessage: string = "";
140
+
141
+ /**
142
+ * The feedback shown after submitting if the answer is wrong.
143
+ */
144
+ @property({ type: String, attribute: "wrong-message", reflect: true })
145
+ accessor wrongMessage: string = "";
146
+
147
+ /** @internal */
148
+ get dynamicOptions(): Record<string, OptionDeclaration> {
149
+ const isText = this.type === "long-text" || this.type === "text";
150
+ const options: Record<string, OptionDeclaration> = {
151
+ type: {
152
+ type: "select",
153
+ label: { _: msg("Type") },
154
+ options: [
155
+ { value: "long-text", label: { _: msg("Long Text") } },
156
+ { value: "text", label: { _: msg("Short Text") } },
157
+ { value: "number", label: { _: msg("Number") } },
158
+ { value: "date", label: { _: msg("Date") } },
159
+ { value: "time", label: { _: msg("Time") } },
160
+ { value: "datetime-local", label: { _: msg("Date & Time") } },
161
+ ],
162
+ },
163
+ };
164
+
165
+ if (isText || this.type === "number") {
166
+ options.placeholder = { type: "string", label: { _: msg("Placeholder") } };
167
+ }
168
+
169
+ options["free-text"] = { type: "boolean", label: { _: msg("Free answer") } };
170
+
171
+ if (!this.freeText) {
172
+ if (isText) {
173
+ options["ignore-case"] = {
174
+ type: "boolean",
175
+ label: { _: msg("Ignore capitalization") },
176
+ };
177
+ }
178
+ options["correct-message"] = {
179
+ type: "string",
180
+ label: { _: msg("Message for correct solution") },
181
+ };
182
+ options["wrong-message"] = {
183
+ type: "string",
184
+ label: { _: msg("Message for wrong solution") },
185
+ };
186
+ }
187
+
188
+ return options;
189
+ }
190
+
191
+ /**
192
+ * The value counting as correct. It is obfuscated in the markup so that learners cannot read it directly.
193
+ */
194
+ @property({ type: String, attribute: true, reflect: true, converter: encryptedProperty })
195
+ accessor solution: string = "";
196
+
197
+ @state() private accessor currentAnswer: string | undefined;
198
+ @state() private accessor graded: boolean = false;
199
+ @state() private accessor detailedFeedback: boolean = false;
200
+
201
+ @query("sl-textarea, sl-input")
202
+ private accessor input!: SlTextarea | SlInput;
203
+
204
+ protected firstUpdated(changed: PropertyValues): void {
205
+ super.firstUpdated(changed);
206
+ encryptPlaintextAttributes(this);
207
+ }
208
+
209
+ private get value(): string {
210
+ return this.isContentEditable ? this.solution : (this.currentAnswer ?? "");
211
+ }
212
+
213
+ private set value(value: string) {
214
+ if (this.isContentEditable) this.solution = value;
215
+ else this.currentAnswer = value;
216
+ }
217
+
218
+ private isCorrect(): boolean {
219
+ const answer = (this.currentAnswer ?? "").trim();
220
+ if (this.freeText) return answer !== "";
221
+ const solution = this.solution.trim();
222
+ if (!solution) return false;
223
+ return this.ignoreCase ? answer.toLowerCase() === solution.toLowerCase() : answer === solution;
224
+ }
225
+
226
+ override focus() {
227
+ this.input?.focus();
228
+ }
229
+
230
+ checkValidity(): boolean {
231
+ return (this.currentAnswer ?? "").trim() !== "";
232
+ }
233
+
234
+ reset() {
235
+ this.graded = false;
236
+ this.detailedFeedback = false;
237
+ this.currentAnswer = undefined;
238
+ }
239
+
240
+ checkAnswer(detailedFeedback: boolean): number {
241
+ this.graded = true;
242
+ this.detailedFeedback = detailedFeedback;
243
+ return this.isCorrect() ? 1 : 0;
244
+ }
245
+
246
+ private handleChange = (e: Event) => {
247
+ const target = e.target as SlTextarea | SlInput;
248
+ this.value = target.value?.trim() ?? "";
249
+ };
250
+
251
+ private get feedbackState(): "correct" | "incorrect" | undefined {
252
+ // Free answers are locked on submit but never marked correct/incorrect.
253
+ if (this.freeText || this.isContentEditable || !this.graded || !this.detailedFeedback) return undefined;
254
+ return this.isCorrect() ? "correct" : "incorrect";
255
+ }
256
+
257
+ render() {
258
+ const value = this.value;
259
+ const state = this.feedbackState;
260
+ const message = state === "correct" ? this.correctMessage : this.wrongMessage;
261
+ const showSolution = this.detailedFeedback && this.solution.trim() !== "";
262
+
263
+ const field =
264
+ this.type === "long-text"
265
+ ? html`<sl-textarea
266
+ data-state=${state ?? nothing}
267
+ value=${value}
268
+ placeholder=${this.placeholder}
269
+ resize="none"
270
+ ?disabled=${this.graded}
271
+ @sl-change=${this.handleChange}
272
+ ></sl-textarea>`
273
+ : html`<sl-input
274
+ data-state=${state ?? nothing}
275
+ value=${value}
276
+ placeholder=${this.placeholder}
277
+ type=${this.type}
278
+ ?disabled=${this.graded}
279
+ @sl-change=${this.handleChange}
280
+ ></sl-input>`;
281
+
282
+ return html`${field}${state && (message || showSolution)
283
+ ? html`<div class="feedback" data-state=${state} role="status">
284
+ ${message ? html`<div>${message}</div>` : nothing}
285
+ ${showSolution
286
+ ? html`<div class="solution"><strong>${msg("Solution")}:</strong> ${this.solution}</div>`
287
+ : nothing}
288
+ </div>`
289
+ : nothing}`;
290
+ }
291
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "emitDeclarationOnly": false,
6
+ "outDir": "./dist/api",
7
+ "rootDir": "./src/api",
8
+ "skipLibCheck": true,
9
+ "sourceMap": false
10
+ },
11
+ "include": ["src/api/index.ts"]
12
+ }
package/tsconfig.json CHANGED
@@ -1,7 +1,8 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2022",
4
- "moduleResolution": "Bundler",
5
- "module": "ES2022"
6
- }
7
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "moduleResolution": "Bundler",
5
+ "module": "ES2022",
6
+ "lib": ["ESNext", "DOM", "DOM.Iterable"]
7
+ }
8
+ }