Add ability to enter custom component CSS

This commit is contained in:
Andrew Kingston 2021-01-04 18:39:17 +00:00
parent 736adca7a4
commit 1a1aa5963d
4 changed files with 47 additions and 22 deletions

View File

@ -366,20 +366,20 @@ export const getFrontendStore = () => {
await Promise.all(promises) await Promise.all(promises)
}, },
updateStyle: async (type, name, value) => { updateStyle: async (type, name, value) => {
let promises = []
const selected = get(selectedComponent) const selected = get(selectedComponent)
if (!selected._styles) {
store.update(state => { selected._styles = {}
if (!selected._styles) { }
selected._styles = {} selected._styles[type][name] = value
} await store.actions.preview.saveSelected()
selected._styles[type][name] = value },
updateCustomStyle: async style => {
// save without messing with the store const selected = get(selectedComponent)
promises.push(store.actions.preview.saveSelected()) if (!selected._styles) {
return state selected._styles = {}
}) }
await Promise.all(promises) selected._styles.custom = style
await store.actions.preview.saveSelected()
}, },
updateProp: (name, value) => { updateProp: (name, value) => {
store.update(state => { store.update(state => {

View File

@ -31,6 +31,7 @@
componentPropDefinition.properties[selectedCategory.value] componentPropDefinition.properties[selectedCategory.value]
const onStyleChanged = store.actions.components.updateStyle const onStyleChanged = store.actions.components.updateStyle
const onCustomStyleChanged = store.actions.components.updateCustomStyle
$: isComponentOrScreen = $: isComponentOrScreen =
$store.currentView === "component" || $store.currentView === "component" ||
@ -93,7 +94,11 @@
<div class="component-props-container"> <div class="component-props-container">
{#if selectedCategory.value === 'design'} {#if selectedCategory.value === 'design'}
<DesignView {panelDefinition} {componentInstance} {onStyleChanged} /> <DesignView
{panelDefinition}
{componentInstance}
{onStyleChanged}
{onCustomStyleChanged} />
{:else if selectedCategory.value === 'settings'} {:else if selectedCategory.value === 'settings'}
<SettingsView <SettingsView
{componentInstance} {componentInstance}

View File

@ -1,5 +1,5 @@
<script> <script>
import { onMount } from "svelte" import { TextArea, DetailSummary } from "@budibase/bbui"
import PropertyGroup from "./PropertyGroup.svelte" import PropertyGroup from "./PropertyGroup.svelte"
import FlatButtonGroup from "./FlatButtonGroup.svelte" import FlatButtonGroup from "./FlatButtonGroup.svelte"
@ -7,6 +7,7 @@
export let componentInstance = {} export let componentInstance = {}
export let componentDefinition = {} export let componentDefinition = {}
export let onStyleChanged = () => {} export let onStyleChanged = () => {}
export let onCustomStyleChanged = () => {}
let selectedCategory = "normal" let selectedCategory = "normal"
let propGroup = null let propGroup = null
@ -44,6 +45,17 @@
open={currentGroup === groupName} open={currentGroup === groupName}
on:open={() => (currentGroup = groupName)} /> on:open={() => (currentGroup = groupName)} />
{/each} {/each}
<DetailSummary
name="Custom Styles"
on:open={() => (currentGroup = 'custom')}
show={currentGroup === 'custom'}
thin>
<div class="custom-styles">
<TextArea
value={componentInstance._styles.custom}
on:change={event => onCustomStyleChanged(event.target.value)} />
</div>
</DetailSummary>
{:else} {:else}
<div class="no-design"> <div class="no-design">
This component doesn't have any design properties. This component doesn't have any design properties.
@ -85,4 +97,10 @@
font-size: var(--font-size-xs); font-size: var(--font-size-xs);
color: var(--grey-5); color: var(--grey-5);
} }
.custom-styles :global(textarea) {
font-family: monospace;
min-height: 120px;
font-size: var(--font-size-xs);
}
</style> </style>

View File

@ -1,16 +1,17 @@
/** /**
* Helper to build a CSS string from a style object * Helper to build a CSS string from a style object
*/ */
const buildStyleString = (styles, selected) => { const buildStyleString = (styleObject, customStyles, selected) => {
let str = "" let str = ""
if (selected) { Object.entries(styleObject).forEach(([style, value]) => {
styles.border = "2px solid #0055ff !important"
}
Object.entries(styles).forEach(([style, value]) => {
if (style && value != null) { if (style && value != null) {
str += `${style}: ${value}; ` str += `${style}: ${value}; `
} }
}) })
str += customStyles || ""
if (selected) {
str += ";border: 2px solid #0055ff !important;"
}
return str return str
} }
@ -24,6 +25,7 @@ export const styleable = (node, styles = {}) => {
// Creates event listeners and applies initial styles // Creates event listeners and applies initial styles
const setupStyles = newStyles => { const setupStyles = newStyles => {
const selected = newStyles.selected const selected = newStyles.selected
const customStyles = newStyles.custom
const normalStyles = newStyles.normal || {} const normalStyles = newStyles.normal || {}
const hoverStyles = { const hoverStyles = {
...normalStyles, ...normalStyles,
@ -31,11 +33,11 @@ export const styleable = (node, styles = {}) => {
} }
applyNormalStyles = () => { applyNormalStyles = () => {
node.style = buildStyleString(normalStyles, selected) node.style = buildStyleString(normalStyles, customStyles, selected)
} }
applyHoverStyles = () => { applyHoverStyles = () => {
node.style = buildStyleString(hoverStyles, selected) node.style = buildStyleString(hoverStyles, customStyles, selected)
} }
// Add listeners to toggle hover styles // Add listeners to toggle hover styles