Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions sdk/server-ktor/src/main/resources/devconsole-web/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,46 @@ body.detail-zoom .split-shell.collapse-detail { grid-template-columns: 1fr; }
without the path/headers/body rows wrapping too tightly. */
.modal.modal-wide { max-width: 640px; }
.mock-body-toolbar { display: flex; align-items: center; gap: 12px; margin-top: -4px; }
/* The JSON status is the one thing on its row that can run long (a parse error carries the
engine's own message), so it takes the slack and ellipses rather than widening the dialog.
The full-window toggle lives on the find bar below and parks on that row's right edge. */
#mockRuleBodyStatus { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#mockRuleBodyExpand { margin-left: auto; }
/* A mock body is JSON far more often than it is one line, so the editor opens tall enough to read
a formatted payload without scrolling; `rows` only sets the floor before this applies. */
#mockRuleBody { min-height: 232px; line-height: 1.6; resize: vertical; }

/* Find-in-body. A textarea can only ever show ONE hit -- its selection, and only while focused --
so every match is painted by a mirrored, non-interactive layer sitting underneath a
now-transparent textarea. The mirror only ever renders color: transparent text; the glyphs the
user reads are still the textarea's own, drawn on top. That means every property affecting glyph
position (font, size, line-height, padding, wrapping) MUST stay identical between the two rules
below, or the marks slide off the words they belong to. The scrollbar gutter is the one that
can't be written here -- its width is measured and applied in syncMockBodyHighlightMetrics. */
.mock-body-editor { position: relative; display: block; }
.mock-body-editor > textarea { position: relative; z-index: 1; display: block; width: 100%; background: transparent; }
.mock-body-highlight {
position: absolute; inset: 1px; z-index: 0; overflow: hidden; pointer-events: none;
padding: 9px 11px; border-radius: 6px; background: var(--code-bg);
font-family: var(--font-mono); font-size: 12.5px; line-height: 1.6;
white-space: pre-wrap; overflow-wrap: break-word; color: transparent;
}
/* Same --signal-soft the .code-line/.kv-grid search hits use. The current match is stronger but
still translucent: the readable glyphs belong to the textarea above and stay --ink either way,
so a solid --signal fill would bury them. */
.mock-body-highlight mark { background: var(--signal-soft); color: transparent; border-radius: 2px; }
.mock-body-highlight mark.current { background: color-mix(in srgb, var(--signal) 45%, transparent); }
.mock-body-findbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.mock-body-find-wrap { flex: 1 1 200px; min-width: 160px; max-width: 320px; }
.mock-body-findbar .xs { flex: 0 0 auto; }
#mockRuleBodyFindCount { flex: 1 1 auto; min-width: 0; white-space: nowrap; }
/* Full window mode: same near-viewport box as .modal-fullscreen, with the extra height handed to
the body editor. The grid's own rows stay content-sized, so the editor is sized in vh directly
rather than flexed -- a grid item can't stretch into a row it doesn't define. */
.modal.mock-modal-expanded { max-width: 96vw; width: 96vw; height: 92vh; max-height: 92vh; }
.mock-modal-expanded #mockRuleBody { min-height: clamp(232px, 62vh, 78vh); }
/* Capped so the live preview can't push the editor itself off-screen in the scrolling body. */
.mock-modal-expanded #mockRuleBodyPreview .code-block { max-height: 34vh; overflow-y: auto; }

