Prevent undo after programically setting codemirror value #5558
-
First Check
Example Codefrom nicegui import ui
LORUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n"
def clear_history(editor):
ui.run_javascript(f'''
const element = getElement({editor.id});
const view = element.editor;
if (view) {{
const CM = await import('nicegui-codemirror');
// Get current content
const currentContent = view.state.doc.toString();
// First, mark a history boundary
view.dispatch({{
annotations: [
CM.isolateHistory.of("full"),
CM.Transaction.addToHistory.of(true),
]
}});
// Replace document with itself, marking it as not part of history
// and isolating history on both sides
view.dispatch({{
changes: {{ from: 0, to: view.state.doc.length, insert: currentContent }},
annotations: [
CM.Transaction.addToHistory.of(false),
CM.isolateHistory.of("full")
]
}});
}}
''')
def undo(editor: ui.codemirror):
ui.run_javascript(f'''
const element = getElement({editor.id});
const view = element.editor;
if (view) {{
const CM = await import('nicegui-codemirror');
console.log("Attempting undo");
const result = CM.undo(view);
console.log("Undo result:", result);
}}
''')
editor = ui.codemirror(line_wrapping=True)
ui.button("do", on_click=lambda: do())
ui.button("set text", on_click=lambda: editor.set_value(LORUM * 2))
editor.value = "initial text"
ui.run(show=False)
async def do():
editor.value = LORUM
clear_history(editor)
undo() # simulate CTRL-ZDescriptionI have a use case to read, modify and save files using ui.codemirror. I use the same codemirror instance and when a new file is read, I change it with This was my attempt to reset the undo history of codemirror, but when the value is set programically, the original value is appended to the end. ("Lorum ipsum..." becomes "Lorum ipsum... initial value"). How can I prevent users undoing programical changes, without preventing them from undoing their own changes. NiceGUI Version3.2.0 Python Version3.12 BrowserChrome, Other Operating SystemWindows Additional ContextI will be using native mode |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
Hello @ZeroPoint095 Sorry for sounding dumb but what about just destroy the old codemirror instance and start off with a new one? from nicegui import ui
@ui.page('/')
def page():
with ui.column().classes('w-full') as editor_container:
editor = ui.codemirror('initial', line_wrapping=True)
def force_set_text(text: str):
nonlocal editor
editor_container.clear()
with editor_container:
editor = ui.codemirror(text, line_wrapping=True)
ui.button('set text', on_click=lambda: editor.set_value('set text')) # can undo
ui.button('force set text', on_click=lambda: force_set_text('force set text')) # cannot undo
ui.run(show=False)Blunt-knife solution, but a blunt knife is still a knife I guess 😉 |
Beta Was this translation helpful? Give feedback.
-
|
This almost works: LORUM = 'Lorem ipsum dolor sit amet'
editor = ui.codemirror(line_wrapping=True)
ui.button('Set text (with history)', on_click=lambda: editor.set_value(LORUM))
ui.button('Set text (without history)', on_click=lambda: ui.run_javascript(f'''
const element = getElement({editor.id});
const view = element.editor;
if (view) {{
const CM = await import('nicegui-codemirror');
const newValue = {json.dumps(LORUM)};
// Set value without adding to history
element.emitting = false;
view.dispatch({{
changes: {{ from: 0, to: view.state.doc.length, insert: newValue }},
annotations: [CM.Transaction.addToHistory.of(false)]
}});
element.emitting = true;
}}
'''))But in some edge cases, which I don't understand yet, text is appended. 🫤 |
Beta Was this translation helpful? Give feedback.
-
|
I bit the bullet and decided to refactor my code to re-init the codemirror object as @evnchn suggested. A more elegant solution might be a nice-to-have for future NiceGUI releases, but I won't need one atm. |
Beta Was this translation helpful? Give feedback.
Hello @ZeroPoint095
Sorry for sounding dumb but what about just destroy the old codemirror instance and start off with a new one?
Blun…