/
ContextToolbar.ts
315 lines (274 loc) · 11.1 KB
/
ContextToolbar.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
315
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
import { AddEventsBehaviour, AlloyEvents, AlloySpec, AlloyTriggers, AnchorSpec, Behaviour, Boxes, Bubble, GuiFactory, InlineView, Keying, Layout, LayoutInside, MaxHeight, MaxWidth, Positioning } from '@ephox/alloy';
import { Objects } from '@ephox/boulder';
import { Toolbar } from '@ephox/bridge';
import { Element as DomElement } from '@ephox/dom-globals';
import { Cell, Id, Merger, Option, Result, Thunk } from '@ephox/katamari';
import { PlatformDetection } from '@ephox/sand';
import { Css, Element, Focus, Scroll } from '@ephox/sugar';
import Editor from 'tinymce/core/api/Editor';
import Delay from 'tinymce/core/api/util/Delay';
import { hideContextToolbarEvent, showContextToolbarEvent } from './ui/context/ContextEditorEvents';
import { ContextForm } from './ui/context/ContextForm';
import * as ContextToolbarBounds from './ui/context/ContextToolbarBounds';
import ToolbarLookup from './ui/context/ContextToolbarLookup';
import ToolbarScopes, { ScopedToolbars } from './ui/context/ContextToolbarScopes';
import { forwardSlideEvent, renderContextToolbar } from './ui/context/ContextUi';
import { renderToolbar } from './ui/toolbar/CommonToolbar';
import { identifyButtons } from './ui/toolbar/Integration';
const bubbleSize = 12;
const bubbleAlignments = {
valignCentre: [],
alignCentre: [],
alignLeft: ['tox-pop--align-left'],
alignRight: ['tox-pop--align-right'],
right: ['tox-pop--right'],
left: ['tox-pop--left'],
bottom: ['tox-pop--bottom'],
top: ['tox-pop--top']
};
const anchorOverrides = {
maxHeightFunction: MaxHeight.expandable(),
maxWidthFunction: MaxWidth.expandable()
};
// On desktop we prioritise north-then-south because it's cleaner, but on mobile we prioritise south to try to avoid overlapping with native context toolbars
const desktopAnchorSpecLayouts = {
onLtr: () => [Layout.north, Layout.south, Layout.northeast, Layout.southeast, Layout.northwest, Layout.southwest,
LayoutInside.north, LayoutInside.south, LayoutInside.northeast, LayoutInside.southeast, LayoutInside.northwest, LayoutInside.southwest],
onRtl: () => [Layout.north, Layout.south, Layout.northwest, Layout.southwest, Layout.northeast, Layout.southeast,
LayoutInside.north, LayoutInside.south, LayoutInside.northwest, LayoutInside.southwest, LayoutInside.northeast, LayoutInside.southeast]
};
const mobileAnchorSpecLayouts = {
onLtr: () => [Layout.south, Layout.southeast, Layout.southwest, Layout.northeast, Layout.northwest, Layout.north,
LayoutInside.north, LayoutInside.south, LayoutInside.northeast, LayoutInside.southeast, LayoutInside.northwest, LayoutInside.southwest],
onRtl: () => [Layout.south, Layout.southwest, Layout.southeast, Layout.northwest, Layout.northeast, Layout.north,
LayoutInside.north, LayoutInside.south, LayoutInside.northwest, LayoutInside.southwest, LayoutInside.northeast, LayoutInside.southeast]
};
const getAnchorLayout = (position: Toolbar.ContextToolbarPosition, isTouch: boolean): Partial<AnchorSpec> => {
if (position === 'line') {
return {
bubble: Bubble.nu(bubbleSize, 0, bubbleAlignments),
layouts: {
onLtr: () => [ Layout.east ],
onRtl: () => [ Layout.west ]
},
overrides: anchorOverrides
};
} else {
return {
bubble: Bubble.nu(0, bubbleSize, bubbleAlignments),
layouts: isTouch ? mobileAnchorSpecLayouts : desktopAnchorSpecLayouts,
overrides: anchorOverrides
};
}
};
const register = (editor: Editor, registryContextToolbars, sink, extras) => {
const isTouch = PlatformDetection.detect().deviceType.isTouch;
const contextbar = GuiFactory.build(
renderContextToolbar({
sink,
onEscape: () => {
editor.focus();
return Option.some(true);
}
})
);
const getBounds = () => ContextToolbarBounds.getContextToolbarBounds(editor);
const isRangeOverlapping = (aTop: number, aBottom: number, bTop: number, bBottom: number) => {
return Math.max(aTop, bTop) <= Math.min(aBottom, bBottom);
};
const getLastElementVerticalBound = () => {
const nodeBounds = lastElement.get().map((ele) => ele.getBoundingClientRect()).getOrThunk(() => {
return editor.selection.getRng().getBoundingClientRect();
});
// Translate to the top level document, as nodeBounds is relative to the iframe viewport
const diffTop = editor.inline ? Scroll.get().top() : Boxes.absolute(Element.fromDom(editor.getBody())).y();
return {
y: nodeBounds.top + diffTop,
bottom: nodeBounds.bottom + diffTop
};
};
const shouldContextToolbarHide = (): boolean => {
// If a mobile context menu is open, don't launch else they'll probably overlap. For android, specifically.
if (isTouch() && extras.backstage.isContextMenuOpen()) {
return true;
}
const lastElementBounds = getLastElementVerticalBound();
const contextToolbarBounds = getBounds();
// If the element bound isn't overlapping with the contexToolbarBound, the contextToolbar should hide
return !isRangeOverlapping(
lastElementBounds.y,
lastElementBounds.bottom,
contextToolbarBounds.y(),
contextToolbarBounds.bottom()
);
};
const forceHide = () => {
InlineView.hide(contextbar);
};
// FIX: make a lot nicer.
const hideOrRepositionIfNecessary = () => {
lastAnchor.get().each((anchor) => {
const contextBarEle = contextbar.element();
Css.remove(contextBarEle, 'display');
if (shouldContextToolbarHide()) {
Css.set(contextBarEle, 'display', 'none');
} else {
Positioning.positionWithinBounds(sink, anchor, contextbar, Option.some(getBounds()));
}
});
};
const lastAnchor = Cell(Option.none<AnchorSpec>());
const lastElement = Cell<Option<DomElement>>(Option.none<DomElement>());
const timer = Cell(null);
const wrapInPopDialog = (toolbarSpec: AlloySpec) => {
return {
dom: {
tag: 'div',
classes: ['tox-pop__dialog'],
},
components: [toolbarSpec],
behaviours: Behaviour.derive([
Keying.config({
mode: 'acyclic'
}),
AddEventsBehaviour.config('pop-dialog-wrap-events', [
AlloyEvents.runOnAttached((comp) => {
editor.shortcuts.add('ctrl+F9', 'focus statusbar', () => Keying.focusIn(comp));
}),
AlloyEvents.runOnDetached((comp) => {
editor.shortcuts.remove('ctrl+F9');
})
])
])
};
};
const getScopes: () => ScopedToolbars = Thunk.cached(() => {
return ToolbarScopes.categorise(registryContextToolbars, (toolbarApi) => {
const alloySpec = buildToolbar(toolbarApi);
AlloyTriggers.emitWith(contextbar, forwardSlideEvent, {
forwardContents: wrapInPopDialog(alloySpec)
});
});
});
const buildToolbar = (ctx): AlloySpec => {
const { buttons } = editor.ui.registry.getAll();
const scopes = getScopes();
return ctx.type === 'contexttoolbar' ? (() => {
const allButtons = Merger.merge(buttons, scopes.formNavigators);
const initGroups = identifyButtons(editor, { buttons: allButtons, toolbar: ctx.items }, extras, Option.some([ 'form:' ]));
return renderToolbar({
uid: Id.generate('context-toolbar'),
initGroups,
onEscape: Option.none,
cyclicKeying: true,
backstage: extras.backstage,
getSink: () => Result.error('')
});
})() : (() => {
return ContextForm.renderContextForm(ctx, extras.backstage);
})();
};
editor.on(showContextToolbarEvent, (e) => {
const scopes = getScopes();
// TODO: Have this stored in a better structure
Objects.readOptFrom<Toolbar.ContextToolbar | Toolbar.ContextForm>(scopes.lookupTable, e.toolbarKey).each((ctx) => {
launchContext(ctx, e.target === editor ? Option.none() : Option.some(e as DomElement));
// Forms launched via this way get immediate focus
InlineView.getContent(contextbar).each(Keying.focusIn);
});
});
const getAnchor = (position: Toolbar.ContextToolbarPosition, element: Option<Element>): AnchorSpec => {
const anchorage = position === 'node' ? extras.backstage.shared.anchors.node(element) : extras.backstage.shared.anchors.cursor();
return Merger.deepMerge(
anchorage,
getAnchorLayout(position, isTouch())
);
};
const launchContext = (toolbarApi: Toolbar.ContextToolbar | Toolbar.ContextForm, elem: Option<DomElement>) => {
clearTimer();
// If a mobile context menu is open, don't launch else they'll probably overlap. For android, specifically.
if (isTouch() && extras.backstage.isContextMenuOpen()) {
return;
}
const toolbarSpec = buildToolbar(toolbarApi);
const sElem = elem.map(Element.fromDom);
const anchor = getAnchor(toolbarApi.position, sElem);
lastAnchor.set(Option.some((anchor)));
lastElement.set(elem);
const contextBarEle = contextbar.element();
Css.remove(contextBarEle, 'display');
InlineView.showWithinBounds(contextbar, anchor, wrapInPopDialog(toolbarSpec), () => Option.some(getBounds()));
// It's possible we may have launched offscreen, if so then hide
if (shouldContextToolbarHide()) {
Css.set(contextBarEle, 'display', 'none');
}
};
const launchContextToolbar = () => {
const scopes = getScopes();
ToolbarLookup.lookup(scopes, editor).fold(
() => {
lastAnchor.set(Option.none());
InlineView.hide(contextbar);
},
(info) => {
launchContext(info.toolbarApi, Option.some(info.elem.dom()));
}
);
};
const clearTimer = () => {
const current = timer.get();
if (current !== null) {
Delay.clearTimeout(current);
timer.set(null);
}
};
const resetTimer = (t) => {
clearTimer();
timer.set(t);
};
editor.on('init', () => {
editor.on(hideContextToolbarEvent, forceHide);
editor.on('ScrollContent ScrollWindow longpress', hideOrRepositionIfNecessary);
// FIX: Make it go away when the action makes it go away. E.g. deleting a column deletes the table.
editor.on('click keyup SetContent ObjectResized ResizeEditor', (e) => {
// Fixing issue with chrome focus on img.
resetTimer(
Delay.setEditorTimeout(editor, launchContextToolbar, 0)
);
});
editor.on('focusout', (e) => {
Delay.setEditorTimeout(editor, () => {
if (Focus.search(sink.element()).isNone() && Focus.search(contextbar.element()).isNone()) {
lastAnchor.set(Option.none());
InlineView.hide(contextbar);
}
}, 0);
});
editor.on('SwitchMode', () => {
if (editor.readonly) {
lastAnchor.set(Option.none());
InlineView.hide(contextbar);
}
});
editor.on('NodeChange', (e) => {
Focus.search(contextbar.element()).fold(
() => {
resetTimer(
Delay.setEditorTimeout(editor, launchContextToolbar, 0)
);
},
(_) => {
}
);
});
});
};
export default {
register
};