1
1
<script setup lang="ts">
2
+ import type { Post , PostAccount } from ' @/types'
2
3
import { useStore } from ' @/stores'
3
- import { Info } from ' lucide-vue-next'
4
- import { Primitive } from ' radix-vue'
4
+ import { Check , Info } from ' lucide-vue-next'
5
+ import { CheckboxIndicator , CheckboxRoot , Primitive } from ' radix-vue'
5
6
6
7
const store = useStore ()
7
- const { output } = storeToRefs (store )
8
+ const { output, editor } = storeToRefs (store )
8
9
9
10
const dialogVisible = ref (false )
10
11
const extensionInstalled = ref (false )
11
- const form = ref <any >({
12
+ const allAccounts = ref <PostAccount []>([])
13
+ const postTaskDialogVisible = ref (false )
14
+
15
+ const form = ref <Post >({
12
16
title: ` ` ,
13
17
desc: ` ` ,
14
18
thumb: ` ` ,
15
19
content: ` ` ,
16
- auto: {},
20
+ markdown: ` ` ,
21
+ accounts: [] as PostAccount [],
17
22
})
18
23
19
- function prePost() {
20
- let auto = {}
24
+ const allowPost = computed (() => extensionInstalled .value && form .value .accounts .some (a => a .checked ))
25
+
26
+ async function prePost() {
27
+ let auto: Post = {
28
+ thumb: ` ` ,
29
+ title: ` ` ,
30
+ desc: ` ` ,
31
+ content: ` ` ,
32
+ markdown: ` ` ,
33
+ accounts: [],
34
+ }
21
35
try {
22
36
auto = {
23
- thumb: document .querySelector <HTMLImageElement >(` #output img ` )?.src ,
37
+ thumb: document .querySelector <HTMLImageElement >(` #output img ` )?.src ?? ` ` ,
24
38
title: [1 , 2 , 3 , 4 , 5 , 6 ]
25
39
.map (h => document .querySelector (` #output h${h } ` )! )
26
40
.filter (h => h )[0 ]
27
- .textContent ,
28
- desc: document .querySelector (` #output p ` )! .textContent ,
41
+ .textContent ?? ` ` ,
42
+ desc: document .querySelector (` #output p ` )! .textContent ?? ` ` ,
29
43
content: output .value ,
44
+ markdown: editor .value ?.getValue () ?? ` ` ,
45
+ accounts: allAccounts .value ,
30
46
}
31
47
}
32
48
catch (error ) {
33
49
console .log (` error ` , error )
34
50
}
35
- form .value = {
36
- ... auto ,
37
- auto ,
51
+ finally {
52
+ form .value = {
53
+ ... auto ,
54
+ }
55
+ console .log (form .value , ` ==== ` )
38
56
}
39
57
}
40
58
@@ -45,24 +63,49 @@ declare global {
45
63
}
46
64
}
47
65
48
- function post() {
49
- dialogVisible .value = false ;
50
- (window .syncPost )({
51
- thumb: form .value .thumb || form .value .auto .thumb ,
52
- title: form .value .title || form .value .auto .title ,
53
- desc: form .value .desc || form .value .auto .desc ,
54
- content: form .value .content || form .value .auto .content ,
66
+ async function getAccounts() {
67
+ await window .$syncer ?.getAccounts ((resp : PostAccount []) => {
68
+ allAccounts .value = resp .map (a => ({ ... a , checked: true }))
55
69
})
56
70
}
57
71
72
+ function post() {
73
+ form .value .accounts = form .value .accounts .filter (a => a .checked )
74
+ postTaskDialogVisible .value = true
75
+ dialogVisible .value = false
76
+ }
77
+
58
78
function onUpdate(val : boolean ) {
59
79
if (! val ) {
60
80
dialogVisible .value = false
61
81
}
62
82
}
63
83
64
- onMounted (() => {
65
- extensionInstalled .value = window .$syncer !== undefined
84
+ function checkExtension() {
85
+ if (window .$syncer !== undefined ) {
86
+ extensionInstalled .value = true
87
+ return
88
+ }
89
+
90
+ // 如果插件还没加载,5秒内每 500ms 检查一次
91
+ let count = 0
92
+ const timer = setInterval (() => {
93
+ if (window .$syncer !== undefined ) {
94
+ extensionInstalled .value = true
95
+ getAccounts ()
96
+ clearInterval (timer )
97
+ return
98
+ }
99
+
100
+ count ++
101
+ if (count > 10 ) { // 5秒后还是没有检测到,就停止检查
102
+ clearInterval (timer )
103
+ }
104
+ }, 500 )
105
+ }
106
+
107
+ onBeforeMount (() => {
108
+ checkExtension ()
66
109
})
67
110
</script >
68
111
@@ -81,7 +124,7 @@ onMounted(() => {
81
124
<Info class =" h-4 w-4" />
82
125
<AlertTitle >提示</AlertTitle >
83
126
<AlertDescription >
84
- 此功能由第三方浏览器插件支持,本平台不保证安全性 。
127
+ 此功能由第三方浏览器插件支持,本平台不保证安全性及同步准确度 。
85
128
</AlertDescription >
86
129
</Alert >
87
130
@@ -91,9 +134,7 @@ onMounted(() => {
91
134
<AlertDescription >
92
135
请安装
93
136
<Primitive
94
- as =" a"
95
- class =" text-blue-500"
96
- href =" https://www.wechatsync.com/?utm_source=syncicon#install"
137
+ as =" a" class =" text-blue-500" href =" https://www.wechatsync.com/?utm_source=syncicon#install"
97
138
target =" _blank"
98
139
>
99
140
文章同步助手
@@ -121,14 +162,44 @@ onMounted(() => {
121
162
<Textarea id =" desc" v-model =" form.desc" placeholder =" 自动提取第一个段落" />
122
163
</div >
123
164
165
+ <div class =" w-full flex items-start gap-4" >
166
+ <Label class =" w-10 text-end" >
167
+ 账号
168
+ </Label >
169
+ <div class =" flex flex-1 flex-col gap-2" >
170
+ <div v-for =" account in form.accounts" :key =" account.uid + account.displayName" class =" flex items-center gap-2" >
171
+ <label class =" flex flex-row items-center gap-4" >
172
+ <CheckboxRoot
173
+ v-model:checked =" account.checked"
174
+ class =" bg-background hover:bg-muted h-[25px] w-[25px] flex appearance-none items-center justify-center border border-gray-200 rounded-[4px] outline-none"
175
+ >
176
+ <CheckboxIndicator >
177
+ <Check v-if =" account.checked" class =" h-4 w-4" />
178
+ </CheckboxIndicator >
179
+ </CheckboxRoot >
180
+ <span class =" flex items-center gap-2 text-sm" >
181
+ <img
182
+ :src =" account.icon"
183
+ alt =" "
184
+ class =" inline-block h-[20px] w-[20px]"
185
+ >
186
+ {{ account.title }} - {{ account.displayName ?? account.home }}
187
+ </span >
188
+ </label >
189
+ </div >
190
+ </div >
191
+ </div >
192
+
124
193
<DialogFooter >
125
194
<Button variant =" outline" @click =" dialogVisible = false" >
126
195
取 消
127
196
</Button >
128
- <Button :disabled =" !extensionInstalled " @click =" post" >
197
+ <Button :disabled =" !allowPost " @click =" post" >
129
198
确 定
130
199
</Button >
131
200
</DialogFooter >
132
201
</DialogContent >
133
202
</Dialog >
203
+
204
+ <PostTaskDialog v-model:open =" postTaskDialogVisible" :post =" form" />
134
205
</template >
0 commit comments