1

I have a preview component which renders dynamically:

  <div className={styles.mainPrev}>
        <nav className={styles.dp} style={{margin: `${position.top} ${position.right} ${position.bottom} ${position.left}`, width: `${avatar}px`}} >
            <img src={contact.imageAdd} alt="" style={{borderRadius: `${style}%`}} />
        </nav>
        <section>
            <h2 style={{color: `${color}`,}}>{contact.firstName}  {contact.lastName}</h2>
            <h4><span> {contact.jobTitle} </span> - <span>{contact.companyName}</span></h4>
            <p>Email: <b style={{color: `${color}`, fontFamily: `${font}`, fontSize: `${size}px`}}>{contact.email}</b></p>
            <p>Phone: <b style={{color: `${color}`, fontFamily: `${font}`, fontSize: `${size}px`}}>{contact.phone}</b></p>
            <p>Website: <b style={{color: `${color}`, fontFamily: `${font}`, fontSize: `${size}px`}}>{contact.website}</b></p>
            <p>Address: <b style={{color: `${color}`, fontFamily: `${font}`, fontSize: `${size}px`}}>{contact.address}</b></p>
                {
                    display && ( 
                        <nav className={styles.prevIcons}>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.github}`} id={social.github ? '' : styles.none}><img src={icon1} alt="" /> </a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.behance}`} id={social.behance ? '' : styles.none}><img src={icon2} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.snapchat}`} id={social.snapchat ? '' : styles.none}><img src={icon3} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.pinterest}`} id={social.pinterest ? '' : styles.none}><img src={icon4} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.skype}`} id={social.skype ? '' : styles.none}><img src={icon5} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.linkedIn}`} id={social.linkedIn ? '' : styles.none}><img src={icon6} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.youtube}`} id={social.youtube ? '' : styles.none}><img src={icon7} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.vimeo}`} id={social.vimeo ? '' : styles.none}>
                            <img src={icon8} alt="" />
                            </a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.twitter}`} id={social.twitter ? '' : styles.none}><img src={icon9} alt="" /></a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.facebook}`} id={social.facebook ? '' : styles.none}>
                            <Facebook  sx={{ fontSize: 25, color: '#03132C', borderRadius: '50%'}} />
                            </a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.instagram}`} id={social.instagram ? '' : styles.none}>
                            <Instagram  sx={{ fontSize: 25, color: '#03132C', }} />
                            </a>
                            <a style={{borderRadius: `${shapes}%`, background: `${color}`}} href={`https://${link.slack}`} id={social.slack ? '' : styles.none}>
                            <span className={styles.hash}>#</span>
                            </a>
                        </nav>
                    )
                }
        </section>
    </div>

I want to be able to copy to clipboard all this text and image while maintaining exactly how it appears in the browser i.e with the display flex, color, border, etc

I have tried so far with the following code:

function handleCopy(e) {
let doc = document
let text = doc.getElementById('preview')
let range, selection;

if (doc.body.createTextRange)
{
    range = doc.body.createRange();
    range.moveToElementText(text);
    range.select();
} 

else if (window.getSelection)
{
    selection = window.getSelection();        
    range = doc.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
}
document.execCommand('copy');
window.getSelection().removeAllRanges();
e.target.textContent = "Copied";
}

But this only copies the text without maintaining the styling and doesn't also copy image (which is updated also dynamically..)

Jdavydz
  • 21
  • 3
  • I think the `window.getSelection` is working fine for you. I believe, though, that the stuff in the if statement only copies text, but I can't be sure. I once got frustrated enough to make a [library](https://github.com/codingjlu/copyjs). – code Mar 18 '22 at 17:06
  • Where do you want to paste that data ? I guess you want to copy in the same way as it works when you e.g. select content in the browser with the mouse and __copy/paste it into e.g. MS-Word__, correct ? But that would only keep the layout partially. Exactly the same behavior including flex is probably not possible. Or do you want to copy the underlying __HTML code__, to past it into a text editor ? Or the __DOM tree__, to use it somewhere else in your app ? _(I probably can't answer the question, I only ask to clarify that.)_ – kca Mar 19 '22 at 21:48
  • I intend to use it as an e-signature for mail – Jdavydz Mar 21 '22 at 19:34

0 Answers0