Debian Codegraph @

frontend/src/entry-forms/Posting.svelte

raw
1 <script lang="ts">
2 import AutocompleteInput from "../AutocompleteInput.svelte";
3 import type { EntryMetadata, Posting } from "../entries";
4 import { _ } from "../i18n";
5 import { currencies } from "../stores";
6 import AccountInput from "./AccountInput.svelte";
7 import AddMetadataButton from "./AddMetadataButton.svelte";
8 import EntryMetadataSvelte from "./EntryMetadata.svelte";
9
10 interface Props {
11 /** The posting to show and edit. */
12 posting: Posting;
13 /** Index in the list of postings, used to move it. */
14 index: number;
15 /** Account suggestions. */
16 suggestions?: string[];
17 /** Entry date to limit account suggestions. */
18 date?: string;
19 /** Handler to move a posting to another position on drag. */
20 move: (arg: { from: number; to: number }) => void;
21 /** Handler to remove this posting. */
22 remove: () => void;
23 }
24
25 let {
26 posting = $bindable(),
27 index,
28 suggestions,
29 date,
30 move,
31 remove,
32 }: Props = $props();
33
34 let amount_number = $derived(posting.amount.replace(/[^\-?0-9.]/g, ""));
35 let amountSuggestions = $derived(
36 $currencies.map((c) => `${amount_number} ${c}`),
37 );
38
39 let drag = $state.raw(false);
40 let draggable = $state.raw(true);
41
42 function mousemove(event: MouseEvent) {
43 draggable = !(event.target instanceof HTMLInputElement);
44 }
45 function dragstart(event: DragEvent) {
46 event.dataTransfer?.setData("fava/posting", index.toString());
47 }
48 function dragenter(event: DragEvent) {
49 const types = event.dataTransfer?.types ?? [];
50 if (types.includes("fava/posting")) {
51 event.preventDefault();
52 drag = true;
53 }
54 }
55 function dragleave() {
56 drag = false;
57 }
58 function drop(event: DragEvent) {
59 event.preventDefault();
60 const from = event.dataTransfer?.getData("fava/posting");
61 if (from != null) {
62 move({ from: +from, to: index });
63 drag = false;
64 }
65 }
66 </script>
67
68 <div
69 class="flex-row"
70 class:drag
71 {draggable}
72 onmousemove={mousemove}
73 ondragstart={dragstart}
74 ondragenter={dragenter}
75 ondragover={dragenter}
76 ondragleave={dragleave}
77 ondrop={drop}
78 role="group"
79 >
80 <button
81 type="button"
82 class="muted round remove-row"
83 onclick={remove}
84 tabindex={-1}
85 >
86 ×
87 </button>
88 <AccountInput
89 className="grow"
90 bind:value={
91 () => posting.account,
92 (account: string) => {
93 posting = posting.set("account", account);
94 }
95 }
96 {suggestions}
97 {date}
98 />
99 <AutocompleteInput
100 className="amount"
101 placeholder={_("Amount")}
102 suggestions={amountSuggestions}
103 bind:value={
104 () => posting.amount,
105 (amount: string) => {
106 posting = posting.set("amount", amount);
107 }
108 }
109 />
110 <AddMetadataButton
111 bind:meta={
112 () => posting.meta,
113 (meta: EntryMetadata) => {
114 posting = posting.set("meta", meta);
115 }
116 }
117 />
118 <EntryMetadataSvelte
119 bind:meta={
120 () => posting.meta,
121 (meta: EntryMetadata) => {
122 posting = posting.set("meta", meta);
123 }
124 }
125 />
126 </div>
127
128 <style>
129 .drag {
130 box-shadow: var(--box-shadow-button);
131 }
132
133 div {
134 padding-left: 50px;
135 cursor: grab;
136 }
137
138 div > * {
139 cursor: initial;
140 }
141
142 div:last-child .remove-row {
143 visibility: hidden;
144 }
145
146 div :global(.amount) {
147 width: 220px;
148 }
149
150 @media (width <= 767px) {
151 div {
152 padding-left: 0;
153 }
154 }
155 </style>