Skip to content

Commit

Permalink
Merge pull request #3988 from digabi/feature/editable-grading-instruc…
Browse files Browse the repository at this point in the history
…tions-misc

Feature/editable grading instructions misc
  • Loading branch information
pejuam authored Oct 3, 2024
2 parents 13fae0f + 6c90c92 commit ff44a15
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/core/__tests__/editor/nbsp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Editor - NBSP', () => {
await act(async () => {
await userEvent.click(await result.findByText('NBSP'))
})
expect(onContentChangeMock).toHaveBeenCalledTimes(2) // focus causes another call
expect(onContentChangeMock).toHaveBeenCalledTimes(1)
expect(onContentChangeMock).toHaveBeenLastCalledWith(expectedOutput, '')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,23 @@ function EditableGradingInstruction({ element }: { element: Element }) {
mount={mount}
state={state}
dispatchTransaction={tr => {
setState(s => s.apply(tr))
const fragment = DOMSerializer.fromSchema(outputSchema).serializeFragment(tr.doc.content)
const div = document.createElement('div')
div.appendChild(fragment)
const path = element.getAttribute('path') ?? ''
if (onContentChange) {
const nbspFixed = div.innerHTML
.replace(/ /g, ' ')
.replace(/<br>/g, '<br/>')
.replace(/<hr>/g, '<hr/>')
onContentChange(nbspFixed, path)
}
setState(s => {
const newContent = s.apply(tr)
if (tr.docChanged) {
const fragment = DOMSerializer.fromSchema(outputSchema).serializeFragment(tr.doc.content)
const div = document.createElement('div')
div.appendChild(fragment)
const path = element.getAttribute('path') ?? ''
if (onContentChange) {
const nbspFixed = div.innerHTML
.replace(/&nbsp;/g, '&#160;')
.replace(/<br>/g, '<br/>')
.replace(/<hr>/g, '<hr/>')
onContentChange(nbspFixed, path)
}
}
return newContent
})
}}
>
<Menu formulaState={formulaState} setFormulaState={setFormulaState} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import React, { useContext } from 'react'
import React from 'react'
import { ExamComponentProps } from '../../createRenderChildNodes'
import { GradingInstructionContext } from '../context/GradingInstructionContext'
import EditableGradingInstruction from './EditableGradingInstruction'

const ExamGradingInstruction: React.FunctionComponent<ExamComponentProps> = ({ element, renderChildNodes }) => {
const { editable } = useContext(GradingInstructionContext)
const ExamGradingInstruction: React.FunctionComponent<ExamComponentProps> = ({ element, renderChildNodes }) => (
<div className="e-exam-grading-instruction notification e-pad-4" data-annotation-anchor="exam-grading-instruction">
{renderChildNodes(element)}
</div>
)

return (
<div className="e-exam-grading-instruction notification e-pad-4" data-annotation-anchor="exam-grading-instruction">
{editable ? <EditableGradingInstruction element={element} /> : renderChildNodes(element)}
</div>
)
}
export default ExamGradingInstruction
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@
}
}

.ProseMirror-focused {
padding: 3px;
}
13 changes: 1 addition & 12 deletions packages/mastering/src/mastering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,6 @@ function findLocalization(gradingInstruction: GradingInstruction, language: stri
)
}

function addGradingInstructionAttributesForExam(exam: Exam, language: string, type: ExamType) {
if (exam.examGradingInstruction) {
const localization = findLocalization(exam.examGradingInstruction, language, type)
exam.examGradingInstruction.element.attr('path', localization?.path() ?? exam.examGradingInstruction.element.path())
}
}

function addGradingInstructionAttributesForQuestions(questions: Question[], language: string, type: ExamType) {
for (const question of questions) {
if (question.gradingInstruction) {
Expand All @@ -725,7 +718,6 @@ function addGradingInstructionAttributesForAnswers(answers: Answer[], language:

function addGradingInstructionAttributes(root: Element, language: string, type: ExamType) {
const exam = parseExamStructure(root)
addGradingInstructionAttributesForExam(exam, language, type)
addGradingInstructionAttributesForQuestions(exam.questions, language, type)
addGradingInstructionAttributesForAnswers(exam.answers, language, type)
}
Expand Down Expand Up @@ -868,9 +860,6 @@ function mkError(message: string, element: Element): SyntaxError {

function parseExamStructure(element: Element): Exam {
const sections = element.find<Element>('//e:section', ns).map(parseSection)
const [examGradingInstruction] = element
.find<Element>('//e:exam-grading-instruction', ns)
.map(parseGradingInstruction)
const topLevelQuestions = sections.flatMap(s => s.questions)
const questions: Question[] = []
const answers: Answer[] = []
Expand All @@ -883,7 +872,7 @@ function parseExamStructure(element: Element): Exam {

topLevelQuestions.forEach(collect)

return { element, sections, questions, topLevelQuestions, answers, examGradingInstruction }
return { element, sections, questions, topLevelQuestions, answers }
}

function parseSection(element: Element): Section {
Expand Down
1 change: 0 additions & 1 deletion packages/mastering/src/mastering/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface Exam {
questions: Question[]
topLevelQuestions: Question[]
answers: Answer[]
examGradingInstruction?: GradingInstruction
}

export interface Section {
Expand Down

0 comments on commit ff44a15

Please sign in to comment.