Skip to content

Commit

Permalink
moved text editor to it's own repository
Browse files Browse the repository at this point in the history
  • Loading branch information
zodern committed Apr 2, 2015
0 parents commit 0a2482d
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Text Editor",
"title": "Text Editor",
"url": "/textEditor/index.html",
"icon": "logo.png",
"multipleWindows": true,
"opens": [
"text/plain",
"*"
]
}
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var methods = Silk.methods;

methods.add({
"te/open": function (file, callObj, send) {
var fs = require("fs");
var fileName = file;
console.log("fileName");
fs.exists(fileName, function (exists) {
if (!exists) return send("this file does not exist");

fs.stat(fileName, function (err, stats) {
if (err) return send(err);
if (stats.isDirectory())
return send(new Error("Editing directories in a text editor is not currently supported"));
fs.readFile(fileName, function (err, data) {
if (err) return send(err);
var ret = {
state: "ready",
content: data.toString("utf-8")
}
send(void(0), ret);
})
});
});
return {
state: "loading"
}
}
});

methods.add({
"te/save": function (data, callObj, send) {
var fs = require("fs");
console.log(data);
path = data.path;
contents = data.contents;
console.log("==========");
// console.log(contents);
fs.writeFile(path, contents, function (err) {
if (err) return console.log(err);
console.log("saved: " + path);
});
console.log("finished");

// tricks silk into sending return value;
return " ";
}
})
23 changes: 23 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>

<head>
<link rel="stylesheet" href="textEditor.css">

</head>

<body>
<div id="toolBar">
<div id="notifications"></div>
<button id="save">Save</button>
</div>
<textarea wrap="soft" class="mousetrap" id="text"></textarea>
<script src="/bc/jquery"></script>
<script src="/bc/jschannel"></script>
<script src="js/mousetrap.js"></script>
<script src="js/Silk.js"></script>
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
<script src="/js/call.js"></script>
<script src="js/textEditor.js"></script>
</body>

</html>
35 changes: 35 additions & 0 deletions public/js/Silk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*! Silk.js 2014-11-13 */
var Silk = {};

Silk.events = {}, Silk.events.openFile = function() {}, Silk.event = function(name, func) {
switch (Silk.events[name] = func, name) {
case "openFile":
Silk.fileToOpen();
}
};

var chan = Channel.build({
window: window.parent,
origin: "*",
scope: "testScope"
});

Silk.openFile = function(path, mime) {
chan.notify({
method: "openFile",
params: {
path: path,
mime: mime
}
});
}, chan.bind("fileToOpen", function(context, data) {
Silk.events.openFile(data.path);
}), Silk.fileToOpen = function() {
name = "file", name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)", regex = new RegExp(regexS), results = regex.exec(window.location.href);
if (null == results) return null;
try {
Silk.events.openFile(decodeURIComponent(results[1]));
} catch (e) {}
return decodeURIComponent(results[1]);
};
9 changes: 9 additions & 0 deletions public/js/mousetrap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions public/js/textEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var file = {};
file.path = null;
file.changed = false;
$("#notifications").html("Please open a file using File Explorer");
Silk.event("openFile", function (path) {

if (path == null) {
$("#notifications").html("Please open a file using File Explorer");
file.path == null;
} else {
if(file.changed === true){
var fileName = path.split("/");
fileName = fileName[fileName.length - 1];
if(file.path != null){
file.name = file.path.split("/");
file.name = file.name[file.name.length - 1];
}
else{
file.name = "Untitled"
}
var answer = confirm("Are you sure you want to open " + fileName + "\n \n You will lose your changes to " + file.name);
if(answer === false){
return false;
}
}
file.path = path;
methods.listen("te/open", file.path, function (error, data) {
if (error) {
alert(error);
}

if (data.state === "loading") {
$("#notifications").html("Loading");
}
if (data.content != undefined) {
$("#text").val(data.content);
$("#notifications").html(file.path);
}

});
}
});

$("#text").on("keydown", function(e){
if(file.changed == false){
$("#toolBar").css("borderBottomColor", "rgba(219, 179, 53, 1)");
}
file.changed = true;
})

$("#save").click(function () {
if (file.path != null) {
$("#save").text("Saving...");

methods.call("te/save", {
path: file.path,
contents: $("#text").val()
}, function (err, result) {
$("#toolBar").css("borderBottomColor", " rgba(128, 128, 128, 0.8)");
$("#save").text("Save");
})
file.changed = false;

}
});

$(document).delegate('#text', 'keydown', function(e) {
var keyCode = e.keyCode || e.which;

if (keyCode == 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;

// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start)
+ "\t"
+ $(this).val().substring(end));

// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});

/* keyboard shortcuts */
Mousetrap.bind(['ctrl+s', 'command+s'], function(e){
if (e.preventDefault) {
e.preventDefault();
} else {
// internet explorer
e.returnValue = false;
}
$("#save").click();
});
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions public/textEditor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#text {
width: 100%;
height: 100%;
padding: 20px;
padding-bottom: 50px;
padding-top: 50px;
position: absolute;
top: 0px;
left: 0px;
box-sizing: border-box;
background-color: rgb(237, 237, 237);
color: rgb(69, 69, 69);
-webkit-transition: .2s all;
font-family: monospace;
line-height: 1.5;
font-size: 14px;
}
#text:focus {
background-color: rgb(255, 255, 255);
}
#toolBar {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
padding: 10px;
width: 100%;
height: 40px;
border-bottom: 3px solid rgba(128, 128, 128, 0.8);
box-sizing: border-box;
background-color: rgba(237, 237, 237, 0.8);
}
#save {
position: absolute;
height: 37px;
width: 60px;
top: 0px;
right: 0px;
background-color: rgba(219, 179, 53, 1);
border: 0px;
color: rgb(71, 71, 71);
font-size: 14px;
cursor: pointer;
-webkit-transition: .2s background-color;
}
#save:hover{
background-color: rgb(219, 166, 0);
}

0 comments on commit 0a2482d

Please sign in to comment.