fix: delete DocsAPI global + reload script on close for fresh ONLYOFFICE state
This commit is contained in:
@@ -267,13 +267,24 @@ function forceSaveOnlyoffice() {
|
||||
}
|
||||
}
|
||||
|
||||
// Close the editor — standard ONLYOFFICE teardown
|
||||
// Close the editor — full teardown so the next open is completely fresh
|
||||
function closeOnlyofficeEditor() {
|
||||
if (onlyofficeEditorInstance) {
|
||||
try { onlyofficeEditorInstance.destroyEditor(); } catch (e) {}
|
||||
onlyofficeEditorInstance = null;
|
||||
}
|
||||
closeModal();
|
||||
|
||||
// Delete the DocsAPI global and remove the script tag so the next
|
||||
// openOnlyofficeEditor call loads a completely fresh ONLYOFFICE API.
|
||||
// This is the key: destroyEditor() cleans the editor instance, but
|
||||
// the DocsAPI object itself retains internal state (websocket pools,
|
||||
// document session references) that causes the next editor to load
|
||||
// a stale document. Removing the script + global forces a fresh init.
|
||||
delete window.DocsAPI;
|
||||
const oldScript = document.querySelector('script[src*="api/documents/api.js"]');
|
||||
if (oldScript) oldScript.remove();
|
||||
|
||||
loadOnlyofficeTemplates();
|
||||
}
|
||||
|
||||
@@ -305,16 +316,16 @@ async function openOnlyofficeEditor(templateId, templateName) {
|
||||
`;
|
||||
showModal(body, '', 'xlarge');
|
||||
|
||||
// Load ONLYOFFICE API script (once)
|
||||
if (typeof DocsAPI === 'undefined') {
|
||||
await new Promise((resolve, reject) => {
|
||||
const s = document.createElement('script');
|
||||
s.src = onlyofficeUrl + '/web-apps/apps/api/documents/api.js';
|
||||
s.onload = resolve;
|
||||
s.onerror = () => reject(new Error('Failed to load ONLYOFFICE API'));
|
||||
document.head.appendChild(s);
|
||||
});
|
||||
}
|
||||
// Load ONLYOFFICE API script fresh each time with cache-buster.
|
||||
// This ensures a completely new DocsAPI instance with no stale state.
|
||||
const scriptUrl = onlyofficeUrl + '/web-apps/apps/api/documents/api.js?cb=' + Date.now();
|
||||
await new Promise((resolve, reject) => {
|
||||
const s = document.createElement('script');
|
||||
s.src = scriptUrl;
|
||||
s.onload = resolve;
|
||||
s.onerror = () => reject(new Error('Failed to load ONLYOFFICE API'));
|
||||
document.head.appendChild(s);
|
||||
});
|
||||
|
||||
if (typeof DocsAPI === 'undefined' || !DocsAPI.DocEditor) {
|
||||
throw new Error('ONLYOFFICE API not loaded');
|
||||
|
||||
Reference in New Issue
Block a user