Skip to content

Commit

Permalink
update marks store: Add userId and location
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakamiShinyu committed Sep 17, 2024
1 parent afbc20b commit 4dcda37
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/viewer/src/models/document-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ko, { Observable } from "knockout";
import PageStyle from "./page-style";
import urlParameters from "../stores/url-parameters";
import stringUtil from "../utils/string-util";
import { marksStore } from "../viewmodels/marks-store";

function getDocumentOptionsFromURL(): DocumentOptionsType {
const srcUrls = urlParameters.getParameter("src");
Expand Down Expand Up @@ -111,6 +112,7 @@ class DocumentOptions {
encodeURIComponent,
);
urlParameters.setParameter("f", encoded);
marksStore.putLocation(encoded);
}
});

Expand Down
41 changes: 34 additions & 7 deletions packages/viewer/src/viewmodels/marks-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,33 +877,37 @@ export interface MarkJson {
}

export interface MarksStoreInterface {
init(documentId: string): Promise<void>;
init(epubId: string, userId: string): Promise<void>;
persistMark(mark: MarkJson): Promise<string>; // persists and return id
getMark(id: string): Promise<MarkJson>;
updateMark(mark: MarkJson): Promise<void>;
removeMark(mark: MarkJson): Promise<void>;
allMarks(): Promise<MarkJson[]>;
allMarksIterator?(): Promise<AsyncIterable<MarkJson>>; //
putLocation(cfi: string): Promise<void>;
getLocation(): Promise<string | null>;
}

let seqId = 0;

export class URLMarksStore implements MarksStoreInterface {
private markArray: MarkJson[] = [];
private markKeyToArrayIndex: Map<string, number>;
public documentId = "";
public epubId = "";
public userId = "";

constructor() {
this.markKeyToArrayIndex = new Map();
}

async init(documentId: string): Promise<void> {
async init(epubId: string, userId: string): Promise<void> {
const marksParam = urlParameters.getParameter("mark");
marksParam.forEach((m) => {
const mark = this.urlStringToMark(m);
this.pushMarkInternal(mark, "doNotAddToUrl", "persist");
});
this.documentId = documentId;
this.epubId = epubId;
this.userId = userId;
}

async persistMark(mark: MarkJson): Promise<string> {
Expand Down Expand Up @@ -941,6 +945,12 @@ export class URLMarksStore implements MarksStoreInterface {
})();
}

async putLocation(cfi: string): Promise<void> {}

async getLocation(): Promise<string | null> {
return null;
}

private markToURLString(mark: MarkJson): string {
const memo = mark.memo ? encodeURIComponent(mark.memo) : "";
const markedText = mark.markedText
Expand Down Expand Up @@ -1091,7 +1101,10 @@ export class MarksStoreFacade {
if (viewer) {
this.viewer = viewer;
}
if (!this.viewerOptions || !this.viewerOptions.enableMarker()) {
if (
!this.viewerOptions ||
(!this.viewerOptions.enableMarker() && !window["marksStorePlugin"])
) {
return;
}

Expand All @@ -1103,8 +1116,11 @@ export class MarksStoreFacade {
} else {
this.actualStore = new URLMarksStore();
}
const documentId = urlParameters.getParameter("src").join();
await this.actualStore.init(documentId);
const urlSearchParams = new URLSearchParams(window.location.search);
const epubId =
urlSearchParams.get("epubId") || urlParameters.getParameter("src").join();
const userId = urlSearchParams.get("userId") || "";
await this.actualStore.init(epubId, userId);
this.initialized = true;
}

Expand Down Expand Up @@ -1222,6 +1238,17 @@ export class MarksStoreFacade {
}
this.marksBox.addMarkToList(mark);
}

async putLocation(cfi: string): Promise<void> {
if (!this.initialized) return;
await this.actualStore.putLocation(cfi);
}

async getLocation(): Promise<string | null> {
if (!this.initialized) return null;
const cfi = await this.actualStore.getLocation();
return cfi;
}
}

export const marksStore = new MarksStoreFacade();
Expand Down
25 changes: 22 additions & 3 deletions packages/viewer/src/viewmodels/viewer-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ class ViewerApp {
// "false" - disable restore view settings
// not specified - enable restore view settings if saved view settings exist
let restoreView = urlParameters.getParameter("restoreView")[0];
const fragmentSpecifiedAtStart = urlParameters.hasParameter("f");
if (restoreView !== "false") {
// The viewing location specified by "f=epubcfi(…)" takes precedence over the saved one
if (!urlParameters.hasParameter("f")) {
if (!fragmentSpecifiedAtStart) {
if (urlParameters.restoreViewSettings()) {
// If saved view settings exist and restored to the URL parameters,
// the "restoreView" value is also restored and it must be "true".
Expand Down Expand Up @@ -248,7 +249,18 @@ class ViewerApp {
}

this.marksStore = marksStore;
this.marksStore.init(this.viewerOptions, this.viewer);
this.marksStore.init(this.viewerOptions, this.viewer).then(() => {
if (!fragmentSpecifiedAtStart) {
this.marksStore.getLocation().then((f) => {
if (f) {
urlParameters.setParameter("f", f);
}
if (window.location.href !== urlParameters.storedUrl) {
window.location.reload();
}
});
}
});
this.marksMenuStatus = marksStore.menuStatus;
this.marksBox = marksStore.marksBox;
this.viewer.rerenderTrigger.subscribe(() => {
Expand All @@ -258,7 +270,14 @@ class ViewerApp {
this.viewer.loadDocument(this.documentOptions);

window.onhashchange = (): void => {
if (window.location.href != urlParameters.storedUrl) {
if (
window.location.href !== urlParameters.storedUrl &&
!(
window["marksStorePlugin"] &&
!fragmentSpecifiedAtStart &&
urlParameters.hasParameter("f")
)
) {
// Reload when address bar change is detected
window.location.reload();
}
Expand Down

0 comments on commit 4dcda37

Please sign in to comment.