File size: 6,716 Bytes
03d0e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3

import gradio as gr
from gradio_markdownlabel import MarkdownLabel

# Sample data for the editable demo
sample_data = {
    "markdown_content": """# Editable Document Example

## Introduction

This document demonstrates the **editable functionality** of the MarkdownLabel component. You can click the "Edit" button to modify this content.

## Features

- **Real-time editing**: Changes appear live in preview mode
- **Highlight preservation**: Existing highlights remain intact
- **Multiple edit modes**: Split view, tabs, and overlay options
- Position-based highlighting support

## Try Editing

1. Click the **Edit** button above
2. Modify this text in the editor
3. See live preview (in split mode)
4. Save or cancel your changes

Feel free to experiment with the content!
""",
    "highlights": [
        {
            "term": "editable functionality",
            "title": "Editable Functionality",
            "content": "This feature allows users to modify markdown content directly in the interface with real-time preview.",
            "category": "feature",
            "color": "#e3f2fd"
        },
        {
            "position": [200, 220],  # "MarkdownLabel component"
            "title": "MarkdownLabel Component",
            "content": "The main component that renders markdown with interactive highlights and editing capabilities.",
            "category": "component",
            "color": "#f3e5f5"
        },
        {
            "term": "Real-time editing",
            "title": "Real-time Editing",
            "content": "Changes in the editor are immediately reflected in the preview pane, providing instant feedback.",
            "category": "feature",
            "color": "#e8f5e8"
        },
        {
            "position": [450, 470],  # "Split view, tabs, and"
            "title": "Edit Modes",
            "content": "Different layout options for the editing interface: split view shows editor and preview side-by-side, tabs separate them, and overlay mode provides full-screen editing.",
            "category": "ui",
            "color": "#fff3e0"
        }
    ]
}

def handle_content_change(value):
    """Handle content changes (only on save)"""
    print(f"Content saved: {len(value['markdown_content'])} characters")
    return value

def handle_edit_start(value):
    """Handle when user starts editing"""
    print("User started editing")

def handle_save(value):
    """Handle when user saves changes"""
    print("Changes saved!")
    gr.Info("Document saved successfully!")
    return value

def handle_cancel(value):
    """Handle when user cancels editing"""
    print("Edit cancelled")
    gr.Info("Changes cancelled")
    return gr.update()

def load_sample_content():
    """Load sample content"""
    # Return data for both components (split and tabs editors)
    return sample_data, sample_data


with gr.Blocks(title="Editable MarkdownLabel Demo") as demo:
    gr.Markdown("# Editable MarkdownLabel Component Demo")
    gr.Markdown("This demo showcases the **interactive editing capabilities** of the MarkdownLabel component.")
    
    with gr.Row():
        with gr.Column(scale=2):
            gr.Markdown("## Split View Mode (Default)")
            gr.Markdown("Editor and preview side-by-side when editing.")
            
            editor_split = MarkdownLabel(
                value=sample_data,
                interactive=True,
                edit_mode="split",
                show_preview=True,
                label="Split View Editor",
                show_side_panel=True,
                panel_width="300px"
            )
            
            # Event handlers for split view
            # Note: Removed real-time change handler to prevent infinite loops
            # Changes are now handled only on explicit save via submit event
            editor_split.edit(handle_edit_start, inputs=[editor_split])
            editor_split.submit(handle_save, inputs=[editor_split], outputs=[editor_split])
            editor_split.clear(handle_cancel, inputs=[editor_split])
            
        with gr.Column(scale=1):
            gr.Markdown("## Tab Mode")
            gr.Markdown("Switch between edit and preview tabs.")
            
            editor_tabs = MarkdownLabel(
                value=sample_data,
                interactive=True,
                edit_mode="tabs",
                show_preview=True,
                label="Tab View Editor",
                show_side_panel=False
            )
            
            # Event handlers for tab view
            # Note: Only handling submit (save) events to prevent loops
            editor_tabs.submit(handle_save, inputs=[editor_tabs], outputs=[editor_tabs])
    
    with gr.Row():
        gr.Markdown("## Control Buttons")
        
    with gr.Row():
        load_btn = gr.Button("πŸ“„ Load Sample", variant="secondary")
        
    # Button event handlers
    load_btn.click(load_sample_content, outputs=[editor_split, editor_tabs])
    
    with gr.Row():
        gr.Markdown("## Non-Interactive (Read-Only) Version")
        
    # Read-only version for comparison
    readonly_viewer = MarkdownLabel(
        value=sample_data,
        interactive=False,  # Read-only mode
        label="Read-Only Viewer",
        show_side_panel=True,
        panel_width="250px"
    )
    
    gr.Markdown("""
    ## How to Use
    
    1. **Start Editing**: Click the "✏️ Edit" button on any interactive component
    2. **Edit Content**: Modify the markdown text in the editor
    3. **Live Preview**: See changes in real-time (split mode) or switch to preview tab
    4. **Save Changes**: Click "πŸ’Ύ Save" to confirm and apply your changes
    5. **Cancel Changes**: Click "❌ Cancel" to discard changes and revert
    6. **Interact with Highlights**: Click on highlighted terms to see details in the side panel
    
    ## Features Demonstrated
    
    - βœ… **Interactive editing** with explicit save/cancel workflow
    - βœ… **Multiple edit modes**: Split view and tabs
    - βœ… **Live preview** with real-time markdown rendering (visual only)
    - βœ… **Highlight preservation** during editing
    - βœ… **Manual save workflow** - changes are applied only when you save
    - βœ… **Event handling** for edit, save (submit), and cancel events
    - βœ… **Mixed highlighting** with both term-based and position-based highlights
    
    ## Important Notes
    
    - **Changes are NOT auto-saved** - you must click "πŸ’Ύ Save" to apply changes
    - **Live preview** is for visual feedback only - actual content updates on save
    - **Cancel** reverts all changes made since editing started
    """)

if __name__ == "__main__":
    demo.launch()