Skip to content

Commit

Permalink
release 0.0.2: make the srcipt reloadable
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkHighness committed Mar 2, 2022
1 parent fdbed76 commit 6d6b5e1
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 40 deletions.
22 changes: 15 additions & 7 deletions dist/vue3-mathjax.es.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { defineComponent, onMounted, getCurrentInstance, nextTick, onUpdated, renderSlot } from "vue";
import { defineComponent, onMounted, getCurrentInstance, nextTick, onUpdated, onUnmounted, renderSlot } from "vue";
let mathJaxInjected = false;
let mathJaxReady = false;
let pendingQueue = [];
function mathJax() {
return window.MathJax;
}
function injectMathJaxScript() {
const instance = mathJax();
if (!instance || !instance.version) {
mathJaxInjected = false;
mathJaxReady = false;
pendingQueue.splice(0, pendingQueue.length);
}
if (!mathJaxInjected) {
const mathJaxScript = document.createElement("script");
mathJaxScript.src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js";
Expand Down Expand Up @@ -43,6 +49,7 @@ function initMathJax(options = {}, callback) {
if (v.type == "async")
v.callback();
});
pendingQueue.splice(0, pendingQueue.length);
callback && callback();
}
},
Expand Down Expand Up @@ -104,9 +111,9 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
setup(__props) {
let el;
const renderMathJax = async () => {
if (el) {
await renderByMathJax(el);
}
if (!el)
return;
await renderByMathJax(el);
};
onMounted(() => {
var _a, _b, _c;
Expand All @@ -119,9 +126,10 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
onUpdated(() => {
var _a, _b, _c;
el = (_c = (_b = (_a = getCurrentInstance()) == null ? void 0 : _a.vnode) == null ? void 0 : _b.el) == null ? void 0 : _c.parentNode;
nextTick(() => {
renderMathJax();
});
renderMathJax();
});
onUnmounted(() => {
el = null;
});
return (_ctx, _cache) => {
return renderSlot(_ctx.$slots, "default");
Expand Down
2 changes: 1 addition & 1 deletion dist/vue3-mathjax.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue3-mathjax",
"private": false,
"version": "0.0.1",
"version": "0.0.2",
"main": "dist/vue3-mathjax.umd.js",
"module": "dist/vue3-mathjax.es.js",
"types": "types/index.d.ts",
Expand Down
29 changes: 27 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
<template>
<math-jax>
<div class="container">
<p>This is a vue component for $MathJax$</p>
<p>This is a vue component for $MathJax \, 3$</p>
<p>Instead of using <strong>span</strong> and pass formula as prop</p>
<p>But using <strong>slots</strong></p>
<p>$$c^2 = a ^ 2 + b ^ 2$$</p>
<p>{{ formula }}</p>
</div>
</math-jax>
</template>

<script lang="ts" setup>
import { computed, ref, onMounted, onUnmounted } from "vue";
let idx = ref(0);
let formulas = ref([
"$$\\alpha \\beta \\gamma \\rho \\sigma \\delta \\epsilon$$",
"$$c^2 = a ^ 2 + b ^ 2$$",
"$$e^{ \\pm i\\theta } = \\cos \\theta \\pm i\\sin \\theta$$",
"$$ \\int \\oint \\sum \\prod$$"
]);
let timer: any;
let formula = computed(() => formulas.value[idx.value]);
onMounted(() => {
timer = setInterval(() => {
idx.value = (idx.value + 1) % formulas.value.length;
}, 3000);
});
onUnmounted(() => {
clearInterval(timer);
});
</script>

<style>
.container {
display: flex;
Expand Down
24 changes: 16 additions & 8 deletions src/components/MathJax.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
</template>

<script lang="ts" setup>
import { onMounted, getCurrentInstance, onUpdated, nextTick } from "vue";
import {
onMounted,
getCurrentInstance,
nextTick,
onUpdated,
onUnmounted
} from "vue";
import { renderByMathJax, initMathJax } from "../utils/util";
let el: HTMLElement;
let el: HTMLElement | null;
const renderMathJax = async () => {
if (el) {
await renderByMathJax(el);
}
if (!el) return;
await renderByMathJax(el);
};
onMounted(() => {
Expand All @@ -27,8 +33,10 @@ onMounted(() => {
onUpdated(() => {
el = getCurrentInstance()?.vnode?.el?.parentNode;
nextTick(() => {
renderMathJax();
});
renderMathJax();
});
onUnmounted(() => {
el = null;
});
</script>
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MathJax from "./components/MathJax.vue";
import {
initMathJax,
renderByMathJax,
renderByMathJaxSync,
renderByMathJaxSync
} from "./utils/util";

function install(Vue: any) {
Expand All @@ -14,5 +14,5 @@ export default {
MathJax,
initMathJax,
renderByMathJax,
renderByMathJaxSync,
renderByMathJaxSync
};
7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createApp } from "vue";

// @ts-ignore
import MathJax from "../dist/vue3-mathjax.es.js";
import MathJax from "./index";
import App from "./App.vue";

createApp(App).use(MathJax).mount("#app");
createApp(App)
.use(MathJax)
.mount("#app");
39 changes: 25 additions & 14 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export function mathJax(): any {
}

export function injectMathJaxScript() {
// test if unloaded
const instance = mathJax();

if (!instance || !instance.version) {
mathJaxInjected = false;
mathJaxReady = false;
pendingQueue.splice(0, pendingQueue.length);
}

if (!mathJaxInjected) {
const mathJaxScript = document.createElement("script");

Expand All @@ -33,7 +42,7 @@ export function initMathJax(options = {}, callback?: () => void) {
inlineMath: [["$", "$"]],
displayMath: [["$$", "$$"]],
processEnvironments: true,
processRefs: true,
processRefs: true
},
options: {
skipHtmlTags: [
Expand All @@ -44,26 +53,28 @@ export function initMathJax(options = {}, callback?: () => void) {
"pre",
"code",
"annotation",
"annotation-xml",
"annotation-xml"
],
ignoreHtmlClass: "tex2jax_ignore",
ignoreHtmlClass: "tex2jax_ignore"
},
startup: {
pageReady: () => {
mathJaxReady = true;

mathJax().typeset(pendingQueue.map((v) => v.el));
mathJax().typeset(pendingQueue.map(v => v.el));

pendingQueue.forEach((v) => {
pendingQueue.forEach(v => {
if (v.type == "async") v.callback();
});

pendingQueue.splice(0, pendingQueue.length);

callback && callback();
},
}
},
svg: {
fontCache: "global",
},
fontCache: "global"
}
};

const mergedOptions = Object.assign({}, defaultOptions, options);
Expand All @@ -85,17 +96,17 @@ export function renderByMathJaxSync(el: HTMLElement | HTMLElement[]): void {
if (!mathJaxReady) {
if (Array.isArray(el)) {
pendingQueue.concat(
el.map((v) => {
el.map(v => {
return {
type: "sync",
el: v,
el: v
};
})
);
} else {
pendingQueue.push({
type: "sync",
el,
el
});
}

Expand All @@ -118,20 +129,20 @@ export async function renderByMathJax(
for (let i = 0; i < el.length; i++) {
pendingQueue.push({
type: "sync",
el: el[i],
el: el[i]
});
}

pendingQueue.push({
type: "async",
el: el[el.length - 1],
callback: resolve,
callback: resolve
});
} else {
pendingQueue.push({
type: "async",
el,
callback: resolve,
callback: resolve
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion types/src/components/MathJax.vue.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare const _sfc_main: import("vue").DefineComponent<{}, {
el: HTMLElement;
el: HTMLElement | null;
renderMathJax: () => Promise<void>;
}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, import("vue").EmitsOptions, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}>;
export default _sfc_main;
2 changes: 1 addition & 1 deletion types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare function install(Vue: any): void;
declare const _default: {
install: typeof install;
MathJax: import("vue").DefineComponent<{}, {
el: HTMLElement;
el: HTMLElement | null;
renderMathJax: () => Promise<void>;
}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, import("vue").EmitsOptions, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}>;
initMathJax: typeof initMathJax;
Expand Down

0 comments on commit 6d6b5e1

Please sign in to comment.