You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inlineSystemTagsAction.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @copyright Copyright (c) 2023 Lucas Azevedo <lhs_azevedo@hotmail.com>
  3. *
  4. * @author Lucas Azevedo <lhs_azevedo@hotmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { FileAction, Node, registerDavProperty, registerFileAction } from "@nextcloud/files";
  23. import { translate as t } from '@nextcloud/l10n'
  24. import '../css/fileEntryInlineSystemTags.scss'
  25. const getNodeSystemTags = function (node: Node): string[] {
  26. const tags = node.attributes?.['system-tags']?.['system-tag'] as string|string[]|undefined
  27. if (tags === undefined) {
  28. return []
  29. }
  30. return [tags].flat()
  31. }
  32. const renderTag = function (tag: string, isMore: boolean = false): HTMLElement {
  33. const tagElement = document.createElement('li')
  34. tagElement.classList.add('files-list__system-tag')
  35. tagElement.textContent = tag
  36. if (isMore) {
  37. tagElement.classList.add('files-list__system-tag--more')
  38. }
  39. return tagElement;
  40. }
  41. export const action = new FileAction({
  42. id: 'system-tags',
  43. displayName: () => '',
  44. iconSvgInline: () => '',
  45. exec: async () => null,
  46. async renderInline(node: Node) {
  47. // Ensure we have the system tags as an array
  48. const tags = getNodeSystemTags(node)
  49. if (tags.length === 0) {
  50. return null
  51. }
  52. const systemTagsElement = document.createElement('ul')
  53. systemTagsElement.classList.add('files-list__system-tags')
  54. if (tags.length === 1) {
  55. systemTagsElement.setAttribute('aria-label', t('files', 'This file has the tag {tag}', { tag: tags[0] }));
  56. } else {
  57. var firstTags = tags.slice(0, -1).join(', ');
  58. var lastTag = tags[tags.length - 1];
  59. systemTagsElement.setAttribute('aria-label', t('files', 'This file has the tags {firstTags} and {lastTag}', { firstTags, lastTag }));
  60. }
  61. systemTagsElement.append(renderTag(tags[0]))
  62. // More tags than the one we're showing
  63. if (tags.length > 1) {
  64. const moreTagElement = renderTag('+' + (tags.length - 1), true)
  65. moreTagElement.setAttribute('title', tags.slice(1).join(', '));
  66. systemTagsElement.append(moreTagElement);
  67. }
  68. return systemTagsElement
  69. },
  70. order: 0
  71. })
  72. registerDavProperty('nc:system-tags')
  73. registerFileAction(action)