@tinacms/app 0.0.0-942e18f-20250102010803 → 0.0.0-96fe3b4-20251217041317
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/CHANGELOG.md +274 -11
- package/index.html +1 -1
- package/package.json +17 -9
- package/src/App.tsx +24 -24
- package/src/Playground.tsx +56 -58
- package/src/dummy-client.ts +1 -1
- package/src/fields/rich-text/index.tsx +2 -2
- package/src/fields/rich-text/monaco/error-message.tsx +59 -59
- package/src/fields/rich-text/monaco/index.tsx +68 -74
- package/src/fields/rich-text/monaco/use-debounce.ts +8 -8
- package/src/global.css +3 -3
- package/src/index.css +24 -15
- package/src/lib/build-form.ts +24 -24
- package/src/lib/errors.tsx +7 -7
- package/src/lib/expand-query.ts +74 -69
- package/src/lib/graphql-reducer.ts +301 -276
- package/src/lib/types.ts +35 -33
- package/src/lib/util.ts +48 -53
- package/src/main.tsx +7 -7
- package/src/preflight.css +10 -10
- package/src/preview.tsx +12 -12
- package/src/vite-env.d.ts +3 -3
package/src/Playground.tsx
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { createGraphiQLFetcher } from '@graphiql/toolkit'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
1
|
+
import { createGraphiQLFetcher } from '@graphiql/toolkit';
|
|
2
|
+
import { FolderIcon } from '@heroicons/react/outline';
|
|
3
|
+
import { queries } from 'CLIENT_IMPORT';
|
|
4
|
+
import { GraphiQL } from 'graphiql';
|
|
5
|
+
import { parse, print } from 'graphql';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import { useCMS } from 'tinacms';
|
|
8
8
|
|
|
9
|
-
import 'graphiql/graphiql.min.css'
|
|
9
|
+
import 'graphiql/graphiql.min.css';
|
|
10
10
|
|
|
11
11
|
const Playground = () => {
|
|
12
|
-
const cms = useCMS()
|
|
13
|
-
const [query, setQuery] = React.useState('')
|
|
14
|
-
const [variables, setVariables] = React.useState('')
|
|
15
|
-
const [autoQueries, setAutoQueries] = React.useState()
|
|
12
|
+
const cms = useCMS();
|
|
13
|
+
const [query, setQuery] = React.useState('');
|
|
14
|
+
const [variables, setVariables] = React.useState('');
|
|
15
|
+
const [autoQueries, setAutoQueries] = React.useState();
|
|
16
16
|
const [collectionInfo, setCollectionInfo] = React.useState<{
|
|
17
17
|
collections: {
|
|
18
|
-
name: string
|
|
19
|
-
documents: { edges: { node: { _sys: { relativePath: string } } }[] }
|
|
20
|
-
}[]
|
|
21
|
-
}>()
|
|
18
|
+
name: string;
|
|
19
|
+
documents: { edges: { node: { _sys: { relativePath: string } } }[] };
|
|
20
|
+
}[];
|
|
21
|
+
}>();
|
|
22
22
|
React.useEffect(() => {
|
|
23
23
|
const run = async () => {
|
|
24
24
|
if (queries) {
|
|
25
|
-
const q = queries({ request: async () => query })
|
|
25
|
+
const q = queries({ request: async () => query });
|
|
26
26
|
setCollectionInfo(
|
|
27
27
|
await cms.api.tina.request(
|
|
28
28
|
`
|
|
@@ -45,52 +45,50 @@ const Playground = () => {
|
|
|
45
45
|
`,
|
|
46
46
|
{ variables: {} }
|
|
47
47
|
)
|
|
48
|
-
)
|
|
49
|
-
setAutoQueries(q)
|
|
48
|
+
);
|
|
49
|
+
setAutoQueries(q);
|
|
50
50
|
} else {
|
|
51
|
-
setAutoQueries({})
|
|
51
|
+
setAutoQueries({});
|
|
52
52
|
}
|
|
53
|
-
}
|
|
54
|
-
run()
|
|
55
|
-
}, [])
|
|
53
|
+
};
|
|
54
|
+
run();
|
|
55
|
+
}, []);
|
|
56
56
|
|
|
57
|
-
const ref = React.useRef()
|
|
57
|
+
const ref = React.useRef();
|
|
58
58
|
|
|
59
59
|
const getToken = () => {
|
|
60
|
-
return JSON.parse(localStorage.getItem('tinacms-auth') || '{}')?.id_token
|
|
61
|
-
}
|
|
60
|
+
return JSON.parse(localStorage.getItem('tinacms-auth') || '{}')?.id_token;
|
|
61
|
+
};
|
|
62
62
|
|
|
63
63
|
if (!autoQueries) {
|
|
64
|
-
return null
|
|
64
|
+
return null;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
const Plugin = () => {
|
|
68
|
-
const noAutoQueries = Object.keys(autoQueries).length === 0
|
|
68
|
+
const noAutoQueries = Object.keys(autoQueries).length === 0;
|
|
69
69
|
return (
|
|
70
70
|
<div>
|
|
71
|
-
<div className=
|
|
72
|
-
<div className=
|
|
73
|
-
<div className=
|
|
71
|
+
<div className='graphiql-doc-explorer-title'>Queries</div>
|
|
72
|
+
<div className='graphiql-doc-explorer-content'>
|
|
73
|
+
<div className='graphiql-markdown-description'>
|
|
74
74
|
{noAutoQueries
|
|
75
75
|
? 'No auto-generated queries found, the Tina config is likely set to client.skip = true'
|
|
76
76
|
: "Tina's auto-generated queries can be found here as well as any queries you may have defined yourself."}{' '}
|
|
77
|
-
<a href=
|
|
78
|
-
Learn more here
|
|
79
|
-
</a>
|
|
77
|
+
<a href='https://tina.io/docs/r/custom-queries/'>Learn more here</a>
|
|
80
78
|
</div>
|
|
81
|
-
<nav className=
|
|
79
|
+
<nav className='space-y-1' aria-label='Sidebar'>
|
|
82
80
|
<ul>
|
|
83
81
|
{Object.entries(autoQueries).map(([key, value]) => {
|
|
84
82
|
const collection = collectionInfo?.collections.find(
|
|
85
83
|
({ name }) => name === key
|
|
86
|
-
)
|
|
87
|
-
let variables = ''
|
|
88
|
-
let relativePath = ''
|
|
84
|
+
);
|
|
85
|
+
let variables = '';
|
|
86
|
+
let relativePath = '';
|
|
89
87
|
if (collection) {
|
|
90
88
|
relativePath =
|
|
91
89
|
collection?.documents?.edges[0]?.node?._sys.relativePath ||
|
|
92
|
-
''
|
|
93
|
-
variables = JSON.stringify({ relativePath }, null, 2)
|
|
90
|
+
'';
|
|
91
|
+
variables = JSON.stringify({ relativePath }, null, 2);
|
|
94
92
|
}
|
|
95
93
|
return (
|
|
96
94
|
<li>
|
|
@@ -100,47 +98,47 @@ const Playground = () => {
|
|
|
100
98
|
false
|
|
101
99
|
? 'bg-gray-100 text-gray-900'
|
|
102
100
|
: 'text-gray-600 hover:bg-gray-50 hover:text-gray-900',
|
|
103
|
-
'flex items-center rounded
|
|
101
|
+
'flex items-center rounded px-3 py-2 text-sm font-medium w-full text-left'
|
|
104
102
|
)}
|
|
105
103
|
onClick={async () => {
|
|
106
104
|
if (typeof value === 'function') {
|
|
107
|
-
const v = await value({})
|
|
108
|
-
const ast = parse(v.query)
|
|
109
|
-
setVariables(variables)
|
|
110
|
-
setQuery(print(ast))
|
|
105
|
+
const v = await value({});
|
|
106
|
+
const ast = parse(v.query);
|
|
107
|
+
setVariables(variables);
|
|
108
|
+
setQuery(print(ast));
|
|
111
109
|
}
|
|
112
110
|
}}
|
|
113
111
|
>
|
|
114
|
-
<span className=
|
|
112
|
+
<span className='truncate'>
|
|
115
113
|
{key}{' '}
|
|
116
114
|
{relativePath && (
|
|
117
|
-
<span className=
|
|
115
|
+
<span className='pl-2 text-sm text-gray-300'>
|
|
118
116
|
({relativePath})
|
|
119
117
|
</span>
|
|
120
118
|
)}{' '}
|
|
121
119
|
</span>
|
|
122
120
|
</button>
|
|
123
121
|
</li>
|
|
124
|
-
)
|
|
122
|
+
);
|
|
125
123
|
})}
|
|
126
124
|
</ul>
|
|
127
125
|
</nav>
|
|
128
126
|
</div>
|
|
129
127
|
</div>
|
|
130
|
-
)
|
|
131
|
-
}
|
|
128
|
+
);
|
|
129
|
+
};
|
|
132
130
|
return (
|
|
133
131
|
<div style={{ height: '100vh' }}>
|
|
134
132
|
<GraphiQL
|
|
135
133
|
fetcher={async (params, options) => {
|
|
136
134
|
const fetcher = createGraphiQLFetcher({
|
|
137
|
-
url: __API_URL__,
|
|
135
|
+
url: cms.api.tina.contentApiUrl || __API_URL__,
|
|
138
136
|
headers: { Authorization: `Bearer ${getToken()}` },
|
|
139
|
-
})
|
|
140
|
-
return fetcher(params, options)
|
|
137
|
+
});
|
|
138
|
+
return fetcher(params, options);
|
|
141
139
|
}}
|
|
142
140
|
query={query}
|
|
143
|
-
defaultEditorToolsVisibility=
|
|
141
|
+
defaultEditorToolsVisibility='variables'
|
|
144
142
|
isHeadersEditorEnabled={false}
|
|
145
143
|
defaultTabs={[]}
|
|
146
144
|
plugins={[
|
|
@@ -153,11 +151,11 @@ const Playground = () => {
|
|
|
153
151
|
variables={variables}
|
|
154
152
|
></GraphiQL>
|
|
155
153
|
</div>
|
|
156
|
-
)
|
|
157
|
-
}
|
|
154
|
+
);
|
|
155
|
+
};
|
|
158
156
|
|
|
159
|
-
export default Playground
|
|
157
|
+
export default Playground;
|
|
160
158
|
|
|
161
159
|
function classNames(...classes: string[]) {
|
|
162
|
-
return classes.filter(Boolean).join(' ')
|
|
160
|
+
return classes.filter(Boolean).join(' ');
|
|
163
161
|
}
|
package/src/dummy-client.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const queries = false
|
|
1
|
+
export const queries = false;
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
import React from 'react'
|
|
7
|
-
import { XCircleIcon } from '@heroicons/react/solid'
|
|
8
1
|
import {
|
|
9
2
|
Popover,
|
|
10
3
|
PopoverButton,
|
|
11
4
|
PopoverPanel,
|
|
12
5
|
Transition,
|
|
13
|
-
} from '@headlessui/react'
|
|
14
|
-
import {
|
|
6
|
+
} from '@headlessui/react';
|
|
7
|
+
import { XCircleIcon } from '@heroicons/react/solid';
|
|
8
|
+
/**
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
*/
|
|
13
|
+
import React from 'react';
|
|
14
|
+
import { Fragment } from 'react';
|
|
15
15
|
// import { InvalidMarkdownElement } from '@tinacms/mdx/src/parse/plate'
|
|
16
|
-
export type EmptyTextElement = { type: 'text'; text: '' }
|
|
16
|
+
export type EmptyTextElement = { type: 'text'; text: '' };
|
|
17
17
|
export type PositionItem = {
|
|
18
|
-
line?: number | null
|
|
19
|
-
column?: number | null
|
|
20
|
-
offset?: number | null
|
|
21
|
-
_index?: number | null
|
|
22
|
-
_bufferIndex?: number | null
|
|
23
|
-
}
|
|
18
|
+
line?: number | null;
|
|
19
|
+
column?: number | null;
|
|
20
|
+
offset?: number | null;
|
|
21
|
+
_index?: number | null;
|
|
22
|
+
_bufferIndex?: number | null;
|
|
23
|
+
};
|
|
24
24
|
export type Position = {
|
|
25
|
-
start: PositionItem
|
|
26
|
-
end: PositionItem
|
|
27
|
-
}
|
|
25
|
+
start: PositionItem;
|
|
26
|
+
end: PositionItem;
|
|
27
|
+
};
|
|
28
28
|
export type InvalidMarkdownElement = {
|
|
29
|
-
type: 'invalid_markdown'
|
|
30
|
-
value: string
|
|
31
|
-
message: string
|
|
32
|
-
position?: Position
|
|
33
|
-
children: [EmptyTextElement]
|
|
34
|
-
}
|
|
29
|
+
type: 'invalid_markdown';
|
|
30
|
+
value: string;
|
|
31
|
+
message: string;
|
|
32
|
+
position?: Position;
|
|
33
|
+
children: [EmptyTextElement];
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
type ErrorType = {
|
|
37
|
-
message: string
|
|
37
|
+
message: string;
|
|
38
38
|
position?: {
|
|
39
|
-
startColumn: number
|
|
40
|
-
endColumn: number
|
|
41
|
-
startLineNumber: number
|
|
42
|
-
endLineNumber: number
|
|
43
|
-
}
|
|
44
|
-
}
|
|
39
|
+
startColumn: number;
|
|
40
|
+
endColumn: number;
|
|
41
|
+
startLineNumber: number;
|
|
42
|
+
endLineNumber: number;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
45
|
export const buildError = (element: InvalidMarkdownElement): ErrorType => {
|
|
46
46
|
return {
|
|
47
47
|
message: element.message,
|
|
@@ -51,28 +51,28 @@ export const buildError = (element: InvalidMarkdownElement): ErrorType => {
|
|
|
51
51
|
startLineNumber: element.position.start.line,
|
|
52
52
|
endLineNumber: element.position.end.line,
|
|
53
53
|
},
|
|
54
|
-
}
|
|
55
|
-
}
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
56
|
export const buildErrorMessage = (element: InvalidMarkdownElement): string => {
|
|
57
57
|
if (!element) {
|
|
58
|
-
return ''
|
|
58
|
+
return '';
|
|
59
59
|
}
|
|
60
|
-
const errorMessage = buildError(element)
|
|
60
|
+
const errorMessage = buildError(element);
|
|
61
61
|
const message = errorMessage
|
|
62
62
|
? `${errorMessage.message}${
|
|
63
63
|
errorMessage.position
|
|
64
64
|
? ` at line: ${errorMessage.position.startLineNumber}, column: ${errorMessage.position.startColumn}`
|
|
65
65
|
: ''
|
|
66
66
|
}`
|
|
67
|
-
: null
|
|
68
|
-
return message
|
|
69
|
-
}
|
|
67
|
+
: null;
|
|
68
|
+
return message;
|
|
69
|
+
};
|
|
70
70
|
|
|
71
71
|
export function ErrorMessage({ error }: { error: InvalidMarkdownElement }) {
|
|
72
|
-
const message = buildErrorMessage(error)
|
|
72
|
+
const message = buildErrorMessage(error);
|
|
73
73
|
|
|
74
74
|
return (
|
|
75
|
-
<Popover className=
|
|
75
|
+
<Popover className='relative'>
|
|
76
76
|
{() => (
|
|
77
77
|
<>
|
|
78
78
|
<PopoverButton
|
|
@@ -80,29 +80,29 @@ export function ErrorMessage({ error }: { error: InvalidMarkdownElement }) {
|
|
|
80
80
|
error ? '' : ' opacity-0 hidden '
|
|
81
81
|
}`}
|
|
82
82
|
>
|
|
83
|
-
<span className=
|
|
84
|
-
<XCircleIcon className=
|
|
83
|
+
<span className='sr-only'>Errors</span>
|
|
84
|
+
<XCircleIcon className='h-5 w-5 text-red-400' aria-hidden='true' />
|
|
85
85
|
</PopoverButton>
|
|
86
86
|
<Transition
|
|
87
|
-
enter=
|
|
88
|
-
enterFrom=
|
|
89
|
-
enterTo=
|
|
90
|
-
leave=
|
|
91
|
-
leaveFrom=
|
|
92
|
-
leaveTo=
|
|
87
|
+
enter='transition ease-out duration-200'
|
|
88
|
+
enterFrom='opacity-0 translate-y-1'
|
|
89
|
+
enterTo='opacity-100 translate-y-0'
|
|
90
|
+
leave='transition ease-in duration-150'
|
|
91
|
+
leaveFrom='opacity-100 translate-y-0'
|
|
92
|
+
leaveTo='opacity-0 translate-y-1'
|
|
93
93
|
>
|
|
94
|
-
<PopoverPanel className=
|
|
95
|
-
<div className=
|
|
96
|
-
<div className=
|
|
97
|
-
<div className=
|
|
98
|
-
<div className=
|
|
94
|
+
<PopoverPanel className='absolute top-8 w-[300px] -right-3 z-10 mt-3 px-4 sm:px-0'>
|
|
95
|
+
<div className='overflow-hidden rounded-lg shadow-lg ring-1 ring-black ring-opacity-5'>
|
|
96
|
+
<div className='rounded bg-red-50 p-4 overflow-scroll'>
|
|
97
|
+
<div className='flex'>
|
|
98
|
+
<div className='flex-shrink-0'>
|
|
99
99
|
<XCircleIcon
|
|
100
|
-
className=
|
|
101
|
-
aria-hidden=
|
|
100
|
+
className='h-5 w-5 text-red-400'
|
|
101
|
+
aria-hidden='true'
|
|
102
102
|
/>
|
|
103
103
|
</div>
|
|
104
|
-
<div className=
|
|
105
|
-
<h3 className=
|
|
104
|
+
<div className='ml-3'>
|
|
105
|
+
<h3 className='text-sm font-medium text-red-800 whitespace-pre-wrap'>
|
|
106
106
|
{message}
|
|
107
107
|
</h3>
|
|
108
108
|
</div>
|
|
@@ -114,5 +114,5 @@ export function ErrorMessage({ error }: { error: InvalidMarkdownElement }) {
|
|
|
114
114
|
</>
|
|
115
115
|
)}
|
|
116
116
|
</Popover>
|
|
117
|
-
)
|
|
117
|
+
);
|
|
118
118
|
}
|
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import React from 'react'
|
|
8
|
-
import MonacoEditor, { useMonaco, loader } from '@monaco-editor/react'
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import MonacoEditor, { useMonaco, loader } from '@monaco-editor/react';
|
|
9
3
|
/**
|
|
10
4
|
* MDX is built directly to the app because of how we load dependencies.
|
|
11
5
|
* Since we drop the package.json in to the end users folder, we can't
|
|
12
6
|
* easily install the current version of the mdx package in all scenarios
|
|
13
7
|
* (when we're working in the monorepo, or working with a tagged npm version)
|
|
14
8
|
*/
|
|
15
|
-
import { parseMDX,
|
|
16
|
-
import
|
|
17
|
-
import
|
|
9
|
+
import { parseMDX, serializeMDX } from '@tinacms/mdx';
|
|
10
|
+
import type * as monaco from 'monaco-editor';
|
|
11
|
+
import { RichTextType } from 'tinacms';
|
|
18
12
|
import {
|
|
19
|
-
buildError,
|
|
20
13
|
ErrorMessage,
|
|
21
14
|
InvalidMarkdownElement,
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
buildError,
|
|
16
|
+
} from './error-message';
|
|
17
|
+
import { useDebounce } from './use-debounce';
|
|
24
18
|
|
|
25
19
|
export const uuid = () => {
|
|
26
20
|
// @ts-ignore
|
|
@@ -29,15 +23,10 @@ export const uuid = () => {
|
|
|
29
23
|
c ^
|
|
30
24
|
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
|
|
31
25
|
).toString(16)
|
|
32
|
-
)
|
|
33
|
-
}
|
|
26
|
+
);
|
|
27
|
+
};
|
|
34
28
|
|
|
35
|
-
type Monaco = typeof monaco
|
|
36
|
-
|
|
37
|
-
// 0.33.0 has a bug https://github.com/microsoft/monaco-editor/issues/2947
|
|
38
|
-
loader.config({
|
|
39
|
-
paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.31.1/min/vs' },
|
|
40
|
-
})
|
|
29
|
+
type Monaco = typeof monaco;
|
|
41
30
|
|
|
42
31
|
/**
|
|
43
32
|
* Since monaco lazy-loads we may have a delay from when the block is inserted
|
|
@@ -45,81 +34,81 @@ loader.config({
|
|
|
45
34
|
*
|
|
46
35
|
* Will try for 3 seconds before moving on
|
|
47
36
|
*/
|
|
48
|
-
let retryCount = 0
|
|
37
|
+
let retryCount = 0;
|
|
49
38
|
const retryFocus = (ref) => {
|
|
50
39
|
if (ref.current) {
|
|
51
|
-
ref.current.focus()
|
|
40
|
+
ref.current.focus();
|
|
52
41
|
} else {
|
|
53
42
|
if (retryCount < 30) {
|
|
54
43
|
setTimeout(() => {
|
|
55
|
-
retryCount = retryCount + 1
|
|
56
|
-
retryFocus(ref)
|
|
57
|
-
}, 100)
|
|
44
|
+
retryCount = retryCount + 1;
|
|
45
|
+
retryFocus(ref);
|
|
46
|
+
}, 100);
|
|
58
47
|
}
|
|
59
48
|
}
|
|
60
|
-
}
|
|
49
|
+
};
|
|
61
50
|
|
|
62
51
|
export const RawEditor = (props: RichTextType) => {
|
|
63
|
-
const monaco = useMonaco() as Monaco
|
|
52
|
+
const monaco = useMonaco() as Monaco;
|
|
64
53
|
const monacoEditorRef =
|
|
65
|
-
React.useRef<monaco.editor.IStandaloneCodeEditor>(null)
|
|
66
|
-
const [height, setHeight] = React.useState(100)
|
|
67
|
-
const id = React.useMemo(() => uuid(), [])
|
|
68
|
-
const field = props.field
|
|
54
|
+
React.useRef<monaco.editor.IStandaloneCodeEditor>(null);
|
|
55
|
+
const [height, setHeight] = React.useState(100);
|
|
56
|
+
const id = React.useMemo(() => uuid(), []);
|
|
57
|
+
const field = props.field;
|
|
69
58
|
const inputValue = React.useMemo(() => {
|
|
70
59
|
// @ts-ignore no access to the rich-text type from this package
|
|
71
|
-
const res =
|
|
72
|
-
return typeof props.input.value === 'string' ? props.input.value : res
|
|
73
|
-
}, [])
|
|
74
|
-
const [value, setValue] = React.useState(inputValue)
|
|
75
|
-
const [error, setError] = React.useState<InvalidMarkdownElement>(null)
|
|
60
|
+
const res = serializeMDX(props.input.value, field, (value) => value);
|
|
61
|
+
return typeof props.input.value === 'string' ? props.input.value : res;
|
|
62
|
+
}, []);
|
|
63
|
+
const [value, setValue] = React.useState(inputValue);
|
|
64
|
+
const [error, setError] = React.useState<InvalidMarkdownElement>(null);
|
|
76
65
|
|
|
77
|
-
const debouncedValue = useDebounce(value, 500)
|
|
66
|
+
const debouncedValue = useDebounce(value, 500);
|
|
78
67
|
|
|
79
68
|
React.useEffect(() => {
|
|
80
69
|
// @ts-ignore no access to the rich-text type from this package
|
|
81
|
-
const parsedValue = parseMDX(value, field, (value) => value)
|
|
70
|
+
const parsedValue = parseMDX(value, field, (value) => value);
|
|
82
71
|
if (
|
|
83
72
|
parsedValue.children[0] &&
|
|
84
73
|
parsedValue.children[0].type === 'invalid_markdown'
|
|
85
74
|
) {
|
|
86
|
-
const invalidMarkdown = parsedValue.children[0]
|
|
87
|
-
setError(invalidMarkdown)
|
|
75
|
+
const invalidMarkdown = parsedValue.children[0];
|
|
76
|
+
setError(invalidMarkdown);
|
|
88
77
|
} else {
|
|
89
|
-
setError(null)
|
|
78
|
+
setError(null);
|
|
90
79
|
}
|
|
91
|
-
props.input.onChange(parsedValue)
|
|
92
|
-
}, [JSON.stringify(debouncedValue)])
|
|
80
|
+
props.input.onChange(parsedValue);
|
|
81
|
+
}, [JSON.stringify(debouncedValue)]);
|
|
93
82
|
|
|
94
83
|
React.useEffect(() => {
|
|
95
84
|
if (monacoEditorRef.current) {
|
|
96
85
|
if (error) {
|
|
97
|
-
const errorMessage = buildError(error)
|
|
86
|
+
const errorMessage = buildError(error);
|
|
98
87
|
monaco.editor.setModelMarkers(monacoEditorRef.current.getModel(), id, [
|
|
99
88
|
{
|
|
100
89
|
...errorMessage.position,
|
|
101
90
|
message: errorMessage.message,
|
|
102
91
|
severity: 8,
|
|
103
92
|
},
|
|
104
|
-
])
|
|
93
|
+
]);
|
|
105
94
|
} else {
|
|
106
95
|
monaco.editor.setModelMarkers(
|
|
107
96
|
monacoEditorRef.current.getModel(),
|
|
108
97
|
id,
|
|
109
98
|
[]
|
|
110
|
-
)
|
|
99
|
+
);
|
|
111
100
|
}
|
|
112
101
|
}
|
|
113
|
-
}, [JSON.stringify(error), monacoEditorRef.current])
|
|
102
|
+
}, [JSON.stringify(error), monacoEditorRef.current]);
|
|
114
103
|
|
|
115
104
|
React.useEffect(() => {
|
|
116
105
|
if (monaco) {
|
|
117
|
-
monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true)
|
|
106
|
+
monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);
|
|
118
107
|
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
|
|
119
108
|
// disable errors
|
|
120
109
|
noSemanticValidation: true,
|
|
121
110
|
noSyntaxValidation: true,
|
|
122
|
-
})
|
|
111
|
+
});
|
|
123
112
|
// TODO: autocomplete suggestions
|
|
124
113
|
// monaco.languages.registerCompletionItemProvider('markdown', {
|
|
125
114
|
// provideCompletionItems: function (model, position) {
|
|
@@ -143,24 +132,28 @@ export const RawEditor = (props: RichTextType) => {
|
|
|
143
132
|
// },
|
|
144
133
|
// })
|
|
145
134
|
}
|
|
146
|
-
}, [monaco])
|
|
135
|
+
}, [monaco]);
|
|
147
136
|
|
|
148
137
|
function handleEditorDidMount(
|
|
149
138
|
monacoEditor: monaco.editor.IStandaloneCodeEditor,
|
|
150
139
|
monaco: Monaco
|
|
151
140
|
) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
141
|
+
if (monacoEditor) {
|
|
142
|
+
monacoEditorRef.current = monacoEditor;
|
|
143
|
+
monacoEditor.onDidContentSizeChange(() => {
|
|
144
|
+
// FIXME: if the window is too tall the performance degrades, come up with a nice
|
|
145
|
+
// balance between the two
|
|
146
|
+
setHeight(
|
|
147
|
+
Math.min(Math.max(100, monacoEditor.getContentHeight()), 1000)
|
|
148
|
+
);
|
|
149
|
+
monacoEditor.layout();
|
|
150
|
+
});
|
|
151
|
+
}
|
|
159
152
|
}
|
|
160
153
|
|
|
161
154
|
return (
|
|
162
|
-
<div className=
|
|
163
|
-
<div className=
|
|
155
|
+
<div className='relative'>
|
|
156
|
+
<div className='sticky top-1 w-full flex justify-between mb-2 z-50 max-w-full bg-white'>
|
|
164
157
|
<Button onClick={() => props.setRawMode(false)}>
|
|
165
158
|
View in rich-text editor 📝
|
|
166
159
|
</Button>
|
|
@@ -168,6 +161,9 @@ export const RawEditor = (props: RichTextType) => {
|
|
|
168
161
|
</div>
|
|
169
162
|
<div style={{ height: `${height}px` }}>
|
|
170
163
|
<MonacoEditor
|
|
164
|
+
beforeMount={() => {}}
|
|
165
|
+
height='100%'
|
|
166
|
+
width='100%'
|
|
171
167
|
path={id}
|
|
172
168
|
onMount={handleEditorDidMount}
|
|
173
169
|
// Setting a custom theme is kind of buggy because it doesn't get defined until monaco has mounted.
|
|
@@ -204,33 +200,31 @@ export const RawEditor = (props: RichTextType) => {
|
|
|
204
200
|
value={value}
|
|
205
201
|
onChange={(value) => {
|
|
206
202
|
try {
|
|
207
|
-
setValue(value)
|
|
203
|
+
setValue(value);
|
|
208
204
|
} catch (e) {
|
|
209
|
-
console.log('error', e)
|
|
205
|
+
console.log('error', e);
|
|
210
206
|
}
|
|
211
207
|
}}
|
|
212
208
|
/>
|
|
213
209
|
</div>
|
|
214
210
|
</div>
|
|
215
|
-
)
|
|
216
|
-
}
|
|
211
|
+
);
|
|
212
|
+
};
|
|
217
213
|
|
|
218
214
|
const Button = (props) => {
|
|
219
215
|
return (
|
|
220
216
|
<button
|
|
221
217
|
className={`${
|
|
222
|
-
props.align === 'left'
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
} flex justify-center w-full shadow rounded-md bg-white cursor-pointer relative inline-flex items-center px-2 py-2 border border-gray-200 hover:text-white text-sm font-medium transition-all ease-out duration-150 hover:bg-blue-500 focus:z-10 focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500`}
|
|
226
|
-
type="button"
|
|
218
|
+
props.align === 'left' ? 'rounded-l border-r-0' : 'rounded-r border-l-0'
|
|
219
|
+
} flex justify-center w-full shadow rounded bg-white cursor-pointer relative inline-flex items-center px-2 py-2 border border-gray-200 hover:text-white text-sm font-medium transition-all ease-out duration-150 hover:bg-blue-500 focus:z-10 focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500`}
|
|
220
|
+
type='button'
|
|
227
221
|
onClick={props.onClick}
|
|
228
222
|
>
|
|
229
|
-
<span className=
|
|
223
|
+
<span className='text-sm font-semibold tracking-wide align-baseline mr-1'>
|
|
230
224
|
{props.children}
|
|
231
225
|
</span>
|
|
232
226
|
</button>
|
|
233
|
-
)
|
|
234
|
-
}
|
|
227
|
+
);
|
|
228
|
+
};
|
|
235
229
|
|
|
236
|
-
export default RawEditor
|
|
230
|
+
export default RawEditor;
|