spire.officejs-react-brook 1.1.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.
package/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # spire.officejs-react
2
+
3
+ A React component library for integrating Spire.OfficeJS editors into your React applications.
4
+
5
+ # Installation
6
+
7
+ ```bash
8
+ npm install spire.officejs-react
9
+ ```
10
+
11
+ # Usage
12
+
13
+ ```jsx
14
+ import React, { useRef, useEffect, useState } from 'react';
15
+ import { useNavigate } from 'react-router-dom'
16
+ import OfficejsEditor from 'spire.officejs-react';
17
+
18
+ function App() {
19
+ const file = new File() // The actual File Data of the document
20
+ const fileUint8Data = new Uint8Array() // The actual Uint8Array data of the document
21
+ const navigate = useNavigate()
22
+ const EditorRef = React.useRef()
23
+ const config = useRef({})
24
+ const serverUrl = useRef('')
25
+ let Editor = useRef(null)
26
+ let Api = useRef(null)
27
+ let originUrl = window.location.origin
28
+ let env = ''
29
+ const [isEditorReady, setIsEditorReady] = useState(false)
30
+ const editorTypes = {
31
+ document: 'document',
32
+ pdf: 'pdf',
33
+ spreadsheet: 'spreadsheet',
34
+ presentation: 'presentation'
35
+ }
36
+ const exts_document = ['docx', 'docm', 'doc', 'dotx', 'dotm', 'dot', 'odt', 'fodt', 'ott', 'txt', 'wps', 'wpt']
37
+ const exts_spreadsheet = ['xlsx', 'csv', 'xlsm', 'xls', 'xltx', 'xltm', 'xlt', 'et', 'ett']
38
+ const exts_presentation = ['pptx', 'pptm', 'ppt', 'ppsx', 'ppsm', 'pps', 'potx', 'potm', 'dps', 'dpt']
39
+ const exts_pdf = ['pdf', 'xps']
40
+
41
+ useEffect(() => {
42
+ if (!file) {
43
+ navigate('/')
44
+ return
45
+ }
46
+ init()
47
+ }, [])
48
+
49
+ useEffect(() => {
50
+ if (config.current && config.current.fileAttrs && EditorRef.current) {
51
+ const timer = setTimeout(() => {
52
+ setIsEditorReady(true)
53
+ }, 100)
54
+ return () => clearTimeout(timer)
55
+ }
56
+ }, [config.current, EditorRef.current])
57
+
58
+ useEffect(() => {
59
+ if (isEditorReady && EditorRef.current && EditorRef.current.open) {
60
+ EditorRef.current.open()
61
+ }
62
+ }, [isEditorReady])
63
+
64
+ const init = () => {
65
+ initConfig()
66
+ }
67
+ const initConfig = () => {
68
+ var url = new URL(import.meta.url)
69
+ var editorType = ''
70
+ var ext = getFileExtension()
71
+ if (import.meta.env.DEV) {
72
+ serverUrl.current = `http://127.0.0.1:7000`
73
+ originUrl = `http://127.0.0.1:7000`
74
+ env = 'develop'
75
+ } else if (import.meta.env.PROD) {
76
+ serverUrl.current = `${url.origin}/spire.officejs`
77
+ originUrl = `${url.origin}/spire.officejs`
78
+ env = 'product'
79
+ }
80
+
81
+ if (exts_document.includes(ext)) editorType = editorTypes.document
82
+ else if (exts_spreadsheet.includes(ext))
83
+ editorType = editorTypes.spreadsheet
84
+ else if (exts_presentation.includes(ext))
85
+ editorType = editorTypes.presentation
86
+ else if (exts_pdf.includes(ext)) editorType = editorTypes.pdf
87
+
88
+ config.current = {
89
+ fileAttrs: {
90
+ fileInfo: {
91
+ name: file.name,
92
+ ext: ext,
93
+ primary: String(new Date().getTime()), //this is key
94
+ creator: 'Jonn',
95
+ createTime: '2022-04-18 11:30:43'
96
+ },
97
+ sourceUrl: originUrl + '/files/__ffff_192.168.2.134/' + file.name,
98
+ createUrl: originUrl + '/open',
99
+ mergeFolderUrl: '',
100
+ fileChoiceUrl: '',
101
+ templates: {}
102
+ },
103
+ user: {
104
+ id: 'uid-1',
105
+ name: 'Jonn',
106
+ canSave: true
107
+ },
108
+ editorAttrs: {
109
+ editorMode: file.name.endsWith('.pdf') ? 'view' : 'edit',
110
+ editorWidth: '100%',
111
+ editorHeight: '100%',
112
+ editorType: editorType,
113
+ platform: 'desktop', //desktop£¬ mobile£¬ embedded
114
+ viewLanguage: 'zh', //en/zh
115
+ isReadOnly: false,
116
+ canChat: true,
117
+ canComment: true,
118
+ canReview: true,
119
+ canDownload: true,
120
+ canEdit: file.name.endsWith('.pdf') ? false : true,
121
+ canForcesave: true,
122
+ embedded: {
123
+ saveUrl: '',
124
+ embedUrl: '',
125
+ shareUrl: '',
126
+ toolbarDocked: 'top'
127
+ },
128
+ useWebAssemblyDoc: true,
129
+ useWebAssemblyExcel: true,
130
+ useWebAssemblyPpt: true,
131
+ useWebAssemblyPdf: true,
132
+ spireDocJsLicense: '',
133
+ spireXlsJsLicense: '',
134
+ spirePresentationJsLicense: '',
135
+ spirePdfJsLicense: '',
136
+ serverless: {
137
+ useServerless: true,
138
+ baseUrl: originUrl,
139
+ fileData: fileUint8Data
140
+ },
141
+ events: {
142
+ onSave: onFileSave
143
+ },
144
+ plugins: {
145
+ pluginsData: []
146
+ }
147
+ }
148
+ }
149
+ }
150
+ const getFileExtension = () => {
151
+ const filename = file.name.split(/[\\/]/).pop()
152
+ return filename.substring(filename.lastIndexOf('.') + 1).toLowerCase() || ''
153
+ }
154
+ const onFileSave = data => {
155
+ console.log('save data', data)
156
+ }
157
+
158
+ return (
159
+ <>
160
+ <SpireOfficejsEditor
161
+ ref={EditorRef}
162
+ config={config.current}
163
+ serverUrl={serverUrl.current}
164
+ ></SpireOfficejsEditor>
165
+ </>
166
+ )
167
+ }
168
+ ```
169
+
170
+ # Parameters
171
+
172
+ ## Props
173
+
174
+ | Property | Description | Type | Default |
175
+ |----------|-------------|------|---------|
176
+ | id | Editor container ID | `string` | `'SpireofficejsEditor'` |
177
+ | config | Editor configuration object | `object` | **Required** |
178
+ | serverUrl | Server URL for Spire.OfficeJS | `string` | **Required** |
179
+
180
+ ## Methods
181
+
182
+ | Method | Description |
183
+ |--------|-------------|
184
+ | open() | Manually initialize and open the editor |
185
+
186
+ # Important Notes
187
+
188
+ 1. This component requires the `spire.officejs` package to be installed and configured on your server.
189
+ 2. Make sure your server is running and accessible from the client application.
190
+ 3. The component will automatically load the necessary editor scripts from the provided `serverUrl`.
191
+ 4. For production use, ensure proper error handling and loading states.
192
+
193
+ # Supported File Formats
194
+
195
+ ## Document
196
+ .docx, .docm, .doc, .dotx, .dotm, .dot, .odt, .fodt, .ott, .txt, .wps, .wpt
197
+
198
+ ## Spreadsheet
199
+ .xlsx, .csv, .xlsm, .xls, .xltx, .xltm, .xlt, .et, .ett
200
+
201
+ ## Presentation
202
+ .pptx, .pptm, .ppt, .ppsx, .ppsm, .pps, .potx, .potm, .dps, .dpt
203
+
204
+ ## PDF
205
+ .pdf, .xps
206
+
207
+ # Build Tool Plugins
208
+
209
+ | Build Tool | Plugin Name | NPM Package |
210
+ |------------|-------------|-------------|
211
+ | Vite | vite-plugin-spire.officejs | [vite-plugin-spire.officejs](https://www.npmjs.com/package/vite-plugin-spire.officejs) |
package/es/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import { default as r } from "./src/officejsEditor/officejsEditor.mjs";
2
+ export {
3
+ r as SpireOfficejsEditor
4
+ };
@@ -0,0 +1 @@
1
+ export * from './officejsEditor/index';
@@ -0,0 +1,3 @@
1
+ import SpireOfficejsEditor from './officejsEditor';
2
+ export default SpireOfficejsEditor;
3
+ export { SpireOfficejsEditor };
@@ -0,0 +1,16 @@
1
+ export interface OfficejsEditorProps {
2
+ id?: string;
3
+ config: Object;
4
+ serverUrl: string;
5
+ }
6
+ declare global {
7
+ interface Window {
8
+ SpireofficejsEditors?: {
9
+ OpenApi: (id: string, config: any) => void;
10
+ };
11
+ SpireofficeEditor?: {
12
+ OpenApi?: (id: string, config: any) => void;
13
+ };
14
+ Api: any;
15
+ }
16
+ }
@@ -0,0 +1,2 @@
1
+ declare const officejsEditor: any;
2
+ export default officejsEditor;
@@ -0,0 +1,47 @@
1
+ import r, { forwardRef as S, useEffect as A, useImperativeHandle as j } from "react";
2
+ import n from "spire.officejs-web-editors";
3
+ const R = S(
4
+ ({ id: s = "SpireofficejsEditor", config: p, serverUrl: i }, c) => {
5
+ const d = r.useRef();
6
+ let t, o;
7
+ const a = () => {
8
+ if (!p) throw new Error('Missing parameter "config"');
9
+ if (i == null || i == null)
10
+ throw new Error('Missing parameter "serverUrl"');
11
+ };
12
+ A(() => {
13
+ l();
14
+ }, []);
15
+ const l = () => {
16
+ g(), f(), u();
17
+ }, f = () => {
18
+ i ? i.endsWith("/") ? o = i.substring(0, i.length - 1) : o = i : o = window.location.origin;
19
+ }, w = () => {
20
+ if (a(), n && n.OpenApi) {
21
+ const e = n.OpenApi;
22
+ t = new e(s, p), window.Api = t.GetOpenApi(), E();
23
+ }
24
+ }, u = () => {
25
+ if (!document.querySelector('script[src*="SpireCloudEditor.js"]')) {
26
+ var e = document.createElement("script"), h = `${o}/editors/spireapi/SpireCloudEditor.js`;
27
+ e.setAttribute("src", h), document.body.appendChild(e);
28
+ }
29
+ }, E = () => {
30
+ let e = document.getElementsByClassName(
31
+ "spire-officejs-EditorWarpper"
32
+ );
33
+ e.length && (e[0] && (e[0].style.height = screen.availHeight + "px"), window.scrollTo(0, -1), e[0] && (e[0].style.height = window.innerHeight + "px"));
34
+ }, m = () => {
35
+ w();
36
+ }, g = () => {
37
+ t && (t?.destroyEditor(), t = null), window.Api && (window.Api = null);
38
+ };
39
+ return j(c, () => ({
40
+ // 定义暴露的方法
41
+ open: m
42
+ })), /* @__PURE__ */ r.createElement("div", { className: "spire-officejs-EditorWarpper" }, /* @__PURE__ */ r.createElement("div", { id: s, ref: d }));
43
+ }
44
+ );
45
+ export {
46
+ R as default
47
+ };
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./src/officejsEditor/officejsEditor.js");exports.SpireOfficejsEditor=e.default;
@@ -0,0 +1 @@
1
+ export * from './officejsEditor/index';
@@ -0,0 +1,3 @@
1
+ import SpireOfficejsEditor from './officejsEditor';
2
+ export default SpireOfficejsEditor;
3
+ export { SpireOfficejsEditor };
@@ -0,0 +1,16 @@
1
+ export interface OfficejsEditorProps {
2
+ id?: string;
3
+ config: Object;
4
+ serverUrl: string;
5
+ }
6
+ declare global {
7
+ interface Window {
8
+ SpireofficejsEditors?: {
9
+ OpenApi: (id: string, config: any) => void;
10
+ };
11
+ SpireofficeEditor?: {
12
+ OpenApi?: (id: string, config: any) => void;
13
+ };
14
+ Api: any;
15
+ }
16
+ }
@@ -0,0 +1,2 @@
1
+ declare const officejsEditor: any;
2
+ export default officejsEditor;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("react"),n=require("spire.officejs-web-editors"),S=t.forwardRef(({id:s="SpireofficejsEditor",config:c,serverUrl:i},d)=>{const p=t.useRef();let r,o;const l=()=>{if(!c)throw new Error('Missing parameter "config"');if(i==null||i==null)throw new Error('Missing parameter "serverUrl"')};t.useEffect(()=>{a()},[]);const a=()=>{m(),f(),w()},f=()=>{i?i.endsWith("/")?o=i.substring(0,i.length-1):o=i:o=window.location.origin},u=()=>{if(l(),n&&n.OpenApi){const e=n.OpenApi;r=new e(s,c),window.Api=r.GetOpenApi(),E()}},w=()=>{if(!document.querySelector('script[src*="SpireCloudEditor.js"]')){var e=document.createElement("script"),h=`${o}/editors/spireapi/SpireCloudEditor.js`;e.setAttribute("src",h),document.body.appendChild(e)}},E=()=>{let e=document.getElementsByClassName("spire-officejs-EditorWarpper");e.length&&(e[0]&&(e[0].style.height=screen.availHeight+"px"),window.scrollTo(0,-1),e[0]&&(e[0].style.height=window.innerHeight+"px"))},g=()=>{u()},m=()=>{r&&(r?.destroyEditor(),r=null),window.Api&&(window.Api=null)};return t.useImperativeHandle(d,()=>({open:g})),t.createElement("div",{className:"spire-officejs-EditorWarpper"},t.createElement("div",{id:s,ref:p}))});exports.default=S;
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "spire.officejs-react-brook",
3
+ "version": "1.1.1",
4
+ "main": "lib/index.js",
5
+ "module": "es/index.mjs",
6
+ "scripts": {
7
+ "release": "release-it"
8
+ },
9
+ "files": [
10
+ "es",
11
+ "lib"
12
+ ],
13
+ "keywords": [
14
+ "spire.officejs",
15
+ "React",
16
+ "TypeScript"
17
+ ],
18
+ "sideEffects": [
19
+ "**/*.css"
20
+ ],
21
+ "author": "icebluezhang",
22
+ "license": "MIT",
23
+ "description": "",
24
+ "typings": "lib/index.d.ts"
25
+ }