@dxos/react-ui-syntax-highlighter 0.8.4-main.fd6878d → 0.8.4-main.fffef41
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 +33 -15
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +33 -15
- 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 +5 -2
- package/dist/types/src/Json/Json.d.ts.map +1 -1
- package/dist/types/src/Json/Json.stories.d.ts +15 -0
- package/dist/types/src/Json/Json.stories.d.ts.map +1 -0
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts +7 -3
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts.map +1 -1
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts +6 -3
- package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -14
- package/src/Json/Json.stories.tsx +106 -0
- package/src/Json/Json.tsx +22 -9
- package/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx +12 -7
- package/src/SyntaxHighlighter/SyntaxHighlighter.tsx +33 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-syntax-highlighter",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.fffef41",
|
|
4
4
|
"description": "A syntax highlighter wrapper.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -27,25 +27,28 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@preact-signals/safe-react": "^0.9.0",
|
|
29
29
|
"jsonpath": "^1.1.1",
|
|
30
|
-
"react-syntax-highlighter": "^15.6.1"
|
|
30
|
+
"react-syntax-highlighter": "^15.6.1",
|
|
31
|
+
"@dxos/util": "0.8.4-main.fffef41"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@types/jsonpath": "^0.2.4",
|
|
34
|
-
"@types/react": "~
|
|
35
|
-
"@types/react-dom": "~
|
|
35
|
+
"@types/react": "~19.2.2",
|
|
36
|
+
"@types/react-dom": "~19.2.2",
|
|
36
37
|
"@types/react-syntax-highlighter": "^15.5.13",
|
|
37
|
-
"react": "~
|
|
38
|
-
"react-dom": "~
|
|
39
|
-
"vite": "
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/react-ui
|
|
42
|
-
"@dxos/
|
|
38
|
+
"react": "~19.2.0",
|
|
39
|
+
"react-dom": "~19.2.0",
|
|
40
|
+
"vite": "7.1.9",
|
|
41
|
+
"@dxos/random": "0.8.4-main.fffef41",
|
|
42
|
+
"@dxos/react-ui": "0.8.4-main.fffef41",
|
|
43
|
+
"@dxos/react-ui-theme": "0.8.4-main.fffef41",
|
|
44
|
+
"@dxos/storybook-utils": "0.8.4-main.fffef41",
|
|
45
|
+
"@dxos/util": "0.8.4-main.fffef41"
|
|
43
46
|
},
|
|
44
47
|
"peerDependencies": {
|
|
45
|
-
"react": "
|
|
46
|
-
"react-dom": "
|
|
47
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
48
|
-
"@dxos/react-ui
|
|
48
|
+
"react": "^19.0.0",
|
|
49
|
+
"react-dom": "^19.0.0",
|
|
50
|
+
"@dxos/react-ui-theme": "0.8.4-main.fffef41",
|
|
51
|
+
"@dxos/react-ui": "0.8.4-main.fffef41"
|
|
49
52
|
},
|
|
50
53
|
"publishConfig": {
|
|
51
54
|
"access": "public"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
|
|
8
|
+
import { faker } from '@dxos/random';
|
|
9
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
10
|
+
|
|
11
|
+
import { Json } from './Json';
|
|
12
|
+
|
|
13
|
+
faker.seed(0);
|
|
14
|
+
|
|
15
|
+
const createNode = () => {
|
|
16
|
+
const data: Record<string, any> = {};
|
|
17
|
+
const keys = [...Array(faker.number.int({ min: 1, max: 5 }))].map(() => faker.lorem.word());
|
|
18
|
+
keys.forEach((key) => {
|
|
19
|
+
switch (faker.helpers.arrayElement(['object', 'string', 'number', 'boolean', 'null'])) {
|
|
20
|
+
case 'object':
|
|
21
|
+
data[key] = createNode();
|
|
22
|
+
break;
|
|
23
|
+
case 'string':
|
|
24
|
+
data[key] = faker.lorem.word();
|
|
25
|
+
break;
|
|
26
|
+
case 'number':
|
|
27
|
+
data[key] = faker.number.int();
|
|
28
|
+
break;
|
|
29
|
+
case 'boolean':
|
|
30
|
+
data[key] = faker.datatype.boolean();
|
|
31
|
+
break;
|
|
32
|
+
case 'null':
|
|
33
|
+
data[key] = null;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const createData = ({ depth = 2, children = 3 } = {}): any => {
|
|
42
|
+
const createChildren = (root: any, d = 0) => {
|
|
43
|
+
if (d < depth) {
|
|
44
|
+
const num = faker.number.int({ min: 1, max: Math.round(Math.log(depth + 1 - d) * children) });
|
|
45
|
+
root.children = [...new Array(num)].map(() => {
|
|
46
|
+
return createChildren(createNode(), d + 1);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return root;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return createChildren(createNode());
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const meta = {
|
|
57
|
+
title: 'ui/react-ui-syntax-highlighter/Json',
|
|
58
|
+
component: Json,
|
|
59
|
+
decorators: [withTheme, withLayout({ container: 'column' })],
|
|
60
|
+
} satisfies Meta<typeof Json>;
|
|
61
|
+
|
|
62
|
+
export default meta;
|
|
63
|
+
|
|
64
|
+
type Story = StoryObj<typeof Json>;
|
|
65
|
+
|
|
66
|
+
const data = createData();
|
|
67
|
+
|
|
68
|
+
export const Default: Story = {
|
|
69
|
+
args: {
|
|
70
|
+
classNames: 'text-sm',
|
|
71
|
+
data,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const Filter: Story = {
|
|
76
|
+
args: {
|
|
77
|
+
classNames: 'text-sm',
|
|
78
|
+
filter: true,
|
|
79
|
+
data,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const Large: Story = {
|
|
84
|
+
args: {
|
|
85
|
+
classNames: 'text-sm',
|
|
86
|
+
filter: true,
|
|
87
|
+
data: createData({ depth: 5 }),
|
|
88
|
+
replacer: {
|
|
89
|
+
maxDepth: 3,
|
|
90
|
+
maxArrayLen: 10,
|
|
91
|
+
maxStringLen: 10,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
};
|
|
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,25 +2,38 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
// TODO(burdon):
|
|
5
|
+
// TODO(burdon): Use to jsonpath-plus.
|
|
6
6
|
import jp from 'jsonpath';
|
|
7
7
|
import React, { useEffect, useState } from 'react';
|
|
8
8
|
|
|
9
9
|
import { Input, type ThemedClassName } from '@dxos/react-ui';
|
|
10
|
+
import { type CreateReplacerProps, createReplacer, safeStringify } from '@dxos/util';
|
|
10
11
|
|
|
11
12
|
import { SyntaxHighlighter } from '../SyntaxHighlighter';
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
const defaultClassNames = 'grow overflow-y-auto text-sm';
|
|
14
15
|
|
|
15
|
-
export
|
|
16
|
+
export type JsonProps = ThemedClassName<{
|
|
17
|
+
data?: any;
|
|
18
|
+
filter?: boolean;
|
|
19
|
+
replacer?: CreateReplacerProps;
|
|
20
|
+
testId?: string;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
export const Json = ({ filter, ...params }: JsonProps) => {
|
|
24
|
+
if (filter) {
|
|
25
|
+
return <JsonFilter {...params} />;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { classNames, data, replacer, testId } = params;
|
|
16
29
|
return (
|
|
17
|
-
<SyntaxHighlighter language='json' classNames={[
|
|
18
|
-
{
|
|
30
|
+
<SyntaxHighlighter language='json' classNames={[defaultClassNames, classNames]} data-testid={testId}>
|
|
31
|
+
{safeStringify(data, replacer && createReplacer(replacer), 2)}
|
|
19
32
|
</SyntaxHighlighter>
|
|
20
33
|
);
|
|
21
34
|
};
|
|
22
35
|
|
|
23
|
-
export const JsonFilter = ({ data: initialData,
|
|
36
|
+
export const JsonFilter = ({ classNames, data: initialData, replacer, testId }: JsonProps) => {
|
|
24
37
|
const [data, setData] = useState(initialData);
|
|
25
38
|
const [text, setText] = useState('');
|
|
26
39
|
const [error, setError] = useState<Error | null>(null);
|
|
@@ -42,15 +55,15 @@ export const JsonFilter = ({ data: initialData, classNames, testId }: JsonProps)
|
|
|
42
55
|
<div className='flex flex-col grow overflow-hidden'>
|
|
43
56
|
<Input.Root validationValence={error ? 'error' : 'success'}>
|
|
44
57
|
<Input.TextInput
|
|
45
|
-
classNames={['p-1
|
|
58
|
+
classNames={['p-1 pli-2 font-mono', error && 'border-red-500']}
|
|
46
59
|
variant='subdued'
|
|
47
60
|
value={text}
|
|
48
61
|
onChange={(event) => setText(event.target.value)}
|
|
49
62
|
placeholder='JSONPath (e.g., $.graph.nodes)'
|
|
50
63
|
/>
|
|
51
64
|
</Input.Root>
|
|
52
|
-
<SyntaxHighlighter language='json' classNames={[
|
|
53
|
-
{
|
|
65
|
+
<SyntaxHighlighter language='json' classNames={[defaultClassNames, classNames]} data-testid={testId}>
|
|
66
|
+
{safeStringify(data, replacer && createReplacer(replacer), 2)}
|
|
54
67
|
</SyntaxHighlighter>
|
|
55
68
|
</div>
|
|
56
69
|
);
|
|
@@ -2,19 +2,18 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@dxos-theme';
|
|
6
|
-
|
|
7
5
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
6
|
|
|
9
|
-
import { withTheme } from '@dxos/
|
|
7
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
8
|
+
import { trim } from '@dxos/util';
|
|
10
9
|
|
|
11
10
|
import { SyntaxHighlighter } from './SyntaxHighlighter';
|
|
12
11
|
|
|
13
|
-
const meta
|
|
12
|
+
const meta = {
|
|
14
13
|
title: 'ui/react-ui-syntax-highlighter/SyntaxHighlighter',
|
|
15
14
|
component: SyntaxHighlighter,
|
|
16
15
|
decorators: [withTheme],
|
|
17
|
-
}
|
|
16
|
+
} satisfies Meta<typeof SyntaxHighlighter>;
|
|
18
17
|
|
|
19
18
|
export default meta;
|
|
20
19
|
|
|
@@ -30,8 +29,14 @@ export const Default: Story = {
|
|
|
30
29
|
|
|
31
30
|
export const Typescript: Story = {
|
|
32
31
|
args: {
|
|
33
|
-
language: '
|
|
34
|
-
children:
|
|
32
|
+
language: 'tsx',
|
|
33
|
+
children: trim`
|
|
34
|
+
import React from 'react'
|
|
35
|
+
|
|
36
|
+
const Test = () => {
|
|
37
|
+
return <div>Test</div>
|
|
38
|
+
}
|
|
39
|
+
`,
|
|
35
40
|
},
|
|
36
41
|
};
|
|
37
42
|
|
|
@@ -3,13 +3,9 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import React from 'react';
|
|
6
|
-
import { type SyntaxHighlighterProps as
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// eslint-disable-next-line no-restricted-imports
|
|
10
|
-
import NativeSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/light-async';
|
|
11
|
-
// eslint-disable-next-line no-restricted-imports
|
|
12
|
-
import { a11yDark as dark, github as light } from 'react-syntax-highlighter/dist/esm/styles/hljs';
|
|
6
|
+
import { type SyntaxHighlighterProps as NaturalSyntaxHighlighterProps } from 'react-syntax-highlighter';
|
|
7
|
+
import NativeSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-async-light';
|
|
8
|
+
import { coldarkDark as dark, coldarkCold as light } from 'react-syntax-highlighter/dist/esm/styles/prism';
|
|
13
9
|
|
|
14
10
|
import { type ThemedClassName, useThemeContext } from '@dxos/react-ui';
|
|
15
11
|
import { mx } from '@dxos/react-ui-theme';
|
|
@@ -17,33 +13,51 @@ import { mx } from '@dxos/react-ui-theme';
|
|
|
17
13
|
const zeroWidthSpace = '\u200b';
|
|
18
14
|
|
|
19
15
|
export type SyntaxHighlighterProps = ThemedClassName<
|
|
20
|
-
|
|
16
|
+
NaturalSyntaxHighlighterProps & {
|
|
21
17
|
fallback?: string;
|
|
22
18
|
}
|
|
23
19
|
>;
|
|
24
20
|
|
|
25
|
-
light.hljs.background = '';
|
|
26
|
-
dark.hljs.background = '';
|
|
27
|
-
|
|
28
21
|
/**
|
|
22
|
+
* NOTE: Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
|
|
23
|
+
* The lightweight version will load specific language parsers asynchronously.
|
|
24
|
+
*
|
|
29
25
|
* https://github.com/react-syntax-highlighter/react-syntax-highlighter
|
|
26
|
+
* https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/prism.html
|
|
30
27
|
*/
|
|
28
|
+
// TODO(burdon): Replace with react-ui-editor (and reuse styles).
|
|
31
29
|
export const SyntaxHighlighter = ({
|
|
32
30
|
classNames,
|
|
33
31
|
children,
|
|
32
|
+
language = 'text',
|
|
34
33
|
fallback = zeroWidthSpace,
|
|
35
34
|
...props
|
|
36
35
|
}: SyntaxHighlighterProps) => {
|
|
37
36
|
const { themeMode } = useThemeContext();
|
|
38
37
|
|
|
39
38
|
return (
|
|
40
|
-
<
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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>
|
|
48
57
|
);
|
|
49
58
|
};
|
|
59
|
+
|
|
60
|
+
const languages = {
|
|
61
|
+
js: 'javascript',
|
|
62
|
+
ts: 'typescript',
|
|
63
|
+
};
|