diff --git a/wysiwyg.css b/wysiwyg.css index d4357d4..5c76bb2 100644 --- a/wysiwyg.css +++ b/wysiwyg.css @@ -1,90 +1,90 @@ -.wysiwyg { - width: 40rem; - min-height: 18rem; - box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.3); - border-top: 6px solid #4a4a4a; - border-radius: 3px; - margin: 2rem; -} -.wysiwyg .toolbar { - box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); -} -.wysiwyg .toolbar .line { - display: flex; - border-bottom: 1px solid #e2e2e2; -} -.wysiwyg .toolbar .line:last-child { - border-bottom: none; -} -.wysiwyg .toolbar .line .box { - display: flex; - border-left: 1px solid #e2e2e2; -} -.wysiwyg .toolbar .line .box .btn { - display: block; - display: flex; - align-items: center; - justify-content: center; - position: relative; - transition: 0.2s ease all; -} -.wysiwyg .toolbar .line .box .btn:hover, .wysiwyg .toolbar .line .box .btn.active { - background-color: #e1e1e1; - cursor: pointer; -} -.wysiwyg .toolbar .line .box .btn.icon img { - width: 15px; - padding: 10px; -} -.wysiwyg .toolbar .line .box .btn.icon.smaller img { - width: 12px; -} -.wysiwyg .toolbar .line .box .btn.has-submenu { - width: 20px; - padding: 0 10px; -} -.wysiwyg .toolbar .line .box .btn.has-submenu::after { - content: ''; - width: 6px; - height: 6px; - position: absolute; - background-image: url(https://image.flaticon.com/icons/svg/25/25243.svg); - background-repeat: no-repeat; - background-size: cover; - background-position: center; - right: 4px; -} -.wysiwyg .toolbar .line .box .btn.has-submenu .submenu { - display: none; - position: absolute; - top: 36px; - left: -1px; - z-index: 10; - background-color: #fff; - border: 1px solid #b5b5b5; - border-top: none; -} -.wysiwyg .toolbar .line .box .btn.has-submenu .submenu .btn { - width: 39px; -} -.wysiwyg .toolbar .line .box .btn.has-submenu:hover .submenu { - display: block; -} -.wysiwyg .content-area { - padding: 15px 12px; - line-height: 1.5; -} -.wysiwyg .content-area .visuell-view { - outline: none; -} -.wysiwyg .content-area .visuell-view p { - margin: 12px 0; -} -.wysiwyg .content-area .html-view { - outline: none; - display: none; - width: 100%; - height: 200px; - border: none; - resize: none; +.wysiwyg { + width: 40rem; + min-height: 18rem; + box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.3); + border-top: 6px solid #4a4a4a; + border-radius: 3px; + margin: 2rem; +} +.wysiwyg .toolbar { + box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); +} +.wysiwyg .toolbar .line { + display: flex; + border-bottom: 1px solid #e2e2e2; +} +.wysiwyg .toolbar .line:last-child { + border-bottom: none; +} +.wysiwyg .toolbar .line .box { + display: flex; + border-left: 1px solid #e2e2e2; +} +.wysiwyg .toolbar .line .box .btn { + display: block; + display: flex; + align-items: center; + justify-content: center; + position: relative; + transition: 0.2s ease all; +} +.wysiwyg .toolbar .line .box .btn:hover, .wysiwyg .toolbar .line .box .btn.active { + background-color: #e1e1e1; + cursor: pointer; +} +.wysiwyg .toolbar .line .box .btn.icon img { + width: 15px; + padding: 10px; +} +.wysiwyg .toolbar .line .box .btn.icon.smaller img { + width: 12px; +} +.wysiwyg .toolbar .line .box .btn.has-submenu { + width: 20px; + padding: 0 10px; +} +.wysiwyg .toolbar .line .box .btn.has-submenu::after { + content: ''; + width: 6px; + height: 6px; + position: absolute; + background-image: url(https://image.flaticon.com/icons/svg/25/25243.svg); + background-repeat: no-repeat; + background-size: cover; + background-position: center; + right: 4px; +} +.wysiwyg .toolbar .line .box .btn.has-submenu .submenu { + display: none; + position: absolute; + top: 36px; + left: -1px; + z-index: 10; + background-color: #fff; + border: 1px solid #b5b5b5; + border-top: none; +} +.wysiwyg .toolbar .line .box .btn.has-submenu .submenu .btn { + width: 39px; +} +.wysiwyg .toolbar .line .box .btn.has-submenu:hover .submenu { + display: block; +} +.wysiwyg .content-area { + padding: 15px 12px; + line-height: 1.5; +} +.wysiwyg .content-area .visuell-view { + outline: none; +} +.wysiwyg .content-area .visuell-view p { + margin: 12px 0; +} +.wysiwyg .content-area .html-view { + outline: none; + display: none; + width: 100%; + height: 200px; + border: none; + resize: none; } \ No newline at end of file diff --git a/wysiwyg.js b/wysiwyg.js index b9f7581..ce96a3c 100644 --- a/wysiwyg.js +++ b/wysiwyg.js @@ -1,140 +1,140 @@ -function initWYSIWYG() { - $('.wysiwyg').html('
'+ - '
'+ - ' '+ - '
'+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - '
'+ - ' '+ - '
'+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - '
'+ - '
'+ - ' '+ - ' '+ - ' '+ - '
'+ - ' '+ - '
'+ - '
'+ - ' '+ - '
'+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - '
'+ - ' '+ - '
'+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - ' '+ - '
'+ - '
'+ - ' '+ - ' '+ - ' '+ - '
'+ - ' '+ - '
'+ - '
'+ - '
'+ - '
'+ - '
'+ - ''+ - '
'); -} - -initWYSIWYG(); - -const editor = document.getElementsByClassName('wysiwyg')[0]; -console.log(editor); -const toolbar = editor.getElementsByClassName('toolbar')[0]; -const buttons = toolbar.querySelectorAll('.btn:not(.has-submenu)'); -for(let i = 0; i < buttons.length; i++) { - let button = buttons[i]; - - button.addEventListener('click', function(e) { - let action = this.dataset.action; - - switch(action) { - case 'code': - execCodeAction(this, editor); - break; - case 'createLink': - execLinkAction(); - break; - default: - execDefaultAction(action); - } - - }); -} -function execCodeAction(button, editor) { - const contentArea = editor.getElementsByClassName('content-area')[0]; - const visuellView = contentArea.getElementsByClassName('visuell-view')[0]; - const htmlView = contentArea.getElementsByClassName('html-view')[0]; - if(button.classList.contains('active')) { // show visuell view - visuellView.innerHTML = htmlView.value; - htmlView.style.display = 'none'; - visuellView.style.display = 'block'; - button.classList.remove('active'); - } else { // show html view - htmlView.innerText = visuellView.innerHTML; - visuellView.style.display = 'none'; - htmlView.style.display = 'block'; - button.classList.add('active'); - } -} -function execLinkAction() { - let linkValue = prompt('Link (e.g. https://webdeasy.de/)'); - document.execCommand('createLink', false, linkValue); -} -function execDefaultAction(action) { - document.execCommand(action, false); +function initWYSIWYG() { + $('.wysiwyg').html('
'+ + '
'+ + ' '+ + '
'+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + '
'+ + ' '+ + '
'+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + '
'+ + '
'+ + ' '+ + ' '+ + ' '+ + '
'+ + ' '+ + '
'+ + '
'+ + ' '+ + '
'+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + '
'+ + ' '+ + '
'+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + ' '+ + '
'+ + '
'+ + ' '+ + ' '+ + ' '+ + '
'+ + ' '+ + '
'+ + '
'+ + '
'+ + '
'+ + '
'+ + ''+ + '
'); +} + +initWYSIWYG(); + +const editor = document.getElementsByClassName('wysiwyg')[0]; +console.log(editor); +const toolbar = editor.getElementsByClassName('toolbar')[0]; +const buttons = toolbar.querySelectorAll('.btn:not(.has-submenu)'); +for(let i = 0; i < buttons.length; i++) { + let button = buttons[i]; + + button.addEventListener('click', function(e) { + let action = this.dataset.action; + + switch(action) { + case 'code': + execCodeAction(this, editor); + break; + case 'createLink': + execLinkAction(); + break; + default: + execDefaultAction(action); + } + + }); +} +function execCodeAction(button, editor) { + const contentArea = editor.getElementsByClassName('content-area')[0]; + const visuellView = contentArea.getElementsByClassName('visuell-view')[0]; + const htmlView = contentArea.getElementsByClassName('html-view')[0]; + if(button.classList.contains('active')) { // show visuell view + visuellView.innerHTML = htmlView.value; + htmlView.style.display = 'none'; + visuellView.style.display = 'block'; + button.classList.remove('active'); + } else { // show html view + htmlView.innerText = visuellView.innerHTML; + visuellView.style.display = 'none'; + htmlView.style.display = 'block'; + button.classList.add('active'); + } +} +function execLinkAction() { + let linkValue = prompt('Link (e.g. https://webdeasy.de/)'); + document.execCommand('createLink', false, linkValue); +} +function execDefaultAction(action) { + document.execCommand(action, false); } \ No newline at end of file