Skip to content

Commit a68cf8d

Browse files
committed
refactor(comments): migrate to new Files Sidebar API
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 7ba5a39 commit a68cf8d

File tree

6 files changed

+107
-67
lines changed

6 files changed

+107
-67
lines changed

apps/comments/src/actions/inlineUnreadCommentsAction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
/**
1+
/*!
22
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5+
56
import CommentProcessingSvg from '@mdi/svg/svg/comment-processing.svg?raw'
6-
import { FileAction } from '@nextcloud/files'
7+
import { FileAction, getSidebar } from '@nextcloud/files'
78
import { n, t } from '@nextcloud/l10n'
89
import logger from '../logger.js'
910

@@ -34,8 +35,8 @@ export const action = new FileAction({
3435
}
3536

3637
try {
37-
window.OCA.Files.Sidebar.setActiveTab('comments')
38-
await window.OCA.Files.Sidebar.open(nodes[0].path)
38+
const sidebar = getSidebar()
39+
sidebar.open(nodes[0], 'comments')
3940
return null
4041
} catch (error) {
4142
logger.error('Error while opening sidebar', { error })

apps/comments/src/comments-tab.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

apps/comments/src/files-sidebar.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import MessageReplyText from '@mdi/svg/svg/message-reply-text.svg?raw'
7+
import { getCSPNonce } from '@nextcloud/auth'
8+
import { registerSidebarTab } from '@nextcloud/files'
9+
import { loadState } from '@nextcloud/initial-state'
10+
import { t } from '@nextcloud/l10n'
11+
import wrap from '@vue/web-component-wrapper'
12+
import { createPinia, PiniaVuePlugin } from 'pinia'
13+
import Vue from 'vue'
14+
import { registerCommentsPlugins } from './comments-activity-tab.ts'
15+
import FilesSidebarTab from './views/FilesSidebarTab.vue'
16+
17+
__webpack_nonce__ = getCSPNonce()
18+
19+
const tagName = 'comments_files-sidebar-tab'
20+
21+
if (loadState('comments', 'activityEnabled', false) && OCA?.Activity?.registerSidebarAction !== undefined) {
22+
// Do not mount own tab but mount into activity
23+
window.addEventListener('DOMContentLoaded', function() {
24+
registerCommentsPlugins()
25+
})
26+
} else {
27+
registerSidebarTab({
28+
id: 'comments',
29+
displayName: t('comments', 'Comments'),
30+
iconSvgInline: MessageReplyText,
31+
order: 50,
32+
tagName,
33+
enabled() {
34+
if (!window.customElements.get(tagName)) {
35+
setupSidebarTab()
36+
}
37+
return true
38+
},
39+
})
40+
}
41+
42+
/**
43+
* Setup the sidebar tab as a web component
44+
*/
45+
function setupSidebarTab() {
46+
Vue.use(PiniaVuePlugin)
47+
Vue.mixin({ pinia: createPinia() })
48+
const webComponent = wrap(Vue, FilesSidebarTab) as unknown as CustomElementConstructor
49+
// In Vue 2, wrap doesn't support disabling shadow. Disable with a hack
50+
Object.defineProperty(webComponent.prototype, 'attachShadow', {
51+
value() { return this },
52+
})
53+
Object.defineProperty(webComponent.prototype, 'shadowRoot', {
54+
get() { return this },
55+
})
56+
window.customElements.define(tagName, webComponent)
57+
}

apps/comments/src/mixins/CommentView.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { getCurrentUser } from '@nextcloud/auth'
2-
/**
1+
/*!
32
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
43
* SPDX-License-Identifier: AGPL-3.0-or-later
54
*/
5+
6+
import { getCurrentUser } from '@nextcloud/auth'
67
import axios from '@nextcloud/axios'
78
import { loadState } from '@nextcloud/initial-state'
89
import { generateOcsUrl } from '@nextcloud/router'
@@ -32,7 +33,7 @@ export default defineComponent({
3233
},
3334
methods: {
3435
/**
35-
* Autocomplete @mentions
36+
* Autocomplete `@mentions`
3637
*
3738
* @param search the query
3839
* @param callback the callback to process the results with
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!--
2+
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
- SPDX-License-Identifier: AGPL-3.0-or-later
4+
-->
5+
6+
<script setup lang="ts">
7+
import type { IFolder, INode, IView } from '@nextcloud/files'
8+
9+
import { computed } from 'vue'
10+
import Comments from './Comments.vue'
11+
12+
const props = defineProps<{
13+
node?: INode
14+
// eslint-disable-next-line vue/no-unused-properties -- Required on the web component interface
15+
folder?: IFolder
16+
// eslint-disable-next-line vue/no-unused-properties -- Required on the web component interface
17+
view?: IView
18+
}>()
19+
20+
defineExpose({ setActive })
21+
22+
const resourceId = computed(() => props.node?.fileid)
23+
24+
/**
25+
* Set this tab as active
26+
*
27+
* @param active - The active state
28+
*/
29+
function setActive(active: boolean) {
30+
return active
31+
}
32+
</script>
33+
34+
<template>
35+
<Comments
36+
v-if="resourceId !== undefined"
37+
:key="resourceId"
38+
:resource-id="resourceId"
39+
resource-type="files" />
40+
</template>

build/frontend-legacy/webpack.modules.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const path = require('path')
77
module.exports = {
88
comments: {
99
'comments-app': path.join(__dirname, 'apps/comments/src', 'comments-app.js'),
10-
'comments-tab': path.join(__dirname, 'apps/comments/src', 'comments-tab.js'),
10+
'comments-tab': path.join(__dirname, 'apps/comments/src', 'files-sidebar.ts'),
1111
init: path.join(__dirname, 'apps/comments/src', 'init.ts'),
1212
},
1313
core: {

0 commit comments

Comments
 (0)