@tinacms/app 0.0.0-003e348-20251023020516

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.
@@ -0,0 +1,230 @@
1
+ import React from 'react';
2
+ import MonacoEditor, { useMonaco, loader } from '@monaco-editor/react';
3
+ /**
4
+ * MDX is built directly to the app because of how we load dependencies.
5
+ * Since we drop the package.json in to the end users folder, we can't
6
+ * easily install the current version of the mdx package in all scenarios
7
+ * (when we're working in the monorepo, or working with a tagged npm version)
8
+ */
9
+ import { parseMDX, serializeMDX } from '@tinacms/mdx';
10
+ import type * as monaco from 'monaco-editor';
11
+ import { RichTextType } from 'tinacms';
12
+ import {
13
+ ErrorMessage,
14
+ InvalidMarkdownElement,
15
+ buildError,
16
+ } from './error-message';
17
+ import { useDebounce } from './use-debounce';
18
+
19
+ export const uuid = () => {
20
+ // @ts-ignore
21
+ return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
22
+ (
23
+ c ^
24
+ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
25
+ ).toString(16)
26
+ );
27
+ };
28
+
29
+ type Monaco = typeof monaco;
30
+
31
+ /**
32
+ * Since monaco lazy-loads we may have a delay from when the block is inserted
33
+ * to when monaco has intantiated, keep trying to focus on it.
34
+ *
35
+ * Will try for 3 seconds before moving on
36
+ */
37
+ let retryCount = 0;
38
+ const retryFocus = (ref) => {
39
+ if (ref.current) {
40
+ ref.current.focus();
41
+ } else {
42
+ if (retryCount < 30) {
43
+ setTimeout(() => {
44
+ retryCount = retryCount + 1;
45
+ retryFocus(ref);
46
+ }, 100);
47
+ }
48
+ }
49
+ };
50
+
51
+ export const RawEditor = (props: RichTextType) => {
52
+ const monaco = useMonaco() as Monaco;
53
+ const monacoEditorRef =
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;
58
+ const inputValue = React.useMemo(() => {
59
+ // @ts-ignore no access to the rich-text type from this package
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);
65
+
66
+ const debouncedValue = useDebounce(value, 500);
67
+
68
+ React.useEffect(() => {
69
+ // @ts-ignore no access to the rich-text type from this package
70
+ const parsedValue = parseMDX(value, field, (value) => value);
71
+ if (
72
+ parsedValue.children[0] &&
73
+ parsedValue.children[0].type === 'invalid_markdown'
74
+ ) {
75
+ const invalidMarkdown = parsedValue.children[0];
76
+ setError(invalidMarkdown);
77
+ } else {
78
+ setError(null);
79
+ }
80
+ props.input.onChange(parsedValue);
81
+ }, [JSON.stringify(debouncedValue)]);
82
+
83
+ React.useEffect(() => {
84
+ if (monacoEditorRef.current) {
85
+ if (error) {
86
+ const errorMessage = buildError(error);
87
+ monaco.editor.setModelMarkers(monacoEditorRef.current.getModel(), id, [
88
+ {
89
+ ...errorMessage.position,
90
+ message: errorMessage.message,
91
+ severity: 8,
92
+ },
93
+ ]);
94
+ } else {
95
+ monaco.editor.setModelMarkers(
96
+ monacoEditorRef.current.getModel(),
97
+ id,
98
+ []
99
+ );
100
+ }
101
+ }
102
+ }, [JSON.stringify(error), monacoEditorRef.current]);
103
+
104
+ React.useEffect(() => {
105
+ if (monaco) {
106
+ monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);
107
+ monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
108
+ // disable errors
109
+ noSemanticValidation: true,
110
+ noSyntaxValidation: true,
111
+ });
112
+ // TODO: autocomplete suggestions
113
+ // monaco.languages.registerCompletionItemProvider('markdown', {
114
+ // provideCompletionItems: function (model, position) {
115
+ // const word = model.getWordUntilPosition(position)
116
+ // const range = {
117
+ // startLineNumber: position.lineNumber,
118
+ // endLineNumber: position.lineNumber,
119
+ // startColumn: word.startColumn,
120
+ // endColumn: word.endColumn,
121
+ // }
122
+ // return {
123
+ // suggestions: [
124
+ // {
125
+ // label: '<DateTime />',
126
+ // insertText: '<DateTime format="iso" />',
127
+ // kind: 0,
128
+ // range,
129
+ // },
130
+ // ],
131
+ // }
132
+ // },
133
+ // })
134
+ }
135
+ }, [monaco]);
136
+
137
+ function handleEditorDidMount(
138
+ monacoEditor: monaco.editor.IStandaloneCodeEditor,
139
+ monaco: Monaco
140
+ ) {
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
+ }
152
+ }
153
+
154
+ return (
155
+ <div className='relative'>
156
+ <div className='sticky top-1 w-full flex justify-between mb-2 z-50 max-w-full bg-white'>
157
+ <Button onClick={() => props.setRawMode(false)}>
158
+ View in rich-text editor 📝
159
+ </Button>
160
+ <ErrorMessage error={error} />
161
+ </div>
162
+ <div style={{ height: `${height}px` }}>
163
+ <MonacoEditor
164
+ beforeMount={() => {}}
165
+ height='100%'
166
+ width='100%'
167
+ path={id}
168
+ onMount={handleEditorDidMount}
169
+ // Setting a custom theme is kind of buggy because it doesn't get defined until monaco has mounted.
170
+ // So we end up with the default (light) theme in some scenarios. Seems like a race condition.
171
+ // theme="vs-dark"
172
+ options={{
173
+ scrollBeyondLastLine: false,
174
+ tabSize: 2,
175
+ disableLayerHinting: true,
176
+ accessibilitySupport: 'off',
177
+ codeLens: false,
178
+ wordWrap: 'on',
179
+ minimap: {
180
+ enabled: false,
181
+ },
182
+ fontSize: 14,
183
+ lineHeight: 2,
184
+ formatOnPaste: true,
185
+ lineNumbers: 'on',
186
+ lineNumbersMinChars: 2,
187
+ formatOnType: true,
188
+ fixedOverflowWidgets: true,
189
+ // Takes too much horizontal space for iframe
190
+ folding: false,
191
+ renderLineHighlight: 'none',
192
+ scrollbar: {
193
+ verticalScrollbarSize: 4,
194
+ horizontalScrollbarSize: 4,
195
+ // https://github.com/microsoft/monaco-editor/issues/2007#issuecomment-644425664
196
+ alwaysConsumeMouseWheel: false,
197
+ },
198
+ }}
199
+ language={'markdown'}
200
+ value={value}
201
+ onChange={(value) => {
202
+ try {
203
+ setValue(value);
204
+ } catch (e) {
205
+ console.log('error', e);
206
+ }
207
+ }}
208
+ />
209
+ </div>
210
+ </div>
211
+ );
212
+ };
213
+
214
+ const Button = (props) => {
215
+ return (
216
+ <button
217
+ className={`${
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'
221
+ onClick={props.onClick}
222
+ >
223
+ <span className='text-sm font-semibold tracking-wide align-baseline mr-1'>
224
+ {props.children}
225
+ </span>
226
+ </button>
227
+ );
228
+ };
229
+
230
+ export default RawEditor;
@@ -0,0 +1,25 @@
1
+ /**
2
+
3
+
4
+
5
+ */
6
+ import { useState, useEffect } from 'react';
7
+ export function useDebounce(value, delay) {
8
+ const [debouncedValue, setDebouncedValue] = useState(value);
9
+ useEffect(
10
+ () => {
11
+ // Update debounced value after delay
12
+ const handler = setTimeout(() => {
13
+ setDebouncedValue(value);
14
+ }, delay);
15
+ // Cancel the timeout if value changes (also on delay change or unmount)
16
+ // This is how we prevent debounced value from updating if value is changed ...
17
+ // .. within the delay period. Timeout gets cleared and restarted.
18
+ return () => {
19
+ clearTimeout(handler);
20
+ };
21
+ },
22
+ [value, delay] // Only re-call effect if value or delay changes
23
+ );
24
+ return debouncedValue;
25
+ }
package/src/global.css ADDED
@@ -0,0 +1,120 @@
1
+ :root {
2
+ --tina-color-primary-light: #2296fe;
3
+ --tina-color-primary: #0084ff;
4
+ --tina-color-primary-dark: #0574e4;
5
+ --tina-color-error-light: #eb6337;
6
+ --tina-color-error: #ec4815;
7
+ --tina-color-error-dark: #dc4419;
8
+ --tina-color-warning-light: #f5e06e;
9
+ --tina-color-warning: #e9d050;
10
+ --tina-color-warning-dark: #d3ba38;
11
+ --tina-color-success-light: #57c355;
12
+ --tina-color-success: #3cad3a;
13
+ --tina-color-success-dark: #249a21;
14
+ --tina-color-grey-0: #ffffff;
15
+ --tina-color-grey-1: #f6f6f9;
16
+ --tina-color-grey-2: #edecf3;
17
+ --tina-color-grey-3: #e1ddec;
18
+ --tina-color-grey-4: #b2adbe;
19
+ --tina-color-grey-5: #918c9e;
20
+ --tina-color-grey-6: #716c7f;
21
+ --tina-color-grey-7: #565165;
22
+ --tina-color-grey-8: #433e52;
23
+ --tina-color-grey-9: #363145;
24
+ --tina-color-grey-10: #252336;
25
+ --tina-color-indicator: var(--tina-color-primary);
26
+
27
+ --tina-radius-small: 5px;
28
+ --tina-radius-big: 24px;
29
+
30
+ --tina-padding-small: 12px;
31
+ --tina-padding-big: 20px;
32
+
33
+ --tina-font-size-0: 12px;
34
+ --tina-font-size-1: 13px;
35
+ --tina-font-size-2: 15px;
36
+ --tina-font-size-3: 16px;
37
+ --tina-font-size-4: 18px;
38
+ --tina-font-size-5: 20px;
39
+ --tina-font-size-6: 22px;
40
+ --tina-font-size-7: 26px;
41
+ --tina-font-size-8: 32px;
42
+
43
+ --tina-font-family: "Inter", sans-serif;
44
+
45
+ --tina-font-weight-regular: 400;
46
+ --tina-font-weight-bold: 600;
47
+
48
+ --tina-shadow-big: 0px 2px 3px rgba(0, 0, 0, 0.05), 0 4px 12px
49
+ rgba(0, 0, 0, 0.1);
50
+ --tina-shadow-small: 0px 2px 3px rgba(0, 0, 0, 0.12);
51
+
52
+ --tina-timing-short: 85ms;
53
+ --tina-timing-medium: 150ms;
54
+ --tina-timing-long: 250ms;
55
+
56
+ --tina-z-index-0: 0;
57
+ --tina-z-index-1: 10;
58
+ --tina-z-index-2: 20;
59
+ --tina-z-index-3: 30;
60
+ --tina-z-index-4: 40;
61
+ --tina-z-index-5: 50;
62
+
63
+ --tina-sidebar-width: 340px;
64
+ --tina-sidebar-header-height: 60px;
65
+ --tina-toolbar-height: 62px;
66
+ }
67
+
68
+ @keyframes fly-in-left {
69
+ 0% {
70
+ transform: translate3d(100%, 0, 0);
71
+ }
72
+
73
+ 100% {
74
+ transform: translate3d(0, 0, 0);
75
+ }
76
+ }
77
+
78
+ @keyframes fly-in-up {
79
+ 0% {
80
+ transform: translate3d(0, 100%, 0);
81
+ }
82
+
83
+ 100% {
84
+ transform: translate3d(0, 0, 0);
85
+ }
86
+ }
87
+
88
+ @keyframes fade-in {
89
+ 0% {
90
+ opacity: 0;
91
+ }
92
+
93
+ 100% {
94
+ opacity: 1;
95
+ }
96
+ }
97
+
98
+ @keyframes popup-right {
99
+ 0% {
100
+ transform: translate3d(-2rem, 0, 0);
101
+ opacity: 0;
102
+ }
103
+
104
+ 100% {
105
+ transform: translate3d(0, 0, 0);
106
+ opacity: 1;
107
+ }
108
+ }
109
+
110
+ @keyframes popup-down {
111
+ 0% {
112
+ transform: translate3d(0, -2rem, 0);
113
+ opacity: 0;
114
+ }
115
+
116
+ 100% {
117
+ transform: translate3d(0, 0, 0);
118
+ opacity: 1;
119
+ }
120
+ }