Skip to content

Commit 35eba11

Browse files
authored
Merge pull request #8629 from nextcloud/backport/8628/stable34
[stable34] fix(translate): use assistant app dialog
2 parents 843e8ec + cdfb573 commit 35eba11

5 files changed

Lines changed: 54 additions & 403 deletions

File tree

cypress/e2e/Assistant.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ describe('Assistant', () => {
8181
// eslint-disable-next-line cypress/no-unnecessary-waiting
8282
cy.wait(1000)
8383

84-
cy.get('[data-cy="translate-input"]').should('be.visible').focus()
84+
cy.get('.assistant-modal--content #input-input').type('Hello World', {
85+
force: true,
86+
})
8587

86-
cy.get('[data-cy="translate-input"]').should('be.focused')
87-
cy.get('[data-cy="translate-input"]').type('Hello World')
88+
cy.get('.assistant-modal--content button').contains('Translate').click()
8889
})
8990
})

lib/Service/InitialStateProvider.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,10 @@ public function provideState(): void {
5656
);
5757

5858
$taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
59-
$fromLanguages = $taskTypes['core:text2text:translate']['inputShapeEnumValues']['origin_language'] ?? [];
60-
$toLanguages = $taskTypes['core:text2text:translate']['inputShapeEnumValues']['target_language'] ?? [];
6159

6260
$this->initialState->provideInitialState(
63-
'translation_languages',
64-
[
65-
'from' => $fromLanguages,
66-
'to' => $toLanguages,
67-
]
61+
'translation_available',
62+
isset($taskTypes['core:text2text:translate']),
6863
);
6964

