question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select Span as a single element

See original GitHub issue

I have the ability for users to insert some meta data (e.g. the day). it goes into the edit like this…

<span class="metaData">Wednesday </span>

there could be user text on either side of the span e.g. My favourite day of the week is <span class="metaData">Wednesday </span> becuase I have PE at school.

I want to make it so the metaData span is treated as a single “lump”, e.g. if they click anywhere in it, it highlights the entire span. If they overwrite it, it gets rid of the entire span, etc.

Hopefully, this makes sense. ANy pointers would be much appriciated

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
JiHong88commented, May 28, 2022
editorInstance.onClick = (event, core) => {
    const element = event.target
    if(core.util.hasClass(element, "metaData")){
        core.setRange(element, 0, element, 1);
    }
}

editorInstance.onKeyDown = (event, core) => {
    const range = core.getRange();
    if(core.util.hasClass(range.commonAncestorContainer, "metaData")){
        core.util.removeItem(range.commonAncestorContainer);
    }
}

1reaction
JiHong88commented, May 20, 2022

In a simple implementation, you can do something like this:

this.editor.onClick = function (e, core) {
    const element = event.target
    if(core.util.hasClass(element, "metaData")){
        const a = core.util.createTextNode(core.util.zeroWidthSpace);
        const a1 = core.util.createTextNode(core.util.zeroWidthSpace);
        element.parentNode.insertBefore(a, element);
        element.parentNode.insertBefore(a1, element.nextSibling);
        core.setRange(a, 0, a1, 1);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Content Span element - HTML - MDN Web Docs
The <span> HTML element is a generic inline container for phrasing content, which does not inherently represent anything.
Read more >
how can i select only last span element in a div ? it should not ...
If you want to select the last span in a div, but only where there is more than one span in the div,...
Read more >
Span HTML – How to Use the Span Tag with CSS
You can use the HTML span tag as a container to group inline elements together so you can style or manipulate them with...
Read more >
Divs, Spans, Ids, and Classes
The span tag is an inline HTML element that is used to group a set of inline elements. Generally speaking, you use span...
Read more >
.find() | jQuery API Documentation
A string containing a selector expression to match elements against. ... Only spans within p tags are changed to red while others are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found