Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/translate modal #4299

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-editors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-editors.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/components/Menu/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ export default {
},
showTranslate() {
const { from, to } = this.$editor.view.state.selection
const selectedText = this.$editor.view.state.doc.textBetween(from, to, ' ')
let selectedText = this.$editor.view.state.doc.textBetween(from, to, ' ')

if (!selectedText.trim().length) {
this.$editor.commands.selectAll()
selectedText = this.$editor.view.state.doc.textContent
}

console.debug('translation click', this.$editor.view.state.selection, selectedText)
this.displayTranslate = selectedText ?? ''
},
Expand Down
118 changes: 99 additions & 19 deletions src/components/Modal/Translate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,43 @@
-->

<template>
<NcModal @close="$emit('close')">
<NcModal size="large" @close="$emit('close')">
<div class="translate-dialog">
<h2>{{ t('text', 'Translate') }}</h2>
<textarea v-model="input" />
<div class="language-selector">
<label for="fromLanguage">{{ t('text', 'Source language') }}</label>
<NcSelect v-model="fromLanguage"
input-id="fromLanguage"
:options="fromLanguages"
:append-to-body="false" />
<em>{{ t('text', 'To translate individual parts of the text, select it before using the translate function.') }}</em>
<div class="wrapper">
<div class="col">
<div class="language-selector">
<label for="fromLanguage">{{ t('text', 'Translate from') }}</label>
<NcSelect v-model="fromLanguage"
input-id="fromLanguage"
:placeholder="t('text', 'Select language')"
:options="fromLanguages"
:disabled="disableFromLanguageSelect"
:append-to-body="false" />
</div>
<textarea ref="input"
v-model="input"
autofocus
@input="autosize"
@focus="onInputFocus" />
</div>
<div class="col">
<div class="language-selector">
<label for="toLanguage">{{ t('text', 'to') }}</label>
<NcSelect v-model="toLanguage"
input-id="toLanguage"
:placeholder="t('text', 'Select language')"
:options="toLanguages"
:disabled="!fromLanguage"
:append-to-body="false" />
</div>
<textarea ref="result"
v-model="result"
readonly
:class="{'icon-loading': loading }" />
</div>
</div>
<div class="language-selector">
<label for="toLanguage">{{ t('text', 'Target language') }}</label>
<NcSelect v-model="toLanguage"
input-id="toLanguage"
:options="toLanguages"
:disabled="!fromLanguage"
:append-to-body="false" />
</div>
<textarea v-model="result" readonly :class="{'icon-loading': loading }" />
<div class="translate-actions">
<NcLoadingIcon v-if="loading" />
<NcButton v-if="!result"
Expand All @@ -64,6 +81,7 @@ import axios from '@nextcloud/axios'
import { loadState } from '@nextcloud/initial-state'
import { generateOcsUrl } from '@nextcloud/router'
import { NcModal, NcButton, NcSelect, NcLoadingIcon } from '@nextcloud/vue'
import { useIsMobileMixin } from '../Editor.provider.js'

const detectLanguageEntry = {
id: null,
Expand All @@ -78,6 +96,9 @@ export default {
NcSelect,
NcLoadingIcon,
},
mixins: [
useIsMobileMixin,
],
props: {
content: {
type: String,
Expand All @@ -94,6 +115,7 @@ export default {
canDetect: loadState('text', 'translation_can_detect'),
loading: false,
error: null,
disableFromLanguageSelect: true,
mejo- marked this conversation as resolved.
Show resolved Hide resolved
}
},
computed: {
Expand All @@ -105,7 +127,7 @@ export default {
set.add(item.from)
result.push({
id: item.from,
label: item.fromLabel,
label: !this.$isMobile ? item.fromLabel : t('text', 'Translate from {language}', { language: item.fromLabel }),
})
}
}
Expand All @@ -129,7 +151,7 @@ export default {
set.add(item.to)
result.push({
id: item.to,
label: item.toLabel,
label: !this.$isMobile ? item.toLabel : t('text', 'Translate to {language}', { language: item.toLabel }),
})
}
}
Expand Down Expand Up @@ -172,20 +194,78 @@ export default {
async contentReplace() {
this.$emit('replace-content', this.result)
},
autosize() {
this.$refs.input.style.overflowY = 'hidden'
this.$refs.input.style.height = 'auto'
const height = this.$refs.input.scrollHeight + 10
this.$refs.input.style.height = height + 'px'
this.$refs.result.style.height = height + 'px'
this.$refs.input.style.overflowY = 'auto'
},
onInputFocus() {
this.disableFromLanguageSelect = false
this.autosize()
},
},
}
</script>

<style lang="scss" scoped>
.translate-dialog {
margin: 24px;

.wrapper {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-column-gap: 16px;
margin-top: calc(var(--default-grid-baseline) * 6);

.language-selector {
font-weight: bold;
flex-wrap: wrap;
gap: var(--default-grid-baseline);
}

luka-nextcloud marked this conversation as resolved.
Show resolved Hide resolved
.col {
grid-row: 1/2;
}
}
}

textarea {
display: block;
width: 100%;
margin-bottom: 12px;
height: auto;
resize: none;
box-sizing: border-box;
overflow-y: auto;
max-height: 58vh;
}

@media (max-width: 670px) {
.translate-dialog {
.wrapper {
display: block;
}

.language-selector {
.select {
width: 100%;
}
}
}

textarea {
max-height: 20vh;
}

label {
&[for="fromLanguage"],
&[for="toLanguage"] {
display: none;
}
}
}

.language-selector {
Expand Down