-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c909f9
commit d913d45
Showing
2 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<script setup> | ||
import { NModal, NButton, NCard, NPopover, NGrid, NGridItem, NSpace } from 'naive-ui' | ||
import 'katex/dist/katex.css' | ||
import katex from 'katex' | ||
import {ref, watch} from "vue"; | ||
const showModal = ref(false) | ||
const tex = ref('') | ||
const TEXAREA = ref(null) | ||
function addTex(val) { | ||
const indexStart = TEXAREA.value.selectionStart | ||
const indexEnd = TEXAREA.value.selectionEnd | ||
tex.value = tex.value.substr(0, indexStart) + val + tex.value.substr(indexEnd, tex.value.length) | ||
} | ||
watch(tex, () => { | ||
init() | ||
}) | ||
const box = ref(null) | ||
function init(){ | ||
if (tex.value) { | ||
katex.render(tex.value, box.value, { | ||
throwOnError: false | ||
}) | ||
} | ||
} | ||
const letters = ref({ | ||
'α': '\\alpha', | ||
'β': '\\beta', | ||
'γ': '\\gamma', | ||
'δ': '\\delta', | ||
'ϵ': '\\epsilon', | ||
'ζ': '\\zeta', | ||
'η': '\\eta', | ||
'θ': '\\theta', | ||
'ι': '\\iota', | ||
'κ': '\\kappa', | ||
'λ': '\\lambda', | ||
'μ': '\\mu', | ||
'ν': '\\nu', | ||
'ξ': '\\xi', | ||
'ο': '\\omicron', | ||
'π': '\\pi', | ||
'ρ': '\\rho', | ||
'σ': '\\sigma', | ||
'τ': '\\tau', | ||
'υ': '\\upsilon', | ||
'ϕ': '\\phi', | ||
'χ': '\\chi', | ||
'ψ': '\\psi', | ||
'ω': '\\omega', | ||
'ε': '\\varepsilon', | ||
'ϰ': '\\varkappa', | ||
'ϑ': '\\vartheta', | ||
'ϖ': '\\varpi', | ||
'ϱ': '\\varrho', | ||
'ς': '\\varsigma', | ||
'φ': '\\varphi', | ||
'ϝ': '\\digamma', | ||
}) | ||
const logic = ref({ | ||
'×': '\\times', | ||
'÷': '\\div', | ||
'≈': '\\approx', | ||
'≪': '\\ll', | ||
'≫': '\\gg', | ||
'≂': '\\eqsim', | ||
'⪖': '\\eqslantgtr', | ||
'⪕': '\\eqslantless', | ||
'⊕': '\\oplus', | ||
'±': '\\pm', | ||
'∙': '\\bullet', | ||
'∀': '\\forall', | ||
'∁': '\\complement', | ||
'∴': '\\therefore', | ||
'∅': '\\varnothing', | ||
'∃': '\\exists', | ||
'⊂': '\\subset', | ||
'∵': '\\because', | ||
'⊃': '\\supset', | ||
'↦': '\\mapsto', | ||
'∄': '\\nexists', | ||
'∣': '\\mid', | ||
'→': '\\to', | ||
'⟹': '\\implies', | ||
'∈': '\\in', | ||
'∧': '\\land', | ||
'←': '\\gets', | ||
'⟸': '\\impliedby', | ||
'∨': '\\lor', | ||
'↔': '\\leftrightarrow', | ||
'⟺': '\\iff', | ||
'∋': '\\ni', | ||
'¬': '\\neg', | ||
}) | ||
function open(val = '') { | ||
showModal.value = true | ||
tex.value = val | ||
} | ||
const emit = defineEmits(['ok']) | ||
function onOk() { | ||
showModal.value = false | ||
emit('ok', tex.value) | ||
} | ||
function onCancel() { | ||
showModal.value = false | ||
} | ||
defineExpose({open}) | ||
</script> | ||
|
||
<template> | ||
<n-modal v-model:show="showModal" preset="card" style="width: 500px"> | ||
<template #header> | ||
<div>插入公式</div> | ||
</template> | ||
<div> | ||
<div> | ||
<n-popover> | ||
<template #trigger> | ||
<n-button>希腊字母</n-button> | ||
</template> | ||
<div style="width: 300px;height: 100px;"> | ||
<n-grid :y-gap="5" :cols="12"> | ||
<n-grid-item class="item-hover" v-for="(val, key, i) in letters" @click="addTex(val)"> | ||
{{ key }} | ||
</n-grid-item> | ||
</n-grid> | ||
</div> | ||
</n-popover> | ||
<n-popover> | ||
<template #trigger> | ||
<n-button>逻辑符号</n-button> | ||
</template> | ||
<div style="width: 300px;height: 100px;"> | ||
<n-grid :y-gap="5" :cols="12"> | ||
<n-grid-item class="item-hover" v-for="(val, key, i) in logic" @click="addTex(val)"> | ||
{{ key }} | ||
</n-grid-item> | ||
</n-grid> | ||
</div> | ||
</n-popover> | ||
</div> | ||
<div class="texbox"> | ||
<textarea ref="TEXAREA" v-model="tex" class="texarea" placeholder="输入TeX公式" /> | ||
</div> | ||
<div class="preview"> | ||
<p ref="box">预览</p> | ||
</div> | ||
</div> | ||
<template #footer> | ||
<n-space justify="end"> | ||
<n-button @click="onCancel">取消</n-button> | ||
<n-button type="info" @click="onOk">确定</n-button> | ||
</n-space> | ||
</template> | ||
</n-modal> | ||
</template> | ||
|
||
<style scoped> | ||
.item-hover{ | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
border-radius: 3px; | ||
cursor: pointer; | ||
font-size: 17px; | ||
} | ||
.item-hover:hover{ | ||
background-color: #F6F6F6; | ||
} | ||
.texbox{ | ||
padding: 10px; | ||
box-sizing: border-box; | ||
border: 1px solid #aaa; | ||
border-radius: 3px; | ||
} | ||
.texarea{ | ||
width: 100%; | ||
height: 100px; | ||
border: none; | ||
outline: none; | ||
resize: none; | ||
} | ||
.preview{ | ||
padding: 10px; | ||
box-sizing: border-box; | ||
border: 2px dotted #aaa; | ||
margin-top: 10px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters