-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: implement dots background * refactor: move background to types * docs: add doc for dots pattern * fix: generate unique ID for pattern * feat: add description for interface * docs: add dots bg to main demo * refactor: remove zone.js * docs: add item * up version
- Loading branch information
1 parent
9b829d1
commit 062783b
Showing
18 changed files
with
227 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...categories/examples/pages/custom-background/demo/dots-custom-background-demo.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { Edge, Node, VflowModule } from 'projects/ngx-vflow-lib/src/public-api'; | ||
|
||
@Component({ | ||
template: `<vflow | ||
[nodes]="nodes" | ||
[edges]="edges" | ||
[background]="{ type: 'dots' }" | ||
/>`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [VflowModule] | ||
}) | ||
export class DotsCustomBackgroundDemoComponent { | ||
public nodes: Node[] = [ | ||
{ | ||
id: '1', | ||
point: { x: 10, y: 200 }, | ||
type: 'default', | ||
text: '1' | ||
}, | ||
{ | ||
id: '2', | ||
point: { x: 200, y: 100 }, | ||
type: 'default', | ||
text: '2' | ||
}, | ||
{ | ||
id: '3', | ||
point: { x: 200, y: 300 }, | ||
type: 'default', | ||
text: '3' | ||
}, | ||
] | ||
|
||
public edges: Edge[] = [ | ||
{ | ||
id: '1 -> 2', | ||
source: '1', | ||
target: '2' | ||
}, | ||
{ | ||
id: '1 -> 3', | ||
source: '1', | ||
target: '3' | ||
}, | ||
] | ||
} | ||
|
12 changes: 11 additions & 1 deletion
12
...cts/ngx-vflow-demo/src/app/categories/examples/pages/custom-background/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
# {{ NgDocPage.title }} | ||
|
||
You're able to select background color for your flow. Currently, it is limited to selecting a color, but in the future, it will be possible to set more complex backgrounds. To select a color, simply pass it to the `[background]` input. | ||
You're able to select background for your flow. | ||
|
||
## Solid color | ||
|
||
To select a color, simply pass a color string it to the `[background]` input. | ||
|
||
{{ NgDocActions.demo("CustomBackgroundDemoComponent", { expanded: true }) }} | ||
|
||
## Dots pattern | ||
|
||
To make dots pattern, pass an object to the `[background]` input according to `DotsBackground` interface | ||
|
||
{{ NgDocActions.demo("DotsCustomBackgroundDemoComponent", { expanded: true }) }} |
3 changes: 2 additions & 1 deletion
3
projects/ngx-vflow-demo/src/app/categories/examples/pages/custom-background/ng-doc.page.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
projects/ngx-vflow-lib/src/lib/vflow/components/background/background.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<ng-container *ngIf="backgroundSignal().type === 'dots'"> | ||
<svg:pattern | ||
[attr.id]="patternId" | ||
[attr.x]="x()" | ||
[attr.y]="y()" | ||
[attr.width]="scaledGap()" | ||
[attr.height]="scaledGap()" | ||
patternUnits="userSpaceOnUse" | ||
> | ||
<svg:circle | ||
[attr.cx]="patternSize()" | ||
[attr.cy]="patternSize()" | ||
[attr.r]="patternSize()" | ||
[attr.fill]="patternColor()" | ||
/> | ||
</svg:pattern> | ||
|
||
<svg:rect | ||
x="0" | ||
y="0" | ||
width="100%" | ||
height="100%" | ||
[attr.fill]="patternUrl" | ||
/> | ||
</ng-container> |
79 changes: 79 additions & 0 deletions
79
projects/ngx-vflow-lib/src/lib/vflow/components/background/background.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Component, Input, computed, effect, inject, signal } from '@angular/core'; | ||
import { Background } from '../../types/background.type'; | ||
import { ViewportService } from '../../services/viewport.service'; | ||
import { RootSvgReferenceDirective } from '../../directives/reference.directive'; | ||
import { id } from '../../utils/id'; | ||
|
||
const defaultBg = '#fff' | ||
const defaultGap = 20 | ||
const defaultDotSize = 2 | ||
const defaultDotColor = 'rgb(177, 177, 183)' | ||
|
||
@Component({ | ||
selector: 'g[background]', | ||
templateUrl: './background.component.html' | ||
}) | ||
export class BackgroundComponent { | ||
private viewportService = inject(ViewportService) | ||
private rootSvg = inject(RootSvgReferenceDirective).element | ||
|
||
@Input({ required: true, transform }) | ||
set background(value: Background) { | ||
this.backgroundSignal.set(value) | ||
} | ||
|
||
protected backgroundSignal = signal<Background>({ type: 'solid', color: defaultBg }) | ||
|
||
protected scaledGap = computed(() => { | ||
const background = this.backgroundSignal() | ||
|
||
if (background.type === 'dots') { | ||
const zoom = this.viewportService.readableViewport().zoom | ||
|
||
return zoom * (background.gap ?? defaultGap) | ||
} | ||
|
||
return 0 | ||
}) | ||
|
||
protected x = computed(() => this.viewportService.readableViewport().x % this.scaledGap()) | ||
|
||
protected y = computed(() => this.viewportService.readableViewport().y % this.scaledGap()) | ||
|
||
protected patternColor = computed(() => this.backgroundSignal().color ?? defaultDotColor) | ||
|
||
protected patternSize = computed(() => { | ||
const background = this.backgroundSignal() | ||
|
||
if (background.type === 'dots') { | ||
return (this.viewportService.readableViewport().zoom * (background.size ?? defaultDotSize)) / 2 | ||
} | ||
|
||
return 0 | ||
}) | ||
|
||
// Without ID there will be pattern collision for several flows on the page | ||
// Later pattern ID may be exposed to API | ||
protected patternId = id(); | ||
protected patternUrl = `url(#${this.patternId})` | ||
|
||
constructor() { | ||
effect(() => { | ||
const background = this.backgroundSignal() | ||
|
||
if (background.type === 'dots') { | ||
this.rootSvg.style.backgroundColor = background.backgroundColor ?? defaultBg | ||
} | ||
|
||
if (background.type === 'solid') { | ||
this.rootSvg.style.backgroundColor = background.color | ||
} | ||
}) | ||
} | ||
} | ||
|
||
function transform(background: Background | string): Background { | ||
return typeof background === 'string' | ||
? { type: 'solid', color: background } | ||
: background | ||
} |
2 changes: 1 addition & 1 deletion
2
projects/ngx-vflow-lib/src/lib/vflow/components/handle/handle.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
projects/ngx-vflow-lib/src/lib/vflow/types/background.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export type Background = ColorBackground | DotsBackground | ||
|
||
export interface ColorBackground { | ||
type: 'solid' | ||
color: string | ||
} | ||
|
||
export interface DotsBackground { | ||
type: 'dots' | ||
/** | ||
* Gap between dots | ||
*/ | ||
gap?: number | ||
|
||
/** | ||
* Color of the dot | ||
*/ | ||
color?: string | ||
|
||
/** | ||
* Diameter of the dot | ||
*/ | ||
size?: number | ||
|
||
/** | ||
* Color behind tha dot pattern | ||
*/ | ||
backgroundColor?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function id() { | ||
const randomLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26)); | ||
|
||
return randomLetter + Date.now(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters