Skip to content

Commit

Permalink
Load Parse snippets from astexplorer server
Browse files Browse the repository at this point in the history
This also merges the Revision classes into the main storage modules
themselves. There is no reason to keep the class separate.
  • Loading branch information
fkling committed Jan 30, 2017
1 parent 665bfc4 commit beb4cc0
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 375 deletions.
1 change: 0 additions & 1 deletion website/package.json
Expand Up @@ -74,7 +74,6 @@
"jscodeshift": "^0.3.28",
"json-stringify-safe": "^5.0.1",
"lodash.isequal": "^4.5.0",
"parse": "^1.6.9",
"parse5": "^3.0.1",
"postcss": "^5.0.12",
"postcss-less": "^0.15.0",
Expand Down
3 changes: 1 addition & 2 deletions website/src/components/GistBanner.js
Expand Up @@ -4,7 +4,6 @@
*/

import React from 'react';
import {matchesURL} from '../storage/parse';
import {connect} from 'react-redux';
import {getRevision} from '../store/selectors';

Expand Down Expand Up @@ -46,7 +45,7 @@ class GistBanner extends React.Component {
return null;
}

if (!(this.props.revision && matchesURL())) {
if (!this.props.revision || this.props.revision.canSave()) {
return null;
}

Expand Down
95 changes: 94 additions & 1 deletion website/src/storage/gist.js
@@ -1,5 +1,6 @@
import React from 'react';
import api from './api';
import Revision from './gist/Revision';
import {getParserByID} from '../parsers';

function getIDAndRevisionFromHash() {
let match = global.location.hash.match(/^#\/gist\/([^\/]+)(?:\/([^\/]+))?/);
Expand Down Expand Up @@ -126,3 +127,95 @@ export function fork(revision, data) {
})
.then(data => new Revision(data));
}

class Revision {
constructor(gist) {
this._gist = gist;
this._config = JSON.parse(gist.files['astexplorer.json'].content);
}

canSave() {
return true;
}

getPath() {
return `/gist/${this.getSnippetID()}/${this.getRevisionID()}`;
}

getSnippetID() {
return this._gist.id;
}
getRevisionID() {
return this._gist.history[0].version;
}

getTransformerID() {
return this._config.toolID;
}

getTransformCode() {
const transformFile = this._gist.files['transform.js'];
return transformFile ? transformFile.content : '';
}

getParserID() {
return this._config.parserID;
}

getCode() {
if (this._code == null) {
this._code = getSource(this._config, this._gist) || '';
}
return this._code;
}

getParserSettings() {
return this._config.settings[this._config.parserID];
}

getShareInfo() {
const snippetID = this.getSnippetID();
const revisionID = this.getRevisionID();
return (
<div className="shareInfo">
<dl>
<dt>Current Revision</dt>
<dd>
<input
readOnly={true}
onFocus={e => e.target.select()}
value={`https://astexplorer.net/#/gist/${snippetID}/${revisionID}`}
/>
</dd>
<dt>Latest Revision</dt>
<dd>
<input
readOnly={true}
onFocus={e => e.target.select()}
value={`https://astexplorer.net/#/gist/${snippetID}/latest`}
/>
</dd>
<dt>Gist</dt>
<dd>
<input
readOnly={true}
onFocus={e => e.target.select()}
value={`https://gist.github.com/${snippetID}/${revisionID}`}
/>
</dd>
</dl>
</div>
);
}
}

function getSource(config, gist) {
switch (config.v) {
case 1:
return gist.files['code.js'].content;
case 2: {
const ext = getParserByID(config.parserID).category.fileExtension;
return gist.files[`source.${ext}`].content;
}
}
}
90 changes: 0 additions & 90 deletions website/src/storage/gist/Revision.js

This file was deleted.

0 comments on commit beb4cc0

Please sign in to comment.