@gedit/editor-2d 0.1.97 → 0.1.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/lib/browser/editor2d-frontend-module.d.ts.map +1 -1
  2. package/lib/browser/editor2d-frontend-module.js +1 -0
  3. package/lib/browser/editor2d-frontend-module.js.map +1 -1
  4. package/lib/browser/editor2d-service.d.ts +14 -2
  5. package/lib/browser/editor2d-service.d.ts.map +1 -1
  6. package/lib/browser/editor2d-service.js +33 -9
  7. package/lib/browser/editor2d-service.js.map +1 -1
  8. package/lib/browser/model/editor2d-document.d.ts +2 -0
  9. package/lib/browser/model/editor2d-document.d.ts.map +1 -1
  10. package/lib/browser/model/editor2d-document.js +22 -3
  11. package/lib/browser/model/editor2d-document.js.map +1 -1
  12. package/lib/browser/model/editor2d-selection.d.ts +1 -0
  13. package/lib/browser/model/editor2d-selection.d.ts.map +1 -1
  14. package/lib/browser/model/editor2d-selection.js +11 -0
  15. package/lib/browser/model/editor2d-selection.js.map +1 -1
  16. package/lib/browser/model/editor2d.d.ts +8 -0
  17. package/lib/browser/model/editor2d.d.ts.map +1 -1
  18. package/lib/browser/model/editor2d.js +34 -3
  19. package/lib/browser/model/editor2d.js.map +1 -1
  20. package/lib/browser/playground/canvas-layer.js +1 -1
  21. package/lib/browser/playground/canvas-layer.js.map +1 -1
  22. package/lib/browser/playground/playground-context.d.ts +7 -3
  23. package/lib/browser/playground/playground-context.d.ts.map +1 -1
  24. package/lib/browser/playground/playground-context.js +9 -6
  25. package/lib/browser/playground/playground-context.js.map +1 -1
  26. package/lib/browser/playground/playground-contribution.d.ts +3 -2
  27. package/lib/browser/playground/playground-contribution.d.ts.map +1 -1
  28. package/lib/browser/playground/playground-contribution.js +10 -5
  29. package/lib/browser/playground/playground-contribution.js.map +1 -1
  30. package/lib/browser/playground/selection-entity-manager.d.ts.map +1 -1
  31. package/lib/browser/playground/selection-entity-manager.js +19 -17
  32. package/lib/browser/playground/selection-entity-manager.js.map +1 -1
  33. package/lib/browser/utils/snapshot.d.ts +16 -2
  34. package/lib/browser/utils/snapshot.d.ts.map +1 -1
  35. package/lib/browser/utils/snapshot.js +91 -47
  36. package/lib/browser/utils/snapshot.js.map +1 -1
  37. package/package.json +7 -7
  38. package/src/browser/editor2d-frontend-module.ts +1 -0
  39. package/src/browser/editor2d-service.ts +33 -11
  40. package/src/browser/model/editor2d-document.ts +25 -4
  41. package/src/browser/model/editor2d-selection.ts +10 -0
  42. package/src/browser/model/editor2d.ts +30 -2
  43. package/src/browser/playground/canvas-layer.ts +1 -1
  44. package/src/browser/playground/playground-context.ts +10 -8
  45. package/src/browser/playground/playground-contribution.ts +8 -3
  46. package/src/browser/playground/selection-entity-manager.tsx +18 -16
  47. package/src/browser/style/canvas-draw-layer.less +1 -1
  48. package/src/browser/style/index.less +0 -1
  49. package/src/browser/utils/snapshot.ts +59 -7
@@ -5,7 +5,7 @@ import {
5
5
  SelectionService,
6
6
  } from '@gedit/application-common';
7
7
  import { Editor2dModel } from './model/editor2d-model';
8
- import { EditorAccess, EditorManager, EditorWidget } from '@gedit/editor';
8
+ import { EditorAccess, EditorDocumentChangeEvent, EditorManager, EditorWidget } from '@gedit/editor';
9
9
  import { Editor2dNode } from './model/editor2d';
10
10
  import { Editor2dDocument } from './model/editor2d-document';
11
11
  import { SelectableTreeNode, TreeSelection } from '@gedit/tree';
@@ -17,6 +17,10 @@ export interface Editor2dSelectedNodesChangedEvent {
17
17
  document: Editor2dDocument | undefined; // 如果为空,代表当前没有在编辑文档
18
18
  }
