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 Jul 21, 2022
1 parent ee283c2 commit 717e9e2
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 258 deletions.
4 changes: 0 additions & 4 deletions src/components/EditorWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@
<div ref="contentWrapper"
data-text-el="editor-content-wrapper"
class="content-wrapper text-editor__content-wrapper">
<MenuBubble v-if="renderMenus"
:content-wrapper="contentWrapper"
:file-path="relativePath" />
<EditorContent v-show="contentLoaded"
tabindex="0"
role="document"
Expand Down Expand Up @@ -142,7 +139,6 @@ export default {
EditorContent,
EditorMidiaHandler,
MenuBar,
MenuBubble: () => import(/* webpackChunkName: "editor-rich" */'./MenuBubble.vue'),
Reader: () => import(/* webpackChunkName: "editor" */'./Reader.vue'),
CollisionResolveDialog: () => import(/* webpackChunkName: "editor" */'./CollisionResolveDialog.vue'),
GuestNameDialog: () => import(/* webpackChunkName: "editor-guest" */'./GuestNameDialog.vue'),
Expand Down
201 changes: 201 additions & 0 deletions src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<!--
- @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>
<Actions ref="menu"
class="entry-action entry-action__insert-link"
aria-haspopup
:aria-label="actionEntry.label"
:class="activeClass"
:data-text-action-entry="actionEntry.key"
:title="actionEntry.label">
<template #icon>
<component :is="icon"
:title="actionEntry.label"
:aria-label="actionEntry.label"
aria-haspopup />
</template>
<ActionButton v-if="state.active"
close-after-click
:data-text-action-entry="`${actionEntry.key}-remove`"
@click="removeLink">
<template #icon>
<LinkOff />
</template>
{{ t('text', 'Remove Link') }}
</ActionButton>
<ActionButton close-after-click
:data-text-action-entry="`${actionEntry.key}-file`"
@click="linkFile">
<template #icon>
<FileLink />
</template>
{{ t('text', 'Link to File or Directory') }}
</ActionButton>
<ActionInput 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') }}
</ActionInput>
<ActionButton 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') }}
</ActionButton>
</Actions>
</template>

<script>
import Actions from '@nextcloud/vue/dist/Components/Actions'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import ActionInput from '@nextcloud/vue/dist/Components/ActionInput'

import { Loading, FileLink, LinkOff, Web } from '../icons.js'
import { BaseActionEntry } from './BaseActionEntry.js'
import { useFileMixin } from '../EditorWrapper.provider.js'
import { optimalPath } from '../../helpers/files.js'
import { getMarkAttributes, isActive } from '@tiptap/core'

export default {
name: 'ActionInsertLink',
components: {
Actions,
ActionButton,
ActionInput,
Loading,
FileLink,
LinkOff,
Web,
},
extends: BaseActionEntry,
mixins: [
useFileMixin,
],
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 @@ -220,7 +220,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
40 changes: 25 additions & 15 deletions src/components/Menu/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ import {
Emoticon,
Help,
Images,
Link,
} from '../icons.js'
import EmojiPickerAction from './EmojiPickerAction.vue'
import ActionImageUpload from './ActionImageUpload.vue'
import ActionInsertLink from './ActionInsertLink.vue'

export default [
{
Expand All @@ -58,7 +60,7 @@ export default [
keyModifiers: ['ctrl'],
icon: Undo,
action: (command) => command.undo(),
priority: 5,
priority: 6,
},
{
key: 'redo',
Expand All @@ -67,7 +69,7 @@ export default [
keyModifiers: ['ctrl'],
icon: Redo,
action: (command) => command.redo(),
priority: 11,
priority: 12,
},
{
key: 'bold',
Expand All @@ -79,7 +81,7 @@ export default [
action: (command) => {
return command.toggleBold()
},
priority: 6,
priority: 7,
},
{
key: 'italic',
Expand All @@ -91,7 +93,7 @@ export default [
action: (command) => {
return command.toggleItalic()
},
priority: 7,
priority: 8,
},
{
key: 'underline',
Expand All @@ -103,7 +105,7 @@ export default [
action: (command) => {
return command.toggleUnderline()
},
priority: 14,
priority: 15,
},
{
key: 'strikethrough',
Expand All @@ -115,7 +117,7 @@ export default [
action: (command) => {
return command.toggleStrike()
},
priority: 15,
priority: 16,
},
{
key: 'headings',
Expand Down Expand Up @@ -193,7 +195,7 @@ export default [
action: (command) => {
return command.toggleBulletList()
},
priority: 8,
priority: 9,
},
{
key: 'ordered-list',
Expand All @@ -205,15 +207,23 @@ export default [
action: (command) => {
return command.toggleOrderedList()
},
priority: 9,
priority: 10,
},
{
key: 'task-list',
label: t('text', 'To-Do list'),
isActive: 'taskList',
icon: FormatListCheckbox,
action: (command) => command.toggleTaskList(),
priority: 10,
priority: 11,
},
{
key: 'insert-link',
label: t('text', 'Insert link'),
isActive: 'link',
icon: Link,
component: ActionInsertLink,
priority: 2,
},
{
key: 'blockquote',
Expand All @@ -225,7 +235,7 @@ export default [
action: (command) => {
return command.toggleBlockquote()
},
priority: 12,
priority: 13,
},
{
key: 'callouts',
Expand Down Expand Up @@ -281,7 +291,7 @@ export default [
action: (command) => {
return command.toggleCodeBlock()
},
priority: 13,
priority: 14,
},
{
key: 'table',
Expand All @@ -291,7 +301,7 @@ export default [
action: (command) => {
return command.insertTable()
},
priority: 16,
priority: 17,
},
{
key: 'emoji-picker',
Expand All @@ -301,20 +311,20 @@ export default [
action: (command, emojiObject = {}) => {
return command.emoji(emojiObject)
},
priority: 4,
priority: 5,
},
{
key: 'insert-image',
label: t('text', 'Insert image'),
icon: Images,
component: ActionImageUpload,
priority: 2,
priority: 4,
},
{
key: 'formatting-help',
label: t('text', 'Formatting help'),
icon: Help,
click: (view) => view.$emit('call:help'),
priority: 17,
priority: 18,
},
]
Loading

0 comments on commit 717e9e2

Please sign in to comment.