mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-04-07 14:04:35 +08:00
[feature] Add Example in Admin Panel
This commit is contained in:
@ -1,6 +1,4 @@
|
|||||||
import {useState, useEffect, useRef, useCallback} from 'react';
|
import {useState, useEffect, useRef, useCallback} from 'react';
|
||||||
// Импорт файла на этапе сборки (альтернативный подход)
|
|
||||||
// import sampleDocumentUrl from '../../assets/sample-document.docx';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preview page component with ONLYOFFICE Document Editor
|
* Preview page component with ONLYOFFICE Document Editor
|
||||||
@ -13,36 +11,6 @@ function Preview(props) {
|
|||||||
const [editorConfig, setEditorConfig] = useState(null);
|
const [editorConfig, setEditorConfig] = useState(null);
|
||||||
const editorRef = useRef(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
|
* Initialize the ONLYOFFICE editor
|
||||||
*/
|
*/
|
||||||
@ -54,7 +22,7 @@ function Preview(props) {
|
|||||||
fileType: 'docx',
|
fileType: 'docx',
|
||||||
key: '0' + Math.random(),
|
key: '0' + Math.random(),
|
||||||
title: 'Example Document',
|
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',
|
documentType: 'word',
|
||||||
editorConfig: {
|
editorConfig: {
|
||||||
@ -77,20 +45,13 @@ function Preview(props) {
|
|||||||
config.type = 'mobile';
|
config.type = 'mobile';
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
setEditorConfig(config);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}, [user]);
|
}, [user]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Load ONLYOFFICE API script
|
// Load ONLYOFFICE API script
|
||||||
const script = document.createElement('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.async = true;
|
||||||
script.onload = () => {
|
script.onload = () => {
|
||||||
initEditor();
|
initEditor();
|
||||||
|
|||||||
@ -61,8 +61,6 @@ module.exports = (env, argv) => {
|
|||||||
from: '*.svg',
|
from: '*.svg',
|
||||||
to: 'static/[name][ext]'
|
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'),
|
context: path.resolve(__dirname, 'src', 'pages', 'AiIntegration', 'css'),
|
||||||
from: '**/*',
|
from: '**/*',
|
||||||
@ -77,6 +75,11 @@ module.exports = (env, argv) => {
|
|||||||
context: path.resolve(__dirname, 'src', 'pages', 'AiIntegration', 'ai'),
|
context: path.resolve(__dirname, 'src', 'pages', 'AiIntegration', 'ai'),
|
||||||
from: '**/*',
|
from: '**/*',
|
||||||
to: 'ai'
|
to: 'ai'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
context: path.resolve(__dirname, '../../../document-templates/sample'),
|
||||||
|
from: 'sample.docx',
|
||||||
|
to: 'assets/sample.docx'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user