@dxos/react-ui-syntax-highlighter 0.8.4-main.c1de068 → 0.8.4-main.c85a9c8dae

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.c1de068",
3
+ "version": "0.8.4-main.c85a9c8dae",
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,28 @@
24
29
  "src"
25
30
  ],
26
31
  "dependencies": {
27
- "@preact-signals/safe-react": "^0.9.0",
28
- "jsonpath": "^1.1.1",
29
- "react-syntax-highlighter": "^15.5.0"
32
+ "jsonpath-plus": "^10.3.0",
33
+ "react-syntax-highlighter": "^15.6.1",
34
+ "@dxos/util": "0.8.4-main.c85a9c8dae"
30
35
  },
31
36
  "devDependencies": {
32
- "@types/jsonpath": "^0.2.4",
33
- "@types/react": "~18.2.0",
34
- "@types/react-dom": "~18.2.0",
37
+ "@types/react": "~19.2.7",
38
+ "@types/react-dom": "~19.2.3",
35
39
  "@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.c1de068",
40
- "@dxos/react-ui-theme": "0.8.4-main.c1de068",
41
- "@dxos/storybook-utils": "0.8.4-main.c1de068"
40
+ "react": "~19.2.3",
41
+ "react-dom": "~19.2.3",
42
+ "vite": "^7.1.11",
43
+ "@dxos/random": "0.8.4-main.c85a9c8dae",
44
+ "@dxos/util": "0.8.4-main.c85a9c8dae",
45
+ "@dxos/ui-theme": "0.8.4-main.c85a9c8dae",
46
+ "@dxos/storybook-utils": "0.8.4-main.c85a9c8dae",
47
+ "@dxos/react-ui": "0.8.4-main.c85a9c8dae"
42
48
  },
43
49
  "peerDependencies": {
44
- "react": "~18.2.0",
45
- "react-dom": "~18.2.0",
46
- "@dxos/react-ui": "0.8.4-main.c1de068",
47
- "@dxos/react-ui-theme": "0.8.4-main.c1de068"
50
+ "react": "~19.2.3",
51
+ "react-dom": "~19.2.3",
52
+ "@dxos/react-ui": "0.8.4-main.c85a9c8dae",
53
+ "@dxos/ui-theme": "0.8.4-main.c85a9c8dae"
48
54
  },
49
55
  "publishConfig": {
50
56
  "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,78 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- // TODO(burdon): Move to jsonpath-plus.
6
- import jp from 'jsonpath';
7
- import React, { useEffect, useState } from 'react';
5
+ import { JSONPath } from 'jsonpath-plus';
6
+ import React, { forwardRef, useEffect, useState } from 'react';
8
7
 
9
8
  import { Input, type ThemedClassName } from '@dxos/react-ui';
9
+ import { type CreateReplacerProps, createReplacer, safeStringify } from '@dxos/util';
10
10
 
11
11
  import { SyntaxHighlighter } from '../SyntaxHighlighter';
12
12
 
13
- export type JsonProps = ThemedClassName<{ data?: any; testId?: string }>;
13
+ export type JsonProps = ThemedClassName<{
14
+ data?: any;
15
+ filter?: boolean;
16
+ replacer?: CreateReplacerProps;
17
+ testId?: string;
18
+ }>;
14
19
 
15
- export const Json = ({ data, classNames, testId }: JsonProps) => {
20
+ export const Json = forwardRef<HTMLDivElement, JsonProps>((props, forwardedRef) => {
21
+ if (props.filter) {
22
+ return <JsonFilter {...props} />;
23
+ }
24
+
25
+ const { classNames, data, replacer, testId } = props;
16
26
  return (
17
- <SyntaxHighlighter language='json' classNames={['w-full', classNames]} data-testid={testId}>
18
- {JSON.stringify(data, null, 2)}
27
+ <SyntaxHighlighter
28
+ language='json'
29
+ classNames={['w-full overflow-y-auto text-sm', classNames]}
30
+ data-testid={testId}
31
+ ref={forwardedRef}
32
+ >
33
+ {safeStringify(data, replacer && createReplacer(replacer), 2)}
19
34
  </SyntaxHighlighter>
20
35
  );
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) {
36
+ });
37
+
38
+ export const JsonFilter = forwardRef<HTMLDivElement, JsonProps>(
39
+ ({ classNames, data: initialData, replacer, testId }, forwardedRef) => {
40
+ const [data, setData] = useState(initialData);
41
+ const [text, setText] = useState('');
42
+ const [error, setError] = useState<Error | null>(null);
43
+
44
+ useEffect(() => {
45
+ if (!initialData || !text.trim().length) {
35
46
  setData(initialData);
36
- setError(err as Error);
47
+ } else {
48
+ try {
49
+ setData(JSONPath({ path: text, json: initialData }));
50
+ setError(null);
51
+ } catch (err) {
52
+ setData(initialData);
53
+ setError(err as Error);
54
+ }
37
55
  }
38
- }
39
- }, [initialData, text]); // TODO(burdon): Need structural diff.
56
+ }, [initialData, text]); // TODO(burdon): Need structural diff.
40
57
 
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
- };
58
+ return (
59
+ <div className='flex flex-col h-full overflow-hidden' ref={forwardedRef}>
60
+ <Input.Root validationValence={error ? 'error' : 'success'}>
61
+ <Input.TextInput
62
+ classNames={['p-1 px-2 font-mono', error && 'border-rose-500']}
63
+ variant='subdued'
64
+ value={text}
65
+ placeholder='JSONPath (e.g., $.graph.nodes)'
66
+ onChange={(event) => setText(event.target.value)}
67
+ />
68
+ </Input.Root>
69
+ <SyntaxHighlighter
70
+ language='json'
71
+ classNames={['w-full overflow-y-auto text-sm', classNames]}
72
+ data-testid={testId}
73
+ >
74
+ {safeStringify(data, replacer && createReplacer(replacer), 2)}
75
+ </SyntaxHighlighter>
76
+ </div>
77
+ );
78
+ },
79
+ );
@@ -2,19 +2,24 @@
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 { withLayout, withTheme } from '@dxos/react-ui/testing';
8
+ import { trim } from '@dxos/util';
9
+
10
+ // @ts-ignore - Vite raw import.
11
+ import TEXT from '../../package.json?raw';
10
12
 
