Skip to content

Commit 2589600

Browse files
committed
chore: clean up
1 parent aad86cd commit 2589600

File tree

19 files changed

+180
-1392
lines changed

19 files changed

+180
-1392
lines changed

packages/canvas/SVG/Circle.ts

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,23 @@
1-
import { Property } from "@nativescript/core";
2-
import { SVGItem } from "./SVGItem";
3-
import { CanvasGradient, Path2D } from "../Canvas2D";
1+
import { Property } from '@nativescript/core';
2+
import { SVGItem } from './SVGItem';
3+
import { CanvasGradient, Path2D } from '../Canvas2D';
44

55
export const cxProperty = new Property<Circle, any>({
6-
name: 'cx'
6+
name: 'cx',
77
});
88

99
export const cyProperty = new Property<Circle, any>({
10-
name: 'cy'
10+
name: 'cy',
1111
});
1212

1313
export const rProperty = new Property<Circle, any>({
14-
name: 'r'
14+
name: 'r',
1515
});
1616

17-
1817
export class Circle extends SVGItem {
1918
cx: any;
2019
cy: any;
2120
r: any;
22-
23-
handleValues(canvas?) {
24-
let ctx: any;
25-
if (canvas) {
26-
ctx = canvas.getContext('2d');
27-
} else {
28-
ctx = this._canvas.getContext('2d') as any;
29-
}
30-
const circle: any = new Path2D();
31-
circle.arc(this.cx, this.cy, this.r, 0, 2 * Math.PI);
32-
ctx.save();
33-
ctx.lineWidth = this.strokeWidth;
34-
ctx.globalAlpha = this._realOpacity;
35-
let lastOpacity;
36-
if (this._doFillOpacity()) {
37-
lastOpacity = ctx.globalAlpha;
38-
ctx.globalAlpha = this._realFillOpacity;
39-
}
40-
if (this._doFill()) {
41-
if (this.fill !== undefined && this.fill !== 'none') {
42-
if (this.fill && this.fill.indexOf('url') > -1) {
43-
const fill = this._getViewById(this.fill);
44-
if (fill) {
45-
const style = fill._getFillOrStrokeStyle();
46-
if (style instanceof CanvasGradient) {
47-
if (typeof fill.gradientTransform === 'string' && fill.gradientTransform.indexOf('rotate')) {
48-
let rotation = parseInt(fill.gradientTransform.replace('rotate(', '').replace(')', ''));
49-
if (!isNaN(rotation)) {
50-
ctx.rotate(rotation);
51-
}
52-
}
53-
ctx.fillStyle = style;
54-
} else {
55-
ctx.fillStyle = ctx.createPattern(style, 'repeat');
56-
}
57-
}
58-
} else {
59-
ctx.fillStyle = this.fill;
60-
}
61-
}
62-
ctx.fill(circle);
63-
}
64-
if (lastOpacity !== undefined) {
65-
ctx.globalAlpha = lastOpacity;
66-
}
67-
68-
if (this._doStroke()) {
69-
if (this.stroke !== undefined && this.stroke !== 'none') {
70-
if (this.stroke && this.stroke.indexOf('url') > -1) {
71-
const stroke = this._getViewById(this.stroke);
72-
if (stroke) {
73-
const style = stroke._getFillOrStrokeStyle();
74-
if (stroke instanceof CanvasGradient) {
75-
ctx.strokeStyle = style;
76-
} else {
77-
ctx.strokeStyle = ctx.createPattern(style, 'repeat');
78-
}
79-
}
80-
} else {
81-
ctx.strokeStyle = this.stroke;
82-
}
83-
}
84-
ctx.stroke(circle);
85-
}
86-
ctx.restore();
87-
}
8821
}
8922

9023
cxProperty.register(Circle);

packages/canvas/SVG/Defs.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import {AddChildFromBuilder} from "@nativescript/core";
22
import {SVGItem} from "./SVGItem";
33