19
19
 
20
+ export interface Editor2dModelContentChangedEvent extends Editor2dSelectedNodesChangedEvent {
21
+ event: EditorDocumentChangeEvent<Editor2dDocument>
22
+ }
23
+
20
24
  @injectable()
21
25
  export class Editor2dService {
22
26
  @inject(SelectionService) protected readonly selectionService: SelectionService;
@@ -59,7 +63,21 @@ export class Editor2dService {
59
63
  fn(this.getCurrentModel());
60
64
  });
61
65
  }
62
- onDeleteNode(fn: (e: Editor2dSelectedNodesChangedEvent & { deleteNodes: Editor2dNode[] }) => void): Disposable {
66
+ onDeleteNode(fn: (e: Editor2dModelContentChangedEvent & { deleteNodes: Editor2dNode[] }) => void): Disposable {
67
+ return this.onCurrentModelContentChanged(e => {
68
+ if (e.event.type === 'delete') {
69
+ return fn({
70
+ ...e,
71
+ deleteNodes: (e.event.nodes as Editor2dNode[]) || [],
72
+ });
73
+ }
74
+ });
75
+ }
76
+ /**
77
+ * 内容修改触发
78
+ * @param fn
79
+ */
80
+ onCurrentModelContentChanged(fn: (e: Editor2dModelContentChangedEvent) => void): Disposable {
63
81
  const self = this;
64
82
  let model: Editor2dModel | undefined;
65
83
  const toDispose = new DisposableCollection();
@@ -71,15 +89,13 @@ export class Editor2dService {
71
89
  preDispose.dispose();
72
90
  preDispose = new DisposableCollection();
73
91
  if (model) {
74
- preDispose.push(model.onDocumentContentChanged(e => {
75
- if (e.type === 'delete') {
76
- fn({
77
- selectedNodes: model?.selection.selectedNodes || [],
78
- deleteNodes: (e.nodes as Editor2dNode[]) || [],
79
- document: model?.document,
80
- model,
81
- });
82
- }
92
+ preDispose.push(model.onDocumentContentChanged(event => {
93
+ fn({
94
+ selectedNodes: model?.selection.selectedNodes || [],
95
+ document: model?.document,
96
+ model,
97
+ event
98
+ });
83
99
  }));
84
100
  }
85
101
  toDispose.push(preDispose);
@@ -153,3 +169,9 @@ export class Editor2dService {
153
169
  return toDispose;
154
170
  }
155
171
  }
172
+
173
+ export namespace Editor2dService {
174
+ // 动态获取, 用于循环依赖情况, 可以在AppShell加载之后使用
175
+ export type Provider = () => Editor2dService;
176
+ export const Provider = Symbol('Editor2dService.Provider');
177
+ }
@@ -12,7 +12,13 @@ import {
12
12
  PromiseDeferred,
13
13
  URI,
14
14
  } from '@gedit/utils';
15
- import { Editor2dContainerNode, Editor2dContent, Editor2dModelOptions, Editor2dNode, } from './editor2d';
15
+ import {
16
+ checkDuplicateNodes,
17
+ Editor2dContainerNode,
18
+ Editor2dContent,
19
+ Editor2dModelOptions,
20
+ Editor2dNode,
21
+ } from './editor2d';
16
22
  import { GameObject, GameObjectBaseType, GameObjectType } from '@gedit/render-engine';
17
23
  import { GameObjectDecoration, RenderEngineIDEService } from '@gedit/render-engine-ide';
18
24
  import { CompositeTreeNode, ExpandableTreeNode, Tree, TreeImpl, TreeNode } from '@gedit/tree';
@@ -36,7 +42,8 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
36
42
  readonly onSaved = this.onSavedEmitter.event;
37
43
  public animationStarted: boolean = false;
38
44
  protected animationIds: string[] = []; // 当前正在录制的动画节点
39
- private _localVersion: number = versionId++;
45
+ private _localVersion: number = 0;
46
+ private _saveVersion: number = 0;
40
47
  protected errors: ResourceRefError[] = [];
41
48
  protected componentContents: { [key: string]: Editor2dNode } = {};
42
49
  protected _snaplines: any[] = [];
@@ -61,6 +68,7 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
61
68
  super();
62
69
  this.toDispose.push(this.onContentChangedEmitter);
63
70
  this.toDispose.push(this.onSavedEmitter);
71
+ // this.toDispose.
64
72
  // this.toDispose.push(this.resourceAutoSaveService);
65
73
  this.toDispose.push(this.resourceAutoSaveService.onSyncContent(content => this.reloadContent(content || '')));
66
74
  this.toDispose.push(this.onSaved(event => this.resourceAutoSaveService.fireDidChangeContent(event)));
@@ -217,6 +225,11 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
217
225
  // }
218
226
  // }
219
227
  // 这里采用深度比较, 因为数据层次定义不会太多
228
+ if (data && data.id && data.id !== treeNode.id) {
229
+ alert('节点更新出错,不同节点无法互相覆盖');
230
+ console.error('无法覆盖同步节点数据: ', treeNode, data);
231
+ return;
232
+ }
220
233
  const changed = forceUpdate ? true : Compare.isDeepChanged(treeNode, data);
221
234
  if (changed) {
222
235
  Object.assign(treeNode, data);
@@ -264,7 +277,9 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
264
277
  }
265
278
  if (data.content) {
266
279
  this._localVersion = data.content.version;
267
- this.root = data.content;
280
+ (data.content as any).visible = false;
281
+ this._saveVersion = data.version || 0;
282
+ this.root = checkDuplicateNodes(data.content);
268
283
  } else {
269
284
  this.root = this.createRootNode();
270
285
  }
@@ -296,6 +311,10 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
296
311
  this.onContentChangedEmitter.fire({ document: this, contentChanges: contents, nodes, type });
297
312
  }
298
313
  if (!this.loading && !noSave) {
314
+ this._saveVersion++;
315
+ if (this._saveVersion === Number.MAX_VALUE) {
316
+ this._saveVersion = 0;
317
+ }
299
318
  this.onSavedEmitter.fire({ document: this, contentChanges: contents, nodes, type });
300
319
  }
301
320
  };
@@ -576,7 +595,6 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
576
595
  });
577
596
  if (nodes.length) {
578
597
  // TODO: 音乐素材变更
579
- console.log('document update to timeline', nodes, e.type);
580
598
  onNodeChange(nodes, e.type);
581
599
  }
582
600
  }
