@gedit/editor-2d 0.1.92 → 0.1.98

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 (74) hide show
  1. package/lib/browser/editor2d-contribution.js +6 -6
  2. package/lib/browser/editor2d-contribution.js.map +1 -1
  3. package/lib/browser/editor2d-frontend-module.d.ts.map +1 -1
  4. package/lib/browser/editor2d-frontend-module.js +1 -0
  5. package/lib/browser/editor2d-frontend-module.js.map +1 -1
  6. package/lib/browser/editor2d-service.d.ts +14 -2
  7. package/lib/browser/editor2d-service.d.ts.map +1 -1
  8. package/lib/browser/editor2d-service.js +33 -9
  9. package/lib/browser/editor2d-service.js.map +1 -1
  10. package/lib/browser/index.d.ts +1 -0
  11. package/lib/browser/index.d.ts.map +1 -1
  12. package/lib/browser/index.js +6 -1
  13. package/lib/browser/index.js.map +1 -1
  14. package/lib/browser/model/editor2d-document.d.ts +2 -0
  15. package/lib/browser/model/editor2d-document.d.ts.map +1 -1
  16. package/lib/browser/model/editor2d-document.js +17 -2
  17. package/lib/browser/model/editor2d-document.js.map +1 -1
  18. package/lib/browser/model/editor2d-selection.d.ts +1 -0
  19. package/lib/browser/model/editor2d-selection.d.ts.map +1 -1
  20. package/lib/browser/model/editor2d-selection.js +11 -0
  21. package/lib/browser/model/editor2d-selection.js.map +1 -1
  22. package/lib/browser/model/editor2d.d.ts +1 -0
  23. package/lib/browser/model/editor2d.d.ts.map +1 -1
  24. package/lib/browser/model/editor2d.js +3 -2
  25. package/lib/browser/model/editor2d.js.map +1 -1
  26. package/lib/browser/model/index.js +5 -1
  27. package/lib/browser/model/index.js.map +1 -1
  28. package/lib/browser/model/utils/anim.utils.d.ts.map +1 -1
  29. package/lib/browser/model/utils/anim.utils.js +6 -2
  30. package/lib/browser/model/utils/anim.utils.js.map +1 -1
  31. package/lib/browser/model/utils/index.js +5 -1
  32. package/lib/browser/model/utils/index.js.map +1 -1
  33. package/lib/browser/playground/canvas-draw.d.ts +2 -7
  34. package/lib/browser/playground/canvas-draw.d.ts.map +1 -1
  35. package/lib/browser/playground/canvas-draw.js +1 -8
  36. package/lib/browser/playground/canvas-draw.js.map +1 -1
  37. package/lib/browser/playground/canvas-layer.js +1 -1
  38. package/lib/browser/playground/canvas-layer.js.map +1 -1
  39. package/lib/browser/playground/entities/index.js +5 -1
  40. package/lib/browser/playground/entities/index.js.map +1 -1
  41. package/lib/browser/playground/index.js +5 -1
  42. package/lib/browser/playground/index.js.map +1 -1
  43. package/lib/browser/playground/playground-context.d.ts +7 -3
  44. package/lib/browser/playground/playground-context.d.ts.map +1 -1
  45. package/lib/browser/playground/playground-context.js +9 -6
  46. package/lib/browser/playground/playground-context.js.map +1 -1
  47. package/lib/browser/playground/playground-contribution.d.ts +6 -1
  48. package/lib/browser/playground/playground-contribution.d.ts.map +1 -1
  49. package/lib/browser/playground/playground-contribution.js +50 -0
  50. package/lib/browser/playground/playground-contribution.js.map +1 -1
  51. package/lib/browser/playground/selection-entity-manager.d.ts.map +1 -1
  52. package/lib/browser/playground/selection-entity-manager.js +18 -0
  53. package/lib/browser/playground/selection-entity-manager.js.map +1 -1
  54. package/lib/browser/utils/snapshot.d.ts +61 -0
  55. package/lib/browser/utils/snapshot.d.ts.map +1 -0
  56. package/lib/browser/utils/snapshot.js +257 -0
  57. package/lib/browser/utils/snapshot.js.map +1 -0
  58. package/package.json +7 -7
  59. package/src/browser/editor2d-contribution.ts +2 -2
  60. package/src/browser/editor2d-frontend-module.ts +1 -0
  61. package/src/browser/editor2d-service.ts +33 -11
  62. package/src/browser/index.ts +1 -0
  63. package/src/browser/model/editor2d-document.ts +13 -2
  64. package/src/browser/model/editor2d-selection.ts +10 -0
  65. package/src/browser/model/editor2d.ts +3 -2
  66. package/src/browser/model/utils/anim.utils.ts +7 -2
  67. package/src/browser/playground/canvas-draw.ts +3 -16
  68. package/src/browser/playground/canvas-layer.ts +1 -1
  69. package/src/browser/playground/playground-context.ts +10 -8
  70. package/src/browser/playground/playground-contribution.ts +44 -1
  71. package/src/browser/playground/selection-entity-manager.tsx +17 -0
  72. package/src/browser/style/canvas-draw-layer.less +1 -1
  73. package/src/browser/style/index.less +0 -1
  74. package/src/browser/utils/snapshot.ts +203 -0
