Skip to content

Commit

Permalink
Prevent loop when updating placeholders after saving.
Browse files Browse the repository at this point in the history
Unfortunately, the DocumentSaved API doesn't support an event just before saving.
But looking at the annoyance prevention (document changes on selecting stuff), it is a good tradeoff.
  • Loading branch information
Bram van Mensvoort committed Sep 15, 2020
1 parent 028f55f commit e77a0ab
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
Binary file modified Demo page.sketch
Binary file not shown.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Sketch plugin to update placeholders with with metadata about last save, such as
# Installation

1. Download
[latest .zip file](https://github.com/bvmensvoort/sketch-last-updated/releases/download/v0.0.1/sketch-last-updated-0.0.1.zip) or via [releases page](https://github.com/bvmensvoort/sketch-last-updated/releases).
[latest .zip file](https://github.com/bvmensvoort/sketch-last-updated/releases/download/v1.2.0/sketch-last-updated-1.2.0.zip) or via [releases page](https://github.com/bvmensvoort/sketch-last-updated/releases).
2. Unzip and double click to install it.

<br>
Expand Down Expand Up @@ -54,6 +54,8 @@ Placeholders which the plugin monitors (on January, 31st 2020 at 3:31:59 PM):
* `[lastupdated-second]` : 59
* `[lastupdated-image]` : [Blockies](https://github.com/download13/blockies) image,
* `[lastupdated-increment]` : 1
* `[lastupdated-size-bytes]` : 237234
* `[lastupdated-is-autosaved]` : 0

The placeholders are updated on elements or symbol overrides inside an artboard.
Note: `[lastupdated-image]` can only be used on shapes (like a rectangle) on the artboard, or on images within a symbol.
Expand All @@ -66,7 +68,16 @@ This plugin didn't exist for some great inspiration:
<br>

# Releases
## V1
## v1.2.0
- Marks document as changed on document change instead of selection change (finally). This will prevent unnecessary saves and might be a bit faster.
- Add [size-bytes] and [autosaved] to possible placeholders
Unfortunately, a double save is needed to save the updated placeholders in the Sketch file.

## v1.1.0
- Updates the placeholders when saving the document. This makes selecting stuff much faster.
- Now the correct month number is used in month-placeholders

## V0.0.1
- Automatically updates on select
- Supports date and time: `[lastupdated]`, `[lastupdatedUS]`, `[lastupdated-full-date]`, `[lastupdated-full-dateUS]`, `[lastupdated-time]`, `[[lastupdated-year]`, `[lastupdated-month]`, `[lastupdated-day]`, `[lastupdated-day-str]`, `[lastupdated-hour]`, `[lastupdated-minute]`, `[lastupdated-second]`
- Supports Blockies: `[lastupdated-image]`
Expand Down
4 changes: 2 additions & 2 deletions appcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<item>
<title>Version 1.2</title>
<description>
- Marks document as changed on document change instead of selection change (finally). This will prevent unnecessary saves.
- Add [size-bytes] and [autosaved] to possible variables
- Marks document as changed on document change instead of selection change (finally). This will prevent unnecessary saves and might be a bit faster.
- Add [size-bytes] and [autosaved] to possible placeholders
</description>
<enclosure url="https://github.com/bvmensvoort/sketch-last-updated/releases/download/v1.2.0/sketch-last-updated-1.2.0.zip" sparkle:version="0.1"/>
</item>
Expand Down
61 changes: 53 additions & 8 deletions last-updated.sketchplugin/Contents/Sketch/document-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
const Settings = require('sketch/settings');
const Sketch = require('sketch');
const verbose = false;
const placeholders = [
"[lastupdated]",
"[lastupdatedus]",
"[lastupdated-full-date]",
"[lastupdated-full-dateus]",
"[lastupdated-time]",
"[lastupdated-year]",
"[lastupdated-month]",
"[lastupdated-month-str]",
"[lastupdated-date]",
"[lastupdated-day]",
"[lastupdated-day-str]",
"[lastupdated-hour]",
"[lastupdated-minute]",
"[lastupdated-second]",
"[lastupdated-image]",
"[lastupdated-increment]",
"[lastupdated-size-bytes]",
"[lastupdated-is-autosaved]"
];
var onDocumentChanged = function (context) {

if (verbose) console.log("On document changed", context.actionContext);
Expand All @@ -19,18 +39,26 @@ var onDocumentChanged = function (context) {
return;

function getArtboard(objRef, fullPath, document) {
var artboardToSelect = null;
layerParentGroup = objRef;
let artboardToSelect = null;
let layerParentGroup = objRef;
let isPlaceholderChanged = false;

if (typeof objRef.parentGroup === "undefined") {
// Don't update when override is updated by the plugin itself
// Otherwise we will be in a loop
if (objRef.class() === "MSOverrideValue") {
// Work to do here
console.log("ObjectRef", objRef.layerName());
}


// Apparently for MSImmutableRectangleShape there is no 'parentGroup' function
layerParentGroup = getArtboardFallback(fullPath, document);
layerParentGroup = getObjectFromFullPath(fullPath, document);

// Skip change when it is a [lastupdated] placeholder
if (objRef.class().toString() == "MSOverrideValue") { // === doesn't work for some reason
isPlaceholderChanged = detectPlaceholderInOverride(objRef, layerParentGroup);
if (isPlaceholderChanged) {
if (verbose) console.log("This change is caused by this plugin, ignore change to prevent a loop.");
return artboardToSelect;
}
}
}

//get the parent artboard if a layer is selected by the user
Expand All @@ -45,7 +73,7 @@ var onDocumentChanged = function (context) {

// Try to get parent based on fullpath
// Solution from: https://sketchplugins.com/d/1886-ondocumentchange-fullpath/4
function getArtboardFallback(fullPath, document) {
function getObjectFromFullPath(fullPath, document) {
// Remove item after last . to get parent
let path = fullPath.split('.').slice(0, -1);

Expand All @@ -68,6 +96,23 @@ var onDocumentChanged = function (context) {

return parent.sketchObject;
}

function detectPlaceholderInOverride(overrideValueReference, object) {
let isPlaceholder = false;
let overrideId = overrideValueReference.overrideName().toString();

// Find overridePoint in order to get the name of the changed overrideValue
let matchingOverridePoint = object.overridePoints().find(function (overridePoint) {
return overridePoint.name().toString()+"" == overrideId;
});

// Check if overridePoint matches a placeholder name
if (matchingOverridePoint) {
isPlaceholder = placeholders.includes(matchingOverridePoint.layerName().toLowerCase());
}

return isPlaceholder;
}
}

// Update date in artboard meta info
Expand Down
3 changes: 2 additions & 1 deletion last-updated.sketchplugin/Contents/Sketch/document-saved.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const sketch = require('sketch');
const Settings = require('sketch/settings');
var sizeBytes;
var autoSaved;
const verbose = false;

var onDocumentSaved = function (context) {
// The action context for this action contains three keys:
Expand All @@ -23,7 +24,7 @@ var onDocumentSaved = function (context) {
});
});

console.log(changedArtboards)
if (verbose) console.log("Document saved. After that, placeholders on artboards were updated: ", changedArtboards);

// Remove mark for updated documents
Settings.setDocumentSettingForKey(document, 'last-updated-marked-artboards', {});
Expand Down
Binary file modified lastupdated-features.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e77a0ab

Please sign in to comment.