-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.qml
299 lines (271 loc) · 9.16 KB
/
main.qml
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import QtQuick 2.11
import QtQuick.Window 2.2
import Qt.labs.folderlistmodel 1.0
import io.singleton 1.0
Window {
id: root
visible: true
title: qsTr("edit")
width: screen.width
height: screen.height
property int rotation: 90
property string doc: "scratch"
property int mode: 1
property bool ctrlPressed: false
property bool isOmni: false
property string omniQuery: ""
property string currentFile: "scratch.md"
// property string folder: "file://%1/edit/".arg(home_dir)
FolderListModel {
id: folderModel
// folder: root.folder
folder: "file:///home/root/edit/"
nameFilters: ["*.md"]
}
function toggleMode() { // temp: esc to save file; no need for md view
if (mode == 0) {
mode = 1
} else {
doc = query.text;
mode = 0
}
saveFile()
}
function doLoad(name) {
var xhr = new XMLHttpRequest
// xhr.open("GET", folder + name)
xhr.open("GET", "file:///home/root/edit/" + name)
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
var response = xhr.responseText
isOmni = false
mode = 1 // first edit mode?
currentFile = name
doc = response
}
}
xhr.send()
}
function saveFile() {
console.log("Save " + currentFile)
var fileUrl = "file:///home/root/edit/" + currentFile
console.log(fileUrl)
var request = new XMLHttpRequest()
request.open("PUT", fileUrl, false)
request.send(doc)
console.log("save -> " + request.status + " " + request.statusText)
return request.status
}
function initFile(name) {
console.log("Init " + name)
var fileUrl = "file:///home/root/edit/" + name + ".md"
var request = new XMLHttpRequest()
request.open("PUT", fileUrl, false)
request.send("# " + name)
console.log("save -> " + request.status + " " + request.statusText)
return request.status
}
function handleKeyDown(event) {
if (event.key === Qt.Key_Control) {
ctrlPressed = true
} else if (event.key === Qt.Key_K && ctrlPressed) {
isOmni = !isOmni
event.accepted = true
} else if (event.key === Qt.Key_Q && ctrlPressed) {
Qt.quit()
}
saveFile()
}
function handleKeyUp(event) {
if (event.key === Qt.Key_Control) {
ctrlPressed = false
}
}
function handleKey(event) {
if (event.key === Qt.Key_Escape) {
if (isOmni) {
isOmni = false
} else {
toggleMode()
}
}
if (mode == 1)
switch (event.key) {
case Qt.Key_Home:
Qt.quit()
break
case Qt.Key_G: // G contra right arrow
if (ctrlPressed) // ctrl
root.rotation = (root.rotation + 90) % 360
break
case Qt.Key_H: // H contra left arrow
if (ctrlPressed) // ctrl
root.rotation = (root.rotation - 90) % 360
break
}
}
Component.onCompleted: {
doLoad(currentFile)
}
Rectangle {
rotation: root.rotation
id: body
width: root.rotation % 180 ? root.height * 0.74 : root.width * 0.74 //this works in portrait
height: root.rotation % 180 ? root.width : root.height // root.height affects portrait
anchors.centerIn: parent // ?
color: "white"
border.color: "black"
border.width: 0
EditUtils {
id: utils
}
Flickable {
id: flick
anchors.fill: parent
contentWidth: query.paintedWidth
contentHeight: query.paintedHeight
bottomMargin: parent.height /2
clip: true
function ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}
function scrollUpSmall() {
contentY -= 400;
}
function scrollDownSmall() {
contentY += 400;
}
function scrollUpBig() {
contentY -= 45000;
saveFile()
}
function scrollDownBig() {
contentY += 45000;
saveFile()
}
TextEdit {
id: query
// width: body.width
// height: body.height
// width:1404;
width: root.rotation % 180 ? root.height * 0.74 : root.width * 0.74
Keys.enabled: true
wrapMode: TextEdit.Wrap
textMargin: 12
textFormat: mode == 0 ? TextEdit.PlainText : TextEdit.PlainText
font.family: mode == 0 ? "Georgia" : "Georgia"
text: mode == 0 ? doc : doc
focus: !isOmni
Component {
id: curDelegate
Rectangle { width:8; height: 20; visible: query.cursorVisible; color: "black";}
}
cursorDelegate: curDelegate
readOnly: mode == 0 ? false : false
font.pointSize: mode == 0 ? 6 : 6
onLinkActivated: {
console.log("Link activated: " + link)
doLoad(link)
}
Keys.onPressed: {
if (mode == 1 && (event.key == Qt.Key_Down)) {
flick.scrollDownSmall();
}
if (mode == 1 && (event.key == Qt.Key_Up)) {
flick.scrollUpSmall();
}
if (mode == 1 && (event.key == Qt.Key_S && ctrlPressed)) {
flick.scrollDownBig();
}
if (mode == 1 && (event.key == Qt.Key_W && ctrlPressed)) {
flick.scrollUpBig();
}
handleKeyDown(event);
}
Keys.onReleased: {
handleKeyUp(event)
handleKey(event)
}
}
}
Rectangle {
id: quick
anchors.centerIn: parent
width: parent.width
height: parent.height
color: "white"
visible: isOmni ? true : false
radius: 20
TextEdit {
id: omniQueryTextEdit
text: omniQuery
textFormat: TextEdit.PlainText
x: 40
width: parent.width
color: "white"
font.pointSize: 7.7
font.family: "Noto Mono"
focus: isOmni
Keys.enabled: true
Keys.onPressed: {
if (event.key === Qt.Key_Enter
|| event.key === Qt.Key_Return) {
saveFile()
if (!omniList.currentItem) {
initFile(omniQuery)
doLoad(omniQuery + ".md")
} else {
doLoad(omniList.currentItem.text)
}
isOmni = true // isOmni = false
event.accepted = true
return
}
handleKeyDown(event)
}
Keys.onReleased: {
handleKeyUp(event)
handleKey(event)
omniQuery = omniQueryTextEdit.text
folderModel.nameFilters = [omniQuery + "*"]
}
Keys.forwardTo: omniList
}
ListView {
id: omniList
anchors.top: omniQueryTextEdit.bottom
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.leftMargin: 68
anchors.rightMargin: 40
anchors.right: parent.right
highlightResizeDuration: 0
highlight: Rectangle {
color: "black"
radius: 2
width: 600
}
Component {
id: fileDelegate
Text {
width: parent.width
text: fileName
font.pointSize: 6.6
font.family: "Noto Mono"
color: ListView.isCurrentItem ? "white" : "black"
}
}
model: folderModel
delegate: fileDelegate
}
}
}
}