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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
<!--
- @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>
<EditorMenuBar v-slot="{ commands, isActive, focused }" :editor="editor">
<div class="menubar" :class="{ 'is-focused': focused, 'autohide': autohide }">
<div v-if="isRichEditor" ref="menubar" class="menubar-icons">
<template v-for="(icon, $index) in allIcons">
<button v-if="icon.class"
v-show="$index < iconCount"
:key="icon.label"
:title="icon.label"
:class="getIconClasses(isActive, icon)"
@click="clickIcon(commands, icon)" />
<template v-else>
<div v-show="$index < iconCount || !icon.class"
:key="icon.label"
v-click-outside="() => hideChildMenu(icon)"
class="submenu">
<button :class="childIconClass(isActive, icon.children, )"
:title="icon.label"
@click.prevent="toggleChildMenu(icon)" />
<div :class="{open: isChildMenuVisible(icon)}" class="popovermenu menu-center">
<PopoverMenu :menu="childPopoverMenu(isActive, commands, icon.children, icon)" />
</div>
</div>
</template>
</template>
<Actions>
<template v-for="(icon, $index) in allIcons">
<ActionButton v-if="icon.class && isHiddenInMenu($index)"
:key="icon.class"
:icon="icon.class"
@click="clickIcon(commands, icon)">
{{ icon.label }}
</ActionButton>
<!--<template v-else-if="!icon.class && isHiddenInMenu($index)">
<ActionButton v-for="childIcon in icon.children"
:key="childIcon.class"
:icon="childIcon.class"
@click="clickIcon(commands, childIcon)">
{{ childIcon.label }}
</ActionButton>
</template>-->
</template>
</Actions>
</div>
<slot>
Left side
</slot>
</div>
</EditorMenuBar>
</template>
<script>
import { EditorMenuBar } from 'tiptap'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
import menuBarIcons from './../mixins/menubar'
import Actions from '@nextcloud/vue/dist/Components/Actions'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import PopoverMenu from '@nextcloud/vue/dist/Components/PopoverMenu'
import ClickOutside from 'vue-click-outside'
export default {
name: 'MenuBar',
components: {
EditorMenuBar,
ActionButton,
PopoverMenu,
Actions,
},
directives: {
Tooltip,
ClickOutside,
},
props: {
editor: {
type: Object,
required: false,
default: null,
},
isRichEditor: {
type: Boolean,
default: true,
},
autohide: {
type: Boolean,
default: false,
},
isPublic: {
type: Boolean,
default: false,
},
},
data: () => {
return {
windowWidth: 0,
windowHeight: 0,
forceRecompute: 0,
submenuVisibility: {},
icons: [...menuBarIcons],
}
},
computed: {
isHiddenInMenu() {
return ($index) => $index - this.iconCount >= 0
},
getIconClasses() {
return (isActive, icon) => {
const classes = {
'is-active': icon.isActive(isActive),
}
classes[icon.class] = true
return classes
}
},
isChildMenuVisible() {
return (icon) => {
return Object.prototype.hasOwnProperty.call(this.submenuVisibility, icon.label) ? this.submenuVisibility[icon.label] : false
}
},
allIcons() {
if (this.isPublic) {
return this.icons
}
return [...this.icons, {
label: t('text', 'Insert image'),
class: 'icon-image',
isActive: () => {
},
action: (commands) => {
this.showImagePrompt(commands.image)
},
}]
},
childPopoverMenu() {
return (isActive, commands, icons, parent) => {
const popoverMenuItems = []
for (const index in icons) {
popoverMenuItems.push({
text: icons[index].label,
icon: icons[index].class,
action: () => {
icons[index].action(commands)
this.hideChildMenu(parent)
},
active: icons[index].isActive(isActive),
})
}
return popoverMenuItems
}
},
childIconClass() {
return (isActive, icons) => {
for (const index in icons) {
const icon = icons[index]
if (icon.isActive(isActive)) {
return icon.class
}
}
return 'icon-h1'
}
},
iconCount() {
this.forceRecompute // eslint-disable-line
this.windowWidth // eslint-disable-line
const menuBarWidth = this.$refs.menubar && this.$refs.menubar.clientWidth > 200 ? this.$refs.menubar.clientWidth : 200
const iconCount = Math.max((Math.floor(menuBarWidth / 44) - 2), 0)
return iconCount
},
},
mounted() {
window.addEventListener('resize', this.getWindowWidth)
this.checkInterval = setInterval(() => {
const isWidthAvailable = (this.$refs.menubar && this.$refs.menubar.clientWidth > 0)
if (this.isRichEditor && isWidthAvailable) {
this.redrawMenuBar()
}
if (!this.isRichEditor || isWidthAvailable) {
clearInterval(this.checkInterval)
}
}, 100)
},
beforeDestroy() {
window.removeEventListener('resize', this.getWindowWidth)
},
methods: {
redrawMenuBar() {
this.$nextTick(() => {
this.getWindowWidth()
this.forceRecompute++
})
},
clickIcon(commands, icon) {
return icon.action(commands)
},
getWindowWidth(event) {
this.windowWidth = document.documentElement.clientWidth
},
getWindowHeight(event) {
this.windowHeight = document.documentElement.clientHeight
},
hideChildMenu(icon) {
this.$set(this.submenuVisibility, icon.label, false)
},
toggleChildMenu(icon) {
const lastValue = Object.prototype.hasOwnProperty.call(this.submenuVisibility, icon.label) ? this.submenuVisibility[icon.label] : false
this.$set(this.submenuVisibility, icon.label, !lastValue)
},
showImagePrompt(command) {
const currentUser = OC.getCurrentUser()
if (!currentUser) {
return
}
const _command = command
OC.dialogs.filepicker('Insert an image', (file) => {
const client = OC.Files.getClient()
client.getFileInfo(file).then((_status, fileInfo) => {
const previewUrl = OC.generateUrl('/core/preview?') + `fileId=${fileInfo.id}&x=1024&y=1024&a=true`
const internalLink = OC.generateUrl('/f/' + fileInfo.id)
// dirty but works so we have the information stored in markdown
const appendMeta = {
mimetype: fileInfo.mimetype,
hasPreview: fileInfo.hasPreview,
fileId: fileInfo.id,
}
const src = (fileInfo.hasPreview ? previewUrl : internalLink)
+ '#'
+ Object.entries(appendMeta).map(([key, val]) => `${key}=${encodeURIComponent(val)}`).join('&')
_command({
src,
alt: fileInfo.name,
})
})
}, false, [], true)
},
},
}
</script>
<style scoped lang="scss">
.menubar {
position: fixed;
position: -webkit-sticky;
position: sticky;
top: 0;
display: flex;
z-index: 10010; // above modal-header so buttons are clickable
background-color: var(--color-main-background-translucent);
height: 44px;
&.autohide {
visibility: hidden;
opacity: 0;
transition: visibility 0.2s 0.4s, opacity 0.2s 0.4s;
&.is-focused {
visibility: visible;
opacity: 1;
}
}
.menubar-icons {
flex-grow: 1;
margin-left: calc((100% - 660px) / 2);
}
@media (max-width: 660px) {
.menubar-icons {
margin-left: 0;
}
}
&::v-deep .action-item__menu ul {
max-height: calc(100vh - 88px);
overflow: scroll;
}
}
.menubar button {
width: 44px;
height: 44px;
margin: 0;
background-size: 16px;
border: 0;
background-color: transparent;
opacity: .5;
color: var(--color-main-text);
background-position: center center;
vertical-align: top;
&:hover, &:focus, &:active {
background-color: var(--color-background-dark);
}
&.is-active,
&:hover,
&:focus {
opacity: 1;
}
&.icon-undo, &.icon-redo {
opacity: .4;
}
}
.menubar .submenu {
display: inline-block;
width: 44px;
height: 44px;
position: relative;
vertical-align: top;
}
</style>
|