@dxos/react-ui-syntax-highlighter 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-syntax-highlighter",
3
- "version": "0.8.4-main.84f28bd",
3
+ "version": "0.8.4-main.ae835ea",
4
4
  "description": "A syntax highlighter wrapper.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -10,6 +10,7 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
+ "source": "./src/index.ts",
13
14
  "types": "./dist/types/src/index.d.ts",
14
15
  "browser": "./dist/lib/browser/index.mjs",
15
16
  "node": "./dist/lib/node-esm/index.mjs"
@@ -26,25 +27,27 @@
26
27
  "dependencies": {
27
28
  "@preact-signals/safe-react": "^0.9.0",
28
29
  "jsonpath": "^1.1.1",
29
- "react-syntax-highlighter": "^15.5.0"
30
+ "react-syntax-highlighter": "^15.6.1"
30
31
  },
31
32
  "devDependencies": {
32
33
  "@types/jsonpath": "^0.2.4",
33
- "@types/react": "~18.2.0",
34
- "@types/react-dom": "~18.2.0",
34
+ "@types/react": "~19.2.2",
35
+ "@types/react-dom": "~19.2.2",
35
36
  "@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.84f28bd",
40
- "@dxos/react-ui-theme": "0.8.4-main.84f28bd",
41
- "@dxos/storybook-utils": "0.8.4-main.84f28bd"
37
+ "react": "~19.2.0",
38
+ "react-dom": "~19.2.0",
39
+ "vite": "7.1.9",
40
+ "@dxos/random": "0.8.4-main.ae835ea",
41
+ "@dxos/react-ui-theme": "0.8.4-main.ae835ea",
42
+ "@dxos/react-ui": "0.8.4-main.ae835ea",
43
+ "@dxos/util": "0.8.4-main.ae835ea",
44
+ "@dxos/storybook-utils": "0.8.4-main.ae835ea"
42
45
  },
43
46
  "peerDependencies": {
44
- "react": "~18.2.0",
45
- "react-dom": "~18.2.0",
46
- "@dxos/react-ui": "0.8.4-main.84f28bd",
47
- "@dxos/react-ui-theme": "0.8.4-main.84f28bd"
47
+ "react": "^19.0.0",
48
+ "react-dom": "^19.0.0",
49
+ "@dxos/react-ui": "0.8.4-main.ae835ea",
50
+ "@dxos/react-ui-theme": "0.8.4-main.ae835ea"
48
51
  },
