-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathSuggestionsBar.vue
More file actions
166 lines (147 loc) Β· 3.77 KB
/
Copy pathSuggestionsBar.vue
File metadata and controls
166 lines (147 loc) Β· 3.77 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
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div v-if="isEmptyContent" class="container-suggestions">
<NcButton ref="linkFileOrFolder"
type="secondary"
size="normal"
class="suggestions--button"
@click="linkFile">
<template #icon>
<Document :size="20" />
</template>
<template v-if="!isMobile" #default>
{{ t('text', 'Link to file or folder') }}
</template>
</NcButton>
<NcButton type="secondary"
size="normal"
class="suggestions--button"
@click="$callChooseLocalAttachment">
<template #icon>
<Upload :size="20" />
</template>
<template v-if="!isMobile" #default>
{{ t('text', 'Upload') }}
</template>
</NcButton>
<NcButton type="secondary"
size="normal"
class="suggestions--button"
@click="insertTable">
<template #icon>
<TableIcon :size="20" />
</template>
<template v-if="!isMobile" #default>
{{ t('text', 'Insert Table') }}
</template>
</NcButton>
</div>
</template>
<script>
import { NcButton } from '@nextcloud/vue'
import { Document, Upload, Table as TableIcon } from '../components/icons.js'
import { useActionChooseLocalAttachmentMixin } from './Editor/MediaHandler.provider.js'
import { useEditorMixin, useFileMixin } from './Editor.provider.js'
import { generateUrl } from '@nextcloud/router'
import { buildFilePicker } from '../helpers/filePicker.js'
import { useIsMobile } from '@nextcloud/vue/composables/useIsMobile'
export default {
name: 'SuggestionsBar',
components: {
TableIcon,
Document,
NcButton,
Upload,
},
mixins: [
useActionChooseLocalAttachmentMixin,
useEditorMixin,
useFileMixin,
],
setup() {
const isMobile = useIsMobile()
return {
isMobile,
}
},
data: () => {
return {
startPath: null,
isEmptyContent: false,
}
},
computed: {
relativePath() {
return this.$file?.relativePath ?? '/'
},
},
mounted() {
this.$editor.on('update', this.onUpdate)
this.onUpdate({ editor: this.$editor })
},
beforeDestroy() {
this.$editor.off('update', this.onUpdate)
},
methods: {
/**
* 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
},
},
}
</script>
<style scoped lang="scss">
.container-suggestions {
display: flex;
margin-left: max(0px, (100% - var(--text-editor-max-width)) / 2);
}
.suggestions--button {
margin: 5px;
}
</style>