7065
$filteredTypes = array_filter($taskTypes, static function (string $taskType) {

src/components/Editor.vue

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@
7474
:has-connection-issue="displayConnectionIssue"
7575
:has-indexed-db-conflict="!!indexedDbConflictContent"
7676
@reconnect="reconnect" />
77-
<Translate
78-
:show="translateModal"
79-
:content="translateContent"
80-
:file-id="fileId"
81-
@insert-content="translateInsert"
82-
@replace-content="translateReplace"
83-
@close="hideTranslate" />
8477
</div>
8578
</template>
8679

@@ -140,7 +133,6 @@ import MainContainer from './Editor/MainContainer.vue'
140133
import Status from './Editor/Status.vue'
141134
import Wrapper from './Editor/Wrapper.vue'
142135
import MenuBar from './Menu/MenuBar.vue'
143-
import Translate from './Modal/Translate.vue'
144136
import SkeletonLoading from './SkeletonLoading.vue'
145137
import SuggestionsBar from './SuggestionsBar.vue'
146138
@@ -157,7 +149,6 @@ export default defineComponent({
157149
MenuBar,
158150
Reader: () => import('./Reader.vue'),
159151
Status,
160-
Translate,
161152
SuggestionsBar,
162153
},
163154
mixins: [isMobile],
@@ -359,8 +350,6 @@ export default defineComponent({
359350
draggedOver: false,
360351
361352
contentWrapper: null,
362-
translateModal: false,
363-
translateContent: '',
364353
indexedDbConflictContent: '',
365354
}
366355
},
@@ -475,7 +464,6 @@ export default defineComponent({
475464
subscribe('text:image-node:add', this.onAddImageNode)
476465
subscribe('text:image-node:delete', this.onDeleteImageNode)
477466
this.emit('update:loaded', true)
478-
subscribe('text:translate-modal:show', this.showTranslateModal)
479467
exposeForDebugging(this)
480468
481469
await this.whenSynced
@@ -511,7 +499,6 @@ export default defineComponent({
511499
unsubscribe('text:keyboard:save', this.onKeyboardSave)
512500
unsubscribe('text:image-node:add', this.onAddImageNode)
513501
unsubscribe('text:image-node:delete', this.onDeleteImageNode)
514-
unsubscribe('text:translate-modal:show', this.showTranslateModal)
515502
if (this.dirty && !this.hasOutdatedDocument && !this.hasSyncCollision) {
516503
const timeout = new Promise((resolve) => setTimeout(resolve, 2000))
517504
await Promise.any([timeout, this.saveService.save()])
@@ -884,33 +871,9 @@ export default defineComponent({
884871
this.setEditable(this.editMode)
885872
},
886873
887-
showTranslateModal(e) {
888-
this.translateContent = e.content
889-
this.translateModal = true
890-
},
891-
hideTranslate() {
892-
this.translateModal = false
893-
},
894874
applyCommand(fn) {
895875
this.editor.commands.command(fn)
896876
},
897-
translateInsert(content) {
898-
this.applyCommand(({ tr, commands }) => {
899-
return commands.insertContentAt(tr.selection.to, content)
900-
})
901-
this.translateModal = false
902-
},
903-
translateReplace(content) {
904-
this.applyCommand(({ tr, commands }) => {
905-
const selection = tr.selection
906-
const range = {
907-
from: selection.from,
908-
to: selection.to,
909-
}
910-
return commands.insertContentAt(range, content)
911-
})
912-
this.translateModal = false
913-
},
914877
915878
saveBeforeUnload() {
916879
this.saveService.saveViaSendBeacon()

src/components/Menu/AssistantAction.vue

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
<script>
154154
import axios from '@nextcloud/axios'
155155
import { showError, showSuccess } from '@nextcloud/dialogs'
156-
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
156+
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
157157
import { loadState } from '@nextcloud/initial-state'
158158
import { t } from '@nextcloud/l10n'
159159
import { generateOcsUrl } from '@nextcloud/router'
@@ -234,8 +234,7 @@ export default {
234234
STATUS_UNKNOWN,
235235
236236
showTaskList: false,
237-
canTranslate:
238-
loadState('text', 'translation_languages', []).from?.length > 0,
237+
canTranslate: loadState('text', 'translation_available', false),
239238
}
240239
},
241240
computed: {
@@ -343,15 +342,37 @@ export default {
343342
})
344343
},
345344
openTranslateDialog() {
346-
let selectAll = false
347-
if (!this.selection.trim().length) {
348-
this.editor.commands.selectAll()
349-
selectAll = true
350-
}
351-
emit('text:translate-modal:show', { content: this.selection || '' })
352-
if (selectAll) {
353-
this.editor.commands.setTextSelection(0)
354-
}
345+
const { selection, doc } = this.editor.state
346+
const hasSelection = selection.from !== selection.to
347+
348+
const inputText = hasSelection
349+
? doc.textBetween(selection.from, selection.to, ' ')
350+
: doc.textBetween(0, doc.content.size, ' ')
351+
352+
const replaceRange = hasSelection
353+
? { from: selection.from, to: selection.to }
354+
: null
355+
356+
window.OCA.Assistant.openAssistantForm({
357+
appId: 'text',
358+
customId: this.identifier,
359+
taskType: 'core:text2text:translate',
360+
inputs: { input: inputText },
361+
isInsideViewer: true,
362+
closeOnResult: false,
363+
actionButtons: [
364+
{
365+
variant: 'primary',
366+
title: t('text', 'Insert'),
367+
label: t('text', 'Insert'),
368+
onClick: (task) => {
369+
this.translateInsert(task, replaceRange)
370+
},
371+
},
372+
],
373+
}).finally(() => {
374+
this.fetchTasks()
375+
})
355376
},
356377
async openResult(task) {
357378
window.OCA.Assistant.openAssistantTask(task, {
@@ -400,6 +421,21 @@ export default {
400421
this.$delete(this.tasks, taskIndex)
401422
}
402423
},
424+
async translateInsert(task, replaceRange) {
425+
const isMarkdown = shouldInterpretAsMarkdown(task.output.output)
426+
const content = isMarkdown
427+
? markdownit.render(task.output.output)
428+
: task.output.output
429+
if (replaceRange) {
430+
this.editor.commands.insertContentAt(replaceRange, content)
431+
} else {
432+
this.editor.commands.insertContent(content)
433+
}
434+
435+
if (this.fileId) {
436+
await markFileAsAiGenerated(this.fileId)
437+
}
438+
},
403439
t,
404440
},
405441
}

0 commit comments

Comments
 (0)