class Editor {
constructor(parent) {
this.parent = $(parent);
+ this.data = this.parent.data();
+
+ // Hide the preview here
+ this.preview = $("#preview");
+ this.preview.hide();
// Get the textarea
this.textarea = this.parent.find("textarea");
// Bind all keys
this.bindKeys();
+ // Change timer for preview
+ var update = null;
+
console.log("Editor initialised for " + this.parent);
// Set focus on the textarea
activateTextArea() {
var editor = this;
+ // Render preview when content has changed
+ this.textarea.on("keyup change", function(e) {
+ if (editor.update)
+ clearTimeout(editor.update);
+
+ var content = editor.textarea.val();
+
+ // If the field is all empty, we will hide it
+ if (content)
+ editor.preview.show();
+ else
+ editor.preview.hide();
+
+ // Go into update mode
+ editor.preview.addClass("updating");
+
+ // Render content and show it
+ editor.update = setTimeout(function() {
+ var c = $("#preview-content");
+
+ $.post(editor.data.render_url, { content : content },
+ function(data) {
+ c.html(data);
+
+ // Update finished
+ editor.preview.removeClass("updating");
+ }
+ );
+ }, 750);
+ });
+
// Remember any selected text
this.textarea.on("select keyup click", function(e) {
// Ignore any keyboard shortcuts