Skip to content
Browse files

extend selection to word boundaries on bold/italic

  • Loading branch information...
1 parent 9dd2f30 commit 8195279b6c8efe6679fcfaf07cd92e895c712f52 @johnny committed Nov 18, 2011
View
2 TODO
@@ -1,12 +1,12 @@
* [30/40] Further improvements
- [-] wysiwyg
- - [ ] expand selection to word boundaries, just as in textile
- [ ] Firefox: fix checkState for one bold word
- [ ] br tags should be top level, because Border expects that
or alternatively change behavior of the node search within Border
This would be the approach with a future since there might be other
cases of nested nodes ( b>i, i>b would be different now, but should
be the same)
+ - [X] expand selection to word boundaries, just as in textile
- [X] pressing enter on empty list item should remove the list item and add a paragraph
- [X] join lists on backspace/entf between lists
- [X] Toolbar should work only if there is a range inside the preview
View
35 source/javascripts/modes/wysiwygMode.js
@@ -446,6 +446,39 @@
}
}
+ function extendRangeToSpaces(){
+ // extend selection to word boundaries
+ var text, changedRange,
+ range = selection.getRangeAt(0),
+ startNode = range.startContainer,
+ startOffset = range.startOffset,
+ endNode = range.endContainer,
+ endOffset = range.endOffset;
+
+ if(startNode.nodeType == 3){ // Its a textnode
+ changedRange = true;
+ text = startNode.nodeValue;
+ range.setStart(startNode, text.lastIndexOf(' ', startOffset) + 1);
+ }
+
+ if(endNode.nodeType == 3){ // Its a textnode
+ changedRange = true;
+ text = endNode.nodeValue;
+
+ endOffset = text.indexOf(' ', endOffset -1); // -1, otherwise it
+ // might overextend
+ if(endOffset === -1){
+ endOffset = text.length;
+ }
+ range.setEnd(endNode, endOffset);
+ }
+
+ if(changedRange){
+ selection.removeAllRanges();
+ selection.addRange(range);
+ }
+ }
+
/**
* @name wysiwygMode
* @namespace
@@ -464,6 +497,7 @@
items: {
"default": {
clicked: function(editor, target) {
+ extendRangeToSpaces();
document.execCommand(this.name, false, null);
}
},
@@ -500,6 +534,7 @@
},
link: {
clicked: function(editor, target) {
+ extendRangeToSpaces();
var dialog, linkNode, titleString,
range = selection.getRangeAt(0),
callback = {
View
23 source/javascripts/test/support/testHelper.js
@@ -155,6 +155,29 @@ WysiwygHelper.prototype = (function(){
selectAll: function(){
return this.select(this.preview.children());
},
+ // TODO this doesn't work, if the node has multiple text nodes
+ selectText: function(text, offset){
+ var index, startOffset, endOffset,
+ node = this.preview.find(':contains('+ text + ')').last()[0].firstChild;
+
+ if(node.nodeType == 3){ // Its a textnode
+ index = node.nodeValue.indexOf(text);
+ if(offset){
+ endOffset = startOffset = index + offset;
+ } else {
+ startOffset = index;
+ endOffset = index + text.length;
+ }
+ this.range.setStart(node, startOffset);
+ this.range.setEnd(node, endOffset);
+ } else {
+ console.log('node is not a text node', node);
+ }
+
+ this.selection.removeAllRanges();
+ this.selection.addRange(this.range);
+ return this.click('.preview');
+ },
select: function select(identifier){
var node;
if(typeof identifier === "string"){
View
20 source/javascripts/test/wysiwygMode.js
@@ -43,6 +43,22 @@ $(document).ready(function(){
w.match("p");
});
+ test('should extend selection to spaces', function(){
+ w.set('this and that')
+ .selectText('and', 2)
+ .click('.bold')
+ .match('b:contains(and)')
+ .selectText('and', 1)
+ .click('.bold')
+ .notMatch('b')
+ .set('this and that')
+ .selectText(' and ')
+ .click('.bold')
+ .match('b:contains(and)')
+ .notMatch('b:contains( and)')
+ .notMatch('b:contains(and )');
+ });
+
test("Image remove", function(){
w.set("!remove(Title)!")
.select("img")
@@ -189,13 +205,13 @@ $(document).ready(function(){
test("Link create", function(){
w.set("This")
- .select("p")
+ .selectText("This",2)
.off('link')
.dialog('link', function(d){
d.set('uri','src');
d.click("Create");
})
- .match("a")
+ .match("a:contains(This)")
.on('link');
});

0 comments on commit 8195279

Please sign in to comment.
Something went wrong with that request. Please try again.