kaorahi / lizgoban

Leela Zero & KataGo visualizer
GNU General Public License v3.0
169 stars 28 forks source link

Why is the pasted tsumego image not displayed? #106

Closed qcgm1978 closed 7 months ago

qcgm1978 commented 8 months ago

I'm trying to solve tsumego problems using this app, but when pasting the tsumego image, it shows up as gray and the tsumego position is not displayed. The effect and image are shown below:

update: Do I need to drag the img into gray area? It displays if I drag it but the stones aren't recognized correctly.

截屏2023-12-14 13 24 18 截屏2023-12-14 11 10 52 截屏2023-12-14 11 04 37
kaorahi commented 8 months ago

The gray screen suggests that something isn't working properly.

This works for me:

  1. Open this page on Chromium, right-click the tsumego diagram image, and select "Copy image".
  2. In this app, select [Edit] > [Paste] from the menu bar.
  3. Then, the diagram image appears in the "SGF from Image" window.
qcgm1978 commented 8 months ago

I copied the files wrongly. I directly copied the image files from Finder, which may not be supported. Now I'm able to solve the tsumego problems, thank you very much!

qcgm1978 commented 7 months ago

I found the app only support copying the contents of the SGF or IMG file instead of the file directly. It seems should prompt user about the error as the following code:

function paste_sgf_or_url_from_clipboard() {
    const s = clipboard.readText(1);
    if (/\.\w{0,5}$/.test(s)) { // match .sgf, .png etc
        return toast('Please copy the contents of the SGF or IMG file instead of copying the file directly.',3000)
    }
    ...
}
kaorahi commented 7 months ago

Could you explain this in detail? I'd like to know:

  1. OS
  2. Specific mouse/keyboard actions to reproduce the issue, including the exact meaning of "copying the file directly"
  3. Example values of clipboard.readText() and clipboard.readText(1) in this situation

By the way, is clipboard.readText(1) correct? Do you mean clipboard.readText('selection')?

https://www.electronjs.org/docs/latest/api/clipboard

qcgm1978 commented 7 months ago

So I think it is not supported that copy the file in finder(Mac folder) directly.

kaorahi commented 7 months ago

Would you try this in paste_sgf_or_url_from_clipboard()?

console.log(clipboard.availableFormats().map(fmt => [fmt, clipboard.read(fmt)]))

If we can obtain the full path of the file, it is better to open the file directly rather than to show an error message. We may need to use a library to detect the file type for this approach.

https://www.npmjs.com/package/file-type

qcgm1978 commented 7 months ago

Very correct, your words inspired me to find a solution. You need to first obtain the file path. The code is as follows:

function get_clipboard_path() {
    const buffer = clipboard.readBuffer('public.file-url')
    const filePath = buffer.toString('utf-8')
    return filePath
}
function paste_sgf_or_url_from_clipboard() {
    let s = get_clipboard_path()
    // the copied would be data instead of file if s is empty string by my observation
    if (s == '') {
        s = clipboard.readText(1);
        if (!clipboard.readImage().isEmpty()) {
            open_clipboard_image();
            return
        }
    }
    if (s.match('^(file|https?)://')) {
        open_url(s)
    } else {
        read_sgf(s)
    }
}
kaorahi commented 7 months ago

So could you test this code for the next release?

--- a/src/main.js
+++ b/src/main.js
@@ -2127,7 +2127,11 @@ function copy_sgf_to_clipboard(cache_suggestions_p) {
 }
 function paste_sgf_or_url_from_clipboard() {
     if (!clipboard.readImage().isEmpty()) {open_clipboard_image(); return}
-    const s = clipboard.readText(); s.match('^(file|https?)://') ? open_url(s) : read_sgf(s)
+    // clipboard.has and clipboard.read are experimental in Electron v28 [2024-02-02]
+    const read = format => clipboard.has(format) && clipboard.read(format)
+    const mac_url = 'public.file-url'  // for Cmd-C on Finder (Mac)
+    const s = read(mac_url) || clipboard.readText()
+    s.match('^(file|https?)://') ? open_url(s) : read_sgf(s)
 }

 function open_sgf_etc() {open_sgf_etc_in(option_path('sgf_dir'))}
qcgm1978 commented 7 months ago

clipboard.readImage().isEmpty() can't check whether the copied is img on mac. If the copied is a sgf file, this method !clipboard.readImage().isEmpty() would still return true. It may still need to check the result of read(mac_url). For example change to the following code:

function paste_sgf_or_url_from_clipboard() {
    // clipboard.has and clipboard.read are experimental in Electron v28 [2024-02-02]
    const read = format => clipboard.has(format) && clipboard.read(format)
    const mac_url = 'public.file-url'  // for Cmd-C on Finder (Mac)
    const mac_s = read(mac_url);
    const s = mac_s || clipboard.readText()
    if (mac_s=='' && !clipboard.readImage().isEmpty()) {open_clipboard_image(); return}
    s.match('^(file|https?)://') ? open_url(s) : read_sgf(s)
}
kaorahi commented 7 months ago

Thanks for pointing that out. Does this work for various cases?

--- a/src/main.js
+++ b/src/main.js
@@ -2126,6 +2126,9 @@ function copy_sgf_to_clipboard(cache_suggestions_p) {
     clipboard.writeText(game.to_sgf(cache_suggestions_p)); wink()
 }
 function paste_sgf_or_url_from_clipboard() {
+    // clipboard.read is experimental in Electron v28 [2024-02-02]
+    const mac_url = clipboard.read('public.file-url')  // for Cmd-C on Finder (Mac)
+    if (mac_url) {open_url(mac_url); return}
     if (!clipboard.readImage().isEmpty()) {open_clipboard_image(); return}
     const s = clipboard.readText(); s.match('^(file|https?)://') ? open_url(s) : read_sgf(s)
 }
qcgm1978 commented 7 months ago

Yes. All the five cases work.

kaorahi commented 7 months ago

fixed