Skip to content

Commit 131d0d0

Browse files
committed
style: modify Map/Set style.
1 parent 35ed469 commit 131d0d0

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

core/src/comps/Copied.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,23 @@ export const Copied = <T extends object, K extends TagType>(props: CopiedProps<T
2828

2929
const click = (event: React.MouseEvent<SVGSVGElement, MouseEvent>) => {
3030
event.stopPropagation();
31-
let copyText = JSON.stringify(
32-
value,
33-
(key, value) => {
34-
if (typeof value === 'bigint') {
35-
return value.toString();
36-
}
37-
return value;
38-
},
39-
2,
40-
);
41-
if (typeof value === 'number' && value === Infinity) copyText = 'Infinity';
42-
if (typeof value === 'number' && isNaN(value)) copyText = 'NaN';
43-
if (typeof value === 'bigint') copyText = value + 'n';
31+
let copyText = '';
32+
if (typeof value === 'number' && value === Infinity) {
33+
copyText = 'Infinity';
34+
} else if (typeof value === 'number' && isNaN(value)) {
35+
copyText = 'NaN';
36+
} else if (typeof value === 'bigint') {
37+
copyText = value + 'n';
38+
} else if (value instanceof Date) {
39+
copyText = value.toLocaleString();
40+
} else {
41+
copyText = JSON.stringify(value, null, 2);
42+
}
43+
onCopied && onCopied(copyText, value);
44+
setCopied(true);
4445
navigator.clipboard
4546
.writeText(copyText)
4647
.then(() => {
47-
onCopied && onCopied(copyText, value);
48-
setCopied(true);
4948
const timer = setTimeout(() => {
5049
setCopied(false);
5150
clearTimeout(timer);

core/src/store.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ export const useDispatchStore = () => {
4949
return useContext(DispatchContext);
5050
};
5151

52-
export interface ProviderProps {
52+
export interface ProviderProps<T extends TagType> {
5353
initialState?: InitialState<object>;
54-
initialTypes?: InitialTypesState;
54+
initialTypes?: InitialTypesState<T>;
5555
}
5656

57-
export const Provider: React.FC<PropsWithChildren<ProviderProps>> = ({
57+
export const Provider = <T extends TagType>({
5858
children,
5959
initialState: init,
6060
initialTypes,
61-
}) => {
61+
}: PropsWithChildren<ProviderProps<T>>) => {
6262
const [state, dispatch] = useReducer(reducer, Object.assign({}, initialState, init));
6363
const [showTools, showToolsDispatch] = useShowTools();
6464
const [expands, expandsDispatch] = useExpands();

core/src/store/Types.tsx

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import { FC, PropsWithChildren, ComponentPropsWithoutRef, createContext, useContext, useReducer } from 'react';
1+
import { PropsWithChildren, ComponentPropsWithoutRef, createContext, useContext, useReducer } from 'react';
22

33
export type TagType = React.ElementType | keyof JSX.IntrinsicElements;
44

5-
type TypesElementProps<T extends TagType> = {
5+
type TypesElementProps<T extends TagType = 'span'> = {
66
as?: T;
77
render?: (props: TypesElement<T>, result: { type: 'type' | 'value'; value?: unknown }) => React.ReactNode;
88
'data-type'?: string;
99
};
1010

1111
export type TypesElement<T extends TagType> = TypesElementProps<T> & ComponentPropsWithoutRef<T>;
12-
export type InitialTypesState = {
12+
export type InitialTypesState<T extends TagType = 'span'> = {
1313
displayDataTypes?: boolean;
14-
Url?: TypesElement<TagType>;
15-
Str?: TypesElement<TagType>;
16-
Undefined?: TypesElement<TagType>;
17-
Null?: TypesElement<TagType>;
18-
True?: TypesElement<TagType>;
19-
False?: TypesElement<TagType>;
20-
Date?: TypesElement<TagType>;
21-
Float?: TypesElement<TagType>;
22-
Set?: TypesElement<TagType>;
23-
Int?: TypesElement<TagType>;
24-
Map?: TypesElement<TagType>;
25-
Nan?: TypesElement<TagType>;
26-
Bigint?: TypesElement<TagType>;
14+
Url?: TypesElement<T>;
15+
Str?: TypesElement<T>;
16+
Undefined?: TypesElement<T>;
17+
Null?: TypesElement<T>;
18+
True?: TypesElement<T>;
19+
False?: TypesElement<T>;
20+
Date?: TypesElement<T>;
21+
Float?: TypesElement<T>;
22+
Set?: TypesElement<T>;
23+
Int?: TypesElement<T>;
24+
Map?: TypesElement<T>;
25+
Nan?: TypesElement<T>;
26+
Bigint?: TypesElement<T>;
2727
};
28-
type Dispatch = React.Dispatch<InitialTypesState>;
28+
type Dispatch<T extends TagType> = React.Dispatch<InitialTypesState<T>>;
2929

30-
const initialState: InitialTypesState = {
30+
const initialState: InitialTypesState<TagType | 'span'> = {
3131
Str: {
3232
as: 'span',
3333
'data-type': 'string',
@@ -67,6 +67,7 @@ const initialState: InitialTypesState = {
6767
Map: {
6868
style: {
6969
color: 'var(--w-rjv-type-map-color, #268bd2)',
70+
marginRight: 3,
7071
},
7172
as: 'span',
7273
'data-type': 'map',
@@ -103,6 +104,7 @@ const initialState: InitialTypesState = {
103104
Set: {
104105
style: {
105106
color: 'var(--w-rjv-type-set-color, #268bd2)',
107+
marginRight: 3,
106108
},
107109
as: 'span',
108110
'data-type': 'set',
@@ -146,9 +148,10 @@ const initialState: InitialTypesState = {
146148
children: 'date',
147149
},
148150
};
149-
const Context = createContext<InitialTypesState>(initialState);
150151

151-
const reducer = (state: InitialTypesState, action: InitialTypesState) => ({
152+
const Context = createContext<InitialTypesState<TagType>>(initialState);
153+
154+
const reducer = <T extends TagType>(state: InitialTypesState<T>, action: InitialTypesState<T>) => ({
152155
...state,
153156
...action,
154157
});
@@ -157,7 +160,7 @@ export const useTypesStore = () => {
157160
return useContext(Context);
158161
};
159162

160-
const DispatchTypes = createContext<Dispatch>(() => {});
163+
const DispatchTypes = createContext<Dispatch<TagType>>(() => {});
161164
DispatchTypes.displayName = 'JVR.DispatchTypes';
162165

163166
export function useTypes() {
@@ -168,17 +171,17 @@ export function useTypesDispatch() {
168171
return useContext(DispatchTypes);
169172
}
170173

171-
interface TypesProps {
172-
initial: InitialTypesState;
173-
dispatch: Dispatch;
174+
interface TypesProps<T extends TagType> {
175+
initial: InitialTypesState<T>;
176+
dispatch: Dispatch<TagType>;
174177
}
175178

176-
export const Types: FC<PropsWithChildren<TypesProps>> = ({ initial, dispatch, children }) => {
179+
export function Types<T extends TagType>({ initial, dispatch, children }: PropsWithChildren<TypesProps<T>>) {
177180
return (
178-
<Context.Provider value={initial}>
181+
<Context.Provider value={initial as unknown as InitialTypesState<TagType>}>
179182
<DispatchTypes.Provider value={dispatch}>{children}</DispatchTypes.Provider>
180183
</Context.Provider>
181184
);
182-
};
185+
}
183186

184187
Types.displayName = 'JVR.Types';

core/src/types/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export const TypeDate: FC<{ children?: Date } & Omit<TypeProps, 'children'>> = (
284284

285285
const isRender = render && typeof render === 'function';
286286
const type = isRender && render({ ...reset, style }, { type: 'type', value: children });
287-
const childStr = children?.toString();
287+
const childStr = children instanceof Date ? children.toLocaleString() : children;
288288
const child =
289289
isRender && render({ ...reset, children: childStr, className: 'w-rjv-value' }, { type: 'value', value: children });
290290

0 commit comments

Comments
 (0)