Skip to content

Commit 0a2482d

Browse files
committed
moved text editor to it's own repository
0 parents  commit 0a2482d

File tree

8 files changed

+269
-0
lines changed

8 files changed

+269
-0
lines changed

app.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Text Editor",
3+
"title": "Text Editor",
4+
"url": "/textEditor/index.html",
5+
"icon": "logo.png",
6+
"multipleWindows": true,
7+
"opens": [
8+
"text/plain",
9+
"*"
10+
]
11+
}

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var methods = Silk.methods;
2+
3+
methods.add({
4+
"te/open": function (file, callObj, send) {
5+
var fs = require("fs");
6+
var fileName = file;
7+
console.log("fileName");
8+
fs.exists(fileName, function (exists) {
9+
if (!exists) return send("this file does not exist");
10+
11+
fs.stat(fileName, function (err, stats) {
12+
if (err) return send(err);
13+
if (stats.isDirectory())
14+
return send(new Error("Editing directories in a text editor is not currently supported"));
15+
fs.readFile(fileName, function (err, data) {
16+
if (err) return send(err);
17+
var ret = {
18+
state: "ready",
19+
content: data.toString("utf-8")
20+
}
21+
send(void(0), ret);
22+
})
23+
});
24+
});
25+
return {
26+
state: "loading"
27+
}
28+
}
29+
});
30+
31+
methods.add({
32+
"te/save": function (data, callObj, send) {
33+
var fs = require("fs");
34+
console.log(data);
35+
path = data.path;
36+
contents = data.contents;
37+
console.log("==========");
38+
// console.log(contents);
39+
fs.writeFile(path, contents, function (err) {
40+
if (err) return console.log(err);
41+
console.log("saved: " + path);
42+
});
43+
console.log("finished");
44+
45+
// tricks silk into sending return value;
46+
return " ";
47+
}
48+
})

public/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
3+
<head>
4+
<link rel="stylesheet" href="textEditor.css">
5+
6+
</head>
7+
8+
<body>
9+
<div id="toolBar">
10+
<div id="notifications"></div>
11+
<button id="save">Save</button>
12+
</div>
13+
<textarea wrap="soft" class="mousetrap" id="text"></textarea>
14+
<script src="/bc/jquery"></script>
15+
<script src="/bc/jschannel"></script>
16+
<script src="js/mousetrap.js"></script>
17+
<script src="js/Silk.js"></script>
18+
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
19+
<script src="/js/call.js"></script>
20+
<script src="js/textEditor.js"></script>
21+
</body>
22+
23+
</html>

public/js/Silk.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*! Silk.js 2014-11-13 */
2+
var Silk = {};
3+
4+
Silk.events = {}, Silk.events.openFile = function() {}, Silk.event = function(name, func) {
5+
switch (Silk.events[name] = func, name) {
6+
case "openFile":
7+
Silk.fileToOpen();
8+
}
9+
};
10+
11+
var chan = Channel.build({
12+
window: window.parent,
13+
origin: "*",
14+
scope: "testScope"
15+
});
16+
17+
Silk.openFile = function(path, mime) {
18+
chan.notify({
19+
method: "openFile",
20+
params: {
21+
path: path,
22+
mime: mime
23+
}
24+
});
25+
}, chan.bind("fileToOpen", function(context, data) {
26+
Silk.events.openFile(data.path);
27+
}), Silk.fileToOpen = function() {
28+
name = "file", name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
29+
var regexS = "[\\?&]" + name + "=([^&#]*)", regex = new RegExp(regexS), results = regex.exec(window.location.href);
30+
if (null == results) return null;
31+
try {
32+
Silk.events.openFile(decodeURIComponent(results[1]));
33+
} catch (e) {}
34+
return decodeURIComponent(results[1]);
35+
};

