Skip to content

Commit 445a09c

Browse files
authored
Merge pull request #42104 from nextcloud/fix/reference-picker
fix: Adjust reference picker code for the vue based FilePicker
2 parents f5415d4 + 6acdd59 commit 445a09c

10 files changed

+91
-79
lines changed

apps/files/src/views/FileReferencePickerElement.vue

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020
-->
2121

2222
<template>
23-
<div ref="picker" class="reference-file-picker" />
23+
<div :id="containerId">
24+
<FilePicker v-bind="filepickerOptions" @close="onClose" />
25+
</div>
2426
</template>
2527

26-
<script>
27-
import { FilePickerType } from '@nextcloud/dialogs'
28+
<script lang="ts">
29+
import type { Node as NcNode } from '@nextcloud/files'
30+
import type { IFilePickerButton } from '@nextcloud/dialogs'
31+
32+
import { FilePickerVue as FilePicker } from '@nextcloud/dialogs/filepicker.js'
33+
import { translate as t } from '@nextcloud/l10n'
2834
import { generateUrl } from '@nextcloud/router'
29-
export default {
35+
import { defineComponent } from 'vue'
36+
37+
export default defineComponent({
3038
name: 'FileReferencePickerElement',
3139
components: {
40+
FilePicker,
3241
},
3342
props: {
3443
providerId: {
@@ -40,74 +49,55 @@ export default {
4049
default: false,
4150
},
4251
},
43-
mounted() {
44-
this.openFilePicker()
45-
window.addEventListener('click', this.onWindowClick)
46-
},
47-
beforeDestroy() {
48-
window.removeEventListener('click', this.onWindowClick)
52+
computed: {
53+
containerId() {
54+
return `filepicker-${Math.random().toString(36).slice(7)}`
55+
},
56+
filepickerOptions() {
57+
return {
58+
allowPickDirectory: false,
59+
buttons: this.buttonFactory,
60+
container: `#${this.containerId}`,
61+
multiselect: false,
62+
name: t('files', 'Select file or folder to link to'),
63+
}
64+
},
4965
},
5066
methods: {
51-
onWindowClick(e) {
52-
if (e.target.tagName === 'A' && e.target.classList.contains('oc-dialog-close')) {
53-
this.$emit('cancel')
67+
t,
68+
69+
buttonFactory(selected: NcNode[]): IFilePickerButton[] {
70+
const buttons = [] as IFilePickerButton[]
71+
if (selected.length === 0) {
72+
buttons.push({
73+
label: t('files', 'Choose file'),
74+
type: 'tertiary' as never,
75+
callback: this.onClose,
76+
})
77+
} else {
78+
buttons.push({
79+
label: t('files', 'Choose {file}', { file: selected[0].basename }),
80+
type: 'primary',
81+
callback: this.onClose,
82+
})
5483
}
84+
return buttons
5585
},
56-
async openFilePicker() {
57-
OC.dialogs.filepicker(
58-
t('files', 'Select file or folder to link to'),
59-
(file) => {
60-
const client = OC.Files.getClient()
61-
client.getFileInfo(file).then((_status, fileInfo) => {
62-
this.submit(fileInfo.id)
63-
})
64-
},
65-
false, // multiselect
66-
[], // mime filter
67-
false, // modal
68-
FilePickerType.Choose, // type
69-
'',
70-
{
71-
target: this.$refs.picker,
72-
},
73-
)
86+
87+
onClose(nodes?: NcNode[]) {
88+
if (nodes === undefined || nodes.length === 0) {
89+
this.$emit('cancel')
90+
} else {
91+
this.onSubmit(nodes[0])
92+
}
7493
},
75-
submit(fileId) {
76-
const fileLink = window.location.protocol + '//' + window.location.host
77-
+ generateUrl('/f/{fileId}', { fileId })
78-
this.$emit('submit', fileLink)
94+
95+
onSubmit(node: NcNode) {
96+
const url = new URL(window.location.href)
97+
url.pathname = generateUrl('/f/{fileId}', { fileId: node.fileid! })
98+
url.search = ''
99+
this.$emit('submit', url.href)
79100
},
80101
},
81-
}
102+
})
82103
</script>
83-
84-
<style scoped lang="scss">
85-
.reference-file-picker {
86-
flex-grow: 1;
87-
padding: 12px 16px 16px 16px;
88-
89-
&:deep(.oc-dialog) {
90-
transform: none !important;
91-
box-shadow: none !important;
92-
flex-grow: 1 !important;
93-
position: static !important;
94-
width: 100% !important;
95-
height: auto !important;
96-
padding: 0 !important;
97-
max-width: initial;
98-
99-
.oc-dialog-close {
100-
display: none;
101-
}
102-
103-
.oc-dialog-buttonrow.onebutton.aside {
104-
position: absolute;
105-
padding: 12px 32px;
106-
}
107-
108-
.oc-dialog-content {
109-
max-width: 100% !important;
110-
}
111-
}
112-
}
113-
</style>

dist/core-unified-search.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-unified-search.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-reference-files.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-reference-files.js.LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@
1818
* You should have received a copy of the GNU Affero General Public License
1919
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*/
21+
22+
/**
23+
* @copyright Copyright (c) 2023 Ferdinand Thiessen <[email protected]>
24+
*
25+
* @author Ferdinand Thiessen <[email protected]>
26+
*
27+
* @license AGPL-3.0-or-later
28+
*
29+
* This program is free software: you can redistribute it and/or modify
30+
* it under the terms of the GNU Affero General Public License as
31+
* published by the Free Software Foundation, either version 3 of the
32+
* License, or (at your option) any later version.
33+
*
34+
* This program is distributed in the hope that it will be useful,
35+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37+
* GNU Affero General Public License for more details.
38+
*
39+
* You should have received a copy of the GNU Affero General Public License
40+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
41+
*
42+
*/

dist/files-reference-files.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/settings-vue-settings-admin-delegation.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/settings-vue-settings-admin-delegation.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/settings-vue-settings-admin-security.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/settings-vue-settings-admin-security.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)