Skip to content

Commit

Permalink
more embed intent ux fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Jul 22, 2014
1 parent 822d40a commit a6321a4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dist/content-kit-editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
font-size: 2em;
line-height: 0.7em;
cursor: pointer;
transition: color 0.15s, border-color 0.15s, -webkit-transform 0.35s;
transition: color 0.15s, border-color 0.15s, transform 0.35s;
-webkit-animation: pop 0.5s linear;
animation: pop 0.5s linear;
}
Expand Down
28 changes: 16 additions & 12 deletions dist/content-kit-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @version 0.1.0
* @author Garth Poitras <garth22@gmail.com> (http://garthpoitras.com/)
* @license MIT
* Last modified: Jul 18, 2014
* Last modified: Jul 22, 2014
*/

(function(exports, document) {
Expand Down Expand Up @@ -166,7 +166,8 @@ function positionElementToRightOf(element, rightOfElement) {
}

function getDirectionOfSelection(selection) {
var position = selection.anchorNode.compareDocumentPosition(selection.focusNode);
var node = selection.anchorNode;
var position = node && node.compareDocumentPosition(selection.focusNode);
if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
return SelectionDirection.LEFT_TO_RIGHT;
} else if (position & Node.DOCUMENT_POSITION_PRECEDING) {
Expand All @@ -183,7 +184,7 @@ function getCurrentSelectionNode(selection) {

function getCurrentSelectionRootNode() {
var node = getCurrentSelectionNode();
var tag = node.tagName;
var tag = node && node.tagName;
while (tag && RootTags.indexOf(tag) === -1) {
if (node.contentEditable === 'true') { break; } // Stop traversing up dom when hitting an editor element
node = node.parentNode;
Expand Down Expand Up @@ -625,7 +626,6 @@ ContentKit.Editor = (function() {
if (element) {
var className = element.className;
var dataset = element.dataset;
var textFormatToolbar = new Toolbar({ commands: editor.textFormatCommands });

if (!editorClassNameRegExp.test(className)) {
className += (className ? ' ' : '') + editorClassName;
Expand All @@ -641,20 +641,23 @@ ContentKit.Editor = (function() {

element.setAttribute('contentEditable', true);
editor.element = element;
editor.textFormatToolbar = textFormatToolbar;

bindTextSelectionEvents(editor);
bindTypingEvents(editor);
bindPasteEvents(editor);

var linkTooltips = new Tooltip({ rootElement: element, showForTag: Tags.LINK });

editor.textFormatToolbar = new Toolbar({ commands: editor.textFormatCommands });

if(editor.embedCommands) {
// NOTE: must come after bindTypingEvents so those keyup handlers are executed first.
// TODO: manage event listener order
var embedIntent = new EmbedIntent({
commands: editor.embedCommands,
rootElement: element
});
}

bindTextSelectionEvents(editor);
bindTypingEvents(editor);
bindPasteEvents(editor);

if(editor.autofocus) { element.focus(); }
}
Expand Down Expand Up @@ -1040,11 +1043,11 @@ var EmbedIntent = (function() {
embedIntent.isActive = false;

function embedIntentHandler(e) {
if (!selectionIsInElement(window.getSelection(), rootElement)) {
var currentNode = getCurrentSelectionRootNode();
if (!currentNode) {
embedIntent.hide();
return;
}
var currentNode = getCurrentSelectionRootNode();
var currentNodeHTML = currentNode.innerHTML;
if (currentNodeHTML === '' || currentNodeHTML === '<br>') {
embedIntent.showAt(currentNode);
Expand All @@ -1059,7 +1062,7 @@ var EmbedIntent = (function() {

document.addEventListener('keyup', function(e) {
if (e.keyCode === Keycodes.ESC) {
embedIntent.deactivate();
embedIntent.hide();
}
});

Expand All @@ -1078,6 +1081,7 @@ var EmbedIntent = (function() {
}
},
showAt: function(node) {
this.hide();
this.show();
this.atNode = node;
positionElementToLeftOf(this.element, node);
Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ gulp.task('lint', function() {
.pipe(jshint.reporter('default'));
});

gulp.task('lint-built', function() {
gulp.task('lint-built', ['build'], function() {
return gulp.src(jsDistPath)
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'));
Expand All @@ -91,12 +91,12 @@ gulp.task('build', function() {
.pipe(gulp.dest(distDest));
});

gulp.task('test', function() {
gulp.task('test', ['build', 'lint-built'], function() {
return gulp.src(testRunner)
.pipe(qunit());
});

gulp.task('test-browser', function(){
gulp.task('test-browser', ['build', 'lint-built'], function(){
return gulp.src(testRunner)
.pipe(open('<% file.path %>'));
});
Expand Down
2 changes: 1 addition & 1 deletion src/css/embeds.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
font-size: 2em;
line-height: 0.7em;
cursor: pointer;
transition: color 0.15s, border-color 0.15s, -webkit-transform 0.35s;
transition: color 0.15s, border-color 0.15s, transform 0.35s;
-webkit-animation: pop 0.5s linear;
animation: pop 0.5s linear;
}
Expand Down
14 changes: 8 additions & 6 deletions src/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ ContentKit.Editor = (function() {
if (element) {
var className = element.className;
var dataset = element.dataset;
var textFormatToolbar = new Toolbar({ commands: editor.textFormatCommands });

if (!editorClassNameRegExp.test(className)) {
className += (className ? ' ' : '') + editorClassName;
Expand All @@ -69,20 +68,23 @@ ContentKit.Editor = (function() {

element.setAttribute('contentEditable', true);
editor.element = element;
editor.textFormatToolbar = textFormatToolbar;

bindTextSelectionEvents(editor);
bindTypingEvents(editor);
bindPasteEvents(editor);

var linkTooltips = new Tooltip({ rootElement: element, showForTag: Tags.LINK });

editor.textFormatToolbar = new Toolbar({ commands: editor.textFormatCommands });

if(editor.embedCommands) {
// NOTE: must come after bindTypingEvents so those keyup handlers are executed first.
// TODO: manage event listener order
var embedIntent = new EmbedIntent({
commands: editor.embedCommands,
rootElement: element
});
}

bindTextSelectionEvents(editor);
bindTypingEvents(editor);
bindPasteEvents(editor);

if(editor.autofocus) { element.focus(); }
}
Expand Down
7 changes: 4 additions & 3 deletions src/js/embed-intent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ var EmbedIntent = (function() {
embedIntent.isActive = false;

function embedIntentHandler(e) {
if (!selectionIsInElement(window.getSelection(), rootElement)) {
var currentNode = getCurrentSelectionRootNode();
if (!currentNode) {
embedIntent.hide();
return;
}
var currentNode = getCurrentSelectionRootNode();
var currentNodeHTML = currentNode.innerHTML;
if (currentNodeHTML === '' || currentNodeHTML === '<br>') {
embedIntent.showAt(currentNode);
Expand All @@ -42,7 +42,7 @@ var EmbedIntent = (function() {

document.addEventListener('keyup', function(e) {
if (e.keyCode === Keycodes.ESC) {
embedIntent.deactivate();
embedIntent.hide();
}
});

Expand All @@ -61,6 +61,7 @@ var EmbedIntent = (function() {
}
},
showAt: function(node) {
this.hide();
this.show();
this.atNode = node;
positionElementToLeftOf(this.element, node);
Expand Down
5 changes: 3 additions & 2 deletions src/js/utils/selection-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function getDirectionOfSelection(selection) {
var position = selection.anchorNode.compareDocumentPosition(selection.focusNode);
var node = selection.anchorNode;
var position = node && node.compareDocumentPosition(selection.focusNode);
if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
return SelectionDirection.LEFT_TO_RIGHT;
} else if (position & Node.DOCUMENT_POSITION_PRECEDING) {
Expand All @@ -16,7 +17,7 @@ function getCurrentSelectionNode(selection) {

function getCurrentSelectionRootNode() {
var node = getCurrentSelectionNode();
var tag = node.tagName;
var tag = node && node.tagName;
while (tag && RootTags.indexOf(tag) === -1) {
if (node.contentEditable === 'true') { break; } // Stop traversing up dom when hitting an editor element
node = node.parentNode;
Expand Down

0 comments on commit a6321a4

Please sign in to comment.