@netless/forge-imagery-doc 0.1.2 → 0.1.3-appha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import EventEmitter from "eventemitter3";
1
+ import EventEmitter from 'eventemitter3';
2
2
 
3
3
  interface InfinityScrollEvents {
4
4
  scale: (value: number) => void;
@@ -7,49 +7,50 @@ interface InfinityScrollEvents {
7
7
 
8
8
  export class InfinityScroll extends EventEmitter<InfinityScrollEvents> {
9
9
 
10
- public readonly view: HTMLDivElement;
11
- private lastDelta: {x: number, y: number} = { x: 0, y: 0 };
12
- private lastTriggerTime: number = 0;
10
+ public readonly view: HTMLDivElement;
11
+ private lastDelta: {x: number, y: number} = { x: 0, y: 0 };
12
+ private lastTriggerTime: number = 0;
13
13
 
14
- public constructor(view: HTMLDivElement) {
15
- super();
16
- this.view = view;
17
- this.view.addEventListener("wheel", this.handleWheel, { passive: false, capture: true });
18
- }
19
-
20
- private handleWheel = (evt: WheelEvent) => {
21
- evt.preventDefault();
22
- evt.stopImmediatePropagation();
23
- evt.stopPropagation();
14
+ public constructor(view: HTMLDivElement) {
15
+ super();
16
+ this.view = view;
17
+ this.view.addEventListener('wheel', this.handleWheel, { passive: false, capture: true });
18
+ }
24
19
 
25
- if (Date.now() - this.lastTriggerTime < 32) {
26
- this.lastDelta.y += evt.deltaY;
27
- this.lastDelta.x += evt.deltaX;
28
- return;
29
- }
30
- if (evt.ctrlKey) { // 双指捏合
31
- // @ts-ignore
32
- const wheelDelta = Math.sign((evt.wheelDelta || 100)) * 120;
33
- const deltaY = evt.deltaY + this.lastDelta.y;
34
- let scale: number;
35
- if (deltaY > 0) {
36
- scale = Math.max(1 - Math.abs(deltaY / wheelDelta), 0.00000001)
37
- } else { // 放大
38
- scale = 1 + Math.abs(deltaY / wheelDelta)
39
- }
40
- scale = Math.max(0.9, scale);
41
- scale = Math.min(1.1, scale);
42
- this.emit("scale", scale);
43
- } else { // 拖动
44
- const deltaX = this.lastDelta.x + evt.deltaX;
45
- const deltaY = this.lastDelta.y + evt.deltaY;
46
- this.emit("translate", deltaX, deltaY);
47
- }
48
- this.lastTriggerTime = Date.now();
49
- this.lastDelta = { x: 0, y: 0 };
50
- };
20
+ private handleWheel = (evt: WheelEvent) => {
21
+ evt.preventDefault();
22
+ evt.stopImmediatePropagation();
23
+ evt.stopPropagation();
51
24
 
52
- public dispose() {
53
- this.view.removeEventListener("wheel", this.handleWheel);
25
+ if (Date.now() - this.lastTriggerTime < 32) {
26
+ this.lastDelta.y += evt.deltaY;
27
+ this.lastDelta.x += evt.deltaX;
28
+ return;
54
29
  }
30
+ if (evt.ctrlKey) { // 双指捏合
31
+ // @ts-ignore
32
+ const wheelDelta = Math.sign((evt.wheelDelta || 100)) * 120;
33
+ const deltaY = evt.deltaY + this.lastDelta.y;
34
+ let scale: number;
35
+ if (deltaY > 0) {
36
+ scale = Math.max(1 - Math.abs(deltaY / wheelDelta), 0.00000001);
37
+ } else { // 放大
38
+ scale = 1 + Math.abs(deltaY / wheelDelta);
39
+ }
40
+ scale = Math.max(0.9, scale);
41
+ scale = Math.min(1.1, scale);
42
+ this.emit('scale', scale);
43
+ } else { // 拖动
44
+ const deltaX = this.lastDelta.x + evt.deltaX;
45
+ const deltaY = this.lastDelta.y + evt.deltaY;
46
+ this.emit('translate', deltaX, deltaY);
47
+ }
48
+ this.lastTriggerTime = Date.now();
49
+ this.lastDelta = { x: 0, y: 0 };
50
+ };
51
+
52
+ public dispose() {
53
+ this.view.removeEventListener('wheel', this.handleWheel);
54
+ this.removeAllListeners();
55
+ }
55
56
  }
package/src/LazyImage.ts CHANGED
@@ -1,66 +1,66 @@
1
- import { kvStore } from "@netless/forge-room";
1
+ import { kvStore } from '@netless/forge-room';
2
2
 
3
3
  export class LazyImage {
4
4
 
5
- public readonly view: HTMLDivElement;
6
- public readonly width: number;
7
- public readonly height: number;
8
- private src: string;
5
+ public readonly view: HTMLDivElement;
6
+ public readonly width: number;
7
+ public readonly height: number;
8
+ private src: string;
9
9
 
10
- public constructor(src: string, width: number, height: number) {
11
- this.src = src;
12
- this.width = width;
13
- this.height = height;
14
- this.view = document.createElement("div");
15
- this.view.setAttribute("data-forge-src", src);
16
- this.view.style.width = `${width}px`;
17
- this.view.style.height = `${height}px`;
18
- this.view.style.transformOrigin = "0 0";
19
- }
10
+ public constructor(src: string, width: number, height: number) {
11
+ this.src = src;
12
+ this.width = width;
13
+ this.height = height;
14
+ this.view = document.createElement('div');
15
+ this.view.setAttribute('data-forge-src', src);
16
+ this.view.style.width = `${width}px`;
17
+ this.view.style.height = `${height}px`;
18
+ this.view.style.transformOrigin = '0 0';
19
+ }
20
+
21
+ public scale(scale: number) {
22
+ // this.localScale = scale;
23
+ this.view.style.width = `${this.width * scale}px`;
24
+ this.view.style.height = `${this.height * scale}px`;
25
+ // this.img.style.width = "100%";
26
+ }
20
27
 
21
- public scale(scale: number) {
22
- // this.localScale = scale;
23
- this.view.style.width = `${this.width * scale}px`;
24
- this.view.style.height = `${this.height * scale}px`;
25
- // this.img.style.width = "100%";
28
+ public async getImgContent(): Promise<string> {
29
+ let base64Data: string | null = null;
30
+ try {
31
+ base64Data = await kvStore.getItem(this.src);
32
+ } catch {
33
+ // ignore
34
+ }
35
+ if (base64Data) {
36
+ return base64Data;
26
37
  }
27
38
 
28
- public async getImgContent(): Promise<string> {
29
- let base64Data: string | null = null;
30
- try {
31
- base64Data = await kvStore.getItem(this.src);
32
- } catch {
33
- // ignore
34
- }
35
- if (base64Data) {
36
- return base64Data;
37
- }
39
+ const response = await fetch(this.src);
40
+ const blob = await response.blob();
41
+ return new Promise<string>((resolve, reject) => {
42
+ const reader = new FileReader();
43
+ reader.onload = (_e) => {
44
+ kvStore.setItem(this.src, reader.result as string);
45
+ resolve(reader.result as string);
46
+ };
47
+ reader.onerror = () => {
48
+ reject(reader.error);
49
+ };
50
+ reader.readAsDataURL(blob);
51
+ });
52
+ }
38
53
 
39
- const response = await fetch(this.src);
40
- const blob = await response.blob();
41
- return new Promise<string>((resolve, reject) => {
42
- const reader = new FileReader();
43
- reader.onload = (e) => {
44
- kvStore.setItem(this.src, reader.result as string);
45
- resolve(reader.result as string);
46
- };
47
- reader.onerror = () => {
48
- reject(reader.error);
49
- };
50
- reader.readAsDataURL(blob);
51
- });
54
+ public async prepare(): Promise<void> {
55
+ if (this.view.children.length > 0) {
56
+ return;
52
57
  }
53
-
54
- public async prepare(): Promise<void> {
55
- if (this.view.children.length > 0) {
56
- return;
57
- }
58
- const base64Data: string = await this.getImgContent();
59
- const img = document.createElement("img");
60
- img.src = base64Data;
61
- img.style.width = "100%";
62
- if (this.view.children.length === 0) {
63
- this.view.appendChild(img);
64
- }
58
+ const base64Data: string = await this.getImgContent();
59
+ const img = document.createElement('img');
60
+ img.src = base64Data;
61
+ img.style.width = '100%';
62
+ if (this.view.children.length === 0) {
63
+ this.view.appendChild(img);
65
64
  }
65
+ }
66
66
  }