@tinacms/app 0.0.9 → 0.0.12
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.
- package/appFiles/index.dev.html +23 -0
- package/appFiles/lib/formify/formify-utils.ts +299 -0
- package/appFiles/lib/formify/formify.ts +574 -0
- package/appFiles/lib/formify/index.ts +67 -0
- package/appFiles/lib/formify/types.ts +130 -0
- package/appFiles/lib/formify/util.ts +598 -0
- package/appFiles/lib/machines/document-machine.ts +351 -0
- package/appFiles/lib/machines/query-machine.ts +728 -0
- package/appFiles/lib/machines/util.ts +205 -0
- package/appFiles/src/App.tsx +12 -3
- package/appFiles/src/index.css +3 -0
- package/appFiles/src/main.tsx +1 -0
- package/appFiles/src/preview/index.tsx +150 -0
- package/appFiles/src/vite-env.d.ts +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +332 -10
- package/package.json +24 -10
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import { DocumentBlueprint } from '../formify/types'
|
|
14
|
+
|
|
15
|
+
type Location = number[] | null
|
|
16
|
+
|
|
17
|
+
export const getBlueprintValues = (
|
|
18
|
+
data: object,
|
|
19
|
+
path: string,
|
|
20
|
+
index: number = 0
|
|
21
|
+
): string[] | undefined => {
|
|
22
|
+
if (!data) {
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
const pathArray = path.split('.')
|
|
26
|
+
const next = pathArray[index]
|
|
27
|
+
if (next === '[]') {
|
|
28
|
+
if (Array.isArray(data)) {
|
|
29
|
+
const values: string[] = []
|
|
30
|
+
data.forEach((item) => {
|
|
31
|
+
const res = getBlueprintValues(item, path, index + 1)
|
|
32
|
+
if (res) {
|
|
33
|
+
res.forEach((item) => {
|
|
34
|
+
values.push(item)
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
return values
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const value = data[next]
|
|
42
|
+
// Is last item
|
|
43
|
+
if (pathArray.length === index + 1) {
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
return value
|
|
46
|
+
} else {
|
|
47
|
+
return [value]
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
return getBlueprintValues(value, path)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const getAllIn = (
|
|
56
|
+
data: object | object[] | null,
|
|
57
|
+
path: string,
|
|
58
|
+
index: number = 0,
|
|
59
|
+
location: Location = null
|
|
60
|
+
): { value: { id: string }; location: Location }[] | undefined => {
|
|
61
|
+
if (!data) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
const pathArray = path.split('.')
|
|
65
|
+
const next = pathArray[index]
|
|
66
|
+
if (next === '[]') {
|
|
67
|
+
if (Array.isArray(data)) {
|
|
68
|
+
const values: { value: { id: string }; location: Location }[] = []
|
|
69
|
+
data.forEach((item, dataIndex) => {
|
|
70
|
+
const res = getAllIn(item, path, index + 1, [
|
|
71
|
+
...(location || []),
|
|
72
|
+
dataIndex,
|
|
73
|
+
])
|
|
74
|
+
if (res) {
|
|
75
|
+
res.forEach((item) => {
|
|
76
|
+
values.push(item)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
return values
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
const value = data[next]
|
|
84
|
+
// Is last item
|
|
85
|
+
if (pathArray.length === index + 1) {
|
|
86
|
+
if (Array.isArray(value)) {
|
|
87
|
+
const v = value.map((item, index) => ({
|
|
88
|
+
value: item,
|
|
89
|
+
location: [...(location || []), index],
|
|
90
|
+
}))
|
|
91
|
+
return v
|
|
92
|
+
} else {
|
|
93
|
+
if (typeof value === 'string') {
|
|
94
|
+
const v = [{ value: { id: value }, location }]
|
|
95
|
+
return v
|
|
96
|
+
} else {
|
|
97
|
+
// FIXME: this should always be `_internalSys.path` but we're not populating the
|
|
98
|
+
// data properly on resolveData yet. But even when we are, will need to keep that out
|
|
99
|
+
// of the user-provided payload (maybe not?)
|
|
100
|
+
const id = value?._internalSys?.path || value?.id
|
|
101
|
+
if (id) {
|
|
102
|
+
const v = [{ value: { id, ...value }, location }]
|
|
103
|
+
return v
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
const v = getAllIn(value, path, index + 1, location)
|
|
109
|
+
return v
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export const getAllInBlueprint = (
|
|
114
|
+
data: object | object[] | null,
|
|
115
|
+
path: string,
|
|
116
|
+
index: number = 0,
|
|
117
|
+
location: Location = null
|
|
118
|
+
):
|
|
119
|
+
| { value: { _internalSys: { path: string } }; location: Location }[]
|
|
120
|
+
| undefined => {
|
|
121
|
+
if (!data) {
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
const pathArray = path.split('.')
|
|
125
|
+
const next = pathArray[index]
|
|
126
|
+
if (next === '[]') {
|
|
127
|
+
if (Array.isArray(data)) {
|
|
128
|
+
const values: {
|
|
129
|
+
value: { _internalSys: { path: string } }
|
|
130
|
+
location: Location
|
|
131
|
+
}[] = []
|
|
132
|
+
data.forEach((item, dataIndex) => {
|
|
133
|
+
const res = getAllInBlueprint(item, path, index + 1, [
|
|
134
|
+
...(location || []),
|
|
135
|
+
dataIndex,
|
|
136
|
+
])
|
|
137
|
+
if (res) {
|
|
138
|
+
res.forEach((item) => {
|
|
139
|
+
values.push(item)
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
return values
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
const value = data[next]
|
|
147
|
+
// Is last item
|
|
148
|
+
if (pathArray.length === index + 1) {
|
|
149
|
+
if (Array.isArray(value)) {
|
|
150
|
+
throw new Error(`Unexpected array value for getAllInBlueprint`)
|
|
151
|
+
} else {
|
|
152
|
+
if (typeof value === 'string') {
|
|
153
|
+
throw new Error(`Unexpected string value for getAllInBlueprint`)
|
|
154
|
+
} else {
|
|
155
|
+
const id = value?._internalSys?.path || value?.id
|
|
156
|
+
if (id) {
|
|
157
|
+
const v = [
|
|
158
|
+
{ value: { _internalSys: value._internalSys }, location },
|
|
159
|
+
]
|
|
160
|
+
return v
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
const v = getAllInBlueprint(value, path, index + 1, location)
|
|
166
|
+
return v
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export const getBlueprintFromLocation = (
|
|
172
|
+
location: string,
|
|
173
|
+
blueprints: DocumentBlueprint[]
|
|
174
|
+
) => {
|
|
175
|
+
const blueprintString = location
|
|
176
|
+
.split('.')
|
|
177
|
+
.map((item) => (isNaN(Number(item)) ? item : '[]'))
|
|
178
|
+
.join('.')
|
|
179
|
+
const blueprint = blueprints.find(({ id }) => id === blueprintString)
|
|
180
|
+
if (!blueprint) {
|
|
181
|
+
throw new Error(`Unable to find blueprint at ${blueprintString}`)
|
|
182
|
+
}
|
|
183
|
+
return blueprint
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// FIXME: this assumes children are in order (which they
|
|
187
|
+
// are AFAIK but shouldn't be relied on)
|
|
188
|
+
export const getBlueprintChildren = (
|
|
189
|
+
blueprint: DocumentBlueprint,
|
|
190
|
+
blueprints: DocumentBlueprint[]
|
|
191
|
+
) => {
|
|
192
|
+
const foundChildren: DocumentBlueprint[] = []
|
|
193
|
+
blueprints.forEach((otherBlueprint) => {
|
|
194
|
+
if (foundChildren.find(({ id }) => otherBlueprint.id.startsWith(id))) {
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
if (
|
|
198
|
+
otherBlueprint.id.startsWith(blueprint.id) &&
|
|
199
|
+
otherBlueprint.id !== blueprint.id
|
|
200
|
+
) {
|
|
201
|
+
foundChildren.push(otherBlueprint)
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
return foundChildren
|
|
205
|
+
}
|
package/appFiles/src/App.tsx
CHANGED
|
@@ -12,17 +12,26 @@ limitations under the License.
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import React from 'react'
|
|
15
|
-
import TinaCMS, { TinaAdmin } from 'tinacms'
|
|
15
|
+
import TinaCMS, { TinaAdmin, useCMS } from 'tinacms'
|
|
16
16
|
import { TinaEditProvider, useEditState } from 'tinacms/dist/edit-state'
|
|
17
|
+
import { Preview } from './preview'
|
|
17
18
|
|
|
18
19
|
// TODO: Resolve this to local file in tsconfig.json
|
|
19
20
|
// @ts-expect-error
|
|
20
21
|
import config from 'TINA_IMPORT'
|
|
21
22
|
|
|
23
|
+
const SetPreview = ({ outputFolder }: { outputFolder: string }) => {
|
|
24
|
+
const cms = useCMS()
|
|
25
|
+
cms.flags.set('tina-preview', outputFolder)
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
export const TinaAdminWrapper = () => {
|
|
23
30
|
return (
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
// @ts-ignore JSX element type 'TinaCMS' does not have any construct or call signatures.ts(2604)
|
|
32
|
+
<TinaCMS {...config} client={{ apiUrl: __API_URL__ }}>
|
|
33
|
+
<SetPreview outputFolder={config.build.outputFolder} />
|
|
34
|
+
<TinaAdmin preview={<Preview {...config} />} />
|
|
26
35
|
</TinaCMS>
|
|
27
36
|
)
|
|
28
37
|
}
|
package/appFiles/src/main.tsx
CHANGED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import React from 'react'
|
|
14
|
+
import { useMachine } from '@xstate/react'
|
|
15
|
+
import { queryMachine, initialContext } from '../../lib/machines/query-machine'
|
|
16
|
+
import { ChevronRightIcon, textFieldClasses, useCMS } from 'tinacms'
|
|
17
|
+
import { ArrowRightIcon, ChevronLeftIcon } from '@heroicons/react/outline'
|
|
18
|
+
import { useSearchParams } from 'react-router-dom'
|
|
19
|
+
|
|
20
|
+
export const Preview = (props) => {
|
|
21
|
+
const cms = useCMS()
|
|
22
|
+
const [searchParams, setSearchParams] = useSearchParams()
|
|
23
|
+
React.useEffect(() => {
|
|
24
|
+
// We'll often land here with the preview route in the URL (eg. ?iframe-url=/posts)
|
|
25
|
+
setSearchParams({})
|
|
26
|
+
}, [])
|
|
27
|
+
const machine = React.useMemo(
|
|
28
|
+
() =>
|
|
29
|
+
queryMachine.withContext({
|
|
30
|
+
...initialContext,
|
|
31
|
+
url:
|
|
32
|
+
searchParams.get('iframe-url') ||
|
|
33
|
+
localStorage.getItem('tina-url') ||
|
|
34
|
+
'/',
|
|
35
|
+
cms,
|
|
36
|
+
formifyCallback: props.formifyCallback,
|
|
37
|
+
}),
|
|
38
|
+
[]
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
const [state, send] = useMachine(machine)
|
|
42
|
+
const ref = React.useRef<HTMLIFrameElement>(null)
|
|
43
|
+
|
|
44
|
+
React.useEffect(() => {
|
|
45
|
+
if (ref.current) {
|
|
46
|
+
send({ type: 'IFRAME_MOUNTED', value: ref.current })
|
|
47
|
+
window.addEventListener('message', (event) => {
|
|
48
|
+
console.log('parent: event received', event.data.type)
|
|
49
|
+
if (event.data.type === 'open') {
|
|
50
|
+
send({ type: 'ADD_QUERY', value: event.data })
|
|
51
|
+
}
|
|
52
|
+
if (event.data.type === 'close') {
|
|
53
|
+
send({ type: 'REMOVE_QUERY', value: event.data.id })
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}, [ref.current])
|
|
58
|
+
React.useEffect(() => {
|
|
59
|
+
setInterval(() => {
|
|
60
|
+
if (ref.current) {
|
|
61
|
+
const href = ref.current.contentWindow?.location.pathname
|
|
62
|
+
if (href) {
|
|
63
|
+
if (href === 'blank') return
|
|
64
|
+
|
|
65
|
+
send({
|
|
66
|
+
type: 'SET_DISPLAY_URL',
|
|
67
|
+
value: href,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}, 300)
|
|
72
|
+
}, [ref.current])
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div className="h-full overflow-scroll">
|
|
76
|
+
<div className="">
|
|
77
|
+
<div className="col-span-5 ">
|
|
78
|
+
<div className="h-screen flex flex-col">
|
|
79
|
+
<div className="col-span-2 bg-gray-50 border-b border-gray-200">
|
|
80
|
+
<div className="px-16 py-3 flex gap-3">
|
|
81
|
+
<div className="relative flex-1">
|
|
82
|
+
<form
|
|
83
|
+
onSubmit={(e) => {
|
|
84
|
+
e.preventDefault()
|
|
85
|
+
send({ type: 'UPDATE_URL' })
|
|
86
|
+
}}
|
|
87
|
+
>
|
|
88
|
+
<input
|
|
89
|
+
type="text"
|
|
90
|
+
style={{ borderRadius: '50px' }}
|
|
91
|
+
className={`${textFieldClasses}`}
|
|
92
|
+
onChange={(e) =>
|
|
93
|
+
send({ type: 'SET_INPUT_URL', value: e.target.value })
|
|
94
|
+
}
|
|
95
|
+
value={
|
|
96
|
+
state.context.inputURL !== null
|
|
97
|
+
? state.context.inputURL
|
|
98
|
+
: state.context.displayURL || state.context.url
|
|
99
|
+
}
|
|
100
|
+
/>
|
|
101
|
+
<div className="absolute right-2 top-0 bottom-0 flex items-center">
|
|
102
|
+
<button
|
|
103
|
+
className={`relative inline-flex items-center px-2 py-2 rounded-full border text-sm font-medium focus:z-10 focus:outline-none focus:ring-1 ${
|
|
104
|
+
state.context.inputURL
|
|
105
|
+
? 'border-blue-300 bg-blue-500 text-white hover:bg-blue-400'
|
|
106
|
+
: 'border-gray-300 bg-white text-gray-500 hover:bg-gray-50'
|
|
107
|
+
} focus:ring-blue-500 focus:border-blue-500`}
|
|
108
|
+
>
|
|
109
|
+
<span className="sr-only">Go</span>
|
|
110
|
+
<ArrowRightIcon
|
|
111
|
+
className="h-3 w-3"
|
|
112
|
+
aria-hidden="true"
|
|
113
|
+
/>
|
|
114
|
+
</button>
|
|
115
|
+
</div>
|
|
116
|
+
</form>
|
|
117
|
+
</div>
|
|
118
|
+
<span className="relative z-0 inline-flex shadow-sm rounded-md">
|
|
119
|
+
<button
|
|
120
|
+
type="button"
|
|
121
|
+
onClick={() => history.back()}
|
|
122
|
+
className="relative inline-flex items-center px-2 py-2 rounded-l-full border border-gray-200 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500"
|
|
123
|
+
>
|
|
124
|
+
<span className="sr-only">Previous</span>
|
|
125
|
+
<ChevronLeftIcon className="h-5 w-5" aria-hidden="true" />
|
|
126
|
+
</button>
|
|
127
|
+
<button
|
|
128
|
+
type="button"
|
|
129
|
+
onClick={() => history.forward()}
|
|
130
|
+
className="-ml-px relative inline-flex items-center px-2 py-2 rounded-r-full border border-gray-200 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500"
|
|
131
|
+
>
|
|
132
|
+
<span className="sr-only">Next</span>
|
|
133
|
+
<ChevronRightIcon className="h-5 w-5" aria-hidden="true" />
|
|
134
|
+
</button>
|
|
135
|
+
</span>
|
|
136
|
+
</div>
|
|
137
|
+
</div>
|
|
138
|
+
<div className="relative flex-1 bg-gray-300 col-span-2 overflow-scroll flex items-center justify-center">
|
|
139
|
+
<iframe
|
|
140
|
+
ref={ref}
|
|
141
|
+
className="h-full w-full bg-white"
|
|
142
|
+
src={state.context.url}
|
|
143
|
+
/>
|
|
144
|
+
</div>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
</div>
|
|
148
|
+
</div>
|
|
149
|
+
)
|
|
150
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare const viteBuild: (args: any) => any
|