Skip to content

Commit

Permalink
add workaround for Chrome 40+ selectionChanged regression
Browse files Browse the repository at this point in the history
  • Loading branch information
oslego committed Jan 13, 2015
1 parent 5b59339 commit 55f914f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
54 changes: 46 additions & 8 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,20 @@
Extension.prototype.onSelectedElementChange = function(){
var self = this;

if (this.activeEditor){
this.removeEditor(this.activeEditor);
}

self.getSelectedElementStyles().then(function(data){
self.controller.setModel(new app.Model(data));
self.controller.setView();
});
this.ensureDifferentElementSelected()
.then(function(){
if (self.activeEditor){
self.removeEditor(self.activeEditor);
}

self.getSelectedElementStyles().then(function(data){
self.controller.setModel(new app.Model(data));
self.controller.setView();
});
})
.catch(function(err){
console.info("reselecting prev element; stop")
})
};

Extension.prototype.onEditorStateChange = function(editor){
Expand All @@ -101,6 +107,38 @@
}
};

/*
Workaround for Chrome 40+ regression in DevTools API where `onSelectionChanged` triggers twice.
@see https://code.google.com/p/chromium/issues/detail?id=438267
Check if the selection change happens consecutively on the same element.
Return a promise which resolves if the selection change occurs on different elements as expected,
and rejects if the selection change occurs consecutively on the same element which is caused by the regression.
*/
Extension.prototype.ensureDifferentElementSelected = function(){
return new Promise(function(resolve, reject){

function check(role){
var marker = 'editor-target';

// data-role matches, same element is selected
if (role == marker){
reject();
}

// data-role is undefined, element is not the previously selected one
// mark currently selected element ($0), unmark previously selected element ($1)
if (role == undefined){
chrome.devtools.inspectedWindow.eval('$0.dataset.role="'+ marker.toString() +'"; delete $1.dataset.role', function(){
resolve();
});
}
}

chrome.devtools.inspectedWindow.eval('$0.dataset.role', check);
})
}

Extension.prototype.setupEditor = function(editor){
chrome.devtools.inspectedWindow.eval('setup($0, "'+ editor.property.toString() +'", "'+ editor.value.toString() +'")', { useContentScriptContext: true });
this.activeEditor = editor;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-css-shapes-editor",
"version": "1.2.0",
"version": "1.2.1",
"description": "Interactive editor for CSS Shapes",
"main": "background.js",
"scripts": {
Expand Down

0 comments on commit 55f914f

Please sign in to comment.