-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtwc-sky.js
More file actions
356 lines (334 loc) · 11.8 KB
/
twc-sky.js
File metadata and controls
356 lines (334 loc) · 11.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
(function () {
window.twits = {
// Original text of the TiddlyWiki file
originalText: null,
// Dropbox keys
isProd: (window.location.protocol + "//" + window.location.host + window.location.pathname) ===
"https://dl.dropboxusercontent.com/spa/4f6lw6nhu5zn5pr/TiddlyWiki/public/index.html",
apiKeyProd: "waukml5k6zt6vzr",
apiKeyDev: "5zahnrxzw6wsy70",
// The start of valid TiddlyWiki 2.x.x documents
start: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n<head>\n<' + 'script id="versionArea" type="text/javascript">\n//<![CDATA[\nvar version = {title: "TiddlyWiki", major: ',
// Definitions of the type, start and end of each block of a TiddlyWiki document
blocks: [{
type: 'html',
start: '<' + '/noscript>',
end: '<!--POST-STOREAREA-->'
}, {
type: 'script',
start: '<' + 'script id="versionArea" type="text/javascript">\n//<![CDATA[',
end: '//]]>\n</' + 'script>'
}, {
type: 'style', // Recent TiddlyWikis
start: '<' + 'style id="styleArea" type="text/css">',
end: '</' + 'style>'
}, {
type: 'style', // Older TiddlyWikis
start: '<' + 'style type="text/css">',
end: '</' + 'style>'
}, {
type: 'script',
start: '<' + 'script id="jsArea" type="text/javascript">\n//<![CDATA[',
end: '</' + 'script>'
}, {
type: 'script',
start: '<' + 'script id="jsdeprecatedArea" type="text/javascript">',
end: '</' + 'script>'
}, {
type: 'script',
start: '<' + 'script id="jslibArea" type="text/javascript">',
end: '</' + 'script>'
}, {
type: 'script',
start: '<' + 'script id="jqueryArea" type="text/javascript">',
end: '</' + 'script>'
}]
};
// Main application
twits.initApp = function () {
// Indicate whether we're running in dev
if (!twits.isProd) {
var dev = document.getElementById("twits-dev");
dev.appendChild(document.createTextNode("(dev)"));
}
// Initialise Dropbox for full access
twits.setStatusMessage("Initializing...");
var apiKey = twits.isProd ? twits.apiKeyProd : twits.apiKeyDev;
twits.client = new Dropbox.Client({
key: apiKey, sandbox: false
});
// Apparently not needed any more (since Dropbox.js 10.x)
// // Use the basic redirection authentication driver
// twits.client.authDriver(new Dropbox.Drivers.Redirect({useQuery: false}));
// Authenticate against Dropbox
twits.setStatusMessage("Authenticating with Dropbox...");
twits.client.authenticate(function (error, client) {
twits.clearStatusMessage();
if (error) {
return twits.showError(error); // Something went wrong.
}
twits.readFolder("/", document.getElementById("twits-files"));
});
};
twits.readFolder = function (path, parentNode) {
// Loading message
var listParent = document.createElement("ol");
listParent.appendChild(document.createTextNode("Loading..."));
parentNode.appendChild(listParent);
// Read the top level directory
twits.client.stat(path, { readDir: true }, function (error, stat, stats) {
if (error) {
return twits.showError(error); // Something went wrong.
}
// Remove loading message
while (listParent.hasChildNodes()) {
listParent.removeChild(listParent.firstChild);
}
// Load entries
for (var t = 0; t < stats.length; t++) {
stat = stats[t];
var listItem = document.createElement("li"),
classes = [];
if (stat.isFolder) {
classes.push("twits-folder");
} else {
classes.push("twits-file");
if (stat.mimeType === "text/html") {
classes.push("twits-file-html");
}
}
var link;
classes.push("twits-file-entry");
if (stat.isFolder || (stat.isFile && stat.mimeType === "text/html")) {
link = document.createElement("a");
link.href = "#";
link.setAttribute("data-twits-path", stat.path);
link.addEventListener("click", twits.onClickFolderEntry, false);
} else {
link = document.createElement("span");
}
link.className = classes.join(" ");
var img = document.createElement("img");
img.src = "dropbox-icons/16x16/" + stat.typeIcon + ".gif";
img.style.width = "16px";
img.style.height = "16px";
link.appendChild(img);
link.appendChild(document.createTextNode(stat.name));
if (stat.isFile && stat.humanSize) {
var size = document.createElement("span");
size.appendChild(document.createTextNode(" (" + stat.humanSize + ")"));
link.appendChild(size);
}
listItem.appendChild(link);
listParent.appendChild(listItem);
}
});
};
twits.onClickFolderEntry = function (event) {
var path = this.getAttribute("data-twits-path"),
classes = this.className.split(" ");
if (classes.indexOf("twits-folder") !== -1 && classes.indexOf("twits-folder-open") === -1) {
classes.push("twits-folder-open");
twits.readFolder(path, this.parentNode);
}
if (classes.indexOf("twits-file-html") !== -1) {
twits.openFile(path);
}
this.className = classes.join(" ");
event.preventDefault();
return false;
};
twits.openFile = function (path) {
// Read the TiddlyWiki file
// We can't trust Dropbox to have detected that the file is UTF8, so we load it in binary and manually decode it
twits.setStatusMessage("Reading HTML file...");
twits.trackProgress(twits.client.readFile(path, { arrayBuffer: true }, function (error, data) {
if (error) {
return twits.showError(error); // Something went wrong.
}
// We have to manually decode the file as UTF8, annoyingly
var byteData = new Uint8Array(data);
data = twits.manualConvertUTF8ToUnicode(byteData);
twits.clearStatusMessage();
// Check it is a valid TiddlyWiki
if (twits.isTiddlyWiki(data)) {
// Save the text and path
twits.originalPath = path;
twits.originalText = data;
// Fillet the content out of the TiddlyWiki
twits.filletTiddlyWiki(data);
} else {
twits.showError("Not a TiddlyWiki!");
}
}));
}
twits.getStatusPanel = function () {
var getElement = function (id, parentNode) {
parentNode = parentNode || document;
var el = document.getElementById(id);
if (!el) {
el = document.createElement("div");
el.setAttribute("id", id);
parentNode.appendChild(el);
}
return el;
},
status = getElement("twits-status", document.body),
message = getElement("twits-message", status),
progress = getElement("twits-progress", status);
status.style.display = "block";
return { status: status, message: message, progress: progress };
};
twits.clearStatusMessage = function () {
var status = twits.getStatusPanel();
status.status.style.display = "none";
}
twits.setStatusMessage = function (text) {
var status = twits.getStatusPanel();
while (status.message.hasChildNodes()) {
status.message.removeChild(status.message.firstChild);
}
status.message.appendChild(document.createTextNode(text));
};
twits.setProgress = function (text) {
var status = twits.getStatusPanel();
while (status.progress.hasChildNodes()) {
status.progress.removeChild(status.progress.firstChild);
}
status.progress.appendChild(document.createTextNode(text));
};
// Display an error
twits.showError = function (error) {
twits.setStatusMessage("Error: " + error);
twits.setProgress("");
};
twits.trackProgress = function (xhr, isUpload) {
var onProgressHandler = function (event) {
twits.setProgress(Math.ceil(event.loaded / 1024) + "KB");
},
onLoadHandler = function () {
},
onErrorHandler = function () {
twits.setStatusMessage("XHR error");
};
var src = isUpload ? xhr.upload : xhr;
src.addEventListener("progress", onProgressHandler, false);
src.addEventListener("load", onLoadHandler, false);
src.addEventListener("error", onErrorHandler, false);
};
// Determine whether a string is a valid TiddlyWiki 2.x.x document
twits.isTiddlyWiki = function (text) {
return text.indexOf(twits.start) === 0;
};
// Extract the blocks of a TiddlyWiki 2.x.x document and add them to the current document
twits.filletTiddlyWiki = function (text) {
// Extract a block from a string given start and end markers
var extractBlock = function (start, end) {
var s = text.indexOf(start);
if (s !== -1) {
var e = text.indexOf(end, s);
if (e !== -1) {
return text.substring(s + start.length, e);
}
}
return null;
};
// Collect up all the blocks in the document
var output = { html: [], script: [], style: [] };
for (var block = 0; block < twits.blocks.length; block++) {
var blockInfo = twits.blocks[block],
blockText = extractBlock(blockInfo.start, blockInfo.end);
if (blockText) {
output[blockInfo.type].push(blockText);
}
}
// Process the HTML blocks
document.body.innerHTML = output.html.join("\n");
// Process the style blocks
var styleElement = document.createElement("style");
styleElement.type = "text/css";
styleElement.appendChild(document.createTextNode(output.style.join("\n")));
document.getElementsByTagName("head")[0].appendChild(styleElement);
// Compose the boot tail script
var tail = "twits.patchTiddlyWiki();";
// Process the script blocks
var scr = document.createElement("script");
scr.type = "text/javascript";
scr.appendChild(document.createTextNode(output.script.join("\n") + "\n" + tail + "\n"));
document.getElementsByTagName("head")[0].appendChild(scr);
};
twits.patchedSaveChanges = function (onlyIfDirty, tiddlers) {
if (onlyIfDirty && !store.isDirty())
return;
clearMessage();
twits.setStatusMessage("Saving changes...");
twits.setProgress("");
var posDiv = locateStoreArea(twits.originalText);
if (!posDiv) {
alert(config.messages.invalidFileError.format([localPath]));
return;
}
var revised = twits.originalText;
// Update the file
revised = revised.substr(0, posDiv[0] + startSaveArea.length) + "\n" +
store.allTiddlersAsHtml() + "\n" +
revised.substr(posDiv[1]);
var newSiteTitle = getPageTitle().htmlEncode();
revised = revised.replaceChunk("<title" + ">", "</title" + ">", " " + newSiteTitle + " ");
revised = updateLanguageAttribute(revised);
revised = updateMarkupBlock(revised, "PRE-HEAD", "MarkupPreHead");
revised = updateMarkupBlock(revised, "POST-HEAD", "MarkupPostHead");
revised = updateMarkupBlock(revised, "PRE-BODY", "MarkupPreBody");
revised = updateMarkupBlock(revised, "POST-SCRIPT", "MarkupPostBody");
// Save the file to Dropbox
twits.trackProgress(twits.client.writeFile(twits.originalPath, revised, function (error, stat) {
if (error) {
return twits.showError(error); // Something went wrong.
}
twits.clearStatusMessage();
displayMessage(config.messages.mainSaved);
store.setDirty(false);
}), true);
};
twits.patchTiddlyWiki = function () {
window.saveChanges = twits.patchedSaveChanges;
config.tasks.save.action = twits.patchedSaveChanges;
// Older TiddlyWikis use loadOptionsCookie()
var overrideFn = window.loadOptions ? "loadOptions" : "loadOptionsCookie";
var _old_ = window[overrideFn];
window[overrideFn] = function () {
_old_();
config.options.chkHttpReadOnly = false;
};
main();
window[overrideFn] = _old_;
story.closeAllTiddlers();
story.displayTiddlers(null, store.filterTiddlers(store.getTiddlerText("DefaultTiddlers")));
};
twits.manualConvertUTF8ToUnicode = function (utf) {
var uni = [],
src = 0,
b1, b2, b3,
c;
while (src < utf.length) {
b1 = utf[src++];
if (b1 < 0x80) {
uni.push(String.fromCharCode(b1));
} else if (b1 < 0xE0) {
b2 = utf[src++];
c = String.fromCharCode(((b1 & 0x1F) << 6) | (b2 & 0x3F));
uni.push(c);
} else {
b2 = utf[src++];
b3 = utf[src++];
c = String.fromCharCode(((b1 & 0xF) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F));
uni.push(c);
}
}
return uni.join("");
};
// Do our stuff when the page has loaded
document.addEventListener("DOMContentLoaded", function (event) {
twits.initApp();
}, false);
})();