Skip to content

Commit b1cd58f

Browse files
supporting prebuilt widget (custom code injection)
1 parent 9933a3a commit b1cd58f

File tree

15 files changed

+261
-14
lines changed

15 files changed

+261
-14
lines changed

lib/builder/buildable-widget.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class BuildableTree implements Buildable {
1919
result.set(key, Object.getOwnPropertyDescriptor(this, key))
2020
}
2121

22-
const tree = new BuildingTree(this.widgetClassName, depth)
22+
const tree = new BuildingTree(this.constructorName, depth)
2323
function registerOnParam(name: string, value: string) {
2424
// checker logic if default field or not
2525
const named: boolean = !defaultParamKeys.includes(name);
@@ -82,18 +82,33 @@ export class BuildableTree implements Buildable {
8282
return tree.build();
8383
}
8484

85-
get widgetClassName(): string {
85+
/**
86+
* I.E "Transform" is default class name, when you want to make "Transform.rotate()", override with this.
87+
* @param name new name for the class invocation
88+
*/
89+
// @ignore()
90+
private factoryName: string = null;
91+
extendWithFactoryName(name: string) {
92+
this.factoryName = this.factoryName
93+
return this;
94+
}
95+
96+
get constructorName(): string {
97+
if (this.factoryName) {
98+
return `${this.constructor.name}.${this.factoryName}`;
99+
}
86100
return this.constructor.name;
87101
}
88102
}
89103

90104

91105
export class SnippetBuildableTree extends BuildableTree implements SnippetBuilder {
106+
92107
lookup() {
93108
return this._defaultSnippet;
94109
}
95110
_defaultSnippet: string;
96-
get widgetClassName(): string {
111+
get constructorName(): string {
97112
return this.constructor.name;
98113
}
99114
}

lib/builder/building-tree.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,44 @@ export class BuildingTree implements Buildable {
6464
return this.lookup();
6565
}
6666

67-
lookup(): string {
67+
lookup(options?: {
68+
withComma?: boolean
69+
}): string {
70+
71+
if (options.withComma && canAddComma(this.code)) {
72+
return addComma({ code: this.code, safetyCheck: true });
73+
}
74+
6875
return this.code
6976
}
77+
}
78+
79+
function canAddComma(code: string): boolean {
80+
const trimmed = code.trimEnd();
81+
if (trimmed.endsWith(";")) {
82+
return false;
83+
}
84+
if (trimmed.endsWith(",")) {
85+
return false;
86+
}
87+
if (trimmed.endsWith(")")) {
88+
return true;
89+
}
90+
return false;
91+
}
92+
93+
function addComma({ code, safetyCheck = false }: { code: string; safetyCheck?: boolean; }): string {
94+
function add() {
95+
code = code + ",";
96+
}
97+
98+
if (safetyCheck) {
99+
if (canAddComma(code)) {
100+
add();
101+
}
102+
} else {
103+
add();
104+
}
105+
106+
return code;
70107
}

lib/builder/prebuilt-widget.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Widget } from "../widgets";
2+
import { BuildingTree } from "./building-tree";
3+
4+
/**
5+
* this is used for containing prebuilt code snippet as an widget
6+
*/
7+
export class PrebuiltWidget extends Widget {
8+
widget: string | Widget
9+
10+
// accepts both types anonymously.
11+
constructor(widget: string | Widget) {
12+
super()
13+
this.widget = widget;
14+
}
15+
16+
build(): BuildingTree {
17+
if (this.widget instanceof Widget) {
18+
return this.widget.build();
19+
} else {
20+
return new PrebuiltBuildingTree(this.widget);
21+
}
22+
}
23+
}
24+
25+
class PrebuiltBuildingTree extends BuildingTree {
26+
final: string
27+
constructor(final: string) {
28+
super()
29+
this.final = final
30+
}
31+
32+
build() {
33+
return this;
34+
}
35+
36+
lookup() {
37+
return this.final;
38+
}
39+
}

lib/builder/snippet-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class SnippetBuilder {
1616
return this._defaultSnippet;
1717
}
1818

19-
get widgetClassName(): string {
19+
get constructorName(): string {
2020
return this.constructor.name;
2121
}
2222
}

lib/dart-ui/color.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { BuildableTree } from "../builder/buildable-widget";
2+
import { SnippetBuilder } from "../builder/snippet-builder";
3+
import { defaultParam } from "../decorations/params";
4+
5+
/**
6+
* https://api.flutter.dev/flutter/dart-ui/Color-class.html
7+
*/
8+
export class Color extends BuildableTree {
9+
@defaultParam()
10+
value: SnippetBuilder | number
11+
constructor(value: number) {
12+
super()
13+
this.value = SnippetBuilder.fromStatic("0x" + value.toString(16))
14+
}
15+
16+
static fromHex(hex: string): Color {
17+
return new Color(parseInt(hex, 16));
18+
}
19+
}

lib/dart-ui/offset.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { BuildableTree } from "../builder/buildable-widget";
2+
import { double } from "../dart";
3+
import { defaultParam } from "../decorations/params";
4+
5+
/**
6+
* https://api.flutter.dev/flutter/dart-ui/Offset-class.html
7+
*/
8+
export class Offset extends BuildableTree {
9+
@defaultParam()
10+
dx: double
11+
@defaultParam()
12+
dy: double
13+
14+
constructor(dx: double, dy: double) {
15+
super()
16+
this.dx = dx
17+
this.dy = dy;
18+
}
19+
}

lib/dynamic/x-color.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Color } from "../dart-ui/color";
2+
3+
export class XColor extends Color {
4+
5+
}

lib/example/colors.example.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Color } from "../dart-ui/color";
2+
3+
4+
const color = new Color(0xFF0000)
5+
6+
console.log(color.build().lookup())

lib/example/prebuilt.example.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PrebuiltWidget } from "../builder/prebuilt-widget";
2+
import { Double } from "../dart";
3+
import { Opacity } from "../widgets/opacity";
4+
5+
6+
7+
const prebuilt = new PrebuiltWidget("UndifinedCustomWidget()")
8+
9+
10+
const opacity = new Opacity({
11+
opacity: 0.0,
12+
child: prebuilt
13+
})
14+
15+
console.log(opacity.build().finalize())

lib/painting/box-shadow.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { BuildableTree } from "../builder/buildable-widget";
2+
3+
/**
4+
* https://api.flutter.dev/flutter/painting/BoxShadow-class.html
5+
*/
6+
export class BoxShadow extends BuildableTree {
7+
// color:
8+
constructor() {
9+
super()
10+
}
11+
}

0 commit comments

Comments
 (0)