Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new tool): Math Formula format converter #1101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ declare module '@vue/runtime-core' {
MacAddressGenerator: typeof import('./src/tools/mac-address-generator/mac-address-generator.vue')['default']
MacAddressLookup: typeof import('./src/tools/mac-address-lookup/mac-address-lookup.vue')['default']
MathEvaluator: typeof import('./src/tools/math-evaluator/math-evaluator.vue')['default']
MathFormatsConverter: typeof import('./src/tools/math-formats-converter/math-formats-converter.vue')['default']
MenuBar: typeof import('./src/tools/html-wysiwyg-editor/editor/menu-bar.vue')['default']
MenuBarItem: typeof import('./src/tools/html-wysiwyg-editor/editor/menu-bar-item.vue')['default']
MenuIconItem: typeof import('./src/components/MenuIconItem.vue')['default']
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@it-tools/bip39": "^0.0.4",
"@it-tools/oggen": "^1.3.0",
"@plurimath/plurimath": "^0.2.0",
"@sindresorhus/slugify": "^2.2.1",
"@tiptap/pm": "2.1.6",
"@tiptap/starter-kit": "2.1.6",
Expand Down
20 changes: 14 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion src/composable/queryParams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useRouteQuery } from '@vueuse/router';
import { computed } from 'vue';
import { useStorage } from '@vueuse/core';

export { useQueryParam };
export { useQueryParam, useQueryParamOrStorage };

const transformers = {
number: {
Expand Down Expand Up @@ -33,3 +34,31 @@ function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue:
},
});
}

function useQueryParamOrStorage<T>({ name, storageName, defaultValue }: { name: string; storageName: string; defaultValue?: T }) {
const type = typeof defaultValue;
const transformer = transformers[type as keyof typeof transformers] ?? transformers.string;

const storageRef = useStorage(storageName, defaultValue);
const storageDefaultValue = storageRef.value ?? defaultValue;

const proxy = useRouteQuery(name, transformer.toQuery(storageDefaultValue as never));

const ref = computed<T>({
get() {
return transformer.fromQuery(proxy.value) as unknown as T;
},
set(value) {
proxy.value = transformer.toQuery(value as never);
},
});

watch(
ref,
(newValue) => {
storageRef.value = newValue;
},
);

return ref;
}
8 changes: 7 additions & 1 deletion src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as mathFormatsConverter } from './math-formats-converter';

import { tool as asciiTextDrawer } from './ascii-text-drawer';

Expand Down Expand Up @@ -156,7 +157,12 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Math',
components: [mathEvaluator, etaCalculator, percentageCalculator],
components: [
mathEvaluator,
etaCalculator,
percentageCalculator,
mathFormatsConverter,
],
},
{
name: 'Measurement',
Expand Down
12 changes: 12 additions & 0 deletions src/tools/math-formats-converter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EqualNot } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Math Formats Converter',
path: '/math-formats-converter',
description: 'Convert mathematical expression between formats',
keywords: ['math', 'formats', 'converter', 'latex', 'mathml', 'asciimath', 'omml', 'html'],
component: () => import('./math-formats-converter.vue'),
icon: EqualNot,
createdAt: new Date('2024-05-11'),
});
85 changes: 85 additions & 0 deletions src/tools/math-formats-converter/math-formats-converter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<script setup lang="ts">
import Plurimath from '@plurimath/plurimath';
import { useQueryParamOrStorage } from '@/composable/queryParams';

const formats = [
{ value: 'asciimath', label: 'AsciiMath' },
{ value: 'latex', label: 'Latex' },
{ value: 'mathml', label: 'MathML' },
{ value: 'html', label: 'Html' },
{ value: 'omml', label: 'OOML' },
];

const source = ref('');
const sourceFormat = useQueryParamOrStorage({ name: 'src', storageName: 'math-fmts-conv:src', defaultValue: 'latex' });
const targetFormat = useQueryParamOrStorage({ name: 'target', storageName: 'math-fmts-conv:target', defaultValue: 'mathml' });
const target = computedAsync(async () => {
const sourceValue = source.value;
const sourceFormatValue = sourceFormat.value;
const targetFormatValue = targetFormat.value;
if (sourceValue === '') {
return '';
}
if (sourceFormatValue === targetFormatValue) {
return sourceValue;
}
return new Promise<string>((resolve, _reject) => {
try {
const formula = new Plurimath(sourceValue, sourceFormatValue);
let result;
switch (targetFormatValue) {
case 'asciimath':
result = formula.toAsciimath();
break;
case 'latex':
result = formula.toLatex();
break;
case 'mathml':
result = formula.toMathml();
break;
case 'html':
result = formula.toHtml();
break;
case 'omml':
result = formula.toOmml();
break;
default:
result = '# unknown format';
break;
}
resolve(result);
}
catch (e: any) {
resolve(`# error converting formula: ${e.toString()}`);
}
});
});
</script>

<template>
<div>
<c-input-text
v-model:value="source"
multiline
placeholder="Put your math expression here..."
rows="5"
label="Mathematical expression to convert"
raw-text
mb-5
/>
<c-select
v-model:value="sourceFormat"
:options="formats"
placeholder="Source format"
/>

<n-divider />

<c-select
v-model:value="targetFormat"
:options="formats"
placeholder="Source format"
/>
<textarea-copyable v-if="target !== ''" :value="target" :language="targetFormat" />
</div>
</template>
Loading