-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
cf6d001
commit f051248
Showing
20 changed files
with
286 additions
and
133 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
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
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,78 @@ | ||
import { | ||
StoriesStore, | ||
getComponentName, | ||
} from '@component-controls/specification'; | ||
|
||
import { COMPONENT_CONTROLS_STORAGE } from '../types'; | ||
|
||
const encodeFn = (name: string, val: any) => { | ||
// convert RegExp to string | ||
if (val && val.constructor === RegExp) { | ||
return val.toString(); | ||
} else if (name === 'component') { | ||
//serialize components as string of their name | ||
const component = getComponentName(val); | ||
return component ? component : val; | ||
} | ||
return val; | ||
}; | ||
export const saveStore = (store: StoriesStore) => { | ||
for (var key in localStorage) { | ||
if (key.indexOf(COMPONENT_CONTROLS_STORAGE) == 0) { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
localStorage.setItem( | ||
COMPONENT_CONTROLS_STORAGE, | ||
JSON.stringify(store, encodeFn), | ||
); | ||
}; | ||
|
||
export const readStore = ( | ||
store?: StoriesStore, | ||
storyId?: string, | ||
propName?: string, | ||
): StoriesStore | undefined => { | ||
const data = localStorage.getItem(COMPONENT_CONTROLS_STORAGE); | ||
if (data) { | ||
const newStore = JSON.parse(data) as StoriesStore; | ||
if (store && storyId && propName) { | ||
const newValue = (newStore.stories[storyId] as any)[propName]; | ||
store.stories = { | ||
...store.stories, | ||
[storyId]: { | ||
...store.stories[storyId], | ||
[propName]: newValue, | ||
}, | ||
}; | ||
return store; | ||
} | ||
return newStore; | ||
} | ||
return store; | ||
}; | ||
|
||
export const updateStory = ( | ||
store: StoriesStore | undefined, | ||
storyId: string, | ||
propName: string, | ||
newValue: any, | ||
updateLocalStorage?: boolean, | ||
): StoriesStore | undefined => { | ||
if (store) { | ||
store.stories = { | ||
...store.stories, | ||
[storyId]: { | ||
...store.stories[storyId], | ||
[propName]: newValue, | ||
}, | ||
}; | ||
if (updateLocalStorage) { | ||
localStorage.setItem( | ||
COMPONENT_CONTROLS_STORAGE, | ||
JSON.stringify(store, encodeFn), | ||
); | ||
} | ||
} | ||
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
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
Oops, something went wrong.