Skip to content

Commit

Permalink
feat: Trigger ignore whitespace on PR load (#340)
Browse files Browse the repository at this point in the history
Automatically trigger ignore whitespace setting when
loading pull request in the Old PR experience.

Fixes #306
Fixes #311
  • Loading branch information
jwallet committed Apr 15, 2020
1 parent 2958224 commit a588ad3
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 332 deletions.
12 changes: 11 additions & 1 deletion src/ignore-whitespace/ignore-whitespace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import elementReady from 'element-ready'

export default function init(prRow: Element) {
export function ignoreWhitespaceSearchParam(prRow: Element) {
const link: HTMLAnchorElement = (prRow.querySelector(
'a[data-qa="pull-request-row-link"]'
): any)
Expand All @@ -10,3 +11,12 @@ export default function init(prRow: Element) {
url.search = searchParams.toString()
link.href = url.href
}

export async function ignoreWhitespaceInit() {
const toggle: HTMLElement = await elementReady(
'[data-key="ignoreWhitespace"]'
)
if (toggle && toggle.getAttribute('aria-checked') === 'false') {
toggle.click()
}
}
8 changes: 5 additions & 3 deletions src/ignore-whitespace/ignore-whitespace.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { h } from 'dom-chef'
import test from 'ava'

import ignoreWhitespace from '.'
import { ignoreWhitespaceSearchParam } from '.'

import '../../test/setup-jsdom'
import { ignoreWhitespaceInit } from './ignore-whitespace'
import { cleanDocumentBody } from '../../test/test-utils'

test('should transform pull request link to add ignore whitespace query param to 1', t => {
const actual = (
Expand All @@ -30,7 +32,7 @@ test('should transform pull request link to add ignore whitespace query param to
</div>
)

ignoreWhitespace(actual)
ignoreWhitespaceSearchParam(actual)

t.is(actual.outerHTML, expected.outerHTML)
})
Expand Down Expand Up @@ -60,7 +62,7 @@ test('should transform pull request link to toggle ignore whitespace query param
</div>
)

ignoreWhitespace(actual)
ignoreWhitespaceSearchParam(actual)

t.is(actual.outerHTML, expected.outerHTML)
})
5 changes: 4 additions & 1 deletion src/ignore-whitespace/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// @flow
export { default } from './ignore-whitespace'
export {
ignoreWhitespaceSearchParam,
ignoreWhitespaceInit,
} from './ignore-whitespace'
11 changes: 9 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import collapseDiff from './collapse-diff'
import defaultMergeStrategy from './default-merge-strategy'
import diffIgnore from './diff-ignore'
import removeDiffsPlusesAndMinuses from './diff-pluses-and-minuses'
import ignoreWhitespace from './ignore-whitespace'
import {
ignoreWhitespaceSearchParam,
ignoreWhitespaceInit,
} from './ignore-whitespace'
import insertCopyFilename from './insert-copy-filename'
import insertCopyFilenameNew from './insert-copy-filename-new'
import keymap from './keymap'
Expand Down Expand Up @@ -110,7 +113,7 @@ function pullrequestListRelatedFeatures(config) {
'tr[data-qa="pull-request-row"]',
function() {
if (config.ignoreWhitespace) {
ignoreWhitespace(this)
ignoreWhitespaceSearchParam(this)
}

if (config.augmentPrEntry) {
Expand Down Expand Up @@ -205,6 +208,10 @@ function codeReviewFeatures(config) {
setLineLengthLimit(config.lineLengthLimit, config.stickyHeader)
}

if (config.ignoreWhitespace) {
ignoreWhitespaceInit()
}

if (config.stickyHeader) {
setStickyHeader()
}
Expand Down

0 comments on commit a588ad3

Please sign in to comment.