-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82dc731
commit d8e92a4
Showing
11 changed files
with
1,426 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dist | ||
node_modules | ||
public | ||
extension | ||
extension | ||
plugin/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { toTailwindcss } from 'transform-to-tailwindcss-core' | ||
import { toUnocssClass } from 'transform-to-unocss-core' | ||
|
||
export const transformToAtomic = ( | ||
style: Record<string, string>, | ||
options: { engine: 'unocss' | 'tailwind'; isRem: boolean; prefix: string }, | ||
) => { | ||
const { engine = 'unocss', isRem = false, prefix = '' } = options | ||
const raw = Object.entries(style) | ||
|
||
const cssCode = raw.map(([key, value]) => `${key}: ${value.replace(/\/\*.*\*\//g, '').trim()};`).join('\n') | ||
|
||
const uno = raw | ||
.map( | ||
([key, value]) => | ||
`${key}: ${value | ||
.replace(/\/\*.*\*\//g, '') | ||
.replace(/var\(--[\w-]*,\s*(.*)\)/g, (_, $1) => $1) | ||
.trim()}`, | ||
) | ||
.map((i) => (engine === 'unocss' ? toUnocssClass(i, isRem)[0] : toTailwindcss(i, isRem))) | ||
.map((i) => `${prefix}${i}`) | ||
.join(' ') | ||
.replace(/border-(\d+\.\d+|\d+)/g, (_, $1) => `border-${Number($1) * 4}`) | ||
.replace(/(border-[xylrtb]-)(\d+\.\d+|\d+)/g, (_, $1, $2) => `${$1}${Number($2) * 4}`) | ||
.replace(/(p[xylrtb])-(\d+\.\d+|\d+)px/g, (_, $1, $2) => `${$1}-${$2 / 4}`) | ||
|
||
const unoMini = raw | ||
.filter(([key]) => | ||
['font-feature-settings', 'font-family', 'text-transform'].every((item) => !key?.startsWith(item)), | ||
) | ||
.map( | ||
([key, value]) => | ||
`${key}: ${value | ||
.replace(/\/\*.*\*\//g, '') | ||
.replace(/var\(--[\w-]*,\s*(.*)\)/g, (_, $1) => $1) | ||
.trim()}`, | ||
) | ||
.map((i) => (engine === 'unocss' ? toUnocssClass(i, isRem)[0] : toTailwindcss(i, isRem))) | ||
.filter((i) => ['lh-normal', 'font-not-italic', 'bg-[url(]'].every((item) => !i?.startsWith(item))) | ||
.map((i) => `${prefix}${i}`) | ||
.join(' ') | ||
.replace(/border-(\d+\.\d+|\d+)/g, (_, $1) => `border-${Number($1) * 4}`) | ||
.replace(/(border-[xylrtb]-)(\d+\.\d+|\d+)/g, (_, $1, $2) => `${$1}${Number($2) * 4}`) | ||
.replace(/(p[xylrtb])-(\d+\.\d+|\d+)px/g, (_, $1, $2) => `${$1}-${$2 / 4}`) | ||
|
||
return { | ||
cssCode, | ||
uno, | ||
unoMini, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default defineUnlistedScript(async () => { | ||
const markers = ['delete window.figma', '.createAPI()'] | ||
|
||
const current = document.currentScript as HTMLScriptElement | ||
const src = current.src | ||
|
||
function replaceScript(src: string) { | ||
const script = document.createElement('script') | ||
script.src = src | ||
script.defer = true | ||
current.replaceWith(script) | ||
} | ||
|
||
function matchFile(content: string) { | ||
return markers.every((marker) => content.includes(marker)) | ||
} | ||
|
||
try { | ||
let content = await (await fetch(src)).text() | ||
|
||
if (matchFile(content)) { | ||
content = content.replace(/if\(!([a-zA-Z\d]+)\.userID\|\|/, 'if(true){}else if(!$1.userID||') | ||
} | ||
|
||
// document.currentScript will be `null` if we run with `new Function()` | ||
content = content.replaceAll('document.currentScript.src', `"${src}"`) | ||
|
||
// delete window.figma may throw Error in strict mode | ||
content = content.replaceAll('delete window.figma', 'window.figma = undefined') | ||
|
||
new Function(content)() | ||
} catch (_) { | ||
replaceScript(`${src}?fallback`) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { definePlugin } from '@tempad-dev/plugins' | ||
|
||
import { transformToAtomic } from '../core/index' | ||
|
||
export default definePlugin({ | ||
name: 'core', | ||
code: { | ||
css: { | ||
title: 'unocss', | ||
lang: 'rust' as 'css', | ||
transform({ style }) { | ||
return transformToAtomic(style, { engine: 'unocss', isRem: true, prefix: '' }).uno | ||
}, | ||
}, | ||
js: false, | ||
}, | ||
}) |
Oops, something went wrong.