0
votes

I'm building a small text editor in raw JavaScript. I have three buttons: bold, cursive and heading. When I click two times on bold and cursive buttons it is disappearing but when I click twice on the heading button, nothing happens. I tried to use true/falses but it can't format the text more than once.

I use ExecCommand API and ContentEditable attribute form my div.

clicked = false;
cmd = (cmd, arg) => {
  clicked = true;
  document.execCommand(cmd, false, arg);
  if(clicked && cmd === 'heading'){
    document.execCommand('undo')
  }
  clicked = false;
  alert(clicked);
}
#toolbar {
  text-align: center;
  border-bottom: 3px solid black;
}

button {
  vertical-align: middle;
  display: inline-block;
  font-size: 35px;
  border-radius: 7px;
  border-style: none;
  border: 2px solid black;
  height: 50px;
  width: 50px;
  margin: 15px;
  background-color: white;
  transition: 0.5s;
}

button:hover {
  background: lightgrey;
}

body {
  margin: 0;
  font-family: Georgia;
  width: 100%;
}

div[contenteditable=true] {
  height: 10em;
  width: auto;
  vertical-align: middle;
  padding: 25px;
  font-size: 25px;
  word-wrap: break-word;
  outline: none;
}

h1 {
  font-size: 30px;
}
<nav id="toolbar">
  <button onclick="cmd('bold')">B</button>
  <button onclick="cmd('italic')">i</button>
  <button onclick="cmd('formatBlock','<h1>')">T</button>
</nav>
<div contenteditable="true">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur quaerat, natus nobis cum ut rerum obcaecati reiciendis beatae sapiente vel.
</div>
1

1 Answers

2
votes

You should use undo (or removeFormat if it works) in the second click handler.

Here for informations about execCommand

From what I know formatBlock replaces the first block element that it finds around the text. So if you've a div that wraps the text it will become a p, but then at the next click you already have a p and so that p is replaced with another p

clicked = false;
cmd = (cmd, arg) => {
  if(clicked && cmd === 'formatBlock'){
    document.execCommand('formatBlock', false, 'p');
  } else {
    document.execCommand(cmd, false, arg);
  }
  clicked = !clicked;
}
#toolbar {
  text-align: center;
  border-bottom: 3px solid black;
}

button {
  vertical-align: middle;
  display: inline-block;
  font-size: 35px;
  border-radius: 7px;
  border-style: none;
  border: 2px solid black;
  height: 50px;
  width: 50px;
  margin: 15px;
  background-color: white;
  transition: 0.5s;
}

button:hover {
  background: lightgrey;
}

body {
  margin: 0;
  font-family: Georgia;
  width: 100%;
}

div[contenteditable=true] {
  height: 10em;
  width: auto;
  vertical-align: middle;
  padding: 25px;
  font-size: 25px;
  word-wrap: break-word;
  outline: none;
}

h1 {
  font-size: 30px;
}

p {
  color: red;
}
<nav id="toolbar">
  <button onclick="cmd('bold')">B</button>
  <button onclick="cmd('italic')">i</button>
  <button onclick="cmd('formatBlock','<h1>')">T</button>
</nav>
<div contenteditable="true">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur quaerat, natus nobis cum ut rerum obcaecati reiciendis beatae sapiente vel.</p>
</div>

Take a look at my snippet, the problem wasnt in the command. Your code was executing two times the chosen command if the user clicked on formatBlock. To test it I've added a

around the text with a red color. Then on the formatBlock I transform that block into an ; on the second click it will be reverted to a

.