feeles-ide
Version:
The hackable and serializable IDE to make learning material
502 lines (413 loc) • 14.4 kB
JavaScript
import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import _extends from 'babel-runtime/helpers/extends';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _getIterator from 'babel-runtime/core-js/get-iterator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
var createUser = function () {
var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
var user;
return _regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
// Create new user with random id
user = {
uuid: uuid()
};
_context9.next = 3;
return personalDB.users.add(user);
case 3:
return _context9.abrupt('return', user);
case 4:
case 'end':
return _context9.stop();
}
}
}, _callee9, this);
}));
return function createUser() {
return _ref9.apply(this, arguments);
};
}();
import Dexie from 'dexie';
import uuid from 'uuid/v1';
import deepEqual from 'deep-equal';
var personalDB = new Dexie('personal');
// DB migrations
personalDB.version(1).stores({
projects: '++id, &title, size, created, updated',
files: '++id, [projectId+fileName]'
});
personalDB.version(2).stores({
projects: '++id, &title, size, created, updated, url',
files: '++id, [projectId+fileName]'
}).upgrade(function () {
personalDB.projects.toCollection().modify(function (project) {
project.url = '';
});
});
personalDB.version(3).stores({
projects: '++id, &title, size, created, updated, url',
files: '++id, [projectId+fileName]',
users: '&uuid'
});
export default personalDB;
export var createProject = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var serializedFiles = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var timestamp, size, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, file, project;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return 1;
case 2:
// Be async
timestamp = Date.now();
size = 0;
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
_context.prev = 7;
for (_iterator = _getIterator(serializedFiles); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
file = _step.value;
size += file.blob ? file.blob.size : file.text.length;
}
// Create project
_context.next = 15;
break;
case 11:
_context.prev = 11;
_context.t0 = _context['catch'](7);
_didIteratorError = true;
_iteratorError = _context.t0;
case 15:
_context.prev = 15;
_context.prev = 16;
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
case 18:
_context.prev = 18;
if (!_didIteratorError) {
_context.next = 21;
break;
}
throw _iteratorError;
case 21:
return _context.finish(18);
case 22:
return _context.finish(15);
case 23:
project = {
title: null,
size: size,
created: timestamp,
updated: timestamp,
url: location.origin + location.pathname,
CORE_VERSION: '',
CORE_CDN_URL: 'https://unpkg.com/feeles-ide@latest/umd/index.js',
// Remote project (product) deployment URL<string>
deployURL: null
};
_context.next = 26;
return personalDB.projects.add(project);
case 26:
project.id = _context.sent;
_context.next = 29;
return personalDB.files.bulkAdd(serializedFiles.map(function (item) {
return {
projectId: project.id,
fileName: item.name,
serializedFile: item
};
}));
case 29:
return _context.abrupt('return', project);
case 30:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[7, 11, 15, 23], [16,, 18, 22]]);
}));
return function createProject() {
return _ref.apply(this, arguments);
};
}();
export var readProject = function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(title) {
var project, query;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return personalDB.projects.where('title').equalsIgnoreCase(title).first();
case 2:
project = _context2.sent;
if (project) {
_context2.next = 5;
break;
}
return _context2.abrupt('return', null);
case 5:
// select * from files where projectId=project.id;
query = personalDB.files.where('[projectId+fileName]').between([project.id, ''], [project.id, '\uFFFF']);
_context2.t0 = project;
_context2.t1 = query;
_context2.next = 10;
return query.clone().count();
case 10:
_context2.t2 = _context2.sent;
return _context2.abrupt('return', {
project: _context2.t0,
query: _context2.t1,
length: _context2.t2
});
case 12:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
return function readProject(_x2) {
return _ref2.apply(this, arguments);
};
}();
export var findProject = function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(id) {
var project, query;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return personalDB.projects.get(id);
case 2:
project = _context3.sent;
if (project) {
_context3.next = 5;
break;
}
return _context3.abrupt('return', null);
case 5:
// select * from files where projectId=project.id;
query = personalDB.files.where('[projectId+fileName]').between([project.id, ''], [project.id, '\uFFFF']);
_context3.t0 = project;
_context3.t1 = query;
_context3.next = 10;
return query.clone().count();
case 10:
_context3.t2 = _context3.sent;
return _context3.abrupt('return', {
project: _context3.t0,
query: _context3.t1,
length: _context3.t2
});
case 12:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
return function findProject(_x3) {
return _ref3.apply(this, arguments);
};
}();
export var updateProject = function () {
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(projectId, update) {
var prevProject, nextProject, duplicated;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return personalDB.projects.where(':id').equals(projectId).first();
case 2:
prevProject = _context4.sent;
nextProject = _extends({}, prevProject, update);
if (!deepEqual(prevProject, nextProject)) {
_context4.next = 6;
break;
}
return _context4.abrupt('return', nextProject);
case 6:
if (!(nextProject.title !== null)) {
_context4.next = 12;
break;
}
_context4.next = 9;
return personalDB.projects.where('title').equalsIgnoreCase(nextProject.title).first();
case 9:
_context4.t0 = _context4.sent;
_context4.next = 13;
break;
case 12:
_context4.t0 = null;
case 13:
duplicated = _context4.t0;
if (!(duplicated && duplicated.id !== nextProject.id)) {
_context4.next = 16;
break;
}
throw 'failedToRename';
case 16:
_context4.next = 18;
return personalDB.projects.put(nextProject);
case 18:
return _context4.abrupt('return', nextProject);
case 19:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
return function updateProject(_x4, _x5) {
return _ref4.apply(this, arguments);
};
}();
export var deleteProject = function () {
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(projectId) {
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return personalDB.projects.delete(projectId);
case 2:
_context5.next = 4;
return personalDB.files.where('[projectId+fileName]').between([projectId, ''], [projectId, '\uFFFF']).delete();
case 4:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
return function deleteProject(_x6) {
return _ref5.apply(this, arguments);
};
}();
// Create or Update file
export var putFile = function () {
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(projectId, serializedFile) {
var found, added;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return personalDB.projects.where(':id').equals(projectId).modify({
updated: serializedFile.lastModified || Date.now()
});
case 2:
_context6.next = 4;
return personalDB.files.where('[projectId+fileName]').equals([projectId, serializedFile.name]).first();
case 4:
found = _context6.sent;
if (found) {
_context6.next = 13;
break;
}
// Any files found, so create new.
added = {
projectId: projectId,
fileName: serializedFile.name,
serializedFile: serializedFile
};
_context6.next = 9;
return personalDB.files.add(added);
case 9:
added.id = _context6.sent;
return _context6.abrupt('return', added);
case 13:
_context6.next = 15;
return personalDB.files.where(':id').equals(found.id).modify({
serializedFile: serializedFile
});
case 15:
return _context6.abrupt('return', serializedFile);
case 16:
case 'end':
return _context6.stop();
}
}
}, _callee6, this);
}));
return function putFile(_x7, _x8) {
return _ref6.apply(this, arguments);
};
}();
export var deleteFile = function () {
var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(projectId) {
var _personalDB$files$whe;
for (var _len = arguments.length, fileNames = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
fileNames[_key - 1] = arguments[_key];
}
var project, keys;
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return personalDB.projects.get(projectId);
case 2:
project = _context7.sent;
if (!project) {
project.modify({
updated: Date.now()
});
}
// Delete files included fileNames
keys = fileNames.map(function (fn) {
return [projectId + '', fn];
});
_context7.next = 7;
return (_personalDB$files$whe = personalDB.files.where('[projectId+fileName]')).anyOfIgnoreCase.apply(_personalDB$files$whe, _toConsumableArray(keys)).delete();
case 7:
case 'end':
return _context7.stop();
}
}
}, _callee7, this);
}));
return function deleteFile(_x9) {
return _ref7.apply(this, arguments);
};
}();
export var getPrimaryUser = function () {
var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return personalDB.users.toCollection().first();
case 2:
_context8.t0 = _context8.sent;
if (_context8.t0) {
_context8.next = 7;
break;
}
_context8.next = 6;
return createUser();
case 6:
_context8.t0 = _context8.sent;
case 7:
return _context8.abrupt('return', _context8.t0);
case 8:
case 'end':
return _context8.stop();
}
}
}, _callee8, this);
}));
return function getPrimaryUser() {
return _ref8.apply(this, arguments);
};
}();