-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Text Editor and V2 Editor Framework (#9)
Text Editor and V2 Editor Framework. Documentation to come.
- Loading branch information
1 parent
c93c5e9
commit d8c6b8d
Showing
21 changed files
with
929 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useContext, useEffect } from 'react'; | ||
import { | ||
Spinner, ActionRow, Button, ModalDialog, Toast, | ||
} from '@edx/paragon'; | ||
import { FormattedMessage } from '@edx/frontend-platform/i18n'; | ||
import EditorPageContext from './EditorPageContext'; | ||
import { ActionStates } from './data/constants'; | ||
|
||
const navigateAway = (destination) => { | ||
window.location.assign(destination); | ||
}; | ||
|
||
export default function EditorFooter() { | ||
const { | ||
blockLoading, | ||
setBlockContent, | ||
unitUrlLoading, | ||
unitUrl, | ||
setSaveUnderway, | ||
saveUnderway, | ||
saveResponse, | ||
studioEndpointUrl, | ||
editorRef, | ||
} = useContext(EditorPageContext); | ||
|
||
const onSaveClicked = () => { | ||
if (blockLoading === ActionStates.FINISHED && unitUrlLoading === ActionStates.FINISHED && editorRef) { | ||
const content = editorRef.current.getContent(); | ||
setBlockContent(content); | ||
setSaveUnderway(ActionStates.IN_PROGRESS); | ||
} | ||
}; | ||
const onCancelClicked = () => { | ||
if (unitUrlLoading === ActionStates.FINISHED) { | ||
const destination = `${studioEndpointUrl}/container/${unitUrl.data.ancestors[0].id}`; | ||
navigateAway(destination); | ||
} | ||
}; | ||
useEffect(() => { | ||
if (saveUnderway === ActionStates.FINISHED | ||
&& blockLoading === ActionStates.FINISHED | ||
&& unitUrlLoading === ActionStates.FINISHED) { | ||
const destination = `${studioEndpointUrl}/container/${unitUrl.data.ancestors[0].id}`; | ||
navigateAway(destination); | ||
} | ||
}, [saveUnderway]); | ||
return ( | ||
<div className="editor-footer mt-auto"> | ||
{ saveUnderway === 'complete' && saveResponse.error != null | ||
&& ( | ||
<Toast><FormattedMessage | ||
id="authoring.editorfooter.save.error" | ||
defaultMessage="Error: Content save failed. Try again later." | ||
description="Error message displayed when content fails to save." | ||
/> | ||
</Toast> | ||
)} | ||
<ModalDialog.Footer> | ||
<ActionRow> | ||
<ActionRow.Spacer /> | ||
<Button aria-label="Discard Changes and Return to Learning Context" variant="tertiary" onClick={onCancelClicked}>Cancel</Button> | ||
<Button aria-label="Save Changes and Return to Learning Context" onClick={onSaveClicked}> | ||
{unitUrlLoading !== ActionStates.FINISHED | ||
? <Spinner animation="border" className="mr-3" /> | ||
: ( | ||
<FormattedMessage | ||
id="authoring.editorfooter.savebutton.label" | ||
defaultMessage="Add To Course" | ||
description="Label for Save button" | ||
/> | ||
)} | ||
</Button> | ||
</ActionRow> | ||
</ModalDialog.Footer> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { mount } from 'enzyme'; | ||
import EditorFooter from './EditorFooter'; | ||
import EditorPageContext from './EditorPageContext'; | ||
import { ActionStates } from './data/constants'; | ||
import EditorPageProvider from './EditorPageProvider'; | ||
import { saveBlock } from './data/api'; | ||
|
||
const locationTemp = window.location; | ||
beforeAll(() => { | ||
delete window.location; | ||
window.location = { | ||
assign: jest.fn(), | ||
}; | ||
}); | ||
afterAll(() => { | ||
window.location = locationTemp; | ||
}); | ||
|
||
jest.mock('./data/api', () => { | ||
const originalModule = jest.requireActual('./data/api'); | ||
// Mock the default export and named export saveBlock | ||
return { | ||
__esModule: true, | ||
...originalModule, | ||
saveBlock: jest.fn(() => {}), | ||
}; | ||
}); | ||
|
||
jest.spyOn(React, 'useRef').mockReturnValue({ | ||
current: { | ||
getContent: () => '', | ||
}, | ||
}); | ||
|
||
test('Rendering: loaded', () => { | ||
const context = { | ||
unitUrlLoading: ActionStates.FINISHED, | ||
}; | ||
render( | ||
<EditorPageContext.Provider value={context}> | ||
<EditorFooter /> | ||
</EditorPageContext.Provider>, | ||
); | ||
expect(screen.getByText('Cancel')).toBeTruthy(); | ||
expect(screen.getByText('Add To Course')).toBeTruthy(); | ||
}); | ||
|
||
test('Rendering: loading url', () => { | ||
const context = { | ||
unitUrlLoading: ActionStates.NOT_BEGUN, | ||
}; | ||
render( | ||
<EditorPageContext.Provider value={context}> | ||
<EditorFooter /> | ||
</EditorPageContext.Provider>, | ||
); | ||
expect(screen.getByText('Cancel')).toBeTruthy(); | ||
expect(screen.getAllByRole('button', { 'aria-label': 'Save' })).toBeTruthy(); | ||
expect(screen.queryByText('Add To Course')).toBeNull(); | ||
}); | ||
|
||
test('Navigation: Cancel', () => { | ||
const context = { | ||
unitUrlLoading: ActionStates.FINISHED, | ||
unitUrl: { | ||
data: { | ||
ancestors: | ||
[ | ||
{ id: 'fakeblockid' }, | ||
], | ||
}, | ||
}, | ||
studioEndpointUrl: 'Testurl', | ||
}; | ||
render( | ||
<EditorPageContext.Provider value={context}> | ||
<EditorFooter /> | ||
</EditorPageContext.Provider>, | ||
); | ||
expect(screen.getByText('Cancel')).toBeTruthy(); | ||
userEvent.click(screen.getByText('Cancel')); | ||
expect(window.location.assign).toHaveBeenCalled(); | ||
}); | ||
|
||
test('Navigation: Save', () => { | ||
const wrapper = mount( | ||
<EditorPageProvider | ||
blockType="html" | ||
courseId="myCourse101" | ||
blockId="redosablocksalot" | ||
studioEndpointUrl="celaicboss.axe" | ||
> | ||
<EditorFooter /> | ||
</EditorPageProvider>, | ||
); | ||
const button = wrapper.find({ children: 'Add To Course' }); | ||
expect(button).toBeTruthy(); | ||
button.simulate('click'); | ||
expect(saveBlock).toHaveBeenCalled(); | ||
expect(window.location.assign).toHaveBeenCalled(); | ||
}); |
Oops, something went wrong.