From 6bc4780afbb7a71a77649a2d008199efa1f96bf5 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Thu, 23 Nov 2023 15:47:01 +0800 Subject: [PATCH] `visit-tag` - New feature (#7090) --- readme.md | 1 + source/features/visit-tag.tsx | 75 +++++++++++++++++++++++++++++++++++ source/refined-github.ts | 1 + 3 files changed, 77 insertions(+) create mode 100644 source/features/visit-tag.tsx diff --git a/readme.md b/readme.md index c0448d094f3..44d6f8db933 100644 --- a/readme.md +++ b/readme.md @@ -160,6 +160,7 @@ Thanks for contributing! 🦋🙌 - [](# "action-pr-link") 🔥 [Adds a link back to the PR that ran the workflow.](https://github-production-user-asset-6210df.s3.amazonaws.com/50487467/241645264-076a0137-36a2-4fd0-a66e-735ef3b3a563.png) - [](# "mobile-tabs") [Makes the tabs more compact on mobile so more of them can be seen.](https://user-images.githubusercontent.com/1402241/245446231-28f44b59-0151-4986-8cb9-05b5645592d8.png) - [](# "repo-header-info") [Shows whether a repo is a fork and adds the number of stars to its header.](https://github-production-user-asset-6210df.s3.amazonaws.com/1402241/267216946-404d79ab-46d7-4bc8-ba88-ae8f8029150d.png) +- [](# "visit-tag") [When navigating a repo's file on a specific tag, it adds a link to see the release/tag itself.](https://github-production-user-asset-6210df.s3.amazonaws.com/1402241/285123739-e5f4fa0a-3f48-49ef-9b87-2fd6f183c923.png) diff --git a/source/features/visit-tag.tsx b/source/features/visit-tag.tsx new file mode 100644 index 00000000000..52177cb5a19 --- /dev/null +++ b/source/features/visit-tag.tsx @@ -0,0 +1,75 @@ +import React from 'react'; +import {ArrowUpRightIcon, CodeIcon} from '@primer/octicons-react'; +import * as pageDetect from 'github-url-detection'; + +import {branchSelector} from '../github-helpers/selectors.js'; +import features from '../feature-manager.js'; +import observe from '../helpers/selector-observer.js'; +import {wrapAll} from '../helpers/dom-utils.js'; +import {buildRepoURL} from '../github-helpers/index.js'; +import {isHasSelectorSupported} from '../helpers/select-has.js'; + +async function addLink(branchSelector: HTMLButtonElement): Promise { + const tag = branchSelector.getAttribute('aria-label')?.replace(/ tag$/, ''); + if (!tag) { + features.log.error(import.meta.url, 'Tag not found in DOM. The feature needs to be updated'); + return; + } + + wrapAll( + [ + branchSelector, + + + , + ], +
, + ); +} + +function replaceIcon(tagIcon: SVGElement): void { + // https://github.com/refined-github/refined-github/issues/6499#issuecomment-1505256426 + tagIcon.replaceWith(); +} + +function clarifyIcon(signal: AbortSignal): void { + observe('.Link[href*="/tree/"] svg.octicon-tag', replaceIcon, {signal}); +} + +function init(signal: AbortSignal): void { + observe(`:is(${branchSelector}):has(.octicon-tag)`, addLink, {signal}); +} + +void features.add(import.meta.url, { + asLongAs: [ + isHasSelectorSupported, + ], + include: [ + pageDetect.isRepoTree, + pageDetect.isSingleFile, + ], + init, +}, { + include: [ + pageDetect.isReleasesOrTags, + ], + init: clarifyIcon, +}); + +/* + +Test URLs: + +- https://github.com/refined-github/refined-github/tree/23.11.15 +- https://github.com/refined-github/refined-github/blob/23.4.10/.editorconfig + +Second part: + +- https://github.com/refined-github/refined-github/releases +- https://github.com/refined-github/refined-github/releases/tag/23.11.15 + +*/ diff --git a/source/refined-github.ts b/source/refined-github.ts index e8d950b77ba..59be32cd09f 100644 --- a/source/refined-github.ts +++ b/source/refined-github.ts @@ -216,4 +216,5 @@ import './features/repo-header-info.js'; import './features/rgh-pr-template.js'; import './features/close-as-unplanned.js'; import './features/locked-issue.js'; +import './features/visit-tag.js'; import './features/prevent-comment-loss.js';