Skip to content

Commit

Permalink
fix plugin.json
Browse files Browse the repository at this point in the history
  • Loading branch information
2234839 committed Jun 5, 2024
1 parent ee89f08 commit ef2fd55
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 83 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expr",
"version": "0.0.3",
"version": "0.0.4",
"type": "module",
"description": "",
"repository": "https://github.com/2234839/siyuan_expr",
Expand All @@ -24,6 +24,6 @@
"vite-plugin-zip-pack": "^1.2.3"
},
"dependencies": {
"@llej/js_util": "^1.0.8"
"@llej/js_util": "^1.0.9"
}
}
46 changes: 23 additions & 23 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
121 changes: 63 additions & 58 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<Block> {
let sqlScript = `select * from blocks where id ='${blockId}'`;
let data = await sql(sqlScript);
return data[0];
}
function generateTimestamp() {
const now = new Date();
return [
Expand Down

0 comments on commit ef2fd55

Please sign in to comment.