Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove beaker storage #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 0 additions & 224 deletions js/simply-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2748,230 +2748,6 @@
return true;
}
},
beaker : {
init : function(endpoint) {
this.endpoint = endpoint;
this.dataEndpoint = endpoint + "data.json";
if (this.endpoint.indexOf("hyper://") === 0 && window.beaker) {
this.hyperdrive = beaker.hyperdrive.drive(this.endpoint);
this.hyperdrive.getInfo().then(function(meta) {
editor.storage.meta = meta;
editor.storage.meta.web_root = "/";
});
}
this.load = editor.storageConnectors.default.load;
this.sitemap = editor.storageConnectors.default.sitemap;
this.page = editor.storageConnectors.default.page;
this.listSitemap = editor.storageConnectors.default.listSitemap;

if (editor.responsiveImages) {
if (
editor.settings['simply-image'] &&
editor.settings['simply-image'].responsive
) {
if (typeof editor.settings['simply-image'].responsive.sizes === "function") {
editor.responsiveImages.sizes = editor.settings['simply-image'].responsive.sizes;
} else if (typeof editor.settings['simply-image'].responsive.sizes === "object") {
editor.responsiveImages.sizes = (function(sizes) {
return function(src) {
var result = {};
var info = src.split(".");
var extension = info.pop().toLowerCase();
if (extension === "jpg" || extension === "jpeg" || extension === "png") {
for (var i=0; i<sizes.length; i++) {
result[sizes[i] + "w"] = info.join(".") + "-simply-scaled-" + sizes[i] + "." + extension;
}
}
return result;
};
}(editor.settings['simply-image'].responsive.sizes));
}
}
window.addEventListener("resize", editor.responsiveImages.resizeHandler);
}
},
connect : function(callback) {
callback();
},
save: function(data,callback) {
editor.storage.file.save(this.dataEndpoint, data, callback);
},
saveTemplate : function(pageTemplate, callback) {
var dataPath = location.pathname.split(/\//, 3)[2];
if (dataPath.match(/\/$/)) {
dataPath += "index.html";
}

editor.storage.hyperdrive.readFile(editor.storage.meta.web_root + pageTemplate).then(function(result) {
if (result) {
editor.storage.file.save(dataPath, result, callback);
}
});
},
list : function(url, callback) {
if (url.indexOf(editor.storage.dataEndpoint) === 0) {
return this.listSitemap(url, callback);
}
if (url == editor.storage.endpoint) {
var result = {
images : [],
folders : [],
files : []
};
result.folders.push({url : editor.storage.dataEndpoint, name : 'My pages'});
var parser = document.createElement("A");

if (document.querySelector("[data-simply-images]")) {
var imagesEndpoint = document.querySelector("[data-simply-images]").getAttribute("data-simply-images");
parser.href = imagesEndpoint;
imagesEndpoint = parser.href;
result.folders.push({url : imagesEndpoint, name : 'My images'});
}
if (document.querySelector("[data-simply-files]")) {
var filesEndpoint = document.querySelector("[data-simply-files]").getAttribute("data-simply-files");
parser.href = filesEndpoint;
filesEndpoint = parser.href;
result.folders.push({url : filesEndpoint, name : 'My files'});
}
return callback(result);
}

var files = beaker.hyperdrive.readdir(url)
.then(function(files) {
var result = {
images : [],
folders : [],
files : []
};
files.forEach(function(file) {
var targetUrl = url + file;
if (targetUrl.substring(-1) === "/") {
result.folders.push({url : targetUrl, name : file});
} else {
if (targetUrl === editor.storage.dataEndpoint) {
result.folders.push({url : targetUrl, name: "My pages"});
} else {
result.files.push({url : targetUrl, name : file});
if (targetUrl.match(/(jpg|jpeg|gif|png|bmp|tif|svg)$/i)) {
result.images.push({url : targetUrl, name : file});
}
}
}
});

return result;
})
.then(function(files) {
callback(files);
})
.catch(function(error) {
console.log("The target endpoint could not be accessed.");
console.log(error);
});
},
file : {
save : function(path, data, callback) {
if (path.indexOf("hyper://") === 0 ) {
path = path.replace("hyper://" + document.location.host + "/", '');
}
if (!editor.storage.hyperdrive) {
callback({
error : true,
message : "No connection to hyperdrive (are you on https?)"
});
console.log("Warning: no connection to hyperdrive (are you on https?)");
return;
}
editor.storage.hyperdrive.getInfo().then(function(info) {
if (!info.writable) {
callback({
error : true,
message : "Not writable."
});
console.log("Warning: Save failed the hyperdrive is not writable.");
return;
}

var executeSave = function(path, data) {
createDirectories(path)
.then(function() {
if (path.match(/\/$/)) {
// path points to a directory;
callback({});
} else {
editor.storage.hyperdrive.writeFile(editor.storage.meta.web_root + path, data).then(function() {
var saveResult = {path : path, response: "Saved."};
callback(saveResult);
});
}
});
};
var createDirectory = function(path, callback) {
return new Promise(function(resolve, reject) {
path = path.replace(/^\/\//, "/");
path = path.replace(/\/$/, "");
editor.storage.hyperdrive.readdir(path).then(
function() {
resolve('already exists');
},
function () {
editor.storage.hyperdrive.mkdir(path).then(function() {
editor.storage.hyperdrive.commit().then(function() {
resolve('created');
});
});
}
);
});
};
var createDirectories = function(path, callback) {
return new Promise(function(resolve, reject) {
var parts = path.split("/");
if (!path.match(/\/$/)) {
parts.pop(); // last part is the filename
}
var dirToCreate = '/';

var promises = [];

for (var i=0; i<parts.length; i++) {
if (parts[i] !== "") {
dirToCreate += parts[i] + "/";
}
if (dirToCreate != "/") {
promises.push(createDirectory(editor.storage.meta.web_root + dirToCreate));
}
}
Promise.all(promises).then(function() {
resolve('created');
});
});
};
if (data instanceof File) {
var fileReader = new FileReader();
fileReader.onload = function(evt) {
executeSave(path, this.result);
};
fileReader.readAsArrayBuffer(data);
} else {
executeSave(path, data);
}
});
},
delete : function(path, callback) {
if (path.match(/\/$/)) {
// path points to a directory;
editor.storage.hyperdrive.rmdir(editor.storage.meta.web_root + path, {recursive: true}).then(function() {
callback();
});
} else {
editor.storage.hyperdrive.unlink(editor.storage.meta.web_root + path).then(function() {
callback();
});
}
}
}
},
github : {
repoName : null,
repoUser : null,
Expand Down