Fix Issue 14: manual merge of text-enabled branch into trunk to enable text element creation/editing

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@85 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2009-06-10 03:12:19 +00:00
parent cd86b6520c
commit 4e27f43026
4 changed files with 183 additions and 31 deletions

View File

@ -57,10 +57,6 @@
height: 16px; height: 16px;
} }
#svg_editor div#tools {
float: left;
}
#svg_editor div#workarea { #svg_editor div#workarea {
float: left; float: left;
} }
@ -71,11 +67,12 @@
float: left; float: left;
} }
#tools { .tools_panel {
background: #E8E8E8; background: #E8E8E8;
height: 580px;
border: 1px solid #808080; border: 1px solid #808080;
padding: 4px; padding: 4px;
float:left;
clear:both;
} }
.tool_button, .tool_button_current { .tool_button, .tool_button_current {

View File

@ -29,7 +29,7 @@
<div id="tool_fhellipse" title="Free-Hand Ellipse"></div> <div id="tool_fhellipse" title="Free-Hand Ellipse"></div>
</div> </div>
<div id="tools"> <div id="tools" class="tools_panel">
<div> <div>
<img class="tool_button" id="tool_select" src="images/select.png" title="Select Tool [1]" alt="Select"/><br/> <img class="tool_button" id="tool_select" src="images/select.png" title="Select Tool [1]" alt="Select"/><br/>
@ -97,7 +97,7 @@
</select> </select>
</div> </div>
<hr/> <hr/>
<div> <div>opacity<br/>
<select id="group_opacity" title="Change group opacity"> <select id="group_opacity" title="Change group opacity">
<option selected="selected" value="1">100 %</option> <option selected="selected" value="1">100 %</option>
<option value="0.9">90 %</option> <option value="0.9">90 %</option>
@ -126,6 +126,33 @@
</div> </div>
</div> </div>
<div id="text_panel" class="tools_panel">
<div>text
<select id="font_family" class="text_tool" title="Change Font Family" disabled>
<option selected="selected" value="serif">serif</option>
<option selected="selected" value="sans-serif">sans-serif</option>
<option selected="selected" value="cursive">cursive</option>
<option selected="selected" value="fantasy">fantasy</option>
<option selected="selected" value="monospace">monospace</option>
</select>
<select id="font_size" class="text_tool" title="Change Font Size" disabled>
<option selected="selected" value="6pt">6pt</option>
<option selected="selected" value="8pt">8pt</option>
<option selected="selected" value="10pt">10pt</option>
<option selected="selected" value="12pt">12pt</option>
<option selected="selected" value="14pt">14pt</option>
<option selected="selected" value="16pt">16pt</option>
<option selected="selected" value="24pt">24pt</option>
<option selected="selected" value="32pt">32pt</option>
<option selected="selected" value="48pt">48pt</option>
<option selected="selected" value="64pt">64pt</option>
<option selected="selected" value="80pt">80pt</option>
<option selected="selected" value="100pt">100pt</option>
</select>
<input id="text" class="text_tool" type="text" title="Change text contents" size="60" disabled/>
</div>
</div>
</div> </div>
</div> </div>

View File

@ -3,8 +3,20 @@ var palette = ["#000000","#202020","#404040","#606060","#808080","#a0a0a0","#c0c
function svg_edit_setup() { function svg_edit_setup() {
var svgCanvas = new SvgCanvas(document.getElementById("svgcanvas")); var svgCanvas = new SvgCanvas(document.getElementById("svgcanvas"));
var setSelectMode = function() {
$('.tool_button_current').removeClass('tool_button_current').addClass('tool_button');
$('#tool_select').addClass('tool_button_current');
$('#styleoverrides').text('*{cursor:move;pointer-events:all} svg{cursor:default}');
svgCanvas.setMode('select');
}
var textBeingEntered = false;
var selectedChanged = function(window,elem) { var selectedChanged = function(window,elem) {
if (elem != null) { if (elem != null) {
// set the mode of the editor to select
setSelectMode();
// update fill color // update fill color
var fillColor = elem.getAttribute("fill"); var fillColor = elem.getAttribute("fill");
@ -56,9 +68,24 @@ function svg_edit_setup() {
strokeDashArray = "none"; strokeDashArray = "none";
} }
$('#stroke_style').val(strokeDashArray); $('#stroke_style').val(strokeDashArray);
if (elem.tagName == "text") {
$('.text_tool').removeAttr('disabled');
$('#font_family').val(elem.getAttribute("font-family"));
$('#font_size').val(elem.getAttribute("font-size"));
$('#text').val(elem.textContent);
$('#text').focus();
$('#text').select();
}
else {
$('.text_tool').attr('disabled', "disabled");
}
} }
} }
$('#text').focus( function(){ textBeingEntered = true; } );
$('#text').blur( function(){ textBeingEntered = false; } );
// bind the selected event to our function that handles updates to the UI // bind the selected event to our function that handles updates to the UI
svgCanvas.bind("selected", selectedChanged); svgCanvas.bind("selected", selectedChanged);
@ -92,6 +119,18 @@ function svg_edit_setup() {
$('#group_opacity').change(function(){ $('#group_opacity').change(function(){
svgCanvas.setOpacity(this.options[this.selectedIndex].value); svgCanvas.setOpacity(this.options[this.selectedIndex].value);
}); });
$('#font_size').change(function(){
svgCanvas.setFontSize(this.options[this.selectedIndex].value);
});
$('#font_family').change(function(){
svgCanvas.setFontFamily(this.options[this.selectedIndex].value);
});
$('#text').keyup(function(){
svgCanvas.setTextContent(this.value);
});
$('.palette_item').click(function(){ $('.palette_item').click(function(){
color = $(this).css('background-color'); color = $(this).css('background-color');
@ -210,6 +249,7 @@ function svg_edit_setup() {
$('#tool_save').click(clickSave); $('#tool_save').click(clickSave);
$('#workarea').keyup(function(event){ $('#workarea').keyup(function(event){
if( textBeingEntered ) { return; }
switch (event.keyCode) { switch (event.keyCode) {
case 37: // left-arrow case 37: // left-arrow
break; break;

View File

@ -29,6 +29,8 @@ function SvgCanvas(c)
var current_opacity = 1; var current_opacity = 1;
var current_stroke_opacity = 1; var current_stroke_opacity = 1;
var current_fill_opacity = 1; var current_fill_opacity = 1;
var current_font_size = "12pt";
var current_font_family = "serif";
var freehand_min_x = null; var freehand_min_x = null;
var freehand_max_x = null; var freehand_max_x = null;
var freehand_min_y = null; var freehand_min_y = null;
@ -119,18 +121,16 @@ function SvgCanvas(c)
// remove selected outline from previously selected element // remove selected outline from previously selected element
if (selected != null && selectedOutline != null) { if (selected != null && selectedOutline != null) {
svgroot.removeChild(selectedOutline); // remove from DOM and store reference in JS
selectedOutline = null; selectedOutline = svgroot.removeChild(selectedOutline);
} }
selected = newSelected; selected = newSelected;
if (selected != null) { if (selected != null) {
var bbox = selected.getBBox(); // we create this element for the first time here
// ideally we should create this element once during init, then remove from the DOM if (selectedOutline == null) {
// and re-append to end of documentElement. This will also allow us to do some selectedOutline = addSvgElementFromJson({
// interesting things like animate the stroke-dashoffset using a SMIL <animate> child
selectedOutline = addSvgElementFromJson({
"element": "rect", "element": "rect",
"attr": { "attr": {
"id": "selectedBox", "id": "selectedBox",
@ -138,14 +138,17 @@ function SvgCanvas(c)
"stroke": "blue", "stroke": "blue",
"stroke-width": "1", "stroke-width": "1",
"stroke-dasharray": "5,5", "stroke-dasharray": "5,5",
"x": bbox.x-1, "width": 1,
"y": bbox.y-1, "height": 1,
"width": bbox.width+2,
"height": bbox.height+2,
// need to specify this style so that the selectedOutline is not selectable // need to specify this style so that the selectedOutline is not selectable
"style": "pointer-events:none", "style": "pointer-events:none",
} }
}); });
// TODO: add SMIL animate child on stroke-dashoffset here
}
// recalculate size and then re-append to bottom of document
recalculateSelectedOutline();
svgroot.appendChild(selectedOutline);
// set all our current styles to the selected styles // set all our current styles to the selected styles
current_fill = selected.getAttribute("fill"); current_fill = selected.getAttribute("fill");
@ -154,10 +157,29 @@ function SvgCanvas(c)
current_stroke_opacity = selected.getAttribute("stroke-opacity"); current_stroke_opacity = selected.getAttribute("stroke-opacity");
current_stroke_width = selected.getAttribute("stroke-width"); current_stroke_width = selected.getAttribute("stroke-width");
current_stroke_style = selected.getAttribute("stroke-dasharray"); current_stroke_style = selected.getAttribute("stroke-dasharray");
if (selected.tagName == "text") {
current_font_size = selected.getAttribute("font-size");
current_font_family = selected.getAttribute("font-family");
}
} }
call("selected", selected); call("selected", selected);
} }
var recalculateSelectedOutline = function() {
if (selected != null && selectedOutline != null) {
var bbox = selected.getBBox();
var sw = selected.getAttribute("stroke-width");
var offset = 1;
if (sw != null && sw != "") {
offset += parseInt(sw)/2;
}
selectedOutline.setAttribute("x", bbox.x-offset);
selectedOutline.setAttribute("y", bbox.y-offset);
selectedOutline.setAttribute("width", bbox.width+(offset<<1));
selectedOutline.setAttribute("height", bbox.height+(offset<<1));
}
}
var mouseDown = function(evt) var mouseDown = function(evt)
{ {
@ -278,6 +300,27 @@ function SvgCanvas(c)
} }
}); });
break; break;
case "text":
started = true;
var newText = addSvgElementFromJson({
"element": "text",
"attr": {
"x": x,
"y": y,
"id": getId(),
"fill": current_fill,
"stroke": current_stroke,
"stroke-width": current_stroke_width,
"stroke-dasharray": current_stroke_style,
"stroke-opacity": current_stroke_opacity,
"fill-opacity": current_fill_opacity,
"opacity": current_opacity / 2,
"font-size": current_font_size,
"font-family": current_font_family,
}
});
newText.textContent = "text";
break;
case "delete": case "delete":
var t = evt.target; var t = evt.target;
if (t == svgroot) return; if (t == svgroot) return;
@ -293,7 +336,6 @@ function SvgCanvas(c)
var x = evt.pageX - container.offsetLeft; var x = evt.pageX - container.offsetLeft;
var y = evt.pageY - container.offsetTop; var y = evt.pageY - container.offsetTop;
var shape = svgdoc.getElementById(getId()); var shape = svgdoc.getElementById(getId());
if (!shape) return; // Error?
switch (current_mode) switch (current_mode)
{ {
case "select": case "select":
@ -306,7 +348,11 @@ function SvgCanvas(c)
selected.setAttributeNS(null, "transform", "translate(" + dx + "," + dy + ")"); selected.setAttributeNS(null, "transform", "translate(" + dx + "," + dy + ")");
selectedOutline.setAttributeNS(null, "transform", "translate(" + dx + "," + dy + ")"); selectedOutline.setAttributeNS(null, "transform", "translate(" + dx + "," + dy + ")");
} }
break; break;
case "text":
shape.setAttribute("x", x);
shape.setAttribute("y", y);
break;
case "line": case "line":
shape.setAttributeNS(null, "x2", x); shape.setAttributeNS(null, "x2", x);
shape.setAttributeNS(null, "y2", y); shape.setAttributeNS(null, "y2", y);
@ -477,18 +523,20 @@ function SvgCanvas(c)
})); }));
} }
break; break;
case "text":
keep = true;
selectElement(element);
break;
} }
d_attr = null; d_attr = null;
obj_num++; obj_num++;
if (element != null) { if (!keep) {
if (!keep) { element.parentNode.removeChild(element);
element.parentNode.removeChild(element); element = null;
element = null; } else if (element != null) {
} else { element.setAttribute("opacity", current_opacity);
element.setAttribute("opacity", current_opacity); cleanupElement(element);
cleanupElement(element); call("changed",element);
call("changed",element);
}
} }
} }
@ -557,6 +605,7 @@ function SvgCanvas(c)
current_stroke_width = val; current_stroke_width = val;
if (selected != null) { if (selected != null) {
selected.setAttribute("stroke-width", val); selected.setAttribute("stroke-width", val);
recalculateSelectedOutline();
call("changed", selected); call("changed", selected);
} }
} }
@ -627,6 +676,45 @@ function SvgCanvas(c)
events[event] = f; events[event] = f;
} }
this.getFontFamily = function() {
return current_font_family;
}
this.setFontFamily = function(val) {
current_font_family = val;
if (selected != null) {
selected.setAttribute("font-family", val);
recalculateSelectedOutline();
call("changed", selected);
}
}
this.getFontSize = function() {
return current_font_size;
}
this.setFontSize = function(val) {
current_font_size = val;
if (selected != null) {
selected.setAttribute("font-size", val);
recalculateSelectedOutline();
call("changed", selected);
}
}
this.getText = function() {
if (selected == null) { return ""; }
return selected.textContent;
}
this.setTextContent = function(val) {
if (selected != null) {
selected.textContent = val;
recalculateSelectedOutline();
call("changed", selected);
}
}
$(container).mouseup(mouseUp); $(container).mouseup(mouseUp);
$(container).mousedown(mouseDown); $(container).mousedown(mouseDown);
$(container).mousemove(mouseMove); $(container).mousemove(mouseMove);