MathDialog.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <el-dialog
  3. title="输入公式"
  4. :visible="visible"
  5. width="850px"
  6. top="20vh"
  7. :show-close="false"
  8. :close-on-press-escape="false"
  9. :close-on-click-modal="false"
  10. @close="dialogClose"
  11. >
  12. <el-input
  13. v-model="math"
  14. type="textarea"
  15. :autosize="{ minRows: 4, maxRows: 8 }"
  16. resize="none"
  17. placeholder="请输入公式"
  18. @input="renderMathDialog"
  19. />
  20. <el-tabs v-model="activeTab" type="card">
  21. <el-tab-pane
  22. v-for="(macros, category) in mathMacrosListByCategory"
  23. :key="category"
  24. :label="categoryList[category]"
  25. >
  26. <div class="math-macros">
  27. <span
  28. v-for="macro in macros"
  29. :key="macro"
  30. :ref="macro"
  31. :class="['macros-item', `${macro}`]"
  32. @click="insertMacro(macro)"
  33. >
  34. {{ computedMacrosText(macro) }}
  35. </span>
  36. </div>
  37. </el-tab-pane>
  38. </el-tabs>
  39. <div ref="mathContainer" class="formula-render" v-html="math"></div>
  40. <template slot="footer">
  41. <el-button size="medium" @click="dialogClose">取消</el-button>
  42. <el-button type="primary" size="medium" @click="mathConfirm">确定</el-button>
  43. </template>
  44. </el-dialog>
  45. </template>
  46. <script>
  47. import {
  48. mathMacrosListByCategory,
  49. onlyMultiMacros,
  50. onlyAlignMacros,
  51. commonMacros,
  52. categoryList,
  53. } from '@/views/book/courseware/data/richText';
  54. export default {
  55. name: 'MathDialog',
  56. props: {
  57. visible: {
  58. type: Boolean,
  59. default: false,
  60. },
  61. },
  62. data() {
  63. return {
  64. math: '',
  65. activeTab: '0', // 用于控制选中的标签页
  66. mathMacrosListByCategory,
  67. categoryList,
  68. openNumber: 0, // 用于控制 MathJax 渲染
  69. };
  70. },
  71. watch: {
  72. visible(newVal) {
  73. if (newVal) {
  74. this.openNumber += 1;
  75. this.$nextTick(() => {
  76. const macrosRefs = Object.values(mathMacrosListByCategory)
  77. .flat()
  78. .map((macro) => this.$refs[macro]);
  79. window.MathJax.typesetPromise(macrosRefs).catch((err) => {
  80. console.error('MathJax 初始化失败:', err);
  81. });
  82. });
  83. } else {
  84. this.math = '';
  85. this.activeTab = '0';
  86. }
  87. },
  88. },
  89. methods: {
  90. dialogClose() {
  91. this.$emit('update:visible', false);
  92. this.$emit('close');
  93. },
  94. mathConfirm() {
  95. this.$emit('confirm', this.math);
  96. this.dialogClose();
  97. },
  98. computedMacrosText(macro) {
  99. if (onlyMultiMacros.includes(macro.split(' ')[0])) {
  100. return `\\begin{multline}\\${macro}\\end{multline}`;
  101. }
  102. if (onlyAlignMacros.includes(macro.split('{')[0])) {
  103. return `\\begin{align}\\${macro}\\end{align}`;
  104. }
  105. if (commonMacros.includes(macro)) {
  106. return macro;
  107. }
  108. return `$\\${macro}$`;
  109. },
  110. /**
  111. * 插入宏到公式中
  112. * @param {string} macro - 宏字符串
  113. */
  114. insertMacro(macro) {
  115. let _macro = macro.trim();
  116. let math = this.math || '';
  117. // 去除首尾空白
  118. math = math.trim();
  119. // 如果宏是多行公式
  120. if (onlyMultiMacros.includes(_macro.split(' ')[0])) {
  121. _macro = `begin{multline}\\${_macro}\\end{multline}`;
  122. }
  123. if (onlyAlignMacros.includes(_macro.split('{')[0])) {
  124. _macro = `begin{align}\\${_macro}\\end{align}`;
  125. }
  126. // 判断头部和尾部是否有$
  127. let hasHead = math.startsWith('$');
  128. let hasTail = math.endsWith('$');
  129. if (!hasHead) math = `$${math}`;
  130. if (!hasTail) math += '$';
  131. // 将宏插入到末尾$前
  132. if (commonMacros.includes(_macro)) {
  133. math = math.replace(/\$$/, ` ${_macro}$`);
  134. } else {
  135. math = math.replace(/\$$/, ` \\${_macro}$`);
  136. }
  137. this.math = math;
  138. this.renderMathDialog();
  139. },
  140. async renderMathDialog() {
  141. this.dialogMathValidate = false;
  142. await this.$nextTick();
  143. try {
  144. await window.MathJax.typesetPromise([this.$refs.mathContainer]);
  145. let mathRes = this.$refs.mathContainer.innerHTML;
  146. if (mathRes && mathRes.indexOf('merror') === -1) this.dialogMathValidate = true;
  147. } catch (err) {
  148. console.error('公式渲染失败:', err);
  149. }
  150. },
  151. },
  152. };
  153. </script>
  154. <style lang="scss" scoped>
  155. .el-dialog {
  156. :deep &__body {
  157. display: flex;
  158. flex-direction: column;
  159. row-gap: 12px;
  160. .math-macros {
  161. display: flex;
  162. flex-wrap: wrap;
  163. gap: 8px 12px;
  164. align-items: center;
  165. max-height: 300px;
  166. overflow: auto;
  167. .macros-item {
  168. padding: 4px;
  169. cursor: pointer;
  170. border: $border;
  171. border-radius: 4px;
  172. :deep .MathJax {
  173. font-size: 24px !important;
  174. }
  175. }
  176. }
  177. .formula-render {
  178. min-height: 50px;
  179. padding: 15px;
  180. margin-top: 8px;
  181. background: #f8f9fa;
  182. border: 1px solid #e9ecef;
  183. border-radius: 4px;
  184. }
  185. }
  186. }
  187. </style>