dina1 commited on
Commit
4aae82b
·
verified ·
1 Parent(s): 45bb7ad

Update playwright_model.py

Browse files
Files changed (1) hide show
  1. playwright_model.py +79 -18
playwright_model.py CHANGED
@@ -77,13 +77,87 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
77
  except Exception as e:
78
  print(f"[WARN] Sidebar fix failed: {e}")
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  # Wait for sidebar to appear
81
  await page.wait_for_selector("aside, .sidebar, nav", timeout=5000)
82
  await wait_for_layout_stable()
83
  await fix_sidebar_permanently()
84
  await asyncio.sleep(1)
85
 
86
- # JS to enumerate workflows and highlight active menu item
87
  js_logic = """
88
  (function(){
89
  const menus=[...document.querySelectorAll('.menu-item')];
@@ -109,23 +183,6 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
109
  window.__currentIndex=0;
110
  window.__done=false;
111
 
112
- // Add CSS for active highlight
113
- const highlightCSS = document.createElement('style');
114
- highlightCSS.innerHTML = `
115
- .menu-item.active {
116
- background-color: #007bff !important;
117
- color: #fff !important;
118
- border-radius: 4px;
119
- transition: background-color 0.3s ease;
120
- }
121
- .tab.active, .nav-link.active, [role="tab"].active, .sub-tab.active, .tab-item.active {
122
- background-color: #007bff !important;
123
- color: #fff !important;
124
- border-radius: 4px;
125
- }
126
- `;
127
- document.head.appendChild(highlightCSS);
128
-
129
  window.__getSubScreens = function(screen){
130
  const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
131
  const list=[];
@@ -187,10 +244,12 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
187
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
188
  print(f"📸 Capturing main screen: {screen_name}")
189
 
 
190
  await wait_for_layout_stable()
191
  await fix_sidebar_permanently()
192
  await asyncio.sleep(0.8)
193
  await page.screenshot(path=screenshot_path, full_page=True)
 
194
  screenshots.append(screenshot_path)
195
 
196
  first_active_skipped = False
@@ -213,11 +272,13 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
213
  await page.evaluate(f"window.__clickSubScreen('{sub}')")
214
  await wait_for_layout_stable()
215
  await fix_sidebar_permanently()
 
216
  await asyncio.sleep(0.8)
217
 
218
  sub_name_clean = sub.replace(" ", "_").lower()
219
  sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
220
  await page.screenshot(path=sub_path, full_page=True)
 
221
  screenshots.append(sub_path)
222
 
223
  index += 1
 
77
  except Exception as e:
78
  print(f"[WARN] Sidebar fix failed: {e}")
79
 
80
+ # Highlight sidebar item for current screen
81
+ async def highlight_sidebar(screen_name):
82
+ try:
83
+ await page.evaluate(f"""
84
+ (() => {{
85
+ const name = `{screen_name}`.toLowerCase().trim();
86
+ const sidebarItems = document.querySelectorAll('aside a, nav a, .sidebar a, li a');
87
+
88
+ // Remove any previous highlights
89
+ sidebarItems.forEach(item => {{
90
+ item.style.backgroundColor = '';
91
+ item.style.color = '';
92
+ item.style.fontWeight = '';
93
+ }});
94
+
95
+ let matched = null;
96
+ for (const item of sidebarItems) {{
97
+ const text = (item.textContent || '').toLowerCase().trim();
98
+ if (name.includes(text) || text.includes(name)) {{
99
+ matched = item;
100
+ break;
101
+ }}
102
+ }}
103
+
104
+ // Fallback mapping for alternate names
105
+ if (!matched) {{
106
+ const mapping = {{
107
+ 'my dashboard': 'dashboard',
108
+ 'dashboard': 'dashboard',
109
+ 'my tasks': 'activity',
110
+ 'tasks': 'activity',
111
+ 'activities': 'activity',
112
+ 'requisitions': 'requisition',
113
+ 'new requisition': 'requisition'
114
+ }};
115
+ const keyword = mapping[name];
116
+ if (keyword) {{
117
+ for (const item of sidebarItems) {{
118
+ const text = (item.textContent || '').toLowerCase().trim();
119
+ if (text.includes(keyword)) {{
120
+ matched = item;
121
+ break;
122
+ }}
123
+ }}
124
+ }}
125
+ }}
126
+
127
+ if (matched) {{
128
+ matched.style.backgroundColor = '#0078D4';
129
+ matched.style.color = 'white';
130
+ matched.style.fontWeight = 'bold';
131
+ matched.scrollIntoView({{ behavior: 'smooth', block: 'center' }});
132
+ }}
133
+ }})();
134
+ """)
135
+ except Exception as e:
136
+ print(f"[WARN] Sidebar highlight failed for {screen_name}: {e}")
137
+
138
+ # Reset sidebar highlights
139
+ async def reset_sidebar_highlight():
140
+ try:
141
+ await page.evaluate("""
142
+ (() => {
143
+ const sidebarItems = document.querySelectorAll('aside a, nav a, .sidebar a, li a');
144
+ sidebarItems.forEach(item => {
145
+ item.style.backgroundColor = '';
146
+ item.style.color = '';
147
+ item.style.fontWeight = '';
148
+ });
149
+ })();
150
+ """)
151
+ except Exception as e:
152
+ print(f"[WARN] Sidebar reset failed: {e}")
153
+
154
  # Wait for sidebar to appear
155
  await page.wait_for_selector("aside, .sidebar, nav", timeout=5000)
156
  await wait_for_layout_stable()
157
  await fix_sidebar_permanently()
158
  await asyncio.sleep(1)
159
 
160
+ # JS to enumerate workflows and tabs
161
  js_logic = """
162
  (function(){
163
  const menus=[...document.querySelectorAll('.menu-item')];
 
183
  window.__currentIndex=0;
184
  window.__done=false;
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  window.__getSubScreens = function(screen){
187
  const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
188
  const list=[];
 
244
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
245
  print(f"📸 Capturing main screen: {screen_name}")
246
 
247
+ await highlight_sidebar(screen_name)
248
  await wait_for_layout_stable()
249
  await fix_sidebar_permanently()
250
  await asyncio.sleep(0.8)
251
  await page.screenshot(path=screenshot_path, full_page=True)
252
+ await reset_sidebar_highlight()
253
  screenshots.append(screenshot_path)
254
 
255
  first_active_skipped = False
 
272
  await page.evaluate(f"window.__clickSubScreen('{sub}')")
273
  await wait_for_layout_stable()
274
  await fix_sidebar_permanently()
275
+ await highlight_sidebar(f"{screen_name} - {sub}")
276
  await asyncio.sleep(0.8)
277
 
278
  sub_name_clean = sub.replace(" ", "_").lower()
279
  sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
280
  await page.screenshot(path=sub_path, full_page=True)
281
+ await reset_sidebar_highlight()
282
  screenshots.append(sub_path)
283
 
284
  index += 1