feat(canvas): add persistent node favorites with toolbar star and glow
This commit is contained in:
36
lib/canvas-node-favorite.ts
Normal file
36
lib/canvas-node-favorite.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function toRecord(value: unknown): Record<string, unknown> {
|
||||
return isRecord(value) ? value : {};
|
||||
}
|
||||
|
||||
export function readNodeFavorite(data: unknown): boolean {
|
||||
const source = toRecord(data);
|
||||
return source.isFavorite === true;
|
||||
}
|
||||
|
||||
export function setNodeFavorite(
|
||||
nextValue: boolean,
|
||||
currentData: unknown,
|
||||
): Record<string, unknown> {
|
||||
const source = toRecord(currentData);
|
||||
|
||||
if (nextValue) {
|
||||
return {
|
||||
...source,
|
||||
isFavorite: true,
|
||||
};
|
||||
}
|
||||
|
||||
const { isFavorite: _isFavorite, ...rest } = source;
|
||||
return rest;
|
||||
}
|
||||
|
||||
export function preserveNodeFavorite(
|
||||
nextData: unknown,
|
||||
previousData: unknown,
|
||||
): Record<string, unknown> {
|
||||
return setNodeFavorite(readNodeFavorite(previousData), nextData);
|
||||
}
|
||||
Reference in New Issue
Block a user