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

RTL Support #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/PageFlip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ export class PageFlip extends EventObject {
this.trigger('changeOrientation', this, newOrientation);
}

/**
* Call a page direction change event trigger. Update UI and rendering area
*
* @param {boolean} newRTL - New page direction
*/
public updateRTL(newRTL: boolean): void {
this.ui.setRTLStyle(newRTL);
this.update();
this.trigger('changeRTL', this, newRTL);
}

/**
* Get the total number of pages in a book
*
Expand Down
14 changes: 14 additions & 0 deletions src/Render/Render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export abstract class Render {
protected direction: FlipDirection = null;
/** Current book orientation */
protected orientation: Orientation = null;
protected rtl: boolean = null;
/** Сurrent state of the shadows */
protected shadow: Shadow = null;
/** Сurrent animation process */
Expand Down Expand Up @@ -191,11 +192,17 @@ export abstract class Render {
public update(): void {
this.boundsRect = null;
const orientation = this.calculateBoundsRect();
const rtl = this.app.getSettings().rtl;

if (this.orientation !== orientation) {
this.orientation = orientation;
this.app.updateOrientation(orientation);
}

if (this.rtl !== rtl) {
this.rtl = rtl;
this.app.updateRTL(rtl);
}
}

/**
Expand Down Expand Up @@ -337,6 +344,13 @@ export abstract class Render {
return this.orientation;
}

/**
* Get current book direction
*/
public getRTL(): boolean {
return this.rtl;
}

/**
* Set page area while flipping
*
Expand Down
3 changes: 3 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface FlipSetting {

/** if this value is true, flipping by clicking on the whole book will be locked. Only on corners */
disableFlipByClick: boolean;

rtl: boolean;
}

export class Settings {
Expand All @@ -82,6 +84,7 @@ export class Settings {
useMouseEvents: true,
showPageCorners: true,
disableFlipByClick: false,
rtl: false,
};

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Style/stPageFlip.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@
position: absolute;
left: 0;
top: 0;
}

/* rtl */
.stf__wrapper.--rtl {
transform: scaleX(-1);
}
.stf__wrapper.--rtl .stf__item > * {
transform: scaleX(-1);
}
18 changes: 16 additions & 2 deletions src/UI/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ export abstract class UI {
this.update();
}

/**
* Updates styles based on book direction
*
* @param {boolean} rtl - New book direction
*/
public setRTLStyle(rtl: boolean): void {
this.wrapper.classList.remove('--rtl');

if (rtl) {
this.wrapper.classList.add('--rtl');
}

this.update();
}

protected removeHandlers(): void {
window.removeEventListener('resize', this.onResize);

Expand Down Expand Up @@ -159,9 +174,8 @@ export abstract class UI {
*/
private getMousePos(x: number, y: number): Point {
const rect = this.distElement.getBoundingClientRect();

return {
x: x - rect.left,
x: this.app.getSettings().rtl ? rect.width - (x - rect.left) : x - rect.left,
y: y - rect.top,
};
}
Expand Down