AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

基于Mathlive將數學公式編輯器集成到可視化搭建平臺

來源: 責編: 時間:2024-02-05 09:32:25 216觀看
導讀hi, 大家好, 我是徐小夕. 上篇文章和大家分享了剛開發完的可視化搭建產品——橙子試卷. 收到了很多用戶的反饋和建議, 其中有一個建議我覺得非常有意思, 所以花了一天時間研究和實現了這個用戶需求。具體需求如下:對

MXK28資訊網——每日最新資訊28at.com

hi, 大家好, 我是徐小夕. 上篇文章和大家分享了剛開發完的可視化搭建產品——橙子試卷. 收到了很多用戶的反饋和建議, 其中有一個建議我覺得非常有意思, 所以花了一天時間研究和實現了這個用戶需求。MXK28資訊網——每日最新資訊28at.com

具體需求如下:MXK28資訊網——每日最新資訊28at.com

對于高等數學類課程的試卷, 能不能實現編輯數學公式的功能呢?MXK28資訊網——每日最新資訊28at.com

經過了一系列的調研和可行性分析, 我覺得這個需求非常有價值, 而且應用面很廣, 技術上從 web 的角度也是可以實現的, 所以我花了一點時間實現了它。MXK28資訊網——每日最新資訊28at.com

在文章末尾我也會把集成了數學公式的可視化編輯器地址分享給大家, 供大學學習參考。MXK28資訊網——每日最新資訊28at.com

接下里我會和大家分享一下如何從零實現一個支持數學公式編輯器的組件, 并集成到 vue3 項目中。MXK28資訊網——每日最新資訊28at.com

數學公式編輯器的技術實現

MXK28資訊網——每日最新資訊28at.com

首先要想實現展示我們熟知的數學公式, 在 web 里我們需要了解以下幾種表示法:MXK28資訊網——每日最新資訊28at.com

  • latex
  • mathml
  • ascimath

以上三種表示法實際上都是標記語言, 通過特定的語法格式來優雅的展示數學公式, 簡單舉例如下:MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

如果大家熟悉這些標記語言, 我們就可以很容易的使用前端開源庫 MathJax 來編寫數學公式。MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

具體使用如下:MXK28資訊網——每日最新資訊28at.com

