1
0
mirror of https://github.com/convergencelabs/monaco-collab-ext.git synced 2024-10-27 20:34:17 +00:00

Compare commits

...

9 Commits

Author SHA1 Message Date
Alec LaLonde
d13b0ec4d5
Merge pull request from CGNonofr/show-tooltip-on-hover
Show tooltip on cursor hover
2022-09-19 20:40:23 -06:00
Alec LaLonde
6edfb6253d
Merge branch 'master' into show-tooltip-on-hover 2022-09-19 20:40:14 -06:00
Alec LaLonde
03f782bda8
Merge pull request from CodinGame/fix-label-xss
Prevent XSS in tooltip label
2022-09-19 20:30:32 -06:00
Loïc Mangeonjean
e37ce9b99c fix: prevent XSS in tooltip label 2022-09-15 10:28:11 +02:00
Alec LaLonde
cdefbb151f
Merge pull request from CodinGame/provide-class-name-to-tweak-display
Allow adding custom element class names
2022-06-22 14:22:28 -06:00
Samuel Olivier
91bdec1bb7 add additional elements class names 2022-06-13 10:19:46 +02:00
Loïc Mangeonjean
38713d59b9 Add showTooltipOnHover option 2022-05-18 15:10:45 +02:00
Loïc Mangeonjean
a3b0b8a57c Add missing type 2022-05-18 15:09:57 +02:00
Loïc Mangeonjean
4c03bc00eb Refactor tooltip management a little 2022-05-18 15:09:46 +02:00
2 changed files with 45 additions and 12 deletions

View File

@ -23,6 +23,11 @@ export interface IRemoteCursorManagerOptions {
*/
editor: monaco.editor.ICodeEditor;
/**
* An additional class name for the cursor element, to tweak the default style
*/
className?: string;
/**
* Determines if tooltips will be shown when the cursor is moved.
*/
@ -33,6 +38,16 @@ export interface IRemoteCursorManagerOptions {
* it was last moved.
*/
tooltipDuration?: number;
/**
* Show the tooltip when the cursor is hovered
*/
showTooltipOnHover?: boolean;
/**
* An additional class name for the tooltip element, to tweak the default style
*/
tooltipClassName?: string;
}
/**
@ -47,7 +62,7 @@ export class RemoteCursorManager {
* The default values for optional parameters.
* @internal
*/
private static readonly DEFAULT_OPTIONS = {tooltips: true, tooltipDuration: 1};
private static readonly DEFAULT_OPTIONS: Partial<IRemoteCursorManagerOptions> = {tooltips: true, tooltipDuration: 1, showTooltipOnHover: false};
/**
* A counter that generates unique ids for the cursor widgets.
@ -116,10 +131,13 @@ export class RemoteCursorManager {
const cursorWidget = new RemoteCursorWidget(
this._options.editor,
widgetId,
this._options.className,
color,
label,
this._options.tooltips,
tooltipDurationMs,
this._options.showTooltipOnHover,
this._options.tooltipClassName,
() => this.removeCursor(id));
this._cursorWidgets.set(id, cursorWidget);

View File

@ -51,10 +51,13 @@ export class RemoteCursorWidget implements editor.IContentWidget, IDisposable {
constructor(codeEditor: editor.ICodeEditor,
widgetId: string,
className: string | undefined,
color: string,
label: string,
tooltipEnabled: boolean,
tooltipDuration: number,
showTooltipOnHover: boolean,
tooltipClassName: string | undefined,
onDisposed: OnDisposed) {
this._editor = codeEditor;
this._tooltipDuration = tooltipDuration;
@ -64,16 +67,16 @@ export class RemoteCursorWidget implements editor.IContentWidget, IDisposable {
// Create the main node for the cursor element.
const {lineHeight} = getConfiguration(this._editor);
this._domNode = document.createElement("div");
this._domNode.className = "monaco-remote-cursor";
this._domNode.className = classNames('monaco-remote-cursor', className)
this._domNode.style.background = color;
this._domNode.style.height = `${lineHeight}px`;
// Create the tooltip element if the tooltip is enabled.
if (tooltipEnabled) {
this._tooltipNode = document.createElement("div");
this._tooltipNode.className = "monaco-remote-cursor-tooltip";
this._tooltipNode.className = classNames('monaco-remote-cursor-tooltip', tooltipClassName)
this._tooltipNode.style.background = color;
this._tooltipNode.innerHTML = label;
this._tooltipNode.innerText = label;
this._domNode.appendChild(this._tooltipNode);
// we only need to listen to scroll positions to update the
@ -81,6 +84,16 @@ export class RemoteCursorWidget implements editor.IContentWidget, IDisposable {
this._scrollListener = this._editor.onDidScrollChange(() => {
this._updateTooltipPosition();
});
if (showTooltipOnHover) {
this._domNode.style.pointerEvents = 'auto';
this._domNode.addEventListener('mouseover', () => {
this._setTooltipVisible(true);
})
this._domNode.addEventListener('mouseout', () => {
this._setTooltipVisible(false);
})
}
} else {
this._tooltipNode = null;
this._scrollListener = null;
@ -171,17 +184,10 @@ export class RemoteCursorWidget implements editor.IContentWidget, IDisposable {
}
private _showTooltip(): void {
this._updateTooltipPosition();
if (this._hideTimer !== null) {
clearTimeout(this._hideTimer);
} else {
this._setTooltipVisible(true);
}
this._setTooltipVisible(true);
this._hideTimer = setTimeout(() => {
this._setTooltipVisible(false);
this._hideTimer = null;
}, this._tooltipDuration);
}
@ -197,7 +203,12 @@ export class RemoteCursorWidget implements editor.IContentWidget, IDisposable {
}
private _setTooltipVisible(visible: boolean): void {
if (this._hideTimer !== null) {
clearTimeout(this._hideTimer);
this._hideTimer = null;
}
if (visible) {
this._updateTooltipPosition();
this._tooltipNode.style.opacity = "1.0";
} else {
this._tooltipNode.style.opacity = "0";
@ -243,3 +254,7 @@ export class RemoteCursorWidget implements editor.IContentWidget, IDisposable {
}
}
}
function classNames(...names: (string|undefined|null)[]) {
return names.filter(className => className != null && className.length > 0).join(' ')
}