Skip to content

Commit

Permalink
Fix checkAnswer triggered by input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
otacke committed Apr 24, 2024
1 parent d0f710a commit 78686b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/scripts/h5p-crossword-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ export default class CrosswordInput {
return;
}

if (this.noListeners) {
return;
}

const start = inputField.selectionStart;
inputField.value = `${inputField.value.substr(0, start + 1)}${inputField.value.substr(start + 1)}`;
inputField.selectionEnd = start + 1;
Expand Down Expand Up @@ -236,6 +240,10 @@ export default class CrosswordInput {
return; // workaround for Android specific code
}

if (this.noListeners) {
return;
}

if (Util.CONTROL_KEY_CODES.indexOf(event.keyCode) !== -1) {
if ([8, 35, 36, 37, 38, 39, 40, 46].indexOf(event.keyCode) === -1) {
// None of backspace, home, end, left, right, up, down, delete
Expand Down Expand Up @@ -632,8 +640,10 @@ export default class CrosswordInput {

/**
* Enable input fields.

Check warning on line 642 in src/scripts/h5p-crossword-input.js

View workflow job for this annotation

GitHub Actions / build / build

Expected only 0 line after block description
*
* @param {boolean} [noListeners] If true, no listeners will be active.
*/
enable() {
enable(noListeners = false) {
this.inputFields.forEach((field) => {
field.inputField.removeAttribute('disabled');
});
Expand All @@ -644,13 +654,15 @@ export default class CrosswordInput {
this.content.classList.remove('h5p-crossword-disabled');

this.disabled = false;
this.noListeners = noListeners;
}

/**
* Disable input fields.
*/
disable() {
this.disabled = true;
this.noListeners = true;

this.extraClues.forEach((extraClue) => {
extraClue.setAttribute('disabled', true);
Expand Down
6 changes: 5 additions & 1 deletion src/scripts/h5p-crossword-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,14 +909,18 @@ export default class CrosswordTable {
* @param {object} params Parameters.
*/
fillGrid(params) {
let hasSomeCellChanged = false;
const cells = [].concat(...this.cells)
.filter((cell) => cell.getClueId(params.orientation) === params.clueId);

cells.forEach((cell, index) => {
const answerBefore = cell.getAnswer();
cell.setAnswer(
params.text[index] || '',
(params.readOffset === -1) ? false : index === params.cursorPosition - params.readOffset
);
hasSomeCellChanged = hasSomeCellChanged ||
answerBefore !== cell.getAnswer();

// At crossection, other input fields needs to be updated
if (cell.getClueId('down') && cell.getClueId('across')) {
Expand Down Expand Up @@ -952,7 +956,7 @@ export default class CrosswordTable {

// Check if table is filled
this.callbacks.onInput({
checkFilled: true
checkFilled: hasSomeCellChanged
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/scripts/h5p-crossword.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ export default class Crossword extends H5P.Question {
* Check answer.
*/
checkAnswer() {
if (this.getViewState().id !== Crossword.VIEW_STATES.task) {
return; // Prevent double checking
}

if (!this.content) {
return; // Call by previous state, not ready yet
}
Expand Down

0 comments on commit 78686b1

Please sign in to comment.