-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathaiPhoenixConnectors.js
More file actions
728 lines (660 loc) · 28.5 KB
/
Copy pathaiPhoenixConnectors.js
File metadata and controls
728 lines (660 loc) · 28.5 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* NodeConnector handlers for bridging the node-side Claude Code agent with
* the Phoenix browser runtime. Handles editor state queries, screenshot
* capture, file content reads, and edit application to document buffers.
*
* Called via execPeer from src-node/claude-code-agent.js and
* src-node/mcp-editor-tools.js.
*/
define(function (require, exports, module) {
const DocumentManager = require("document/DocumentManager"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
MainViewManager = require("view/MainViewManager"),
EditorManager = require("editor/EditorManager"),
FileSystem = require("filesystem/FileSystem"),
LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),
LiveDevMain = require("LiveDevelopment/main"),
LivePreviewConstants = require("LiveDevelopment/LivePreviewConstants"),
WorkspaceManager = require("view/WorkspaceManager"),
SnapshotStore = require("core-ai/AISnapshotStore"),
EventDispatcher = require("utils/EventDispatcher"),
StringUtils = require("utils/StringUtils"),
Strings = require("strings");
// filePath → previous content before edit, for undo/snapshot support
const _previousContentMap = {};
// Last screenshot base64 data, for displaying in tool indicators
let _lastScreenshotBase64 = null;
// Banner / live preview mode state
let _activeExecJsCount = 0;
let _savedLivePreviewMode = null;
let _bannerDismissed = false;
let _bannerEl = null;
let _bannerStyleInjected = false;
let _bannerAutoHideTimer = null;
/**
* Inject banner CSS once into the document head.
*/
function _injectBannerStyles() {
if (_bannerStyleInjected) {
return;
}
_bannerStyleInjected = true;
const style = document.createElement("style");
style.textContent =
"@keyframes ai-banner-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }" +
".ai-lp-banner {" +
" position: absolute; top: 0; left: 0; right: 0; bottom: 0;" +
" display: flex; align-items: center; justify-content: center; gap: 8px;" +
" background: rgba(24,24,28,0.52);" +
" backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);" +
" z-index: 10; border-radius: 3px;" +
" font-size: 12px; color: #e0e0e0; pointer-events: auto;" +
" transition: opacity 0.3s ease;" +
"}" +
".ai-lp-banner .ai-lp-banner-icon {" +
" color: #66bb6a; animation: ai-banner-pulse 1.5s ease-in-out infinite;" +
"}" +
".ai-lp-banner .ai-lp-banner-close {" +
" position: absolute; right: 6px; top: 50%; transform: translateY(-50%);" +
" background: none; border: none; color: #aaa; cursor: pointer;" +
" font-size: 14px; padding: 2px 5px; line-height: 1;" +
"}" +
".ai-lp-banner .ai-lp-banner-close:hover { color: #fff; }";
document.head.appendChild(style);
}
/**
* Show a banner overlay on the live preview toolbar.
* @param {string} text - Banner message text
*/
function _showBanner(text) {
if (_bannerDismissed) {
return;
}
_injectBannerStyles();
const toolbar = document.getElementById("live-preview-plugin-toolbar");
if (!toolbar) {
return;
}
// Ensure toolbar can host absolutely positioned children
if (getComputedStyle(toolbar).position === "static") {
toolbar.style.position = "relative";
}
if (_bannerEl && _bannerEl.parentNode) {
// Update text on existing banner
const textSpan = _bannerEl.querySelector(".ai-lp-banner-text");
if (textSpan) {
textSpan.textContent = text;
}
_bannerEl.style.opacity = "1";
return;
}
const banner = document.createElement("div");
banner.className = "ai-lp-banner";
banner.innerHTML =
'<i class="fa-solid fa-eye ai-lp-banner-icon"></i>' +
'<span class="ai-lp-banner-text">' + text.replace(/</g, "<") + '</span>' +
'<button class="ai-lp-banner-close" title="Dismiss">×</button>';
banner.querySelector(".ai-lp-banner-close").addEventListener("click", function () {
_bannerDismissed = true;
_hideBanner();
});
toolbar.appendChild(banner);
_bannerEl = banner;
}
/**
* Hide and remove the banner overlay with a fade-out transition.
*/
function _hideBanner() {
if (!_bannerEl) {
return;
}
_bannerEl.style.opacity = "0";
const el = _bannerEl;
setTimeout(function () {
if (el.parentNode) {
el.parentNode.removeChild(el);
}
}, 300);
_bannerEl = null;
}
/**
* Called when an execJsInLivePreview call starts. Increments the active
* count, saves mode and shows banner on first call.
*/
function _onExecJsStart() {
_activeExecJsCount++;
if (_activeExecJsCount === 1) {
// Cancel any pending auto-hide from a previous exec batch
if (_bannerAutoHideTimer) {
clearTimeout(_bannerAutoHideTimer);
_bannerAutoHideTimer = null;
}
_savedLivePreviewMode = LiveDevMain.getCurrentMode();
if (_savedLivePreviewMode !== LivePreviewConstants.LIVE_PREVIEW_MODE) {
LiveDevMain.setMode(LivePreviewConstants.LIVE_PREVIEW_MODE);
}
_bannerDismissed = false;
_showBanner(Strings.AI_LIVE_PREVIEW_BANNER_TEXT);
}
}
/**
* Called when an execJsInLivePreview call finishes. Decrements the count
* and restores mode / hides banner when all calls are done.
*/
function _onExecJsDone() {
_activeExecJsCount = Math.max(0, _activeExecJsCount - 1);
if (_activeExecJsCount === 0) {
if (_savedLivePreviewMode && _savedLivePreviewMode !== LivePreviewConstants.LIVE_PREVIEW_MODE) {
LiveDevMain.setMode(_savedLivePreviewMode);
}
_savedLivePreviewMode = null;
// Keep the banner visible briefly so the user can read it
if (_bannerAutoHideTimer) {
clearTimeout(_bannerAutoHideTimer);
}
_bannerAutoHideTimer = setTimeout(function () {
_hideBanner();
_bannerAutoHideTimer = null;
}, 5000);
}
}
// --- Editor state ---
/**
* Get the current editor state: active file, working set, live preview file.
* Called from the node-side MCP server via execPeer.
*/
function getEditorState() {
const deferred = new $.Deferred();
const activeFile = MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE);
const workingSet = MainViewManager.getWorkingSet(MainViewManager.ALL_PANES);
let activeFilePath = null;
if (activeFile) {
activeFilePath = activeFile.fullPath;
if (activeFilePath.startsWith("/tauri/")) {
activeFilePath = activeFilePath.replace("/tauri", "");
}
}
const workingSetPaths = workingSet.map(function (file) {
let p = file.fullPath;
if (p.startsWith("/tauri/")) {
p = p.replace("/tauri", "");
}
return p;
});
let livePreviewFile = null;
const $panelTitle = $("#panel-live-preview-title");
if ($panelTitle.length) {
livePreviewFile = $panelTitle.attr("data-fullPath") || null;
if (livePreviewFile && livePreviewFile.startsWith("/tauri/")) {
livePreviewFile = livePreviewFile.replace("/tauri", "");
}
}
const result = {
activeFile: activeFilePath,
workingSet: workingSetPaths,
livePreviewFile: livePreviewFile
};
// Include cursor/selection info from the active editor
const editor = EditorManager.getActiveEditor();
if (editor) {
const doc = editor.document;
const totalLines = doc.getLine(doc.getText().split("\n").length - 1) !== undefined
? doc.getText().split("\n").length : 0;
if (editor.hasSelection()) {
const sel = editor.getSelection();
let selectedText = editor.getSelectedText();
if (selectedText.length > 10000) {
selectedText = selectedText.slice(0, 10000) + "...(truncated, use Read tool for full content)";
}
result.cursorInfo = {
hasSelection: true,
startLine: sel.start.line + 1,
endLine: sel.end.line + 1,
selectedText: selectedText,
totalLines: totalLines
};
} else {
const cursor = editor.getCursorPos();
const lineNum = cursor.line;
const lineText = doc.getLine(lineNum) || "";
// Include a few surrounding lines for context
const contextStart = Math.max(0, lineNum - 2);
const contextEnd = Math.min(totalLines - 1, lineNum + 2);
const MAX_LINE_LEN = 200;
const contextLines = [];
for (let i = contextStart; i <= contextEnd; i++) {
const prefix = (i === lineNum) ? "> " : " ";
let text = doc.getLine(i) || "";
if (text.length > MAX_LINE_LEN) {
text = text.slice(0, MAX_LINE_LEN) + "...";
}
contextLines.push(prefix + (i + 1) + ": " + text);
}
const trimmedLineText = lineText.length > MAX_LINE_LEN
? lineText.slice(0, MAX_LINE_LEN) + "..." : lineText;
result.cursorInfo = {
hasSelection: false,
line: lineNum + 1,
column: cursor.ch + 1,
lineText: trimmedLineText,
context: contextLines.join("\n"),
totalLines: totalLines
};
}
}
// If live preview is connected, query the selected element (best-effort)
if (LiveDevProtocol.getConnectionIds().length > 0) {
const LP_SELECTED_EL_JS =
"(function(){" +
"var el=window.__current_ph_lp_selected;" +
"if(!el||!el.isConnected)return null;" +
"var tag=el.tagName.toLowerCase();" +
"var id=el.id?'#'+el.id:'';" +
"var cls=el.className&&typeof el.className==='string'?" +
"'.'+el.className.trim().split(/\\s+/).join('.'):" +
"'';" +
"var text=el.textContent||'';" +
"if(text.length>80)text=text.slice(0,80)+'...';" +
"text=text.replace(/\\n/g,' ').trim();" +
"return{tag:tag,selector:tag+id+cls,textPreview:text};" +
"})()";
LiveDevProtocol.evaluate(LP_SELECTED_EL_JS)
.done(function (evalResult) {
if (evalResult && evalResult.tag) {
result.livePreviewSelectedElement = evalResult;
}
deferred.resolve(result);
})
.fail(function () {
deferred.resolve(result);
});
} else {
deferred.resolve(result);
}
return deferred.promise();
}
// --- Screenshot ---
/**
* Take a screenshot of the Phoenix editor window.
* Called from the node-side MCP server via execPeer.
* @param {Object} params - { selector?: string }
* @return {{ base64: string|null, error?: string }}
*/
function takeScreenshot(params) {
const deferred = new $.Deferred();
if (!Phoenix || !Phoenix.app || !Phoenix.app.screenShotBinary) {
deferred.resolve({ base64: null, error: "Screenshot API not available" });
return deferred.promise();
}
Phoenix.app.screenShotBinary(params.selector || undefined)
.then(function (bytes) {
let binary = "";
const chunkSize = 8192;
for (let i = 0; i < bytes.length; i += chunkSize) {
const chunk = bytes.subarray(i, Math.min(i + chunkSize, bytes.length));
binary += String.fromCharCode.apply(null, chunk);
}
const base64 = btoa(binary);
_lastScreenshotBase64 = base64;
exports.trigger("screenshotCaptured", base64);
deferred.resolve({ base64: base64 });
})
.catch(function (err) {
deferred.resolve({ base64: null, error: err.message || String(err) });
});
return deferred.promise();
}
// --- File content ---
/**
* Check if a file has unsaved changes in the editor and return its content.
* Used by the node-side Read hook to serve dirty buffer content to Claude.
*/
function getFileContent(params) {
const vfsPath = SnapshotStore.realToVfsPath(params.filePath);
const doc = DocumentManager.getOpenDocumentForPath(vfsPath);
if (doc && doc.isDirty) {
return { isDirty: true, content: doc.getText() };
}
return { isDirty: false, content: null };
}
// --- Edit application ---
/**
* Apply a single edit to a document buffer and save to disk.
* Called immediately when Claude's Write/Edit is intercepted, so
* subsequent Reads see the new content both in the buffer and on disk.
* @param {Object} edit - {file, oldText, newText}
* @return {$.Promise} resolves with {previousContent} for undo support
*/
function _applySingleEdit(edit) {
const result = new $.Deferred();
const vfsPath = SnapshotStore.realToVfsPath(edit.file);
function _applyToDoc() {
DocumentManager.getDocumentForPath(vfsPath)
.done(function (doc) {
try {
const previousContent = doc.getText();
if (edit.oldText === null) {
// Write (new file or full replacement)
doc.setText(edit.newText);
} else {
// Edit — find oldText and replace
const docText = doc.getText();
const idx = docText.indexOf(edit.oldText);
if (idx === -1) {
result.reject(new Error(Strings.AI_CHAT_EDIT_NOT_FOUND));
return;
}
const startPos = doc.posFromIndex(idx);
const endPos = doc.posFromIndex(idx + edit.oldText.length);
doc.replaceRange(edit.newText, startPos, endPos);
}
// Open the file in the editor and save to disk
CommandManager.execute(Commands.CMD_OPEN, { fullPath: vfsPath });
SnapshotStore.saveDocToDisk(doc).always(function () {
result.resolve({ previousContent: previousContent });
});
} catch (err) {
result.reject(err);
}
})
.fail(function (err) {
result.reject(err || new Error("Could not open document"));
});
}
if (edit.oldText === null) {
// Write — file may not exist yet. Only create on disk if it doesn't
// already exist, to avoid triggering "external change" warnings.
const file = FileSystem.getFileForPath(vfsPath);
file.exists(function (existErr, exists) {
if (exists) {
// File exists — just open and set content, no disk write
_applyToDoc();
} else {
// New file — create on disk first so getDocumentForPath works
file.write("", function (writeErr) {
if (writeErr) {
result.reject(new Error("Could not create file: " + writeErr));
return;
}
_applyToDoc();
});
}
});
} else {
// Edit — file must already exist
_applyToDoc();
}
return result.promise();
}
/**
* Apply an edit to the editor buffer immediately (called by node-side hooks).
* The file appears as a dirty tab so subsequent Reads see the new content.
* @param {Object} params - {file, oldText, newText}
* @return {Promise<{applied: boolean, error?: string}>}
*/
function _isFileInLivePreview(filePath) {
const liveDetails = LiveDevMain.getLivePreviewDetails();
if (!liveDetails || !liveDetails.liveDocument) {
return false;
}
const vfsPath = SnapshotStore.realToVfsPath(filePath);
const liveDocPath = liveDetails.liveDocument.doc.file.fullPath;
if (vfsPath === liveDocPath) {
return true;
}
return !!(liveDetails.liveDocument.isRelated && liveDetails.liveDocument.isRelated(vfsPath));
}
function applyEditToBuffer(params) {
const deferred = new $.Deferred();
_applySingleEdit(params)
.done(function (result) {
if (result && result.previousContent !== undefined) {
_previousContentMap[params.file] = result.previousContent;
}
deferred.resolve({
applied: true,
isLivePreviewRelated: _isFileInLivePreview(params.file)
});
})
.fail(function (err) {
deferred.resolve({ applied: false, error: err.message || String(err) });
});
return deferred.promise();
}
// --- Previous content map access (used by AIChatPanel for snapshot tracking) ---
/**
* Get the previous content recorded for a file before the last edit.
* @param {string} filePath
* @return {string|undefined}
*/
function getPreviousContent(filePath) {
return _previousContentMap[filePath];
}
/**
* Clear all recorded previous content entries (called on new session).
*/
function clearPreviousContentMap() {
Object.keys(_previousContentMap).forEach(function (key) {
delete _previousContentMap[key];
});
}
/**
* Get the last captured screenshot as base64 PNG.
* @return {string|null}
*/
function getLastScreenshot() {
return _lastScreenshotBase64;
}
// --- Live preview JS execution ---
/**
* Execute JavaScript in the live preview iframe.
* Reuses the pattern from phoenix-builder-client.js: fast path if connected,
* otherwise auto-opens live preview and waits for connection.
* @param {Object} params - { code: string }
* @return {$.Promise} resolves with { result } or { error }
*/
function execJsInLivePreview(params) {
const deferred = new $.Deferred();
_onExecJsStart();
function _evaluate() {
LiveDevProtocol.evaluate(params.code)
.done(function (evalResult) {
_onExecJsDone();
deferred.resolve({ result: JSON.stringify(evalResult) });
})
.fail(function (err) {
_onExecJsDone();
deferred.resolve({ error: (err && err.message) || String(err) || "evaluate() failed" });
});
}
// Fast path: already connected
if (LiveDevProtocol.getConnectionIds().length > 0) {
_evaluate();
return deferred.promise();
}
// Need to ensure live preview is open and connected
const panel = WorkspaceManager.getPanelForID("live-preview-panel");
if (!panel || !panel.isVisible()) {
CommandManager.execute("file.liveFilePreview");
} else {
LiveDevMain.openLivePreview();
}
// Wait for a live preview connection (up to 30s)
const TIMEOUT = 30000;
const POLL_INTERVAL = 500;
let settled = false;
let pollTimer = null;
function cleanup() {
settled = true;
if (pollTimer) {
clearInterval(pollTimer);
pollTimer = null;
}
LiveDevProtocol.off("ConnectionConnect.aiExecJsLivePreview");
}
const timeoutTimer = setTimeout(function () {
if (settled) { return; }
cleanup();
_onExecJsDone();
deferred.resolve({ error: "Timed out waiting for live preview connection (30s)" });
}, TIMEOUT);
function onConnected() {
if (settled) { return; }
cleanup();
clearTimeout(timeoutTimer);
_evaluate();
}
LiveDevProtocol.on("ConnectionConnect.aiExecJsLivePreview", onConnected);
// Safety-net poll in case the event was missed
pollTimer = setInterval(function () {
if (settled) {
clearInterval(pollTimer);
return;
}
if (LiveDevProtocol.getConnectionIds().length > 0) {
onConnected();
}
}, POLL_INTERVAL);
return deferred.promise();
}
// --- Editor control ---
/**
* Control the editor: open/close files, set cursor, set selection.
* Called from the node-side MCP server via execPeer.
* @param {Object} params - { operation, filePath, startLine, startCh, endLine, endCh, line, ch }
* @return {$.Promise} resolves with { success: true } or { success: false, error: "..." }
*/
function controlEditor(params) {
const deferred = new $.Deferred();
const vfsPath = SnapshotStore.realToVfsPath(params.filePath);
switch (params.operation) {
case "open":
CommandManager.execute(Commands.CMD_OPEN, { fullPath: vfsPath })
.done(function () { deferred.resolve({ success: true }); })
.fail(function (err) { deferred.resolve({ success: false, error: String(err) }); });
break;
case "close": {
const file = FileSystem.getFileForPath(vfsPath);
CommandManager.execute(Commands.FILE_CLOSE, { file: file, _forceClose: true })
.done(function () { deferred.resolve({ success: true }); })
.fail(function (err) { deferred.resolve({ success: false, error: String(err) }); });
break;
}
case "openInWorkingSet":
CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN, { fullPath: vfsPath })
.done(function () { deferred.resolve({ success: true }); })
.fail(function (err) { deferred.resolve({ success: false, error: String(err) }); });
break;
case "setSelection":
CommandManager.execute(Commands.CMD_OPEN, { fullPath: vfsPath })
.done(function () {
const editor = EditorManager.getActiveEditor();
if (editor) {
editor.setSelection(
{ line: params.startLine - 1, ch: params.startCh - 1 },
{ line: params.endLine - 1, ch: params.endCh - 1 },
true
);
deferred.resolve({ success: true });
} else {
deferred.resolve({ success: false, error: "No active editor after opening file" });
}
})
.fail(function (err) { deferred.resolve({ success: false, error: String(err) }); });
break;
case "setCursorPos":
CommandManager.execute(Commands.CMD_OPEN, { fullPath: vfsPath })
.done(function () {
const editor = EditorManager.getActiveEditor();
if (editor) {
editor.setCursorPos(params.line - 1, params.ch - 1, true);
deferred.resolve({ success: true });
} else {
deferred.resolve({ success: false, error: "No active editor after opening file" });
}
})
.fail(function (err) { deferred.resolve({ success: false, error: String(err) }); });
break;
default:
deferred.resolve({ success: false, error: "Unknown operation: " + params.operation });
}
return deferred.promise();
}
// --- Live preview resize ---
/**
* Resize the live preview panel to a specific width in pixels.
* @param {Object} params - { width: number }
* @return {$.Promise} resolves with { actualWidth } or { error }
*/
function resizeLivePreview(params) {
const deferred = new $.Deferred();
if (!params.width) {
deferred.resolve({ error: "Provide 'width' as a number in pixels" });
return deferred.promise();
}
const targetWidth = params.width;
const label = targetWidth + "px";
// Ensure live preview panel is open
const panel = WorkspaceManager.getPanelForID("live-preview-panel");
if (!panel || !panel.isVisible()) {
CommandManager.execute("file.liveFilePreview");
}
// Give the panel a moment to open, then resize
setTimeout(function () {
WorkspaceManager.setPluginPanelWidth(targetWidth);
// Read back actual width from the toolbar
const toolbar = document.getElementById("live-preview-plugin-toolbar");
const actualWidth = toolbar ? toolbar.offsetWidth : targetWidth;
// Show brief banner
_bannerDismissed = false;
_showBanner(StringUtils.format(Strings.AI_LIVE_PREVIEW_BANNER_RESIZE, label));
if (_bannerAutoHideTimer) {
clearTimeout(_bannerAutoHideTimer);
}
_bannerAutoHideTimer = setTimeout(function () {
_hideBanner();
_bannerAutoHideTimer = null;
}, 5000);
const result = { actualWidth: actualWidth };
if (actualWidth !== targetWidth) {
result.clamped = true;
result.note = "Requested " + targetWidth + "px but the editor window can only " +
"accommodate " + actualWidth + "px. The user needs to increase the editor " +
"window size to allow a wider preview.";
}
deferred.resolve(result);
}, 100);
return deferred.promise();
}
exports.getEditorState = getEditorState;
exports.takeScreenshot = takeScreenshot;
exports.getFileContent = getFileContent;
exports.applyEditToBuffer = applyEditToBuffer;
exports.getPreviousContent = getPreviousContent;
exports.clearPreviousContentMap = clearPreviousContentMap;
exports.getLastScreenshot = getLastScreenshot;
exports.execJsInLivePreview = execJsInLivePreview;
exports.controlEditor = controlEditor;
exports.resizeLivePreview = resizeLivePreview;
EventDispatcher.makeEventDispatcher(exports);
});