var find = require('lodash-node/modern/collections/find');
var util = require('chessground').util;
var drag = require('chessground').drag;

module.exports = function(ctrl, e) {
  if (e.button !== 0) return; // only left click
  var role = e.target.getAttribute('data-role'),
  color = e.target.getAttribute('data-color');
  if (!role || !color) return;
  e.stopPropagation();
  e.preventDefault();
  var key = find(util.allKeys, function(k) {
    return !ctrl.chessground.data.pieces[k];
  });
  if (!key) return;
  var coords = util.key2pos(ctrl.chessground.data.orientation === 'white' ? key : util.invertKey(key));
  var obj = {};
  obj[key] = {
    role: role,
    color: color
  };
  ctrl.chessground.setPieces(obj);
  var bounds = ctrl.chessground.data.bounds();
  var squareBounds = e.target.parentNode.getBoundingClientRect();
  var rel = [
    (coords[0] - 1) * squareBounds.width + bounds.left,
    (8 - coords[1]) * squareBounds.height + bounds.top
  ];
  ctrl.chessground.data.draggable.current = {
    orig: key,
    rel: rel,
    epos: [e.clientX, e.clientY],
    pos: [e.clientX - rel[0], e.clientY - rel[1]],
    dec: [-squareBounds.width / 2, -squareBounds.height / 2],
    bounds: bounds,
    started: true
  };
  drag.processDrag(ctrl.chessground.data);
}

@@ -621,28 +621,13 @@
 var 
-obj = %7B%7D;%0A  obj%5Bkey%5D
+piece
  = %7B
@@ -665,16 +665,52 @@
 or%0A  %7D;%0A
+  var obj = %7B%7D;%0A  obj%5Bkey%5D = piece;%0A
   ctrl.c
@@ -1040,16 +1040,59 @@
 g: key,%0A
+    piece: piece.color + ' ' + piece.role,%0A
     rel:

fix board editor drag piece into board