gitea源码

ContextPopup.vue 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <script lang="ts" setup>
  2. import {SvgIcon} from '../svg.ts';
  3. import {GET} from '../modules/fetch.ts';
  4. import {getIssueColor, getIssueIcon} from '../features/issue.ts';
  5. import {computed, onMounted, shallowRef, useTemplateRef} from 'vue';
  6. import type {IssuePathInfo} from '../types.ts';
  7. const {appSubUrl, i18n} = window.config;
  8. const loading = shallowRef(false);
  9. const issue = shallowRef(null);
  10. const renderedLabels = shallowRef('');
  11. const i18nErrorOccurred = i18n.error_occurred;
  12. const i18nErrorMessage = shallowRef(null);
  13. const createdAt = computed(() => new Date(issue.value.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'}));
  14. const body = computed(() => {
  15. const body = issue.value.body.replace(/\n+/g, ' ');
  16. if (body.length > 85) {
  17. return `${body.substring(0, 85)}…`;
  18. }
  19. return body;
  20. });
  21. const root = useTemplateRef('root');
  22. onMounted(() => {
  23. root.value.addEventListener('ce-load-context-popup', (e: CustomEventInit<IssuePathInfo>) => {
  24. if (!loading.value && issue.value === null) {
  25. load(e.detail);
  26. }
  27. });
  28. });
  29. async function load(issuePathInfo: IssuePathInfo) {
  30. loading.value = true;
  31. i18nErrorMessage.value = null;
  32. try {
  33. const response = await GET(`${appSubUrl}/${issuePathInfo.ownerName}/${issuePathInfo.repoName}/issues/${issuePathInfo.indexString}/info`); // backend: GetIssueInfo
  34. const respJson = await response.json();
  35. if (!response.ok) {
  36. i18nErrorMessage.value = respJson.message ?? i18n.network_error;
  37. return;
  38. }
  39. issue.value = respJson.convertedIssue;
  40. renderedLabels.value = respJson.renderedLabels;
  41. } catch {
  42. i18nErrorMessage.value = i18n.network_error;
  43. } finally {
  44. loading.value = false;
  45. }
  46. }
  47. </script>
  48. <template>
  49. <div ref="root">
  50. <div v-if="loading" class="tw-h-12 tw-w-12 is-loading"/>
  51. <div v-if="!loading && issue !== null" class="tw-flex tw-flex-col tw-gap-2">
  52. <div class="tw-text-12">{{ issue.repository.full_name }} on {{ createdAt }}</div>
  53. <div class="flex-text-block">
  54. <svg-icon :name="getIssueIcon(issue)" :class="['text', getIssueColor(issue)]"/>
  55. <span class="issue-title tw-font-semibold tw-break-anywhere">
  56. {{ issue.title }}
  57. <span class="index">#{{ issue.number }}</span>
  58. </span>
  59. </div>
  60. <div v-if="body">{{ body }}</div>
  61. <!-- eslint-disable-next-line vue/no-v-html -->
  62. <div v-if="issue.labels.length" v-html="renderedLabels"/>
  63. </div>
  64. <div class="tw-flex tw-flex-col tw-gap-2" v-if="!loading && issue === null">
  65. <div class="tw-text-12">{{ i18nErrorOccurred }}</div>
  66. <div>{{ i18nErrorMessage }}</div>
  67. </div>
  68. </div>
  69. </template>