/* Code block fullscreen overlay: near-viewport-size instead of the generic 480px modal so a long
JSON/code body gets real room. Its .code-block-lg content scrolls inside .modal-body-code. */
Expand Down
189 changes: 180 additions & 9 deletions sdk/server-ktor/src/main/resources/devconsole-web/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
// draft came from "Mock this response" (or an edit of a rule that already had one), reset on
// every dialog open, and included in the save payload as-is (never re-derived from edited fields).
let mockRuleDraftSourceBodySnapshot = null;
// Full-window mode for the mock rule dialog. Deliberately outside openMockRuleDialog so it
// sticks for the rest of the page session: someone hand-writing JSON bodies wants the big
// editor on every rule, not one dialog at a time.
let mockBodyExpanded = false;
// Find-in-body for the response body editor. `ranges` are [start, end) offsets into the
// textarea's CURRENT value, so they are recomputed from scratch on every body change (Format
// rewrites the whole string) rather than carried across edits. Reset on each dialog open.
let mockBodyFind = { query: '', ranges: [], index: 0 };
let preferencesEditable = false;
let databaseEditable = false;
let filesEditable = false;
Expand Down Expand Up @@ -1401,6 +1409,10 @@
// codeBlockHtml, which already esc()s every token.
// ================================================================
const MOCK_RULE_ID_RE = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
// Find-in-body stops counting here: a one-character query against a large body would
// otherwise build tens of thousands of <mark>s on every keystroke. The count renders as
// "N+" when it caps, so the number on screen is never quietly wrong.
const MOCK_BODY_FIND_MAX = 2000;
const MOCK_HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
function mockRuleFieldEls() {
return {
Expand Down Expand Up @@ -1450,17 +1462,27 @@
const raw = $('mockRuleBody').value;
const statusEl = $('mockRuleBodyStatus');
const previewEl = $('mockRuleBodyPreview');
if (!raw.trim()) { statusEl.textContent = ''; statusEl.className = 'card-field-help'; previewEl.innerHTML = ''; return; }
try {
const parsed = JSON.parse(raw);
statusEl.textContent = 'valid JSON';
statusEl.className = 'card-field-help tone-text-signal';
previewEl.innerHTML = codeBlockHtml(formatJsonLines(parsed), false, 'Mock rule body preview');
} catch (err) {
statusEl.textContent = 'not JSON — will be sent verbatim: ' + err.message;
statusEl.className = 'card-field-help tone-text-warn';
if (!raw.trim()) {
statusEl.textContent = '';
statusEl.className = 'card-field-help';
previewEl.innerHTML = '';
} else {
try {
const parsed = JSON.parse(raw);
statusEl.textContent = 'valid JSON';
statusEl.className = 'card-field-help tone-text-signal';
previewEl.innerHTML = codeBlockHtml(formatJsonLines(parsed), false, 'Mock rule body preview');
} catch (err) {
statusEl.textContent = 'not JSON — will be sent verbatim: ' + err.message;
statusEl.className = 'card-field-help tone-text-warn';
previewEl.innerHTML = '';
}
}
// Every path that changes the body funnels through here (the input listener, Format's direct
// .value rewrite, and the dialog opening on a different rule), so it is the one place find
// offsets can be kept honest. An empty body takes the branch above rather than returning
// early: reopening on a blank rule still has to clear the previous rule's highlights.
refreshMockBodyFind();
}
function formatMockRuleBody() {
const el = $('mockRuleBody');
Expand All @@ -1472,6 +1494,130 @@
}
refreshMockBodyEditor();
}
/** Literal (never regex) case-insensitive scan for non-overlapping matches of [query] in [text].
* A mock body is JSON, so `"`, `{` and `[` are ordinary things to search for -- treating the
* query as a pattern would make the common case throw or silently match the wrong thing. */
function mockBodyFindRanges(text, query) {
if (!query) return [];
const haystack = text.toLowerCase();
const needle = query.toLowerCase();
const ranges = [];
for (let i = haystack.indexOf(needle); i !== -1; i = haystack.indexOf(needle, i + needle.length)) {
ranges.push([i, i + needle.length]);
if (ranges.length >= MOCK_BODY_FIND_MAX) break;
}
return ranges;
}
/** The mirror is laid out by CSS except for the scrollbar gutter: once the textarea overflows,
* its content box narrows by the scrollbar's width and its text rewraps. The mirror has
* `overflow: hidden` and never grows one, so that width is measured off the live element and
* added as padding -- otherwise every mark below the first wrapped line drifts. */
function syncMockBodyHighlightMetrics() {
const ta = $('mockRuleBody');
const layer = $('mockRuleBodyHighlight');
if (!ta || !layer) return;
const gutter = Math.max(0, ta.offsetWidth - ta.clientWidth - 2);
layer.style.paddingRight = 11 + gutter + 'px';
layer.scrollTop = ta.scrollTop;
layer.scrollLeft = ta.scrollLeft;
}
/** Paints the mirror. Skipped entirely with no active query -- the layer still has to exist to
* carry the editor's background (the textarea above it is transparent), but building a copy of
* the whole body on every keystroke would be pure waste when nothing is highlighted. */
function renderMockBodyHighlight() {
const layer = $('mockRuleBodyHighlight');
if (!layer) return;
const { query, ranges, index } = mockBodyFind;
if (!query || !ranges.length) { layer.innerHTML = ''; return; }
const text = $('mockRuleBody').value;
let html = '';
let at = 0;
ranges.forEach(([start, end], i) => {
html += esc(text.slice(at, start));
html += `<mark${i === index ? ' class="current"' : ''}>${esc(text.slice(start, end))}</mark>`;
at = end;
});
// The trailing newline keeps the mirror's last line box in step with the textarea's, which
// always reserves one for the caret.
layer.innerHTML = html + esc(text.slice(at)) + '\n';
syncMockBodyHighlightMetrics();
}
/** Scrolls the current match into view without focusing the textarea, so the find field keeps
* the caret and Enter can keep stepping. The mark's own laid-out position in the mirror is the
* measurement -- the textarea can't report where a range sits, and asking it to scroll would
* mean focusing it. */
function revealMockBodyMatch() {
const ta = $('mockRuleBody');
const mark = $('mockRuleBodyHighlight')?.querySelector('mark.current');
if (!ta || !mark) return;
const top = mark.offsetTop;
const bottom = top + mark.offsetHeight;
if (top < ta.scrollTop || bottom > ta.scrollTop + ta.clientHeight) {
ta.scrollTop = Math.max(0, top - ta.clientHeight / 2 + mark.offsetHeight / 2);
}
// Also park the textarea's own selection on the match, so clicking into the editor lands the
// caret where the user was looking rather than back where they last typed.
const [start, end] = mockBodyFind.ranges[mockBodyFind.index];
ta.setSelectionRange(start, end);
syncMockBodyHighlightMetrics();
}
function setMockBodyFindCount(text, tone) {
const el = $('mockRuleBodyFindCount');
el.textContent = text;
el.className = 'card-field-help' + (tone ? ' ' + tone : '');
}
/** Recomputes matches against the live body and repaints. [keepIndex] holds the user's place
* across a body edit; the find field's own input resets to the first match instead. */
function refreshMockBodyFind({ keepIndex = true, reveal = false } = {}) {
const ranges = mockBodyFindRanges($('mockRuleBody').value, mockBodyFind.query);
mockBodyFind.ranges = ranges;
mockBodyFind.index = ranges.length ? (keepIndex ? Math.min(mockBodyFind.index, ranges.length - 1) : 0) : 0;
const capped = ranges.length >= MOCK_BODY_FIND_MAX;
$('mockRuleBodyFindPrev').disabled = ranges.length < 2;
$('mockRuleBodyFindNext').disabled = ranges.length < 2;
if (!mockBodyFind.query) setMockBodyFindCount('');
else if (!ranges.length) setMockBodyFindCount('no matches', 'tone-text-warn');
else setMockBodyFindCount(mockBodyFind.index + 1 + ' of ' + ranges.length + (capped ? '+' : ''));
renderMockBodyHighlight();
if (reveal) revealMockBodyMatch();
}
function stepMockBodyFind(delta) {
const total = mockBodyFind.ranges.length;
if (!total) return;
mockBodyFind.index = (mockBodyFind.index + delta + total) % total;
setMockBodyFindCount(mockBodyFind.index + 1 + ' of ' + total + (total >= MOCK_BODY_FIND_MAX ? '+' : ''));
renderMockBodyHighlight();
revealMockBodyMatch();
}
function applyMockBodyFindQuery(query) {
mockBodyFind.query = query;
refreshMockBodyFind({ keepIndex: false, reveal: true });
}
/** Applies full-window mode to the dialog shell and syncs the toggle's label, icon, and pressed
* state. Only the box grows -- no field is hidden, so a validation error can never land on
* something the user can't see. */
function setMockBodyExpanded(expanded) {
mockBodyExpanded = expanded;
const modal = $('mockRuleModal')?.querySelector('.modal');
const btn = $('mockRuleBodyExpand');
if (!modal || !btn) return;
modal.classList.toggle('mock-modal-expanded', expanded);
btn.setAttribute('aria-pressed', String(expanded));
btn.title = expanded ? 'Shrink the dialog back to its normal size' : 'Expand the dialog to fill the window (the body editor takes the extra room)';
$('mockRuleBodyExpandLabel').textContent = expanded ? 'Exit full window' : 'Full window';
btn.querySelector('use')?.setAttribute('href', expanded ? '#dc-collapse' : '#dc-expand');
}
function toggleMockBodyExpanded() {
setMockBodyExpanded(!mockBodyExpanded);
// The box just changed width, so the body rewraps and the scrollbar may come or go.
syncMockBodyHighlightMetrics();
const el = $('mockRuleBody');
// A plain focus() scrolls the *end* of a now-tall textarea into view, hiding the start of the
// body; park the field at the top of the scrolling .modal-body instead so the toggle lands on
// line 1 either way.
el.focus({ preventScroll: true });
el.closest('.field')?.scrollIntoView({ block: 'start' });
}
/** Only a plain (optionally delayed) static response with an untruncated body survives a round
* trip through this dialog -- richer actions (ConnectionFailure/Timeout/TemplateResponse/etc.)
* and truncated bodies have no representation in the form, so saving one back would silently
Expand Down Expand Up @@ -1502,6 +1648,9 @@
setFieldError(f.headers, false);
setMockDialogError('');
$('mockRuleModalTitleText').textContent = editing ? 'Edit mock rule' : 'New mock rule';
setMockBodyExpanded(mockBodyExpanded);
$('mockRuleBodyFind').value = '';
mockBodyFind = { query: '', ranges: [], index: 0 };
refreshMockBodyEditor();
syncMockRuleDialogGate();
mockDialogOpenerEl = document.activeElement;
Expand Down Expand Up @@ -7516,8 +7665,30 @@
$('mockRuleCancel').onclick = closeMockRuleDialog;
$('mockRuleSave').onclick = saveMockRuleDialog;
$('mockRuleBodyFormat').onclick = formatMockRuleBody;
$('mockRuleBodyExpand').onclick = toggleMockBodyExpanded;
$('mockRuleBody').addEventListener('input', refreshMockBodyEditor);
$('mockRuleBody').addEventListener('blur', formatMockRuleBody);
$('mockRuleBody').addEventListener('scroll', syncMockBodyHighlightMetrics);
$('mockRuleBodyFind').addEventListener('input', (e) => applyMockBodyFindQuery(e.target.value));
$('mockRuleBodyFind').addEventListener('keydown', (e) => {
if (e.key === 'Enter') { e.preventDefault(); stepMockBodyFind(e.shiftKey ? -1 : 1); return; }
// Escape clears the search before it closes the dialog -- stopPropagation keeps
// mockDialogKeydown (a document listener) from dismissing a dialog mid-edit over a
// keystroke the user meant for the find field. With the field already empty it falls
// through and Escape closes the dialog as it does everywhere else.
if (e.key === 'Escape' && e.target.value) {
e.preventDefault();
e.stopPropagation();
e.target.value = '';
applyMockBodyFindQuery('');
}
});
for (const [id, delta] of [['mockRuleBodyFindPrev', -1], ['mockRuleBodyFindNext', 1]]) {
// Stepping must not pull focus out of the find field (and must not blur the textarea into
// formatMockRuleBody, which would rewrite the body under the offsets being stepped through).
$(id).addEventListener('mousedown', (e) => e.preventDefault());
$(id).onclick = () => stepMockBodyFind(delta);
}
wireCardGrid('mockRuleList', {
onToggle: (id) => {
const checked = document.querySelector(`[data-card-toggle="${CSS.escape(id)}"]`)?.getAttribute('aria-checked') === 'true';
Expand Down
12 changes: 11 additions & 1 deletion sdk/server-ktor/src/main/resources/devconsole-web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,22 @@
<label class="field field-full"><span>Response headers (Name: value, one per line)</span><textarea id="mockRuleHeaders" rows="3"></textarea></label>
<label class="field field-full">
<span>Response body</span>
<textarea id="mockRuleBody" rows="7" placeholder="{}"></textarea>
<span class="mock-body-editor">
<span id="mockRuleBodyHighlight" class="mock-body-highlight" aria-hidden="true"></span>
<textarea id="mockRuleBody" rows="14" placeholder="{}"></textarea>
</span>
</label>
<div class="field-full mock-body-toolbar">
<button type="button" id="mockRuleBodyFormat" class="xs">Format</button>
<span id="mockRuleBodyStatus" class="card-field-help"></span>
</div>
<div class="field-full mock-body-findbar">
<span class="search-wrap mock-body-find-wrap"><svg class="ic ic-sm search-lead" aria-hidden="true"><use href="#dc-search"/></svg><input type="search" id="mockRuleBodyFind" placeholder="Find in response body…" aria-describedby="mockRuleBodyFindCount"></span>
<button type="button" id="mockRuleBodyFindPrev" class="xs" aria-label="Previous match" title="Previous match (Shift+Enter)" disabled><svg class="ic ic-sm" aria-hidden="true"><use href="#dc-arrow-up"/></svg></button>
<button type="button" id="mockRuleBodyFindNext" class="xs" aria-label="Next match" title="Next match (Enter)" disabled><svg class="ic ic-sm" aria-hidden="true"><use href="#dc-arrow-down"/></svg></button>
<span id="mockRuleBodyFindCount" class="card-field-help" role="status" aria-live="polite" aria-atomic="true"></span>
<button type="button" id="mockRuleBodyExpand" class="xs" aria-pressed="false" aria-controls="mockRuleBody" title="Expand the dialog to fill the window (the body editor takes the extra room)"><svg class="ic ic-sm" aria-hidden="true"><use href="#dc-expand"/></svg><span id="mockRuleBodyExpandLabel">Full window</span></button>
</div>
<div id="mockRuleBodyPreview" class="field-full"></div>
<label class="field"><span>Priority</span><input id="mockRulePriority" type="number" value="0"></label>
<label class="field"><span>Scope</span><select id="mockRuleScope"><option value="SESSION">Session</option><option value="PERSISTENT_INTERNAL">Persistent (internal)</option></select></label>
Expand Down
Loading