Skip to content

Commit

Permalink
fix: unhandeld check in auto translate extensions in notes and news i…
Browse files Browse the repository at this point in the history
…n case of null contents - EXO-65764 (#50)

prior to this change, when trying to translate a news with empty summary or a note with empty content, an exceptio is raised.
This PR adds missing checks of those empty attributes to avoid such issue
  • Loading branch information
hakermi authored and exo-swf committed Oct 20, 2023
1 parent e78bb20 commit 91dfe1b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default {
isAutoTranslating: false,
autoTranslatedContent: null,
autoTranslatedTitle: null,
autoTranslatedSummary: null
autoTranslatedSummary: null,
hasSummary: false,
};
},
props: {
Expand All @@ -53,7 +54,8 @@ export default {
},
computed: {
hasAutoTranslation() {
return this.autoTranslatedTitle && this.autoTranslatedContent && this.autoTranslatedSummary;
return this.hasSummary && this.autoTranslatedTitle && this.autoTranslatedContent && this.autoTranslatedSummary
|| this.autoTranslatedTitle && this.autoTranslatedContent;
}
},
methods: {
Expand All @@ -62,18 +64,34 @@ export default {
this.resetAutoTranslation();
} else {
this.isAutoTranslating = true;
fetchAutoTranslation(this.news.title).then(translated => {
fetchAutoTranslation(this.news?.title).then(translated => {
this.handleTranslatedTitle(translated.translation);
fetchAutoTranslation(this.news.summary).then(translated => {
this.handleTranslatedSummary(translated.translation);
const content = this.toHtml(this.news.body).replace(/ /gi, '@nbsp@');
fetchAutoTranslation(this.toHtml(content)).then(translated => {
this.handleTranslatedContent(translated.translation);
});
});
});
if (this.news?.summary) {
this.hasSummary = true;
fetchAutoTranslation(this.news?.summary).then(translated => {
this.handleTranslatedSummary(translated.translation);
this.fetchBodyTranslation();
}).catch(() => this.isAutoTranslating = false);
} else {
this.hasSummary = false;
this.fetchBodyTranslation();
}
}).catch(() => this.isAutoTranslating = false);
}
},
excludeHtmlSpaceEntities(content) {
return content.replace(/&nbsp;/gi, '<span class="notranslate">&nbsp;</span>');
},
restoreHtmlSpaceEntities(content) {
return content.replace(/<span class="notranslate">&nbsp;<\/span>/gi, '&nbsp;');
},
fetchBodyTranslation() {
const content = this.excludeHtmlSpaceEntities(this.toHtml(this.news.body));
fetchAutoTranslation(this.toHtml(content)).then(translated => {
this.handleTranslatedContent(translated.translation);
this.isAutoTranslating = false;
}).catch(() => this.isAutoTranslating = false);
},
toHtml(content) {
const domParser = new DOMParser();
const docElement = domParser.parseFromString(content, 'text/html').documentElement;
Expand All @@ -83,7 +101,7 @@ export default {
this.isResetAutoTranslating = true;
this.autoTranslatedTitle = this.autoTranslatedSummary = this.autoTranslatedContent = null;
this.updateNewsTitle(this.news.title);
this.updateNewsSummary(this.news.title);
this.updateNewsSummary(this.news.summary);
this.updateNewsContent(this.news.body);
this.isResetAutoTranslating = false;
},
Expand All @@ -96,9 +114,8 @@ export default {
this.updateNewsSummary(translatedText);
},
handleTranslatedContent(translatedText) {
this.autoTranslatedContent = translatedText.replace(/@nbsp@/gi, '&nbsp;');
this.autoTranslatedContent = this.restoreHtmlSpaceEntities(translatedText);
this.updateNewsContent(this.autoTranslatedContent);
this.checkAutoTranslatedStatus();
},
updateNewsTitle(title) {
this.$root.$emit('update-news-title', title);
Expand All @@ -109,11 +126,6 @@ export default {
updateNewsContent(content) {
this.$root.$emit('update-news-body', content);
},
checkAutoTranslatedStatus() {
if (this.autoTranslatedTitle && this.autoTranslatedContent && this.autoTranslatedSummary) {
this.isAutoTranslating = false;
}
},
toggleTopBarLoading(loading) {
if (loading) {
document.dispatchEvent(new CustomEvent('displayTopBarLoading'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default {
autoTranslation: { value: 'autoTranslation', text: this.$t('notes.automatic.translation.label') },
previouslySelectedVersion: null,
autoTranslatedContent: null,
autoTranslatedTitle: null
autoTranslatedTitle: null,
hasContent: false
};
},
props: {
Expand Down Expand Up @@ -65,18 +66,36 @@ export default {
this.isAutoTranslating = true;
fetchAutoTranslation(this.note.title).then(translated => {
this.handleTranslatedTitle(translated.translation);
const content = this.note.content.replace(/&nbsp;/gi, '@nbsp@');
fetchAutoTranslation(content).then(translated => {
this.handleTranslatedContent(translated.translation);
});
});
if (this.note?.content) {
this.hasContent = true;
const content = this.excludeHtmlSpaceEntities(this.note.content);
fetchAutoTranslation(content).then(translated => {
this.handleTranslatedContent(translated.translation);
this.isAutoTranslating = false;
}).catch(() => this.isAutoTranslating = false);
} else {
this.hasContent = false;
this.isAutoTranslating = false;
}
}).catch(() => this.isAutoTranslating = false);
},
excludeHtmlSpaceEntities(content) {
return content.replace(/&nbsp;/gi, '<span class="notranslate">&nbsp;</span>');
},
restoreHtmlSpaceEntities(content) {
return content.replace(/<span class="notranslate">&nbsp;<\/span>/gi, '&nbsp;');
},
setAutoTranslationSelected() {
if (!this.isAutoTranslationSelected && this.autoTranslatedTitle && this.autoTranslatedContent) {
if (this.isAutoTranslationSelected) {
return;
}
if (this.autoTranslatedTitle) {
this.handleTranslatedTitle(this.autoTranslatedTitle);
}
if (this.autoTranslatedContent) {
this.handleTranslatedContent(this.autoTranslatedContent);
this.updateSelectedTranslation(this.autoTranslation);
}
this.updateSelectedTranslation(this.autoTranslation);
},
updateNoteContent(content) {
this.$root.$emit('update-note-content', content);
Expand All @@ -98,19 +117,12 @@ export default {
handleTranslatedTitle(translatedText) {
this.autoTranslatedTitle = translatedText;
this.updateNoteTitle(translatedText);
this.checkAutoTranslatedStatus();
},
handleTranslatedContent(translatedText) {
this.autoTranslatedContent = translatedText.replace(/@nbsp@/gi, '&nbsp;');
this.autoTranslatedContent = this.restoreHtmlSpaceEntities(translatedText);
this.updateNoteContent(this.autoTranslatedContent);
this.previouslySelectedVersion = this.selectedTranslation;
this.updateSelectedTranslation(this.autoTranslation);
this.checkAutoTranslatedStatus();
},
checkAutoTranslatedStatus() {
if (this.autoTranslatedTitle && this.autoTranslatedContent) {
this.isAutoTranslating = false;
}
},
toggleTopBarLoading(loading) {
if (loading) {
Expand Down

0 comments on commit 91dfe1b

Please sign in to comment.