Skip to content

Commit

Permalink
Merge pull request #4 from KamiC6238/feat-breakpoint-tips
Browse files Browse the repository at this point in the history
feat: support hover message for added & unadded breakpoint
  • Loading branch information
KamiC6238 authored Jun 30, 2024
2 parents 18d99a3 + ac6be76 commit 4a9a3e1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"url": "https://github.com/KamiC6238/monaco-breakpoints"
},
"license": "MIT",
"version": "0.1.2",
"version": "0.2.0",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
Expand Down
49 changes: 42 additions & 7 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
EditorMouseTarget,
ModelDeltaDecoration,
MonacoBreakpointProps,
CursorPositionChangedEvent
CursorPositionChangedEvent,
HoverMessage
} from '../types';

import {
Expand Down Expand Up @@ -44,14 +45,17 @@ export default class MonacoBreakpoint {
private decorationIdAndRangeMap = new Map<string, Range>();
private lineNumberAndDecorationIdMap = new Map<number, string>();

private hoverMessage: HoverMessage | null = null

constructor(params: MonacoBreakpointProps) {
if (!params?.editor) {
throw new Error("Missing 'editor' parameter");
}

const { editor } = params;
const { editor, hoverMessage = null } = params;

this.editor = editor;
this.hoverMessage = hoverMessage
this.initMouseEvent();
this.initEditorEvent();
}
Expand All @@ -61,7 +65,7 @@ export default class MonacoBreakpoint {
this.mouseMoveDisposable = this.editor!.onMouseMove(
(e: EditorMouseEvent) => {
const model = this.getModel();
const { range, detail, type } = this.getMouseEventTarget(e);
const { range, detail, type, position } = this.getMouseEventTarget(e);

// This indicates that the current position of the mouse is over the total number of lines in the editor
if (detail?.isAfterLines) {
Expand All @@ -73,6 +77,10 @@ export default class MonacoBreakpoint {
// remove previous hover breakpoint decoration
this.removeHoverDecoration();

if (this.checkIfBreakpointExistInLine(position.lineNumber)) {
return
}

// create new hover breakpoint decoration
const newDecoration = this.createBreakpointDecoration(
range,
Expand All @@ -81,7 +89,15 @@ export default class MonacoBreakpoint {
// render decoration
const decorationIds = model.deltaDecorations(
[],
[newDecoration]
[
{
range: newDecoration.range,
options: {
...newDecoration.options,
glyphMarginHoverMessage: this.hoverMessage?.unAdded ?? null
}
}
]
);
// record the hover decoraion id
this.hoverDecorationId = decorationIds[0];
Expand Down Expand Up @@ -224,7 +240,7 @@ export default class MonacoBreakpoint {
return { ...(e.target as EditorMouseTarget) };
}

private getLineDecoration(lineNumber: number) {
private getBreankpointDecorationInLine(lineNumber: number) {
return (
this.getModel()
?.getLineDecorations(lineNumber)
Expand Down Expand Up @@ -318,7 +334,15 @@ export default class MonacoBreakpoint {
// render decoration
const newDecorationId = model.deltaDecorations(
[],
[newDecoration]
[
{
range: { ...newDecoration.range },
options: {
...newDecoration.options,
glyphMarginHoverMessage: this.hoverMessage?.added ?? null
}
}
]
)[0];

// record the new breakpoint decoration id
Expand All @@ -329,7 +353,7 @@ export default class MonacoBreakpoint {
this.emitBreakpointChanged();

// record the new decoration
const decoration = this.getLineDecoration(range.endLineNumber);
const decoration = this.getBreankpointDecorationInLine(range.endLineNumber);

if (decoration) {
this.decorationIdAndRangeMap.set(newDecorationId, decoration.range);
Expand Down Expand Up @@ -422,6 +446,17 @@ export default class MonacoBreakpoint {
return lineBreakInHead;
}

private checkIfBreakpointExistInLine(lineNumber: number) {
const decorationInLine = this.getBreankpointDecorationInLine(lineNumber)
const decorationIdInLine = this.lineNumberAndDecorationIdMap.get(lineNumber)

return (
decorationInLine &&
decorationIdInLine &&
decorationInLine.id === decorationIdInLine
)
}

private emit<T extends keyof BreakpointEvents>(event: T, data: BreakpointEvents[T]) {
this.eventEmitter.emit(event, data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {default as MonacoBreakpoint} from './core';
export {default as MonacoBreakpoint} from './core';
8 changes: 4 additions & 4 deletions src/style/index.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.monaco-hover-breakpoint {
width: 10px !important;
height: 10px !important;
width: 8px !important;
height: 8px !important;
border-radius: 100%;
background-color: #eabebc;
margin: 4px 0 0 8px;
cursor: pointer;
}

.monaco-breakpoint {
width: 10px !important;
height: 10px !important;
width: 8px !important;
height: 8px !important;
border-radius: 100%;
background-color: #d47d78;
margin: 4px 0 0 8px;
Expand Down
14 changes: 14 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type * as monaco from 'monaco-editor';
export type Range = monaco.IRange;
export type Position = monaco.IPosition;
export type Disposable = monaco.IDisposable;
export type Model = monaco.editor.ITextModel
export type MonacoEditor = monaco.editor.IStandaloneCodeEditor;

export type EditorMouseEvent = monaco.editor.IEditorMouseEvent;
Expand All @@ -13,6 +14,8 @@ export type ModelDecoration = monaco.editor.IModelDecoration;
export type ModelDeltaDecoration = monaco.editor.IModelDeltaDecoration;
export type ModelDecorationOptions = monaco.editor.IModelDecorationOptions;

export type IMarkdownString = monaco.IMarkdownString

/**
* The meaning of 'Exist' is that the current breakpoint is actually present
*/
Expand All @@ -21,8 +24,19 @@ export enum BreakpointEnum {
Hover,
}

export type HoverMessage = {
/** Hover message for added breakpoint. Defaults to empty. */
added?: IMarkdownString | IMarkdownString[]
/** Hover message for unadded breakpoints. Defaults to 'Click to add a breakpoint'. */
unAdded?: IMarkdownString | IMarkdownString[]
}

export interface MonacoBreakpointProps {
editor: MonacoEditor;
/**
* Hover message for added/unadded breakpoint
*/
hoverMessage?: HoverMessage
}

export type Handler<T = any> = (data: T) => void;
Expand Down

0 comments on commit 4a9a3e1

Please sign in to comment.