Skip to content

Commit

Permalink
fix: cleanup cloze preview
Browse files Browse the repository at this point in the history
  • Loading branch information
h16nning committed Feb 21, 2024
1 parent 3201d2e commit 3f330ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 49 deletions.
56 changes: 9 additions & 47 deletions src/components/editcard/CardEditor/ClozeCardEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@ import CardEditor, { useCardEditor } from "./CardEditor";
import classes from "./ClozeCardEditor.module.css";

import { Stack } from "@mantine/core";
import { Mark } from "@tiptap/core";
import { mergeAttributes } from "@tiptap/react";
import { useCallback, useEffect, useMemo, useState } from "react";
import CardEditorFooter from "../CardEditorFooter";
import {
addFailed,
saveFailed,
successfullyAdded,
successfullySaved,
} from "../../custom/Notification/Notification";
import {
ClozeCardUtils,
createClozeCardSet,
Expand All @@ -27,42 +18,13 @@ import {
getSharedValue,
useSharedValue,
} from "../../../logic/sharedvalue";

//not used right now, use ui control or remove later
const Gap = Mark.create({
name: "gap",
inline: true,
exitable: true,
group: "inline",
selectable: true,
defaultTag: "span",
addAttributes() {
return {
group: 0,
};
},
parseHTML() {
return [{ tag: "span.gap" }];
},
renderHTML({ HTMLAttributes }) {
const elem = document.createElement("span");
const content = document.createElement("div");
content.innerHTML = "Test: " + HTMLAttributes.group;
content.className = "inline-menu";
elem.appendChild(content);

Object.entries(mergeAttributes(HTMLAttributes, { class: "gap" })).forEach(
([attr, val]) => elem.setAttribute(attr, val)
);

elem.addEventListener("click", () => {
elem.classList.toggle("clicked");
});

return elem;
//return ['span', mergeAttributes(HTMLAttributes, {class: 'gap'}), 0];
},
});
import {
addFailed,
saveFailed,
successfullyAdded,
successfullySaved,
} from "../../custom/Notification/Notification";
import CardEditorFooter from "../CardEditorFooter";

interface ClozeCardEditorProps {
card: Card<CardType.Cloze> | null;
Expand All @@ -82,7 +44,6 @@ export default function ClozeCardEditor({
//fix sometime
const editor = useCardEditor({
content: useSharedValue(card?.content.textReferenceId ?? "")?.value ?? "",
extensions: [Gap],
onUpdate: ({ editor }) => {
if (editor?.getHTML().valueOf() !== editorContent.valueOf()) {
setEditorContent(editor?.getHTML() ?? "");
Expand All @@ -99,11 +60,12 @@ export default function ClozeCardEditor({

const smallestAvailableOcclusionNumber = useMemo(() => {
const occlusionNumberSet = getOcclusionNumberSet(editorContent);
for (let i = 1; i < 100; i++) {
for (let i = 1; i < 10; i++) {
if (!occlusionNumberSet.includes(i)) {
return i;
}
}
return 9;
}, [editorContent]);

const clear = useCallback(() => {
Expand Down
12 changes: 10 additions & 2 deletions src/logic/CardTypeImplementations/ClozeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export const ClozeCardUtils: CardTypeManager<CardType.Cloze> = {
update(params: { text: string }, existingCard: Card<CardType.Cloze>) {
setSharedValue(existingCard.content.textReferenceId, params.text);
updateCard(existingCard.id, {
preview: toPreviewString(params.text),
preview: toPreviewString(
params.text.replace(/\{\{c\d::((?!\{\{|}}).)*\}\}/g, (match) =>
match.slice(6, -2)
)
),
});
return { preview: toPreviewString(params.text), ...existingCard };
},
Expand All @@ -38,7 +42,11 @@ export const ClozeCardUtils: CardTypeManager<CardType.Cloze> = {
}): Card<CardType.Cloze> {
return {
...createCardSkeleton(),
preview: toPreviewString(params.text),
preview: toPreviewString(
params.text.replace(/\{\{c\d::((?!\{\{|}}).)*\}\}/g, (match) =>
match.slice(6, -2)
)
),
content: {
type: CardType.Cloze,
occlusionNumber: params.occlusionNumber,
Expand Down

0 comments on commit 3f330ca

Please sign in to comment.