|
1 | | -import {createApp} from 'vue'; |
2 | 1 | import ContextPopup from '../components/ContextPopup.vue'; |
| 2 | +import {createVueRoot} from '../utils/vue.js'; |
3 | 3 | import {parseIssueHref} from '../utils.js'; |
4 | 4 | import {createTippy} from '../modules/tippy.js'; |
| 5 | +import {GET} from '../modules/fetch.js'; |
5 | 6 |
|
6 | | -export function initContextPopups() { |
7 | | - const refIssues = document.querySelectorAll('.ref-issue'); |
8 | | - attachRefIssueContextPopup(refIssues); |
| 7 | +const {appSubUrl} = window.config; |
| 8 | + |
| 9 | +async function show(e) { |
| 10 | + const link = e.currentTarget; |
| 11 | + const {owner, repo, index} = parseIssueHref(link.getAttribute('href')); |
| 12 | + if (!owner) return; |
| 13 | + |
| 14 | + const res = await GET(`${appSubUrl}/${owner}/${repo}/issues/${index}/info`); // backend: GetIssueInfo |
| 15 | + if (!res.ok) return; |
| 16 | + |
| 17 | + let issue, labelsHtml; |
| 18 | + try { |
| 19 | + ({issue, labelsHtml} = await res.json()); |
| 20 | + } catch {} |
| 21 | + if (!issue) return; |
| 22 | + |
| 23 | + const content = createVueRoot(ContextPopup, {issue, labelsHtml}); |
| 24 | + if (!content) return; |
| 25 | + |
| 26 | + const tippy = createTippy(link, { |
| 27 | + theme: 'default', |
| 28 | + trigger: 'mouseenter focus', |
| 29 | + content, |
| 30 | + placement: 'top-start', |
| 31 | + interactive: true, |
| 32 | + role: 'dialog', |
| 33 | + interactiveBorder: 15, |
| 34 | + }); |
| 35 | + |
| 36 | + // show immediately because this runs during mouseenter and focus |
| 37 | + tippy.show(); |
9 | 38 | } |
10 | 39 |
|
11 | | -export function attachRefIssueContextPopup(refIssues) { |
12 | | - for (const refIssue of refIssues) { |
13 | | - if (refIssue.classList.contains('ref-external-issue')) { |
14 | | - return; |
15 | | - } |
16 | | - |
17 | | - const {owner, repo, index} = parseIssueHref(refIssue.getAttribute('href')); |
18 | | - if (!owner) return; |
19 | | - |
20 | | - const el = document.createElement('div'); |
21 | | - el.classList.add('tw-p-3'); |
22 | | - refIssue.parentNode.insertBefore(el, refIssue.nextSibling); |
23 | | - |
24 | | - const view = createApp(ContextPopup); |
25 | | - |
26 | | - try { |
27 | | - view.mount(el); |
28 | | - } catch (err) { |
29 | | - console.error(err); |
30 | | - el.textContent = 'ContextPopup failed to load'; |
31 | | - } |
32 | | - |
33 | | - createTippy(refIssue, { |
34 | | - theme: 'default', |
35 | | - content: el, |
36 | | - placement: 'top-start', |
37 | | - interactive: true, |
38 | | - role: 'dialog', |
39 | | - interactiveBorder: 5, |
40 | | - onShow: () => { |
41 | | - el.firstChild.dispatchEvent(new CustomEvent('ce-load-context-popup', {detail: {owner, repo, index}})); |
42 | | - }, |
43 | | - }); |
| 40 | +export function attachRefIssueContextPopup(els) { |
| 41 | + for (const link of els) { |
| 42 | + link.addEventListener('mouseenter', show); |
| 43 | + link.addEventListener('focus', show); |
44 | 44 | } |
45 | 45 | } |
| 46 | + |
| 47 | +export function initContextPopups() { |
| 48 | + // TODO: Use MutationObserver to detect newly inserted .ref-issue |
| 49 | + attachRefIssueContextPopup(document.querySelectorAll('.ref-issue:not(.ref-external-issue)')); |
| 50 | +} |
0 commit comments