diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 6d721979..95b3bd52 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -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", diff --git a/packages/runtime/src/store.ts b/packages/runtime/src/store.ts index be8bd431..ce94a98b 100644 --- a/packages/runtime/src/store.ts +++ b/packages/runtime/src/store.ts @@ -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>(() => ({})); @@ -31,6 +35,9 @@ export function parseExpression(raw: string): { } // TODO: use web worker +const builtIn = { + dayjs, +}; export function evalInContext(raw: string, ctx: Record) { try { const { dynamic, expression } = parseExpression(raw); @@ -42,6 +49,10 @@ export function evalInContext(raw: string, ctx: Record) { 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) { // 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>(); // EXPERIMENT: utils -emitter.on("$utils", ({ name, parameters, ...rest }) => { - console.log(name, parameters, rest); +emitter.on("$utils", ({ name, parameters }) => { if (name === "alert") { window.alert(parameters); } diff --git a/yarn.lock b/yarn.lock index 2d9b2834..c09cfbb3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"