Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.4.1 #757

Merged
merged 21 commits into from
Jan 31, 2024
Merged

2.4.1 #757

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7b2ed1c
feat: get landmarks from the reader instance
gotson Nov 15, 2023
464242e
Merge pull request #661 from gotson/feat/gh640-landmarks
aferditamuriqi Nov 15, 2023
a9b2e39
feat: expose pagelist on the reader instance
gotson Nov 17, 2023
38e8ba5
initial user settings now accept the same 'appearance' values as appl…
gotson Nov 21, 2023
2cca9f6
prevent error when toc is not defined in the publication
gotson Nov 23, 2023
43e3d77
align pdfjs version between package.json and PDFNavigator.ts
gotson Nov 23, 2023
0552d19
fix: properly unregister keydown event on top-level document on stop
gotson Nov 27, 2023
6187968
fix: draw epub2 page breaks
gotson Dec 6, 2023
10202b3
feat: expose publicationLayout in the reader
gotson Dec 13, 2023
32bddbb
Merge pull request #665 from gotson/feat/reader-pagelist
aferditamuriqi Jan 31, 2024
293f01e
Merge pull request #675 from gotson/feat/initial-appearance
aferditamuriqi Jan 31, 2024
87c1123
Merge pull request #678 from gotson/fix/gh626-missing-toc
aferditamuriqi Jan 31, 2024
0022920
Merge pull request #682 from gotson/fix/keyboard-removelistener
aferditamuriqi Jan 31, 2024
8bb77d1
Merge pull request #704 from gotson/fix/page-break-epub2
aferditamuriqi Jan 31, 2024
c006c64
Merge pull request #715 from gotson/feat/expose-layout
aferditamuriqi Jan 31, 2024
c96c121
Merge pull request #680 from gotson/fix/gh643-pdfjs
aferditamuriqi Jan 31, 2024
fb33796
checks viewport parsing if it already contains "px" in height and width
aferditamuriqi Jan 31, 2024
47df832
fixes issue which caused text to stay scrambled when switching betwee…
aferditamuriqi Jan 31, 2024
d9ad625
Merge pull request #755 from d-i-t-a/bugfix/viewport-parsing
aferditamuriqi Jan 31, 2024
19a9848
Merge pull request #756 from d-i-t-a/bugfix/scrambling-in-content-pro…
aferditamuriqi Jan 31, 2024
9c0a613
version bump
aferditamuriqi Jan 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
801 changes: 73 additions & 728 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@d-i-t-a/reader",
"version": "2.4.0",
"version": "2.4.1",
"description": "A viewer application for EPUB files.",
"repository": "https://github.com/d-i-t-a/R2D2BC",
"license": "Apache-2.0",
Expand Down Expand Up @@ -36,7 +36,7 @@
"lodash.clonedeep": "^4.5.0",
"loglevel": "^1.8.0",
"mark.js": "^8.11.1",
"pdfjs-dist": "3.11.174",
"pdfjs-dist": "2.14.305",
"promise-polyfill": "^8.2.0",
"r2-shared-js": "^1.0.51",
"recursive-readdir": "^2.2.2",
Expand Down
6 changes: 3 additions & 3 deletions src/model/Publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Publication extends R2Publication {
if (this.sample?.isSampleRead && this.positions?.length > 0) {
return this.limitedTOC();
}
return this.TOC;
return this.TOC || [];
}

private limitedTOC() {
Expand All @@ -74,7 +74,7 @@ export class Publication extends R2Publication {
}
}

