@dorsk/tsumikit 0.11.0 → 0.11.1
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.
|
@@ -20,6 +20,11 @@
|
|
|
20
20
|
import Badge from '../atoms/Badge.svelte';
|
|
21
21
|
import Icon from '../atoms/Icon.svelte';
|
|
22
22
|
import { filters, freeText, type Query } from '../../query/ast';
|
|
23
|
+
import {
|
|
24
|
+
autoQuoteEdit,
|
|
25
|
+
backspaceEmptyQuotes,
|
|
26
|
+
closingQuoteExit
|
|
27
|
+
} from '../../query/edit';
|
|
23
28
|
import { findField, operatorById, type Schema } from '../../query/schema';
|
|
24
29
|
import { parse } from '../../query/parser';
|
|
25
30
|
import { suggest, type SuggestState } from '../../query/suggest';
|
|
@@ -29,6 +34,7 @@
|
|
|
29
34
|
value = $bindable(''),
|
|
30
35
|
placeholder = 'artist:"Daft Punk" AND year>=2000',
|
|
31
36
|
showChips = true,
|
|
37
|
+
autoQuote = true,
|
|
32
38
|
onchange,
|
|
33
39
|
onsubmit
|
|
34
40
|
}: {
|
|
@@ -38,6 +44,12 @@
|
|
|
38
44
|
placeholder?: string;
|
|
39
45
|
/** Render the parsed, removable filter chips under the bar. */
|
|
40
46
|
showChips?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* When a string field's value step opens (`title:`), auto-insert a `""`
|
|
49
|
+
* pair with the caret inside so multi-word values stay together; Tab exits
|
|
50
|
+
* the quotes. Set false for bare typing where spaces split the value.
|
|
51
|
+
*/
|
|
52
|
+
autoQuote?: boolean;
|
|
41
53
|
/** Fires the parsed AST on every change — feed this to your backend. */
|
|
42
54
|
onchange?: (query: Query, raw: string) => void;
|
|
43
55
|
/** Fires on Enter / clear / chip-remove with the raw query string. */
|
|
@@ -64,6 +76,28 @@
|
|
|
64
76
|
return findField(schema, fieldName)?.label ?? fieldName;
|
|
65
77
|
}
|
|
66
78
|
|
|
79
|
+
function setCaret(pos: number) {
|
|
80
|
+
queueMicrotask(() => {
|
|
81
|
+
if (!el) return;
|
|
82
|
+
el.focus();
|
|
83
|
+
el.setSelectionRange(pos, pos);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function oninput(e: Event) {
|
|
88
|
+
if (autoQuote && el && (e as InputEvent).inputType?.startsWith('insert')) {
|
|
89
|
+
const pos = el.selectionStart ?? value.length;
|
|
90
|
+
const edit = autoQuoteEdit(schema, value, pos);
|
|
91
|
+
if (edit) {
|
|
92
|
+
value = edit.value;
|
|
93
|
+
setCaret(edit.pos);
|
|
94
|
+
queueMicrotask(refresh);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
refresh();
|
|
99
|
+
}
|
|
100
|
+
|
|
67
101
|
async function refresh() {
|
|
68
102
|
if (!el) return;
|
|
69
103
|
const pos = el.selectionStart ?? value.length;
|
|
@@ -81,18 +115,51 @@
|
|
|
81
115
|
if (!s) return;
|
|
82
116
|
const [a, b] = menu.span;
|
|
83
117
|
value = value.slice(0, a) + s.insert + value.slice(b);
|
|
118
|
+
let caret = s.caret;
|
|
119
|
+
// A field/operator pick that opens a string field's value step gets the
|
|
120
|
+
// same auto-quotes as manual typing.
|
|
121
|
+
if (autoQuote && s.advance) {
|
|
122
|
+
const edit = autoQuoteEdit(schema, value, caret);
|
|
123
|
+
if (edit) {
|
|
124
|
+
value = edit.value;
|
|
125
|
+
caret = edit.pos;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
84
128
|
// Restore caret after the inserted text, then re-run suggestions if the
|
|
85
129
|
// step advanced (field → operator → value) so the flow keeps going.
|
|
86
130
|
queueMicrotask(() => {
|
|
87
131
|
if (!el) return;
|
|
88
132
|
el.focus();
|
|
89
|
-
el.setSelectionRange(
|
|
133
|
+
el.setSelectionRange(caret, caret);
|
|
90
134
|
if (s.advance) refresh();
|
|
91
135
|
else open = false;
|
|
92
136
|
});
|
|
93
137
|
}
|
|
94
138
|
|
|
95
139
|
function onkeydown(e: KeyboardEvent) {
|
|
140
|
+
if (autoQuote && el) {
|
|
141
|
+
const pos = el.selectionStart ?? value.length;
|
|
142
|
+
if (e.key === 'Tab') {
|
|
143
|
+
// Inside auto-quotes Tab exits them (takes priority over accepting a
|
|
144
|
+
// dropdown item); elsewhere it falls through to accept.
|
|
145
|
+
const exit = closingQuoteExit(value, pos);
|
|
146
|
+
if (exit !== null) {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
open = false;
|
|
149
|
+
setCaret(exit);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
} else if (e.key === 'Backspace') {
|
|
153
|
+
const edit = backspaceEmptyQuotes(value, pos);
|
|
154
|
+
if (edit) {
|
|
155
|
+
e.preventDefault();
|
|
156
|
+
value = edit.value;
|
|
157
|
+
setCaret(edit.pos);
|
|
158
|
+
queueMicrotask(refresh);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
96
163
|
if (!open || !menu) {
|
|
97
164
|
if (e.key === 'Enter') submit();
|
|
98
165
|
return;
|
|
@@ -150,7 +217,7 @@
|
|
|
150
217
|
spellcheck="false"
|
|
151
218
|
autocomplete="off"
|
|
152
219
|
{placeholder}
|
|
153
|
-
oninput
|
|
220
|
+
{oninput}
|
|
154
221
|
onclick={refresh}
|
|
155
222
|
onkeyup={(e) => {
|
|
156
223
|
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') refresh();
|
|
@@ -7,6 +7,12 @@ type $$ComponentProps = {
|
|
|
7
7
|
placeholder?: string;
|
|
8
8
|
/** Render the parsed, removable filter chips under the bar. */
|
|
9
9
|
showChips?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* When a string field's value step opens (`title:`), auto-insert a `""`
|
|
12
|
+
* pair with the caret inside so multi-word values stay together; Tab exits
|
|
13
|
+
* the quotes. Set false for bare typing where spaces split the value.
|
|
14
|
+
*/
|
|
15
|
+
autoQuote?: boolean;
|
|
10
16
|
/** Fires the parsed AST on every change — feed this to your backend. */
|
|
11
17
|
onchange?: (query: Query, raw: string) => void;
|
|
12
18
|
/** Fires on Enter / clear / chip-remove with the raw query string. */
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Schema } from './schema';
|
|
2
|
+
/**
|
|
3
|
+
* When the text just before `pos` completes a string field's operator
|
|
4
|
+
* (`title:` / `title!:`) and no quote already follows, return the value with an
|
|
5
|
+
* empty `""` pair spliced in at `pos` and the caret parked between them. Returns
|
|
6
|
+
* `null` when it does not apply.
|
|
7
|
+
*/
|
|
8
|
+
export declare function autoQuoteEdit(schema: Schema, value: string, pos: number): {
|
|
9
|
+
value: string;
|
|
10
|
+
pos: number;
|
|
11
|
+
} | null;
|
|
12
|
+
/** True when the caret sits inside an open quote. */
|
|
13
|
+
export declare function insideQuoteAtCaret(value: string, pos: number): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* If the caret is inside a quote and its closing quote follows, return the index
|
|
16
|
+
* just past that quote (where Tab should move the caret to); otherwise `null`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function closingQuoteExit(value: string, pos: number): number | null;
|
|
19
|
+
/**
|
|
20
|
+
* If the caret sits between an empty matching quote pair (`"|"`), return the
|
|
21
|
+
* value with both quotes removed so a Backspace clears an unwanted auto-quote
|
|
22
|
+
* instead of leaving an orphan.
|
|
23
|
+
*/
|
|
24
|
+
export declare function backspaceEmptyQuotes(value: string, pos: number): {
|
|
25
|
+
value: string;
|
|
26
|
+
pos: number;
|
|
27
|
+
} | null;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
2
|
+
// Editor affordances — pure text edits an input host applies as the user types.
|
|
3
|
+
//
|
|
4
|
+
// The parser treats an unquoted value as ending at the first space, so
|
|
5
|
+
// `title:star wars` means `title:star` + free-text `wars`. To type a multi-word
|
|
6
|
+
// value the user otherwise has to add the quotes by hand. These helpers let the
|
|
7
|
+
// FilterSearchBar auto-insert the `""` pair the moment a string field's value
|
|
8
|
+
// step opens, so spaces land inside the quotes — the already-supported phrase
|
|
9
|
+
// form — without any grammar change. UI-free and framework-agnostic.
|
|
10
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
11
|
+
import { findField } from './schema';
|
|
12
|
+
// A field name immediately followed by a string operator (`:` contains / `!:`
|
|
13
|
+
// not-contains), anchored at the caret.
|
|
14
|
+
const STRING_CLAUSE = /([A-Za-z_][\w-]*)(!?:)$/;
|
|
15
|
+
function isQuote(c) {
|
|
16
|
+
return c === '"' || c === "'";
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* When the text just before `pos` completes a string field's operator
|
|
20
|
+
* (`title:` / `title!:`) and no quote already follows, return the value with an
|
|
21
|
+
* empty `""` pair spliced in at `pos` and the caret parked between them. Returns
|
|
22
|
+
* `null` when it does not apply.
|
|
23
|
+
*/
|
|
24
|
+
export function autoQuoteEdit(schema, value, pos) {
|
|
25
|
+
if (isQuote(value[pos]))
|
|
26
|
+
return null;
|
|
27
|
+
const m = STRING_CLAUSE.exec(value.slice(0, pos));
|
|
28
|
+
if (!m)
|
|
29
|
+
return null;
|
|
30
|
+
const field = findField(schema, m[1]);
|
|
31
|
+
if (field?.type !== 'string')
|
|
32
|
+
return null;
|
|
33
|
+
return { value: `${value.slice(0, pos)}""${value.slice(pos)}`, pos: pos + 1 };
|
|
34
|
+
}
|
|
35
|
+
// Scan from the start tracking quote state (spaces inside quotes are NOT
|
|
36
|
+
// boundaries), so the caret's quote context is correct even for `"star wars"`.
|
|
37
|
+
function openQuoteAt(value, pos) {
|
|
38
|
+
let quote = null;
|
|
39
|
+
for (let i = 0; i < pos; i++) {
|
|
40
|
+
const c = value[i];
|
|
41
|
+
if (quote) {
|
|
42
|
+
if (c === quote)
|
|
43
|
+
quote = null;
|
|
44
|
+
}
|
|
45
|
+
else if (isQuote(c)) {
|
|
46
|
+
quote = c;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return quote;
|
|
50
|
+
}
|
|
51
|
+
/** True when the caret sits inside an open quote. */
|
|
52
|
+
export function insideQuoteAtCaret(value, pos) {
|
|
53
|
+
return openQuoteAt(value, pos) !== null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* If the caret is inside a quote and its closing quote follows, return the index
|
|
57
|
+
* just past that quote (where Tab should move the caret to); otherwise `null`.
|
|
58
|
+
*/
|
|
59
|
+
export function closingQuoteExit(value, pos) {
|
|
60
|
+
const quote = openQuoteAt(value, pos);
|
|
61
|
+
if (!quote)
|
|
62
|
+
return null;
|
|
63
|
+
for (let i = pos; i < value.length; i++) {
|
|
64
|
+
if (value[i] === quote)
|
|
65
|
+
return i + 1;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* If the caret sits between an empty matching quote pair (`"|"`), return the
|
|
71
|
+
* value with both quotes removed so a Backspace clears an unwanted auto-quote
|
|
72
|
+
* instead of leaving an orphan.
|
|
73
|
+
*/
|
|
74
|
+
export function backspaceEmptyQuotes(value, pos) {
|
|
75
|
+
const open = value[pos - 1];
|
|
76
|
+
if (!isQuote(open) || value[pos] !== open)
|
|
77
|
+
return null;
|
|
78
|
+
return { value: value.slice(0, pos - 1) + value.slice(pos + 1), pos: pos - 1 };
|
|
79
|
+
}
|
package/dist/query/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type { AndNode, ExprNode, FilterNode, LeafNode, NotNode, OrNode, Query, QueryNode, TextNode, } from './ast';
|
|
2
2
|
export { filters, freeText, walk } from './ast';
|
|
3
|
+
export { autoQuoteEdit, backspaceEmptyQuotes, closingQuoteExit, insideQuoteAtCaret, } from './edit';
|
|
3
4
|
export { parse } from './parser';
|
|
4
5
|
export { compilePredicate, serialize, serializeFilter, toSql } from './query';
|
|
5
6
|
export type { FieldDef, FieldType, Operator, OperatorId, Schema, ValueOption, ValueProvider, } from './schema';
|
package/dist/query/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// no Svelte, no DOM. A host app supplies a `Schema` (optionally with async
|
|
4
4
|
// `ValueProvider`s) and consumes the `Query` AST against its own backend.
|
|
5
5
|
export { filters, freeText, walk } from './ast';
|
|
6
|
+
export { autoQuoteEdit, backspaceEmptyQuotes, closingQuoteExit, insideQuoteAtCaret, } from './edit';
|
|
6
7
|
export { parse } from './parser';
|
|
7
8
|
export { compilePredicate, serialize, serializeFilter, toSql } from './query';
|
|
8
9
|
export { defaultOperator, findField, OPERATORS, operatorByCode, operatorById, operatorsFor, resolveValues, } from './schema';
|
package/package.json
CHANGED