Skip to content

Commit

Permalink
Update links to chapters when chapter IDs change.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Mar 3, 2024
1 parent 0a53feb commit d85ff74
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 15 additions & 12 deletions src/lib/components/page/Chapter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
},
);
}
}}
Expand All @@ -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}
/>
</Muted>
Expand All @@ -320,7 +323,7 @@
setChapter(
edition,
chapter,
chapter.asNumbered(on)
chapter.asNumbered(on),
)}
>
<ChapterNumber
Expand All @@ -347,7 +350,7 @@
setChapter(
edition,
chapter,
chapter.withRenamedAuthor(index, text)
chapter.withRenamedAuthor(index, text),
)}
remove={(index) =>
setChapter(edition, chapter, chapter.withoutAuthor(index))}
Expand Down Expand Up @@ -375,7 +378,7 @@
? setChapter(
edition,
chapter,
chapter.withAST(ast)
chapter.withAST(ast),
)
: undefined}
chapter={true}
Expand Down Expand Up @@ -426,7 +429,7 @@
<Permissions
uids={chapter.uids}
inheriteduids={Array.from(
new Set([...$edition.uids, ...($book ? $book.uids : [])])
new Set([...$edition.uids, ...($book ? $book.uids : [])]),
)}
atleastone={false}
writable={isChapterEditable()}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/page/Contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ export function getRoot() {
export function setChapter(
store: EditionStore,
previous: Chapter,
chapter: Chapter,
revised: Chapter,
) {
// Don't set if its the same chapter.
if (previous === chapter) return;
if (previous === revised) return;
const edition = get(store);
if (edition) store.set(edition.withRevisedChapter(previous, chapter));
if (edition) store.set(edition.withRevisedChapter(previous, revised));
}

function isRouteWrite(): boolean {
Expand Down
38 changes: 29 additions & 9 deletions src/lib/components/page/EditionLoader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
export let write: boolean;
let currentEditionID: string | undefined;
let auth = getUser();
let book = getBook();
let edition = getEdition();
Expand Down Expand Up @@ -43,12 +45,12 @@
error = 'Unknown book.';
return;
} else {
const editionNumber = $page.params.editionid;
currentEditionID = $page.params.editionid;
const latestPublished = !write;
// Figure out which edition to load.
let editionID;
if (editionNumber === undefined) {
if (currentEditionID === undefined) {
if (latestPublished) {
editionID = $book.getLatestPublishedEditionID();
if (editionID === undefined)
Expand All @@ -57,9 +59,11 @@
editionID = $book.getLatestEditionID();
}
} else {
editionID = $book.getEditionNumberID(parseFloat(editionNumber));
editionID = $book.getEditionNumberID(
parseFloat(currentEditionID),
);
if (editionID === undefined)
error = `There is no ${editionNumber} edition of this book`;
error = `There is no ${currentEditionID} edition of this book`;
}
// Listen to the doc for changes and listen to all of its chapters.
Expand All @@ -75,7 +79,7 @@
edition.set(undefined);
error = 'Unable to load edition';
}
}
},
);
chaptersUnsub = listenToChapters(
Expand All @@ -86,16 +90,32 @@
for (const [ref, text] of chapters)
chapterText.set(ref.id, text);
// Update the chapter text in the current edition.
if ($edition)
if ($edition) {
edition.set($edition.withChapterText(chapterText));
}
}
},
);
}
}
}
// Whenever the book or page changes, change the listener.
$: if ($page && $auth && $auth.user !== null) listen();
// Remember the chapter text whenever the edition changes, so we don't overwrite stale text.
// when other things change.
$: if ($edition) {
for (const chapter of $edition.chapters) {
if (chapter.ref && chapter.text !== null)
chapterText.set(chapter.ref.id, chapter.text);
}
}
// Whenever the book changes, change the listener.
$: if (
(currentEditionID === undefined ||
$page.params.editionid !== currentEditionID) &&
$auth &&
$auth.user !== null
)
listen();
// When unmounted, unset the stores — no longer viewing a book.
onDestroy(() => {
Expand Down
41 changes: 34 additions & 7 deletions src/lib/models/book/Edition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d85ff74

Please sign in to comment.