Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tip: prefill an empty editor with <p><br></p> to avoid a small bug #174

Open
dvtan opened this issue May 23, 2019 · 4 comments
Open

tip: prefill an empty editor with <p><br></p> to avoid a small bug #174

dvtan opened this issue May 23, 2019 · 4 comments
Projects

Comments

@dvtan
Copy link

dvtan commented May 23, 2019

like this:

if (initBody) {
	editor.content.innerHTML = initBody;
} else {
	editor.content.innerHTML = '<p><br></p>'
}

Most of the time images get inserted inside <p></p> tags. But if the user starts uploading images before he writes even a single letter, the image will be inserted at the top level since there's nothing else there.

Oh and will also need this in case user writes something and deletes it all again (in which case Pell sets it back to completely blank):

onChange: (html) => {
	if (html === '') {
		editor.content.innerHTML = '<p><br></p>'
	}
}
@dvtan dvtan changed the title protip (or noobtip idk): prefill an empty editor with <p><br></p> to avoid insertImage bug protip (or noobtip): prefill an empty editor with <p><br></p> to avoid a small bug May 23, 2019
@dvtan dvtan changed the title protip (or noobtip): prefill an empty editor with <p><br></p> to avoid a small bug tip: prefill an empty editor with <p><br></p> to avoid a small bug May 23, 2019
@jaredreich
Copy link
Owner

@dvtan thanks! Slotted this for v2.

@dvtan
Copy link
Author

dvtan commented Jun 4, 2019

Just realized this particular method won't work with #140 because it uses a :empty selector, which the <p><br></p> breaks. So instead, I leave everything as it previously was, and only insert <p><br></p> if it's empty just before runningexec('insertImage'). Seems like a cleaner solution to me.

@algorys
Copy link

algorys commented Jul 23, 2019

Maybe only review image handler:

result: () => {
      const url = window.prompt('Enter the image URL')
      if (url) document.execCommand('insertHTML', false, '<p><img src="' + url '"></img></p>');
}

But it weighs down the function ... so to see if this is the goal of this library ?

@gagdiez
Copy link

gagdiez commented Aug 22, 2020

I fixed it using:

var pell_focused = false

var pell_content = document.getElementsByClassName('pell-content')[0]
pell_content.addEventListener('focus', ()=>{pell_focused=true}, false)

function pell_add_image(url){
  if (!pell_focused){
    pell_content.innerHTML += "<img src='"+url+"'>"
    $('html-output').textContent = pell_content.innerHTML
    pell_content.focus()
  }
  else{
    pell.exec('insertImage', url)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
v2
Awaiting triage
Development

No branches or pull requests

4 participants