@dxos/react-ui-syntax-highlighter 0.8.4-main.b97322e → 0.8.4-main.bc674ce

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/package.json CHANGED
@@ -1,15 +1,20 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-syntax-highlighter",
3
- "version": "0.8.4-main.b97322e",
3
+ "version": "0.8.4-main.bc674ce",
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": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
17
+ "source": "./src/index.ts",
13
18
  "types": "./dist/types/src/index.d.ts",
14
19
  "browser": "./dist/lib/browser/index.mjs",
15
20
  "node": "./dist/lib/node-esm/index.mjs"
@@ -24,27 +29,29 @@
24
29
  "src"
25
30
  ],
26
31
  "dependencies": {
27
- "@preact-signals/safe-react": "^0.9.0",
28
32
  "jsonpath": "^1.1.1",
29
- "react-syntax-highlighter": "^15.5.0"
33
+ "react-syntax-highlighter": "^15.6.1",
34
+ "@dxos/util": "0.8.4-main.bc674ce"
30
35
  },
31
36
  "devDependencies": {
32
37
  "@types/jsonpath": "^0.2.4",
33
- "@types/react": "~18.2.0",
34
- "@types/react-dom": "~18.2.0",
38
+ "@types/react": "~19.2.7",
39
+ "@types/react-dom": "~19.2.3",
35
40
  "@types/react-syntax-highlighter": "^15.5.13",
36
- "react": "~18.2.0",
37
- "react-dom": "~18.2.0",
38
- "vite": "5.4.7",
39
- "@dxos/react-ui": "0.8.4-main.b97322e",
40
- "@dxos/react-ui-theme": "0.8.4-main.b97322e",
41
- "@dxos/storybook-utils": "0.8.4-main.b97322e"
41
+ "react": "~19.2.3",
42
+ "react-dom": "~19.2.3",
43
+ "vite": "7.1.9",
44
+ "@dxos/random": "0.8.4-main.bc674ce",
45
+ "@dxos/react-ui": "0.8.4-main.bc674ce",
46
+ "@dxos/storybook-utils": "0.8.4-main.bc674ce",
47
+ "@dxos/util": "0.8.4-main.bc674ce",
48
+ "@dxos/ui-theme": "0.8.4-main.bc674ce"
42
49
  },
43
50
  "peerDependencies": {
44
- "react": "~18.2.0",
45
- "react-dom": "~18.2.0",
46
- "@dxos/react-ui": "0.8.4-main.b97322e",
47
- "@dxos/react-ui-theme": "0.8.4-main.b97322e"
51
+ "react": "~19.2.3",
52
+ "react-dom": "~19.2.3",
53
+ "@dxos/react-ui": "0.8.4-main.bc674ce",
54
+ "@dxos/ui-theme": "0.8.4-main.bc674ce"
48
55
  },
