Skip to content

Commit

Permalink
Merge pull request #3058 from nextcloud/fix/target-picker
Browse files Browse the repository at this point in the history
  • Loading branch information
juliushaertl committed Jul 13, 2023
2 parents f2f10e6 + 4dbf412 commit 403a34a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 67 deletions.
27 changes: 20 additions & 7 deletions lib/Service/FileTargetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OCP\Files\IRootFolder;
use OCP\ICacheFactory;
use OCP\IL10N;
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;

class FileTargetService {
Expand All @@ -16,6 +17,7 @@ public function __construct(
private IRootFolder $rootFolder,
private LoggerInterface $logger,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private ?string $userId,
) {
}
Expand Down Expand Up @@ -64,6 +66,13 @@ public function getFileTargets(File $file): array {
];
}

if (isset($targets['Slide'])) {
$categories['slides'] = [
'label' => $this->l10n->t('Slides'),
'entries' => $this->mapTargets($filePath, $targets['Slide'], true)
];
}

$cache->set($cacheKey, $categories);

return $categories;
Expand All @@ -73,19 +82,23 @@ public function getTargetPreview($file, $target) {
return $this->remoteService->fetchTargetThumbnail($file, $target);
}

private function mapTargets(string $filePath, array $targets): array {
private function mapTargets(string $filePath, array $targets, bool $showPreview = false): array {
$result = [];
foreach ($targets as $name => $identifier) {
$result[] = [
$targetData = [
'id' => $identifier,
'name' => $name,
// Disable previews for now as they may cause endless requests against Collabora
// 'preview' => $this->urlGenerator->linkToOCSRouteAbsolute('richdocuments.Target.getPreview', [
// 'path' => $filePath,
// 'target' => $identifier,
// ]),
];

if ($showPreview) {
$targetData['preview'] = $this->urlGenerator->linkToOCSRouteAbsolute('richdocuments.Target.getPreview', [
'path' => $filePath,
'target' => $identifier,
]);
}

$result[] = $targetData;

}
return $result;
}
Expand Down
63 changes: 3 additions & 60 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { emit } from '@nextcloud/event-bus'
import { generateOcsUrl, getRootUrl, imagePath } from '@nextcloud/router'
import { getRequestToken } from '@nextcloud/auth'
import { loadState } from '@nextcloud/initial-state'
import { showError } from '@nextcloud/dialogs'
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js'
import Config from './services/config.tsx'
import { setGuestName, shouldAskForGuestName } from './helpers/guestName.js'
import { getUIDefaults, generateCSSVarTokens, getCollaboraTheme } from './helpers/coolParameters.js'
Expand Down Expand Up @@ -368,6 +366,9 @@ const documentsMain = {
'Action_FollowUser',
'Host_VersionRestore',
'Action_RemoveView',
'Action_InsertLink',
'Action_Paste',
'Action_GetLinkPreview_Resp',
]
PostMessages.registerPostMessageHandler(({ parsed, data }) => {
console.debug('[document] Received post message ', parsed)
Expand Down Expand Up @@ -497,12 +498,6 @@ const documentsMain = {
case 'UI_Mention':
documentsMain.sendUserList(parsed.args.text)
break
case 'UI_PickLink':
documentsMain.openLinkPicker()
break
case 'Action_GetLinkPreview':
documentsMain.resolveLink(args.url)
break
default:
console.debug('[document] Unhandled post message', parsed)
}
Expand Down Expand Up @@ -659,58 +654,6 @@ const documentsMain = {
PostMessages.sendWOPIPostMessage('loolframe', 'Action_Mention', { list })
},

async openLinkPicker() {
try {
const link = await getLinkWithPicker(null, true)
try {
const url = new URL(link)
if (url.protocol === 'http:' || url.protocol === 'https:') {
PostMessages.sendWOPIPostMessage('loolframe', 'Action_InsertLink', { url: link })
return
}
} catch (e) {
console.debug('error when parsing the link picker result')
}
PostMessages.sendWOPIPostMessage('loolframe', 'Action_Paste', { Mimetype: 'text/plain', Data: link })
} catch (e) {
showError(t('richdocuments', 'Failed to get a link with the picker'))
console.error('Link picker promise rejected :', e)
}
},

async resolveLink(url) {
try {
const result = await axios.get(generateOcsUrl('references/resolve', 2), {
params: {
reference: url,
},
})
const resolvedLink = result.data.ocs.data.references[url]
const title = resolvedLink?.openGraphObject?.name
const thumbnailUrl = resolvedLink?.openGraphObject?.thumb
if (thumbnailUrl) {
try {
const imageResponse = await axios.get(thumbnailUrl, { responseType: 'blob' })
if (imageResponse?.status === 200 && imageResponse?.data) {
const reader = new FileReader()
reader.addEventListener('loadend', (e) => {
const b64Image = e.target.result
PostMessages.sendWOPIPostMessage('loolframe', 'Action_GetLinkPreview_Resp', { url, title, image: b64Image })
})
reader.readAsDataURL(imageResponse.data)
}
} catch (e) {
console.error('Error loading the reference image', e)
}
} else {
PostMessages.sendWOPIPostMessage('loolframe', 'Action_GetLinkPreview_Resp', { url, title, image: null })
}
} catch (e) {
showError(t('richdocuments', 'Failed to get the link preview'))
console.error('Error resolving a reference', e)
}
},

onStartup() {
// Does anything indicate that we need to autostart a session?
const fileId = (getSearchParam('fileId') || '').replace(/^\W*/, '')
Expand Down
5 changes: 5 additions & 0 deletions src/view/DocumentTargetPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,9 @@ h3 {
box-shadow: none !important;
flex-shrink: 0;
}
img {
max-width: 100px;
margin-left: 20px;
}
</style>
65 changes: 65 additions & 0 deletions src/view/Office.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ import PostMessageService from '../services/postMessage.tsx'
import FilesAppIntegration from './FilesAppIntegration.js'
import { LOADING_ERROR, checkCollaboraConfiguration, checkProxyStatus } from '../services/collabora.js'
import { enableScrollLock, disableScrollLock } from '../helpers/safariFixer.js'
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js'
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
const FRAME_DOCUMENT = 'FRAME_DOCUMENT'
const PostMessages = new PostMessageService({
Expand Down Expand Up @@ -132,7 +135,9 @@ export default {
loadingTimeout: null,
error: null,
views: [],
showZotero: false,
showLinkPicker: false,
}
},
computed: {
Expand Down Expand Up @@ -221,6 +226,60 @@ export default {
async share() {
FilesAppIntegration.share()
},
async pickLink() {
try {
if (this.showLinkPicker) {
return
}
this.showLinkPicker = true
const link = await getLinkWithPicker(null, true)
try {
const url = new URL(link)
if (url.protocol === 'http:' || url.protocol === 'https:') {
PostMessages.sendWOPIPostMessage(FRAME_DOCUMENT, 'Action_InsertLink', { url: link })
return
}
} catch (e) {
console.debug('error when parsing the link picker result')
}
PostMessages.sendWOPIPostMessage(FRAME_DOCUMENT, 'Action_Paste', { Mimetype: 'text/plain', Data: link })
} catch (e) {
console.error('Link picker promise rejected :', e)
} finally {
this.showLinkPicker = false
}
},
async resolveLink(url) {
try {
const result = await axios.get(generateOcsUrl('references/resolve', 2), {
params: {
reference: url,
},
})
const resolvedLink = result.data.ocs.data.references[url]
const title = resolvedLink?.openGraphObject?.name
const thumbnailUrl = resolvedLink?.openGraphObject?.thumb
if (thumbnailUrl) {
try {
const imageResponse = await axios.get(thumbnailUrl, { responseType: 'blob' })
if (imageResponse?.status === 200 && imageResponse?.data) {
const reader = new FileReader()
reader.addEventListener('loadend', (e) => {
const b64Image = e.target.result
PostMessages.sendWOPIPostMessage(FRAME_DOCUMENT, 'Action_GetLinkPreview_Resp', { url, title, image: b64Image })
})
reader.readAsDataURL(imageResponse.data)
}
} catch (e) {
console.error('Error loading the reference image', e)
}
} else {
PostMessages.sendWOPIPostMessage(FRAME_DOCUMENT, 'Action_GetLinkPreview_Resp', { url, title, image: null })
}
} catch (e) {
console.error('Error resolving a reference', e)
}
},
close() {
FilesAppIntegration.close()
disableScrollLock()
Expand Down Expand Up @@ -306,6 +365,12 @@ export default {
case 'UI_ZoteroKeyMissing':
this.showZotero = true
break
case 'UI_PickLink':
this.pickLink()
break
case 'Action_GetLinkPreview':
this.resolveLink(args.url)
break
}
},
},
Expand Down

0 comments on commit 403a34a

Please sign in to comment.