-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathAttributeNote.svelte
More file actions
375 lines (344 loc) · 11.8 KB
/
Copy pathAttributeNote.svelte
File metadata and controls
375 lines (344 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<script lang="ts">
/**
* AttributeNote.svelte
*
* Reusable inline note component for a single collection attribute/column.
* Renders as a subtle "Add note" trigger when empty, or shows the note text
* with an edit pencil icon when a note exists.
*
* Usage:
* <AttributeNote
* databaseId={$page.params.database}
* collectionId={$page.params.collection}
* attributeKey={attribute.key}
* />
*
* Issue: https://github.com/appwrite/appwrite/issues/11945
*/
import { attributeNotes } from '$lib/stores/attributeNotes';
interface Props {
databaseId: string;
collectionId: string;
attributeKey: string;
}
let { databaseId, collectionId, attributeKey }: Props = $props();
// Current persisted note value
let note = $state(attributeNotes.getNote(databaseId, collectionId, attributeKey));
// Whether the inline editor is open
let editing = $state(false);
// Draft copy while the user types
let draft = $state(note);
$effect(() => {
const nextNote = attributeNotes.getNote(databaseId, collectionId, attributeKey);
note = nextNote;
// Only overwrite draft if user is NOT editing
if (!editing) {
draft = nextNote;
}
});
// Textarea ref for auto-focus
let textareaRef = $state<HTMLTextAreaElement | null>(null);
$effect(() => {
if (editing && textareaRef) {
textareaRef.focus();
// Place cursor at end
const len = textareaRef.value.length;
textareaRef.setSelectionRange(len, len);
}
});
function openEditor() {
draft = note;
editing = true;
}
function saveNote() {
const trimmed = draft.trim();
attributeNotes.setNote(databaseId, collectionId, attributeKey, trimmed);
note = trimmed;
editing = false;
}
function cancelEdit() {
draft = note;
editing = false;
}
function handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
cancelEdit();
} else if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {
saveNote();
}
}
function clearNote() {
attributeNotes.setNote(databaseId, collectionId, attributeKey, '');
note = '';
editing = false;
}
</script>
<div class="attribute-note">
{#if editing}
<div class="attribute-note__editor">
<textarea
bind:this={textareaRef}
bind:value={draft}
class="attribute-note__textarea"
aria-label={`Note for ${attributeKey}`}
placeholder="Add a note about this column (optional)…"
rows="3"
maxlength="500"
onkeydown={handleKeydown}></textarea>
<div class="attribute-note__editor-actions">
<span class="attribute-note__hint">Ctrl+Enter / Cmd+Enter to save · Esc to cancel</span>
<div class="attribute-note__buttons">
{#if note}
<button
type="button"
class="attribute-note__btn attribute-note__btn--danger"
onclick={clearNote}
title="Remove note">
Remove
</button>
{/if}
<button
type="button"
class="attribute-note__btn attribute-note__btn--secondary"
onclick={cancelEdit}>
Cancel
</button>
<button
type="button"
class="attribute-note__btn attribute-note__btn--primary"
onclick={saveNote}>
Save
</button>
</div>
</div>
</div>
{:else if note}
<button
type="button"
class="attribute-note__display"
onclick={openEditor}
title="Click to edit note">
<span class="attribute-note__icon attribute-note__icon--note" aria-hidden="true">
<!-- Sticky note icon (inline SVG, no external deps) -->
<svg
width="12"
height="12"
viewBox="0 0 20 20"
fill="none"
xmlns="http://www.w3.org/2000/svg">
<path
d="M4 2h12a2 2 0 0 1 2 2v10l-4 4H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2z"
stroke="currentColor"
stroke-width="1.5"
stroke-linejoin="round" />
<path d="M14 14v4l4-4h-4z" fill="currentColor" opacity="0.4" />
<path
d="M6 7h8M6 10h6"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round" />
</svg>
</span>
<span class="attribute-note__text">{note}</span>
<span class="attribute-note__icon attribute-note__icon--edit" aria-hidden="true">
<!-- Pencil icon -->
<svg
width="11"
height="11"
viewBox="0 0 20 20"
fill="none"
xmlns="http://www.w3.org/2000/svg">
<path
d="M14.69 2.21a1.5 1.5 0 0 1 2.1 2.1L6 15.1 2 16l.9-4L14.69 2.21z"
stroke="currentColor"
stroke-width="1.5"
stroke-linejoin="round" />
</svg>
</span>
</button>
{:else}
<button
type="button"
class="attribute-note__add"
onclick={openEditor}
title="Add a note to this column">
<span class="attribute-note__icon" aria-hidden="true">
<svg
width="11"
height="11"
viewBox="0 0 20 20"
fill="none"
xmlns="http://www.w3.org/2000/svg">
<path
d="M10 4v12M4 10h12"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round" />
</svg>
</span>
Add note
</button>
{/if}
</div>
<style>
.attribute-note {
display: inline-flex;
flex-direction: column;
max-width: 100%;
}
/* ── "Add note" trigger ─────────────────────────────────────── */
.attribute-note__add {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 2px 6px;
border: 1px dashed var(--color-border, #e0e0e0);
border-radius: 4px;
background: transparent;
color: var(--color-text-tertiary, #999);
font-size: 11px;
line-height: 1.4;
cursor: pointer;
transition:
color 0.15s,
border-color 0.15s;
white-space: nowrap;
}
.attribute-note__add:hover {
color: var(--color-text-secondary, #666);
border-color: var(--color-border-strong, #bbb);
}
/* ── Existing note display ──────────────────────────────────── */
.attribute-note__display {
display: inline-flex;
align-items: flex-start;
gap: 5px;
padding: 4px 6px;
border: 1px solid transparent;
border-radius: 4px;
background: var(--color-surface-note, rgba(255, 200, 0, 0.08));
color: var(--color-text-secondary, #555);
font-size: 12px;
line-height: 1.4;
cursor: pointer;
text-align: left;
max-width: 320px;
transition:
background 0.15s,
border-color 0.15s;
word-break: break-word;
}
.attribute-note__display:hover {
background: var(--color-surface-note-hover, rgba(255, 200, 0, 0.15));
border-color: var(--color-border, #e0e0e0);
}
.attribute-note__text {
flex: 1;
white-space: pre-wrap;
/* Clamp to 3 lines with ellipsis */
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* ── Icons ───────────────────────────────────────────────────── */
.attribute-note__icon {
display: inline-flex;
align-items: center;
flex-shrink: 0;
margin-top: 1px;
color: var(--color-text-tertiary, #aaa);
}
.attribute-note__icon--edit {
opacity: 0;
transition: opacity 0.15s;
}
.attribute-note__display:hover .attribute-note__icon--edit {
opacity: 1;
}
/* ── Inline editor ───────────────────────────────────────────── */
.attribute-note__editor {
display: flex;
flex-direction: column;
gap: 6px;
width: 320px;
}
.attribute-note__textarea {
width: 100%;
padding: 8px 10px;
border: 1px solid var(--color-border-strong, #ccc);
border-radius: 6px;
background: var(--color-surface-input, #fff);
color: var(--color-text-primary, #111);
font-size: 12px;
line-height: 1.5;
font-family: inherit;
resize: vertical;
min-height: 72px;
box-sizing: border-box;
transition:
border-color 0.15s,
box-shadow 0.15s;
}
.attribute-note__textarea:focus {
outline: none;
border-color: var(--color-primary, #e05a4b);
box-shadow: 0 0 0 2px rgba(224, 90, 75, 0.15);
}
.attribute-note__editor-actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.attribute-note__hint {
font-size: 10px;
color: var(--color-text-tertiary, #aaa);
flex-shrink: 0;
}
.attribute-note__buttons {
display: flex;
align-items: center;
gap: 6px;
flex-shrink: 0;
}
.attribute-note__btn {
padding: 4px 10px;
border-radius: 4px;
font-size: 12px;
font-weight: 500;
cursor: pointer;
border: 1px solid transparent;
transition:
background 0.15s,
border-color 0.15s,
color 0.15s;
line-height: 1.4;
}
.attribute-note__btn--primary {
background: var(--color-primary, #e05a4b);
color: #fff;
border-color: var(--color-primary, #e05a4b);
}
.attribute-note__btn--primary:hover {
background: var(--color-primary-dark, #c94b3d);
border-color: var(--color-primary-dark, #c94b3d);
}
.attribute-note__btn--secondary {
background: transparent;
color: var(--color-text-secondary, #555);
border-color: var(--color-border, #ddd);
}
.attribute-note__btn--secondary:hover {
background: var(--color-surface-hover, #f5f5f5);
}
.attribute-note__btn--danger {
background: transparent;
color: var(--color-danger, #e05a4b);
border-color: transparent;
}
.attribute-note__btn--danger:hover {
background: var(--color-surface-danger, rgba(224, 90, 75, 0.08));
border-color: var(--color-danger, #e05a4b);
}
</style>