Skip to content

Commit d06b47c

Browse files
committed
fix
1 parent 30668e0 commit d06b47c

File tree

9 files changed

+95
-56
lines changed

9 files changed

+95
-56
lines changed

templates/repo/issue/view_content/sidebar.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,9 @@
664664
{{if and (not (eq .Issue.PullRequest.HeadRepo.FullName .Issue.PullRequest.BaseRepo.FullName)) .CanWriteToHeadRepo}}
665665
<div class="ui divider"></div>
666666
<div class="inline field">
667-
<div class="ui checkbox" id="allow-edits-from-maintainers"
667+
<div class="ui checkbox tooltip" id="allow-edits-from-maintainers"
668668
data-url="{{.Issue.Link}}"
669-
data-prompt-tip="{{.locale.Tr "repo.pulls.allow_edits_from_maintainers_desc"}}"
669+
data-tooltip-content="{{.locale.Tr "repo.pulls.allow_edits_from_maintainers_desc"}}"
670670
data-prompt-error="{{.locale.Tr "repo.pulls.allow_edits_from_maintainers_err"}}"
671671
>
672672
<label><strong>{{.locale.Tr "repo.pulls.allow_edits_from_maintainers"}}</strong></label>

templates/repo/sub_menu.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</div>
4141
<a class="ui segment language-stats">
4242
{{range .LanguageStats}}
43-
<div class="bar tooltip" style="width: {{.Percentage}}%; background-color: {{.Color}}" data-placement="top" data-content={{.Language}}>&nbsp;</div>
43+
<div class="bar tooltip" style="width: {{.Percentage}}%; background-color: {{.Color}}" data-tooltip-placement="top" data-tooltip-content={{.Language}}>&nbsp;</div>
4444
{{end}}
4545
</a>
4646
{{end}}

web_src/js/components/DashboardRepoList.vue