<template>  <div class="Formula">    <p id="math"></p>    <p ref="math" v-html=“str”></p>  </div></template><script>export default {  name: 'Formula',  data() {    return {      str: ''    }  },  mounted() {    this.$nextTick(() => {      // typesetPromise 需要 [] 包裹      this.str = '//[x = {-b //pm //sqrt{b^2-4ac} //over 2a}.//]'      window.MathJax.typesetPromise([this.$refs.math]).catch(err => err)            // tex2chtml 不需要 [] 包裹      const str = `x = {-b //pm //sqrt{b^2-4ac} //over 2a}`      document        .querySelector('#math')        .appendChild(window.MathJax.tex2chtml(str))    })  }}</script>

但是作為極具產品觀念的我來說, 讓用戶學習這些標記語言是非常痛苦的, 所以我們要想一種更簡單的方式, 讓用戶不用學習, 也能可視化的編寫復雜數學公式。MXK28資訊網——每日最新資訊28at.com

我研究了一些成熟的庫之后發現, 有一個開源庫非常適合我的“簡單化”訴求, 它就是——mathlive。MXK28資訊網——每日最新資訊28at.com

MathLive 是一個功能強大的 Web 組件,它提供了一個易于使用的界面來編輯數學公式。MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

但是網上它的文檔和在 vue3 中的使用非常稀少, 可以說是完全沒有. 因為我做的橙子試卷搭建平臺采用 vue3 來實現的, 所以我需要研究一種支持 vue3 的方案。MXK28資訊網——每日最新資訊28at.com

好在我找到了它們純英文版的文檔, 咬了一遍它的文檔之后, 對 MathLive 有了更深的理解。MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

文檔里提供了原生 webcomponent 的使用方法 和 react的使用案例, 好在我有5年多的 react 駕齡, 看起來還是非常順手的. 下面我就直接分享如何把它集成到 vue3 項目里. 感興趣的朋友可以直接拿來應用到自己的項目里。MXK28資訊網——每日最新資訊28at.com

1、安裝和引入MathLive組件

我們可以用 npm 或者 yarn 或者 pnpm(推薦) 安裝:MXK28資訊網——每日最新資訊28at.com

pnpm install mathlive

接下來我們來注冊一下組件:MXK28資訊網——每日最新資訊28at.com

import * as MathLive from 'mathlive';import VueMathfield from '@/assets/vue-mathlive.mjs';app.use(VueMathfield, MathLive);

這樣我們就可以在全局使用 mathlive 公式編輯器組件了。MXK28資訊網——每日最新資訊28at.com

2、在項目中使用

MXK28資訊網——每日最新資訊28at.com

為了實現上圖的效果, 我們需要在頁面里定義組件:MXK28資訊網——每日最新資訊28at.com

<mathlive-mathfield  :options="{ smartFence: false }"  @input="handleChange"  :value="content">  {{ content }}</mathlive-mathfield>

這個是 mathlive 默認是引入標簽, 當然我們可以修改它的定義, 如果你是 react 選手, 也可以直接這么使用:MXK28資訊網——每日最新資訊28at.com

// d.tsdeclare global {  namespace JSX {    interface IntrinsicElements {      'math-field': React.DetailedHTMLProps<React.HTMLAttributes<MathfieldElement>, MathfieldElement>;    }  }}// app.tsximport "./App.css";import "http://unpkg.com/mathlive";import { useState } from "react";function App() {  const [value, setValue] = useState<string>("");  return (    <div className="App">      <math-field         onInput={          (evt: React.ChangeEvent<HTMLElement>) =>             setValue(evt.target.value)        }      >        {value}      </math-field>      <p>Value: {value}</p>    </div>  );}export default App;

接下來就來學習一下它的屬性, 下面是 vue 版的 props, 非常重要, 大家可以收藏一下:MXK28資訊網——每日最新資訊28at.com

圖片MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

這里我整理了幾個常用的api:MXK28資訊網——每日最新資訊28at.com

  • value 組件綁定的值。
  • input 輸入內容時的監聽函數, 用來更新和獲取value。
  • options 組件選項屬性, 比如編輯模式, 可讀性等, 非常重要。

當然如果你想修改它的顯示樣式, 可以通過操作 dom 或屬性, 也可以直接用 css 覆蓋:MXK28資訊網——每日最新資訊28at.com

.content {  :deep(math-field) {    width: 100%;  }}

通過以上步驟, 基本上能實現我們上面分享的公式編輯器了:MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

快速集成到可視化搭建平臺

接下來分享一下如何集成到我們的橙子試卷零代碼搭建平臺中。MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

首先我們需要先在物料庫中添加數學公式編輯器組件, 具體思路如下:MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

UI代碼:MXK28資訊網——每日最新資訊28at.com

<template>  <div>    <div class="title" :style="{ color: editorStore.data[index].titleColor }">      <mathlive-mathfield        :options="{          smartFence: false,          readOnly: true,        }"      >        {{ editorStore.data[index].titleText }}      </mathlive-mathfield>    </div>    <a-radio-group      :direction="editorStore.data[index].direction"      v-model="editorStore.data[index].value"    >      <a-radio        v-for="item in editorStore.data[index].options"        :value="item.label"        :key="item.label"        :style="{ '--radio-options-color': editorStore.data[index].optionsColor }"        @click.stop      >        {{ item.label }} . {{ item.value }}</a-radio      >    </a-radio-group>    <Message      :value="editorStore.data[index].value"      :answer="editorStore.data[index].answer"      :auto="editorStore.data[index].auto"      :analysis="editorStore.data[index].analysis"    />  </div></template>

其中我們需要關注 mathlive-mathfield 的一個屬性: readonly, 它是讓我們把 latex 渲染成可視的數學公式的必備屬性, 否則我們只能在編輯模式下看到數學公式了。MXK28資訊網——每日最新資訊28at.com

接下來我們來編寫組件配置層代碼, 具體效果如下:MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

當我們編輯標題時, 會打開公式編輯器:MXK28資訊網——每日最新資訊28at.com

MXK28資訊網——每日最新資訊28at.com

這部分我們是通過配置DSL自動生成的屬性面板, 這塊的知識我在分享H 5-Dooring 零代碼實現原理時有具體的介紹, 這里就不一一分析了, 直接上代碼:MXK28資訊網——每日最新資訊28at.com

export default class Math {    component: any;    constructor(id: string, arr=[{label:'A',value:'蘋果'},{label:'B',value:'香蕉'}]) {        this.component = {            component: 'math',            type: 'editor.math',            id,            check: true,            titleText: '數學題目',            titleColor: 'black',            options: arr,            symbol: 'A,B,C...',            direction: 'horizontal',            optionsColor:'black',            answer:undefined,            analysis: '',            auto: '',            value:undefined,            margin: [10, 10, 10, 10],            scores:0,            required:false,            attrbite: [                {                    name: 'editor.titleText',                    field: 'titleText',                    component: 'math'                },                {                    name: 'editor.titleColor',                    field: 'titleColor',                    component: 'color',                    props: {                        type: 'color'                    }                },                {                    name: 'editor.optionConfig',                    field: 'options',                    component: 'options',                    props: {                        options:arr                    }                },                {                    name: 'editor.optionSymbol',                    field: 'symbol',                    component: 'select',                    props: {                        options: [                            {label:'A,B,C...',value:'A,B,C...'},                            {label:'1,2,3...',value:'1,2,3...'},                            {label:'a,b,c...',value:'a,b,c...'},                            {label:'I,II,III...',value:'I,II,III...'}                        ],                    }                },                {                    name: 'editor.optionDirection',                    field: 'direction',                    component: "select",                    props: {                        options: [{ label:'editor.horizontal', value: 'horizontal' }, { label: 'editor.vertical', value: 'vertical' }],                    }                },                {                    name: 'editor.optionsColor',                    field: 'optionsColor',                    component: 'color',                    props: {                        type: 'color'                    }                },                {                    name: 'editor.answerSettings',                    field: 'answer',                    component: 'select-lable',                },                {                    name: 'editor.answerillustrate',                    field: 'analysis',                    component: 'textarea'                },                {                    name: 'editor.grading',                    field: 'auto',                    component: 'switch'                },                {                    name: 'editor.scores',                    field: 'scores',                    component: 'numberInput',                    props: {                        min: 0                    }                },                {                    name: 'editor.required',                    field: 'required',                    component: 'switch'                },                {                    name: 'editor.margin',                    field: 'margin',                    component: "padding",                    props: {                        min: 0,                        type:'margin'                    }                },            ]        }    }}

這樣我們就能把編輯器組件成功變成一個零代碼可消費的組件, 當然這離不開我實現的零代碼渲染引擎, 這塊我會在后面的文章中詳細分享.MXK28資訊網——每日最新資訊28at.com

以上我們就實現了橙子試卷 可視化搭建系統的數學公式編輯器功能,MXK28資訊網——每日最新資訊28at.com

體驗地址: https://turntip.cn/formManager。MXK28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-73793-0.html基于Mathlive將數學公式編輯器集成到可視化搭建平臺

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 做好設計:架構模式

下一篇: 我敢說:99.9%的程序員根本沒在項目中使用過Java的這個功能!

標簽:
  • 熱門焦點
  • Rust中的高吞吐量流處理

    作者 | Noz編譯 | 王瑞平本篇文章主要介紹了Rust中流處理的概念、方法和優化。作者不僅介紹了流處理的基本概念以及Rust中常用的流處理庫,還使用這些庫實現了一個流處理程序
  • 從 Pulsar Client 的原理到它的監控面板

    背景前段時間業務團隊偶爾會碰到一些 Pulsar 使用的問題,比如消息阻塞不消費了、生產者消息發送緩慢等各種問題。雖然我們有個監控頁面可以根據 topic 維度查看他的發送狀態,
  • 從零到英雄:高并發與性能優化的神奇之旅

    作者 | 波哥審校 | 重樓作為公司的架構師或者程序員,你是否曾經為公司的系統在面對高并發和性能瓶頸時感到手足無措或者焦頭爛額呢?筆者在出道那會為此是吃盡了苦頭的,不過也得
  • 每天一道面試題-CPU偽共享

    前言:了不起:又到了每天一到面試題的時候了!學弟,最近學習的怎么樣啊 了不起學弟:最近學習的還不錯,每天都在學習,每天都在進步! 了不起:那你最近學習的什么呢? 了不起學弟:最近在學習C
  • 小紅書1周漲粉49W+,我總結了小白可以用的N條漲粉筆記

    作者:黃河懂運營一條性教育視頻,被54萬人&ldquo;珍藏&rdquo;是什么體驗?最近,情感博主@公主是用鮮花做的,火了!僅僅憑借一條視頻,光小紅書就有超過128萬人,為她瘋狂點贊!更瘋狂的是,這
  • 網傳小米汽車開始篩選交付中心 建筑面積不低于3000平方米

    7月7日消息,近日有微博網友@長三角行健者爆料稱,據經銷商集團反饋,小米汽車目前已經開始了交付中心的篩選工作,要求候選場地至少有120個車位,建筑不能低
  • iQOO 11S新品發布會

    iQOO將在7月4日19:00舉行新品發布會,推出杭州亞運會電競賽事官方用機iQOO 11S。
  • iQOO Neo8系列新品發布會

    旗艦雙芯 更強更Pro
  • 朋友圈可以修改可見范圍了 蘋果用戶可率先體驗

    近日,iOS用戶迎來微信8.0.27正式版更新,除了可更換二維碼背景外,還新增了多項實用功能。在新版微信中,朋友圈終于可以修改可見范圍,簡單來說就是已發布的朋友圈
Top