r/JavaScriptTips • u/TranslatorSavings470 • Jul 08 '23
HELP PLEASE!!
Hi, beginner coder here. I am tasked with building a wysiwyf editor, and I am running into some problems. I cant make the fontSizeChange work, nor can i make it so that when it turns from text to html code, it shows whether there was a change in color. Also, I can't make it so that i can drag the images that are inserted around at will, and finally, i want to make it possible so that you can drag the different headings into the editor.
If you have any idea to solve one or more of my problems i would really appreciate it.
Here is the code:
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>WYSIWYG Editor</title> <link rel="stylesheet" href="css_editor.css"> <style> \* { margin: 0; padding: 0; box-sizing: border-box; } body { padding-left: 20px; padding-top:50 px; } form{ margin: 10px 0; } button{ border:none; outline:none; width: 30px; height:30px; cursor:pointer; } button.heading{ width: 75px; } \#output{ width:700px; height:400px; border:1px solid #111 } .active{ border:1px solid crimson ; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); padding: 12px 16px; z-index: 1; }
.dropdown:hover .dropdown-content {
display: block;
cursor:grab;
}
ul{
list-style: none;
}
li{
border:#111;
}
.heading1{
font-size: 70px;
}
.heading2{
font-size: 50px;
}
.heading3{
font-size: 30px;
}
.heading4{
font-size: 10px;
}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
</head> <body> <form> <input id="font-size" type="number" value="12" min="1" max="100" onchange="fontSizeChange(this)"> <input type="color" onchange="colorChange(this)"> <button type="button" data-cmd="bold"> <i class="fa-solid fa-bold" aria-hidden="true"></i> </button> <button type="button" data-cmd="italic"> <i class="fa-solid fa-italic" aria-hidden="true"></i> </button> <button type="button" data-cmd="underline"> <i class="fa-solid fa-underline" aria-hidden="true"></i> </button> <button type="button" data-cmd="capsOrNot"> aA </button> <button type="button" data-cmd="changeHeadingSize" class="heading dropdown">Heading <div class="dropdown-content"> <ul> <li class="heading1"> Heading 1</li> <li class="heading2">Heading 2</li> <li class="heading3">Heading 3</li> <li class="heading4">Heading 4</li> </ul> </div> </button> <button type="button" data-cmd="justifyleft"> <i class="fa-solid fa-align-left" aria-hidden="true"></i> </button> <button type="button" data-cmd="justifycenter"> <i class="fa-solid fa-align-center" aria-hidden="true"></i> </button> <button type="button" data-cmd="justifyright"> <i class="fa-solid fa-align-right" aria-hidden="true"></i> </button> <button type="button" data-cmd="insertOrderedList"> <i class="fa-solid fa-list-ol" aria-hidden="true"></i> </button> <button type="button" data-cmd="insertUnorderedList"> <i class="fa-solid fa-list-ul" aria-hidden="true"></i> </button> <button type="button" data-cmd="insertImage"> <i class="fa-solid fa-file-image" aria-hidden="true"></i> </button> <button type="button" data-cmd="createLink"> <i class="fa-solid fa-link" aria-hidden="true"></i> </button> <button type="button" data-cmd="showCode" name="active"> <i class="fa-solid fa-code" aria-hidden="true"></i> </button> </form> <iframe id="output" name="textField"></iframe> <script> const buttons = document.querySelectorAll('button'); const iframe = document.getElementById('output'); const textField = iframe.contentDocument; textField.designMode = 'On'; let show = false; let isUppercase = false; function fontSizeChange(e) { let value = e.target.value; textField.document.body.style.fontSize = value + "px"; }
function colorChange(e) {
let value = e.value;
textField.body.style.color = value;
}
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', () => {
const cmd = buttons[i].getAttribute('data-cmd');
if (buttons[i].name === 'active') {
buttons[i].classList.toggle('active');
}
if (cmd === 'insertImage' || cmd === 'createLink') {
let url = prompt('Enter link here: ', '');
textField.execCommand(cmd, false, url);
if (cmd === 'insertImage') {
const imgs = textField.querySelectorAll('img');
imgs.forEach(item => {
item.style.maxWidth = '500px';
});
} else {
const links = textField.querySelectorAll('a');
links.forEach(item => {
item.target = '_blank';
item.addEventListener('mouseover', () => {
textField.designMode = 'off';
});
item.addEventListener('mouseout', () => {
textField.designMode = 'on';
});
});
}
} else if (cmd === 'capsOrNot') {
const selectedText = textField.getSelection().toString();
const modifiedText = isUppercase ? selectedText.toLowerCase() : selectedText.toUpperCase();
textField.execCommand('insertText', false, modifiedText);
isUppercase = !isUppercase;
} else {
textField.execCommand(cmd, false, null);
}
if (cmd === 'showCode') {
const textBody = textField.querySelector('body');
if (show) {
textBody.innerHTML = textBody.textContent;
show = false;
} else {
textBody.textContent = textBody.innerHTML;
show = true;
}
}
});
}
$(document).ready(function(){
$(".dropdown-content").draggable({ helper:'clone'});
$("#output").droppable(
{
accept:".dropdown-content",
drop: function(ev,ui){
alert('item dropped');
}
}
);
});
</script>
</body> </html>
1
u/[deleted] Jul 08 '23
I have a suspicion that few people are going to feel like reading through that unformatted HTML-file looking for what might be wrong (debugging without logs/breakpoints is masochism!), so I took the liberty and got some answers to your questions this way:
https://chat.openai.com/share/6d2657e3-7245-459f-b1b4-189cb7ac0506