Image selection #641
Image selection #641
Changes from 4 commits
e34c686
7780bdc
2da645b
c523284
0dcea99
4a47c58
75ce430
5f6e1fb
1e7dcd8
bf090a9
8b918a2
5458c49
580db7b
92a1af5
eab4b0e
cf02d83
9a5525f
545c3fb
d9a4160
97d1753
d7e9f1b
bb09bdb
b936084
d581582
73a9760
149f898
4388986
1f56d9c
be5fcfd
b7d405b
82715fe
686aac6
37d06d4
ec95b71
0560db2
6e4b123
306fad5
cb38bce
99cbd40
e1aed3a
6c88b05
f25a80a
c6d0553
3c1604f
f1921f8
7d90d19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ | |
prepareUnitTestModule, | ||
test, | ||
expect, | ||
equal, | ||
deepEqual | ||
strictEqual | ||
*/ | ||
"use strict"; | ||
|
||
|
@@ -45,39 +44,29 @@ test("Inserts image into the body", function () { | |
}); | ||
}); | ||
|
||
test("._selectedImage is saved on mousedown", function () { | ||
var initHtml = ["" | ||
, '<p id="noimage">Images? We dont need no silly images</p>' | ||
, '<p>' | ||
, '<img id="google" src="http://example.com/example.jpg" />' | ||
, '</p>' | ||
].join(''), | ||
wymeditor = jQuery.wymeditors(0), | ||
$body, | ||
$noimage, | ||
$google; | ||
module("images-selection", {setup: prepareUnitTestModule}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not how this PR rolls. |
||
expect(3); | ||
test("Image is selected on mousedown", function () { | ||
var noChangeHtml = ["" | ||
, "<p>" | ||
, "A " | ||
, "<img alt=\"Pen\" src=\"http://goo.gl/N9nqUc\" />" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised you're not using your There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Between all of these different branches... It is like juggling. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Fixed. |
||
, "</p>" | ||
].join(""); | ||
|
||
wymeditor.rawHtml(initHtml); | ||
$body = wymeditor.$body(); | ||
|
||
// Editor starts with no selected image. Use equal instead of strictEqual | ||
// because wymeditor._selectedImage intermittently changes between being | ||
// undefined and null, but either value should be acceptable for this test. | ||
equal( | ||
wymeditor._selectedImage, | ||
null | ||
); | ||
|
||
// Clicking on a non-image doesn't change that | ||
$noimage = $body.find('#noimage'); | ||
$noimage.mousedown(); | ||
deepEqual(wymeditor._selectedImage, null); | ||
|
||
|
||
// Clicking an image does update the selected image | ||
$google = $body.find('#google'); | ||
$google.mousedown(); | ||
deepEqual(wymeditor._selectedImage, $google[0]); | ||
manipulationTestHelper({ | ||
startHtml: noChangeHtml, | ||
prepareFunc: function (wymeditor) { | ||
wymeditor.$body().find("img").mousedown(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The manipulation in this test is a |
||
}, | ||
expectedResultHtml: noChangeHtml, | ||
additionalAssertionsFunc: function (wymeditor) { | ||
expect(expect() + 1); | ||
strictEqual( | ||
wymeditor._getSelectedNodes()[0].tagName.toLowerCase(), | ||
"img", | ||
"Image is the only selected node" | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One assertion tests that only one node is selected. |
||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A test for image selection, instead of the test for |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,3 +196,75 @@ test("IE unlinks when collapsed selection inside link", function () { | |
} | ||
}); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some tests for linking/unlinking images. |
||
test("Links selected unlinked image", function () { | ||
manipulationTestHelper({ | ||
startHtml: "<p>A <img alt=\"pen\" src=\"http://goo.gl/N9nqUc\" /></p>", | ||
prepareFunc: function (wymeditor) { | ||
wymeditor.$body().find("img").mousedown(); | ||
}, | ||
manipulationFunc: function (wymeditor) { | ||
wymeditor.link({href: "http://example.com/"}); | ||
}, | ||
expectedResultHtml: ["" | ||
, "<p>" | ||
, "A " | ||
, "<a href=\"http://example.com/\">" | ||
, "<img alt=\"pen\" src=\"http://goo.gl/N9nqUc\" />" | ||
, "</a>" | ||
, "</p>" | ||
].join("") | ||
}); | ||
}); | ||
|
||
test("Modifies linked image", function () { | ||
manipulationTestHelper({ | ||
startHtml: ["" | ||
, "<p>" | ||
, "<a href=\"http://example.com/\">" | ||
, "A " | ||
, "<img alt=\"pen\" src=\"http://goo.gl/N9nqUc\" />" | ||
, "</a>" | ||
, "</p>" | ||
].join(""), | ||
prepareFunc: function (wymeditor) { | ||
wymeditor.$body().find("img").mousedown(); | ||
}, | ||
manipulationFunc: function (wymeditor) { | ||
wymeditor.link({href: "http://example.com/foo"}); | ||
}, | ||
expectedResultHtml: ["" | ||
, "<p>" | ||
, "<a href=\"http://example.com/foo\">" | ||
, "A " | ||
, "<img alt=\"pen\" src=\"http://goo.gl/N9nqUc\" />" | ||
, "</a>" | ||
, "</p>" | ||
].join("") | ||
}); | ||
}); | ||
|
||
test("Unlinks linked image", function () { | ||
manipulationTestHelper({ | ||
startHtml: ["" | ||
, "<p>" | ||
, "A " | ||
, "<a href=\"http://example.com/\">" | ||
, "<img alt=\"pen\" src=\"http://goo.gl/N9nqUc\" />" | ||
, "</a>" | ||
, "</p>" | ||
].join(""), | ||
prepareFunc: function (wymeditor) { | ||
wymeditor.$body().find("img").mousedown(); | ||
}, | ||
manipulationFunc: function (wymeditor) { | ||
wymeditor.exec(WYMeditor.EXEC_COMMANDS.UNLINK); | ||
}, | ||
expectedResultHtml: ["" | ||
, "<p>" | ||
, "A " | ||
, "<img alt=\"pen\" src=\"http://goo.gl/N9nqUc\" />" | ||
, "</p>" | ||
].join("") | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -658,7 +658,7 @@ WYMeditor.editor.prototype.exec = function (cmd) { | |
|
||
case WYMeditor.EXEC_COMMANDS.CREATE_LINK: | ||
container = wym.getRootContainer(); | ||
if (container || wym._selectedImage) { | ||
if (container) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems obvious. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic of when dialogs pop up or not is in the works in #639 anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And it is pretty messed up as it is and without tests. So I don't worry about it because that is the next PR, it seems. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
wym.dialog(WYMeditor.DIALOG_LINK); | ||
} | ||
break; | ||
|
@@ -1226,12 +1226,7 @@ WYMeditor.editor.prototype.keyCanCreateBlockElement = function (keyCode) { | |
*/ | ||
WYMeditor.editor.prototype.toggleClass = function (sClass, jqexpr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modify |
||
var wym = this, | ||
$container; | ||
if (wym._selectedImage) { | ||
$container = jQuery(wym._selectedImage); | ||
} else { | ||
$container = jQuery(wym.selectedContainer()); | ||
} | ||
$container = $container.parentsOrSelf(jqexpr); | ||
$container.toggleClass(sClass); | ||
|
||
|
@@ -3955,11 +3950,15 @@ WYMeditor.editor.prototype._handlePasteEvent = function () { | |
}; | ||
|
||
WYMeditor.editor.prototype._mousedown = function (evt) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The crux of this PR. We don't store Instead, upon mousedown on an image, we set selection to encompass it, exclusively. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting idea. I hadn't thought of that. It seems to perform well in old IE? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Seems so, yes. |
||
var wym = this; | ||
// Store the selected image if we clicked an <img> tag | ||
wym._selectedImage = null; | ||
var wym = this, | ||
selection, | ||
imageRange; | ||
|
||
if (evt.target.tagName.toLowerCase() === WYMeditor.IMG) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not using |
||
wym._selectedImage = evt.target; | ||
selection = wym.selection(); | ||
imageRange = rangy.createRangyRange(); | ||
imageRange.selectNode(evt.target); | ||
selection.setSingleRange(imageRange); | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The crux of this PR. Simply selects, using Rangy, the image that was clicked upon. No more unsetting & setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a simple mechanism for selecting images. It could be more pretty, but it is simple and works in all browsers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous mechanism didn't work well enough, because selection wasn't set to the image and there was otherwise no indication that the image was indeed selected. Users don't look at the debugger and put a watch on |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,6 @@ WYMeditor.WymClassGecko.prototype._keyup = function (evt) { | |
wym.documentStructureManager.structureRules.notValidRootContainers; | ||
defaultRootContainer = | ||
wym.documentStructureManager.structureRules.defaultRootContainer; | ||
wym._selectedImage = null; | ||
container = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gecko's |
||
|
||
// If the inputted key cannont create a block element and is not a command, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,7 +163,6 @@ WYMeditor.WymClassTridentPre7.prototype.unwrap = function () { | |
|
||
WYMeditor.WymClassTridentPre7.prototype._keyup = function (evt) { | ||
var wym = this, | ||
doc = wym._doc, | ||
container, | ||
defaultRootContainer, | ||
notValidRootContainers, | ||
|
@@ -176,7 +175,6 @@ WYMeditor.WymClassTridentPre7.prototype._keyup = function (evt) { | |
wym.documentStructureManager.structureRules.notValidRootContainers; | ||
defaultRootContainer = | ||
wym.documentStructureManager.structureRules.defaultRootContainer; | ||
doc._selectedImage = null; | ||
|
||
// If the pressed key can't create a block element and is not a command, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just like in Gecko. |
||
// check to make sure the selection is properly wrapped in a container | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,7 +161,6 @@ WYMeditor.WymClassWebKit.prototype._keyup = function (evt) { | |
wym.documentStructureManager.structureRules.notValidRootContainers; | ||
defaultRootContainer = | ||
wym.documentStructureManager.structureRules.defaultRootContainer; | ||
wym._selectedImage = null; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just like in Gecko. |
||
// Fix to allow shift + return to insert a line break in older safari | ||
if (jQuery.browser.version < 534.1) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A module for testing that images are being selected on mousedown.