@scalar/api-client
Version:
the open source API testing client
204 lines (203 loc) • 8.64 kB
JavaScript
import { defineComponent as W, useAttrs as j, ref as d, computed as s, toRef as r, watch as H, createElementBlock as i, openBlock as n, Fragment as Q, createBlock as c, createCommentVNode as f, normalizeClass as G, createElementVNode as w, toDisplayString as J, mergeProps as X, unref as T, withKeys as b, withModifiers as D, createTextVNode as V, renderSlot as $ } from "vue";
import { useCodeMirror as Y, useDropdown as Z, colorPicker as _ } from "@scalar/use-codemirror";
import { nanoid as ee } from "nanoid";
import B from "../data-table/DataTableInputSelect.vue.js";
import te from "../../features/environments/components/EnvironmentVariablesDropdown.vue.js";
import { pillPlugin as le, backspaceCommand as ne } from "./code-variable-widget.js";
const oe = { class: "whitespace-nowrap" }, ae = ["id"], re = {
key: 0,
class: "z-context text-c-2 absolute right-1.5 bottom-1 hidden font-sans group-has-[:focus-visible]/input:block",
role: "alert"
}, ie = {
key: 5,
class: "centered-y text-orange absolute right-7 text-xs"
}, ue = {
key: 6,
class: "centered-y absolute right-0 flex h-full items-center p-1.5 group-has-[.cm-focused]:z-1"
}, de = {
key: 7,
class: "required centered-y text-xxs text-c-3 group-[.error]:text-red bg-b-1 pointer-events-none absolute right-0 mr-0.5 pt-px pr-2 opacity-100 shadow-[-8px_0_4px_var(--scalar-background-1)] transition-opacity duration-150 group-[.alert]:bg-transparent group-[.alert]:shadow-none group-[.error]:bg-transparent group-[.error]:shadow-none peer-has-[.cm-focused]:opacity-0"
}, se = {
inheritAttrs: !1
}, ge = /* @__PURE__ */ W({
...se,
__name: "CodeInput",
props: {
modelValue: {},
environment: {},
type: {},
disabled: { type: Boolean, default: !1 },
error: { type: Boolean, default: !1 },
layout: { default: "desktop" },
enum: {},
examples: {},
default: {},
nullable: { type: Boolean, default: !1 },
placeholder: {},
required: { type: Boolean },
colorPicker: { type: Boolean, default: !1 },
lineNumbers: { type: Boolean, default: !1 },
lint: { type: Boolean, default: !1 },
lineWrapping: { type: Boolean, default: !1 },
language: {},
extensions: { default: () => [] },
disableTabIndent: { type: Boolean, default: !1 },
disableEnter: { type: Boolean, default: !1 },
disableCloseBrackets: { type: Boolean, default: !1 },
emitOnBlur: { type: Boolean, default: !0 },
withVariables: { type: Boolean, default: !0 },
importCurl: { type: Boolean, default: !1 },
handleFieldChange: { type: Function },
handleFieldSubmit: { type: Function }
},
emits: ["update:modelValue", "submit", "blur", "curl", "redirectToEnvironment"],
setup(t, { expose: M, emit: R }) {
const u = R, k = j(), q = k.id || `id-${ee()}`, p = d(!1), A = s(() => t.enum?.length ? !1 : t.type === "boolean" || Array.isArray(t.type) && t.type.includes("boolean")), x = s(() => t.nullable ? ["true", "false", "null"] : ["true", "false"]), I = s(() => Array.isArray(t.type) ? t.type.find((e) => e !== "null") ?? "string" : t.type), C = (e) => {
if (e !== t.modelValue) {
if (t.importCurl && e.trim().toLowerCase().startsWith("curl")) {
u("curl", e), o.value && o.value.dispatch({
changes: {
from: 0,
to: o.value.state.doc.length,
insert: String(t.modelValue)
}
});
return;
}
t.handleFieldChange ? t.handleFieldChange(e) : u("update:modelValue", e);
}
}, y = (e) => {
t.handleFieldSubmit ? t.handleFieldSubmit(e) : u("submit", e);
}, S = (e) => {
p.value = !1, t.emitOnBlur && t.modelValue && y(e), u("blur", e);
}, g = (e) => {
u("update:modelValue", e);
}, N = () => {
const e = [...t.extensions];
return t.colorPicker && e.push(_), e;
}, O = s(
() => le({
environment: t.environment,
isReadOnly: t.layout === "modal"
})
), z = s(() => [
...N(),
O.value,
ne
]), E = d(null), { codeMirror: o } = Y({
content: r(() => String(t.modelValue ?? "")),
onChange: (e) => {
C(e), L();
},
onFocus: () => {
p.value = !0;
},
onBlur: S,
codeMirrorRef: E,
disableTabIndent: r(() => t.disableTabIndent),
disableEnter: r(() => t.disableEnter),
disableCloseBrackets: r(() => t.disableCloseBrackets),
lineNumbers: r(() => t.lineNumbers),
language: r(() => t.language),
lint: r(() => t.lint),
extensions: z,
placeholder: r(() => t.placeholder)
});
H(o, () => {
o.value && Object.hasOwn(k, "autofocus") && o.value.focus();
});
const h = d(!1), F = d(""), P = d({ left: 0, top: 0 }), v = d(null), { handleDropdownSelect: K, updateDropdownVisibility: L } = Z({
codeMirror: o,
query: F,
showDropdown: h,
dropdownPosition: P
}), U = s(() => h.value && t.withVariables && t.layout !== "modal" && !!t.environment), m = (e, l) => {
if (h.value) {
e === "down" || e === "up" ? (l.preventDefault(), v.value?.handleArrowKey(e)) : e === "enter" && (l.preventDefault(), v.value?.handleSelect());
return;
}
e === "escape" && !t.disableTabIndent && l.stopPropagation(), e === "enter" && l.target instanceof HTMLDivElement && y(l.target.textContent ?? "");
};
return M({
focus: () => o.value?.focus(),
isFocused: p,
handleChange: C,
handleSubmit: y,
handleBlur: S,
booleanOptions: x,
codeMirror: o,
modelValue: t.modelValue
}), (e, l) => (n(), i(Q, null, [
e.disabled ? (n(), i("div", {
key: 0,
class: G(["text-c-2 flex cursor-default items-center justify-center", e.layout === "modal" ? "font-code pr-2 pl-1 text-base" : "px-2"]),
"data-testid": "code-input-disabled"
}, [
w("span", oe, J(e.modelValue), 1)
], 2)) : t.enum?.length ? (n(), c(B, {
key: 1,
default: t.default,
modelValue: e.modelValue,
type: I.value,
value: t.enum,
"onUpdate:modelValue": g
}, null, 8, ["default", "modelValue", "type", "value"])) : A.value ? (n(), c(B, {
key: 2,
default: t.default,
modelValue: e.modelValue,
value: x.value,
"onUpdate:modelValue": g
}, null, 8, ["default", "modelValue", "value"])) : e.examples?.length ? (n(), c(B, {
key: 3,
default: t.default,
modelValue: e.modelValue,
value: e.examples,
"onUpdate:modelValue": g
}, null, 8, ["default", "modelValue", "value"])) : (n(), i("div", X({
key: 4,
id: T(q)
}, e.$attrs, {
ref_key: "codeMirrorRef",
ref: E,
class: ["group/input group-[.alert]:outline-orange group-[.error]:outline-red font-code peer relative w-full overflow-hidden text-xs leading-[1.44] whitespace-nowrap -outline-offset-1 has-[:focus-visible]:rounded-[4px] has-[:focus-visible]:outline", {
"line-wrapping has-[:focus-visible]:bg-b-1 has-[:focus-visible]:absolute has-[:focus-visible]:z-1": e.lineWrapping,
"flow-code-input--error": e.error
}],
onKeydown: [
l[0] || (l[0] = b(D((a) => m("down", a), ["stop"]), ["down"])),
l[1] || (l[1] = b((a) => m("enter", a), ["enter"])),
l[2] || (l[2] = b((a) => m("escape", a), ["escape"])),
l[3] || (l[3] = b(D((a) => m("up", a), ["stop"]), ["up"]))
]
}), [
e.disableTabIndent ? f("", !0) : (n(), i("div", re, [...l[5] || (l[5] = [
V(" Press ", -1),
w("kbd", { class: "-mx-0.25 rounded border px-0.5 font-mono" }, "Esc", -1),
V(" then ", -1),
w("kbd", { class: "-mx-0.25 rounded border px-0.5 font-mono" }, "Tab", -1),
V(" to exit ", -1)
])]))
], 16, ae)),
e.$slots.warning ? (n(), i("div", ie, [
$(e.$slots, "warning", {}, void 0, !0)
])) : f("", !0),
e.$slots.icon ? (n(), i("div", ue, [
$(e.$slots, "icon", {}, void 0, !0)
])) : f("", !0),
e.required ? (n(), i("div", de, " Required ")) : f("", !0),
U.value && e.environment ? (n(), c(te, {
key: 8,
ref_key: "dropdownRef",
ref: v,
dropdownPosition: P.value,
environment: e.environment,
query: F.value,
onRedirect: l[4] || (l[4] = (a) => u("redirectToEnvironment")),
onSelect: T(K)
}, null, 8, ["dropdownPosition", "environment", "query", "onSelect"])) : f("", !0)
], 64));
}
});
export {
ge as default
};