Fix Bergamot plugin insert functionality for PDF and other editor types

This commit is contained in:
Alexander Trofimov
2026-02-02 19:03:31 +03:00
parent 6cbccc5622
commit bbf518fa1e

View File

@ -316,11 +316,11 @@
// Event handlers // Event handlers
$(sourceSelect).on("change", function() { $(sourceSelect).on("change", function() {
updateTargetLanguages(); updateTargetLanguages();
runTranslation(); RunTranslate(txt);
}); });
$(targetSelect).on("change", function() { $(targetSelect).on("change", function() {
runTranslation(); RunTranslate(txt);
}); });
} }
@ -358,13 +358,61 @@
$(targetSelect).trigger("change.select2"); $(targetSelect).trigger("change.select2");
} }
// Run translation based on editor type
function RunTranslate(sText) {
switch (window.Asc.plugin.info.editorType) {
case 'word':
case 'slide': {
window.Asc.plugin.executeMethod("GetSelectedText",
[{Numbering:false, Math: false, TableCellSeparator: '\n', ParaSeparator: '\n'}],
function(data) {
sText = data.replace(/\r/g, ' ');
runTranslation(sText);
});
break;
}
case 'cell': {
window.Asc.plugin.executeMethod("GetSelectedText",
[{Numbering:false, Math: false, TableCellSeparator: '\n', ParaSeparator: '\n'}],
function(data) {
if (data == '')
sText = txt.replace(/\r/g, ' ').replace(/\t/g, '\n');
else
sText = data.replace(/\r/g, ' ');
runTranslation(sText);
});
break;
}
case 'pdf': {
window.Asc.plugin.executeMethod("GetSelectedText",
[{Numbering:false, Math: false, TableCellSeparator: '\n', ParaSeparator: '\n'}],
function(data) {
if (data && data.trim() !== '')
sText = data.replace(/\r/g, ' ');
runTranslation(sText);
});
break;
}
default: {
// Fallback for other editor types
runTranslation(sText);
break;
}
}
}
// Run translation // Run translation
async function runTranslation() { async function runTranslation(textToTranslate) {
if (!txt || !txt.trim()) { const textInput = textToTranslate || txt;
if (!textInput || !textInput.trim()) {
clearTranslation(); clearTranslation();
return; return;
} }
// Update txt with the actual text to translate
txt = textInput;
if (!isInitialized) { if (!isInitialized) {
await initializeTranslator(); await initializeTranslator();
} }
@ -482,6 +530,10 @@
// Plugin initialization // Plugin initialization
window.Asc.plugin.init = function(text) { window.Asc.plugin.init = function(text) {
// Hide paste button in view mode (e.g., PDF viewer)
if (window.Asc.plugin.info.isViewMode)
document.getElementById("paste").classList.add('hidden');
txt = text || ""; txt = text || "";
// Populate manual entry field with selected text // Populate manual entry field with selected text
@ -495,11 +547,11 @@
if (!isInitialized && !isInitializing) { if (!isInitialized && !isInitializing) {
initializeTranslator().then(() => { initializeTranslator().then(() => {
if (txt) { if (txt) {
runTranslation(); RunTranslate(txt);
} }
}); });
} else if (txt) { } else if (txt) {
runTranslation(); RunTranslate(txt);
} }
}; };
@ -612,7 +664,7 @@
if (enterContainer) { if (enterContainer) {
const debouncedTranslate = debounce(function() { const debouncedTranslate = debounce(function() {
txt = enterContainer.value; txt = enterContainer.value;
runTranslation(); RunTranslate(txt);
}, 500); }, 500);
enterContainer.addEventListener("input", debouncedTranslate); enterContainer.addEventListener("input", debouncedTranslate);