public/js/mousetrap.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/textEditor.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var file = {};
2+
file.path = null;
3+
file.changed = false;
4+
$("#notifications").html("Please open a file using File Explorer");
5+
Silk.event("openFile", function (path) {
6+
7+
if (path == null) {
8+
$("#notifications").html("Please open a file using File Explorer");
9+
file.path == null;
10+
} else {
11+
if(file.changed === true){
12+
var fileName = path.split("/");
13+
fileName = fileName[fileName.length - 1];
14+
if(file.path != null){
15+
file.name = file.path.split("/");
16+
file.name = file.name[file.name.length - 1];
17+
}
18+
else{
19+
file.name = "Untitled"
20+
}
21+
var answer = confirm("Are you sure you want to open " + fileName + "\n \n You will lose your changes to " + file.name);
22+
if(answer === false){
23+
return false;
24+
}
25+
}
26+
file.path = path;
27+
methods.listen("te/open", file.path, function (error, data) {
28+
if (error) {
29+
alert(error);
30+
}
31+
32+
if (data.state === "loading") {
33+
$("#notifications").html("Loading");
34+
}
35+
if (data.content != undefined) {
36+
$("#text").val(data.content);
37+
$("#notifications").html(file.path);
38+
}
39+
40+
});
41+
}
42+
});
43+
44+
$("#text").on("keydown", function(e){
45+
if(file.changed == false){
46+
$("#toolBar").css("borderBottomColor", "rgba(219, 179, 53, 1)");
47+
}
48+
file.changed = true;
49+
})
50+
51+
$("#save").click(function () {
52+
if (file.path != null) {
53+
$("#save").text("Saving...");
54+
55+
methods.call("te/save", {
56+
path: file.path,
57+
contents: $("#text").val()
58+
}, function (err, result) {
59+
$("#toolBar").css("borderBottomColor", " rgba(128, 128, 128, 0.8)");
60+
$("#save").text("Save");
61+
})
62+
file.changed = false;
63+
64+
}
65+
});
66+
67+
$(document).delegate('#text', 'keydown', function(e) {
68+
var keyCode = e.keyCode || e.which;
69+
70+
if (keyCode == 9) {
71+
e.preventDefault();
72+
var start = $(this).get(0).selectionStart;
73+
var end = $(this).get(0).selectionEnd;
74+
75+
// set textarea value to: text before caret + tab + text after caret
76+
$(this).val($(this).val().substring(0, start)
77+
+ "\t"
78+
+ $(this).val().substring(end));
79+
80+
// put caret at right position again
81+
$(this).get(0).selectionStart =
82+
$(this).get(0).selectionEnd = start + 1;
83+
}
84+
});
85+
86+
/* keyboard shortcuts */
87+
Mousetrap.bind(['ctrl+s', 'command+s'], function(e){
88+
if (e.preventDefault) {
89+
e.preventDefault();
90+
} else {
91+
// internet explorer
92+
e.returnValue = false;
93+
}
94+
$("#save").click();
95+
});

public/logo.png

9.13 KB
Loading

public/textEditor.css

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#text {
2+
width: 100%;
3+
height: 100%;
4+
padding: 20px;
5+
padding-bottom: 50px;
6+
padding-top: 50px;
7+
position: absolute;
8+
top: 0px;
9+
left: 0px;
10+
box-sizing: border-box;
11+
background-color: rgb(237, 237, 237);
12+
color: rgb(69, 69, 69);
13+
-webkit-transition: .2s all;
14+
font-family: monospace;
15+
line-height: 1.5;
16+
font-size: 14px;
17+
}
18+
#text:focus {
19+
background-color: rgb(255, 255, 255);
20+
}
21+
#toolBar {
22+
position: absolute;
23+
top: 0px;
24+
left: 0px;
25+
z-index: 1;
26+
padding: 10px;
27+
width: 100%;
28+
height: 40px;
29+
border-bottom: 3px solid rgba(128, 128, 128, 0.8);
30+
box-sizing: border-box;
31+
background-color: rgba(237, 237, 237, 0.8);
32+
}
33+
#save {
34+
position: absolute;
35+
height: 37px;
36+
width: 60px;
37+
top: 0px;
38+
right: 0px;
39+
background-color: rgba(219, 179, 53, 1);
40+
border: 0px;
41+
color: rgb(71, 71, 71);
42+
font-size: 14px;
43+
cursor: pointer;
44+
-webkit-transition: .2s background-color;
45+
}
46+
#save:hover{
47+
background-color: rgb(219, 166, 0);
48+
}

0 commit comments

Comments
 (0)