Skip to content

Commit

Permalink
link tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Jul 11, 2014
1 parent 8542c4e commit 1bd3276
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 7 deletions.
75 changes: 74 additions & 1 deletion dist/content-kit-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ function swapElements(elementToShow, elementToHide) {
showElement(elementToShow);
}

function getTargetNodeDescendentWithTag(tag, target, container) {
while (target && target !== container) {
if (target.tagName === tag) {
return target;
}
target = target.parentNode;
}
}

function getElementOffset(element) {
var offset = { left: 0, top: 0 };
var elementStyle = window.getComputedStyle(element);
Expand Down Expand Up @@ -496,7 +505,8 @@ ContentKit.Editor = (function() {
bindTextSelectionEvents(editor);
bindTypingEvents(editor);
bindPasteEvents(editor);

bindLinkTooltips(editor);

editor.enable();
if(editor.autofocus) { element.focus(); }
}
Expand Down Expand Up @@ -623,6 +633,28 @@ ContentKit.Editor = (function() {
});
}

function bindLinkTooltips(editor) {
var linkTooltip = new Tooltip();
var editorElement = editor.element;
var tooltipTimeout = null;
editorElement.addEventListener('mouseover', function(e) {
if (!editor.toolbar.isShowing) {
tooltipTimeout = setTimeout(function() {
var linkTarget = getTargetNodeDescendentWithTag(Tags.LINK, e.target, this);
if (linkTarget) {
linkTooltip.showLink(linkTarget.href, linkTarget);
}
}, 200);
}
});
editorElement.addEventListener('mouseout', function(e) {
clearTimeout(tooltipTimeout);
if (e.toElement && e.toElement.className !== 'ck-tooltip') {
linkTooltip.hide();
}
});
}

function plainTextToBlocks(plainText, blockTag) {
var blocks = plainText.split(Regex.NEWLINE),
len = blocks.length,
Expand Down Expand Up @@ -822,4 +854,45 @@ var ToolbarButton = (function() {
return ToolbarButton;
}());

var Tooltip = (function() {

var container = document.body;

function Tooltip() {
var tooltip = this;
tooltip.element = createDiv('ck-tooltip');
tooltip.isShowing = false;
}

Tooltip.prototype = {
showMessage: function(message, element) {
var tooltip = this;
var tooltipElement = tooltip.element;
var elementRect = element.getBoundingClientRect();

tooltipElement.innerHTML = message;
if (!tooltip.isShowing) {
container.appendChild(tooltipElement);
tooltip.isShowing = true;
}

tooltipElement.style.left = parseInt(elementRect.left + (element.offsetWidth / 2) - (tooltipElement.offsetWidth / 2), 10) + 'px';
tooltipElement.style.top = parseInt(window.pageYOffset + elementRect.top + element.offsetHeight + 2, 10) + 'px';
},
showLink: function(link, element) {
var message = '<a href="' + link + '" target="_blank">' + link + '</a>';
this.showMessage(message, element);
},
hide: function() {
var tooltip = this;
if (tooltip.isShowing) {
container.removeChild(tooltip.element);
tooltip.isShowing = false;
}
}
};

return Tooltip;
}());

}(this, document));
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var src = [
'./src/js/commands.js',
'./src/js/editor.js',
'./src/js/toolbar.js',
'./src/js/toolbar-button.js'
'./src/js/toolbar-button.js',
'./src/js/tooltip.js'
];

var distName = 'content-kit-editor.js';
Expand Down
41 changes: 37 additions & 4 deletions src/css/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,41 @@
z-index: -1;
background-color: rgba(76,217,100,0.05);
border-bottom: 2px dotted #4CD964;
-webkit-animation: hiliteAppear 0.2s;
animation: hiliteAppear 0.2s;
-webkit-animation: fadeIn 0.2s;
animation: fadeIn 0.2s;
}

