diff --git a/projects/common/models/section.ts b/projects/common/models/section.ts index edb66eb8852d990845e207b67b8bc8d06af6b8c4..9960eaee9c88b831a096c75769440ef7e1c443ca 100644 --- a/projects/common/models/section.ts +++ b/projects/common/models/section.ts @@ -99,6 +99,13 @@ export class Section { element.getVariableInfos())) .flat(); } + + getLastRowIndex(): number { + const x: number[] = this.elements + .map(el => el.position.gridRow) + .filter((gridRow: number | null): gridRow is number => gridRow !== null); + return Math.max(...x, 0); + } } export interface SectionProperties { diff --git a/projects/editor/src/app/services/unit-services/element.service.ts b/projects/editor/src/app/services/unit-services/element.service.ts index a5b2d6cd329f4dd982bc6b5ab1cfcc55113929b6..49fae17e7e85c5ab254d839ed6ea9d86217af148 100644 --- a/projects/editor/src/app/services/unit-services/element.service.ts +++ b/projects/editor/src/app/services/unit-services/element.service.ts @@ -44,7 +44,7 @@ export class ElementService { command: async () => { let newElementProperties: Partial<UIElementProperties> = {}; try { - newElementProperties = await this.prepareElementProps(elementType, section.dynamicPositioning, coordinates); + newElementProperties = await this.prepareElementProps(elementType, section, coordinates); } catch (e) { if (e === 'dialogCanceled') return {}; } @@ -65,7 +65,7 @@ export class ElementService { } private async prepareElementProps(elementType: UIElementType, - dynamicPositioning: boolean, + section: Section, coordinates?: { x: number, y: number }): Promise<Partial<UIElementProperties>> { const newElementProperties: Partial<UIElementProperties> = {}; @@ -96,13 +96,20 @@ export class ElementService { // no default } - // Coordinates are given if an element is dragged directly onto the canvas - if (coordinates) { + /* Coordinates are given if an element is dragged directly onto the canvas. + x and y have different meaning depending on the layouting of the section, being either absolute + or grid coordinates. */ + if (section.dynamicPositioning) { newElementProperties.position = { - ...dynamicPositioning && { gridColumn: coordinates.x }, - ...dynamicPositioning && { gridRow: coordinates.y }, - ...!dynamicPositioning && { xPosition: coordinates.x }, - ...!dynamicPositioning && { yPosition: coordinates.y } + gridRow: coordinates ? coordinates.x : section.getLastRowIndex() + 1, + gridColumn: coordinates ? coordinates.y : 1, + ...newElementProperties.position + } as PositionProperties; + } else { + newElementProperties.position = { + xPosition: coordinates ? coordinates.x : 0, + yPosition: coordinates ? coordinates.y : 0, + ...newElementProperties.position } as PositionProperties; } return newElementProperties;