Skip to content

Commit f80d526

Browse files
authored
feat(web): connect Substack from the publish panel (#2020)
1 parent 7c56dd7 commit f80d526

7 files changed

Lines changed: 100 additions & 5 deletions

File tree

‎apps/web/src/components/editor/editor-header/PostInfo.vue‎

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const platformCategories = [
8686
},
8787
{
8888
id: `blog`,
89-
platforms: [`csdn`, `cnblogs`, `juejin`, `medium`, `devto`, `cto51`, `segmentfault`, `oschina`, `infoq`, `jianshu`, `wordpress`],
89+
platforms: [`csdn`, `cnblogs`, `juejin`, `medium`, `devto`, `cto51`, `segmentfault`, `oschina`, `infoq`, `jianshu`, `wordpress`, `substack`],
9090
},
9191
{
9292
id: `cloud`,
@@ -106,12 +106,41 @@ function toggleCategory(categoryName: string) {
106106
}
107107
}
108108
109+
const substackSetupOpen = ref(false)
110+
const substackFormHeight = ref(120)
111+
const substackSetupUrl = computed(() => {
112+
const src = extensionInstalled.value && window.$cose?.getSubstackSetupUrl?.()
113+
if (!src)
114+
return null
115+
try {
116+
const url = new URL(src)
117+
if (!['chrome-extension:', 'moz-extension:'].includes(url.protocol) || url.pathname !== '/substack-connect.html')
118+
return null
119+
url.searchParams.set('lang', locale.value)
120+
return url
121+
}
122+
catch {
123+
return null
124+
}
125+
})
126+
const substackPlaceholder: PostAccount = {
127+
uid: 'substack',
128+
type: 'substack',
129+
title: 'Substack',
130+
displayName: 'Substack',
131+
icon: 'https://substack.com/favicon.ico',
132+
home: '',
133+
checked: false,
134+
loggedIn: false,
135+
}
136+
109137
const accountsByCategory = computed(() => {
110138
return platformCategories.map(category => ({
111139
id: category.id,
112140
name: t(`postInfo.categories.${category.id}`),
113141
accounts: category.platforms
114-
.map(type => allAccounts.value.find(a => a.type === type))
142+
.map(type => allAccounts.value.find(a => a.type === type)
143+
?? (type === 'substack' && extensionInstalled.value ? substackPlaceholder : undefined))
115144
.filter((a): a is PostAccount => a !== undefined),
116145
}))
117146
})
@@ -157,6 +186,7 @@ watch(dialogVisible, (newVal) => {
157186
}
158187
else {
159188
wordPressSetupOpen.value = false
189+
substackSetupOpen.value = false
160190
}
161191
})
162192
@@ -362,6 +392,30 @@ useEventListener('message', (event: MessageEvent) => {
362392
}
363393
})
364394
395+
function initializeSubstackFrame(event: Event) {
396+
const url = substackSetupUrl.value
397+
const frame = event.currentTarget as HTMLIFrameElement
398+
if (url)
399+
frame.contentWindow?.postMessage({ type: 'cose-substack-initialize' }, `${url.protocol}//${url.host}`)
400+
}
401+
402+
useEventListener('message', (event: MessageEvent) => {
403+
const url = substackSetupUrl.value
404+
const frame = document.querySelector<HTMLIFrameElement>('#substack-setup-frame')
405+
if (!url || !frame || event.source !== frame.contentWindow || event.origin !== `${url.protocol}//${url.host}`)
406+
return
407+
408+
if (event.data?.type === 'cose-substack-form-height') {
409+
const height = event.data.height
410+
if (Number.isInteger(height) && height > 0 && height <= 640)
411+
substackFormHeight.value = height
412+
}
413+
else if (event.data?.type === 'cose-substack-connection-changed') {
414+
substackSetupOpen.value = false
415+
startLoginDetection()
416+
}
417+
})
418+
365419
function onAvatarError(account: PostAccount, event: Event) {
366420
const img = event.target as HTMLImageElement
367421
if (!account)
@@ -410,7 +464,7 @@ onBeforeMount(() => {
410464
</DialogTrigger>
411465
<DialogContent
412466
class="!max-w-[95vw] !w-[min(750px,95vw)] max-h-[85vh] flex flex-col overflow-hidden"
413-
@interact-outside="wordPressSetupOpen && $event.preventDefault()"
467+
@interact-outside="(wordPressSetupOpen || substackSetupOpen) && $event.preventDefault()"
414468
>
415469
<DialogHeader>
416470
<DialogTitle>{{ t('postInfo.title') }}</DialogTitle>
@@ -528,7 +582,7 @@ onBeforeMount(() => {
528582
<Loader2 class="ml-1 h-3.5 w-3.5 animate-spin text-muted-foreground" />
529583
<span class="text-xs text-muted-foreground">{{ t('postInfo.checking') }}</span>
530584
</template>
531-
<template v-else-if="isSignedInAccount(account)">
585+
<template v-else-if="isSignedInAccount(account) || (account.type === 'substack' && account.loggedIn)">
532586
<img
533587
v-if="account.avatar"
534588
:src="account.avatar"
@@ -558,8 +612,11 @@ onBeforeMount(() => {
558612
>
559613
{{ t(devtoGuideOpen ? 'postInfo.devtoCollapse' : 'postInfo.devtoConnect') }}
560614
</button>
615+
<span v-else-if="account.type === 'substack' && !substackSetupUrl" class="text-xs text-muted-foreground">
616+
{{ t('postInfo.substackUpdateExtension') }}
617+
</span>
561618
<Primitive
562-
v-else-if="account.type !== 'wordpress'"
619+
v-else-if="account.type !== 'wordpress' && account.type !== 'substack'"
563620
as="a"
564621
:href="getPlatformUrl(account.type)"
565622
target="_blank"
@@ -568,6 +625,16 @@ onBeforeMount(() => {
568625
>
569626
{{ t('common.login') }}
570627
</Primitive>
628+
<button
629+
v-if="account.type === 'substack' && !account.isChecking && substackSetupUrl"
630+
type="button"
631+
class="ml-1 shrink-0 text-sm text-primary hover:underline"
632+
:aria-expanded="substackSetupOpen"
633+
aria-controls="substack-setup"
634+
@click="substackSetupOpen = !substackSetupOpen"
635+
>
636+
{{ substackSetupOpen ? t('common.close') : isSignedInAccount(account) ? t('commandPalette.group.settings') : t('postInfo.substackConnect') }}
637+
</button>
571638
<button
572639
v-if="account.type === 'wordpress' && !account.isChecking && wordPressSetupUrl"
573640
type="button"
@@ -579,6 +646,21 @@ onBeforeMount(() => {
579646
{{ wordPressSetupOpen ? t('common.close') : isSignedInAccount(account) ? t('commandPalette.group.settings') : t('common.login') }}
580647
</button>
581648
</div>
649+
<div
650+
v-if="substackSetupOpen && substackSetupUrl && category.accounts.some(account => account.type === 'substack')"
651+
id="substack-setup"
652+
class="rounded-md border p-3 sm:col-span-2"
653+
>
654+
<iframe
655+
id="substack-setup-frame"
656+
:src="substackSetupUrl.href"
657+
title="Substack"
658+
referrerpolicy="no-referrer"
659+
class="block w-full rounded-md border-0"
660+
:style="{ height: `${substackFormHeight}px` }"
661+
@load="initializeSubstackFrame"
662+
/>
663+
</div>
582664
<div
583665
v-if="wordPressSetupOpen && wordPressSetupUrl && category.accounts.some(account => account.type === 'wordpress')"
584666
id="wordpress-setup"

‎apps/web/src/i18n/messages/en-US/dialog.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export default {
180180
extensionMissingTitle: `Extension not detected`,
181181
extensionMissingDescription: `Install the {link} browser extension`,
182182
extensionLinkText: `COSE Article Sync Assistant`,
183+
substackUpdateExtension: 'Current COSE build does not support Substack',
184+
substackConnect: 'Connect',
183185
devtoConnect: `Connect`,
184186
devtoCollapse: `Collapse`,
185187
devtoReplaceKey: `Replace key`,

‎apps/web/src/i18n/messages/ja-JP/dialog.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export default {
180180
extensionMissingTitle: `拡張機能が検出されません`,
181181
extensionMissingDescription: `{link} ブラウザ拡張機能をインストールしてください`,
182182
extensionLinkText: `COSE 記事同期アシスタント`,
183+
substackUpdateExtension: '現在の COSE バージョンは Substack に対応していません',
184+
substackConnect: '接続',
183185
devtoConnect: `接続`,
184186
devtoCollapse: `折りたたむ`,
185187
devtoReplaceKey: `キーを更新`,

‎apps/web/src/i18n/messages/zh-CN/dialog.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export default {
180180
extensionMissingTitle: `未检测到插件`,
181181
extensionMissingDescription: `请安装 {link} 浏览器扩展`,
182182
extensionLinkText: `COSE 文章同步助手`,
183+
substackUpdateExtension: '当前 COSE 版本不支持 Substack',
184+
substackConnect: '连接',
183185
devtoConnect: `连接`,
184186
devtoCollapse: `折叠`,
185187
devtoReplaceKey: `更换 Key`,

‎apps/web/src/i18n/messages/zh-TW/dialog.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export default {
180180
extensionMissingTitle: `未檢測到外掛`,
181181
extensionMissingDescription: `請安裝 {link} 瀏覽器擴充套件`,
182182
extensionLinkText: `COSE 文章同步助手`,
183+
substackUpdateExtension: '目前的 COSE 版本不支援 Substack',
184+
substackConnect: '連線',
183185
devtoConnect: `連線`,
184186
devtoCollapse: `摺疊`,
185187
devtoReplaceKey: `更換金鑰`,

‎apps/web/src/lib/post/account-label.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
interface AccountIdentity {
22
loggedIn?: boolean
3+
type?: string
4+
home?: string
35
title?: string
46
displayName?: string
57
avatar?: string
@@ -20,6 +22,8 @@ export function accountHandle(account: AccountIdentity): string {
2022
export function isSignedInAccount(account: AccountIdentity): boolean {
2123
if (!account.loggedIn)
2224
return false
25+
if (account.type === `substack`)
26+
return Boolean(account.home)
2327
if (account.avatar?.trim())
2428
return true
2529
return accountHandle(account) !== ``

‎apps/web/src/types/cose.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface CoseAddTaskData {
4343
export interface CoseApi {
4444
getPlatforms?: () => CosePlatform[]
4545
getDevtoSetupUrl?: () => string | null
46+
getSubstackSetupUrl?: () => string | null
4647
getWordPressSetupUrl?: () => string | null
4748
getAccounts?: (cb: (accounts: PostAccount[]) => void, platformIds?: string[]) => void | Promise<PostAccount[]>
4849
getAccountsProgressive?: (

0 commit comments

Comments
 (0)