Skip to content

Commit 0cea449

Browse files
committed
functionalities improvements & bug fixes
1 parent a090dca commit 0cea449

20 files changed

+430
-33217
lines changed

brandfetch.sketchplugin/Contents/Sketch/__open.js

+266-32,886
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

brandfetch.sketchplugin/Contents/Sketch/__open.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

brandfetch.sketchplugin/Contents/Sketch/manifest.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
"commands": [
1111
{
1212
"name": "Open Brandfetch",
13-
"identifier": "open-brandfetch",
13+
"identifier": "brandfetch",
1414
"script": "__open.js",
1515
"shortcut": "alt cmd b",
16-
"handler": "openBrandfetch"
16+
"handler": "open"
1717
}
1818
],
1919
"menu": {
2020
"title": "Brandfetch",
2121
"items": [
22-
"open-brandfetch"
22+
"brandfetch"
2323
]
2424
},
2525
"identifier": "Brandfetch",

my-plugin/.gitignore

-16
This file was deleted.

my-plugin/README.md

-92
This file was deleted.

my-plugin/assets/icon.png

-777 Bytes
Binary file not shown.

my-plugin/package.json

-27
This file was deleted.

my-plugin/sketch-assets/icons.sketch

-292 KB
Binary file not shown.

my-plugin/src/manifest.json

-15
This file was deleted.

my-plugin/src/my-command.js

-6
This file was deleted.

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
"@skpm/builder": "^0.9.5"
2323
},
2424
"dependencies": {
25-
"axios": "^1.6.8",
26-
"core-js": "^3.37.0",
27-
"regenerator-runtime": "^0.14.1",
2825
"sketch-module-web-view": "^3.5.1"
2926
},
3027
"description": "Brandfetch is the home for the world's brands. Pull the latest logos, colors, fonts and more directly into Sketch.",

src/handleColor.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const dom = require("sketch/dom");
2-
const sketch = require("sketch");
1+
import dom from "sketch/dom";
2+
import sketch from "sketch";
33

4-
const handleColor = ({ hexColor }) => {
4+
export const handleColor = ({ hexColor }) => {
55
// Get Document, from document get page & layers.
66
const document = sketch.getSelectedDocument();
77
const selection = document.selectedLayers;
@@ -25,5 +25,3 @@ const handleColor = ({ hexColor }) => {
2525
sketch.UI.message(`Copied ${hexColor} to clipboard.`);
2626
}
2727
};
28-
29-
module.exports = { handleColor };

src/handleFile/index.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import sketch from "sketch";
2+
3+
import { handleSVG } from "./utils.js";
4+
5+
const allowedFormats = ["jpg", "jpeg", "png", "gif", "svg", "webp"];
6+
7+
export const handleFile = async ({ name, formats, currentFormat, type }) => {
8+
console.log(name, formats, currentFormat, type);
9+
10+
// Check if the file is an image.
11+
if (!allowedFormats.includes(currentFormat)) {
12+
sketch.UI.message(
13+
`Could not load ${currentFormat} file. Please make sure the format is supported.`
14+
);
15+
return;
16+
}
17+
18+
const file = formats.find((file) => file.format === currentFormat);
19+
20+
// Get Document, from document get page & layers.
21+
const document = sketch.getSelectedDocument();
22+
const selection = document.selectedLayers;
23+
24+
if (file.format === "svg") {
25+
fetch(file.src)
26+
.then((response) => response.text())
27+
.then((content) => handleSVG({ name, content }))
28+
.catch((err) => {
29+
console.log(err);
30+
sketch.UI.message(`Something went wrong with this file.`);
31+
});
32+
33+
if (!selection.isEmpty) {
34+
sketch.UI.message(`SVG files cannot be inserted into shapes.`);
35+
}
36+
}
37+
38+
if (file.format !== "svg") {
39+
if (!selection.isEmpty) {
40+
selection.forEach((layer) => {
41+
layer.style.fills = [
42+
{
43+
fillType: "Pattern",
44+
pattern: {
45+
image: NSURL.URLWithString(encodeURI(file.src)),
46+
patternType: type,
47+
},
48+
},
49+
];
50+
51+
layer.style.borders = [{ enabled: false }];
52+
});
53+
} else {
54+
const canvasView = context.document.contentDrawView();
55+
const center = canvasView.viewCenterInAbsoluteCoordinatesForViewPort(
56+
canvasView.viewPort()
57+
);
58+
59+
const width = 350;
60+
const height = (file.height / file.width) * width;
61+
62+
const x = center.x - width / 2;
63+
const y = center.y - height / 2;
64+
65+
new sketch.Image({
66+
name: name,
67+
image: NSURL.URLWithString(encodeURI(file.src)),
68+
frame: { x, y, width, height },
69+
parent: sketch.getSelectedDocument().selectedPage,
70+
});
71+
}
72+
}
73+
};

src/handleFile/utils.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import dom from "sketch/dom";
2+
3+
const parentOffsetInArtboard = (layer) => {
4+
var offset = { x: 0, y: 0 };
5+
var parent = layer.parent;
6+
while (parent.name && parent.type !== "Artboard") {
7+
offset.x += parent.frame.x;
8+
offset.y += parent.frame.y;
9+
parent = parent.parent;
10+
}
11+
return offset;
12+
};
13+
14+
const positionInArtboard = (layer, x, y, w, h) => {
15+
var parentOffset = parentOffsetInArtboard(layer);
16+
var newFrame = new dom.Rectangle(layer.frame);
17+
newFrame.x = x - parentOffset.x;
18+
newFrame.y = y - parentOffset.y;
19+
newFrame.width = w;
20+
newFrame.height = h;
21+
layer.frame = newFrame;
22+
};
23+
24+
export const handleSVG = ({ name, content }) => {
25+
const svgString = NSString.stringWithString(content);
26+
const svgData = svgString.dataUsingEncoding(NSUTF8StringEncoding);
27+
28+
const svgImporter = MSSVGImporter.svgImporter();
29+
svgImporter.prepareToImportFromData(svgData);
30+
31+
const svgLayer = svgImporter.importAsLayer();
32+
svgLayer.setName(name);
33+
34+
context.document.currentPage().addLayers([svgLayer]);
35+
const layer = dom.getSelectedDocument().getLayersNamed(name).pop();
36+
const canvasView = context.document.contentDrawView();
37+
const center = canvasView.viewCenterInAbsoluteCoordinatesForViewPort(
38+
canvasView.viewPort()
39+
);
40+
41+
const width = 220;
42+
const height = (layer.frame.height / layer.frame.width) * width;
43+
44+
const x = center.x - width / 2;
45+
const y = center.y - height / 2;
46+
47+
positionInArtboard(layer, x, y, width, height);
48+
};

src/handleFont.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const sketch = require("sketch");
1+
import sketch from "sketch";
22

3-
const handleFont = ({ fontFamily }) => {
3+
export const handleFont = ({ fontFamily }) => {
44
// Get Document, from document get page & layers.
55
const document = sketch.getSelectedDocument();
66
const selection = document.selectedLayers;
@@ -29,5 +29,3 @@ const handleFont = ({ fontFamily }) => {
2929
sketch.UI.message(`Copied ${fontFamily} to clipboard.`);
3030
}
3131
};
32-
33-
module.exports = { handleFont };

0 commit comments

Comments
 (0)