|
| 1 | +import { TreeViewCommandHandler } from '@/commands/command-handler'; |
| 2 | +import { Post } from '@/models/post'; |
| 3 | +import { AlertService } from '@/services/alert.service'; |
| 4 | +import { PostFileMapManager } from '@/services/post-file-map'; |
| 5 | +import { postService } from '@/services/post.service'; |
| 6 | +import { PostTreeItem } from '@/tree-view-providers/models/post-tree-item'; |
| 7 | +import { env, MessageItem, Uri, window } from 'vscode'; |
| 8 | + |
| 9 | +type LinkFormat = 'markdown' | 'raw' | 'id'; |
| 10 | +interface CopyStrategy { |
| 11 | + name: string; |
| 12 | + provideContent: (post: Post) => Thenable<string>; |
| 13 | +} |
| 14 | + |
| 15 | +export class CopyPostLinkCommandHandler extends TreeViewCommandHandler<Thenable<Post | null | undefined>> { |
| 16 | + private readonly _strategies: { [key in LinkFormat]: CopyStrategy } = { |
| 17 | + raw: { |
| 18 | + name: '复制链接', |
| 19 | + provideContent: ({ url }) => Promise.resolve(url), |
| 20 | + }, |
| 21 | + markdown: { |
| 22 | + name: '复制markdown格式链接', |
| 23 | + provideContent: ({ url, title }) => Promise.resolve(`[${title}](${url})`), |
| 24 | + }, |
| 25 | + id: { |
| 26 | + name: '复制Id', |
| 27 | + provideContent: ({ id }) => Promise.resolve(`${id}`), |
| 28 | + }, |
| 29 | + }; |
| 30 | + |
| 31 | + constructor(public readonly input: unknown) { |
| 32 | + super(); |
| 33 | + } |
| 34 | + |
| 35 | + async handle(): Promise<void> { |
| 36 | + const post = await this.parseInput(); |
| 37 | + |
| 38 | + if (post == null) return; |
| 39 | + |
| 40 | + const linkFormat = await this.askFormat(); |
| 41 | + if (linkFormat == null) return; |
| 42 | + |
| 43 | + const contentToCopy = await this._strategies[linkFormat].provideContent(post); |
| 44 | + if (contentToCopy.length > 0) await env.clipboard.writeText(contentToCopy); |
| 45 | + } |
| 46 | + |
| 47 | + parseInput(): Thenable<Post | null | undefined> { |
| 48 | + const { input } = this; |
| 49 | + if (input instanceof Post) { |
| 50 | + return Promise.resolve(input); |
| 51 | + } else if (input instanceof PostTreeItem) { |
| 52 | + return Promise.resolve(input.post); |
| 53 | + } else if (input instanceof Uri) { |
| 54 | + const postId = PostFileMapManager.findByFilePath(input.fsPath)?.[0]; |
| 55 | + return postId == null || postId <= 0 |
| 56 | + ? Promise.resolve(undefined).then(() => void AlertService.fileNotLinkedToPost(input)) |
| 57 | + : postService.fetchPostEditDto(postId).then(v => v?.post); |
| 58 | + } |
| 59 | + |
| 60 | + return Promise.resolve(undefined); |
| 61 | + } |
| 62 | + |
| 63 | + private askFormat(): Thenable<LinkFormat | undefined | null> { |
| 64 | + return window |
| 65 | + .showInformationMessage( |
| 66 | + '选择链接格式', |
| 67 | + { modal: true }, |
| 68 | + ...(Object.keys(this._strategies) as LinkFormat[]).map<MessageItem & { format: LinkFormat }>(f => ({ |
| 69 | + title: this._strategies[f].name, |
| 70 | + format: f, |
| 71 | + isCloseAffordance: false, |
| 72 | + })) |
| 73 | + ) |
| 74 | + .then(x => x?.format); |
| 75 | + } |
| 76 | +} |
0 commit comments