Skip to content

Commit 05d9298

Browse files
committed
noscript support
1 parent 2f907e4 commit 05d9298

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/app/advanced/[engine]/index.html/TestForm.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,30 @@ import { type TestInput, type TestOutput } from '@regexplanet/common';
77
import { useRouter } from 'next/navigation';
88
import { formDataToTestInput } from '@/functions/formDataToTestInput';
99
import { runTestPage } from './runTestPage';
10+
import Link from 'next/link';
1011

1112
type TestFormProps = {
1213
engine: RegexEngine;
1314
testUrl: string;
1415
testInput: TestInput;
1516
testOutput: TestOutput|null;
17+
inputRows: React.JSX.Element[];
1618
}
1719

1820
const pendingTestOutput: TestOutput = {
1921
success: true,
2022
html: `<p><img src="/images/spinner.gif" alt="spinner" /> Running, please wait...</p>`,
2123
};
2224

25+
function testInputToSearchParams(testInput: TestInput): URLSearchParams {
26+
const searchParams = new URLSearchParams();
27+
searchParams.set('regex', testInput.regex);
28+
searchParams.set('replacement', testInput.replacement);
29+
testInput.options.forEach(option => searchParams.append('option', option));
30+
testInput.inputs.forEach(input => searchParams.append('input', input));
31+
return searchParams;
32+
}
33+
2334
function setTestInput(testInput: TestInput): string {
2435
const searchParams = new URLSearchParams();
2536
searchParams.set('regex', testInput.regex);
@@ -40,12 +51,7 @@ export default function TestForm(props: TestFormProps) {
4051
//const [testInput, setTestInput] = useState<TestInput>(props.testInput);
4152
const testInput = props.testInput;
4253

43-
const inputRows = testInput.inputs.map((input, index) => (
44-
<div className="mb-2" key={`ikey${index}`}>
45-
<input type="text" className="form-control" id={`input${index}`} name="input" defaultValue={input} />
46-
</div>
47-
));
48-
console.log("render", testInput.inputs);
54+
const inputRows = props.inputRows;
4955

5056
const onClearResults = () => {
5157
setTestOutput(null);
@@ -96,7 +102,8 @@ export default function TestForm(props: TestFormProps) {
96102
}
97103
console.log("after more", localInput.inputs);
98104

99-
router.push(setTestInput(localInput));
105+
router.replace(setTestInput(localInput));
106+
router.refresh();
100107
}
101108

102109
const onFewerInputs = (event: React.MouseEvent<HTMLButtonElement>) => {
@@ -114,7 +121,8 @@ export default function TestForm(props: TestFormProps) {
114121
localInput.inputs.push('');
115122
}
116123
console.log("after fewer", localInput.inputs);
117-
router.push(setTestInput(localInput));
124+
router.replace(setTestInput(localInput));
125+
router.refresh();
118126
};
119127

120128
const onSwitchEngines = (event: React.MouseEvent<HTMLButtonElement>) => {
@@ -162,9 +170,10 @@ export default function TestForm(props: TestFormProps) {
162170
<h2 className="pt-3">Inputs</h2>
163171
{inputRows}
164172
<button type="submit" className="btn btn-primary">Test</button>
165-
<button className="ms-3 btn btn-outline-primary" onClick={onMoreInputs}>More inputs</button>
166-
{ testInput.inputs.length > 5 ? <button className="ms-3 btn btn-outline-primary" onClick={onFewerInputs}>Fewer inputs</button> : null }
167-
<button type="submit" className="btn btn-outline-primary float-end" onClick={onSwitchEngines}>Switch Engines</button>
173+
<button className="ms-3 btn btn-outline-primary scripting-required" onClick={onMoreInputs}>More inputs</button>
174+
{ testInput.inputs.length > 5 ? <button className="ms-3 btn btn-outline-primary scripting-required" onClick={onFewerInputs}>Fewer inputs</button> : null }
175+
<button type="submit" className="btn btn-outline-primary float-end scripting-required" onClick={onSwitchEngines}>Switch Engines</button>
176+
<Link className="btn btn-outline-primary float-end noscript-only" href={`/advanced/select.html?${testInputToSearchParams(testInput).toString()}`}>Switch Engines</Link>
168177
</form>
169178
</>
170179
);

src/app/advanced/[engine]/index.html/page.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ export default async function Page({
6060

6161
const testOutput = testInput.regex ? await runTestPage(testUrl, testInput) : null;
6262

63+
const inputRows = testInput.inputs.map((input, index) => (
64+
<div className="mb-2" key={`ikey${index}`}>
65+
<input type="text" className="form-control" id={`input${index}`} name="input" defaultValue={input} />
66+
</div>
67+
));
68+
console.log("render", testInput.inputs);
69+
70+
6371
return (
6472
<>
6573
<div className="d-flex justify-content-between align-items-center">
@@ -69,7 +77,7 @@ export default async function Page({
6977
<ShareLinks url={`https://regexplanet.com/advanced/${engine.handle}/index.html`} text={`Test your ${engine.short_name} regular expression`} />
7078
<hr />
7179
{flash}
72-
<TestForm engine={engine} testUrl={testUrl} testInput={testInput} testOutput={testOutput}/>
80+
<TestForm engine={engine} testUrl={testUrl} testInput={testInput} testOutput={testOutput} inputRows={inputRows}/>
7381
</>
7482
);
7583
}

src/app/global.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@
1111
--bs-body-line-height: 1.5;
1212
/*--bs-body-color: #212121;
1313
--bs-body-bg: #fff;*/
14+
}
15+
16+
@media (scripting: none) {
17+
.scripting-required {
18+
display: none;
19+
}
20+
}
21+
22+
@media (scripting: enabled) {
23+
.only-noscript {
24+
display: none;
25+
}
1426
}

0 commit comments

Comments
 (0)