-
Notifications
You must be signed in to change notification settings - Fork 6
/
buffer.py
562 lines (465 loc) · 20.4 KB
/
buffer.py
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env python3# -*- coding: utf-8 -*-
# Copyright (C) 2018 Andy Stewart
#
# Author: Andy Stewart <[email protected]>
# Maintainer: Andy Stewart <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import base64
import json
import os
import random
import sys
import time
from html import escape, unescape
from core.utils import *
from core.webengine import BrowserBuffer
from PyQt6 import QtCore
from PyQt6.QtCore import QEvent, QPointF, Qt, QTimer, QUrl
from PyQt6.QtGui import QMouseEvent
Py_version=sys.version_info
class AppBuffer(BrowserBuffer):
def __init__(self, buffer_id, url, arguments):
BrowserBuffer.__init__(self, buffer_id, url, arguments, False)
self.url = url
index_file = os.path.join(os.path.dirname(__file__), "index.html")
self.buffer_widget.setUrl(QUrl.fromLocalFile(index_file))
self.cut_node_id = None
(self.mindmap_edit_mode, self.mindmap_save_path) = get_emacs_vars(["eaf-mindmap-edit-mode", "eaf-mindmap-save-path"])
edit_mode = "true" if self.mindmap_edit_mode else "false"
for method_name in ["add_sub_node", "add_brother_node", "add_middle_node"]:
self.build_js_method(method_name, True, js_kwargs={"inline": edit_mode})
for method_name in ["remove_node", "remove_middle_node", "update_node_topic_inline"]:
self.build_js_method(method_name, True)
for method_name in ["zoom_in", "zoom_out", "zoom_reset",
"select_up_node", "select_down_node", "select_left_node", "select_right_node",
"toggle_node", "toggle_node_selection", "save_screenshot"]:
self.build_js_method(method_name)
for method_name in ["zoom_in", "zoom_out", "zoom_reset", "remove_node",
"remove_middle_node", "add_middle_node", "refresh_page",
"select_up_node", "select_down_node", "select_left_node", "select_right_node",
"toggle_node", "toggle_node_selection", "save_screenshot"]:
self.build_insert_or_do(method_name)
self.build_all_methods(self)
self.watcher = QtCore.QFileSystemWatcher([self.url])
self.watcher.fileChanged.connect(self.file_changed)
self.cache = HeaderTree(self.url)
self.buffer_widget.loadFinished.connect(lambda _: self.initialize())
def resize_view(self):
self.buffer_widget.eval_js_function("relayout")
@PostGui()
def initialize(self):
self.init_file()
# The .jsmind-inner element is move right and bottom 30px,
# so we must use a point greater than (30, 30), ex (100, 100).
self.focus_widget(QMouseEvent(QEvent.Type.MouseButtonPress,
QPointF(100, 100),
Qt.MouseButton.LeftButton,
Qt.MouseButton.LeftButton,
Qt.KeyboardModifier.NoModifier))
def init_file(self):
self.url = os.path.expanduser(self.url)
if os.path.exists(self.url):
with open(self.url, "r") as f:
if self.url.endswith(".org") or self.url.endswith(".md"):
self.cache.parse_lines(f.readlines())
emm_json = json.dumps(self.cache.to_emm())
self.buffer_widget.eval_js_function("open_file", string_to_base64(emm_json), False)
else:
_, ext = os.path.splitext(self.url)
is_freemind = ext == ".mm"
self.buffer_widget.eval_js_function("open_file", string_to_base64(f.read()), is_freemind)
else:
self.buffer_widget.eval_js_function("init_root_node")
QTimer.singleShot(200, lambda: self.buffer_widget.eval_js_function("select_root_node"))
self.buffer_widget.eval_js_function("init_background", self.theme_background_color)
self.change_title(self.get_title())
@interactive
def update_theme(self):
super().update_theme()
self.buffer_widget.eval_js_function("init_background", self.theme_background_color)
def build_js_method(self, method_name, auto_save=False, js_kwargs=None):
js_kwargs = js_kwargs or {}
js_func_args = ", ".join('{}={}'.format(k, v) for k, v in js_kwargs.items())
def _do ():
self.buffer_widget.eval_js("{}({});".format(method_name, js_func_args))
if auto_save:
self.save_file(False)
setattr(self, method_name, _do)
@interactive
def refresh_page(self):
self.url = os.path.expanduser(self.url)
if os.path.exists(self.url):
with open(self.url, "r") as f:
if self.url.endswith(".org") or self.url.endswith(".md"):
orgcache = HeaderTree(self.url)
orgcache.parse_lines(f.readlines())
if orgcache.header_list == self.cache.header_list:
self.cache.merge(orgcache, "topic")
else:
self.cache = orgcache
emm_json = json.dumps(self.cache.to_emm())
self.buffer_widget.eval_js_function("refresh", string_to_base64(emm_json), False)
else:
self.buffer_widget.eval_js_function("refresh", string_to_base64(f.read()))
self.buffer_widget.eval_js_function("init_background", self.theme_background_color)
self.change_title(self.get_title())
message_to_emacs("refresh file")
@PostGui()
def file_changed(self, path):
mode = get_emacs_vars(['major-mode'])[0].value()
if mode != "eaf-mode":
self.refresh_page()
@interactive(insert_or_do=True)
def change_background_color(self):
self.send_input_message("Change node background color(Input color): ", "change_background_color")
@interactive(insert_or_do=True)
def change_text_color(self):
self.send_input_message("Change node text color(Input color): ", "change_text_color")
@interactive(insert_or_do=True)
def copy_node_topic(self):
node_topic = self.buffer_widget.execute_js("get_node_topic();")
eval_in_emacs('kill-new', [node_topic])
message_to_emacs("Copy: {}".format(node_topic))
@interactive(insert_or_do=True)
def paste_node_topic(self):
text = self.get_clipboard_text()
if text.strip() != "":
self.buffer_widget.eval_js_function("update_node_topic", text)
message_to_emacs("Paste: {}".format(text))
self.save_file(False)
else:
message_to_emacs("Nothing in clipboard, can't paste.")
@interactive(insert_or_do=True)
def cut_node_tree(self):
self.cut_node_id = self.buffer_widget.execute_js("get_selected_nodeid();")
if self.cut_node_id:
if self.cut_node_id != "root":
message_to_emacs("Root node not allowed cut.")
else:
message_to_emacs("Cut node tree: {}".format(self.cut_node_id))
@interactive(insert_or_do=True)
def paste_node_tree(self):
if self.cut_node_id:
self.buffer_widget.eval_js_function("paste_node_tree", self.cut_node_id)
self.save_file(False)
message_to_emacs("Paste node tree: {}".format(self.cut_node_id))
@interactive(insert_or_do=True)
def change_node_background(self):
self.send_input_message("Change node background: ", "change_node_background", "file")
@interactive(insert_or_do=True)
def update_node_topic(self):
self.send_input_message(
"Update topic: ",
"update_node_topic",
"string",
unescape(self.buffer_widget.execute_js("get_node_topic();")))
def handle_update_node_topic(self, topic):
self.buffer_widget.eval_js_function("update_node_topic", escape(topic))
self.change_title(self.get_title())
self.save_file(False)
@PostGui()
def handle_input_response(self, callback_tag, result_content):
if callback_tag == "update_node_topic":
self.handle_update_node_topic(str(result_content))
elif callback_tag == "change_node_background":
print(str(result_content))
self.buffer_widget.eval_js_function("change_node_background", str(result_content))
elif callback_tag == "change_background_color":
self.buffer_widget.eval_js_function("change_background_color", str(result_content))
elif callback_tag == "change_text_color":
self.buffer_widget.eval_js_function("change_text_color", str(result_content))
def add_multiple_sub_nodes(self):
node_id = self.buffer_widget.execute_js("_jm.get_selected_node();")
if node_id is not None:
eval_in_emacs('eaf--add-multiple-sub-nodes', [self.buffer_id])
else:
message_to_emacs("No selected node.")
def add_multiple_brother_nodes(self):
node_id = self.buffer_widget.execute_js("_jm.get_selected_node();")
if node_id is None:
message_to_emacs("No selected node.")
elif not self.buffer_widget.execute_js("_jm.get_selected_node().parent;"):
message_to_emacs("No parent node for selected node.")
else:
eval_in_emacs('eaf--add-multiple-brother-nodes', [self.buffer_id])
def add_multiple_middle_nodes(self):
node_id = self.buffer_widget.execute_js("_jm.get_selected_node();")
if node_id is None:
message_to_emacs("No selected node.")
elif not self.buffer_widget.execute_js("_jm.get_selected_node().parent;"):
message_to_emacs("No parent node for selected node.")
else:
eval_in_emacs('eaf--add-multiple-middle-nodes', [self.buffer_id])
@interactive
def add_texted_sub_node(self,text):
self.buffer_widget.eval_js_function("add_texted_sub_node", str(text))
@interactive
def add_texted_brother_node(self,text):
self.buffer_widget.eval_js_function("add_texted_brother_node", str(text))
@interactive
def add_texted_middle_node(self,text):
self.buffer_widget.eval_js_function("add_texted_middle_node", str(text))
def is_focus(self):
return self.buffer_widget.execute_js("node_is_focus();")
def get_title(self):
return os.path.basename(self.url) or self.get_root_node_topic()
def get_root_node_topic(self):
return self.buffer_widget.execute_js("get_root_node_topic();")
def handle_download_request(self, download_item):
download_data = download_item.url().toString()
if self.should_skip_download_item(download_item) or not download_data.startswith("data:image/"):
return
# Note:
# Set some delay to make get_root_node_topic works expect.
# get_root_node_topic will return None if execute immediately.
QTimer.singleShot(200, lambda : self.save_screenshot_data(download_data))
def get_save_path(self, extension_name):
if self.url.strip() == "":
return os.path.join(os.path.expanduser(self.mindmap_save_path), self.get_root_node_topic().replace(" ", "_") + time.strftime("_%Y%m%d_%H%M%S", time.localtime(int(time.time()))) + "." + extension_name)
else:
return os.path.splitext(self.url)[0] + "." + extension_name
def save_screenshot_data(self, download_data):
image_path = self.get_save_path("png")
touch(image_path)
with open(image_path, "wb") as f:
if Py_version > (3,8):
f.write(base64.decodebytes(download_data.split("data:image/png;base64,")[1].encode("utf-8")))
else:
f.write(base64.decodestring(download_data.split("data:image/png;base64,")[1].encode("utf-8")))
message_to_emacs("Save image: " + image_path)
@interactive(insert_or_do=True)
def save_file(self, notify=True):
if self.url.endswith(".org") or self.url.endswith(".md"):
file_path = self.url
data = self.buffer_widget.execute_js("save_file();")
jscache = HeaderTree(self.url)
jscache.parse_emm(json.loads(data))
if jscache.header_list != self.cache.header_list:
jscache.merge(self.cache)
print(jscache.data)
self.cache = jscache
data = "".join(self.cache.flatten())
eval_in_emacs('eaf-mindmap--write-content-to-file', [self.url, data])
else:
file_path = self.get_save_path("emm")
with open(file_path, "w") as f:
data = self.buffer_widget.execute_js("save_file();")
f.write(data)
if notify:
message_to_emacs("Save file: " + file_path)
@interactive(insert_or_do=True)
def save_org_file(self):
if not self.url.endswith(".org"):
file_path = self.get_save_path("org")
touch(file_path)
eval_in_emacs('eaf--export-org-json', [self.buffer_widget.execute_js("save_file();"), file_path])
message_to_emacs("Save org file: " + file_path)
@interactive(insert_or_do=True)
def save_freemind_file(self, notify=True):
file_path = self.get_save_path("mm")
with open(file_path, "w") as f:
f.write(self.buffer_widget.execute_js("save_freemind_file();"))
if notify:
message_to_emacs("Save freemind file: " + file_path)
def dark_mode_is_enabled(self):
''' Return bool of whether dark mode is enabled.'''
return get_app_dark_mode("eaf-mindmap-dark-mode")
@PostGui()
def update_multiple_sub_nodes(self, new_text):
''' Update multiplt sub nodes.'''
for line in str(new_text).split("\n"):
self.add_texted_sub_node(line)
@PostGui()
def update_multiple_brother_nodes(self, new_text):
''' Update multiplt brother nodes.'''
for line in str(new_text).split("\n"):
self.add_texted_brother_node(line)
@PostGui()
def update_multiple_middle_nodes(self, new_text):
''' Update multiplt middle nodes.'''
for line in str(new_text).split("\n"):
self.add_texted_middle_node(line)
@interactive
def jump_to_keywords(self):
if self.url.endswith(".org"):
node_id = self.buffer_widget.execute_js("get_selected_nodeid();")
keywords = self.cache.path_finder(node_id)
eval_in_emacs('eaf-mindmap--search-succesive-in-file', [self.url, keywords])
class HeaderTree:
def __init__(self, url=""):
self.header_list = []
self.data = {}
self.header_prefix = "*"
self.root_name = os.path.basename(url),
if url.endswith(".org"):
self.header_prefix = "*"
elif url.endswith(".md"):
self.header_prefix = "#"
def parse_emm(self, emm):
"""
read a emm format structure
"""
self.data = emm["data"]
self.header = self.flatten(False)
def parse_lines(self, lines):
"""
from lines to tree
"""
self.data = {
"id": "root",
"topic": self.root_name,
}
path = [self.data]
i, level = 0, 0
n = len(lines)
while i < n:
content = []
while i < n and not self.is_header(lines[i]):
content.append(lines[i])
i += 1
path[-1]["content"] = "".join(content)
if i == n:
break
stars, header = lines[i].split(" ", 1)
self.header_list.append(header.strip())
new_level = len(stars)
node = {
"id": generate_id(),
"topic": header.strip(),
}
if new_level <= level:
for _ in range(level - new_level + 1):
path.pop()
else:
path[-1]["children"] = []
new_level = level + 1 # fix ill format
path[-1]["children"].append(node)
path.append(node)
level = new_level
i += 1
def to_emm(self):
"""
convert self.data to emm format, a tree copy algorithm(bfs)
"""
queue = [self.data]
level = 1
root = {
"id": "root",
"topic": self.data["topic"],
"expanded": True,
"children": [],
}
dual = {"root": root}
while queue:
temp_queue = []
for node in queue:
for i, child in enumerate(node.get("children", [])):
if level == 1:
half = (len(node["children"]) + 1) // 2
direction = "right" if i < half else "left"
else:
direction = dual[node["id"]]["direction"]
new_node = {
"id": child["id"],
"topic": child["topic"],
"expanded": True,
"direction": direction,
"children": [],
}
temp_queue.append(child)
dual[child["id"]] = new_node
dual[node["id"]]["children"].append(new_node)
level += 1
queue = temp_queue
emm = {
"meta": {
"name": "jsMind",
"author": "[email protected]",
"version": "0.4.6",
},
"format": "node_tree",
}
emm["data"] = dual["root"]
return emm
def flatten(self, with_content=True):
"""
convert tree to lines, preorder traverse
"""
def dfs(root, level=0, res=[]):
content = root["content"] if "content" in root else ""
header = root["topic"]
if level == 0:
if with_content:
res.append(content)
else:
res.append(self.header_prefix * level + " " + header + "\n")
if with_content:
res.append(content)
children = root["children"] if "children" in root else []
for child in children:
dfs(child, level + 1, res)
lines = []
dfs(self.data, 0, lines)
return lines
def is_header(self, line, level=None):
if not line.startswith(self.header_prefix):
return False
if len(line.split()) <= 1:
return False
stars = line.split()[0]
if level:
return (
list(set(stars)) == [self.header_prefix]
and len(stars) == level
)
else:
return list(set(stars)) == [self.header_prefix]
def merge(self, other, by="id"):
"""
merge other tree into self.data
by: id or topic
"""
lookup = {}
def build_map(root):
lookup[root[by]] = root
for node in root.get("children", []):
build_map(node)
build_map(self.data)
def _merge(root):
if root[by] in lookup:
if "content" in root:
lookup[root[by]]["content"] = root["content"]
for node in root.get("children", []):
_merge(node)
_merge(other.data)
def path_finder(self, target_id):
"""
traverse emm tree to get the topic path from root to target_id
"""
res, path = [], []
def dfs(root, level=0):
if root["id"] == target_id:
res.append(list(path))
elif "children" in root:
for node in root["children"]:
path.append(
self.header_prefix * (level + 1) + " " + node["topic"]
)
dfs(node, level + 1)
path.pop()
dfs(self.data)
return res[0]
def generate_id():
return str(random.random())[2:]