-
-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
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,20 @@ | ||
import { createStore, applyMiddleware, compose } from 'redux' | ||
import thunk from 'redux-thunk' | ||
import rootReducer from './modules' | ||
|
||
import { listenConfig } from './modules/config' | ||
import { listenSelection } from './modules/selection' | ||
|
||
export default () => { | ||
const composeEnhancers = window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose | ||
|
||
const store = createStore( | ||
rootReducer, | ||
composeEnhancers(applyMiddleware(thunk)) | ||
) | ||
|
||
store.dispatch(listenConfig()) | ||
store.dispatch(listenSelection()) | ||
|
||
return store | ||
} |
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,42 @@ | ||
import { appConfigFactory, AppConfig } from '@/app-config' | ||
import { addAppConfigListener } from '@/_helpers/config-manager' | ||
|
||
/*-----------------------------------------------*\ | ||
Actions | ||
\*-----------------------------------------------*/ | ||
|
||
const enum Actions { | ||
NEW_CONFIG = 'configs/NEW_CONFIG' | ||
} | ||
|
||
/*-----------------------------------------------*\ | ||
State | ||
\*-----------------------------------------------*/ | ||
|
||
export type ConfigState = AppConfig | ||
|
||
export default function reducer (state = appConfigFactory(), action) { | ||
switch (action.type) { | ||
case Actions.NEW_CONFIG: | ||
return action.payload | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
/*-----------------------------------------------*\ | ||
Action Creators | ||
\*-----------------------------------------------*/ | ||
|
||
/** When app config is updated */ | ||
export const newConfig = config => ({ type: Actions.NEW_CONFIG, payload: config }) | ||
|
||
/*-----------------------------------------------*\ | ||
Side Effects | ||
\*-----------------------------------------------*/ | ||
|
||
export function listenConfig () { | ||
return dispatch => { | ||
addAppConfigListener(({ config }) => dispatch(newConfig(config))) | ||
} | ||
} |
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,19 @@ | ||
import { combineReducers } from 'redux' | ||
import config, { ConfigState } from './config' | ||
import selection, { SelectionState } from './selection' | ||
import dictionaries, { DictionariesState } from './dictionaries' | ||
import widget, { WidgetState } from './widget' | ||
|
||
export default combineReducers({ | ||
config, | ||
selection, | ||
dictionaries, | ||
widget, | ||
}) | ||
|
||
export type StoreState = { | ||
config: ConfigState | ||
selection: SelectionState | ||
dictionaries: DictionariesState | ||
widget: WidgetState | ||
} |
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,59 @@ | ||
import { message } from '@/_helpers/browser-api' | ||
import { MsgSelection, MsgType } from '@/typings/message' | ||
|
||
/*-----------------------------------------------*\ | ||
Actions | ||
\*-----------------------------------------------*/ | ||
|
||
const enum Actions { | ||
NEW_SELECTION = 'selection/NEW_SELECTION' | ||
} | ||
|
||
/*-----------------------------------------------*\ | ||
State | ||
\*-----------------------------------------------*/ | ||
|
||
export type SelectionState = MsgSelection | ||
|
||
const initState: SelectionState = { | ||
type: MsgType.Selection, | ||
selectionInfo: { | ||
text: '', | ||
context: '', | ||
title: '', | ||
url: '', | ||
favicon: '', | ||
trans: '', | ||
note: '', | ||
}, | ||
mouseX: 0, | ||
mouseY: 0, | ||
dbClick: false, | ||
ctrlKey: false, | ||
} | ||
|
||
export default function reducer (state = initState, action) { | ||
switch (action.type) { | ||
case Actions.NEW_SELECTION: | ||
return action.payload | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
/*-----------------------------------------------*\ | ||
Action Creators | ||
\*-----------------------------------------------*/ | ||
|
||
/** When new selection is made */ | ||
export const newSelection = selection => ({ type: Actions.NEW_SELECTION, payload: selection }) | ||
|
||
/*-----------------------------------------------*\ | ||
Side Effects | ||
\*-----------------------------------------------*/ | ||
|
||
export function listenSelection () { | ||
return dispatch => { | ||
message.self.addListener(MsgType.Selection, message => dispatch(newSelection(message))) | ||
} | ||
} |