let toc = this.TOC.map((item) => {
let toc = this.TOC?.map((item) => {
if (item.Href) {
const positions = this.positionsByHref(this.getRelativeHref(item.Href));
if (positions?.length > 0) {
Expand All @@ -98,7 +98,7 @@ export class Publication extends R2Publication {
}
return item;
});
return toc;
return toc || [];
}

get landmarks() {
Expand Down
54 changes: 29 additions & 25 deletions src/model/user-settings/UserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ export class UserSettings implements IUserSettings {
log.log(settings.verticalScroll);
}
if (initialUserSettings.appearance) {
settings.appearance = UserSettings.appearanceValues.findIndex(
(el: any) => el === initialUserSettings.appearance
);
settings.appearance = UserSettings.parseAppearanceSetting(initialUserSettings.appearance)
let prop = settings.userProperties.getByRef(ReadiumCSS.APPEARANCE_REF);
if (prop) {
prop.value = settings.appearance;
Expand Down Expand Up @@ -989,28 +987,7 @@ export class UserSettings implements IUserSettings {

async applyUserSettings(userSettings: Partial<UserSettings>): Promise<void> {
if (userSettings.appearance) {
let a: string;
if (
userSettings.appearance === "day" ||
userSettings.appearance === "readium-default-on"
) {
a = UserSettings.appearanceValues[0];
} else if (
userSettings.appearance === "sepia" ||
userSettings.appearance === "readium-sepia-on"
) {
a = UserSettings.appearanceValues[1];
} else if (
userSettings.appearance === "night" ||
userSettings.appearance === "readium-night-on"
) {
a = UserSettings.appearanceValues[2];
} else {
a = userSettings.appearance;
}
this.appearance = UserSettings.appearanceValues.findIndex(
(el: any) => el === a
);
this.appearance = UserSettings.parseAppearanceSetting(userSettings.appearance)
let prop = this.userProperties?.getByRef(ReadiumCSS.APPEARANCE_REF);
if (prop) {
prop.value = this.appearance;
Expand Down Expand Up @@ -1138,6 +1115,33 @@ export class UserSettings implements IUserSettings {
}
}

private static parseAppearanceSetting(
inputSetting: InitialUserSettings["appearance"]
): number {
let a: string;
if (
inputSetting === "day" ||
inputSetting === "readium-default-on"
) {
a = UserSettings.appearanceValues[0];
} else if (
inputSetting === "sepia" ||
inputSetting === "readium-sepia-on"
) {
a = UserSettings.appearanceValues[1];
} else if (
inputSetting === "night" ||
inputSetting === "readium-night-on"
) {
a = UserSettings.appearanceValues[2];
} else {
a = inputSetting;
}
return UserSettings.appearanceValues.findIndex(
(el: any) => el === a
);
}

async scroll(scroll: boolean): Promise<void> {
const position = this.view?.getCurrentPosition();
this.verticalScroll = scroll;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/pagebreak/PageBreakModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export class PageBreakModule implements ReaderModule {
if (pageBreaks?.length === 0) {
pageBreaks = body?.querySelectorAll("[epub\\:type='pagebreak']");
}
if (pageBreaks?.length === 0) {
pageBreaks = body?.querySelectorAll("[role='doc-pagebreak']");
}
let self = this;

function getCssSelector(element: Element): string {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/protection/ContentProtectionModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ export class ContentProtectionModule implements ReaderModule {
this.toggleRect(rect, this.securityContainer, this.isHacked)
);
}
handleResize() {
async handleResize() {
if (this.properties?.enableObfuscation) {
const onDoResize = debounce(() => {
this.calcRects(this.rects);
Expand All @@ -946,7 +946,7 @@ export class ContentProtectionModule implements ReaderModule {
this.toggleRect(rect, this.securityContainer, this.isHacked)
);
}
}, 10);
}, 50);
if (this.rects) {
this.observe();
onDoResize();
Expand Down Expand Up @@ -1126,8 +1126,8 @@ export class ContentProtectionModule implements ReaderModule {
}
return true;
}
copyToClipboard(textToClipboard) {
textToClipboard = textToClipboard.substring(
copyToClipboard(textToClipboard: string | undefined) {
textToClipboard = textToClipboard?.substring(
0,
this.properties?.charactersToCopy ?? 0
);
Expand Down
28 changes: 21 additions & 7 deletions src/navigator/IFrameNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ export class IFrameNavigator extends EventEmitter implements Navigator {
this.iframes.forEach((iframe) => {
removeEventListenerOptional(iframe, "resize", this.onResize);
});

if (this.didInitKeyboardEventHandler)
this.keyboardEventHandler.removeEvents(document);
}
spreads: HTMLDivElement;
firstSpread: HTMLDivElement;
Expand Down Expand Up @@ -2257,6 +2260,12 @@ export class IFrameNavigator extends EventEmitter implements Navigator {
tableOfContents(): any {
return this.publication.tableOfContents;
}
landmarks(): any {
return this.publication.landmarks;
}
pageList(): any {
return this.publication.pageList;
}
readingOrder(): any {
return this.publication.readingOrder;
}
Expand Down Expand Up @@ -2567,8 +2576,12 @@ export class IFrameNavigator extends EventEmitter implements Navigator {
return obj;
}, {});
if (parseInt(obj["height"]) !== 0 || parseInt(obj["width"]) !== 0) {
height = obj["height"] + "px";
width = obj["width"] + "px";
height = obj["height"].endsWith("px")
? obj["height"]
: obj["height"] + "px";
width = obj["width"].endsWith("px")
? obj["width"]
: obj["width"] + "px";
}
}
}
Expand Down Expand Up @@ -2633,7 +2646,11 @@ export class IFrameNavigator extends EventEmitter implements Navigator {
if (oldPosition) {
this.view?.goToProgression(oldPosition);
}
await this.updatePositionInfo(false);
this.updatePositionInfo(false);

if (this.contentProtectionModule !== undefined) {
await this.contentProtectionModule.handleResize();
}

if (this.annotationModule !== undefined) {
await this.annotationModule.handleResize();
Expand All @@ -2650,14 +2667,11 @@ export class IFrameNavigator extends EventEmitter implements Navigator {
if (this.pageBreakModule !== undefined) {
await this.pageBreakModule.handleResize();
}
if (this.contentProtectionModule !== undefined) {
this.contentProtectionModule.handleResize();
}
if (this.lineFocusModule !== undefined) {
this.lineFocusModule.handleResize();
}
if (this.historyModule !== undefined) {
this.historyModule.handleResize();
await this.historyModule.handleResize();
}
}, 150);
}
Expand Down
2 changes: 2 additions & 0 deletions src/navigator/Navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ interface Navigator {
deactivateMarker?(): any;

tableOfContents(): any;
landmarks(): any;
pageList(): any;

readingOrder(): any;

Expand Down
2 changes: 2 additions & 0 deletions src/navigator/PDFNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ export class PDFNavigator extends EventEmitter implements Navigator {
tableOfContents(): any {
return this.publication.tableOfContents;
}
landmarks(): any {};
pageList(): any {};

//TODO:
currentResource(): any {}
Expand Down
12 changes: 12 additions & 0 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ export default class D2Reader {
get tableOfContents() {
return convertAndCamel(this.navigator.tableOfContents()) ?? [];
}
/** Landmarks */
get landmarks() {
return convertAndCamel(this.navigator.landmarks()) ?? [];
}
/** Page List */
get pageList() {
return convertAndCamel(this.navigator.pageList()) ?? [];
}
/** Reading Order or Spine */
get readingOrder() {
return convertAndCamel(this.navigator.readingOrder()) ?? [];
Expand All @@ -586,6 +594,10 @@ export default class D2Reader {
return this.annotationModule?.getAnnotations();
}

get publicationLayout() {
return this.navigator.publication.layout;
}

/** History */
get history() {
return this.historyModule?.history;
Expand Down
108 changes: 63 additions & 45 deletions src/utils/KeyboardEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,59 +35,77 @@ export default class KeyboardEventHandler {
}
};

public removeEvents = (element: HTMLElement | Document | null): void => {
if (element) {
const self = this;
element.removeEventListener("focusin", this.onFocusIn(self), true);
element.removeEventListener("keydown", this.onKeyDown(self), false);
}
};

public focusin = (element: HTMLElement | Document): void => {
const self = this;
element.addEventListener(
"focusin",
function (event: KeyboardEvent) {
self.navigator.view?.snap(event.target as HTMLElement);
},
true
);
element.addEventListener("focusin", this.onFocusIn(self), true);
};

public keydown = (element: HTMLElement | Document): void => {
const self = this;
if (!this.navigator.rights.customKeyboardEvents) {
element.addEventListener(
"keydown",
function (event: KeyboardEvent) {
// Ignore input elements
const eventTarget = event.target as HTMLElement;
if (/input|select|option|textarea/i.test(eventTarget.tagName)) {
return;
}
element.addEventListener("keydown", this.onKeyDown(self), false);
}
};

// Ignore when active text selection
const ownerDocument = (eventTarget.ownerDocument ||
eventTarget) as HTMLDocument;
const ownerWindow = ownerDocument.defaultView as Window;
const selection = ownerWindow.getSelection() as Selection;
if (!selection.isCollapsed) {
return;
}
// store the generated event handlers, so they can be returned
// when removing the event listeners
private handlers = {};

const key = event.key;
switch (key) {
case "ArrowRight":
self.onForwardSwipe(event);
break;
case "ArrowLeft":
private onFocusIn(self: this) {
return (
this.handlers["onFocusIn"] ||
(this.handlers["onFocusIn"] = function (event: KeyboardEvent) {
self.navigator.view?.snap(event.target as HTMLElement);
})
);
}

private onKeyDown(self: this) {
return (
this.handlers["onKeyDown"] ||
(this.handlers["onKeyDown"] = function(event: KeyboardEvent) {
// Ignore input elements
const eventTarget = event.target as HTMLElement;
if (/input|select|option|textarea/i.test(eventTarget.tagName)) {
return;
}

// Ignore when active text selection
const ownerDocument = (eventTarget.ownerDocument ||
eventTarget) as HTMLDocument;
const ownerWindow = ownerDocument.defaultView as Window;
const selection = ownerWindow.getSelection() as Selection;
if (!selection.isCollapsed) {
return;
}

const key = event.key;
switch (key) {
case "ArrowRight":
self.onForwardSwipe(event);
break;
case "ArrowLeft":
self.onBackwardSwipe(event);
break;
}
switch (event.code) {
case "Space":
if (event.ctrlKey) {
self.onBackwardSwipe(event);
break;
}
switch (event.code) {
case "Space":
if (event.ctrlKey) {
self.onBackwardSwipe(event);
} else {
self.onForwardSwipe(event);
}
break;
}
},
false
);
}
};
} else {
self.onForwardSwipe(event);
}
break;
}
})
);
}
}
Loading
Loading