refactor: refactor logic of value history
This commit is contained in:
@@ -34,18 +34,17 @@ export const PendantHistoryPanel = ({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
const currentBlockValues = getRecastItemValue(block).getAllValue();
|
const currentBlockValues = getRecastItemValue(block).getAllValue();
|
||||||
const allProperties = getProperties();
|
const missValues = getProperties().filter(
|
||||||
const missProperties = allProperties.filter(
|
|
||||||
property => !currentBlockValues.find(v => v.id === property.id)
|
property => !currentBlockValues.find(v => v.id === property.id)
|
||||||
);
|
);
|
||||||
const pendantHistory = getValueHistory({
|
const valueHistory = getValueHistory({
|
||||||
recastBlockId: recastBlock.id,
|
recastBlockId: recastBlock.id,
|
||||||
});
|
});
|
||||||
const historyMap = missProperties.reduce<{
|
const historyMap = missValues.reduce<{
|
||||||
[key: RecastPropertyId]: string;
|
[key: RecastPropertyId]: string[];
|
||||||
}>((history, property) => {
|
}>((history, property) => {
|
||||||
if (pendantHistory[property.id]) {
|
if (valueHistory[property.id]) {
|
||||||
history[property.id] = pendantHistory[property.id];
|
history[property.id] = valueHistory[property.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
return history;
|
return history;
|
||||||
@@ -54,18 +53,30 @@ export const PendantHistoryPanel = ({
|
|||||||
const blockHistory = (
|
const blockHistory = (
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.entries(historyMap).map(
|
Object.entries(historyMap).map(
|
||||||
async ([propertyId, blockId]) => {
|
async ([propertyId, blockIds]) => {
|
||||||
const latestValueBlock = (
|
const blocks = await groupBlock.children();
|
||||||
await groupBlock.children()
|
const latestChangeBlock = blockIds
|
||||||
).find((block: AsyncBlock) => block.id === blockId);
|
.reverse()
|
||||||
|
.reduce<AsyncBlock>((block, id) => {
|
||||||
|
if (!block) {
|
||||||
|
return blocks.find(
|
||||||
|
block => block.id === id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return block;
|
||||||
|
}, null);
|
||||||
|
|
||||||
return getRecastItemValue(
|
if (latestChangeBlock) {
|
||||||
latestValueBlock
|
return getRecastItemValue(
|
||||||
).getValue(propertyId as RecastPropertyId);
|
latestChangeBlock
|
||||||
|
).getValue(propertyId as RecastPropertyId);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
).filter(v => v);
|
).filter(v => v);
|
||||||
|
|
||||||
setHistory(blockHistory);
|
setHistory(blockHistory);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ type Props = {
|
|||||||
|
|
||||||
type HistoryStorageMap = {
|
type HistoryStorageMap = {
|
||||||
[recastBlockId: string]: {
|
[recastBlockId: string]: {
|
||||||
[propertyId: RecastPropertyId]: string;
|
[propertyId: RecastPropertyId]: string[];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -21,18 +21,34 @@ const ensureLocalStorage = () => {
|
|||||||
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify({}));
|
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify({}));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const ensureHistoryAtom = (
|
||||||
|
data: HistoryStorageMap,
|
||||||
|
recastBlockId: string,
|
||||||
|
propertyId: RecastPropertyId
|
||||||
|
): HistoryStorageMap => {
|
||||||
|
if (!data[recastBlockId]) {
|
||||||
|
data[recastBlockId] = {};
|
||||||
|
}
|
||||||
|
if (!data[recastBlockId][propertyId]) {
|
||||||
|
data[recastBlockId][propertyId] = [];
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
export const setHistory = ({ recastBlockId, blockId, propertyId }: Props) => {
|
export const setHistory = ({ recastBlockId, blockId, propertyId }: Props) => {
|
||||||
ensureLocalStorage();
|
ensureLocalStorage();
|
||||||
const data: HistoryStorageMap = JSON.parse(
|
const data: HistoryStorageMap = JSON.parse(
|
||||||
localStorage.getItem(LOCAL_STORAGE_NAME) as string
|
localStorage.getItem(LOCAL_STORAGE_NAME) as string
|
||||||
);
|
);
|
||||||
|
ensureHistoryAtom(data, recastBlockId, propertyId);
|
||||||
|
const propertyHistory = data[recastBlockId][propertyId];
|
||||||
|
|
||||||
if (!data[recastBlockId]) {
|
if (propertyHistory.includes(blockId)) {
|
||||||
data[recastBlockId] = {};
|
const idIndex = propertyHistory.findIndex(id => id === blockId);
|
||||||
|
propertyHistory.splice(idIndex, 1);
|
||||||
}
|
}
|
||||||
const propertyValueRecord = data[recastBlockId];
|
|
||||||
propertyValueRecord[propertyId] = blockId;
|
propertyHistory.push(blockId);
|
||||||
|
|
||||||
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify(data));
|
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify(data));
|
||||||
};
|
};
|
||||||
@@ -48,20 +64,21 @@ export const getHistory = ({ recastBlockId }: { recastBlockId: string }) => {
|
|||||||
|
|
||||||
export const removeHistory = ({
|
export const removeHistory = ({
|
||||||
recastBlockId,
|
recastBlockId,
|
||||||
|
blockId,
|
||||||
propertyId,
|
propertyId,
|
||||||
}: {
|
}: Props) => {
|
||||||
recastBlockId: string;
|
|
||||||
propertyId: RecastPropertyId;
|
|
||||||
}) => {
|
|
||||||
ensureLocalStorage();
|
ensureLocalStorage();
|
||||||
const data: HistoryStorageMap = JSON.parse(
|
const data: HistoryStorageMap = JSON.parse(
|
||||||
localStorage.getItem(LOCAL_STORAGE_NAME) as string
|
localStorage.getItem(LOCAL_STORAGE_NAME) as string
|
||||||
);
|
);
|
||||||
if (!data[recastBlockId]) {
|
ensureHistoryAtom(data, recastBlockId, propertyId);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete data[recastBlockId][propertyId];
|
const propertyHistory = data[recastBlockId][propertyId];
|
||||||
|
|
||||||
|
if (propertyHistory.includes(blockId)) {
|
||||||
|
const idIndex = propertyHistory.findIndex(id => id === blockId);
|
||||||
|
propertyHistory.splice(idIndex, 1);
|
||||||
|
}
|
||||||
|
|
||||||
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify(data));
|
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify(data));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -260,6 +260,7 @@ export const getRecastItemValue = (block: RecastItem | AsyncBlock) => {
|
|||||||
removeHistory({
|
removeHistory({
|
||||||
recastBlockId: block.id,
|
recastBlockId: block.id,
|
||||||
propertyId: propertyId,
|
propertyId: propertyId,
|
||||||
|
blockId: block.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
return recastItem.setProperty(TABLE_VALUES_KEY, restProps);
|
return recastItem.setProperty(TABLE_VALUES_KEY, restProps);
|
||||||
|
|||||||
Reference in New Issue
Block a user