Skip to content

Commit 703eae6

Browse files
authored
Generate reactive <style> from React (#206)
1 parent a344f77 commit 703eae6

File tree

3 files changed

+69
-39
lines changed

3 files changed

+69
-39
lines changed

ts/DiffOptions.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {isLegitKeypress} from './file_diff';
77
export interface Props {
88
options: Partial<DiffOptions>;
99
setOptions: (newOptions: Partial<DiffOptions>) => void;
10+
defaultMaxDiffWidth: number;
11+
maxDiffWidth: number;
12+
setMaxDiffWidth: (maxDiffWidth: number) => void;
1013
isVisible: boolean;
1114
setIsVisible: (isVisible: boolean) => void;
1215
}
@@ -49,7 +52,7 @@ const popupStyle: React.CSSProperties = {
4952
};
5053

5154
export function DiffOptionsControl(props: Props) {
52-
const {options, setOptions, isVisible, setIsVisible} = props;
55+
const {options, setOptions, isVisible, setIsVisible, maxDiffWidth, setMaxDiffWidth} = props;
5356

5457
const togglePopup = () => {
5558
setIsVisible(!isVisible);
@@ -69,6 +72,9 @@ export function DiffOptionsControl(props: Props) {
6972
const changeDiffAlgorithm: React.ChangeEventHandler<HTMLSelectElement> = e => {
7073
setOptions({...options, diffAlgorithm: e.currentTarget.value as DiffAlgorithm});
7174
};
75+
const changeMaxDiffWidth: React.ChangeEventHandler<HTMLInputElement> = e => {
76+
setMaxDiffWidth(e.currentTarget.valueAsNumber);
77+
};
7278

7379
React.useEffect(() => {
7480
const handleKeydown = (e: KeyboardEvent) => {
@@ -169,12 +175,26 @@ export function DiffOptionsControl(props: Props) {
169175
</select>
170176
</td>
171177
</tr>
178+
<tr>
179+
<td style={{textAlign: 'right', verticalAlign: 'top'}}>Max line width:</td>
180+
<td>
181+
<input
182+
type="number"
183+
min={1}
184+
max={1000}
185+
value={maxDiffWidth}
186+
onChange={changeMaxDiffWidth}
187+
/>{' '}
188+
lines
189+
</td>
190+
</tr>
172191
</tbody>
173192
</table>
174193

175-
<p>
176-
<code>git diff {diffOptsStr}</code>
177-
</p>
194+
{diffOptsStr ? <pre>git diff {diffOptsStr}</pre> : null}
195+
{maxDiffWidth !== props.defaultMaxDiffWidth ? (
196+
<pre>git config webdiff.maxDiffWidth {maxDiffWidth}</pre>
197+
) : null}
178198
</div>
179199
</>
180200
) : null}

ts/Root.tsx

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import {ImageDiffMode} from './ImageDiffModeSelector';
99
import {filePairDisplayName} from './utils';
1010
import {DiffOptionsControl} from './DiffOptions';
1111
import {KeyboardShortcuts} from './codediff/KeyboardShortcuts';
12+
import {GitConfig} from './options';
1213

1314
declare const pairs: FilePair[];
1415
declare const initialIdx: number;
16+
declare const GIT_CONFIG: GitConfig;
1517

1618
type Props = RouteComponentProps<{index?: string}>;
1719

@@ -20,6 +22,7 @@ export function Root(props: Props) {
2022
const [pdiffMode, setPDiffMode] = React.useState<PerceptualDiffMode>('off');
2123
const [imageDiffMode, setImageDiffMode] = React.useState<ImageDiffMode>('side-by-side');
2224
const [diffOptions, setDiffOptions] = React.useState<Partial<DiffOptions>>({});
25+
const [maxDiffWidth, setMaxDiffWidth] = React.useState(GIT_CONFIG.webdiff.maxDiffWidth);
2326
const [showKeyboardHelp, setShowKeyboardHelp] = React.useState(false);
2427
const [showOptions, setShowOptions] = React.useState(false);
2528
// An explicit list is better, unless there are a ton of files.
@@ -69,38 +72,49 @@ export function Root(props: Props) {
6972
};
7073
}, [idx, selectIndex]);
7174

75+
const inlineStyle = `
76+
td.code {
77+
width: ${1 + maxDiffWidth}ch;
78+
}`;
79+
7280
return (
73-
<div>
74-
<DiffOptionsControl
75-
options={diffOptions}
76-
setOptions={setDiffOptions}
77-
isVisible={showOptions}
78-
setIsVisible={setShowOptions}
79-
/>
80-
<FileSelector
81-
selectedFileIndex={idx}
82-
filePairs={pairs}
83-
fileChangeHandler={selectIndex}
84-
mode={fileSelectorMode}
85-
onChangeMode={setFileSelectorMode}
86-
/>
87-
{showKeyboardHelp ? (
88-
<KeyboardShortcuts
89-
onClose={() => {
90-
setShowKeyboardHelp(false);
91-
}}
81+
<>
82+
<style>{inlineStyle}</style>
83+
<div>
84+
<DiffOptionsControl
85+
options={diffOptions}
86+
setOptions={setDiffOptions}
87+
maxDiffWidth={maxDiffWidth}
88+
setMaxDiffWidth={setMaxDiffWidth}
89+
defaultMaxDiffWidth={GIT_CONFIG.webdiff.maxDiffWidth}
90+
isVisible={showOptions}
91+
setIsVisible={setShowOptions}
92+
/>
93+
<FileSelector
94+
selectedFileIndex={idx}
95+
filePairs={pairs}
96+
fileChangeHandler={selectIndex}
97+
mode={fileSelectorMode}
98+
onChangeMode={setFileSelectorMode}
99+
/>
100+
{showKeyboardHelp ? (
101+
<KeyboardShortcuts
102+
onClose={() => {
103+
setShowKeyboardHelp(false);
104+
}}
105+
/>
106+
) : null}
107+
<DiffView
108+
key={`diff-${idx}`}
109+
thinFilePair={filePair}
110+
imageDiffMode={imageDiffMode}
111+
pdiffMode={pdiffMode}
112+
diffOptions={diffOptions}
113+
changeImageDiffMode={setImageDiffMode}
114+
changePDiffMode={setPDiffMode}
115+
changeDiffOptions={setDiffOptions}
92116
/>
93-
) : null}
94-
<DiffView
95-
key={`diff-${idx}`}
96-
thinFilePair={filePair}
97-
imageDiffMode={imageDiffMode}
98-
pdiffMode={pdiffMode}
99-
diffOptions={diffOptions}
100-
changeImageDiffMode={setImageDiffMode}
101-
changePDiffMode={setPDiffMode}
102-
changeDiffOptions={setDiffOptions}
103-
/>
104-
</div>
117+
</div>
118+
</>
105119
);
106120
}

ts/options.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ export interface ColorsConfig {
3030
declare const GIT_CONFIG: GitConfig;
3131

3232
export function injectStylesFromConfig() {
33-
const config = GIT_CONFIG.webdiff;
3433
const colors = GIT_CONFIG['webdiff.colors'];
3534
document.write(`
3635
<style>
37-
td.code {
38-
width: ${1 + config.maxDiffWidth}ch;
39-
}
4036
.diff .delete, .before.replace {
4137
background-color: ${colors.delete};
4238
}

0 commit comments

Comments
 (0)