Преглед на файлове

change to single generic select callback

Natan преди 1 година
родител
ревизия
3ed7c29580
променени са 4 файла, в които са добавени 12 реда и са изтрити 37 реда
  1. 1 6
      nicegui/elements/json_editor.js
  2. 8 17
      nicegui/elements/json_editor.py
  3. 2 12
      nicegui/events.py
  4. 1 2
      website/more_documentation/json_editor_documentation.py

+ 1 - 6
nicegui/elements/json_editor.js

@@ -7,12 +7,7 @@ export default {
       this.$emit("change", { content: updatedContent, errors: contentErrors });
     };
     this.properties.onSelect = (selection) => {
-      if (selection.type === "text") {
-        this.$emit("select_text", { main: selection.main, ranges: selection.ranges, type: selection.type });
-      }
-      if (selection.type === "key" || selection.type === "value") {
-        this.$emit("select_json", { edit: selection.edit, path: selection.path, type: selection.type });
-      }
+      this.$emit("select", { selection: selection });
     };
     this.editor = new JSONEditor({
       target: this.$el,

+ 8 - 17
nicegui/elements/json_editor.py

@@ -1,16 +1,14 @@
 from typing import Callable, Dict, Optional
 
 from ..element import Element
-from ..events import (GenericEventArguments, JsonEditorChangeEventArguments, JsonEditorSelectJsonEventArguments,
-                      JsonEditorSelectTextEventArguments, handle_event)
+from ..events import GenericEventArguments, JsonEditorChangeEventArguments, JsonEditorSelectEventArguments, handle_event
 
 
 class JsonEditor(Element, component='json_editor.js', exposed_libraries=['lib/vanilla-jsoneditor/index.js']):
 
     def __init__(self,
                  properties: Dict, *,
-                 on_select_json: Optional[Callable] = None,
-                 on_select_text: Optional[Callable] = None,
+                 on_select: Optional[Callable] = None,
                  on_change: Optional[Callable] = None,
                  ) -> None:
         """JSONEditor
@@ -20,25 +18,18 @@ class JsonEditor(Element, component='json_editor.js', exposed_libraries=['lib/va
         After data has changed, call the `update` method to refresh the editor.
 
         :param properties: dictionary of JSONEditor properties
-        :param on_select_json: callback function that is called when a JSON path is selected
-        :param on_select_text: callback function that is called when text is selected
+        :param on_select: callback function that is called when the editor's content has been selected
         :param on_change: callback function that is called when the editor's content has changed
         """
         super().__init__()
         self._props['properties'] = properties
         self._classes = ['nicegui-json-editor']
 
-        if on_select_json:
-            def handle_select_json(e: GenericEventArguments) -> None:
-                handle_event(on_select_json,
-                             JsonEditorSelectJsonEventArguments(sender=self, client=self.client, **e.args))
-            self.on('select_json', handle_select_json, ['edit', 'path', 'type'])
-
-        if on_select_text:
-            def handle_on_select_text(e: GenericEventArguments) -> None:
-                handle_event(on_select_text,
-                             JsonEditorSelectTextEventArguments(sender=self, client=self.client, **e.args))
-            self.on('select_text', handle_on_select_text, ['main', 'ranges', 'type'])
+        if on_select:
+            def handle_on_select(e: GenericEventArguments) -> None:
+                handle_event(on_select,
+                             JsonEditorSelectEventArguments(sender=self, client=self.client, **e.args))
+            self.on('select', handle_on_select, ['selection'])
 
         if on_change:
             def handle_on_change(e: GenericEventArguments) -> None:

+ 2 - 12
nicegui/events.py

@@ -352,18 +352,8 @@ class ScrollEventArguments(UiEventArguments):
 
 
 @dataclass(**KWONLY_SLOTS)
-class JsonEditorSelectJsonEventArguments(UiEventArguments):
-    edit: bool
-    path: Dict
-    type: str
-
-
-@dataclass(**KWONLY_SLOTS)
-class JsonEditorSelectTextEventArguments(UiEventArguments):
-    main: int
-    ranges: Dict
-    type: str
-
+class JsonEditorSelectEventArguments(UiEventArguments):
+    selection: Dict
 
 @dataclass(**KWONLY_SLOTS)
 class JsonEditorChangeEventArguments(UiEventArguments):

+ 1 - 2
website/more_documentation/json_editor_documentation.py

@@ -13,6 +13,5 @@ def main_demo() -> None:
         'string': 'Hello World'
     }
     ui.json_editor({'content': {'json': json}},
-                   on_select_json=lambda e: ui.notify(f'Select JSON: {e}'),
-                   on_select_text=lambda e: ui.notify(f'Select text: {e}'),
+                   on_select=lambda e: ui.notify(f'Select: {e}'),
                    on_change=lambda e: ui.notify(f'Change: {e}')).classes('h-80')