-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathActionInsertLink.vue
More file actions
280 lines (270 loc) Β· 7.1 KB
/
ActionInsertLink.vue
File metadata and controls
280 lines (270 loc) Β· 7.1 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!--
- SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcActions
class="entry-action entry-action__insert-link"
:title="actionEntry.label"
:aria-label="actionEntry.label"
:class="activeClass"
:container="menuIDSelector"
:data-text-action-entry="actionEntry.key"
:name="actionEntry.label"
:open="menuOpen"
@update:open="
(open) => {
menuOpen = menuOpen || open
}
">
<template #icon>
<component
:is="icon"
:name="actionEntry.label"
:aria-label="actionEntry.label" />
</template>
<NcActionButton
v-if="state.active"
:data-text-action-entry="`${actionEntry.key}-remove`"
@click="removeLink">
<template #icon>
<LinkOff />
</template>
{{ t('text', 'Remove link') }}
</NcActionButton>
<NcActionButton v-if="hasMenubarLinkCustomAction" @click="linkCustomAction">
<template #icon>
<component :is="menubarLinkCustomAction.icon" />
</template>
{{ menubarLinkCustomAction.label }}
</NcActionButton>
<NcActionButton
v-if="!isUsingDirectEditing"
ref="buttonFile"
:data-text-action-entry="`${actionEntry.key}-file`"
@click="linkFile">
<template #icon>
<Folder />
</template>
{{ t('text', 'Link to file or folder') }}
</NcActionButton>
<NcActionInput
v-if="isInputMode"
type="text"
:value.sync="href"
:data-text-action-entry="`${actionEntry.key}-input`"
@submit="linkWebsite">
<template #icon>
<Web />
</template>
{{ t('text', 'Link to website') }}
</NcActionInput>
<NcActionButton
v-else
:data-text-action-entry="`${actionEntry.key}-website`"
@click="linkWebsite">
<template #icon>
<Web />
</template>
{{
state.active
? t('text', 'Update link')
: t('text', 'Link to website')
}}
</NcActionButton>
<NcActionButton
:data-text-action-entry="`${actionEntry.key}-picker`"
@click="linkPicker">
<template #icon>
<Shape />
</template>
{{ t('text', 'Open the Smart Picker') }}
</NcActionButton>
</NcActions>
</template>
<script>
import { loadState } from '@nextcloud/initial-state'
import { generateUrl } from '@nextcloud/router'
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
import NcActionInput from '@nextcloud/vue/components/NcActionInput'
import NcActions from '@nextcloud/vue/components/NcActions'
import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js'
import { getMarkAttributes, isActive } from '@tiptap/core'
import { t } from '@nextcloud/l10n'
import { buildFilePicker } from '../../helpers/filePicker.js'
import { HOOK_MENUBAR_LINK_CUSTOM_ACTION, useFileMixin } from '../Editor.provider.ts'
import { Document, LinkOff, Loading, Shape, Web } from '../icons.js'
import { BaseActionEntry } from './BaseActionEntry.js'
import { useMenuIDMixin } from './MenuBar.provider.js'
export default {
name: 'ActionInsertLink',
components: {
NcActions,
NcActionButton,
NcActionInput,
Folder,
Loading,
LinkOff,
Web,
Shape,
},
extends: BaseActionEntry,
mixins: [useFileMixin, useMenuIDMixin],
inject: {
menubarLinkCustomAction: {
from: HOOK_MENUBAR_LINK_CUSTOM_ACTION,
default: null,
},
},
data: () => {
return {
href: '',
isInputMode: false,
startPath: null,
/** Open state of the actions menu */
menuOpen: false,
isUsingDirectEditing:
loadState('text', 'directEditingToken', null) !== null,
}
},
computed: {
activeClass() {
return this.state.active ? 'is-active' : ''
},
relativePath() {
return this.$file?.relativePath ?? '/'
},
hasMenubarLinkCustomAction() {
return (
typeof this.menubarLinkCustomAction?.action === 'function'
&& this.menubarLinkCustomAction?.icon
)
},
},
methods: {
/**
* Open dialog and ask user which file to link to
* Triggered by the "link file" 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}/` : '')
})
this.menuOpen = false
})
.catch(() => {
// do not close menu but keep focus
this.$refs.buttonFile?.$el.focus()
})
},
/**
* Allow user to enter an URL manually
* Triggered when by the "link url" button
*
* @param {Event} event Triggering event
*/
linkWebsite(event) {
if (event?.type === 'submit') {
const href = [...event.target.elements].filter(
(e) => e?.type === 'text',
)[0].value
this.menuOpen = false
this.isInputMode = false
this.href = ''
return this.setLink(href, href)
}
if (isActive(this.editor?.state, 'link')) {
const attrs = getMarkAttributes(this.editor?.state, 'link')
this.href = attrs.href
}
this.isInputMode = true
},
/**
* 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) {
// Heuristics for determining if we need a https:// prefix.
const noPrefixes = [
/^[a-zA-Z]+:/, // url with protocol ("mailTo:email@domain.tld")
/^\//, // absolute path
/\?fileId=/, // relative link with fileId
/^\.\.?\//, // relative link starting with ./ or ../
/^[^.]*[/$]/, // no dots before first '/' - not a domain name
/^#/, // url fragment
]
if (url && !noPrefixes.find((regex) => url.match(regex))) {
url = 'https://' + url
}
// Avoid issues when parsing urls later on in markdown that might be entered in an invalid format (e.g. "mailto: example@example.com")
const href = url.replaceAll(' ', '%20')
const chain = this.editor?.chain()
chain.insertOrSetLink(text, { href })
chain.focus().run()
},
/**
* Remove link markup at current position
* Triggered by the "remove link" button
*/
removeLink() {
this.editor?.chain().unsetLink().focus().run()
this.menuOpen = false
},
linkPicker() {
getLinkWithPicker(null, true)
.then((link) => {
this.insertLink(link)
})
.catch((error) => {
console.error('Smart picker promise rejected', error)
})
},
linkCustomAction() {
this.menubarLinkCustomAction
.action()
.then((link) => {
this.insertLink(link)
})
.catch((error) => {
console.error('Custom link action promise rejected', error)
})
},
insertLink(link) {
if (!link) {
return
}
const chain = this.editor?.chain()
if (this.editor?.view.state?.selection.empty) {
chain.focus().insertPreview(link).run()
} else {
chain.setLink({ href: link }).focus().run()
}
},
t,
},
}
</script>
<style scoped>
.action {
/* to unify width of ActionInput and ActionButton */
min-width: 218px;
}
</style>