-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepsync_note_editor.py
More file actions
1350 lines (1181 loc) · 49.6 KB
/
Copy pathkeepsync_note_editor.py
File metadata and controls
1350 lines (1181 loc) · 49.6 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Note editor panel widget."""
from datetime import datetime, timezone
from pathlib import Path
from tkinter import filedialog, messagebox
from typing import Any, Callable, List, Optional
import threading
import uuid
import webbrowser
import customtkinter as ctk
from PIL import Image, ImageGrab
from keepsync_attachment_editing import (
IMAGE_FILETYPES,
copy_image_attachments,
copy_image_attachment,
parse_drop_file_paths,
save_clipboard_image_attachment,
)
from keepsync_audio_recording import (
AudioRecorder,
AudioRecordingError,
AudioTranscriptionError,
append_audio_transcript,
transcribe_audio_file,
)
from keepsync_dragdrop import drop_copy_action, enable_file_drop
from keepsync_models import (
Attachment,
ChecklistItem,
KEEP_COLOR_PALETTE,
Note,
NoteType,
SyncStatus,
clamp_checklist_indent,
normalize_keep_color,
)
from keepsync_markdown_editing import format_markdown_selection
from keepsync_note_text import (
format_reminder_datetime,
markdown_preview_blocks,
note_uses_markdown,
parse_reminder_datetime,
)
from keepsync_storage import DatabaseManager
from keepsync_theme import COLORS
from keepsync_ui_components import IconManager, SyncStatusBadge
class NoteEditor(ctk.CTkFrame):
"""Full note editor panel"""
def __init__(self, parent, db: DatabaseManager, sync_engine: Any,
on_save: Callable, on_close: Callable, on_popout: Optional[Callable] = None, **kwargs):
super().__init__(parent, fg_color=COLORS["bg_dark"], **kwargs)
self.db = db
self.sync_engine = sync_engine
self.on_save_callback = on_save
self.on_close_callback = on_close
self.on_popout_callback = on_popout
self.current_note: Optional[Note] = None
self.is_modified = False
self.selected_color = ""
self._image_drop_enabled = False
self._image_drop_target_ids = set()
self.audio_recorder: Optional[AudioRecorder] = None
self._build_ui()
def _build_ui(self):
# Header
header = ctk.CTkFrame(self, fg_color="transparent", height=50)
header.pack(fill="x", padx=16, pady=(12, 0))
header.pack_propagate(False)
# Close button
self.close_btn = ctk.CTkButton(
header,
text="",
image=IconManager.get_icon("close", 20, COLORS["text_secondary"]),
width=36,
height=36,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
command=self._handle_close
)
self.close_btn.pack(side="left")
# Title
self.header_title = ctk.CTkLabel(
header,
text="New Note",
font=ctk.CTkFont(size=16, weight="bold"),
text_color=COLORS["text_primary"]
)
self.header_title.pack(side="left", padx=12)
# Sync status
self.sync_badge = SyncStatusBadge(header, SyncStatus.LOCAL_ONLY)
self.sync_badge.pack(side="left", padx=8)
# Actions
actions_frame = ctk.CTkFrame(header, fg_color="transparent")
actions_frame.pack(side="right")
self.pin_btn = ctk.CTkButton(
actions_frame,
text="",
image=IconManager.get_icon("pin", 18, COLORS["text_secondary"]),
width=36,
height=36,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
command=self._toggle_pin
)
self.pin_btn.pack(side="left", padx=2)
if self.on_popout_callback:
popout_btn = ctk.CTkButton(
actions_frame,
text="",
image=IconManager.get_icon("note", 18, COLORS["text_secondary"]),
width=36,
height=36,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
command=self._handle_popout,
)
popout_btn.pack(side="left", padx=2)
self.save_btn = ctk.CTkButton(
actions_frame,
text="Save",
font=ctk.CTkFont(size=13, weight="bold"),
width=80,
height=36,
fg_color=COLORS["accent_green"],
hover_color=COLORS["accent_green_hover"],
text_color=COLORS["bg_darkest"],
command=self._save_note
)
self.save_btn.pack(side="left", padx=(8, 0))
# Divider
divider = ctk.CTkFrame(self, fg_color=COLORS["divider"], height=1)
divider.pack(fill="x", pady=12)
# Editor content
editor_frame = ctk.CTkFrame(self, fg_color="transparent")
editor_frame.pack(fill="both", expand=True, padx=16, pady=(0, 16))
# Title input
self.title_entry = ctk.CTkEntry(
editor_frame,
placeholder_text="Title",
font=ctk.CTkFont(size=20, weight="bold"),
height=45,
fg_color="transparent",
border_width=0,
text_color=COLORS["text_primary"],
placeholder_text_color=COLORS["text_muted"]
)
self.title_entry.pack(fill="x", pady=(0, 8))
self.title_entry.bind("<KeyRelease>", self._on_modify)
# Note type selector
type_frame = ctk.CTkFrame(editor_frame, fg_color="transparent")
type_frame.pack(fill="x", pady=(0, 12))
self.note_type_var = ctk.StringVar(value="note")
self.note_radio = ctk.CTkRadioButton(
type_frame,
text="Text Note",
variable=self.note_type_var,
value="note",
font=ctk.CTkFont(size=12),
text_color=COLORS["text_secondary"],
fg_color=COLORS["accent_blue"],
hover_color=COLORS["accent_blue_hover"],
command=self._on_type_change
)
self.note_radio.pack(side="left", padx=(0, 16))
self.checklist_radio = ctk.CTkRadioButton(
type_frame,
text="Checklist",
variable=self.note_type_var,
value="checklist",
font=ctk.CTkFont(size=12),
text_color=COLORS["text_secondary"],
fg_color=COLORS["accent_blue"],
hover_color=COLORS["accent_blue_hover"],
command=self._on_type_change
)
self.checklist_radio.pack(side="left")
# Color selector
color_section = ctk.CTkFrame(editor_frame, fg_color="transparent")
color_section.pack(fill="x", pady=(0, 12))
ctk.CTkLabel(
color_section,
text="Color",
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["text_secondary"]
).pack(side="left", padx=(0, 10))
self.color_buttons = {}
for color_key, (color_name, color_hex) in KEEP_COLOR_PALETTE.items():
button = ctk.CTkButton(
color_section,
text="",
width=24,
height=24,
fg_color=color_hex,
hover_color=color_hex,
border_width=2,
border_color=COLORS["border"],
corner_radius=12,
command=lambda c=color_key: self._select_color(c)
)
button.pack(side="left", padx=(0, 5))
self.color_buttons[color_key] = button
# Content area (switchable between text and checklist)
self.content_container = ctk.CTkFrame(editor_frame, fg_color="transparent")
self.content_container.pack(fill="both", expand=True)
# Text editor
self.text_frame = ctk.CTkFrame(self.content_container, fg_color="transparent")
self.markdown_mode_var = ctk.StringVar(value="Edit")
self.markdown_controls = ctk.CTkFrame(self.text_frame, fg_color="transparent")
ctk.CTkLabel(
self.markdown_controls,
text="Markdown",
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["accent_cyan"]
).pack(side="left", padx=(0, 10))
self.markdown_toolbar = ctk.CTkFrame(self.markdown_controls, fg_color="transparent")
self.markdown_toolbar.pack(side="left")
for label, style in (("B", "bold"), ("I", "italic"), ("Code", "code"), ("Link", "link"), ("Task", "task")):
button = ctk.CTkButton(
self.markdown_toolbar,
text=label,
width=48 if len(label) > 1 else 30,
height=28,
font=ctk.CTkFont(size=11, weight="bold" if style == "bold" else "normal"),
fg_color=COLORS["bg_medium"],
hover_color=COLORS["bg_hover"],
text_color=COLORS["text_primary"],
command=lambda item=style: self._apply_markdown_format(item)
)
button.pack(side="left", padx=(0, 4))
self.markdown_toggle = ctk.CTkSegmentedButton(
self.markdown_controls,
values=["Edit", "Preview"],
variable=self.markdown_mode_var,
command=self._set_markdown_mode,
selected_color=COLORS["accent_blue"],
selected_hover_color=COLORS["accent_blue_hover"],
unselected_color=COLORS["bg_medium"],
unselected_hover_color=COLORS["bg_hover"],
text_color=COLORS["text_primary"],
height=30
)
self.markdown_toggle.pack(side="right")
self.content_text = ctk.CTkTextbox(
self.text_frame,
font=ctk.CTkFont(size=14),
fg_color=COLORS["bg_medium"],
border_width=1,
border_color=COLORS["border"],
text_color=COLORS["text_primary"],
corner_radius=8
)
self.content_text.pack(fill="both", expand=True)
self.content_text.bind("<KeyRelease>", self._on_modify)
self.markdown_preview = ctk.CTkTextbox(
self.text_frame,
font=ctk.CTkFont(size=14),
fg_color=COLORS["bg_medium"],
border_width=1,
border_color=COLORS["border"],
text_color=COLORS["text_primary"],
corner_radius=8,
wrap="word"
)
self._configure_markdown_preview_tags()
# Checklist editor
self.checklist_frame = ctk.CTkFrame(self.content_container, fg_color="transparent")
self.checklist_scroll = ctk.CTkScrollableFrame(
self.checklist_frame,
fg_color=COLORS["bg_medium"],
corner_radius=8
)
self.checklist_scroll.pack(fill="both", expand=True)
self.add_item_btn = ctk.CTkButton(
self.checklist_frame,
text="+ Add Item",
font=ctk.CTkFont(size=13),
height=36,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_blue"],
anchor="w",
command=self._add_checklist_item
)
self.add_item_btn.pack(fill="x", pady=(8, 0))
# Labels section
labels_section = ctk.CTkFrame(editor_frame, fg_color="transparent")
labels_section.pack(fill="x", pady=(12, 0))
labels_header = ctk.CTkLabel(
labels_section,
text="Labels",
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["text_secondary"]
)
labels_header.pack(anchor="w")
self.labels_frame = ctk.CTkFrame(labels_section, fg_color="transparent")
self.labels_frame.pack(fill="x", pady=(4, 0))
self.label_entry = ctk.CTkEntry(
self.labels_frame,
placeholder_text="Add label...",
font=ctk.CTkFont(size=12),
width=150,
height=30,
fg_color=COLORS["bg_medium"],
border_width=1,
border_color=COLORS["border"]
)
self.label_entry.pack(side="left")
self.label_entry.bind("<Return>", self._add_label)
self.labels_display = ctk.CTkFrame(self.labels_frame, fg_color="transparent")
self.labels_display.pack(side="left", fill="x", expand=True, padx=(8, 0))
# Reminder section
reminder_section = ctk.CTkFrame(editor_frame, fg_color="transparent")
reminder_section.pack(fill="x", pady=(12, 0))
ctk.CTkLabel(
reminder_section,
text="Reminder",
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["text_secondary"]
).pack(anchor="w")
reminder_inputs = ctk.CTkFrame(reminder_section, fg_color="transparent")
reminder_inputs.pack(fill="x", pady=(4, 0))
self.reminder_entry = ctk.CTkEntry(
reminder_inputs,
placeholder_text="YYYY-MM-DD HH:MM",
font=ctk.CTkFont(size=12),
width=150,
height=30,
fg_color=COLORS["bg_medium"],
border_width=1,
border_color=COLORS["border"]
)
self.reminder_entry.pack(side="left")
self.reminder_entry.bind("<KeyRelease>", self._on_modify)
self.reminder_location_entry = ctk.CTkEntry(
reminder_inputs,
placeholder_text="Optional location",
font=ctk.CTkFont(size=12),
height=30,
fg_color=COLORS["bg_medium"],
border_width=1,
border_color=COLORS["border"]
)
self.reminder_location_entry.pack(side="left", fill="x", expand=True, padx=(8, 0))
self.reminder_location_entry.bind("<KeyRelease>", self._on_modify)
self.clear_reminder_btn = ctk.CTkButton(
reminder_inputs,
text="Clear",
font=ctk.CTkFont(size=12),
width=58,
height=30,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["text_secondary"],
command=self._clear_reminder
)
self.clear_reminder_btn.pack(side="left", padx=(8, 0))
shared_section = ctk.CTkFrame(editor_frame, fg_color="transparent")
shared_section.pack(fill="x", pady=(12, 0))
ctk.CTkLabel(
shared_section,
text="Shared With",
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["text_secondary"]
).pack(anchor="w")
self.shared_display = ctk.CTkLabel(
shared_section,
text="Not shared",
font=ctk.CTkFont(size=12),
text_color=COLORS["text_muted"],
anchor="w",
justify="left",
wraplength=520
)
self.shared_display.pack(fill="x", pady=(4, 0))
attachments_section = ctk.CTkFrame(editor_frame, fg_color="transparent")
attachments_section.pack(fill="x", pady=(12, 0))
attachments_header = ctk.CTkFrame(attachments_section, fg_color="transparent")
attachments_header.pack(fill="x")
ctk.CTkLabel(
attachments_header,
text="Attachments",
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["text_secondary"]
).pack(side="left")
self.add_image_btn = ctk.CTkButton(
attachments_header,
text="Add Image",
font=ctk.CTkFont(size=12),
width=86,
height=28,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_blue"],
command=self._add_image_attachment
)
self.add_image_btn.pack(side="right", padx=(8, 0))
self.paste_image_btn = ctk.CTkButton(
attachments_header,
text="Paste Image",
font=ctk.CTkFont(size=12),
width=92,
height=28,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_blue"],
command=self._paste_image_attachment
)
self.paste_image_btn.pack(side="right")
self.record_audio_btn = ctk.CTkButton(
attachments_header,
text="Record Audio",
font=ctk.CTkFont(size=12),
width=104,
height=28,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_cyan"],
command=self._toggle_audio_recording
)
self.record_audio_btn.pack(side="right", padx=(0, 8))
self.audio_status_label = ctk.CTkLabel(
attachments_section,
text="",
font=ctk.CTkFont(size=11),
text_color=COLORS["text_muted"],
anchor="w"
)
self.audio_status_label.pack(fill="x", pady=(4, 0))
self.attachments_frame = ctk.CTkFrame(attachments_section, fg_color="transparent")
self.attachments_frame.pack(fill="x", pady=(4, 0))
self.attachment_images = []
self._register_image_drop_targets(
self,
editor_frame,
self.content_container,
self.text_frame,
self.content_text,
self._content_text_widget(),
self.checklist_frame,
attachments_section,
attachments_header,
self.attachments_frame,
)
# Advanced options (collapsible)
self.advanced_frame = ctk.CTkFrame(editor_frame, fg_color="transparent")
self.advanced_frame.pack(fill="x", pady=(12, 0))
# Unlink from Keep button (only shown for synced notes)
self.unlink_btn = ctk.CTkButton(
self.advanced_frame,
text="Unlink from Google Keep",
font=ctk.CTkFont(size=12),
height=32,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_red"],
border_width=1,
border_color=COLORS["accent_red"],
command=self._unlink_from_keep
)
# Initialize with text editor
self.text_frame.pack(fill="both", expand=True)
self.checklist_items_widgets: List[ctk.CTkFrame] = []
def load_note(self, note: Optional[Note] = None):
"""Load a note into the editor"""
if note:
self.current_note = note
self.header_title.configure(text="Edit Note")
self.title_entry.delete(0, "end")
self.title_entry.insert(0, note.title)
self.note_type_var.set(note.note_type.value)
self._on_type_change()
if note.note_type == NoteType.CHECKLIST:
self._load_checklist_items(note.checklist_items)
else:
self.content_text.delete("1.0", "end")
self.content_text.insert("1.0", note.content)
self.selected_color = normalize_keep_color(note.color)
self._refresh_color_buttons()
self.reminder_entry.delete(0, "end")
self.reminder_entry.insert(0, format_reminder_datetime(note.reminder_at))
self.reminder_location_entry.delete(0, "end")
self.reminder_location_entry.insert(0, note.reminder_location)
self._load_shared_with(note.shared_with)
self._load_attachments(note.attachments)
self._load_labels(note.labels)
self.sync_badge.update_status(note.sync_status)
# Show/hide unlink button
if note.keep_id:
self.unlink_btn.pack(side="left")
else:
self.unlink_btn.pack_forget()
# Update pin button
pin_color = COLORS["accent_yellow"] if note.pinned else COLORS["text_secondary"]
self.pin_btn.configure(image=IconManager.get_icon("pin", 18, pin_color))
else:
self.current_note = Note(
id=str(uuid.uuid4()),
title="",
content="",
sync_status=SyncStatus.LOCAL_ONLY
)
self.header_title.configure(text="New Note")
self.title_entry.delete(0, "end")
self.content_text.delete("1.0", "end")
self.note_type_var.set("note")
self._on_type_change()
self.selected_color = ""
self._refresh_color_buttons()
self._clear_checklist_items()
self._load_labels([])
self._clear_reminder(mark_modified=False)
self._load_shared_with([])
self._load_attachments([])
self.sync_badge.update_status(SyncStatus.LOCAL_ONLY)
self.unlink_btn.pack_forget()
self.pin_btn.configure(image=IconManager.get_icon("pin", 18, COLORS["text_secondary"]))
self.is_modified = False
def _on_modify(self, event=None):
"""Mark note as modified"""
self.is_modified = True
def _register_image_drop_targets(self, *widgets):
"""Register widgets as native file drop targets when the optional bridge is available."""
for widget in widgets:
if not widget:
continue
target_id = str(widget)
if target_id in self._image_drop_target_ids:
continue
if enable_file_drop(widget, self._handle_image_drop):
self._image_drop_target_ids.add(target_id)
self._image_drop_enabled = True
def _register_image_drop_tree(self, widget):
"""Register a widget and its current children as image drop targets."""
self._register_image_drop_targets(widget)
try:
children = widget.winfo_children()
except Exception:
return
for child in children:
self._register_image_drop_tree(child)
def _content_text_widget(self):
return getattr(self.content_text, "_textbox", self.content_text)
def _apply_markdown_format(self, style: str):
"""Apply markdown formatting to the current selection or insert placeholder text."""
if self.markdown_mode_var.get() == "Preview":
self.markdown_mode_var.set("Edit")
self._refresh_markdown_controls()
widget = self._content_text_widget()
try:
start = widget.index("sel.first")
end = widget.index("sel.last")
selected = widget.get(start, end)
except Exception:
start = widget.index("insert")
end = start
selected = ""
replacement = format_markdown_selection(selected, style)
if selected:
widget.delete(start, end)
widget.insert(start, replacement)
try:
widget.tag_remove("sel", "1.0", "end")
widget.tag_add("sel", start, f"{start}+{len(replacement)}c")
widget.mark_set("insert", f"{start}+{len(replacement)}c")
except Exception:
pass
self._on_modify()
if self.markdown_mode_var.get() == "Preview":
self._render_markdown_preview()
def _markdown_preview_widget(self):
return getattr(self.markdown_preview, "_textbox", self.markdown_preview)
def _configure_markdown_preview_tags(self):
widget = self._markdown_preview_widget()
widget.tag_configure("paragraph", foreground=COLORS["text_primary"], font=("Segoe UI", 13), spacing3=4)
widget.tag_configure("heading_1", foreground=COLORS["text_primary"], font=("Segoe UI", 22, "bold"), spacing1=8, spacing3=6)
widget.tag_configure("heading_2", foreground=COLORS["text_primary"], font=("Segoe UI", 18, "bold"), spacing1=8, spacing3=5)
widget.tag_configure("heading_3", foreground=COLORS["text_primary"], font=("Segoe UI", 15, "bold"), spacing1=6, spacing3=4)
widget.tag_configure("list_item", foreground=COLORS["text_primary"], lmargin1=18, lmargin2=30, spacing3=3)
widget.tag_configure("task", foreground=COLORS["text_primary"], lmargin1=18, lmargin2=30, spacing3=3)
widget.tag_configure("quote", foreground=COLORS["text_secondary"], lmargin1=18, lmargin2=18, spacing1=4, spacing3=4)
widget.tag_configure("code_block", foreground=COLORS["accent_cyan"], background=COLORS["bg_darkest"], font=("Consolas", 12), lmargin1=12, lmargin2=12, spacing1=3, spacing3=3)
widget.tag_configure("bold", font=("Segoe UI", 13, "bold"))
widget.tag_configure("italic", font=("Segoe UI", 13, "italic"))
widget.tag_configure("inline_code", foreground=COLORS["accent_cyan"], background=COLORS["bg_darkest"], font=("Consolas", 12))
def _markdown_enabled(self) -> bool:
return (
self.current_note is not None
and self.note_type_var.get() == NoteType.NOTE.value
and note_uses_markdown(self.current_note.labels)
)
def _set_markdown_mode(self, value: str):
self.markdown_mode_var.set(value)
self._refresh_markdown_controls()
def _refresh_markdown_controls(self):
if not hasattr(self, "markdown_controls"):
return
self.markdown_controls.pack_forget()
self.content_text.pack_forget()
self.markdown_preview.pack_forget()
if not self._markdown_enabled():
self.markdown_mode_var.set("Edit")
self.content_text.pack(fill="both", expand=True)
return
self.markdown_controls.pack(fill="x", pady=(0, 8))
if self.markdown_mode_var.get() == "Preview":
self._render_markdown_preview()
self.markdown_preview.pack(fill="both", expand=True)
else:
self.content_text.pack(fill="both", expand=True)
def _render_markdown_preview(self):
widget = self._markdown_preview_widget()
widget.configure(state="normal")
widget.delete("1.0", "end")
for block in markdown_preview_blocks(self.content_text.get("1.0", "end-1c")):
segments = block["segments"]
if not segments:
widget.insert("end", "\n")
continue
line_style = block["style"]
for segment in segments:
text = segment["text"]
if not text:
continue
start = widget.index("end-1c")
widget.insert("end", text)
end = widget.index("end-1c")
widget.tag_add(line_style, start, end)
if segment["style"] != "plain":
widget.tag_add(segment["style"], start, end)
widget.insert("end", "\n")
widget.configure(state="disabled")
def _select_color(self, color: str):
"""Select a Keep color for the note."""
self.selected_color = normalize_keep_color(color)
self._refresh_color_buttons()
self._on_modify()
def _refresh_color_buttons(self):
"""Update color selector button borders."""
if not hasattr(self, "color_buttons"):
return
for color_key, button in self.color_buttons.items():
is_selected = color_key == self.selected_color
button.configure(
border_color=COLORS["text_primary"] if is_selected else COLORS["border"],
border_width=3 if is_selected else 2
)
def _clear_reminder(self, mark_modified: bool = True):
"""Clear reminder inputs."""
self.reminder_entry.delete(0, "end")
self.reminder_location_entry.delete(0, "end")
if mark_modified:
self._on_modify()
def _load_shared_with(self, shared_with: List[str]):
"""Display imported sharing metadata."""
if shared_with:
self.shared_display.configure(
text=", ".join(shared_with),
text_color=COLORS["accent_cyan"]
)
else:
self.shared_display.configure(
text="Not shared",
text_color=COLORS["text_muted"]
)
def _load_attachments(self, attachments: List[Attachment]):
"""Render imported attachments in the editor."""
for widget in self.attachments_frame.winfo_children():
widget.destroy()
self.attachment_images.clear()
if not attachments:
ctk.CTkLabel(
self.attachments_frame,
text="No attachments - drop images here or use Add/Paste",
font=ctk.CTkFont(size=12),
text_color=COLORS["text_muted"],
anchor="w"
).pack(anchor="w")
self._register_image_drop_tree(self.attachments_frame)
return
for attachment in attachments:
row = ctk.CTkFrame(self.attachments_frame, fg_color=COLORS["bg_medium"], corner_radius=8)
row.pack(fill="x", pady=(0, 6))
if attachment.is_image and attachment.exists:
try:
image = Image.open(attachment.stored_path)
image.thumbnail((180, 120))
ctk_image = ctk.CTkImage(light_image=image.copy(), dark_image=image.copy(), size=image.size)
self.attachment_images.append(ctk_image)
preview = ctk.CTkLabel(row, text="", image=ctk_image)
preview.pack(side="left", padx=8, pady=8)
except Exception:
pass
details = ctk.CTkFrame(row, fg_color="transparent")
details.pack(side="left", fill="x", expand=True, padx=8, pady=8)
ctk.CTkLabel(
details,
text=attachment.filename,
font=ctk.CTkFont(size=12, weight="bold"),
text_color=COLORS["text_primary"],
anchor="w"
).pack(fill="x")
ctk.CTkLabel(
details,
text=attachment.mime_type,
font=ctk.CTkFont(size=11),
text_color=COLORS["text_muted"],
anchor="w"
).pack(fill="x")
open_btn = ctk.CTkButton(
row,
text="Open",
font=ctk.CTkFont(size=12),
width=58,
height=30,
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_blue"],
command=lambda item=attachment: self._open_attachment(item)
)
open_btn.pack(side="right", padx=8)
self._register_image_drop_tree(self.attachments_frame)
def _open_attachment(self, attachment: Attachment):
"""Open an attachment with the system default handler."""
target = attachment.stored_path
try:
path = Path(target)
if path.exists():
webbrowser.open(path.resolve().as_uri())
return
except (OSError, ValueError):
pass
if target:
webbrowser.open(target)
def _add_image_attachment(self):
"""Copy an image file into the note attachment store."""
if not self.current_note:
return
path = filedialog.askopenfilename(title="Add Image", filetypes=IMAGE_FILETYPES)
if not path:
return
try:
attachment = copy_image_attachment(Path(path), self.db.db_path, self.current_note.id)
except Exception as e:
messagebox.showerror("Add Image Failed", str(e))
return
self.current_note.attachments.append(attachment)
self._load_attachments(self.current_note.attachments)
self._on_modify()
def _paste_image_attachment(self):
"""Paste a clipboard image into the note attachment store."""
if not self.current_note:
return
try:
clipboard = ImageGrab.grabclipboard()
if isinstance(clipboard, Image.Image):
attachment = save_clipboard_image_attachment(clipboard, self.db.db_path, self.current_note.id)
elif isinstance(clipboard, list) and clipboard:
attachment = copy_image_attachment(Path(clipboard[0]), self.db.db_path, self.current_note.id)
else:
messagebox.showinfo("No Image", "Clipboard does not contain an image.")
return
except Exception as e:
messagebox.showerror("Paste Image Failed", str(e))
return
self.current_note.attachments.append(attachment)
self._load_attachments(self.current_note.attachments)
self._on_modify()
def _handle_image_drop(self, event):
"""Copy dropped image files into the note attachment store."""
if not self.current_note:
return drop_copy_action()
paths = parse_drop_file_paths(getattr(event, "data", ""), splitlist=self.tk.splitlist)
if not paths:
messagebox.showinfo("No Image", "Drop image files to attach them.")
return drop_copy_action()
result = copy_image_attachments(paths, self.db.db_path, self.current_note.id)
if result.attachments:
self.current_note.attachments.extend(result.attachments)
self._load_attachments(self.current_note.attachments)
self._on_modify()
if result.failed_paths and not result.attachments:
first_path, first_error = result.failed_paths[0]
messagebox.showerror(
"Drop Image Failed",
f"No images were attached.\n{first_path.name}: {first_error}"
)
elif not result.attachments:
messagebox.showinfo("No Image", "Drop image files to attach them.")
elif result.skipped_paths or result.failed_paths:
lines = [f"Attached {len(result.attachments)} image file(s)."]
if result.skipped_paths:
lines.append(f"Skipped {len(result.skipped_paths)} non-image file(s).")
if result.failed_paths:
lines.append(f"Failed {len(result.failed_paths)} image file(s).")
messagebox.showwarning("Some Files Skipped", "\n".join(lines))
return drop_copy_action()
def _toggle_audio_recording(self):
"""Start or stop inline voice note recording."""
if self.audio_recorder and self.audio_recorder.is_recording:
self._stop_audio_recording()
else:
self._start_audio_recording()
def _start_audio_recording(self):
if not self.current_note:
return
try:
self.audio_recorder = AudioRecorder()
self.audio_recorder.start()
except AudioRecordingError as e:
self.audio_recorder = None
messagebox.showerror("Audio Recording Failed", str(e))
return
self.record_audio_btn.configure(
text="Stop Audio",
fg_color=COLORS["accent_red"],
hover_color=COLORS["accent_red"],
text_color=COLORS["text_primary"],
)
self._set_audio_status("Recording voice note...")
def _stop_audio_recording(self):
if not self.current_note or not self.audio_recorder:
return
recorder = self.audio_recorder
self.audio_recorder = None
try:
attachment = recorder.stop_to_attachment(self.db.db_path, self.current_note.id)
except AudioRecordingError as e:
self._reset_audio_record_button()
messagebox.showerror("Audio Recording Failed", str(e))
return
self._reset_audio_record_button()
self.current_note.attachments.append(attachment)
self._load_attachments(self.current_note.attachments)
self._on_modify()
self._set_audio_status("Audio saved. Transcribing...")
self._transcribe_audio_attachment(attachment)
def _reset_audio_record_button(self):
self.record_audio_btn.configure(
text="Record Audio",
fg_color="transparent",
hover_color=COLORS["bg_hover"],
text_color=COLORS["accent_cyan"],
)
def _set_audio_status(self, text: str, error: bool = False):
self.audio_status_label.configure(
text=text,
text_color=COLORS["accent_red"] if error else COLORS["text_muted"],
)
def _transcribe_audio_attachment(self, attachment: Attachment):
thread = threading.Thread(
target=self._transcribe_audio_worker,
args=(attachment,),
daemon=True,
)
thread.start()
def _transcribe_audio_worker(self, attachment: Attachment):
try:
transcript = transcribe_audio_file(Path(attachment.stored_path))
except AudioTranscriptionError as e:
self._after_safe(lambda: self._set_audio_status(str(e), error=True))
return
self._after_safe(lambda: self._apply_audio_transcript(transcript))
def _after_safe(self, callback: Callable):
try:
self.after(0, callback)
except Exception:
pass
def _apply_audio_transcript(self, transcript: str):
text = transcript.strip()
if not text:
self._set_audio_status("Audio saved. No speech detected.")
return
if self.note_type_var.get() == "checklist":
self._add_checklist_item(f"Audio transcript: {text}", focus=False)
else:
updated = append_audio_transcript(self.content_text.get("1.0", "end-1c"), text)
self.content_text.delete("1.0", "end")