-
-
Notifications
You must be signed in to change notification settings - Fork 458
/
docs-ui-plugin.ts
172 lines (155 loc) · 5.86 KB
/
docs-ui-plugin.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
/**
* 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 {
ILogService,
IUniverInstanceService,
LocaleService,
Plugin,
Tools,
UniverInstanceType,
} from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
import { IEditorService, IShortcutService } from '@univerjs/ui';
import { IRenderManagerService } from '@univerjs/engine-render';
import {
MoveCursorDownShortcut,
MoveCursorLeftShortcut,
MoveCursorRightShortcut,
MoveCursorUpShortcut,
MoveSelectionDownShortcut,
MoveSelectionLeftShortcut,
MoveSelectionRightShortcut,
MoveSelectionUpShortcut,
SelectAllShortcut,
} from './shortcuts/cursor.shortcut';
import type { IUniverDocsUIConfig } from './basics';
import { DefaultDocUiConfig } from './basics';
import { DOC_UI_PLUGIN_NAME } from './basics/const/plugin-name';
import { AppUIController } from './controllers';
import { DocUIController } from './controllers/doc-ui.controller';
import { BreakLineShortcut, DeleteLeftShortcut, DeleteRightShortcut } from './shortcuts/core-editing.shortcut';
import { DocClipboardService, IDocClipboardService } from './services/clipboard/clipboard.service';
import { DocClipboardController } from './controllers/clipboard.controller';
import { DocEditorBridgeController } from './controllers/doc-editor-bridge.controller';
import { DocRenderController } from './views/doc-canvas-view';
import { DocFloatingObjectController } from './controllers/doc-floating-object.controller';
import { DocZoomRenderController } from './controllers/zoom.controller';
import { TextSelectionController } from './controllers/text-selection.controller';
import { BackScrollController } from './controllers/back-scroll.controller';
import { DocCanvasPopManagerService } from './services/doc-popup-manager.service';
import { DocsRenderService } from './services/doc-render.service';
export class UniverDocsUIPlugin extends Plugin {
static override pluginName = DOC_UI_PLUGIN_NAME;
static override type = UniverInstanceType.UNIVER_DOC;
constructor(
private readonly _config: IUniverDocsUIConfig,
@Inject(Injector) override _injector: Injector,
@Inject(LocaleService) private readonly _localeService: LocaleService,
@IRenderManagerService private readonly _renderManagerSrv: IRenderManagerService,
@ILogService private _logService: ILogService
) {
super();
this._config = Tools.deepMerge({}, DefaultDocUiConfig, this._config);
this._initDependencies(_injector);
this._initializeCommands();
}
override onReady(): void {
this._initRenderBasics();
}
override onRendered(): void {
this._initUI();
this._markDocAsFocused();
}
private _initializeCommands(): void {
[
MoveCursorUpShortcut,
MoveCursorDownShortcut,
MoveCursorRightShortcut,
MoveCursorLeftShortcut,
MoveSelectionUpShortcut,
MoveSelectionDownShortcut,
MoveSelectionLeftShortcut,
MoveSelectionRightShortcut,
SelectAllShortcut,
DeleteLeftShortcut,
DeleteRightShortcut,
BreakLineShortcut,
].forEach((shortcut) => {
this._injector.get(IShortcutService).registerShortcut(shortcut);
});
}
private _initDependencies(injector: Injector) {
const dependencies: Dependency[] = [
// Controller
[
DocUIController,
{
useFactory: () => this._injector.createInstance(DocUIController, this._config),
},
],
[DocClipboardController],
// Some of the controllers here should be moved to RenderUnit.
[DocEditorBridgeController],
[DocsRenderService],
[DocFloatingObjectController],
[TextSelectionController],
[BackScrollController],
[
// controllers
AppUIController,
{
useFactory: () => this._injector.createInstance(AppUIController, this._config),
},
],
[
IDocClipboardService,
{
useClass: DocClipboardService,
},
],
[DocCanvasPopManagerService],
];
dependencies.forEach((d) => {
injector.add(d);
});
}
private _markDocAsFocused() {
const currentService = this._injector.get(IUniverInstanceService);
const editorService = this._injector.get(IEditorService);
try {
const doc = currentService.getCurrentUniverDocInstance();
if (!doc) return;
const id = doc.getUnitId();
if (!editorService.isEditor(id)) {
currentService.focusUnit(doc.getUnitId());
}
} catch (err) {
this._logService.warn(err);
}
}
private _initUI(): void {
this._injector.get(AppUIController);
}
private _initRenderBasics(): void {
([
DocRenderController,
DocZoomRenderController,
]).forEach((m) => {
this._renderManagerSrv.registerRenderController(UniverInstanceType.UNIVER_DOC, m);
});
}
}