From dad9c28d5b5a2e393fafc8aa2d8d61fd8ce5cd80 Mon Sep 17 00:00:00 2001 From: Sean Boult <996134+Hacksore@users.noreply.github.com> Date: Sun, 3 Jan 2021 11:16:01 -0600 Subject: [PATCH] Refactor single diff implmentation --- src/js/components/tree/index.jsx | 14 +------------- src/js/index.jsx | 28 +++++++++++++++++++++++++++- src/js/lib.js | 9 ++++----- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/js/components/tree/index.jsx b/src/js/components/tree/index.jsx index bac4775..6beacca 100644 --- a/src/js/components/tree/index.jsx +++ b/src/js/components/tree/index.jsx @@ -1,6 +1,6 @@ import React from 'react' import Actions from '../actions' -import { createFileTree, isElementVisible, StorageSync, getBrowserApi, isElementTargetAndVisible, hideAllDiffs } from '../../lib' +import { createFileTree, isElementVisible, StorageSync, getBrowserApi, isElementTargetAndVisible } from '../../lib' import { createTree } from '../../createTree' const MIN_RESIZE_WIDTH = 55 @@ -64,18 +64,6 @@ class Tree extends React.Component { if (!this.unmounted) { this.setState({ options }) } - - if (options.singleDiff) { - hideAllDiffs() - document.getElementsByClassName('__better_github_pr')[0].style.height = 'auto' - - // if we came from a link we should unhide the diff so perma linking works - const id = window.location.href.split('#')[1] - if (id) { - const currentDiff = document.getElementById(id) - currentDiff.style.display = 'block' - } - } } componentWillUnmount () { diff --git a/src/js/index.jsx b/src/js/index.jsx index 7d12915..df5bf0d 100644 --- a/src/js/index.jsx +++ b/src/js/index.jsx @@ -1,12 +1,38 @@ import React from 'react' import { render } from 'react-dom' import Tree from './components/tree' -import { createFileTree, createRootElement, getBrowserApi } from './lib' +import { createFileTree, createRootElement, getBrowserApi, hideAllDiffs, StorageSync } from './lib' import './style.css' const { document, MutationObserver, FontFace, parseInt = Number.parseInt } = window +// div that contains the diffs +const diffContainerElement = document.getElementsByClassName('js-diff-progressive-container')[1] + +// hide all diffs when content is added to this div +const diffObserver = new MutationObserver(async () => { + const options = await StorageSync.get() + if (!options.singleDiff) { + return + } + + hideAllDiffs() + + const id = window.location.href.split('#')[1] + + // if we have a diff ref try to unhide it + if (id) { + const currentDiff = document.getElementById(id) + + if (currentDiff) { + currentDiff.style.display = 'block' + } + } +}) + +diffObserver.observe(diffContainerElement, { childList: true }) + let observer const observe = () => { observer && observer.disconnect() diff --git a/src/js/lib.js b/src/js/lib.js index c696c27..417473c 100644 --- a/src/js/lib.js +++ b/src/js/lib.js @@ -239,10 +239,9 @@ export const isFileViewed = diffElement => { // hide all the diff elements export const hideAllDiffs = () => { - const diffNodeList = document.querySelectorAll('.file-info > a') + const diffNodeList = document.getElementsByClassName('js-file') - diffNodeList.forEach(item => { - const diffElement = item.parentElement.parentElement.parentElement - diffElement.style.display = 'none' - }) + for (let i = 0; i < diffNodeList.length; i++) { + diffNodeList[i].style.display = 'none' + } }