Skip to content

Commit

Permalink
refactor(links): move prosemirror plugin into own file
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Mar 15, 2024
1 parent c8b1e9e commit 2478693
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 42 deletions.
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')
}
}
},
},
},
})
}

0 comments on commit 2478693

Please sign in to comment.