Skip to content

Commit 12469cf

Browse files
authored
Merge pull request #253 from nextcloud/fix/noid/assistant-in-viewer
Make sure the assistant is mounted after the existing viewer
2 parents 8fe10f8 + ea6ea87 commit 12469cf

1 file changed

Lines changed: 42 additions & 18 deletions

File tree

src/assistant.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,26 @@ export async function openAssistantForm({
6363
let modalMountPoint
6464
const content = document.querySelector('#content') ?? document.querySelector('#content-vue')
6565

66-
if (mountPoint === null) {
66+
if (mountPoint !== null) {
67+
// if a mount point is specified, always use it
68+
modalMountPoint = mountPoint
69+
} else {
6770
const modalId = 'assistantTextProcessingModal'
6871
modalMountPoint = document.createElement('div')
6972
modalMountPoint.id = modalId
70-
document.querySelector('body').insertBefore(modalMountPoint, content.nextSibling)
71-
} else {
72-
modalMountPoint = mountPoint
73+
// the default mount point location is different whether the assistant is opened from the viewer or not
74+
if (isInsideViewer) {
75+
// so the assistant modal is opened on top of the current viewer
76+
document.querySelector('body').append(modalMountPoint)
77+
} else {
78+
// so the viewer can be later opened on top of the assistant
79+
document.querySelector('body').insertBefore(modalMountPoint, content.nextSibling)
80+
}
7381
}
7482

75-
/*
76-
// just in case, if the smart picker exists, move it right after #content (before the assistant modal)
77-
// to make sure things get displayed in this order: picker, assistant modal, viewer
78-
const referencePickerModal = document.querySelector('body .reference-picker-modal')
79-
if (referencePickerModal) {
80-
document.querySelector('body').insertBefore(referencePickerModal, content.nextSibling)
81-
}
82-
*/
83+
// TODO remaining issue: we can't open output files in the viewer if the assistant is displayed in the viewer
84+
// because the new viewer will replace the existing one...
85+
// Maybe that's an acceptable limitation
8386

8487
const View = Vue.extend(AssistantTextProcessingModal)
8588
const view = new View({
@@ -410,19 +413,40 @@ async function showAssistantTaskResult(taskId) {
410413
* @param {object} params parameters for the assistant
411414
* @param {boolean} params.isInsideViewer Should be true if this function is called while the Viewer is displayed
412415
* @param {Array} params.actionButtons List of extra buttons to show in the assistant result form
416+
* @param {HTMLElement} params.mountPoint The DOM element in which the assistant modal will be mounted
413417
* @return {Promise<void>}
414418
*/
415-
export async function openAssistantTask(task, { isInsideViewer = undefined, actionButtons = undefined } = {}) {
419+
export async function openAssistantTask(
420+
task,
421+
{
422+
isInsideViewer = undefined,
423+
actionButtons = undefined,
424+
mountPoint = null,
425+
} = {}) {
416426
const { default: Vue } = await import('vue')
417427
Vue.mixin({ methods: { t, n } })
418428
const { showError } = await import('@nextcloud/dialogs')
419429
const { default: AssistantTextProcessingModal } = await import('./components/AssistantTextProcessingModal.vue')
420430

421-
const modalId = 'assistantTextProcessingModal'
422-
const modalElement = document.createElement('div')
423-
modalElement.id = modalId
431+
let modalMountPoint
424432
const content = document.querySelector('#content') ?? document.querySelector('#content-vue')
425-
document.querySelector('body').insertBefore(modalElement, content.nextSibling)
433+
434+
if (mountPoint !== null) {
435+
// if a mount point is specified, always use it
436+
modalMountPoint = mountPoint
437+
} else {
438+
const modalId = 'assistantTextProcessingModal'
439+
modalMountPoint = document.createElement('div')
440+
modalMountPoint.id = modalId
441+
// the default mount point location is different whether the assistant is opened from the viewer or not
442+
if (isInsideViewer) {
443+
// so the assistant modal is opened on top of the current viewer
444+
document.querySelector('body').append(modalMountPoint)
445+
} else {
446+
// so the viewer can be later opened on top of the assistant
447+
document.querySelector('body').insertBefore(modalMountPoint, content.nextSibling)
448+
}
449+
}
426450

427451
const View = Vue.extend(AssistantTextProcessingModal)
428452
const view = new View({
@@ -435,7 +459,7 @@ export async function openAssistantTask(task, { isInsideViewer = undefined, acti
435459
showScheduleConfirmation: false,
436460
actionButtons,
437461
},
438-
}).$mount(modalElement)
462+
}).$mount(modalMountPoint)
439463
let lastTask = task
440464

441465
view.$on('cancel', () => {

0 commit comments

Comments
 (0)