@netless/app-presentation 0.1.8 → 0.1.9-beta.0

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.
@@ -0,0 +1,205 @@
1
+ import './style.scss?inline';
2
+ import type { View, AnimationMode } from "@netless/window-manager";
3
+ import { makeDraggable } from "./Draggable";
4
+
5
+ export interface ScrollbarOption {
6
+ getPageSize: () => {
7
+ width: number;
8
+ height: number;
9
+ };
10
+ getWritable: () => boolean;
11
+ syncView: () => void;
12
+ }
13
+
14
+ export class Scrollbar {
15
+ readonly namespace = "netless-app-presentation"
16
+ private container: HTMLElement;
17
+ private scrollContainer: HTMLDivElement = document.createElement('div');
18
+ private option: ScrollbarOption = {
19
+ getPageSize: () => {
20
+ return {
21
+ width: 0,
22
+ height: 0
23
+ }
24
+ },
25
+ getWritable: () => false,
26
+ syncView: () => void 0
27
+ };
28
+ private view: View;
29
+ private scrollbarContainerX?: HTMLDivElement;
30
+ private scrollbarContainerY?: HTMLDivElement;
31
+ private scrollbarX?: HTMLDivElement;
32
+ private scrollbarY?: HTMLDivElement;
33
+ private draggableX?: { destroy: () => void };
34
+ private draggableY?: { destroy: () => void };
35
+ private cameraCache?: {
36
+ centerX: number;
37
+ centerY: number;
38
+ scale: number;
39
+ };
40
+ constructor(container: HTMLElement, option: ScrollbarOption, view: View) {
41
+ this.scrollContainer.className = this.c('scrollbar-container');
42
+ this.container = container;
43
+ this.option = option;
44
+ this.view = view;
45
+ this.init();
46
+ this.view.callbacks.on('onCameraUpdated', this.onCameraUpdated);
47
+ }
48
+ private c(className: string): string {
49
+ return `${this.namespace}-${className}`
50
+ }
51
+
52
+ private getOriginScale() {
53
+ const { size } = this.view;
54
+ const { width, height } = this.option.getPageSize();
55
+ return Math.min(size.height / height, size.width / width);
56
+ }
57
+
58
+ onCameraUpdated = () => {
59
+ const { scale, centerX, centerY } = this.view.camera;
60
+ const originScale = this.getOriginScale();
61
+ const ratio = Math.round(originScale / scale * 1000) / 1000;
62
+ const scrollX = Math.round(centerX * originScale);
63
+ const scrollY = Math.round(centerY * originScale);
64
+ if (this.scrollbarX) {
65
+ this.scrollbarX.style.width = `${ratio * 100}%`;
66
+ this.scrollbarX.style.display = ratio === 1 ? 'none' : 'block';
67
+ this.scrollbarX.style.transform = `translateX(${scrollX}px)`;
68
+ }
69
+ if (this.scrollbarY) {
70
+ this.scrollbarY.style.height = `${ratio * 100}%`;
71
+ this.scrollbarY.style.display = ratio === 1 ? 'none' : 'block';
72
+ this.scrollbarY.style.transform = `translateY(${scrollY}px)`;
73
+ }
74
+ }
75
+
76
+
77
+ onDragStart = () => {
78
+ const camera = this.view.camera;
79
+ this.cameraCache = {
80
+ centerX: camera.centerX,
81
+ centerY: camera.centerY,
82
+ scale: camera.scale,
83
+ }
84
+ }
85
+
86
+ getScrollXRange(camera: {scale: number}) {
87
+ const { width } = this.option.getPageSize();
88
+ const originScale = this.getOriginScale();
89
+ const scale = camera.scale;
90
+ const ratio = Math.round(originScale / scale * 1000) / 1000;
91
+ const scrollWidth = width * (1-ratio);
92
+ const minX = -scrollWidth / 2;
93
+ const maxX = scrollWidth / 2;
94
+ return { minX, maxX };
95
+ }
96
+
97
+ getScrollYRange(camera: {scale: number}) {
98
+ const { height } = this.option.getPageSize();
99
+ const originScale = this.getOriginScale();
100
+ const scale = camera.scale;
101
+ const ratio = Math.round(originScale / scale * 1000) / 1000;
102
+ const scrollHeight = height * (1-ratio);
103
+ const minY = -scrollHeight / 2;
104
+ const maxY = scrollHeight / 2;
105
+ return { minY, maxY };
106
+ }
107
+
108
+ onDragX = (transformedDrag: {x: number, y: number}) => {
109
+ if (this.cameraCache) {
110
+ const {x} = transformedDrag;
111
+ const originScale = this.getOriginScale();
112
+ const { minX, maxX } = this.getScrollXRange(this.cameraCache);
113
+ const scrollX = Math.round(x / originScale);
114
+ let centerX = scrollX + this.cameraCache.centerX;
115
+ if (centerX < minX) {
116
+ centerX = minX;
117
+ } else if (centerX > maxX) {
118
+ centerX = maxX;
119
+ }
120
+ this.view.moveCamera({
121
+ centerX,
122
+ animationMode: 'immediately' as AnimationMode
123
+ })
124
+ }
125
+ }
126
+
127
+ onDragY = (transformedDrag: {x: number, y: number}) => {
128
+ if(this.cameraCache){
129
+ const {y} = transformedDrag;
130
+ const originScale = this.getOriginScale();
131
+ const { minY, maxY } = this.getScrollYRange(this.cameraCache);
132
+ const scrollY = Math.round(y / originScale);
133
+ let centerY = scrollY + this.cameraCache.centerY;
134
+ if (centerY < minY) {
135
+ centerY = minY;
136
+ } else if (centerY > maxY) {
137
+ centerY = maxY;
138
+ }
139
+ this.view.moveCamera({
140
+ centerY,
141
+ animationMode: 'immediately' as AnimationMode
142
+ })
143
+ }
144
+ }
145
+
146
+ onDragEnd = () => {
147
+ this.cameraCache = undefined
148
+ this.option.syncView();
149
+ }
150
+
151
+ private init() {
152
+ this.scrollbarX = document.createElement('div');
153
+ this.scrollbarX.classList.add(this.c('scrollbar-x'));
154
+ if (this.option.getWritable()) {
155
+ this.draggableX = makeDraggable(this.scrollbarX, {
156
+ direction: 'x',
157
+ onDrag: this.onDragX,
158
+ onDragEnd: this.onDragEnd,
159
+ onDragStart: this.onDragStart,
160
+ })
161
+ }
162
+ Object.assign(this.scrollbarX.style, {
163
+ width: '100%',
164
+ display: 'none'
165
+ });
166
+ this.scrollbarContainerX = document.createElement('div');
167
+ this.scrollbarContainerX.classList.add(this.c('scrollbar-container-x'));
168
+ this.scrollbarContainerX.appendChild(this.scrollbarX);
169
+
170
+ this.scrollbarY = document.createElement('div');
171
+ this.scrollbarY.classList.add(this.c('scrollbar-y'));
172
+ if (this.option.getWritable()) {
173
+ this.draggableY = makeDraggable(this.scrollbarY, {
174
+ direction: 'y',
175
+ onDrag: this.onDragY,
176
+ onDragEnd: this.onDragEnd,
177
+ onDragStart: this.onDragStart,
178
+ })
179
+ }
180
+ Object.assign(this.scrollbarY.style, {
181
+ height: '100%',
182
+ display: 'none'
183
+ });
184
+ this.scrollbarContainerY = document.createElement('div');
185
+ this.scrollbarContainerY.classList.add(this.c('scrollbar-container-y'));
186
+ this.scrollbarContainerY.appendChild(this.scrollbarY);
187
+
188
+ this.scrollContainer.append(this.scrollbarContainerX, this.scrollbarContainerY);
189
+ this.container.appendChild(this.scrollContainer);
190
+ }
191
+
192
+ destroy() {
193
+ this.view.callbacks.off('onCameraUpdated', this.onCameraUpdated);
194
+ this.scrollbarX?.remove();
195
+ this.scrollbarY?.remove();
196
+ this.scrollbarX = undefined;
197
+ this.scrollbarY = undefined;
198
+
199
+ this.draggableX?.destroy();
200
+ this.draggableY?.destroy();
201
+ this.draggableX = undefined;
202
+ this.draggableY = undefined;
203
+ }
204
+
205
+ }
@@ -0,0 +1,53 @@
1
+ $namespace: 'netless-app-presentation';
2
+
3
+ .#{$namespace}-scrollbar-container{
4
+ position: absolute;
5
+ left: 0;
6
+ top: 0;
7
+ width: 100%;
8
+ height: 100%;
9
+ pointer-events: none;
10
+ z-index: 101;
11
+ }
12
+ .#{$namespace}-scrollbar-container-x,
13
+ .#{$namespace}-scrollbar-container-y {
14
+ position: absolute;
15
+ pointer-events: none;
16
+ display: flex;
17
+ justify-content: center;
18
+ box-sizing: border-box;
19
+ padding: 0;
20
+ }
21
+
22
+ .#{$namespace}-scrollbar-container-x {
23
+ bottom: 0;
24
+ left: 0;
25
+ height: 6px;
26
+ width: 100%;
27
+ padding-right: 6px;
28
+ }
29
+
30
+ .#{$namespace}-scrollbar-container-y {
31
+ top: 0;
32
+ right: 0;
33
+ width: 6px;
34
+ height: 100%;
35
+ flex-direction: column;
36
+ padding-bottom: 6px;
37
+ }
38
+
39
+ .#{$namespace}-scrollbar-x,
40
+ .#{$namespace}-scrollbar-y {
41
+ border-radius: 2px;
42
+ background-color: rgba(27, 31, 77, .3);
43
+ pointer-events: auto;
44
+ cursor: default;
45
+ }
46
+
47
+ .#{$namespace}-scrollbar-x {
48
+ height: 6px;
49
+ }
50
+
51
+ .#{$namespace}-scrollbar-y {
52
+ width: 6px;
53
+ }
package/src/style.scss CHANGED
@@ -257,3 +257,54 @@ $namespace: 'netless-app-presentation';
257
257
  background: rgba(50, 50, 50, 0.9);
