/*\ title: js/macros/edit.js \*/ (function(){ /*jslint node: true, browser: true */ "use strict"; var Tiddler = require("../Tiddler.js").Tiddler, Renderer = require("../Renderer.js").Renderer, Dependencies = require("../Dependencies.js").Dependencies, utils = require("../Utils.js"); function BitmapEditor(macroNode) { this.macroNode = macroNode; } BitmapEditor.prototype.getContent = function() { return [Renderer.ElementNode("canvas",{ "class": ["tw-edit-field"] },[])]; }; BitmapEditor.prototype.renderInDom = function() { var tiddler = this.macroNode.store.getTiddler(this.macroNode.tiddlerTitle), canvas = this.macroNode.content[0].domNode, currImage = new Image(); // Set the macro node itself to be position: relative this.macroNode.domNode.style.position = "relative"; // Get the current bitmap into an image object currImage.src = "data:" + tiddler.type + ";base64," + tiddler.text; // Copy it to the on-screen canvas canvas.width = currImage.width; canvas.height = currImage.height; var ctx = canvas.getContext("2d"); ctx.drawImage(currImage,0,0); // And also copy the current bitmap to the off-screen canvas this.currCanvas = document.createElement("canvas"); this.currCanvas.width = currImage.width; this.currCanvas.height = currImage.height; ctx = this.currCanvas.getContext("2d"); ctx.drawImage(currImage,0,0); }; BitmapEditor.prototype.addEventHandlers = function() { var self = this; this.macroNode.domNode.addEventListener("touchstart",function(event) { self.brushDown = true; self.strokeStart(event.touches[0].clientX,event.touches[0].clientY); event.preventDefault(); event.stopPropagation(); return false; },false); this.macroNode.domNode.addEventListener("touchmove",function(event) { if(self.brushDown) { self.strokeMove(event.touches[0].clientX,event.touches[0].clientY); } event.preventDefault(); event.stopPropagation(); return false; },false); this.macroNode.domNode.addEventListener("touchend",function(event) { if(self.brushDown) { self.brushDown = false; self.strokeEnd(); } event.preventDefault(); event.stopPropagation(); return false; },false); this.macroNode.domNode.addEventListener("mousedown",function(event) { self.strokeStart(event.clientX,event.clientY); self.brushDown = true; event.preventDefault(); event.stopPropagation(); return false; },false); this.macroNode.domNode.addEventListener("mousemove",function(event) { if(self.brushDown) { self.strokeMove(event.clientX,event.clientY); event.preventDefault(); event.stopPropagation(); return false; } },false); this.macroNode.domNode.addEventListener("mouseup",function(event) { if(self.brushDown) { self.brushDown = false; self.strokeEnd(); event.preventDefault(); event.stopPropagation(); return false; } return true; },false); }; BitmapEditor.prototype.strokeStart = function(x,y) { var canvas = this.macroNode.content[0].domNode, canvasRect = canvas.getBoundingClientRect(); // Start off a new stroke this.stroke = [{x: x - canvasRect.left, y: y - canvasRect.top}]; }; BitmapEditor.prototype.strokeMove = function(x,y) { var canvas = this.macroNode.content[0].domNode, canvasRect = canvas.getBoundingClientRect(), ctx = canvas.getContext("2d"), t; // Add the new position to the end of the stroke this.stroke.push({x: x - canvasRect.left, y: y - canvasRect.top}); // Redraw the previous image ctx.drawImage(this.currCanvas,0,0); // Render the stroke ctx.lineWidth = 3; ctx.lineCap = "round"; ctx.lineJoin = "round"; ctx.beginPath(); ctx.moveTo(this.stroke[0].x,this.stroke[0].y); for(t=1; t;base64," var dataURL = this.macroNode.content[0].domNode.toDataURL(tiddler.type,0.95), posColon = dataURL.indexOf(":"), posSemiColon = dataURL.indexOf(";"), posComma = dataURL.indexOf(","), type = dataURL.substring(posColon+1,posSemiColon), text = dataURL.substring(posComma+1); var update = {type: type, text: text}; this.macroNode.store.addTiddler(new Tiddler(tiddler,update)); } }; BitmapEditor.prototype.isRefreshable = function() { // Don't ever refresh the bitmap editor return false; }; function TextEditor(macroNode) { this.macroNode = macroNode; } TextEditor.prototype.getContent = function() { var tiddler = this.macroNode.store.getTiddler(this.macroNode.tiddlerTitle), field = this.macroNode.hasParameter("field") ? this.macroNode.params.field : "title", value; if(tiddler) { value = tiddler[field]; } else { switch(field) { case "text": value = "Type the text for the tiddler '" + this.macroNode.tiddlerTitle + "'"; break; case "title": value = this.macroNode.tiddlerTitle; break; default: value = ""; break; } } var type = "div"; if(field === "text") { type = "pre"; } var attributes = { "contenteditable": true, "class": ["tw-edit-field"] }; return [Renderer.ElementNode(type,attributes,[Renderer.TextNode(value)])]; }; TextEditor.prototype.getText = function(text,node) { if(node.nodeType === window.Node.TEXT_NODE) { text.push(node.data); } else if(node.nodeType === window.Node.ELEMENT_NODE && node.nodeName.toLowerCase() === "br") { // Firefox has `
` tags instead of line feeds text.push("\n"); } if(node.hasChildNodes && node.hasChildNodes()) { for(var t=0; t