From 09c7b10975b1507791a067ca471b4d1cdf936e6c Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Fri, 21 Jun 2024 07:38:48 +0000 Subject: [PATCH] Update --- .github/workflows/build.yml | 1 + README.md | 46 ++++++---------- README_EN.md | 104 ------------------------------------ main.js | 4 +- 4 files changed, 18 insertions(+), 137 deletions(-) delete mode 100644 README_EN.md diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5536bde..399226c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,5 +1,6 @@ name: Build on: push +permissions: write-all jobs: build: diff --git a/README.md b/README.md index f742cbe..820418e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # Pot-App 翻译插件模板仓库 (以 [Lingva](https://github.com/TheDavidDelta/lingva-translate) 为例) -### [English](./README_EN.md) | 简体中文 - ### 此仓库为模板仓库,编写插件时可以直接由此仓库创建插件仓库 ## 插件编写指南 @@ -23,32 +21,29 @@ - `key`: 依赖 key,对应该项依赖在配置文件中的名称,例如 `requestPath` - `display`: 依赖显示名称,对应用户显示的名称,例如 `请求地址` - `type`: 组件类型 `input` | `select` - - `options`: 选项列表(仅select组件需要),例如 `{"engine_a":"Engina A","engine_b":"Engina B"}` + - `options`: 选项列表(仅 select 组件需要),例如 `{"engine_a":"Engina A","engine_b":"Engina B"}` - `language`: 插件支持的语言映射,将 pot 的语言代码和插件发送请求时的语言代码一一对应 -### 3. 插件编写/编译 +### 3. 插件编写 -编辑 `src/lib.rs` 实现 `translate` 函数 +编辑 `main.js` 实现 `translate` 函数 #### 输入参数 -```rust - text: &str, // 待翻译文本 - from: &str, // 源语言代码 - to: &str, // 目标语言代码 - detect: &str, // 检测到的语言代码(未转换) - needs: HashMap, // 插件需要的其他配置信息,由info.json定义 +```javascript + async function translate(text, from, to, options) { + const { config, utils,setResult } = options; + const { tauriFetch } = utils; + } ``` #### 返回值 -```rust -// 文本翻译 -// 返回用Value包裹的String - return Ok(Value::String(result)); -// 词典 -// 返回指定格式的json - return Ok(json!(result)); +```javascript +// 文本翻译直接返回字符串 +return "result"; +// 流式输出使用options中的setResult函数 +setResult("result"); ``` 词典返回 json 示例: @@ -78,20 +73,11 @@ } ``` -#### 测试/编译 - -```bash -cargo test --package plugin --lib -- tests --nocapture # 运行测试用例 -cargo build --release # 编译 -``` - ### 4. 打包 pot 插件 -1. 在`target/release`目录找到`plugin.dll`(Windows)/`libplugin.dylib`(MacOS)/`libplugin.so`(Linux)文件,统一删除`lib`前缀. - -2. 将`plugin.dll`/`libplugin.dylib`/`libplugin.so`文件和`info.json`以及图标文件压缩为 zip 文件。 +1. 将 `main.js` 文件和 `info.json` 以及图标文件压缩为 zip 文件。 -3. 将文件重命名为`<插件id>.potext`,例如`[plugin].com.pot-app.lingva.potext`,即可得到 pot 需要的插件。 +2. 将文件重命名为`<插件id>.potext`,例如`[plugin].com.pot-app.lingva.potext`,即可得到 pot 需要的插件。 ## 自动编译打包 @@ -100,5 +86,3 @@ cargo build --release # 编译 每次将仓库推送到 GitHub 之后 actions 会自动运行,将打包好的插件上传到 artifact,在 actions 页面可以下载 每次提交 Tag 之后,actions 会自动运行,将打包好的插件上传到 release,在 release 页面可以下载打包好的插件 - -> 注意需要在仓库设置中添加一个名为`TOKEN`的 secret,值为一个有权限的 GitHub Token,用于上传 release diff --git a/README_EN.md b/README_EN.md deleted file mode 100644 index c6d9821..0000000 --- a/README_EN.md +++ /dev/null @@ -1,104 +0,0 @@ -# Pot-App Translation Plugin Template Repository (Take [Lingva](https://github.com/TheDavidDelta/lingva-translate) for example) - -### English | [简体中文](./README.md) - -### This repository is a template repository. You can directly create a plugin repository from this repository when writing plugins - -## Plugin Writing Guide - -### 1. Create a plugin repository - -- Create a new repository using this repository as a template -- Named `pot-app-translate-plugin-`,Eg: `pot-app-translate-plugin-lingva` - -### 2. Plugin information configuration - -Edit the `info.json` file and modify the following fields: - -- `id`: The unique ID of the plugin, which must start with `[plugin]`, for example `[plugin].com.pot-app.lingva` -- `homepage`: The homepage of the plugin, simply fill in your repository address, for example `https://github.com/pot-app/pot-app-translate-plugin-template` -- `display`: The display name of the plugin, for example `Lingva` -- `icon`: The icon of the plugin, for example `lingva.svg` -- `needs`: Dependencies required by the plugin. It is an array where each dependency is an object that includes the following fields: - - `key`: The key of the dependency, corresponding to its name in the configuration file. For example,`requestPath`. - - `display`: The display name of the dependency as shown to users, for example `Request Path`. - - `type`: Component type `input` | `select` - - `options`: Option list(Only `select` component), for example `{"engine_a":"Engina A","engine_b":"Engina B"}` -- `language`: Mapping between language codes used in Pot and language codes used when sending requests to plugins. - -### 3. Plugin writing/compiling - -Edit `src/lib.rs` to implement `translate` function - -#### Input parameters - -```rust - text: &str, // text to be translated - from: &str, // source language code - to: &str, // target language code - detect: &str, // detected language code (not convert) - needs: HashMap, // Additional configuration information required by the plugin, defined by info.json -``` - -#### Return value - -```rust -// Text Translation -// Returns a String wrapped in Value - return Ok(Value::String(result)); -// Dictionary -// Returns json in the specified format - return Ok(json!(result)); -``` - -Dictionary returns json example: - -```json -{ - "pronunciations": [ - { - "region": "", - "symbol": "", - "voice": [u8] - } - ], - "explanations": [ - { - "trait": "", - "explains": [""] - } - ], - "associations": [""], - "sentence": [ - { - "source": "", - "target": "" - } - ] -} -``` - -#### Test/Compile - -```bash -cargo test --package plugin --lib -- tests --nocapture # run the test case -cargo build --release # Compile -``` - -### 4. Package pot Plugin - -1. Find the `plugin.dll` (Windows)/`libplugin.dylib` (MacOS)/`libplugin.so` (Linux) file in the `target/release` directory and delete the prefix `lib`. - -2. Compress the `plugin.dll`/`libplugin.dylib`/`libplugin.so`, with the `info.json` and icon files, into a zip file. - -3. Rename the file as `.potext`, for example `[plugin].com.pot-app.lingva.potext`, to obtain the plugin required by pot. - -## Automatic Compilation and Package - -This repository is configured with Github Actions, which allows for automatic compilation and packaging of plugins after pushing. - -Every time the commit is pushed to GitHub, actions will run automatically and upload the packaged plugin to artifacts. The packaged plugin can be downloaded from the actions page. - -After each tag submission, actions will also run automatically and upload the packaged plugin to releases. The packaged plugin can be downloaded from the release page. - -> Please note that you need to add a secret named `TOKEN` in the repository settings. The value should be a GitHub Token with appropriate permissions, which will be used for uploading releases. diff --git a/main.js b/main.js index cee33d4..cc36667 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,6 @@ async function translate(text, from, to, options) { const { config, utils } = options; - const { tauriFetch } = utils; + const { tauriFetch: fetch } = utils; let { requestPath: url } = config; let plain_text = text.replaceAll("/", "@@"); let encode_text = encodeURIComponent(plain_text); @@ -10,7 +10,7 @@ async function translate(text, from, to, options) { if (!url.startsWith("http")) { url = `https://${url}`; } - const res = tauriFetch(`${url}/api/v1/${from}/${to}/${encode_text}`, { + const res = await fetch(`${url}/api/v1/${from}/${to}/${encode_text}`, { method: 'GET', });