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
|
<!--
- @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
-
- @author Julius Härtl <jus@bitgrid.net>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<BubbleMenu :editor="editor"
:tippy-options="{ onHide: hideLinkMenu, duration: 200, placement: 'bottom' }"
class="menububble">
<form v-if="linkMenuIsActive" class="menububble__form" @submit.prevent="setLinkUrl()">
<input ref="linkInput"
v-model="linkUrl"
class="menububble__input"
type="text"
placeholder="https://"
@keydown.esc="hideLinkMenu">
<button class="menububble__button icon-confirm"
type="button"
tabindex="0"
@click="setLinkUrl()" />
</form>
<template v-else>
<button class="menububble__button"
:class="{ 'is-active': isActive('link') }"
@click="showLinkMenu()">
<span class="icon-link" />
<span class="menububble__buttontext">
{{ isActive('link') ? t('text', 'Update Link') : t('text', 'Add Link') }}
</span>
</button>
<button v-if="!isUsingDirectEditing"
class="menububble__button"
:class="{ 'is-active': isActive('link') }"
@click="selectFile()">
<span class="icon-file" />
<span class="menububble__buttontext">{{ t('text', 'Link file') }}</span>
</button>
<button v-if="isActive('link')"
class="menububble__button"
:class="{ 'is-active': isActive('link') }"
@click="removeLinkUrl()">
<span class="icon-delete" />
<span class="menububble__buttontext">
{{ t('text', 'Remove Link') }}
</span>
</button>
</template>
</BubbleMenu>
</template>
<script>
import { BubbleMenu } from '@tiptap/vue-2'
import { getMarkAttributes } from '@tiptap/core'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
import { getCurrentUser } from '@nextcloud/auth'
import { optimalPath } from './../helpers/files'
import { loadState } from '@nextcloud/initial-state'
export default {
name: 'MenuBubble',
components: {
BubbleMenu,
},
directives: {
tooltip: Tooltip,
},
props: {
editor: {
type: Object,
required: true,
},
// used to calculate the position based on the scrollOffset
contentWrapper: {
type: HTMLDivElement,
required: false,
default: null,
},
filePath: {
type: String,
required: false,
default: '',
},
},
data: () => {
return {
linkUrl: null,
linkMenuIsActive: false,
isUsingDirectEditing: loadState('text', 'directEditingToken', null) !== null,
}
},
methods: {
showLinkMenu() {
const attrs = getMarkAttributes(this.editor.state, 'link')
this.linkUrl = attrs.href
this.linkMenuIsActive = true
this.$nextTick(() => {
this.$refs.linkInput.focus()
})
},
hideLinkMenu() {
this.linkUrl = null
this.linkMenuIsActive = false
},
selectFile() {
const currentUser = getCurrentUser()
if (!currentUser) {
return
}
const startPath = this.filePath.split('/').slice(0, -1).join('/')
OC.dialogs.filepicker(t('text', 'Select file to link to'), (file) => {
const client = OC.Files.getClient()
client.getFileInfo(file).then((_status, fileInfo) => {
const path = optimalPath(this.filePath, `${fileInfo.path}/${fileInfo.name}`)
const encodedPath = path.split('/').map(encodeURIComponent).join('/')
const href = `${encodedPath}?fileId=${fileInfo.id}`
this.editor.chain().setLink({ href }).focus().run()
this.hideLinkMenu()
})
}, false, [], true, undefined, startPath)
},
setLinkUrl() {
let url = this.linkUrl
// 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')
this.editor.chain().setLink({ href }).focus().run()
this.hideLinkMenu()
},
removeLinkUrl() {
this.editor.chain().unsetLink().focus().run()
},
isActive(selector, args = {}) {
return this.editor.isActive(selector, args)
},
},
}
</script>
<style scoped lang="scss">
.menububble {
display: flex;
z-index: 10020;
background: var(--color-main-background-translucent);
box-shadow: 0 1px 5px var(--color-box-shadow);
border-radius: var(--border-radius-large);
overflow: hidden;
padding: 0;
margin-left: 10px;
height: 44px;
&__button {
display: block;
flex-grow: 1;
border: 0;
padding: 0.9rem 0.7rem;
margin: 0;
border-radius: 0;
cursor: pointer;
background-color: var(--color-main-background);
border-right: 1px solid var(--color-border);
&:focus,
&:hover {
background-color: var(--color-background-hover);
border: 0;
border-right: 1px solid var(--color-border) !important;
}
&:last-child {
border: 0 !important;
}
}
&__buttontext {
padding: 0.4rem;
padding-right: 0;
}
&__form {
display: flex;
align-items: center;
}
&__input {
font: inherit;
border: none;
background: transparent;
min-width: 250px;
}
}
</style>
|