@tinacms/app 1.2.6 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,196 +0,0 @@
1
- /**
2
-
3
- */
4
- import { DocumentBlueprint } from '../formify/types'
5
-
6
- type Location = number[] | null
7
-
8
- export const getBlueprintValues = (
9
- data: object,
10
- path: string,
11
- index: number = 0
12
- ): string[] | undefined => {
13
- if (!data) {
14
- return
15
- }
16
- const pathArray = path.split('.')
17
- const next = pathArray[index]
18
- if (next === '[]') {
19
- if (Array.isArray(data)) {
20
- const values: string[] = []
21
- data.forEach((item) => {
22
- const res = getBlueprintValues(item, path, index + 1)
23
- if (res) {
24
- res.forEach((item) => {
25
- values.push(item)
26
- })
27
- }
28
- })
29
- return values
30
- }
31
- } else {
32
- const value = data[next]
33
- // Is last item
34
- if (pathArray.length === index + 1) {
35
- if (Array.isArray(value)) {
36
- return value
37
- } else {
38
- return [value]
39
- }
40
- } else {
41
- return getBlueprintValues(value, path)
42
- }
43
- }
44
- }
45
-
46
- export const getAllIn = (
47
- data: object | object[] | null,
48
- path: string,
49
- index: number = 0,
50
- location: Location = null
51
- ): { value: { id: string }; location: Location }[] | undefined => {
52
- if (!data) {
53
- return
54
- }
55
- const pathArray = path.split('.')
56
- const next = pathArray[index]
57
- if (next === '[]') {
58
- if (Array.isArray(data)) {
59
- const values: { value: { id: string }; location: Location }[] = []
60
- data.forEach((item, dataIndex) => {
61
- const res = getAllIn(item, path, index + 1, [
62
- ...(location || []),
63
- dataIndex,
64
- ])
65
- if (res) {
66
- res.forEach((item) => {
67
- values.push(item)
68
- })
69
- }
70
- })
71
- return values
72
- }
73
- } else {
74
- const value = data[next]
75
- // Is last item
76
- if (pathArray.length === index + 1) {
77
- if (Array.isArray(value)) {
78
- const v = value.map((item, index) => ({
79
- value: item,
80
- location: [...(location || []), index],
81
- }))
82
- return v
83
- } else {
84
- if (typeof value === 'string') {
85
- const v = [{ value: { id: value }, location }]
86
- return v
87
- } else {
88
- // FIXME: this should always be `_internalSys.path` but we're not populating the
89
- // data properly on resolveData yet. But even when we are, will need to keep that out
90
- // of the user-provided payload (maybe not?)
91
- const id = value?._internalSys?.path || value?.id
92
- if (id) {
93
- const v = [{ value: { id, ...value }, location }]
94
- return v
95
- }
96
- }
97
- }
98
- } else {
99
- const v = getAllIn(value, path, index + 1, location)
100
- return v
101
- }
102
- }
103
- }
104
- export const getAllInBlueprint = (
105
- data: object | object[] | null,
106
- path: string,
107
- index: number = 0,
108
- location: Location = null
109
- ):
110
- | { value: { _internalSys: { path: string } }; location: Location }[]
111
- | undefined => {
112
- if (!data) {
113
- return
114
- }
115
- const pathArray = path.split('.')
116
- const next = pathArray[index]
117
- if (next === '[]') {
118
- if (Array.isArray(data)) {
119
- const values: {
120
- value: { _internalSys: { path: string } }
121
- location: Location
122
- }[] = []
123
- data.forEach((item, dataIndex) => {
124
- const res = getAllInBlueprint(item, path, index + 1, [
125
- ...(location || []),
126
- dataIndex,
127
- ])
128
- if (res) {
129
- res.forEach((item) => {
130
- values.push(item)
131
- })
132
- }
133
- })
134
- return values
135
- }
136
- } else {
137
- const value = data[next]
138
- // Is last item
139
- if (pathArray.length === index + 1) {
140
- if (Array.isArray(value)) {
141
- throw new Error(`Unexpected array value for getAllInBlueprint`)
142
- } else {
143
- if (typeof value === 'string') {
144
- throw new Error(`Unexpected string value for getAllInBlueprint`)
145
- } else {
146
- const id = value?._internalSys?.path || value?.id
147
- if (id) {
148
- const v = [
149
- { value: { _internalSys: value._internalSys }, location },
150
- ]
151
- return v
152
- }
153
- }
154
- }
155
- } else {
156
- const v = getAllInBlueprint(value, path, index + 1, location)
157
- return v
158
- }
159
- }
160
- }
161
-
162
- export const getBlueprintFromLocation = (
163
- location: string,
164
- blueprints: DocumentBlueprint[]
165
- ) => {
166
- const blueprintString = location
167
- .split('.')
168
- .map((item) => (isNaN(Number(item)) ? item : '[]'))
169
- .join('.')
170
- const blueprint = blueprints.find(({ id }) => id === blueprintString)
171
- if (!blueprint) {
172
- throw new Error(`Unable to find blueprint at ${blueprintString}`)
173
- }
174
- return blueprint
175
- }
176
-
177
- // FIXME: this assumes children are in order (which they
178
- // are AFAIK but shouldn't be relied on)
179
- export const getBlueprintChildren = (
180
- blueprint: DocumentBlueprint,
181
- blueprints: DocumentBlueprint[]
182
- ) => {
183
- const foundChildren: DocumentBlueprint[] = []
184
- blueprints.forEach((otherBlueprint) => {
185
- if (foundChildren.find(({ id }) => otherBlueprint.id.startsWith(id))) {
186
- return
187
- }
188
- if (
189
- otherBlueprint.id.startsWith(blueprint.id) &&
190
- otherBlueprint.id !== blueprint.id
191
- ) {
192
- foundChildren.push(otherBlueprint)
193
- }
194
- })
195
- return foundChildren
196
- }