2024-07-07 15:32:30 +00:00
|
|
|
<script lang="ts">
|
|
|
|
import {SvgIcon} from '../svg.ts';
|
|
|
|
import {GET} from '../modules/fetch.ts';
|
2024-07-25 01:48:51 +00:00
|
|
|
import {generateAriaId} from '../modules/fomantic/base.ts';
|
2023-07-28 19:18:12 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {SvgIcon},
|
|
|
|
data: () => {
|
2024-06-10 20:49:33 +00:00
|
|
|
const el = document.querySelector('#diff-commit-select');
|
2023-07-28 19:18:12 +00:00
|
|
|
return {
|
|
|
|
menuVisible: false,
|
|
|
|
isLoading: false,
|
2024-07-25 01:48:51 +00:00
|
|
|
queryParams: el.getAttribute('data-queryparams'),
|
|
|
|
issueLink: el.getAttribute('data-issuelink'),
|
2023-08-14 02:16:40 +00:00
|
|
|
locale: {
|
|
|
|
filter_changes_by_commit: el.getAttribute('data-filter_changes_by_commit'),
|
2024-12-08 02:58:18 +00:00
|
|
|
} as Record<string, string>,
|
2023-07-28 19:18:12 +00:00
|
|
|
commits: [],
|
|
|
|
hoverActivated: false,
|
2024-03-22 14:06:53 +00:00
|
|
|
lastReviewCommitSha: null,
|
2024-07-25 01:48:51 +00:00
|
|
|
uniqueIdMenu: generateAriaId(),
|
|
|
|
uniqueIdShowAll: generateAriaId(),
|
2023-07-28 19:18:12 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
commitsSinceLastReview() {
|
|
|
|
if (this.lastReviewCommitSha) {
|
|
|
|
return this.commits.length - this.commits.findIndex((x) => x.id === this.lastReviewCommitSha) - 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
document.body.addEventListener('click', this.onBodyClick);
|
|
|
|
this.$el.addEventListener('keydown', this.onKeyDown);
|
|
|
|
this.$el.addEventListener('keyup', this.onKeyUp);
|
|
|
|
},
|
|
|
|
unmounted() {
|
|
|
|
document.body.removeEventListener('click', this.onBodyClick);
|
|
|
|
this.$el.removeEventListener('keydown', this.onKeyDown);
|
|
|
|
this.$el.removeEventListener('keyup', this.onKeyUp);
|
|
|
|
},
|
|
|
|
methods: {
|
2024-12-08 02:58:18 +00:00
|
|
|
onBodyClick(event: MouseEvent) {
|
2023-07-28 19:18:12 +00:00
|
|
|
// close this menu on click outside of this element when the dropdown is currently visible opened
|
|
|
|
if (this.$el.contains(event.target)) return;
|
|
|
|
if (this.menuVisible) {
|
|
|
|
this.toggleMenu();
|
|
|
|
}
|
|
|
|
},
|
2024-12-08 02:58:18 +00:00
|
|
|
onKeyDown(event: KeyboardEvent) {
|
2023-07-28 19:18:12 +00:00
|
|
|
if (!this.menuVisible) return;
|
2024-12-08 02:58:18 +00:00
|
|
|
const item = document.activeElement as HTMLElement;
|
2023-07-28 19:18:12 +00:00
|
|
|
if (!this.$el.contains(item)) return;
|
|
|
|
switch (event.key) {
|
|
|
|
case 'ArrowDown': // select next element
|
|
|
|
event.preventDefault();
|
|
|
|
this.focusElem(item.nextElementSibling, item);
|
|
|
|
break;
|
|
|
|
case 'ArrowUp': // select previous element
|
|
|
|
event.preventDefault();
|
|
|
|
this.focusElem(item.previousElementSibling, item);
|
|
|
|
break;
|
|
|
|
case 'Escape': // close menu
|
|
|
|
event.preventDefault();
|
|
|
|
item.tabIndex = -1;
|
|
|
|
this.toggleMenu();
|
|
|
|
break;
|
|
|
|
}
|
2024-07-25 01:48:51 +00:00
|
|
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
|
|
const item = document.activeElement; // try to highlight the selected commits
|
|
|
|
const commitIdx = item?.matches('.item') ? item.getAttribute('data-commit-idx') : null;
|
|
|
|
if (commitIdx) this.highlight(this.commits[commitIdx]);
|
|
|
|
}
|
2023-07-28 19:18:12 +00:00
|
|
|
},
|
2024-12-08 02:58:18 +00:00
|
|
|
onKeyUp(event: KeyboardEvent) {
|
2023-07-28 19:18:12 +00:00
|
|
|
if (!this.menuVisible) return;
|
|
|
|
const item = document.activeElement;
|
|
|
|
if (!this.$el.contains(item)) return;
|
|
|
|
if (event.key === 'Shift' && this.hoverActivated) {
|
|
|
|
// shift is not pressed anymore -> deactivate hovering and reset hovered and selected
|
|
|
|
this.hoverActivated = false;
|
|
|
|
for (const commit of this.commits) {
|
|
|
|
commit.hovered = false;
|
|
|
|
commit.selected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
highlight(commit) {
|
|
|
|
if (!this.hoverActivated) return;
|
|
|
|
const indexSelected = this.commits.findIndex((x) => x.selected);
|
|
|
|
const indexCurrentElem = this.commits.findIndex((x) => x.id === commit.id);
|
|
|
|
for (const [idx, commit] of this.commits.entries()) {
|
|
|
|
commit.hovered = Math.min(indexSelected, indexCurrentElem) <= idx && idx <= Math.max(indexSelected, indexCurrentElem);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/** Focus given element */
|
2024-12-08 02:58:18 +00:00
|
|
|
focusElem(elem: HTMLElement, prevElem: HTMLElement) {
|
2023-07-28 19:18:12 +00:00
|
|
|
if (elem) {
|
|
|
|
elem.tabIndex = 0;
|
2023-11-06 02:05:24 +00:00
|
|
|
if (prevElem) prevElem.tabIndex = -1;
|
2023-07-28 19:18:12 +00:00
|
|
|
elem.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/** Opens our menu, loads commits before opening */
|
|
|
|
async toggleMenu() {
|
|
|
|
this.menuVisible = !this.menuVisible;
|
|
|
|
// load our commits when the menu is not yet visible (it'll be toggled after loading)
|
|
|
|
// and we got no commits
|
2024-03-25 18:37:55 +00:00
|
|
|
if (!this.commits.length && this.menuVisible && !this.isLoading) {
|
2023-07-28 19:18:12 +00:00
|
|
|
this.isLoading = true;
|
|
|
|
try {
|
|
|
|
await this.fetchCommits();
|
|
|
|
} finally {
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set correct tabindex to allow easier navigation
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.menuVisible) {
|
2024-07-25 01:48:51 +00:00
|
|
|
this.focusElem(this.$refs.showAllChanges, this.$refs.expandBtn);
|
2023-07-28 19:18:12 +00:00
|
|
|
} else {
|
2024-07-25 01:48:51 +00:00
|
|
|
this.focusElem(this.$refs.expandBtn, this.$refs.showAllChanges);
|
2023-07-28 19:18:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/** Load the commits to show in this dropdown */
|
|
|
|
async fetchCommits() {
|
2023-09-19 00:50:30 +00:00
|
|
|
const resp = await GET(`${this.issueLink}/commits/list`);
|
2023-07-28 19:18:12 +00:00
|
|
|
const results = await resp.json();
|
|
|
|
this.commits.push(...results.commits.map((x) => {
|
|
|
|
x.hovered = false;
|
|
|
|
return x;
|
|
|
|
}));
|
|
|
|
this.commits.reverse();
|
|
|
|
this.lastReviewCommitSha = results.last_review_commit_sha || null;
|
2024-06-18 08:10:30 +00:00
|
|
|
if (this.lastReviewCommitSha && !this.commits.some((x) => x.id === this.lastReviewCommitSha)) {
|
2023-07-28 19:18:12 +00:00
|
|
|
// the lastReviewCommit is not available (probably due to a force push)
|
|
|
|
// reset the last review commit sha
|
|
|
|
this.lastReviewCommitSha = null;
|
|
|
|
}
|
|
|
|
Object.assign(this.locale, results.locale);
|
|
|
|
},
|
|
|
|
showAllChanges() {
|
2024-10-31 14:57:40 +00:00
|
|
|
window.location.assign(`${this.issueLink}/files${this.queryParams}`);
|
2023-07-28 19:18:12 +00:00
|
|
|
},
|
|
|
|
/** Called when user clicks on since last review */
|
|
|
|
changesSinceLastReviewClick() {
|
2024-10-31 14:57:40 +00:00
|
|
|
window.location.assign(`${this.issueLink}/files/${this.lastReviewCommitSha}..${this.commits.at(-1).id}${this.queryParams}`);
|
2023-07-28 19:18:12 +00:00
|
|
|
},
|
|
|
|
/** Clicking on a single commit opens this specific commit */
|
2024-12-08 02:58:18 +00:00
|
|
|
commitClicked(commitId: string, newWindow = false) {
|
2023-07-28 19:18:12 +00:00
|
|
|
const url = `${this.issueLink}/commits/${commitId}${this.queryParams}`;
|
|
|
|
if (newWindow) {
|
|
|
|
window.open(url);
|
|
|
|
} else {
|
2024-10-31 14:57:40 +00:00
|
|
|
window.location.assign(url);
|
2023-07-28 19:18:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* When a commit is clicked with shift this enables the range
|
|
|
|
* selection. Second click (with shift) defines the end of the
|
|
|
|
* range. This opens the diff of this range
|
|
|
|
* Exception: first commit is the first commit of this PR. Then
|
|
|
|
* the diff from beginning of PR up to the second clicked commit is
|
|
|
|
* opened
|
|
|
|
*/
|
|
|
|
commitClickedShift(commit) {
|
|
|
|
this.hoverActivated = !this.hoverActivated;
|
|
|
|
commit.selected = true;
|
|
|
|
// Second click -> determine our range and open links accordingly
|
|
|
|
if (!this.hoverActivated) {
|
|
|
|
// find all selected commits and generate a link
|
|
|
|
if (this.commits[0].selected) {
|
|
|
|
// first commit is selected - generate a short url with only target sha
|
|
|
|
const lastCommitIdx = this.commits.findLastIndex((x) => x.selected);
|
|
|
|
if (lastCommitIdx === this.commits.length - 1) {
|
|
|
|
// user selected all commits - just show the normal diff page
|
2024-10-31 14:57:40 +00:00
|
|
|
window.location.assign(`${this.issueLink}/files${this.queryParams}`);
|
2023-07-28 19:18:12 +00:00
|
|
|
} else {
|
2024-10-31 14:57:40 +00:00
|
|
|
window.location.assign(`${this.issueLink}/files/${this.commits[lastCommitIdx].id}${this.queryParams}`);
|
2023-07-28 19:18:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const start = this.commits[this.commits.findIndex((x) => x.selected) - 1].id;
|
|
|
|
const end = this.commits.findLast((x) => x.selected).id;
|
2024-10-31 14:57:40 +00:00
|
|
|
window.location.assign(`${this.issueLink}/files/${start}..${end}${this.queryParams}`);
|
2023-07-28 19:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-03-22 14:06:53 +00:00
|
|
|
},
|
2023-07-28 19:18:12 +00:00
|
|
|
};
|
|
|
|
</script>
|
2023-09-02 14:59:07 +00:00
|
|
|
<template>
|
2024-07-25 01:48:51 +00:00
|
|
|
<div class="ui scrolling dropdown custom diff-commit-selector">
|
2023-09-02 14:59:07 +00:00
|
|
|
<button
|
2024-07-25 01:48:51 +00:00
|
|
|
ref="expandBtn"
|
2023-09-02 14:59:07 +00:00
|
|
|
class="ui basic button"
|
|
|
|
@click.stop="toggleMenu()"
|
|
|
|
:data-tooltip-content="locale.filter_changes_by_commit"
|
|
|
|
aria-haspopup="true"
|
|
|
|
:aria-label="locale.filter_changes_by_commit"
|
2024-07-25 01:48:51 +00:00
|
|
|
:aria-controls="uniqueIdMenu"
|
|
|
|
:aria-activedescendant="uniqueIdShowAll"
|
2023-09-02 14:59:07 +00:00
|
|
|
>
|
|
|
|
<svg-icon name="octicon-git-commit"/>
|
|
|
|
</button>
|
2024-07-25 01:48:51 +00:00
|
|
|
<!-- this dropdown is not managed by Fomantic UI, so it needs some classes like "transition" explicitly -->
|
|
|
|
<div class="left menu transition" :id="uniqueIdMenu" :class="{visible: menuVisible}" v-show="menuVisible" v-cloak :aria-expanded="menuVisible ? 'true': 'false'">
|
2023-09-02 14:59:07 +00:00
|
|
|
<div class="loading-indicator is-loading" v-if="isLoading"/>
|
2024-07-25 01:48:51 +00:00
|
|
|
<div v-if="!isLoading" class="item" :id="uniqueIdShowAll" ref="showAllChanges" role="menuitem" @keydown.enter="showAllChanges()" @click="showAllChanges()">
|
2023-09-02 14:59:07 +00:00
|
|
|
<div class="gt-ellipsis">
|
|
|
|
{{ locale.show_all_commits }}
|
|
|
|
</div>
|
Migrate margin and padding helpers to tailwind (#30043)
This will conclude the refactor of 1:1 class replacements to tailwind,
except `gt-hidden`. Commands ran:
```bash
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-0#tw-$1$2-0#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-1#tw-$1$2-0.5#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-2#tw-$1$2-1#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-3#tw-$1$2-2#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-4#tw-$1$2-4#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-5#tw-$1$2-8#g' {web_src/js,templates,routers,services}/**/*
```
2024-03-24 16:42:49 +00:00
|
|
|
<div class="gt-ellipsis text light-2 tw-mb-0">
|
2023-09-02 14:59:07 +00:00
|
|
|
{{ locale.stats_num_commits }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- only show the show changes since last review if there is a review AND we are commits ahead of the last review -->
|
|
|
|
<div
|
2024-07-25 01:48:51 +00:00
|
|
|
v-if="lastReviewCommitSha != null"
|
|
|
|
class="item" role="menuitem"
|
2024-03-25 18:37:55 +00:00
|
|
|
:class="{disabled: !commitsSinceLastReview}"
|
2023-09-02 14:59:07 +00:00
|
|
|
@keydown.enter="changesSinceLastReviewClick()"
|
|
|
|
@click="changesSinceLastReviewClick()"
|
|
|
|
>
|
|
|
|
<div class="gt-ellipsis">
|
|
|
|
{{ locale.show_changes_since_your_last_review }}
|
|
|
|
</div>
|
|
|
|
<div class="gt-ellipsis text light-2">
|
|
|
|
{{ commitsSinceLastReview }} commits
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-18 14:47:05 +00:00
|
|
|
<span v-if="!isLoading" class="info text light-2">{{ locale.select_commit_hold_shift_for_range }}</span>
|
2024-07-25 01:48:51 +00:00
|
|
|
<template v-for="(commit, idx) in commits" :key="commit.id">
|
2023-09-02 14:59:07 +00:00
|
|
|
<div
|
2024-07-25 01:48:51 +00:00
|
|
|
class="item" role="menuitem"
|
|
|
|
:class="{selected: commit.selected, hovered: commit.hovered}"
|
|
|
|
:data-commit-idx="idx"
|
2023-09-02 14:59:07 +00:00
|
|
|
@keydown.enter.exact="commitClicked(commit.id)"
|
|
|
|
@keydown.enter.shift.exact="commitClickedShift(commit)"
|
|
|
|
@mouseover.shift="highlight(commit)"
|
|
|
|
@click.exact="commitClicked(commit.id)"
|
|
|
|
@click.ctrl.exact="commitClicked(commit.id, true)"
|
|
|
|
@click.meta.exact="commitClicked(commit.id, true)"
|
|
|
|
@click.shift.exact.stop.prevent="commitClickedShift(commit)"
|
|
|
|
>
|
2024-03-24 14:31:35 +00:00
|
|
|
<div class="tw-flex-1 tw-flex tw-flex-col tw-gap-1">
|
2023-09-02 14:59:07 +00:00
|
|
|
<div class="gt-ellipsis commit-list-summary">
|
|
|
|
{{ commit.summary }}
|
|
|
|
</div>
|
|
|
|
<div class="gt-ellipsis text light-2">
|
|
|
|
{{ commit.committer_or_author_name }}
|
|
|
|
<span class="text right">
|
2024-01-02 01:25:30 +00:00
|
|
|
<!-- TODO: make this respect the PreferredTimestampTense setting -->
|
2024-03-15 21:57:53 +00:00
|
|
|
<relative-time prefix="" :datetime="commit.time" data-tooltip-content data-tooltip-interactive="true">{{ commit.time }}</relative-time>
|
2023-09-02 14:59:07 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-28 08:31:07 +00:00
|
|
|
<div class="tw-font-mono">
|
2023-09-02 14:59:07 +00:00
|
|
|
{{ commit.short_sha }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
2023-07-28 19:18:12 +00:00
|
|
|
<style scoped>
|
2024-07-25 01:48:51 +00:00
|
|
|
.ui.dropdown.diff-commit-selector .menu {
|
|
|
|
margin-top: 0.25em;
|
2023-07-28 19:18:12 +00:00
|
|
|
overflow-x: hidden;
|
|
|
|
max-height: 450px;
|
|
|
|
}
|
|
|
|
|
2024-07-25 01:48:51 +00:00
|
|
|
.ui.dropdown.diff-commit-selector .menu .loading-indicator {
|
2023-07-28 19:18:12 +00:00
|
|
|
height: 200px;
|
|
|
|
width: 350px;
|
|
|
|
}
|
|
|
|
|
2024-07-25 01:48:51 +00:00
|
|
|
.ui.dropdown.diff-commit-selector .menu > .item,
|
|
|
|
.ui.dropdown.diff-commit-selector .menu > .info {
|
|
|
|
display: flex;
|
2023-07-28 19:18:12 +00:00
|
|
|
flex-direction: row;
|
|
|
|
line-height: 1.4;
|
2024-07-25 01:48:51 +00:00
|
|
|
gap: 0.25em;
|
2023-07-28 19:18:12 +00:00
|
|
|
padding: 7px 14px !important;
|
2024-07-25 01:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.ui.dropdown.diff-commit-selector .menu > .item:not(:first-child),
|
|
|
|
.ui.dropdown.diff-commit-selector .menu > .info:not(:first-child) {
|
2024-03-18 14:47:05 +00:00
|
|
|
border-top: 1px solid var(--color-secondary) !important;
|
2023-07-28 19:18:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 01:48:51 +00:00
|
|
|
.ui.dropdown.diff-commit-selector .menu > .item:focus {
|
|
|
|
background: var(--color-active);
|
|
|
|
}
|
|
|
|
|
|
|
|
.ui.dropdown.diff-commit-selector .menu > .item.hovered {
|
|
|
|
background-color: var(--color-small-accent);
|
|
|
|
}
|
|
|
|
|
|
|
|
.ui.dropdown.diff-commit-selector .menu > .item.selected {
|
|
|
|
background-color: var(--color-accent);
|
2023-07-28 19:18:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 01:48:51 +00:00
|
|
|
.ui.dropdown.diff-commit-selector .menu .commit-list-summary {
|
2023-07-28 19:18:12 +00:00
|
|
|
max-width: min(380px, 96vw);
|
|
|
|
}
|
|
|
|
</style>
|