@@ -756,4 +774,7 @@ export class Editor2dDocument extends TreeImpl<Editor2dNode, Editor2dContainerNo
756
774
  this.fireContentChanged([], 'update');
757
775
  }
758
776
  }
777
+ get saveVersion(): number {
778
+ return this._saveVersion;
779
+ }
759
780
  }
@@ -2,10 +2,20 @@ import { injectable } from 'inversify';
2
2
  import { EditorDocumentSelection } from '@gedit/editor';
3
3
  import { TreeSelectionServiceImpl } from '@gedit/tree/lib/browser/tree-selection-impl';
4
4
  import { Editor2dNode } from './editor2d';
5
+ import { SelectableTreeNode, TreeSelection } from '@gedit/tree';
6
+ import SelectionType = TreeSelection.SelectionType;
5
7
 
6
8
  @injectable()
7
9
  export class Editor2dSelection extends TreeSelectionServiceImpl implements EditorDocumentSelection {
8
10
  get selectedNodes(): Editor2dNode[] {
9
11
  return this.state.selection() as Editor2dNode[];
10
12
  }
13
+ clearSelection(): void {
14
+ this.selectedNodes.forEach(node =>
15
+ this.addSelection({
16
+ node: node as Readonly<SelectableTreeNode>,
17
+ type: SelectionType.TOGGLE
18
+ })
19
+ );
20
+ }
11
21
  }
@@ -113,6 +113,14 @@ export namespace Editor2dNode {
113
113
  }
114
114
  return path.join('/');
115
115
  }
