@tinacms/app 0.0.0-649e1f8-20241115062112 → 0.0.0-67b5c0b-20250414080731

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.
@@ -3,45 +3,45 @@
3
3
 
4
4
 
5
5
  */
6
- import React from 'react'
7
- import { XCircleIcon } from '@heroicons/react/solid'
6
+ import React from 'react';
7
+ import { XCircleIcon } from '@heroicons/react/solid';
8
8
  import {
9
9
  Popover,
10
10
  PopoverButton,
11
11
  PopoverPanel,
12
12
  Transition,
13
- } from '@headlessui/react'
14
- import { Fragment } from 'react'
13
+ } from '@headlessui/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="relative">
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="sr-only">Errors</span>
84
- <XCircleIcon className="h-5 w-5 text-red-400" aria-hidden="true" />
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="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"
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="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-md bg-red-50 p-4 overflow-scroll">
97
- <div className="flex">
98
- <div className="flex-shrink-0">
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-md bg-red-50 p-4 overflow-scroll'>
97
+ <div className='flex'>
98
+ <div className='flex-shrink-0'>
99
99
  <XCircleIcon
100
- className="h-5 w-5 text-red-400"
101
- aria-hidden="true"
100
+ className='h-5 w-5 text-red-400'
101
+ aria-hidden='true'
102
102
  />
103
103
  </div>
104
- <div className="ml-3">
105
- <h3 className="text-sm font-medium text-red-800 whitespace-pre-wrap">
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,166 +1,168 @@
1
- /**
2
-
3
-
4
-
5
- */
6
-
7
- import React from 'react'
8
- import MonacoEditor, { useMonaco, loader } from '@monaco-editor/react'
9
- /**
10
- * MDX is built directly to the app because of how we load dependencies.
11
- * Since we drop the package.json in to the end users folder, we can't
12
- * easily install the current version of the mdx package in all scenarios
13
- * (when we're working in the monorepo, or working with a tagged npm version)
14
- */
15
- import { parseMDX, stringifyMDX } from '@tinacms/mdx'
16
- import { useDebounce } from './use-debounce'
17
- import type * as monaco from 'monaco-editor'
1
+ import MonacoEditor, { Monaco } from '@monaco-editor/react';
2
+ import { parseMDX, stringifyMDX } from '@tinacms/mdx';
3
+ import type * as monaco from 'monaco-editor';
4
+ import React from 'react';
5
+ import { RichTextType } from 'tinacms';
18
6
  import {
19
- buildError,
20
7
  ErrorMessage,
21
8
  InvalidMarkdownElement,
22
- } from './error-message'
23
- import { RichTextType } from 'tinacms'
9
+ buildError,
10
+ } from './error-message';
11
+ import { useDebounce } from './use-debounce';
12
+ import useCustomMonaco from './use-monaco';
24
13
 
25
14
  export const uuid = () => {
26
- // @ts-ignore
27
15
  return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
28
16
  (
29
17
  c ^
30
18
  (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
31
19
  ).toString(16)
32
- )
33
- }
34
-
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
- })
20
+ );
21
+ };
41
22
 
42
23
  /**
43
24
  * Since monaco lazy-loads we may have a delay from when the block is inserted
44
- * to when monaco has intantiated, keep trying to focus on it.
45
- *
46
- * Will try for 3 seconds before moving on
25
+ * to when monaco has instantiated.
47
26
  */
48
- let retryCount = 0
49
- const retryFocus = (ref) => {
50
- if (ref.current) {
51
- ref.current.focus()
52
- } else {
53
- if (retryCount < 30) {
54
- setTimeout(() => {
55
- retryCount = retryCount + 1
56
- retryFocus(ref)
57
- }, 100)
27
+ const retryFocus = (editor) => {
28
+ if (editor && editor.focus) {
29
+ try {
30
+ editor.focus();
31
+ } catch (err) {
32
+ console.warn('Error focusing editor:', err);
58
33
  }
59
34
  }
60
- }
35
+ };
61
36
 
62
37
  export const RawEditor = (props: RichTextType) => {
63
- const monaco = useMonaco() as Monaco
64
- 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
38
+ const monacoInstance = useCustomMonaco();
39
+ const editorRef = React.useRef<monaco.editor.IStandaloneCodeEditor | null>(
40
+ null
41
+ );
42
+ const [height, setHeight] = React.useState(100);
43
+ const id = React.useMemo(() => uuid(), []);
44
+ const field = props.field;
45
+
46
+ // Get initial value safely
69
47
  const inputValue = React.useMemo(() => {
70
- // @ts-ignore no access to the rich-text type from this package
71
- const res = stringifyMDX(props.input.value, field, (value) => value)
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)
48
+ try {
49
+ // @ts-ignore no access to the rich-text type from this package
50
+ const res = stringifyMDX(props.input.value, field, (value) => value);
51
+ return typeof props.input.value === 'string' ? props.input.value : res;
52
+ } catch (err) {
53
+ console.error('Error stringifying MDX:', err);
54
+ return '';
55
+ }
56
+ }, []); // Empty dependency array to only run once
57
+
58
+ const [value, setValue] = React.useState(inputValue);
59
+ const [error, setError] = React.useState<InvalidMarkdownElement>(null);
76
60
 
77
- const debouncedValue = useDebounce(value, 500)
61
+ const debouncedValue = useDebounce(value, 500);
78
62
 
63
+ // Update parsed MDX when value changes
79
64
  React.useEffect(() => {
80
- // @ts-ignore no access to the rich-text type from this package
81
- const parsedValue = parseMDX(value, field, (value) => value)
82
- if (
83
- parsedValue.children[0] &&
84
- parsedValue.children[0].type === 'invalid_markdown'
85
- ) {
86
- const invalidMarkdown = parsedValue.children[0]
87
- setError(invalidMarkdown)
88
- } else {
89
- setError(null)
65
+ let isMounted = true;
66
+
67
+ try {
68
+ // @ts-ignore no access to the rich-text type from this package
69
+ const parsedValue = parseMDX(debouncedValue, field, (value) => value);
70
+
71
+ if (!isMounted) return;
72
+
73
+ if (
74
+ parsedValue.children[0] &&
75
+ parsedValue.children[0].type === 'invalid_markdown'
76
+ ) {
77
+ setError(parsedValue.children[0]);
78
+ } else {
79
+ setError(null);
80
+ props.input.onChange(parsedValue);
81
+ }
82
+ } catch (err) {
83
+ console.error('Error parsing MDX:', err);
90
84
  }
91
- props.input.onChange(parsedValue)
92
- }, [JSON.stringify(debouncedValue)])
93
85
 
86
+ return () => {
87
+ isMounted = false;
88
+ };
89
+ }, [debouncedValue, field]); // Only dependency should be the debounced value and field
90
+
91
+ // Handle error markers in editor
94
92
  React.useEffect(() => {
95
- if (monacoEditorRef.current) {
93
+ if (!monacoInstance || !editorRef.current) return;
94
+
95
+ try {
96
+ const model = editorRef.current.getModel();
97
+ if (!model) return;
98
+
96
99
  if (error) {
97
- const errorMessage = buildError(error)
98
- monaco.editor.setModelMarkers(monacoEditorRef.current.getModel(), id, [
99
- {
100
- ...errorMessage.position,
101
- message: errorMessage.message,
102
- severity: 8,
103
- },
104
- ])
100
+ const errorMessage = buildError(error);
101
+
102
+ // Make sure all position properties are numbers (not undefined)
103
+ const markerData = {
104
+ message: errorMessage.message,
105
+ severity: monacoInstance.MarkerSeverity?.Error || 8,
106
+ startLineNumber: errorMessage.position?.startLineNumber || 1,
107
+ endLineNumber: errorMessage.position?.endLineNumber || 1,
108
+ startColumn: errorMessage.position?.startColumn || 1,
109
+ endColumn: errorMessage.position?.endColumn || 1,
110
+ };
111
+
112
+ monacoInstance.editor.setModelMarkers(model, id, [markerData]);
105
113
  } else {
106
- monaco.editor.setModelMarkers(
107
- monacoEditorRef.current.getModel(),
108
- id,
109
- []
110
- )
114
+ monacoInstance.editor.setModelMarkers(model, id, []);
111
115
  }
116
+ } catch (err) {
117
+ console.error('Error setting model markers:', err);
112
118
  }
113
- }, [JSON.stringify(error), monacoEditorRef.current])
119
+ }, [error, monacoInstance, id]);
114
120
 
121
+ // Configure TypeScript settings once when Monaco loads
115
122
  React.useEffect(() => {
116
- if (monaco) {
117
- monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true)
118
- monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
119
- // disable errors
120
- noSemanticValidation: true,
121
- noSyntaxValidation: true,
122
- })
123
- // TODO: autocomplete suggestions
124
- // monaco.languages.registerCompletionItemProvider('markdown', {
125
- // provideCompletionItems: function (model, position) {
126
- // const word = model.getWordUntilPosition(position)
127
- // const range = {
128
- // startLineNumber: position.lineNumber,
129
- // endLineNumber: position.lineNumber,
130
- // startColumn: word.startColumn,
131
- // endColumn: word.endColumn,
132
- // }
133
- // return {
134
- // suggestions: [
135
- // {
136
- // label: '<DateTime />',
137
- // insertText: '<DateTime format="iso" />',
138
- // kind: 0,
139
- // range,
140
- // },
141
- // ],
142
- // }
143
- // },
144
- // })
123
+ if (!monacoInstance) return;
124
+
125
+ try {
126
+ monacoInstance.languages.typescript.typescriptDefaults.setEagerModelSync(
127
+ true
128
+ );
129
+ monacoInstance.languages.typescript.typescriptDefaults.setDiagnosticsOptions(
130
+ {
131
+ noSemanticValidation: true,
132
+ noSyntaxValidation: true,
133
+ }
134
+ );
135
+ } catch (err) {
136
+ console.error('Error configuring Monaco TypeScript settings:', err);
145
137
  }
146
- }, [monaco])
138
+ }, [monacoInstance]);
147
139
 
148
140
  function handleEditorDidMount(
149
- monacoEditor: monaco.editor.IStandaloneCodeEditor,
141
+ editor: monaco.editor.IStandaloneCodeEditor,
150
142
  monaco: Monaco
151
143
  ) {
152
- monacoEditorRef.current = monacoEditor
153
- monacoEditor.onDidContentSizeChange(() => {
154
- // FIXME: if the window is too tall the performance degrades, come up with a nice
155
- // balance between the two
156
- setHeight(Math.min(Math.max(100, monacoEditor.getContentHeight()), 1000))
157
- monacoEditor.layout()
158
- })
144
+ if (!editor) return;
145
+
146
+ try {
147
+ editorRef.current = editor;
148
+
149
+ // Focus the editor once when mounted
150
+ setTimeout(() => editor.focus(), 100);
151
+
152
+ // Set up content size listener
153
+ editor.onDidContentSizeChange(() => {
154
+ const contentHeight = editor.getContentHeight();
155
+ setHeight(Math.min(Math.max(100, contentHeight), 1000));
156
+ editor.layout();
157
+ });
158
+ } catch (err) {
159
+ console.error('Error in editor mount handler:', err);
160
+ }
159
161
  }
160
162
 
161
163
  return (
162
- <div className="relative">
163
- <div className="sticky top-1 w-full flex justify-between mb-2 z-50 max-w-full bg-white">
164
+ <div className='relative'>
165
+ <div className='sticky top-1 w-full flex justify-between mb-2 z-50 max-w-full bg-white'>
164
166
  <Button onClick={() => props.setRawMode(false)}>
165
167
  View in rich-text editor 📝
166
168
  </Button>
@@ -170,9 +172,6 @@ export const RawEditor = (props: RichTextType) => {
170
172
  <MonacoEditor
171
173
  path={id}
172
174
  onMount={handleEditorDidMount}
173
- // Setting a custom theme is kind of buggy because it doesn't get defined until monaco has mounted.
174
- // So we end up with the default (light) theme in some scenarios. Seems like a race condition.
175
- // theme="vs-dark"
176
175
  options={{
177
176
  scrollBeyondLastLine: false,
178
177
  tabSize: 2,
@@ -190,30 +189,26 @@ export const RawEditor = (props: RichTextType) => {
190
189
  lineNumbersMinChars: 2,
191
190
  formatOnType: true,
192
191
  fixedOverflowWidgets: true,
193
- // Takes too much horizontal space for iframe
194
192
  folding: false,
195
193
  renderLineHighlight: 'none',
196
194
  scrollbar: {
197
195
  verticalScrollbarSize: 4,
198
196
  horizontalScrollbarSize: 4,
199
- // https://github.com/microsoft/monaco-editor/issues/2007#issuecomment-644425664
200
197
  alwaysConsumeMouseWheel: false,
201
198
  },
202
199
  }}
203
- language={'markdown'}
200
+ language='markdown'
204
201
  value={value}
205
- onChange={(value) => {
206
- try {
207
- setValue(value)
208
- } catch (e) {
209
- console.log('error', e)
202
+ onChange={(newValue) => {
203
+ if (newValue !== undefined) {
204
+ setValue(newValue);
210
205
  }
211
206
  }}
212
207
  />
213
208
  </div>
214
209
  </div>
215
- )
216
- }
210
+ );
211
+ };
217
212
 
218
213
  const Button = (props) => {
219
214
  return (
@@ -223,14 +218,14 @@ const Button = (props) => {
223
218
  ? 'rounded-l-md border-r-0'
224
219
  : 'rounded-r-md border-l-0'
225
220
  } 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"
221
+ type='button'
227
222
  onClick={props.onClick}
228
223
  >
229
- <span className="text-sm font-semibold tracking-wide align-baseline mr-1">
224
+ <span className='text-sm font-semibold tracking-wide align-baseline mr-1'>
230
225
  {props.children}
231
226
  </span>
232
227
  </button>
233
- )
234
- }
228
+ );
229
+ };
235
230
 
