Skip to content

Commit

Permalink
Update code style to fit project
Browse files Browse the repository at this point in the history
  • Loading branch information
berzniz committed Oct 4, 2018
1 parent 641c9ae commit bd29d22
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
24 changes: 13 additions & 11 deletions src/js/components/tree.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React from 'react'
import Branch from './branch.jsx'
import { isElementVisible } from '../lib'

// Minimum and maximum resizable area width
const MIN_WIDTH = 55
const MAX_WIDTH = 700
const MIN_RESIZE_WIDTH = 55
const MAX_RESIZE_WIDTH = 700

const widthLocalStorageKey = '__better_github_pr_tree_width'

Expand All @@ -25,11 +24,7 @@ class Tree extends React.Component {
this.treeContainer = document.querySelector('.__better_github_pr')
this.reviewContainers = document.querySelectorAll('.enable_better_github_pr .diff-view, .enable_better_github_pr .commit.full-commit.prh-commit')

// Set initial width from value saved to localStorrage
const savedWitdh = window.localStorage.getItem(widthLocalStorageKey)
if (savedWitdh) {
this.setWidth(parseInt(savedWitdh, 10))
}
this.setInitialWidth()

this.state = {
show: true,
Expand Down Expand Up @@ -101,14 +96,21 @@ class Tree extends React.Component {
this.setWidth(0, false)
}

setInitialWidth () {
const savedWitdh = window.localStorage.getItem(widthLocalStorageKey)
if (savedWitdh) {
this.setWidth(parseInt(savedWitdh, 10))
}
}

setWidth (width, withConstraints = true) {
if (withConstraints) {
if (width <= MIN_WIDTH) width = MIN_WIDTH
if (width >= MAX_WIDTH) width = MAX_WIDTH
if (width <= MIN_RESIZE_WIDTH) width = MIN_RESIZE_WIDTH
if (width >= MAX_RESIZE_WIDTH) width = MAX_RESIZE_WIDTH
}

this.treeContainer.style.width = `${width}px`
this.reviewContainers.forEach((element, index) => {
this.reviewContainers.forEach((element) => {
element.style['margin-left'] = `${width + 10}px`
})
}
Expand Down
19 changes: 0 additions & 19 deletions src/js/folderconcat/folder-concat.jsx

This file was deleted.

4 changes: 1 addition & 3 deletions src/js/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react'
import { render } from 'react-dom'
import Tree from './components/tree'
import { createFileTree, createRootElement } from './lib'
import { folderConcat } from './folderconcat/folder-concat'
import './style.css'

const { document, MutationObserver, parseInt } = window
Expand All @@ -24,8 +23,7 @@ const renderTree = () => {
return
}

let { tree, count } = createFileTree()
tree = folderConcat(tree)
const { tree, count } = createFileTree()
render(<Tree root={tree} />, rootElement)
if (fileCount !== count) {
setTimeout(renderTree.bind(this, rootElement), 100)
Expand Down
24 changes: 23 additions & 1 deletion src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ const hasCommentsForFileIndex = (fileIndex) => {
return diffTable.querySelectorAll('.inline-comments').length
}

export const folderConcat = (node) => {
const isFileOrEmpty = (node.list === undefined || node.list.length === 0 || (node.href !== null && node.href !== undefined))
if (isFileOrEmpty) {
return node
}

const hasSingleChild = (node.list.length === 1)
if (hasSingleChild) {
const collapsed = folderConcat(node.list[0])
const isLastCollapsedIsFolder = node.nodeLabel !== '/' && (collapsed.href === null || collapsed.href === undefined)
if (isLastCollapsedIsFolder) {
node.nodeLabel = node.nodeLabel + '/' + collapsed.nodeLabel
node.hasComments = collapsed.hasComments || node.hasComments
node.list = collapsed.list
}
return node
}

node.list.map(x => folderConcat(x))
return node
}

export const createFileTree = () => {
const fileInfo = [...document.querySelectorAll('.file-info > a')]
const files = fileInfo.map(({ title, href }) => {
Expand Down Expand Up @@ -68,7 +90,7 @@ export const createFileTree = () => {
})
})
return {
tree,
tree: folderConcat(tree),
count: fileInfo.length
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/js/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
.__better_github_pr_wide .container {
min-width: 980px;
width: 96% !important;
transition: width .2s ease;
}

.__better_github_pr_wide .repository-content .discussion-timeline {
Expand Down Expand Up @@ -110,6 +109,10 @@
max-width: 708px;
}

.enable_better_github_pr .container {
transition: width .2s ease;
}

/* react-treeview */

.tree-view_item {
Expand Down

0 comments on commit bd29d22

Please sign in to comment.