-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheditor.js
188 lines (147 loc) · 4.87 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Bookmark Commander by Tom J Demuyt is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Permissions beyond the scope of this license are available by contacting [email protected]
*/
var editor = {};
editor.width = screenwidth - 2;
//This is where the magic happens, so to say
editor.view = function( id )
{
//Function keys
editor.function_keys =
[
{ id : 1 , description : "Help " },
{ id : 2 , description : "Save " },
{ id : 3 , description : "Quit " },
{ id : 4 , description : "Quit " },
{ id : 5 , description : "Test " },
{ id : 6 , description : " " },
{ id : 7 , description : " " },
{ id : 8 , description : " " },
{ id : 9 , description : " " },
{ id : 10 , description : "Quit " },
];
var bookmark = findBookmarkId( commander.bookmarks , id );
if(!bookmark)
bookmark = { title: 'Something went terribly wrong' , url: '' };
if(!bookmark.title)
bookmark.title = 'Something went terribly wrong';
if(!bookmark.url)
{
//Avoid the dreaded undefined ;]
bookmark.url = '';
//Dont show test for folders
editor.function_keys[4].description = ' ';
editor.urlreadonly = "readonly='readonly'";
}
else
{
editor.urlreadonly = "";
}
editor.id = id;
editor.bookmark = bookmark;
editor.changed = false;
editor.saved = false;
//Show javascript nicely, clone into content as to not mess up the original object, this is view after all
var content = bookmark.url;
if( content.startsWith("j") )
content = js_beautify( content , { 'indent_size': 2 } );
editor.bookmark.content = content;
//Table due to general textarea weirdness
var s = "<table><tr><td style='background: rgb(0,0,128);'><pre>";
//Menu
s = s + ("<span class='menu'>" + (" Folder/Bookmark").extend() + "</span>\n" );
//Its a mess, but a short mess ;\
s = s + "<textarea class='blue' cols='" + editor.width + "' rows='3' id='title'>" + bookmark.title + "</textarea>\n"
s = s + ("<span class='menu'>" + (" URL").extend() + "</span>\n" );
s = s + "<textarea class='blue' cols='" + editor.width + "' rows='24' id='url'" + editor.urlreadonly + ">" + content + "</textarea>\n"
for( key in editor.function_keys )
{
var f = editor.function_keys[key];
s = s + ( "<span class='fcode'>F" + f.id + "</span><span class='menu'>" + f.description + "</span><span class='fcode'> </span>" );
}
s = s + ( "<span id='end' class='fcode'>" + " ".repeat( screenwidth - 91 ) + "</span>\n" );
document.body.innerHTML = s;
key_mapping = editor.key_mapping;
//Put focus on the title, at the end
var title = document.getElementById("title");
title.focus();
var position = title.value.length * 2;
title.setSelectionRange(position,position);
}
editor.considerTextAreas = function()
{
var titleElement = document.getElementById( "title" )
var urlElement = document.getElementById( "url" )
var _changed = false;
//Consider the title
if( titleElement.value != editor.bookmark.title )
{
_changed = true;
titleElement.style.fontStyle = "italic"
}
else
{
titleElement.style.fontStyle = "normal"
}
//Consider the url
if( urlElement.value != editor.bookmark.content )
{
_changed = true;
urlElement.style.fontStyle = "italic"
}
else
{
urlElement.style.fontStyle = "normal"
}
editor.changed = _changed;
}
editor.test = function()
{
chrome.tabs.create( { 'url': document.getElementById( "url" ).value }, null );
}
editor.save = function()
{
var o = { title : document.getElementById( "title" ).value };
var url = document.getElementById( "url" ).value.trim();
if( url.length > 0 )
//o.url = url;
o.url = editor.condense( url );
chrome.bookmarks.update( editor.id, o );
editor.saved = true;
document.getElementById("end").innerHTML = ( "<span style='color: yellow'>" + ("SAVED!").extend( screenwidth - 91 ) + "</span>");
editor.bookmark = o;
//Yes, this is messy
//For editing purpose we keep refering to the uncondensed url
editor.bookmark.content = url;
}
editor.condense = function( url )
{
var s = "";
//This only is needed with js bookmarklets
if( !url.startsWith( "j" ) )
return url;
var ta = url.split("\n");
for( var key in ta )
{
//The beautifier inlines comment
//So we need to explitly outline those
var line = ta[key].trim();
if( line.startsWith( "/*" ) )
line = "\n" + line;
s = s + line;
}
return s;
}
//TODO, ask whether the user wants to save ?
editor.quit = function()
{
document.body.innerHTML = commander.backup;
//We need to boot (reread bookmarks from Chome ) if we changed something
if( editor.saved )
commander.boot();
else
commander.draw();
key_mapping = commander.key_mapping;
commander.editing = false;
}