@netless/fastboard 0.0.8 → 0.0.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/fastboard",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "An open sourced sdk based on white-web-sdk.",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.es.js",
@@ -22,6 +22,14 @@
22
22
  "default": "./dist/vue.es.js",
23
23
  "types": "./src/vue.ts"
24
24
  },
25
+ "./svelte": {
26
+ "node": {
27
+ "import": "./dist/svelte.es.js",
28
+ "require": "./dist/svelte.cjs.js"
29
+ },
30
+ "default": "./dist/svelte.es.js",
31
+ "types": "./src/svelte.ts"
32
+ },
25
33
  "./package.json": "./package.json"
26
34
  },
27
35
  "files": [
@@ -83,5 +91,5 @@
83
91
  "tippy.js": "^6.3.7",
84
92
  "vue-demi": "^0.12.1"
85
93
  },
86
- "readme": "# @netless/fastboard\n\nA whiteboard starter, based on [white-web-sdk](https://www.npmjs.com/package/white-web-sdk), [@netless/window-manager](https://www.npmjs.com/package/@netless/window-manager)\nand [netless-app](https://github.com/netless-io/netless-app).\n\n## 目录\n\n- [安装](#install)\n- [使用](#usage)\n- [进阶](./docs/advanced.md)\n- [开发](#开发)\n\n<h2 id=\"install\">安装</h2>\n\n```bash\nnpm add @netless/fastboard @netless/window-manager white-web-sdk react react-dom\n```\n\n<h2 id=\"usage\">使用</h2>\n\n<h3 id=\"mount-whiteboard\">挂载白板</h3>\n\n> 原生 `javascript`\n\n```js\nimport { createWhiteboardApp } from \"@netless/fastboard\";\n\nlet whiteboard = await createWhiteboardApp({\n target: document.getElementById(\"whiteboard\"),\n // [1]\n sdkConfig: {\n appIdentifier: \"whiteboard-appid\",\n },\n // [2]\n joinRoom: {\n uid: \"unique_id_for_each_client\",\n uuid: \"room-uuid\",\n roomToken: \"NETLESSROOM_...\",\n },\n // [3]\n managerConfig: {\n cursor: true,\n },\n});\n```\n\n> 使用 `React`\n\n```typescript\nimport { useFastboard, FastBoardConfig } from \"@netless/fastboard\";\nimport ReactDOM from \"react-dom\";\n\nconst config: FastBoardConfig = {\n sdkConfig: {\n appIdentifier: \"whiteboard-appid\",\n },\n joinRoom: {\n uid: \"unique_id_for_each_client\",\n uuid: \"room-uuid\",\n roomToken: \"NETLESSROOM_...\",\n },\n};\n\nfunction App() {\n const [app, ref] = useFastboard(config);\n return <div ref={ref} />;\n}\nReactDOM.render(<App />, document.getElementById(\"root\"));\n```\n\n[1] 关于 SDK 更多配置请看 [构造 WhiteWebSDK](https://developer.netless.link/javascript-zh/home/construct-white-web-sdk)\n\n[2] 加入房间更多配置请看 [构造 Room 与 Player 对象](https://developer.netless.link/javascript-zh/home/construct-room-and-player)\n\n[3] 配置 `WindowManager` 请看 [WindowManager](https://github.com/netless-io/window-manager#mount)\n\n### 使用 APPS\n\n**注意:** 需要先安装对应的 APP\n\n```bash\nnpm add @netless/app-slide\n```\n\n```typescript\n// 插入动态 PPTX 至白板\nconst appId = await whiteboard.insertDocs({\n fileType: \"pptx\",\n params: {\n scenePath: `/ppt/${uuid}`, // [1]\n title: \"a.pptx\",\n taskId: \"1234567...\", // [2]\n url: \"https://convertcdn.netless.link/dynamicConvert\", // [3]\n },\n});\n\n// 插入 PDF/静态 PPT 至白板\nconst appId = await whiteboard.insertDocs({\n fileType: \"pdf\", // or ppt\n options: {\n scenePath: `/pdf${uuid}`,\n title: \"a.pdf\", // 可选\n scenes: [], // SceneDefinition[] 静态/动态 Scene 数据\n },\n});\n\n// 插入音频/视频至白板\nconst appId = await whiteboard.manager.addApp({\n kind: \"MediaPlayer\",\n options: {\n title: \"test.mp3\", // 可选\n },\n attributes: {\n src: \"xxxx\", // 音视频 url\n },\n});\n```\n\n更多 `app` 请看 [netless-app](#https://github.com/netless-io/netless-app)\n\n## Develop\n\n```bash\npnpm i\n# upgrade dependencies\npnpm up -Li\n# build and see bundle size\npnpm build\nopen node_modules/.visualizer/stats.html\n```\n\n## License\n\nMIT @ [netless](https://github.com/netless-io)\n"
94
+ "readme": "# @netless/fastboard\n\nA whiteboard starter, based on [white-web-sdk](https://www.npmjs.com/package/white-web-sdk), [@netless/window-manager](https://www.npmjs.com/package/@netless/window-manager)\nand [netless-app](https://github.com/netless-io/netless-app).\n\n## 目录\n\n- [安装](#install)\n- [使用](#usage)\n- [进阶](./docs/advanced.md)\n- [开发](#开发)\n\n<h2 id=\"install\">安装</h2>\n\n```bash\nnpm add @netless/fastboard @netless/window-manager white-web-sdk react react-dom\n```\n\n<h2 id=\"usage\">使用</h2>\n\n<h3 id=\"mount-whiteboard\">挂载白板</h3>\n\n> 原生 `javascript`\n\n```js\nimport { createWhiteboardApp } from \"@netless/fastboard\";\n\nlet app = await createWhiteboardApp({\n target: document.getElementById(\"whiteboard\"),\n // [1]\n sdkConfig: {\n appIdentifier: \"whiteboard-appid\",\n },\n // [2]\n joinRoom: {\n uid: \"unique_id_for_each_client\",\n uuid: \"room-uuid\",\n roomToken: \"NETLESSROOM_...\",\n },\n // [3]\n managerConfig: {\n cursor: true,\n },\n});\n```\n\n> 使用 `React`\n\n```jsx\nimport { createWhiteboardApp, Fastboard } from \"@netless/fastboard\";\nimport ReactDOM from \"react-dom\";\n\nlet app = await createWhiteboardApp({\n /* ... */\n});\n\nfunction App() {\n return <Fastboard app={app} />;\n}\nReactDOM.render(<App />, document.getElementById(\"root\"));\n```\n\n[1] 关于 SDK 更多配置请看 [构造 WhiteWebSDK](https://developer.netless.link/javascript-zh/home/construct-white-web-sdk)\n\n[2] 加入房间更多配置请看 [构造 Room 与 Player 对象](https://developer.netless.link/javascript-zh/home/construct-room-and-player)\n\n[3] 配置 `WindowManager` 请看 [WindowManager](https://github.com/netless-io/window-manager#mount)\n\n### 使用 APPS\n\n**注意:** 需要先安装对应的 APP\n\n```bash\nnpm add @netless/app-slide\n```\n\n```typescript\n// 插入动态 PPTX 至白板\nconst appId = await app.insertDocs({\n fileType: \"pptx\",\n params: {\n scenePath: `/ppt/${uuid}`, // [1]\n title: \"a.pptx\",\n taskId: \"1234567...\", // [2]\n url: \"https://convertcdn.netless.link/dynamicConvert\", // [3]\n },\n});\n\n// 插入 PDF/静态 PPT 至白板\nconst appId = await app.insertDocs({\n fileType: \"pdf\", // or ppt\n options: {\n scenePath: `/pdf${uuid}`,\n title: \"a.pdf\", // 可选\n scenes: [], // SceneDefinition[] 静态/动态 Scene 数据\n },\n});\n\n// 插入音频/视频至白板\nconst appId = await app.manager.addApp({\n kind: \"MediaPlayer\",\n options: {\n title: \"test.mp3\", // 可选\n },\n attributes: {\n src: \"xxxx\", // 音视频 url\n },\n});\n```\n\n更多 `app` 请看 [netless-app](#https://github.com/netless-io/netless-app)\n\n## Develop\n\n```bash\npnpm i\n# upgrade dependencies\npnpm up -Li\n# build and see bundle size\npnpm build\nopen node_modules/.visualizer/stats.html\n```\n\n## License\n\nMIT @ [netless](https://github.com/netless-io)\n"
87
95
  }
