Skip to content

Commit dbbcfdf

Browse files
authored
Merge pull request #22 from youngdo212/feature/ts-utils
타입스크립트 적용 - utils 및 config
2 parents 1aef1b0 + a3d6857 commit dbbcfdf

22 files changed

+262
-344
lines changed

.prettierrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"singleQuote": true,
3-
"arrowParens": "avoid"
3+
"arrowParens": "avoid",
4+
"trailingComma": "all"
45
}

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@storybook/addon-links": "^6.1.0",
3232
"@storybook/html": "^6.1.0",
3333
"@types/jest": "^26.0.15",
34+
"@types/lodash": "^4.14.167",
3435
"@typescript-eslint/eslint-plugin": "^4.11.1",
3536
"@typescript-eslint/parser": "^4.11.1",
3637
"babel-jest": "^26.6.3",

src/components/html-video/HTMLVideo.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,11 @@ export default class HTMLVideo extends Video {
4545
* 인스턴스를 생성하고 비디오 엘리먼트에 속성을 추가한다.
4646
*
4747
* @param {object} config
48-
* @param {object} config.source
49-
* @param {string} config.source.src
50-
* @param {string=} config.source.type
48+
* @param {string} config.source
5149
*/
5250
constructor(config) {
5351
super(config);
54-
this.el.src = config.source.src;
55-
this.el.type = config.source.type || '';
52+
this.el.src = config.source;
5653
this.lastVolume = this.el.volume; // 음소거를 해제했을 때 이전 볼륨으로 되돌리기 위한 속성
5754
}
5855

@@ -254,16 +251,10 @@ export default class HTMLVideo extends Video {
254251
/**
255252
* 리소스가 이 인스턴스에서 재생 가능한 포맷인지 검사한다.
256253
*
257-
* @param {object} source
258-
* @param {string} source.src
259-
* @param {string=} source.type
254+
* @param {string} source
260255
*/
261-
static canPlayType({ src, type }) {
262-
let mimeType = type;
263-
264-
if (!mimeType) {
265-
mimeType = mime.getType(src) || '';
266-
}
256+
static canPlayType(source) {
257+
let mimeType = mime.getType(source) || '';
267258

268259
return canPlayVideoType(mimeType);
269260
}

src/components/html-video/HTMLVideo.test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ beforeEach(() => {
88

99
it('비디오 엘리먼트를 생성한다', () => {
1010
const video = new HTMLVideo({
11-
source: { src: 'http://localhost/test.mp4', type: 'video/mp4' },
11+
source: 'http://localhost/test.mp4',
1212
});
1313

1414
expect(video.el.tagName).toBe('VIDEO');
1515
expect(video.el.src).toBe('http://localhost/test.mp4');
16-
expect(video.el.type).toBe('video/mp4');
1716
});
1817

1918
describe('비디오 이벤트 발생 관련', () => {
@@ -95,7 +94,7 @@ it('비디오 엘리먼트를 DOM에서 제거하고 src attribute를 초기화
9594

9695
const wrapper = document.createElement('div');
9796
const video = new HTMLVideo({
98-
source: { src: 'http://localhost/test.mp4' },
97+
source: 'http://localhost/test.mp4',
9998
});
10099

101100
wrapper.appendChild(video.render().el);

src/components/no-video/NoVideo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class NoVideo extends Video {
4040
/**
4141
* 재생 가능한 비디오 소스인지를 확인합니다.
4242
* NoVideo의 경우 어떤 비디오 소스인지 관심 없으므로 항상 true를 반환합니다.
43-
* @param {object=} source
43+
* @param {string=} source
4444
* @returns {true}
4545
*/
4646
// eslint-disable-next-line no-unused-vars

src/components/player/Player.js

-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import Events from '../../base/events';
44
import defaultConfig from '../../config/defaults';
55
import { getElementById } from '../../utils/element';
6-
import { isObject } from '../../utils/type';
76
import Core from '../core';
87

98
/**
@@ -20,7 +19,6 @@ export default class Player extends Events {
2019

2120
// 환경 설정
2221
this.config = { ...defaultConfig, ...options };
23-
this.config.source = this.normalizeSource(this.config);
2422
this.config.parentElement = this.getParentElement(this.config);
2523

2624
// 비디오 플레이어 엘리먼트 생성 및 이벤트 리스너 추가
@@ -31,22 +29,6 @@ export default class Player extends Events {
3129
this.core.render();
3230
}
3331

34-
/**
35-
* source를 규정된 객체 형식으로 변환합니다
36-
*
37-
* @param {object} config
38-
* @param {object|string|undefined} config.source
39-
* @returns {{src, type?}}
40-
*/
41-
normalizeSource({ source }) {
42-
if (isObject(source)) {
43-
source.src = source.src || '';
44-
return source;
45-
}
46-
47-
return { src: source || '' };
48-
}
49-
5032
/**
5133
* 비디오 엘리먼트가 추가될 부모 엘리먼트를 반환합니다.
5234
*

src/config/defaults.js src/config/defaults.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import type { Config } from '../types';
12
import cloneDeep from 'lodash/cloneDeep';
23

3-
const defaults = {
4-
source: { src: '' },
4+
const defaults: Config = {
5+
source: '',
56
clickToPlay: true, // 비디오를 클릭해서 재생 가능하게 하는 기능
67
keyboard: true,
78
seekTime: 5, // 앞으로 가기나 뒤로 가기 시 이동할 초 단위

src/plugins/controller/Controller.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import {
77
appendChild,
88
innerHTML,
99
removeClass,
10-
querySelector,
10+
getElementsBySelectorMap,
1111
} from '../../utils/element';
1212
import template from './template';
1313
import Events from '../../base/events';
14-
import { isNumber } from '../../utils/type';
1514
import formatTime from '../../utils/time';
1615

1716
/**
@@ -215,7 +214,7 @@ export default class Controller extends UIPlugin {
215214
const duration = this.video.getDuration();
216215
const currentTime = this.video.getCurrentTime();
217216
const value = currentTime / duration;
218-
this.childElements.seekBar.value = isNumber(value) ? value : 0;
217+
this.childElements.seekBar.value = isNaN(value) ? 0 : value;
219218
}
220219

221220
/**
@@ -305,7 +304,7 @@ export default class Controller extends UIPlugin {
305304
if (!this.video.canPlay) return;
306305

307306
innerHTML(this.el, template());
308-
this.childElements = querySelector(this.el, this.selectors); // 자식 엘리먼트를 캐싱한다.
307+
this.childElements = getElementsBySelectorMap(this.el, this.selectors); // 자식 엘리먼트를 캐싱한다.
309308
appendChild(this.core.el, this.el);
310309
return this;
311310
}

src/types.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export interface Config {
2+
source: string;
3+
clickToPlay: boolean;
4+
keyboard: boolean;
5+
seekTime: number;
6+
volumeStep: number;
7+
i18n: I18N;
8+
iconUrl: string;
9+
}
10+
11+
export interface I18N {
12+
notSupportVideoFormat: string;
13+
notFoundVideo: string;
14+
}
15+
16+
/**
17+
* 타입 T의 K 속성의 타입을 Partial 타입으로 변경한다.
18+
*/
19+
export type PartialProperty<T, K extends keyof T> = {
20+
[P in keyof T]: P extends K ? Partial<T[P]> : T[P];
21+
};

src/utils/element.js

-168
This file was deleted.

0 commit comments

Comments
 (0)