mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-15 12:42:45 +00:00
Add getHistoryItems function
This commit is contained in:
@@ -5,6 +5,8 @@ const createClass = require('create-react-class');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const cx = require('classnames');
|
const cx = require('classnames');
|
||||||
|
|
||||||
|
import { getHistoryItems, historyExists } from '../../utils/versionHistory.js';
|
||||||
|
|
||||||
//Import all themes
|
//Import all themes
|
||||||
const ThemeSnippets = {};
|
const ThemeSnippets = {};
|
||||||
ThemeSnippets['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js');
|
ThemeSnippets['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js');
|
||||||
@@ -46,7 +48,9 @@ const Snippetbar = createClass({
|
|||||||
return {
|
return {
|
||||||
renderer : this.props.renderer,
|
renderer : this.props.renderer,
|
||||||
themeSelector : false,
|
themeSelector : false,
|
||||||
snippets : []
|
snippets : [],
|
||||||
|
historyExists : false,
|
||||||
|
showHistory : false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -58,14 +62,21 @@ const Snippetbar = createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
componentDidUpdate : async function(prevProps) {
|
componentDidUpdate : async function(prevProps) {
|
||||||
if(prevProps.renderer != this.props.renderer || prevProps.theme != this.props.theme || prevProps.snippetBundle != this.props.snippetBundle) {
|
const update = {};
|
||||||
const snippets = this.compileSnippets();
|
let newData = false;
|
||||||
this.setState({
|
|
||||||
snippets : snippets
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
|
if(prevProps.renderer != this.props.renderer || prevProps.theme != this.props.theme || prevProps.snippetBundle != this.props.snippetBundle) {
|
||||||
|
update.snippets = this.compileSnippets();
|
||||||
|
newData = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
if(historyExists(this.props.brew) != this.state.historyExists){
|
||||||
|
update.historyExists = !this.state.historyExists;
|
||||||
|
newData = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
newData && this.setState(update);
|
||||||
|
},
|
||||||
|
|
||||||
mergeCustomizer : function(oldValue, newValue, key) {
|
mergeCustomizer : function(oldValue, newValue, key) {
|
||||||
if(key == 'snippets') {
|
if(key == 'snippets') {
|
||||||
@@ -139,7 +150,24 @@ const Snippetbar = createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
showHistory : function () {
|
showHistory : function () {
|
||||||
console.log('show history');
|
if(!this.state.historyExists) return;
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
showHistory : !this.state.showHistory
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
renderHistoryItems : function() {
|
||||||
|
const historyItems = getHistoryItems(this.props.brew);
|
||||||
|
|
||||||
|
return <div className='dropdown'>
|
||||||
|
{_.map(historyItems, (item, index)=>{
|
||||||
|
return <div className='snippet' key={index} >
|
||||||
|
<i className={`fas fa-${index+1}`} />
|
||||||
|
<span className='name' title={item.title}>{item.title}</span>
|
||||||
|
</div>;
|
||||||
|
})}
|
||||||
|
</div>;
|
||||||
},
|
},
|
||||||
|
|
||||||
renderEditorButtons : function(){
|
renderEditorButtons : function(){
|
||||||
@@ -162,9 +190,10 @@ const Snippetbar = createClass({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return <div className='editors'>
|
return <div className='editors'>
|
||||||
<div className={'editorTool history'}
|
<div className={`editorTool history ${this.state.historyExists ? 'active' : ''}`}
|
||||||
onClick={this.showHistory} >
|
onClick={this.showHistory} >
|
||||||
<i className='fas fa-clock-rotate-left' />
|
<i className='fas fa-clock-rotate-left' />
|
||||||
|
{this.state.showHistory && this.renderHistoryItems() }
|
||||||
</div>
|
</div>
|
||||||
<div className={`editorTool undo ${this.props.historySize.undo ? 'active' : ''}`}
|
<div className={`editorTool undo ${this.props.historySize.undo ? 'active' : ''}`}
|
||||||
onClick={this.props.undo} >
|
onClick={this.props.undo} >
|
||||||
|
|||||||
@@ -57,7 +57,12 @@
|
|||||||
.tooltipLeft('History');
|
.tooltipLeft('History');
|
||||||
font-size : 0.75em;
|
font-size : 0.75em;
|
||||||
color : grey;
|
color : grey;
|
||||||
&.active { color : inherit; }
|
&.active {
|
||||||
|
color : inherit;
|
||||||
|
&>.dropdown { visibility: visible; }
|
||||||
|
}
|
||||||
|
&:hover>.dropdown { visibility: visible; }
|
||||||
|
|
||||||
}
|
}
|
||||||
&.editorTheme {
|
&.editorTheme {
|
||||||
.tooltipLeft('Editor Themes');
|
.tooltipLeft('Editor Themes');
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
|
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
|
||||||
|
export const HISTORY_SLOTS = 5;
|
||||||
// const HISTORY_SAVE_DELAYS = {
|
// const HISTORY_SAVE_DELAYS = {
|
||||||
// 0: 0, // 0 minutes (if not specified)
|
// 0: 0, // 0 minutes (if not specified)
|
||||||
// 1: 2, // 2 minutes
|
// 1: 2, // 2 minutes
|
||||||
@@ -68,16 +69,29 @@ function updateStoredBrew(brew, slot=0){
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function updateHistory(brew) {
|
export function historyExists(brew){
|
||||||
|
return Object.keys(localStorage)
|
||||||
|
.some((key)=>{
|
||||||
|
return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadHistory(brew){
|
||||||
const history = {};
|
const history = {};
|
||||||
|
|
||||||
// Load data from local storage to History object
|
// Load data from local storage to History object
|
||||||
for (let i = 1; i<=5; i++){
|
for (let i = 1; i <= HISTORY_SLOTS; i++){
|
||||||
history[i] = getVersionBySlot(brew, i);
|
history[i] = getVersionBySlot(brew, i);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return history;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function updateHistory(brew) {
|
||||||
|
const history = loadHistory(brew);
|
||||||
|
|
||||||
// Walk each version position
|
// Walk each version position
|
||||||
for (let slot = 5; slot>0; slot--){
|
for (let slot = HISTORY_SLOTS; slot > 0; slot--){
|
||||||
const storedVersion = history[slot];
|
const storedVersion = history[slot];
|
||||||
|
|
||||||
// If slot has expired, update all lower slots and break
|
// If slot has expired, update all lower slots and break
|
||||||
@@ -96,6 +110,16 @@ export function updateHistory(brew) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function getHistoryItems(brew){
|
||||||
|
const historyArray = [];
|
||||||
|
|
||||||
|
for (let i = 1; i <= HISTORY_SLOTS; i++){
|
||||||
|
historyArray.push(getVersionBySlot(brew, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return historyArray;
|
||||||
|
};
|
||||||
|
|
||||||
export function versionHistoryGarbageCollection(){
|
export function versionHistoryGarbageCollection(){
|
||||||
Object.keys(localStorage)
|
Object.keys(localStorage)
|
||||||
.filter((key)=>{
|
.filter((key)=>{
|
||||||
|
|||||||
Reference in New Issue
Block a user