/*\ 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",{},[])] }; BitmapEditor.prototype.renderInDom = function() { var tiddler = this.macroNode.store.getTiddler(this.macroNode.tiddlerTitle), canvas = this.macroNode.content[0].domNode, img = new Image(); this.macroNode.domNode.style.position = "relative"; img.src = "data:" + tiddler.type + ";base64," + tiddler.text; canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext("2d"); ctx.drawImage(img,0,0); }; BitmapEditor.prototype.addEventHandlers = function() { var self = this; this.macroNode.domNode.addEventListener("mousedown",function(event) { self.brushDown = true; event.stopPropagation(); return false; },false); this.macroNode.domNode.addEventListener("mousemove",function(event) { if(self.brushDown) { self.moveTo(event.clientX,event.clientY); event.stopPropagation(); return false; } },false); this.macroNode.domNode.addEventListener("mouseup",function(event) { if(self.brushDown) { self.brushDown = false; self.saveChanges(); event.stopPropagation(); return false; } return true; },false); }; BitmapEditor.prototype.moveTo = function(x,y) { var canvas = this.macroNode.content[0].domNode, canvasRect = canvas.getBoundingClientRect(), ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.arc(x - canvasRect.left,y - canvasRect.top,10,0,Math.PI*2,false); ctx.fill(); ctx.closePath(); }; BitmapEditor.prototype.saveChanges = function() { var tiddler = this.macroNode.store.getTiddler(this.macroNode.tiddlerTitle); if(tiddler) { // data URIs look like "data:;base64," var dataURL = this.macroNode.content[0].domNode.toDataURL(tiddler.type), 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