ngx-svg-graphics 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/ng-package.json +7 -0
- package/package.json +23 -0
- package/src/lib/components/arrows/arrow-between-boxes/arrow-between-boxes.component.css +0 -0
- package/src/lib/components/arrows/arrow-between-boxes/arrow-between-boxes.component.spec.ts +32 -0
- package/src/lib/components/arrows/arrow-between-boxes/arrow-between-boxes.component.svg +12 -0
- package/src/lib/components/arrows/arrow-between-boxes/arrow-between-boxes.component.ts +61 -0
- package/src/lib/components/arrows/arrow-between-elems/arrow-between-elems.component.css +0 -0
- package/src/lib/components/arrows/arrow-between-elems/arrow-between-elems.component.spec.ts +23 -0
- package/src/lib/components/arrows/arrow-between-elems/arrow-between-elems.component.svg +9 -0
- package/src/lib/components/arrows/arrow-between-elems/arrow-between-elems.component.ts +97 -0
- package/src/lib/components/arrows/arrow-between-points/arrow-between-points.component.css +0 -0
- package/src/lib/components/arrows/arrow-between-points/arrow-between-points.component.spec.ts +23 -0
- package/src/lib/components/arrows/arrow-between-points/arrow-between-points.component.svg +15 -0
- package/src/lib/components/arrows/arrow-between-points/arrow-between-points.component.ts +39 -0
- package/src/lib/components/draggable/draggable.component.css +0 -0
- package/src/lib/components/draggable/draggable.component.spec.ts +33 -0
- package/src/lib/components/draggable/draggable.component.svg +9 -0
- package/src/lib/components/draggable/draggable.component.ts +49 -0
- package/src/lib/models/arrow-style-configuration.ts +8 -0
- package/src/lib/models/bounding-box.ts +7 -0
- package/src/lib/models/dragger.spec.ts +9 -0
- package/src/lib/models/dragger.ts +57 -0
- package/src/lib/models/identifiable.ts +3 -0
- package/src/lib/models/positionable.ts +8 -0
- package/src/lib/services/arrow-style-configuration.service.spec.ts +16 -0
- package/src/lib/services/arrow-style-configuration.service.ts +25 -0
- package/src/lib/services/svg-access.service.spec.ts +16 -0
- package/src/lib/services/svg-access.service.ts +37 -0
- package/src/lib/utils/path-layouter.spec.ts +7 -0
- package/src/lib/utils/path-layouter.ts +41 -0
- package/src/lib/utils/position-helper.spec.ts +7 -0
- package/src/lib/utils/position-helper.ts +32 -0
- package/src/public-api.ts +18 -0
- package/tsconfig.lib.json +15 -0
- package/tsconfig.lib.prod.json +11 -0
- package/tsconfig.spec.json +15 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Arrows
|
|
2
|
+
|
|
3
|
+
This project was generated using [Angular CLI](https://github.com/angular/angular-cli) version 19.1.0.
|
|
4
|
+
|
|
5
|
+
## Code scaffolding
|
|
6
|
+
|
|
7
|
+
Angular CLI includes powerful code scaffolding tools. To generate a new component, run:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
ng generate component component-name
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
ng generate --help
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Building
|
|
20
|
+
|
|
21
|
+
To build the library, run:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
ng build arrows
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This command will compile your project, and the build artifacts will be placed in the `dist/` directory.
|
|
28
|
+
|
|
29
|
+
### Publishing the Library
|
|
30
|
+
|
|
31
|
+
Once the project is built, you can publish your library by following these steps:
|
|
32
|
+
|
|
33
|
+
1. Navigate to the `dist` directory:
|
|
34
|
+
```bash
|
|
35
|
+
cd dist/arrows
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
2. Run the `npm publish` command to publish your library to the npm registry:
|
|
39
|
+
```bash
|
|
40
|
+
npm publish
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Running unit tests
|
|
44
|
+
|
|
45
|
+
To execute unit tests with the [Karma](https://karma-runner.github.io) test runner, use the following command:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
ng test
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Running end-to-end tests
|
|
52
|
+
|
|
53
|
+
For end-to-end (e2e) testing, run:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
ng e2e
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Angular CLI does not come with an end-to-end testing framework by default. You can choose one that suits your needs.
|
|
60
|
+
|
|
61
|
+
## Additional Resources
|
|
62
|
+
|
|
63
|
+
For more information on using the Angular CLI, including detailed command references, visit the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
|
package/ng-package.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ngx-svg-graphics",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Small svg library to link components or svg elements with arrows and allow to drag components.",
|
|
5
|
+
"keywords": ["Drag","Arrows", "SVG", "Angular", "EMF"],
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Susanne Göbel",
|
|
8
|
+
"email": "goebel@uni-koblenz.de"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/softlang/EMFular/tree/main/projects/arrows"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@angular/common": "^19.2.4",
|
|
16
|
+
"@angular/core": "^19.2.4",
|
|
17
|
+
"@types/uuid": "^10.0.0"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"tslib": "^2.3.0"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false
|
|
23
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import {ArrowBetweenBoxesComponent} from './arrow-between-boxes.component';
|
|
4
|
+
import {NO_ERRORS_SCHEMA} from "@angular/core";
|
|
5
|
+
import {BoundingBox} from "../../../models/bounding-box";
|
|
6
|
+
|
|
7
|
+
describe('ArrowBetweenBoxesComponent', () => {
|
|
8
|
+
let component: ArrowBetweenBoxesComponent;
|
|
9
|
+
let fixture: ComponentFixture<ArrowBetweenBoxesComponent>;
|
|
10
|
+
|
|
11
|
+
beforeEach(async () => {
|
|
12
|
+
await TestBed.configureTestingModule({
|
|
13
|
+
imports: [ArrowBetweenBoxesComponent],
|
|
14
|
+
schemas: [NO_ERRORS_SCHEMA],
|
|
15
|
+
})
|
|
16
|
+
.compileComponents();
|
|
17
|
+
|
|
18
|
+
let start: BoundingBox = {x: 50, y: -20, w: 200, h: 80}
|
|
19
|
+
let end: BoundingBox = {x: 80, y: 20, w: 100, h: 200}
|
|
20
|
+
|
|
21
|
+
fixture = TestBed.createComponent(ArrowBetweenBoxesComponent);
|
|
22
|
+
component = fixture.componentInstance;
|
|
23
|
+
component.start = start
|
|
24
|
+
component.end = end
|
|
25
|
+
fixture.detectChanges();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should create', () => {
|
|
29
|
+
expect(component).toBeTruthy();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { AfterViewInit, ChangeDetectorRef, Component, Input, OnChanges,} from '@angular/core';
|
|
2
|
+
import { Point } from "@angular/cdk/drag-drop";
|
|
3
|
+
import {v4 as uuidv4} from "uuid";
|
|
4
|
+
import {BoundingBox} from "../../../models/bounding-box";
|
|
5
|
+
import {PathLayouter} from "../../../utils/path-layouter";
|
|
6
|
+
import {ArrowBetweenPointsComponent} from "../arrow-between-points/arrow-between-points.component";
|
|
7
|
+
|
|
8
|
+
@Component({
|
|
9
|
+
selector: '[arrow-between-boxes]',
|
|
10
|
+
templateUrl: './arrow-between-boxes.component.svg',
|
|
11
|
+
styleUrl: './arrow-between-boxes.component.css',
|
|
12
|
+
standalone: true,
|
|
13
|
+
imports: [ArrowBetweenPointsComponent]
|
|
14
|
+
})
|
|
15
|
+
export class ArrowBetweenBoxesComponent implements OnChanges, AfterViewInit {
|
|
16
|
+
@Input() start!: BoundingBox;
|
|
17
|
+
@Input() end!: BoundingBox;
|
|
18
|
+
@Input() arrowType?: string;
|
|
19
|
+
@Input() text?: string;
|
|
20
|
+
@Input() style?: string;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
x1: number = 0;
|
|
24
|
+
y1: number = 0;
|
|
25
|
+
x2: number = 5;
|
|
26
|
+
y2: number = 5;
|
|
27
|
+
|
|
28
|
+
id = uuidv4();
|
|
29
|
+
|
|
30
|
+
positioned= false;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
private cdr: ChangeDetectorRef,
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
ngAfterViewInit() {
|
|
37
|
+
this.computePositions()
|
|
38
|
+
this.positioned = true;
|
|
39
|
+
this.cdr.detectChanges();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
ngOnChanges() {
|
|
43
|
+
if(this.positioned) {
|
|
44
|
+
this.computePositions()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
private computePositions() {
|
|
50
|
+
let res = PathLayouter.bestPoints(this.start, this.end);
|
|
51
|
+
this.applyBestPoints(res)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private applyBestPoints(res: Point[]) {
|
|
55
|
+
this.x1 = res[0].x;
|
|
56
|
+
this.y1 = res[0].y;
|
|
57
|
+
this.x2 = res[1].x;
|
|
58
|
+
this.y2 = res[1].y;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { ArrowBetweenElemsComponent } from './arrow-between-elems.component';
|
|
4
|
+
|
|
5
|
+
describe('ArrowBetweenElemsComponent', () => {
|
|
6
|
+
let component: ArrowBetweenElemsComponent;
|
|
7
|
+
let fixture: ComponentFixture<ArrowBetweenElemsComponent>;
|
|
8
|
+
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
await TestBed.configureTestingModule({
|
|
11
|
+
imports: [ArrowBetweenElemsComponent]
|
|
12
|
+
})
|
|
13
|
+
.compileComponents();
|
|
14
|
+
|
|
15
|
+
fixture = TestBed.createComponent(ArrowBetweenElemsComponent);
|
|
16
|
+
component = fixture.componentInstance;
|
|
17
|
+
fixture.detectChanges();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should create', () => {
|
|
21
|
+
expect(component).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AfterViewInit,
|
|
3
|
+
ChangeDetectorRef,
|
|
4
|
+
Component, ElementRef,
|
|
5
|
+
Input, OnChanges, OnDestroy,
|
|
6
|
+
OnInit, SimpleChanges,
|
|
7
|
+
ViewChild
|
|
8
|
+
} from '@angular/core';
|
|
9
|
+
import {Observable, Subscription} from "rxjs";
|
|
10
|
+
import { NgIf } from '@angular/common';
|
|
11
|
+
import {BoundingBox} from "../../../models/bounding-box";
|
|
12
|
+
import {ArrowBetweenBoxesComponent} from "../arrow-between-boxes/arrow-between-boxes.component";
|
|
13
|
+
import {SVGAccessService} from "../../../services/svg-access.service";
|
|
14
|
+
|
|
15
|
+
@Component({
|
|
16
|
+
selector: '[arrowElems]',
|
|
17
|
+
templateUrl: './arrow-between-elems.component.svg',
|
|
18
|
+
styleUrl: './arrow-between-elems.component.css',
|
|
19
|
+
standalone: true,
|
|
20
|
+
imports: [NgIf, ArrowBetweenBoxesComponent]
|
|
21
|
+
})
|
|
22
|
+
export class ArrowBetweenElemsComponent implements OnInit, AfterViewInit, OnChanges, OnDestroy {
|
|
23
|
+
|
|
24
|
+
@Input() startGID!: string;
|
|
25
|
+
@Input() startSuffix!: string;
|
|
26
|
+
@Input() endGID!: string;
|
|
27
|
+
@Input() endSuffix!: string;
|
|
28
|
+
@Input() arrowType?: string;
|
|
29
|
+
|
|
30
|
+
@Input() breaks: BoundingBox[] = [];
|
|
31
|
+
@Input() text?: string;
|
|
32
|
+
@Input() style?: string; //todo move into ArrowStyleConfig?
|
|
33
|
+
|
|
34
|
+
startId!: string;
|
|
35
|
+
endId!: string;
|
|
36
|
+
|
|
37
|
+
start?: BoundingBox;
|
|
38
|
+
end?: BoundingBox;
|
|
39
|
+
|
|
40
|
+
positioned= false;
|
|
41
|
+
@ViewChild('arrow') node!: ElementRef<SVGGraphicsElement>;
|
|
42
|
+
|
|
43
|
+
changeNotifier: Observable<string>;
|
|
44
|
+
changeSubscription: Subscription;
|
|
45
|
+
|
|
46
|
+
//idea: compute the two input positions as relative to the current elem
|
|
47
|
+
constructor(
|
|
48
|
+
private svgAccessService: SVGAccessService,
|
|
49
|
+
private cdr: ChangeDetectorRef) {
|
|
50
|
+
this.changeNotifier = this.svgAccessService.listenToPositionChange()
|
|
51
|
+
this.changeSubscription = this.changeNotifier.subscribe(nextString => {
|
|
52
|
+
if (nextString == this.startGID || nextString == this.endGID) {
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
this.computePositionsByIds()
|
|
55
|
+
this.cdr.detectChanges()
|
|
56
|
+
}, 0)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
ngOnInit() {
|
|
62
|
+
this.startId = this.startGID+this.startSuffix;
|
|
63
|
+
this.endId = this.endGID+this.endSuffix;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
ngOnChanges(_: SimpleChanges) {
|
|
67
|
+
this.startId = this.startGID+this.startSuffix;
|
|
68
|
+
this.endId = this.endGID+this.endSuffix;
|
|
69
|
+
this.computePositionsByIds()
|
|
70
|
+
this.cdr.detectChanges()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
ngAfterViewInit() {
|
|
74
|
+
this.positioned = true;
|
|
75
|
+
this.computePositionsByIds()
|
|
76
|
+
this.cdr.detectChanges()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private computePositionsByIds() {
|
|
80
|
+
if (this.node?.nativeElement){
|
|
81
|
+
let rel = this.node.nativeElement as SVGGraphicsElement
|
|
82
|
+
let startOpt = this.svgAccessService.getRelativePosition(this.startId, rel)
|
|
83
|
+
if (startOpt) {
|
|
84
|
+
this.start = startOpt
|
|
85
|
+
}
|
|
86
|
+
let endOpt = this.svgAccessService.getRelativePosition(this.endId, rel)
|
|
87
|
+
if (endOpt) {
|
|
88
|
+
this.end = endOpt
|
|
89
|
+
}
|
|
90
|
+
} else console.log('No native element yet')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
ngOnDestroy() {
|
|
94
|
+
this.changeSubscription.unsubscribe();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { ArrowBetweenPointsComponent } from './arrow-between-points.component';
|
|
4
|
+
|
|
5
|
+
describe('ArrowBetweenPointsComponent', () => {
|
|
6
|
+
let component: ArrowBetweenPointsComponent;
|
|
7
|
+
let fixture: ComponentFixture<ArrowBetweenPointsComponent>;
|
|
8
|
+
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
await TestBed.configureTestingModule({
|
|
11
|
+
imports: [ArrowBetweenPointsComponent]
|
|
12
|
+
})
|
|
13
|
+
.compileComponents();
|
|
14
|
+
|
|
15
|
+
fixture = TestBed.createComponent(ArrowBetweenPointsComponent);
|
|
16
|
+
component = fixture.componentInstance;
|
|
17
|
+
fixture.detectChanges();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should create', () => {
|
|
21
|
+
expect(component).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<svg:g>
|
|
2
|
+
<path [attr.id]="id+'-path'"
|
|
3
|
+
[attr.d]="'M '+startX+','+startY+' L '+endX+','+endY"
|
|
4
|
+
[attr.stroke]="arrowStyleConfiguration.color"
|
|
5
|
+
[attr.stroke-dasharray]="arrowStyleConfiguration.dashed"
|
|
6
|
+
[attr.marker-start]='"url(#"+arrowStyleConfiguration.startPointer+")"'
|
|
7
|
+
[attr.marker-end]='"url(#"+arrowStyleConfiguration.endPointer+")"'
|
|
8
|
+
style="{{style}}">
|
|
9
|
+
</path>
|
|
10
|
+
<text *ngIf="text">
|
|
11
|
+
<textPath [attr.href]="'#'+id+'-path'">
|
|
12
|
+
{{text}}
|
|
13
|
+
</textPath>
|
|
14
|
+
</text>
|
|
15
|
+
</svg:g>
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {Component, Input, OnChanges} from '@angular/core';
|
|
2
|
+
import {NgIf} from "@angular/common";
|
|
3
|
+
import {ArrowStyleConfigurationService} from "../../../services/arrow-style-configuration.service";
|
|
4
|
+
import {ArrowStyleConfiguration} from "../../../models/arrow-style-configuration";
|
|
5
|
+
import {v4 as uuidv4} from "uuid";
|
|
6
|
+
|
|
7
|
+
@Component({
|
|
8
|
+
selector: '[arrow-between-points]',
|
|
9
|
+
imports: [
|
|
10
|
+
NgIf
|
|
11
|
+
],
|
|
12
|
+
templateUrl: './arrow-between-points.component.svg',
|
|
13
|
+
styleUrl: './arrow-between-points.component.css'
|
|
14
|
+
})
|
|
15
|
+
export class ArrowBetweenPointsComponent implements OnChanges {
|
|
16
|
+
|
|
17
|
+
@Input() startX!: number;
|
|
18
|
+
@Input() startY!: number;
|
|
19
|
+
@Input() endX!: number;
|
|
20
|
+
@Input() endY!: number;
|
|
21
|
+
@Input() text?: string;
|
|
22
|
+
@Input() style?: string;
|
|
23
|
+
|
|
24
|
+
@Input() arrowType?: string;
|
|
25
|
+
|
|
26
|
+
arrowStyleConfiguration: ArrowStyleConfiguration;
|
|
27
|
+
id = uuidv4();
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
private arrowStyleConfigService: ArrowStyleConfigurationService,) {
|
|
31
|
+
|
|
32
|
+
this.arrowStyleConfiguration = this.arrowStyleConfigService.styleArrow()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
ngOnChanges() {
|
|
36
|
+
this.arrowStyleConfiguration = this.arrowStyleConfigService.styleArrow(this.arrowType)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { DraggableComponent } from './draggable.component';
|
|
4
|
+
import {Draggable} from "../../models/positionable";
|
|
5
|
+
import {SVGAccessService} from "../../services/svg-access.service";
|
|
6
|
+
|
|
7
|
+
describe('DraggableComponent', () => {
|
|
8
|
+
let example = {gId: "id", position: { x: 1, y: 2, w: 2, h: 1 }};
|
|
9
|
+
let component: DraggableComponent<Draggable>;
|
|
10
|
+
let fixture: ComponentFixture<DraggableComponent<Draggable>>;
|
|
11
|
+
|
|
12
|
+
class DraggableTestComponent<T extends Draggable> extends DraggableComponent<T> {
|
|
13
|
+
constructor(svgAccessService: SVGAccessService) {
|
|
14
|
+
super(svgAccessService);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
await TestBed.configureTestingModule({
|
|
20
|
+
imports: [DraggableComponent]
|
|
21
|
+
})
|
|
22
|
+
.compileComponents();
|
|
23
|
+
|
|
24
|
+
fixture = TestBed.createComponent(DraggableTestComponent<Draggable>);
|
|
25
|
+
component = fixture.componentInstance;
|
|
26
|
+
component.elem = example;
|
|
27
|
+
fixture.detectChanges();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should create', () => {
|
|
31
|
+
expect(component).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {AfterViewInit, Component, EventEmitter, Output} from '@angular/core';
|
|
2
|
+
import {SVGAccessService} from '../../services/svg-access.service';
|
|
3
|
+
import { Draggable } from '../../models/positionable';
|
|
4
|
+
import {Dragger} from "../../models/dragger";
|
|
5
|
+
|
|
6
|
+
@Component({
|
|
7
|
+
imports: [],
|
|
8
|
+
selector: '[draggable]',
|
|
9
|
+
templateUrl: './draggable.component.svg',
|
|
10
|
+
styleUrl: './draggable.component.css'
|
|
11
|
+
})
|
|
12
|
+
export abstract class DraggableComponent<T extends Draggable> implements AfterViewInit {
|
|
13
|
+
|
|
14
|
+
@Output() chooseElem = new EventEmitter<T>();
|
|
15
|
+
//the caller must initialize both required elements (elem and elementDragger) either in the constructor
|
|
16
|
+
// (or if they are inputs) in the ngOnInit life cycle hook
|
|
17
|
+
elem!: T;
|
|
18
|
+
elemDragger!: Dragger<T>;
|
|
19
|
+
|
|
20
|
+
protected constructor(
|
|
21
|
+
protected svgAccessService: SVGAccessService
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
ngAfterViewInit() {
|
|
26
|
+
this.svgAccessService.notifyPositionChange(this.elem.gId)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
startDrag(event: MouseEvent) {
|
|
30
|
+
this.elemDragger.startDrag(event);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
drag(event: MouseEvent) {
|
|
34
|
+
if (this.elemDragger.drag(event)) {
|
|
35
|
+
this.svgAccessService.notifyPositionChange(this.elem.gId)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
endDrag(event: MouseEvent) {
|
|
40
|
+
this.elemDragger.endDrag(event);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
clickElem(event: MouseEvent) {
|
|
44
|
+
if (this.elemDragger.clickElem(event)) {
|
|
45
|
+
this.chooseElem.emit(this.elem);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {Positionable} from './positionable';
|
|
2
|
+
|
|
3
|
+
export class Dragger<T extends Positionable> {
|
|
4
|
+
elem: T;
|
|
5
|
+
|
|
6
|
+
dragActive = false;
|
|
7
|
+
wasReallyDragged = false;
|
|
8
|
+
dragStartX: number = 0;
|
|
9
|
+
dragStartY: number = 0;
|
|
10
|
+
|
|
11
|
+
constructor(private element: T) {
|
|
12
|
+
this.elem = element;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
startDrag(event: MouseEvent) {
|
|
16
|
+
console.log('startDrag');
|
|
17
|
+
this.dragActive = true;
|
|
18
|
+
this.dragStartX = event.clientX;
|
|
19
|
+
this.dragStartY = event.clientY;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// returns true in the case of a real drag event, false otherwise
|
|
23
|
+
drag(event: MouseEvent): boolean {
|
|
24
|
+
if (this.dragActive) {
|
|
25
|
+
this.wasReallyDragged = true;
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
const dragX = event.clientX;
|
|
28
|
+
this.elem.position.x+= (dragX - this.dragStartX);
|
|
29
|
+
this.dragStartX = dragX;
|
|
30
|
+
const dragY = event.clientY;
|
|
31
|
+
this.elem.position.y+= (dragY - this.dragStartY);
|
|
32
|
+
this.dragStartY = dragY;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
endDrag(event: MouseEvent) {
|
|
39
|
+
console.log('endDrag');
|
|
40
|
+
this.dragActive = false;
|
|
41
|
+
// todo was working for click vs drag, not now setTimeout(() => {this.dragActive = false;}, 50);
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//returns true if the click should be treated as click, false if it was from drag
|
|
46
|
+
clickElem(event: MouseEvent): boolean {
|
|
47
|
+
if (this.wasReallyDragged) {
|
|
48
|
+
this.wasReallyDragged = false;
|
|
49
|
+
return false;
|
|
50
|
+
} else {
|
|
51
|
+
event.preventDefault();
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { ArrowStyleConfigurationService } from './arrow-style-configuration.service';
|
|
4
|
+
|
|
5
|
+
describe('ArrowStyleConfigurationService', () => {
|
|
6
|
+
let service: ArrowStyleConfigurationService;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
TestBed.configureTestingModule({});
|
|
10
|
+
service = TestBed.inject(ArrowStyleConfigurationService);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should be created', () => {
|
|
14
|
+
expect(service).toBeTruthy();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import {ArrowStyleConfiguration} from "../models/arrow-style-configuration";
|
|
3
|
+
|
|
4
|
+
/*****
|
|
5
|
+
default implementation
|
|
6
|
+
You can extend this base case with an own service implementation.
|
|
7
|
+
Make sure to configure DI to use your service instead of this one
|
|
8
|
+
In 20205, the way to do so is to declare it as provider in app.config.ts or app.component.ts
|
|
9
|
+
providers: [{ provide: ArrowStyleConfigurationService, useClass: YourServiceImplementation }],
|
|
10
|
+
See https://angular.dev/guide/di/dependency-injection-providers
|
|
11
|
+
***/
|
|
12
|
+
@Injectable({
|
|
13
|
+
providedIn: 'root'
|
|
14
|
+
})
|
|
15
|
+
export class ArrowStyleConfigurationService {
|
|
16
|
+
|
|
17
|
+
constructor() { }
|
|
18
|
+
|
|
19
|
+
styleArrow(arrowType?: string): ArrowStyleConfiguration {
|
|
20
|
+
return {
|
|
21
|
+
color: 'black',
|
|
22
|
+
dashed: [0]
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { SVGAccessService } from './svg-access.service';
|
|
4
|
+
|
|
5
|
+
describe('SVGAccessService', () => {
|
|
6
|
+
let service: SVGAccessService;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
TestBed.configureTestingModule({});
|
|
10
|
+
service = TestBed.inject(SVGAccessService);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should be created', () => {
|
|
14
|
+
expect(service).toBeTruthy();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import {BehaviorSubject, Observable} from "rxjs";
|
|
3
|
+
import {BoundingBox} from "../models/bounding-box";
|
|
4
|
+
import {PositionHelper} from "../utils/position-helper";
|
|
5
|
+
|
|
6
|
+
@Injectable({
|
|
7
|
+
providedIn: 'root'
|
|
8
|
+
})
|
|
9
|
+
export class SVGAccessService {
|
|
10
|
+
|
|
11
|
+
positionChange: BehaviorSubject<string> = new BehaviorSubject<string>('');
|
|
12
|
+
|
|
13
|
+
constructor() { }
|
|
14
|
+
|
|
15
|
+
notifyPositionChange(id: string) {
|
|
16
|
+
this.positionChange.next(id);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
listenToPositionChange(): Observable<string> {
|
|
20
|
+
return this.positionChange.asObservable()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getElemById(id: string): SVGGraphicsElement | undefined {
|
|
24
|
+
let elem = document.getElementById(id)
|
|
25
|
+
return elem as unknown as SVGGraphicsElement
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getRelativePosition(id: string, node: SVGGraphicsElement): BoundingBox | undefined {
|
|
29
|
+
let elem = this.getElemById(id)
|
|
30
|
+
if (elem) {
|
|
31
|
+
let abs = PositionHelper.absolutePosition(elem)
|
|
32
|
+
PositionHelper.makeRelativeToElem(abs, node)
|
|
33
|
+
return abs;
|
|
34
|
+
}
|
|
35
|
+
return undefined
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {BoundingBox} from "../models/bounding-box";
|
|
2
|
+
import {Point} from "@angular/cdk/drag-drop";
|
|
3
|
+
|
|
4
|
+
export class PathLayouter {
|
|
5
|
+
|
|
6
|
+
static bestPoints(p1: BoundingBox, p2: BoundingBox) {
|
|
7
|
+
//assumption is that both boxes do not intersect
|
|
8
|
+
//since we only deal with bounding boxes, all relevant lines are parallel/orthogonal
|
|
9
|
+
|
|
10
|
+
let xPoints = this.getPointsInOneDimension(p1.x, p1.w, p2.x, p2.w)
|
|
11
|
+
let yPoints = this.getPointsInOneDimension(p1.y, p1.h, p2.y, p2.h)
|
|
12
|
+
let result1: Point = {x: xPoints[0], y: yPoints[0]}
|
|
13
|
+
let result2: Point = {x: xPoints[1], y: yPoints[1]}
|
|
14
|
+
return [result1, result2];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// assumes that x1 is smaller than x2
|
|
18
|
+
//works for width (careful with height, y axis is wrong way)
|
|
19
|
+
static determineOverlapX(x1: number, w1: number, x2: number, w2: number): number[] {
|
|
20
|
+
let x1right = x1+w1
|
|
21
|
+
if (x1right >= x2) {
|
|
22
|
+
//find end of overlap
|
|
23
|
+
let x2right = x2+w2
|
|
24
|
+
let end = Math.min(x1right, x2right)
|
|
25
|
+
//overlap interval is [x2, end]
|
|
26
|
+
let middle = (x2+end)/2
|
|
27
|
+
return [middle, middle]
|
|
28
|
+
} else return [x1right, x2]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
static getPointsInOneDimension(x1: number, w1: number, x2: number, w2: number): number[] {
|
|
33
|
+
//determine overlap (and then middle of it) or closest points in one dimension
|
|
34
|
+
if (x1 < x2) {
|
|
35
|
+
return this.determineOverlapX(x1, w1, x2, w2)
|
|
36
|
+
} else {
|
|
37
|
+
return this.determineOverlapX(x2, w2, x1, w1).reverse()
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {Point} from "@angular/cdk/drag-drop";
|
|
2
|
+
import {BoundingBox} from "../models/bounding-box";
|
|
3
|
+
|
|
4
|
+
export class PositionHelper {
|
|
5
|
+
|
|
6
|
+
static absolutePosition(elem: SVGGraphicsElement): BoundingBox {
|
|
7
|
+
let relativePosition: DOMRect = elem.getBBox();
|
|
8
|
+
let translationMatrix: DOMMatrix = elem.getCTM()!;
|
|
9
|
+
let x = relativePosition.x;
|
|
10
|
+
let y = relativePosition.y;
|
|
11
|
+
let x_abs = translationMatrix.a*x+translationMatrix.c*y+translationMatrix.e;
|
|
12
|
+
let y_abs = translationMatrix.b*x+translationMatrix.d*y+translationMatrix.f;
|
|
13
|
+
return {x: x_abs, y: y_abs, w: relativePosition.width, h: relativePosition.height}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static makeRelativeToElem(p: Point, elem: SVGGraphicsElement): void {
|
|
17
|
+
this.matrixTransform(p, elem.getCTM()!.inverse())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static matrixTransform(p: Point, translationMatrix: DOMMatrix): void {
|
|
21
|
+
let x = p.x;
|
|
22
|
+
let y = p.y;
|
|
23
|
+
let x_trans = translationMatrix.a*x+translationMatrix.c*y+translationMatrix.e;
|
|
24
|
+
let y_trans = translationMatrix.b*x+translationMatrix.d*y+translationMatrix.f;
|
|
25
|
+
p.x = x_trans
|
|
26
|
+
p.y = y_trans
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static newBoundingBox(x: number = 0, y: number = 0, width: number = 5, height: number = 5): BoundingBox {
|
|
30
|
+
return {x: x, y: y, w: width, h: height};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Public API Surface of arrows
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from './lib/models/dragger'
|
|
6
|
+
export * from './lib/components/draggable/draggable.component';
|
|
7
|
+
export * from './lib/components/arrows/arrow-between-elems/arrow-between-elems.component';
|
|
8
|
+
export * from './lib/components/arrows/arrow-between-boxes/arrow-between-boxes.component';
|
|
9
|
+
export * from './lib/components/arrows/arrow-between-points/arrow-between-points.component';
|
|
10
|
+
export * from './lib/utils/position-helper';
|
|
11
|
+
export * from './lib/services/arrow-style-configuration.service';
|
|
12
|
+
export * from './lib/services/svg-access.service';
|
|
13
|
+
|
|
14
|
+
export type {BoundingBox} from './lib/models/bounding-box';
|
|
15
|
+
export type {ArrowStyleConfiguration} from './lib/models/arrow-style-configuration';
|
|
16
|
+
export type {Identifiable} from './lib/models/identifiable'; //gId: string - soft connection to Referencable
|
|
17
|
+
export type {Positionable, Draggable} from './lib/models/positionable';
|
|
18
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
|
2
|
+
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
|
3
|
+
{
|
|
4
|
+
"extends": "../../tsconfig.json",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"outDir": "../../out-tsc/lib",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"inlineSources": true,
|
|
10
|
+
"types": []
|
|
11
|
+
},
|
|
12
|
+
"exclude": [
|
|
13
|
+
"**/*.spec.ts"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
|
2
|
+
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
|
3
|
+
{
|
|
4
|
+
"extends": "./tsconfig.lib.json",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"declarationMap": false
|
|
7
|
+
},
|
|
8
|
+
"angularCompilerOptions": {
|
|
9
|
+
"compilationMode": "partial"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
|
2
|
+
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
|
3
|
+
{
|
|
4
|
+
"extends": "../../tsconfig.json",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"outDir": "../../out-tsc/spec",
|
|
7
|
+
"types": [
|
|
8
|
+
"jasmine"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
"include": [
|
|
12
|
+
"**/*.spec.ts",
|
|
13
|
+
"**/*.d.ts"
|
|
14
|
+
]
|
|
15
|
+
}
|