1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Major additions. Only updates if has changes

This commit is contained in:
Zack Scholl
2016-02-07 08:45:42 -05:00
parent 46fad8792c
commit a6d89e5585
13 changed files with 729 additions and 635 deletions

View File

@@ -23,100 +23,16 @@
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<script src="https://code.jquery.com/jquery-1.8.1.min.js"></script>
<script src="/static/js/jquery-1.8.1.min.js"></script>
<script src="/static/js/jquery.autogrowtextarea.min.js"></script>
<script src="/static/js/cowyo.js"></script>
<script>
jQuery.fn.autoGrow = function() {
return this.each(function() {
var createMirror = function(textarea) {
jQuery(textarea).after('<div class="autogrow-textarea-mirror"></div>');
return jQuery(textarea).next(".autogrow-textarea-mirror")[0]
};
var sendContentToMirror = function(textarea) {
mirror.innerHTML = String(textarea.value).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/ /g, "&nbsp;").replace(/\n/g, "<br />") + ".<br/>.";
if (jQuery(textarea).height() != jQuery(mirror).height()) jQuery(textarea).height(jQuery(mirror).height())
};
var growTextarea = function() {
sendContentToMirror(this)
};
var mirror = createMirror(this);
mirror.style.display = "none";
mirror.style.wordWrap = "break-word";
mirror.style.padding = jQuery(this).css("padding");
mirror.style.width = jQuery(this).css("width");
mirror.style.fontFamily = jQuery(this).css("font-family");
mirror.style.fontSize = jQuery(this).css("font-size");
mirror.style.lineHeight = jQuery(this).css("line-height");
this.style.overflow = "hidden";
this.style.minHeight = this.rows + "em";
this.onkeyup = growTextarea;
sendContentToMirror(this)
})
};
external_ip = '{{ .ExternalIP }}'
title_name = '{{ .Title }}'
</script>
<script charset="utf-8" type="text/javascript">
$(document).ready(function() {
var isTyping = false;
var typingTimer; //timer identifier
var updateInterval;
var doneTypingInterval = 1000; //time in ms, 5 second for example
//on keyup, start the countdown
$('#emit').keyup(function() {
clearTimeout(typingTimer);
clearInterval(updateInterval);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$('#emit').keydown(function() {
clearTimeout(typingTimer);
clearInterval(updateInterval);
document.title = "[UNSAVED] {{ .Title }}";
});
//user is "finished typing," do something
function doneTyping() {
payload = JSON.stringify({ TextData: $('#emit_data').val(), Title: "{{ .Title }}", UpdateServer: true, UpdateClient: false })
send(payload)
console.log("Done typing")
updateInterval = setInterval(updateText, doneTypingInterval);
document.title = "[SAVED] {{ .Title }}";
}
function updateText() {
console.log("Getting server's latest copy")
payload = JSON.stringify({ TextData: $('#emit_data').val(), Title: "{{ .Title }}", UpdateServer: false, UpdateClient: true })
send(payload)
}
// websockets
url = 'ws://{{ .ExternalIP }}/ws';
c = new WebSocket(url);
send = function(data){
console.log("Sending: " + data)
c.send(data)
}
c.onmessage = function(msg){
console.log(msg)
data = JSON.parse(msg.data);
if (data.UpdateClient == true) {
console.log("Updating...")
$('#emit_data').val(data.TextData)
document.title = "[LOADED] {{ .Title }}";
}
console.log(data)
}
c.onopen = function(){
updateText();
updateInterval = setInterval(updateText, doneTypingInterval);
}
});
</script>
<style type="text/css">
textarea {
width: 100%;