-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Make ContextPopup stateless, improve fetching logic
#31181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
faeb616
758918e
eaea1e7
3ec74c9
bc8dcbc
0ec0e43
d553d4c
c5cde74
e350fdc
5323ead
e05b59b
434287a
abf514f
9b3a922
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,54 @@ | ||
| import {createApp} from 'vue'; | ||
| import ContextPopup from '../components/ContextPopup.vue'; | ||
| import {createVueRoot} from '../utils/vue.js'; | ||
| import {parseIssueHref} from '../utils.js'; | ||
| import {createTippy} from '../modules/tippy.js'; | ||
| import {GET} from '../modules/fetch.js'; | ||
|
|
||
| export function initContextPopups() { | ||
| const refIssues = document.querySelectorAll('.ref-issue'); | ||
| attachRefIssueContextPopup(refIssues); | ||
| const {appSubUrl} = window.config; | ||
| const initAttr = 'data-contextpopup-init-done'; | ||
|
|
||
| async function init(e) { | ||
| const link = e.currentTarget; | ||
| if (link.hasAttribute(initAttr)) return; | ||
| link.setAttribute(initAttr, 'true'); | ||
|
|
||
| const {owner, repo, index} = parseIssueHref(link.getAttribute('href')); | ||
| if (!owner) return; | ||
|
|
||
| const res = await GET(`${appSubUrl}/${owner}/${repo}/issues/${index}/info`); // backend: GetIssueInfo | ||
| if (!res.ok) return; | ||
|
|
||
| let issue, labelsHtml; | ||
| try { | ||
| ({issue, labelsHtml} = await res.json()); | ||
| } catch {} | ||
| if (!issue) return; | ||
|
|
||
| const content = createVueRoot(ContextPopup, {issue, labelsHtml}); | ||
| if (!content) return; | ||
|
|
||
| const tippy = createTippy(link, { | ||
| theme: 'default', | ||
| trigger: 'mouseenter focus', | ||
| content, | ||
| placement: 'top-start', | ||
| interactive: true, | ||
| role: 'tooltip', | ||
| interactiveBorder: 15, | ||
| }); | ||
|
|
||
| // show immediately because this runs during mouseenter and focus | ||
| tippy.show(); | ||
| } | ||
|
|
||
| export function attachRefIssueContextPopup(refIssues) { | ||
| for (const refIssue of refIssues) { | ||
| if (refIssue.classList.contains('ref-external-issue')) { | ||
| return; | ||
| } | ||
|
|
||
| const {owner, repo, index} = parseIssueHref(refIssue.getAttribute('href')); | ||
| if (!owner) return; | ||
|
|
||
| const el = document.createElement('div'); | ||
| el.classList.add('tw-p-3'); | ||
| refIssue.parentNode.insertBefore(el, refIssue.nextSibling); | ||
|
|
||
| const view = createApp(ContextPopup); | ||
|
|
||
| try { | ||
| view.mount(el); | ||
| } catch (err) { | ||
| console.error(err); | ||
| el.textContent = 'ContextPopup failed to load'; | ||
| } | ||
|
|
||
| createTippy(refIssue, { | ||
| theme: 'default', | ||
| content: el, | ||
| placement: 'top-start', | ||
| interactive: true, | ||
| role: 'dialog', | ||
| interactiveBorder: 5, | ||
| onShow: () => { | ||
| el.firstChild.dispatchEvent(new CustomEvent('ce-load-context-popup', {detail: {owner, repo, index}})); | ||
| }, | ||
| }); | ||
| export function attachRefIssueContextPopup(els) { | ||
| for (const el of els) { | ||
| el.addEventListener('mouseenter', init, {once: true}); | ||
| el.addEventListener('focus', init, {once: true}); | ||
| } | ||
| } | ||
|
|
||
| export function initContextPopups() { | ||
| // TODO: Use MutationObserver to detect newly inserted .ref-issue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need any new MutationObserver now. My
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, global shared |
||
| attachRefIssueContextPopup(document.querySelectorAll('.ref-issue:not(.ref-external-issue)')); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import {createApp} from 'vue'; | ||
|
|
||
| // create a new vue root and container and mount a component into it | ||
| export function createVueRoot(component, props) { | ||
| const container = document.createElement('div'); | ||
| const view = createApp(component, props); | ||
| try { | ||
| view.mount(container); | ||
| return container; | ||
| } catch (err) { | ||
| console.error(err); | ||
| return null; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.