Skip to content
This repository was archived by the owner on Jun 24, 2023. It is now read-only.

Commit 6f2dd81

Browse files
committed
prop types simplify to one file
1 parent d779bb4 commit 6f2dd81

File tree

5 files changed

+115
-202
lines changed

5 files changed

+115
-202
lines changed

packages/editable/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ export * from "./EditableElement"
44
export * from "./Editor"
55
export * from "./EditorContext"
66
export * from "./history"
7-
export * from "./prop-types/createProp"
8-
export * from "./prop-types/createPropTypes"
9-
export * from "./prop-types/types"
7+
export * from "./prop-types"
108
export * from "./ui/element"
119
export * from "./ui/ElementIcon"
1210
export * from "./ui/OpenInEditorButton"

packages/editable/src/prop-types.tsx

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { EditableElement } from "./EditableElement"
2+
3+
export type PropType = {
4+
get: (obj: any, prop: string) => any
5+
set: (obj: any, prop: string, value: any) => void
6+
control?: any
7+
load?: (obj: any, prop: string, value: any) => Promise<any> | any
8+
init?: (obj: any, prop: string, value: any) => void
9+
serialize?: (obj: any, prop: string, input: any, value: any) => any
10+
}
11+
12+
export function createProp(
13+
type: PropType | ((props: PropInput) => any),
14+
{
15+
element = {} as EditableElement,
16+
path = [],
17+
onChange,
18+
...settings
19+
}: PropInput & {
20+
step?: number
21+
min?: number
22+
max?: number
23+
options?: string[] | Record<any, any>
24+
lock?: boolean
25+
default?: any
26+
}
27+
) {
28+
if (typeof type === "function") {
29+
return type({
30+
element,
31+
path,
32+
onChange,
33+
...settings
34+
})
35+
}
36+
37+
const el = element.getObjectByPath(path.slice(0, path.length - 1))
38+
39+
let prop = path[path.length - 1]
40+
41+
let initialValue = el
42+
? type.get(el, prop) ?? settings.default
43+
: settings.default
44+
45+
let handleChange = (value: any, controlPath: string, context: any) => {
46+
element.handlePropChange({
47+
path,
48+
input: value,
49+
controlPath,
50+
context,
51+
onChange,
52+
type
53+
} as PropChange)
54+
}
55+
56+
return type.control
57+
? type.control({
58+
value: initialValue,
59+
onChange: handleChange,
60+
...settings
61+
})
62+
: {
63+
value: initialValue,
64+
onChange: handleChange,
65+
...settings
66+
}
67+
}
68+
69+
export interface PropInput {
70+
path: string[]
71+
element: EditableElement
72+
73+
step?: number
74+
min?: number
75+
max?: number
76+
options?: string[] | Record<any, any>
77+
lock?: boolean
78+
default?: any
79+
label?: string
80+
collapsed?: boolean
81+
82+
onChange?: (value: any, controlPath: string) => void
83+
render?(get: (prop: string) => any): boolean
84+
}
85+
86+
export type PropChange = {
87+
controlPath: string
88+
input: any
89+
type: PropType
90+
path: string[]
91+
context: any
92+
onChange?: (value: any, controlPath: string) => void
93+
94+
object: any
95+
prop: string
96+
closestEditable?: EditableElement
97+
value: any
98+
remainingPath: string[]
99+
}
100+
101+
type PropTypes<K> = {
102+
[k in keyof K]: (props: PropInput) => any
103+
}
104+
105+
export let createPropTypes = function <K extends object>(
106+
types: K
107+
): PropTypes<K> {
108+
return Object.fromEntries(
109+
Object.entries(types).map(([k, v]) => [
110+
k,
111+
(props: PropInput) => createProp(v, props)
112+
])
113+
) as any
114+
}

packages/editable/src/prop-types/createProp.tsx

-148
This file was deleted.

packages/editable/src/prop-types/createPropTypes.tsx

-17
This file was deleted.

packages/editable/src/prop-types/types.tsx

-34
This file was deleted.

0 commit comments

Comments
 (0)