Created
May 12, 2016 12:53
-
-
Save BertrandBordage/cef9b4b1f3ac4c3e39424e48af1b85e6 to your computer and use it in GitHub Desktop.
Home-made WYSIWYG editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<style type="text/less"> | |
body { | |
margin: 30px; | |
font-family: Helvetica, sans-serif; | |
} | |
.wysiwyg { | |
.buttons { | |
@button-color: grey; | |
@button-content-size: 20px; | |
@button-padding: 10px; | |
a, .vertical-separator { | |
display: inline-block; | |
text-align: center; | |
vertical-align: middle; | |
} | |
a { | |
width: @button-content-size; | |
height: @button-content-size; | |
line-height: @button-content-size; | |
padding: @button-padding; | |
border: 1px solid @button-color; | |
color: @button-color; | |
background: #f7f7f7; | |
text-decoration: none; | |
&:hover, &.active { | |
padding: @button-padding - 1px; | |
color: black; | |
border-color: black; | |
border-width: 2px; | |
} | |
&.bold { | |
font-weight: bold; | |
} | |
&.italic { | |
font-style: italic; | |
} | |
&.underline { | |
text-decoration: underline; | |
} | |
&.line-through { | |
text-decoration: line-through; | |
} | |
&.code { | |
font-family: Courier, monospace; | |
} | |
} | |
.vertical-separator { | |
width: 1px; | |
height: @button-content-size + @button-padding * 2; | |
margin: 0 5px; | |
background: @button-color; | |
} | |
} | |
.content { | |
min-height: 200px; | |
margin-top: 15px; | |
padding: 5px; | |
border: 1px solid grey; | |
} | |
} | |
</style> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.1/less.min.js"></script> | |
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script> | |
</head> | |
<body> | |
<div class="wysiwyg"> | |
<div class="buttons"> | |
<a href="#" class="bold">B</a> | |
<a href="#" class="italic">I</a> | |
<a href="#" class="underline">U</a> | |
<a href="#" class="line-through">S</a> | |
<a href="#" class="code">{}</a> | |
<span class="vertical-separator"></span> | |
<a href="#" class="p">P</a> | |
<a href="#" class="h2">H2</a> | |
<a href="#" class="h3">H3</a> | |
<a href="#" class="h4">H4</a> | |
<a href="#" class="h5">H5</a> | |
<a href="#" class="h6">H6</a> | |
</div> | |
<div class="content" contenteditable="true"> | |
<p> | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit. | |
</p> | |
<p> | |
Ut velit mauris, egestas sed, gravida nec, ornare ut, mi. Aenean ut orci vel massa suscipit pulvinar. Nulla sollicitudin. Fusce varius, ligula non tempus aliquam, nunc turpis ullamcorper nibh, in tempus sapien eros vitae ligula. Pellentesque rhoncus nunc et augue. Integer id felis. Curabitur aliquet pellentesque diam. Integer quis metus vitae elit lobortis egestas. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi vel erat non mauris convallis vehicula. Nulla et sapien. Integer tortor tellus, aliquam faucibus, convallis id, congue eu, quam. Mauris ullamcorper felis vitae erat. Proin feugiat, augue non elementum posuere, metus purus iaculis lectus, et tristique ligula justo vitae magna. | |
</p> | |
<p> | |
Aliquam convallis sollicitudin purus. Praesent aliquam, enim at fermentum mollis, ligula massa adipiscing nisl, ac euismod nibh nisl eu lectus. Fusce vulputate sem at sapien. Vivamus leo. Aliquam euismod libero eu enim. Nulla nec felis sed leo placerat imperdiet. Aenean suscipit nulla in justo. Suspendisse cursus rutrum augue. Nulla tincidunt tincidunt mi. Curabitur iaculis, lorem vel rhoncus faucibus, felis magna fermentum augue, et ultricies lacus lorem varius purus. Curabitur eu amet. | |
</p> | |
</div> | |
</div> | |
<script> | |
$(function () { | |
$('.wysiwyg').each(function () { | |
var $wysiwyg = $(this); | |
var $buttons = $wysiwyg.find('.buttons a'); | |
var $content = $wysiwyg.find('.content'); | |
function addElement(tagName) { | |
return function (e) { | |
e.preventDefault(); | |
var selection = window.getSelection(); | |
var range; | |
if (isActive(tagName)[0]) { | |
unwrap(tagName); | |
} else { | |
for (var i = 0; i < selection.rangeCount; i++) { | |
range = selection.getRangeAt(i).cloneRange(); | |
var isInContent = $(range.commonAncestorContainer) | |
.closest($content).length == 1; | |
if (!isInContent) { | |
continue; | |
} | |
var isCollapsed = selection.isCollapsed; | |
var tag = document.createElement(tagName); | |
range.surroundContents(tag); | |
if (isCollapsed) { | |
// We create a character without width so the caret | |
// positions itself inside the empty tag. | |
tag.innerHTML = '\u200B'; | |
} | |
range.selectNodeContents(tag); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} | |
} | |
$content.focus(); | |
selectionChanged(); | |
} | |
} | |
var buttonsBindings = { | |
'.bold': 'b', | |
'.italic': 'i', | |
'.underline': 'u', | |
'.line-through': 'del', | |
'.code': 'code', | |
'.p': 'p', | |
'.h2': 'h2', | |
'.h3': 'h3', | |
'.h4': 'h4', | |
'.h5': 'h5', | |
'.h6': 'h6' | |
}; | |
$.each(buttonsBindings, function (buttonSelector, tagName) { | |
$buttons.filter(buttonSelector).click(addElement(tagName)); | |
}); | |
function isActive(tagName) { | |
var selection = window.getSelection(); | |
var active = true; | |
var $ancestors = $(); | |
for (var i = 0; i < selection.rangeCount; i++) { | |
var range = selection.getRangeAt(i); | |
var $ancestor = $(range.commonAncestorContainer) | |
.parentsUntil($content).andSelf().filter(tagName).last(); | |
active = active && $ancestor.length != 0; | |
$ancestors = $ancestors.add($ancestor); | |
} | |
return [active, $ancestors]; | |
} | |
function unwrap(tagName) { | |
isActive(tagName)[1].contents().unwrap(); | |
} | |
function selectionChanged() { | |
$.each(buttonsBindings, function (buttonSelector, tagName) { | |
var $button = $buttons.filter(buttonSelector); | |
$button.toggleClass('active', isActive(tagName)[0]); | |
}); | |
} | |
$content.mouseup(selectionChanged); | |
$content.keyup(selectionChanged); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment