|
1 | 1 | import React from "react" |
2 | | -import { CodeConfig, CodeSpring } from "../smooth-code" |
| 2 | +import { CodeSpring } from "../smooth-code" |
| 3 | +import { EditorSpring, EditorStep } from "../mini-editor" |
3 | 4 | import { |
4 | | - EditorSpring, |
5 | | - EditorProps, |
6 | | - EditorStep, |
7 | | -} from "../mini-editor" |
8 | | -import { CodeHikeConfig } from "../remark/config" |
| 5 | + CodeConfigProps, |
| 6 | + ElementProps, |
| 7 | + GlobalConfig, |
| 8 | +} from "../core/types" |
9 | 9 |
|
10 | | -export function Code( |
11 | | - props: EditorProps & Partial<CodeHikeConfig> |
12 | | -) { |
13 | | - const [step, setStep] = React.useState(props) |
| 10 | +type Props = { |
| 11 | + editorStep: EditorStep |
| 12 | + globalConfig: GlobalConfig |
| 13 | +} & ElementProps & |
| 14 | + CodeConfigProps |
| 15 | + |
| 16 | +export function Code(props: Props) { |
| 17 | + const { editorStep, globalConfig, ...codeConfigProps } = |
| 18 | + props |
| 19 | + const [step, setStep] = React.useState(editorStep) |
14 | 20 |
|
15 | 21 | function onTabClick(filename: string) { |
16 | | - const newStep = updateEditorStep(step, filename, null) |
| 22 | + const newStep = updateEditorStep( |
| 23 | + props.editorStep, |
| 24 | + filename, |
| 25 | + null |
| 26 | + ) |
17 | 27 | setStep({ ...step, ...newStep }) |
18 | 28 | } |
19 | 29 |
|
20 | | - return <InnerCode {...step} onTabClick={onTabClick} /> |
| 30 | + return ( |
| 31 | + <InnerCode |
| 32 | + editorStep={step} |
| 33 | + onTabClick={onTabClick} |
| 34 | + globalConfig={globalConfig} |
| 35 | + codeConfigProps={codeConfigProps} |
| 36 | + /> |
| 37 | + ) |
21 | 38 | } |
22 | 39 |
|
23 | | -// build the CodeConfig from props and props.codeConfig |
24 | | -export function mergeCodeConfig<T>( |
25 | | - props: Partial<CodeConfig> & { |
26 | | - codeConfig: Partial<CodeConfig> |
27 | | - } & T |
28 | | -) { |
29 | | - const { |
30 | | - lineNumbers, |
31 | | - showCopyButton, |
32 | | - showExpandButton, |
33 | | - minZoom, |
34 | | - maxZoom, |
35 | | - ...rest |
36 | | - } = props |
37 | | - const codeConfig = { |
38 | | - ...props.codeConfig, |
39 | | - maxZoom: |
40 | | - maxZoom == null ? props.codeConfig?.maxZoom : maxZoom, |
41 | | - minZoom: |
42 | | - minZoom == null ? props.codeConfig?.minZoom : minZoom, |
43 | | - horizontalCenter: |
44 | | - props.codeConfig?.horizontalCenter ?? |
45 | | - props.horizontalCenter, |
46 | | - lineNumbers: |
47 | | - lineNumbers == null |
48 | | - ? props.codeConfig?.lineNumbers |
49 | | - : lineNumbers, |
50 | | - showCopyButton: |
51 | | - showCopyButton == null |
52 | | - ? props.codeConfig?.showCopyButton |
53 | | - : showCopyButton, |
54 | | - showExpandButton: |
55 | | - showExpandButton == null |
56 | | - ? props.codeConfig?.showExpandButton |
57 | | - : showExpandButton, |
58 | | - rows: props.rows, |
59 | | - debug: props.debug ?? props.codeConfig?.debug, |
60 | | - } |
61 | | - return { ...rest, codeConfig } |
| 40 | +type InnerCodeProps = { |
| 41 | + onTabClick: (filename: string) => void |
| 42 | + globalConfig: GlobalConfig |
| 43 | + editorStep: EditorStep |
| 44 | + codeConfigProps: CodeConfigProps & ElementProps |
62 | 45 | } |
63 | 46 |
|
64 | 47 | export function InnerCode({ |
65 | 48 | onTabClick, |
66 | | - ...props |
67 | | -}: EditorProps & { |
68 | | - onTabClick?: (filename: string) => void |
69 | | -} & Partial<CodeHikeConfig>) { |
70 | | - const { className, style, codeConfig, ...editorProps } = |
71 | | - mergeCodeConfig(props) |
| 49 | + globalConfig, |
| 50 | + editorStep, |
| 51 | + codeConfigProps, |
| 52 | +}: InnerCodeProps) { |
| 53 | + const { className, style, ...config } = mergeCodeConfig( |
| 54 | + globalConfig, |
| 55 | + codeConfigProps |
| 56 | + ) |
72 | 57 |
|
73 | 58 | if ( |
74 | | - !props.southPanel && |
75 | | - props.files.length === 1 && |
76 | | - !props.files[0].name |
| 59 | + !editorStep.southPanel && |
| 60 | + editorStep.files.length === 1 && |
| 61 | + !editorStep.files[0].name |
77 | 62 | ) { |
78 | 63 | return ( |
79 | 64 | <div |
80 | 65 | className={`ch-codeblock not-prose ${ |
81 | 66 | className || "" |
82 | 67 | }`} |
83 | | - data-ch-theme={props.codeConfig?.themeName} |
| 68 | + data-ch-theme={globalConfig.themeName} |
84 | 69 | style={style} |
85 | 70 | > |
86 | 71 | <CodeSpring |
87 | 72 | className="ch-code" |
88 | | - config={codeConfig} |
89 | | - step={editorProps.files[0]} |
| 73 | + config={config} |
| 74 | + step={editorStep.files[0]} |
90 | 75 | /> |
91 | 76 | </div> |
92 | 77 | ) |
93 | 78 | } else { |
94 | 79 | const frameProps = { |
95 | | - ...editorProps?.frameProps, |
| 80 | + // ...editorStep?.frameProps, |
96 | 81 | onTabClick, |
97 | 82 | } |
98 | 83 | return ( |
99 | 84 | <div |
100 | 85 | className={`ch-codegroup not-prose ${ |
101 | 86 | className || "" |
102 | 87 | }`} |
103 | | - data-ch-theme={props.codeConfig?.themeName} |
| 88 | + data-ch-theme={globalConfig.themeName} |
104 | 89 | style={style} |
105 | 90 | > |
106 | 91 | <EditorSpring |
107 | | - {...editorProps} |
| 92 | + {...editorStep} |
108 | 93 | frameProps={frameProps} |
109 | | - codeConfig={codeConfig} |
| 94 | + codeConfig={config} |
110 | 95 | /> |
111 | 96 | </div> |
112 | 97 | ) |
113 | 98 | } |
114 | 99 | } |
115 | 100 |
|
| 101 | +export function mergeCodeConfig( |
| 102 | + globalConfig: GlobalConfig, |
| 103 | + local: CodeConfigProps & ElementProps |
| 104 | +) { |
| 105 | + const { |
| 106 | + // ignore these |
| 107 | + staticMediaQuery, |
| 108 | + themeName, |
| 109 | + triggerPosition, |
| 110 | + // keep the rest |
| 111 | + ...global |
| 112 | + } = globalConfig |
| 113 | + return { |
| 114 | + ...global, |
| 115 | + ...local, |
| 116 | + lineNumbers: local.lineNumbers ?? global.lineNumbers, |
| 117 | + maxZoom: local.maxZoom ?? global.maxZoom, |
| 118 | + minZoom: local.minZoom ?? global.minZoom, |
| 119 | + horizontalCenter: |
| 120 | + local.horizontalCenter ?? global.horizontalCenter, |
| 121 | + showCopyButton: |
| 122 | + local.showCopyButton ?? global.showCopyButton, |
| 123 | + } |
| 124 | +} |
| 125 | + |
116 | 126 | export function updateEditorStep( |
117 | 127 | step: EditorStep, |
118 | 128 | filename: string | undefined, |
|
0 commit comments