2021-04-12 06:18:30 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
2021-04-26 15:34:33 +02:00
|
|
|
import * as tr from "lib/i18n";
|
2021-06-12 17:48:53 +02:00
|
|
|
import { getContext } from "svelte";
|
2021-06-12 00:18:50 +02:00
|
|
|
import WithTooltip from "./WithTooltip.svelte";
|
|
|
|
import Badge from "./Badge.svelte";
|
2021-04-12 06:18:30 +02:00
|
|
|
import { revertIcon } from "./icons";
|
2021-06-12 17:48:53 +02:00
|
|
|
import { touchDeviceKey } from "components/contextKeys";
|
2021-05-24 10:26:01 +02:00
|
|
|
import { isEqual as isEqualLodash, cloneDeep } from "lodash-es";
|
2021-04-26 15:34:33 +02:00
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
type T = unknown;
|
2021-04-26 15:34:33 +02:00
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
export let value: T;
|
|
|
|
export let defaultValue: T;
|
2021-04-12 06:18:30 +02:00
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
function isEqual(a: T, b: T): boolean {
|
2021-05-24 10:26:01 +02:00
|
|
|
if (typeof a === "number" && typeof b === "number") {
|
|
|
|
// round to .01 precision before comparing,
|
|
|
|
// so the values coming out of the UI match
|
|
|
|
// the originals
|
2021-06-12 00:18:50 +02:00
|
|
|
a = Math.round(a * 100) / 100;
|
|
|
|
b = Math.round(b * 100) / 100;
|
2021-05-24 10:26:01 +02:00
|
|
|
}
|
2021-06-12 00:18:50 +02:00
|
|
|
|
|
|
|
return isEqualLodash(a, b);
|
2021-05-24 10:26:01 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 06:18:30 +02:00
|
|
|
let modified: boolean;
|
2021-04-25 11:05:19 +02:00
|
|
|
$: modified = !isEqual(value, defaultValue);
|
2021-04-12 06:18:30 +02:00
|
|
|
|
|
|
|
function revert(): void {
|
2021-04-25 11:05:19 +02:00
|
|
|
value = cloneDeep(defaultValue);
|
2021-04-12 06:18:30 +02:00
|
|
|
}
|
2021-06-12 17:48:53 +02:00
|
|
|
|
|
|
|
const touchDevice = getContext<boolean>(touchDeviceKey);
|
2021-04-12 06:18:30 +02:00
|
|
|
</script>
|
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
<span class:invisible={!modified}>
|
2021-06-12 17:48:53 +02:00
|
|
|
{#if touchDevice}
|
|
|
|
<Badge class="px-1" on:click={revert}>{@html revertIcon}</Badge>
|
|
|
|
{:else}
|
|
|
|
<WithTooltip
|
|
|
|
trigger="hover"
|
|
|
|
tooltip={tr.deckConfigRevertButtonTooltip()}
|
|
|
|
let:createTooltip
|
2021-06-12 00:18:50 +02:00
|
|
|
>
|
2021-06-12 17:48:53 +02:00
|
|
|
<Badge
|
|
|
|
class="px-1"
|
|
|
|
on:mount={(event) => createTooltip(event.detail.span)}
|
|
|
|
on:click={revert}>{@html revertIcon}</Badge
|
|
|
|
>
|
|
|
|
</WithTooltip>
|
|
|
|
{/if}
|
2021-05-28 00:52:49 +02:00
|
|
|
</span>
|