diff --git a/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx b/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx index a3620b812..6c3bb2423 100644 --- a/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx +++ b/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx @@ -56,43 +56,47 @@ const getEditorIconType = (identifier: string): IconType | null => { return null; }; +const makeEditorDefault = ( + application: WebApplication, + component: SNComponent, + currentDefault: SNComponent +) => { + if (currentDefault) { + application.changeItem(currentDefault.uuid, (m) => { + const mutator = m as ComponentMutator; + mutator.defaultEditor = false; + }); + } + application.changeAndSaveItem(component.uuid, (m) => { + const mutator = m as ComponentMutator; + mutator.defaultEditor = true; + }); +}; + +const removeEditorDefault = ( + application: WebApplication, + component: SNComponent +) => { + application.changeAndSaveItem(component.uuid, (m) => { + const mutator = m as ComponentMutator; + mutator.defaultEditor = false; + }); +}; + +const getDefaultEditor = (application: WebApplication) => { + return application.componentManager + .componentsForArea(ComponentArea.Editor) + .filter((e) => e.isDefaultEditor())[0]; +}; + export const Defaults: FunctionComponent = ({ application }) => { const [editorItems, setEditorItems] = useState([]); const [defaultEditorValue] = useState( () => - getDefaultEditor()?.package_info?.identifier || + getDefaultEditor(application)?.package_info?.identifier || EditorIdentifier.PlainEditor ); - const makeEditorDefault = ( - component: SNComponent, - currentDefault: SNComponent - ) => { - if (currentDefault) { - application.changeItem(currentDefault.uuid, (m) => { - const mutator = m as ComponentMutator; - mutator.defaultEditor = false; - }); - } - application.changeAndSaveItem(component.uuid, (m) => { - const mutator = m as ComponentMutator; - mutator.defaultEditor = true; - }); - }; - - const removeEditorDefault = (component: SNComponent) => { - application.changeAndSaveItem(component.uuid, (m) => { - const mutator = m as ComponentMutator; - mutator.defaultEditor = false; - }); - }; - - const getDefaultEditor = () => { - return application.componentManager - .componentsForArea(ComponentArea.Editor) - .filter((e) => e.isDefaultEditor())[0]; - }; - useEffect(() => { const editors = application.componentManager .componentsForArea(ComponentArea.Editor) @@ -124,15 +128,15 @@ export const Defaults: FunctionComponent = ({ application }) => { const editors = application.componentManager.componentsForArea( ComponentArea.Editor ); - const currentDefault = getDefaultEditor(); + const currentDefault = getDefaultEditor(application); if (value !== EditorIdentifier.PlainEditor) { const editorComponent = editors.filter( (e) => e.package_info.identifier === value )[0]; - makeEditorDefault(editorComponent, currentDefault); + makeEditorDefault(application, editorComponent, currentDefault); } else { - removeEditorDefault(currentDefault); + removeEditorDefault(application, currentDefault); } };