Skip to content

Commit

Permalink
update: plugin api
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Aug 27, 2023
1 parent 62f343a commit 0714466
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
46 changes: 42 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,68 @@
import { SERVICES } from "./utils/config";
import { getPref } from "./utils/prefs";
import { TranslateTask } from "./utils/task";
import { version } from "../package.json";

/**
* To plugin developers: Please use this API to translate your custom text.
*
* @param raw raw text for translation.
* @param service service id. See src/utils/config.ts > SERVICES
* If not provided, the default service will be used.
* If you want to use multiple services, please provide an array of service ids.
* The first service in the array will be used as the default service.
* Others will be used as fallback services.
* @returns TranslateTask object.
*/
async function translate(raw: string, service?: string) {
async function translate(raw: string, service?: string | string[]) {
let currentService: string;
let candidateServices: string[] = [];
if (typeof service === "string") {
currentService = service;
} else if (Array.isArray(service)) {
currentService = service[0];
candidateServices = service.slice(1);
} else {
currentService = getPref("translateSource") as string;
}

const data: TranslateTask = {
id: `${Zotero.Utilities.randomString()}-${new Date().getTime()}`,
type: "custom",
raw,
result: "",
audio: [],
service: service || (getPref("translateSource") as string),
candidateServices: [],
service: currentService,
candidateServices,
itemId: -1,
status: "waiting",
extraTasks: [],
silent: true,
};
await addon.data.translate.services.runTranslationTask(data, {
noDisplay: true,
});
return data;
}

export default { translate };
/**
* Get all available services.
* @returns Array of services.
*/
function getServices() {
return SERVICES.map((service) => Object.assign({}, service));
}

/**
* Get version of the plugin.
* @returns Version of the plugin.
*/
function getVersion() {
return version;
}

export default {
translate,
getServices,
getVersion,
};
2 changes: 1 addition & 1 deletion src/modules/services/cnki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getPref, setPref } from "../../utils/prefs";
import { TranslateTaskProcessor } from "../../utils/task";

export default <TranslateTaskProcessor>async function (data) {
if (data.raw.length > 1000) {
if (data.raw.length > 1000 && !data.silent) {
new ztoolkit.ProgressWindow("PDF Translate")
.createLine({
text: `Maximam text length is 1000, ${data.raw.length} selected. Will only translate first 1000 characters.`,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export const SERVICES: Readonly<Readonly<TranslateService>[]> = <const>[
type: "sentence",
id: "caiyun",
defaultSecret: "3975l6lr5pcbvidl6jl2",
secretValidator(secret: string) {
return {
secret,
status: secret !== "",
info: "",
};
},
},
{
type: "sentence",
Expand Down
4 changes: 4 additions & 0 deletions src/utils/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export interface TranslateTask {
* For extra services function.
*/
extraTasks: TranslateTask[] & { extraTasks: [] }[];
/**
* Whether to mute error info, depends on the implementation of the service.
*/
silent?: boolean;
}

export type TranslateTaskProcessor = (
Expand Down

0 comments on commit 0714466

Please sign in to comment.