Skip to content

Commit 6f3f395

Browse files
committed
[palette] expand feature
1 parent 0bf75a5 commit 6f3f395

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

packages/gephi-lite/src/components/GraphAppearance/color/ColorPartitionEditor.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MISSING_PALETTE_COLOR, PartitionColor } from "@gephi/gephi-lite-sdk";
22
import cx from "classnames";
3-
import { map } from "lodash";
3+
import { values as getValues, map } from "lodash";
44
import { FC, useEffect, useMemo, useRef, useState } from "react";
55
import AnimateHeight from "react-animate-height";
66
import { useTranslation } from "react-i18next";
@@ -9,6 +9,7 @@ import { useDynamicItemData, useGraphDataset } from "../../../core/context/dataC
99
import { uniqFieldValuesAsStrings } from "../../../core/graph/utils";
1010
import { ItemType } from "../../../core/types";
1111
import ColorPicker from "../../ColorPicker";
12+
import { getPalette } from "./utils";
1213

1314
const COLLAPSED_HEIGHT = 200;
1415

@@ -97,6 +98,18 @@ export const ColorPartitionEditor: FC<{
9798
{t("appearance.color.default_value", { items: t(`graph.model.${itemType}`) })}
9899
</label>
99100
</div>
101+
{getValues(color.colorPalette).some((v) => v === null) && (
102+
<button
103+
className="gl-btn gl-btn-outline"
104+
onClick={() => {
105+
const newPalette = getPalette(values, color.colorPalette);
106+
setColor({ ...color, colorPalette: newPalette });
107+
}}
108+
>
109+
{" "}
110+
expand colors
111+
</button>
112+
)}
100113
</div>
101114

102115
{!expanded && shouldShowButton && <div className="filler-fade-out position-absolute bottom-0" />}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import iwanthue from "iwanthue";
2-
import { every } from "lodash";
2+
import { every, values as getValues } from "lodash";
33

44
export function isColor(strColor: string): boolean {
55
const s = new Option().style;
66
s.color = strColor;
77
return s.color !== "";
88
}
99

10-
export function getPalette(values: string[]): Record<string, string> {
10+
export function getPalette(values: string[], originalPalette?: Record<string, string | null>): Record<string, string> {
1111
if (every(values, (v) => isColor(v))) {
1212
return values.reduce((iter, v) => ({ ...iter, [v]: v }), {});
1313
} else {
14-
const palette = iwanthue(values.length);
15-
return values.reduce((iter, v, i) => ({ ...iter, [v]: palette[i] }), {});
14+
const currentColors = getValues(originalPalette).filter((c) => c !== null);
15+
const palette = iwanthue(values.length, { originalColorsToExpand: currentColors, colorSpace: "all" });
16+
const newColors = palette.filter((c) => !currentColors.includes(c));
17+
console.log(currentColors, palette, newColors);
18+
return values.reduce(
19+
(iter, v, i) => ({ ...iter, [v]: originalPalette && originalPalette[v] ? originalPalette[v] : newColors[i] }),
20+
{},
21+
);
1622
}
1723
}

0 commit comments

Comments
 (0)