Skip to content

Commit

Permalink
Prevent triggering onContentChange if nothing changed
Browse files Browse the repository at this point in the history
  • Loading branch information
pejuam committed Oct 3, 2024
1 parent a1bb319 commit 6c90c92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 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

0 comments on commit 6c90c92

Please sign in to comment.