Parcourir la source

渲染填空题类型cloze

Michael Wang il y a 3 ans
Parent
commit
1f320b459e
1 fichiers modifiés avec 5 ajouts et 0 suppressions
  1. 5 0
      src/utils/renderJSON.ts

+ 5 - 0
src/utils/renderJSON.ts

@@ -3,6 +3,7 @@
 import { RichTextBlockJSON, RichTextJSON, RichTextSectionJSON } from "@/types";
 
 let _container = document.createElement("div");
+let clozeIndex = 0;
 /**
  * 将富文本 JSON 渲染到指定的元素中
  *
@@ -11,6 +12,7 @@ let _container = document.createElement("div");
  */
 export function renderRichText(body: RichTextJSON, container?: HTMLDivElement) {
   _container = container || document.createElement("div");
+  clozeIndex = 0;
   let sections = body?.sections || [];
   let nodes = [] as Array<Node>;
   sections.forEach((section) => {
@@ -117,6 +119,9 @@ function renderBlock(block: RichTextBlockJSON, inline: boolean) {
     node.className = "audio";
     node.src = block.value;
     node.controls = true;
+  } else if (block.type === "cloze") {
+    node = document.createElement("span");
+    node.innerHTML = `___${++clozeIndex}___`;
   }
 
   return node as Node;