@dxos/react-ui-syntax-highlighter 0.8.4-main.fffef41 → 0.8.4-staging.ac66bdf99f
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/dist/lib/browser/index.mjs +132 -93
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +132 -93
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/Json/Json.d.ts +31 -6
- package/dist/types/src/Json/Json.d.ts.map +1 -1
- package/dist/types/src/Json/Json.stories.d.ts +11 -5
- package/dist/types/src/Json/Json.stories.d.ts.map +1 -1
- package/dist/types/src/Json/index.d.ts +1 -1
- package/dist/types/src/Json/index.d.ts.map +1 -1
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts +5 -4
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts.map +1 -1
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts +6 -1
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +23 -20
- package/src/Json/Json.stories.tsx +40 -40
- package/src/Json/Json.tsx +159 -39
- package/src/Json/index.ts +1 -1
- package/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx +10 -9
- package/src/SyntaxHighlighter/SyntaxHighlighter.tsx +45 -43
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-syntax-highlighter",
|
|
3
|
-
"version": "0.8.4-
|
|
3
|
+
"version": "0.8.4-staging.ac66bdf99f",
|
|
4
4
|
"description": "A syntax highlighter wrapper.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -25,30 +29,29 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"
|
|
29
|
-
"jsonpath": "^1.1.1",
|
|
32
|
+
"jsonpath-plus": "^10.3.0",
|
|
30
33
|
"react-syntax-highlighter": "^15.6.1",
|
|
31
|
-
"@dxos/
|
|
34
|
+
"@dxos/ui-types": "0.8.4-staging.ac66bdf99f",
|
|
35
|
+
"@dxos/util": "0.8.4-staging.ac66bdf99f"
|
|
32
36
|
},
|
|
33
37
|
"devDependencies": {
|
|
34
|
-
"@types/
|
|
35
|
-
"@types/react": "~19.2.
|
|
36
|
-
"@types/react-dom": "~19.2.2",
|
|
38
|
+
"@types/react": "~19.2.7",
|
|
39
|
+
"@types/react-dom": "~19.2.3",
|
|
37
40
|
"@types/react-syntax-highlighter": "^15.5.13",
|
|
38
|
-
"react": "~19.2.
|
|
39
|
-
"react-dom": "~19.2.
|
|
40
|
-
"vite": "7.1.
|
|
41
|
-
"@dxos/random": "0.8.4-
|
|
42
|
-
"@dxos/react-ui": "0.8.4-
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/util": "0.8.4-
|
|
41
|
+
"react": "~19.2.3",
|
|
42
|
+
"react-dom": "~19.2.3",
|
|
43
|
+
"vite": "^7.1.11",
|
|
44
|
+
"@dxos/random": "0.8.4-staging.ac66bdf99f",
|
|
45
|
+
"@dxos/react-ui": "0.8.4-staging.ac66bdf99f",
|
|
46
|
+
"@dxos/storybook-utils": "0.8.4-staging.ac66bdf99f",
|
|
47
|
+
"@dxos/ui-theme": "0.8.4-staging.ac66bdf99f",
|
|
48
|
+
"@dxos/util": "0.8.4-staging.ac66bdf99f"
|
|
46
49
|
},
|
|
47
50
|
"peerDependencies": {
|
|
48
|
-
"react": "
|
|
49
|
-
"react-dom": "
|
|
50
|
-
"@dxos/react-ui
|
|
51
|
-
"@dxos/
|
|
51
|
+
"react": "~19.2.3",
|
|
52
|
+
"react-dom": "~19.2.3",
|
|
53
|
+
"@dxos/react-ui": "0.8.4-staging.ac66bdf99f",
|
|
54
|
+
"@dxos/ui-theme": "0.8.4-staging.ac66bdf99f"
|
|
52
55
|
},
|
|
53
56
|
"publishConfig": {
|
|
54
57
|
"access": "public"
|
|
@@ -5,29 +5,29 @@
|
|
|
5
5
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
6
|
import React from 'react';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { random } from '@dxos/random';
|
|
9
9
|
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
10
10
|
|
|
11
11
|
import { Json } from './Json';
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
random.seed(0);
|
|
14
14
|
|
|
15
15
|
const createNode = () => {
|
|
16
16
|
const data: Record<string, any> = {};
|
|
17
|
-
const keys = [...Array(
|
|
17
|
+
const keys = [...Array(random.number.int({ min: 1, max: 5 }))].map(() => random.lorem.word());
|
|
18
18
|
keys.forEach((key) => {
|
|
19
|
-
switch (
|
|
19
|
+
switch (random.helpers.arrayElement(['object', 'string', 'number', 'boolean', 'null'])) {
|
|
20
20
|
case 'object':
|
|
21
21
|
data[key] = createNode();
|
|
22
22
|
break;
|
|
23
23
|
case 'string':
|
|
24
|
-
data[key] =
|
|
24
|
+
data[key] = random.lorem.word();
|
|
25
25
|
break;
|
|
26
26
|
case 'number':
|
|
27
|
-
data[key] =
|
|
27
|
+
data[key] = random.number.int();
|
|
28
28
|
break;
|
|
29
29
|
case 'boolean':
|
|
30
|
-
data[key] =
|
|
30
|
+
data[key] = random.datatype.boolean();
|
|
31
31
|
break;
|
|
32
32
|
case 'null':
|
|
33
33
|
data[key] = null;
|
|
@@ -41,7 +41,7 @@ const createNode = () => {
|
|
|
41
41
|
const createData = ({ depth = 2, children = 3 } = {}): any => {
|
|
42
42
|
const createChildren = (root: any, d = 0) => {
|
|
43
43
|
if (d < depth) {
|
|
44
|
-
const num =
|
|
44
|
+
const num = random.number.int({ min: 1, max: Math.round(Math.log(depth + 1 - d) * children) });
|
|
45
45
|
root.children = [...new Array(num)].map(() => {
|
|
46
46
|
return createChildren(createNode(), d + 1);
|
|
47
47
|
});
|
|
@@ -53,54 +53,54 @@ const createData = ({ depth = 2, children = 3 } = {}): any => {
|
|
|
53
53
|
return createChildren(createNode());
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
+
const createCycle = () => {
|
|
57
|
+
const data: any = {
|
|
58
|
+
a: 1,
|
|
59
|
+
b: [],
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
data.b.push(data);
|
|
63
|
+
return data;
|
|
64
|
+
};
|
|
65
|
+
|
|
56
66
|
const meta = {
|
|
57
67
|
title: 'ui/react-ui-syntax-highlighter/Json',
|
|
58
|
-
component: Json,
|
|
59
|
-
decorators: [withTheme, withLayout({
|
|
60
|
-
} satisfies Meta
|
|
68
|
+
component: Json.Root,
|
|
69
|
+
decorators: [withTheme(), withLayout({ layout: 'column' })],
|
|
70
|
+
} satisfies Meta;
|
|
61
71
|
|
|
62
72
|
export default meta;
|
|
63
73
|
|
|
64
|
-
type Story = StoryObj<typeof
|
|
65
|
-
|
|
66
|
-
const data = createData();
|
|
74
|
+
type Story = StoryObj<typeof meta>;
|
|
67
75
|
|
|
76
|
+
/** Standalone Json.Data — simplest usage. */
|
|
68
77
|
export const Default: Story = {
|
|
78
|
+
render: (args) => <Json.Data {...args} />,
|
|
69
79
|
args: {
|
|
70
|
-
|
|
71
|
-
data,
|
|
80
|
+
data: createData(),
|
|
72
81
|
},
|
|
73
82
|
};
|
|
74
83
|
|
|
75
|
-
|
|
84
|
+
/** Circular reference handling. */
|
|
85
|
+
export const Cycle: Story = {
|
|
86
|
+
render: (args) => <Json.Data {...args} />,
|
|
76
87
|
args: {
|
|
77
|
-
|
|
78
|
-
filter: true,
|
|
79
|
-
data,
|
|
88
|
+
data: createCycle(),
|
|
80
89
|
},
|
|
81
90
|
};
|
|
82
91
|
|
|
83
|
-
|
|
92
|
+
/** Large dataset with replacer. */
|
|
93
|
+
export const Filter: Story = {
|
|
94
|
+
render: (args) => (
|
|
95
|
+
<Json.Root {...args}>
|
|
96
|
+
<Json.Content>
|
|
97
|
+
<Json.Filter />
|
|
98
|
+
<Json.Data />
|
|
99
|
+
</Json.Content>
|
|
100
|
+
</Json.Root>
|
|
101
|
+
),
|
|
84
102
|
args: {
|
|
85
|
-
classNames: 'text-sm',
|
|
86
|
-
filter: true,
|
|
87
103
|
data: createData({ depth: 5 }),
|
|
88
|
-
replacer: {
|
|
89
|
-
maxDepth: 3,
|
|
90
|
-
maxArrayLen: 10,
|
|
91
|
-
maxStringLen: 10,
|
|
92
|
-
},
|
|
104
|
+
replacer: { maxDepth: 3, maxArrayLen: 10, maxStringLen: 10 },
|
|
93
105
|
},
|
|
94
106
|
};
|
|
95
|
-
|
|
96
|
-
const cycle: any = {
|
|
97
|
-
a: 1,
|
|
98
|
-
b: [],
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
cycle.b.push(cycle);
|
|
102
|
-
|
|
103
|
-
// NOTE: Storybook args cannot be circular.
|
|
104
|
-
export const Cycle: Story = {
|
|
105
|
-
render: () => <Json data={cycle} />,
|
|
106
|
-
};
|
package/src/Json/Json.tsx
CHANGED
|
@@ -2,69 +2,189 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
|
|
5
|
+
import { JSONPath } from 'jsonpath-plus';
|
|
6
|
+
import React, {
|
|
7
|
+
createContext,
|
|
8
|
+
type PropsWithChildren,
|
|
9
|
+
forwardRef,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
useState,
|
|
14
|
+
} from 'react';
|
|
8
15
|
|
|
9
|
-
import { Input
|
|
16
|
+
import { Input } from '@dxos/react-ui';
|
|
17
|
+
import { composable, composableProps } from '@dxos/ui-theme';
|
|
18
|
+
import { type ComposableProps } from '@dxos/ui-types';
|
|
10
19
|
import { type CreateReplacerProps, createReplacer, safeStringify } from '@dxos/util';
|
|
11
20
|
|
|
12
21
|
import { SyntaxHighlighter } from '../SyntaxHighlighter';
|
|
13
22
|
|
|
14
|
-
|
|
23
|
+
//
|
|
24
|
+
// Context
|
|
25
|
+
//
|
|
15
26
|
|
|
16
|
-
|
|
17
|
-
data
|
|
18
|
-
|
|
27
|
+
type JsonContextType = {
|
|
28
|
+
data: any;
|
|
29
|
+
filteredData: any;
|
|
30
|
+
filterText: string;
|
|
31
|
+
setFilterText: (text: string) => void;
|
|
32
|
+
filterError: Error | null;
|
|
19
33
|
replacer?: CreateReplacerProps;
|
|
20
|
-
|
|
21
|
-
}>;
|
|
34
|
+
};
|
|
22
35
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
const JsonContext = createContext<JsonContextType | null>(null);
|
|
37
|
+
|
|
38
|
+
/** Require Json context (throws if used outside Json.Root). */
|
|
39
|
+
const useJsonContext = (consumerName: string): JsonContextType => {
|
|
40
|
+
const context = useContext(JsonContext);
|
|
41
|
+
if (!context) {
|
|
42
|
+
throw new Error(`\`${consumerName}\` must be used within \`Json.Root\`.`);
|
|
26
43
|
}
|
|
44
|
+
return context;
|
|
45
|
+
};
|
|
27
46
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
{safeStringify(data, replacer && createReplacer(replacer), 2)}
|
|
32
|
-
</SyntaxHighlighter>
|
|
33
|
-
);
|
|
47
|
+
/** Optional Json context (returns null outside Json.Root). */
|
|
48
|
+
const useOptionalJsonContext = (): JsonContextType | null => {
|
|
49
|
+
return useContext(JsonContext);
|
|
34
50
|
};
|
|
35
51
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
52
|
+
//
|
|
53
|
+
// Root
|
|
54
|
+
//
|
|
55
|
+
|
|
56
|
+
const JSON_ROOT_NAME = 'Json.Root';
|
|
57
|
+
|
|
58
|
+
type JsonRootProps = PropsWithChildren<{
|
|
59
|
+
data?: any;
|
|
60
|
+
replacer?: CreateReplacerProps;
|
|
61
|
+
}>;
|
|
62
|
+
|
|
63
|
+
/** Headless context provider for Json composite. */
|
|
64
|
+
const JsonRoot = forwardRef<HTMLDivElement, JsonRootProps>(({ children, data, replacer }, _forwardedRef) => {
|
|
65
|
+
const [filterText, setFilterText] = useState('');
|
|
66
|
+
const [filteredData, setFilteredData] = useState(data);
|
|
67
|
+
const [filterError, setFilterError] = useState<Error | null>(null);
|
|
68
|
+
|
|
40
69
|
useEffect(() => {
|
|
41
|
-
if (!
|
|
42
|
-
|
|
70
|
+
if (!filterText.trim().length) {
|
|
71
|
+
setFilteredData(data);
|
|
72
|
+
setFilterError(null);
|
|
43
73
|
} else {
|
|
44
74
|
try {
|
|
45
|
-
|
|
46
|
-
|
|
75
|
+
setFilteredData(JSONPath({ path: filterText, json: data }));
|
|
76
|
+
setFilterError(null);
|
|
47
77
|
} catch (err) {
|
|
48
|
-
|
|
49
|
-
|
|
78
|
+
setFilteredData(data);
|
|
79
|
+
setFilterError(err as Error);
|
|
50
80
|
}
|
|
51
81
|
}
|
|
52
|
-
}, [
|
|
82
|
+
}, [data, filterText]);
|
|
83
|
+
|
|
84
|
+
const context = useMemo(
|
|
85
|
+
() => ({ data, filteredData, filterText, setFilterText, filterError, replacer }),
|
|
86
|
+
[data, filteredData, filterText, setFilterText, filterError, replacer],
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return <JsonContext.Provider value={context}>{children}</JsonContext.Provider>;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
JsonRoot.displayName = JSON_ROOT_NAME;
|
|
53
93
|
|
|
94
|
+
//
|
|
95
|
+
// Content
|
|
96
|
+
//
|
|
97
|
+
|
|
98
|
+
const JSON_CONTENT_NAME = 'Json.Content';
|
|
99
|
+
|
|
100
|
+
type JsonContentProps = ComposableProps;
|
|
101
|
+
|
|
102
|
+
/** Layout container for Json composite parts. */
|
|
103
|
+
const JsonContent = composable<HTMLDivElement, JsonContentProps>(({ children, ...props }, forwardedRef) => {
|
|
54
104
|
return (
|
|
55
|
-
<div
|
|
56
|
-
|
|
105
|
+
<div {...composableProps(props, { classNames: 'flex flex-col h-full min-h-0 overflow-hidden' })} ref={forwardedRef}>
|
|
106
|
+
{children}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
JsonContent.displayName = JSON_CONTENT_NAME;
|
|
112
|
+
|
|
113
|
+
//
|
|
114
|
+
// Filter
|
|
115
|
+
//
|
|
116
|
+
|
|
117
|
+
const JSON_FILTER_NAME = 'Json.Filter';
|
|
118
|
+
|
|
119
|
+
type JsonFilterProps = ComposableProps<{
|
|
120
|
+
placeholder?: string;
|
|
121
|
+
}>;
|
|
122
|
+
|
|
123
|
+
/** JSONPath filter input. Must be used within Json.Root. */
|
|
124
|
+
const JsonFilter = forwardRef<HTMLInputElement, JsonFilterProps>(
|
|
125
|
+
({ classNames, placeholder = 'JSONPath (e.g., $.graph.nodes)' }, forwardedRef) => {
|
|
126
|
+
const { filterText, setFilterText, filterError } = useJsonContext(JSON_FILTER_NAME);
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<Input.Root validationValence={filterError ? 'error' : 'success'}>
|
|
57
130
|
<Input.TextInput
|
|
58
|
-
classNames={['p-1
|
|
131
|
+
classNames={['p-1 px-2 font-mono', filterError && 'border-rose-500', classNames]}
|
|
59
132
|
variant='subdued'
|
|
60
|
-
value={
|
|
61
|
-
|
|
62
|
-
|
|
133
|
+
value={filterText}
|
|
134
|
+
placeholder={placeholder}
|
|
135
|
+
onChange={(event) => setFilterText(event.target.value)}
|
|
136
|
+
ref={forwardedRef}
|
|
63
137
|
/>
|
|
64
138
|
</Input.Root>
|
|
65
|
-
|
|
139
|
+
);
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
JsonFilter.displayName = JSON_FILTER_NAME;
|
|
144
|
+
|
|
145
|
+
//
|
|
146
|
+
// Data
|
|
147
|
+
//
|
|
148
|
+
|
|
149
|
+
const JSON_DATA_NAME = 'Json.Data';
|
|
150
|
+
|
|
151
|
+
type JsonDataProps = ComposableProps<{
|
|
152
|
+
data?: any;
|
|
153
|
+
replacer?: CreateReplacerProps;
|
|
154
|
+
testId?: string;
|
|
155
|
+
}>;
|
|
156
|
+
|
|
157
|
+
/** Syntax-highlighted JSON display. Works standalone or within Json.Root. */
|
|
158
|
+
const JsonData = composable<HTMLDivElement, JsonDataProps>(
|
|
159
|
+
({ data: dataProp, replacer: replacerProp, testId, ...props }, forwardedRef) => {
|
|
160
|
+
const context = useOptionalJsonContext();
|
|
161
|
+
const data = dataProp ?? context?.filteredData;
|
|
162
|
+
const replacer = replacerProp ?? context?.replacer;
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<SyntaxHighlighter
|
|
166
|
+
{...composableProps(props, { classNames: 'flex-1 min-h-0 w-full py-1 px-2 overflow-y-auto text-sm' })}
|
|
167
|
+
language='json'
|
|
168
|
+
data-testid={testId}
|
|
169
|
+
ref={forwardedRef}
|
|
170
|
+
>
|
|
66
171
|
{safeStringify(data, replacer && createReplacer(replacer), 2)}
|
|
67
172
|
</SyntaxHighlighter>
|
|
68
|
-
|
|
69
|
-
|
|
173
|
+
);
|
|
174
|
+
},
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
JsonData.displayName = JSON_DATA_NAME;
|
|
178
|
+
|
|
179
|
+
//
|
|
180
|
+
// Json
|
|
181
|
+
//
|
|
182
|
+
|
|
183
|
+
export const Json = {
|
|
184
|
+
Root: JsonRoot,
|
|
185
|
+
Content: JsonContent,
|
|
186
|
+
Filter: JsonFilter,
|
|
187
|
+
Data: JsonData,
|
|
70
188
|
};
|
|
189
|
+
|
|
190
|
+
export type { JsonRootProps, JsonContentProps, JsonFilterProps, JsonDataProps };
|
package/src/Json/index.ts
CHANGED
|
@@ -4,15 +4,20 @@
|
|
|
4
4
|
|
|
5
5
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
6
|
|
|
7
|
-
import { withTheme } from '@dxos/react-ui/testing';
|
|
7
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
8
8
|
import { trim } from '@dxos/util';
|
|
9
9
|
|
|
10
|
+
// @ts-ignore - Vite raw import.
|
|
11
|
+
import TEXT from '../../package.json?raw';
|
|
10
12
|
import { SyntaxHighlighter } from './SyntaxHighlighter';
|
|
11
13
|
|
|
12
14
|
const meta = {
|
|
13
15
|
title: 'ui/react-ui-syntax-highlighter/SyntaxHighlighter',
|
|
14
16
|
component: SyntaxHighlighter,
|
|
15
|
-
decorators: [withTheme],
|
|
17
|
+
decorators: [withTheme(), withLayout({ layout: 'column' })],
|
|
18
|
+
parameters: {
|
|
19
|
+
layout: 'fullscreen',
|
|
20
|
+
},
|
|
16
21
|
} satisfies Meta<typeof SyntaxHighlighter>;
|
|
17
22
|
|
|
18
23
|
export default meta;
|
|
@@ -23,7 +28,7 @@ export const Default: Story = {
|
|
|
23
28
|
args: {
|
|
24
29
|
language: 'json',
|
|
25
30
|
classNames: 'text-sm',
|
|
26
|
-
children:
|
|
31
|
+
children: TEXT,
|
|
27
32
|
},
|
|
28
33
|
};
|
|
29
34
|
|
|
@@ -32,7 +37,7 @@ export const Typescript: Story = {
|
|
|
32
37
|
language: 'tsx',
|
|
33
38
|
children: trim`
|
|
34
39
|
import React from 'react'
|
|
35
|
-
|
|
40
|
+
|
|
36
41
|
const Test = () => {
|
|
37
42
|
return <div>Test</div>
|
|
38
43
|
}
|
|
@@ -40,8 +45,4 @@ export const Typescript: Story = {
|
|
|
40
45
|
},
|
|
41
46
|
};
|
|
42
47
|
|
|
43
|
-
export const Empty: Story = {
|
|
44
|
-
args: {
|
|
45
|
-
children: false,
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
+
export const Empty: Story = {};
|
|
@@ -2,21 +2,24 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React from 'react';
|
|
5
|
+
import React, { CSSProperties } from 'react';
|
|
6
6
|
import { type SyntaxHighlighterProps as NaturalSyntaxHighlighterProps } from 'react-syntax-highlighter';
|
|
7
7
|
import NativeSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-async-light';
|
|
8
8
|
import { coldarkDark as dark, coldarkCold as light } from 'react-syntax-highlighter/dist/esm/styles/prism';
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
10
|
+
import { ScrollArea, useThemeContext } from '@dxos/react-ui';
|
|
11
|
+
import { composable, composableProps } from '@dxos/ui-theme';
|
|
12
12
|
|
|
13
13
|
const zeroWidthSpace = '\u200b';
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
const languages = {
|
|
16
|
+
js: 'javascript',
|
|
17
|
+
ts: 'typescript',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type SyntaxHighlighterProps = NaturalSyntaxHighlighterProps & {
|
|
21
|
+
fallback?: string;
|
|
22
|
+
};
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* NOTE: Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
|
|
@@ -25,39 +28,38 @@ export type SyntaxHighlighterProps = ThemedClassName<
|
|
|
25
28
|
* https://github.com/react-syntax-highlighter/react-syntax-highlighter
|
|
26
29
|
* https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/prism.html
|
|
27
30
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
...props
|
|
35
|
-
}: SyntaxHighlighterProps) => {
|
|
36
|
-
const { themeMode } = useThemeContext();
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<div className={mx('flex is-full p-1 overflow-hidden', classNames)}>
|
|
40
|
-
<NativeSyntaxHighlighter
|
|
41
|
-
className='!m-0 is-full overflow-auto scrollbar-thin'
|
|
42
|
-
language={languages[language as keyof typeof languages] || language}
|
|
43
|
-
style={themeMode === 'dark' ? dark : light}
|
|
44
|
-
customStyle={{
|
|
45
|
-
background: 'unset',
|
|
46
|
-
border: 'none',
|
|
47
|
-
boxShadow: 'none',
|
|
48
|
-
padding: 0,
|
|
49
|
-
margin: 0,
|
|
50
|
-
}}
|
|
51
|
-
{...props}
|
|
52
|
-
>
|
|
53
|
-
{/* Non-empty fallback prevents collapse. */}
|
|
54
|
-
{children || fallback}
|
|
55
|
-
</NativeSyntaxHighlighter>
|
|
56
|
-
</div>
|
|
57
|
-
);
|
|
58
|
-
};
|
|
31
|
+
export const SyntaxHighlighter = composable<HTMLDivElement, SyntaxHighlighterProps>(
|
|
32
|
+
(
|
|
33
|
+
{ children, language = 'text', fallback = zeroWidthSpace, classNames, className, style, ...nativeProps },
|
|
34
|
+
forwardedRef,
|
|
35
|
+
) => {
|
|
36
|
+
const { themeMode } = useThemeContext();
|
|
59
37
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
38
|
+
return (
|
|
39
|
+
<ScrollArea.Root {...composableProps({ classNames, className })} thin ref={forwardedRef}>
|
|
40
|
+
<ScrollArea.Viewport>
|
|
41
|
+
{/* NOTE: The div prevents NativeSyntaxHighlighter from managing scrolling. */}
|
|
42
|
+
<div role='none'>
|
|
43
|
+
<NativeSyntaxHighlighter
|
|
44
|
+
language={languages[language as keyof typeof languages] || language}
|
|
45
|
+
style={(style as { [key: string]: CSSProperties }) ?? (themeMode === 'dark' ? dark : light)}
|
|
46
|
+
customStyle={{
|
|
47
|
+
background: 'unset',
|
|
48
|
+
border: 'none',
|
|
49
|
+
boxShadow: 'none',
|
|
50
|
+
padding: 0,
|
|
51
|
+
margin: 0,
|
|
52
|
+
}}
|
|
53
|
+
{...nativeProps}
|
|
54
|
+
>
|
|
55
|
+
{/* Non-empty fallback prevents collapse. */}
|
|
56
|
+
{children || fallback}
|
|
57
|
+
</NativeSyntaxHighlighter>
|
|
58
|
+
</div>
|
|
59
|
+
</ScrollArea.Viewport>
|
|
60
|
+
</ScrollArea.Root>
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
SyntaxHighlighter.displayName = 'SyntaxHighlighter';
|