diff options
author | Max <max@nextcloud.com> | 2022-04-11 17:11:37 +0300 |
---|---|---|
committer | Jonas <jonas@freesources.org> | 2022-07-06 13:32:57 +0300 |
commit | 52587b6b351c53168b66f1ad0f6b8763262948cb (patch) | |
tree | c5cc77a69e7afe9d3340fe62b3d51969676e70f7 /src/helpers | |
parent | e1f374ff8b88d40cdbc95ffa6e9b1f8240f59c75 (diff) |
backport of feature: emit clickLink from `ReadOnlyEditor`
The Prosemirror plugin with the `handleClick` handler
only customizes the prosemirror handling of the click event.
In read only mode we are not in a content-editable section.
So clicking a link will cause the browser to open the url with a page reload.
Allow overwriting this behavior by handling all link clicks via prosemirror.
Set `onClick` option on the `Link` mark to customize the behavior.
Emit a `click-link` event from `ReadOnlyEditor` with info about the event
and the attributes of the link mark.
Find the link that was clicked based on the clicked marks
rather than the element in the event.
This way we can get access to the attributes of the mark
without relying on the selection or even changing it.
Also add plugin key to link click handler
Signed-off-by: Max <max@nextcloud.com>
Signed-off-by: Jonas <jonas@nextcloud.com>
Diffstat (limited to 'src/helpers')
-rw-r--r-- | src/helpers/links.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/helpers/links.js b/src/helpers/links.js index 18f556b5c..81df51444 100644 --- a/src/helpers/links.js +++ b/src/helpers/links.js @@ -21,6 +21,7 @@ */ import { generateUrl } from '@nextcloud/router' +import markdownit from './../markdownit/index.js' const absolutePath = function(base, rel) { if (!rel) { @@ -77,7 +78,41 @@ const parseHref = function(dom) { return ref } +const openLink = function(event, _attrs) { + const linkElement = event.target.closest('a') + event.stopPropagation() + const htmlHref = linkElement.href + if (event.button === 0 && !event.ctrlKey && htmlHref.startsWith(window.location.origin)) { + const query = OC.parseQueryString(htmlHref) + const fragment = OC.parseQueryString(htmlHref.split('#').pop()) + if (query.dir && fragment.relPath) { + const filename = fragment.relPath.split('/').pop() + const path = `${query.dir}/${filename}` + document.title = `${filename} - ${OC.theme.title}` + if (window.location.pathname.match(/apps\/files\/$/)) { + // The files app still lacks a popState handler + // to allow for using the back button + // OC.Util.History.pushState('', htmlHref) + } + OCA.Viewer.open({ path }) + return + } + if (query.fileId) { + // open the direct file link + window.open(generateUrl(`/f/${query.fileId}`)) + return + } + } + if (!markdownit.validateLink(htmlHref)) { + console.error('Invalid link', htmlHref) + return false + } + window.open(htmlHref) + return true +} + export { domHref, parseHref, + openLink, } |