derek-thomas commited on
Commit
466dfc4
·
verified ·
1 Parent(s): b1be69e

Adding box

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -4,14 +4,9 @@ import gradio as gr
4
  snippet1 = "This is the first predefined text snippet."
5
  snippet2 = "This is the second predefined text snippet."
6
 
7
- def insert_snippet1(text):
8
- return text + snippet1
9
-
10
- def insert_snippet2(text):
11
- return text + snippet2
12
-
13
- def preview_text(text):
14
- return text
15
 
16
  with gr.Blocks() as demo:
17
  gr.Markdown("# Blog Writing Helper")
@@ -22,10 +17,31 @@ with gr.Blocks() as demo:
22
  button1 = gr.Button("Insert Snippet 1")
23
  button2 = gr.Button("Insert Snippet 2")
24
  with gr.Column():
25
- markdown_preview = gr.Markdown(label="Live Preview")
 
 
 
 
 
 
26
 
27
- button1.click(insert_snippet1, inputs=text_input, outputs=text_input)
28
- button2.click(insert_snippet2, inputs=text_input, outputs=text_input)
29
- text_input.change(preview_text, inputs=text_input, outputs=markdown_preview)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  demo.launch()
 
4
  snippet1 = "This is the first predefined text snippet."
5
  snippet2 = "This is the second predefined text snippet."
6
 
7
+ # Function to insert snippet at a given position
8
+ def insert_snippet(text, snippet, position):
9
+ return text[:position] + snippet + text[position:]
 
 
 
 
 
10
 
11
  with gr.Blocks() as demo:
12
  gr.Markdown("# Blog Writing Helper")
 
17
  button1 = gr.Button("Insert Snippet 1")
18
  button2 = gr.Button("Insert Snippet 2")
19
  with gr.Column():
20
+ gr.Markdown("**Live Preview**")
21
+ with gr.Box():
22
+ markdown_preview = gr.Markdown(label="Live Preview")
23
+
24
+ button1.click(lambda text, position: insert_snippet(text, snippet1, position), inputs=[text_input, gr.State(0)], outputs=text_input)
25
+ button2.click(lambda text, position: insert_snippet(text, snippet2, position), inputs=[text_input, gr.State(0)], outputs=text_input)
26
+ text_input.change(lambda text: text, inputs=text_input, outputs=markdown_preview)
27
 
28
+ # Add custom JavaScript to get cursor position
29
+ js_code = """
30
+ <script>
31
+ let textbox = document.querySelector('textarea');
32
+ let position = 0;
33
+
34
+ textbox.addEventListener('click', () => {
35
+ position = textbox.selectionStart;
36
+ });
37
+
38
+ textbox.addEventListener('keyup', () => {
39
+ position = textbox.selectionStart;
40
+ });
41
+
42
+ gradio.input('state', position);
43
+ </script>
44
+ """
45
+ gr.HTML(js_code)
46
 
47
  demo.launch()