|
| 1 | +<script setup lang="ts"> |
| 2 | +import Plurimath from '@plurimath/plurimath'; |
| 3 | +import { useQueryParamOrStorage } from '@/composable/queryParams'; |
| 4 | +
|
| 5 | +const formats = [ |
| 6 | + { value: 'asciimath', label: 'AsciiMath' }, |
| 7 | + { value: 'latex', label: 'Latex' }, |
| 8 | + { value: 'mathml', label: 'MathML' }, |
| 9 | + { value: 'html', label: 'Html' }, |
| 10 | + { value: 'omml', label: 'OOML' }, |
| 11 | +]; |
| 12 | +
|
| 13 | +const source = ref(''); |
| 14 | +const sourceFormat = useQueryParamOrStorage({ name: 'src', storageName: 'math-fmts-conv:src', defaultValue: 'latex' }); |
| 15 | +const targetFormat = useQueryParamOrStorage({ name: 'target', storageName: 'math-fmts-conv:target', defaultValue: 'mathml' }); |
| 16 | +const target = computedAsync(async () => { |
| 17 | + const sourceValue = source.value; |
| 18 | + const sourceFormatValue = sourceFormat.value; |
| 19 | + const targetFormatValue = targetFormat.value; |
| 20 | + if (sourceValue === '') { |
| 21 | + return ''; |
| 22 | + } |
| 23 | + if (sourceFormatValue === targetFormatValue) { |
| 24 | + return sourceValue; |
| 25 | + } |
| 26 | + return new Promise<string>((resolve, _reject) => { |
| 27 | + try { |
| 28 | + const formula = new Plurimath(sourceValue, sourceFormatValue); |
| 29 | + let result; |
| 30 | + switch (targetFormatValue) { |
| 31 | + case 'asciimath': |
| 32 | + result = formula.toAsciimath(); |
| 33 | + break; |
| 34 | + case 'latex': |
| 35 | + result = formula.toLatex(); |
| 36 | + break; |
| 37 | + case 'mathml': |
| 38 | + result = formula.toMathml(); |
| 39 | + break; |
| 40 | + case 'html': |
| 41 | + result = formula.toHtml(); |
| 42 | + break; |
| 43 | + case 'omml': |
| 44 | + result = formula.toOmml(); |
| 45 | + break; |
| 46 | + default: |
| 47 | + result = '# unknown format'; |
| 48 | + break; |
| 49 | + } |
| 50 | + resolve(result); |
| 51 | + } |
| 52 | + catch (e: any) { |
| 53 | + resolve(`# error converting formula: ${e.toString()}`); |
| 54 | + } |
| 55 | + }); |
| 56 | +}); |
| 57 | +</script> |
| 58 | + |
| 59 | +<template> |
| 60 | + <div> |
| 61 | + <c-input-text |
| 62 | + v-model:value="source" |
| 63 | + multiline |
| 64 | + placeholder="Put your math expression here..." |
| 65 | + rows="5" |
| 66 | + label="Mathematical expression to convert" |
| 67 | + raw-text |
| 68 | + mb-5 |
| 69 | + /> |
| 70 | + <c-select |
| 71 | + v-model:value="sourceFormat" |
| 72 | + :options="formats" |
| 73 | + placeholder="Source format" |
| 74 | + /> |
| 75 | + |
| 76 | + <n-divider /> |
| 77 | + |
| 78 | + <c-select |
| 79 | + v-model:value="targetFormat" |
| 80 | + :options="formats" |
| 81 | + placeholder="Source format" |
| 82 | + /> |
| 83 | + <textarea-copyable v-if="target !== ''" :value="target" :language="targetFormat" /> |
| 84 | + </div> |
| 85 | +</template> |
0 commit comments