@@ -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
  /**
@@ -1,6 +1,8 @@
1
1
  import { inject, injectable } from 'inversify';
2
2
  import {
3
+ EditorStateConfigEntity,
3
4
  Playground,
5
+ PlaygroundConfig,
4
6
  PlaygroundContribution,
5
7
  PlaygroundRegistry,
6
8
  RulerConfigEntity,
@@ -12,13 +14,19 @@ import { CanvasDrawLayer } from './canvas-draw-layer';
12
14
  // import { SelectionLayer } from './selection-layer';
13
15
  import { AppConfigData, AppConfigService } from '@gedit/app-config';
14
16
  import { HistoryService } from '@gedit/history';
15
- import { Editor2dModelOptions } from '../model/editor2d';
17
+ import { Editor2dModelOptions, Editor2dNode } from '../model/editor2d';
18
+ import { ContextMenuRenderer } from '@gedit/layout';
19
+ import { CommandService } from '@gedit/command';
20
+ import { CommonCommands } from '@gedit/application-common/lib/browser';
16
21
 
17
22
  @injectable()
18
23
  export class PlaygroundContribution2d implements PlaygroundContribution {
19
24
  @inject(AppConfigService) readonly appConfig: AppConfigService;
20
25
  @inject(Editor2dModelOptions) readonly modelOpts: Editor2dModelOptions;
21
26
  @inject(HistoryService) readonly historyService: HistoryService;
27
+ @inject(PlaygroundConfig) readonly config: PlaygroundConfig;
28
+ @inject(CommandService) readonly commandService: CommandService;
29
+ @inject(ContextMenuRenderer) readonly menuRender: ContextMenuRenderer;
22
30
  registerPlayground(registry: PlaygroundRegistry): void {
23
31
  registry.registerLayer(CanvasLayer);
24
32
  registry.registerLayer(CanvasDrawLayer);
@@ -34,6 +42,7 @@ export class PlaygroundContribution2d implements PlaygroundContribution {
34
42
  // 开启缩放
35
43
  playground.zoomEnable = true;
36
44
  const rulerEntity = playground.entityManager.getEntity<RulerConfigEntity>(RulerConfigEntity);
45
+ const stateEntity = playground.entityManager.getEntity<EditorStateConfigEntity>(EditorStateConfigEntity);
37
46
  // 新版隐藏标尺
38
47
  if (rulerEntity) {
39
48
  rulerEntity.updateConfig({
@@ -41,6 +50,40 @@ export class PlaygroundContribution2d implements PlaygroundContribution {
41
50
  gridVisible: false,
42
51
  });
43
52
  }
53
+ if (stateEntity) {
54
+ stateEntity.registerState({
55
+ id: 'FULL_SCREEN',
56
+ icon: 'gedit-toolbar-full',
57
+ title: '全屏',
58
+ priority: 6,
59
+ cancelMode: 'once',
60
+ handle: () => {
61
+ this.commandService.executeCommand(CommonCommands.TOGGLE_MAXIMIZED.id);
62
+ setTimeout(() => {
63
+ playground.config.scrollPageBoundsToCenter();
64
+ }, 10);
65
+ }
66
+ });
67
+ stateEntity.registerState({
68
+ id: 'ADD_ELEMENT',
69
+ icon: 'gedit-toolbar-add',
70
+ title: '添加',
71
+ shortcut: '',
72
+ showTitle: true,
73
+ priority: 0,
74
+ cancelMode: 'once',
75
+ handle: (_: any, e: any) => {
76
+ const dom = e.event.target as HTMLElement;
77
+ const parentNode = (dom.className.match('gedit-toolbar-item') ? dom : dom.parentNode) as HTMLDivElement;
78
+ const rect = parentNode.getBoundingClientRect();
79
+ this.menuRender.render({
80
+ menuPath: this.config.contextMenuPath!,
81
+ anchor: {x: rect.x + rect.width + 8, y: rect.y},
82
+ args: [Editor2dNode.GLOBAL_ADD_EVENT],
83
+ });
84
+ }
85
+ });
86
+ }
44
87
  // 模板编辑器模式
45
88
  if (this.modelOpts.isTemplate) {
46
89
  // playground.getConfigEntity<RulerConfigEntity>(RulerConfigEntity).updateConfig({
@@ -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,6 +111,7 @@ 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
116
  document.updateNode(entity.nodeId, newTransform, true);
100
117
  // lastTransform = newTransform;
@@ -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;
@@ -0,0 +1,203 @@
1
+ import { CanvasDraw, CanvasDrawOpts } from '../playground/canvas-draw';
2
+ import {
3
+ Disposable,
4
+ DisposableCollection,
5
+ DisposableImpl,
6
+ URI,
7
+ Emitter,
8
+ PromiseDeferred,
9
+ SizeSchema,
10
+ debounce
11
+ } from '@gedit/utils';
12
+ import type { Editor2dModel } from '../model/editor2d-model';
13
+ import { Editor2dNode } from '../model';
14
+ import { GameConfig } from '@gedit/render-engine/lib/common';
15
+
16
+ export interface SnapshotOpts extends Omit<CanvasDrawOpts, 'host'> {
17
+ imageType?: string, // default image/webp
18
+ quility?: number, // default 0.8
19
+ limitSize?: SizeSchema // 生成的图片最大值
20
+ appConfig: GameConfig | (() => GameConfig),
21
+ engineName: string
22
+ }
23
+
24
+ export const SnapshotOptsDefault = {
25
+ quility: 0.8,
26
+ imageType: 'image/webp',
27
+ };
28
+
29
+ export interface Snapshot {
30
+ uri: string,
31
+ name?: string,
32
+ version?: string | number,
33
+ imageData: string // base64 data
34
+ }
35
+
36
+ export interface SnapshotInput {
37
+ uri: string,
38
+ name?: string,
39
+ version?: string | number,
40
+ content: Editor2dNode
41
+ }
42
+
43
+ function fixDrawerScale(drawer: CanvasDraw, limitSize: SizeSchema): void {
44
+ // const appConfig = drawer.getData()?.appConfig;
45
+ // if (!appConfig) return;
46
+ const currentSize = drawer.getSceneSize();
47
+ const scale = SizeSchema.fixSize(currentSize!, {
48
+ width: limitSize.width,
49
+ height: limitSize.height,
50
+ });
51
+ drawer.refreshScale(false, scale);
52
+ }
53
+
54
+ export async function createSnapshots(opts: SnapshotOpts, sceneDatas: SnapshotInput[]): Promise<Snapshot[]> {
55
+ const creator = new SnapshotCreator(opts);
56
+ const result = await creator.draw(sceneDatas);
57
+ creator.dispose();
58
+ return result;
59
+ }
60
+
61
+ let host: HTMLElement | undefined;
62
+
63
+ export class SnapshotCreator extends DisposableImpl {
64
+ protected drawer: CanvasDraw;
65
+ protected processing?: PromiseDeferred<void>;
66
+ protected result: { [key: string]: Snapshot } = {};
67
+ protected processingData: SnapshotInput[] = [];
68
+ protected onUpdateEmitter = new Emitter<void>();
69
+ readonly onUpdate = this.onUpdateEmitter.event;
70
+ protected current?: SnapshotInput;
71
+ constructor(
72
+ protected readonly opts: SnapshotOpts
73
+ ) {
74
+ super();
75
+ if (!host) {
76
+ host = document.createElement('div');
77
+ host.className = 'gedit-editor-snapshots';
78
+ host.style.display = 'none';
79
+ document.body.appendChild(host);
80
+ }
81
+ this.drawer = new CanvasDraw({
82
+ host,
83
+ ...opts,
84
+ });
85
+ this.toDispose.pushAll([
86
+ this.drawer,
87
+ this.onUpdateEmitter,
88
+ ]);
89
+ }
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
+ }
106
+ getSnapshot(uri: string): Snapshot | undefined {
107
+ return this.result[uri];
108
+ }
109
+ async draw(scenes: SnapshotInput[]): Promise<Snapshot[]> {
110
+ this.processingData = scenes.filter(s => {
111
+ const result = this.result[s.uri];
112
+ if (result && s.version === result.version) return false;
113
+ if (this.current && this.current.version === s.version) return false;
114
+ return true;
115
+ });
116
+ await this.process();
117
+ return scenes.map(s => this.result[s.uri]!);
118
+ }
119
+ protected process(): Promise<void> {
120
+ if (this.processing) {
121
+ return this.processing.promise;
122
+ }
123
+ const deferred = this.processing = new PromiseDeferred<void>();
124
+ const { drawer } = this;
125
+ const dispose = new DisposableCollection();
126
+ dispose.push(Disposable.create(() => {
127
+ deferred.resolve();
128
+ this.current = undefined;
129
+ this.processing = undefined;
130
+ }));
131
+ dispose.push(
132
+ drawer!.onLoading(e => {
133
+ const {loaded, allCount, currentCount} = e;
134
+ if (loaded || allCount <= currentCount) {
135
+ const widget = drawer!.renderWidget!;
136
+
137
+ if (this.opts.limitSize) {
138
+ fixDrawerScale(drawer, this.opts.limitSize);
139
+ } else {
140
+ drawer.refreshScale(false, 1);
141
+ }
142
+ widget.createSnapShot(this.opts.imageType || SnapshotOptsDefault.imageType,
143
+ this.opts.quility || SnapshotOptsDefault.quility).then(imageData => {
144
+ const current = this.current;
145
+ if (current) {
146
+ this.result[current.uri] = {
147
+ uri: current.uri,
148
+ version: current.version,
149
+ name: current.name,
150
+ imageData
151
+ };
152
+ this.onUpdateEmitter.fire();
153
+ }
154
+ this.next(() => dispose.dispose());
155
+ });
156
+ }
157
+ }),
158
+ );
159
+ this.next(() => dispose.dispose());
160
+ return deferred.promise;
161
+ }
162
+ protected next(end: () => void): void {
163
+ const next = this.processingData.shift();
164
+ if (next) {
165
+ this.current = next;
166
+ this.drawer.renderWidget?.currentScene?.dispose();
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
+ }
175
+ this.drawer.update({
176
+ visible: true,
177
+ uri: next.uri,
178
+ engineName: this.opts.engineName,
179
+ appConfig,
180
+ content,
181
+ });
182
+ } else {
183
+ end();
184
+ }
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
+ widget.createSnapShot(this.opts.imageType, quility).then((base64: string) => fn(base64));
200
+ }
201
+ }, delay));
202
+ }
203
+ }