11
13
  import { SyntaxHighlighter } from './SyntaxHighlighter';
12
14
 
13
- const meta: Meta<typeof SyntaxHighlighter> = {
15
+ const meta = {
14
16
  title: 'ui/react-ui-syntax-highlighter/SyntaxHighlighter',
15
17
  component: SyntaxHighlighter,
16
- decorators: [withTheme],
17
- };
18
+ decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
19
+ parameters: {
20
+ layout: 'fullscreen',
21
+ },
22
+ } satisfies Meta<typeof SyntaxHighlighter>;
18
23
 
19
24
  export default meta;
20
25
 
@@ -24,19 +29,21 @@ export const Default: Story = {
24
29
  args: {
25
30
  language: 'json',
26
31
  classNames: 'text-sm',
27
- children: JSON.stringify({ message: 'DXOS', initialized: true }, null, 2),
32
+ children: TEXT,
28
33
  },
29
34
  };
30
35
 
31
36
  export const Typescript: Story = {
32
37
  args: {
33
- language: 'ts',
34
- children: 'const x = 100;',
38
+ language: 'tsx',
39
+ children: trim`
40
+ import React from 'react'
41
+
42
+ const Test = () => {
43
+ return <div>Test</div>
44
+ }
45
+ `,
35
46
  },
36
47
  };
37
48
 
38
- export const Empty: Story = {
39
- args: {
40
- children: false,
41
- },
42
- };
49
+ export const Empty: Story = {};
@@ -3,47 +3,64 @@
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
- import { type ThemedClassName, useThemeContext } from '@dxos/react-ui';
15
- import { mx } from '@dxos/react-ui-theme';
10
+ import { ScrollArea, type ThemedClassName, useThemeContext } from '@dxos/react-ui';
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
+ <ScrollArea.Root thin classNames={mx('p1', classNames)}>
40
+ <ScrollArea.Viewport>
41
+ <div role='none'>
42
+ <NativeSyntaxHighlighter
43
+ language={languages[language as keyof typeof languages] || language}
44
+ style={themeMode === 'dark' ? dark : light}
45
+ customStyle={{
46
+ background: 'unset',
47
+ border: 'none',
48
+ boxShadow: 'none',
49
+ padding: 0,
50
+ margin: 0,
51
+ }}
52
+ {...props}
53
+ >
54
+ {/* Non-empty fallback prevents collapse. */}
55
+ {children || fallback}
56
+ </NativeSyntaxHighlighter>
57
+ </div>
58
+ </ScrollArea.Viewport>
59
+ </ScrollArea.Root>
48
60
  );
49
61
  };
62
+
63
+ const languages = {
64
+ js: 'javascript',
65
+ ts: 'typescript',
66
+ };