Skip to content

Commit

Permalink
Add callback for onChangeText to jsoneditor.ts (#7610)
Browse files Browse the repository at this point in the history
* Add callback for jsoneditor.onChangeText

* Test jsoneditor editing while in text mode

* Fix lint issue
  • Loading branch information
jonatantreijs authored Jan 10, 2025
1 parent 8662665 commit 6544d78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions panel/models/jsoneditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export class JSONEditorView extends HTMLBoxView {
onChangeJSON: (json: any) => {
this.model.data = json
},
onChangeText: (text: any) => {
try {
this.model.data = JSON.parse(text)
} catch (e) {
console.warn(e)
}
},
onSelectionChange: (start: any, end: any) => {
this.model.selection = [start, end]
},
Expand Down
16 changes: 16 additions & 0 deletions panel/tests/ui/widgets/test_jsoneditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ def test_json_editor_edit(page):
page.locator('.jsoneditor').click()

wait_until(lambda: editor.value['str'] == 'new', page)

def test_json_editor_edit_in_text_mode(page):
editor = JSONEditor(value={'str': 'string', 'int': 1}, mode='text')

msgs, _ = serve_component(page, editor)

expect(page.locator('.jsoneditor')).to_have_count(1)

page.locator('.jsoneditor-text').click()
ctrl_key = 'Meta' if sys.platform == 'darwin' else 'Control'
page.keyboard.press(f'{ctrl_key}+A')
page.keyboard.press('Backspace')
page.keyboard.type('{"str": "new"}')
page.locator('.jsoneditor').click()

wait_until(lambda: editor.value['str'] == 'new', page)

0 comments on commit 6544d78

Please sign in to comment.