116
+ export function getDepth(node: Editor2dNode | Editor2dContainerNode | TreeNode): number {
117
+ let depth = 0;
118
+ while (node && !Editor2dNode.isRootNode(node)) {
119
+ depth ++;
120
+ node = node.parent as Editor2dContainerNode;
121
+ }
122
+ return depth;
123
+ }
116
124
  export function toLeafNode(node: Editor2dNode | Editor2dContainerNode): Editor2dNode {
117
125
  if (node.children) {
118
126
  delete node.children;
@@ -189,7 +197,7 @@ export namespace Editor2dNode {
189
197
  parent: undefined,
190
198
  name: uri.displayName,
191
199
  children: [],
192
- visible: true,
200
+ visible: false,
193
201
  expanded: true,
194
202
  selected: false,
195
203
  locked: false,
@@ -248,7 +256,7 @@ export async function getEditor2dModelOpts(resource: Resource, resourceService:
248
256
  const options: ResourceAutoSaveOptions = {
249
257
  encoding,
250
258
  resource,
251
- saveAsString: e => Editor2dContent.parseToString({version: e.document.version, content: e.document.root, snaplines: e.document.snaplines})
259
+ saveAsString: e => Editor2dContent.parseToString({version: e.document.saveVersion, content: e.document.root, snaplines: e.document.snaplines})
252
260
  };
253
261
  const autoSaveService = await resourceService.createAutoSaveResource(options);
254
262
  return {
@@ -256,3 +264,23 @@ export async function getEditor2dModelOpts(resource: Resource, resourceService:
256
264
  autoSaveService,
257
265
  };
258
266
  }
267
+
268
+ /**
269
+ * 检测重复节点数据, 可能为误操作造成,避免对编辑态造成影响
270
+ * @param node
271
+ */
272
+ export function checkDuplicateNodes(node: Editor2dNode, map: { [key: string]: Editor2dNode } = {}): Editor2dNode {
273
+ if (node.children) {
274
+ node.children.slice().forEach((c: Editor2dNode) => {
275
+ if (map[c.id]) {
276
+ const index = node.children!.indexOf(c);
277
+ node.children!.splice(index, 1);
278
+ console.log('>>> 清除重复数据: ', c.name);
279
+ } else {
280
+ map[c.id] = c;
281
+ checkDuplicateNodes(c, map);
282
+ }
283
+ });
284
+ }
285
+ return node;
286
+ }
@@ -61,7 +61,7 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
61
61
  this.document?.updateNode(e.node, {error: e.error});
62
62
  break;
63
63
  }
64
- this.context.onCanvasDataChanged(this.canvasDrawer!);
64
+ this.context.onCanvasDataChangedEmitter.fire(this.canvasDrawer!);
65
65
  }
66
66
 
67
67
  getAssetsURI(): string {
@@ -2,7 +2,7 @@ import { inject, injectable } from 'inversify';
2
2
  import { RenderEngineIDEService } from '@gedit/render-engine-ide';
3
3
  import { SelectableTreeNode, TreeSelection } from '@gedit/tree';
4
4
  import { AppConfigService } from '@gedit/app-config';
5
- import { debounce, URI } from '@gedit/utils';
5
+ import { debounce, Emitter, URI } from '@gedit/utils';
6
6
  import { Editor2dEntity } from './entities/editor2d-entity';
7
7
  import type { SelectionEntityManager } from './selection-entity-manager';
8
8
  import { Playground, Selectable } from '@gedit/playground';
@@ -16,10 +16,15 @@ import type { CanvasDraw } from './canvas-draw';
16
16
 
17
17
  export const Editor2dPlayGround = Symbol('Editor2dPlayGround');
18
18
 
19
- export type Editor2dPlayGround = Playground;
19
+ export type Editor2dPlayGround = Playground<PlaygroundContext2d>;
20
20
 
21
21
  @injectable()
22
22
  export class PlaygroundContext2d {
23
+ readonly onCanvasDataChangedEmitter = new Emitter<CanvasDraw>();
24
+ /**
25
+ * 监听画布变化,可用于生成缩略图
26
+ */
27
+ readonly onCanvasDataChanged = this.onCanvasDataChangedEmitter.event;
23
28
  constructor(
24
29
  @inject(RenderEngineIDEService) readonly renderEngine: RenderEngineIDEService,
25
30
  @inject(Editor2dDocument) readonly document: Editor2dDocument,
@@ -29,12 +34,9 @@ export class PlaygroundContext2d {
29
34
  @inject(Editor2dModelOptions) readonly options: Editor2dModelOptions,
30
35
  @inject(Editor2dContextKeyService) readonly contextKeyService: Editor2dContextKeyService
31
36
  ) {
32
- }
33
-
34
- // 画布数据更新
35
- onCanvasDataChanged(drawer: CanvasDraw): void {
36
- if (this.options.onCanvasDataChanged) {
37
- this.options.onCanvasDataChanged(drawer);
37
+ this.document.toDispose.push(this.onCanvasDataChangedEmitter);
38
+ if (options.onCanvasDataChanged) {
39
+ this.onCanvasDataChanged(drawer => options.onCanvasDataChanged!(drawer));
38
40
  }
39
41
  }
40
42
  /**
@@ -15,15 +15,17 @@ import { CanvasDrawLayer } from './canvas-draw-layer';
15
15
  import { AppConfigData, AppConfigService } from '@gedit/app-config';
16
16
  import { HistoryService } from '@gedit/history';
17
17
  import { Editor2dModelOptions, Editor2dNode } from '../model/editor2d';
18
- import { ApplicationShell, ContextMenuRenderer } from '@gedit/layout';
18
+ import { ContextMenuRenderer } from '@gedit/layout';
19
+ import { CommandService } from '@gedit/command';
20
+ import { CommonCommands } from '@gedit/application-common/lib/browser';
19
21
 
20
22
  @injectable()
21
23
  export class PlaygroundContribution2d implements PlaygroundContribution {
22
24
  @inject(AppConfigService) readonly appConfig: AppConfigService;
23
25
  @inject(Editor2dModelOptions) readonly modelOpts: Editor2dModelOptions;
24
26
  @inject(HistoryService) readonly historyService: HistoryService;
25
- @inject(ApplicationShell) readonly appShell: ApplicationShell;
26
27
  @inject(PlaygroundConfig) readonly config: PlaygroundConfig;
28
+ @inject(CommandService) readonly commandService: CommandService;
27
29
  @inject(ContextMenuRenderer) readonly menuRender: ContextMenuRenderer;
28
30
  registerPlayground(registry: PlaygroundRegistry): void {
29
31
  registry.registerLayer(CanvasLayer);
@@ -56,7 +58,10 @@ export class PlaygroundContribution2d implements PlaygroundContribution {
56
58
  priority: 6,
57
59
  cancelMode: 'once',
58
60
  handle: () => {
59
- this.appShell.toggleMaximized();
61
+ this.commandService.executeCommand(CommonCommands.TOGGLE_MAXIMIZED.id);
62
+ setTimeout(() => {
63
+ playground.config.scrollPageBoundsToCenter();
64
+ }, 10);
60
65
  }
61
66
  });
62
67
  stateEntity.registerState({
@@ -12,6 +12,22 @@ import type { PlaygroundContext2d } from './playground-context';
12
12
  import { Rectangle } from '@gedit/math';
13
13
  import { GameObject } from '@gedit/render-engine';
14
14
 
15
+ const numberToFixed4 = (data: {[key: string]: any}) => {
16
+ const d: any = {};
17
+ Object.keys(data).forEach(key => {
18
+ const value = data[key];
19
+ // 旋转不能四舍五入,不然属性面板旋转角度误差很大
20
+ if (typeof value === 'number' && key !== 'rotation') {
21
+ d[key] = parseFloat(value.toFixed(6));
22
+ } else if (typeof value === 'object') {
23
+ d[key] = numberToFixed4(value);
24
+ } else {
25
+ d[key] = value;
26
+ }
27
+ });
28
+ return d;
29
+ };
30
+
15
31
  /**
16
32
  * 选择器实体相关逻辑
17
33
  */
@@ -95,23 +111,9 @@ export class SelectionEntityManager {
95
111
  lastTransform = newTransform;
96
112
  return;
97
113
  }; */
114
+ newTransform = numberToFixed4(newTransform);
98
115
  // const delta = TransformSchema.getDelta(lastTransform, newTransform);
99
- const numberToFixed4 = (data: {[key: string]: any}) => {
100
- const d: any = {};
101
- Object.keys(data).forEach(key => {
102
- const value = data[key];
103
- if (typeof value === 'number') {
104
- d[key] = parseFloat(value.toFixed(6));
105
- } else if (typeof value === 'object') {
106
- d[key] = numberToFixed4(value);
107
- } else {
108
- d[key] = key;
109
- }
110
- });
111
- return d;
112
- };
113
-
114
- document.updateNode(entity.nodeId, numberToFixed4(newTransform), true);
116
+ document.updateNode(entity.nodeId, newTransform, true);
115
117
  // lastTransform = newTransform;
116
118
  });
117
119
  // 设置控制key
@@ -8,7 +8,7 @@
8
8
  position: absolute;
9
9
  width: 8px;
10
10
  height: 8px;
11
- border-radius: 4px;
11
+ border-radius: var(--g-small-radius);
12
12
  margin-top: -4px;
13
13
  margin-left: -4px;
14
14
  background-color: var(--g-playground-select);
@@ -17,7 +17,6 @@
17
17
  height: 16px;
18
18
  color: var(--g-foreground);
19
19
  background-color: var(--g-foreground);
20
- margin-right: 4px;
21
20
  vertical-align: center;
22
21
  opacity: 0.7;
23
22
  background-repeat: no-repeat;
@@ -1,5 +1,15 @@
1
1
  import { CanvasDraw, CanvasDrawOpts } from '../playground/canvas-draw';
2
- import { Disposable, DisposableCollection, DisposableImpl, Emitter, PromiseDeferred, SizeSchema } from '@gedit/utils';
2
+ import {
3
+ debounce,
4
+ Disposable,
5
+ DisposableCollection,
6
+ DisposableImpl,
7
+ Emitter,
8
+ PromiseDeferred,
9
+ SizeSchema,
10
+ URI
11
+ } from '@gedit/utils';
12
+ import type { Editor2dModel } from '../model/editor2d-model';
3
13
  import { Editor2dNode } from '../model';
4
14
  import { GameConfig } from '@gedit/render-engine/lib/common';
5
15
 
@@ -77,7 +87,22 @@ export class SnapshotCreator extends DisposableImpl {
77
87
  this.onUpdateEmitter,
78
88
  ]);
79
89
  }
80
-
90
+ /**
91
+ * 清楚缓存数据
92
+ */
93
+ clearResultCache(includeUris: string[]): void {
94
+ Object.keys(this.result).forEach(key => {
95
+ if (!includeUris.includes(key)) {
96
+ delete this.result[key];
97
+ }
98
+ });
99
+ }
100
+ getResultFromCache(): Snapshot[] {
101
+ return Object.keys(this.result).map(k => this.result[k]);
102
+ }
103
+ updateResultCache(data: Snapshot): void {
104
+ this.result[data.uri] = data;
105
+ }
81
106
  getSnapshot(uri: string): Snapshot | undefined {
82
107
  return this.result[uri];
83
108
  }
@@ -91,10 +116,7 @@ export class SnapshotCreator extends DisposableImpl {
91
116
  await this.process();
92
117
  return scenes.map(s => this.result[s.uri]!);
93
118
  }
94
- getResult(): Snapshot[] {
95
- return Object.keys(this.result).map(k => this.result[k]);
96
- }
97
- protected async process(): Promise<void> {
119
+ protected process(): Promise<void> {
98
120
  if (this.processing) {
99
121
  return this.processing.promise;
100
122
  }
@@ -143,15 +165,45 @@ export class SnapshotCreator extends DisposableImpl {
143
165
  this.current = next;
144
166
  this.drawer.renderWidget?.currentScene?.dispose();
145
167
  const appConfig = typeof this.opts.appConfig === 'function' ? this.opts.appConfig() : this.opts.appConfig;
168
+ let content: Editor2dNode = next.content;
169
+ // Empty
170
+ if (!content || !content.children || Object.keys(content).length === 0) {
171
+ content = Editor2dNode.createRootNode(new URI(next.uri));
172
+ const attrs = this.opts.renderEngine.getDisplayDefaultData(this.opts.engineName, content.displayType);
173
+ Object.assign(content, { ...attrs });
174
+ }
146
175
  this.drawer.update({
147
176
  visible: true,
148
177
  uri: next.uri,
149
178
  engineName: this.opts.engineName,
150
179
  appConfig,
151
- content: next.content,
180
+ content,
152
181
  });
153
182
  } else {
154
183
  end();
155
184
  }
156
185
  }
186
+ /**
187
+ * 监听变化,并自动刷新缩略图缓存
188
+ * @param model
189
+ * @param fn
190
+ * @param quility 缩略图质量
191
+ * @param delay 延迟生成
192
+ */
193
+ listenModelChanged(model: Editor2dModel, fn: (imageData: string) => void, quility = 0.1, delay = 1000): Disposable {
194
+ return model.playground.context.onCanvasDataChanged(debounce(drawer => {
195
+ const {content, visible} = drawer.getData() || {};
196
+ if (!content || drawer.loading) return;
197
+ if (visible) {
198
+ const widget = drawer.renderWidget;
199
+ if (widget) {
200
+ widget.createSnapShot(this.opts.imageType, quility).then((base64: string) => {
201
+ if (base64) {
202
+ fn(base64);
203
+ }
204
+ });
205
+ }
206
+ }
207
+ }, delay));
208
+ }
157
209
  }