feeles-ide
Version:
The hackable and serializable IDE to make learning material
172 lines (156 loc) • 5.01 kB
JavaScript
import _WeakMap from 'babel-runtime/core-js/weak-map';
import _Array$from from 'babel-runtime/core-js/array/from';
import _Promise from 'babel-runtime/core-js/promise';
import _JSON$stringify from 'babel-runtime/core-js/json/stringify';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import md5 from 'md5';
import { parse } from './JSON6';
import _File from './_File';
import configs from './configs';
import { encode, decode } from './sanitizeHTML';
var SourceFile = function (_File2) {
_inherits(SourceFile, _File2);
function SourceFile(props) {
_classCallCheck(this, SourceFile);
if (props.composed && !props.text) {
var text = '';
try {
// base64 encode された文字列の展開
text = decodeURIComponent(escape(atob(props.composed)));
} catch (e) {
// 旧仕様(sanitizeHTML)で compose された文字列の展開 (backword compatibility)
text = decode(props.composed);
}
props = _extends({}, props, { text: text });
}
var _this = _possibleConstructorReturn(this, (SourceFile.__proto__ || _Object$getPrototypeOf(SourceFile)).call(this, props));
_this._hash = null;
return _this;
}
_createClass(SourceFile, [{
key: 'set',
value: function set(change) {
if (!change.text && this.hash) {
change.hash = this.hash;
}
var seed = _extends({}, this.serialize(), change);
seed.key = this.key;
seed.lastModified = Date.now();
return new this.constructor(seed);
}
}, {
key: 'compose',
value: function compose() {
var serialized = this.serialize();
delete serialized.text;
serialized.composed = encode(this.text);
if (this.sign && this.sign === this.credit) {
var credits = this.credits.concat(_extends({}, this.sign, {
timestamp: Date.now(),
hash: this.hash
}));
serialized.credits = _JSON$stringify(credits);
} else {
serialized.credits = _JSON$stringify(this.credits);
}
return _Promise.resolve(serialized);
}
/**
* @param file File|Blob
* @return Promise gives SourceFile
*/
}, {
key: 'text',
get: function get() {
return this.props.text;
}
}, {
key: 'isScript',
get: function get() {
return this.is('javascript');
}
}, {
key: 'blob',
get: function get() {
var blobCache = this.constructor.blobCache;
if (blobCache.has(this)) {
return blobCache.get(this);
}
var blob = new Blob([this.text], { type: this.type });
blobCache.set(this, blob);
return blob;
}
}, {
key: 'json',
get: function get() {
var _this2 = this;
if (!this._json) {
var model = _Array$from(configs.values()).find(function (config) {
return config.test.test(_this2.name);
});
var defaultValue = model ? model.defaultValue : {};
try {
this._json = _extends({}, defaultValue, parse(this.text));
} catch (e) {
return {};
}
}
return this._json;
}
}, {
key: 'hash',
get: function get() {
return this._hash = this._hash || md5(this.text);
}
}], [{
key: 'load',
value: function load(file) {
return new _Promise(function (resolve) {
var reader = new FileReader();
reader.onload = function (e) {
resolve(new SourceFile({
type: file.type,
name: file.name || SourceFile.defaultProps.name,
text: e.target.result,
lastModified: file.lastModified
}));
};
reader.readAsText(file);
});
}
}, {
key: 'shot',
value: function shot(text) {
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return new SourceFile({ type: 'text/javascript', name: name, text: text });
}
}, {
key: 'html',
value: function html() {
var text = '<!DOCTYPE html>\n<html>\n <head>\n <meta charset="utf-8">\n <title>404 Not Found</title>\n </head>\n <body style="background-color: white;">\n File Not Found\n </body>\n</html>';
return new SourceFile({
name: '404.html',
type: 'text/html',
text: text
});
}
}]);
return SourceFile;
}(_File);
SourceFile.defaultProps = {
name: '.SourceFile',
text: '',
json: null,
sign: null
};
SourceFile.defaultOptions = {
isTrashed: false
};
SourceFile.visible = _File.visible.concat('text', 'isScript');
SourceFile.blobCache = new _WeakMap();
export default SourceFile;