Skip to content

Commit d560934

Browse files
SaraVieiraCompuIves
authored andcommitted
add vscode icons (codesandbox#1154)
* add vscode icons * change function name * use styled componnents instead of inline * change to material * fix git * update snaps+ * really update now * Revert "fix git" This reverts commit 776809e. * Revert "really update now" This reverts commit ee3ef8d. * Revert "update snaps+" This reverts commit 8862e89. * small fixes * clean code
1 parent 8e4cd41 commit d560934

File tree

10 files changed

+138
-121
lines changed

10 files changed

+138
-121
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import folderSvg from 'common/components/icons/folder.svg';
2+
import folderOpenSvg from 'common/components/icons/folder-open.svg';
3+
import faviconSvg from 'common/components/icons/favicon.svg';
4+
import fileSvg from 'common/components/icons/file.svg';
5+
import imageSvg from 'common/components/icons/image.svg';
6+
import codesandboxSvg from 'common/components/icons/codesandbox.svg';
7+
8+
function imageExists(url) {
9+
return new Promise((resolve, reject) => {
10+
const img = new Image();
11+
img.onload = resolve;
12+
img.onerror = reject;
13+
img.src = url;
14+
});
15+
}
16+
17+
const icons = {
18+
directory: folderSvg,
19+
'directory-open': folderOpenSvg,
20+
favicon: faviconSvg,
21+
image: imageSvg,
22+
codesandbox: codesandboxSvg,
23+
};
24+
25+
async function getIconURL(type) {
26+
const base =
27+
'https://cdn.rawgit.com/PKief/vscode-material-icon-theme/e04ab459/icons';
28+
29+
let url;
30+
31+
if (type === 'codesandbox') {
32+
url = codesandboxSvg;
33+
} else {
34+
url = `${base}/${type}.svg`;
35+
}
36+
37+
try {
38+
await imageExists(url);
39+
40+
return url;
41+
} catch (_) {
42+
return icons[type] || fileSvg;
43+
}
44+
}
45+
46+
export default getIconURL;

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/elements.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,13 @@
11
import styled from 'styled-components';
22

3-
import jsSvg from 'common/components/icons/js.svg';
4-
import tsSvg from 'common/components/icons/ts.svg';
5-
import cssSvg from 'common/components/icons/css.svg';
6-
import reactSvg from 'common/components/icons/react.svg';
7-
import folderSvg from 'common/components/icons/folder.svg';
8-
import folderOpenSvg from 'common/components/icons/folder-open.svg';
9-
import jsonSvg from 'common/components/icons/json.svg';
10-
import reasonSvg from 'common/components/icons/reason.svg';
11-
import yarnSvg from 'common/components/icons/yarn.svg';
12-
import markdownSvg from 'common/components/icons/markdown.svg';
13-
import faviconSvg from 'common/components/icons/favicon.svg';
14-
import htmlSvg from 'common/components/icons/html.svg';
15-
import npmSvg from 'common/components/icons/npm.svg';
16-
import vueSvg from 'common/components/icons/vue.svg';
17-
import fileSvg from 'common/components/icons/file.svg';
18-
import svgSvg from 'common/components/icons/svg.svg';
19-
import imageSvg from 'common/components/icons/image.svg';
20-
import prettierSvg from 'common/components/icons/prettier.svg';
21-
import codesandboxSvg from 'common/components/icons/codesandbox.svg';
22-
import babelSvg from 'common/components/icons/babel.svg';
23-
import sassSvg from 'common/components/icons/sass.svg';
24-
25-
const icons = {
26-
directory: folderSvg,
27-
'directory-open': folderOpenSvg,
28-
react: reactSvg,
29-
css: cssSvg,
30-
json: jsonSvg,
31-
yarn: yarnSvg,
32-
md: markdownSvg,
33-
favicon: faviconSvg,
34-
html: htmlSvg,
35-
npm: npmSvg,
36-
vue: vueSvg,
37-
js: jsSvg,
38-
ts: tsSvg,
39-
svg: svgSvg,
40-
image: imageSvg,
41-
prettier: prettierSvg,
42-
codesandbox: codesandboxSvg,
43-
babel: babelSvg,
44-
sass: sassSvg,
45-
reason: reasonSvg,
46-
};
47-
48-
function getIconSvg(type) {
49-
return icons[type] || fileSvg;
50-
}
51-
523
export const RedIcon = styled.span`
534
color: ${props => props.theme.red};
545
width: ${props => props.width}px;
556
height: ${props => props.height}px;
567
`;
578

589
export const SVGIcon = styled.span`
59-
background-image: url(${props => getIconSvg(props.type)});
10+
background-image: url(${props => props.url});
6011
background-size: ${props => props.width}px;
6112
background-position: 0;
6213
background-repeat: no-repeat;

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/index.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22

33
import ErrorIcon from 'react-icons/lib/md/error';
44

55
import { RedIcon, SVGIcon } from './elements';
6+
import getIconURL from './GetIconURL';
67

7-
const getIcon = (type, error, width, height) => {
8-
if (error) {
9-
return (
10-
<RedIcon>
11-
<ErrorIcon width={width} height={height} />
12-
</RedIcon>
13-
);
8+
class GetIcon extends Component {
9+
state = { icon: null };
10+
11+
getIcon = async type => {
12+
const icon = await getIconURL(type);
13+
14+
this.setState({
15+
icon,
16+
});
17+
};
18+
19+
async componentDidMount() {
20+
this.getIcon(this.props.type);
1421
}
1522

16-
return <SVGIcon type={type} width={width} height={height} />;
17-
};
23+
async componentDidUpdate(prevProps) {
24+
if (this.props.type !== prevProps.type) {
25+
this.getIcon(this.props.type);
26+
}
27+
}
28+
29+
render() {
30+
const { type, error, width, height } = this.props;
31+
const { icon } = this.state;
32+
33+
if (error) {
34+
return (
35+
<RedIcon>
36+
<ErrorIcon width={width} height={height} />
37+
</RedIcon>
38+
);
39+
}
40+
return <SVGIcon url={icon} type={type} width={width} height={height} />;
41+
}
42+
}
1843

1944
function EntryIcon({ type, width = 16, height = 16, error }) {
2045
return (
2146
<div style={{ display: 'inline-block', verticalAlign: 'middle' }}>
22-
{getIcon(type, error, width, height)}
47+
<GetIcon type={type} error={error} width={width} height={height} />
2348
</div>
2449
);
2550
}
Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
11
/* @flow */
22
import isImage from 'common/utils/is-image';
33

4-
const reactRegex = /import.*from\s['|"]react['|"]/;
5-
export function hasReact(code: string) {
6-
return reactRegex.test(code);
7-
}
8-
9-
const cssRegex = /\.css$/;
10-
const sassRegex = /\.s[a|c]ss$/;
11-
const jsonRegex = /\.json$/;
12-
const htmlRegex = /\.html$/;
13-
const mdRegex = /\.md$/;
14-
const vueRegex = /\.vue$/;
154
const svgRegex = /\.svg$/;
16-
const reasonRegex = /\.re$/;
5+
6+
const specialCasesMap = {
7+
'favicon.ico': 'favicon',
8+
'yarn.lock': 'yarn',
9+
'package.json': 'npm',
10+
'sandbox.config.json': 'codesandbox',
11+
'readme.md': 'readme',
12+
'contributing.md': 'contributing',
13+
'tsconfig.json': 'typescript',
14+
'tslint.json': 'typescript',
15+
dockerfile: 'docker',
16+
};
17+
18+
const regexCasesMap = {
19+
markdown: /\.md$/,
20+
yaml: /\.yml$/,
21+
react: /\.jsx$/,
22+
reason: /\.re$/,
23+
sass: /\.scss$/,
24+
javascript: /\.js$/,
25+
typescript: /\.tsx?$/,
26+
console: /\.sh$/,
27+
// STARTS WITH
28+
git: /^.git/i,
29+
flow: /^.flow/i,
30+
};
31+
32+
const getKeyByValue = (object, value) =>
33+
Object.keys(object).find(key => object[key] === value);
1734

1835
export function getMode(title: string = '') {
19-
if (title === 'favicon.ico') {
20-
return 'favicon';
21-
}
36+
// Remove Ignore
37+
const removeIgnoreTitle = title.split('ignore')[0].toLowerCase();
2238

23-
if (title === 'yarn.lock') {
24-
return 'yarn';
25-
}
39+
const titleArr = removeIgnoreTitle.split('.');
2640

27-
if (title === 'package.json') {
28-
return 'npm';
41+
// RemoveTitle
42+
if (removeIgnoreTitle.endsWith('rc')) {
43+
return titleArr.join('').split('rc')[0];
2944
}
3045

31-
if (title === '.prettierrc') {
32-
return 'prettier';
33-
}
46+
// Name Bases
47+
const keys = Object.keys(specialCasesMap);
48+
if (keys.includes(removeIgnoreTitle))
49+
return specialCasesMap[removeIgnoreTitle];
3450

35-
if (title === 'sandbox.config.json') {
36-
return 'codesandbox';
37-
}
51+
// TEST BASED
52+
const regexValues = Object.values(regexCasesMap);
53+
const match = regexValues.find(value =>
54+
new RegExp(value).test(removeIgnoreTitle)
55+
);
3856

39-
if (title === '.babelrc') {
40-
return 'babel';
41-
}
57+
if (match) return getKeyByValue(regexCasesMap, match);
4258

43-
if (cssRegex.test(title)) return 'css';
44-
if (jsonRegex.test(title)) return 'json';
45-
if (htmlRegex.test(title)) return 'html';
46-
if (mdRegex.test(title)) return 'md';
47-
if (vueRegex.test(title)) return 'vue';
48-
if (svgRegex.test(title)) return 'svg';
49-
if (sassRegex.test(title)) return 'sass';
50-
if (reasonRegex.test(title)) return 'reason';
51-
if (!title.includes('.')) return 'raw';
59+
// Include tests
60+
if (!removeIgnoreTitle.includes('.')) return 'raw';
61+
if (
62+
removeIgnoreTitle.endsWith('.module.ts') ||
63+
removeIgnoreTitle.endsWith('.component.ts')
64+
)
65+
return 'angular';
66+
if (removeIgnoreTitle.includes('webpack')) return 'webpack';
5267

53-
if (isImage(title)) {
68+
if (isImage(removeIgnoreTitle) && !svgRegex.test(removeIgnoreTitle)) {
5469
return 'image';
5570
}
5671

57-
return '';
58-
}
59-
60-
const jsRegex = /\.jsx?$/;
61-
const tsRegex = /\.tsx?$/;
62-
function isJS(title: string) {
63-
if (jsRegex.test(title)) return 'js';
64-
if (tsRegex.test(title)) return 'ts';
65-
return undefined;
72+
return titleArr[titleArr.length - 1];
6673
}
6774

6875
export default function getType(title: string) {
69-
const isJSType = isJS(title);
70-
if (isJSType) {
71-
// if (hasReact(code || '')) return 'react';
72-
return isJSType;
73-
}
74-
7576
return getMode(title);
7677
}

packages/common/components/icons/css.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/common/components/icons/js.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/common/components/icons/json.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/common/components/icons/markdown.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/common/components/icons/npm.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/common/components/icons/react.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)