Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-ties-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@compai/css-gui": patch
---

Use text node rather than string
27 changes: 22 additions & 5 deletions apps/docs/pages/html-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const initialValue: any = {
unit: 'number',
},
},
children: ['CSS.GUI'],
children: [{ type: 'text', value: 'CSS.GUI' }],
},
{
tagName: 'h2',
Expand All @@ -72,7 +72,11 @@ const initialValue: any = {
},
},
children: [
'Quickly build components with custom styling panels. No coding required.',
{
type: 'text',
value:
'Quickly build components with custom styling panels. No coding required.',
},
],
},
{
Expand All @@ -89,7 +93,10 @@ const initialValue: any = {
},
},
children: [
'Click anywhere on the canvas to start. Go ahead. Click away.',
{
type: 'text',
value: 'Click anywhere on the canvas to start. Go ahead. Click away.',
},
],
},
{
Expand Down Expand Up @@ -140,7 +147,12 @@ const initialValue: any = {
},
},
tagName: 'a',
children: ['Primary CTA'],
children: [
{
type: 'text',
value: 'Primary CTA',
},
],
},
{
attributes: { href: 'https://components.ai' },
Expand Down Expand Up @@ -182,7 +194,12 @@ const initialValue: any = {
},
},
tagName: 'a',
children: ['Secondary link'],
children: [
{
type: 'text',
value: 'Secondary link',
},
],
},
],
}
Expand Down
12 changes: 8 additions & 4 deletions packages/gui/src/components/html/FontTags.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { debounce, uniq } from 'lodash-es'
import { useEffect, useState } from 'react'
import { buildFontFamiliesHref, buildVariableFontFamiliesHref } from '../inputs/FontFamily/FontTags'
import {
buildFontFamiliesHref,
buildVariableFontFamiliesHref,
} from '../inputs/FontFamily/FontTags'
import { HtmlNode } from './types'

export function getStyleFonts(style: any): string[] {
if (!style) return []
Expand Down Expand Up @@ -28,7 +32,7 @@ export function getHTMLTreeFonts(root: any): string[] {
}

for (const node of root.children) {
if (typeof node === 'object') {
if (node.type !== 'text') {
treeFonts = [...treeFonts, ...getHTMLTreeFonts(node)]
}
}
Expand All @@ -47,11 +51,11 @@ async function buildHrefs({
style,
setStaticHref,
setVariableHref,
}: BuildHrefProps) {
}: BuildHrefProps) {
const fonts = style ? getStyleFonts(style) : getHTMLTreeFonts(tree)
const staticHref = await buildFontFamiliesHref(fonts)
const variableHref = await buildVariableFontFamiliesHref(fonts)

setStaticHref(staticHref)
setVariableHref(variableHref)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gui/src/components/html/Renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function ElementRenderer({ value, path }: ElementRendererProps) {
<span sx={{ cursor: 'default', a: { cursor: 'default' } }}>
<Tag {...props}>
{children.map((child, i) => {
if (typeof child === 'string') {
return child
if (child.type === 'text') {
return child.value
}
return <ElementRenderer key={i} value={child} path={[...path, i]} />
})}
Expand Down
37 changes: 28 additions & 9 deletions packages/gui/src/components/html/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface TagEditorProps extends EditorProps {
}

function NodeEditor({ value, onChange, onRemove }: TagEditorProps) {
const nodeType = typeof value === 'string' ? 'text' : 'tag'
const nodeType = value.type === 'text' ? 'text' : 'tag'
return (
<div
sx={{
Expand All @@ -115,9 +115,10 @@ function NodeEditor({ value, onChange, onRemove }: TagEditorProps) {
value={nodeType}
onChange={(value) => {
if (value === 'text') {
onChange('')
onChange({ type: 'text', value: '' })
} else {
onChange({
type: 'element',
tagName: 'div',
})
}
Expand All @@ -136,12 +137,20 @@ function NodeEditor({ value, onChange, onRemove }: TagEditorProps) {
}

function NodeSwitch({ value, onChange }: EditorProps) {
if (typeof value === 'string') {
if (value.type === 'text') {
return (
<div>
<Label>
Content
<input value={value} onChange={(e) => onChange(e.target.value)} />
<input
value={value.value}
onChange={(e) =>
onChange({
...value,
value: e.target.value,
})
}
/>
</Label>
</div>
)
Expand Down Expand Up @@ -182,7 +191,7 @@ function NodeSwitch({ value, onChange }: EditorProps) {
onChange={(newAttributes) =>
onChange({ ...value, attributes: newAttributes })
}
element={value.tagName}
element={value.tagName as string}
/>
</div>
</article>
Expand Down Expand Up @@ -241,11 +250,11 @@ function TreeNode({ value, path, onSelect, onChange }: TreeNodeProps) {
onClick={() => onSelect(path)}
>
&lt;{value.tagName}
{!open || isVoidElement(value.tagName) ? ' /' : null}&gt;
{!open || isVoidElement(value.tagName as string) ? ' /' : null}&gt;
</button>
)

if (isVoidElement(value.tagName)) {
if (isVoidElement(value.tagName as string)) {
return tagButton
}

Expand Down Expand Up @@ -276,7 +285,12 @@ function TreeNode({ value, path, onSelect, onChange }: TreeNodeProps) {
<Fragment key={i}>
<AddChildButton
onClick={() => {
onChange(addChildAtPath(value, [i], ''))
onChange(
addChildAtPath(value, [i], {
type: 'text',
value: '',
})
)
onSelect(null)
}}
/>
Expand All @@ -296,7 +310,12 @@ function TreeNode({ value, path, onSelect, onChange }: TreeNodeProps) {
})}
<AddChildButton
onClick={() => {
onChange(addChildAtPath(value, [value.children?.length ?? 0], ''))
onChange(
addChildAtPath(value, [value.children?.length ?? 0], {
type: 'text',
value: '',
})
)
onSelect(null)
}}
/>
Expand Down
6 changes: 4 additions & 2 deletions packages/gui/src/components/html/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export interface ElementData {
tagName: string
type: 'element' | 'text'
tagName?: string
attributes?: Record<string, string>
// `style` is an attribute, but we treat it specially for CSS.gui
style?: Record<string, any>
value?: string
children?: HtmlNode[]
}

export type HtmlNode = ElementData | string
export type HtmlNode = ElementData
export type ElementPath = number[]

export const enum HTMLTag {
Expand Down
1 change: 0 additions & 1 deletion packages/gui/src/lib/transformers/html-to-editor-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const htmlToEditorSchema = (text: string) => {
.use(cleanNewLines)
// @ts-ignore
.use(propertiesToAttributes)
.use(textNodesToStrings)
.runSync(tree)

const htmlBody = processedTree.children[0]
Expand Down