From 867d536f8270567b0bb3739e4f05642b03171481 Mon Sep 17 00:00:00 2001 From: Nick Beckley Date: Sun, 14 Jun 2026 09:37:48 +0100 Subject: [PATCH] Bug fix: Word Companion Create Scene buttons are still disabled after Phase 10J. --- PlotLine/wwwroot/js/word-companion-host.js | 37 +++++++++++++++------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/PlotLine/wwwroot/js/word-companion-host.js b/PlotLine/wwwroot/js/word-companion-host.js index 8f6fca2..c9d4622 100644 --- a/PlotLine/wwwroot/js/word-companion-host.js +++ b/PlotLine/wwwroot/js/word-companion-host.js @@ -1520,23 +1520,24 @@ } if (detectedChapterAnchorId) { - const anchoredChapterScene = await findSceneInBookStructure( - book.bookId, - (chapter, scene) => chapter.chapterId === detectedChapterAnchorId - && normalizeTitleForResolve(scene.title, "scene").toLocaleLowerCase() === requestSceneTitle.toLocaleLowerCase() - ); + const chapters = await getBookStructureChapters(book.bookId); + const anchoredChapter = chapters.find((chapter) => chapter.chapterId === detectedChapterAnchorId); + const anchoredChapterScene = anchoredChapter + ? (Array.isArray(anchoredChapter.scenes) ? anchoredChapter.scenes : []) + .find((scene) => normalizeTitleForResolve(scene.title, "scene").toLocaleLowerCase() === requestSceneTitle.toLocaleLowerCase()) + : null; if (anchoredChapterScene) { setResolveDiagnostics({ result: "Matched", - chapterId: anchoredChapterScene.chapter.chapterId, - sceneId: anchoredChapterScene.scene.sceneId + chapterId: anchoredChapter.chapterId, + sceneId: anchoredChapterScene.sceneId }); - await loadResolvedCompanionData(book.bookId, anchoredChapterScene.scene.sceneId, anchoredChapterScene.chapter.chapterId, "Anchor", "ChapterID Anchor"); + await loadResolvedCompanionData(book.bookId, anchoredChapterScene.sceneId, anchoredChapter.chapterId, "Anchor", "ChapterID Anchor"); return true; } - if (!detectedSceneAnchorId) { + if (anchoredChapter && !detectedSceneAnchorId) { currentResolutionAnchorType = "ChapterID Anchor"; setResolvedScene(null, detectedChapterAnchorId, "Chapter anchor"); setResolveDiagnostics({ @@ -1544,6 +1545,16 @@ chapterId: detectedChapterAnchorId, sceneId: null }); + } else if (!detectedSceneAnchorId) { + storedAnchorInvalid = true; + currentResolutionAnchorType = "ChapterID Anchor"; + setPlotDirectorSceneStatus("Not found in PlotDirector"); + setDocumentMessage("Stored PlotDirector ID is invalid."); + setResolveDiagnostics({ + result: "Error", + chapterId: detectedChapterAnchorId, + sceneId: null + }); } } @@ -1555,11 +1566,13 @@ if (!resolveResponse?.matched || !resolveResponse.sceneId) { const hadInvalidAnchor = storedAnchorInvalid; const matchedChapterId = Number.parseInt(resolveResponse?.chapterId || "", 10); - const chapterMatched = !!resolveResponse?.chapterMatched || (Number.isInteger(matchedChapterId) && matchedChapterId > 0); + const anchorChapterId = Number.isInteger(resolvedChapterId) && resolvedChapterId > 0 ? resolvedChapterId : null; + const unmatchedChapterId = Number.isInteger(matchedChapterId) && matchedChapterId > 0 ? matchedChapterId : anchorChapterId; + const chapterMatched = !!resolveResponse?.chapterMatched || !!unmatchedChapterId; resetPlotDirectorScene("Not found in PlotDirector"); storedAnchorInvalid = hadInvalidAnchor; if (chapterMatched) { - setResolvedScene(null, matchedChapterId, "Chapter title"); + setResolvedScene(null, unmatchedChapterId, unmatchedChapterId === anchorChapterId ? "Chapter anchor" : "Chapter title"); setPlotDirectorSceneStatus("Scene not found in PlotDirector."); } setDocumentMessage(hadInvalidAnchor @@ -1569,7 +1582,7 @@ : "This chapter does not exist in PlotDirector. Create or link the chapter first."); setResolveDiagnostics({ result: "Not matched", - chapterId: chapterMatched ? matchedChapterId : resolveResponse?.chapterId, + chapterId: chapterMatched ? unmatchedChapterId : resolveResponse?.chapterId, sceneId: resolveResponse?.sceneId }); if (!hadInvalidAnchor) {