mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-22 07:51:00 +00:00
Merge pull request #209 from dingedi/feature/keep-selected-langs-in-url
keep selected languages and text in url
This commit is contained in:
commit
1df501db7f
1 changed files with 32 additions and 0 deletions
|
@ -82,6 +82,24 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sourceLanguage = self.langs.find(l => l.code === self.getQueryParam('source'))
|
||||||
|
const isSourceAuto = !sourceLanguage && self.getQueryParam('source') === "auto"
|
||||||
|
const targetLanguage = self.langs.find(l => l.code === self.getQueryParam('target'))
|
||||||
|
|
||||||
|
if (sourceLanguage || isSourceAuto) {
|
||||||
|
self.sourceLang = isSourceAuto ? "auto" : sourceLanguage.code
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetLanguage) {
|
||||||
|
self.targetLang = targetLanguage.code
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultText = self.getQueryParam('q')
|
||||||
|
|
||||||
|
if(defaultText) {
|
||||||
|
self.inputText = decodeURI(defaultText)
|
||||||
|
}
|
||||||
|
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
} else {
|
} else {
|
||||||
self.error = "Cannot load /languages";
|
self.error = "Cannot load /languages";
|
||||||
|
@ -195,9 +213,23 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
dismissError: function(){
|
dismissError: function(){
|
||||||
this.error = '';
|
this.error = '';
|
||||||
},
|
},
|
||||||
|
getQueryParam: function (key) {
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
return params.get(key)
|
||||||
|
},
|
||||||
|
updateQueryParam: function (key, value) {
|
||||||
|
let searchParams = new URLSearchParams(window.location.search)
|
||||||
|
searchParams.set(key, value);
|
||||||
|
let newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
|
||||||
|
history.pushState(null, '', newRelativePathQuery);
|
||||||
|
},
|
||||||
handleInput: function(e){
|
handleInput: function(e){
|
||||||
this.closeSuggestTranslation(e)
|
this.closeSuggestTranslation(e)
|
||||||
|
|
||||||
|
this.updateQueryParam('source', this.sourceLang)
|
||||||
|
this.updateQueryParam('target', this.targetLang)
|
||||||
|
this.updateQueryParam('q', encodeURI(this.inputText))
|
||||||
|
|
||||||
if (this.timeout) clearTimeout(this.timeout);
|
if (this.timeout) clearTimeout(this.timeout);
|
||||||
this.timeout = null;
|
this.timeout = null;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue