.
This commit is contained in:
parent
293a2a452d
commit
e7de5eeb8d
178
wysiwyg.css
178
wysiwyg.css
@ -1,90 +1,90 @@
|
|||||||
.wysiwyg {
|
.wysiwyg {
|
||||||
width: 40rem;
|
width: 40rem;
|
||||||
min-height: 18rem;
|
min-height: 18rem;
|
||||||
box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.3);
|
box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.3);
|
||||||
border-top: 6px solid #4a4a4a;
|
border-top: 6px solid #4a4a4a;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
margin: 2rem;
|
margin: 2rem;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar {
|
.wysiwyg .toolbar {
|
||||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
|
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line {
|
.wysiwyg .toolbar .line {
|
||||||
display: flex;
|
display: flex;
|
||||||
border-bottom: 1px solid #e2e2e2;
|
border-bottom: 1px solid #e2e2e2;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line:last-child {
|
.wysiwyg .toolbar .line:last-child {
|
||||||
border-bottom: none;
|
border-bottom: none;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box {
|
.wysiwyg .toolbar .line .box {
|
||||||
display: flex;
|
display: flex;
|
||||||
border-left: 1px solid #e2e2e2;
|
border-left: 1px solid #e2e2e2;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn {
|
.wysiwyg .toolbar .line .box .btn {
|
||||||
display: block;
|
display: block;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
position: relative;
|
position: relative;
|
||||||
transition: 0.2s ease all;
|
transition: 0.2s ease all;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn:hover, .wysiwyg .toolbar .line .box .btn.active {
|
.wysiwyg .toolbar .line .box .btn:hover, .wysiwyg .toolbar .line .box .btn.active {
|
||||||
background-color: #e1e1e1;
|
background-color: #e1e1e1;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.icon img {
|
.wysiwyg .toolbar .line .box .btn.icon img {
|
||||||
width: 15px;
|
width: 15px;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.icon.smaller img {
|
.wysiwyg .toolbar .line .box .btn.icon.smaller img {
|
||||||
width: 12px;
|
width: 12px;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.has-submenu {
|
.wysiwyg .toolbar .line .box .btn.has-submenu {
|
||||||
width: 20px;
|
width: 20px;
|
||||||
padding: 0 10px;
|
padding: 0 10px;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.has-submenu::after {
|
.wysiwyg .toolbar .line .box .btn.has-submenu::after {
|
||||||
content: '';
|
content: '';
|
||||||
width: 6px;
|
width: 6px;
|
||||||
height: 6px;
|
height: 6px;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
background-image: url(https://image.flaticon.com/icons/svg/25/25243.svg);
|
background-image: url(https://image.flaticon.com/icons/svg/25/25243.svg);
|
||||||
background-repeat: no-repeat;
|
background-repeat: no-repeat;
|
||||||
background-size: cover;
|
background-size: cover;
|
||||||
background-position: center;
|
background-position: center;
|
||||||
right: 4px;
|
right: 4px;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.has-submenu .submenu {
|
.wysiwyg .toolbar .line .box .btn.has-submenu .submenu {
|
||||||
display: none;
|
display: none;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 36px;
|
top: 36px;
|
||||||
left: -1px;
|
left: -1px;
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
border: 1px solid #b5b5b5;
|
border: 1px solid #b5b5b5;
|
||||||
border-top: none;
|
border-top: none;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.has-submenu .submenu .btn {
|
.wysiwyg .toolbar .line .box .btn.has-submenu .submenu .btn {
|
||||||
width: 39px;
|
width: 39px;
|
||||||
}
|
}
|
||||||
.wysiwyg .toolbar .line .box .btn.has-submenu:hover .submenu {
|
.wysiwyg .toolbar .line .box .btn.has-submenu:hover .submenu {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
.wysiwyg .content-area {
|
.wysiwyg .content-area {
|
||||||
padding: 15px 12px;
|
padding: 15px 12px;
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
}
|
}
|
||||||
.wysiwyg .content-area .visuell-view {
|
.wysiwyg .content-area .visuell-view {
|
||||||
outline: none;
|
outline: none;
|
||||||
}
|
}
|
||||||
.wysiwyg .content-area .visuell-view p {
|
.wysiwyg .content-area .visuell-view p {
|
||||||
margin: 12px 0;
|
margin: 12px 0;
|
||||||
}
|
}
|
||||||
.wysiwyg .content-area .html-view {
|
.wysiwyg .content-area .html-view {
|
||||||
outline: none;
|
outline: none;
|
||||||
display: none;
|
display: none;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 200px;
|
height: 200px;
|
||||||
border: none;
|
border: none;
|
||||||
resize: none;
|
resize: none;
|
||||||
}
|
}
|
||||||
278
wysiwyg.js
278
wysiwyg.js
@ -1,140 +1,140 @@
|
|||||||
function initWYSIWYG() {
|
function initWYSIWYG() {
|
||||||
$('.wysiwyg').html('<div class="toolbar">'+
|
$('.wysiwyg').html('<div class="toolbar">'+
|
||||||
' <div class="line">'+
|
' <div class="line">'+
|
||||||
' '+
|
' '+
|
||||||
' <div class="box">'+
|
' <div class="box">'+
|
||||||
' <span class="btn icon smaller" data-action="bold" title="Bold">'+
|
' <span class="btn icon smaller" data-action="bold" title="Bold">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25432.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25432.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon smaller" data-action="italic" title="Italic">'+
|
' <span class="btn icon smaller" data-action="italic" title="Italic">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25392.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25392.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon smaller" data-action="underline" title="Underline">'+
|
' <span class="btn icon smaller" data-action="underline" title="Underline">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25433.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25433.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon smaller" data-action="strikeThrough" title="Strike through">'+
|
' <span class="btn icon smaller" data-action="strikeThrough" title="Strike through">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25626.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25626.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' '+
|
' '+
|
||||||
' <div class="box">'+
|
' <div class="box">'+
|
||||||
' <span class="btn icon has-submenu">'+
|
' <span class="btn icon has-submenu">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25351.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25351.svg">'+
|
||||||
' <div class="submenu">'+
|
' <div class="submenu">'+
|
||||||
' <span class="btn icon" data-action="justifyLeft" title="Justify left">'+
|
' <span class="btn icon" data-action="justifyLeft" title="Justify left">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25351.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25351.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="justifyCenter" title="Justify center">'+
|
' <span class="btn icon" data-action="justifyCenter" title="Justify center">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25440.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25440.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="justifyRight" title="Justify right">'+
|
' <span class="btn icon" data-action="justifyRight" title="Justify right">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25288.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25288.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="formatBlock" title="Justify block">'+
|
' <span class="btn icon" data-action="formatBlock" title="Justify block">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25181.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25181.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="insertOrderedList" title="Insert ordered list">'+
|
' <span class="btn icon" data-action="insertOrderedList" title="Insert ordered list">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25242.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25242.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="insertUnorderedList" title="Insert unordered list">'+
|
' <span class="btn icon" data-action="insertUnorderedList" title="Insert unordered list">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25648.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25648.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="outdent" title="Outdent">'+
|
' <span class="btn icon" data-action="outdent" title="Outdent">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25410.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25410.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="indent" title="Indent">'+
|
' <span class="btn icon" data-action="indent" title="Indent">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25233.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25233.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' '+
|
' '+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' <div class="box">'+
|
' <div class="box">'+
|
||||||
' <span class="btn icon" data-action="insertHorizontalRule" title="Insert horizontal rule">'+
|
' <span class="btn icon" data-action="insertHorizontalRule" title="Insert horizontal rule">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25232.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25232.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' '+
|
' '+
|
||||||
'</div>'+
|
'</div>'+
|
||||||
'<div class="line">'+
|
'<div class="line">'+
|
||||||
' '+
|
' '+
|
||||||
' <div class="box">'+
|
' <div class="box">'+
|
||||||
' <span class="btn icon smaller" data-action="undo" title="Undo">'+
|
' <span class="btn icon smaller" data-action="undo" title="Undo">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25249.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25249.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon" data-action="removeFormat" title="Remove format">'+
|
' <span class="btn icon" data-action="removeFormat" title="Remove format">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25454.svg"> '+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25454.svg"> '+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' '+
|
' '+
|
||||||
' <div class="box">'+
|
' <div class="box">'+
|
||||||
' <span class="btn icon smaller" data-action="createLink" title="Insert Link">'+
|
' <span class="btn icon smaller" data-action="createLink" title="Insert Link">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25385.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25385.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' <span class="btn icon smaller" data-action="unlink" title="Unlink">'+
|
' <span class="btn icon smaller" data-action="unlink" title="Unlink">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25341.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25341.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' <div class="box">'+
|
' <div class="box">'+
|
||||||
' <span class="btn icon" data-action="code" title="Show HTML-Code">'+
|
' <span class="btn icon" data-action="code" title="Show HTML-Code">'+
|
||||||
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25185.svg">'+
|
' <img loading="lazy" src="https://image.flaticon.com/icons/svg/25/25185.svg">'+
|
||||||
' </span>'+
|
' </span>'+
|
||||||
' </div>'+
|
' </div>'+
|
||||||
' '+
|
' '+
|
||||||
'</div>'+
|
'</div>'+
|
||||||
'</div>'+
|
'</div>'+
|
||||||
'<div class="content-area">'+
|
'<div class="content-area">'+
|
||||||
'<div class="visuell-view" contenteditable>'+
|
'<div class="visuell-view" contenteditable>'+
|
||||||
'</div>'+
|
'</div>'+
|
||||||
'<textarea class="html-view"></textarea>'+
|
'<textarea class="html-view"></textarea>'+
|
||||||
'</div>');
|
'</div>');
|
||||||
}
|
}
|
||||||
|
|
||||||
initWYSIWYG();
|
initWYSIWYG();
|
||||||
|
|
||||||
const editor = document.getElementsByClassName('wysiwyg')[0];
|
const editor = document.getElementsByClassName('wysiwyg')[0];
|
||||||
console.log(editor);
|
console.log(editor);
|
||||||
const toolbar = editor.getElementsByClassName('toolbar')[0];
|
const toolbar = editor.getElementsByClassName('toolbar')[0];
|
||||||
const buttons = toolbar.querySelectorAll('.btn:not(.has-submenu)');
|
const buttons = toolbar.querySelectorAll('.btn:not(.has-submenu)');
|
||||||
for(let i = 0; i < buttons.length; i++) {
|
for(let i = 0; i < buttons.length; i++) {
|
||||||
let button = buttons[i];
|
let button = buttons[i];
|
||||||
|
|
||||||
button.addEventListener('click', function(e) {
|
button.addEventListener('click', function(e) {
|
||||||
let action = this.dataset.action;
|
let action = this.dataset.action;
|
||||||
|
|
||||||
switch(action) {
|
switch(action) {
|
||||||
case 'code':
|
case 'code':
|
||||||
execCodeAction(this, editor);
|
execCodeAction(this, editor);
|
||||||
break;
|
break;
|
||||||
case 'createLink':
|
case 'createLink':
|
||||||
execLinkAction();
|
execLinkAction();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
execDefaultAction(action);
|
execDefaultAction(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function execCodeAction(button, editor) {
|
function execCodeAction(button, editor) {
|
||||||
const contentArea = editor.getElementsByClassName('content-area')[0];
|
const contentArea = editor.getElementsByClassName('content-area')[0];
|
||||||
const visuellView = contentArea.getElementsByClassName('visuell-view')[0];
|
const visuellView = contentArea.getElementsByClassName('visuell-view')[0];
|
||||||
const htmlView = contentArea.getElementsByClassName('html-view')[0];
|
const htmlView = contentArea.getElementsByClassName('html-view')[0];
|
||||||
if(button.classList.contains('active')) { // show visuell view
|
if(button.classList.contains('active')) { // show visuell view
|
||||||
visuellView.innerHTML = htmlView.value;
|
visuellView.innerHTML = htmlView.value;
|
||||||
htmlView.style.display = 'none';
|
htmlView.style.display = 'none';
|
||||||
visuellView.style.display = 'block';
|
visuellView.style.display = 'block';
|
||||||
button.classList.remove('active');
|
button.classList.remove('active');
|
||||||
} else { // show html view
|
} else { // show html view
|
||||||
htmlView.innerText = visuellView.innerHTML;
|
htmlView.innerText = visuellView.innerHTML;
|
||||||
visuellView.style.display = 'none';
|
visuellView.style.display = 'none';
|
||||||
htmlView.style.display = 'block';
|
htmlView.style.display = 'block';
|
||||||
button.classList.add('active');
|
button.classList.add('active');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function execLinkAction() {
|
function execLinkAction() {
|
||||||
let linkValue = prompt('Link (e.g. https://webdeasy.de/)');
|
let linkValue = prompt('Link (e.g. https://webdeasy.de/)');
|
||||||
document.execCommand('createLink', false, linkValue);
|
document.execCommand('createLink', false, linkValue);
|
||||||
}
|
}
|
||||||
function execDefaultAction(action) {
|
function execDefaultAction(action) {
|
||||||
document.execCommand(action, false);
|
document.execCommand(action, false);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user