Compare commits

...

4 Commits
v1.0.0 ... main

Author SHA1 Message Date
fd4dca22e3
1.0.2 2021-11-20 14:43:08 +03:00
113dd67d07
made private fields, useless outside the class 2021-11-20 14:38:31 +03:00
d9079f52e1
1.0.1 2021-10-05 10:06:28 +03:00
10793fb770
Fixed publishing not full lib folder 2021-10-05 10:05:08 +03:00
3 changed files with 20 additions and 19 deletions

1
.npmignore Normal file
View File

@ -0,0 +1 @@
*.html

View File

@ -1,11 +1,11 @@
{ {
"name": "html-pagination", "name": "html-pagination",
"version": "1.0.0", "version": "1.0.2",
"description": "Package for html document pagination to fit container with fixed width", "description": "Package for html document pagination to fit container with fixed width",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",
"files": [ "files": [
"./lib/**/*" "lib"
], ],
"scripts": { "scripts": {
"test:browser": "npm run build && cp ./src/__tests__/index.html ./lib/ && serve -s lib", "test:browser": "npm run build && cp ./src/__tests__/index.html ./lib/ && serve -s lib",

View File

@ -3,17 +3,16 @@ import { isElementNode, isTextNode } from './types';
import { binSearch } from './utils'; import { binSearch } from './utils';
class HTMLPagination { class HTMLPagination {
content: HTMLElement; private content: HTMLElement;
container: HTMLElement; private container: HTMLElement;
cache: CacheInterface | undefined; private cache: CacheInterface | undefined;
initialJump: number; private initialJump: number;
elementPositions: [number, Node][]; private elementPositions: [number, Node][];
idPositions: Map<string, number>; private idPositions: Map<string, number>;
pages: number[]; private pages: number[];
/** /**
* /**
* @param content HTML element with html content to display paginationly * @param content HTML element with html content to display paginationly
* @param container HTML element which will store content to display * @param container HTML element which will store content to display
* @param cache Class implementing `g` and `s` methods for getting and * @param cache Class implementing `g` and `s` methods for getting and
@ -43,6 +42,7 @@ class HTMLPagination {
/** /**
* Method computes book's pages till specified number, * Method computes book's pages till specified number,
* sets container's content to `n`th page and returns it as string * sets container's content to `n`th page and returns it as string
*
* `n` starts from 1 * `n` starts from 1
*/ */
getPage(n: number): string { getPage(n: number): string {
@ -78,7 +78,7 @@ class HTMLPagination {
* Computes html elements and text nodes positions. * Computes html elements and text nodes positions.
* Must be run only on first setup * Must be run only on first setup
*/ */
computeElementsPositions(): void { private computeElementsPositions(): void {
const recursive = (currentPosition: number, root: Node): number => { const recursive = (currentPosition: number, root: Node): number => {
if (isTextNode(root)) { if (isTextNode(root)) {
this.elementPositions.push([currentPosition, root]); this.elementPositions.push([currentPosition, root]);
@ -99,7 +99,7 @@ class HTMLPagination {
/** /**
* Finds position for next page break * Finds position for next page break
*/ */
getPageBreak(start: number): number { private getPageBreak(start: number): number {
let previousEnd = this.getMaxPosition; let previousEnd = this.getMaxPosition;
let end = this.getNextSpaceForPosition( let end = this.getNextSpaceForPosition(
Math.min(start + this.initialJump, this.getMaxPosition) Math.min(start + this.initialJump, this.getMaxPosition)
@ -137,7 +137,7 @@ class HTMLPagination {
/** /**
* Gets next space or gap between elements for position * Gets next space or gap between elements for position
*/ */
getNextSpaceForPosition(startPos: number): number { private getNextSpaceForPosition(startPos: number): number {
const nodeIndex = this.getElementIndexForPosition(startPos); const nodeIndex = this.getElementIndexForPosition(startPos);
const [nodePosition, node] = this.elementPositions[nodeIndex]; const [nodePosition, node] = this.elementPositions[nodeIndex];
@ -158,7 +158,7 @@ class HTMLPagination {
/** /**
* Gets previous space or gap between elements for position * Gets previous space or gap between elements for position
*/ */
getPreviousSpaceForPosition(startPos: number): number { private getPreviousSpaceForPosition(startPos: number): number {
const nodeIndex = this.getElementIndexForPosition(startPos); const nodeIndex = this.getElementIndexForPosition(startPos);
const [nodePosition, node] = this.elementPositions[nodeIndex]; const [nodePosition, node] = this.elementPositions[nodeIndex];
@ -172,14 +172,14 @@ class HTMLPagination {
/** /**
* Checks if container is overflowing with content * Checks if container is overflowing with content
*/ */
get scrollNecessary(): boolean { private get scrollNecessary(): boolean {
return this.container.clientHeight < this.container.scrollHeight; return this.container.clientHeight < this.container.scrollHeight;
} }
/** /**
* @returns end position of content * @returns end position of content
*/ */
get getMaxPosition(): number { private get getMaxPosition(): number {
const [offset, element] = const [offset, element] =
this.elementPositions[this.elementPositions.length - 1]; this.elementPositions[this.elementPositions.length - 1];
return offset + (element.nodeValue?.length || 0); return offset + (element.nodeValue?.length || 0);
@ -195,7 +195,7 @@ class HTMLPagination {
/** /**
* Wrapper for `binSearch` util to find index of element for position * Wrapper for `binSearch` util to find index of element for position
*/ */
getElementIndexForPosition(pos: number): number { private getElementIndexForPosition(pos: number): number {
return binSearch( return binSearch(
this.elementPositions, this.elementPositions,
pos, pos,
@ -207,7 +207,7 @@ class HTMLPagination {
* Finds node inside which `pos` is located. * Finds node inside which `pos` is located.
* @returns node positions and itself * @returns node positions and itself
*/ */
getElementForPosition(pos: number): [number, Node] { private getElementForPosition(pos: number): [number, Node] {
const elementIndex = this.getElementIndexForPosition(pos); const elementIndex = this.getElementIndexForPosition(pos);
return this.elementPositions[elementIndex]; return this.elementPositions[elementIndex];
@ -217,7 +217,7 @@ class HTMLPagination {
* Sets `container` element content and * Sets `container` element content and
* return as string html content between `from` and `to` * return as string html content between `from` and `to`
*/ */
getContentFromRange(from: number, to: number): string { private getContentFromRange(from: number, to: number): string {
this.container.innerHTML = ''; this.container.innerHTML = '';
const range = new Range(); const range = new Range();