Skip to content

Commit 278162f

Browse files
authored
Merge pull request #8368 from nextcloud/fix/link_menu_custom_action
fix(menubar): Allow to inject custom link action
2 parents 966652e + 5819549 commit 278162f

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/components/Editor.provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const EDITOR_UPLOAD = Symbol('editor:upload')
1212
export const HOOK_MENTION_SEARCH = Symbol('hook:mention-search')
1313
export const HOOK_MENTION_INSERT = Symbol('hook:mention-insert')
1414
export const OPEN_LINK_HANDLER = Symbol('editor:open-link-handler')
15+
export const HOOK_MENUBAR_LINK_CUSTOM_ACTION = Symbol('menubar:link-custom-action')
1516

1617
export const useIsMobileMixin = {
1718
inject: {

src/components/Menu/ActionInsertLink.vue

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
</template>
3434
{{ t('text', 'Remove link') }}
3535
</NcActionButton>
36+
<NcActionButton v-if="hasMenubarLinkCustomAction" @click="linkCustomAction">
37+
<template #icon>
38+
<component :is="menubarLinkCustomAction.icon" />
39+
</template>
40+
{{ menubarLinkCustomAction.label }}
41+
</NcActionButton>
3642
<NcActionButton
3743
v-if="!isUsingDirectEditing"
3844
ref="buttonFile"
@@ -92,6 +98,7 @@ import { t } from '@nextcloud/l10n'
9298
import { useFileProps } from '../../composables/useFileProps.ts'
9399
import { useLinkFile } from '../../composables/useLinkFile.ts'
94100
import { useNetworkState } from '../../composables/useNetworkState.ts'
101+
import { HOOK_MENUBAR_LINK_CUSTOM_ACTION } from '../Editor.provider.ts'
95102
import { Document, LinkOff, Loading, Shape, Web } from '../icons.js'
96103
import { BaseActionEntry } from './BaseActionEntry.js'
97104
import { useMenuIDMixin } from './MenuBar.provider.js'
@@ -110,6 +117,12 @@ export default {
110117
},
111118
extends: BaseActionEntry,
112119
mixins: [useMenuIDMixin],
120+
inject: {
121+
menubarLinkCustomAction: {
122+
from: HOOK_MENUBAR_LINK_CUSTOM_ACTION,
123+
default: null,
124+
},
125+
},
113126
setup() {
114127
const base = BaseActionEntry.setup()
115128
const { networkOnline } = useNetworkState()
@@ -135,6 +148,12 @@ export default {
135148
activeClass() {
136149
return this.state.active ? 'is-active' : ''
137150
},
151+
hasMenubarLinkCustomAction() {
152+
return (
153+
typeof this.menubarLinkCustomAction?.action === 'function'
154+
&& this.menubarLinkCustomAction?.icon
155+
)
156+
},
138157
},
139158
methods: {
140159
/**
@@ -207,17 +226,33 @@ export default {
207226
linkPicker() {
208227
getLinkWithPicker(null, true)
209228
.then((link) => {
210-
const chain = this.editor?.chain()
211-
if (this.editor?.view.state?.selection.empty) {
212-
chain.focus().insertPreview(link).run()
213-
} else {
214-
chain.setLink({ href: link }).focus().run()
215-
}
229+
this.insertLink(link)
216230
})
217231
.catch((error) => {
218232
console.error('Smart picker promise rejected', error)
219233
})
220234
},
235+
linkCustomAction() {
236+
this.menubarLinkCustomAction
237+
.action()
238+
.then((link) => {
239+
this.insertLink(link)
240+
})
241+
.catch((error) => {
242+
console.error('Custom link action promise rejected', error)
243+
})
244+
},
245+
insertLink(link) {
246+
if (!link) {
247+
return
248+
}
249+
const chain = this.editor?.chain()
250+
if (this.editor?.view.state?.selection.empty) {
251+
chain.focus().insertPreview(link).run()
252+
} else {
253+
chain.setLink({ href: link }).focus().run()
254+
}
255+
},
221256
t,
222257
},
223258
}

src/editor.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
EDITOR_UPLOAD,
1111
HOOK_MENTION_INSERT,
1212
HOOK_MENTION_SEARCH,
13+
HOOK_MENUBAR_LINK_CUSTOM_ACTION,
1314
OPEN_LINK_HANDLER,
1415
} from './components/Editor.provider.ts'
1516
import { ACTION_ATTACHMENT_PROMPT } from './components/Editor/MediaHandler.provider.js'
@@ -249,6 +250,7 @@ window.OCA.Text.createEditor = async function ({
249250
component: null,
250251
props: null,
251252
},
253+
menubarLinkCustomAction = undefined,
252254

253255
onCreate = ({ markdown }) => {},
254256
onLoaded = () => {},
@@ -296,6 +298,7 @@ window.OCA.Text.createEditor = async function ({
296298
]
297299
},
298300
},
301+
[HOOK_MENUBAR_LINK_CUSTOM_ACTION]: menubarLinkCustomAction,
299302
}
300303
},
301304
data() {

0 commit comments

Comments
 (0)