forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.ts
More file actions
141 lines (113 loc) · 2.76 KB
/
Copy pathwidget.ts
File metadata and controls
141 lines (113 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ReactNode, createElement } from 'react';
import { makeObservable, obx } from '@alilc/lowcode-editor-core';
import { createContent, uniqueId } from '@alilc/lowcode-utils';
import { getEvent } from '@alilc/lowcode-shell';
import { WidgetConfig, IWidgetBaseConfig } from '../types';
import { Skeleton } from '../skeleton';
import { WidgetView } from '../components/widget-views';
import { TitleContent } from '@alilc/lowcode-types';
export interface IWidget {
readonly name: string;
readonly content: ReactNode;
readonly align?: string;
readonly isWidget: true;
readonly visible: boolean;
readonly disabled?: boolean;
readonly body: ReactNode;
readonly skeleton: Skeleton;
readonly config: IWidgetBaseConfig;
getName(): string;
getContent(): any;
show(): void;
hide(): void;
toggle(): void;
enable?(): void;
disable?(): void;
}
export default class Widget implements IWidget {
readonly isWidget = true;
readonly id = uniqueId('widget');
readonly name: string;
readonly align?: string;
@obx.ref private _visible = true;
get visible(): boolean {
return this._visible;
}
@obx.ref inited = false;
@obx.ref private _disabled = false;
private _body: ReactNode;
get body() {
if (this.inited) {
return this._body;
}
this.inited = true;
const { content, contentProps } = this.config;
this._body = createContent(content, {
...contentProps,
config: this.config,
editor: getEvent(this.skeleton.editor),
});
return this._body;
}
get content(): ReactNode {
return createElement(WidgetView, {
widget: this,
key: this.id,
});
}
readonly title: TitleContent;
constructor(readonly skeleton: Skeleton, readonly config: WidgetConfig) {
makeObservable(this);
const { props = {}, name } = config;
this.name = name;
this.align = props.align;
this.title = props.title || name;
if (props.onInit) {
props.onInit.call(this, this);
}
}
getId() {
return this.id;
}
getName() {
return this.name;
}
getContent() {
return this.content;
}
hide() {
this.setVisible(false);
}
show() {
this.setVisible(true);
}
setVisible(flag: boolean) {
if (flag === this._visible) {
return;
}
if (flag) {
this._visible = true;
} else if (this.inited) {
this._visible = false;
}
}
toggle() {
this.setVisible(!this._visible);
}
private setDisabled(flag: boolean) {
if (this._disabled === flag) return;
this._disabled = flag;
}
disable() {
this.setDisabled(true);
}
enable() {
this.setDisabled(false);
}
get disabled(): boolean {
return this._disabled;
}
}
export function isWidget(obj: any): obj is IWidget {
return obj && obj.isWidget;
}