Skip to content

Commit

Permalink
feat: added repo and code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sardar1208 committed Sep 29, 2024
1 parent c895475 commit 255bc62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"engines": {
"node": ">=10"
},
"repository": {
"type": "git",
"url": "https://github.com/Sardar1208/input-on-drugs"
},
"keywords": ["typescript", "input", "dynamic input", "code editor"],
"scripts": {
"start": "tsdx watch",
Expand Down
10 changes: 8 additions & 2 deletions src/utility.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Returns the cursor position in the input field
function getCursorPosition() {
let editor = document.getElementById('main_input');

Expand Down Expand Up @@ -37,6 +38,7 @@ interface HighlightOptions {
onlyHighlightIndependentWord?: boolean; // Option to highlight as an independent word or part of another word
}

/// Returns styled HTML from plain text based on the provided configuration
export function highlightWords(
text: string,
wordsToHighlight: string[],
Expand Down Expand Up @@ -64,6 +66,7 @@ export function highlightWords(
});
}

/// The HTML returned from onChangeOD is filtered to verfiy that it only contains the text and span nodes
function sanitizeToSpanOnly(html: string): string {
// Create a temporary div element to manipulate the HTML
const tempDiv = document.createElement('div');
Expand All @@ -80,6 +83,10 @@ function sanitizeToSpanOnly(html: string): string {
return tempDiv.innerHTML;
}

/// Responsible for the following things -
/// - Record cursor position
/// - insert HTML in the input field
/// - Restore the cursor position
export function updateCode(
onChangeOD: (input: string) => string,
editorRef: React.MutableRefObject<any>
Expand All @@ -88,8 +95,7 @@ export function updateCode(
let text = editor ? editor.innerText : '';

if (editor != null && text.length <= 0) {
// editor.innerHTML = "<span style={{color: 'grey'}}>Enter something<span>";
editor.innerHTML = '&nbsp;';
editor.innerHTML = '&nbsp;'; /// If the input field is empty, then enter a empty space
const range = document.createRange();
const selection = window.getSelection();

Expand Down

0 comments on commit 255bc62

Please sign in to comment.