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

Add epubReadingSystem property to frame documents #80

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions navigator-html-injectables/src/helpers/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import type { getCssSelector } from "css-selector-generator";

type BlockedEventData = [0, Function, any[], any[]] | [1, Event, EventTarget];

export interface EPUBReadingSystem {
name: string;
version: string;
layoutStyle: "paginated" | "scrolling"; // Technically, more are allowed
hasFeature: (feature: string, version?: string) => boolean;
}

// This is what is injected into the HTML documents
export interface ReadiumWindow extends Window {
_readium_blockEvents: boolean;
Expand All @@ -13,6 +20,7 @@ export interface ReadiumWindow extends Window {
_readium_cssSelectorGenerator: {
getCssSelector: typeof getCssSelector;
};
navigator: Navigator & { epubReadingSystem: EPUBReadingSystem };
}

export function deselect(wnd: ReadiumWindow) {
Expand Down
2 changes: 2 additions & 0 deletions navigator-html-injectables/src/modules/setup/FixedSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class FixedSetup extends Setup {
mount(wnd: ReadiumWindow, comms: Comms): boolean {
if(!super.mount(wnd, comms)) return false;

wnd.navigator.epubReadingSystem.layoutStyle = "paginated"; // TODO: what if we support scrolling?

const style = wnd.document.createElement("style");
style.id = FIXED_STYLE_ID;
style.dataset.readium = "true";
Expand Down
31 changes: 30 additions & 1 deletion navigator-html-injectables/src/modules/setup/Setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Comms } from "../../comms/comms";
import { ReadiumWindow } from "../../helpers/dom";
import { ReadiumWindow, EPUBReadingSystem } from "../../helpers/dom";
import { Module } from "../Module";
import { ModuleName } from "../ModuleLibrary";

Expand Down Expand Up @@ -72,6 +72,35 @@ export abstract class Setup extends Module {
false
);

// Add reading system property to navigator
Reflect.defineProperty(wnd.navigator, "epubReadingSystem", {
value: {
name: "readium-ts-toolkit",
version: import.meta.env.PACKAGE_VERSION,
hasFeature: (feature: string, _version = "") => {
switch (feature) {
case "dom-manipulation":
return true;
case "layout-changes":
return true;
case "touch-events":
return true;
case "mouse-events":
return true;
case "keyboard-events":
return true;
case "spine-scripting":
return true;
case "embedded-web-content":
return true;
default:
return false;
}
}
} as EPUBReadingSystem,
writable: false
});

// Add all currently active animations and cancel them
if("getAnimations" in wnd.document) {
wnd.document.getAnimations().forEach((a) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ export class ColumnSnapper extends Snapper {
this.comms = comms;
if(!super.mount(wnd, comms)) return false;

wnd.navigator.epubReadingSystem.layoutStyle = "paginated";

// Add styling to hide the scrollbar
const d = wnd.document.createElement("style");
d.dataset.readium = "true";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class ScrollSnapper extends Snapper {
this.wnd = wnd;
this.comms = comms;

wnd.navigator.epubReadingSystem.layoutStyle = "scrolling";

// Add styling to hide the scrollbar
const style = wnd.document.createElement("style");
style.dataset.readium = "true";
Expand Down
3 changes: 2 additions & 1 deletion navigator-html-injectables/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"forceConsistentCasingInFileNames": true,
// emit only .d.ts
"noEmit": false,
"emitDeclarationOnly": true
"emitDeclarationOnly": true,
"types": ["vite/client"]
},
}
5 changes: 5 additions & 0 deletions navigator-html-injectables/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from "path";
import { defineConfig } from "vite";
import packageJson from "./package.json";

export default defineConfig({
build: {
Expand All @@ -8,5 +9,9 @@ export default defineConfig({
name: "navigator-html-injectables",
fileName: "index"
}
},
define: {
"import.meta.env.PACKAGE_NAME": JSON.stringify(packageJson.name),
"import.meta.env.PACKAGE_VERSION": JSON.stringify(packageJson.version),
}
});
3 changes: 2 additions & 1 deletion navigator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"forceConsistentCasingInFileNames": true,
// emit only .d.ts
"noEmit": false,
"emitDeclarationOnly": true
"emitDeclarationOnly": true,
"types": ["vite/client"]
}
}
5 changes: 5 additions & 0 deletions navigator/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from "path";
import { defineConfig } from "vite";
import libAssetsPlugin from "@laynezh/vite-plugin-lib-assets";
import packageJson from "./package.json";

export default defineConfig({
plugins: [
Expand All @@ -15,5 +16,9 @@ export default defineConfig({
name: "navigator",
fileName: "index"
}
},
define: {
"import.meta.env.PACKAGE_NAME": JSON.stringify(packageJson.name),
"import.meta.env.PACKAGE_VERSION": JSON.stringify(packageJson.version),
}
});
3 changes: 2 additions & 1 deletion shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"forceConsistentCasingInFileNames": true,
// emit only .d.ts
"noEmit": false,
"emitDeclarationOnly": true
"emitDeclarationOnly": true,
"types": ["vite/client"]
},
}
5 changes: 5 additions & 0 deletions shared/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from "path";
import { defineConfig } from "vite";
import packageJson from "./package.json";

export default defineConfig({
build: {
Expand All @@ -8,5 +9,9 @@ export default defineConfig({
name: "shared",
fileName: "index"
}
},
define: {
"import.meta.env.PACKAGE_NAME": JSON.stringify(packageJson.name),
"import.meta.env.PACKAGE_VERSION": JSON.stringify(packageJson.version),
}
});
Loading