Skip to content

Add default-to-rich-diff feature - #1932

Merged
fregante merged 16 commits into
refined-github:masterfrom
idasbiste:default-to-rich-diff
May 3, 2019
Merged

fregante merged 16 commits into
refined-github:masterfrom
idasbiste:default-to-rich-diff

Conversation

@idasbiste

Copy link
Copy Markdown
Contributor

I've used a similar approach as the one in #1924. With MutationObserver we can watch changes in the DOM for files that are loaded asynchronously and added to the page a posteriori.

This functionality can be extended to other files (markdown ones, for example): just add the intended extension to the array defined in default-to-rich-diff.tsx::7.

Example of a PR with SVG files: elementary/icons#792 (as the issue's OP said "pretty much any PR at icon repos, such as https://github.com/elementary/icons/pulls")

Closes #1599

@fregante fregante left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@sindresorhus

Copy link
Copy Markdown
Member

Can you rebase from master (we enabled TS strict mode) and fix any issues?

Eduardo Fernandes added 5 commits April 17, 2019 17:35
* This functionality can be extended to other files: just add the intended extension in default-to-rich-diff.tsx
* Use selector to find SVG files for accessing the rich-diff button
@idasbiste
idasbiste force-pushed the default-to-rich-diff branch from b708304 to 446b8d5 Compare April 17, 2019 16:39
@fregante

fregante commented Apr 18, 2019 •

Copy link
Copy Markdown
Member

I simplified the loading mechanism (I'd still like this moved to its own file), however I noticed a pretty big problem:

  1. If GitHub's JS isn't loaded, the click will cause the form to be submitted to an empty page and navigate away
  2. We can avoid this problem with preventDefault, but the Rich Preview button will remain unclickable and therefore the user can't see the preview at all

I added a setTimeout but that slows us down and doesn't necessarily prevent the problem.

@idasbiste

Copy link
Copy Markdown
Contributor Author

@bfred-it
We might be able to revert the button disable attribute after clicking:

// hacky...
button.addEventListener('click', event => setTimeout(() => {
	(event.target as Element).closest('button')!.removeAttribute('disabled')
}, 1000));

Or use MutationObserver on these elements looking for attribute changes and trigger an attribute removal if the element gets disabled.

@fregante

fregante commented Apr 19, 2019 •

Copy link
Copy Markdown
Member

That restores the button and at least the user can click it, even though I'd rather have a solution that keeps trying to load it. From my tests it looks like no further requests are triggered if we keep clicking it and re-enabling it until it's .selected

This would let us drop the 1000ms timeout.

You can use something like this where we have button.click()

button.click();
const interval = setInterval(() => {
	if (button.classList.contains('selected')) {
		clearInterval(interval);
	} else {
		button.disabled = false;
		button.click();
	}
}, 300);

@fregante fregante left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work for me!

Left before merging:

  • Description in readme and file header
  • @sindresorhus is the feature name ok?

@fregante
fregante requested a review from sindresorhus April 24, 2019 03:48
fregante added a commit that referenced this pull request Apr 24, 2019
@sindresorhus

Copy link
Copy Markdown
Member

@sindresorhus is the feature name ok?

👍

@fregante

fregante commented May 2, 2019

Copy link
Copy Markdown
Member

@idasbiste Can you commit to maintain this feature going forward? For example, in case GitHub changes something that breaks it. We already have way too many features to maintain, so we need to find a way to make this sustainable.

If so, can you add a line to the CODEOWNERS file?

Eduardo Fernandes added 2 commits May 3, 2019 08:36
Comment thread .github/CODEOWNERS Resolved
Comment thread readme.md Resolved
@fregante fregante changed the title Default to rich diff each SVG file in a pull request Add default-to-rich-diff feature May 3, 2019
@fregante
fregante merged commit 3150af6 into refined-github:master May 3, 2019
@fregante

fregante commented May 3, 2019

Copy link
Copy Markdown
Member

🌈

@idasbiste
idasbiste deleted the default-to-rich-diff branch May 3, 2019 08:21
@fregante fregante mentioned this pull request May 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Default to rich diffs for SVG files

3 participants