Skip to content

Commit

Permalink
snapshot: relate vuejs/language-tools#1815
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryC-3 committed Mar 3, 2023
1 parent 8a4eece commit 9551dde
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
21 changes: 3 additions & 18 deletions README.md
@@ -1,18 +1,3 @@
# Vue 3 + TypeScript + Vite

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.

## Recommended IDE Setup

- [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).

## Type Support For `.vue` Imports in TS

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.

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:

1. Disable the built-in TypeScript Extension
1. Run `Extensions: Show Built-in Extensions` from VSCode's command palette
2. Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.
- pnpm i
- Open the project in VS Code.
- Open `src/components/addBookPage`, check the error.
32 changes: 24 additions & 8 deletions src/background.ts
@@ -1,17 +1,33 @@
import { Book } from "./types";
// import { dataURLToBlob } from "blob-util";
import { Client } from "@notionhq/client";
import { error } from "console";
import { BackgroundRes, Book } from "./types";
import { Client, NotionClientError } from "@notionhq/client";

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

chrome.runtime.onMessage.addListener((message: { book: Book }) => {
if (message) {
// const cover = dataURLToBlob(message.book.cover);
addBook(message.book);
// TODO: 移除该变量
let book = {};
chrome.runtime.onMessage.addListener(
(message, _, response: (msg: BackgroundRes) => void) => {
console.log("message", message);
if (message.book) {
book = message.book;
}
if (message.triggered) {
addBook(book as Book)
.then(() => {
// TODO: 测试是否能跳转到错误页
throw error;
response({ success: true });
})
.catch((error) =>
response({ success: false, error: error as NotionClientError })
);
}
return true;
}
});
);

async function addBook(book: Book) {
try {
Expand Down
7 changes: 6 additions & 1 deletion src/components/AddBookPage.vue
Expand Up @@ -21,12 +21,17 @@

<script setup lang="ts">
import { useRouter } from "vue-router";
import type { BackgroundRes } from "../types";
const router = useRouter();
const bookTitle = "";
const saveBook = () => {
router.push("/open");
// TODO: 修复此处的 eslint 警告
// eslint-disable-next-line no-undef
chrome.runtime.sendMessage({ triggered: true }, (res: BackgroundRes) => {
res.success ? router.push("/open") : router.push("/error");
});
};
</script>

Expand Down
24 changes: 24 additions & 0 deletions src/message.ts
@@ -0,0 +1,24 @@
export const { sendTriggered, watchTriggered } = getMessenger("triggered");

type MessageType =
| "wrongPage" // current page doesn't match https://book.douban.com/subject/27594044/
| "ready" // content script has collected book information
| "triggered" // user triggers the save book button
| "failed" // background script fails to send the book to notion
| "success" // background script sends the book to notion successfully
| "duplicate"; // background scripts detects a duplicate book in notion

function getMessenger<T>(type: MessageType) {
return {
["send" + type.toUpperCase()]: (data: T) => {
chrome.runtime.sendMessage({ type, data });
},
["watch" + type.toUpperCase()]: (action: (data: T) => void) => {
chrome.runtime.onMessage.addListener((message) => {
if (message.type === type) {
action(message.data);
}
});
},
};
}
5 changes: 5 additions & 0 deletions src/types.ts
Expand Up @@ -12,3 +12,8 @@ export interface Book {
ratingCount: number; // 评价人数
cover: string;
}

export interface BackgroundRes {
success: boolean;
error?: Error;
}
8 changes: 1 addition & 7 deletions tsconfig.json
Expand Up @@ -14,12 +14,6 @@
"skipLibCheck": true,
"noEmit": true
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue",
"src/**.ts"
],
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.vite.json" }]
}

0 comments on commit 9551dde

Please sign in to comment.