forked from logseq/logseq-plugin-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (46 loc) · 1.31 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import '@logseq/libs'
import { setCORS } from 'google-translate-api-browser'
// setting up cors-anywhere server address
const translate = setCORS('https://thingproxy.freeboard.io/fetch/')
/**
* main entry
*/
async function main () {
logseq.App.showMsg('hello, a translator :)')
logseq.Editor.onInputSelectionEnd((e) => {
const { x, y } = e.point
const dsl = (text: string, text2: string = '') => {
return {
key: 'selection-end-text-dialog',
close: 'outside',
template: `
<div style="padding: 10px; overflow: auto;">
<h3>${text}</h3>
<h3>${text2}</h3>
</div>
`,
style: {
left: x + 'px',
top: y + 'px',
width: '300px',
backgroundColor: 'var(--ls-primary-background-color)',
color: 'var(--ls-primary-text-color)',
boxShadow: '1px 2px 5px var(--ls-secondary-background-color)',
},
attrs: {
title: 'A Translator',
},
}
}
logseq.provideUI(dsl('Loading...'))
translate(e.text, { to: 'zh' }).then((res) => {
//@ts-ignore
logseq.provideUI(dsl(e.text, decodeURIComponent(res.text)))
}).catch((e) => {
logseq.provideUI(dsl('ERROR'))
console.error(e)
})
})
}
// bootstrap
logseq.ready(main).catch(console.error)