From 26d6922bd7ab65851f2e71f96b72a6d4f6913e2e Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Wed, 26 Feb 2020 15:02:29 +0800 Subject: [PATCH] Press a to save screenshot of mind map. --- app/mindmap/buffer.py | 4 ++-- app/mindmap/index.html | 5 +++++ core/browser.py | 16 +++++++++++++--- eaf.el | 1 + 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/mindmap/buffer.py b/app/mindmap/buffer.py index ebcaee3..a0b03a0 100644 --- a/app/mindmap/buffer.py +++ b/app/mindmap/buffer.py @@ -33,12 +33,12 @@ class AppBuffer(BrowserBuffer): for method_name in ["zoom_in", "zoom_out", "zoom_reset", "add_sub_node", "remove_node", "select_up_node", "select_down_node", "select_left_node", "select_right_node", - "toggle_node"]: + "toggle_node", "save_screenshot"]: self.build_js_method(method_name) for method_name in ["zoom_in", "zoom_out", "zoom_reset", "remove_node", "update_node_topic", "refresh_page", "select_up_node", "select_down_node", "select_left_node", "select_right_node", - "toggle_node"]: + "toggle_node", "save_screenshot"]: self.build_insert_or_do(method_name) def build_js_method(self, method_name): diff --git a/app/mindmap/index.html b/app/mindmap/index.html index c2f097c..7546484 100644 --- a/app/mindmap/index.html +++ b/app/mindmap/index.html @@ -158,6 +158,11 @@ _jm.toggle_node(selected_node); } } + + function save_screenshot() { + "use strict"; + _jm.screenshot.shootDownload(); + } diff --git a/core/browser.py b/core/browser.py index 4a6989e..cee31a3 100644 --- a/core/browser.py +++ b/core/browser.py @@ -31,6 +31,7 @@ import os import base64 import subprocess import re +import base64 MOUSE_BACK_BUTTON = 8 MOUSE_FORWARD_BUTTON = 16 @@ -424,10 +425,19 @@ class BrowserBuffer(Buffer): def handle_download_request(self, download_item): self.try_start_aria2_daemon() - with open(os.devnull, "w") as null_file: - subprocess.Popen(["aria2p", "add", download_item.url().toString()], stdout=null_file) + download_data = download_item.url().toString() + if download_data.startswith("data:image/png;base64,"): + image_path = os.path.join(os.path.expanduser(str(self.emacs_var_dict["eaf-browser-download-path"])), + os.path.splitext(os.path.basename(self.buffer_widget.url().toString()))[0] + ".png") + with open(image_path, "wb") as f: + f.write(base64.decodestring(download_data.split("data:image/png;base64,")[1].encode("utf-8"))) + + self.message_to_emacs.emit("Save image: " + image_path) + else: + with open(os.devnull, "w") as null_file: + subprocess.Popen(["aria2p", "add", download_data], stdout=null_file) - self.message_to_emacs.emit("Start download: " + download_item.url().toString()) + self.message_to_emacs.emit("Start download: " + download_data) def handle_destroy(self): self.close_page.emit(self.buffer_widget.url().toString()) diff --git a/eaf.el b/eaf.el index 4216cb0..9e9919c 100644 --- a/eaf.el +++ b/eaf.el @@ -440,6 +440,7 @@ Try not to modify this alist directly. Use `eaf-setq' to modify instead." ("d" . "insert_or_remove_node") ("f" . "insert_or_update_node_topic") ("t" . "insert_or_toggle_node") + ("a" . "insert_or_save_screenshot") ) "The keybinding of EAF Mindmap." :type 'cons)