-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@
147147
<script>
148148
import {createApp, nextTick} from 'vue';
149149
import $ from 'jquery';
150-
import {initTooltip} from '../modules/tippy.js';
151150
import {SvgIcon} from '../svg.js';
152151
153152
const {appSubUrl, assetUrlPrefix, pageData} = window.config;
@@ -238,9 +237,6 @@ const sfc = {
238237
mounted() {
239238
const el = document.getElementById('dashboard-repo-list');
240239
this.changeReposFilter(this.reposFilter);
241-
for (const elTooltip of el.querySelectorAll('.tooltip')) {
242-
initTooltip(elTooltip);
243-
}
244240
$(el).find('.dropdown').dropdown();
245241
nextTick(() => {
246242
this.$refs.search.focus();

web_src/js/components/DiffFileList.vue

-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
</template>
2222

2323
<script>
24-
import {initTooltip} from '../modules/tippy.js';
2524
import {doLoadMoreFiles} from '../features/repo-diff.js';
2625
2726
const {pageData} = window.config;
@@ -30,17 +29,6 @@ export default {
3029
data: () => {
3130
return pageData.diffFileInfo;
3231
},
33-
watch: {
34-
fileListIsVisible(newValue) {
35-
if (newValue === true) {
36-
this.$nextTick(() => {
37-
for (const el of this.$refs.root.querySelectorAll('.tooltip')) {
38-
initTooltip(el);
39-
}
40-
});
41-
}
42-
}
43-
},
4432
mounted() {
4533
document.getElementById('show-file-list-btn').addEventListener('click', this.toggleFileList);
4634
},

web_src/js/features/common-global.js

-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {initCompColorPicker} from './comp/ColorPicker.js';
66
import {showGlobalErrorMessage} from '../bootstrap.js';
77
import {attachCheckboxAria, attachDropdownAria} from './aria.js';
88
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js';
9-
import {initTooltip} from '../modules/tippy.js';
109
import {svg} from '../svg.js';
1110
import {hideElem, showElem, toggleElem} from '../utils/dom.js';
1211

@@ -67,12 +66,6 @@ export function initGlobalButtonClickOnEnter() {
6766
});
6867
}
6968

70-
export function initGlobalTooltips() {
71-
for (const el of document.getElementsByClassName('tooltip')) {
72-
initTooltip(el);
73-
}
74-
}
75-
7669
export function initGlobalCommon() {
7770
// Undo Safari emoji glitch fix at high enough zoom levels
7871
if (navigator.userAgent.match('Safari')) {

web_src/js/features/repo-diff.js

-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {initCompReactionSelector} from './comp/ReactionSelector.js';
33
import {initRepoIssueContentHistory} from './repo-issue-content.js';
44
import {validateTextareaNonEmpty} from './comp/EasyMDE.js';
55
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles} from './pull-view-file.js';
6-
import {initTooltip} from '../modules/tippy.js';
76

87
const {csrfToken} = window.config;
98

@@ -60,10 +59,6 @@ export function initRepoDiffConversationForm() {
6059
const $newConversationHolder = $(await $.post($form.attr('action'), formDataString));
6160
const {path, side, idx} = $newConversationHolder.data();
6261

63-
$newConversationHolder.find('.tooltip').each(function () {
64-
initTooltip(this);
65-
});
66-
6762
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
6863
if ($form.closest('tr').data('line-type') === 'same') {
6964
$(`[data-path="${path}"] a.add-code-comment[data-idx="${idx}"]`).addClass('invisible');

web_src/js/features/repo-issue.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {attachTribute} from './tribute.js';
44
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/EasyMDE.js';
55
import {initEasyMDEImagePaste} from './comp/ImagePaste.js';
66
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
7-
import {initTooltip, showTemporaryTooltip, createTippy} from '../modules/tippy.js';
7+
import {showTemporaryTooltip, createTippy} from '../modules/tippy.js';
88
import {hideElem, showElem, toggleElem} from '../utils/dom.js';
99
import {setFileFolding} from './file-fold.js';
1010

@@ -280,10 +280,7 @@ export function initRepoPullRequestAllowMaintainerEdit() {
280280
const $checkbox = $('#allow-edits-from-maintainers');
281281
if (!$checkbox.length) return;
282282

283-
const promptTip = $checkbox.attr('data-prompt-tip');
284283
const promptError = $checkbox.attr('data-prompt-error');
285-
286-
initTooltip($checkbox[0], {content: promptTip});
287284
$checkbox.checkbox({
288285
'onChange': () => {
289286
const checked = $checkbox.checkbox('is checked');

web_src/js/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import {
5656
initGlobalFormDirtyLeaveConfirm,
5757
initGlobalLinkActions,
5858
initHeadNavbarContentToggle,
59-
initGlobalTooltips,
6059
} from './features/common-global.js';
6160
import {initRepoTopicBar} from './features/repo-home.js';
6261
import {initAdminEmails} from './features/admin/emails.js';
@@ -89,6 +88,7 @@ import {initFormattingReplacements} from './features/formatting.js';
8988
import {initCopyContent} from './features/copycontent.js';
9089
import {initCaptcha} from './features/captcha.js';
9190
import {initRepositoryActionView} from './components/RepoActionView.vue';
91+
import {initGlobalTooltips} from './modules/tippy.js';
9292

9393
// Run time-critical code as soon as possible. This is safe to do because this
9494
// script appears at the end of <body> and rendered HTML is accessible at that point.

web_src/js/modules/tippy.js

+90-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import tippy from 'tippy.js';
33
export function createTippy(target, opts = {}) {
44
const instance = tippy(target, {
55
appendTo: document.body,
6-
placement: target.getAttribute('data-placement') || 'top-start',
6+
placement: 'top-start',
77
animation: false,
88
allowHTML: false,
99
hideOnClick: false,
@@ -25,38 +25,108 @@ export function createTippy(target, opts = {}) {
2525
return instance;
2626
}
2727

28-
export function initTooltip(el, props = {}) {
29-
const content = el.getAttribute('data-content') || props.content;
28+
function getTippyTooltipContent(target) {
29+
// prefer to always use the "[data-tooltip-content]" attribute
30+
// for backward compatibility, we also support the ".tooltip[data-content]" attribute
31+
let content = target.getAttribute('data-tooltip-content');
32+
if (!content && target.classList.contains('tooltip')) {
33+
content = target.getAttribute('data-content');
34+
}
35+
return content;
36+
}
37+
38+
/**
39+
* Attach a tippy tooltip to the given target element.
40+
* If the target element already has a tippy tooltip attached, the tooltip will be updated with the new content.
41+
* If the target element has no content, then no tooltip will be attached, and it returns null.
42+
* @param target {HTMLElement}
43+
* @param content {null|string}
44+
* @returns {null|tippy}
45+
*/
46+
function attachTippyTooltip(target, content = null) {
47+
content = content ?? getTippyTooltipContent(target);
3048
if (!content) return null;
31-
if (!el.hasAttribute('aria-label')) el.setAttribute('aria-label', content);
32-
return createTippy(el, {
49+
50+
const props = {
3351
content,
3452
delay: 100,
3553
role: 'tooltip',
36-
...(el.getAttribute('data-tooltip-interactive') === 'true' ? {interactive: true} : {}),
37-
...props,
38-
});
39-
}
54+
placement: target.getAttribute('data-tooltip-placement') || 'top-start',
55+
...(target.getAttribute('data-tooltip-interactive') === 'true' ? {interactive: true} : {}),
56+
};
4057

41-
export function showTemporaryTooltip(target, content) {
42-
let tippy, oldContent;
43-
if (target._tippy) {
44-
tippy = target._tippy;
45-
oldContent = tippy.props.content;
58+
if (!target._tippy) {
59+
createTippy(target, props);
4660
} else {
47-
tippy = initTooltip(target, {content});
61+
target._tippy.setProps(props);
62+
}
63+
return target._tippy;
64+
}
65+
66+
/**
67+
* creating tippy instance is expensive, so we only create it when the user hovers over the element
68+
* @param e {Event}
69+
*/
70+
function lazyTippyOnMouseEnter(e) {
71+
e.target.removeEventListener('mouseenter', lazyTippyOnMouseEnter, true);
72+
attachTippyTooltip(this);
73+
}
74+
75+
/**
76+
* Activate the tippy tooltip for all children elements
77+
* And if the element has no aria-label, use the tooltip content as aria-label
78+
* @param target {HTMLElement}
79+
*/
80+
function attachChildrenLazyTippyTooltip(target) {
81+
// the selector must match the logic in getTippyTooltipContent
82+
for (const el of target.querySelectorAll('[data-tooltip-content], .tooltip[data-content]')) {
83+
el.addEventListener('mouseenter', lazyTippyOnMouseEnter, true);
84+
85+
// meanwhile, if the element has no aria-label, use the tooltip content as aria-label
86+
if (!el.hasAttribute('aria-label')) {
87+
const content = getTippyTooltipContent(el);
88+
if (content) {
89+
el.setAttribute('aria-label', content);
90+
}
91+
}
4892
}
93+
}
4994

95+
export function initGlobalTooltips() {
96+
// use MutationObserver to detect new elements added to the DOM, or attributes changed
97+
const observer = new MutationObserver((mutationList) => {
98+
for (const mutation of mutationList) {
99+
if (mutation.type === 'childList') {
100+
for (const el of mutation.addedNodes) {
101+
// handle all "tooltip" elements in newly added nodes, skip non-related nodes (eg: "#text")
102+
if (el.querySelectorAll) {
103+
attachChildrenLazyTippyTooltip(el);
104+
}
105+
}
106+
} else if (mutation.type === 'attributes') {
107+
// sync the tooltip content if the attributes change
108+
attachTippyTooltip(mutation.target);
109+
}
110+
}
111+
});
112+
observer.observe(document, {
113+
subtree: true,
114+
childList: true,
115+
attributeFilter: ['data-tooltip-content', 'data-content'],
116+
});
117+
118+
attachChildrenLazyTippyTooltip(document.documentElement);
119+
}
120+
121+
export function showTemporaryTooltip(target, content) {
122+
const tippy = target._tippy ?? attachTippyTooltip(target, content);
50123
tippy.setContent(content);
51124
if (!tippy.state.isShown) tippy.show();
52125
tippy.setProps({
53126
onHidden: (tippy) => {
54-
if (oldContent) {
55-
tippy.setContent(oldContent);
56-
tippy.setProps({onHidden: undefined});
57-
} else {
127+
// reset the default tooltip content, if no default, then this temporary tooltip could be destroyed
128+
if (!attachTippyTooltip(target)) {
58129
tippy.destroy();
59-
// after destroy, the `_tippy` is detached, it can't do "setProps (etc...)" anymore
60130
}
61131
},
62132
});

0 commit comments

Comments
 (0)