From 59527f4b0fdfcf0a790345a147254309ac9acb3c Mon Sep 17 00:00:00 2001
From: Oliver Tacke <o.tacke@posteo.de>
Date: Thu, 12 Sep 2024 15:26:54 +0200
Subject: [PATCH] Fix cell creation

---
 src/scripts/components/h5p-crossword-content.js |  8 ++++----
 src/scripts/services/h5p-crossword-generator.js | 12 +++++++-----
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/scripts/components/h5p-crossword-content.js b/src/scripts/components/h5p-crossword-content.js
index 6c1f5bb..81ac04a 100644
--- a/src/scripts/components/h5p-crossword-content.js
+++ b/src/scripts/components/h5p-crossword-content.js
@@ -118,10 +118,10 @@ export default class CrosswordContent {
     this.clueAnnouncer = new CrosswordClueAnnouncer();
     tableWrapper.appendChild(this.clueAnnouncer.getDOM());
 
-    this.crosswordLayout.result = this.crosswordLayout.result.map((word, index) => {
-      word.clue = this.params.words[index].clue;
-      word.answer = this.params.words[index].answer;
-      word.extraClue = this.params.words[index].extraClue;
+    this.crosswordLayout.result = this.crosswordLayout.result.map((word) => {
+      word.clue = this.params.words[word.index].clue;
+      word.answer = this.params.words[word.index].answer;
+      word.extraClue = this.params.words[word.index].extraClue;
 
       return word;
     });
diff --git a/src/scripts/services/h5p-crossword-generator.js b/src/scripts/services/h5p-crossword-generator.js
index c0fb8ca..8310d16 100644
--- a/src/scripts/services/h5p-crossword-generator.js
+++ b/src/scripts/services/h5p-crossword-generator.js
@@ -697,7 +697,7 @@ export default class CrosswordGenerator {
           continue;
         }
 
-        if (cell.down && cell.down.isStartOfWord) {
+        if (cell.down?.isStartOfWord) {
           result.push({
             clue: this.getWordElement(cell.down.index, 'clue'),
             answer: this.getWordElement(cell.down.index, 'answer'),
@@ -705,11 +705,12 @@ export default class CrosswordGenerator {
             startx: c + 1,
             starty: r + 1,
             orientation: 'down',
-            clueId: nextClueId
+            clueId: nextClueId,
+            index: cell.down.index
           });
         }
 
-        if (cell.across && cell.across.isStartOfWord) {
+        if (cell.across?.isStartOfWord) {
           result.push({
             clue: this.getWordElement(cell.across.index, 'clue'),
             answer: this.getWordElement(cell.across.index, 'answer'),
@@ -717,11 +718,12 @@ export default class CrosswordGenerator {
             startx: c + 1,
             starty: r + 1,
             orientation: 'across',
-            clueId: nextClueId
+            clueId: nextClueId,
+            index: cell.across.index
           });
         }
 
-        if (cell.down && cell.down.isStartOfWord || cell.across && cell.across.isStartOfWord) {
+        if (cell.down?.isStartOfWord || cell.across?.isStartOfWord) {
           nextClueId++;
         }
       }