weboffice-js-sdk 2.0.5-beta.2 → 2.0.5
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/README.md +72 -65
- package/dist/OfficeSDK.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/types/EmptyPage.d.ts +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,22 +6,22 @@
|
|
|
6
6
|
|
|
7
7
|
_注:此 SDK 无法用于石墨文档官网产品 (即 shimo.im) 。_
|
|
8
8
|
|
|
9
|
-
## [文档详情](https://shimo-open.github.io/
|
|
9
|
+
## [文档详情](https://shimo-open.github.io/weboffice-js-sdk/#/)
|
|
10
10
|
|
|
11
11
|
### 安装
|
|
12
12
|
|
|
13
13
|
```shell
|
|
14
14
|
# 通过 npm
|
|
15
|
-
npm install --save
|
|
15
|
+
npm install --save weboffice-js-sdk
|
|
16
16
|
|
|
17
17
|
# 通过 yarn
|
|
18
|
-
yarn add
|
|
18
|
+
yarn add weboffice-js-sdk
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
### 初始化 iframe
|
|
22
22
|
|
|
23
23
|
```js
|
|
24
|
-
const { connect } = require('
|
|
24
|
+
const { connect } = require('weboffice-js-sdk')
|
|
25
25
|
|
|
26
26
|
connect({
|
|
27
27
|
fileId: '您系统中的 file id',
|
|
@@ -29,13 +29,13 @@ connect({
|
|
|
29
29
|
signature: '用您的 app id 和 secret 签发的签名',
|
|
30
30
|
token: '用于您系统识别用户请求的 token',
|
|
31
31
|
container: document.querySelector('#shimo-file'), // iframe 挂载的目标容器元素
|
|
32
|
-
lang: 'en', //
|
|
32
|
+
lang: 'en-US', // 可选;未指定时使用编辑器默认语言
|
|
33
33
|
disableAiEntry: true, // 可选,隐藏 iframe 内 AI 入口
|
|
34
34
|
theme: {
|
|
35
35
|
name: 'light'
|
|
36
36
|
}, // 可选,透传给 iframe 内编辑器主题配置
|
|
37
37
|
userUuid:'您的uuid' // 仅在v2版本回调时需要传入(co-1.3+支持)
|
|
38
|
-
}).then((
|
|
38
|
+
}).then((officeSDK) => {
|
|
39
39
|
// ...
|
|
40
40
|
})
|
|
41
41
|
```
|
|
@@ -44,31 +44,31 @@ connect({
|
|
|
44
44
|
|
|
45
45
|
其中:
|
|
46
46
|
|
|
47
|
-
- `disableAiEntry` 为可选布尔值,透传到 iframe 内 `
|
|
47
|
+
- `disableAiEntry` 为可选布尔值,透传到 iframe 内 `OfficeSDK` 的 AI 入口开关能力。
|
|
48
48
|
- `theme` 为可选主题对象,宿主侧仅负责透传;未传时 iframe 内仍使用默认主题配置。
|
|
49
49
|
|
|
50
50
|
返回值:
|
|
51
51
|
|
|
52
52
|
```
|
|
53
|
-
Promise<
|
|
53
|
+
Promise<OfficeSDK>
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
**使用传统的 `<script>` 的方式加载:**
|
|
57
57
|
|
|
58
58
|
1. 使用 [npm view](https://docs.npmjs.com/cli/v7/commands/npm-view) 和 [npm pack](https://docs.npmjs.com/cli/v7/commands/npm-pack) 下载代码包 (`.tgz` 格式)
|
|
59
59
|
2. 将 `.tgz` 解压缩后的 `dist` 目录下的文件放置到您托管静态资源的空间,然后使用 `<script>` 引入 `index.js` 资源
|
|
60
|
-
3. 通过 `window.
|
|
60
|
+
3. 通过 `window.WebOfficeJSSDK` 对象获取对应的方法
|
|
61
61
|
|
|
62
62
|
```js
|
|
63
|
-
const { connect, FileType } = window.
|
|
63
|
+
const { connect, FileType } = window.WebOfficeJSSDK
|
|
64
64
|
// 等价于
|
|
65
|
-
const { connect, FileType } = require('
|
|
65
|
+
const { connect, FileType } = require('weboffice-js-sdk')
|
|
66
66
|
```
|
|
67
67
|
|
|
68
68
|
#### 使用示例
|
|
69
69
|
|
|
70
70
|
```js
|
|
71
|
-
const { connect } = require('
|
|
71
|
+
const { connect } = require('weboffice-js-sdk')
|
|
72
72
|
|
|
73
73
|
const fileId = '1234'
|
|
74
74
|
const uuid = 'youruuid'
|
|
@@ -84,19 +84,19 @@ connect({
|
|
|
84
84
|
container: document.querySelector('#shimo-file'), // iframe 挂载的目标容器元素
|
|
85
85
|
userUuid: uuid
|
|
86
86
|
}).then((sdk) => {
|
|
87
|
-
// sdk 即为
|
|
87
|
+
// sdk 即为 OfficeSDK 实例
|
|
88
88
|
})
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
调用 `connect()` 时,会以传入参数为基础,初始化一个 `<iframe>` 并插入 `container` 对应的元素中。
|
|
92
92
|
|
|
93
|
-
返回的 `sdk` 为 `
|
|
93
|
+
返回的 `sdk` 为 `OfficeSDK` 实例,用于和 SDK、编辑器交互。
|
|
94
94
|
|
|
95
95
|
### SDK 和编辑器实例
|
|
96
96
|
|
|
97
97
|
石墨 JS SDK 共有两种实例用于和 JS SDK 交互:
|
|
98
98
|
|
|
99
|
-
- `
|
|
99
|
+
- `OfficeSDK` 由 `connect()` 返回,处理初始化编辑器和编辑器交互的工作
|
|
100
100
|
- `Editor` 文档编辑器,直接和文档内容交互。**`Editor` 所有接口均返回 Promise**
|
|
101
101
|
|
|
102
102
|
两者之间各有独立的方法和事件,具体请查看 `docs` 目录的文档。
|
|
@@ -104,7 +104,7 @@ connect({
|
|
|
104
104
|
获取编辑器实例和与其交互:
|
|
105
105
|
|
|
106
106
|
```js
|
|
107
|
-
const { FileType } = require('
|
|
107
|
+
const { FileType } = require('weboffice-js-sdk')
|
|
108
108
|
|
|
109
109
|
// 获取编辑器实例
|
|
110
110
|
const editor = sdk.getEditor()
|
|
@@ -123,7 +123,7 @@ if (sdk.fileType === FileType.Document) {
|
|
|
123
123
|
若为 `TypeScript`,可使用 `Generic`:
|
|
124
124
|
|
|
125
125
|
```typescript
|
|
126
|
-
const { Document } = require('
|
|
126
|
+
const { Document } = require('weboffice-js-sdk')
|
|
127
127
|
|
|
128
128
|
const editor = sdk.getEditor<Document.Editor>()
|
|
129
129
|
editor.on('saveStatusChanged', (payload) => {
|
|
@@ -193,7 +193,7 @@ const sdk = await connect({
|
|
|
193
193
|
|
|
194
194
|
### 协作者模块使用说明
|
|
195
195
|
|
|
196
|
-
当 iframe 套件开启协作者能力后(依赖 `ENABLE_SDK_COLLABORATORS_MODULE` 开关),
|
|
196
|
+
当 iframe 套件开启协作者能力后(依赖 `ENABLE_SDK_COLLABORATORS_MODULE` 开关),weboffice-js-sdk 使用方可按以下方式接入:
|
|
197
197
|
|
|
198
198
|
1. **监听 `collaboratorsChanged`**
|
|
199
199
|
|
|
@@ -237,56 +237,63 @@ editor.on('editForbiddenConfirmed', ({ reason }) => {
|
|
|
237
237
|
})
|
|
238
238
|
```
|
|
239
239
|
|
|
240
|
-
###
|
|
240
|
+
### 凭证自动刷新(建议)
|
|
241
241
|
|
|
242
242
|
- `signature` 为石墨区分请求来源,并实现数据隔离的基础
|
|
243
243
|
- `token` 为您用于识别回调请求来源、是否合法的依据
|
|
244
244
|
|
|
245
245
|
具体说明请查阅在线文档:[https://platform.shimo.im/v2/docs/concepts/](https://platform.shimo.im/v2/docs/concepts/)。
|
|
246
246
|
|
|
247
|
-
由于 `signature` 和 `token`
|
|
247
|
+
由于 `signature` 和 `token` 有过期时间,建议在 `ConnectOptions` 中配置自动刷新,避免用户长时间编辑时因凭证失效而中断。
|
|
248
248
|
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
getCredentials: async () => {
|
|
281
|
-
const res = await getCredentialsFromServer()
|
|
282
|
-
return {
|
|
283
|
-
signature: res.signature,
|
|
284
|
-
token: res.token,
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
})
|
|
249
|
+
```typescript
|
|
250
|
+
const { expireMs } = (await appService.getExpireConfig()).data
|
|
251
|
+
|
|
252
|
+
const options: ConnectOptions = {
|
|
253
|
+
...config,
|
|
254
|
+
// 在过期时长的 80% 处刷新,给网络重试等情况预留时间
|
|
255
|
+
refreshCredentialsInterval: Math.ceil(expireMs * 0.8),
|
|
256
|
+
getCredentials: async () => (await appService.getCredentials()).data
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const officeSDK = await connect(options)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
示例配套接口:
|
|
263
|
+
|
|
264
|
+
- `GET /api/apps/expire-config`:返回凭证过期时长,例如 `{ expireMs: 900000 }`
|
|
265
|
+
- `GET /api/credentials`:返回新凭证,例如 `{ signature, token }`
|
|
266
|
+
|
|
267
|
+
`getCredentials` 应从接入方后端获取最新凭证,并返回同时包含 `signature` 和 `token` 的对象。接口路径可按接入方系统实际情况调整。
|
|
268
|
+
|
|
269
|
+
### 国际化:编辑器多语言(可选,co-1.8+)
|
|
270
|
+
|
|
271
|
+
接入方可在调用 `connect` 时通过 `lang` 指定编辑器界面语言,传入对应语言码即可;用户选择“系统默认”时省略 `lang`(或传 `undefined`;纯 JavaScript 也可传 `null`)。
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
const options: ConnectOptions = {
|
|
275
|
+
...config,
|
|
276
|
+
...(editorLang ? { lang: editorLang } : {}) // “系统默认”时不传 lang
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const officeSDK = await connect(options)
|
|
288
280
|
```
|
|
289
281
|
|
|
282
|
+
1.8 版本编辑器支持以下 16 种语言:
|
|
283
|
+
|
|
284
|
+
| 语言 | `lang` 语言码 | 语言 | `lang` 语言码 |
|
|
285
|
+
| ------------- | ------------- | ---------------- | ------------- |
|
|
286
|
+
| 简体中文 | `zh-CN` | 繁體中文 | `zh-TW` |
|
|
287
|
+
| English | `en-US` | 日本語 | `ja-JP` |
|
|
288
|
+
| 한국어 | `ko-KR` | Español | `es-ES` |
|
|
289
|
+
| Português | `pt-PT` | Deutsch | `de-DE` |
|
|
290
|
+
| Français | `fr-FR` | Italiano | `it-IT` |
|
|
291
|
+
| Русский | `ru-RU` | Bahasa Indonesia | `id-ID` |
|
|
292
|
+
| Tiếng Việt | `vi-VN` | ไทย | `th-TH` |
|
|
293
|
+
| Bahasa Melayu | `ms-MY` | العربية | `ar-SA` |
|
|
294
|
+
|
|
295
|
+
为兼容旧版写法,仍可传入 `en`、`ja`,SDK 会分别映射为 `en-US`、`ja-JP`。新接入建议使用表中的标准语言码。
|
|
296
|
+
|
|
290
297
|
### 如何处理 URL
|
|
291
298
|
|
|
292
299
|
由于石墨 SDK 以 `iframe` 的形式挂载到当前页面,`iframe.src` 对应的 URL 并不适合用于分享,而且在一些功能上,比如 @ 文件,需要用到您系统中对应的 URL 格式,比如 `https://your-domain/files/:id`。
|
|
@@ -294,9 +301,9 @@ const shimoSDK = await connect({
|
|
|
294
301
|
为了解决这个问题,石墨 SDK 引入 `generateUrl()` 和 `openLink()` 方法:
|
|
295
302
|
|
|
296
303
|
```js
|
|
297
|
-
import { UrlSharingType } from '
|
|
304
|
+
import { UrlSharingType } from 'weboffice-js-sdk'
|
|
298
305
|
|
|
299
|
-
const
|
|
306
|
+
const officeSDK = await connect({
|
|
300
307
|
...,
|
|
301
308
|
|
|
302
309
|
generateUrl(fileId: string, info: GenerateUrlInfo): string {
|
|
@@ -497,9 +504,9 @@ connect({
|
|
|
497
504
|
- `ui.toast.tips.edit.noPermission`
|
|
498
505
|
|
|
499
506
|
```typescript
|
|
500
|
-
import { connect } from '
|
|
507
|
+
import { connect } from 'weboffice-js-sdk'
|
|
501
508
|
|
|
502
|
-
const
|
|
509
|
+
const officeSDK = await connect({
|
|
503
510
|
ui: {
|
|
504
511
|
toast: {
|
|
505
512
|
tips: {
|
|
@@ -521,9 +528,9 @@ const shimoSDK = await connect({
|
|
|
521
528
|
此方法可显示接入方自定义 toast,具体用法如下
|
|
522
529
|
|
|
523
530
|
```typescript
|
|
524
|
-
import { connect, ShowToastOptions } from '
|
|
531
|
+
import { connect, ShowToastOptions } from 'weboffice-js-sdk'
|
|
525
532
|
|
|
526
|
-
const
|
|
533
|
+
const officeSDK = await connect({
|
|
527
534
|
// 初始化sdk时传了该方法将会拦截编辑器内的toast
|
|
528
535
|
showToast: (options: ShowToastOptions) => {
|
|
529
536
|
// show your toast
|
package/dist/OfficeSDK.d.ts
CHANGED
|
@@ -465,7 +465,7 @@ export interface SDKToastOptions {
|
|
|
465
465
|
}
|
|
466
466
|
/**
|
|
467
467
|
* 透传给 iframe 编辑器的主题配置。
|
|
468
|
-
* 当前
|
|
468
|
+
* 当前 weboffice-js-sdk 仅负责跨 iframe 传输,不在宿主侧约束字段细节。
|
|
469
469
|
*/
|
|
470
470
|
export interface CommonThemeConfig {
|
|
471
471
|
[key: string]: unknown;
|
|
@@ -596,7 +596,7 @@ export interface OfficeSDKOptions extends Omit<ContainerMethods, 'getContainerRe
|
|
|
596
596
|
headerBarsVisible?: boolean;
|
|
597
597
|
/**
|
|
598
598
|
* 是否隐藏 iframe 内 AI 入口。
|
|
599
|
-
* 对应 iframe 内
|
|
599
|
+
* 对应 iframe 内 OfficeSDK 的现有 AI 入口开关能力。
|
|
600
600
|
*/
|
|
601
601
|
disableAiEntry?: boolean;
|
|
602
602
|
/**
|