diff --git a/editor/draw.js b/editor/draw.js index e1bc1006..d030b9e9 100644 --- a/editor/draw.js +++ b/editor/draw.js @@ -277,7 +277,8 @@ svgedit.draw.Drawing.prototype.setCurrentLayer = function(name) { }; // Function: svgedit.draw.Drawing.identifyLayers -// Updates layer system +// Updates layer system and sets the current layer to the +// top-most layer (last child of this drawing). svgedit.draw.Drawing.prototype.identifyLayers = function() { this.all_layers = []; var numchildren = this.svgElem_.childNodes.length; @@ -341,5 +342,25 @@ svgedit.draw.Drawing.prototype.identifyLayers = function() { this.current_layer.setAttribute("style","pointer-events:all"); }; +// Function: svgedit.draw.Drawing.createLayer +// Creates a new top-level layer in the drawing with the given name and +// sets the current layer to it. +// +// Parameters: +// name - The given name +// +// Returns: +// The SVGGElement of the new layer, which is also the current layer +// of this drawing. +svgedit.draw.Drawing.prototype.createLayer = function(name) { + var svgdoc = this.svgElem_.ownerDocument; + var new_layer = svgdoc.createElementNS(svg_ns, "g"); + var layer_title = svgdoc.createElementNS(svg_ns, "title"); + layer_title.textContent = name; + new_layer.appendChild(layer_title); + this.svgElem_.appendChild(new_layer); + this.identifyLayers(); + return new_layer; +}; })(); diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 3007fb9a..f35f2cd3 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -6974,16 +6974,10 @@ var identifyLayers = canvas.identifyLayers = function() { // name - The given name this.createLayer = function(name) { var batchCmd = new BatchCommand("Create Layer"); - var new_layer = svgdoc.createElementNS(svgns, "g"); - var layer_title = svgdoc.createElementNS(svgns, "title"); - layer_title.textContent = name; - new_layer.appendChild(layer_title); - new_layer = svgcontent.appendChild(new_layer); + var new_layer = getCurrentDrawing().createLayer(name); batchCmd.addSubCommand(new InsertElementCommand(new_layer)); addCommandToHistory(batchCmd); clearSelection(); - identifyLayers(); - canvas.setCurrentLayer(name); call("changed", [new_layer]); }; diff --git a/test/draw_test.html b/test/draw_test.html index efe7a570..7168f0f2 100644 --- a/test/draw_test.html +++ b/test/draw_test.html @@ -355,6 +355,27 @@ drawing.setCurrentLayer(LAYER3); equals(drawing.getCurrentLayerName(LAYER3), LAYER3); equals(drawing.getCurrentLayer(), drawing.all_layers[2][1]); + + cleanupSvg(svg); + }); + + test('Test createLayer()', function() { + expect(6); + + var drawing = new svgedit.draw.Drawing(svg); + setupSvgWith3Layers(svg); + + ok(drawing.createLayer); + equals(typeof drawing.createLayer, typeof function(){}); + + var NEW_LAYER_NAME = 'Layer A'; + var layer_g = drawing.createLayer(NEW_LAYER_NAME); + equals(4, drawing.getNumLayers()); + equals(layer_g, drawing.getCurrentLayer()); + equals(NEW_LAYER_NAME, drawing.getCurrentLayerName()); + equals(NEW_LAYER_NAME, drawing.getLayerName(3)); + + cleanupSvg(svg); }); });