236
- export default RawEditor
231
+ export default RawEditor;
@@ -3,23 +3,23 @@
3
3
 
4
4
 
5
5
  */
6
- import { useState, useEffect } from 'react'
6
+ import { useState, useEffect } from 'react';
7
7
  export function useDebounce(value, delay) {
8
- const [debouncedValue, setDebouncedValue] = useState(value)
8
+ const [debouncedValue, setDebouncedValue] = useState(value);
9
9
  useEffect(
10
10
  () => {
11
11
  // Update debounced value after delay
12
12
  const handler = setTimeout(() => {
13
- setDebouncedValue(value)
14
- }, delay)
13
+ setDebouncedValue(value);
14
+ }, delay);
15
15
  // Cancel the timeout if value changes (also on delay change or unmount)
16
16
  // This is how we prevent debounced value from updating if value is changed ...
17
17
  // .. within the delay period. Timeout gets cleared and restarted.
18
18
  return () => {
19
- clearTimeout(handler)
20
- }
19
+ clearTimeout(handler);
20
+ };
21
21
  },
22
22
  [value, delay] // Only re-call effect if value or delay changes
23
- )
24
- return debouncedValue
23
+ );
24
+ return debouncedValue;
25
25
  }
@@ -0,0 +1,53 @@
1
+ import { loader } from '@monaco-editor/react';
2
+ import * as monaco from 'monaco-editor';
3
+ // hooks/useCustomMonaco.ts
4
+ import { useEffect, useRef, useState } from 'react';
5
+
6
+ export function useCustomMonaco() {
7
+ const [monacoInstance, setMonacoInstance] = useState<typeof monaco | null>(
8
+ null
9
+ );
10
+ const mountedRef = useRef(true);
11
+ const loaderRef = useRef<any>(null);
12
+
13
+ useEffect(() => {
14
+ const instance = loader.__getMonacoInstance();
15
+
16
+ if (instance) {
17
+ setMonacoInstance(instance);
18
+ return;
19
+ }
20
+
21
+ if (!loaderRef.current) {
22
+ loader.config({
23
+ 'vs/nls': { availableLanguages: {} },
24
+ });
25
+
26
+ try {
27
+ loaderRef.current = loader.init();
28
+
29
+ loaderRef.current
30
+ .then((monacoApi: typeof monaco) => {
31
+ if (mountedRef.current) {
32
+ setMonacoInstance(monacoApi);
33
+ }
34
+ })
35
+ .catch((error: any) => {
36
+ if (mountedRef.current && error.type !== 'cancelation') {
37
+ console.error('Monaco initialization error:', error);
38
+ }
39
+ });
40
+ } catch (err) {
41
+ console.error('Failed to initialize Monaco:', err);
42
+ }
43
+ }
44
+
45
+ return () => {
46
+ mountedRef.current = false;
47
+ };
48
+ }, []);
49
+
50
+ return monacoInstance;
51
+ }
52
+
53
+ export default useCustomMonaco;
package/src/global.css CHANGED
@@ -40,13 +40,13 @@
40
40
  --tina-font-size-7: 26px;
41
41
  --tina-font-size-8: 32px;
42
42
 
43
- --tina-font-family: 'Inter', sans-serif;
43
+ --tina-font-family: "Inter", sans-serif;
44
44
 
45
45
  --tina-font-weight-regular: 400;
46
46
  --tina-font-weight-bold: 600;
47
47
 
48
- --tina-shadow-big: 0px 2px 3px rgba(0, 0, 0, 0.05),
49
- 0 4px 12px rgba(0, 0, 0, 0.1);
48
+ --tina-shadow-big: 0px 2px 3px rgba(0, 0, 0, 0.05), 0 4px 12px
49
+ rgba(0, 0, 0, 0.1);
50
50
  --tina-shadow-small: 0px 2px 3px rgba(0, 0, 0, 0.12);
51
51
 
52
52
  --tina-timing-short: 85ms;