@sarv-in/document-editor-react
v1.0.0
Published
React component for SarvOffice Document Server
Readme
@sarv/document-editor-react
This repo contains the SarvOffice Docs React component which integrates SarvOffice Document Server into React projects.
Please note: Before working with this component, you need to install SarvOffice Docs. To do so, you can use Docker (recommended).
Prerequisites
This procedure requires Node.js (and npm).
Creating the demo React application with SarvOffice Docs editor
This procedure creates a basic React application and installs a SarvOffice Docs editor in it.
- Create a new React project named sarvoffice-react-demo using the Create React App package:
npx create-react-app sarvoffice-react-demo- Go to the newly created directory:
cd sarvoffice-react-demo- Install SarvOffice Docs React component from npm and save it to the package.json file with --save:
npm install --save @sarv/document-editor-react- Open the ./src/App.js file in the sarvoffice-react-demo project and replace its contents with the following code:
import React, { useRef } from 'react';
import { DocumentEditor } from "@sarv/document-editor-react";
var onDocumentReady = function (event) {
console.log("Document is loaded");
};
var onLoadComponentError = function (errorCode, errorDescription) {
switch(errorCode) {
case -1: // Unknown error loading component
console.log(errorDescription);
break;
case -2: // Error load DocsAPI from http://documentserver/
console.log(errorDescription);
break;
case -3: // DocsAPI is not defined
console.log(errorDescription);
break;
}
};
export default function App() {
return (
<>
<DocumentEditor
id="docxEditor"
documentServerUrl="http://documentserver/api.js"
config={{
"document": {
"fileType": "docx",
"key": "Khirz6zTPdfd7",
"title": "Example Document Title.docx",
"url": "https://example.com/url-to-example-document.docx"
},
"documentType": "word",
"editorConfig": {
"callbackUrl": "https://example.com/url-to-callback.js"
},
"events": {
"onDocumentReady": onDocumentReady
}
}}
onLoadComponentError={onLoadComponentError}
/>
</>
);
}Replace the following lines with your own data:
- "http://documentserver/" - replace with the URL of your server;
- "https://example.com/url-to-example-document.docx" - replace with the URL to your file;
- "https://example.com/url-to-callback.ashx" - replace with your callback URL (this is required for the saving functionality to work).
This JavaScript file will create the App component containing the SarvOffice Docs editor configured with basic features.
- Test the application using the Node.js development server:
- To start the development server, navigate to the sarvoffice-react-demo directory and run:
npm run start- To stop the development server, select on the command line or command prompt and press Ctrl+C.
Deploying the demo React application
The easiest way to deploy the application to a production environment is to install serve and create a static server:
- Install the serve package globally:
npm install -g serve- Serve your static site on the 3000 port:
serve -s buildAnother port can be adjusted using the -l or --listen flags:
serve -s build -l 4000- To serve the project folder, go to it and run the serve command:
cd sarvoffice-react-demo
serveNow you can deploy the application to the created server:
- Navigate to the sarvoffice-react-demo directory and run:
npm run buildThe build directory will be created with a production build of your app.
- Copy the contents of the sarvoffice-react-demo/build directory to the root directory of the web server (to the sarvoffice-react-demo folder).
The application will be deployed on the web server (http://localhost:3000 by default).
API
Props
| Name | Type | Default | Required | Description |
| ------------- | ------------- | ------------- | ------------- | ------------- |
| id | string | null | yes | Component unique identifier. |
| documentServerUrl | string | null | yes | Address of SarvOffice Document Server. |
| shardkey | string | boolean | true | no | The string or boolean parameter required to request load balancing during collaborative editing: all users editing the same document are served by the same server. |
| config | object | null | yes | Generic configuration object for opening a file with token. |
| onLoadComponentError | (errorCode: number, errorDescription: string) => void | null | no | The function called when an error occurs while loading a component |
Storybook
Change the address of the document server in the config/default.json file:
"documentServerUrl": "http://documentserver/"Build Storybook:
npm run build-storybookStart Storybook:
npm run storybookDevelopment
Clone project from the repository:
git clone https://github.com/Sarv/document-editor-reactInstall the project dependencies:
npm installTest the component:
npm run testBuild the project:
npm run rollupCreate the package:
npm packFeedback and support
In case you have any issues, questions, or suggestions for the SarvOffice Document Server React component, please contact us at [email protected].
Official project website: sarv.com.
