diff --git a/package.json b/package.json index 1179686..0e29e66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "expr", - "version": "0.0.3", + "version": "0.0.4", "type": "module", "description": "", "repository": "https://github.com/2234839/siyuan_expr", @@ -24,6 +24,6 @@ "vite-plugin-zip-pack": "^1.2.3" }, "dependencies": { - "@llej/js_util": "^1.0.8" + "@llej/js_util": "^1.0.9" } } diff --git a/plugin.json b/plugin.json index d518ab5..847a116 100644 --- a/plugin.json +++ b/plugin.json @@ -1,30 +1,30 @@ { "name": "expr", - "version": "0.0.1", - "type": "module", - "description": "", - "repository": "https://github.com/2234839/siyuan_expr", - "homepage": "", "author": "崮生_子虚", - "license": "GPL-3.0", - "scripts": { - "make-link": "node --no-warnings ./scripts/make_dev_link.js", - "dev": "vite build --watch", - "build": "vite build" + "url": "https://github.com/2234839/siyuan_expr", + "version": "0.0.3", + "minAppVersion": "2.10.3", + "backends": [ + "all" + ], + "frontends": [ + "all" + ], + "displayName": { + "en_US": "OceanPress siyuan plugin", + "zh_CN": "expr 思源插件" }, - "devDependencies": { - "fast-glob": "^3.3.2", - "minimist": "^1.2.8", - "rollup-plugin-livereload": "^2.0.5", - "siyuan": "0.8.3", - "ts-node": "^10.9.2", - "typescript": "^5.4.5", - "vite": "^4.5.3", - "vite-plugin-static-copy": "^0.15.0", - "vite-plugin-zip-pack": "^1.2.3" + "description": { + "en_US": "To bestow OceanPress with enhanced capabilities.", + "zh_CN": "为 OceanPress 提供更多能力" }, - "dependencies": { - "@llej/js_util": "^1.0.8" + "readme": { + "en_US": "README.md", + "zh_CN": "README.md" }, - "url": "https://github.com/2234839/siyuan_expr" + "funding": { + "custom": [ + "https://afdian.net/@llej0" + ] + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 3d2bee3..c7d2e5a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,14 +12,23 @@ type aliasAttribute = { [K in keyof attribute as `a_${K}`]: attribute[K] }; type MergedBlock = aliasAttribute & Block; const dev = console.log; - +declare global { + var Expr: Expr; +} export default class Expr extends Plugin { IntervalId = 0; /** 只更新这个时间戳以后的表达式 */ - static updated = 0; + updated = 0; /** 为 true 代表正在进行求值运算中 */ - static evalState = false; + evalState = false; async onload() { + globalThis.Expr = this; + this.onunloadFn.push(() => { + //@ts-ignore + delete globalThis.Expr; + }); + + const updateExprEval = this.updateExprEval.bind(this); this.IntervalId = setInterval(updateExprEval, 1_000); updateExprEval(); this.onunloadFn.push(() => clearInterval(this.IntervalId)); @@ -33,72 +42,68 @@ export default class Expr extends Plugin { dev("expr plugin unload"); this.onunloadFn.forEach((fn) => fn()); } -} -/** 对最近更新过的表达式进行求值 */ -async function updateExprEval() { - if (Expr.evalState) { - /** 只有上一轮求值计算进行完毕后才会开始新一轮计算 */ - return; - } - try { - Expr.evalState = true; - const exprAttr: MergedBlock[] = await sql( - `SELECT b.*,a.id AS a_id,a."name" AS a_name,a."value" as a_value,a."type" AS a_type,a.block_id AS a_block_id,a.root_id AS a_root_id,a.box AS a_box,a."path" AS a_path + + /** 对最近更新过的表达式进行求值 */ + async updateExprEval() { + if (this.evalState) { + /** 只有上一轮求值计算进行完毕后才会开始新一轮计算 */ + return; + } + try { + this.evalState = true; + const exprAttr: MergedBlock[] = await sql( + `SELECT b.*,a.id AS a_id,a."name" AS a_name,a."value" as a_value,a."type" AS a_type,a.block_id AS a_block_id,a.root_id AS a_root_id,a.box AS a_box,a."path" AS a_path FROM blocks AS b INNER JOIN attributes AS a ON b.id = a.block_id - WHERE a.name = "${exprName}" and CAST(b.updated AS INTEGER) > ${Expr.updated} + WHERE a.name = "${exprName}" and CAST(b.updated AS INTEGER) > ${this.updated} ORDER BY b.updated DESC;`, - ); - if (exprAttr.length > 0) { - console.log(exprAttr, Expr.updated); - await Promise.all(exprAttr.map(exprEval)); + ); + if (exprAttr.length > 0) { + console.log(exprAttr, this.updated); + await Promise.all(exprAttr.map(this.exprEval.bind(this))); + } + } catch (error) { + dev("求值错误", error); + } finally { + this.evalState = false; } - } catch (error) { - dev("求值错误", error); - } finally { - Expr.evalState = false; } -} -async function exprEval(block: MergedBlock) { - const evalValue = await eval(block.a_value); + async exprEval(block: MergedBlock) { + const evalValue = await eval(block.a_value); - const updated = generateTimestamp(); - if (Number(updated) > Expr.updated) { - Expr.updated = Number(updated); - } - /** TODO,这里应该要考虑ial中不存在相关字段的情况,需要进行添加而非替换 更新块的update时间戳 - * ial = `{: updated="20240604233920" custom-expr="10-11+Math.random()+"2"" custom-expr-value="-0.95897021536132312" id="20240514180539-3zvaoab" style="background-color: var(--b3-font-background4);"} ` - */ - let newKramdownAttr = block.ial!; - if (/updated="\d+"/.test(newKramdownAttr)) { - newKramdownAttr = newKramdownAttr.replace(/updated="\d+"/, `updated="${updated}"`); - } else { - newKramdownAttr = newKramdownAttr.replace(/}$/, ` updated="${updated}"`); - } - if (/custom-expr-value=".*?"/.test(newKramdownAttr)) { - newKramdownAttr = newKramdownAttr.replace( - /custom-expr-value=".*?"/, - `custom-expr-value="${encodeHTML(String(evalValue))}"`, - ); - } else { - newKramdownAttr = newKramdownAttr.replace( - /}$/, - ` custom-expr-value="${encodeHTML(String(evalValue))}"`, - ); + const updated = generateTimestamp(); + if (Number(updated) > this.updated) { + this.updated = Number(updated); + } + /** TODO,这里应该要考虑ial中不存在相关字段的情况,需要进行添加而非替换 更新块的update时间戳 + * ial = `{: updated="20240604233920" custom-expr="10-11+Math.random()+"2"" custom-expr-value="-0.95897021536132312" id="20240514180539-3zvaoab" style="background-color: var(--b3-font-background4);"} ` + */ + let newKramdownAttr = block.ial!; + if (/updated="\d+"/.test(newKramdownAttr)) { + newKramdownAttr = newKramdownAttr.replace(/updated="\d+"/, `updated="${updated}"`); + } else { + newKramdownAttr = newKramdownAttr.replace(/}$/, ` updated="${updated}"`); + } + if (/custom-expr-value=".*?"/.test(newKramdownAttr)) { + newKramdownAttr = newKramdownAttr.replace( + /custom-expr-value=".*?"/, + `custom-expr-value="${encodeHTML(String(evalValue))}"`, + ); + } else { + newKramdownAttr = newKramdownAttr.replace( + /}$/, + ` custom-expr-value="${encodeHTML(String(evalValue))}"`, + ); + } + // custom-expr-value="-0.56273369360008952" + /** 将求值结果更新到块文本 */ + await updateBlock("markdown", String(evalValue + "\n" + newKramdownAttr), block.id); + dev(newKramdownAttr, this.updated); } - // custom-expr-value="-0.56273369360008952" - /** 将求值结果更新到块文本 */ - await updateBlock("markdown", String(evalValue + "\n" + newKramdownAttr), block.id); - dev(newKramdownAttr, Expr.updated); } -async function getBlockByID(blockId: string): Promise { - let sqlScript = `select * from blocks where id ='${blockId}'`; - let data = await sql(sqlScript); - return data[0]; -} function generateTimestamp() { const now = new Date(); return [