Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Preview): hide link bubble when preview menu opens #5445

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 3 additions & 42 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/

import TipTapLink from '@tiptap/extension-link'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { domHref, parseHref } from './../helpers/links.js'
import { linkClicking } from '../plugins/links.js'

const Link = TipTapLink.extend({

Expand Down Expand Up @@ -76,48 +76,9 @@ const Link = TipTapLink.extend({
// Custom click handler plugins
return [
...plugins,
new Plugin({
key: new PluginKey('textHandleClickLink'),
props: {
handleDOMEvents: {
// Open link in new tab on middle click
auxclick: (view, event) => {
if (event.target.closest('a') && event.button === 1 && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
event.preventDefault()
event.stopImmediatePropagation()

const linkElement = event.target.closest('a')
window.open(linkElement.href, '_blank')
}
},
// Prevent paste into links
// On Linux, middle click pastes, which breaks "open in new tab" on middle click
// Pasting into links will break the link anyway, so just disable it altogether.
paste: (view, event) => {
if (event.target.closest('a')) {
event.stopPropagation()
event.preventDefault()
event.stopImmediatePropagation()
}
},
// Prevent open link (except anchor links) on left click (required for read-only mode)
// Open link in new tab on Ctrl/Cmd + left click
click: (view, event) => {
const linkEl = event.target.closest('a')
if (event.button === 0 && linkEl) {
event.preventDefault()
if (linkEl.attributes.href?.value?.startsWith('#')) {
// Open anchor links directly
location.href = linkEl.attributes.href.value
} else if (event.ctrlKey || event.metaKey) {
window.open(linkEl.href, '_blank')
}
}
},
},
},
}),
linkClicking(),
]

},
})

Expand Down
67 changes: 67 additions & 0 deletions src/plugins/links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @copyright Copyright (c) 2024 Max <[email protected]>
*
* @author Max <[email protected]>
*
* @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/>.
*
*/

import { Plugin, PluginKey } from '@tiptap/pm/state'

export function linkClicking() {
return new Plugin({
key: new PluginKey('textHandleClickLink'),
props: {
handleDOMEvents: {
// Open link in new tab on middle click
auxclick: (view, event) => {
if (event.target.closest('a') && event.button === 1 && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
event.preventDefault()
event.stopImmediatePropagation()

const linkElement = event.target.closest('a')
window.open(linkElement.href, '_blank')
}
},
// Prevent paste into links
// On Linux, middle click pastes, which breaks "open in new tab" on middle click
// Pasting into links will break the link anyway, so just disable it altogether.
paste: (view, event) => {
if (event.target.closest('a')) {
event.stopPropagation()
event.preventDefault()
event.stopImmediatePropagation()
}
},
// Prevent open link (except anchor links) on left click (required for read-only mode)
// Open link in new tab on Ctrl/Cmd + left click
click: (view, event) => {
const linkEl = event.target.closest('a')
if (event.button === 0 && linkEl) {
event.preventDefault()
if (linkEl.attributes.href?.value?.startsWith('#')) {
// Open anchor links directly
location.href = linkEl.attributes.href.value
} else if (event.ctrlKey || event.metaKey) {
window.open(linkEl.href, '_blank')
}
}
},
},
},
})
}
Loading