Skip to content

Commit

Permalink
handle live update special cases (enter, backspace)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Aug 31, 2014
1 parent 6f0a1de commit f6b5d9d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
39 changes: 32 additions & 7 deletions dist/content-kit-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ define("content-kit-utils/node-utils",
/**
* Extracts attributes of a `Node` to a hash of key/value pairs
*/
function attributesForNode(node /*,blacklist*/) {
function attributesForNode(node, blacklist) {
var attrs = node.attributes;
var len = attrs && attrs.length;
var i, attr, name, hash;
Expand All @@ -252,7 +252,7 @@ define("content-kit-utils/node-utils",
attr = attrs[i];
name = attr.name;
if (attr.specified && attr.value) {
//if (blacklist && name in blacklist)) { continue; }
if (blacklist && (name in blacklist)) { continue; }
hash = hash || {};
hash[name] = attr.value;
}
Expand Down Expand Up @@ -982,7 +982,7 @@ define("content-kit-compiler/parsers/html-parser",
type_name : this.includeTypeNames && type.name,
start : startIndex,
end : endIndex,
attributes : attributesForNode(node)
attributes : attributesForNode(node, { style: 1 }) // filter out inline styles
});
}
}
Expand Down Expand Up @@ -1310,13 +1310,17 @@ define("content-kit-compiler/types/type-set",
* Returns type info for a given Node
*/
findByNode: function(node) {
return this.findByTag(node.tagName);
if (node) {
return this.findByTag(node.tagName);
}
},
/**
* Returns type info for a given tag
*/
findByTag: function(tag) {
return this.tagLookup[tag.toLowerCase()];
if (tag) {
return this.tagLookup[tag.toLowerCase()];
}
},
/**
* Returns type info for a given id
Expand Down Expand Up @@ -2022,9 +2026,22 @@ define("content-kit-editor/editor/editor",
}

function bindLiveUpdate(editor) {
editor.element.addEventListener('input', function() {
editor.element.addEventListener('input', function(e) {
editor.syncModelAtSelection();
});

// Handle special cases
editor.element.addEventListener('keyup', function(e) {
// When pressing enter: parse block before cursor too
if(!e.shiftKey && e.which === Keycodes.ENTER) {
editor.syncModelAt(editor.getCurrentBlockIndex()-1);
}
// When pressing backspace/del: parse block after cursor too
else if(e.which === Keycodes.BKSP || e.which === Keycodes.DEL) {
editor.syncModelAt(editor.getCurrentBlockIndex()+1);
}

});
}

function initEmbedCommands(editor) {
Expand Down Expand Up @@ -2100,7 +2117,11 @@ define("content-kit-editor/editor/editor",
if (index > -1) {
var blockElements = toArray(this.element.children);
var parsedBlockModel = this.compiler.parser.parseBlock(blockElements[index]);
this.model[index] = parsedBlockModel;
if (parsedBlockModel) {
this.model[index] = parsedBlockModel;
} else {
this.model.splice(index, 1);
}
this.trigger('update', { index: index });
}
};
Expand Down Expand Up @@ -2149,6 +2170,10 @@ define("content-kit-editor/editor/editor",
this.textFormatToolbar.addCommand(command);
};

Editor.prototype.text = function() {
getCursorIndexInSelectionBlockElement();
}

__exports__["default"] = Editor;
});
define("content-kit-editor/utils/element-utils",
Expand Down
2 changes: 1 addition & 1 deletion src/js/content-kit-compiler/parsers/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ HTMLParser.prototype.parseElementMarkup = function(node, startIndex) {
type_name : this.includeTypeNames && type.name,
start : startIndex,
end : endIndex,
attributes : attributesForNode(node)
attributes : attributesForNode(node, { style: 1 }) // filter out inline styles
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/js/content-kit-compiler/types/type-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ TypeSet.prototype = {
* Returns type info for a given Node
*/
findByNode: function(node) {
return this.findByTag(node.tagName);
if (node) {
return this.findByTag(node.tagName);
}
},
/**
* Returns type info for a given tag
*/
findByTag: function(tag) {
return this.tagLookup[tag.toLowerCase()];
if (tag) {
return this.tagLookup[tag.toLowerCase()];
}
},
/**
* Returns type info for a given id
Expand Down
25 changes: 23 additions & 2 deletions src/js/content-kit-editor/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,22 @@ function bindAutoTypingListeners(editor) {
}

function bindLiveUpdate(editor) {
editor.element.addEventListener('input', function() {
editor.element.addEventListener('input', function(e) {
editor.syncModelAtSelection();
});

// Handle special cases
editor.element.addEventListener('keyup', function(e) {
// When pressing enter: parse block before cursor too
if(!e.shiftKey && e.which === Keycodes.ENTER) {
editor.syncModelAt(editor.getCurrentBlockIndex()-1);
}
// When pressing backspace/del: parse block after cursor too
else if(e.which === Keycodes.BKSP || e.which === Keycodes.DEL) {
editor.syncModelAt(editor.getCurrentBlockIndex()+1);
}

});
}

function initEmbedCommands(editor) {
Expand Down Expand Up @@ -177,7 +190,11 @@ Editor.prototype.syncModelAt = function(index) {
if (index > -1) {
var blockElements = toArray(this.element.children);
var parsedBlockModel = this.compiler.parser.parseBlock(blockElements[index]);
this.model[index] = parsedBlockModel;
if (parsedBlockModel) {
this.model[index] = parsedBlockModel;
} else {
this.model.splice(index, 1);
}
this.trigger('update', { index: index });
}
};
Expand Down Expand Up @@ -226,4 +243,8 @@ Editor.prototype.addTextFormat = function(opts) {
this.textFormatToolbar.addCommand(command);
};

Editor.prototype.text = function() {
getCursorIndexInSelectionBlockElement();
}

export default Editor;
4 changes: 2 additions & 2 deletions src/js/content-kit-utils/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function unwrapNode(node) {
/**
* Extracts attributes of a `Node` to a hash of key/value pairs
*/
function attributesForNode(node /*,blacklist*/) {
function attributesForNode(node, blacklist) {
var attrs = node.attributes;
var len = attrs && attrs.length;
var i, attr, name, hash;
Expand All @@ -59,7 +59,7 @@ function attributesForNode(node /*,blacklist*/) {
attr = attrs[i];
name = attr.name;
if (attr.specified && attr.value) {
//if (blacklist && name in blacklist)) { continue; }
if (blacklist && (name in blacklist)) { continue; }
hash = hash || {};
hash[name] = attr.value;
}
Expand Down

0 comments on commit f6b5d9d

Please sign in to comment.