-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Draft
silverwind
wants to merge
14
commits into
go-gitea:main
Choose a base branch
from
silverwind:cpop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
faeb616
Rewrite context popup to stateless
silverwind 758918e
init only once, use tooltip role to enable singleton
silverwind eaea1e7
fmt
silverwind 3ec74c9
try fix test
silverwind bc8dcbc
just check for _tippy
silverwind 0ec0e43
support the case of changing link url
silverwind d553d4c
add comment
silverwind c5cde74
also try-catch the GET
silverwind e350fdc
move check for ref-external-issue into init
silverwind 5323ead
rename init to attach
silverwind e05b59b
comment
silverwind 434287a
add tracking for loading state
silverwind abf514f
rename attr
silverwind 9b3a922
turn repo name into a link
silverwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
attachRefIssueContextPopup(document.querySelectorAll('.ref-issue:not(.ref-external-issue)')); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.