Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
enh(editorAPI): Allow to pass an openLinkHandler to editor
Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Jul 23, 2025
commit bca4e51b671f95b04b79ebbb47e5e10fda7252e7
12 changes: 12 additions & 0 deletions src/components/Editor.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { openLink } from '../helpers/links.js'
import { logger } from '../helpers/logger.js'

export const FILE = Symbol('editor:file')
Expand All @@ -11,6 +12,7 @@ export const IS_MOBILE = Symbol('editor:is-mobile')
export const EDITOR_UPLOAD = Symbol('editor:upload')
export const HOOK_MENTION_SEARCH = Symbol('hook:mention-search')
export const HOOK_MENTION_INSERT = Symbol('hook:mention-insert')
export const OPEN_LINK_HANDLER = Symbol('editor:open-link-handler')

export const useIsMobileMixin = {
inject: {
Expand Down Expand Up @@ -66,3 +68,13 @@ export const useMentionHook = {
},
},
}
export const useOpenLinkHandler = {
inject: {
$openLinkHandler: {
from: OPEN_LINK_HANDLER,
default: {
openLink,
},
},
},
}
10 changes: 6 additions & 4 deletions src/components/Link/LinkBubbleView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@

<script>
import { t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcTextField from '@nextcloud/vue/components/NcTextField'
Expand All @@ -118,7 +117,7 @@ import OpenInNewIcon from 'vue-material-design-icons/OpenInNew.vue'
import PencilOutlineIcon from 'vue-material-design-icons/PencilOutline.vue'

import CopyToClipboardMixin from '../../mixins/CopyToClipboardMixin.js'
import { openLink } from '../../helpers/links.js'
import { useOpenLinkHandler } from '../Editor.provider.ts'

const PROTOCOLS_WITH_PREVIEW = ['http:', 'https:']

Expand All @@ -138,7 +137,7 @@ export default {
PencilOutlineIcon,
},

mixins: [CopyToClipboardMixin],
mixins: [CopyToClipboardMixin, useOpenLinkHandler],

props: {
editor: {
Expand Down Expand Up @@ -212,13 +211,16 @@ export default {
},

methods: {
openLink,
resetBubble() {
this.edit = false
this.newHref = null
this.referenceTitle = null
},

openLink(href) {
this.$openLinkHandler.openLink(href)
},

async copyLink() {
await this.copyToClipboard(this.href)
},
Expand Down
6 changes: 6 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {
EDITOR_UPLOAD,
HOOK_MENTION_INSERT,
HOOK_MENTION_SEARCH,
OPEN_LINK_HANDLER,
} from './components/Editor.provider.ts'
import { ACTION_ATTACHMENT_PROMPT } from './components/Editor/MediaHandler.provider.js'
import { openLink } from './helpers/links.js'
// eslint-disable-next-line import/no-unresolved, n/no-missing-import
import 'vite/modulepreload-polyfill'

Expand Down Expand Up @@ -192,6 +194,7 @@ window.OCA.Text.createEditor = async function ({
onFileInsert = undefined,
onMentionSearch = undefined,
onMentionInsert = undefined,
openLinkHandler = undefined,
onSearch = undefined,
}) {
const { default: MarkdownContentEditor } = await import(
Expand All @@ -217,6 +220,9 @@ window.OCA.Text.createEditor = async function ({
[EDITOR_UPLOAD]: !!sessionEditor,
[HOOK_MENTION_SEARCH]: sessionEditor ? true : onMentionSearch,
[HOOK_MENTION_INSERT]: sessionEditor ? true : onMentionInsert,
[OPEN_LINK_HANDLER]: {
openLink: openLinkHandler || openLink,
},
[ATTACHMENT_RESOLVER]: {
resolve(src, preferRaw) {
return [
Expand Down
12 changes: 8 additions & 4 deletions src/helpers/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ const openLink = function (href) {
const linkUrl = new URL(href, window.location.href)
// Consider rerouting links to Collectives if already inside Collectives app
const collectivesUrlBase = '/apps/collectives'
if (window.OCA.Collectives?.vueRouter
&& linkUrl.pathname.toString().startsWith(generateUrl(collectivesUrlBase))) {
const collectivesUrl = linkUrl.href.substring(linkUrl.href.indexOf(collectivesUrlBase) + collectivesUrlBase.length)
if (
window.OCA.Collectives?.vueRouter
&& linkUrl.pathname.toString().startsWith(generateUrl(collectivesUrlBase))
) {
const collectivesUrl = linkUrl.href.substring(
linkUrl.href.indexOf(collectivesUrlBase) + collectivesUrlBase.length,
)
window.OCA.Collectives.vueRouter.push(collectivesUrl)
return
}
window.open(linkUrl, '_blank')
}

export { domHref, isLinkToSelfWithHash, parseHref, openLink }
export { domHref, isLinkToSelfWithHash, openLink, parseHref }