Skip to content

Commit

Permalink
Add an empty text node to prose elements
Browse files Browse the repository at this point in the history
  • Loading branch information
johno committed Aug 29, 2022
1 parent d22fa88 commit 603fbfe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-tables-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compai/css-gui': patch
---

Add an empty text node to prose elements
9 changes: 7 additions & 2 deletions packages/gui/src/components/html/Editors/NodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ComponentEditor } from '../Component'
import { SlotEditor } from './SlotEditor'
import { HTML_TAGS } from '../data'
import { useNodeTypes } from './util'
import { isProseElement } from '../../../lib/elements'

interface EditorProps {
value: HtmlNode
Expand Down Expand Up @@ -196,12 +197,16 @@ function NodeSwitch({ value, onChange }: EditorProps) {
...defaultAttributes,
...(value.attributes || {}),
}
onChange({
const fullValue = {
...value,
attributes: mergedAttributes,
tagName: selectedItem,
style: mergedStyles,
})
}
if (isProseElement(selectedItem) && !fullValue.children?.length) {
fullValue.children = [{ type: 'text', value: '' }]
}
onChange(fullValue)
}}
items={HTML_TAGS}
value={value.tagName}
Expand Down
12 changes: 8 additions & 4 deletions packages/gui/src/components/html/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HtmlNode, ElementPath } from './types'
import * as Collapsible from '@radix-ui/react-collapsible'
import { useState } from 'react'
import { useHtmlEditor } from './Provider'
import { isVoidElement } from '../../lib/elements'
import { isProseElement, isVoidElement } from '../../lib/elements'
import { addChildAtPath, isSamePath, replaceAt } from './util'
import { hasChildrenSlot } from '../../lib/codegen/util'
import { Combobox } from '../primitives'
Expand Down Expand Up @@ -128,13 +128,17 @@ export function TreeNode({ value, path, onSelect, onChange }: TreeNodeProps) {
...defaultAttributes,
...(value.attributes || {}),
}
setEditing(false)
onChange({
const fullValue = {
...value,
attributes: mergedAttributes,
tagName: selectedItem,
style: mergedStyles,
})
}
if (isProseElement(selectedItem) && !fullValue.children?.length) {
fullValue.children = [{ type: 'text', value: '' }]
}
setEditing(false)
onChange(fullValue)
}}
items={HTML_TAGS}
value={value.tagName}
Expand Down
5 changes: 5 additions & 0 deletions packages/gui/src/lib/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export const isElement = (str: string): boolean => {
export const isVoidElement = (str: string): boolean => {
return !!voidElements.filter((el) => str === el).length
}

const PROSE_ELEMENTS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
export const isProseElement = (str: string): boolean => {
return PROSE_ELEMENTS.includes(str)
}

0 comments on commit 603fbfe

Please sign in to comment.