Skip to content

Commit

Permalink
feat: handle assistant sync task
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <luka@nextcloud.com>
  • Loading branch information
luka-nextcloud committed Jan 16, 2024
1 parent 5a97124 commit 6915ba4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
use OCA\Text\Middleware\SessionMiddleware;
use OCA\Text\Notification\Notifier;
use OCA\Text\Service\ConfigService;
use OCA\TPAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down
2 changes: 1 addition & 1 deletion lib/Listeners/BeforeAssistantNotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace OCA\Text\Listeners;

use OCA\Text\AppInfo\Application;
use OCA\TPAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IURLGenerator;
Expand Down
44 changes: 37 additions & 7 deletions src/components/Assistant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import {
} from './Editor.provider.js'
import { FloatingMenu } from '@tiptap/vue-2'
import Translate from './Modal/Translate.vue'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'

const limitInRange = (num, min, max) => {
return Math.min(Math.max(parseInt(num), parseInt(min)), parseInt(max))
Expand Down Expand Up @@ -213,7 +214,7 @@ export default {
},
computed: {
showAssistant() {
return !this.$isRichWorkspace && !this.$isPublic && window?.OCA?.TPAssistant?.openAssistantForm
return !this.$isRichWorkspace && !this.$isPublic && window?.OCA?.TpAssistant?.openAssistantForm
},
identifier() {
return 'text-file:' + this.$file.fileId
Expand Down Expand Up @@ -241,15 +242,15 @@ export default {

this.$editor.on('selectionUpdate', this.onSelection)
this.fetchTasks()
this.$interval = setInterval(() => this.fetchTasks(), 60 * 1000)
subscribe('notifications:notification:received', this.checkNotification)
},
beforeDestroy() {
if (!this.showAssistant) {
return
}

this.$editor.off('selectionUpdate', this.onSelection)
clearInterval(this.$interval)
unsubscribe('notifications:notification:received', this.checkNotification)
},
methods: {
async fetchTasks() {
Expand All @@ -268,19 +269,36 @@ export default {
}
}).sort((a, b) => b.id - a.id)
},
async checkNotification(event) {
if (event.notification.app !== 'assistant' || event.notification.actions[0].type !== 'WEB') {
return
}
await this.fetchTasks()
},
onSelection() {
const { state } = this.$editor
const { from, to } = state.selection
this.selection = state.doc.textBetween(from, to, ' ')
},
async openAssistantForm(taskType = null) {
await window.OCA.TPAssistant.openAssistantForm(
await window.OCA.TpAssistant.openAssistantForm(
{
appId: 'text',
identifier: this.identifier,
taskType,
input: this.selection,
isInsideViewer: true,
closeOnResult: false,
actionButtons: [
{
type: 'primary',
title: t('text', 'Insert result'),
label: t('text', 'Insert result'),
onClick: (lastTask) => {
this.insertResult(lastTask)
},
},
],
})
await this.fetchTasks()
},
Expand Down Expand Up @@ -308,7 +326,7 @@ export default {
this.displayTranslate = false
},
async openResult(task) {
window.OCA?.TPAssistant.openAssistantResult(task)
window.OCA?.TpAssistant.openAssistantResult(task)
},
async insertResult(task) {
this.$editor.commands.insertContent(task.output)
Expand Down Expand Up @@ -344,11 +362,23 @@ export default {
getReferenceClientRect: () => {
const editorRect = this.$parent.$el.querySelector('.ProseMirror').getBoundingClientRect()
const pos = posToDOMRect(this.$editor.view, this.$editor.state.selection.from, this.$editor.state.selection.to)
let rightSpacing = 0
let addTopSpacing = 0

if (editorRect.width < 670) {
rightSpacing = 20
}

if (editorRect.width < 425) {
rightSpacing = 66
addTopSpacing = 30
}

return {
...pos,
width: editorRect.width,
width: editorRect.width - rightSpacing,
height: limitInRange(pos.height, buttonSize, window.innerHeight),
top: limitInRange(pos.top, topSpacing, window.innerHeight - bottomSpacing),
top: limitInRange(pos.top, topSpacing, window.innerHeight - bottomSpacing) + addTopSpacing,
left: editorRect.left,
right: editorRect.right,
bottom: limitInRange(pos.top + buttonSize, bottomSpacing, window.innerHeight - topSpacing) + 22,
Expand Down

0 comments on commit 6915ba4

Please sign in to comment.