.ck-tooltip {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 0.7em;
white-space: nowrap;
position: absolute;
background-color: rgba(43,43,43,0.9);
border-radius: 3px;
line-height: 1em;
padding: 0.7em 0.9em;
color: #FFF;
-webkit-animation: fadeIn 0.2s;
animation: fadeIn 0.2s;
}
.ck-tooltip:before {
content: '';
position: absolute;
left: 50%;
width: 0;
height: 0;
border-left: 0.4em solid transparent;
border-right: 0.4em solid transparent;
border-bottom: 0.4em solid rgba(43,43,43,0.9);
top: -0.4em;
margin-left: -0.4em;
}
.ck-tooltip a {
color: #FFF;
text-decoration: none;
}
.ck-tooltip a:hover {
text-decoration: underline;
}

/* icons */
Expand Down Expand Up @@ -187,11 +220,11 @@
}

/* animations */
@-webkit-keyframes hiliteAppear {
@-webkit-keyframes fadeIn {
0% { opacity: 0; }
100% { opacity: 1; }
}
@keyframes hiliteAppear {
@keyframes fadeIn {
0% { opacity: 0; }
100% { opacity: 1; }
}
Expand Down
25 changes: 24 additions & 1 deletion src/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ ContentKit.Editor = (function() {
bindTextSelectionEvents(editor);
bindTypingEvents(editor);
bindPasteEvents(editor);

bindLinkTooltips(editor);

editor.enable();
if(editor.autofocus) { element.focus(); }
}
Expand Down Expand Up @@ -199,6 +200,28 @@ ContentKit.Editor = (function() {
});
}

function bindLinkTooltips(editor) {
var linkTooltip = new Tooltip();
var editorElement = editor.element;
var tooltipTimeout = null;
editorElement.addEventListener('mouseover', function(e) {
if (!editor.toolbar.isShowing) {
tooltipTimeout = setTimeout(function() {
var linkTarget = getTargetNodeDescendentWithTag(Tags.LINK, e.target, this);
if (linkTarget) {
linkTooltip.showLink(linkTarget.href, linkTarget);
}
}, 200);
}
});
editorElement.addEventListener('mouseout', function(e) {
clearTimeout(tooltipTimeout);
if (e.toElement && e.toElement.className !== 'ck-tooltip') {
linkTooltip.hide();
}
});
}

function plainTextToBlocks(plainText, blockTag) {
var blocks = plainText.split(Regex.NEWLINE),
len = blocks.length,
Expand Down
40 changes: 40 additions & 0 deletions src/js/tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var Tooltip = (function() {

var container = document.body;

function Tooltip() {
var tooltip = this;
tooltip.element = createDiv('ck-tooltip');
tooltip.isShowing = false;
}

Tooltip.prototype = {
showMessage: function(message, element) {
var tooltip = this;
var tooltipElement = tooltip.element;
var elementRect = element.getBoundingClientRect();

tooltipElement.innerHTML = message;
if (!tooltip.isShowing) {
container.appendChild(tooltipElement);
tooltip.isShowing = true;
}

tooltipElement.style.left = parseInt(elementRect.left + (element.offsetWidth / 2) - (tooltipElement.offsetWidth / 2), 10) + 'px';
tooltipElement.style.top = parseInt(window.pageYOffset + elementRect.top + element.offsetHeight + 2, 10) + 'px';
},
showLink: function(link, element) {
var message = '<a href="' + link + '" target="_blank">' + link + '</a>';
this.showMessage(message, element);
},
hide: function() {
var tooltip = this;
if (tooltip.isShowing) {
container.removeChild(tooltip.element);
tooltip.isShowing = false;
}
}
};

return Tooltip;
}());
9 changes: 9 additions & 0 deletions src/js/utils/element-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ function swapElements(elementToShow, elementToHide) {
showElement(elementToShow);
}

function getTargetNodeDescendentWithTag(tag, target, container) {
while (target && target !== container) {
if (target.tagName === tag) {
return target;
}
target = target.parentNode;
}
}

function getElementOffset(element) {
var offset = { left: 0, top: 0 };
var elementStyle = window.getComputedStyle(element);
Expand Down

0 comments on commit 1bd3276

Please sign in to comment.