Skip to content

Commit

Permalink
Fixing ISSUE #10 in which color commands wouldn't work on grouped shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosMendonca committed Nov 1, 2020
1 parent 724c586 commit 657d843
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 64 deletions.
122 changes: 61 additions & 61 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,67 +177,67 @@ function menuAlignToHorizontalCenter() { alignSelectOrAllElements(POSITION_X.CEN

function menuInspectShapeColor() { inspectShapeColor(); }

function menuSetTransparency0() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0 ); }); }
function menuSetTransparency10() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.1 ); }); }
function menuSetTransparency25() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.25); }); }
function menuSetTransparency33() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.33); }); }
function menuSetTransparency50() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.5 ); }); }
function menuSetTransparency66() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.66); }); }
function menuSetTransparency75() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.75); }); }
function menuSetTransparency90() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 0.9 ); }); }
function menuSetTransparency100() { withSelectedOrAllShapes(function(s) { setAlphaToShape(s, 1 ); }); }

function menuSetColorSwap() { withSelectedOrAllShapes(function(s) { swapColorsOnShape(s); }); }
function menuSetColorInverse() { withSelectedOrAllShapes(function(s) { invertColorsOnShape(s); }); }
function menuSetColorMaxContrast() { withSelectedOrAllShapes(function(s) { setMaxContrastToTextOnShape(s); }); }

function menuSetColorSaturationMinus33() { withSelectedOrAllShapes(function(s) { changeHSL(s, -0.33, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationMinus10() { withSelectedOrAllShapes(function(s) { changeHSL(s, -0.1, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationMinus5() { withSelectedOrAllShapes(function(s) { changeHSL(s, -0.05, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationPlus5() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.05, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationPlus10() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.1, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationPlus33() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.33, false, HSL_COMPONENT.SATURATION); }); }

function menuSetColorSaturationAbs10() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.10, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs25() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.25, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs33() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.33, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs50() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.5, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs66() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.66, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs75() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.75, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs90() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.9, true, HSL_COMPONENT.SATURATION); }); }

function menuSetColorLuminosityMinus33() { withSelectedOrAllShapes(function(s) { changeHSL(s, -0.33, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityMinus10() { withSelectedOrAllShapes(function(s) { changeHSL(s, -0.1, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityMinus5() { withSelectedOrAllShapes(function(s) { changeHSL(s, -0.05, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityPlus5() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.05, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityPlus10() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.1, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityPlus33() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.33, false, HSL_COMPONENT.LUMINOSITY); }); }

function menuSetColorLuminosityAbs10() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.10, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs25() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.25, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs33() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.33, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs50() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.5, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs66() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.66, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs75() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.75, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs90() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0.9, true, HSL_COMPONENT.LUMINOSITY); }); }

function menuSetColorHueMinus30() { withSelectedOrAllShapes(function(s) { changeHSL(s, -30/360, false, HSL_COMPONENT.HUE); }); }
function menuSetColorHueMinus15() { withSelectedOrAllShapes(function(s) { changeHSL(s, -15/360, false, HSL_COMPONENT.HUE); }); }
function menuSetColorHuePlus15() { withSelectedOrAllShapes(function(s) { changeHSL(s, 15/360, false, HSL_COMPONENT.HUE); }); }
function menuSetColorHuePlus30() { withSelectedOrAllShapes(function(s) { changeHSL(s, 30/360, false, HSL_COMPONENT.HUE); }); }

function menuSetColorHue0() { withSelectedOrAllShapes(function(s) { changeHSL(s, 0/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue30() { withSelectedOrAllShapes(function(s) { changeHSL(s, 30/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue60() { withSelectedOrAllShapes(function(s) { changeHSL(s, 60/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue90() { withSelectedOrAllShapes(function(s) { changeHSL(s, 90/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue120() { withSelectedOrAllShapes(function(s) { changeHSL(s, 120/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue150() { withSelectedOrAllShapes(function(s) { changeHSL(s, 150/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue180() { withSelectedOrAllShapes(function(s) { changeHSL(s, 180/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue210() { withSelectedOrAllShapes(function(s) { changeHSL(s, 210/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue240() { withSelectedOrAllShapes(function(s) { changeHSL(s, 240/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue270() { withSelectedOrAllShapes(function(s) { changeHSL(s, 270/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue300() { withSelectedOrAllShapes(function(s) { changeHSL(s, 300/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue330() { withSelectedOrAllShapes(function(s) { changeHSL(s, 330/360, true, HSL_COMPONENT.HUE); }); }
function menuSetTransparency0() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0 ); }); }
function menuSetTransparency10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.1 ); }); }
function menuSetTransparency25() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.25); }); }
function menuSetTransparency33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.33); }); }
function menuSetTransparency50() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.5 ); }); }
function menuSetTransparency66() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.66); }); }
function menuSetTransparency75() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.75); }); }
function menuSetTransparency90() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 0.9 ); }); }
function menuSetTransparency100() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setAlphaToShape(s, 1 ); }); }

function menuSetColorSwap() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { swapColorsOnShape(s); }); }
function menuSetColorInverse() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { invertColorsOnShape(s); }); }
function menuSetColorMaxContrast() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { setMaxContrastToTextOnShape(s); }); }

function menuSetColorSaturationMinus33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -0.33, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationMinus10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -0.1, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationMinus5() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -0.05, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationPlus5() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.05, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationPlus10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.1, false, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationPlus33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.33, false, HSL_COMPONENT.SATURATION); }); }

function menuSetColorSaturationAbs10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.10, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs25() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.25, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.33, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs50() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.5, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs66() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.66, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs75() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.75, true, HSL_COMPONENT.SATURATION); }); }
function menuSetColorSaturationAbs90() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.9, true, HSL_COMPONENT.SATURATION); }); }

