-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
doc.render-controller.ts
228 lines (182 loc) · 8.25 KB
/
doc.render-controller.ts
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
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { DocumentDataModel, EventState, ICommandInfo, Nullable } from '@univerjs/core';
import { ICommandService, IConfigService, RxDisposable } from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { DOCS_COMPONENT_BACKGROUND_LAYER_INDEX, DOCS_COMPONENT_DEFAULT_Z_INDEX, DOCS_COMPONENT_HEADER_LAYER_INDEX, DOCS_COMPONENT_MAIN_LAYER_INDEX, DOCS_VIEW_KEY, DocSkeletonManagerService, RichTextEditingMutation, VIEWPORT_KEY } from '@univerjs/docs';
import type { DocumentSkeleton, IRenderContext, IRenderModule, IWheelEvent } from '@univerjs/engine-render';
import { DocBackground, Documents, EVENT_TYPE, Layer, PageLayoutType, ScrollBar, Viewport } from '@univerjs/engine-render';
import { IEditorService } from '@univerjs/ui';
import { Inject } from '@wendellhu/redi';
import { takeUntil } from 'rxjs';
export class DocRenderController extends RxDisposable implements IRenderModule {
constructor(
private readonly _context: IRenderContext<DocumentDataModel>,
@ICommandService private readonly _commandService: ICommandService,
@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,
@IConfigService private readonly _configService: IConfigService,
@IEditorService private readonly _editorService: IEditorService
) {
super();
this._addNewRender();
this._initRenderRefresh();
this._initCommandListener();
}
private _addNewRender() {
const { scene, engine, unit } = this._context;
const viewMain = new Viewport(VIEWPORT_KEY.VIEW_MAIN, scene, {
left: 0,
top: 0,
bottom: 0,
right: 0,
isRelativeX: true,
isRelativeY: true,
isWheelPreventDefaultX: true,
});
scene.attachControl();
scene.on(EVENT_TYPE.wheel, (evt: unknown, state: EventState) => {
const e = evt as IWheelEvent;
if (e.ctrlKey) {
const deltaFactor = Math.abs(e.deltaX);
let scrollNum = deltaFactor < 40 ? 0.2 : deltaFactor < 80 ? 0.4 : 0.2;
scrollNum *= e.deltaY > 0 ? -1 : 1;
if (scene.scaleX < 1) {
scrollNum /= 2;
}
if (scene.scaleX + scrollNum > 4) {
scene.scale(4, 4);
} else if (scene.scaleX + scrollNum < 0.1) {
scene.scale(0.1, 0.1);
} else {
// const value = e.deltaY > 0 ? 0.1 : -0.1;
// scene.scaleBy(scrollNum, scrollNum);
e.preventDefault();
}
} else {
viewMain.onMouseWheel(e, state);
}
});
const hasScroll = this._configService.getConfig('hasScroll') as Nullable<boolean>;
if (hasScroll !== false) {
new ScrollBar(viewMain);
}
scene.addLayer(
new Layer(scene, [], DOCS_COMPONENT_MAIN_LAYER_INDEX),
new Layer(scene, [], DOCS_COMPONENT_HEADER_LAYER_INDEX)
);
this._addComponent();
const should = unit.getShouldRenderLoopImmediately();
if (should) {
engine.runRenderLoop(() => {
scene.render();
});
}
}
private _addComponent() {
const { scene, unit: documentModel, components } = this._context;
const config = {
pageMarginLeft: documentModel.documentStyle.marginLeft || 0,
pageMarginTop: documentModel.documentStyle.marginTop || 0,
};
const documents = new Documents(DOCS_VIEW_KEY.MAIN, undefined, config);
documents.zIndex = DOCS_COMPONENT_DEFAULT_Z_INDEX;
const docBackground = new DocBackground(DOCS_VIEW_KEY.BACKGROUND, undefined, config);
docBackground.zIndex = DOCS_COMPONENT_DEFAULT_Z_INDEX;
this._context.mainComponent = documents;
components.set(DOCS_VIEW_KEY.MAIN, documents);
components.set(DOCS_VIEW_KEY.BACKGROUND, docBackground);
scene.addObjects([documents], DOCS_COMPONENT_MAIN_LAYER_INDEX);
scene.addObjects([docBackground], DOCS_COMPONENT_BACKGROUND_LAYER_INDEX);
if (this._editorService.getEditor(documentModel.getUnitId()) == null) {
scene.enableLayerCache(DOCS_COMPONENT_MAIN_LAYER_INDEX);
}
}
private _initRenderRefresh() {
this._docSkeletonManagerService.currentSkeletonBefore$.pipe(takeUntil(this.dispose$)).subscribe((param) => {
this._create(param);
});
}
private _create(skeleton: Nullable<DocumentSkeleton>) {
if (!skeleton) {
return;
}
const { mainComponent, components } = this._context;
const docsComponent = mainComponent as Documents;
const docBackground = components.get(DOCS_VIEW_KEY.BACKGROUND) as DocBackground;
docsComponent.changeSkeleton(skeleton);
docBackground.changeSkeleton(skeleton);
this._recalculateSizeBySkeleton(skeleton);
}
private _initCommandListener() {
const updateCommandList = [RichTextEditingMutation.id];
this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {
// TODO@Jocs: performance, only update the skeleton when the command is related to the current unit.
if (updateCommandList.includes(command.id)) {
const params = command.params as IRichTextEditingMutationParams;
const { unitId } = params;
const skeleton = this._docSkeletonManagerService.getSkeleton();
if (!skeleton) {
return;
}
// TODO: `disabled` is only used for read only demo, and will be removed in the future.
const disabled = !!skeleton.getViewModel().getDataModel().getSnapshot().disabled;
if (disabled) {
return;
}
skeleton.calculate();
if (this._editorService.isEditor(unitId)) {
this._context.mainComponent?.makeDirty();
return;
}
this._recalculateSizeBySkeleton(skeleton);
}
}));
}
private _recalculateSizeBySkeleton(skeleton: DocumentSkeleton) {
const { mainComponent, scene, unitId, components } = this._context;
const docsComponent = mainComponent as Documents;
const docBackground = components.get(DOCS_VIEW_KEY.BACKGROUND) as DocBackground;
const pages = skeleton.getSkeletonData()?.pages;
if (pages == null) {
return;
}
let width = 0;
let height = 0;
for (let i = 0, len = pages.length; i < len; i++) {
const page = pages[i];
const { pageWidth, pageHeight } = page;
if (docsComponent.pageLayoutType === PageLayoutType.VERTICAL) {
height += pageHeight;
height += docsComponent.pageMarginTop;
if (i === len - 1) {
height += docsComponent.pageMarginTop;
}
width = Math.max(width, pageWidth);
} else if (docsComponent.pageLayoutType === PageLayoutType.HORIZONTAL) {
width += pageWidth;
if (i !== len - 1) {
width += docsComponent.pageMarginLeft;
}
height = Math.max(height, pageHeight);
}
}
docsComponent.resize(width, height);
docBackground.resize(width, height);
if (!this._editorService.isEditor(unitId)) {
scene.resize(width, height);
}
}
}