Skip to content

Commit 42614e8

Browse files
committed
Add Figma to Tailwind bookmarklet functionality & make it work in the browser
Adds JIT support for the translation functionality Also adds support for passing in a config file
1 parent 3495c0a commit 42614e8

15 files changed

+2841
-222
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["@babel/plugin-transform-modules-commonjs"]
3+
}

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.svelte
2-
node_modules
2+
node_modules
3+
dist

bin/figma-tailwind/build.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const util = require('util');
4+
const translate = require('../../dist/lib/translate.js');
5+
6+
const byProperties = translate.loadConfig(path.resolve('./tailwind.config.js'));
7+
8+
const bookmarklet = `${fs.readFileSync('./bin/figma-tailwind/runtime.js', 'utf-8')}
9+
let byProperties = ${util.inspect(byProperties)};`;
10+
11+
fs.writeFileSync('./dist/bookmarklet.js', bookmarklet, {
12+
flag: 'w+',
13+
});

bin/figma-tailwind/runtime.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { propertiesToClass } from './lib/translate-slim.js';
2+
3+
function translate(event) {
4+
if (event.button !== 0) {
5+
return;
6+
}
7+
8+
const paneEl = document.querySelector('[class*="inspect_panels--tabularInspectionPanel"] > div > div > div > div');
9+
paneEl.style.display = 'flex';
10+
paneEl.style.flexDirection = 'column';
11+
12+
// Remove previously translated elements if any.
13+
paneEl.querySelectorAll('span.translated').forEach((el) => el.remove());
14+
const lastPaneChild = paneEl.children[paneEl.children.length - 1];
15+
if (lastPaneChild.tagName === 'DIV' && lastPaneChild.classList.length === 0) {
16+
lastPaneChild.style.display = 'none';
17+
}
18+
19+
const findPropertyGroup = (i) => {
20+
let lineEls = [];
21+
const separatorEl = Array.from(paneEl.querySelectorAll('div')).filter((el) => el.classList.length === 0)?.[i];
22+
if (!separatorEl) {
23+
return {
24+
lastEl: null,
25+
classnames: null,
26+
};
27+
}
28+
29+
let iteratorEl = separatorEl.nextElementSibling;
30+
while (true) {
31+
if (i === 0 && iteratorEl?.classList.length === 0) {
32+
break;
33+
}
34+
35+
if (iteratorEl.tagName === 'DIV' && iteratorEl?.classList.length > 0) {
36+
lineEls.push(iteratorEl);
37+
}
38+
39+
if (i === 1 && !iteratorEl.nextElementSibling) {
40+
break;
41+
}
42+
iteratorEl = iteratorEl.nextElementSibling;
43+
}
44+
45+
const properties = Array.from(lineEls).map((el) => el.textContent.slice(0, -1));
46+
const classnames = properties
47+
.map((prop) => propertiesToClass(prop, byProperties)?.replace('\\', ''))
48+
.filter(Boolean)
49+
.join(' ');
50+
51+
return {
52+
lastEl: iteratorEl,
53+
classnames,
54+
};
55+
};
56+
57+
// Attach translated elements to the inspector pane
58+
const attach = (el, classnames, position) => {
59+
if (!el || !classnames) {
60+
return;
61+
}
62+
const translatedEl = document.createElement('SPAN');
63+
translatedEl.textContent = classnames;
64+
translatedEl.classList.add('translated');
65+
translatedEl.style.border = '1px solid #06B6D4';
66+
translatedEl.style.borderRadius = '6px';
67+
translatedEl.style.padding = '2px 8px';
68+
translatedEl.style.marginTop = '8px';
69+
translatedEl.style.display = 'block';
70+
translatedEl.style.alignSelf = 'self-start';
71+
el.insertAdjacentElement(position, translatedEl);
72+
};
73+
74+
const { lastEl: positionEl, classnames: positionClassnames } = findPropertyGroup(0);
75+
const { lastEl: restEl, classnames: restClassnames } = findPropertyGroup(1);
76+
77+
attach(positionEl, positionClassnames, 'beforebegin');
78+
attach(restEl, restClassnames, 'afterend');
79+
}
80+
81+
function translateDelayed(event) {
82+
window.setTimeout(() => {
83+
translate(event);
84+
}, 50);
85+
}
86+
87+
const canvas = document.querySelector('canvas');
88+
canvas.removeEventListener('mouseup', translateDelayed);
89+
canvas.addEventListener('mouseup', translateDelayed);

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"main": "src/index.js",
66
"scripts": {
7+
"build-bookmarklet": "babel src --out-dir ./dist/lib --copy-files && node bin/figma-tailwind/build.js && rollup -c",
78
"test": "uvu -r esm test"
89
},
910
"repository": {
@@ -17,7 +18,15 @@
1718
},
1819
"homepage": "https://github.com/metafy-gg/tailwind-utils#readme",
1920
"devDependencies": {
21+
"@babel/cli": "^7.14.5",
22+
"@babel/core": "^7.14.6",
23+
"@babel/preset-env": "^7.14.5",
24+
"@rollup/plugin-commonjs": "^19.0.0",
2025
"esm": "^3.2.25",
26+
"rollup": "^2.51.2",
27+
"rollup-plugin-analyzer": "^4.0.0",
28+
"rollup-plugin-terser": "^7.0.2",
29+
"terser": "^5.7.0",
2130
"uvu": "^0.5.1"
2231
},
2332
"dependencies": {

0 commit comments

Comments
 (0)