From d85ff74f31cad015b7caa446d84b38478749dd30 Mon Sep 17 00:00:00 2001 From: "Amy J. Ko" Date: Sun, 3 Mar 2024 11:56:03 -0800 Subject: [PATCH] Update links to chapters when chapter IDs change. --- CHANGELOG.md | 1 + src/lib/components/page/Chapter.svelte | 27 +++++++------ src/lib/components/page/Contexts.ts | 6 +-- src/lib/components/page/EditionLoader.svelte | 38 +++++++++++++----- src/lib/models/book/Edition.ts | 41 ++++++++++++++++---- 5 files changed, 82 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5c23fe1..d3956db6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ## Fixed - Fixed issue where image alignment setting was not saved. +- Update links to chapters when chapter IDs change. # 0.6.5 - 2024-02-25 diff --git a/src/lib/components/page/Chapter.svelte b/src/lib/components/page/Chapter.svelte index 1baba47e..060c04ee 100644 --- a/src/lib/components/page/Chapter.svelte +++ b/src/lib/components/page/Chapter.svelte @@ -121,7 +121,7 @@ history.pushState( '', document.title, - window.location.pathname + window.location.search + window.location.pathname + window.location.search, ); observer.unobserve(entry.target); highlightedID = undefined; @@ -169,7 +169,7 @@ // If there's a word we're trying to highlight in the URL path, scroll to the corresponding match. if (location.search) { const highlights = document.getElementsByClassName( - 'bookish-text bookish-content-highlight' + 'bookish-text bookish-content-highlight', ); const num = getHighlightedNumber(); if ( @@ -288,16 +288,19 @@ text={chapter.getID()} label="Chapter URL ID editor" save={// After the ID is edited, reload the page with the new URL. - (newChapterID) => { + async (newChapterID) => { if ($book) { setChapter( edition, chapter, - chapter.withChapterID(newChapterID) + chapter.withChapterID(newChapterID), ); // Navigate to the new ID goto( - `/write/${$book.getID()}/${editionNumber}/${newChapterID}` + `/write/${$book.getID()}/${editionNumber}/${newChapterID}`, + { + keepFocus: true, + }, ); } }} @@ -306,9 +309,9 @@ !/^[a-zA-Z0-9]+$/.test(newChapterID) ? 'Chapter IDs must be one or more letters or numbers' : chapter.getID() !== newChapterID && - $edition?.hasChapter(newChapterID) - ? "There's already a chapter that has this ID." - : undefined} + $edition?.hasChapter(newChapterID) + ? "There's already a chapter that has this ID." + : undefined} saveOnExit={true} /> @@ -320,7 +323,7 @@ setChapter( edition, chapter, - chapter.asNumbered(on) + chapter.asNumbered(on), )} > setChapter(edition, chapter, chapter.withoutAuthor(index))} @@ -375,7 +378,7 @@ ? setChapter( edition, chapter, - chapter.withAST(ast) + chapter.withAST(ast), ) : undefined} chapter={true} @@ -426,7 +429,7 @@ { diff --git a/src/lib/models/book/Edition.ts b/src/lib/models/book/Edition.ts index aa999932..6f45bb22 100644 --- a/src/lib/models/book/Edition.ts +++ b/src/lib/models/book/Edition.ts @@ -511,13 +511,40 @@ export default class Edition { } withRevisedChapter(previous: Chapter, edited: Chapter) { - const index = this.chapters.indexOf(previous); - if (index < 0) return this; - return this.withChapters([ - ...this.chapters.slice(0, index), - edited, - ...this.chapters.slice(index + 1), - ]); + return this.withChapters( + this.chapters.map((chapter) => { + // Is this the edited chapter? Return the edited one. + if (chapter === previous) return edited; + + // If it's a different chapter, did the chapter change it's ID? Change all links to the chapter to have the new ID. + if (previous.id !== edited.id) { + // Start with the chapter as is. + let newChapter = chapter; + + // Replace the chapter ID in the chapter's text. + if (newChapter.text !== null) { + const revisedText = newChapter.text.replaceAll( + new RegExp( + `\\|(${previous.id})(:[a-zA-Z0-9]+)?\\]`, + 'g', + ), + `|${edited.id}$2]`, + ); + if (revisedText !== chapter.text) { + newChapter = newChapter.withText(revisedText); + console.log( + 'Updated chapter text: ' + newChapter.text, + ); + } + } + + // Return the possibily revised chapter. + return newChapter; + } + // Otherwise, just return the chapter as is. + else return chapter; + }), + ); } withoutRefs() {