mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2024-11-27 08:39:59 +08:00
add dayjs to eval context
This commit is contained in:
parent
456f018ffd
commit
6be1af76dc
@ -10,6 +10,7 @@
|
||||
"@emotion/styled": "^11",
|
||||
"@meta-ui/core": "^0.1.0",
|
||||
"@sinclair/typebox": "^0.19.2",
|
||||
"dayjs": "^1.10.6",
|
||||
"framer-motion": "^4",
|
||||
"lodash": "^4.17.21",
|
||||
"mitt": "^3.0.0",
|
||||
|
@ -1,7 +1,11 @@
|
||||
import { useMemo, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import create from "zustand";
|
||||
import _ from "lodash";
|
||||
import mitt from "mitt";
|
||||
import dayjs from "dayjs";
|
||||
import relativeTime from "dayjs/plugin/relativeTime";
|
||||
|
||||
dayjs.extend(relativeTime);
|
||||
|
||||
export const useStore = create<Record<string, any>>(() => ({}));
|
||||
|
||||
@ -31,6 +35,9 @@ export function parseExpression(raw: string): {
|
||||
}
|
||||
|
||||
// TODO: use web worker
|
||||
const builtIn = {
|
||||
dayjs,
|
||||
};
|
||||
export function evalInContext(raw: string, ctx: Record<string, any>) {
|
||||
try {
|
||||
const { dynamic, expression } = parseExpression(raw);
|
||||
@ -42,6 +49,10 @@ export function evalInContext(raw: string, ctx: Record<string, any>) {
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
Object.keys(builtIn).forEach((key) => {
|
||||
// @ts-ignore
|
||||
self[key] = builtIn[key];
|
||||
});
|
||||
Object.keys(ctx).forEach((key) => {
|
||||
// @ts-ignore
|
||||
self[key] = ctx[key];
|
||||
@ -51,6 +62,10 @@ export function evalInContext(raw: string, ctx: Record<string, any>) {
|
||||
// console.error(error);
|
||||
return undefined;
|
||||
} finally {
|
||||
Object.keys(builtIn).forEach((key) => {
|
||||
// @ts-ignore
|
||||
delete self[key];
|
||||
});
|
||||
Object.keys(ctx).forEach((key) => {
|
||||
// @ts-ignore
|
||||
delete self[key];
|
||||
@ -77,8 +92,7 @@ export function useExpression(raw: string) {
|
||||
export const emitter = mitt<Record<string, any>>();
|
||||
|
||||
// EXPERIMENT: utils
|
||||
emitter.on("$utils", ({ name, parameters, ...rest }) => {
|
||||
console.log(name, parameters, rest);
|
||||
emitter.on("$utils", ({ name, parameters }) => {
|
||||
if (name === "alert") {
|
||||
window.alert(parameters);
|
||||
}
|
||||
|
@ -3290,6 +3290,11 @@ dateformat@^3.0.0:
|
||||
resolved "http://192.168.26.29:7001/dateformat/download/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
|
||||
integrity sha1-puN0maTZqc+F71hyBE1ikByYia4=
|
||||
|
||||
dayjs@^1.10.6:
|
||||
version "1.10.6"
|
||||
resolved "http://192.168.26.29:7001/dayjs/download/dayjs-1.10.6.tgz#288b2aa82f2d8418a6c9d4df5898c0737ad02a63"
|
||||
integrity sha1-KIsqqC8thBimydTfWJjAc3rQKmM=
|
||||
|
||||
debug@4, debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1:
|
||||
version "4.3.2"
|
||||
resolved "http://192.168.26.29:7001/debug/download/debug-4.3.2.tgz#f0a49c18ac8779e31d4a0c6029dfb76873c7428b"
|
||||
|
Loading…
Reference in New Issue
Block a user