-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(playground): created a utility class to hold re-occurring fu…
…nctions
- Loading branch information
JannisDommer
committed
Jan 3, 2024
1 parent
049a132
commit d49cdcf
Showing
4 changed files
with
101 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {editor} from "monaco-editor"; | ||
import * as monaco from "monaco-editor"; | ||
import {globalEditor} from "./main.ts"; | ||
import lodash from 'lodash'; | ||
|
||
|
||
let currentWidget: editor.IOverlayWidget | null; | ||
|
||
export function displayEditorError(msg: string) { | ||
if (!globalEditor) { | ||
return; | ||
} | ||
const overlayWidget: editor.IOverlayWidget = { | ||
getId(): string { | ||
return 'myCustomWidget'; | ||
}, getPosition(): editor.IOverlayWidgetPosition | null { | ||
return { | ||
preference: monaco.editor.OverlayWidgetPositionPreference.TOP_CENTER | ||
} | ||
}, getDomNode(): HTMLElement { | ||
const node = document.createElement('div'); | ||
const span = document.createElement('span'); | ||
span.textContent = msg; | ||
span.className = "error"; | ||
node.replaceChildren(span); | ||
return node; | ||
} | ||
} | ||
if (currentWidget) { | ||
globalEditor.removeOverlayWidget(currentWidget); | ||
} | ||
currentWidget = overlayWidget; | ||
globalEditor.addOverlayWidget(overlayWidget); | ||
} | ||
|
||
let currentContentWidget: editor.IContentWidget | null; | ||
|
||
export function displayEditorErrorAtContent(msg: string) { | ||
if(!globalEditor){ | ||
return; | ||
} | ||
const selection = globalEditor.getSelection(); | ||
const contentWidget: editor.IContentWidget = { | ||
getId(): string { | ||
return 'myCustomWidget'; | ||
}, getPosition(): editor.IContentWidgetPosition | null { | ||
if (selection) { | ||
return { | ||
position: selection.getStartPosition(), | ||
preference: [monaco.editor.ContentWidgetPositionPreference.BELOW] | ||
} | ||
} | ||
return { | ||
position: {lineNumber: 1, column: 1}, preference: [monaco.editor.ContentWidgetPositionPreference.BELOW] | ||
} | ||
}, getDomNode(): HTMLElement { | ||
const node = document.createElement('div'); | ||
const span = document.createElement('span'); | ||
node.className = "uvl-tooltip"; | ||
span.className = "tooltip-text"; | ||
span.textContent = msg; | ||
node.replaceChildren(span); | ||
return node; | ||
} | ||
} | ||
removeWidget(); | ||
currentContentWidget = contentWidget; | ||
globalEditor.addContentWidget(contentWidget); | ||
|
||
debounceRemoveWidget(); | ||
} | ||
|
||
const debounceRemoveWidget = lodash.debounce(removeWidget, 2000); | ||
|
||
function removeWidget() { | ||
if (currentContentWidget) { | ||
globalEditor?.removeContentWidget(currentContentWidget); | ||
} | ||
currentContentWidget = null; | ||
} | ||
|
||
export function aggregateCharacters(model: editor.ITextModel): number { | ||
let addReducer = (previousValue: number, currentValue: string) => { | ||
return previousValue + currentValue.length | ||
}; | ||
return model?.getLinesContent().reduce(addReducer, 0); | ||
} |