49
52
  "publishConfig": {
50
53
  "access": "public"
@@ -0,0 +1,93 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+
7
+ import { faker } from '@dxos/random';
8
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
9
+
10
+ import { Json } from './Json';
11
+
12
+ faker.seed(0);
13
+
14
+ const createNode = () => {
15
+ const data: Record<string, any> = {};
16
+ const keys = [...Array(faker.number.int({ min: 1, max: 5 }))].map(() => faker.lorem.word());
17
+ keys.forEach((key) => {
18
+ switch (faker.helpers.arrayElement(['object', 'string', 'number', 'boolean', 'null'])) {
19
+ case 'object':
20
+ data[key] = createNode();
21
+ break;
22
+ case 'string':
23
+ data[key] = faker.lorem.word();
24
+ break;
25
+ case 'number':
26
+ data[key] = faker.number.int();
27
+ break;
28
+ case 'boolean':
29
+ data[key] = faker.datatype.boolean();
30
+ break;
31
+ case 'null':
32
+ data[key] = null;
33
+ break;
34
+ }
35
+ });
36
+
37
+ return data;
38
+ };
39
+
40
+ const createData = ({ depth = 2, children = 3 } = {}): any => {
41
+ const createChildren = (root: any, d = 0) => {
42
+ if (d < depth) {
43
+ const num = faker.number.int({ min: 1, max: Math.round(Math.log(depth + 1 - d) * children) });
44
+ root.children = [...new Array(num)].map(() => {
45
+ return createChildren(createNode(), d + 1);
46
+ });
47
+ }
48
+
49
+ return root;
50
+ };
51
+
52
+ return createChildren(createNode());
53
+ };
54
+
55
+ const meta = {
56
+ title: 'ui/react-ui-syntax-highlighter/Json',
57
+ component: Json,
58
+ decorators: [withTheme, withLayout({ container: 'column' })],
59
+ } satisfies Meta<typeof Json>;
60
+
61
+ export default meta;
62
+
63
+ type Story = StoryObj<typeof Json>;
64
+
65
+ const data = createData();
66
+
67
+ export const Default: Story = {
68
+ args: {
69
+ classNames: 'text-sm',
70
+ data,
71
+ },
72
+ };
73
+
74
+ export const Filter: Story = {
75
+ args: {
76
+ classNames: 'text-sm',
77
+ filter: true,
78
+ data,
79
+ },
80
+ };
81
+
82
+ export const Large: Story = {
83
+ args: {
84
+ classNames: 'text-sm',
85
+ filter: true,
86
+ data: createData({ depth: 5 }),
87
+ replacer: {
88
+ maxDepth: 3,
89
+ maxArrayLen: 10,
90
+ maxStringLen: 10,
91
+ },
92
+ },
93
+ };
package/src/Json/Json.tsx CHANGED
@@ -2,26 +2,37 @@
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
7
  import React, { useEffect, useState } from 'react';
8
8
 
9
9
  import { Input, type ThemedClassName } from '@dxos/react-ui';
10
- import { mx } from '@dxos/react-ui-theme';
11
10
 
12
11
  import { SyntaxHighlighter } from '../SyntaxHighlighter';
13
12
 
14
- export type JsonProps = ThemedClassName<{ data?: any; testId?: string }>;
13
+ const defaultClassNames = '!m-0 grow overflow-y-auto text-sm';
15
14
 
16
- export const Json = ({ data, classNames, testId }: JsonProps) => {
15
+ export type JsonProps = ThemedClassName<{
16
+ data?: any;
17
+ filter?: boolean;
18
+ replacer?: CreateReplacerProps;
19
+ testId?: string;
20
+ }>;
21
+
22
+ export const Json = ({ filter, ...params }: JsonProps) => {
23
+ if (filter) {
24
+ return <JsonFilter {...params} />;
25
+ }
26
+
27
+ const { classNames, data, replacer, testId } = params;
17
28
  return (
18
- <SyntaxHighlighter language='json' classNames={['w-full', classNames]} data-testid={testId}>
19
- {JSON.stringify(data, null, 2)}
29
+ <SyntaxHighlighter language='json' classNames={[defaultClassNames, classNames]} data-testid={testId}>
30
+ {JSON.stringify(data, replacer && createReplacer(replacer), 2)}
20
31
  </SyntaxHighlighter>
21
32
  );
22
33
  };
23
34
 
24
- export const JsonFilter = ({ data: initialData, classNames, testId }: JsonProps) => {
35
+ export const JsonFilter = ({ classNames, data: initialData, replacer, testId }: JsonProps) => {
25
36
  const [data, setData] = useState(initialData);
26
37
  const [text, setText] = useState('');
27
38
  const [error, setError] = useState<Error | null>(null);
@@ -43,16 +54,77 @@ export const JsonFilter = ({ data: initialData, classNames, testId }: JsonProps)
43
54
  <div className='flex flex-col grow overflow-hidden'>
44
55
  <Input.Root validationValence={error ? 'error' : 'success'}>
45
56
  <Input.TextInput
46
- classNames={mx('p-1 px-2 font-mono', error && 'border-red-500')}
57
+ classNames={['p-1 px-2 font-mono', error && 'border-red-500']}
47
58
  variant='subdued'
48
59
  value={text}
49
60
  onChange={(event) => setText(event.target.value)}
50
61
  placeholder='JSONPath (e.g., $.graph.nodes)'
51
62
  />
52
63
  </Input.Root>
53
- <SyntaxHighlighter language='json' classNames={mx('grow overflow-y-auto', classNames)} data-testid={testId}>
54
- {JSON.stringify(data, null, 2)}
64
+ <SyntaxHighlighter language='json' classNames={[defaultClassNames, classNames]} data-testid={testId}>
65
+ {JSON.stringify(data, replacer && createReplacer(replacer), 2)}
55
66
  </SyntaxHighlighter>
56
67
  </div>
57
68
  );
58
69
  };
70
+
71
+ export type CreateReplacerProps = {
72
+ omit?: string[];
73
+ parse?: string[];
74
+ maxDepth?: number;
75
+ maxArrayLen?: number;
76
+ maxStringLen?: number;
77
+ };
78
+
79
+ export type JsonReplacer = (this: any, key: string, value: any) => any;
80
+
81
+ export const createReplacer = ({
82
+ omit,
83
+ parse,
84
+ maxDepth,
85
+ maxArrayLen,
86
+ maxStringLen,
87
+ }: CreateReplacerProps): JsonReplacer => {
88
+ let currentDepth = 0;
89
+ const depthMap = new WeakMap<object, number>();
90
+
91
+ return function (this: any, key: string, value: any) {
92
+ // Track depth.
93
+ if (key === '') {
94
+ currentDepth = 0;
95
+ } else if (this && typeof this === 'object') {
96
+ const parentDepth = depthMap.get(this) ?? 0;
97
+ currentDepth = parentDepth + 1;
98
+ }
99
+
100
+ // Store depth for this object.
101
+ if (value && typeof value === 'object') {
102
+ depthMap.set(value, currentDepth);
103
+
104
+ // Check max depth.
105
+ if (maxDepth != null && currentDepth >= maxDepth) {
106
+ return Array.isArray(value) ? `[{ length: ${value.length} }]` : `{ keys: ${Object.keys(value).length} }`;
107
+ }
108
+ }
109
+
110
+ // Apply other filters.
111
+ if (omit?.includes(key)) {
112
+ return undefined;
113
+ }
114
+ if (parse?.includes(key) && typeof value === 'string') {
115
+ try {
116
+ return JSON.parse(value);
117
+ } catch {
118
+ return value;
119
+ }
120
+ }
121
+ if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
122
+ return `[length: ${value.length}]`;
123
+ }
124
+ if (maxStringLen != null && typeof value === 'string' && value.length > maxStringLen) {
125
+ return value.slice(0, maxStringLen) + '...';
126
+ }
127
+
128
+ return value;
129
+ };
130
+ };
@@ -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,13 +3,9 @@
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
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
- 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 text-baseText', classNames)}>
40
+ <NativeSyntaxHighlighter
41
+ className='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
+ };