Update RevertButton to use Badge and WithTooltip

This commit is contained in:
Henrik Giesel 2021-06-12 00:18:50 +02:00
parent a035679171
commit 3232712748
2 changed files with 29 additions and 44 deletions

View file

@ -2,7 +2,19 @@
Copyright: Ankitects Pty Ltd and contributors Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<span class="badge"> <script lang="ts">
import { onMount, createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
let spanRef: HTMLSpanElement;
onMount(() => {
dispatch("mount", { span: spanRef });
});
</script>
<span bind:this={spanRef} class="badge" on:click>
<slot /> <slot />
</span> </span>

View file

@ -4,67 +4,40 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<script lang="ts"> <script lang="ts">
import * as tr from "lib/i18n"; import * as tr from "lib/i18n";
import WithTooltip from "./WithTooltip.svelte";
import Badge from "./Badge.svelte";
import { revertIcon } from "./icons"; import { revertIcon } from "./icons";
import { createEventDispatcher } from "svelte";
import { isEqual as isEqualLodash, cloneDeep } from "lodash-es"; import { isEqual as isEqualLodash, cloneDeep } from "lodash-es";
// import { onMount } from "svelte";
// import Tooltip from "bootstrap/js/dist/tooltip";
let ref: HTMLDivElement; type T = unknown;
// fixme: figure out why this breaks halfway down the page export let value: T;
// onMount(() => { export let defaultValue: T;
// new Tooltip(ref, {
// placement: "bottom",
// html: true,
// offset: [0, 20],
// });
// });
export let value: any; function isEqual(a: T, b: T): boolean {
export let defaultValue: any;
const dispatch = createEventDispatcher();
function isEqual(a: unknown, b: unknown): boolean {
if (typeof a === "number" && typeof b === "number") { if (typeof a === "number" && typeof b === "number") {
// round to .01 precision before comparing, // round to .01 precision before comparing,
// so the values coming out of the UI match // so the values coming out of the UI match
// the originals // the originals
return isEqualLodash(Math.round(a * 100) / 100, Math.round(b * 100) / 100); a = Math.round(a * 100) / 100;
} else { b = Math.round(b * 100) / 100;
return isEqualLodash(a, b);
} }
return isEqualLodash(a, b);
} }
let modified: boolean; let modified: boolean;
$: modified = !isEqual(value, defaultValue); $: modified = !isEqual(value, defaultValue);
/// This component can be used either with bind:value, or by listening
/// to the revert event.
function revert(): void { function revert(): void {
value = cloneDeep(defaultValue); value = cloneDeep(defaultValue);
dispatch("revert", { value });
} }
</script> </script>
<span <span class:invisible={!modified}>
bind:this={ref} <WithTooltip tooltip={tr.deckConfigRevertButtonTooltip()} let:createTooltip>
class="badge px-1" <Badge on:mount={(event) => createTooltip(event.detail.span)} on:click={revert}
class:invisible={!modified} >{@html revertIcon}</Badge
title={tr.deckConfigRevertButtonTooltip()}
on:click={revert}
> >
{@html revertIcon} </WithTooltip>
</span> </span>
<style lang="scss">
.badge {
color: inherit;
}
span :global(svg) {
vertical-align: -0.125rem;
opacity: 0.3;
}
</style>