function menuSetColorLuminosityMinus33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -0.33, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityMinus10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -0.1, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityMinus5() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -0.05, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityPlus5() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.05, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityPlus10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.1, false, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityPlus33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.33, false, HSL_COMPONENT.LUMINOSITY); }); }

function menuSetColorLuminosityAbs10() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.10, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs25() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.25, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs33() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.33, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs50() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.5, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs66() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.66, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs75() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.75, true, HSL_COMPONENT.LUMINOSITY); }); }
function menuSetColorLuminosityAbs90() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0.9, true, HSL_COMPONENT.LUMINOSITY); }); }

function menuSetColorHueMinus30() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -30/360, false, HSL_COMPONENT.HUE); }); }
function menuSetColorHueMinus15() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, -15/360, false, HSL_COMPONENT.HUE); }); }
function menuSetColorHuePlus15() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 15/360, false, HSL_COMPONENT.HUE); }); }
function menuSetColorHuePlus30() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 30/360, false, HSL_COMPONENT.HUE); }); }

function menuSetColorHue0() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 0/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue30() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 30/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue60() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 60/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue90() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 90/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue120() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 120/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue150() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 150/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue180() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 180/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue210() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 210/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue240() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 240/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue270() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 270/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue300() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 300/360, true, HSL_COMPONENT.HUE); }); }
function menuSetColorHue330() { withSelectedOrAllShapesOrGroupsRecursively(function(s) { changeHSL(s, 330/360, true, HSL_COMPONENT.HUE); }); }

function menuCenterOnPage() { withSelectedOrAllElements(function(e) { alignShape(fakePageAsReferenceElement(), e, POSITION_X.CENTER, POSITION_Y.CENTER, false); }); }

Expand Down
41 changes: 39 additions & 2 deletions src/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ function withSelectedOrAllShapes(doStuff) {
.filter (function(e) { return e.getPageElementType() == SlidesApp.PageElementType.SHAPE; })
.forEach(function(e) { doStuff(e.asShape()); });
}

function withSelectedOrAllShapesOrGroupsRecursively(doStuff) {
var selectedOrAllElementsOnPage = getSelectedElementsOnPage(true);

withShapesOrGroupsRecursively(selectedOrAllElementsOnPage, doStuff);
}

function withShapesOrGroupsRecursively(pageElementArray, doStuff) {
var groupsArray = pageElementArray
.filter (function(e) { return e.getPageElementType() == SlidesApp.PageElementType.GROUP; });

var elementsArray = pageElementArray
.filter (function(e) { return e.getPageElementType() == SlidesApp.PageElementType.SHAPE; });

// This cannot be achieved programmatically (hence, cannot be tested), but in
// the UI is possible to select multiple elemments inside the group. In
// that case, the selection array contains the individual elements *and*
// the parent group, so to only apply the change (doStuff) to the selected
// elements intead of the entire group, we need to skip the group
// processing. The condition below describes only one group selected and
// the any of the elments belong to it.
var shouldSkipGroup = groupsArray.length == 1
&& elementsArray.length > 0
&& elementsArray[0].getParentGroup() != null
&& elementsArray[0].getParentGroup().getObjectId() == groupsArray[0].getObjectId();

// Handle shapes
elementsArray
.forEach(function(e) { doStuff(e.asShape()); });

if (shouldSkipGroup) return;

// Handle groups, recursively
groupsArray
.forEach(function(e) { withShapesOrGroupsRecursively(e.asGroup().getChildren(), doStuff) });
}

function withSelectedElements(doStuff) {
getSelectedElementsOnPage(false)
.forEach(function (e) { doStuff(e); });
Expand All @@ -28,8 +65,8 @@ function withSelectedOrAllElements(doStuff) {
.forEach(function (e) { doStuff(e); });
}

// Get all selected elements on page as an array. If no elements are selected, get all elements on page.
// If text is selected, get the parent element.
// Get all selected elements on page as an array. If no elements are selected,
// get all elements on page. If text is selected, get the parent element.
function getSelectedElementsOnPage(shouldFallbackToGetAllElementsOnPage) {
var selection = SlidesApp.getActivePresentation().getSelection();

Expand Down
Loading

0 comments on commit 657d843

Please sign in to comment.