0

I'm using Blazor Server Side. I want to select certain text from the text inside textarea.

The thing is I got the following info from JSON and pass it the JS function is like parsing a text to extract the errors. So I have the line number first/end and column number first/end

this my json file:

"error 1": [
       {
           "error ": "issue with sometext",
           "lineStart": 3,
           "lineEnd": 3,
           "columnStart": 1,
           "columnEnd": 3
       }
   ],
"error 2": [
       {
           "error ": "issue with sometext",
           "lineStart": 4,
           "lineEnd": 8,
           "columnStart": 2,
           "columnEnd": 5
       }
   ],
"error 3": [
       {
           "error ": "issue with sometext",
           "lineStart": 7,
           "lineEnd": 5,
           "columnStart": 1,
           "columnEnd": 2
       }
   ]

How can I find a function that do that for me. I've tried this function How to select line of text in textarea but it selects/highlights only one line, while in my case I may need to highlight multi-lines. I've made some changes but could not get the solution.

This the function that i tried.

function selectTextareaLine(tarea,lineNum) {
    lineNum--; // array starts at 0
    var lines = tarea.value.split("\n");

    // calculate start/end
    var startPos = 0, endPos = tarea.value.length;
    for(var x = 0; x < lines.length; x++) {
        if(x == lineNum) {
            break;
        }
        startPos += (lines[x].length+1);

    }

    var endPos = lines[lineNum].length+startPos;

    // do selection
    // Chrome / Firefox

    if(typeof(tarea.selectionStart) != "undefined") {
        tarea.focus();
        tarea.selectionStart = startPos;
        tarea.selectionEnd = endPos;
        return true;
    }
}

function CheckText(tarea,lineNum)

{


var tarea = document.getElementById('TareaID');
window.getSelection().removeAllRanges()

 selectTextareaLine(tarea,3); // selects line 3


}

The desired result if I have like this text inside textarea.

Line 1: Say hello to your friend

Line 2: Say hello to your friend

Line 3: Sya hello to your friend

Line 4: Say hello to your friend

so based on the error 1 in json there is an error the third line with the first three digits. because it is 'Sya' and it should Be 'Say' so the desire result is to highlight the line number and column start/end which the 'Say' in our example

Any idea ?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Fadi
  • 585
  • 4
  • 22
  • I've written code to do this kind of parsing in Blazor, so I could be able to help. However, it is not really clear to me what you are trying to do and why. Could you give a sample of the desired result and the actual result? – Bennyboy1973 Apr 14 '22 at 12:44
  • @Bennyboy1973 thank you. I want to pass a text (like a programming lang) and then validate if the syntax of this text is valid or not. I reach to the validation through an API it returns the json as I mentioned with errors. so the user can know the mistakes and fix them. – Fadi Apr 14 '22 at 12:57
  • "Could you give a sample of the desired result and the actual result?" – Bennyboy1973 Apr 14 '22 at 13:01
  • @Bennyboy1973 i've updated my question to add a sample of what I want – Fadi Apr 14 '22 at 13:47
  • I still don't know enough about the mechanics. Are you actually taking input into this text area, or is it for display only? Is the length of a line guaranteed not to exceed the number of columns (usually 80)? Is the JSON coming from an external API, so that this is the only possible way of expressing error locations? It seems like a very complicated way of expressing the position of text that needs to be marked, and I'm wondering if it's really necessary. – Bennyboy1973 Apr 14 '22 at 14:03
  • @Bennyboy1973 .. the text area already has the input of the text, where the text is coming from the database and sent it to the external API to check the text is valid. I've added a new feature to validate the content. so it is just for display. I just want the end-user to see the errors when he clicks on the validate button that will highlight the lines. yes. it is guaranteed. and yes, JSON is coming from external API and this is the only way to get the issues. – Fadi Apr 14 '22 at 14:09

0 Answers0