mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-12-04 19:04:02 +01:00
Merge branch 'main' of https://github.com/iib0011/omni-tools into merge-video-tool
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { expect, describe, it, vi } from 'vitest';
|
||||
|
||||
// Mock FFmpeg since it doesn't support Node.js
|
||||
vi.mock('@ffmpeg/ffmpeg', () => ({
|
||||
FFmpeg: vi.fn().mockImplementation(() => ({
|
||||
loaded: false,
|
||||
load: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
exec: vi.fn().mockResolvedValue(undefined),
|
||||
readFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4, 5])),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined)
|
||||
}))
|
||||
}));
|
||||
|
||||
vi.mock('@ffmpeg/util', () => ({
|
||||
fetchFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4, 5]))
|
||||
}));
|
||||
|
||||
import { changeAudioSpeed } from './service';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
describe('changeAudioSpeed', () => {
|
||||
it('should return a new File with the correct name and type', async () => {
|
||||
const mockAudioData = new Uint8Array([0, 1, 2, 3, 4, 5]);
|
||||
const mockFile = new File([mockAudioData], 'test.mp3', {
|
||||
type: 'audio/mp3'
|
||||
});
|
||||
const options: InitialValuesType = {
|
||||
newSpeed: 2,
|
||||
outputFormat: 'mp3'
|
||||
};
|
||||
const result = await changeAudioSpeed(mockFile, options);
|
||||
expect(result).toBeInstanceOf(File);
|
||||
expect(result?.name).toBe('test-2x.mp3');
|
||||
expect(result?.type).toBe('audio/mp3');
|
||||
});
|
||||
|
||||
it('should return null if input is null', async () => {
|
||||
const options: InitialValuesType = {
|
||||
newSpeed: 2,
|
||||
outputFormat: 'mp3'
|
||||
};
|
||||
const result = await changeAudioSpeed(null, options);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
120
src/pages/tools/audio/change-speed/index.tsx
Normal file
120
src/pages/tools/audio/change-speed/index.tsx
Normal file
@@ -0,0 +1,120 @@
|
||||
import { Box, FormControlLabel, Radio, RadioGroup } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import { InitialValuesType } from './types';
|
||||
import ToolAudioInput from '@components/input/ToolAudioInput';
|
||||
import ToolFileResult from '@components/result/ToolFileResult';
|
||||
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
||||
import RadioWithTextField from '@components/options/RadioWithTextField';
|
||||
import { changeAudioSpeed } from './service';
|
||||
|
||||
const initialValues: InitialValuesType = {
|
||||
newSpeed: 2,
|
||||
outputFormat: 'mp3'
|
||||
};
|
||||
|
||||
const formatOptions = [
|
||||
{ label: 'MP3', value: 'mp3' },
|
||||
{ label: 'AAC', value: 'aac' },
|
||||
{ label: 'WAV', value: 'wav' }
|
||||
];
|
||||
|
||||
export default function ChangeSpeed({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<File | null>(null);
|
||||
const [result, setResult] = useState<File | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const compute = async (
|
||||
optionsValues: InitialValuesType,
|
||||
input: File | null
|
||||
) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const newFile = await changeAudioSpeed(input, optionsValues);
|
||||
setResult(newFile);
|
||||
} catch (err) {
|
||||
setResult(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> | null = ({
|
||||
values,
|
||||
updateField
|
||||
}) => [
|
||||
{
|
||||
title: 'New Audio Speed',
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
value={values.newSpeed.toString()}
|
||||
onOwnChange={(val) => updateField('newSpeed', Number(val))}
|
||||
description="Default multiplier: 2 means 2x faster"
|
||||
type="number"
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Output Format',
|
||||
component: (
|
||||
<Box mt={2}>
|
||||
<RadioGroup
|
||||
row
|
||||
value={values.outputFormat}
|
||||
onChange={(e) =>
|
||||
updateField(
|
||||
'outputFormat',
|
||||
e.target.value as 'mp3' | 'aac' | 'wav'
|
||||
)
|
||||
}
|
||||
>
|
||||
{formatOptions.map((opt) => (
|
||||
<FormControlLabel
|
||||
key={opt.value}
|
||||
value={opt.value}
|
||||
control={<Radio />}
|
||||
label={opt.label}
|
||||
/>
|
||||
))}
|
||||
</RadioGroup>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
];
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolAudioInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
title={'Input Audio'}
|
||||
/>
|
||||
}
|
||||
resultComponent={
|
||||
loading ? (
|
||||
<ToolFileResult title="Setting Speed" value={null} loading={true} />
|
||||
) : (
|
||||
<ToolFileResult
|
||||
title="Edited Audio"
|
||||
value={result}
|
||||
extension={result ? result.name.split('.').pop() : undefined}
|
||||
/>
|
||||
)
|
||||
}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is ${title}?`, description: longDescription }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
13
src/pages/tools/audio/change-speed/meta.ts
Normal file
13
src/pages/tools/audio/change-speed/meta.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('audio', {
|
||||
name: 'Change speed',
|
||||
path: 'change-speed',
|
||||
icon: 'material-symbols-light:speed-outline',
|
||||
description:
|
||||
'This online utility lets you change the speed of an audio. You can speed it up or slow it down.',
|
||||
shortDescription: 'Quickly change audio speed',
|
||||
keywords: ['change', 'speed'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
80
src/pages/tools/audio/change-speed/service.ts
Normal file
80
src/pages/tools/audio/change-speed/service.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { InitialValuesType } from './types';
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
|
||||
function computeAudioFilter(speed: number): string {
|
||||
if (speed <= 2 && speed >= 0.5) {
|
||||
return `atempo=${speed}`;
|
||||
}
|
||||
const filters: string[] = [];
|
||||
let remainingSpeed = speed;
|
||||
while (remainingSpeed > 2.0) {
|
||||
filters.push('atempo=2.0');
|
||||
remainingSpeed /= 2.0;
|
||||
}
|
||||
while (remainingSpeed < 0.5) {
|
||||
filters.push('atempo=0.5');
|
||||
remainingSpeed /= 0.5;
|
||||
}
|
||||
filters.push(`atempo=${remainingSpeed.toFixed(2)}`);
|
||||
return filters.join(',');
|
||||
}
|
||||
|
||||
export async function changeAudioSpeed(
|
||||
input: File | null,
|
||||
options: InitialValuesType
|
||||
): Promise<File | null> {
|
||||
if (!input) return null;
|
||||
const { newSpeed, outputFormat } = options;
|
||||
let ffmpeg: FFmpeg | null = null;
|
||||
let ffmpegLoaded = false;
|
||||
try {
|
||||
ffmpeg = new FFmpeg();
|
||||
if (!ffmpegLoaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
ffmpegLoaded = true;
|
||||
}
|
||||
const fileName = input.name;
|
||||
const outputName = `output.${outputFormat}`;
|
||||
await ffmpeg.writeFile(fileName, await fetchFile(input));
|
||||
const audioFilter = computeAudioFilter(newSpeed);
|
||||
let args = ['-i', fileName, '-filter:a', audioFilter];
|
||||
if (outputFormat === 'mp3') {
|
||||
args.push('-b:a', '192k', '-f', 'mp3', outputName);
|
||||
} else if (outputFormat === 'aac') {
|
||||
args.push('-c:a', 'aac', '-b:a', '192k', '-f', 'adts', outputName);
|
||||
} else if (outputFormat === 'wav') {
|
||||
args.push(
|
||||
'-acodec',
|
||||
'pcm_s16le',
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
'-f',
|
||||
'wav',
|
||||
outputName
|
||||
);
|
||||
}
|
||||
await ffmpeg.exec(args);
|
||||
const data = await ffmpeg.readFile(outputName);
|
||||
let mimeType = 'audio/mp3';
|
||||
if (outputFormat === 'aac') mimeType = 'audio/aac';
|
||||
if (outputFormat === 'wav') mimeType = 'audio/wav';
|
||||
const blob = new Blob([data], { type: mimeType });
|
||||
const newFile = new File(
|
||||
[blob],
|
||||
fileName.replace(/\.[^/.]+$/, `-${newSpeed}x.${outputFormat}`),
|
||||
{ type: mimeType }
|
||||
);
|
||||
await ffmpeg.deleteFile(fileName);
|
||||
await ffmpeg.deleteFile(outputName);
|
||||
return newFile;
|
||||
} catch (err) {
|
||||
console.error(`Failed to process audio: ${err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
4
src/pages/tools/audio/change-speed/types.ts
Normal file
4
src/pages/tools/audio/change-speed/types.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export type InitialValuesType = {
|
||||
newSpeed: number;
|
||||
outputFormat: 'mp3' | 'aac' | 'wav';
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect, vi, beforeAll } from 'vitest';
|
||||
|
||||
// Mock the service module BEFORE importing it
|
||||
vi.mock('./service', () => ({
|
||||
extractAudioFromVideo: vi.fn(async (input, options) => {
|
||||
const ext = options.outputFormat;
|
||||
return new File([new Blob(['audio data'])], `mock_audio.${ext}`, {
|
||||
type: `audio/${ext}`
|
||||
});
|
||||
})
|
||||
}));
|
||||
|
||||
import { extractAudioFromVideo } from './service';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
function createMockVideoFile(): File {
|
||||
return new File(['video data'], 'test.mp4', { type: 'video/mp4' });
|
||||
}
|
||||
|
||||
describe('extractAudioFromVideo (mocked)', () => {
|
||||
let videoFile: File;
|
||||
|
||||
beforeAll(() => {
|
||||
videoFile = createMockVideoFile();
|
||||
});
|
||||
|
||||
it('should extract audio as AAC', async () => {
|
||||
const options: InitialValuesType = { outputFormat: 'aac' };
|
||||
const audioFile = await extractAudioFromVideo(videoFile, options);
|
||||
expect(audioFile).toBeInstanceOf(File);
|
||||
expect(audioFile.name.endsWith('.aac')).toBe(true);
|
||||
expect(audioFile.type).toBe('audio/aac');
|
||||
});
|
||||
|
||||
it('should extract audio as MP3', async () => {
|
||||
const options: InitialValuesType = { outputFormat: 'mp3' };
|
||||
const audioFile = await extractAudioFromVideo(videoFile, options);
|
||||
expect(audioFile).toBeInstanceOf(File);
|
||||
expect(audioFile.name.endsWith('.mp3')).toBe(true);
|
||||
expect(audioFile.type).toBe('audio/mp3');
|
||||
});
|
||||
|
||||
it('should extract audio as WAV', async () => {
|
||||
const options: InitialValuesType = { outputFormat: 'wav' };
|
||||
const audioFile = await extractAudioFromVideo(videoFile, options);
|
||||
expect(audioFile).toBeInstanceOf(File);
|
||||
expect(audioFile.name.endsWith('.wav')).toBe(true);
|
||||
expect(audioFile.type).toBe('audio/wav');
|
||||
});
|
||||
});
|
||||
91
src/pages/tools/audio/extract-audio/index.tsx
Normal file
91
src/pages/tools/audio/extract-audio/index.tsx
Normal file
@@ -0,0 +1,91 @@
|
||||
import { Box } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import { extractAudioFromVideo } from './service';
|
||||
import { InitialValuesType } from './types';
|
||||
import ToolVideoInput from '@components/input/ToolVideoInput';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import ToolFileResult from '@components/result/ToolFileResult';
|
||||
import SelectWithDesc from '@components/options/SelectWithDesc';
|
||||
|
||||
const initialValues: InitialValuesType = {
|
||||
outputFormat: 'aac'
|
||||
};
|
||||
|
||||
export default function ExtractAudio({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [file, setFile] = useState<File | null>(null);
|
||||
const [audioFile, setAudioFile] = useState<File | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> = ({
|
||||
values,
|
||||
updateField
|
||||
}) => {
|
||||
return [
|
||||
{
|
||||
title: 'Output Format',
|
||||
component: (
|
||||
<Box>
|
||||
<SelectWithDesc
|
||||
selected={values.outputFormat}
|
||||
onChange={(value) => {
|
||||
updateField('outputFormat', value.toString());
|
||||
}}
|
||||
options={[
|
||||
{ label: 'AAC', value: 'aac' },
|
||||
{ label: 'MP3', value: 'mp3' },
|
||||
{ label: 'WAV', value: 'wav' }
|
||||
]}
|
||||
description={
|
||||
'Select the format for the audio to be extracted as.'
|
||||
}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
const compute = async (values: InitialValuesType, input: File | null) => {
|
||||
if (!input) return;
|
||||
try {
|
||||
setLoading(true);
|
||||
const audioFileObj = await extractAudioFromVideo(input, values);
|
||||
setAudioFile(audioFileObj);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={file}
|
||||
inputComponent={
|
||||
<ToolVideoInput value={file} onChange={setFile} title={'Input Video'} />
|
||||
}
|
||||
resultComponent={
|
||||
loading ? (
|
||||
<ToolFileResult
|
||||
title={'Extracting Audio'}
|
||||
value={null}
|
||||
loading={true}
|
||||
/>
|
||||
) : (
|
||||
<ToolFileResult title={'Extracted Audio'} value={audioFile} />
|
||||
)
|
||||
}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is ${title}?`, description: longDescription }}
|
||||
setInput={setFile}
|
||||
/>
|
||||
);
|
||||
}
|
||||
26
src/pages/tools/audio/extract-audio/meta.ts
Normal file
26
src/pages/tools/audio/extract-audio/meta.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('audio', {
|
||||
name: 'Extract audio',
|
||||
path: 'extract-audio',
|
||||
icon: 'mdi:music-note',
|
||||
description:
|
||||
'Extract the audio track from a video file and save it as a separate audio file in your chosen format (AAC, MP3, WAV).',
|
||||
shortDescription:
|
||||
'Extract audio from video files (MP4, MOV, etc.) to AAC, MP3, or WAV.',
|
||||
keywords: [
|
||||
'extract',
|
||||
'audio',
|
||||
'video',
|
||||
'mp3',
|
||||
'aac',
|
||||
'wav',
|
||||
'audio extraction',
|
||||
'media',
|
||||
'convert'
|
||||
],
|
||||
longDescription:
|
||||
'This tool allows you to extract the audio track from a video file (such as MP4, MOV, AVI, etc.) and save it as a standalone audio file in your preferred format (AAC, MP3, or WAV). Useful for podcasts, music, or any scenario where you need just the audio from a video.',
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
70
src/pages/tools/audio/extract-audio/service.ts
Normal file
70
src/pages/tools/audio/extract-audio/service.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
const ffmpeg = new FFmpeg();
|
||||
|
||||
export async function extractAudioFromVideo(
|
||||
input: File,
|
||||
options: InitialValuesType
|
||||
): Promise<File> {
|
||||
if (!ffmpeg.loaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
}
|
||||
|
||||
const inputName = 'input.mp4';
|
||||
await ffmpeg.writeFile(inputName, await fetchFile(input));
|
||||
|
||||
const configuredOutputAudioFormat = options.outputFormat;
|
||||
const outputName = `output.${configuredOutputAudioFormat}`;
|
||||
const args: string[] = ['-i', inputName, '-vn'];
|
||||
|
||||
if (configuredOutputAudioFormat === 'mp3') {
|
||||
args.push(
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
'-b:a',
|
||||
'192k',
|
||||
'-f',
|
||||
'mp3',
|
||||
outputName
|
||||
);
|
||||
} else if (configuredOutputAudioFormat === 'wav') {
|
||||
args.push(
|
||||
'-acodec',
|
||||
'pcm_s16le',
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
'-f',
|
||||
'wav',
|
||||
outputName
|
||||
);
|
||||
} else {
|
||||
// Default to AAC or copy
|
||||
args.push('-acodec', 'copy', outputName);
|
||||
}
|
||||
|
||||
await ffmpeg.exec(args);
|
||||
|
||||
const extractedAudio = await ffmpeg.readFile(outputName);
|
||||
|
||||
return new File(
|
||||
[
|
||||
new Blob([extractedAudio], {
|
||||
type: `audio/${configuredOutputAudioFormat}`
|
||||
})
|
||||
],
|
||||
`${input.name.replace(
|
||||
/\.[^/.]+$/,
|
||||
''
|
||||
)}_audio.${configuredOutputAudioFormat}`,
|
||||
{ type: `audio/${configuredOutputAudioFormat}` }
|
||||
);
|
||||
}
|
||||
3
src/pages/tools/audio/extract-audio/types.ts
Normal file
3
src/pages/tools/audio/extract-audio/types.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export type InitialValuesType = {
|
||||
outputFormat: string;
|
||||
};
|
||||
11
src/pages/tools/audio/index.ts
Normal file
11
src/pages/tools/audio/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { tool as audioMergeAudio } from './merge-audio/meta';
|
||||
import { tool as audioTrim } from './trim/meta';
|
||||
import { tool as audioChangeSpeed } from './change-speed/meta';
|
||||
import { tool as audioExtractAudio } from './extract-audio/meta';
|
||||
|
||||
export const audioTools = [
|
||||
audioExtractAudio,
|
||||
audioChangeSpeed,
|
||||
audioTrim,
|
||||
audioMergeAudio
|
||||
];
|
||||
112
src/pages/tools/audio/merge-audio/index.tsx
Normal file
112
src/pages/tools/audio/merge-audio/index.tsx
Normal file
@@ -0,0 +1,112 @@
|
||||
import { Box, FormControlLabel, Radio, RadioGroup } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import { InitialValuesType } from './types';
|
||||
import ToolMultipleAudioInput, {
|
||||
MultiAudioInput
|
||||
} from '@components/input/ToolMultipleAudioInput';
|
||||
import ToolFileResult from '@components/result/ToolFileResult';
|
||||
import { mergeAudioFiles } from './service';
|
||||
|
||||
const initialValues: InitialValuesType = {
|
||||
outputFormat: 'mp3'
|
||||
};
|
||||
|
||||
const formatOptions = [
|
||||
{ label: 'MP3', value: 'mp3' },
|
||||
{ label: 'AAC', value: 'aac' },
|
||||
{ label: 'WAV', value: 'wav' }
|
||||
];
|
||||
|
||||
export default function MergeAudio({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<MultiAudioInput[]>([]);
|
||||
const [result, setResult] = useState<File | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const compute = async (
|
||||
optionsValues: InitialValuesType,
|
||||
input: MultiAudioInput[]
|
||||
) => {
|
||||
if (input.length === 0) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const files = input.map((item) => item.file);
|
||||
const mergedFile = await mergeAudioFiles(files, optionsValues);
|
||||
setResult(mergedFile);
|
||||
} catch (err) {
|
||||
console.error(`Failed to merge audio: ${err}`);
|
||||
setResult(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> | null = ({
|
||||
values,
|
||||
updateField
|
||||
}) => [
|
||||
{
|
||||
title: 'Output Format',
|
||||
component: (
|
||||
<Box mt={2}>
|
||||
<RadioGroup
|
||||
row
|
||||
value={values.outputFormat}
|
||||
onChange={(e) =>
|
||||
updateField(
|
||||
'outputFormat',
|
||||
e.target.value as 'mp3' | 'aac' | 'wav'
|
||||
)
|
||||
}
|
||||
>
|
||||
{formatOptions.map((opt) => (
|
||||
<FormControlLabel
|
||||
key={opt.value}
|
||||
value={opt.value}
|
||||
control={<Radio />}
|
||||
label={opt.label}
|
||||
/>
|
||||
))}
|
||||
</RadioGroup>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
];
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolMultipleAudioInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
accept={['audio/*', '.mp3', '.wav', '.aac']}
|
||||
title={'Input Audio Files'}
|
||||
type="audio"
|
||||
/>
|
||||
}
|
||||
resultComponent={
|
||||
loading ? (
|
||||
<ToolFileResult title="Merging Audio" value={null} loading={true} />
|
||||
) : (
|
||||
<ToolFileResult
|
||||
title="Merged Audio"
|
||||
value={result}
|
||||
extension={result ? result.name.split('.').pop() : undefined}
|
||||
/>
|
||||
)
|
||||
}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is ${title}?`, description: longDescription }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { expect, describe, it, vi } from 'vitest';
|
||||
|
||||
// Mock FFmpeg since it doesn't support Node.js
|
||||
vi.mock('@ffmpeg/ffmpeg', () => ({
|
||||
FFmpeg: vi.fn().mockImplementation(() => ({
|
||||
loaded: false,
|
||||
load: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
exec: vi.fn().mockResolvedValue(undefined),
|
||||
readFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4, 5])),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined)
|
||||
}))
|
||||
}));
|
||||
|
||||
vi.mock('@ffmpeg/util', () => ({
|
||||
fetchFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4, 5]))
|
||||
}));
|
||||
|
||||
import { mergeAudioFiles } from './service';
|
||||
|
||||
describe('mergeAudioFiles', () => {
|
||||
it('should merge multiple audio files', async () => {
|
||||
// Create mock audio files
|
||||
const mockAudioData1 = new Uint8Array([0, 1, 2, 3, 4, 5]);
|
||||
const mockAudioData2 = new Uint8Array([6, 7, 8, 9, 10, 11]);
|
||||
|
||||
const mockFile1 = new File([mockAudioData1], 'test1.mp3', {
|
||||
type: 'audio/mp3'
|
||||
});
|
||||
const mockFile2 = new File([mockAudioData2], 'test2.mp3', {
|
||||
type: 'audio/mp3'
|
||||
});
|
||||
|
||||
const options = {
|
||||
outputFormat: 'mp3' as const
|
||||
};
|
||||
|
||||
const result = await mergeAudioFiles([mockFile1, mockFile2], options);
|
||||
expect(result).toBeInstanceOf(File);
|
||||
expect(result.name).toBe('merged_audio.mp3');
|
||||
expect(result.type).toBe('audio/mp3');
|
||||
});
|
||||
|
||||
it('should handle different output formats', async () => {
|
||||
const mockAudioData = new Uint8Array([0, 1, 2, 3, 4, 5]);
|
||||
const mockFile = new File([mockAudioData], 'test.wav', {
|
||||
type: 'audio/wav'
|
||||
});
|
||||
|
||||
const options = {
|
||||
outputFormat: 'aac' as const
|
||||
};
|
||||
|
||||
const result = await mergeAudioFiles([mockFile], options);
|
||||
expect(result).toBeInstanceOf(File);
|
||||
expect(result.name).toBe('merged_audio.aac');
|
||||
expect(result.type).toBe('audio/aac');
|
||||
});
|
||||
|
||||
it('should throw error when no input files provided', async () => {
|
||||
const options = {
|
||||
outputFormat: 'mp3' as const
|
||||
};
|
||||
|
||||
try {
|
||||
await mergeAudioFiles([], options);
|
||||
expect.fail('Should have thrown an error');
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect((error as Error).message).toBe('No input files provided');
|
||||
}
|
||||
});
|
||||
});
|
||||
26
src/pages/tools/audio/merge-audio/meta.ts
Normal file
26
src/pages/tools/audio/merge-audio/meta.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('audio', {
|
||||
name: 'Merge Audio',
|
||||
path: 'merge-audio',
|
||||
icon: 'fluent:merge-20-regular',
|
||||
description:
|
||||
'Combine multiple audio files into a single audio file by concatenating them in sequence.',
|
||||
shortDescription: 'Merge multiple audio files into one (MP3, AAC, WAV).',
|
||||
keywords: [
|
||||
'merge',
|
||||
'audio',
|
||||
'combine',
|
||||
'concatenate',
|
||||
'join',
|
||||
'mp3',
|
||||
'aac',
|
||||
'wav',
|
||||
'audio editing',
|
||||
'multiple files'
|
||||
],
|
||||
longDescription:
|
||||
'This tool allows you to merge multiple audio files into a single file by concatenating them in the order you upload them. Perfect for combining podcast segments, music tracks, or any audio files that need to be joined together. Supports various audio formats including MP3, AAC, and WAV.',
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
115
src/pages/tools/audio/merge-audio/service.ts
Normal file
115
src/pages/tools/audio/merge-audio/service.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
const ffmpeg = new FFmpeg();
|
||||
|
||||
export async function mergeAudioFiles(
|
||||
inputs: File[],
|
||||
options: InitialValuesType
|
||||
): Promise<File> {
|
||||
if (!ffmpeg.loaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
}
|
||||
|
||||
if (inputs.length === 0) {
|
||||
throw new Error('No input files provided');
|
||||
}
|
||||
|
||||
const { outputFormat } = options;
|
||||
const outputName = `output.${outputFormat}`;
|
||||
|
||||
// 1. Convert all inputs to WAV
|
||||
const tempWavNames: string[] = [];
|
||||
for (let i = 0; i < inputs.length; i++) {
|
||||
const inputName = `input${i}`;
|
||||
const tempWavName = `temp${i}.wav`;
|
||||
await ffmpeg.writeFile(inputName, await fetchFile(inputs[i]));
|
||||
await ffmpeg.exec([
|
||||
'-i',
|
||||
inputName,
|
||||
'-acodec',
|
||||
'pcm_s16le',
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
tempWavName
|
||||
]);
|
||||
tempWavNames.push(tempWavName);
|
||||
await ffmpeg.deleteFile(inputName);
|
||||
}
|
||||
|
||||
// 2. Create file list for concat
|
||||
const fileListName = 'filelist.txt';
|
||||
const fileListContent = tempWavNames
|
||||
.map((name) => `file '${name}'`)
|
||||
.join('\n');
|
||||
await ffmpeg.writeFile(fileListName, fileListContent);
|
||||
|
||||
// 3. Concatenate WAV files
|
||||
const concatWav = 'concat.wav';
|
||||
await ffmpeg.exec([
|
||||
'-f',
|
||||
'concat',
|
||||
'-safe',
|
||||
'0',
|
||||
'-i',
|
||||
fileListName,
|
||||
'-c',
|
||||
'copy',
|
||||
concatWav
|
||||
]);
|
||||
|
||||
// 4. Convert concatenated WAV to requested output format
|
||||
let finalOutput = concatWav;
|
||||
if (outputFormat !== 'wav') {
|
||||
const args = ['-i', concatWav];
|
||||
if (outputFormat === 'mp3') {
|
||||
args.push(
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
'-b:a',
|
||||
'192k',
|
||||
'-f',
|
||||
'mp3',
|
||||
outputName
|
||||
);
|
||||
} else if (outputFormat === 'aac') {
|
||||
args.push('-c:a', 'aac', '-b:a', '192k', '-f', 'adts', outputName);
|
||||
}
|
||||
await ffmpeg.exec(args);
|
||||
finalOutput = outputName;
|
||||
}
|
||||
|
||||
const mergedAudio = await ffmpeg.readFile(finalOutput);
|
||||
|
||||
let mimeType = 'audio/wav';
|
||||
if (outputFormat === 'mp3') mimeType = 'audio/mp3';
|
||||
if (outputFormat === 'aac') mimeType = 'audio/aac';
|
||||
|
||||
// Clean up files
|
||||
for (const tempWavName of tempWavNames) {
|
||||
await ffmpeg.deleteFile(tempWavName);
|
||||
}
|
||||
await ffmpeg.deleteFile(fileListName);
|
||||
await ffmpeg.deleteFile(concatWav);
|
||||
if (outputFormat !== 'wav') {
|
||||
await ffmpeg.deleteFile(outputName);
|
||||
}
|
||||
|
||||
return new File(
|
||||
[
|
||||
new Blob([mergedAudio], {
|
||||
type: mimeType
|
||||
})
|
||||
],
|
||||
`merged_audio.${outputFormat}`,
|
||||
{ type: mimeType }
|
||||
);
|
||||
}
|
||||
3
src/pages/tools/audio/merge-audio/types.ts
Normal file
3
src/pages/tools/audio/merge-audio/types.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export type InitialValuesType = {
|
||||
outputFormat: 'mp3' | 'aac' | 'wav';
|
||||
};
|
||||
128
src/pages/tools/audio/trim/index.tsx
Normal file
128
src/pages/tools/audio/trim/index.tsx
Normal file
@@ -0,0 +1,128 @@
|
||||
import { Box, FormControlLabel, Radio, RadioGroup } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import { InitialValuesType } from './types';
|
||||
import ToolAudioInput from '@components/input/ToolAudioInput';
|
||||
import ToolFileResult from '@components/result/ToolFileResult';
|
||||
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
||||
import { trimAudio } from './service';
|
||||
|
||||
const initialValues: InitialValuesType = {
|
||||
startTime: '00:00:00',
|
||||
endTime: '00:01:00',
|
||||
outputFormat: 'mp3'
|
||||
};
|
||||
|
||||
const formatOptions = [
|
||||
{ label: 'MP3', value: 'mp3' },
|
||||
{ label: 'AAC', value: 'aac' },
|
||||
{ label: 'WAV', value: 'wav' }
|
||||
];
|
||||
|
||||
export default function Trim({ title, longDescription }: ToolComponentProps) {
|
||||
const [input, setInput] = useState<File | null>(null);
|
||||
const [result, setResult] = useState<File | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const compute = async (
|
||||
optionsValues: InitialValuesType,
|
||||
input: File | null
|
||||
) => {
|
||||
if (!input) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const trimmedFile = await trimAudio(input, optionsValues);
|
||||
setResult(trimmedFile);
|
||||
} catch (err) {
|
||||
console.error(`Failed to trim audio: ${err}`);
|
||||
setResult(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> | null = ({
|
||||
values,
|
||||
updateField
|
||||
}) => [
|
||||
{
|
||||
title: 'Time Settings',
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
value={values.startTime}
|
||||
onOwnChange={(val) => updateField('startTime', val)}
|
||||
description="Start time in format HH:MM:SS (e.g., 00:00:30)"
|
||||
label="Start Time"
|
||||
/>
|
||||
<Box mt={2}>
|
||||
<TextFieldWithDesc
|
||||
value={values.endTime}
|
||||
onOwnChange={(val) => updateField('endTime', val)}
|
||||
description="End time in format HH:MM:SS (e.g., 00:01:30)"
|
||||
label="End Time"
|
||||
/>
|
||||
</Box>
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Output Format',
|
||||
component: (
|
||||
<Box mt={2}>
|
||||
<RadioGroup
|
||||
row
|
||||
value={values.outputFormat}
|
||||
onChange={(e) =>
|
||||
updateField(
|
||||
'outputFormat',
|
||||
e.target.value as 'mp3' | 'aac' | 'wav'
|
||||
)
|
||||
}
|
||||
>
|
||||
{formatOptions.map((opt) => (
|
||||
<FormControlLabel
|
||||
key={opt.value}
|
||||
value={opt.value}
|
||||
control={<Radio />}
|
||||
label={opt.label}
|
||||
/>
|
||||
))}
|
||||
</RadioGroup>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
];
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolAudioInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
title={'Input Audio'}
|
||||
/>
|
||||
}
|
||||
resultComponent={
|
||||
loading ? (
|
||||
<ToolFileResult title="Trimming Audio" value={null} loading={true} />
|
||||
) : (
|
||||
<ToolFileResult
|
||||
title="Trimmed Audio"
|
||||
value={result}
|
||||
extension={result ? result.name.split('.').pop() : undefined}
|
||||
/>
|
||||
)
|
||||
}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is ${title}?`, description: longDescription }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
27
src/pages/tools/audio/trim/meta.ts
Normal file
27
src/pages/tools/audio/trim/meta.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('audio', {
|
||||
name: 'Trim Audio',
|
||||
path: 'trim',
|
||||
icon: 'mdi:scissors-cutting',
|
||||
description:
|
||||
'Cut and trim audio files to extract specific segments by specifying start and end times.',
|
||||
shortDescription:
|
||||
'Trim audio files to extract specific time segments (MP3, AAC, WAV).',
|
||||
keywords: [
|
||||
'trim',
|
||||
'audio',
|
||||
'cut',
|
||||
'segment',
|
||||
'extract',
|
||||
'mp3',
|
||||
'aac',
|
||||
'wav',
|
||||
'audio editing',
|
||||
'time'
|
||||
],
|
||||
longDescription:
|
||||
'This tool allows you to trim audio files by specifying start and end times. You can extract specific segments from longer audio files, remove unwanted parts, or create shorter clips. Supports various audio formats including MP3, AAC, and WAV. Perfect for podcast editing, music production, or any audio editing needs.',
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
108
src/pages/tools/audio/trim/service.ts
Normal file
108
src/pages/tools/audio/trim/service.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
const ffmpeg = new FFmpeg();
|
||||
|
||||
export async function trimAudio(
|
||||
input: File,
|
||||
options: InitialValuesType
|
||||
): Promise<File> {
|
||||
if (!ffmpeg.loaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
}
|
||||
|
||||
const inputName = 'input.mp3';
|
||||
await ffmpeg.writeFile(inputName, await fetchFile(input));
|
||||
|
||||
const { startTime, endTime, outputFormat } = options;
|
||||
const outputName = `output.${outputFormat}`;
|
||||
|
||||
// Build FFmpeg arguments for trimming
|
||||
let args: string[] = [
|
||||
'-i',
|
||||
inputName,
|
||||
'-ss',
|
||||
startTime, // Start time
|
||||
'-to',
|
||||
endTime, // End time
|
||||
'-c',
|
||||
'copy' // Copy without re-encoding for speed
|
||||
];
|
||||
|
||||
// Add format-specific arguments
|
||||
if (outputFormat === 'mp3') {
|
||||
args = [
|
||||
'-i',
|
||||
inputName,
|
||||
'-ss',
|
||||
startTime,
|
||||
'-to',
|
||||
endTime,
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
'-b:a',
|
||||
'192k',
|
||||
'-f',
|
||||
'mp3',
|
||||
outputName
|
||||
];
|
||||
} else if (outputFormat === 'aac') {
|
||||
args = [
|
||||
'-i',
|
||||
inputName,
|
||||
'-ss',
|
||||
startTime,
|
||||
'-to',
|
||||
endTime,
|
||||
'-c:a',
|
||||
'aac',
|
||||
'-b:a',
|
||||
'192k',
|
||||
'-f',
|
||||
'adts',
|
||||
outputName
|
||||
];
|
||||
} else if (outputFormat === 'wav') {
|
||||
args = [
|
||||
'-i',
|
||||
inputName,
|
||||
'-ss',
|
||||
startTime,
|
||||
'-to',
|
||||
endTime,
|
||||
'-acodec',
|
||||
'pcm_s16le',
|
||||
'-ar',
|
||||
'44100',
|
||||
'-ac',
|
||||
'2',
|
||||
'-f',
|
||||
'wav',
|
||||
outputName
|
||||
];
|
||||
}
|
||||
|
||||
await ffmpeg.exec(args);
|
||||
|
||||
const trimmedAudio = await ffmpeg.readFile(outputName);
|
||||
|
||||
let mimeType = 'audio/mp3';
|
||||
if (outputFormat === 'aac') mimeType = 'audio/aac';
|
||||
if (outputFormat === 'wav') mimeType = 'audio/wav';
|
||||
|
||||
return new File(
|
||||
[
|
||||
new Blob([trimmedAudio], {
|
||||
type: mimeType
|
||||
})
|
||||
],
|
||||
`${input.name.replace(/\.[^/.]+$/, '')}_trimmed.${outputFormat}`,
|
||||
{ type: mimeType }
|
||||
);
|
||||
}
|
||||
58
src/pages/tools/audio/trim/trim.service.test.ts
Normal file
58
src/pages/tools/audio/trim/trim.service.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { expect, describe, it, vi } from 'vitest';
|
||||
|
||||
// Mock FFmpeg since it doesn't support Node.js
|
||||
vi.mock('@ffmpeg/ffmpeg', () => ({
|
||||
FFmpeg: vi.fn().mockImplementation(() => ({
|
||||
loaded: false,
|
||||
load: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
exec: vi.fn().mockResolvedValue(undefined),
|
||||
readFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4, 5])),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined)
|
||||
}))
|
||||
}));
|
||||
|
||||
vi.mock('@ffmpeg/util', () => ({
|
||||
fetchFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4, 5]))
|
||||
}));
|
||||
|
||||
import { trimAudio } from './service';
|
||||
|
||||
describe('trimAudio', () => {
|
||||
it('should trim audio file with valid time parameters', async () => {
|
||||
// Create a mock audio file
|
||||
const mockAudioData = new Uint8Array([0, 1, 2, 3, 4, 5]);
|
||||
const mockFile = new File([mockAudioData], 'test.mp3', {
|
||||
type: 'audio/mp3'
|
||||
});
|
||||
|
||||
const options = {
|
||||
startTime: '00:00:10',
|
||||
endTime: '00:00:20',
|
||||
outputFormat: 'mp3' as const
|
||||
};
|
||||
|
||||
const result = await trimAudio(mockFile, options);
|
||||
expect(result).toBeInstanceOf(File);
|
||||
expect(result.name).toContain('_trimmed.mp3');
|
||||
expect(result.type).toBe('audio/mp3');
|
||||
});
|
||||
|
||||
it('should handle different output formats', async () => {
|
||||
const mockAudioData = new Uint8Array([0, 1, 2, 3, 4, 5]);
|
||||
const mockFile = new File([mockAudioData], 'test.wav', {
|
||||
type: 'audio/wav'
|
||||
});
|
||||
|
||||
const options = {
|
||||
startTime: '00:00:00',
|
||||
endTime: '00:00:30',
|
||||
outputFormat: 'wav' as const
|
||||
};
|
||||
|
||||
const result = await trimAudio(mockFile, options);
|
||||
expect(result).toBeInstanceOf(File);
|
||||
expect(result.name).toContain('_trimmed.wav');
|
||||
expect(result.type).toBe('audio/wav');
|
||||
});
|
||||
});
|
||||
5
src/pages/tools/audio/trim/types.ts
Normal file
5
src/pages/tools/audio/trim/types.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export type InitialValuesType = {
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
outputFormat: 'mp3' | 'aac' | 'wav';
|
||||
};
|
||||
164
src/pages/tools/image/generic/convert-to-jpg/index.tsx
Normal file
164
src/pages/tools/image/generic/convert-to-jpg/index.tsx
Normal file
@@ -0,0 +1,164 @@
|
||||
import { Box, Slider, Typography } from '@mui/material';
|
||||
import ToolImageInput from 'components/input/ToolImageInput';
|
||||
import ColorSelector from 'components/options/ColorSelector';
|
||||
import ToolFileResult from 'components/result/ToolFileResult';
|
||||
import Color from 'color';
|
||||
import React, { useState } from 'react';
|
||||
import * as Yup from 'yup';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
|
||||
const initialValues = {
|
||||
quality: 85,
|
||||
backgroundColor: '#ffffff'
|
||||
};
|
||||
|
||||
const validationSchema = Yup.object({
|
||||
quality: Yup.number().min(1).max(100).required('Quality is required'),
|
||||
backgroundColor: Yup.string().required('Background color is required')
|
||||
});
|
||||
|
||||
export default function ConvertToJpg({ title }: ToolComponentProps) {
|
||||
const [input, setInput] = useState<File | null>(null);
|
||||
const [result, setResult] = useState<File | null>(null);
|
||||
|
||||
const compute = async (
|
||||
optionsValues: typeof initialValues,
|
||||
input: any
|
||||
): Promise<void> => {
|
||||
if (!input) return;
|
||||
|
||||
const processImage = async (
|
||||
file: File,
|
||||
quality: number,
|
||||
backgroundColor: string
|
||||
) => {
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (ctx == null) return;
|
||||
|
||||
const img = new Image();
|
||||
img.src = URL.createObjectURL(file);
|
||||
|
||||
try {
|
||||
await img.decode();
|
||||
|
||||
canvas.width = img.width;
|
||||
canvas.height = img.height;
|
||||
|
||||
// Fill background with selected color (important for transparency)
|
||||
let bgColor: [number, number, number];
|
||||
try {
|
||||
//@ts-ignore
|
||||
bgColor = Color(backgroundColor).rgb().array();
|
||||
} catch (err) {
|
||||
bgColor = [255, 255, 255]; // Default to white
|
||||
}
|
||||
|
||||
ctx.fillStyle = `rgb(${bgColor[0]}, ${bgColor[1]}, ${bgColor[2]})`;
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
// Draw the image on top
|
||||
ctx.drawImage(img, 0, 0);
|
||||
|
||||
// Convert to JPG with specified quality
|
||||
canvas.toBlob(
|
||||
(blob) => {
|
||||
if (blob) {
|
||||
const fileName = file.name.replace(/\.[^/.]+$/, '') + '.jpg';
|
||||
const newFile = new File([blob], fileName, {
|
||||
type: 'image/jpeg'
|
||||
});
|
||||
setResult(newFile);
|
||||
}
|
||||
},
|
||||
'image/jpeg',
|
||||
quality / 100
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error processing image:', error);
|
||||
} finally {
|
||||
URL.revokeObjectURL(img.src);
|
||||
}
|
||||
};
|
||||
|
||||
processImage(input, optionsValues.quality, optionsValues.backgroundColor);
|
||||
};
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolImageInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
accept={[
|
||||
'image/png',
|
||||
'image/gif',
|
||||
'image/tiff',
|
||||
'image/tif',
|
||||
'image/webp',
|
||||
'image/svg+xml',
|
||||
'image/heic',
|
||||
'image/heif',
|
||||
'image/raw',
|
||||
'image/x-adobe-dng',
|
||||
'image/x-canon-cr2',
|
||||
'image/x-canon-crw',
|
||||
'image/x-nikon-nef',
|
||||
'image/x-sony-arw',
|
||||
'image/vnd.adobe.photoshop'
|
||||
]}
|
||||
title={'Input Image'}
|
||||
/>
|
||||
}
|
||||
resultComponent={
|
||||
<ToolFileResult title={'Output JPG'} value={result} extension={'jpg'} />
|
||||
}
|
||||
initialValues={initialValues}
|
||||
validationSchema={validationSchema}
|
||||
getGroups={({ values, updateField }) => [
|
||||
{
|
||||
title: 'JPG Quality Settings',
|
||||
component: (
|
||||
<Box>
|
||||
<Box mb={3}>
|
||||
<Typography variant="body2" color="text.secondary" gutterBottom>
|
||||
JPG Quality: {values.quality}%
|
||||
</Typography>
|
||||
<Slider
|
||||
value={values.quality}
|
||||
onChange={(_, value) =>
|
||||
updateField(
|
||||
'quality',
|
||||
Array.isArray(value) ? value[0] : value
|
||||
)
|
||||
}
|
||||
min={1}
|
||||
max={100}
|
||||
step={1}
|
||||
valueLabelDisplay="auto"
|
||||
valueLabelFormat={(value) => `${value}%`}
|
||||
sx={{ mt: 1 }}
|
||||
/>
|
||||
<Typography variant="caption" color="text.secondary">
|
||||
Higher values = better quality, larger file size
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
<ColorSelector
|
||||
value={values.backgroundColor}
|
||||
onColorChange={(val) => updateField('backgroundColor', val)}
|
||||
description={'Background color (for transparent images)'}
|
||||
inputProps={{ 'data-testid': 'background-color-input' }}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
]}
|
||||
compute={compute}
|
||||
setInput={setInput}
|
||||
/>
|
||||
);
|
||||
}
|
||||
27
src/pages/tools/image/generic/convert-to-jpg/meta.ts
Normal file
27
src/pages/tools/image/generic/convert-to-jpg/meta.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('image-generic', {
|
||||
name: 'Convert Images to JPG',
|
||||
path: 'convert-to-jpg',
|
||||
icon: 'ph:file-jpg-thin',
|
||||
description:
|
||||
'Convert various image formats (PNG, GIF, TIF, PSD, SVG, WEBP, HEIC, RAW) to JPG with customizable quality and background color settings.',
|
||||
shortDescription: 'Convert images to JPG with quality control',
|
||||
keywords: [
|
||||
'convert',
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'png',
|
||||
'gif',
|
||||
'tiff',
|
||||
'webp',
|
||||
'heic',
|
||||
'raw',
|
||||
'psd',
|
||||
'svg',
|
||||
'quality',
|
||||
'compression'
|
||||
],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
123
src/pages/tools/image/generic/editor/index.tsx
Normal file
123
src/pages/tools/image/generic/editor/index.tsx
Normal file
@@ -0,0 +1,123 @@
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import { Box } from '@mui/material';
|
||||
import ToolImageInput from '@components/input/ToolImageInput';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
|
||||
// Import the image editor with proper typing
|
||||
import FilerobotImageEditor, {
|
||||
FilerobotImageEditorConfig
|
||||
} from 'react-filerobot-image-editor';
|
||||
|
||||
export default function ImageEditor({ title }: ToolComponentProps) {
|
||||
const [input, setInput] = useState<File | null>(null);
|
||||
const [isEditorOpen, setIsEditorOpen] = useState(false);
|
||||
const [imageUrl, setImageUrl] = useState<string | null>(null);
|
||||
|
||||
// Handle file input change
|
||||
const handleInputChange = useCallback((file: File | null) => {
|
||||
setInput(file);
|
||||
if (file) {
|
||||
// Create object URL for the image editor
|
||||
const url = URL.createObjectURL(file);
|
||||
setImageUrl(url);
|
||||
setIsEditorOpen(true);
|
||||
} else {
|
||||
setImageUrl(null);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const onCloseEditor = (reason: string) => {
|
||||
setIsEditorOpen(false);
|
||||
setImageUrl(null);
|
||||
};
|
||||
|
||||
// Handle save from image editor
|
||||
const handleSave: FilerobotImageEditorConfig['onSave'] = (
|
||||
editedImageObject,
|
||||
designState
|
||||
) => {
|
||||
if (editedImageObject && editedImageObject.imageBase64) {
|
||||
// Convert base64 to blob
|
||||
const base64Data = editedImageObject.imageBase64.split(',')[1];
|
||||
const byteCharacters = atob(base64Data);
|
||||
const byteNumbers = new Array(byteCharacters.length);
|
||||
|
||||
for (let i = 0; i < byteCharacters.length; i++) {
|
||||
byteNumbers[i] = byteCharacters.charCodeAt(i);
|
||||
}
|
||||
|
||||
const byteArray = new Uint8Array(byteNumbers);
|
||||
const blob = new Blob([byteArray], { type: editedImageObject.mimeType });
|
||||
|
||||
const editedFile = new File(
|
||||
[blob],
|
||||
editedImageObject.fullName ?? 'edited.png',
|
||||
{
|
||||
type: editedImageObject.mimeType
|
||||
}
|
||||
);
|
||||
// Create a temporary download link
|
||||
const url = URL.createObjectURL(editedFile);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = editedFile.name; // This will be the name of the downloaded file
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
|
||||
// Release the blob URL
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
};
|
||||
|
||||
const getDefaultImageName = () => {
|
||||
if (!input) return;
|
||||
const originalName = input?.name || 'edited-image';
|
||||
const nameWithoutExt = originalName.replace(/\.[^/.]+$/, '');
|
||||
const editedFileName = `${nameWithoutExt}-edited`;
|
||||
return editedFileName;
|
||||
};
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
initialValues={{}}
|
||||
getGroups={null}
|
||||
input={input}
|
||||
inputComponent={
|
||||
isEditorOpen ? (
|
||||
imageUrl && (
|
||||
<Box style={{ width: '100%', height: '70vh' }}>
|
||||
<FilerobotImageEditor
|
||||
source={imageUrl}
|
||||
onSave={handleSave}
|
||||
onClose={onCloseEditor}
|
||||
annotationsCommon={{
|
||||
fill: 'blue'
|
||||
}}
|
||||
defaultSavedImageName={getDefaultImageName()}
|
||||
Rotate={{ angle: 90, componentType: 'slider' }}
|
||||
savingPixelRatio={1}
|
||||
previewPixelRatio={1}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
) : (
|
||||
<ToolImageInput
|
||||
value={input}
|
||||
onChange={handleInputChange}
|
||||
accept={['image/*']}
|
||||
title="Upload Image to Edit"
|
||||
/>
|
||||
)
|
||||
}
|
||||
toolInfo={{
|
||||
title: 'Image Editor',
|
||||
description:
|
||||
'A powerful image editing tool that provides professional-grade features including cropping, rotating, color adjustments, text annotations, drawing tools, and watermarking. Edit your images directly in your browser without the need for external software.'
|
||||
}}
|
||||
compute={() => {}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
28
src/pages/tools/image/generic/editor/meta.ts
Normal file
28
src/pages/tools/image/generic/editor/meta.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('image-generic', {
|
||||
name: 'Image Editor',
|
||||
path: 'editor',
|
||||
icon: 'mdi:image-edit',
|
||||
description:
|
||||
'Advanced image editor with tools for cropping, rotating, annotating, adjusting colors, and adding watermarks. Edit your images with professional-grade tools directly in your browser.',
|
||||
shortDescription: 'Edit images with advanced tools and features',
|
||||
keywords: [
|
||||
'image',
|
||||
'editor',
|
||||
'edit',
|
||||
'crop',
|
||||
'rotate',
|
||||
'annotate',
|
||||
'adjust',
|
||||
'watermark',
|
||||
'text',
|
||||
'drawing',
|
||||
'filters',
|
||||
'brightness',
|
||||
'contrast',
|
||||
'saturation'
|
||||
],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
@@ -8,7 +8,10 @@ import { tool as createTransparent } from './create-transparent/meta';
|
||||
import { tool as imageToText } from './image-to-text/meta';
|
||||
import { tool as qrCodeGenerator } from './qr-code/meta';
|
||||
import { tool as rotateImage } from './rotate/meta';
|
||||
import { tool as convertToJpg } from './convert-to-jpg/meta';
|
||||
import { tool as imageEditor } from './editor/meta';
|
||||
export const imageGenericTools = [
|
||||
imageEditor,
|
||||
resizeImage,
|
||||
compressImage,
|
||||
removeBackground,
|
||||
@@ -18,5 +21,6 @@ export const imageGenericTools = [
|
||||
createTransparent,
|
||||
imageToText,
|
||||
qrCodeGenerator,
|
||||
rotateImage
|
||||
rotateImage,
|
||||
convertToJpg
|
||||
];
|
||||
|
||||
@@ -240,6 +240,7 @@ export default async function makeTool(
|
||||
description: calcData.longDescription
|
||||
}}
|
||||
verticalGroups
|
||||
// @ts-ignore
|
||||
getGroups={({ values, updateField }) => [
|
||||
...(calcData.presets?.length
|
||||
? [
|
||||
|
||||
29
src/pages/tools/pdf/editor/index.tsx
Normal file
29
src/pages/tools/pdf/editor/index.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import React from 'react';
|
||||
import { Box } from '@mui/material';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import { EmbedPDF } from '@simplepdf/react-embed-pdf';
|
||||
|
||||
export default function PdfEditor({ title }: ToolComponentProps) {
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
initialValues={{}}
|
||||
getGroups={null}
|
||||
input={null}
|
||||
inputComponent={
|
||||
<Box sx={{ width: '100%', height: '80vh' }}>
|
||||
<EmbedPDF mode="inline" style={{ width: '100%', height: '100%' }} />
|
||||
</Box>
|
||||
}
|
||||
toolInfo={{
|
||||
title: 'PDF Editor',
|
||||
description:
|
||||
'Edit, annotate, highlight, fill forms, and export your PDFs entirely in the browser. Add text, drawings, signatures, and more to your PDF documents with this powerful online editor.'
|
||||
}}
|
||||
compute={() => {
|
||||
/* no background compute required */
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
28
src/pages/tools/pdf/editor/meta.ts
Normal file
28
src/pages/tools/pdf/editor/meta.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('pdf', {
|
||||
name: 'PDF Editor',
|
||||
path: 'editor',
|
||||
icon: 'mdi:file-document-edit',
|
||||
description:
|
||||
'Advanced PDF editor with annotation, form-fill, highlight, and export capabilities. Edit your PDFs directly in the browser with professional-grade tools including text insertion, drawing, highlighting, signing and form filling.',
|
||||
shortDescription:
|
||||
'Edit PDFs with advanced annotation, signing and editing tools',
|
||||
keywords: [
|
||||
'pdf',
|
||||
'editor',
|
||||
'edit',
|
||||
'annotate',
|
||||
'highlight',
|
||||
'form',
|
||||
'fill',
|
||||
'text',
|
||||
'drawing',
|
||||
'signature',
|
||||
'export',
|
||||
'annotation',
|
||||
'markup'
|
||||
],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
@@ -6,8 +6,10 @@ import { DefinedTool } from '@tools/defineTool';
|
||||
import { tool as compressPdfTool } from './compress-pdf/meta';
|
||||
import { tool as protectPdfTool } from './protect-pdf/meta';
|
||||
import { meta as pdfToEpub } from './pdf-to-epub/meta';
|
||||
import { tool as pdfEditor } from './editor/meta';
|
||||
|
||||
export const pdfTools: DefinedTool[] = [
|
||||
pdfEditor,
|
||||
splitPdfMeta,
|
||||
pdfRotatePdf,
|
||||
compressPdfTool,
|
||||
|
||||
158
src/pages/tools/string/censor/index.tsx
Normal file
158
src/pages/tools/string/censor/index.tsx
Normal file
@@ -0,0 +1,158 @@
|
||||
import { Box } from '@mui/material';
|
||||
import { useState } from 'react';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import { censorText } from './service';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import { InitialValuesType } from './types';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { CardExampleType } from '@components/examples/ToolExamples';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
||||
import SelectWithDesc from '@components/options/SelectWithDesc';
|
||||
import CheckboxWithDesc from '@components/options/CheckboxWithDesc';
|
||||
|
||||
const initialValues: InitialValuesType = {
|
||||
wordsToCensor: '',
|
||||
censoredBySymbol: true,
|
||||
censorSymbol: '█',
|
||||
eachLetter: true,
|
||||
censorWord: 'CENSORED'
|
||||
};
|
||||
|
||||
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
||||
{
|
||||
title: 'Censor a Word in a Quote',
|
||||
description: `In this example, we hide the unpleasant word "idiot" from Jim Rohn's quote. We specify this word in the words-to-censor option and mask it with a neat smiling face character "☺".`,
|
||||
sampleText:
|
||||
'Motivation alone is not enough. If you have an idiot and you motivate him, now you have a motivated idiot. Jim Rohn',
|
||||
sampleResult:
|
||||
'Motivation alone is not enough. If you have an ☺ and you motivate him, now you have a motivated ☺. Jim Rohn',
|
||||
sampleOptions: {
|
||||
...initialValues,
|
||||
wordsToCensor: 'idiot',
|
||||
censorSymbol: '☺',
|
||||
eachLetter: false
|
||||
}
|
||||
},
|
||||
{
|
||||
title: 'Censor an Excerpt',
|
||||
description: `In this example, we censor multiple words from an excerpt from the novel "The Guns of Avalon" by Roger Zelazny. To do this, we write out all unnecessary words in the multi-line text option and select the "Use a Symbol to Censor" censoring mode. We activate the "Mask Each Letter" option so that in place of each word exactly as many block characters "█" appeared as there are letters in that word.`,
|
||||
sampleText:
|
||||
'“In the mirrors of the many judgments, my hands are the color of blood. I sometimes fancy myself an evil which exists to oppose other evils; and on that great Day of which the prophets speak but in which they do not truly believe, on the day the world is utterly cleansed of evil, then I too will go down into darkness, swallowing curses. Until then, I will not wash my hands nor let them hang useless.” ― Roger Zelazny, The Guns of Avalon',
|
||||
sampleResult:
|
||||
'“In the mirrors of the many judgments, my hands are the color of █████. I sometimes fancy myself an ████ which exists to oppose other █████; and on that great Day of which the prophets speak but in which they do not truly believe, on the day the world is utterly cleansed of ████, then I too will go down into ████████, swallowing ██████. Until then, I will not wash my hands nor let them hang useless.” ― Roger Zelazny, The Guns of Avalon',
|
||||
sampleOptions: {
|
||||
...initialValues,
|
||||
wordsToCensor: 'blood\nevil\ndarkness\ncurses',
|
||||
eachLetter: true
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Censor Agent's Name",
|
||||
description: `In this example, we hide the name of an undercover FBI agent. We replace two words at once (first name and last name) with the code name "Agent 007"`,
|
||||
sampleText:
|
||||
'My name is John and I am an undercover FBI agent. I usually write my name in lowercase as "john" because I find uppercase letters scary. Unfortunately, in documents, my name is properly capitalized as John and it makes me upset.',
|
||||
sampleResult:
|
||||
'My name is Agent 007 and I am an undercover FBI agent. I usually write my name in lowercase as "Agent 007" because I find uppercase letters scary. Unfortunately, in documents, my name is properly capitalized as Agent 007 and it makes me upset.',
|
||||
sampleOptions: {
|
||||
...initialValues,
|
||||
censoredBySymbol: false,
|
||||
wordsToCensor: 'john',
|
||||
censorWord: 'Agent 007'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
export default function CensorText({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
|
||||
function compute(initialValues: InitialValuesType, input: string) {
|
||||
setResult(censorText(input, initialValues));
|
||||
}
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> = ({
|
||||
values,
|
||||
updateField
|
||||
}) => [
|
||||
{
|
||||
title: 'Words to Censor',
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
multiline
|
||||
rows={3}
|
||||
value={values.wordsToCensor}
|
||||
onOwnChange={(val) => updateField('wordsToCensor', val)}
|
||||
description={`Specify all unwanted words that
|
||||
you want to hide in text (separated by a new line)`}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Censor Mode',
|
||||
component: (
|
||||
<Box>
|
||||
<SelectWithDesc
|
||||
selected={values.censoredBySymbol}
|
||||
options={[
|
||||
{ label: 'Censor by Symbol', value: true },
|
||||
{ label: 'Censor by Word', value: false }
|
||||
]}
|
||||
onChange={(value) => updateField('censoredBySymbol', value)}
|
||||
description={'Select the censoring mode.'}
|
||||
/>
|
||||
|
||||
{values.censoredBySymbol && (
|
||||
<TextFieldWithDesc
|
||||
value={values.censorSymbol}
|
||||
onOwnChange={(val) => updateField('censorSymbol', val)}
|
||||
description={`A symbol, character, or pattern to use for censoring.`}
|
||||
/>
|
||||
)}
|
||||
|
||||
{values.censoredBySymbol && (
|
||||
<CheckboxWithDesc
|
||||
checked={values.eachLetter}
|
||||
onChange={(value) => updateField('eachLetter', value)}
|
||||
title="Mask each letter"
|
||||
description="Put a masking symbol in place of each letter of the censored word."
|
||||
/>
|
||||
)}
|
||||
|
||||
{!values.censoredBySymbol && (
|
||||
<TextFieldWithDesc
|
||||
value={values.censorWord}
|
||||
onOwnChange={(val) => updateField('censorWord', val)}
|
||||
description={`Replace all censored words with this word.`}
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
];
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
compute={compute}
|
||||
input={input}
|
||||
setInput={setInput}
|
||||
inputComponent={
|
||||
<ToolTextInput title={'Input text'} value={input} onChange={setInput} />
|
||||
}
|
||||
resultComponent={
|
||||
<ToolTextResult title={'Censored text'} value={result} />
|
||||
}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
exampleCards={exampleCards}
|
||||
/>
|
||||
);
|
||||
}
|
||||
16
src/pages/tools/string/censor/meta.ts
Normal file
16
src/pages/tools/string/censor/meta.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('string', {
|
||||
name: 'Text Censor',
|
||||
path: 'censor',
|
||||
shortDescription:
|
||||
'Quickly mask bad words or replace them with alternative words.',
|
||||
icon: 'hugeicons:text-footnote',
|
||||
description:
|
||||
"utility for censoring words in text. Load your text in the input form on the left, specify all the bad words in the options, and you'll instantly get censored text in the output area.",
|
||||
longDescription:
|
||||
'With this online tool, you can censor certain words in any text. You can specify a list of unwanted words (such as swear words or secret words) and the program will replace them with alternative words and create a safe-to-read text. The words can be specified in a multi-line text field in the options by entering one word per line.',
|
||||
keywords: ['text', 'censor', 'words', 'characters'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
49
src/pages/tools/string/censor/service.ts
Normal file
49
src/pages/tools/string/censor/service.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
export function censorText(input: string, options: InitialValuesType): string {
|
||||
if (!input) return '';
|
||||
if (!options.wordsToCensor) return input;
|
||||
|
||||
if (options.censoredBySymbol && !isSymbol(options.censorSymbol)) {
|
||||
throw new Error('Enter a valid censor symbol (non-alphanumeric or emoji)');
|
||||
}
|
||||
|
||||
const wordsToCensor = options.wordsToCensor
|
||||
.split('\n')
|
||||
.map((word) => word.trim())
|
||||
.filter((word) => word.length > 0);
|
||||
|
||||
let censoredText = input;
|
||||
|
||||
for (const word of wordsToCensor) {
|
||||
const escapedWord = escapeRegex(word);
|
||||
const pattern = new RegExp(`\\b${escapedWord}\\b`, 'giu');
|
||||
|
||||
const replacement = options.censoredBySymbol
|
||||
? options.eachLetter
|
||||
? options.censorSymbol.repeat(word.length)
|
||||
: options.censorSymbol
|
||||
: options.censorWord;
|
||||
|
||||
censoredText = censoredText.replace(pattern, replacement);
|
||||
}
|
||||
|
||||
return censoredText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes RegExp special characters in a string
|
||||
*/
|
||||
function escapeRegex(str: string): string {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a string is a valid symbol or emoji (multi-codepoint supported).
|
||||
*/
|
||||
function isSymbol(input: string): boolean {
|
||||
return (
|
||||
/^[^\p{L}\p{N}]+$/u.test(input) || // Not a letter or number
|
||||
/\p{Extended_Pictographic}/u.test(input) // Emoji or pictographic symbol
|
||||
);
|
||||
}
|
||||
7
src/pages/tools/string/censor/types.ts
Normal file
7
src/pages/tools/string/censor/types.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export type InitialValuesType = {
|
||||
wordsToCensor: string;
|
||||
censoredBySymbol: boolean;
|
||||
censorSymbol: string;
|
||||
eachLetter: boolean;
|
||||
censorWord: string;
|
||||
};
|
||||
@@ -16,6 +16,7 @@ import { tool as stringRepeat } from './repeat/meta';
|
||||
import { tool as stringTruncate } from './truncate/meta';
|
||||
import { tool as stringBase64 } from './base64/meta';
|
||||
import { tool as stringStatistic } from './statistic/meta';
|
||||
import { tool as stringCensor } from './censor/meta';
|
||||
|
||||
export const stringTools = [
|
||||
stringSplit,
|
||||
@@ -35,5 +36,6 @@ export const stringTools = [
|
||||
stringRotate,
|
||||
stringRot13,
|
||||
stringBase64,
|
||||
stringStatistic
|
||||
stringStatistic,
|
||||
stringCensor
|
||||
];
|
||||
|
||||
82
src/pages/tools/time/check-leap-years/index.tsx
Normal file
82
src/pages/tools/time/check-leap-years/index.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import { CardExampleType } from '@components/examples/ToolExamples';
|
||||
import { checkLeapYear } from './service';
|
||||
|
||||
const initialValues = {};
|
||||
|
||||
type InitialValuesType = typeof initialValues;
|
||||
|
||||
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
||||
{
|
||||
title: 'Find Birthdays on February 29',
|
||||
description:
|
||||
"One of our friends was born on a leap year on February 29th and as a consequence, she has a birthday only once every 4 years. As we can never really remember when her birthday is, we are using our program to create a reminder list of the upcoming leap years. To create a list of her next birthdays, we load a sequence of years from 2025 to 2040 into the input and get the status of each year. If the program says that it's a leap year, then we know that we'll be invited to a birthday party on February 29th.",
|
||||
sampleText: `2025
|
||||
2026
|
||||
2027
|
||||
2028
|
||||
2029
|
||||
2030
|
||||
2031
|
||||
2032
|
||||
2033
|
||||
2034
|
||||
2035
|
||||
2036
|
||||
2037
|
||||
2038
|
||||
2039
|
||||
2040`,
|
||||
sampleResult: `2025 is not a leap year.
|
||||
2026 is not a leap year.
|
||||
2027 is not a leap year.
|
||||
2028 is a leap year.
|
||||
2029 is not a leap year.
|
||||
2030 is not a leap year.
|
||||
2031 is not a leap year.
|
||||
2032 is a leap year.
|
||||
2033 is not a leap year.
|
||||
2034 is not a leap year.
|
||||
2035 is not a leap year.
|
||||
2036 is a leap year.
|
||||
2037 is not a leap year.
|
||||
2038 is not a leap year.
|
||||
2039 is not a leap year.
|
||||
2040 is a leap year.`,
|
||||
sampleOptions: {}
|
||||
}
|
||||
];
|
||||
|
||||
export default function ConvertDaysToHours({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
|
||||
const compute = (optionsValues: typeof initialValues, input: string) => {
|
||||
setResult(checkLeapYear(input));
|
||||
};
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> | null = null;
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={<ToolTextInput value={input} onChange={setInput} />}
|
||||
resultComponent={<ToolTextResult value={result} />}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
exampleCards={exampleCards}
|
||||
/>
|
||||
);
|
||||
}
|
||||
14
src/pages/tools/time/check-leap-years/meta.ts
Normal file
14
src/pages/tools/time/check-leap-years/meta.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('time', {
|
||||
path: 'check-leap-years',
|
||||
name: 'Check Leap Years',
|
||||
icon: 'arcticons:calendar-simple-29',
|
||||
description:
|
||||
' You can check if a given calendar year is a leap year. You can enter one or many different years into the input field with one date per line and get the answer to the test question of whether the given year is a leap year.',
|
||||
shortDescription: 'Convert days to hours easily.',
|
||||
keywords: ['check', 'leap', 'years'],
|
||||
longDescription: `This is a quick online utility for testing if the given year is a leap year. Just as a reminder, a leap year has 366 days, which is one more day than a common year. This extra day is added to the month of February and it falls on February 29th. There's a simple mathematical formula for calculating if the given year is a leap year. Leap years are those years that are divisible by 4 but not divisible by 100, as well as years that are divisible by 100 and 400 simultaneously. Our algorithm checks each input year using this formula and outputs the year's status. For example, if you enter the value "2025" as input, the program will display "2025 is not a leap year.", and for the value "2028", the status will be "2028 is a leap year.". You can also enter multiple years as the input in a column and get a matching column of statuses as the output.`,
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
25
src/pages/tools/time/check-leap-years/service.ts
Normal file
25
src/pages/tools/time/check-leap-years/service.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
function isLeapYear(year: number): boolean {
|
||||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
||||
}
|
||||
|
||||
export function checkLeapYear(input: string): string {
|
||||
if (!input) return '';
|
||||
|
||||
const years = input
|
||||
.split('\n')
|
||||
.map((year) => year.trim())
|
||||
.filter((year) => year !== '');
|
||||
|
||||
const results = years.map((yearStr) => {
|
||||
if (!/^\d{1,4}$/.test(yearStr)) {
|
||||
return `${yearStr}: Invalid year`;
|
||||
}
|
||||
|
||||
const year = Number(yearStr);
|
||||
return `${year} ${
|
||||
isLeapYear(year) ? 'is a leap year.' : 'is not a leap year.'
|
||||
}`;
|
||||
});
|
||||
|
||||
return results.join('\n');
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import { validateCrontab, explainCrontab } from './service';
|
||||
|
||||
describe('crontab-guru service', () => {
|
||||
it('validates correct crontab expressions', () => {
|
||||
expect(validateCrontab('35 16 * * 0-5')).toBe(true);
|
||||
expect(validateCrontab('* * * * *')).toBe(true);
|
||||
expect(validateCrontab('0 12 1 * *')).toBe(true);
|
||||
});
|
||||
|
||||
it('invalidates incorrect crontab expressions', () => {
|
||||
expect(validateCrontab('invalid expression')).toBe(false);
|
||||
expect(validateCrontab('61 24 * * *')).toBe(false);
|
||||
});
|
||||
|
||||
it('explains valid crontab expressions', () => {
|
||||
expect(explainCrontab('35 16 * * 0-5')).toMatch(/At 04:35 PM/);
|
||||
expect(explainCrontab('* * * * *')).toMatch(/Every minute/);
|
||||
});
|
||||
|
||||
it('returns error for invalid crontab explanation', () => {
|
||||
expect(explainCrontab('invalid expression')).toMatch(
|
||||
/Invalid crontab expression/
|
||||
);
|
||||
});
|
||||
});
|
||||
128
src/pages/tools/time/crontab-guru/index.tsx
Normal file
128
src/pages/tools/time/crontab-guru/index.tsx
Normal file
@@ -0,0 +1,128 @@
|
||||
import { Alert } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import { CardExampleType } from '@components/examples/ToolExamples';
|
||||
import { main, validateCrontab } from './service';
|
||||
|
||||
const initialValues = {};
|
||||
|
||||
type InitialValuesType = typeof initialValues;
|
||||
|
||||
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
||||
{
|
||||
title: 'Every day at 16:35, Sunday to Friday',
|
||||
description: 'At 16:35 on every day-of-week from Sunday through Friday.',
|
||||
sampleText: '35 16 * * 0-5',
|
||||
sampleResult: 'At 04:35 PM, Sunday through Friday',
|
||||
sampleOptions: {}
|
||||
},
|
||||
{
|
||||
title: 'Every minute',
|
||||
description: 'Runs every minute.',
|
||||
sampleText: '* * * * *',
|
||||
sampleResult: 'Every minute',
|
||||
sampleOptions: {}
|
||||
},
|
||||
{
|
||||
title: 'Every 5 minutes',
|
||||
description: 'Runs every 5 minutes.',
|
||||
sampleText: '*/5 * * * *',
|
||||
sampleResult: 'Every 5 minutes',
|
||||
sampleOptions: {}
|
||||
},
|
||||
{
|
||||
title: 'At 12:00 PM on the 1st of every month',
|
||||
description: 'Runs at noon on the first day of each month.',
|
||||
sampleText: '0 12 1 * *',
|
||||
sampleResult: 'At 12:00 PM, on day 1 of the month',
|
||||
sampleOptions: {}
|
||||
}
|
||||
];
|
||||
|
||||
export default function CrontabGuru({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
const [isValid, setIsValid] = useState<boolean | null>(null);
|
||||
const [hasInteracted, setHasInteracted] = useState<boolean>(false);
|
||||
|
||||
const compute = (values: InitialValuesType, input: string) => {
|
||||
if (hasInteracted) {
|
||||
setIsValid(validateCrontab(input));
|
||||
}
|
||||
setResult(main(input, values));
|
||||
};
|
||||
|
||||
const handleInputChange = (val: string) => {
|
||||
if (!hasInteracted) setHasInteracted(true);
|
||||
setInput(val);
|
||||
};
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolTextInput
|
||||
value={input}
|
||||
onChange={handleInputChange}
|
||||
placeholder="e.g. 35 16 * * 0-5"
|
||||
/>
|
||||
}
|
||||
resultComponent={
|
||||
<div style={{ position: 'relative', minHeight: 80 }}>
|
||||
{hasInteracted && isValid === false && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
zIndex: 2,
|
||||
pointerEvents: 'auto',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
background: 'transparent'
|
||||
}}
|
||||
>
|
||||
<Alert
|
||||
severity="error"
|
||||
style={{
|
||||
width: '80%',
|
||||
opacity: 0.85,
|
||||
textAlign: 'center',
|
||||
pointerEvents: 'none'
|
||||
}}
|
||||
>
|
||||
Invalid crontab expression.
|
||||
</Alert>
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
style={{
|
||||
filter: hasInteracted && isValid === false ? 'blur(1px)' : 'none',
|
||||
transition: 'filter 0.2s'
|
||||
}}
|
||||
>
|
||||
<ToolTextResult
|
||||
value={hasInteracted && isValid === false ? '' : result}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
initialValues={initialValues}
|
||||
exampleCards={exampleCards}
|
||||
getGroups={null}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
24
src/pages/tools/time/crontab-guru/meta.ts
Normal file
24
src/pages/tools/time/crontab-guru/meta.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('time', {
|
||||
name: 'Crontab explainer',
|
||||
path: 'crontab-guru',
|
||||
icon: 'mdi:calendar-clock',
|
||||
description:
|
||||
'Parse, validate, and explain crontab expressions in plain English.',
|
||||
shortDescription: 'Crontab expression parser and explainer',
|
||||
keywords: [
|
||||
'crontab',
|
||||
'cron',
|
||||
'schedule',
|
||||
'guru',
|
||||
'time',
|
||||
'expression',
|
||||
'parser',
|
||||
'explain'
|
||||
],
|
||||
longDescription:
|
||||
'Enter a crontab expression (like "35 16 * * 0-5") to get a human-readable explanation and validation. Useful for understanding and debugging cron schedules. Inspired by crontab.guru.',
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
22
src/pages/tools/time/crontab-guru/service.ts
Normal file
22
src/pages/tools/time/crontab-guru/service.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import cronstrue from 'cronstrue';
|
||||
import { isValidCron } from 'cron-validator';
|
||||
|
||||
export function explainCrontab(expr: string): string {
|
||||
try {
|
||||
return cronstrue.toString(expr);
|
||||
} catch (e: any) {
|
||||
return `Invalid crontab expression: ${e.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
export function validateCrontab(expr: string): boolean {
|
||||
return isValidCron(expr, { seconds: false, allowBlankDay: true });
|
||||
}
|
||||
|
||||
export function main(input: string, _options: any): string {
|
||||
if (!input.trim()) return '';
|
||||
if (!validateCrontab(input)) {
|
||||
return 'Invalid crontab expression.';
|
||||
}
|
||||
return explainCrontab(input);
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { tool as timeCrontabGuru } from './crontab-guru/meta';
|
||||
import { tool as timeBetweenDates } from './time-between-dates/meta';
|
||||
import { tool as daysDoHours } from './convert-days-to-hours/meta';
|
||||
import { tool as hoursToDays } from './convert-hours-to-days/meta';
|
||||
import { tool as convertSecondsToTime } from './convert-seconds-to-time/meta';
|
||||
import { tool as convertTimetoSeconds } from './convert-time-to-seconds/meta';
|
||||
import { tool as truncateClockTime } from './truncate-clock-time/meta';
|
||||
import { tool as checkLeapYear } from './check-leap-years/meta';
|
||||
|
||||
export const timeTools = [
|
||||
daysDoHours,
|
||||
@@ -11,5 +13,7 @@ export const timeTools = [
|
||||
convertSecondsToTime,
|
||||
convertTimetoSeconds,
|
||||
truncateClockTime,
|
||||
timeBetweenDates
|
||||
timeBetweenDates,
|
||||
timeCrontabGuru,
|
||||
checkLeapYear
|
||||
];
|
||||
|
||||
4
src/pages/tools/xml/index.ts
Normal file
4
src/pages/tools/xml/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { tool as xmlXmlValidator } from './xml-validator/meta';
|
||||
import { tool as xmlXmlBeautifier } from './xml-beautifier/meta';
|
||||
|
||||
export const xmlTools = [xmlXmlBeautifier, xmlXmlValidator];
|
||||
54
src/pages/tools/xml/xml-beautifier/index.tsx
Normal file
54
src/pages/tools/xml/xml-beautifier/index.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Box } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import { CardExampleType } from '@components/examples/ToolExamples';
|
||||
import { beautifyXml } from './service';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
const initialValues: InitialValuesType = {};
|
||||
|
||||
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
||||
{
|
||||
title: 'Beautify XML',
|
||||
description: 'Beautify a compact XML string for readability.',
|
||||
sampleText: '<root><item>1</item><item>2</item></root>',
|
||||
sampleResult: `<root>\n <item>1</item>\n <item>2</item>\n</root>`,
|
||||
sampleOptions: {}
|
||||
}
|
||||
];
|
||||
|
||||
export default function XmlBeautifier({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
|
||||
const compute = (_values: InitialValuesType, input: string) => {
|
||||
setResult(beautifyXml(input, {}));
|
||||
};
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolTextInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
placeholder="Paste or import XML here..."
|
||||
/>
|
||||
}
|
||||
resultComponent={<ToolTextResult value={result} extension="xml" />}
|
||||
initialValues={initialValues}
|
||||
exampleCards={exampleCards}
|
||||
getGroups={null}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
13
src/pages/tools/xml/xml-beautifier/meta.ts
Normal file
13
src/pages/tools/xml/xml-beautifier/meta.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('xml', {
|
||||
name: 'XML Beautifier',
|
||||
path: 'xml-beautifier',
|
||||
icon: 'mdi:format-align-left',
|
||||
description:
|
||||
'Beautify and reformat XML for improved readability and structure.',
|
||||
shortDescription: 'Beautify XML for readability.',
|
||||
keywords: ['xml', 'beautify', 'format', 'pretty', 'indent'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
23
src/pages/tools/xml/xml-beautifier/service.ts
Normal file
23
src/pages/tools/xml/xml-beautifier/service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { InitialValuesType } from './types';
|
||||
import { XMLParser, XMLBuilder, XMLValidator } from 'fast-xml-parser';
|
||||
|
||||
export function beautifyXml(
|
||||
input: string,
|
||||
_options: InitialValuesType
|
||||
): string {
|
||||
const valid = XMLValidator.validate(input);
|
||||
if (valid !== true) {
|
||||
if (typeof valid === 'object' && valid.err) {
|
||||
return `Invalid XML: ${valid.err.msg} (line ${valid.err.line}, col ${valid.err.col})`;
|
||||
}
|
||||
return 'Invalid XML';
|
||||
}
|
||||
try {
|
||||
const parser = new XMLParser();
|
||||
const obj = parser.parse(input);
|
||||
const builder = new XMLBuilder({ format: true, indentBy: ' ' });
|
||||
return builder.build(obj);
|
||||
} catch (e: any) {
|
||||
return `Invalid XML: ${e.message}`;
|
||||
}
|
||||
}
|
||||
3
src/pages/tools/xml/xml-beautifier/types.ts
Normal file
3
src/pages/tools/xml/xml-beautifier/types.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export type InitialValuesType = {
|
||||
// splitSeparator: string;
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import { beautifyXml } from './service';
|
||||
|
||||
describe('xml-beautifier', () => {
|
||||
it('beautifies valid XML', () => {
|
||||
const input = '<root><a>1</a><b>2</b></root>';
|
||||
const result = beautifyXml(input, {});
|
||||
expect(result).toContain('<root>');
|
||||
expect(result).toContain(' <a>1</a>');
|
||||
expect(result).toContain(' <b>2</b>');
|
||||
});
|
||||
|
||||
it('returns error for invalid XML', () => {
|
||||
const input = '<root><a>1</b></root>';
|
||||
const result = beautifyXml(input, {});
|
||||
expect(result).toMatch(/Invalid XML/i);
|
||||
});
|
||||
});
|
||||
61
src/pages/tools/xml/xml-validator/index.tsx
Normal file
61
src/pages/tools/xml/xml-validator/index.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Box } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import { CardExampleType } from '@components/examples/ToolExamples';
|
||||
import { validateXml } from './service';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
const initialValues: InitialValuesType = {};
|
||||
|
||||
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
||||
{
|
||||
title: 'Validate XML',
|
||||
description: 'Check if an XML string is well-formed.',
|
||||
sampleText: '<root><item>1</item><item>2</item></root>',
|
||||
sampleResult: 'Valid XML',
|
||||
sampleOptions: {}
|
||||
},
|
||||
{
|
||||
title: 'Invalid XML',
|
||||
description: 'Example of malformed XML.',
|
||||
sampleText: '<root><item>1</item><item>2</root>',
|
||||
sampleResult: 'Invalid XML: ...',
|
||||
sampleOptions: {}
|
||||
}
|
||||
];
|
||||
|
||||
export default function XmlValidator({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
|
||||
const compute = (_values: InitialValuesType, input: string) => {
|
||||
setResult(validateXml(input, {}));
|
||||
};
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
input={input}
|
||||
inputComponent={
|
||||
<ToolTextInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
placeholder="Paste or import XML here..."
|
||||
/>
|
||||
}
|
||||
resultComponent={<ToolTextResult value={result} extension="txt" />}
|
||||
initialValues={initialValues}
|
||||
exampleCards={exampleCards}
|
||||
getGroups={null}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
13
src/pages/tools/xml/xml-validator/meta.ts
Normal file
13
src/pages/tools/xml/xml-validator/meta.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('xml', {
|
||||
name: 'XML Validator',
|
||||
path: 'xml-validator',
|
||||
icon: 'mdi:check-decagram',
|
||||
description:
|
||||
'Validate XML files or strings to ensure they are well-formed and error-free.',
|
||||
shortDescription: 'Validate XML for errors.',
|
||||
keywords: ['xml', 'validate', 'check', 'syntax', 'error'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
16
src/pages/tools/xml/xml-validator/service.ts
Normal file
16
src/pages/tools/xml/xml-validator/service.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { InitialValuesType } from './types';
|
||||
import { XMLValidator } from 'fast-xml-parser';
|
||||
|
||||
export function validateXml(
|
||||
input: string,
|
||||
_options: InitialValuesType
|
||||
): string {
|
||||
const result = XMLValidator.validate(input);
|
||||
if (result === true) {
|
||||
return 'Valid XML';
|
||||
} else if (typeof result === 'object' && result.err) {
|
||||
return `Invalid XML: ${result.err.msg} (line ${result.err.line}, col ${result.err.col})`;
|
||||
} else {
|
||||
return 'Invalid XML: Unknown error';
|
||||
}
|
||||
}
|
||||
3
src/pages/tools/xml/xml-validator/types.ts
Normal file
3
src/pages/tools/xml/xml-validator/types.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export type InitialValuesType = {
|
||||
// splitSeparator: string;
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import { validateXml } from './service';
|
||||
|
||||
describe('xml-validator', () => {
|
||||
it('returns Valid XML for well-formed XML', () => {
|
||||
const input = '<root><a>1</a><b>2</b></root>';
|
||||
const result = validateXml(input, {});
|
||||
expect(result).toBe('Valid XML');
|
||||
});
|
||||
|
||||
it('returns error for invalid XML', () => {
|
||||
const input = '<root><a>1</b></root>';
|
||||
const result = validateXml(input, {});
|
||||
expect(result).toMatch(/Invalid XML/i);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user