Skip to content

Commit

Permalink
list(head, last, tail, clusterBy, compact), range(listPara)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed May 21, 2013
1 parent 908bef0 commit 9f76db4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
64 changes: 54 additions & 10 deletions summernote.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var bMac = navigator.appVersion.indexOf('Mac') > -1;

/**
* iter
* iter utils
*/
var iter = function() {
var hasAttr = function(sAttr) {
Expand All @@ -26,7 +26,41 @@
}();

/**
* dom
* list utils
*/
var list = function() {
var head = function(array) { return array[0]; };
var last = function(array) { return array[array.length - 1]; };
var tail = function(array) { return array.slice(1); };

var clusterBy = function(array, fn) {
if (array.length === 0) { return []; }
var aTail = tail(array);
return aTail.reduce(function (memo, v) {
var aLast = last(memo);
if (fn(last(aLast), v)) {
aLast[aLast.length] = v;
} else {
memo[memo.length] = [v];
}
return memo;
}, [[head(array)]]);
};

var compact = function(array) {
var aResult = [];
for(var idx = 0; idx < array.length; idx ++) {
if (array[idx]) { aResult.push(array[idx]); };
};
return aResult;
};

return { head: head, last: last, tail: tail,
compact: compact, clusterBy: clusterBy };
}();

/**
* dom utils
*/
var dom = function() {
// nodeName of element are always uppercase.
Expand Down Expand Up @@ -88,9 +122,9 @@
/**
* listBetween
* listing all Nodes between nodeA and nodeB
* FIXME: nodeA and nodeB must be sorted, use comparePoints later.
*/
var listBetween = function(nodeA, nodeB) {
//FIXME: must nodeA and nodeB be sorted, use comparePoints later.
var aNode = [];
var elAncestor = commonAncestor(nodeA, nodeB);
//TODO: IE8, createNodeIterator
Expand Down Expand Up @@ -149,9 +183,21 @@
} // TODO: handle IE8+ TextRange
}

this.getParas = function() {
/**
* listPara
*
* listing paragraphs on range
*/
this.listPara = function() {
var aNode = dom.listBetween(sc, ec);
//filter out aPara
// TODO: IE8 use es5-shim(https://github.com/kriskowal/es5-shim) later
var aPara = list.compact(aNode.map(function(node) {
return dom.ancestor(node, dom.isPara);
}));
var aaClustered = list.clusterBy(aPara, function(nodeA, nodeB) {
return nodeA === nodeB;
});
return aaClustered.map(list.head);
};

/**
Expand Down Expand Up @@ -257,8 +303,8 @@

this.lineHeight = function(sValue) {
var rng = new Range();
var aPara = rng.getParas();
//lineHeight
var aPara = rng.listPara();
console.log(aPara);
};

this.unlink = function() {
Expand Down Expand Up @@ -820,9 +866,7 @@
},
// inner object for test
summernoteInner : function() {
return {
dom: dom
};
return { dom: dom, list: list };
}
});
})(jQuery); // jQuery
16 changes: 16 additions & 0 deletions test/list.spec.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>summernote :: list.spec</title>
<link rel="stylesheet" href="qunit-1.11.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="qunit-1.11.0.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="../summernote.js"></script>
<script src="list.spec.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions test/list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* list.spec.js
* (c) 2013~ Youngteac Hong
* summernote may be freely distributed under the MIT license./
*/
var list = $.fn.summernoteInner().list;

test('list.compact', function() {
deepEqual(list.compact([0, 1, false, 2, '', 3]), [1,2,3],
'falsey values of `array` removed');
});

test('list.clusterBy', function() {
var aaClustered = list.clusterBy([1, 1, 2, 2, 3], function(itemA, itemB) {
return itemA === itemB;
});
deepEqual([[1, 1], [2, 2], [3]], aaClustered, 'clusterBy equality 1');

var aaClustered = list.clusterBy([1, 2, 2, 1, 3], function(itemA, itemB) {
return itemA === itemB;
});
deepEqual([[1], [2, 2], [1], [3]], aaClustered, 'clusterBy equality 2');
});

0 comments on commit 9f76db4

Please sign in to comment.