@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
171 lines (156 loc) • 6.46 kB
JavaScript
/**
* To use this feature:
* Apply these changes
*
*
## Copy this to main WebpartProperties
import { IWebpartHistory, IWebpartHistoryItem2, } from '@mikezimm/npmfunctions/dist/Services/PropPane/WebPartHistory/Interface';
//ADDED FOR WEBPART HISTORY:
webpartHistory: IWebpartHistory;
## Copy this to main WebpartProperties
//Add this to MAIN WEBPART.ts
import { IWebpartHistory, IWebpartHistoryItem2, } from '@mikezimm/npmfunctions/dist/Services/PropPane/WebPartHistory/Interface';
import { createWebpartHistory, updateWebpartHistory } from '@mikezimm/npmfunctions/dist/Services/PropPane/WebPartHistoryFunctions';
//ADDED FOR WEBPART HISTORY: -
// === TO main webpart class
private thisHistoryInstance: IWebpartHistoryItem2 = null;
//ADDED FOR WEBPART HISTORY: This sets the webpartHistory -
// === TO END OF onInit function
this.thisHistoryInstance = createWebpartHistory( 'onInit' , 'new', this.context.pageContext.user.displayName );
let priorHistory : IWebpartHistoryItem2[] = this.properties.webpartHistory ? this.properties.webpartHistory.history : [];
this.properties.webpartHistory = {
thisInstance: this.thisHistoryInstance,
history: priorHistory,
};
//ADDED FOR WEBPART HISTORY: This sets the webpartHistory
// === TO PropertyPaneChanged
this.properties.webpartHistory = updateWebpartHistory( this.properties.webpartHistory , propertyPath , newValue, this.context.pageContext.user.displayName );
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateWebpartHistory = exports.updateWebpartHistoryV2 = exports.upgradeV1History = exports.updateCurrentHistorySaved = exports.createWebpartHistory = void 0;
function createWebpartHistory(prop, newValue, user) {
var now = new Date();
var timeString = now.toUTCString();
// fields: prop === 'onInit' ? [] : [ prop ],
// newValues: prop === 'onInit' ? [] : [ newValue ],
var change = {
prop: prop,
value: newValue,
};
var history = {
time: timeString,
user: user,
changes: prop === 'onInit' ? [] : [change],
};
return history;
}
exports.createWebpartHistory = createWebpartHistory;
function updateCurrentHistorySaved(allHistory, thisInstance) {
var maxHistoryLength = 20;
var history = allHistory.history;
if (!history || history.length === 0) {
history = [thisInstance];
}
else {
if (history[0].time !== thisInstance.time || history[0].user !== thisInstance.user) {
history.unshift(thisInstance);
}
else {
history[0] = thisInstance;
}
}
//Trim history to only last 20 saves
if (history.length > maxHistoryLength) {
history.length = maxHistoryLength;
}
allHistory.history = history;
return allHistory;
}
exports.updateCurrentHistorySaved = updateCurrentHistorySaved;
function upgradeV1History(allHistory) {
//rebuild history if needed from IWebpartHistoryItem1
if (allHistory && allHistory.history.length > 0 && Object.keys(allHistory.history[0]).indexOf('newValues') > -1) {
var newHistory_1 = [];
allHistory.history.map(function (instance) {
var changes = [];
var oldProps = instance.fields;
var oldValues = instance.newValues;
oldProps.map(function (field, idx) {
changes.push({ prop: field, value: oldValues[idx] });
});
newHistory_1.push({
time: instance.time,
user: instance.user,
changes: changes,
});
});
allHistory.history = newHistory_1;
}
return allHistory;
}
exports.upgradeV1History = upgradeV1History;
/**
* Added 2022-07-25 from FPSPageInfo to simplify re-usability.
* It just has logic to get the trimThis based on the passed in array of strings
* @param webpartHistory
* @param prop
* @param newValue
* @param user
* @param noTrimProps
* @param startTrimProps
* @param trimLength
* @returns
*/
function updateWebpartHistoryV2(webpartHistory, prop, newValue, user, noTrimProps, startTrimProps, trimLength) {
if (trimLength === void 0) { trimLength = 20; }
//ADDED FOR WEBPART HISTORY: This sets the webpartHistory
var trimThis = 'end';
if (noTrimProps.indexOf(prop) > -1) {
trimThis = 'none';
}
else if (startTrimProps.indexOf(prop) > -1) {
trimThis = 'start';
}
webpartHistory = updateWebpartHistory(webpartHistory, prop, newValue, user, trimThis, trimLength);
return webpartHistory;
}
exports.updateWebpartHistoryV2 = updateWebpartHistoryV2;
function updateWebpartHistory(webpartHistory, prop, newValue, user, trimThis, trimLength) {
if (trimThis === void 0) { trimThis = 'start'; }
if (trimLength === void 0) { trimLength = 20; }
var thisInstance = webpartHistory.thisInstance;
if (!thisInstance) {
thisInstance = createWebpartHistory(prop, newValue, user);
}
var propIdx = -1;
thisInstance.changes.map(function (thisChange, idx) {
if (thisChange.prop === prop) {
propIdx = idx;
}
});
var strValue = typeof newValue === 'string' ? newValue + '' : newValue.toString();
var origLength = strValue.length + 0;
//Need this check ( -5 ) to make sure we don't trim it unneccessarily
if (origLength > trimLength + 5) {
if (trimThis === 'start') {
strValue = strValue.substring(0, trimLength);
strValue += " ...[+".concat(origLength - strValue.length, "]");
}
else if (trimThis === 'end') {
strValue = strValue.substring(origLength - trimLength);
strValue = "[+".concat(origLength - strValue.length, "]...").concat(strValue);
}
}
if (propIdx < 0) {
thisInstance.changes.push({ prop: prop, value: strValue });
}
else {
thisInstance.changes[propIdx].value = strValue;
}
webpartHistory = updateCurrentHistorySaved(webpartHistory, thisInstance);
console.log('webpartHistory: function', webpartHistory);
return webpartHistory;
}
exports.updateWebpartHistory = updateWebpartHistory;
//# sourceMappingURL=Functions.js.map
;