feeles-ide
Version:
The hackable and serializable IDE to make learning material
294 lines (259 loc) • 7.81 kB
JavaScript
import _WeakMap from 'babel-runtime/core-js/weak-map';
import _Object$create from 'babel-runtime/core-js/object/create';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _Promise from 'babel-runtime/core-js/promise';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _Object$assign from 'babel-runtime/core-js/object/assign';
import _Object$freeze from 'babel-runtime/core-js/object/freeze';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import { separate, validateType } from './';
import babelFile from './babelFile';
var _File = function () {
function _File(props) {
_classCallCheck(this, _File);
this.key = props.key || getUniqueId();
var lock = function lock() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _Object$freeze(_Object$assign.apply(Object, [{}].concat(args)));
};
this.props = lock(this.constructor.defaultProps, props);
this.options = lock(this.constructor.defaultOptions, this.props.options);
this._separate = separate(this.props.name);
}
_createClass(_File, [{
key: 'toDataURL',
value: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var _this = this;
var _dataURLCache;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_dataURLCache = this.constructor._dataURLCache;
if (!_dataURLCache.has(this)) {
_context.next = 3;
break;
}
return _context.abrupt('return', _dataURLCache.get(this));
case 3:
_context.next = 5;
return new _Promise(function (resolve) {
var reader = new FileReader();
reader.onload = function () {
var result = reader.result;
_dataURLCache.set(_this, result);
resolve(result);
};
reader.readAsDataURL(_this.blob);
});
case 5:
return _context.abrupt('return', _context.sent);
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function toDataURL() {
return _ref.apply(this, arguments);
}
return toDataURL;
}()
}, {
key: 'collect',
value: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var dataURL;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return this.toDataURL();
case 2:
dataURL = _context2.sent;
return _context2.abrupt('return', {
name: this.name,
type: this.type,
lastModified: this.lastModified,
composed: dataURL.substr(dataURL.indexOf(',') + 1),
options: this.options,
credits: this.credits
});
case 4:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function collect() {
return _ref2.apply(this, arguments);
}
return collect;
}()
}, {
key: 'is',
value: function is(name) {
return validateType(name, this.type);
}
}, {
key: 'babel',
value: function babel(config) {
var _this2 = this;
var onError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var _constructor = this.constructor,
_babelCache = _constructor._babelCache,
_babelConfig = _constructor._babelConfig,
_babelError = _constructor._babelError;
if (_babelConfig === config) {
if (_babelCache.has(this)) return _babelCache.get(this);
if (_babelError.has(this)) throw _babelError.get(this);
} else {
this.constructor._babelConfig = config;
}
var promise = babelFile(this, config).catch(function (err) {
_babelError.set(_this2, err);
onError(err);
});
_babelCache.set(this, promise);
return promise;
}
}, {
key: 'rename',
value: function rename(newName) {
var path = this.path,
ext = this.ext;
var name = path + newName + ext;
return this.set({
name: name
});
}
}, {
key: 'move',
value: function move(newPath) {
if (newPath.lastIndexOf('/') !== newPath.length - 1) {
newPath += '/';
}
var plane = this.plane,
ext = this.ext;
var name = newPath + plane + ext;
return this.set({
name: name
});
}
}, {
key: 'serialize',
value: function serialize() {
var _this3 = this;
var obj = _Object$create(null);
this.constructor.visible.forEach(function (key) {
obj[key] = _this3[key];
});
return obj;
}
}, {
key: 'name',
get: function get() {
return this._separate.name;
}
}, {
key: 'moduleName',
get: function get() {
return this._separate.moduleName;
}
}, {
key: 'path',
get: function get() {
return this._separate.path;
}
}, {
key: 'plane',
get: function get() {
return this._separate.plane;
}
}, {
key: 'ext',
get: function get() {
return this._separate.ext;
}
}, {
key: 'type',
get: function get() {
return this.props.type;
}
}, {
key: 'component',
get: function get() {
return this.props.component;
}
}, {
key: 'header',
get: function get() {
if (this.is('markdown')) {
return this.text.trim() // 前後の空白を削除
.replace(/\n[^]*$/, '') // 改行以降を削除
.trim() // 前後の空白を削除
.replace(/^[#-]*\s*/, '') // 行頭の # - を削除
.replace(/[*~_[\]()`]/g, ''); // * ~ _ [] () `` を削除
}
return this.plane + this.ext;
}
}, {
key: 'credits',
get: function get() {
return this.props.credits instanceof Array ? this.props.credits : [];
}
}, {
key: 'sign',
get: function get() {
return this.props.sign;
}
}, {
key: 'credit',
get: function get() {
var _this4 = this;
var credit = this.credits.find(function (item) {
return item.hash === _this4.hash;
});
if (credit) {
return credit;
}
return this.sign || null;
}
}, {
key: 'isTrashed',
get: function get() {
return !!this.options.isTrashed;
}
}, {
key: 'error',
get: function get() {
return this.constructor._babelError.get(this);
}
}, {
key: 'lastModified',
get: function get() {
return this.props.lastModified || 0;
}
}]);
return _File;
}();
_File.defaultProps = {};
_File.defaultOptions = {};
_File.visible = ['name', 'moduleName', 'type', 'lastModified', 'options', 'credits', 'sign'];
_File._dataURLCache = new _WeakMap();
_File._babelCache = new _WeakMap();
_File._babelConfig = null;
_File._babelError = new _WeakMap();
export default _File;
var getUniqueId = function (i) {
return function () {
return '_File__' + ++i;
};
}(0);