Skip to content

Commit

Permalink
Replace MenuBubble with menu bar entry for links
Browse files Browse the repository at this point in the history
This replaces the popover MenuBubble used for inserting
links with a menu entry in the menu bar, fixing #2392.
This also allows to add links also to directories,
previously only files were allowed as link targets, fixing #2162.

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
  • Loading branch information
susnux committed Oct 5, 2022
1 parent 256998c commit fa2333a
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 275 deletions.
7 changes: 1 addition & 6 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@
</MenuBar>
<div v-if="!menubarLoaded" class="menubar-placeholder" />
<ContentContainer v-show="contentLoaded"
ref="contentWrapper">
<MenuBubble v-if="renderMenus"
:content-wrapper="contentWrapper"
:file-path="relativePath" />
</ContentContainer>
ref="contentWrapper" />
</MainContainer>
<Reader v-if="hasSyncCollission"
:content="syncError.data.outsideChange"
Expand Down Expand Up @@ -114,7 +110,6 @@ export default {
MainContainer,
ContentContainer,
MenuBar,
MenuBubble: () => import(/* webpackChunkName: "editor-rich" */'./MenuBubble.vue'),
Reader: () => import(/* webpackChunkName: "editor" */'./Reader.vue'),
Status,
CollisionResolveDialog: () => import(/* webpackChunkName: "editor" */'./CollisionResolveDialog.vue'),
Expand Down
205 changes: 205 additions & 0 deletions src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<!--
- @copyright Copyright (c) 2022
-
- @license AGPL-3.0-or-later
-
- 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>
<NcActions ref="menu"
class="entry-action entry-action__insert-link"
aria-haspopup
:aria-label="actionEntry.label"
:class="activeClass"
:container="menuIDSelector"
:data-text-action-entry="actionEntry.key"
:title="actionEntry.label">
<template #icon>
<component :is="icon"
:title="actionEntry.label"
:aria-label="actionEntry.label"
aria-haspopup />
</template>
<NcActionButton v-if="state.active"
close-after-click
:data-text-action-entry="`${actionEntry.key}-remove`"
@click="removeLink">
<template #icon>
<LinkOff />
</template>
{{ t('text', 'Remove Link') }}
</NcActionButton>
<NcActionButton close-after-click
:data-text-action-entry="`${actionEntry.key}-file`"
@click="linkFile">
<template #icon>
<FileLink />
</template>
{{ t('text', 'Link to File or Directory') }}
</NcActionButton>
<NcActionInput v-if="isInputMode"
type="text"
:value="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>
</NcActions>
</template>

<script>
import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActionInput from '@nextcloud/vue/dist/Components/NcActionInput.js'

import { getMarkAttributes, isActive } from '@tiptap/core'

import { Loading, FileLink, LinkOff, Web } from '../icons.js'
import { BaseActionEntry } from './BaseActionEntry.js'
import { optimalPath } from '../../helpers/files.js'
import { useFileMixin } from '../Editor.provider.js'
import { useMenuIDMixin } from './MenuBar.provider.js'

export default {
name: 'ActionInsertLink',
components: {
NcActions,
NcActionButton,
NcActionInput,
Loading,
FileLink,
LinkOff,
Web,
},
extends: BaseActionEntry,
mixins: [
useFileMixin,
useMenuIDMixin,
],
data: () => {
return {
href: null,
isInputMode: false,
}
},
computed: {
activeClass() {
return this.state.active ? 'is-active' : ''
},
},
methods: {
/**
* Open dialog and ask user which file to link to
* Triggered by the "link file" button
*/
linkFile() {
const startPath = this.$file.relativePath.split('/').slice(0, -1).join('/')
OC.dialogs.filepicker(t('text', 'Select file or directory to link to'), (file) => {
const client = OC.Files.getClient()
client.getFileInfo(file).then((_status, fileInfo) => {
const path = optimalPath(this.$file.relativePath, `${fileInfo.path}/${fileInfo.name}`)
const encodedPath = path.split('/').map(encodeURIComponent).join('/') + (fileInfo.type === 'dir' ? '/' : '')
const href = `${encodedPath}?fileId=${fileInfo.id}`
this.setLink(href, fileInfo.name)
})
}, false, [], true, 1, startPath, { allowDirectoryChooser: true })
},
/**
* 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
// Close menu manually as autoclose does not work form ActionInput
this.$refs.menu.closeMenu()
this.isInputMode = false
this.href = null
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()
// Check if any text is selected, if not insert the lunk using the given text property
if (this.$editor.view.state?.selection.empty) {
chain.insertContent({
type: 'paragraph',
content: [{
type: 'text',
marks: [{
type: 'link',
attrs: {
href,
},
}],
text,
}],
})
} else {
chain.setLink({ href })
}
chain.focus().run()
},
/**
* Remove link markup at current position
* Triggered by the "remove link" button
*/
removeLink() {
this.$editor.chain().unsetLink().focus().run()
},
},
}
</script>
2 changes: 1 addition & 1 deletion src/components/Menu/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export default {
--background-blur: blur(10px);
position: sticky;
top: 0;
z-index: 10021; // above modal-header and menububble so menubar is always on top
z-index: 10021; // above modal-header so menubar is always on top
background-color: var(--color-main-background-translucent);
backdrop-filter: var(--background-blur);
max-height: 44px; // important for mobile so that the buttons are always inside the container
Expand Down
48 changes: 29 additions & 19 deletions src/components/Menu/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import {
Undo,
Redo,
CodeTags,
Danger,
Emoticon,
FormatBold,
FormatItalic,
FormatUnderline,
Expand All @@ -37,17 +40,16 @@ import {
FormatListBulleted,
FormatListCheckbox,
FormatQuote,
Images,
Info,
LinkIcon,
Positive,
Warn,
Danger,
CodeTags,
Table,
Emoticon,
Images,
Warn,
} from '../icons.js'
import EmojiPickerAction from './EmojiPickerAction.vue'
import ActionAttachmentUpload from './ActionAttachmentUpload.vue'
import ActionInsertLink from './ActionInsertLink.vue'

import { MODIFIERS } from './keys.js'

Expand All @@ -59,7 +61,7 @@ export default [
keyModifiers: [MODIFIERS.Mod],
icon: Undo,
action: (command) => command.undo(),
priority: 5,
priority: 6,
},
{
key: 'redo',
Expand All @@ -68,7 +70,7 @@ export default [
keyModifiers: [MODIFIERS.Mod],
icon: Redo,
action: (command) => command.redo(),
priority: 11,
priority: 12,
},
{
key: 'bold',
Expand All @@ -80,7 +82,7 @@ export default [
action: (command) => {
return command.toggleBold()
},
priority: 6,
priority: 7,
},
{
key: 'italic',
Expand All @@ -92,7 +94,7 @@ export default [
action: (command) => {
return command.toggleItalic()
},
priority: 7,
priority: 8,
},
{
key: 'underline',
Expand All @@ -104,7 +106,7 @@ export default [
action: (command) => {
return command.toggleUnderline()
},
priority: 14,
priority: 15,
},
{
key: 'strikethrough',
Expand All @@ -116,7 +118,7 @@ export default [
action: (command) => {
return command.toggleStrike()
},
priority: 15,
priority: 16,
},
{
key: 'headings',
Expand Down Expand Up @@ -203,7 +205,7 @@ export default [
action: (command) => {
return command.toggleBulletList()
},
priority: 8,
priority: 9,
},
{
key: 'ordered-list',
Expand All @@ -215,7 +217,7 @@ export default [
action: (command) => {
return command.toggleOrderedList()
},
priority: 9,
priority: 10,
},
{
key: 'task-list',
Expand All @@ -225,7 +227,15 @@ export default [
isActive: 'taskList',
icon: FormatListCheckbox,
action: (command) => command.toggleTaskList(),
priority: 10,
priority: 11,
},
{
key: 'insert-link',
label: t('text', 'Insert link'),
isActive: 'link',
icon: LinkIcon,
component: ActionInsertLink,
priority: 2,
},
{
key: 'blockquote',
Expand All @@ -237,7 +247,7 @@ export default [
action: (command) => {
return command.toggleBlockquote()
},
priority: 12,
priority: 13,
},
{
key: 'callouts',
Expand Down Expand Up @@ -295,7 +305,7 @@ export default [
action: (command) => {
return command.toggleCodeBlock()
},
priority: 13,
priority: 14,
},
{
key: 'table',
Expand All @@ -305,7 +315,7 @@ export default [
action: (command) => {
return command.insertTable()
},
priority: 16,
priority: 17,
},
{
key: 'emoji-picker',
Expand All @@ -315,13 +325,13 @@ export default [
action: (command, emojiObject = {}) => {
return command.emoji(emojiObject)
},
priority: 4,
priority: 5,
},
{
key: 'insert-attachment',
label: t('text', 'Insert attachment'),
icon: Images,
component: ActionAttachmentUpload,
priority: 2,
priority: 4,
},
]
Loading

0 comments on commit fa2333a

Please sign in to comment.