Skip to content

Commit 5480b43

Browse files
authored
Merge pull request #590 from components-ai/prose-elements
Add an empty text node to prose elements
2 parents 162feb4 + 603fbfe commit 5480b43

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

.changeset/green-tables-hunt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compai/css-gui': patch
3+
---
4+
5+
Add an empty text node to prose elements

packages/gui/src/components/html/Editors/NodeEditor.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ComponentEditor } from '../Component'
1111
import { SlotEditor } from './SlotEditor'
1212
import { HTML_TAGS } from '../data'
1313
import { useNodeTypes } from './util'
14+
import { isProseElement } from '../../../lib/elements'
1415

1516
interface EditorProps {
1617
value: HtmlNode
@@ -196,12 +197,16 @@ function NodeSwitch({ value, onChange }: EditorProps) {
196197
...defaultAttributes,
197198
...(value.attributes || {}),
198199
}
199-
onChange({
200+
const fullValue = {
200201
...value,
201202
attributes: mergedAttributes,
202203
tagName: selectedItem,
203204
style: mergedStyles,
204-
})
205+
}
206+
if (isProseElement(selectedItem) && !fullValue.children?.length) {
207+
fullValue.children = [{ type: 'text', value: '' }]
208+
}
209+
onChange(fullValue)
205210
}}
206211
items={HTML_TAGS}
207212
value={value.tagName}

packages/gui/src/components/html/TreeNode.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HtmlNode, ElementPath } from './types'
22
import * as Collapsible from '@radix-ui/react-collapsible'
33
import { useState } from 'react'
44
import { useHtmlEditor } from './Provider'
5-
import { isVoidElement } from '../../lib/elements'
5+
import { isProseElement, isVoidElement } from '../../lib/elements'
66
import { addChildAtPath, isSamePath, replaceAt } from './util'
77
import { hasChildrenSlot } from '../../lib/codegen/util'
88
import { Combobox } from '../primitives'
@@ -128,13 +128,17 @@ export function TreeNode({ value, path, onSelect, onChange }: TreeNodeProps) {
128128
...defaultAttributes,
129129
...(value.attributes || {}),
130130
}
131-
setEditing(false)
132-
onChange({
131+
const fullValue = {
133132
...value,
134133
attributes: mergedAttributes,
135134
tagName: selectedItem,
136135
style: mergedStyles,
137-
})
136+
}
137+
if (isProseElement(selectedItem) && !fullValue.children?.length) {
138+
fullValue.children = [{ type: 'text', value: '' }]
139+
}
140+
setEditing(false)
141+
onChange(fullValue)
138142
}}
139143
items={HTML_TAGS}
140144
value={value.tagName}

packages/gui/src/lib/elements.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ export const isElement = (str: string): boolean => {
77
export const isVoidElement = (str: string): boolean => {
88
return !!voidElements.filter((el) => str === el).length
99
}
10+
11+
const PROSE_ELEMENTS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
12+
export const isProseElement = (str: string): boolean => {
13+
return PROSE_ELEMENTS.includes(str)
14+
}

0 commit comments

Comments
 (0)