@@ -1,33 +1,41 @@
1
+ import type { ConversionResponse, SceneDefinition } from "white-web-sdk";
1
2
  import type {
2
3
  InsertDocsParams,
4
+ InsertMediaParams,
3
5
  Language,
4
6
  Layout,
5
7
  WhiteboardAppConfig,
6
8
  } from "./internal";
7
- import { Instance } from "./internal";
9
+ import { genUID, Instance, makeSlideParams } from "./internal";
8
10
 
9
- export type { Language, Layout, WhiteboardAppConfig, InsertDocsParams };
11
+ export type {
12
+ Language,
13
+ Layout,
14
+ WhiteboardAppConfig,
15
+ InsertMediaParams,
16
+ InsertDocsParams,
17
+ };
10
18
 
11
19
  export class WhiteboardApp {
12
20
  private readonly _instance: Instance;
13
21
 
14
- constructor(readonly config: WhiteboardAppConfig) {
22
+ public constructor(readonly config: WhiteboardAppConfig) {
15
23
  this._instance = new Instance(config);
16
24
  }
17
25
 
18
- get room() {
26
+ public get room() {
19
27
  return this._instance.room;
20
28
  }
21
29
 
22
- get manager() {
30
+ public get manager() {
23
31
  return this._instance.manager;
24
32
  }
25
33
 
26
- get sdk() {
34
+ public get sdk() {
27
35
  return this._instance.sdk;
28
36
  }
29
37
 
30
- get i18n() {
38
+ public get i18n() {
31
39
  return this._instance.i18n;
32
40
  }
33
41
 
@@ -39,11 +47,12 @@ export class WhiteboardApp {
39
47
  return this._instance.collector;
40
48
  }
41
49
 
42
- public bindElement(
43
- target?: HTMLElement | null,
44
- collector?: HTMLElement | null
45
- ) {
46
- this._instance.bindElement(target || null, collector || null);
50
+ public bindElement(target?: HTMLElement | null) {
51
+ this._instance.bindElement(target || null);
52
+ }
53
+
54
+ public bindCollector(collector?: HTMLElement | null) {
55
+ this._instance.bindCollector(collector || null);
47
56
  }
48
57
 
49
58
  public get layout() {
@@ -54,8 +63,65 @@ export class WhiteboardApp {
54
63
  this._instance.updateLayout(layout);
55
64
  }
56
65
 
57
- public insertDocs(params: InsertDocsParams) {
58
- return this._instance.insertDocs(params);
66
+ public insertMedia(params: InsertMediaParams) {
67
+ this._instance.insertMedia(params);
68
+ }
69
+
70
+ /**
71
+ * Insert PDF/PPTX from conversion result.
72
+ * @param status https://developer.netless.link/server-en/home/server-conversion#get-query-task-conversion-progress
73
+ */
74
+ public insertDocs(
75
+ filename: string,
76
+ status: ConversionResponse
77
+ ): Promise<string | undefined>;
78
+
79
+ /**
80
+ * Manual way.
81
+ */
82
+ public insertDocs(params: InsertDocsParams): Promise<string | undefined>;
83
+
84
+ public insertDocs(
85
+ arg1: string | InsertDocsParams,
86
+ arg2?: ConversionResponse
87
+ ) {
88
+ if (typeof arg1 === "object" && "fileType" in arg1) {
89
+ return this._instance.insertDocs(arg1);
90
+ } else if (arg2 && arg2.status !== "Finished") {
91
+ throw new Error("[WhiteboardApp] cannot insert a converting doc");
92
+ } else if (arg2 && arg2.progress) {
93
+ const scenes: SceneDefinition[] = arg2.progress.convertedFileList.map(
94
+ (f, i) => ({
95
+ name: String(i + 1),
96
+ ppt: {
97
+ src: f.conversionFileUrl,
98
+ width: f.width,
99
+ height: f.height,
100
+ previewURL: f.preview,
101
+ },
102
+ })
103
+ );
104
+ const uid = genUID();
105
+ const scenePath = `/${arg2.uuid}/${uid}`;
106
+ const { scenesWithoutPPT, taskId, url } = makeSlideParams(scenes);
107
+ if (taskId && url) {
108
+ return this._instance.insertDocs({
109
+ fileType: "pptx",
110
+ scenePath,
111
+ taskId,
112
+ title: arg1,
113
+ url,
114
+ scenes: scenesWithoutPPT,
115
+ });
116
+ } else {
117
+ return this._instance.insertDocs({
118
+ fileType: "pdf",
119
+ scenePath,
120
+ scenes,
121
+ title: arg1,
122
+ });
123
+ }
124
+ }
59
125
  }
60
126
 
61
127
  public insertCodeEditor() {
@@ -1,8 +1,9 @@
1
1
  import type { DependencyList } from "react";
2
2
  import type { Player } from "white-web-sdk";
3
3
 
4
- import { useCallback, useEffect, useRef, useState } from "react";
4
+ import { useCallback, useEffect, useState } from "react";
5
5
  import { PlayerPhase } from "white-web-sdk";
6
+ import { useLastValue } from "../../internal/hooks";
6
7
 
7
8
  const EMPTY_ARRAY: DependencyList = [];
8
9
 
@@ -12,14 +13,6 @@ function useForceUpdate() {
12
13
  return useCallback(() => forceUpdate_({}), EMPTY_ARRAY);
13
14
  }
14
15
 
15
- function useLastValue<T>(value: T) {
16
- const ref = useRef<T>(value);
17
- useEffect(() => {
18
- ref.current = value;
19
- }, [value]);
20
- return ref.current;
21
- }
22
-
23
16
  export function usePlayer(player?: Player | null) {
24
17
  const togglePlay = useCallback(() => {
25
18
  if (player) {
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@ export {
15
15
  type PlayerControlProps,
16
16
  } from "./components/PlayerControl";
17
17
  export * from "./WhiteboardApp";
18
- export * from "./hooks";
18
+ export * from "./react";
19
19
 
20
20
  export const register = WindowManager.register.bind(WindowManager);
21
21
 
@@ -6,6 +6,7 @@ import type { i18n } from "i18next";
6
6
 
7
7
  import React, { createContext, useContext } from "react";
8
8
  import ReactDOM from "react-dom";
9
+ import { BuiltinApps } from "@netless/window-manager";
9
10
 
10
11
  import { Root } from "../components/Root";
11
12
  import { mountWhiteboard } from "./mount-whiteboard";
@@ -18,6 +19,11 @@ export interface AcceptParams {
18
19
  readonly i18n: i18n;
19
20
  }
20
21
 
22
+ export interface InsertMediaParams {
23
+ title: string;
24
+ src: string;
25
+ }
26
+
21
27
  export interface InsertDocsStatic {
22
28
  readonly fileType: "pdf" | "ppt";
23
29
  readonly scenePath: string;
@@ -31,6 +37,7 @@ export interface InsertDocsDynamic {
31
37
  readonly taskId: string;
32
38
  readonly title?: string;
33
39
  readonly url?: string;
40
+ readonly scenes?: SceneDefinition[];
34
41
  }
35
42
 
36
43
  export type InsertDocsParams = InsertDocsStatic | InsertDocsDynamic;
@@ -110,15 +117,21 @@ export class Instance {
110
117
  target: HTMLElement | null = null;
111
118
  collector: HTMLElement | null = null;
112
119
 
113
- bindElement(target: HTMLElement | null, collector: HTMLElement | null) {
114
- if (this.target && target) {
120
+ bindElement(target: HTMLElement | null) {
121
+ if (this.target && this.target !== target) {
115
122
  ReactDOM.unmountComponentAtNode(this.target);
116
123
  }
117
124
  this.target = target;
118
- this.collector = collector;
119
125
  this.forceUpdate();
120
126
  }
121
127
 
128
+ bindCollector(collector: HTMLElement | null) {
129
+ this.collector = collector;
130
+ if (this.manager && collector) {
131
+ this.manager.bindCollectorContainer(collector);
132
+ }
133
+ }
134
+
122
135
  updateLayout(layout: Layout | undefined) {
123
136
  this.config.layout = layout;
124
137
  this.forceUpdate();
@@ -154,10 +167,9 @@ export class Instance {
154
167
  if (!this.manager) {
155
168
  throw new Error(`[WhiteboardApp] mounted, but not found window manager`);
156
169
  }
170
+ this.manager.bindContainer(node);
157
171
  if (this.collector) {
158
- this.manager.bindContainer(node, this.collector);
159
- } else {
160
- this.manager.bindContainer(node);
172
+ this.manager.bindCollectorContainer(this.collector);
161
173
  }
162
174
  }
163
175
 
@@ -198,6 +210,7 @@ export class Instance {
198
210
  options: {
199
211
  scenePath: params.scenePath,
200
212
  title: params.title,
213
+ scenes: params.scenes,
201
214
  },
202
215
  attributes: {
203
216
  taskId: params.taskId,
@@ -237,6 +250,17 @@ export class Instance {
237
250
  });
238
251
  }
239
252
 
253
+ insertMedia({ title, src }: InsertMediaParams) {
254
+ if (!this.manager) {
255
+ throw new Error(`[WhiteboardApp] cannot insert app before mounted`);
256
+ }
257
+ return this.manager.addApp({
258
+ kind: BuiltinApps.MediaPlayer,
259
+ options: { title },
260
+ attributes: { src },
261
+ });
262
+ }
263
+
240
264
  async changeLanguage(language: Language) {
241
265
  try {
242
266
  await this.i18n?.changeLanguage(language);
@@ -1,3 +1,5 @@
1
+ import type { SceneDefinition } from "white-web-sdk";
2
+
1
3
  export function noop() {
2
4
  return;
3
5
  }
@@ -40,3 +42,45 @@ export class Lock {
40
42
  }
41
43
  };
42
44
  }
45
+
46
+ // Copy from https://github.com/crimx/side-effect-manager/blob/main/src/gen-uid.ts
47
+ const SOUP =
48
+ "!#%()*+,-./:;=?@[]^_`{|}~" +
49
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
50
+ const SOUP_LEN = SOUP.length;
51
+ const ID_LEN = 20;
52
+ const reusedIdCarrier = Array(ID_LEN);
53
+
54
+ export const genUID = (): string => {
55
+ for (let i = 0; i < ID_LEN; i++) {
56
+ reusedIdCarrier[i] = SOUP.charAt(Math.random() * SOUP_LEN);
57
+ }
58
+ return reusedIdCarrier.join("");
59
+ };
60
+
61
+ export function makeSlideParams(scenes: SceneDefinition[]) {
62
+ const scenesWithoutPPT: SceneDefinition[] = [];
63
+ let taskId = "";
64
+ let url = "";
65
+
66
+ // e.g. "ppt(x)://cdn/prefix/dynamicConvert/{taskId}/1.slide"
67
+ const pptSrcRE = /^pptx?(?<prefix>:\/\/\S+?dynamicConvert)\/(?<taskId>\w+)\//;
68
+
69
+ for (const { name, ppt } of scenes) {
70
+ // make sure scenesWithoutPPT.length === scenes.length
71
+ scenesWithoutPPT.push({ name });
72
+
73
+ if (!ppt || !ppt.src.startsWith("ppt")) {
74
+ continue;
75
+ }
76
+ const match = pptSrcRE.exec(ppt.src);
77
+ if (!match || !match.groups) {
78
+ continue;
79
+ }
80
+ taskId = match.groups.taskId;
81
+ url = "https" + match.groups.prefix;
82
+ break;
83
+ }
84
+
85
+ return { scenesWithoutPPT, taskId, url };
86
+ }
@@ -0,0 +1,9 @@
1
+ import { useEffect, useRef } from "react";
2
+
3
+ export function useLastValue<T>(value: T) {
4
+ const ref = useRef<T>(value);
5
+ useEffect(() => {
6
+ ref.current = value;
7
+ }, [value]);
8
+ return ref.current;
9
+ }
package/src/react.tsx ADDED
@@ -0,0 +1,26 @@
1
+ import type { WhiteboardApp } from "./index";
2
+
3
+ import React, { useEffect, useRef } from "react";
4
+ import { useLastValue } from "./internal/hooks";
5
+
6
+ /**
7
+ * @example
8
+ * let app = await createWhiteboardApp(config)
9
+ * <Fastboard app={app} />
10
+ * await app.dispose()
11
+ */
12
+ export function Fastboard({ app }: { app?: WhiteboardApp | null }) {
13
+ const ref = useRef<HTMLDivElement>(null);
14
+ const previous = useLastValue(app);
15
+
16
+ useEffect(() => {
17
+ if (previous && previous !== app) {
18
+ previous.bindElement(null);
19
+ }
20
+ if (app) {
21
+ app.bindElement(ref.current);
22
+ }
23
+ }, [app, previous]);
24
+
25
+ return <div className="fastboard" ref={ref} />;
26
+ }
package/src/style.scss CHANGED
@@ -9,6 +9,12 @@
9
9
  @import "./components/Toolbar/Toolbar.scss";
10
10
  @import "./components/PlayerControl/PlayerControl.scss";
11
11
 
12
+ .fastboard {
13
+ width: 100%;
14
+ height: 100%;
15
+ position: relative;
16
+ }
17
+
12
18
  .tippy-box.fastboard-tip {
13
19
  color: #eee;
14
20
  background-color: rgba($color: #000000, $alpha: 0.95);
package/src/hooks.ts DELETED
@@ -1,53 +0,0 @@
1
- import type { WhiteboardApp, WhiteboardAppConfig } from "./WhiteboardApp";
2
-
3
- import { useEffect, useState } from "react";
4
- import { createWhiteboardApp } from "./index";
5
-
6
- export type FastBoardConfig = WhiteboardAppConfig;
7
-
8
- /**
9
- * @example
10
- * const [app, ref] = useFastboard({ sdkConfig, joinRoom })
11
- * if (app) {
12
- * app.insertDocs({...})
13
- * }
14
- * return <div style={{ width: '100%', height: '100%' }} ref={ref} />
15
- */
16
- export function useFastboard(config: FastBoardConfig): readonly [
17
- app: WhiteboardApp | null,
18
- ref: (div: HTMLDivElement | null) => void,
19
- collectorRef: (div: HTMLDivElement | null) => void
20
- ] & {
21
- readonly app: WhiteboardApp | null;
22
- readonly ref: (div: HTMLDivElement | null) => void;
23
- readonly collectorRef: (div: HTMLDivElement | null) => void;
24
- } {
25
- const [app, setApp] = useState<WhiteboardApp | null>(null);
26
- const [currentTarget, ref] = useState<HTMLDivElement | null>(null);
27
- const [collector, collectorRef] = useState<HTMLDivElement | null>(null);
28
-
29
- useEffect(() => {
30
- let isMounted = true;
31
- const promise = createWhiteboardApp(config).then(app => {
32
- if (isMounted) setApp(app);
33
- });
34
- return () => {
35
- isMounted = false;
36
- promise.then(() => app?.dispose());
37
- };
38
- // ignore config and app change
39
- // eslint-disable-next-line react-hooks/exhaustive-deps
40
- }, []);
41
-
42
- useEffect(() => {
43
- if (app) {
44
- app.bindElement(currentTarget, collector);
45
- }
46
- }, [app, collector, currentTarget]);
47
-
48
- return Object.assign([app, ref, collectorRef] as const, {
49
- app,
50
- ref,
51
- collectorRef,
52
- });
53
- }