import $ from "./minilib.js";

const KEYCODE_ALT = 18;
const KEYCODE_TAB = 8;

$.get("article form").on("submit", function(evt) {
	//console.log("hey!", this)
	//evt.preventDefault();
});

$.get("article form").on("keydown", function(evt) {
	var keyCode = String.fromCharCode(evt.keyCode);
	if(navigator.platform.match("Mac") ? evt.metaKey : evt.ctrlKey){
		if (keyCode == "S") {
			updateDocument();
			evt.preventDefault();
			return
		}
	}
	if (keyCode == "\t") {
		var $textarea = $.get("textarea[name=data]");
		var start = $textarea.selectionStart;
		var end = $textarea.selectionEnd;
		var data = $textarea.value;

		$textarea.value = data.substring(0, start) + "\t" + data.substring(end);
		$textarea.selectionStart = $textarea.selectionEnd = start + 1;

		evt.preventDefault();
		return
	}
});
$.get("body").on("keydown", function(evt) {
	switch(evt.keyCode ) {
		case KEYCODE_ALT:
			previewOn();
			evt.preventDefault();
		return
	}
}, true)
$.get("body").on("keyup", function(evt) {
	switch(evt.keyCode ) {
		case KEYCODE_ALT:
			previewOff();
			evt.preventDefault();
		return
	}
}, true)
function updateDocument() {
	var str = $.get("article form textarea").value;
	var path = $.get("article form").getAttribute("action");
	$.request("PUT", path, {
		body: str
	});
}

var $preview = $.get(".panel.preview");
var $tabs  = $.get(".tab-control");
function previewOn(){
	var str = $.get("article form textarea").value;
	$.request("POST", "/!/api/preview", {
		body: str
	}).then(function (res) {
		if ( res.statusCode>=200 && res.statusCode< 300) {
			$preview.innerHTML = res.text;
		} else {
			$preview.innerHTML = "Oops! error on get preview";
		}
		// TOOD only class can handle
		$tabs.classList.add("preview");
		$tabs.classList.remove("editor");
	})
}
function previewOff(){
	$tabs.classList.remove("preview");
	$tabs.classList.add("editor");
}

@@ -796,16 +796,17 @@
 %0A%09%7D%0A%7D);%0A
+%0A
 $.get(%22b
@@ -846,136 +846,172 @@
  %7B%0A%09
-switch(evt.keyCode ) %7B%0A%09%09case KEYCODE_ALT:%0A%09%09%09previewOn();%0A%09%09%09evt.preventDefault();%0A%09%09return%0A%09%7D%0A%7D, true)%0A$.get(%22body
+var keyCode = String.fromCharCode(evt.keyCode);%0A%09if (evt.altKey && keyCode == %22P%22) %7B%0A%09%09previewToggle()%0A%09%09evt.preventDefault();%0A%09%7D%0A%7D)%0A%0A$.get(%22.btn.editor
 %22).on(%22
-keyup
+click
 %22, f
@@ -1030,53 +1030,97 @@
  %7B%0A%09
-switch(evt.keyCode ) %7B%0A%09%09case KEYCODE_ALT:%0A%09%09
+previewOff();%0A%09evt.preventDefault();%0A%7D)%0A$.get(%22.btn.preview%22).on(%22click%22, function(evt)%7B%0A
 %09pre
@@ -1128,16 +1128,13 @@
 iewO
-ff
+n
 ();%0A
-%09%09
 %09evt
@@ -1156,28 +1156,11 @@
 ();%0A
-%09%09return%0A%09%7D%0A%7D, true)
+%7D)%0A
 %0Afun
@@ -1914,12 +1914,127 @@
 editor%22);%0A%7D%0A
+function previewToggle() %7B%0A%09if ($tabs.classList.contains(%22preview%22)) %7B%0A%09%09previewOff()%0A%09%7D else %7B%0A%09%09previewOn()%0A%09%7D%0A%7D%0A

Change preview shotcut