detect a g layer with the className "Layer" instead of a non empty title (#955)

* detect a g layer with the className "Layer" instead of a non empty title

* fix TU
This commit is contained in:
cg-scorpio
2024-03-12 06:19:47 +01:00
committed by GitHub
parent 77b7bd95b7
commit b166aa02c7
2 changed files with 167 additions and 153 deletions

View File

@@ -2,6 +2,7 @@ import 'pathseg'
import { NS } from '../../../packages/svgcanvas/core/namespaces.js'
import * as draw from '../../../packages/svgcanvas/core/draw.js'
import * as units from '../../../packages/svgcanvas/core/units.js'
import { Layer } from '../../../packages/svgcanvas/core/draw'
describe('draw.Drawing', function () {
const addOwnSpies = (obj) => {
@@ -67,18 +68,21 @@ describe('draw.Drawing', function () {
const setupSVGWith3Layers = function (svgElem) {
const layer1 = document.createElementNS(NS.SVG, 'g')
layer1.setAttribute('class', Layer.CLASS_NAME)
const layer1Title = document.createElementNS(NS.SVG, 'title')
layer1Title.append(LAYER1)
layer1.append(layer1Title)
svgElem.append(layer1)
const layer2 = document.createElementNS(NS.SVG, 'g')
layer2.setAttribute('class', Layer.CLASS_NAME)
const layer2Title = document.createElementNS(NS.SVG, 'title')
layer2Title.append(LAYER2)
layer2.append(layer2Title)
svgElem.append(layer2)
const layer3 = document.createElementNS(NS.SVG, 'g')
layer3.setAttribute('class', Layer.CLASS_NAME)
const layer3Title = document.createElementNS(NS.SVG, 'title')
layer3Title.append(LAYER3)
layer3.append(layer3Title)

View File

@@ -47,6 +47,16 @@ function findLayerNameInGroup (group) {
return sel ? sel.textContent : ''
}
/**
* Verify the classList of the given element : if the classList contains 'layer', return true, then return false
*
* @param {Element} element - The given element
* @returns {boolean} Return true if the classList contains 'layer' then return false
*/
function isLayerElement (element) {
return element.classList.contains('layer')
}
/**
* Given a set of names, return a new unique name.
* @param {string[]} existingLayerNames - Existing layer names.
@@ -489,8 +499,8 @@ export class Drawing {
if (child?.nodeType === 1) {
if (child.tagName === 'g') {
childgroups = true
if (isLayerElement(child)) {
const name = findLayerNameInGroup(child)
if (name) {
layernames.push(name)
layer = new Layer(name, child)
this.all_layers.push(layer)