-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix canvas capture, and remove color now makes enabled if shift is pr…
…essed
- Loading branch information
Showing
4 changed files
with
120 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 29 additions & 18 deletions
47
src/components/Windows/Palette/ColorDelete/ColorDelete.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,39 @@ | ||
import { useContext } from "preact/hooks"; | ||
import { useContext, useEffect, useRef, useState } from "preact/hooks"; | ||
import { PaletteContext } from "../../../../managers/palette"; | ||
import styles from "./ColorDelete.module.css"; | ||
import { Icon } from "../../../General/Icon/Icon"; | ||
|
||
export function ColorDelete() { | ||
const palette = useContext(PaletteContext) | ||
const palette = useContext(PaletteContext); | ||
const [shift, setShift] = useState<boolean>(false); | ||
|
||
function onClick(event: MouseEvent) { | ||
if (event.shiftKey) { | ||
palette.reset() | ||
function onClick(event: MouseEvent) { | ||
if (event.shiftKey) { | ||
palette.reset(); | ||
|
||
return; | ||
} | ||
return; | ||
} | ||
|
||
palette.removeColor(palette.palette.value.selected) | ||
} | ||
if (!palette.isDefaultColor(palette.palette.value.selected)) palette.removeColor(palette.palette.value.selected); | ||
} | ||
|
||
return ( | ||
<button | ||
className={styles.button} | ||
onClick={onClick} | ||
disabled={palette.isDefaultColor(palette.palette.value.selected)}> | ||
function onKeyEvent(event: KeyboardEvent) { | ||
setShift(event.shiftKey); | ||
} | ||
|
||
<Icon icon="plus" className={styles.icon} alt={"Удалить выбранный цвет"}/> | ||
</button> | ||
) | ||
} | ||
useEffect(() => { | ||
document.addEventListener("keydown", onKeyEvent); | ||
document.addEventListener("keyup", onKeyEvent); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", onKeyEvent); | ||
document.removeEventListener("keyup", onKeyEvent); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<button className={styles.button} onClick={onClick} disabled={!(shift || !palette.isDefaultColor(palette.palette.value.selected))}> | ||
<Icon icon="plus" className={styles.icon} alt={"Удалить выбранный цвет"} /> | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,81 @@ | ||
import { createContext } from "preact"; | ||
import { AppColor } from "../classes/AppColor"; | ||
import { signal } from "@preact/signals"; | ||
import { computed, signal } from "@preact/signals"; | ||
import { config } from "../config"; | ||
import { AppLocalStorage } from "../classes/AppLocalStorage"; | ||
|
||
export const PaletteManager = { | ||
palette: signal(config.defaults.colors.palette), | ||
reset() { | ||
PaletteManager.palette.value = config.defaults.colors.palette; | ||
|
||
PaletteManager.save() | ||
}, | ||
setCurrentColor: (color: AppColor) => { | ||
PaletteManager.palette.value = { | ||
...PaletteManager.palette.value, | ||
selected: color | ||
} | ||
|
||
PaletteManager.save() | ||
}, | ||
removeColor(color: AppColor) { | ||
PaletteManager.palette.value = { | ||
selected: PaletteManager.palette.value.colors.at(-2) ?? PaletteManager.palette.value.selected, | ||
colors: PaletteManager.palette.value.colors.filter(c => !c.equals(color)) | ||
} | ||
|
||
PaletteManager.save() | ||
}, | ||
isDefaultColor(color: AppColor) { | ||
return config.defaults.colors.palette.colors.some(c => c.equals(color)) | ||
}, | ||
addColor(color: AppColor) { | ||
PaletteManager.palette.value = { | ||
...PaletteManager.palette.value, | ||
colors: [...PaletteManager.palette.value.colors, color] | ||
} | ||
|
||
PaletteManager.save() | ||
}, | ||
addAndSelect(color: AppColor) { | ||
const isColorInPalette = PaletteManager.palette.value.colors.some(c => c.equals(color)) | ||
|
||
if (!isColorInPalette) { | ||
this.addColor(color) | ||
} | ||
|
||
this.setCurrentColor(color) | ||
|
||
PaletteManager.save() | ||
}, | ||
save() { | ||
AppLocalStorage.set("palette", { | ||
colors: PaletteManager.palette.value.colors, | ||
selected: PaletteManager.palette.value.selected | ||
}, ({ colors, selected }) => JSON.stringify({ colors: colors.map(color => color.toHex()), selected: selected.toHex() })) | ||
}, | ||
load() { | ||
const palette = AppLocalStorage.get("palette", (flatPalette) => { | ||
|
||
return { | ||
colors: flatPalette.colors.map(color => new AppColor(color)), | ||
selected: new AppColor(flatPalette.selected) | ||
} | ||
}) | ||
|
||
if (!palette) return | ||
|
||
PaletteManager.palette.value = palette; | ||
}, | ||
|
||
} | ||
palette: signal(config.defaults.colors.palette), | ||
reset() { | ||
PaletteManager.palette.value = config.defaults.colors.palette; | ||
|
||
PaletteManager.save(); | ||
}, | ||
setCurrentColor: (color: AppColor) => { | ||
PaletteManager.palette.value = { | ||
...PaletteManager.palette.value, | ||
selected: color, | ||
}; | ||
|
||
PaletteManager.save(); | ||
}, | ||
removeColor(color: AppColor) { | ||
PaletteManager.palette.value = { | ||
selected: PaletteManager.palette.value.colors.at(-2) ?? PaletteManager.palette.value.selected, | ||
colors: PaletteManager.palette.value.colors.filter((c) => !c.equals(color)), | ||
}; | ||
|
||
PaletteManager.save(); | ||
}, | ||
isDefaultColor(color: AppColor) { | ||
return config.defaults.colors.palette.colors.some((c) => c.equals(color)); | ||
}, | ||
addColor(color: AppColor) { | ||
PaletteManager.palette.value = { | ||
...PaletteManager.palette.value, | ||
colors: [...PaletteManager.palette.value.colors, color], | ||
}; | ||
|
||
PaletteManager.save(); | ||
}, | ||
addAndSelect(color: AppColor) { | ||
const isColorInPalette = PaletteManager.palette.value.colors.some((c) => c.equals(color)); | ||
|
||
if (!isColorInPalette) { | ||
this.addColor(color); | ||
} | ||
|
||
this.setCurrentColor(color); | ||
|
||
PaletteManager.save(); | ||
}, | ||
save() { | ||
AppLocalStorage.set( | ||
"palette", | ||
{ | ||
colors: PaletteManager.palette.value.colors, | ||
selected: PaletteManager.palette.value.selected, | ||
}, | ||
({ colors, selected }) => JSON.stringify({ colors: colors.map((color) => color.toHex()), selected: selected.toHex() }) | ||
); | ||
}, | ||
load() { | ||
const palette = AppLocalStorage.get("palette", (flatPalette) => { | ||
return { | ||
colors: flatPalette.colors.map((color) => new AppColor(color)), | ||
selected: new AppColor(flatPalette.selected), | ||
}; | ||
}); | ||
|
||
if (!palette) return; | ||
|
||
PaletteManager.palette.value = palette; | ||
}, | ||
}; | ||
|
||
export const PaletteContext = createContext({} as typeof PaletteManager); | ||
|
||
|
||
export interface FlatPalette { | ||
colors: string[] | ||
selected: string | ||
colors: string[]; | ||
selected: string; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters