Skip to content

Commit

Permalink
Merge pull request #368 from basiljs/dev-pasteinto
Browse files Browse the repository at this point in the history
adding pasteInto()
  • Loading branch information
ffd8 authored Jan 16, 2025
2 parents fd61fc8 + 90b3464 commit f37eaa6
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
69 changes: 67 additions & 2 deletions basil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ var isString = pub.isString = function(str) {
};

/**
* @summary Checks wether a var is an InDesign text object.
* @summary Checks whether a var is an InDesign text object.
* @description Checks whether a var is an InDesign text object, returns `true` if this is the case.
* NB: a InDesign text frame will return `false` as it is just a container holding text. So you could say that `isText()` refers to all the things inside a text frame.
*
Expand Down Expand Up @@ -4009,6 +4009,71 @@ pub.objectStyle = function(itemOrName, props) {
return style;
};

/**
* @summary Paste one page item into another.
* @description Paste the source page item into the destination item, which then acts like a clipping mask. Optionally, set to a variable, to immediately access the new page item inside your destination.
*
* @cat Document
* @subcat Page Items
* @method pasteInto
*
* @param {PageItem} source The page item to copy.
* @param {PageItem} destination The page item to paste the source into.
* @return {Object} The new page item inside the destination.
*
* @example <caption>Use ellipse as clipping mask for textframe.</caption>
* noStroke();
* textSize(50);
*
* fill(0);
* var myText = text(LOREM, 0, 0, width, height);
*
* noFill();
* var myMask = ellipse(width / 2, height / 2, width / 2, height / 2);
*
* myMaskItem = pasteInto(myText, myMask);
*
* property(myMaskItem, 'fillColor', color(0, 255, 0));
*
* @example <caption>Use ellipse as clipping mask for rect.</caption>
* noStroke();
*
* fill(255, 0, 0);
* var myRect = rect(0, 0, width / 2, height / 2);
*
* fill(0, 0, 255);
* var myMask = ellipse(width / 2, height / 2, width / 2, height / 2);
*
* myMaskItem = pasteInto(myRect, myMask);
*
* property(myMaskItem, 'fillColor', color(255, 0, 255));
*/
pub.pasteInto = function(source, destination) {
checkNull(source);
checkNull(destination);

// temp store previous selection
var tempSelection = app.selection;

// set source to app clipboard
app.selection = source;
app.copy();

// paste source into destination
app.selection = destination;
app.pasteInto();

// return selection
app.selection = tempSelection;

// return new pageItem inside destination
if(destination.pageItems[0].hasOwnProperty('texts')){
return destination.pageItems[0].texts[0];
}else{
return destination.pageItems[0];
}
};

/**
* @summary Returns the first selected object or selects an object.
* @description If no argument is given, returns the first currently selected object. If a page item is given as argument, the page item will be selected.
Expand Down Expand Up @@ -4940,7 +5005,7 @@ pub.SCRIPTNAME = scriptName;
* @subcat Constants
* @property VERSION {String}
*/
pub.VERSION = "2.0.0-beta";
pub.VERSION = "2.0.0-beta 2021-01-04";

// ----------------------------------------
// src/includes/image.js
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ basil.js x.x.x DAY MONTH YEAR
+ Added saveCSV() to stringify data and save it as CSV file in one step
+ Added CSV.parse() and CSV.stringify() to replace CSV.decode() and CSV.encode()
parse and stringify accept optional 2nd parameter for custom delimiter.
+ Added pasteInto() enabling using page items as clipping masks

* translate(), rotate() and scale() now behave like they do in Processing and change the current matrix
* basil scripts will by default use the user's default units or the current units of the document,
Expand Down
2 changes: 1 addition & 1 deletion src/includes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ var isString = pub.isString = function(str) {
};

/**
* @summary Checks wether a var is an InDesign text object.
* @summary Checks whether a var is an InDesign text object.
* @description Checks whether a var is an InDesign text object, returns `true` if this is the case.
* NB: a InDesign text frame will return `false` as it is just a container holding text. So you could say that `isText()` refers to all the things inside a text frame.
*
Expand Down
65 changes: 65 additions & 0 deletions src/includes/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,71 @@ pub.objectStyle = function(itemOrName, props) {
return style;
};

/**
* @summary Paste one page item into another.
* @description Paste the source page item into the destination item, which then acts like a clipping mask. Optionally, set to a variable, to immediately access the new page item inside your destination.
*
* @cat Document
* @subcat Page Items
* @method pasteInto
*
* @param {PageItem} source The page item to copy.
* @param {PageItem} destination The page item to paste the source into.
* @return {Object} The new page item inside the destination.
*
* @example <caption>Use ellipse as clipping mask for textframe.</caption>
* noStroke();
* textSize(50);
*
* fill(0);
* var myText = text(LOREM, 0, 0, width, height);
*
* noFill();
* var myMask = ellipse(width / 2, height / 2, width / 2, height / 2);
*
* myMaskItem = pasteInto(myText, myMask);
*
* property(myMaskItem, 'fillColor', color(0, 255, 0));
*
* @example <caption>Use ellipse as clipping mask for rect.</caption>
* noStroke();
*
* fill(255, 0, 0);
* var myRect = rect(0, 0, width / 2, height / 2);
*
* fill(0, 0, 255);
* var myMask = ellipse(width / 2, height / 2, width / 2, height / 2);
*
* myMaskItem = pasteInto(myRect, myMask);
*
* property(myMaskItem, 'fillColor', color(255, 0, 255));
*/
pub.pasteInto = function(source, destination) {
checkNull(source);
checkNull(destination);

// temp store previous selection
var tempSelection = app.selection;

// set source to app clipboard
app.selection = source;
app.copy();

// paste source into destination
app.selection = destination;
app.pasteInto();

// return selection
app.selection = tempSelection;

// return new pageItem inside destination
if(destination.pageItems[0].hasOwnProperty('texts')){
return destination.pageItems[0].texts[0];
}else{
return destination.pageItems[0];
}
};

/**
* @summary Returns the first selected object or selects an object.
* @description If no argument is given, returns the first currently selected object. If a page item is given as argument, the page item will be selected.
Expand Down
2 changes: 1 addition & 1 deletion src/includes/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,4 @@ pub.SCRIPTNAME = scriptName;
* @subcat Constants
* @property VERSION {String}
*/
pub.VERSION = "2.0.0-beta";
pub.VERSION = "2.0.0-beta 2021-01-04";

0 comments on commit f37eaa6

Please sign in to comment.