-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
inlineSystemTagsAction.ts
91 lines (69 loc) · 2.3 KB
/
inlineSystemTagsAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { FileAction, Node, registerDavProperty, registerFileAction } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import '../css/fileEntryInlineSystemTags.scss'
const getNodeSystemTags = function(node: Node): string[] {
const tags = node.attributes?.['system-tags']?.['system-tag'] as string|string[]|undefined
if (tags === undefined) {
return []
}
return [tags].flat()
}
const renderTag = function(tag: string, isMore = false): HTMLElement {
const tagElement = document.createElement('li')
tagElement.classList.add('files-list__system-tag')
tagElement.textContent = tag
if (isMore) {
tagElement.classList.add('files-list__system-tag--more')
}
return tagElement
}
export const action = new FileAction({
id: 'system-tags',
displayName: () => '',
iconSvgInline: () => '',
enabled(nodes: Node[]) {
// Only show the action on single nodes
if (nodes.length !== 1) {
return false
}
const node = nodes[0]
const tags = getNodeSystemTags(node)
// Only show the action if the node has system tags
if (tags.length === 0) {
return false
}
return true
},
exec: async () => null,
async renderInline(node: Node) {
// Ensure we have the system tags as an array
const tags = getNodeSystemTags(node)
if (tags.length === 0) {
return null
}
const systemTagsElement = document.createElement('ul')
systemTagsElement.classList.add('files-list__system-tags')
if (tags.length === 1) {
systemTagsElement.setAttribute('aria-label', t('files', 'This file has the tag {tag}', { tag: tags[0] }))
} else {
const firstTags = tags.slice(0, -1).join(', ')
const lastTag = tags[tags.length - 1]
systemTagsElement.setAttribute('aria-label', t('files', 'This file has the tags {firstTags} and {lastTag}', { firstTags, lastTag }))
}
systemTagsElement.append(renderTag(tags[0]))
// More tags than the one we're showing
if (tags.length > 1) {
const moreTagElement = renderTag('+' + (tags.length - 1), true)
moreTagElement.setAttribute('title', tags.slice(1).join(', '))
systemTagsElement.append(moreTagElement)
}
return systemTagsElement
},
order: 0,
})
registerDavProperty('nc:system-tags')
registerFileAction(action)