4-
export class Defs extends SVGItem implements AddChildFromBuilder {
5-
_views: any[];
6-
7-
constructor() {
8-
super();
9-
this._views = [];
10-
}
11-
12-
_addChildFromBuilder(name: string, value: any): void {
13-
value.parent = this;
14-
value._canvas = this._canvas;
15-
this._views.push(value);
16-
this._appendChild(value.id || value._domId, value);
17-
}
4+
export class Defs extends SVGItem {
185
}

packages/canvas/SVG/Ellipse.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@ export class Ellipse extends SVGItem {
2020
cy: any;
2121
rx: any;
2222
ry: any;
23-
24-
constructor() {
25-
super();
26-
this.stroke = 'transparent';
27-
}
28-
29-
handleValues(canvas?) {
30-
let ctx: any;
31-
if (canvas) {
32-
ctx = canvas.getContext('2d');
33-
} else {
34-
ctx = this._canvas.getContext('2d') as any;
35-
}
36-
const ellipse = new Path2D();
37-
ellipse.ellipse(this.cx, this.cy, this.rx, this.ry, 0, 0, 2 * Math.PI);
38-
ctx.save();
39-
if (this._doFill()) {
40-
ctx.fillStyle = this._realFill;
41-
ctx.stroke(ellipse);
42-
}
43-
44-
if (this._doStroke()) {
45-
ctx.strokeStyle = this._realStroke;
46-
ctx.fill(ellipse);
47-
}
48-
ctx.restore();
49-
}
5023
}
5124

5225
cxProperty.register(Ellipse);

packages/canvas/SVG/G.ts

Lines changed: 1 addition & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,176 +3,8 @@ import { Canvas } from "../Canvas";
33
import { Svg } from "./SVG";
44
import { SVGItem } from "./SVGItem";
55

6-
export class G extends SVGItem implements AddChildFromBuilder {
7-
_views: any[];
8-
_children: Map<string, View>;
9-
#viewPostion: Map<number, string>;
6+
export class G extends SVGItem {
107
transform: string;
118
x: any;
129
y: any;
13-
14-
constructor() {
15-
super();
16-
this.#viewPostion = new Map();
17-
this._views = [];
18-
this._children = new Map<string, View>();
19-
}
20-
21-
handleValues(canvas) {
22-
this._canvas = canvas;
23-
const ctx = this._canvas.getContext('2d') as any;
24-
ctx.save();
25-
if (typeof this.transform === 'string') {
26-
// TODO use regex
27-
const matrix = this.transform.replace('matrix(', '')
28-
.replace('(', '')
29-
.split(',')
30-
.map(value => {
31-
const val = parseFloat(value);
32-
if (isNaN(val)) {
33-
return 0;
34-
}
35-
return val;
36-
});
37-
ctx.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
38-
}
39-
40-
const updateColors = () => {
41-
if (this._doStroke()) {
42-
ctx.strokeStyle = this._realStroke;
43-
}
44-
if (this._doFill()) {
45-
if (this.fill === undefined) {
46-
// ctx.fillStyle = 'black';
47-
} else {
48-
ctx.fillStyle = this._realFill;
49-
}
50-
}
51-
};
52-
this._views.forEach(view => {
53-
if (typeof view.handleValues === 'function') {
54-
updateColors();
55-
ctx.globalAlpha = this.opacity;
56-
view.handleValues(this._canvas);
57-
} else if (view instanceof G) {
58-
view._views.forEach((view) => {
59-
updateColors();
60-
view.handleValues();
61-
});
62-
}
63-
});
64-
ctx.restore();
65-
}
66-
67-
_refresh() {
68-
if (this.parent && this.parent instanceof Svg) {
69-
this._views.forEach((view) => {
70-
if (typeof view.handleValues === 'function') {
71-
view.handleValues(this._canvas);
72-
}
73-
})
74-
}
75-
}
76-
_forceRedraw() {
77-
if (this.parent && this.parent instanceof Svg) {
78-
const ctx = this._canvas.getContext('2d');
79-
this._views.forEach((view) => {
80-
if (typeof view.handleValues === 'function') {
81-
view.handleValues(this._canvas);
82-
}
83-
});
84-
}
85-
}
86-
87-
eachChildView(callback: (child: View) => boolean) {
88-
this._children.forEach((view, key) => {
89-
callback(view);
90-
});
91-
}
92-
93-
94-
public _addChildFromBuilder(name: string, value: any) {
95-
if (value instanceof View) {
96-
this.addChild(value);
97-
}
98-
}
99-
100-
getChildrenCount(): number {
101-
return this._children.size;
102-
}
103-
104-
// overrides the base property.
105-
get _childrenCount(): number {
106-
return this._children.size;
107-
}
108-
109-
getChildAt(index: number): View {
110-
return this._views[index];
111-
}
112-
113-
getChildIndex(child: View): number {
114-
return this._views.indexOf(child);
115-
}
116-
117-
public getChildById(id: string) {
118-
return this._getViewById(id);
119-
}
120-
121-
public _registerLayoutChild(child: View) {
122-
//Overridden
123-
}
124-
125-
public _unregisterLayoutChild(child: View) {
126-
//Overridden
127-
}
128-
129-
public addChildren(...children: View[]) {
130-
children.forEach(child => {
131-
this._views.push(child);
132-
this._children.set(`${child.id || child._domId}`, child);
133-
this._addView(child);
134-
this._registerLayoutChild(child);
135-
});
136-
this._refresh();
137-
}
138-
139-
public addChild(child: View): void {
140-
this._views.push(child);
141-
this._children.set(`${child.id || child._domId}`, child);
142-
this._addView(child);
143-
this._registerLayoutChild(child);
144-
this._refresh();
145-
}
146-
147-
public insertChild(child: View, atIndex: number): void {
148-
this._views.splice(atIndex, 0, child);
149-
this._children.set(`${child.id || child._domId}`, child);
150-
this._addView(child, atIndex);
151-
this._registerLayoutChild(child);
152-
this._refresh();
153-
}
154-
155-
public removeChild(child: View): void {
156-
this._removeView(child);
157-
// TODO: consider caching the index on the child.
158-
const index = this._views.indexOf(child);
159-
this._views.splice(index, 1);
160-
this._children.delete(`${child.id || child._domId}`)
161-
this._unregisterLayoutChild(child);
162-
if (!this.#isRemoving) {
163-
this._refresh();
164-
}
165-
}
166-
167-
#isRemoving: boolean = false;
168-
public removeChildren(): void {
169-
this.#isRemoving = true;
170-
while (this.getChildrenCount() !== 0) {
171-
this.removeChild(this._views[this.getChildrenCount() - 1]);
172-
}
173-
if (this.#isRemoving) {
174-
this._refresh();
175-
}
176-
this.#isRemoving = false;
177-
}
17810
}

0 commit comments

Comments
 (0)