You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
3.6 KiB
JavaScript
63 lines
3.6 KiB
JavaScript
import App from './App.js';
|
|
import nanoid from './nanoid.min.js';
|
|
import {load_bin_handler, hash_change_handler} from './handlers/index.js';
|
|
const produce = immer.produce;
|
|
immer.setAutoFreeze(false); // needed for high-frequency updated values, like onkeyup->note.temp_text; only once 'save' is called will it produce a new immutable state tree
|
|
|
|
var root = document.body;
|
|
|
|
/* Ruthlessly taken from [https://gist.github.com/kitze/fb65f527803a93fb2803ce79a792fff8]: */
|
|
const handleActions = (actionsMap, defaultState) =>
|
|
(state=defaultState, {type, payload}) =>
|
|
produce(state, draft => {
|
|
const action = actionsMap[type];
|
|
action && action(draft, payload);
|
|
});
|
|
|
|
// get bin id from URL, and load notes from that bin; if a bin id isn't specified in the URL, create a new one and update the URL
|
|
let bin_id = window.location.hash.substring(1); // extract the leading '#'
|
|
if(bin_id === ''){
|
|
bin_id = nanoid();
|
|
window.history.replaceState(null,'', '#'+bin_id);
|
|
}
|
|
// the actual loading from server is done later, after store and dispatch are defined
|
|
|
|
const reducer = handleActions({
|
|
'new-bin': (s, bin) => { s.bin=bin; s.notes=[]; },
|
|
'bin-requested': (s, bin_id) => { s.bin={id:bin_id}; s.notes = []; },
|
|
'bin-loaded': (s, {bin, _notes}) => { s.bin=bin; s.notes=_notes.map(n=>({is_editing: false, temp_text: n.text, bin_id: bin.id, note:n})); },
|
|
'update-search-term': (s, search_term) => { s.search_term=search_term; },
|
|
'update-search-results': (s, _notes) => { s.notes =_notes.map(n=>({is_editing: false, temp_text: '', bin_id: s.bin.id, note:n})); },
|
|
'add-note': (s, {id, date}) => { s.notes.unshift({is_editing: true, temp_text: '', bin_id: s.bin.id, is_focused:true, note: {id: id, text: '', modified: date}}); },
|
|
'notes-loaded': (s, _notes) => { s.notes = _notes.map(n=>({is_editing: false, temp_text: n.text, bin_id: s.bin_id, note:n})); },
|
|
'update-note-text': (s, {id, text}) => { const note_s=s.notes.find(n=>n.note.id===id); note_s.note.text=text; }, // updates underlying note text (i.e. the "model", not the app note_state) "in the background" (e.g. from a server-pushed update), regardless of whether it's being edited; "save" is a separate action, below
|
|
'update-note-editing': (s, {id, is_editing}) => { const note_s=s.notes.find(n=>n.note.id===id); note_s.is_editing=is_editing; note_s.temp_text=note_s.note.text; },
|
|
'save-note-edit': (s, {id, text}) => { const note_s=s.notes.find(n=>n.note.id===id); note_s.note.text=text; note_s.temp_text=text; note_s.is_editing=false; },
|
|
'update-sorting': (s, sorting) => { s.sorting=sorting; }
|
|
}, {
|
|
bin: {id: bin_id},
|
|
notes: [
|
|
//{is_editing: false, temp_text: '', bin_id: '', note: {id: nanoid(), text: 'Note one', modified: 1}},
|
|
],
|
|
search_term: '',
|
|
sorting: 'new->old'
|
|
//search_result_notes: []
|
|
});
|
|
|
|
// create Redux store, with Redux DevTools enabled:
|
|
const store = Redux.createStore(reducer, /* preloadedState, */
|
|
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
|
|
const dispatch = function(action_type, payload){ store.dispatch({type:action_type, payload}); };
|
|
|
|
store.subscribe(()=>{
|
|
m.render(root, m(App, {state: store.getState(), dispatch}));
|
|
});
|
|
|
|
window.addEventListener("hashchange", (e)=>hash_change_handler(store.getState(), dispatch, e), false);
|
|
|
|
let state = store.getState();
|
|
load_bin_handler(state, dispatch, bin_id);
|
|
|
|
// we don't want Mithril auto-redraw system in place, since Redux will manually re-render when necessary with store.subscribe():
|
|
// m.mount(root, App);
|
|
m.render(root, m(App, {state, dispatch})) |