Skip to content

Commit c244b0d

Browse files
committed
Add ColorSchemeType for file list
1 parent 4e6bb49 commit c244b0d

3 files changed

Lines changed: 107 additions & 3 deletions

File tree

src/__tests__/file-list-renderer-tests.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FileListRenderer } from '../file-list-renderer';
22
import HoganJsUtils from '../hoganjs-utils';
3+
import { ColorSchemeType } from '../types';
34

45
describe('FileListRenderer', () => {
56
describe('render', () => {
@@ -175,5 +176,97 @@ describe('FileListRenderer', () => {
175176
</div>"
176177
`);
177178
});
179+
180+
describe('with dark colorScheme', () => {
181+
it('should work for all kinds of files', () => {
182+
const hoganUtils = new HoganJsUtils({});
183+
const fileListRenderer = new FileListRenderer(hoganUtils, {
184+
colorScheme: ColorSchemeType.DARK,
185+
});
186+
187+
const files = [
188+
{
189+
isCombined: false,
190+
isGitDiff: false,
191+
blocks: [],
192+
addedLines: 12,
193+
deletedLines: 41,
194+
language: 'js',
195+
oldName: 'my/file/name.js',
196+
newName: 'my/file/name.js',
197+
},
198+
];
199+
const fileHtml = fileListRenderer.render(files);
200+
expect(fileHtml).toMatchInlineSnapshot(`
201+
"<div class="d2h-file-list-wrapper d2h-dark-color-scheme">
202+
<div class="d2h-file-list-header">
203+
<span class="d2h-file-list-title">Files changed (1)</span>
204+
<a class="d2h-file-switch d2h-hide">hide</a>
205+
<a class="d2h-file-switch d2h-show">show</a>
206+
</div>
207+
<ol class="d2h-file-list">
208+
<li class="d2h-file-list-line">
209+
<span class="d2h-file-name-wrapper">
210+
<svg aria-hidden="true" class="d2h-icon d2h-changed" height="16" title="modified" version="1.1"
211+
viewBox="0 0 14 16" width="14">
212+
<path d="M13 1H1C0.45 1 0 1.45 0 2v12c0 0.55 0.45 1 1 1h12c0.55 0 1-0.45 1-1V2c0-0.55-0.45-1-1-1z m0 13H1V2h12v12zM4 8c0-1.66 1.34-3 3-3s3 1.34 3 3-1.34 3-3 3-3-1.34-3-3z"></path>
213+
</svg> <a href="#d2h-781444" class="d2h-file-name">my/file/name.js</a>
214+
<span class="d2h-file-stats">
215+
<span class="d2h-lines-added">+12</span>
216+
<span class="d2h-lines-deleted">-41</span>
217+
</span>
218+
</span>
219+
</li>
220+
</ol>
221+
</div>"
222+
`);
223+
});
224+
});
225+
226+
describe('with auto colorScheme', () => {
227+
it('should work for all kinds of files', () => {
228+
const hoganUtils = new HoganJsUtils({});
229+
const fileListRenderer = new FileListRenderer(hoganUtils, {
230+
colorScheme: ColorSchemeType.AUTO,
231+
});
232+
233+
const files = [
234+
{
235+
isCombined: false,
236+
isGitDiff: false,
237+
blocks: [],
238+
addedLines: 12,
239+
deletedLines: 41,
240+
language: 'js',
241+
oldName: 'my/file/name.js',
242+
newName: 'my/file/name.js',
243+
},
244+
];
245+
const fileHtml = fileListRenderer.render(files);
246+
expect(fileHtml).toMatchInlineSnapshot(`
247+
"<div class="d2h-file-list-wrapper d2h-auto-color-scheme">
248+
<div class="d2h-file-list-header">
249+
<span class="d2h-file-list-title">Files changed (1)</span>
250+
<a class="d2h-file-switch d2h-hide">hide</a>
251+
<a class="d2h-file-switch d2h-show">show</a>
252+
</div>
253+
<ol class="d2h-file-list">
254+
<li class="d2h-file-list-line">
255+
<span class="d2h-file-name-wrapper">
256+
<svg aria-hidden="true" class="d2h-icon d2h-changed" height="16" title="modified" version="1.1"
257+
viewBox="0 0 14 16" width="14">
258+
<path d="M13 1H1C0.45 1 0 1.45 0 2v12c0 0.55 0.45 1 1 1h12c0.55 0 1-0.45 1-1V2c0-0.55-0.45-1-1-1z m0 13H1V2h12v12zM4 8c0-1.66 1.34-3 3-3s3 1.34 3 3-1.34 3-3 3-3-1.34-3-3z"></path>
259+
</svg> <a href="#d2h-781444" class="d2h-file-name">my/file/name.js</a>
260+
<span class="d2h-file-stats">
261+
<span class="d2h-lines-added">+12</span>
262+
<span class="d2h-lines-deleted">-41</span>
263+
</span>
264+
</span>
265+
</li>
266+
</ol>
267+
</div>"
268+
`);
269+
});
270+
});
178271
});
179272
});

src/file-list-renderer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import * as renderUtils from './render-utils';
22
import HoganJsUtils from './hoganjs-utils';
3-
import { DiffFile } from './types';
3+
import { ColorSchemeType, DiffFile } from './types';
44

55
const baseTemplatesPath = 'file-summary';
66
const iconsBaseTemplatesPath = 'icon';
77

8+
export interface FileListRendererConfig {
9+
colorScheme?: ColorSchemeType;
10+
}
11+
12+
export const defaultFileListRendererConfig = {
13+
colorScheme: ColorSchemeType.LIGHT,
14+
};
15+
816
export class FileListRenderer {
917
private readonly hoganUtils: HoganJsUtils;
18+
private readonly config: typeof defaultFileListRendererConfig;
1019

11-
constructor(hoganUtils: HoganJsUtils) {
20+
constructor(hoganUtils: HoganJsUtils, config: FileListRendererConfig = {}) {
1221
this.hoganUtils = hoganUtils;
22+
this.config = { ...defaultFileListRendererConfig, ...config };
1323
}
1424

1525
render(diffFiles: DiffFile[]): string {
@@ -34,6 +44,7 @@ export class FileListRenderer {
3444
.join('\n');
3545

3646
return this.hoganUtils.render(baseTemplatesPath, 'wrapper', {
47+
colorScheme: renderUtils.colorSchemeToCss(this.config.colorScheme),
3748
filesNumber: diffFiles.length,
3849
files: files,
3950
});

src/templates/file-summary-wrapper.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="d2h-file-list-wrapper">
1+
<div class="d2h-file-list-wrapper{{colorScheme}}">
22
<div class="d2h-file-list-header">
33
<span class="d2h-file-list-title">Files changed ({{filesNumber}})</span>
44
<a class="d2h-file-switch d2h-hide">hide</a>

0 commit comments

Comments
 (0)