Skip to content

Commit

Permalink
Fixed a bug with resizing the window after updating book state
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodlik committed Apr 18, 2021
1 parent 6b5d643 commit c5d3246
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/PageFlip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ export class PageFlip extends EventObject {
});
}

/**
* Clear pages from HTML (remove to initinalState)
*/
public clear(): void {
this.pages.destroy();
(this.ui as HTMLUI).clear();
}

/**
* Turn to the previous page (without animation)
*/
Expand Down
29 changes: 22 additions & 7 deletions src/UI/HTMLUI.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
import {UI} from "./UI";
import {PageFlip} from "../PageFlip";
import {FlipSetting} from "../Settings";
import { UI } from './UI';
import { PageFlip } from '../PageFlip';
import { FlipSetting } from '../Settings';

/**
* UI for HTML mode
*/
export class HTMLUI extends UI {
constructor(inBlock: HTMLElement, app: PageFlip, setting: FlipSetting, items: NodeListOf<HTMLElement> | HTMLElement[]) {
private items: NodeListOf<HTMLElement> | HTMLElement[];

constructor(
inBlock: HTMLElement,
app: PageFlip,
setting: FlipSetting,
items: NodeListOf<HTMLElement> | HTMLElement[]
) {
super(inBlock, app, setting);

// Second wrapper to HTML page
this.wrapper.insertAdjacentHTML('afterbegin', '<div class="stf__block"></div>');

this.distElement = inBlock.querySelector('.stf__block');

this.items = items;
for (const item of items) {
this.distElement.appendChild(item);
}

this.setHandlers();
}

public clear(): void {
for (const item of this.items) {
this.parentElement.appendChild(item);
}
}

/**
* Update page list from HTMLElements
*
*
* @param {(NodeListOf<HTMLElement>|HTMLElement[])} items - List of pages as HTML Element
*/
public updateItems(items: NodeListOf<HTMLElement> | HTMLElement[]): void {
this.removeHandlers();

this.distElement.innerHTML = "";
this.distElement.innerHTML = '';

for (const item of items) {
this.distElement.appendChild(item);
}
this.items = items;

this.setHandlers();
}

public update(): void {
this.app.getRender().update();
}
}
}
7 changes: 6 additions & 1 deletion src/UI/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type SwipeData = {
* UI Class, represents work with DOM
*/
export abstract class UI {
protected readonly parentElement: HTMLElement;

protected readonly app: PageFlip;
protected readonly wrapper: HTMLElement;
protected distElement: HTMLElement;
Expand All @@ -33,6 +35,8 @@ export abstract class UI {
* @param {FlipSetting} setting - Configuration object
*/
protected constructor(inBlock: HTMLElement, app: PageFlip, setting: FlipSetting) {
this.parentElement = inBlock;

inBlock.classList.add('stf__parent');
// Add first wrapper
inBlock.insertAdjacentHTML('afterbegin', '<div class="stf__wrapper"></div>');
Expand Down Expand Up @@ -134,8 +138,9 @@ export abstract class UI {
}

protected setHandlers(): void {
window.addEventListener('resize', this.onResize, false);
if (!this.app.getSettings().useMouseEvents) return;

this.distElement.addEventListener('mousedown', this.onMouseDown);
this.distElement.addEventListener('touchstart', this.onTouchStart);
window.addEventListener('mousemove', this.onMouseMove);
Expand Down

0 comments on commit c5d3246

Please sign in to comment.