mabuseif commited on
Commit
e4f4c54
·
verified ·
1 Parent(s): 4fdac7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -34
app.py CHANGED
@@ -127,8 +127,10 @@ def check_for_fragment(url):
127
 
128
  def copy_to_clipboard_js(text, button_id):
129
  """Generate JavaScript for copying HTML content to clipboard."""
130
- # Encode the text as a JavaScript-safe string
131
- escaped_text = json.dumps(text, ensure_ascii=False)[1:-1].replace('</', '<\\/')
 
 
132
  return f"""
133
  <div>
134
  <textarea id="copy_text_{button_id}" style="display: none;">{escaped_text}</textarea>
@@ -139,47 +141,28 @@ def copy_to_clipboard_js(text, button_id):
139
  function copyToClipboard_{button_id}() {{
140
  console.log('Copy function triggered for {button_id}');
141
  const textElement = document.getElementById('copy_text_{button_id}');
142
- const htmlToCopy = textElement ? textElement.value : '';
143
- if (!htmlToCopy) {{
144
  console.error('No text to copy for {button_id}');
145
  const statusElement = document.getElementById('copy_status_{button_id}');
146
  if (statusElement) statusElement.innerHTML = 'No text to copy';
147
  return;
148
  }}
149
- // Create a temporary div to hold the HTML content
150
- const tempDiv = document.createElement('div');
151
- tempDiv.innerHTML = htmlToCopy;
152
- document.body.appendChild(tempDiv);
153
-
154
- // Select the content
155
- const range = document.createRange();
156
- range.selectNodeContents(tempDiv);
157
- const selection = window.getSelection();
158
- selection.removeAllRanges();
159
- selection.addRange(range);
160
-
161
- // Copy to clipboard as HTML
162
- try {{
163
- const successful = document.execCommand('copy');
164
- console.log('Copy command executed for {button_id}: ' + (successful ? 'success' : 'failed'));
165
  const statusElement = document.getElementById('copy_status_{button_id}');
166
  if (statusElement) {{
167
- statusElement.innerHTML = successful ? 'Copied!' : 'Copy failed';
168
- if (successful) {{
169
- setTimeout(function() {{
170
- statusElement.innerHTML = '';
171
- }}, 2000);
172
- }}
173
  }}
174
- }} catch (err) {{
175
  console.error('Copy failed for {button_id}: ', err);
176
  const statusElement = document.getElementById('copy_status_{button_id}');
177
  if (statusElement) statusElement.innerHTML = 'Copy failed';
178
- }}
179
-
180
- // Clean up
181
- selection.removeAllRanges();
182
- document.body.removeChild(tempDiv);
183
  }}
184
  </script>
185
  """
@@ -322,8 +305,8 @@ with tabs[0]:
322
  """, unsafe_allow_html=True)
323
  else:
324
  scc_hash = generate_citation_hash(author_name, publication_year, source_url, annotated_text, annotated_text, username, task_name, current_date, current_time)
325
- citation_link_start = f'<a href="{source_url}#:~:text={urllib.parse.quote(annotated_text)}" data-hash="{scc_hash}">{author_name} ({publication_year})</a>'
326
- citation_link_end = f'<a href="{source_url}#:~:text={urllib.parse.quote(annotated_text)}" data-hash="{scc_hash}">({author_name}, {publication_year})</a>'
327
 
328
  st.markdown("## Generated Citations")
329
  col_html1, col_html2 = st.columns(2)
 
127
 
128
  def copy_to_clipboard_js(text, button_id):
129
  """Generate JavaScript for copying HTML content to clipboard."""
130
+ # Create a clean text version for copying (remove HTML tags and keep only visible text)
131
+ clean_text = f"{text.split('>')[1].split('<')[0]}"
132
+ # Encode the clean text as a JavaScript-safe string
133
+ escaped_text = json.dumps(clean_text, ensure_ascii=False)[1:-1].replace('</', '<\\/')
134
  return f"""
135
  <div>
136
  <textarea id="copy_text_{button_id}" style="display: none;">{escaped_text}</textarea>
 
141
  function copyToClipboard_{button_id}() {{
142
  console.log('Copy function triggered for {button_id}');
143
  const textElement = document.getElementById('copy_text_{button_id}');
144
+ const textToCopy = textElement ? textElement.value : '';
145
+ if (!textToCopy) {{
146
  console.error('No text to copy for {button_id}');
147
  const statusElement = document.getElementById('copy_status_{button_id}');
148
  if (statusElement) statusElement.innerHTML = 'No text to copy';
149
  return;
150
  }}
151
+ // Use navigator.clipboard API for plain text copying
152
+ navigator.clipboard.writeText(textToCopy).then(() => {{
153
+ console.log('Text copied successfully for {button_id}');
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  const statusElement = document.getElementById('copy_status_{button_id}');
155
  if (statusElement) {{
156
+ statusElement.innerHTML = 'Copied!';
157
+ setTimeout(() => {{
158
+ statusElement.innerHTML = '';
159
+ }}, 2000);
 
 
160
  }}
161
+ }}).catch(err => {{
162
  console.error('Copy failed for {button_id}: ', err);
163
  const statusElement = document.getElementById('copy_status_{button_id}');
164
  if (statusElement) statusElement.innerHTML = 'Copy failed';
165
+ }});
 
 
 
 
166
  }}
167
  </script>
168
  """
 
305
  """, unsafe_allow_html=True)
306
  else:
307
  scc_hash = generate_citation_hash(author_name, publication_year, source_url, annotated_text, annotated_text, username, task_name, current_date, current_time)
308
+ citation_link_start = format_citation_html(source_url, annotated_text, author_name, publication_year, scc_hash)
309
+ citation_link_end = format_citation_html(source_url, annotated_text, f"({author_name}, {publication_year})", publication_year, scc_hash)
310
 
311
  st.markdown("## Generated Citations")
312
  col_html1, col_html2 = st.columns(2)