@@ -2,15 +2,29 @@ import { useHotkeys } from "@mantine/hooks";
2
2
import { RichTextEditor } from "@mantine/tiptap" ;
3
3
import { IconBracketsContain } from "@tabler/icons-react" ;
4
4
import { EditMode } from "../../../logic/CardTypeManager" ;
5
- import { Card , CardType } from "../../../logic/card" ;
5
+ import { Card , CardType , newCards } from "../../../logic/card" ;
6
6
import { Deck } from "../../../logic/deck" ;
7
7
import CardEditor , { useCardEditor } from "./CardEditor" ;
8
8
import classes from "./ClozeCardEditor.module.css" ;
9
9
10
- import { useMantineTheme } from "@mantine/core" ;
10
+ import { Button , Stack , useMantineTheme } from "@mantine/core" ;
11
11
import { Mark } from "@tiptap/core" ;
12
- import { mergeAttributes } from "@tiptap/react" ;
12
+ import { Editor , mergeAttributes } from "@tiptap/react" ;
13
+ import { useCallback , useState } from "react" ;
14
+ import CardEditorFooter from "../CardEditorFooter" ;
15
+ import {
16
+ addFailed ,
17
+ saveFailed ,
18
+ successfullyAdded ,
19
+ successfullySaved ,
20
+ } from "../../custom/Notification/Notification" ;
21
+ import {
22
+ ClozeCardUtils ,
23
+ createClozeCardSet ,
24
+ } from "../../../logic/CardTypeImplementations/ClozeCard" ;
25
+ import { useSharedValue } from "../../../logic/sharedvalue" ;
13
26
27
+ //not used right now, use ui control or remove later
14
28
const Gap = Mark . create ( {
15
29
name : "gap" ,
16
30
inline : true ,
@@ -53,89 +67,95 @@ interface ClozeCardEditorProps {
53
67
mode : EditMode ;
54
68
}
55
69
56
- /*async function finish(
57
- mode: EditMode,
58
- clear: Function,
59
- deck: Deck,
60
- card: Card<CardType.Cloze> | null,
61
- editor: Editor | null,
62
- ) {
63
- const cardInstance = createCardInstance(card, editor);
64
- if (cardInstance !== null) {
65
- if (mode === "edit") {
66
- //SAVE
67
- try {
68
- console.log(cardInstance.id);
69
- const numberOfUpdatedRecords = await updateCard(
70
- cardInstance.id,
71
- cardInstance
72
- );
73
- if (numberOfUpdatedRecords === 0) {
74
- saveFailed();
75
- return;
76
- }
77
- successfullySaved();
78
- } catch (error) {
79
- saveFailed();
80
- }
81
- } else {
82
- //NEW
83
- try {
84
- await newCard(cardInstance, deck);
85
- clear && clear();
86
- successfullyAdded();
87
- } catch (error) {
88
- addFailed();
89
- }
90
- }
91
- }
92
- }*/
93
-
94
- /*function createCardInstance(
95
- card: Card<CardType.Cloze> | null,
96
- editor: Editor | null,
97
- ) {
98
- return card
99
- ? ClozeCardUtils.update(
100
- {
101
- frame: ClozeFrame,
102
- back: backEditor?.getHTML() ?? "",
103
- },
104
- card
105
- )
106
- : ClozeCardUtils.create({
107
- front: frontEditor?.getHTML() ?? "",
108
- back: backEditor?.getHTML() ?? "",
109
- });
110
- }*/
111
-
112
70
export default function ClozeCardEditor ( {
113
71
card,
114
72
deck,
115
73
mode,
116
74
} : ClozeCardEditorProps ) {
117
- const theme = useMantineTheme ( ) ;
118
-
119
75
useHotkeys ( [ [ "mod+Enter" , ( ) => { } ] ] ) ;
120
76
//fix sometime
121
- const editor = useCardEditor ( "" , Gap ) ;
77
+ const editor = useCardEditor (
78
+ useSharedValue ( card ?. content . textReferenceId ?? "" ) ?. value ?? "" ,
79
+ Gap
80
+ ) ;
81
+
82
+ const clear = useCallback ( ( ) => {
83
+ editor ?. commands . setContent ( "" ) ;
84
+ editor ?. commands . focus ( ) ;
85
+ } , [ editor ] ) ;
122
86
123
87
return (
124
- < CardEditor
125
- editor = { editor }
126
- //TODO
127
- className = { classes }
128
- controls = {
129
- < RichTextEditor . Control
130
- tabIndex = { - 1 }
131
- onClick = { ( ) => {
132
- editor ?. commands . toggleMark ( "gap" , { group : Math . random ( ) } ) ;
133
- console . log ( editor ?. getHTML ( ) ) ;
134
- } }
135
- >
136
- < IconBracketsContain />
137
- </ RichTextEditor . Control >
138
- }
139
- />
88
+ < Stack gap = "2rem" >
89
+ < CardEditor
90
+ editor = { editor }
91
+ className = { classes }
92
+ controls = {
93
+ //not used right now, use ui control or remove later
94
+ < RichTextEditor . Control
95
+ tabIndex = { - 1 }
96
+ onClick = { ( ) => {
97
+ editor ?. commands . toggleMark ( "gap" , { group : Math . random ( ) } ) ;
98
+ console . log ( editor ?. getHTML ( ) ) ;
99
+ } }
100
+ >
101
+ < IconBracketsContain />
102
+ </ RichTextEditor . Control >
103
+ }
104
+ />
105
+ < CardEditorFooter
106
+ finish = { ( ) => finish ( mode , clear , deck , card , editor ) }
107
+ mode = { mode }
108
+ />
109
+ </ Stack >
140
110
) ;
141
111
}
112
+
113
+ function finish (
114
+ mode : EditMode ,
115
+ clear : Function ,
116
+ deck : Deck ,
117
+ card : Card < CardType . Cloze > | null ,
118
+ editor : Editor | null
119
+ ) {
120
+ if ( mode === "edit" ) {
121
+ //ISSUE newly introduced cards through edit are not recognized
122
+ try {
123
+ if ( card ) {
124
+ ClozeCardUtils . update (
125
+ {
126
+ text : editor ?. getHTML ( ) ?? "" ,
127
+ } ,
128
+ card
129
+ ) ;
130
+ }
131
+ successfullySaved ( ) ;
132
+ } catch ( error ) {
133
+ saveFailed ( ) ;
134
+ }
135
+ } else {
136
+ const occlusionNumberSet : number [ ] = getOcclusionNumberSet (
137
+ editor ?. getHTML ( ) ?? ""
138
+ ) ;
139
+ try {
140
+ createClozeCardSet ( {
141
+ text : editor ?. getHTML ( ) ?? "" ,
142
+ occlusionNumberSet,
143
+ } ) . then ( ( cards ) => newCards ( cards , deck ) ) ;
144
+ clear ( ) ;
145
+ successfullyAdded ( ) ;
146
+ } catch ( error ) {
147
+ addFailed ( ) ;
148
+ }
149
+ }
150
+ }
151
+
152
+ function getOcclusionNumberSet ( text : string ) {
153
+ const regex = / \{ \{ c \d : : ( (? ! \{ \{ | } } ) .) * \} \} / g;
154
+ const matches = text . match ( regex ) ;
155
+ const cardDigits = new Set < number > ( ) ;
156
+ matches ?. forEach ( ( match ) => {
157
+ const digit = parseInt ( match [ 3 ] ) ;
158
+ cardDigits . add ( digit ) ;
159
+ } ) ;
160
+ return Array . from ( cardDigits ) ;
161
+ }
0 commit comments