derek-thomas HF staff commited on
Commit
823c3ea
1 Parent(s): 9df958d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -9,10 +9,9 @@ snippet2 = """<div style="background-color: #e6f9e6; padding: 16px 32px; outline
9
  This is text with a tip format!
10
  </div>
11
  """
 
12
 
13
- # Function to insert snippet at a given position
14
- def insert_snippet(text, snippet, position):
15
- return text[:position] + snippet + text[position:]
16
 
17
  # JavaScript to get and set cursor position
18
  js_code = """
@@ -29,11 +28,15 @@ js_code = """
29
  textbox.value = newText;
30
  position += snippet.length;
31
  textbox.focus();
 
 
 
32
  }
33
  document.querySelector('textarea').addEventListener('click', updatePosition);
34
  document.querySelector('textarea').addEventListener('keyup', updatePosition);
35
- document.querySelector('#button1').addEventListener('click', function() { insertAtCursor("This is the first predefined text snippet."); });
36
- document.querySelector('#button2').addEventListener('click', function() { insertAtCursor("This is the second predefined text snippet."); });
 
37
  </script>
38
  """
39
 
@@ -44,7 +47,8 @@ with gr.Blocks() as demo:
44
  with gr.Column():
45
  text_input = gr.Textbox(lines=10, placeholder="Write your blog here...", label="Text Editor")
46
  button1 = gr.Button("Insert Image Table", elem_id="button1")
47
- button2 = gr.Button("Insert Snippet 2", elem_id="button2")
 
48
  with gr.Column():
49
  gr.Markdown("**Live Preview**")
50
  markdown_preview = gr.Markdown(label="Live Preview", elem_id="preview-box")
 
9
  This is text with a tip format!
10
  </div>
11
  """
12
+ snippet3 = """[[1]](#1)
13
 
14
+ <a id="1">[1]</a> : First Last, [Example](www.example.com), 2021"""
 
 
15
 
16
  # JavaScript to get and set cursor position
17
  js_code = """
 
28
  textbox.value = newText;
29
  position += snippet.length;
30
  textbox.focus();
31
+ // Triggering input event to update Gradio component
32
+ let event = new Event('input', { bubbles: true });
33
+ textbox.dispatchEvent(event);
34
  }
35
  document.querySelector('textarea').addEventListener('click', updatePosition);
36
  document.querySelector('textarea').addEventListener('keyup', updatePosition);
37
+ document.querySelector('#button1').addEventListener('click', function() { insertAtCursor(`|![Link Text](www.example.com)|\n|:--:|\n|Figure x: Caption|\n`); });
38
+ document.querySelector('#button2').addEventListener('click', function() { insertAtCursor(`<div style="background-color: #e6f9e6; padding: 16px 32px; outline: 2px solid; border-radius: 10px;">\nThis is text with a tip format!\n</div>\n`); });
39
+ document.querySelector('#button3').addEventListener('click', function() { insertAtCursor(`<a id="1">[1]</a> : First Last, [Example](www.example.com), 2021\n`); });
40
  </script>
41
  """
42
 
 
47
  with gr.Column():
48
  text_input = gr.Textbox(lines=10, placeholder="Write your blog here...", label="Text Editor")
49
  button1 = gr.Button("Insert Image Table", elem_id="button1")
50
+ button2 = gr.Button("Insert Tip Format", elem_id="button2")
51
+ button3 = gr.Button("Insert Reference", elem_id="button3")
52
  with gr.Column():
53
  gr.Markdown("**Live Preview**")
54
  markdown_preview = gr.Markdown(label="Live Preview", elem_id="preview-box")