@qikbar/qikbar 0.0.24 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +92 -0
- package/dist/ai/agent.d.ts +42 -0
- package/dist/ai/byok.d.ts +29 -0
- package/dist/ai/index.d.ts +8 -0
- package/dist/ai/providers.d.ts +82 -0
- package/dist/ai/sse.d.ts +9 -0
- package/dist/ai.js +36 -0
- package/dist/ai.js.map +7 -0
- package/dist/chunks/bar-D5CSPT3U.js +2031 -0
- package/dist/chunks/bar-D5CSPT3U.js.map +7 -0
- package/dist/chunks/chunk-2ADXL7KQ.js +358 -0
- package/dist/chunks/chunk-2ADXL7KQ.js.map +7 -0
- package/dist/chunks/chunk-6TC6G2C5.js +107 -0
- package/dist/chunks/chunk-6TC6G2C5.js.map +7 -0
- package/dist/chunks/chunk-HBBSETRF.js +153 -0
- package/dist/chunks/chunk-HBBSETRF.js.map +7 -0
- package/dist/chunks/chunk-I77YSUJN.js +104 -0
- package/dist/chunks/chunk-I77YSUJN.js.map +7 -0
- package/dist/chunks/chunk-IJ3WUM3T.js +544 -0
- package/dist/chunks/chunk-IJ3WUM3T.js.map +7 -0
- package/dist/chunks/chunk-MPTP2EWW.js +8 -0
- package/dist/chunks/chunk-MPTP2EWW.js.map +7 -0
- package/dist/chunks/chunk-N4Y2Z6YD.js +69 -0
- package/dist/chunks/chunk-N4Y2Z6YD.js.map +7 -0
- package/dist/chunks/chunk-PQEHV3D4.js +124 -0
- package/dist/chunks/chunk-PQEHV3D4.js.map +7 -0
- package/dist/chunks/chunk-VXTYD56F.js +453 -0
- package/dist/chunks/chunk-VXTYD56F.js.map +7 -0
- package/dist/chunks/chunk-YCVJQ7U3.js +52 -0
- package/dist/chunks/chunk-YCVJQ7U3.js.map +7 -0
- package/dist/core/define.d.ts +40 -0
- package/dist/core/frecency.d.ts +16 -0
- package/dist/core/hotkeys.d.ts +39 -0
- package/dist/core/lazy.d.ts +48 -0
- package/dist/core/search.d.ts +42 -0
- package/dist/core/storage.d.ts +5 -0
- package/dist/core/types.d.ts +601 -0
- package/dist/extensions/index.d.ts +30 -0
- package/dist/extensions/math.d.ts +14 -0
- package/dist/extensions.js +476 -0
- package/dist/extensions.js.map +7 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +7 -0
- package/dist/qikbar.global.js +259 -0
- package/dist/qikbar.global.js.map +7 -0
- package/dist/react/index.d.ts +66 -0
- package/dist/react.js +119 -0
- package/dist/react.js.map +7 -0
- package/dist/server/index.d.ts +47 -0
- package/dist/server.js +93 -0
- package/dist/server.js.map +7 -0
- package/dist/styles.css +222 -0
- package/dist/theme.d.ts +5 -0
- package/dist/theme.js +20 -0
- package/dist/theme.js.map +7 -0
- package/dist/ui/bar.d.ts +252 -0
- package/dist/ui/dom.d.ts +21 -0
- package/dist/ui/icons.d.ts +88 -0
- package/dist/ui/markdown.d.ts +8 -0
- package/dist/ui/nav.d.ts +8 -0
- package/dist/ui/styles.d.ts +6 -0
- package/dist/ui/themes.d.ts +27 -0
- package/dist/ui/views.d.ts +148 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +50 -0
- package/dist/utils.js.map +7 -0
- package/package.json +88 -43
- package/dist/qikbar.es.js +0 -8748
- package/dist/qikbar.umd.js +0 -113
- package/dist/types/boot.d.ts +0 -72
- package/dist/types/index.d.ts +0 -1
- package/dist/types/init.d.ts +0 -2
- package/dist/types/main.d.ts +0 -4
- package/dist/types/shared/AsyncRoot.d.ts +0 -5
- package/dist/types/shared/CommandFooter.d.ts +0 -5
- package/dist/types/shared/Crumbs.d.ts +0 -6
- package/dist/types/shared/Details.d.ts +0 -3
- package/dist/types/shared/ErrorBoundary.d.ts +0 -15
- package/dist/types/shared/Icon.d.ts +0 -4
- package/dist/types/shared/List.d.ts +0 -30
- package/dist/types/shared/Qikbar.d.ts +0 -5
- package/dist/types/shared/Root.d.ts +0 -4
- package/dist/types/shared/Search.d.ts +0 -1
- package/dist/types/shared/commandContext.d.ts +0 -8
- package/dist/types/shared/useDebounce.d.ts +0 -1
- package/dist/types/utils/liquid.d.ts +0 -4
- package/dist/types/vite-env.d.ts +0 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/ai/agent.ts
|
|
2
|
+
function createAIClient(provider, defaultSystem) {
|
|
3
|
+
async function* stream(prompt, options = {}) {
|
|
4
|
+
for await (let ev of provider.stream({
|
|
5
|
+
messages: [{ role: "user", content: prompt }],
|
|
6
|
+
system: options.system ?? defaultSystem,
|
|
7
|
+
signal: options.signal
|
|
8
|
+
}))
|
|
9
|
+
if (ev.type === "text") yield ev.text;
|
|
10
|
+
else if (ev.type === "error") throw new Error(ev.error);
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
provider,
|
|
14
|
+
stream,
|
|
15
|
+
async ask(prompt, options) {
|
|
16
|
+
let out = "";
|
|
17
|
+
for await (let t of stream(prompt, options)) out += t;
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function stringify(value) {
|
|
23
|
+
if (value === void 0) return "Done.";
|
|
24
|
+
if (typeof value == "string") return value;
|
|
25
|
+
try {
|
|
26
|
+
return JSON.stringify(value);
|
|
27
|
+
} catch {
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
var MAX_RESULT = 12e3;
|
|
32
|
+
async function* runAgent(options) {
|
|
33
|
+
let { provider, system, signal } = options, tools = provider.tools === !1 ? [] : options.tools ?? [], history = [...options.messages], maxSteps = options.maxSteps ?? 6;
|
|
34
|
+
for (let step = 0; step < maxSteps; step++) {
|
|
35
|
+
let calls = [], assistant, text = "";
|
|
36
|
+
for await (let ev of provider.stream({
|
|
37
|
+
messages: [...history],
|
|
38
|
+
system,
|
|
39
|
+
tools: tools.length && step < maxSteps - 1 ? tools.map((t) => t.definition) : void 0,
|
|
40
|
+
signal
|
|
41
|
+
}))
|
|
42
|
+
ev.type === "text" ? (text += ev.text, yield ev) : ev.type === "tool_call" ? calls.push(ev.call) : ev.type === "error" ? yield ev : ev.type === "done" && (assistant = ev.message);
|
|
43
|
+
if (assistant ?? (assistant = { role: "assistant", content: text, toolCalls: calls.length ? calls : void 0 }), history.push(assistant), yield { type: "message", message: assistant }, !calls.length || signal.aborted) return;
|
|
44
|
+
for (let call of calls) {
|
|
45
|
+
let tool = tools.find((t) => t.definition.name === call.name), label = tool?.label?.(call.args) ?? call.name;
|
|
46
|
+
yield { type: "tool_start", call, label };
|
|
47
|
+
let content, isError = !1;
|
|
48
|
+
if (call.error)
|
|
49
|
+
content = call.error, isError = !0;
|
|
50
|
+
else if (!tool)
|
|
51
|
+
content = `Unknown tool "${call.name}".`, isError = !0;
|
|
52
|
+
else
|
|
53
|
+
try {
|
|
54
|
+
content = stringify(await tool.run(call.args, signal));
|
|
55
|
+
} catch (err) {
|
|
56
|
+
content = err instanceof Error ? err.message : String(err), isError = !0;
|
|
57
|
+
}
|
|
58
|
+
content.length > MAX_RESULT && (content = `${content.slice(0, MAX_RESULT)}\u2026 (truncated)`);
|
|
59
|
+
let message = { role: "tool", toolCallId: call.id, toolName: call.name, content, ...isError ? { isError } : {} };
|
|
60
|
+
if (history.push(message), yield { type: "message", message }, yield { type: "tool_end", call, ok: !isError, result: content }, signal.aborted) return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export {
|
|
66
|
+
createAIClient,
|
|
67
|
+
runAgent
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=chunk-N4Y2Z6YD.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/ai/agent.ts"],
|
|
4
|
+
"sourcesContent": ["import type { AIClient, AIMessage, AIProvider, AIToolCall, AIToolDefinition } from '../core/types.js'\n\n/** Create the simple `ask` / `stream` client exposed as `ctx.ai`. */\nexport function createAIClient(provider: AIProvider, defaultSystem?: string): AIClient {\n async function* stream(prompt: string, options: { system?: string; signal?: AbortSignal } = {}): AsyncIterable<string> {\n for await (const ev of provider.stream({\n messages: [{ role: 'user', content: prompt }],\n system: options.system ?? defaultSystem,\n signal: options.signal,\n })) {\n if (ev.type === 'text') yield ev.text\n else if (ev.type === 'error') throw new Error(ev.error)\n }\n }\n return {\n provider,\n stream,\n async ask(prompt, options) {\n let out = ''\n for await (const t of stream(prompt, options)) out += t\n return out\n },\n }\n}\n\nexport interface RuntimeTool {\n definition: AIToolDefinition\n /** Human-readable label for the UI (\"Opening Billing\u2026\"). */\n label?: (args: Record<string, unknown>) => string\n run: (args: Record<string, unknown>, signal: AbortSignal) => Promise<unknown>\n}\n\nexport type AgentEvent =\n | { type: 'text'; text: string }\n | { type: 'tool_start'; call: AIToolCall; label: string }\n | { type: 'tool_end'; call: AIToolCall; ok: boolean; result: string }\n | { type: 'message'; message: AIMessage }\n | { type: 'error'; error: string }\n\nexport interface RunAgentOptions {\n provider: AIProvider\n /** Conversation so far (not mutated). */\n messages: AIMessage[]\n system?: string\n tools?: RuntimeTool[]\n maxSteps?: number\n signal: AbortSignal\n}\n\nfunction stringify(value: unknown): string {\n if (value === undefined) return 'Done.'\n if (typeof value === 'string') return value\n try {\n return JSON.stringify(value)\n } catch {\n return String(value)\n }\n}\n\nconst MAX_RESULT = 12000\n\n/**\n * Run a tool-calling loop: stream the model's answer, execute requested\n * tools, feed results back, repeat until the model stops calling tools.\n */\nexport async function* runAgent(options: RunAgentOptions): AsyncIterable<AgentEvent> {\n const { provider, system, signal } = options\n const tools = provider.tools === false ? [] : (options.tools ?? [])\n const history = [...options.messages]\n const maxSteps = options.maxSteps ?? 6\n\n for (let step = 0; step < maxSteps; step++) {\n const calls: AIToolCall[] = []\n let assistant: AIMessage | undefined\n let text = ''\n for await (const ev of provider.stream({\n messages: [...history],\n system,\n tools: tools.length && step < maxSteps - 1 ? tools.map((t) => t.definition) : undefined,\n signal,\n })) {\n if (ev.type === 'text') {\n text += ev.text\n yield ev\n } else if (ev.type === 'tool_call') calls.push(ev.call)\n else if (ev.type === 'error') yield ev\n else if (ev.type === 'done') assistant = ev.message\n }\n assistant ??= { role: 'assistant', content: text, toolCalls: calls.length ? calls : undefined }\n history.push(assistant)\n yield { type: 'message', message: assistant }\n if (!calls.length || signal.aborted) return\n\n for (const call of calls) {\n const tool = tools.find((t) => t.definition.name === call.name)\n const label = tool?.label?.(call.args) ?? call.name\n yield { type: 'tool_start', call, label }\n let content: string\n let isError = false\n if (call.error) {\n content = call.error\n isError = true\n } else if (!tool) {\n content = `Unknown tool \"${call.name}\".`\n isError = true\n } else {\n try {\n content = stringify(await tool.run(call.args, signal))\n } catch (err) {\n content = err instanceof Error ? err.message : String(err)\n isError = true\n }\n }\n if (content.length > MAX_RESULT) content = `${content.slice(0, MAX_RESULT)}\u2026 (truncated)`\n const message: AIMessage = { role: 'tool', toolCallId: call.id, toolName: call.name, content, ...(isError ? { isError } : {}) }\n history.push(message)\n yield { type: 'message', message }\n yield { type: 'tool_end', call, ok: !isError, result: content }\n if (signal.aborted) return\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAGO,SAAS,eAAe,UAAsB,eAAkC;AACrF,kBAAgB,OAAO,QAAgB,UAAqD,CAAC,GAA0B;AACrH,mBAAiB,MAAM,SAAS,OAAO;AAAA,MACrC,UAAU,CAAC,EAAE,MAAM,QAAQ,SAAS,OAAO,CAAC;AAAA,MAC5C,QAAQ,QAAQ,UAAU;AAAA,MAC1B,QAAQ,QAAQ;AAAA,IAClB,CAAC;AACC,UAAI,GAAG,SAAS,OAAQ,OAAM,GAAG;AAAA,eACxB,GAAG,SAAS,QAAS,OAAM,IAAI,MAAM,GAAG,KAAK;AAAA,EAE1D;AACA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,MAAM,IAAI,QAAQ,SAAS;AACzB,UAAI,MAAM;AACV,qBAAiB,KAAK,OAAO,QAA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QAAQ,KAAK,IAAI,KAAK,KAAK;AAC/C,YAAM,EAAE,MAAM,cAAc,MAAM,MAAM;AACxC,UAAI,SACA,UAAU;AACd,UAAI,KAAK;AACP,kBAAU,KAAK,OACf,UAAU;AAAA,eACD,CAAC;AACV,kBAAU,iBAAiB,KAAK,IAAI,MACpC,UAAU;AAAA;AAEV,YAAI;AACF,oBAAU,UAAU,MAAM,KAAK,IAAI,KAAK,MAAM,MAAM,CAAC;AAAA,QACvD,SAAS,KAAK;AACZ,oBAAU,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,GACzD,UAAU;AAAA,QACZ;AAEF,MAAI,QAAQ,SAAS,eAAY,UAAU,GAAG,QAAQ,MAAM,GAAG,UAAU,CAAC;AAC1E,UAAM,UAAqB,EAAE,MAAM,QAAQ,YAAY,KAAK,IAAI,UAAU,KAAK,MAAM,SAAS,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC,EAAG;AAI9H,UAHA,QAAQ,KAAK,OAAO,GACpB,MAAM,EAAE,MAAM,WAAW,QAAQ,GACjC,MAAM,EAAE,MAAM,YAAY,MAAM,IAAI,CAAC,SAAS,QAAQ,QAAQ,GAC1D,OAAO,QAAS;AAAA,IACtB;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "./chunk-MPTP2EWW.js";
|
|
4
|
+
|
|
5
|
+
// src/core/hotkeys.ts
|
|
6
|
+
var isMac = typeof navigator < "u" && /Mac|iPhone|iPad|iPod/i.test(navigator.userAgentData?.platform ?? navigator.platform ?? navigator.userAgent), KEY_ALIASES = {
|
|
7
|
+
esc: "escape",
|
|
8
|
+
return: "enter",
|
|
9
|
+
space: " ",
|
|
10
|
+
up: "arrowup",
|
|
11
|
+
down: "arrowdown",
|
|
12
|
+
left: "arrowleft",
|
|
13
|
+
right: "arrowright",
|
|
14
|
+
del: "delete",
|
|
15
|
+
plus: "+"
|
|
16
|
+
}, cache = /* @__PURE__ */ new Map();
|
|
17
|
+
function parseHotkey(hotkey) {
|
|
18
|
+
let combos = cache.get(hotkey);
|
|
19
|
+
return combos || (combos = hotkey.trim().split(/\s+/).map((part) => {
|
|
20
|
+
let keys = part.toLowerCase().split("+");
|
|
21
|
+
part.endsWith("++") && keys.splice(keys.length - 2, 2, "+");
|
|
22
|
+
let combo = { key: "", meta: !1, ctrl: !1, alt: !1, shift: !1 };
|
|
23
|
+
for (let k of keys)
|
|
24
|
+
k === "mod" ? isMac ? combo.meta = !0 : combo.ctrl = !0 : k === "cmd" || k === "meta" || k === "command" ? combo.meta = !0 : k === "ctrl" || k === "control" ? combo.ctrl = !0 : k === "alt" || k === "option" || k === "opt" ? combo.alt = !0 : k === "shift" ? combo.shift = !0 : combo.key = KEY_ALIASES[k] ?? k;
|
|
25
|
+
return combo;
|
|
26
|
+
}), cache.set(hotkey, combos), combos);
|
|
27
|
+
}
|
|
28
|
+
function eventKey(event) {
|
|
29
|
+
let code = event.code;
|
|
30
|
+
return code?.startsWith("Key") ? code.slice(3).toLowerCase() : code?.startsWith("Digit") ? code.slice(5) : (event.key ?? "").toLowerCase();
|
|
31
|
+
}
|
|
32
|
+
function matchesCombo(event, combo) {
|
|
33
|
+
if (event.metaKey !== combo.meta || event.ctrlKey !== combo.ctrl || event.altKey !== combo.alt) return !1;
|
|
34
|
+
let key = eventKey(event);
|
|
35
|
+
return combo.shift !== event.shiftKey && !(combo.key.length === 1 && !/[a-z0-9]/.test(combo.key)) ? !1 : key === combo.key || (event.key ?? "").toLowerCase() === combo.key;
|
|
36
|
+
}
|
|
37
|
+
function matchesHotkey(event, hotkey) {
|
|
38
|
+
let combos = parseHotkey(hotkey);
|
|
39
|
+
return combos.length === 1 && matchesCombo(event, combos[0]);
|
|
40
|
+
}
|
|
41
|
+
var MAC_SYMBOLS = {
|
|
42
|
+
meta: "\u2318",
|
|
43
|
+
ctrl: "\u2303",
|
|
44
|
+
alt: "\u2325",
|
|
45
|
+
shift: "\u21E7",
|
|
46
|
+
enter: "\u21B5",
|
|
47
|
+
escape: "esc",
|
|
48
|
+
backspace: "\u232B",
|
|
49
|
+
delete: "\u2326",
|
|
50
|
+
arrowup: "\u2191",
|
|
51
|
+
arrowdown: "\u2193",
|
|
52
|
+
arrowleft: "\u2190",
|
|
53
|
+
arrowright: "\u2192",
|
|
54
|
+
tab: "\u21E5",
|
|
55
|
+
" ": "Space"
|
|
56
|
+
}, PC_NAMES = {
|
|
57
|
+
meta: "Win",
|
|
58
|
+
ctrl: "Ctrl",
|
|
59
|
+
alt: "Alt",
|
|
60
|
+
shift: "Shift",
|
|
61
|
+
enter: "\u21B5",
|
|
62
|
+
escape: "Esc",
|
|
63
|
+
backspace: "\u232B",
|
|
64
|
+
delete: "Del",
|
|
65
|
+
arrowup: "\u2191",
|
|
66
|
+
arrowdown: "\u2193",
|
|
67
|
+
arrowleft: "\u2190",
|
|
68
|
+
arrowright: "\u2192",
|
|
69
|
+
tab: "Tab",
|
|
70
|
+
" ": "Space"
|
|
71
|
+
};
|
|
72
|
+
function formatHotkey(hotkey) {
|
|
73
|
+
return parseHotkey(hotkey).map((combo) => {
|
|
74
|
+
let names = isMac ? MAC_SYMBOLS : PC_NAMES, out = [];
|
|
75
|
+
return isMac ? (combo.ctrl && out.push(names.ctrl), combo.alt && out.push(names.alt), combo.shift && out.push(names.shift), combo.meta && out.push(names.meta)) : (combo.ctrl && out.push(names.ctrl), combo.meta && out.push(names.meta), combo.alt && out.push(names.alt), combo.shift && out.push(names.shift)), out.push(names[combo.key] ?? (combo.key.length === 1 ? combo.key.toUpperCase() : capitalize(combo.key))), out;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function capitalize(s) {
|
|
79
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
80
|
+
}
|
|
81
|
+
function isEditable(target) {
|
|
82
|
+
let el = target;
|
|
83
|
+
if (!el || !el.tagName) return !1;
|
|
84
|
+
let tag = el.tagName;
|
|
85
|
+
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || el.isContentEditable;
|
|
86
|
+
}
|
|
87
|
+
var SequenceMatcher = class {
|
|
88
|
+
constructor(timeout = 900) {
|
|
89
|
+
__publicField(this, "timeout", timeout);
|
|
90
|
+
__publicField(this, "buffer", []);
|
|
91
|
+
__publicField(this, "timer");
|
|
92
|
+
}
|
|
93
|
+
/** Feed an event; returns the hotkeys (from `hotkeys`) that completed. */
|
|
94
|
+
feed(event, hotkeys) {
|
|
95
|
+
if (["Shift", "Meta", "Control", "Alt"].includes(event.key)) return [];
|
|
96
|
+
this.buffer.push(event), this.buffer.length > 4 && this.buffer.shift(), clearTimeout(this.timer), this.timer = setTimeout(() => this.buffer = [], this.timeout);
|
|
97
|
+
let matched = [];
|
|
98
|
+
for (let hotkey of hotkeys) {
|
|
99
|
+
let combos = parseHotkey(hotkey);
|
|
100
|
+
if (combos.length > this.buffer.length) continue;
|
|
101
|
+
let tail = this.buffer.slice(-combos.length);
|
|
102
|
+
combos.every((c, i) => matchesCombo(tail[i], c)) && matched.push(hotkey);
|
|
103
|
+
}
|
|
104
|
+
return matched.length && (this.buffer = []), matched;
|
|
105
|
+
}
|
|
106
|
+
reset() {
|
|
107
|
+
this.buffer = [], clearTimeout(this.timer);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
function allowedInInput(hotkey) {
|
|
111
|
+
let combos = parseHotkey(hotkey);
|
|
112
|
+
return combos.length === 1 && (combos[0].meta || combos[0].ctrl || combos[0].alt);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export {
|
|
116
|
+
isMac,
|
|
117
|
+
parseHotkey,
|
|
118
|
+
matchesHotkey,
|
|
119
|
+
formatHotkey,
|
|
120
|
+
isEditable,
|
|
121
|
+
SequenceMatcher,
|
|
122
|
+
allowedInInput
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=chunk-PQEHV3D4.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/core/hotkeys.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Keyboard shortcut parsing, matching and formatting.\n *\n * Syntax: modifiers joined with `+`, e.g. `\"mod+k\"`, `\"shift+mod+p\"`, `\"alt+enter\"`.\n * `mod` is \u2318 on Apple platforms and Ctrl elsewhere.\n * Sequences are space separated: `\"g s\"` means press G, then S.\n */\n\nexport const isMac: boolean =\n typeof navigator !== 'undefined' &&\n /Mac|iPhone|iPad|iPod/i.test((navigator as any).userAgentData?.platform ?? navigator.platform ?? navigator.userAgent)\n\nexport interface KeyCombo {\n key: string\n meta: boolean\n ctrl: boolean\n alt: boolean\n shift: boolean\n}\n\nconst KEY_ALIASES: Record<string, string> = {\n esc: 'escape',\n return: 'enter',\n space: ' ',\n up: 'arrowup',\n down: 'arrowdown',\n left: 'arrowleft',\n right: 'arrowright',\n del: 'delete',\n plus: '+',\n}\n\nconst cache = new Map<string, KeyCombo[]>()\n\n/** Parse `\"mod+shift+k\"` or a sequence `\"g s\"` into combos. */\nexport function parseHotkey(hotkey: string): KeyCombo[] {\n let combos = cache.get(hotkey)\n if (combos) return combos\n combos = hotkey\n .trim()\n .split(/\\s+/)\n .map((part) => {\n const keys = part.toLowerCase().split('+')\n // \"mod++\" \u2192 trailing plus key\n if (part.endsWith('++')) keys.splice(keys.length - 2, 2, '+')\n const combo: KeyCombo = { key: '', meta: false, ctrl: false, alt: false, shift: false }\n for (const k of keys) {\n if (k === 'mod') isMac ? (combo.meta = true) : (combo.ctrl = true)\n else if (k === 'cmd' || k === 'meta' || k === 'command') combo.meta = true\n else if (k === 'ctrl' || k === 'control') combo.ctrl = true\n else if (k === 'alt' || k === 'option' || k === 'opt') combo.alt = true\n else if (k === 'shift') combo.shift = true\n else combo.key = KEY_ALIASES[k] ?? k\n }\n return combo\n })\n cache.set(hotkey, combos)\n return combos\n}\n\nfunction eventKey(event: KeyboardEvent): string {\n // Use the physical key for letters/digits so alt/shift combos still match.\n const code = event.code\n if (code?.startsWith('Key')) return code.slice(3).toLowerCase()\n if (code?.startsWith('Digit')) return code.slice(5)\n return (event.key ?? '').toLowerCase()\n}\n\n/** Does a keyboard event match a single combo? */\nexport function matchesCombo(event: KeyboardEvent, combo: KeyCombo): boolean {\n if (event.metaKey !== combo.meta || event.ctrlKey !== combo.ctrl || event.altKey !== combo.alt) return false\n const key = eventKey(event)\n // Shift is implied for symbols like \"?\" \u2014 only enforce it for letters/named keys when specified.\n if (combo.shift !== event.shiftKey && !(combo.key.length === 1 && !/[a-z0-9]/.test(combo.key))) return false\n return key === combo.key || (event.key ?? '').toLowerCase() === combo.key\n}\n\n/** Matches a single-combo hotkey (sequences never match here). */\nexport function matchesHotkey(event: KeyboardEvent, hotkey: string): boolean {\n const combos = parseHotkey(hotkey)\n return combos.length === 1 && matchesCombo(event, combos[0]!)\n}\n\nconst MAC_SYMBOLS: Record<string, string> = {\n meta: '\u2318',\n ctrl: '\u2303',\n alt: '\u2325',\n shift: '\u21E7',\n enter: '\u21B5',\n escape: 'esc',\n backspace: '\u232B',\n delete: '\u2326',\n arrowup: '\u2191',\n arrowdown: '\u2193',\n arrowleft: '\u2190',\n arrowright: '\u2192',\n tab: '\u21E5',\n ' ': 'Space',\n}\n\nconst PC_NAMES: Record<string, string> = {\n meta: 'Win',\n ctrl: 'Ctrl',\n alt: 'Alt',\n shift: 'Shift',\n enter: '\u21B5',\n escape: 'Esc',\n backspace: '\u232B',\n delete: 'Del',\n arrowup: '\u2191',\n arrowdown: '\u2193',\n arrowleft: '\u2190',\n arrowright: '\u2192',\n tab: 'Tab',\n ' ': 'Space',\n}\n\n/** Format a hotkey as an array of key labels for display, e.g. `[\"\u2318\", \"K\"]`. */\nexport function formatHotkey(hotkey: string): string[][] {\n return parseHotkey(hotkey).map((combo) => {\n const names = isMac ? MAC_SYMBOLS : PC_NAMES\n const out: string[] = []\n if (isMac) {\n if (combo.ctrl) out.push(names.ctrl!)\n if (combo.alt) out.push(names.alt!)\n if (combo.shift) out.push(names.shift!)\n if (combo.meta) out.push(names.meta!)\n } else {\n if (combo.ctrl) out.push(names.ctrl!)\n if (combo.meta) out.push(names.meta!)\n if (combo.alt) out.push(names.alt!)\n if (combo.shift) out.push(names.shift!)\n }\n out.push(names[combo.key] ?? (combo.key.length === 1 ? combo.key.toUpperCase() : capitalize(combo.key)))\n return out\n })\n}\n\nfunction capitalize(s: string) {\n return s.charAt(0).toUpperCase() + s.slice(1)\n}\n\n/** True if the event target is a text field (where single-key shortcuts must not fire). */\nexport function isEditable(target: EventTarget | null): boolean {\n const el = target as HTMLElement | null\n if (!el || !el.tagName) return false\n const tag = el.tagName\n return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || el.isContentEditable\n}\n\n/**\n * Tracks multi-key sequences (\"g s\") across keydown events.\n */\nexport class SequenceMatcher {\n private buffer: KeyboardEvent[] = []\n private timer: ReturnType<typeof setTimeout> | undefined\n\n constructor(private timeout = 900) {}\n\n /** Feed an event; returns the hotkeys (from `hotkeys`) that completed. */\n feed(event: KeyboardEvent, hotkeys: string[]): string[] {\n if (['Shift', 'Meta', 'Control', 'Alt'].includes(event.key)) return []\n this.buffer.push(event)\n if (this.buffer.length > 4) this.buffer.shift()\n clearTimeout(this.timer)\n this.timer = setTimeout(() => (this.buffer = []), this.timeout)\n const matched: string[] = []\n for (const hotkey of hotkeys) {\n const combos = parseHotkey(hotkey)\n if (combos.length > this.buffer.length) continue\n const tail = this.buffer.slice(-combos.length)\n if (combos.every((c, i) => matchesCombo(tail[i]!, c))) matched.push(hotkey)\n }\n if (matched.length) this.buffer = []\n return matched\n }\n\n reset() {\n this.buffer = []\n clearTimeout(this.timer)\n }\n}\n\n/** Whether a hotkey may fire while focus is in a text field (it must use a modifier). */\nexport function allowedInInput(hotkey: string): boolean {\n const combos = parseHotkey(hotkey)\n return combos.length === 1 && (combos[0]!.meta || combos[0]!.ctrl || combos[0]!.alt)\n}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