Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Allow inserting the current time in table cells https://github.com/siyuan-note/siyuan/issues/20523

Daniel634468a9c583
diff --git a/app/src/protyle/hint/slashUndo.test.ts b/app/src/protyle/hint/slashUndo.test.ts
index 6cf4796fd3..94b1ae709d 100644
--- a/app/src/protyle/hint/slashUndo.test.ts
+++ b/app/src/protyle/hint/slashUndo.test.ts
@@ -4,6 +4,8 @@ import {join} from "node:path";
import {test} from "node:test";
import {runInNewContext} from "node:vm";
import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+import dayjs = require("dayjs");
+import {isBuiltinSlashHint, registerBuiltinSlashHint} from "./builtinSlash";
import {shouldCaptureHintUndoFocus} from "./blockHintRange";
const source = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest);
@@ -11,6 +13,48 @@ const declaration = source.statements.find(node => isClassDeclaration(node) && n
assert.ok(declaration && isClassDeclaration(declaration));
const fill = declaration.members.find(member => member.name?.getText(source) === "fill").getText(source);
+test("current time replaces the command through one insertion with its original undo context", () => {
+ const code = transpileModule(`class Hint { ${fill} } new Hint();`, {
+ compilerOptions: {target: ScriptTarget.ES2020},
+ }).outputText;
+ for (const lite of [false, true]) {
+ for (const splitChar of ["/", "、"]) {
+ let insertions = 0;
+ const block = {outerHTML: `prefix ${splitChar}time suffix`, getAttribute: () => "original"};
+ const range = {startContainer: block, startOffset: 12, endOffset: 12,
+ setStart: (_node: unknown, offset: number) => { range.startOffset = offset; },
+ deleteContents: () => { assert.fail("the insertion must retain the original command for undo"); }};
+ const context = {undoFocusId: "original", undoFocusStart: "12", undoFocusEnd: "12"};
+ const protyle = {lite, toolbar: {range}, wysiwyg: {element: {}},
+ options: {hint: {extend: [{key: "/", hint: registerBuiltinSlashHint(() => [])}]}}};
+ const hint = runInNewContext(code, {
+ Constants: {BLOCK_HINT_KEYS: ["(("], ZWSP: "\u200b"},
+ Lute: {Caret: "caret"}, hideElements() {}, isProtyleListItemFragment: () => false,
+ hasClosestBlock: () => block, shouldCaptureHintUndoFocus, focusByRange() {},
+ isBuiltinSlashHint, dayjs: () => dayjs("2026-10-14T00:07:00"),
+ getUndoFocusContext: () => { assert.equal(range.startOffset, 12); return context; },
+ insertHTML: (html: string, owner: unknown, isBlock: boolean, useProtyleRange: boolean,
+ insertByCursor: boolean, position: unknown, undo: unknown) => {
+ insertions++;
+ assert.equal(html, "00:07");
+ assert.equal(owner, protyle);
+ assert.equal(isBlock, false);
+ assert.equal(useProtyleRange, true);
+ assert.equal(insertByCursor, false);
+ assert.equal(position, undefined);
+ assert.equal(undo, context);
+ assert.equal(range.startOffset, 7);
+ assert.equal(range.endOffset, 12);
+ assert.equal(block.outerHTML, `prefix ${splitChar}time suffix`);
+ },
+ });
+ Object.assign(hint, {source: "hint", splitChar, lastIndex: 7});
+ hint.fill("\u200b7", protyle, false);
+ assert.equal(insertions, 1);
+ }
+ }
+});
+
for (const scenario of ["replace", "insert", "empty-list-item", "list-item"]) {
test(`slash undo retains the original caret for ${scenario}`, () => {
const inList = scenario.includes("list-item");
diff --git a/app/src/protyle/hint/tableCellSlash.test.ts b/app/src/protyle/hint/tableCellSlash.test.ts
new file mode 100644
index 0000000000..62449b40c9
--- /dev/null
+++ b/app/src/protyle/hint/tableCellSlash.test.ts
@@ -0,0 +1,81 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, forEachChild, isVariableDeclaration, ScriptTarget, transpileModule} from "typescript";
+import {TABLE_CELL_SLASH_IDS} from "../util/tableCellRichMenu";
+import {registerBuiltinSlashHint} from "./builtinSlash";
+import {resolveSlashMenuItems} from "./slashMenu";
+
+const Constants = {ZWSP: "\u200b"};
+
+const getInitializer = (file: string, name: string) => {
+ const source = createSourceFile(file, readFileSync(join(__dirname, file), "utf8"), ScriptTarget.Latest);
+ let initializer: string;
+ const visit = (node: import("typescript").Node) => {
+ if (isVariableDeclaration(node) && node.name.getText(source) === name) {
+ initializer = node.initializer.getText(source);
+ }
+ forEachChild(node, visit);
+ };
+ visit(source);
+ assert.ok(initializer);
+ return transpileModule(`(${initializer});`, {compilerOptions: {target: ScriptTarget.ES2020}}).outputText;
+};
+
+const hotkeys = new Proxy({}, {get: () => ({custom: ""})});
+const builtinItems = runInNewContext(getInitializer("extend.ts", "getBuiltinSlashMenuItems"), {
+ Constants,
+ Lute: {Caret: "caret"},
+ window: {siyuan: {
+ languages: new Proxy({}, {get: (_target, key) => key === "currentTime" ? "Current Time" : String(key)}),
+ config: {keymap: {editor: new Proxy({}, {get: () => hotkeys})}},
+ }},
+ getHostCapabilities: () => ({widgets: false, remoteKernel: false}),
+ isDisabledFeature: () => false, updateHotkeyTip: (value: string) => value,
+ getHotkeyOrMarker: () => "", getSuperBlockCommand: (layout: string) => layout,
+ getBuiltinStyleCSS: () => "", getBuiltinStyleLabel: (id: string) => id,
+ getBuiltinInlineStylePreview: () => ({color: "", backgroundColor: ""}),
+}) as typeof import("./extend").getBuiltinSlashMenuItems;
+const hintSource = getInitializer("extend.ts", "hintSlash");
+const safeSlashSource = getInitializer("../render/tableCellRichEditor.ts", "safeSlash");
+
+for (const lite of [false, true]) {
+ for (const richEditor of [false, true]) {
+ test(`current time remains available in ${lite ? "lite" : "full"} ${richEditor ? "rich" : "inline"} table cells`, () => {
+ const root = {};
+ class FocusElement {
+ closest() {
+ return richEditor ? null : {closest: () => root};
+ }
+ }
+ let visible = true;
+ const protyle = {
+ lite, hint: {element: {closest: (): null => null}}, options: {upload: {}}, wysiwyg: {element: root},
+ };
+ const hintSlash = runInNewContext(hintSource, {
+ registerBuiltinSlashHint, getBuiltinSlashMenuItems: builtinItems,
+ SLASH_MENU_ROOT_PATH: "slash", slashBuiltinStyleIDs: {},
+ getConfiguredEntryVisibility: () => visible,
+ getEntryCatalogNode: () => ({}), getSlashMenuEntryPath: (id: string) => id,
+ getEntryOrder: (): string[] => [], refreshSlashMenuCatalog() {},
+ areProtylePluginExtensionsEnabled: () => false,
+ Element: FocusElement, getSelection: () => ({focusNode: new FocusElement()}),
+ TABLE_CELL_SLASH_IDS, resolveSlashMenuItems,
+ }) as typeof import("./extend").hintSlash;
+ const hint = richEditor ? runInNewContext(safeSlashSource, {
+ registerBuiltinSlashHint, hintSlash, TABLE_CELL_SLASH_IDS,
+ }) as typeof hintSlash : hintSlash;
+ for (const alias of ["time", "now", "current time", "当前时间", "dangqianshijian", "dqsj"]) {
+ const items = hint(alias, protyle as unknown as IProtyle, "hint");
+ const time = items.find(item => item.id === "currentTime");
+ assert.ok(time, alias);
+ assert.equal(time.value, Constants.ZWSP + 7);
+ }
+ assert.ok(hint("", protyle as unknown as IProtyle, "hint").every(item => TABLE_CELL_SLASH_IDS.has(item.id)));
+ visible = false;
+ assert.equal(hint("time", protyle as unknown as IProtyle, "hint").length, 0);
+ });
+ }
+}
diff --git a/app/src/protyle/util/tableCellRichMenu.ts b/app/src/protyle/util/tableCellRichMenu.ts
index 0ef98d9102..bdf70fd57d 100644
--- a/app/src/protyle/util/tableCellRichMenu.ts
+++ b/app/src/protyle/util/tableCellRichMenu.ts
@@ -1,5 +1,5 @@
export const TABLE_CELL_SLASH_IDS = new Set([
"ref", "heading1", "heading2", "heading3", "heading4", "heading5", "heading6", "list", "orderedList", "check",
"quote", "code", "math", "link", "bold", "italic", "underline", "strike", "mark", "sup", "sub", "inlineCode",
- "kbd", "tag", "inlineMath", "emoji",
+ "kbd", "tag", "inlineMath", "emoji", "currentTime",
]);