-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathindex.js
205 lines (174 loc) · 4.53 KB
/
index.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/** @jsx h */
import '@logseq/libs'
import { h, render } from 'preact'
import { useCallback, useEffect, useState } from 'preact/hooks'
const googleFonts = [
'Noto Serif SC', 'Nunito',
'Prata', 'PT Sans',
]
function GoogleFontSelect (props) {
useEffect(() => {
const { googleFontFamily } = props
logseq.provideStyle({
key: 'editor-blocks-container-style',
style: `
.blocks-container {
font-family: ${googleFontFamily ? googleFontFamily : 'inherit'}
}
`,
})
}, [props.googleFontFamily])
const changeGoogleFont = useCallback((googleFontFamily) => {
logseq.updateSettings({ googleFontFamily })
}, [])
return (
<div className="rt">
<label>
<strong>
Google fonts:
</strong>
<select onChange={(e) => {
changeGoogleFont(e.target.value)
}}
value={props.googleFontFamily}>
{googleFonts.map((it) => {
return <option value={it} key={it}>{it}</option>
})}
</select>
</label>
</div>
)
}
function ContentWidthMode (props) {
const modes = ['small', 'normal', 'middle', 'widen']
useEffect(() => {
const mode = props.contentWidthMode || 'normal'
const width = ['580px', '700px', '70%', '90%'][modes.indexOf(mode)]
logseq.provideStyle({
key: 'content-widen-mode',
style: `
:root {
--ls-main-content-max-width: ${width};
}
`,
})
}, [props.contentWidthMode])
return (
<div className="rt">
<label>
<strong>
Content width:
</strong>
<select onChange={(e) => {
logseq.updateSettings({
contentWidthMode: e.target.value,
})
}}
value={props.contentWidthMode || 'normal'}>
{modes.map((it) => {
return <option value={it} key={it}>{it}</option>
})}
</select>
</label>
</div>
)
}
function ZoomFactorRange (props) {
useEffect(() => {
let { zoomFactor } = props
if (zoomFactor) {
logseq.App.setZoomFactor(zoomFactor / 100)
}
}, [props.zoomFactor])
return (
<div className="rt">
<label>
<strong>
Zoom factor:
</strong>
<input type="range" min="100" max="130" value={props.zoomFactor}
step="2"
onChange={(e) => {
logseq.updateSettings({
zoomFactor: e.target.value,
})
if (logseq.isMainUIVisible) {
logseq.hideMainUI()
logseq.App.showMsg(
'You can continue to up/down key zoom in/out :)"')
}
}}/>
</label>
</div>
)
}
function App () {
const [settings, setSettings] = useState(logseq.settings)
useEffect(() => {
logseq.on('settings:changed', (a) => {
setSettings(a)
})
}, [])
return (
<div>
<GoogleFontSelect googleFontFamily={settings.googleFontFamily}/>
<ContentWidthMode contentWidthMode={settings.contentWidthMode}/>
<ZoomFactorRange zoomFactor={settings.zoomFactor}/>
<p className="ctl">
<button onClick={() => {
logseq.hideMainUI()
}}>OK
</button>
</p>
</div>
)
}
function main () {
const doc = document
render(<App/>, doc.querySelector('#app'))
logseq.provideModel({
sayHello ({ dataset }) {
logseq.App.showMsg(dataset.args)
},
openFontsPanel (e) {
const { rect } = e
logseq.setMainUIInlineStyle({
top: `${rect.top + 20}px`,
left: `${rect.right - 10}px`,
})
logseq.toggleMainUI()
},
},
)
const id = logseq.baseInfo.id
logseq.provideStyle(`
@import url("https://at.alicdn.com/t/font_2409735_r7em724douf.css");
@import url('https://fonts.googleapis.com/css2?family=Noto+Serif+SC&family=Prata&family=PT+Sans&family=Nunito&display=swap');
`)
logseq.setMainUIInlineStyle({
position: 'fixed',
width: '290px',
zIndex: 999,
transform: 'translateX(-50%)',
})
logseq.App.registerUIItem('toolbar',
{
key: 'awesome-fonts-btn',
template: `
<a
style="font-weight: bold"
data-on-click="openFontsPanel"
data-rect
>
<i class="iconfont icon-font"></i>
</a>
`,
})
document.addEventListener('keydown', function (e) {
if (e.keyCode === 27) {
logseq.hideMainUI()
}
}, false)
}
// bootstrap
logseq.ready(main).catch(console.error)