File tree Expand file tree Collapse file tree 4 files changed +80
-16
lines changed
Expand file tree Collapse file tree 4 files changed +80
-16
lines changed Original file line number Diff line number Diff line change 11<script setup lang="ts">
2- import { useApp , useSnippets , useSnippetUpdate } from ' @/composables'
2+ import {
3+ useApp ,
4+ useEditableField ,
5+ useSnippets ,
6+ useSnippetUpdate ,
7+ } from ' @/composables'
38import { i18n } from ' @/electron'
49
510import {
@@ -30,11 +35,13 @@ const { addToUpdateQueue } = useSnippetUpdate()
3035
3136const isShowDescription = ref (false )
3237
33- const name = computed ({
34- get() {
35- return selectedSnippet ?.value ?.name
36- },
37- set(v : string ) {
38+ const {
39+ model : name,
40+ onFocus : onNameFocus,
41+ onBlur,
42+ } = useEditableField (
43+ () => selectedSnippet ?.value ?.name ,
44+ (v ) => {
3845 addToUpdateQueue (selectedSnippet .value ! .id , {
3946 name: v ,
4047 description: selectedSnippet .value ! .description ,
@@ -43,7 +50,12 @@ const name = computed({
4350 isFavorites: selectedSnippet .value ! .isFavorites ,
4451 })
4552 },
46- })
53+ )
54+
55+ function onNameBlur() {
56+ onBlur ()
57+ isFocusedSnippetName .value = false
58+ }
4759
4860const isShowJsonVisualizerAction = computed (
4961 () => selectedSnippetContent .value ?.language === ' json' ,
@@ -86,7 +98,8 @@ function onJsonVisualizerToggle() {
8698 variant =" ghost"
8799 class =" w-full truncate px-0"
88100 :select =" isFocusedSnippetName"
89- @blur =" isFocusedSnippetName = false"
101+ @focus =" onNameFocus"
102+ @blur =" onNameBlur"
90103 />
91104 <div class =" ml-2 flex" >
92105 <UiActionButton
Original file line number Diff line number Diff line change 11<script setup lang="ts">
22import * as Select from ' @/components/ui/shadcn/select'
3- import { useNotes , useNotesApp , useNoteUpdate } from ' @/composables'
3+ import {
4+ useEditableField ,
5+ useNotes ,
6+ useNotesApp ,
7+ useNoteUpdate ,
8+ } from ' @/composables'
49import { i18n } from ' @/electron'
510import { router , RouterName } from ' @/router'
611import {
@@ -75,16 +80,23 @@ function onPresentationToggle() {
7580 router .push ({ name: RouterName .notesPresentation })
7681}
7782
78- const name = computed ({
79- get() {
80- return selectedNote .value ?.name
81- },
82- set(v : string ) {
83+ const {
84+ model : name,
85+ onFocus : onNameFocus,
86+ onBlur,
87+ } = useEditableField (
88+ () => selectedNote .value ?.name ,
89+ (v ) => {
8390 if (selectedNote .value ) {
8491 addToUpdateQueue (selectedNote .value .id , { name: v })
8592 }
8693 },
87- })
94+ )
95+
96+ function onNameBlur() {
97+ onBlur ()
98+ isFocusedNoteName .value = false
99+ }
88100
89101const editorContent = ref (' ' )
90102
@@ -128,7 +140,8 @@ const textStats = computed(() => getTextStats(content.value))
128140 variant =" ghost"
129141 class =" w-full truncate px-0"
130142 :select =" isFocusedNoteName"
131- @blur =" isFocusedNoteName = false"
143+ @focus =" onNameFocus"
144+ @blur =" onNameBlur"
132145 />
133146 <div class =" ml-2 flex h-7 items-center" >
134147 <UiActionButton
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ export * from './spaces/notes'
33export * from './useApp'
44export * from './useCopyToClipboard'
55export * from './useDialog'
6+ export * from './useEditableField'
67export * from './useEditor'
78export * from './useFolders'
89export * from './useNotesEditor'
Original file line number Diff line number Diff line change 1+ type SourceFn = ( ) => string | undefined
2+
3+ export function useEditableField (
4+ source : SourceFn ,
5+ onUpdate : ( value : string ) => void ,
6+ ) {
7+ const localValue = ref ( '' )
8+ const isFocused = ref ( false )
9+
10+ watch (
11+ source ,
12+ ( newValue ) => {
13+ if ( ! isFocused . value ) {
14+ localValue . value = newValue ?? ''
15+ }
16+ } ,
17+ { immediate : true } ,
18+ )
19+
20+ const model = computed ( {
21+ get : ( ) => localValue . value ,
22+ set : ( v : string ) => {
23+ localValue . value = v
24+ onUpdate ( v )
25+ } ,
26+ } )
27+
28+ function onFocus ( ) {
29+ isFocused . value = true
30+ }
31+
32+ function onBlur ( ) {
33+ isFocused . value = false
34+ }
35+
36+ return { model, onFocus, onBlur }
37+ }
You can’t perform that action at this time.
0 commit comments