forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code writing modules (onlook-dev#81)
* Refactor code writing modules
- Loading branch information
Showing
25 changed files
with
448 additions
and
342 deletions.
There are no files selected for viewing
Binary file not shown.
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 was deleted.
Oops, something went wrong.
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,25 @@ | ||
// DOM | ||
export interface DomElement { | ||
selector: string; | ||
rect: DOMRect; | ||
styles: CSSStyleDeclaration; | ||
encodedTemplateNode?: string; | ||
parent?: ParentDomElement; | ||
} | ||
|
||
export interface ParentDomElement { | ||
selector: string; | ||
rect: DOMRect; | ||
encodedTemplateNode?: string; | ||
} | ||
|
||
// Engine | ||
export interface ElementMetadata { | ||
selector: string; | ||
rect: DOMRect; | ||
parentRect: DOMRect; | ||
computedStyle: CSSStyleDeclaration; | ||
webviewId: string; | ||
dataOnlookId?: string; | ||
tagName: string; | ||
} |
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 @@ | ||
export interface TemplateNode { | ||
path: string; | ||
startTag: TemplateTag; | ||
endTag: TemplateTag; | ||
commit: string; | ||
name?: string; | ||
} | ||
|
||
export interface TemplateTag { | ||
start: TemplateTagPosition; | ||
end: TemplateTagPosition; | ||
} | ||
|
||
export interface TemplateTagPosition { | ||
line: number; | ||
column: number; | ||
} |
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,14 @@ | ||
import { TemplateNode } from './element/templateNode'; | ||
|
||
export interface StyleCodeDiff { | ||
original: string; | ||
generated: string; | ||
param: StyleChangeParam; | ||
} | ||
|
||
export interface StyleChangeParam { | ||
selector: string; | ||
templateNode: TemplateNode; | ||
tailwind: string; | ||
codeBlock: string; | ||
} |
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,4 @@ | ||
export interface TunnelResult { | ||
url: string; | ||
password: string; | ||
} |
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,80 @@ | ||
import generate from '@babel/generator'; | ||
import { parse } from '@babel/parser'; | ||
import traverse from '@babel/traverse'; | ||
import t from '@babel/types'; | ||
import { twMerge } from 'tailwind-merge'; | ||
import { StyleChangeParam, StyleCodeDiff } from '/common/models'; | ||
|
||
export function getStyleCodeDiffs(styleParams: StyleChangeParam[]): StyleCodeDiff[] { | ||
const diffs: StyleCodeDiff[] = []; | ||
const generateOptions = { retainLines: true, compact: false }; | ||
|
||
for (const styleParam of styleParams) { | ||
const codeBlock = styleParam.codeBlock; | ||
const ast = parseJsx(codeBlock); | ||
const original = removeSemiColonIfApplicable( | ||
generate(ast, generateOptions, codeBlock).code, | ||
codeBlock, | ||
); | ||
|
||
addClassToAst(ast, styleParam.tailwind); | ||
|
||
const generated = removeSemiColonIfApplicable( | ||
generate(ast, generateOptions, codeBlock).code, | ||
codeBlock, | ||
); | ||
diffs.push({ original, generated, param: styleParam }); | ||
} | ||
|
||
return diffs; | ||
} | ||
|
||
function removeSemiColonIfApplicable(code: string, original: string) { | ||
if (!original.endsWith(';') && code.endsWith(';')) { | ||
return code.slice(0, -1); | ||
} | ||
return code; | ||
} | ||
|
||
function parseJsx(code: string) { | ||
return parse(code, { | ||
plugins: ['typescript', 'jsx'], | ||
}); | ||
} | ||
|
||
function addClassToAst(ast: t.File, className: string) { | ||
let processed = false; | ||
traverse(ast, { | ||
JSXOpeningElement(path) { | ||
if (processed) { | ||
return; | ||
} | ||
let classNameAttr = null; | ||
path.node.attributes.forEach((attribute) => { | ||
if (t.isJSXAttribute(attribute) && attribute.name.name === 'className') { | ||
classNameAttr = attribute; | ||
|
||
if (t.isStringLiteral(attribute.value)) { | ||
attribute.value.value = twMerge(attribute.value.value, className); | ||
} | ||
// Handle className that is an expression (e.g., cn("class1", className)) | ||
else if ( | ||
t.isJSXExpressionContainer(attribute.value) && | ||
t.isCallExpression(attribute.value.expression) | ||
) { | ||
attribute.value.expression.arguments.push(t.stringLiteral(className)); | ||
} | ||
} | ||
}); | ||
|
||
if (!classNameAttr) { | ||
const newClassNameAttr = t.jsxAttribute( | ||
t.jsxIdentifier('className'), | ||
t.stringLiteral(className), | ||
); | ||
path.node.attributes.push(newClassNameAttr); | ||
} | ||
processed = true; | ||
}, | ||
}); | ||
} |
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
Oops, something went wrong.