forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.ts
More file actions
174 lines (139 loc) · 4.33 KB
/
Copy pathwindow.ts
File metadata and controls
174 lines (139 loc) · 4.33 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { uniqueId } from '@alilc/lowcode-utils';
import { createModuleEventBus, IEventBus, makeObservable, obx } from '@alilc/lowcode-editor-core';
import { Context } from './context/view-context';
import { IWorkspace } from './workspace';
import { IResource } from './resource';
import { IPublicTypeDisposable } from '../../types/es/shell/type/disposable';
import { IPublicModelWindow } from '@alilc/lowcode-types';
interface IWindowCOnfig {
title: string | undefined;
options?: Object;
viewType?: string | undefined;
}
export interface IEditorWindow extends Omit<IPublicModelWindow<IResource>, 'changeViewType'> {
readonly resource: IResource;
editorViews: Map<string, Context>;
changeViewType: (name: string, ignoreEmit?: boolean) => void;
}
export class EditorWindow implements IEditorWindow {
id: string = uniqueId('window');
icon: React.ReactElement | undefined;
private emitter: IEventBus = createModuleEventBus('Project');
title: string | undefined;
url: string | undefined;
@obx.ref editorView: Context;
@obx editorViews: Map<string, Context> = new Map<string, Context>();
@obx initReady = false;
constructor(readonly resource: IResource, readonly workspace: IWorkspace, private config: IWindowCOnfig) {
makeObservable(this);
this.init();
this.title = config.title;
this.icon = resource.icon;
}
async importSchema(schema: any) {
const newSchema = await this.resource.import(schema);
if (!newSchema) {
return;
}
Object.keys(newSchema).forEach(key => {
const view = this.editorViews.get(key);
view?.project.importSchema(newSchema[key]);
});
}
async save() {
const value: any = {};
const editorViews = this.resource.editorViews;
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
const saveResult = await this.editorViews.get(name)?.save();
value[name] = saveResult;
}
return await this.resource.save(value);
}
async init() {
await this.initViewTypes();
await this.execViewTypesInit();
this.url = await this.resource.url();
this.setDefaultViewType();
this.initReady = true;
}
initViewTypes = async () => {
const editorViews = this.resource.editorViews;
if (!editorViews) {
return;
}
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
await this.initViewType(name);
if (!this.editorView) {
this.changeViewType(name);
}
}
};
onChangeViewType(fn: (viewName: string) => void): IPublicTypeDisposable {
this.emitter.on('window.change.view.type', fn);
return () => {
this.emitter.off('window.change.view.type', fn);
};
}
execViewTypesInit = async () => {
const editorViews = this.resource.editorViews;
if (!editorViews) {
return;
}
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
this.changeViewType(name);
await this.editorViews.get(name)?.init();
}
};
setDefaultViewType = () => {
this.changeViewType(this.config.viewType ?? this.resource.defaultViewType);
};
get resourceType() {
return this.resource.resourceType.type;
}
initViewType = async (name: string) => {
const viewInfo = this.resource.getEditorView(name);
if (this.editorViews.get(name)) {
return;
}
const editorView = new Context(this.workspace, this, viewInfo as any, this.config.options);
this.editorViews.set(name, editorView);
};
changeViewType = (name: string, ignoreEmit: boolean = true) => {
this.editorView?.setActivate(false);
this.editorView = this.editorViews.get(name)!;
if (!this.editorView) {
return;
}
if (!ignoreEmit) {
this.emitter.emit('window.change.view.type', name);
}
this.editorView.setActivate(true);
};
get project() {
return this.editorView?.project;
}
get innerProject() {
return this.editorView?.innerProject;
}
get innerSkeleton() {
return this.editorView?.innerSkeleton;
}
get innerSetters() {
return this.editorView?.innerSetters;
}
get innerHotkey() {
return this.editorView?.innerHotkey;
}
get editor() {
return this.editorView?.editor;
}
get designer() {
return this.editorView?.designer;
}
get innerPlugins() {
return this.editorView?.innerPlugins;
}
}