feat(new tool): Math Formula format converter

Fix part of #1009
This commit is contained in:
sharevb 2024-05-18 15:24:02 +02:00 committed by ShareVB
parent e876d03608
commit bc0c344f18
7 changed files with 150 additions and 8 deletions

1
components.d.ts vendored
View file

@ -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']

View file

@ -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",

20
pnpm-lock.yaml generated
View file

@ -11,6 +11,9 @@ dependencies:
'@it-tools/oggen':
specifier: ^1.3.0
version: 1.3.0
'@plurimath/plurimath':
specifier: ^0.2.0
version: 0.2.0
'@sindresorhus/slugify':
specifier: ^2.2.1
version: 2.2.1
@ -2457,6 +2460,10 @@ packages:
fsevents: 2.3.2
dev: true
/@plurimath/plurimath@0.2.0:
resolution: {integrity: sha512-5ca7O58sq/PHA7zZHDjDrg8pr1u9FPRNWOy8nqADKrExw9LHAwHX2FuvU3sz51liNZpwZ1KC5Nwy5U48S6Y5Xw==}
dev: false
/@polka/url@1.0.0-next.21:
resolution: {integrity: sha512-a5Sab1C4/icpTZVzZc5Ghpz88yQtGOyNqYXcZgOssB2uuAr+wF/MvN6bgtW32q7HHrvBki+BsZ0OuNv6EV3K9g==}
dev: true
@ -3351,7 +3358,7 @@ packages:
dependencies:
'@unhead/dom': 0.5.1
'@unhead/schema': 0.5.1
'@vueuse/shared': 10.7.2(vue@3.3.4)
'@vueuse/shared': 10.9.0(vue@3.3.4)
unhead: 0.5.1
vue: 3.3.4
transitivePeerDependencies:
@ -3993,10 +4000,10 @@ packages:
- vue
dev: false
/@vueuse/shared@10.7.2(vue@3.3.4):
resolution: {integrity: sha512-qFbXoxS44pi2FkgFjPvF4h7c9oMDutpyBdcJdMYIMg9XyXli2meFMuaKn+UMgsClo//Th6+beeCgqweT/79BVA==}
/@vueuse/shared@10.9.0(vue@3.3.4):
resolution: {integrity: sha512-Uud2IWncmAfJvRaFYzv5OHDli+FbOzxiVEQdLCKQKLyhz94PIyFC3CHcH7EDMwIn8NPtD06+PNbC/PiO0LGLtw==}
dependencies:
vue-demi: 0.14.6(vue@3.3.4)
vue-demi: 0.14.7(vue@3.3.4)
transitivePeerDependencies:
- '@vue/composition-api'
- vue
@ -9151,8 +9158,8 @@ packages:
vue: 3.3.4
dev: false
/vue-demi@0.14.6(vue@3.3.4):
resolution: {integrity: sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==}
/vue-demi@0.14.7(vue@3.3.4):
resolution: {integrity: sha512-EOG8KXDQNwkJILkx/gPcoL/7vH+hORoBaKgGe+6W7VFMvCYJfmF2dGbvgDroVnI8LU7/kTu8mbjRZGBU1z9NTA==}
engines: {node: '>=12'}
hasBin: true
requiresBuild: true
@ -9442,6 +9449,7 @@ packages:
/workbox-google-analytics@7.0.0:
resolution: {integrity: sha512-MEYM1JTn/qiC3DbpvP2BVhyIH+dV/5BjHk756u9VbwuAhu0QHyKscTnisQuz21lfRpOwiS9z4XdqeVAKol0bzg==}
deprecated: It is not compatible with newer versions of GA starting with v4, as long as you are using GAv3 it should be ok, but the package is not longer being maintained
dependencies:
workbox-background-sync: 7.0.0
workbox-core: 7.0.0

View file

@ -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: {
@ -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;
}

View file

@ -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';
@ -156,7 +157,12 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Math',
components: [mathEvaluator, etaCalculator, percentageCalculator],
components: [
mathEvaluator,
etaCalculator,
percentageCalculator,
mathFormatsConverter,
],
},
{
name: 'Measurement',

View file

@ -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'),
});

View file

@ -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>