
function ResizeToFitArtboard(context) {

  var sketch = context.api()
  sketch.log("resizeToFitArtboard:Sketch version is " + sketch.version)
  sketch.log("resizeToFitArtboard:Sketch API version is " + sketch.api_version)

  var document = sketch.selectedDocument
  var page = document.currentPage
  var c_selection = context.selection // 当前选中内容

  if (context.selection.count() == 0) {
    context.document.showMessage('Please select some objects.');
    return;
  }

  var iter = c_selection.objectEnumerator()
  var c_layer = iter.nextObject()
  var selectedArtboard = getParentArtboard(c_layer)  // 当前所在画板
  log("resizing: "+selectedArtboard)
  if (selectedArtboard == null) {
    context.document.showMessage('No artboard found.');
    return;
  }

  //获取画布和选中内容的尺寸
  artboard_width = getArtboardSize(selectedArtboard).w
  artboard_height = getArtboardSize(selectedArtboard).h
  layer_width = getSelectionSize(c_selection).w
  layer_height = getSelectionSize(c_selection).h
  layer_x = getSelectionSize(c_selection).x
  layer_y = getSelectionSize(c_selection).y

  //calculate scale ratio
  var rate = 1
  var layer_ratio=layer_width/layer_height

  if (layer_ratio <= 1){
      rate = artboard_height/layer_height
  } else {
      rate = artboard_width/layer_width
  }
  //calculate offset
  offset_x = -layer_x
  offset_y = -layer_y

  //do resizing
  resizeAllLayerByScale(selectedArtboard, rate, offset_x, offset_y)
  doc.reloadInspector()
}

// resize selected layers by scale ratio
function resizeAllLayerByScale(artboard, rate, offset_x, offset_y){
  var layers = artboard.children()
  for (i=0;i<layers.length;i++){
    var layer = layers[i];
    // Do not resize artboard
    if (layer.parentGroup().className() == 'MSArtboardGroup'
    & !(layer.className() == 'MSArtboardGroup')) {
        var frame = layer.frame();
        layer.frame().setX(frame.x()+offset_x);
        layer.frame().setY(frame.y()+offset_y);
        layer.setWidthRespectingProportions(frame.width() * rate);
        layer.setHeightRespectingProportions(frame.height() * rate);
    }
  }
}

//getParentArtboard(layer)
function getParentArtboard(layer) {
  if (layer == undefined) {
		return null;
	}
  var currentLayer = layer;

  while (true) {
    var className = currentLayer.className();
    if (className == 'MSPage') {
      return null;
      break;
    } else if (className == 'MSArtboardGroup') {
      break;
    } else {
      currentLayer = currentLayer.parentGroup();
    }
  }
  return currentLayer;
}

function getArtboardSize(artboard){

    minX = artboard.frame().minX()
    maxX = artboard.frame().maxX()
    minY = artboard.frame().minY()
    maxY = artboard.frame().maxY()

  return {
    w: maxX-minX,
    h: maxY-minY,
    x: minX,
    y: minY
  };

}

// get selection edge size
function getSelectionSize(sel) {
  var minX,minY,maxX,maxY;
  minX=minY=Number.MAX_VALUE;
  maxX=maxY=-0xFFFFFFFF;

  for(var i=0;i<sel.count();i++) {
    var rect=sel.objectAtIndex(i).frame();

    minX=Math.min(minX,rect.minX());
    minY=Math.min(minY,rect.minY());
    maxX=Math.max(maxX,rect.maxX());
    maxY=Math.max(maxY,rect.maxY());
  }

  return {
    w: maxX-minX,
    h: maxY-minY,
    x: minX,
    y: minY
  };
}

@@ -1303,17 +1303,16 @@
 set_x = 
--
 layer_x%0A
@@ -1328,16 +1328,117 @@
 y = 
--
 layer_y
+%0A  log(%22resizeToFitArtboard: offset_x%22 + offset_x);%0A  log(%22resizeToFitArtboard: offset_y%22 + offset_y);
 %0A%0A  
@@ -1519,16 +1519,17 @@
 fset_y)%0A
+%0A
   doc.re
@@ -1691,16 +1691,17 @@
 ldren()%0A
+%0A
   for (i
@@ -1752,16 +1752,66 @@
 ers%5Bi%5D;%0A
+    log(%22resizeToFitArtboard: layer i %22 + layer)%0A%0A
     // D
@@ -1892,21 +1892,25 @@
 rdGroup'
+)%7B
 %0A    
+//
 & !(laye
@@ -1964,68 +1964,510 @@
 var 
-frame = layer.frame();%0A        layer.frame().setX(frame.x()+
+layer_offset_x = layer.frame().minX() - offset_x%0A        var layer_offset_y = layer.frame().minY() - offset_y%0A%0A        log(%22resizeToFitArtboard: layer parent %22 +layer.parentGroup().className())%0A        log(%22resizeToFitArtboard: layer i offset x %22 + layer_offset_x)%0A        log(%22resizeToFitArtboard: layer i offset y %22 + layer_offset_y)%0A%0A        var frame = layer.frame();%0A        layer.frame().setX(rate * layer_offset_x);%0A        log(%22resizeToFitArtboard: layer move xto %22 + rate * (frame.x() - layer_
 offs
@@ -2463,32 +2463,33 @@
  layer_offset_x)
+)
 ;%0A        layer.
@@ -2505,18 +2505,111 @@
 etY(
-frame.y()+
+rate * layer_offset_y);%0A        log(%22resizeToFitArtboard: layer move yto %22 + rate * (frame.y() - layer_
 offs
@@ -2605,34 +2605,78 @@
  layer_offset_y)
+)
 ;%0A
+        layer.constrainProportions = true;%0A
         layer.se
@@ -2726,32 +2726,34 @@
  rate);%0A        
+//
 layer.setHeightR
@@ -2793,24 +2793,146 @@
 () * rate);%0A
+        log(%22resizeToFitArtboard: resize rate:%22 + rate)%0A        log(%22resizeToFitArtboard: layer result:%22 + layer.frame())%0A
     %7D%0A  %7D%0A%7D%0A

fix bug: resizing multi selection