[feature] Add Example in Admin Panel

This commit is contained in:
PauI Ostrovckij
2025-10-07 03:30:34 +03:00
parent e505939be2
commit cf9d576d32
2 changed files with 8 additions and 44 deletions

View File

@ -1,6 +1,4 @@
import {useState, useEffect, useRef, useCallback} from 'react';
// Импорт файла на этапе сборки (альтернативный подход)
// import sampleDocumentUrl from '../../assets/sample-document.docx';
/**
* Preview page component with ONLYOFFICE Document Editor
@ -13,36 +11,6 @@ function Preview(props) {
const [editorConfig, setEditorConfig] = useState(null);
const editorRef = useRef(null);
/**
* Create JWT token for ONLYOFFICE API
* @param {Object} json - Configuration object
* @param {string} secret - JWT secret
* @returns {Promise<string>} JWT token
*/
const createJWT = async (json, secret) => {
if (!secret) return null;
const header = {
typ: 'JWT',
alg: 'HS256'
};
const base64EncodeURL = str => {
return btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
};
const encodedHeader = base64EncodeURL(JSON.stringify(header));
const encodedPayload = base64EncodeURL(JSON.stringify(json));
const encoder = new TextEncoder();
const algorithm = {name: 'HMAC', hash: 'SHA-256'};
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), algorithm, false, ['sign', 'verify']);
const buf = encoder.encode(encodedHeader + '.' + encodedPayload);
const sign = await crypto.subtle.sign(algorithm.name, key, buf);
const hash = base64EncodeURL(String.fromCharCode(...new Uint8Array(sign)));
return encodedHeader + '.' + encodedPayload + '.' + hash;
};
/**
* Initialize the ONLYOFFICE editor
*/
@ -54,7 +22,7 @@ function Preview(props) {
fileType: 'docx',
key: '0' + Math.random(),
title: 'Example Document',
url: 'https://static.onlyoffice.com/assets/docs/samples/demo.docx'
url: `${window.location.origin}/${window.location.pathname.split('/')[1].includes('example') ? '' : window.location.pathname.split('/')[1] + '/'}assets/sample.docx`
},
documentType: 'word',
editorConfig: {
@ -77,20 +45,13 @@ function Preview(props) {
config.type = 'mobile';
}
try {
// Create JWT token (using demo secret for now)
config.token = await createJWT(config, 'doc-linux');
setEditorConfig(config);
} catch (error) {
console.error('Error creating JWT:', error);
setEditorConfig(config);
}
setEditorConfig(config);
}, [user]);
useEffect(() => {
// Load ONLYOFFICE API script
const script = document.createElement('script');
script.src = 'https://doc-linux.teamlab.info/web-apps/apps/api/documents/api.js';
script.src = `${process.env.REACT_APP_DOCSERVICE_URL || window.location.origin}/web-apps/apps/api/documents/api.js`;
script.async = true;
script.onload = () => {
initEditor();

View File

@ -61,8 +61,6 @@ module.exports = (env, argv) => {
from: '*.svg',
to: 'static/[name][ext]'
},
// Copy AiIntegration static assets so iframe URLs resolve correctly
// Resulting paths in build/: /css/*, /js/*, /ai/**/*
{
context: path.resolve(__dirname, 'src', 'pages', 'AiIntegration', 'css'),
from: '**/*',
@ -77,6 +75,11 @@ module.exports = (env, argv) => {
context: path.resolve(__dirname, 'src', 'pages', 'AiIntegration', 'ai'),
from: '**/*',
to: 'ai'
},
{
context: path.resolve(__dirname, '../../../document-templates/sample'),
from: 'sample.docx',
to: 'assets/sample.docx'
}
]
}),