GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — lowcode-engine/packages/editor-core/src/editor.ts at main · flydoing/lowcode-engine Expand file tree/ Copy patheditor.tsMore file actions 331 lines (296 loc) · 9.73 KB / Copy patheditor.tsFile metadata and controls331 lines (296 loc) · 9.73 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 /* eslint-disable no-console */ /* eslint-disable max-len */ import { StrictEventEmitter } from 'strict-event-emitter-types'; import { EventEmitter } from 'events'; import { EventBus } from './event-bus'; import { IPublicModelEditor, EditorConfig, PluginClassSet, IPublicTypeEditorValueKey, IPublicTypeEditorGetResult, HookConfig, IPublicTypeComponentDescription, IPublicTypeRemoteComponentDescription, GlobalEvent, } from '@alilc/lowcode-types'; import { engineConfig } from './config'; import { globalLocale } from './intl'; import { obx } from './utils'; import { IPublicTypeAssetsJson, AssetLoader } from '@alilc/lowcode-utils'; import { assetsTransform } from './utils/assets-transform'; EventEmitter.defaultMaxListeners = 100; // inner instance keys which should not be stored in config const keyBlacklist = [ 'designer', 'skeleton', 'currentDocument', 'simulator', 'plugins', 'setters', 'material', 'innerHotkey', 'innerPlugins', ]; export declare interface Editor extends StrictEventEmitter<EventEmitter, GlobalEvent.EventConfig> { addListener(event: string | symbol, listener: (...args: any[]) => void): this; once(event: string | symbol, listener: (...args: any[]) => void): this; removeListener(event: string | symbol, listener: (...args: any[]) => void): this; off(event: string | symbol, listener: (...args: any[]) => void): this; removeAllListeners(event?: string | symbol): this; setMaxListeners(n: number): this; getMaxListeners(): number; listeners(event: string | symbol): Function[]; rawListeners(event: string | symbol): Function[]; listenerCount(type: string | symbol): number; // Added in Node 6... prependListener(event: string | symbol, listener: (...args: any[]) => void): this; prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this; eventNames(): Array<string | symbol>; } // eslint-disable-next-line no-redeclare export class Editor extends (EventEmitter as any) implements IPublicModelEditor { constructor(readonly viewName: string = 'global', readonly workspaceMode: boolean = false) { // eslint-disable-next-line constructor-super super(); // set global emitter maxListeners this.setMaxListeners(200); this.eventBus = new EventBus(this); } /** * Ioc Container */ @obx.shallow private context = new Map<IPublicTypeEditorValueKey, any>(); get locale() { return globalLocale.getLocale(); } // readonly utils = utils; private hooks: HookConfig[] = []; get<T = undefined, KeyOrType = any>( keyOrType: KeyOrType, ): IPublicTypeEditorGetResult<T, KeyOrType> | undefined { return this.context.get(keyOrType as any); } has(keyOrType: IPublicTypeEditorValueKey): boolean { return this.context.has(keyOrType); } set(key: IPublicTypeEditorValueKey, data: any): void | Promise<void> { if (key === 'assets') { return this.setAssets(data); } // store the data to engineConfig while invoking editor.set() if (!keyBlacklist.includes(key as string)) { engineConfig.set(key as any, data); } this.context.set(key, data); this.notifyGot(key); } async setAssets(assets: IPublicTypeAssetsJson) { const { components } = assets; if (components && components.length) { const componentDescriptions: IPublicTypeComponentDescription[] = []; const remoteComponentDescriptions: IPublicTypeRemoteComponentDescription[] = []; components.forEach((component: any) => { if (!component) { return; } if (component.exportName && component.url) { remoteComponentDescriptions.push(component); } else { componentDescriptions.push(component); } }); assets.components = componentDescriptions; assets.componentList = assets.componentList || []; // 如果有远程组件描述协议,则自动加载并补充到资产包中,同时出发 designer.incrementalAssetsReady 通知组件面板更新数据 if (remoteComponentDescriptions && remoteComponentDescriptions.length) { await Promise.all( remoteComponentDescriptions.map(async (component: any) => { const { exportName, url, npm } = component; await (new AssetLoader()).load(url); function setAssetsComponent(component: any, extraNpmInfo: any = {}) { const components = component.components; if (Array.isArray(components)) { components.forEach(d => { assets.components = assets.components.concat({ npm: { ...npm, ...extraNpmInfo, }, ...d, } || []); }); return; } if (component.components) { assets.components = assets.components.concat({ npm: { ...npm, ...extraNpmInfo, }, ...component.components, } || []); } // assets.componentList = assets.componentList.concat(component.componentList || []); } function setArrayAssets(value: any[], preExportName: string = '', preSubName: string = '') { value.forEach((d: any, i: number) => { const exportName = [preExportName, i.toString()].filter(d => !!d).join('.'); const subName = [preSubName, i.toString()].filter(d => !!d).join('.'); Array.isArray(d) ? setArrayAssets(d, exportName, subName) : setAssetsComponent(d, { exportName, subName, }); }); } if (window[exportName]) { if (Array.isArray(window[exportName])) { setArrayAssets(window[exportName] as any); } else { setAssetsComponent(window[exportName] as any); } } return window[exportName]; }), ); } } const innerAssets = assetsTransform(assets); this.context.set('assets', innerAssets); this.notifyGot('assets'); } onceGot<T = undefined, KeyOrType extends IPublicTypeEditorValueKey = any>(keyOrType: KeyOrType): Promise<IPublicTypeEditorGetResult<T, KeyOrType>> { const x = this.context.get(keyOrType); if (x !== undefined) { return Promise.resolve(x); } return new Promise((resolve) => { this.setWait(keyOrType, resolve, true); }); } onGot<T = undefined, KeyOrType extends IPublicTypeEditorValueKey = any>( keyOrType: KeyOrType, fn: (data: IPublicTypeEditorGetResult<T, KeyOrType>) => void, ): () => void { const x = this.context.get(keyOrType); if (x !== undefined) { fn(x); return () => { }; } else { this.setWait(keyOrType, fn); return () => { this.delWait(keyOrType, fn); }; } } register(data: any, key?: IPublicTypeEditorValueKey): void { this.context.set(key || data, data); this.notifyGot(key || data); } config?: EditorConfig; eventBus: EventBus; components?: PluginClassSet; async init(config?: EditorConfig, components?: PluginClassSet): Promise<any> { this.config = config || {}; this.components = components || {}; const { hooks = [], lifeCycles } = this.config; this.emit('editor.beforeInit'); const init = (lifeCycles && lifeCycles.init) || ((): void => { }); try { await init(this); // 注册快捷键 // 注册 hooks this.registerHooks(hooks); this.emit('editor.afterInit'); return true; } catch (err) { console.error(err); } } destroy(): void { if (!this.config) { return; } try { const { lifeCycles = {} } = this.config; this.unregisterHooks(); if (lifeCycles.destroy) { lifeCycles.destroy(this); } } catch (err) { console.warn(err); } } initHooks = (hooks: HookConfig[]) => { this.hooks = hooks.map((hook) => ({ ...hook, // 指定第一个参数为 editor handler: hook.handler.bind(this, this), })); return this.hooks; }; registerHooks = (hooks: HookConfig[]) => { this.initHooks(hooks).forEach(({ message, type, handler }) => { if (['on', 'once'].indexOf(type) !== -1) { this[type]((message as any), handler); } }); }; unregisterHooks = () => { this.hooks.forEach(({ message, handler }) => { this.removeListener(message, handler); }); }; /* eslint-disable */ private waits = new Map< IPublicTypeEditorValueKey, Array<{ once?: boolean; resolve: (data: any) => void; }> >(); /* eslint-enable */ private notifyGot(key: IPublicTypeEditorValueKey) { let waits = this.waits.get(key); if (!waits) { return; } waits = waits.slice().reverse(); let i = waits.length; while (i--) { waits[i].resolve(this.get(key)); if (waits[i].once) { waits.splice(i, 1); } } if (waits.length > 0) { this.waits.set(key, waits); } else { this.waits.delete(key); } } private setWait(key: IPublicTypeEditorValueKey, resolve: (data: any) => void, once?: boolean) { const waits = this.waits.get(key); if (waits) { waits.push({ resolve, once }); } else { this.waits.set(key, [{ resolve, once }]); } } private delWait(key: IPublicTypeEditorValueKey, fn: any) { const waits = this.waits.get(key); if (!waits) { return; } let i = waits.length; while (i--) { if (waits[i].resolve === fn) { waits.splice(i, 1); } } if (waits.length < 1) { this.waits.delete(key); } } } export const commonEvent = new EventBus(new EventEmitter()); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |