Skip to content

Commit 9551dde

Browse files
committed
snapshot: relate vuejs/language-tools#1815
1 parent 8a4eece commit 9551dde

File tree

6 files changed

+63
-34
lines changed

6 files changed

+63
-34
lines changed

Diff for: README.md

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
# Vue 3 + TypeScript + Vite
2-
3-
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4-
5-
## Recommended IDE Setup
6-
7-
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
8-
9-
## Type Support For `.vue` Imports in TS
10-
11-
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
12-
13-
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
14-
15-
1. Disable the built-in TypeScript Extension
16-
1. Run `Extensions: Show Built-in Extensions` from VSCode's command palette
17-
2. Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
18-
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.
1+
- pnpm i
2+
- Open the project in VS Code.
3+
- Open `src/components/addBookPage`, check the error.

Diff for: src/background.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
import { Book } from "./types";
2-
// import { dataURLToBlob } from "blob-util";
3-
import { Client } from "@notionhq/client";
1+
import { error } from "console";
2+
import { BackgroundRes, Book } from "./types";
3+
import { Client, NotionClientError } from "@notionhq/client";
44

55
const { VITE_NOTION_AUTH_TOKEN: token, VITE_NOTION_DB_ID: db } = import.meta
66
.env;
77
const notion = new Client({ auth: token });
88

9-
chrome.runtime.onMessage.addListener((message: { book: Book }) => {
10-
if (message) {
11-
// const cover = dataURLToBlob(message.book.cover);
12-
addBook(message.book);
9+
// TODO: 移除该变量
10+
let book = {};
11+
chrome.runtime.onMessage.addListener(
12+
(message, _, response: (msg: BackgroundRes) => void) => {
13+
console.log("message", message);
14+
if (message.book) {
15+
book = message.book;
16+
}
17+
if (message.triggered) {
18+
addBook(book as Book)
19+
.then(() => {
20+
// TODO: 测试是否能跳转到错误页
21+
throw error;
22+
response({ success: true });
23+
})
24+
.catch((error) =>
25+
response({ success: false, error: error as NotionClientError })
26+
);
27+
}
28+
return true;
1329
}
14-
});
30+
);
1531

1632
async function addBook(book: Book) {
1733
try {

Diff for: src/components/AddBookPage.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121

2222
<script setup lang="ts">
2323
import { useRouter } from "vue-router";
24+
import type { BackgroundRes } from "../types";
2425
2526
const router = useRouter();
2627
2728
const bookTitle = "";
2829
const saveBook = () => {
29-
router.push("/open");
30+
// TODO: 修复此处的 eslint 警告
31+
// eslint-disable-next-line no-undef
32+
chrome.runtime.sendMessage({ triggered: true }, (res: BackgroundRes) => {
33+
res.success ? router.push("/open") : router.push("/error");
34+
});
3035
};
3136
</script>
3237

Diff for: src/message.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const { sendTriggered, watchTriggered } = getMessenger("triggered");
2+
3+
type MessageType =
4+
| "wrongPage" // current page doesn't match https://book.douban.com/subject/27594044/
5+
| "ready" // content script has collected book information
6+
| "triggered" // user triggers the save book button
7+
| "failed" // background script fails to send the book to notion
8+
| "success" // background script sends the book to notion successfully
9+
| "duplicate"; // background scripts detects a duplicate book in notion
10+
11+
function getMessenger<T>(type: MessageType) {
12+
return {
13+
["send" + type.toUpperCase()]: (data: T) => {
14+
chrome.runtime.sendMessage({ type, data });
15+
},
16+
["watch" + type.toUpperCase()]: (action: (data: T) => void) => {
17+
chrome.runtime.onMessage.addListener((message) => {
18+
if (message.type === type) {
19+
action(message.data);
20+
}
21+
});
22+
},
23+
};
24+
}

Diff for: src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ export interface Book {
1212
ratingCount: number; // 评价人数
1313
cover: string;
1414
}
15+
16+
export interface BackgroundRes {
17+
success: boolean;
18+
error?: Error;
19+
}

Diff for: tsconfig.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
"skipLibCheck": true,
1515
"noEmit": true
1616
},
17-
"include": [
18-
"src/**/*.ts",
19-
"src/**/*.d.ts",
20-
"src/**/*.tsx",
21-
"src/**/*.vue",
22-
"src/**.ts"
23-
],
17+
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
2418
"references": [{ "path": "./tsconfig.vite.json" }]
2519
}

0 commit comments

Comments
 (0)