49
56
  "publishConfig": {
50
57
  "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({ layout: '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,56 +2,79 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- // TODO(burdon): Move to jsonpath-plus.
5
+ // TODO(burdon): Use to jsonpath-plus.
6
6
  import jp from 'jsonpath';
7
- import React, { useEffect, useState } from 'react';
7
+ import React, { forwardRef, 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
- export type JsonProps = ThemedClassName<{ data?: any; testId?: string }>;
14
+ export type JsonProps = ThemedClassName<{
15
+ data?: any;
16
+ filter?: boolean;
17
+ replacer?: CreateReplacerProps;
18
+ testId?: string;
19
+ }>;
14
20
 
15
- export const Json = ({ data, classNames, testId }: JsonProps) => {
21
+ export const Json = forwardRef<HTMLDivElement, JsonProps>((props, forwardedRef) => {
22
+ if (props.filter) {
23
+ return <JsonFilter {...props} />;
24
+ }
25
+
26
+ const { classNames, data, replacer, testId } = props;
16
27
  return (
17
- <SyntaxHighlighter language='json' classNames={['w-full', classNames]} data-testid={testId}>
18
- {JSON.stringify(data, null, 2)}
28
+ <SyntaxHighlighter
29
+ language='json'
30
+ classNames={['is-full overflow-y-auto text-sm', classNames]}
31
+ data-testid={testId}
32
+ ref={forwardedRef}
33
+ >
34
+ {safeStringify(data, replacer && createReplacer(replacer), 2)}
19
35
  </SyntaxHighlighter>
20
36
  );
21
- };
22
-
23
- export const JsonFilter = ({ data: initialData, classNames, testId }: JsonProps) => {
24
- const [data, setData] = useState(initialData);
25
- const [text, setText] = useState('');
26
- const [error, setError] = useState<Error | null>(null);
27
- useEffect(() => {
28
- if (!initialData || !text.trim().length) {
29
- setData(initialData);
30
- } else {
31
- try {
32
- setData(jp.query(initialData, text));
33
- setError(null);
34
- } catch (err) {
37
+ });
38
+
39
+ export const JsonFilter = forwardRef<HTMLDivElement, JsonProps>(
40
+ ({ classNames, data: initialData, replacer, testId }, forwardedRef) => {
41
+ const [data, setData] = useState(initialData);
42
+ const [text, setText] = useState('');
43
+ const [error, setError] = useState<Error | null>(null);
44
+
45
+ useEffect(() => {
46
+ if (!initialData || !text.trim().length) {
35
47
  setData(initialData);
36
- setError(err as Error);
48
+ } else {
49
+ try {
50
+ setData(jp.query(initialData, text));
51
+ setError(null);
52
+ } catch (err) {
53
+ setData(initialData);
54
+ setError(err as Error);
55
+ }
37
56
  }
38
- }
39
- }, [initialData, text]); // TODO(burdon): Need structural diff.
57
+ }, [initialData, text]); // TODO(burdon): Need structural diff.
40
58
 
41
- return (
42
- <div className='flex flex-col grow overflow-hidden'>
43
- <Input.Root validationValence={error ? 'error' : 'success'}>
44
- <Input.TextInput
45
- classNames={['p-1 px-2 font-mono', error && 'border-red-500']}
46
- variant='subdued'
47
- value={text}
48
- onChange={(event) => setText(event.target.value)}
49
- placeholder='JSONPath (e.g., $.graph.nodes)'
50
- />
51
- </Input.Root>
52
- <SyntaxHighlighter language='json' classNames={['grow overflow-y-auto', classNames]} data-testid={testId}>
53
- {JSON.stringify(data, null, 2)}
54
- </SyntaxHighlighter>
55
- </div>
56
- );
57
- };
59
+ return (
60
+ <div className='flex flex-col bs-full overflow-hidden' ref={forwardedRef}>
61
+ <Input.Root validationValence={error ? 'error' : 'success'}>
62
+ <Input.TextInput
63
+ classNames={['p-1 pli-2 font-mono', error && 'border-rose-500']}
64
+ variant='subdued'
65
+ value={text}
66
+ placeholder='JSONPath (e.g., $.graph.nodes)'
67
+ onChange={(event) => setText(event.target.value)}
68
+ />
69
+ </Input.Root>
70
+ <SyntaxHighlighter
71
+ language='json'
72
+ classNames={['is-full overflow-y-auto text-sm', classNames]}
73
+ data-testid={testId}
74
+ >
75
+ {safeStringify(data, replacer && createReplacer(replacer), 2)}
76
+ </SyntaxHighlighter>
77
+ </div>
78
+ );
79
+ },
80
+ );
@@ -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/storybook-utils';
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: Meta<typeof SyntaxHighlighter> = {
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: 'ts',
34
- children: 'const x = 100;',
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,47 +3,61 @@
3
3
  //
4
4
 
5
5
  import React from 'react';
6
- import { type SyntaxHighlighterProps as NativeSyntaxHighlighterProps } from 'react-syntax-highlighter';
7
- // Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
8
- // Lightweight version will load specific language parsers asynchronously.
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 { github as light, a11yDark as dark } 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
- import { mx } from '@dxos/react-ui-theme';
11
+ import { mx } from '@dxos/ui-theme';
16
12
 
17
13
  const zeroWidthSpace = '\u200b';
18
14
 
19
15
  export type SyntaxHighlighterProps = ThemedClassName<
20
- NativeSyntaxHighlighterProps & {
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
- <NativeSyntaxHighlighter
41
- className={mx('w-full p-0 font-thin overflow-auto scrollbar-thin !text-baseText', classNames)}
42
- style={themeMode === 'dark' ? dark : light}
43
- {...props}
44
- >
45
- {/* Non-empty fallback prevents collapse. */}
46
- {children || fallback}
47
- </NativeSyntaxHighlighter>
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
+ };