From 255bc62cb2b801b409385a6fdc03de4549bf73f5 Mon Sep 17 00:00:00 2001 From: Sarthak Bakre Date: Sun, 29 Sep 2024 17:56:08 +0530 Subject: [PATCH] feat: added repo and code comments --- package.json | 4 ++++ src/utility.tsx | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6610aa2..da107e9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utility.tsx b/src/utility.tsx index f7f214e..086ff1a 100644 --- a/src/utility.tsx +++ b/src/utility.tsx @@ -1,3 +1,4 @@ +/// Returns the cursor position in the input field function getCursorPosition() { let editor = document.getElementById('main_input'); @@ -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[], @@ -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'); @@ -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 @@ -88,8 +95,7 @@ export function updateCode( let text = editor ? editor.innerText : ''; if (editor != null && text.length <= 0) { - // editor.innerHTML = "Enter something"; - editor.innerHTML = ' '; + editor.innerHTML = ' '; /// If the input field is empty, then enter a empty space const range = document.createRange(); const selection = window.getSelection();