Skip to content

Commit

Permalink
Merge pull request #5521 from nextcloud/fix/link_bubble_hash_to_same_…
Browse files Browse the repository at this point in the history
…page

fix(LinkBubble): Treat links to hashes in same page directly
  • Loading branch information
mejo- authored Apr 4, 2024
2 parents 2d2dfee + c89a0aa commit 7915402
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/helpers/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ const parseHref = function(dom) {
return ref
}

const isLinkToSelfWithHash = function(href) {
const locationNoHash = window.location.origin + window.location.pathname + window.location.search
return href?.startsWith('#') || href?.startsWith(locationNoHash + '#')
}

export {
domHref,
parseHref,
isLinkToSelfWithHash,
}
4 changes: 2 additions & 2 deletions src/nodes/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

import { Node, isNodeActive, getNodeType } from '@tiptap/core'
import { domHref, parseHref } from './../helpers/links.js'
import { domHref, parseHref, isLinkToSelfWithHash } from './../helpers/links.js'
import { VueNodeViewRenderer } from '@tiptap/vue-2'

import Preview from './Preview.vue'
Expand Down Expand Up @@ -149,7 +149,7 @@ function previewPossible({ selection }) {
return false
}
const href = extractHref($from.parent.firstChild)
if (!href || href.startsWith('#')) {
if (!href || isLinkToSelfWithHash(href)) {
return false
}
return true
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/linkHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*
*/

import { isLinkToSelfWithHash } from '../helpers/links.js'

/**
* Get the link mark applied at the current selection or next to it.
*
Expand Down Expand Up @@ -78,7 +80,7 @@ function linkMark(node) {
return undefined
}
// Don't open link bubble for anchor links
if (linkMark.attrs.href.startsWith('#')) {
if (isLinkToSelfWithHash(linkMark.attrs.href)) {
return undefined
} else {
return linkMark
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { Plugin, PluginKey } from '@tiptap/pm/state'
import LinkBubblePluginView from './LinkBubblePluginView.js'
import { activeLinkFromSelection } from './linkHelpers.js'
import { isLinkToSelfWithHash } from '../helpers/links.js'

// Commands

Expand Down Expand Up @@ -167,7 +168,7 @@ export function linkClicking() {
const linkEl = event.target.closest('a')
if (event.button === 0 && linkEl) {
event.preventDefault()
if (linkEl.attributes.href?.value?.startsWith('#')) {
if (isLinkToSelfWithHash(linkEl.attributes.href?.value)) {
// Open anchor links directly
location.href = linkEl.attributes.href.value
} else if (event.ctrlKey || event.metaKey) {
Expand Down

0 comments on commit 7915402

Please sign in to comment.