@@ -4,6 +4,7 @@ import { computed, reactive, ref, type Ref } from 'vue';
4
4
import type { TagLink } from '~/models/tag.model' ;
5
5
6
6
import { storage } from '~/utils/browser/browser-storage.utils' ;
7
+ import { createTab } from '~/utils/browser/browser.utils' ;
7
8
import { debounce } from '~/utils/debounce.utils' ;
8
9
9
10
export const CustomLinkScope = {
@@ -52,8 +53,14 @@ export type CustomLinkDictionary = Record<number, CustomLink>;
52
53
type CustomLinkScopeDictionary = Partial < Record < CustomLinkScopes , CustomLinkDictionary > > ;
53
54
type AliasDictionary = Partial < Record < AliasScope , Record < string , string > > > ;
54
55
56
+ type LinksStoreState = {
57
+ enabled : boolean ;
58
+ backgroundLink : boolean ;
59
+ } ;
60
+
55
61
export const useLinksStore = defineStore ( 'settings.links' , ( ) => {
56
62
const enabled = ref ( false ) ;
63
+ const backgroundLink = ref ( false ) ;
57
64
const aliasDictionary = reactive < AliasDictionary > ( { } ) ;
58
65
59
66
const linkDictionary = reactive < CustomLinkDictionary > ( { } ) ;
@@ -65,7 +72,7 @@ export const useLinksStore = defineStore('settings.links', () => {
65
72
Object . assign ( linkScopeDictionary , { } ) ;
66
73
} ;
67
74
68
- const saveState = debounce ( ( ) => storage . sync . set ( 'settings.links' , { enabled : enabled . value } ) , 1000 ) ;
75
+ const saveState = debounce ( ( ) => storage . sync . set ( 'settings.links' , { enabled : enabled . value , backgroundLink : backgroundLink . value } ) , 1000 ) ;
69
76
const saveAlias = debounce ( ( ) => storage . sync . set ( 'settings.links.aliases' , aliasDictionary ) , 1000 ) ;
70
77
const saveLinks = debounce ( ( ) => storage . sync . set ( 'settings.links.links' , linkDictionary ) , 1000 ) ;
71
78
@@ -88,25 +95,26 @@ export const useLinksStore = defineStore('settings.links', () => {
88
95
89
96
// add new scopes
90
97
if ( link . scopes ) Object . values ( link . scopes ) . forEach ( scope => addToScope ( scope , link ) ) ;
91
- saveLinks ( ) . catch ( console . error ) ;
98
+ saveLinks ( ) . catch ( err => console . error ( 'Failed to save link' , { link , err } ) ) ;
92
99
} ;
93
100
94
101
const removeLink = ( id : CustomLink [ 'id' ] ) => {
95
102
const link = linkDictionary [ id ] ;
96
103
if ( ! link ) return ;
97
104
if ( linkDictionary [ link . id ] ) delete linkDictionary [ link . id ] ;
98
105
if ( link . scopes ) Object . values ( link . scopes ) . forEach ( scope => removeFromScope ( scope , link . id ) ) ;
99
- saveLinks ( ) . catch ( console . error ) ;
106
+ saveLinks ( ) . catch ( err => console . error ( 'Failed to save link' , { id , err } ) ) ;
100
107
} ;
101
108
102
109
const restoreState = async ( ) => {
103
- const [ restoredState , restoredAliases , restoredLinks ] = await Promise . all ( [
104
- storage . sync . get < { enabled : boolean } > ( 'settings.links' ) ,
110
+ const [ restoredState , restoredAliases , restoredLinks ] : [ LinksStoreState , AliasDictionary , CustomLinkDictionary ] = await Promise . all ( [
111
+ storage . sync . get < LinksStoreState > ( 'settings.links' ) ,
105
112
storage . sync . get < AliasDictionary > ( 'settings.links.aliases' ) ,
106
113
storage . sync . get < CustomLinkDictionary > ( 'settings.links.links' ) ,
107
114
] ) ;
108
115
109
- if ( restoredState . enabled !== undefined ) enabled . value = restoredState . enabled ;
116
+ if ( restoredState ?. enabled !== undefined ) enabled . value = restoredState . enabled ;
117
+ if ( restoredState ?. backgroundLink !== undefined ) backgroundLink . value = restoredState . backgroundLink ;
110
118
if ( restoredAliases ) Object . assign ( aliasDictionary , restoredAliases ) ;
111
119
if ( restoredLinks ) {
112
120
Object . values ( restoredLinks )
@@ -140,11 +148,25 @@ export const useLinksStore = defineStore('settings.links', () => {
140
148
get : ( ) => enabled . value ,
141
149
set : ( value : boolean ) => {
142
150
enabled . value = value ;
143
- saveState ( ) . catch ( err => console . error ( 'Failed to save state' , { value, err } ) ) ;
151
+ saveState ( ) . catch ( err => console . error ( 'Failed to save link settings' , { value, err } ) ) ;
152
+ } ,
153
+ } ) ;
154
+
155
+ const openLinkInBackground = computed ( {
156
+ get : ( ) => backgroundLink . value ,
157
+ set : ( value : boolean ) => {
158
+ backgroundLink . value = value ;
159
+ console . info ( 'Open links in new tab' , value ) ;
160
+ saveState ( ) . catch ( err => console . error ( 'Failed to save link settings.' , { value, err } ) ) ;
144
161
} ,
145
162
} ) ;
146
163
147
- return { initLinksStore, clearState, linkDictionary, addLink, removeLink, getLinks, getAlias, aliasEnabled } ;
164
+ const openTab = ( url ?: string ) => {
165
+ if ( ! url ) return ;
166
+ createTab ( { url, active : ! openLinkInBackground . value } ) ;
167
+ } ;
168
+
169
+ return { initLinksStore, clearState, linkDictionary, addLink, removeLink, getLinks, getAlias, aliasEnabled, openLinkInBackground, openTab } ;
148
170
} ) ;
149
171
150
172
export const useLinksStoreRefs = ( ) => storeToRefs ( useLinksStore ( ) ) ;
0 commit comments