@netless/forge-imagery-doc 0.1.3 → 1.0.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.
@@ -1,6 +1,4 @@
1
- import { type RoomUser, type UserManager } from '@netless/forge-room';
2
- import EventEmitter from 'eventemitter3';
3
- import * as Y from 'yjs';
1
+ import { AbstractApplicationPermissions, ApplicationPermissionEvents } from '@netless/forge-room';
4
2
 
5
3
  export enum ImageryDocPermissionFlag {
6
4
  /**
@@ -27,7 +25,7 @@ export enum ImageryDocPermissionFlag {
27
25
  ImageryDocPermissionFlag.sideBar
28
26
  }
29
27
 
30
- export interface ImageryDocPermissionEvents {
28
+ export interface ImageryDocPermissionEvents extends ApplicationPermissionEvents {
31
29
  /**
32
30
  * 当用户权限变更时触发
33
31
  * @param { string } userId 对应 userId
@@ -37,64 +35,7 @@ export interface ImageryDocPermissionEvents {
37
35
  change: (userId: string, flags: ImageryDocPermissionFlag[], value: number) => void;
38
36
  }
39
37
 
40
- export class ImageryDocPermissions extends EventEmitter<ImageryDocPermissionEvents> {
41
-
42
- private requestUserMap: (userId: string) => Y.Map<any>;
43
- private userManager: UserManager;
44
- private observers: Map<string, any> = new Map();
45
-
46
- public constructor(
47
- userManager: UserManager,
48
- requestUserMap: (userId: string) => Y.Map<any>,
49
- ) {
50
- super();
51
- this.userManager = userManager;
52
- this.requestUserMap = requestUserMap;
53
- this.createModel(this.userManager.selfId);
54
- this.userManager.userIdList().forEach((userId) => {
55
- this.addObserve(userId);
56
- });
57
-
58
- this.userManager.on('join', this.handleUserJoin);
59
- this.userManager.on('leave', this.handleUserLeave);
60
- }
61
-
62
- private handleUserLeave = (user: RoomUser) => {
63
- const cb = this.observers.get(user.id);
64
- if (cb) {
65
- this.requestUserMap(user.id).unobserve(cb);
66
- }
67
- };
68
-
69
- private handleUserJoin = (user: RoomUser) => {
70
- this.addObserve(user.id);
71
- };
72
-
73
- private addObserve(userId: string) {
74
- const observer = (evt: Y.YMapEvent<any>) => {
75
- this.handleUserPermissionChange(userId, evt);
76
- };
77
- this.observers.set(userId, observer);
78
- this.requestUserMap(userId).observe(observer);
79
- }
80
-
81
- private createModel(userId: string) {
82
- const userMap = this.requestUserMap(userId);
83
- if (!userMap.has('permission')) {
84
- userMap.set('permission', 0);
85
- }
86
- }
87
-
88
- private handleUserPermissionChange(userId: string, evt: Y.YMapEvent<any>) {
89
- for (const [key, value] of evt.changes.keys.entries()) {
90
- if (key === 'permission') {
91
- if (value.action === 'add' || value.action === 'update') {
92
- const newValue = this.requestUserMap(userId).get('permission');
93
- this.emit('change', userId, this.resolveFlags(newValue), newValue);
94
- }
95
- }
96
- }
97
- }
38
+ export class ImageryDocPermissions extends AbstractApplicationPermissions<ImageryDocPermissionFlag, ImageryDocPermissionEvents> {
98
39
 
99
40
  /**
100
41
  * 解析权限列表组合
@@ -108,62 +49,5 @@ export class ImageryDocPermissions extends EventEmitter<ImageryDocPermissionEven
108
49
  ImageryDocPermissionFlag.camera,
109
50
  ].filter(v => (v & value) !== 0);
110
51
  }
111
-
112
- /**
113
- * 获取权限列表组合对应的数值
114
- * @param { string } userId 不传表示获取自己
115
- */
116
- public getPermissionValue(userId?: string): number {
117
- return this.requestUserMap(userId ?? this.userManager.selfId).get('permission') ?? 0;
118
- }
119
-
120
- /**
121
- * 获取权限列表
122
- * @param {string=} userId 可选, 不传表示获取自己
123
- */
124
- public getPermissionFlags(userId?: string): ImageryDocPermissionFlag[] {
125
- const value = this.requestUserMap(userId ?? this.userManager.selfId).get('permission') ?? 0;
126
- return this.resolveFlags(value);
127
- }
128
-
129
- /**
130
- * 返回对应 userId 是否有相应权限
131
- * @param {string=} userId 可选, 不传表示返回自己是否有相应权限
132
- * @param {WhiteboardPermissionFlag} flag
133
- */
134
- public hasPermission(flag: ImageryDocPermissionFlag, userId?: string): boolean {
135
- return ((this.requestUserMap(userId ?? this.userManager.selfId).get('permission') ?? 0) & flag) !== 0;
136
- }
137
-
138
- /**
139
- * 添加权限
140
- * @param {WhiteboardPermissionFlag} flag 权限标记
141
- * @param {string=} userId 可选, 为 userId 添加权限, 不传表示为自己添加权限
142
- */
143
- public addPermission(flag: ImageryDocPermissionFlag, userId?: string) {
144
- const userMap = this.requestUserMap(userId ?? this.userManager.selfId);
145
- const oldValue = userMap.get('permission') ?? 0;
146
- this.requestUserMap(userId ?? this.userManager.selfId).set('permission', oldValue | flag);
147
- }
148
-
149
- /**
150
- * 移除权限
151
- * @param {WhiteboardPermissionFlag} flag 权限标记
152
- * @param {string=} userId 可选, 为 userId 移除权限, 不传表示为自己移除权限
153
- */
154
- public removePermission(flag: ImageryDocPermissionFlag, userId?: string) {
155
- const userMap = this.requestUserMap(userId ?? this.userManager.selfId);
156
- const oldValue = userMap.get('permission') ?? 0;
157
- this.requestUserMap(userId ?? this.userManager.selfId).set('permission', oldValue & (~flag));
158
- }
159
-
160
- public dispose() {
161
- this.userManager.off('join', this.handleUserJoin);
162
- this.userManager.off('leave', this.handleUserLeave);
163
- for (const [userId, observer] of this.observers) {
164
- this.requestUserMap(userId).unobserve(observer);
165
- }
166
- this.observers.clear();
167
- this.removeAllListeners();
168
- }
52
+
169
53
  }
