|
| 1 | +<template> |
| 2 | + <div class="container-suggestions"> |
| 3 | + <NcButton ref="linkFileOrFolder" |
| 4 | + type="tertiary" |
| 5 | + size="normal" |
| 6 | + @click="linkFile"> |
| 7 | + <template #icon> |
| 8 | + <Document :size="20" /> |
| 9 | + </template> |
| 10 | + {{ t('text', 'Link to file or folder') }} |
| 11 | + </NcButton> |
| 12 | + |
| 13 | + <NcButton type="tertiary" |
| 14 | + size="normal" |
| 15 | + @click="$callChooseLocalAttachment"> |
| 16 | + <template #icon> |
| 17 | + <Document :size="20" /> |
| 18 | + </template> |
| 19 | + {{ t('text', 'Upload') }} |
| 20 | + </NcButton> |
| 21 | + |
| 22 | + <NcButton type="tertiary" |
| 23 | + size="normal" |
| 24 | + @click="insertTable"> |
| 25 | + <template #icon> |
| 26 | + <Table :size="20" /> |
| 27 | + </template> |
| 28 | + {{ t('text', 'Insert Table') }} |
| 29 | + </NcButton> |
| 30 | + |
| 31 | + <NcButton type="tertiary" |
| 32 | + size="normal" |
| 33 | + @click="linkPicker"> |
| 34 | + <template #icon> |
| 35 | + <Shape :size="20" /> |
| 36 | + </template> |
| 37 | + {{ t('text', 'Smart Picker') }} |
| 38 | + </NcButton> |
| 39 | + </div> |
| 40 | +</template> |
| 41 | + |
| 42 | +<script> |
| 43 | +import { NcButton } from '@nextcloud/vue' |
| 44 | +import { Document, Table, Shape } from './icons.js' |
| 45 | +import { useActionChooseLocalAttachmentMixin } from './Editor/MediaHandler.provider.js' |
| 46 | +import { getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js' |
| 47 | +import { useEditorMixin, useFileMixin } from './Editor.provider.js' |
| 48 | +import { generateUrl } from '@nextcloud/router' |
| 49 | +import { buildFilePicker } from '../helpers/filePicker.js' |
| 50 | +
|
| 51 | +export default { |
| 52 | + name: 'SuggestionsBar', |
| 53 | + components: { |
| 54 | + Table, |
| 55 | + Document, |
| 56 | + NcButton, |
| 57 | + Shape, |
| 58 | + }, |
| 59 | + mixins: [ |
| 60 | + useActionChooseLocalAttachmentMixin, |
| 61 | + useEditorMixin, |
| 62 | + useFileMixin, |
| 63 | + ], |
| 64 | +
|
| 65 | + data: () => { |
| 66 | + return { |
| 67 | + startPath: null, |
| 68 | + } |
| 69 | + }, |
| 70 | +
|
| 71 | + computed: { |
| 72 | + relativePath() { |
| 73 | + return this.$file?.relativePath ?? '/' |
| 74 | + }, |
| 75 | + }, |
| 76 | +
|
| 77 | + methods: { |
| 78 | + /** |
| 79 | + * Open smart picker dialog |
| 80 | + * Triggered by the "Smart Picker" button |
| 81 | + */ |
| 82 | + linkPicker() { |
| 83 | + getLinkWithPicker(null, true) |
| 84 | + .then(link => { |
| 85 | + const chain = this.$editor.chain() |
| 86 | + if (this.$editor.view.state?.selection.empty) { |
| 87 | + chain.focus().insertPreview(link).run() |
| 88 | + } else { |
| 89 | + chain.setLink({ href: link }).focus().run() |
| 90 | + } |
| 91 | + }) |
| 92 | + .catch(error => { |
| 93 | + console.error('Smart picker promise rejected', error) |
| 94 | + }) |
| 95 | + }, |
| 96 | +
|
| 97 | + /** |
| 98 | + * Insert table |
| 99 | + * Triggered by the "Insert table" button |
| 100 | + */ |
| 101 | + insertTable() { |
| 102 | + this.$editor.chain().focus().insertTable()?.run() |
| 103 | + }, |
| 104 | +
|
| 105 | + /** |
| 106 | + * Open dialog and ask user which file to link to |
| 107 | + * Triggered by the "link to file or folder" button |
| 108 | + */ |
| 109 | + linkFile() { |
| 110 | + if (this.startPath === null) { |
| 111 | + this.startPath = this.relativePath.split('/').slice(0, -1).join('/') |
| 112 | + } |
| 113 | +
|
| 114 | + const filePicker = buildFilePicker(this.startPath) |
| 115 | +
|
| 116 | + filePicker.pick() |
| 117 | + .then((file) => { |
| 118 | + const client = OC.Files.getClient() |
| 119 | + client.getFileInfo(file).then((_status, fileInfo) => { |
| 120 | + const url = new URL(generateUrl(`/f/${fileInfo.id}`), window.origin) |
| 121 | + this.setLink(url.href, fileInfo.name) |
| 122 | + this.startPath = fileInfo.path + (fileInfo.type === 'dir' ? `/${fileInfo.name}/` : '') |
| 123 | + }) |
| 124 | + }) |
| 125 | + .catch(() => { |
| 126 | + // do not close menu but keep focus |
| 127 | + this.$refs.linkFileOrFolder.$el.focus() |
| 128 | + }) |
| 129 | + }, |
| 130 | +
|
| 131 | + /** |
| 132 | + * Save user entered URL as a link markup |
| 133 | + * Triggered when the user submits the ActionInput |
| 134 | + * |
| 135 | + * @param {string} url href attribute of the link |
| 136 | + * @param {string} text Text part of the link |
| 137 | + */ |
| 138 | + setLink(url, text) { |
| 139 | + this.$editor.chain().setOrInsertLink(url, text).focus().run() |
| 140 | + }, |
| 141 | + }, |
| 142 | +
|
| 143 | +} |
| 144 | +
|
| 145 | +</script> |
| 146 | +
|
| 147 | +<style scoped lang="scss"> |
| 148 | +
|
| 149 | +.container-suggestions { |
| 150 | + display: flex; |
| 151 | + justify-content: center; |
| 152 | + align-items: flex-end; |
| 153 | + align-content: flex-end; |
| 154 | + position: sticky; |
| 155 | +} |
| 156 | +</style> |
0 commit comments