Skip to content

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

:white_check_mark: Test callout flashcard answer visibility https://github.com/siyuan-note/siyuan/issues/18333

Danielcb93cf6d264b
diff --git a/app/src/card/flashcardMode.test.ts b/app/src/card/flashcardMode.test.ts
new file mode 100644
index 0000000000..939224780a
--- /dev/null
+++ b/app/src/card/flashcardMode.test.ts
@@ -0,0 +1,81 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {hideFlashcardAnswer, prepareCalloutFlashcard, showFlashcardAnswer} from "./flashcardMode";
+
+const createClassElement = () => {
+ const classes = new Set<string>();
+ const element = {
+ classList: {
+ add: (...classNames: string[]) => classNames.forEach((className) => classes.add(className)),
+ remove: (...classNames: string[]) => classNames.forEach((className) => classes.delete(className)),
+ },
+ } as unknown as Element;
+ return {classes, element};
+};
+
+describe("flashcardMode", () => {
+ it("prepares a top-level callout with an answer", () => {
+ let removedAttribute = "";
+ const calloutElement = {
+ querySelector(selector: string) {
+ assert.equal(selector, ":scope > .callout-content > [data-node-id]");
+ return {};
+ },
+ removeAttribute(attribute: string) {
+ removedAttribute = attribute;
+ },
+ } as unknown as Element;
+ const wysiwygElement = {
+ querySelector(selector: string) {
+ assert.equal(selector, ":scope > .callout[custom-riff-decks]");
+ return calloutElement;
+ },
+ } as unknown as Element;
+
+ assert.equal(prepareCalloutFlashcard(wysiwygElement, true), true);
+ assert.equal(removedAttribute, "fold");
+ });
+
+ it("ignores callouts when the mode is disabled or no direct answer exists", () => {
+ let queried = false;
+ const disabledElement = {
+ querySelector(): null {
+ queried = true;
+ return null;
+ },
+ } as unknown as Element;
+ assert.equal(prepareCalloutFlashcard(disabledElement, false), false);
+ assert.equal(queried, false);
+
+ const calloutElement = {
+ querySelector(): null {
+ return null;
+ },
+ removeAttribute() {
+ assert.fail("A callout without an answer must keep its fold attribute");
+ },
+ } as unknown as Element;
+ const wysiwygElement = {
+ querySelector() {
+ return calloutElement;
+ },
+ } as unknown as Element;
+ assert.equal(prepareCalloutFlashcard(wysiwygElement, true), false);
+ });
+
+ it("hides and reveals the callout answer", () => {
+ const {classes, element} = createClassElement();
+ hideFlashcardAnswer(element, {
+ blockquote: false,
+ callout: true,
+ heading: false,
+ list: false,
+ mark: false,
+ superBlock: false,
+ });
+ assert.deepEqual([...classes], ["card__block--hidecallout"]);
+
+ showFlashcardAnswer(element);
+ assert.equal(classes.size, 0);
+ });
+});
diff --git a/app/src/card/flashcardMode.ts b/app/src/card/flashcardMode.ts
new file mode 100644
index 0000000000..d8cdb96de5
--- /dev/null
+++ b/app/src/card/flashcardMode.ts
@@ -0,0 +1,38 @@
+type FlashcardCreationConfig = Pick<Config.IFlashCard,
+ "blockquote" | "callout" | "heading" | "list" | "mark" | "superBlock">;
+
+const FLASHCARD_HIDE_CLASS_ENTRIES: Array<[keyof FlashcardCreationConfig, string]> = [
+ ["mark", "card__block--hidemark"],
+ ["list", "card__block--hideli"],
+ ["superBlock", "card__block--hidesb"],
+ ["blockquote", "card__block--hidebq"],
+ ["callout", "card__block--hidecallout"],
+ ["heading", "card__block--hideh"],
+];
+
+const FLASHCARD_HIDE_CLASSES = FLASHCARD_HIDE_CLASS_ENTRIES.map((entry) => entry[1]);
+
+export const showFlashcardAnswer = (element: Element) => {
+ element.classList.remove(...FLASHCARD_HIDE_CLASSES);
+};
+
+export const hideFlashcardAnswer = (element: Element, config: FlashcardCreationConfig) => {
+ showFlashcardAnswer(element);
+ FLASHCARD_HIDE_CLASS_ENTRIES.forEach(([key, className]) => {
+ if (config[key]) {
+ element.classList.add(className);
+ }
+ });
+};
+
+export const prepareCalloutFlashcard = (wysiwygElement: Element, enabled: boolean) => {
+ if (!enabled) {
+ return false;
+ }
+ const calloutElement = wysiwygElement.querySelector(":scope > .callout[custom-riff-decks]");
+ if (!calloutElement?.querySelector(":scope > .callout-content > [data-node-id]")) {
+ return false;
+ }
+ calloutElement.removeAttribute("fold");
+ return true;
+};
diff --git a/app/src/card/openCard.ts b/app/src/card/openCard.ts
index 21781fc6b1..0c171d8b59 100644
--- a/app/src/card/openCard.ts
+++ b/app/src/card/openCard.ts
@@ -26,6 +26,7 @@ import {updateCardHV} from "./util";
import {showMessage} from "../dialog/message";
import {Menu} from "../plugin/Menu";
import {transaction} from "../protyle/wysiwyg/transaction";
+import {hideFlashcardAnswer, prepareCalloutFlashcard, showFlashcardAnswer} from "./flashcardMode";
const genCardCount = (cardsData: ICardData, allIndex = 0) => {
let newIndex = 0;
@@ -195,12 +196,8 @@ const getEditor = (id: string, protyle: IProtyle, element: Element, currentCard:
hasHide = true;
}
}
- if (window.siyuan.config.flashcard.callout) {
- const calloutElement = protyle.wysiwyg.element.querySelector(":scope > .callout[custom-riff-decks]");
- if (calloutElement?.querySelector(":scope > .callout-content > [data-node-id]")) {
- calloutElement.removeAttribute("fold");
- hasHide = true;
- }
+ if (prepareCalloutFlashcard(protyle.wysiwyg.element, window.siyuan.config.flashcard.callout)) {
+ hasHide = true;
}
if (window.siyuan.config.flashcard.heading) {
if (protyle.wysiwyg.element.querySelector(':scope > [data-type="NodeHeading"]')) {
@@ -220,7 +217,7 @@ const getEditor = (id: string, protyle: IProtyle, element: Element, currentCard:
}
const actionElements = element.querySelectorAll(".card__action");
if (!hasHide) {
- protyle.element.classList.remove("card__block--hidemark", "card__block--hideli", "card__block--hidesb", "card__block--hidebq", "card__block--hidecallout", "card__block--hideh");
+ showFlashcardAnswer(protyle.element);
actionElements[0].classList.add("fn__none");
actionElements[1].querySelectorAll("button.b3-button").forEach((element, btnIndex) => {
if (btnIndex < 2) {
@@ -230,24 +227,7 @@ const getEditor = (id: string, protyle: IProtyle, element: Element, currentCard:
});
actionElements[1].classList.remove("fn__none");
} else {
- if (window.siyuan.config.flashcard.superBlock) {
- protyle.element.classList.add("card__block--hidesb");
- }
- if (window.siyuan.config.flashcard.blockquote) {
- protyle.element.classList.add("card__block--hidebq");
- }
- if (window.siyuan.config.flashcard.callout) {
- protyle.element.classList.add("card__block--hidecallout");
- }
- if (window.siyuan.config.flashcard.heading) {
- protyle.element.classList.add("card__block--hideh");
- }
- if (window.siyuan.config.flashcard.list) {
- protyle.element.classList.add("card__block--hideli");
- }
- if (window.siyuan.config.flashcard.mark) {
- protyle.element.classList.add("card__block--hidemark");
- }
+ hideFlashcardAnswer(protyle.element, window.siyuan.config.flashcard);
actionElements[0].classList.remove("fn__none");
actionElements[1].classList.add("fn__none");
}
@@ -708,7 +688,7 @@ export const bindCardEvent = async (options: {
if (actionElements[0].classList.contains("fn__none")) {
type = "3";
} else {
- editor.protyle.element.classList.remove("card__block--hidemark", "card__block--hideli", "card__block--hidesb", "card__block--hidebq", "card__block--hidecallout", "card__block--hideh");
+ showFlashcardAnswer(editor.protyle.element);
actionElements[0].classList.add("fn__none");
actionElements[1].querySelectorAll("button.b3-button").forEach((element, btnIndex) => {
if (btnIndex < 2) {