diff --git a/src/app/ReactDockableApp.tsx b/src/app/ReactDockableApp.tsx index 9c35c11..422a26d 100644 --- a/src/app/ReactDockableApp.tsx +++ b/src/app/ReactDockableApp.tsx @@ -213,8 +213,20 @@ const ModalClickShield = () => { ); }; +let appDispatch: any = undefined; + +function doAppDispatch(data:any) { + + if(appDispatch === undefined) { + console.error('appDispatch was not set'); + } else { + appDispatch(data); + } +} + function App() { const [state, dispatch] = useReducer(breaditorAppReducer, getInitialState()); + appDispatch = dispatch; if (dispatch == undefined) { console.info(state); @@ -311,7 +323,7 @@ function App() { } function init() { - const domNode = document.getElementById('react-root') as Element; + const domNode = document.getElementById('react-root') as HTMLElement; const root = createRoot(domNode); @@ -320,6 +332,24 @@ function init() { addDocument(spriteMaker('Sprite C')); addDocument(mapMaker('Map D')); + /// MAIN<->RENDERER handlers + domNode.addEventListener('OPEN_FILE_SUCCESS', (event:any) => { + debugger; + doAppDispatch({ + type: 'DOC_OPEN_FILE_FOUND', + thunkDispatch: doAppDispatch, + filePath: event.detail.filePath + }); + }); + + domNode.addEventListener('OPEN_FILE_FAILURE', (event:any) => { + doAppDispatch({ + type: 'DOC_OPEN_FILE_FAILURE', + thunkDispatch: doAppDispatch, + error: 'failed to load' + event.detail.filePath + }); + }); + root.render(); } diff --git a/src/app/state-management/in-memory/reducers/filemenu.reducer.ts b/src/app/state-management/in-memory/reducers/filemenu.reducer.ts index fb435d7..75948a2 100644 --- a/src/app/state-management/in-memory/reducers/filemenu.reducer.ts +++ b/src/app/state-management/in-memory/reducers/filemenu.reducer.ts @@ -31,12 +31,14 @@ const initialFileMenuState: FileMenuState = {}; const fileMenuReducer: Reducer = createReducer( initialFileMenuState, { - DOC_OPEN_FILE_FOUND: (state, action: OpenFileCancelledAction) => { + DOC_OPEN_FILE_FOUND: (state, action: OpenFileFoundAction) => { action.thunkDispatch({type: 'SET_IS_LOADING', newState: false}); + alert("now load: " + action.filePath); return {...state}; }, - DOC_OPEN_FILE_FAILURE: (state, action: OpenFileFoundAction) => { + DOC_OPEN_FILE_FAILURE: (state, action: OpenFileCancelledAction) => { action.thunkDispatch({type: 'SET_IS_LOADING', newState: false}); + alert("failed to load load: " + action.error); return {...state}; }, DOC_OPEN_EXISTING: (state, action: OpenDocumentAction) => { @@ -51,31 +53,9 @@ const fileMenuReducer: Reducer = createReducer( }; console.log('INVOKING window.electronAPI.openFileDialog'); + // @ts-ignore window.electronAPI.openFileDialog(options); - console.log('bueller?'); - - /* - dialog - .showOpenDialog(options) - .then((result: any) => { - if (!result.canceled && result.filePaths.length > 0) { - const filePath = result.filePaths[0]; - dispatch({ - type: 'OPEN_FILE_FOUND', - payload: filePath, - thunkDispatch: dispatch, - }); - } - }) - .catch((error: any) => { - dispatch({ - type: 'OPEN_FILE_FAILURE', - payload: error, - thunkDispatch: dispatch, - }); - }); - */ }; thunk(action.thunkDispatch); diff --git a/src/main/main.ts b/src/main/main.ts index 689a9d7..68a6f37 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -54,22 +54,21 @@ function createWindow() { mainWindow.close(); }); - ipcMain.on('open-file-dialog', (_options: any) => { - const options: OpenDialogOptions = _options; + ipcMain.on('open-file-dialog', (event:Electron.IpcMainEvent, arg:any[]) => { + const options: OpenDialogOptions = arg[0]; + const senderWebContents = event.sender; - console.log('MAIN open-file-dialog'); dialog .showOpenDialog(options) .then((result) => { if (!result.canceled && result.filePaths.length > 0) { const filePath = result.filePaths[0]; - console.log('filePath', filePath); - //dispatch({type: 'OPEN_FILE_SUCCESS', payload: filePath}); + + senderWebContents.send('OPEN_FILE_SUCCESS', {type: 'OPEN_FILE_SUCCESS', payload: filePath}); } }) .catch((error) => { - console.log('error', error); - //dispatch({type: 'OPEN_FILE_FAILURE', payload: error}); + senderWebContents.send('OPEN_FILE_FAILURE', {type: 'OPEN_FILE_FAILURE', payload: error}); }); }); diff --git a/src/main/preload/preload.js b/src/main/preload/preload.js index 19d923b..e858ce5 100644 --- a/src/main/preload/preload.js +++ b/src/main/preload/preload.js @@ -2,11 +2,9 @@ const { contextBridge, ipcRenderer, - OpenDialogOptions /*, ipcMain*/, + OpenDialogOptions } = require('electron'); -console.log('preload.ts!'); - contextBridge.exposeInMainWorld('electronAPI', { appClose: () => { ipcRenderer.send('app-close'); @@ -21,3 +19,18 @@ contextBridge.exposeInMainWorld('electronAPI', { ipcRenderer.send('open-file-dialog', options); }, }); + + +ipcRenderer.on('OPEN_FILE_SUCCESS', (e, msg) => { + document.getElementById('react-root').dispatchEvent( + new CustomEvent('OPEN_FILE_SUCCESS', { + detail: { filePath: msg.payload } + })); +}); + +ipcRenderer.on('OPEN_FILE_FAILURE', (e, msg) => { + document.getElementById('react-root').dispatchEvent( + new CustomEvent('OPEN_FILE_FAILURE', { + detail: { filePath: msg.payload } + })); +});