7.8.15 • Published 3 years ago

nehan v7.8.15

Weekly downloads
268
License
MIT
Repository
github
Last release
3 years ago

nehan

Introduction

Dynamic html layout engine for paged-media written by Typescript.

Install

npm install nehan

Demo

See Nehan Reader at Chrome Web Store.

Or nehan-player demo

How to use Nehan Reader

  1. Install Nehan Reader.
  2. Goto any site you want to read as paged-media.
  3. Click the browser action button
  4. Then you'll see paged site.

Example

See example directory.

Usage

simple usage with no styling

import { PagedNehanDocument } from 'nehan';

const $result = document.querySelector("#result");

new PagedNehanDocument("<h1>hello, nehan!</h1>", {}).render({
  onPage: (ctx) => {
    const evaluatedPage = ctx.caller.getPage(ctx.page.index);
    $result.appendChild(evaluatedPage.dom);
    $result.appendChild(document.createElement("hr"));
  },
  onComplete: (ctx) => {
    console.log(`finished! ${ctx.time}msec`);
  }
});

render with style

import { PagedNehanDocument, CssStyleSheet } from 'nehan';

const $result = document.querySelector("#result");

new PagedNehanDocument("<h1>hello, nehan!</h1>", {
  styleSheets:[
    new CssStyleSheet({
      "body":{
        writihgMode:"horizontal-tb", // or "vertical-rl"
        fontSize:"16px",
        padding: "1em",
        measure: "640px", // means 'width' in horizontal-tb, 'height' in vertical-rl.
        extent: "480px"   // means 'height' in horizontal-tb, 'width' in vertical-rl.
      },
      "h1":{
        marginAfter: "1em",   // means 'margin-bottom' in horizontal-tb, 'margin-left' in vertical-rl
        paddingStart: "0.5em" // means 'padding-left' in horizontal-tb, 'padding-top' in vertical-rl
      }
    }
  ]
}).render({
  onPage: (ctx) => {
    const evaluatedPage = ctx.caller.getPage(ctx.page.index);
    $result.appendChild(evaluatedPage.dom);
    $result.appendChild(document.createElement("hr"));
  },
  onComplete: (ctx) => {
    console.log(`finished! ${ctx.time}msec`);
  }
});

About logical size

To handle documents that are independent of the character direction, we use the logical size.

  • measure means the size in the inline direction.
  • extent means the size in the block direction.

logical-size - physical-size table

logical-size\writing-modehorizontal-tbvertical-rlvertical-lr
measurewidthheightheight
extentheightwidthwidth

About logical direction

To handle documents that are independent of the character direction, we use the logical direction.

  • start means inline level heading direction.
  • end means inline level trailing direction.
  • before measn block level heading direction.
  • after means block level trailing direction.

Example1 (logical-margin - physical-margin table)

logical-direction\writing-modehorizontal-tbvertical-rlvertical-lr
margin-startmargin-leftmargin-topmargin-top
margin-endmargin-rightmargin-bottommargin-bottom
margin-beforemargin-topmargin-rightmargin-left
margin-aftermargin-bottommargin-leftmargin-right

The same is true for padding and border(border-xxx-width, border-xxx-color, border-xxx-style).

Example2 (logical-border-radius - physical-border-radius table)

logical-direction\writing-modehorizontal-tbvertical-rlvertical-lr
border-before-start-radiusborder-top-left-radiusborder-top-right-radiusborder-top-left-radius
border-before-end-radiusborder-top-right-radiusborder-bottom-right-radiusborder-bottom-left-radius
border-after-end-radiusborder-bottom-right-radiusborder-bottom-left-radiusborder-bottom-right-radius
border-after-start-radiusborder-bottom-left-radiusborder-top-left-radiusborder-top-right-radius

Shothanded logical property

If you set margin like margin:1em 2em 3em 4em, then it means margin-before:1em, margin-end:2em, margin-after:3em, margin-start:4em.

If you set margin like margin:1em 2em 3em, then it means margin-before:1em, margin-end:2em, margin-after:3em, margin-start:2em.

If you set margin like margin:1em 2em, then it means margin-before:1em, margin-end:2em, margin-after:1em, margin-start:2em.

If you set margin like margin:1em, then it means margin-before:1em, margin-end:1em, margin-after:1em, margin-start:1em.

The same is true for padding and border(border-xxx-width, border-xxx-color, border-xxx-style).

Dynamic Style

You can define functional value for each selector(css property name is !xxx).

In following example, all elements that matches .require-xx will cause page-break if rest block size(restExtent) is smaller than xxpx.

import { CssStyleSheet, DynamicStyleContext, CssDeclarationBlock } from 'nehan';

function requiredExtent(requiredSize: number) {
  return (ctx: DynamicStyleContext): CssDeclarationBlock | undefined => {
    if (!ctx.parentContext) {
      return undefined;
    }
    const restExtent = ctx.parentContext.restExtent;
    if (restExtent >= requiredSize) {
      return undefined; // enough block size is left!
    }
    // restExtent < requiredSize(not enough block size left)
    return { "page-break-before": "always" };
  };
}

const myStyleSheet = new CssStyleSheet({
  ".require-60": {
    "!dynamic": requiredExtent(60)
  }
});

DOM generation hook

You can define dom generation hook for each selector element like this(css property name is @xxx).

import { CssStyleSheet, DomCallbackContext } from 'nehan';

const myStyleSheet = new CssStyleSheet({
  ".foo": {
    "@create": (ctx: DomCallbackContext) => {
      ctx.dom.onclick = (e) => {
        alert(`${ctx.selector} is clicked!`); // .foo is clicked!
      };
    }
  }
});

Plugins

Development

npm run build and library is generated in dist directory.

License

MIT

7.8.15

3 years ago

7.8.14

3 years ago

7.8.13

3 years ago

7.8.12

3 years ago

7.8.11

3 years ago

7.8.10

3 years ago

7.8.8

3 years ago

7.8.7

3 years ago

7.8.9

3 years ago

7.8.6

3 years ago

7.8.5

3 years ago

7.8.4

3 years ago

7.8.3

3 years ago

7.8.2

3 years ago

7.8.1

3 years ago

7.8.0

3 years ago

7.7.4

3 years ago

7.7.3

3 years ago

7.7.2

3 years ago

7.7.1

3 years ago

7.7.0

3 years ago

7.6.5

3 years ago

7.6.4

3 years ago

7.6.2

3 years ago

7.6.3

3 years ago

7.6.1

3 years ago

7.6.0

3 years ago

7.5.1

3 years ago

7.5.0

3 years ago

7.4.0

3 years ago

7.3.7

4 years ago

7.3.5

4 years ago

7.3.6

4 years ago

7.3.4

4 years ago

7.3.3

4 years ago

7.3.2

4 years ago

7.3.1

4 years ago

7.3.0

4 years ago

7.2.9

4 years ago

7.2.8

4 years ago

7.2.7

4 years ago

7.2.6

4 years ago

7.2.5

4 years ago

7.2.4

4 years ago

7.2.3

4 years ago

7.2.2

4 years ago

7.2.1

4 years ago

7.2.0

4 years ago

7.1.2

4 years ago

7.1.1

4 years ago

7.1.0

4 years ago

7.0.12

4 years ago

7.0.13

4 years ago

7.0.11

4 years ago

7.0.10

4 years ago

7.0.9

4 years ago

7.0.8

4 years ago

7.0.7

4 years ago

7.0.6

4 years ago

7.0.5

4 years ago

7.0.4

4 years ago

7.0.3

4 years ago

7.0.2

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

6.0.40

4 years ago

6.0.38

5 years ago

6.0.37

5 years ago

6.0.36

5 years ago

6.0.35

5 years ago

6.0.34

5 years ago

6.0.33

5 years ago

6.0.32

5 years ago

6.0.31

5 years ago

6.0.30

5 years ago

6.0.29

5 years ago

6.0.28

5 years ago

6.0.27

5 years ago

6.0.26

5 years ago

6.0.25

5 years ago

6.0.24

5 years ago

6.0.23

5 years ago

6.0.22

5 years ago

6.0.21

5 years ago

6.0.20

5 years ago

6.0.19

6 years ago

6.0.18

6 years ago

6.0.17

6 years ago

6.0.16

6 years ago

6.0.15

6 years ago

6.0.14

6 years ago

6.0.13

6 years ago

6.0.12

6 years ago

6.0.11

6 years ago

6.0.10

6 years ago

6.0.9

6 years ago

6.0.8

6 years ago

6.0.7

6 years ago

6.0.6

6 years ago

6.0.5

6 years ago

6.0.4

6 years ago

6.0.3

6 years ago

6.0.2

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago