Add style improvements
This commit is contained in:
parent
1048a50627
commit
cf291b60be
|
@ -1,5 +1,6 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Icon } from "@budibase/bbui"
|
import { Icon } from "@budibase/bbui"
|
||||||
|
import { isBoolean } from "lodash"
|
||||||
|
|
||||||
export let label: string | undefined
|
export let label: string | undefined
|
||||||
export let value: any
|
export let value: any
|
||||||
|
@ -7,25 +8,38 @@
|
||||||
export let path: (string | number)[] = []
|
export let path: (string | number)[] = []
|
||||||
|
|
||||||
const Colors = {
|
const Colors = {
|
||||||
Undefined: "var(--spectrum-global-color-gray-600)",
|
Array: "var(--spectrum-global-color-gray-600)",
|
||||||
Null: "purple",
|
Object: "var(--spectrum-global-color-gray-600)",
|
||||||
String: "orange",
|
Other: "var(--spectrum-global-color-indigo-600)",
|
||||||
Number: "blue",
|
Undefined: "var(--spectrum-global-color-gray-500)",
|
||||||
True: "green",
|
Null: "var(--spectrum-global-color-magenta-600)",
|
||||||
False: "red",
|
String: "var(--spectrum-global-color-orange-600)",
|
||||||
Date: "pink",
|
Number: "var(--spectrum-global-color-blue-600)",
|
||||||
|
True: "var(--spectrum-global-color-green-600)",
|
||||||
|
False: "var(--spectrum-global-color-red-600)",
|
||||||
|
Date: "var(--spectrum-global-color-green-600)",
|
||||||
}
|
}
|
||||||
|
|
||||||
let expanded = false
|
let expanded = false
|
||||||
|
|
||||||
$: isArray = Array.isArray(value)
|
$: isArray = Array.isArray(value)
|
||||||
$: isObject = value?.toString?.() === "[object Object]"
|
$: isObject = value?.toString?.() === "[object Object]"
|
||||||
$: keys = isArray || isObject ? Object.keys(value).sort() : []
|
$: keys = getKeys(isArray, isObject, value)
|
||||||
$: expandable = keys.length > 0
|
$: expandable = keys.length > 0
|
||||||
$: displayValue = getDisplayValue(isArray, isObject, keys, value)
|
$: displayValue = getDisplayValue(isArray, isObject, keys, value)
|
||||||
$: style = getStyle(expandable, value)
|
$: style = getStyle(isArray, isObject, value)
|
||||||
$: readableBinding = `{{ ${path.join(".")} }}`
|
$: readableBinding = `{{ ${path.join(".")} }}`
|
||||||
|
|
||||||
|
const getKeys = (isArray: boolean, isObject: boolean, value: any) => {
|
||||||
|
if (isArray) {
|
||||||
|
return [...value.keys()]
|
||||||
|
}
|
||||||
|
if (isObject) {
|
||||||
|
return Object.keys(value).sort()
|
||||||
|
}
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
const pluralise = (text: string, number: number) => {
|
const pluralise = (text: string, number: number) => {
|
||||||
return number === 1 ? text : text + "s"
|
return number === 1 ? text : text + "s"
|
||||||
}
|
}
|
||||||
|
@ -43,24 +57,22 @@
|
||||||
return `{} ${keys.length} ${pluralise("key", keys.length)}`
|
return `{} ${keys.length} ${pluralise("key", keys.length)}`
|
||||||
}
|
}
|
||||||
if (typeof value === "object" && typeof value?.toString === "function") {
|
if (typeof value === "object" && typeof value?.toString === "function") {
|
||||||
return JSON.stringify(value.toString(), null, 2)
|
return value.toString()
|
||||||
} else {
|
} else {
|
||||||
return JSON.stringify(value, null, 2)
|
return JSON.stringify(value, null, 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getStyle = (expandable: boolean, value: any) => {
|
const getStyle = (isArray: boolean, isObject: boolean, value: any) => {
|
||||||
let style = ""
|
return `color:${getColor(isArray, isObject, value)};`
|
||||||
const color = getColor(expandable, value)
|
|
||||||
if (color) {
|
|
||||||
style += `color:${color};`
|
|
||||||
}
|
|
||||||
return style
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getColor = (expandable: boolean, value: any) => {
|
const getColor = (isArray: boolean, isObject: boolean, value: any) => {
|
||||||
if (expandable) {
|
if (isArray) {
|
||||||
return
|
return Colors.Array
|
||||||
|
}
|
||||||
|
if (isObject) {
|
||||||
|
return Colors.Object
|
||||||
}
|
}
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case undefined:
|
case undefined:
|
||||||
|
@ -81,13 +93,12 @@
|
||||||
if (value instanceof Date) {
|
if (value instanceof Date) {
|
||||||
return Colors.Date
|
return Colors.Date
|
||||||
}
|
}
|
||||||
|
return Colors.Other
|
||||||
}
|
}
|
||||||
|
|
||||||
$: console.log(path)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="binding-node">
|
<div class="binding-node">
|
||||||
{#if label}
|
{#if label != null}
|
||||||
<div class="binding-text" title={readableBinding}>
|
<div class="binding-text" title={readableBinding}>
|
||||||
<div class="binding-arrow">
|
<div class="binding-arrow">
|
||||||
{#if expandable}
|
{#if expandable}
|
||||||
|
@ -107,7 +118,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
{#if expandable && (expanded || !label)}
|
{#if expandable && (expanded || label == null)}
|
||||||
<div class="binding-children" class:root>
|
<div class="binding-children" class:root>
|
||||||
{#each keys as key}
|
{#each keys as key}
|
||||||
<svelte:self
|
<svelte:self
|
||||||
|
@ -169,7 +180,6 @@
|
||||||
}
|
}
|
||||||
.binding-value {
|
.binding-value {
|
||||||
flex: 1 1 auto;
|
flex: 1 1 auto;
|
||||||
color: var(--spectrum-global-color-gray-600);
|
|
||||||
}
|
}
|
||||||
.binding-label.expandable {
|
.binding-label.expandable {
|
||||||
flex: 0 1 auto;
|
flex: 0 1 auto;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ActionButton, Modal, ModalContent } from "@budibase/bbui"
|
import { ActionButton, Modal, ModalContent, Helpers } from "@budibase/bbui"
|
||||||
import {
|
import {
|
||||||
previewStore,
|
previewStore,
|
||||||
selectedScreen,
|
selectedScreen,
|
||||||
|
@ -7,14 +7,8 @@
|
||||||
snippets,
|
snippets,
|
||||||
} from "@/stores/builder"
|
} from "@/stores/builder"
|
||||||
import { getBindableProperties } from "@/dataBinding"
|
import { getBindableProperties } from "@/dataBinding"
|
||||||
import { processObjectSync } from "@budibase/string-templates"
|
|
||||||
import BindingNode from "./BindingExplorer/BindingNode.svelte"
|
import BindingNode from "./BindingExplorer/BindingNode.svelte"
|
||||||
|
import { processObjectSync } from "@budibase/string-templates"
|
||||||
enum ValueType {
|
|
||||||
Object = "Object",
|
|
||||||
Array = "Array",
|
|
||||||
Primitive = "Primitive",
|
|
||||||
}
|
|
||||||
|
|
||||||
// Minimal typing for the real data binding structure, as none exists
|
// Minimal typing for the real data binding structure, as none exists
|
||||||
type DataBinding = {
|
type DataBinding = {
|
||||||
|
@ -23,45 +17,21 @@
|
||||||
readableBinding: string
|
readableBinding: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BindingEntry = {
|
|
||||||
readableBinding: string
|
|
||||||
runtimeBinding: string | null
|
|
||||||
value: any
|
|
||||||
valueType: ValueType
|
|
||||||
}
|
|
||||||
|
|
||||||
type BindingMap = {
|
|
||||||
[key: string]: BindingEntry
|
|
||||||
}
|
|
||||||
|
|
||||||
let modal: any
|
let modal: any
|
||||||
|
|
||||||
$: context = {
|
$: previewContext = $previewStore.selectedComponentContext || {}
|
||||||
...($previewStore.selectedComponentContext || {}),
|
|
||||||
date: new Date(),
|
|
||||||
string: "foo",
|
|
||||||
number: 1234,
|
|
||||||
undefined: undefined,
|
|
||||||
null: null,
|
|
||||||
true: true,
|
|
||||||
false: false,
|
|
||||||
array: [1, 2, 3],
|
|
||||||
object: { foo: "bar" },
|
|
||||||
error: new Error(),
|
|
||||||
}
|
|
||||||
$: selectedComponentId = $componentStore.selectedComponentId
|
$: selectedComponentId = $componentStore.selectedComponentId
|
||||||
|
$: context = makeContext(previewContext, bindings)
|
||||||
$: bindings = getBindableProperties($selectedScreen, selectedComponentId)
|
$: bindings = getBindableProperties($selectedScreen, selectedComponentId)
|
||||||
$: enrichedBindings = enrichBindings(bindings, context, $snippets)
|
|
||||||
|
|
||||||
const show = () => {
|
const show = () => {
|
||||||
previewStore.requestComponentContext()
|
previewStore.requestComponentContext()
|
||||||
modal.show()
|
modal.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
const enrichBindings = (
|
const makeContext = (
|
||||||
bindings: DataBinding[],
|
previewContext: Record<string, any>,
|
||||||
context: Record<string, any>,
|
bindings: DataBinding[]
|
||||||
snippets: any
|
|
||||||
) => {
|
) => {
|
||||||
// Create a single big array to enrich in one go
|
// Create a single big array to enrich in one go
|
||||||
const bindingStrings = bindings.map(binding => {
|
const bindingStrings = bindings.map(binding => {
|
||||||
|
@ -74,17 +44,36 @@
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const bindingEvauations = processObjectSync(bindingStrings, {
|
const bindingEvauations = processObjectSync(bindingStrings, {
|
||||||
...context,
|
...previewContext,
|
||||||
snippets,
|
snippets: $snippets,
|
||||||
}) as any[]
|
}) as any[]
|
||||||
|
|
||||||
// Enrich bindings with evaluations and highlighted HTML
|
// Enrich bindings with evaluations and highlighted HTML
|
||||||
const flatBindings = bindings.map((binding, idx) => ({
|
const enrichedBindings: any[] = bindings.map((binding, idx) => {
|
||||||
|
return {
|
||||||
...binding,
|
...binding,
|
||||||
value: bindingEvauations[idx],
|
value: bindingEvauations[idx],
|
||||||
}))
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return flatBindings
|
let context = {
|
||||||
|
_dataTypes: {
|
||||||
|
date: new Date(),
|
||||||
|
string: "foo",
|
||||||
|
number: 1234,
|
||||||
|
undefined: undefined,
|
||||||
|
null: null,
|
||||||
|
true: true,
|
||||||
|
false: false,
|
||||||
|
array: [1, 2, 3],
|
||||||
|
object: { foo: "bar" },
|
||||||
|
error: new Error(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for (let binding of enrichedBindings) {
|
||||||
|
Helpers.deepSet(context, binding.readableBinding, binding.value)
|
||||||
|
}
|
||||||
|
return context
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue