forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.js
More file actions
204 lines (191 loc) · 7.05 KB
/
hooks.js
File metadata and controls
204 lines (191 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { useState } from 'react';
import 'tinymce';
import { StrictDict, convertMarkdownToXml } from '../../../../utils';
import ReactStateSettingsParser from '../../data/ReactStateSettingsParser';
import ReactStateOLXParser from '../../data/ReactStateOLXParser';
import { setAssetToStaticUrl } from '../../../../sharedComponents/TinyMceWidget/hooks';
import { ProblemTypeKeys } from '../../../../data/constants/problem';
export const state = StrictDict({
// eslint-disable-next-line react-hooks/rules-of-hooks
isSaveWarningModalOpen: (val) => useState(val),
});
export const saveWarningModalToggle = () => {
const [isSaveWarningModalOpen, setIsOpen] = state.isSaveWarningModalOpen(false);
return {
isSaveWarningModalOpen,
openSaveWarningModal: () => setIsOpen(true),
closeSaveWarningModal: () => setIsOpen(false),
};
};
/** Checks if any tinymce editor in window is dirty */
export const checkIfEditorsDirty = () => {
const EditorsArray = window.tinymce.editors;
return Object.entries(EditorsArray).some(([id, editor]) => {
if (Number.isNaN(parseInt(id, 10))) {
if (!editor.isNotDirty) {
return true;
}
}
return false;
});
};
export const fetchEditorContent = ({ format }) => {
const editorObject = { hints: [] };
const EditorsArray = window.tinymce.editors;
Object.entries(EditorsArray).forEach(([id, editor]) => {
if (Number.isNaN(parseInt(id, 10))) {
if (id.startsWith('answer')) {
const { answers } = editorObject;
const answerId = id.substring(id.indexOf('-') + 1);
editorObject.answers = { ...answers, [answerId]: editor.getContent({ format }) };
} else if (id.includes('Feedback')) {
const { selectedFeedback, unselectedFeedback, groupFeedback } = editorObject;
const feedbackId = id.substring(id.indexOf('-') + 1);
if (id.startsWith('selected')) {
editorObject.selectedFeedback = { ...selectedFeedback, [feedbackId]: editor.getContent() };
}
if (id.startsWith('unselected')) {
editorObject.unselectedFeedback = { ...unselectedFeedback, [feedbackId]: editor.getContent() };
}
if (id.startsWith('group')) {
editorObject.groupFeedback = { ...groupFeedback, [feedbackId]: editor.getContent() };
}
} else if (id.startsWith('hint')) {
const { hints } = editorObject;
hints.push(editor.getContent());
} else {
editorObject[id] = editor.getContent();
}
}
});
return editorObject;
};
export const parseState = ({
problem,
isAdvanced,
isMarkdownEditorEnabled,
ref,
lmsEndpointUrl,
}) => () => {
// Constructs the save payload by parsing the current state of the problem editor.
// If the Markdown editor is enabled, the editor content is converted to OLX using convertMarkdownToXml.
// For advanced problems, raw editor content is used as OLX; for visual ones, it's built via ReactStateOLXParser.
// Settings are then parsed from the OLX and returned alongside the OLX content,
// including markdown incase of markdown editor.
const contentString = ref?.current?.state.doc.toString();
const rawOLX = isMarkdownEditorEnabled ? convertMarkdownToXml(contentString) : contentString;
let reactBuiltOlx;
if (!isMarkdownEditorEnabled) {
const editorObject = fetchEditorContent({ format: '' });
const reactOLXParser = new ReactStateOLXParser({ problem, editorObject });
reactBuiltOlx = setAssetToStaticUrl({ editorValue: reactOLXParser.buildOLX(), lmsEndpointUrl });
}
const reactSettingsParser = new ReactStateSettingsParser({ problem, rawOLX });
const settings = isAdvanced ? reactSettingsParser.parseRawOlxSettings() : reactSettingsParser.getSettings();
return {
settings: {
...settings,
// If the save action isn’t triggered from the Markdown editor, the Markdown content might be outdated. Since the
// Markdown editor shouldn't be displayed in future in this case, we’re sending `null` instead.
// TODO: Implement OLX-to-Markdown conversion to properly handle this scenario.
markdown: isMarkdownEditorEnabled ? contentString : null,
markdown_edited: isMarkdownEditorEnabled,
},
olx: isAdvanced || isMarkdownEditorEnabled ? rawOLX : reactBuiltOlx,
};
};
export const checkForNoAnswers = ({ openSaveWarningModal, problem }) => {
const simpleTextAreaProblems = [ProblemTypeKeys.DROPDOWN, ProblemTypeKeys.NUMERIC, ProblemTypeKeys.TEXTINPUT];
const editorObject = fetchEditorContent({ format: '' });
const { problemType } = problem;
const { answers } = problem;
const answerTitles = simpleTextAreaProblems.includes(problemType) ? {} : editorObject.answers;
const hasTitle = () => {
const titles = [];
answers.forEach(answer => {
const title = simpleTextAreaProblems.includes(problemType) ? answer.title : answerTitles[answer.id];
if (title?.length > 0) {
titles.push(title);
}
});
if (titles.length > 0) {
return true;
}
return false;
};
const hasCorrectAnswer = () => {
let correctAnswer;
answers.forEach(answer => {
if (answer.correct) {
const title = simpleTextAreaProblems.includes(problemType) ? answer.title : answerTitles[answer.id];
if (title?.length > 0) {
correctAnswer = true;
}
}
});
if (correctAnswer) {
return true;
}
return false;
};
if (problemType === ProblemTypeKeys.NUMERIC && !hasTitle()) {
openSaveWarningModal();
return true;
}
if (!hasCorrectAnswer()) {
openSaveWarningModal();
return true;
}
return false;
};
export const checkForSettingDiscrepancy = ({
problem, ref, openSaveWarningModal, isMarkdownEditorEnabled,
}) => {
const contentString = ref?.current?.state.doc.toString();
const rawOLX = isMarkdownEditorEnabled ? convertMarkdownToXml(contentString) : contentString;
const reactSettingsParser = new ReactStateSettingsParser({ problem, rawOLX });
const problemSettings = reactSettingsParser.getSettings();
const rawOlxSettings = reactSettingsParser.parseRawOlxSettings();
let isMismatched = false;
Object.entries(rawOlxSettings).forEach(([key, value]) => {
if (value !== problemSettings[key]) {
isMismatched = true;
}
});
if (isMismatched) {
openSaveWarningModal();
return true;
}
return false;
};
export const getContent = ({
problemState,
openSaveWarningModal,
isAdvancedProblemType,
isMarkdownEditorEnabled,
editorRef,
lmsEndpointUrl,
}) => {
const problem = problemState;
const hasNoAnswers = isAdvancedProblemType || isMarkdownEditorEnabled ? false : checkForNoAnswers({
problem,
openSaveWarningModal,
});
const hasMismatchedSettings = isAdvancedProblemType || isMarkdownEditorEnabled ? checkForSettingDiscrepancy({
ref: editorRef,
problem,
openSaveWarningModal,
isMarkdownEditorEnabled,
}) : false;
if (!hasNoAnswers && !hasMismatchedSettings) {
const data = parseState({
isAdvanced: isAdvancedProblemType,
ref: editorRef,
isMarkdownEditorEnabled,
problem,
lmsEndpointUrl,
})();
return data;
}
return null;
};