diff --git a/src/components/CreateNewModal.jsx b/src/components/CreateNewModal.jsx index 7515fc6..beaf3a8 100644 --- a/src/components/CreateNewModal.jsx +++ b/src/components/CreateNewModal.jsx @@ -1,37 +1,68 @@ -import { h } from 'preact'; +import { h, Component } from 'preact'; import Modal from './Modal'; import { ItemTile } from './ItemTile'; import templates from '../templateList'; -export function CreateNewModal({ - show, - closeHandler, - onBlankTemplateSelect, - onBlankFileTemplateSelect, - onTemplateSelect -}) { - return ( - - - - Start a blank creation - - - Start a blank files creation - - - - Or choose from a template: - - {templates.map(template => ( - - ))} - - - ); +export class CreateNewModal extends Component { + render() { + const { + show, + closeHandler, + onBlankTemplateSelect, + onBlankFileTemplateSelect, + onImportGithubRepoSelect, + onTemplateSelect + } = this.props; + return ( + + + + Start a blank creation + + + Start a blank files creation + + + + + this.setState({ + isGhRepoInputVisible: true + }) + } + > + Import Github Repository + + + {this.state.isGhRepoInputVisible ? ( + + (this.ghRepoInput = el)} /> + { + onImportGithubRepoSelect(this.ghRepoInput.value); + }} + > + Import + + + ) : null} + + + + Or choose from a template: + + {templates.map(template => ( + + ))} + + + ); + } } diff --git a/src/components/FileIcon.jsx b/src/components/FileIcon.jsx index 0907eee..6dadd92 100644 --- a/src/components/FileIcon.jsx +++ b/src/components/FileIcon.jsx @@ -44,8 +44,14 @@ export function FileIcon({ file }) { /> ); break; - + case 'ts': + path: ; + break; case 'css': + case 'less': path = ( ); break; - + case 'scss': + case 'sass': + path = ( + + ); + break; case 'md': case 'markdown': path = ( diff --git a/src/components/app.jsx b/src/components/app.jsx index fc0de15..82b38b8 100644 --- a/src/components/app.jsx +++ b/src/components/app.jsx @@ -28,7 +28,8 @@ import { assignFilePaths, getFileFromPath, removeFileAtPath, - doesFileExistInFolder + doesFileExistInFolder, + importGithubRepo } from '../fileUtils'; import { itemService } from '../itemService'; @@ -319,7 +320,7 @@ export default class App extends Component { alertsService.add(`"${sourceItem.title}" was forked`); trackEvent('fn', 'itemForked'); } - createNewItem(isFileMode = false) { + createNewItem(isFileMode = false, files) { const d = new Date(); let item = { title: @@ -337,24 +338,26 @@ export default class App extends Component { if (isFileMode) { item = { ...item, - files: assignFilePaths([ - { - name: 'index.html', - content: - 'hello\n\n' - }, - { - name: 'styles', - isFolder: true, - children: [{ name: 'style.css', content: '' }] - }, - { name: 'script.js', content: '' }, - { - name: 'tempo', - isFolder: true, - children: [{ name: 'main.css', content: '' }] - } - ]) + files: assignFilePaths( + files || [ + { + name: 'index.html', + content: + 'hello\n\n' + }, + { + name: 'styles', + isFolder: true, + children: [{ name: 'style.css', content: '' }] + }, + { name: 'script.js', content: '' }, + { + name: 'tempo', + isFolder: true, + children: [{ name: 'main.css', content: '' }] + } + ] + ) }; } else { item = { @@ -1311,6 +1314,12 @@ export default class App extends Component { }); this.setState({ isCreateNewModalOpen: false }); } + importGithubRepoSelectHandler(repoUrl) { + importGithubRepo(repoUrl).then(files => { + this.createNewItem(true, files); + this.setState({ isCreateNewModalOpen: false }); + }); + } addFileHandler(fileName, isFolder) { let newEntry = { name: fileName, content: '' }; if (isFolder) { @@ -1634,6 +1643,9 @@ export default class App extends Component { this )} onTemplateSelect={this.templateSelectHandler.bind(this)} + onImportGithubRepoSelect={this.importGithubRepoSelectHandler.bind( + this + )} /> response.json()); + } + function fetchDir(path, currentDir) { + return fetch( + `https://api.github.com/repos/${repoSlug}/contents/${path}${queryString}` + ) + .then(response => response.json()) + .then(response => { + if (!response) { + return; + } + return Promise.all( + response.map(file => { + if (file.type === 'file') { + return fetchFile(`${file.path}`).then(actualFile => { + currentDir.push({ + name: file.name, + content: atob(actualFile.content) + }); + }); + } else if (file.type === 'dir') { + const newEntry = { + name: file.name, + children: [], + isFolder: true + }; + currentDir.push(newEntry); + return fetchDir(`${file.path}`, newEntry.children); + } + }) + ); + }); + } + const files = []; + return fetchDir('', files).then(() => { + return files; + }); +}
+ + this.setState({ + isGhRepoInputVisible: true + }) + } + > + Import Github Repository + + + {this.state.isGhRepoInputVisible ? ( +