Skip to content

Insert text shortcode at cursor in codemirror #4311

Answered by falkoschindler
arunaseva asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @arunaseva,

You can use NiceGUI's helper function getElement to get a Vue element by ID. But the methods getCursor and replaceRange don't seem to work, so I improvised with the help of some AI:

editor = ui.codemirror("A lot of text")

ui.button("Insert shortcode HELLO at cursor", on_click=lambda: ui.run_javascript(f"""
    const view = getElement("{editor.id}").editor;
    const cursor = view.state.selection.main.head
    view.dispatch(view.state.update({{
        changes: {{from: cursor, insert: "[HELLO]"}}
    }}));
"""))

By the way, JavaScript doesn't care about code indentation. So you don't need to remove the indentation.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@arunaseva
Comment options

Answer selected by arunaseva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants