-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathSuggestionsBar.vue
More file actions
242 lines (218 loc) Β· 5.47 KB
/
SuggestionsBar.vue
File metadata and controls
242 lines (218 loc) Β· 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<transition name="fade">
<div v-if="isEmptyContent && !isMobileDevice" class="container-suggestions">
<NcButton
ref="linkFileOrFolder"
type="secondary"
size="normal"
class="suggestions--button"
@click="linkFile">
<template #icon>
<Folder :size="20" />
</template>
{{ t('text', 'Link to file or folder') }}
</NcButton>
<NcButton
type="secondary"
size="normal"
class="suggestions--button"
:disabled="isUploadDisabled"
:title="uploadTitle"
@click="$callChooseLocalAttachment">
<template #icon>
<Upload :size="20" />
</template>
{{ t('text', 'Upload') }}
</NcButton>
<NcButton
type="secondary"
size="normal"
class="suggestions--button"
@click="insertTable">
<template #icon>
<TableIcon :size="20" />
</template>
{{ t('text', 'Insert Table') }}
</NcButton>
<NcButton
type="secondary"
size="normal"
class="suggestions--button"
@click="linkPicker">
<template #icon>
<Shape :size="20" />
</template>
{{ t('text', 'Smart Picker') }}
</NcButton>
</div>
</transition>
</template>
<script>
import { t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import NcButton from '@nextcloud/vue/components/NcButton'
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js'
import { Folder, Shape, Table as TableIcon, Upload } from '../components/icons.js'
import { useConnection } from '../composables/useConnection.ts'
import { useEditor } from '../composables/useEditor.ts'
import { useNetworkState } from '../composables/useNetworkState.ts'
import { buildFilePicker } from '../helpers/filePicker.js'
import { isMobileDevice } from '../helpers/isMobileDevice.js'
import { useFileMixin } from './Editor.provider.ts'
import { useActionChooseLocalAttachmentMixin } from './Editor/MediaHandler.provider.js'
export default {
name: 'SuggestionsBar',
components: {
TableIcon,
Folder,
NcButton,
Shape,
Upload,
},
mixins: [useActionChooseLocalAttachmentMixin, useFileMixin],
setup() {
const { editor } = useEditor()
const { openData } = useConnection()
const { networkOnline } = useNetworkState()
return {
editor,
isMobileDevice,
networkOnline,
openData,
}
},
data: () => {
return {
startPath: null,
isEmptyContent: false,
}
},
computed: {
relativePath() {
return this.$file?.relativePath ?? '/'
},
isUploadDisabled() {
return !this.openData?.hasOwner || !this.networkOnline
},
uploadTitle() {
if (!this.networkOnline) {
return t('text', 'Disabled because you are currently offline.')
}
if (this.isUploadDisabled) {
return t(
'text',
'Uploading attachments is disabled because the file is shared from another cloud.',
)
}
return ''
},
},
mounted() {
this.editor.on('update', this.onUpdate)
this.onUpdate({ editor: this.editor })
},
beforeDestroy() {
this.editor.off('update', this.onUpdate)
},
methods: {
/**
* Open smart picker dialog
* Triggered by the "Smart Picker" button
*/
linkPicker() {
getLinkWithPicker(null, true)
.then((link) => {
const chain = this.editor.chain()
if (this.editor.view.state.selection.empty) {
chain.focus().insertPreview(link).run()
} else {
chain.setLink({ href: link }).focus().run()
}
})
.catch((error) => {
console.error('Smart picker promise rejected', error)
})
},
/**
* Insert table
* Triggered by the "Insert table" button
*/
insertTable() {
this.editor.chain().focus().insertTable()?.run()
},
/**
* Open dialog and ask user which file to link to
* Triggered by the "link to file or folder" button
*/
linkFile() {
if (this.startPath === null) {
this.startPath = this.relativePath.split('/').slice(0, -1).join('/')
}
const filePicker = buildFilePicker(this.startPath)
filePicker
.pick()
.then((file) => {
const client = OC.Files.getClient()
client.getFileInfo(file).then((_status, fileInfo) => {
const url = new URL(
generateUrl(`/f/${fileInfo.id}`),
window.origin,
)
this.setLink(url.href, fileInfo.name)
this.startPath =
fileInfo.path
+ (fileInfo.type === 'dir' ? `/${fileInfo.name}/` : '')
})
})
.catch(() => {
// do not close menu but keep focus
this.$refs.linkFileOrFolder.$el.focus()
})
},
/**
* Save user entered URL as a link markup
* Triggered when the user submits the ActionInput
*
* @param {string} url href attribute of the link
* @param {string} text Text part of the link
*/
setLink(url, text) {
this.editor.chain().insertOrSetLink(text, { href: url }).focus().run()
},
onUpdate({ editor }) {
/**
* Empty document has an empty document and an empty paragraph (open and close blocks)
*/
const EMPTY_DOCUMENT_SIZE = 4
this.isEmptyContent = editor.state.doc.nodeSize <= EMPTY_DOCUMENT_SIZE
},
t,
},
}
</script>
<style scoped lang="scss">
.container-suggestions {
display: flex;
margin-left: max(0px, (100% - var(--text-editor-max-width)) / 2);
flex-wrap: wrap;
}
.suggestions--button {
margin: 5px;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity var(--animation-slow) ease-in-out;
}
.fade-enter-to,
.fade-leave {
opacity: 1;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
</style>