258
258
  }
259
259
  }
260
+ .#{$namespace}-scrollbar-container{
261
+ position: absolute;
262
+ left: 0;
263
+ top: 0;
264
+ width: 100%;
265
+ height: 100%;
266
+ pointer-events: none;
267
+ z-index: 101;
268
+ }
269
+ .#{$namespace}-scrollbar-container-x,
270
+ .#{$namespace}-scrollbar-container-y {
271
+ position: absolute;
272
+ pointer-events: none;
273
+ display: flex;
274
+ justify-content: center;
275
+ box-sizing: border-box;
276
+ padding: 0;
277
+ }
278
+
279
+ .#{$namespace}-scrollbar-container-x {
280
+ bottom: 0;
281
+ left: 0;
282
+ height: 6px;
283
+ width: 100%;
284
+ padding-right: 6px;
285
+ }
286
+
287
+ .#{$namespace}-scrollbar-container-y {
288
+ top: 0;
289
+ right: 0;
290
+ width: 6px;
291
+ height: 100%;
292
+ flex-direction: column;
293
+ padding-bottom: 6px;
294
+ }
295
+
296
+ .#{$namespace}-scrollbar-x,
297
+ .#{$namespace}-scrollbar-y {
298
+ border-radius: 2px;
299
+ background-color: rgba(27, 31, 77, .3);
300
+ pointer-events: auto;
301
+ cursor: default;
302
+ }
303
+
304
+ .#{$namespace}-scrollbar-x {
305
+ height: 6px;
306
+ }
307
+
308
+ .#{$namespace}-scrollbar-y {
309
+ width: 6px;
310
+ }