-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Refactor clean-conversation-headers
#4152
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
Merged
yakov116
merged 21 commits into
refined-github:main
from
kidonng:clean-conversation-headers
Apr 15, 2021
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
197d5d3
Drop from and into words in `clean-conversation-headers`
kidonng 8ebed2e
Lint
kidonng 7e853ea
Change dot symbol
kidonng d2b22d3
Rework
kidonng 47bd9a9
woo
kidonng 7c1d4a9
Fix quotes
kidonng 1a30a87
Extract author
kidonng 6f5ab40
Update screenshot
kidonng 9f3deca
Update screenshot again
kidonng 3b87b67
Update source/features/clean-conversation-headers.tsx
kidonng 5fd2981
Merge remote-tracking branch 'upstream/main' into clean-conversation-…
yakov116 413550c
Make it faster and use `onConversationHeaderUpdate`
yakov116 db1bb40
The author may not be ready on time
yakov116 c4d456b
use a function and split var
yakov116 779cfd6
Merge branch 'main' into clean-conversation-headers
yakov116 a909893
Lint
yakov116 889bc67
Feedback
yakov116 fa37a54
Drop `fromNodeNumber`
yakov116 20a4811
Drop bylineNodes
yakov116 f69efe3
Review
fregante 726d952
Center the icon
yakov116 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +1,86 @@ | ||
| import './clean-conversation-headers.css'; | ||
| import React from 'dom-chef'; | ||
| import select from 'select-dom'; | ||
| import {observe} from 'selector-observer'; | ||
| import elementReady from 'element-ready'; | ||
| import {ArrowLeftIcon} from '@primer/octicons-react'; | ||
| import * as pageDetect from 'github-url-detection'; | ||
|
|
||
| import features from '.'; | ||
| import getDefaultBranch from '../github-helpers/get-default-branch'; | ||
| import onConversationHeaderUpdate from '../github-events/on-conversation-header-update'; | ||
|
|
||
| const deinit: VoidFunction[] = []; | ||
| async function initIssue(): Promise<void> { | ||
| const byline = await elementReady('.gh-header-meta .flex-auto:not(.rgh-clean-conversation-header)'); | ||
| if (!byline) { | ||
| return; | ||
| } | ||
|
|
||
| function initIssue(): void { | ||
| const observer = observe('.gh-header-meta .flex-auto:not(.rgh-clean-conversation-header)', { | ||
| add(byline) { | ||
| byline.classList.add('rgh-clean-conversation-header'); | ||
| const {childNodes: bylineNodes} = byline; | ||
| // Removes: octocat opened this issue on 1 Jan [·] 1 comments | ||
| bylineNodes[4].textContent = bylineNodes[4].textContent!.replace('·', ''); | ||
| byline.classList.add('rgh-clean-conversation-header'); | ||
|
|
||
| // Removes: [octocat opened this issue on 1 Jan] · 1 comments | ||
| for (const node of [...bylineNodes].slice(0, 4)) { | ||
| node.remove(); | ||
| } | ||
| } | ||
| }); | ||
| deinit.push(observer.abort); | ||
| // Removes: [octocat opened this issue on 1 Jan] · 1 comments | ||
| for (let i = 0; i < 4; i++) { | ||
| byline.firstChild!.remove(); | ||
| } | ||
|
|
||
| // Removes: octocat opened this issue on 1 Jan [·] 1 comments | ||
| byline.firstChild!.textContent = byline.firstChild!.textContent!.replace('·', ''); | ||
| } | ||
|
|
||
| function initPR(): void { | ||
| const observer = observe('.gh-header-meta .flex-auto:not(.rgh-clean-conversation-header)', { | ||
| add(byline) { | ||
| byline.classList.add('rgh-clean-conversation-header'); | ||
| const isSameAuthor = select('.js-discussion > .TimelineItem:first-child .author')?.textContent === select('.author', byline)!.textContent; | ||
| const baseBranch = select('.commit-ref.base-ref', byline)!; | ||
| const isDefaultBranch = (baseBranch.firstElementChild as HTMLAnchorElement).pathname.split('/').length === 3; | ||
| async function initPR(): Promise<void> { | ||
| const byline = await elementReady('.gh-header-meta .flex-auto:not(.rgh-clean-conversation-header)'); | ||
| if (!byline) { | ||
| return; | ||
| } | ||
|
|
||
| byline.classList.add('rgh-clean-conversation-header'); | ||
|
|
||
| const author = select('.author', byline)!; | ||
| const isSameAuthor = pageDetect.isPRConversation() && author.textContent === (await elementReady('.TimelineItem .author'))!.textContent; | ||
|
|
||
| const [base, headBranch] = select.all('.commit-ref', byline)!; | ||
| const baseBranch = base.title.split(':')[1]; | ||
|
|
||
| // Removes: [octocat wants to merge 1] commit into github:master from octocat:feature | ||
| // Removes: [octocat] merged 1 commit into master from feature | ||
| for (const node of [...byline.childNodes].slice(isSameAuthor ? 0 : 2, pageDetect.isMergedPR() ? 2 : 4)) { | ||
| node.remove(); | ||
| } | ||
| // Replace the word "from" with an arrow | ||
| headBranch.previousSibling!.replaceWith(' ', <ArrowLeftIcon className="v-align-middle"/>, ' '); | ||
|
|
||
| baseBranch.previousSibling!.textContent = ' into '; | ||
| if (!isSameAuthor) { | ||
| byline.prepend('by '); | ||
| } | ||
| // Removes: [octocat wants to merge 1 commit into] github:master from octocat:feature | ||
| // Removes: [octocat merged 1 commit into] master from feature | ||
| const duplicateNodes = [...byline.childNodes].slice( | ||
| isSameAuthor ? 0 : 2, | ||
| pageDetect.isMergedPR() ? 3 : 5 | ||
| ); | ||
| for (const node of duplicateNodes) { | ||
| node.remove(); | ||
| } | ||
|
|
||
| if (!isDefaultBranch && !(pageDetect.isClosedPR() && baseBranch.title.endsWith(':master'))) { | ||
| baseBranch.classList.add('rgh-clean-conversation-headers-non-default-branch'); | ||
| } | ||
| } | ||
| }); | ||
| deinit.push(observer.abort); | ||
| if (!isSameAuthor) { | ||
| author.before('by '); | ||
| author.after(' • '); | ||
| } | ||
|
|
||
| const wasDefaultBranch = pageDetect.isClosedPR() && baseBranch === 'master'; | ||
| const isDefaultBranch = baseBranch === await getDefaultBranch(); | ||
| if (!isDefaultBranch && !wasDefaultBranch) { | ||
| base.classList.add('rgh-clean-conversation-headers-non-default-branch'); | ||
| } | ||
| } | ||
|
|
||
| void features.add(__filebasename, { | ||
| include: [ | ||
| pageDetect.isIssue | ||
| ], | ||
| additionalListeners: [ | ||
| onConversationHeaderUpdate | ||
| ], | ||
| awaitDomReady: false, | ||
| init: initIssue, | ||
| deinit | ||
| init: initIssue | ||
| }, { | ||
| include: [ | ||
| pageDetect.isPR | ||
| ], | ||
| additionalListeners: [ | ||
| onConversationHeaderUpdate | ||
| ], | ||
| awaitDomReady: false, | ||
| init: initPR, | ||
| deinit | ||
| init: initPR | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm it doesn't seem to change anything here in Safari
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I didn’t notice. 👍 🚀