@@ -4,6 +4,7 @@ import * as Y from 'yjs';
4
4
  import { WhiteboardApplication } from '@netless/forge-whiteboard';
5
5
  import type { ImageryDoc } from './ImageryDoc';
6
6
  import { Container, ContainerKeys } from './Container';
7
+ import { removeObserver } from '@netless/forge-room';
7
8
 
8
9
  export const delay = (value: number) => new Promise(resolve => setTimeout(resolve, value));
9
10
 
@@ -95,14 +96,14 @@ export class SingleContainer implements Container {
95
96
  this.scale = nextScale;
96
97
  this.handleTranslate(0, 0);
97
98
  });
98
- //@ts-ignore
99
- window.__doc = this;
100
99
  }
101
100
 
102
101
  public init() {
103
102
  this.handleGoto(this.pageIndex)
104
103
  .catch(_error => {
105
- // todo error
104
+ console.error(_error);
105
+ }).finally(() => {
106
+ this.handleResize();
106
107
  });
107
108
  this.updateSyncedTransform();
108
109
  }
@@ -251,7 +252,7 @@ export class SingleContainer implements Container {
251
252
  };
252
253
 
253
254
  public dispose() {
254
- this.map.unobserve(this.handleMapChange);
255
+ removeObserver(this.map, this.handleMapChange);
255
256
  this.resizeObserver.disconnect();
256
257
  this.scroll.dispose();
257
258
  }