Skip to content

Commit 89d51d1

Browse files
authored
Hunk Separators: add hidden expand all button (#496)
* Hunk Separators: add expand all button that's hidden by default for custom css * Add hover decoration to line click
1 parent 8ed263b commit 89d51d1

4 files changed

Lines changed: 541 additions & 5 deletions

File tree

packages/diffs/src/managers/InteractionManager.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface TokenCache {
2626
interface ExpandCache {
2727
hunkIndex: number | undefined;
2828
direction: ExpansionDirections;
29+
all: boolean;
2930
}
3031

3132
export type LogTypes = 'click' | 'move' | 'both' | 'none';
@@ -77,6 +78,7 @@ interface ExpandoEventProps {
7778
type: 'line-info';
7879
hunkIndex: number;
7980
direction: ExpansionDirections;
81+
all: boolean;
8082
}
8183

8284
export type GetHoveredLineResult<TMode extends InteractionManagerMode> =
@@ -517,8 +519,8 @@ export class InteractionManager<TMode extends InteractionManagerMode> {
517519
if (isExpandoPointerTarget(target) && onHunkExpand != null) {
518520
onHunkExpand(
519521
target.hunkIndex,
520-
event.shiftKey ? 'both' : target.direction,
521-
event.shiftKey ? Number.POSITIVE_INFINITY : undefined
522+
target.all || event.shiftKey ? 'both' : target.direction,
523+
target.all || event.shiftKey ? Number.POSITIVE_INFINITY : undefined
522524
);
523525
break;
524526
}
@@ -1458,7 +1460,11 @@ export class InteractionManager<TMode extends InteractionManagerMode> {
14581460
continue;
14591461
}
14601462

1461-
if (expandInfo == null && element.hasAttribute('data-expand-button')) {
1463+
if (
1464+
expandInfo == null &&
1465+
(element.hasAttribute('data-expand-button') ||
1466+
element.hasAttribute('data-unmodified-lines'))
1467+
) {
14621468
expandInfo = {
14631469
hunkIndex: undefined,
14641470
direction: (() => {
@@ -1470,6 +1476,7 @@ export class InteractionManager<TMode extends InteractionManagerMode> {
14701476
}
14711477
return 'both';
14721478
})(),
1479+
all: element.hasAttribute('data-expand-all-button'),
14731480
};
14741481
continue;
14751482
}
@@ -1501,6 +1508,7 @@ export class InteractionManager<TMode extends InteractionManagerMode> {
15011508
type: 'line-info',
15021509
hunkIndex: expandInfo.hunkIndex,
15031510
direction: expandInfo.direction,
1511+
all: expandInfo.all,
15041512
} as ResolvedPointerTarget<TMode>;
15051513
}
15061514

packages/diffs/src/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,11 @@
753753
background-color: var(--diffs-bg-separator);
754754
}
755755

756+
[data-expand-index] [data-separator-content]:hover {
757+
text-decoration: underline;
758+
cursor: pointer;
759+
}
760+
756761
[data-expand-button] {
757762
justify-content: center;
758763
flex-shrink: 0;
@@ -765,6 +770,10 @@
765770
&:hover {
766771
color: var(--diffs-fg);
767772
}
773+
774+
&[data-expand-all-button] {
775+
display: none;
776+
}
768777
}
769778

770779
[data-expand-down] [data-icon] {

packages/diffs/src/utils/createSeparator.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function createSeparator({
4444
isFirstHunk,
4545
isLastHunk,
4646
}: CreateSeparatorProps): HASTElement {
47+
let buttonCount = 0;
4748
const children = [];
4849
if (type === 'metadata' && content != null) {
4950
children.push(
@@ -63,12 +64,15 @@ export function createSeparator({
6364
!isFirstHunk && !isLastHunk ? 'both' : isFirstHunk ? 'down' : 'up'
6465
)
6566
);
67+
buttonCount++;
6668
} else {
6769
if (!isFirstHunk) {
6870
contentChildren.push(createExpandButton('up'));
71+
buttonCount++;
6972
}
7073
if (!isLastHunk) {
7174
contentChildren.push(createExpandButton('down'));
75+
buttonCount++;
7276
}
7377
}
7478
}
@@ -85,14 +89,25 @@ export function createSeparator({
8589
properties: { 'data-separator-content': '' },
8690
})
8791
);
92+
if (chunked && expandIndex != null) {
93+
contentChildren.push(
94+
createHastElement({
95+
tagName: 'span',
96+
children: [createTextNodeElement('Expand All')],
97+
properties: {
98+
'data-expand-button': '',
99+
'data-expand-all-button': '',
100+
},
101+
})
102+
);
103+
}
88104
children.push(
89105
createHastElement({
90106
tagName: 'div',
91107
children: contentChildren,
92108
properties: {
93109
'data-separator-wrapper': '',
94-
'data-separator-multi-button':
95-
contentChildren.length > 2 ? '' : undefined,
110+
'data-separator-multi-button': buttonCount > 1 ? '' : undefined,
96111
},
97112
})
98113
);

0 commit comments

Comments
 (0)