Code Monkey home page Code Monkey logo

Comments (5)

frankvp11 avatar frankvp11 commented on July 19, 2024 1

To help us all, I've figured out what @tjongsma wanted (in brief)

from nicegui import ui

@ui.page("/")
def main():
    flag = False
    def handle_event(e):
        nonlocal flag
        if flag:
            editor.set_value("")
            flag = not flag
        else:
            editor.set_value(None)
            flag = not flag
    
        editor.update()

    def print_dict():
        print(editor._to_dict()['props'])


    editor = ui.editor().on('keypress.enter', handle_event)
    ui.button('Print dict', on_click=print_dict)

ui.run()

We should look into seeing if there might be a better way to handle the setting of None for the editor. Perhaps we should into some core functionality of the ValueElement and how it handles None. The reason that I say this is because despite the fact that this fix that I gave above does technically work, it doens't solve the underlying issue which is that the props aren't properly being cleared. Despite this fix somehow working, after typing some text, then clearing it from the editor, when I run the print_dict() function by the button press, it still shows the editor as having the old value in the model-value. This is something that should be fixed.

from nicegui.

frankvp11 avatar frankvp11 commented on July 19, 2024

This is quite an interesting problem.
Something deeper is going on than just the editor....

from nicegui import ui


def handle_event1():
    editor.set_value(None)
    print(editor._props)


def handle_event2():
    print(editor._props)


editor = ui.editor().on("keypress.enter", lambda: handle_event1())
ui.button("Clear", on_click=handle_event2)


ui.run()

When I run something like this, I can see that the props aren't actually being updated...
When I type something, then press enter, then press the button, it shows me that the model-value is still set to the text I typed.
This is the reason that when you press enter on the second time it doesn't remove it, or atleast that's what I'm thinking.

I think solving this issue will come down to figuring out why the props aren't actually updated

from nicegui.

tjongsma avatar tjongsma commented on July 19, 2024

from nicegui.

frankvp11 avatar frankvp11 commented on July 19, 2024

I think I've figured out the bug, and I don't believe that it's Nicegui's fault.
I would appreciate if someone could verify this somehow, but basically, here is what I think.
TLDR; race condition.
Basically, when you press the enter key, it sends two events. One get's senter to the editor element which thinks that you want to place content on the newline. The other get's picked up by us setting the content to None. The content first get's set to None, then the event get's registered from the element itself and it tries to set the content to the previous, with the newline.
This causes a race condition in some sense because it has to do with the timing.
This is what happens when I print out the contents of the message here

{'id': 4, 'client_id': 'a48113b2-a22d-427d-8bbf-601e4e0500fe', 'listener_id': '650d15a8-6afe-43bf-bfc5-72aa7bdb6627', 'args': 'a'}
{'id': 4, 'client_id': 'a48113b2-a22d-427d-8bbf-601e4e0500fe', 'listener_id': '72ed4040-9518-4a02-b7f1-b13400fca0a5', 'args': {'isTrusted': True, '_vts': 1720035544085, 'key': 'Enter', 'code': 'Enter', 'location': 0, 'ctrlKey': False, 'shiftKey': False, 'altKey': False, 'metaKey': False, 'repeat': False, 'isComposing': False, 'charCode': 13, 'keyCode': 13, 'DOM_KEY_LOCATION_STANDARD': 0, 'DOM_KEY_LOCATION_LEFT': 1, 'DOM_KEY_LOCATION_RIGHT': 2, 'DOM_KEY_LOCATION_NUMPAD': 3, 'detail': 0, 'sourceCapabilities': {}, 'which': 13, 'type': 'keypress', 'eventPhase': 2, 'bubbles': True, 'cancelable': True, 'defaultPrevented': False, 'composed': True, 'timeStamp': 2796.5999999940395, 'returnValue': True, 'cancelBubble': False, 'NONE': 0, 'CAPTURING_PHASE': 1, 'AT_TARGET': 2, 'BUBBLING_PHASE': 3}}
{'id': 4, 'client_id': 'a48113b2-a22d-427d-8bbf-601e4e0500fe', 'listener_id': '72ed4040-9518-4a02-b7f1-b13400fca0a5', 'args': {'isTrusted': True, '_vts': 1720035544085, 'key': 'Enter', 'code': 'Enter', 'location': 0, 'ctrlKey': False, 'shiftKey': False, 'altKey': False, 'metaKey': False, 'repeat': False, 'isComposing': False, 'charCode': 13, 'keyCode': 13, 'DOM_KEY_LOCATION_STANDARD': 0, 'DOM_KEY_LOCATION_LEFT': 1, 'DOM_KEY_LOCATION_RIGHT': 2, 'DOM_KEY_LOCATION_NUMPAD': 3, 'detail': 0, 'sourceCapabilities': {}, 'which': 13, 'type': 'keypress', 'eventPhase': 3, 'bubbles': True, 'cancelable': True, 'defaultPrevented': False, 'composed': True, 'timeStamp': 2796.5999999940395, 'returnValue': True, 'cancelBubble': False, 'NONE': 0, 'CAPTURING_PHASE': 1, 'AT_TARGET': 2, 'BUBBLING_PHASE': 3}}
{'id': 4, 'client_id': 'a48113b2-a22d-427d-8bbf-601e4e0500fe', 'listener_id': '650d15a8-6afe-43bf-bfc5-72aa7bdb6627', 'args': 'a<div><br></div>'}

Note that the previous code block is the result of me pressing the letter 'a' once, then enter.

So how do we fix this?
Ultimately, I have no idea. We need to change the way that event handlers are ordered, and I'm not sure of how it's currently being done.

from nicegui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.