Fix label of added node (when GUI is not running). Fixes #292

This commit is contained in:
ziajka
2019-04-29 06:38:41 +02:00
parent a4c3bd254b
commit d04aea0f25
5 changed files with 32 additions and 13 deletions

View File

@ -182,6 +182,7 @@ import { ResumeLinkActionComponent } from './components/project-map/context-menu
import { StopCaptureActionComponent } from './components/project-map/context-menu/actions/stop-capture/stop-capture-action.component'; import { StopCaptureActionComponent } from './components/project-map/context-menu/actions/stop-capture/stop-capture-action.component';
import { ConsoleService } from './services/settings/console.service'; import { ConsoleService } from './services/settings/console.service';
import { DefaultConsoleService } from './services/settings/default-console.service'; import { DefaultConsoleService } from './services/settings/default-console.service';
import { NodeCreatedLabelStylesFixer } from './components/project-map/helpers/node-created-label-styles-fixer';
if (environment.production) { if (environment.production) {
Raven.config('https://b2b1cfd9b043491eb6b566fd8acee358@sentry.io/842726', { Raven.config('https://b2b1cfd9b043491eb6b566fd8acee358@sentry.io/842726', {
@ -369,7 +370,8 @@ if (environment.production) {
RecentlyOpenedProjectService, RecentlyOpenedProjectService,
ServerManagementService, ServerManagementService,
ConsoleService, ConsoleService,
DefaultConsoleService DefaultConsoleService,
NodeCreatedLabelStylesFixer
], ],
entryComponents: [ entryComponents: [
AddServerDialogComponent, AddServerDialogComponent,

View File

@ -1,24 +1,23 @@
import { NodeCreatedLabelStylesFixer } from "./node-created-label-styles-fixer"; import { NodeCreatedLabelStylesFixer } from "./node-created-label-styles-fixer";
import { MapNode } from '../../../cartography/models/map/map-node';
import { Node } from '../../../cartography/models/node'; import { Node } from '../../../cartography/models/node';
import { Label } from '../../../cartography/models/label'; import { Label } from '../../../cartography/models/label';
describe('NodeCreatedLabelStylesFixer', () => { describe('NodeCreatedLabelStylesFixer', () => {
let fixer: NodeCreatedLabelStylesFixer; let fixer: NodeCreatedLabelStylesFixer;
let nodeToMapNodeConverter; let calculator;
beforeEach(() => { beforeEach(() => {
nodeToMapNodeConverter = { calculator = {
convert: (node) => { calculate: (text, styles) => {
const n = new MapNode(); return {
n.width = node.width; width: 50,
n.height = node.height; height: 10
return n; };
} }
}; };
fixer = new NodeCreatedLabelStylesFixer( fixer = new NodeCreatedLabelStylesFixer(
nodeToMapNodeConverter calculator
); );
}); });
@ -32,5 +31,7 @@ describe('NodeCreatedLabelStylesFixer', () => {
const fixed = fixer.fix(node); const fixed = fixer.fix(node);
expect(fixed.label.style).toEqual('font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;'); expect(fixed.label.style).toEqual('font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;');
expect(fixed.label.x).toEqual(25);
expect(fixed.label.y).toEqual(-18);
}); });
}); });

View File

@ -1,16 +1,24 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Node } from '../../../cartography/models/node'; import { Node } from '../../../cartography/models/node';
import { NodeToMapNodeConverter } from '../../../cartography/converters/map/node-to-map-node-converter'; import { FontBBoxCalculator } from '../../../cartography/helpers/font-bbox-calculator';
@Injectable() @Injectable()
export class NodeCreatedLabelStylesFixer { export class NodeCreatedLabelStylesFixer {
MARGIN_BETWEEN_NODE_AND_LABEL = 8;
constructor( constructor(
private nodeToMapNodeConverter: NodeToMapNodeConverter private fontBBCalculator: FontBBoxCalculator
) {} ) {}
fix(node: Node): Node { fix(node: Node): Node {
const mapNode = this.nodeToMapNodeConverter.convert(node); node.label.style = 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;';
const bb = this.fontBBCalculator.calculate(node.label.text, node.label.style);
// center label
node.label.x = node.width / 2 - bb.width / 2;
// move above the node
node.label.y = -bb.height - this.MARGIN_BETWEEN_NODE_AND_LABEL;
return node; return node;
} }
} }

View File

@ -42,6 +42,7 @@ import { Link } from '../../models/link';
import { Project } from '../../models/project'; import { Project } from '../../models/project';
import { CapturingSettings } from '../../models/capturingSettings'; import { CapturingSettings } from '../../models/capturingSettings';
import { LinkWidget } from '../../cartography/widgets/link'; import { LinkWidget } from '../../cartography/widgets/link';
import { NodeCreatedLabelStylesFixer } from './helpers/node-created-label-styles-fixer';
export class MockedProgressService { export class MockedProgressService {
public activate() {} public activate() {}
@ -182,8 +183,13 @@ describe('ProjectMapComponent', () => {
let drawingsDataSource = new MockedDrawingsDataSource(); let drawingsDataSource = new MockedDrawingsDataSource();
let nodesDataSource = new MockedNodesDataSource(); let nodesDataSource = new MockedNodesDataSource();
let linksDataSource = new MockedLinksDataSource(); let linksDataSource = new MockedLinksDataSource();
let nodeCreatedLabelStylesFixer;
beforeEach(async(() => { beforeEach(async(() => {
nodeCreatedLabelStylesFixer = {
fix: (node) => node
};
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [MatIconModule, MatToolbarModule, MatMenuModule, MatCheckboxModule, CommonModule, NoopAnimationsModule], imports: [MatIconModule, MatToolbarModule, MatMenuModule, MatCheckboxModule, CommonModule, NoopAnimationsModule],
providers: [ providers: [
@ -214,6 +220,7 @@ describe('ProjectMapComponent', () => {
provide: RecentlyOpenedProjectService, provide: RecentlyOpenedProjectService,
useClass: RecentlyOpenedProjectService useClass: RecentlyOpenedProjectService
}, },
{ provide: NodeCreatedLabelStylesFixer, useValue: nodeCreatedLabelStylesFixer}
], ],
declarations: [ProjectMapComponent, D3MapComponent, ...ANGULAR_MAP_DECLARATIONS], declarations: [ProjectMapComponent, D3MapComponent, ...ANGULAR_MAP_DECLARATIONS],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]

View File

@ -279,6 +279,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
nodes.filter((node) => node.label.style === null).forEach((node) => { nodes.filter((node) => node.label.style === null).forEach((node) => {
const fixedNode = this.nodeCreatedLabelStylesFixer.fix(node); const fixedNode = this.nodeCreatedLabelStylesFixer.fix(node);
this.nodeService.updateLabel(this.server, node, fixedNode.label).subscribe();
}); });
this.nodesDataSource.set(nodes); this.nodesDataSource.set(nodes);