Merge pull request #15454 from Budibase/fix-adding-component-after-undo
Fix adding component after undo
This commit is contained in:
commit
5e867e39a6
|
@ -1437,8 +1437,6 @@ class AutomationStore extends BudiStore<AutomationState> {
|
||||||
this.history = createHistoryStore({
|
this.history = createHistoryStore({
|
||||||
getDoc: this.actions.getDefinition.bind(this),
|
getDoc: this.actions.getDefinition.bind(this),
|
||||||
selectDoc: this.actions.select.bind(this),
|
selectDoc: this.actions.select.bind(this),
|
||||||
beforeAction: () => {},
|
|
||||||
afterAction: () => {},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Then wrap save and delete with history
|
// Then wrap save and delete with history
|
||||||
|
|
|
@ -30,9 +30,18 @@ import {
|
||||||
} from "@/constants/backend"
|
} from "@/constants/backend"
|
||||||
import { BudiStore } from "../BudiStore"
|
import { BudiStore } from "../BudiStore"
|
||||||
import { Utils } from "@budibase/frontend-core"
|
import { Utils } from "@budibase/frontend-core"
|
||||||
import { Component, FieldType, Screen, Table } from "@budibase/types"
|
import {
|
||||||
|
Component as ComponentType,
|
||||||
|
FieldType,
|
||||||
|
Screen,
|
||||||
|
Table,
|
||||||
|
} from "@budibase/types"
|
||||||
import { utils } from "@budibase/shared-core"
|
import { utils } from "@budibase/shared-core"
|
||||||
|
|
||||||
|
interface Component extends ComponentType {
|
||||||
|
_id: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface ComponentState {
|
export interface ComponentState {
|
||||||
components: Record<string, ComponentDefinition>
|
components: Record<string, ComponentDefinition>
|
||||||
customComponents: string[]
|
customComponents: string[]
|
||||||
|
@ -254,7 +263,10 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
* @param {object} opts
|
* @param {object} opts
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
enrichEmptySettings(component: Component, opts: any) {
|
enrichEmptySettings(
|
||||||
|
component: Component,
|
||||||
|
opts: { screen?: Screen; parent?: Component; useDefaultValues?: boolean }
|
||||||
|
) {
|
||||||
if (!component?._component) {
|
if (!component?._component) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -364,7 +376,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
getSchemaForDatasource(screen, dataSource, {})
|
getSchemaForDatasource(screen, dataSource, {})
|
||||||
|
|
||||||
// Finds fields by types from the schema of the configured datasource
|
// Finds fields by types from the schema of the configured datasource
|
||||||
const findFieldTypes = (fieldTypes: any) => {
|
const findFieldTypes = (fieldTypes: FieldType | FieldType[]) => {
|
||||||
if (!Array.isArray(fieldTypes)) {
|
if (!Array.isArray(fieldTypes)) {
|
||||||
fieldTypes = [fieldTypes]
|
fieldTypes = [fieldTypes]
|
||||||
}
|
}
|
||||||
|
@ -439,7 +451,11 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
* @param {object} parent
|
* @param {object} parent
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
createInstance(componentName: string, presetProps: any, parent: any) {
|
createInstance(
|
||||||
|
componentName: string,
|
||||||
|
presetProps: any,
|
||||||
|
parent: any
|
||||||
|
): Component | null {
|
||||||
const screen = get(selectedScreen)
|
const screen = get(selectedScreen)
|
||||||
if (!screen) {
|
if (!screen) {
|
||||||
throw "A valid screen must be selected"
|
throw "A valid screen must be selected"
|
||||||
|
@ -451,7 +467,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate basic component structure
|
// Generate basic component structure
|
||||||
let instance = {
|
let instance: Component = {
|
||||||
_id: Helpers.uuid(),
|
_id: Helpers.uuid(),
|
||||||
_component: definition.component,
|
_component: definition.component,
|
||||||
_styles: {
|
_styles: {
|
||||||
|
@ -478,7 +494,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom post processing for creation only
|
// Custom post processing for creation only
|
||||||
let extras: any = {}
|
let extras: Partial<Component> = {}
|
||||||
if (definition.hasChildren) {
|
if (definition.hasChildren) {
|
||||||
extras._children = []
|
extras._children = []
|
||||||
}
|
}
|
||||||
|
@ -487,7 +503,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
if (componentName.endsWith("/formstep")) {
|
if (componentName.endsWith("/formstep")) {
|
||||||
const parentForm = findClosestMatchingComponent(
|
const parentForm = findClosestMatchingComponent(
|
||||||
screen.props,
|
screen.props,
|
||||||
get(selectedComponent)._id,
|
get(selectedComponent)?._id,
|
||||||
(component: Component) => component._component.endsWith("/form")
|
(component: Component) => component._component.endsWith("/form")
|
||||||
)
|
)
|
||||||
const formSteps = findAllMatchingComponents(
|
const formSteps = findAllMatchingComponents(
|
||||||
|
@ -515,7 +531,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
async create(
|
async create(
|
||||||
componentName: string,
|
componentName: string,
|
||||||
presetProps: any,
|
presetProps: any,
|
||||||
parent: any,
|
parent: Component,
|
||||||
index: number
|
index: number
|
||||||
) {
|
) {
|
||||||
const state = get(this.store)
|
const state = get(this.store)
|
||||||
|
@ -772,7 +788,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
if (!cut) {
|
if (!cut) {
|
||||||
componentToPaste = makeComponentUnique(componentToPaste)
|
componentToPaste = makeComponentUnique(componentToPaste)
|
||||||
}
|
}
|
||||||
newComponentId = componentToPaste._id!
|
newComponentId = componentToPaste._id
|
||||||
|
|
||||||
// Strip grid position metadata if pasting into a new screen, but keep
|
// Strip grid position metadata if pasting into a new screen, but keep
|
||||||
// alignment metadata
|
// alignment metadata
|
||||||
|
@ -915,7 +931,7 @@ export class ComponentStore extends BudiStore<ComponentState> {
|
||||||
|
|
||||||
// If we have children, select first child, and the node is not collapsed
|
// If we have children, select first child, and the node is not collapsed
|
||||||
if (
|
if (
|
||||||
component._children?.length &&
|
component?._children?.length &&
|
||||||
(state.selectedComponentId === navComponentId ||
|
(state.selectedComponentId === navComponentId ||
|
||||||
componentTreeNodesStore.isNodeExpanded(component._id))
|
componentTreeNodesStore.isNodeExpanded(component._id))
|
||||||
) {
|
) {
|
||||||
|
@ -1339,12 +1355,15 @@ export const componentStore = new ComponentStore()
|
||||||
|
|
||||||
export const selectedComponent = derived(
|
export const selectedComponent = derived(
|
||||||
[componentStore, selectedScreen],
|
[componentStore, selectedScreen],
|
||||||
([$store, $selectedScreen]) => {
|
([$store, $selectedScreen]): Component | null => {
|
||||||
if (
|
if (
|
||||||
$selectedScreen &&
|
$selectedScreen &&
|
||||||
$store.selectedComponentId?.startsWith(`${$selectedScreen._id}-`)
|
$store.selectedComponentId?.startsWith(`${$selectedScreen._id}-`)
|
||||||
) {
|
) {
|
||||||
return $selectedScreen?.props
|
return {
|
||||||
|
...$selectedScreen.props,
|
||||||
|
_id: $selectedScreen.props._id!,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!$selectedScreen || !$store.selectedComponentId) {
|
if (!$selectedScreen || !$store.selectedComponentId) {
|
||||||
return null
|
return null
|
||||||
|
|
|
@ -16,8 +16,8 @@ export const initialState = {
|
||||||
export const createHistoryStore = ({
|
export const createHistoryStore = ({
|
||||||
getDoc,
|
getDoc,
|
||||||
selectDoc,
|
selectDoc,
|
||||||
beforeAction,
|
beforeAction = () => {},
|
||||||
afterAction,
|
afterAction = () => {},
|
||||||
}) => {
|
}) => {
|
||||||
// Use a derived store to check if we are able to undo or redo any operations
|
// Use a derived store to check if we are able to undo or redo any operations
|
||||||
const store = writable(initialState)
|
const store = writable(initialState)
|
||||||
|
|
|
@ -58,13 +58,12 @@ export class ScreenStore extends BudiStore<ScreenState> {
|
||||||
getDoc: (id: string) =>
|
getDoc: (id: string) =>
|
||||||
get(this.store).screens?.find(screen => screen._id === id),
|
get(this.store).screens?.find(screen => screen._id === id),
|
||||||
selectDoc: this.select,
|
selectDoc: this.select,
|
||||||
beforeAction: () => {},
|
|
||||||
afterAction: () => {
|
afterAction: () => {
|
||||||
// Ensure a valid component is selected
|
// Ensure a valid component is selected
|
||||||
if (!get(selectedComponent)) {
|
if (!get(selectedComponent)) {
|
||||||
this.update(state => ({
|
componentStore.update(state => ({
|
||||||
...state,
|
...state,
|
||||||
selectedComponentId: get(selectedScreen)?.props._id,
|
selectedComponentId: get(selectedScreen)?._id,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue