-
Notifications
You must be signed in to change notification settings - Fork 2
/
quick-action-ui.ts
314 lines (279 loc) · 11.8 KB
/
quick-action-ui.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
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
import {
Action,
Bounds,
BoundsAware,
CursorCSS,
EditorContextService,
GEdge,
GLSPMouseTool,
GModelElement,
GModelRoot,
GRoutableElement,
IActionDispatcherProvider,
IActionHandler,
ICommand,
ISelectionListener,
MouseListener,
RemoveMarqueeAction,
SelectAllAction,
SelectionService,
SetUIExtensionVisibilityAction,
TYPES,
getAbsoluteBounds,
isNotUndefined
} from '@eclipse-glsp/client';
import { inject, injectable, multiInject, postConstruct } from 'inversify';
import { createElement, createIcon } from '../../utils/ui-utils';
import { Edge } from '../../diagram/model';
import { IVY_TYPES } from '../../types';
import { getAbsoluteEdgeBounds } from '../../utils/diagram-utils';
import { Menu } from '../menu/menu';
import { isQuickActionAware } from './model';
import { QuickAction, QuickActionLocation, QuickActionProvider } from './quick-action';
import { InfoQuickActionMenu, QuickActionMenu, ShowInfoQuickActionMenuAction, ShowQuickActionMenuAction } from './quick-action-menu-ui';
import { IvyUIExtension } from '../../utils/ivy-ui-extension';
@injectable()
export class QuickActionUI extends IvyUIExtension implements IActionHandler, ISelectionListener {
static readonly ID = 'quickActionsUi';
private activeQuickActions: QuickAction[] = [];
private activeQuickActionBtn?: HTMLElement;
private quickActionBar?: HTMLElement;
private quickActionMenu?: Menu;
@inject(TYPES.IActionDispatcherProvider) public actionDispatcherProvider: IActionDispatcherProvider;
@inject(SelectionService) protected selectionService: SelectionService;
@inject(GLSPMouseTool) protected mouseTool: GLSPMouseTool;
@inject(EditorContextService) protected readonly editorContext: EditorContextService;
@multiInject(IVY_TYPES.QuickActionProvider) protected quickActionProviders: QuickActionProvider[];
public id(): string {
return QuickActionUI.ID;
}
@postConstruct()
protected init(): void {
this.selectionService.onSelectionChanged(event => this.selectionChanged(event.root, event.selectedElements));
this.mouseTool.register(new QuickActionUiMouseListener(this));
}
public containerClass(): string {
return 'quick-actions-container';
}
public getActiveQuickActions(): QuickAction[] {
return this.activeQuickActions;
}
protected initializeContents(containerElement: HTMLElement): void {
containerElement.style.position = 'absolute';
containerElement.onwheel = ev => (ev.ctrlKey ? ev.preventDefault() : true);
}
selectionChanged(root: Readonly<GModelRoot>, selectedElements: string[]): void {
if (this.editorContext.modelRoot.cssClasses?.includes(CursorCSS.MARQUEE) || selectedElements.length < 1) {
this.hideUi();
} else {
this.showUi();
}
}
handle(action: Action): void | Action | ICommand {
if (ShowQuickActionMenuAction.is(action)) {
this.showItemMenu(action);
}
if (ShowInfoQuickActionMenuAction.is(action)) {
this.showSimpleMenu(action);
}
if (RemoveMarqueeAction.is(action) && this.editorContext.selectedElements.length > 0) {
this.showUi();
}
}
private showSimpleMenu(action: ShowInfoQuickActionMenuAction): void {
this.removeMenu();
this.quickActionMenu = new InfoQuickActionMenu(action);
this.createMenu(this.quickActionMenu);
}
private async showItemMenu(action: ShowQuickActionMenuAction): Promise<void> {
this.removeMenu();
if (action.elementIds.length > 0) {
this.quickActionMenu = new QuickActionMenu(await this.actionDispatcherProvider(), action);
this.createMenu(this.quickActionMenu);
} else {
this.setActiveQuickActionBtn();
}
}
private createMenu(quickActionMenu: Menu): void {
if (this.quickActionBar) {
const menu = quickActionMenu.create(this.containerElement);
if (menu.offsetWidth > this.quickActionBar.offsetWidth) {
menu.classList.add('border-radius');
this.quickActionBar.classList.add('no-bottom-border-radius');
this.shiftBar(menu, 8);
} else {
this.shiftBar(menu, 24);
}
}
}
private removeMenu(): void {
this.quickActionMenu?.remove();
this.quickActionMenu = undefined;
}
public showUi(): void {
this.activeQuickActions = [];
this.actionDispatcherProvider().then(actionDispatcher =>
actionDispatcher.dispatch(
SetUIExtensionVisibilityAction.create({
extensionId: QuickActionUI.ID,
visible: true,
contextElementsId: [...this.selectionService.getSelectedElementIDs()]
})
)
);
}
public hideUi(): void {
this.activeQuickActions = [];
this.actionDispatcherProvider().then(actionDispatcher =>
actionDispatcher.dispatch(SetUIExtensionVisibilityAction.create({ extensionId: QuickActionUI.ID, visible: false }))
);
}
protected onBeforeShow(containerElement: HTMLElement, root: Readonly<GModelRoot>, ...contextElementIds: string[]): void {
containerElement.innerHTML = '';
const elements = getElements(contextElementIds, root);
const elementsWithoutEdges = elements.filter(e => !(e instanceof GRoutableElement) || !(e instanceof Edge));
if (elementsWithoutEdges.length > 1) {
this.showMultiQuickActionUi(containerElement, elementsWithoutEdges);
} else if (elements.length === 1 && elements[0] instanceof GEdge && isQuickActionAware(elements[0])) {
this.showEdgeQuickActionUi(containerElement, elements[0]);
} else {
const element = getFirstQuickActionElement(elementsWithoutEdges, root);
this.showSingleQuickActionUi(containerElement, element);
}
}
private showMultiQuickActionUi(containerElement: HTMLElement, elements: GModelElement[]): void {
this.activeQuickActions = this.loadMultiQuickActions(elements);
if (this.activeQuickActions.length === 0) {
return;
}
const selectionBounds = elements.map(e => getAbsoluteBounds(e)).reduce((b1, b2) => Bounds.combine(b1, b2));
this.quickActionBar = this.createQuickActionsBar(containerElement, selectionBounds, true);
this.createQuickActions(this.quickActionBar, this.activeQuickActions);
this.shiftBar(this.quickActionBar);
}
private showSingleQuickActionUi(containerElement: HTMLElement, element: GModelElement & BoundsAware): void {
if (isNotUndefined(element)) {
this.activeQuickActions = this.loadSingleQuickActions(element);
if (this.activeQuickActions.length === 0) {
return;
}
this.quickActionBar = this.createQuickActionsBar(containerElement, getAbsoluteBounds(element));
this.createQuickActions(this.quickActionBar, this.activeQuickActions);
this.shiftBar(this.quickActionBar);
}
}
private showEdgeQuickActionUi(containerElement: HTMLElement, edge: GEdge): void {
if (isNotUndefined(edge) && !edge.id.endsWith('_feedback_edge') && edge.source && edge.target) {
this.activeQuickActions = this.loadSingleQuickActions(edge);
if (this.activeQuickActions.length === 0) {
return;
}
const absoluteBounds = getAbsoluteEdgeBounds(edge);
this.quickActionBar = this.createQuickActionsBar(containerElement, absoluteBounds);
this.createQuickActions(this.quickActionBar, this.activeQuickActions);
this.shiftBar(this.quickActionBar);
}
}
private shiftBar(bar: HTMLElement, distanceToWindow = 16): void {
const bounds = bar.getBoundingClientRect();
let shift = bounds.width / 2;
const maxShift = bounds.x - distanceToWindow;
if (shift > maxShift) {
shift = maxShift;
}
const minShift = bounds.x + bounds.width + distanceToWindow - shift;
if (minShift > window.innerWidth) {
shift += minShift - window.innerWidth;
}
bar.style.marginLeft = `${-shift}px`;
}
private createQuickActionsBar(containerElement: HTMLElement, parentBounds: Bounds, drawSelectionBox = false): HTMLElement {
const diagramContainer = document.getElementById(this.diagramContainerId);
const offset = diagramContainer?.offsetTop ?? 0;
containerElement.style.left = `${parentBounds.x + parentBounds.width / 2}px`;
containerElement.style.top = `${offset + parentBounds.y + parentBounds.height}px`;
if (drawSelectionBox) {
const selectionDiv = createElement('div', ['multi-selection-box']);
selectionDiv.style.marginLeft = `-${parentBounds.width / 2}px`;
selectionDiv.style.marginTop = `-${parentBounds.height}px`;
selectionDiv.style.height = `${parentBounds.height + 10}px`;
selectionDiv.style.width = `${parentBounds.width + 10}px`;
containerElement.appendChild(selectionDiv);
}
const bar = createElement('div', ['quick-actions-bar']);
containerElement.appendChild(bar);
return bar;
}
private createQuickActions(bar: HTMLElement, quickActions: QuickAction[]): void {
this.createQuickActionGroup(bar, quickActions, 'Left');
this.createQuickActionGroup(bar, quickActions, 'Middle');
this.createQuickActionGroup(bar, quickActions, 'Right');
}
private createQuickActionGroup(bar: HTMLElement, quickActions: QuickAction[], location: QuickActionLocation): void {
const group = createElement('div', ['quick-actions-group']);
quickActions
.filter(quickAction => quickAction.location === location)
.sort((a, b) => a.sorting.localeCompare(b.sorting))
.forEach(quickAction => {
group.appendChild(this.createQuickActionBtn(quickAction));
});
if (group.children.length > 0) {
bar.appendChild(group);
}
}
private createQuickActionBtn(quickAction: QuickAction): HTMLElement {
const button = createElement('span');
button.appendChild(createIcon(quickAction.icon));
button.title = quickAction.title;
const actions = [quickAction.action];
if (!quickAction.letQuickActionsOpen) {
actions.push(SetUIExtensionVisibilityAction.create({ extensionId: QuickActionUI.ID, visible: false }));
}
if (quickAction.removeSelection) {
actions.push(SelectAllAction.create(false));
}
button.onclick = () => this.triggerQuickActionBtn(button, actions);
return button;
}
private triggerQuickActionBtn(button: HTMLElement, actions: Action[]): void {
if (this.activeQuickActionBtn === button && this.quickActionMenu) {
this.setActiveQuickActionBtn();
this.removeMenu();
} else {
this.setActiveQuickActionBtn(button);
this.actionDispatcherProvider().then(dispatcher => dispatcher.dispatchAll(actions));
}
}
private setActiveQuickActionBtn(button?: HTMLElement): void {
this.activeQuickActionBtn?.classList.remove('active');
this.activeQuickActionBtn = button;
this.activeQuickActionBtn?.classList.add('active');
}
private loadSingleQuickActions(element: GModelElement): QuickAction[] {
return this.filterQuickActions(this.quickActionProviders.map(provider => provider.singleQuickAction(element)));
}
private loadMultiQuickActions(elements: GModelElement[]): QuickAction[] {
return this.filterQuickActions(this.quickActionProviders.map(provider => provider.multiQuickAction(elements)));
}
private filterQuickActions(quickActions: (QuickAction | undefined)[]): QuickAction[] {
return quickActions.filter(isNotUndefined).filter(quickAction => !this.isReadonly() || quickAction.readonlySupport);
}
protected isReadonly(): boolean {
return this.editorContext.isReadonly;
}
}
export class QuickActionUiMouseListener extends MouseListener {
constructor(private quickActionUi: QuickActionUI) {
super();
}
wheel(target: GModelElement, event: WheelEvent): Action[] {
this.quickActionUi.showUi();
return [];
}
}
function getElements(contextElementIds: string[], root: Readonly<GModelRoot>): GModelElement[] {
return contextElementIds.map(id => root.index.getById(id)).filter(isNotUndefined);
}
function getFirstQuickActionElement(elements: GModelElement[], root: Readonly<GModelRoot>): GModelElement & BoundsAware {
return elements.filter(isQuickActionAware)[0];
}