tiptap-model-language 1.0.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.
@@ -0,0 +1,207 @@
1
+ import { FieldSchema } from 'model-language';
2
+ export { DataSnapshot, FieldDef, FieldSchema, MLType, Diagnostic as MlDiagnostic, Range as MlRange, Severity as MlSeverity, ParseResult, Quickfix, RenderOptions, RenderResult, ValidateOptions, ValidateResult, parse, render, serialize, validate } from 'model-language';
3
+ import { Extension } from '@tiptap/core';
4
+
5
+ type MlFieldType = "string" | "number" | "datetime" | "boolean" | "enum" | "array";
6
+ interface MlField {
7
+ key: string;
8
+ type: MlFieldType;
9
+ /** Friendly label for the chip / autocomplete row. */
10
+ label: string;
11
+ /** Allowed values for enum fields (used only for hints). */
12
+ values?: string[];
13
+ }
14
+ interface MlNamespace {
15
+ key: string;
16
+ label: string;
17
+ /** Runtime-only namespace (e.g. flow.*) — never autocompleted or flagged. */
18
+ dynamic?: boolean;
19
+ fields: MlField[];
20
+ }
21
+ declare const CONTACT_NAMESPACE_KEY = "contact";
22
+ declare const FLOW_NAMESPACE_KEY = "flow";
23
+ /** Fixed namespaces — schema is constant across orgs. */
24
+ declare const STATIC_NAMESPACES: MlNamespace[];
25
+ interface MlFilter {
26
+ name: string;
27
+ hint: string;
28
+ /** Placeholder args appended on insert, e.g. `: "there"`. */
29
+ argTemplate?: string;
30
+ /** Value types this filter accepts (`"any"` = always offered). */
31
+ inputs: MlFieldType[] | "any";
32
+ /** Resulting type (for chaining `a | f1 | f2`); omit = preserves input. */
33
+ output?: MlFieldType;
34
+ }
35
+ /** Example `date` format tokens. The engine uses Intl/LDML tokens (lowercase
36
+ * `yyyy`, `dd`; `EEEE` weekday; `MM`; 24h `HH`, 12h `hh`) — NOT moment/dayjs
37
+ * style — plus `[literal]` escaping. Offered as insert suggestions. */
38
+ declare const DATE_FORMATS: readonly ["yyyy-MM-dd", "MMM d, yyyy", "HH:mm", "h:mm A", "EEEE"];
39
+ /** Engine filters (model-language). Grouped by input type; wrong input passes
40
+ * through unchanged (the engine never throws). */
41
+ declare const ML_FILTERS: MlFilter[];
42
+ interface ParsedFilter {
43
+ name: string;
44
+ args: string;
45
+ }
46
+ interface ParsedToken {
47
+ /** Full path, e.g. "contact.first_name". */
48
+ path: string;
49
+ namespace: string;
50
+ field: string;
51
+ filters: ParsedFilter[];
52
+ /** True for control tokens ({{if …}}, {{for …}}, {{/if}}, …). */
53
+ directive: boolean;
54
+ }
55
+ /** Parse a token's inner text (no braces) into path + filters. */
56
+ declare function parseModelToken(inner: string): ParsedToken;
57
+
58
+ interface ModelTokenOption {
59
+ /** Inner token text to insert (no braces). */
60
+ insert: string;
61
+ label: string;
62
+ hint: string;
63
+ group: string;
64
+ kind: "path" | "filter" | "block";
65
+ /** Terminal step → wrap `{{…}}` and finish. Non-terminal → keep the token
66
+ * open so the next stage (operator / value / filter) can autocomplete. */
67
+ close: boolean;
68
+ }
69
+ /**
70
+ * Staged autocomplete: each pick advances one step and keeps the token open
71
+ * until a terminal step, so the flow is field → operator → value/filter → done.
72
+ */
73
+ declare function buildModelOptions(namespaces: MlNamespace[], query: string): ModelTokenOption[];
74
+
75
+ type DiagnosticSeverity = "error" | "warning" | "info";
76
+ /** A diagnostic collapsed to a single field path (worst severity wins). */
77
+ interface TokenDiagnostic {
78
+ severity: DiagnosticSeverity;
79
+ message: string;
80
+ code: string;
81
+ }
82
+ /** The flat diagnostic shape surfaced to the host via `onResult`. */
83
+ interface TemplateDiagnostic {
84
+ code: string;
85
+ severity: DiagnosticSeverity;
86
+ message: string;
87
+ fieldPath?: string | null;
88
+ }
89
+ interface ModelValidationResult {
90
+ diagnostics: TemplateDiagnostic[];
91
+ maxTokenEstimate: number | null;
92
+ }
93
+ /**
94
+ * Every user-facing string the extension renders — override any of them for
95
+ * i18n. Some are templated (they receive the block name / operator / tag).
96
+ */
97
+ interface ModelLanguageLabels {
98
+ /** Shown when a value path in a condition isn't followed by an operator. */
99
+ expectedOperator: string;
100
+ /** A block opener (`if` / `for` / `#name`) that never closed in scope. */
101
+ unclosedBlock: (name: string) => string;
102
+ /** A close/branch tag with no matching opener. */
103
+ noOpenBlock: (name: string) => string;
104
+ /** Quick-fix: add a `| default: …` filter. */
105
+ addDefault: string;
106
+ /** Quick-fix: replace a mistyped operator. */
107
+ changeTo: (operator: string) => string;
108
+ /** Quick-fix: insert a close tag, e.g. `{{/if}}`. */
109
+ addCloseTag: (tag: string) => string;
110
+ /** Fallback quick-fix button text. */
111
+ quickFix: string;
112
+ /** Empty autocomplete menu. */
113
+ noMatches: string;
114
+ /** Severity chip text in the hover tooltip (rendered uppercase). */
115
+ severity: Record<DiagnosticSeverity, string>;
116
+ }
117
+ interface ModelSyntaxOptions {
118
+ /** Initial namespaces (autocomplete + highlighting). Static case only — for
119
+ * async / changing data push via the `setModelData` command instead. */
120
+ namespaces: MlNamespace[];
121
+ /** Initial org field schema (local validation). Static case only — for
122
+ * async / changing data push via the `setModelData` command instead. */
123
+ schema: FieldSchema;
124
+ /** Disable the local `validate()` pass entirely (structural squiggles stay). */
125
+ skipValidation: boolean;
126
+ /** Debounce for the validation pass, ms. */
127
+ debounceMs: number;
128
+ /** Which severities to render inline. Default: all three. */
129
+ severities: DiagnosticSeverity[];
130
+ /** Override any user-facing string (merged over the English defaults). */
131
+ labels: Partial<ModelLanguageLabels>;
132
+ /** Localize an engine diagnostic message (ML001/ML101/…) — return undefined
133
+ * to keep the engine's English. Branch on `code`, never on `message`. */
134
+ translateDiagnostic?: (d: TemplateDiagnostic) => string | undefined;
135
+ /** Surface hook for the full result (e.g. a diagnostics list + token meter). */
136
+ onResult?: (result: ModelValidationResult) => void;
137
+ }
138
+ interface ModelSyntaxStorage {
139
+ timer: ReturnType<typeof setTimeout> | null;
140
+ }
141
+ /** English defaults for every label. */
142
+ declare const DEFAULT_LABELS: ModelLanguageLabels;
143
+ /** Merge host overrides over the English defaults. */
144
+ declare function resolveLabels(overrides?: Partial<ModelLanguageLabels>): ModelLanguageLabels;
145
+
146
+ /** Friendly chip label for a token: resolves the path to a field label and
147
+ * appends a short filter summary ("First name · default"). Control tokens
148
+ * ({{if …}}, {{for …}}) render verbatim. */
149
+ declare function modelTokenDisplay(inner: string, namespaces: MlNamespace[]): string;
150
+ type ModelTokenTone = "value" | "directive" | "unknown";
151
+ /** Soft classification for chip colour. `validateTemplate` is authoritative for
152
+ * real errors — "unknown" is only a hint when the path can't be resolved. */
153
+ declare function modelTokenTone(inner: string, namespaces: MlNamespace[]): ModelTokenTone;
154
+
155
+ declare module "@tiptap/core" {
156
+ interface Commands<ReturnType> {
157
+ modelSyntax: {
158
+ /** Push the namespaces (autocomplete + highlighting) and the org field
159
+ * schema (local `validate()`). Re-runs validation against the schema. */
160
+ setModelData: (data: {
161
+ namespaces: MlNamespace[];
162
+ schema?: FieldSchema;
163
+ }) => ReturnType;
164
+ };
165
+ }
166
+ }
167
+ /**
168
+ * Self-contained model-language editing extension. Add it to any Tiptap editor
169
+ * and it owns the whole experience: raw-text `{{…}}` syntax highlighting, a `{{`
170
+ * autocomplete (variables / filters / control blocks), `flow.<cardId>` hover
171
+ * highlighting, hover diagnostics + quick-fixes, and live validation via the
172
+ * `model-language` package. The host pushes namespaces + schema via
173
+ * `setModelData`; everything else is configured through options.
174
+ */
175
+ declare const ModelSyntax: Extension<ModelSyntaxOptions, ModelSyntaxStorage>;
176
+
177
+ type HlKind = "keyword" | "operator" | "math" | "string" | "number" | "filter" | "func" | "path" | "punct";
178
+ interface HlToken {
179
+ start: number;
180
+ end: number;
181
+ kind: HlKind;
182
+ }
183
+ declare const HL_CLASS: Record<HlKind, string>;
184
+ /** Tokenize the raw inner text of a `{{ … }}` (no braces) into coloured spans. */
185
+ declare function tokenizeExpression(s: string): HlToken[];
186
+
187
+ /**
188
+ * Merge the autocomplete namespaces (so every offered field — incl. dynamic
189
+ * `flow.*` — is known to `validate()` and never flagged ML101) with the
190
+ * authoritative org schema (accurate type / nullable / enum values).
191
+ */
192
+ declare function buildValidateSchema(namespaces: MlNamespace[], orgSchema: FieldSchema): FieldSchema;
193
+
194
+ /** Collapse diagnostics to their field path, keeping the worst severity. */
195
+ declare function diagnosticsByPath(diags: TemplateDiagnostic[]): Map<string, TokenDiagnostic>;
196
+ /**
197
+ * Type-appropriate `| default: …` filter for the "Add default" quick-fix, so
198
+ * an enum/array defaults to a real option, a number to `0`, etc.
199
+ */
200
+ declare function defaultFilterFor(field?: MlField): string;
201
+ /**
202
+ * Closest operator whose name starts with the mistyped word (shortest wins),
203
+ * e.g. "contai" → "contains". Null when nothing plausibly matches.
204
+ */
205
+ declare function suggestOperator(word: string): string | null;
206
+
207
+ export { CONTACT_NAMESPACE_KEY, DATE_FORMATS, DEFAULT_LABELS, type DiagnosticSeverity, FLOW_NAMESPACE_KEY, HL_CLASS, type HlKind, ML_FILTERS, type MlField, type MlFieldType, type MlFilter, type MlNamespace, type ModelLanguageLabels, ModelSyntax, type ModelSyntaxOptions, type ModelTokenOption, type ModelTokenTone, type ModelValidationResult, type ParsedToken, STATIC_NAMESPACES, type TemplateDiagnostic, type TokenDiagnostic, buildModelOptions, buildValidateSchema, defaultFilterFor, diagnosticsByPath, modelTokenDisplay, modelTokenTone, parseModelToken, resolveLabels, suggestOperator, tokenizeExpression };
@@ -0,0 +1,207 @@
1
+ import { FieldSchema } from 'model-language';
2
+ export { DataSnapshot, FieldDef, FieldSchema, MLType, Diagnostic as MlDiagnostic, Range as MlRange, Severity as MlSeverity, ParseResult, Quickfix, RenderOptions, RenderResult, ValidateOptions, ValidateResult, parse, render, serialize, validate } from 'model-language';
3
+ import { Extension } from '@tiptap/core';
4
+
5
+ type MlFieldType = "string" | "number" | "datetime" | "boolean" | "enum" | "array";
6
+ interface MlField {
7
+ key: string;
8
+ type: MlFieldType;
9
+ /** Friendly label for the chip / autocomplete row. */
10
+ label: string;
11
+ /** Allowed values for enum fields (used only for hints). */
12
+ values?: string[];
13
+ }
14
+ interface MlNamespace {
15
+ key: string;
16
+ label: string;
17
+ /** Runtime-only namespace (e.g. flow.*) — never autocompleted or flagged. */
18
+ dynamic?: boolean;
19
+ fields: MlField[];
20
+ }
21
+ declare const CONTACT_NAMESPACE_KEY = "contact";
22
+ declare const FLOW_NAMESPACE_KEY = "flow";
23
+ /** Fixed namespaces — schema is constant across orgs. */
24
+ declare const STATIC_NAMESPACES: MlNamespace[];
25
+ interface MlFilter {
26
+ name: string;
27
+ hint: string;
28
+ /** Placeholder args appended on insert, e.g. `: "there"`. */
29
+ argTemplate?: string;
30
+ /** Value types this filter accepts (`"any"` = always offered). */
31
+ inputs: MlFieldType[] | "any";
32
+ /** Resulting type (for chaining `a | f1 | f2`); omit = preserves input. */
33
+ output?: MlFieldType;
34
+ }
35
+ /** Example `date` format tokens. The engine uses Intl/LDML tokens (lowercase
36
+ * `yyyy`, `dd`; `EEEE` weekday; `MM`; 24h `HH`, 12h `hh`) — NOT moment/dayjs
37
+ * style — plus `[literal]` escaping. Offered as insert suggestions. */
38
+ declare const DATE_FORMATS: readonly ["yyyy-MM-dd", "MMM d, yyyy", "HH:mm", "h:mm A", "EEEE"];
39
+ /** Engine filters (model-language). Grouped by input type; wrong input passes
40
+ * through unchanged (the engine never throws). */
41
+ declare const ML_FILTERS: MlFilter[];
42
+ interface ParsedFilter {
43
+ name: string;
44
+ args: string;
45
+ }
46
+ interface ParsedToken {
47
+ /** Full path, e.g. "contact.first_name". */
48
+ path: string;
49
+ namespace: string;
50
+ field: string;
51
+ filters: ParsedFilter[];
52
+ /** True for control tokens ({{if …}}, {{for …}}, {{/if}}, …). */
53
+ directive: boolean;
54
+ }
55
+ /** Parse a token's inner text (no braces) into path + filters. */
56
+ declare function parseModelToken(inner: string): ParsedToken;
57
+
58
+ interface ModelTokenOption {
59
+ /** Inner token text to insert (no braces). */
60
+ insert: string;
61
+ label: string;
62
+ hint: string;
63
+ group: string;
64
+ kind: "path" | "filter" | "block";
65
+ /** Terminal step → wrap `{{…}}` and finish. Non-terminal → keep the token
66
+ * open so the next stage (operator / value / filter) can autocomplete. */
67
+ close: boolean;
68
+ }
69
+ /**
70
+ * Staged autocomplete: each pick advances one step and keeps the token open
71
+ * until a terminal step, so the flow is field → operator → value/filter → done.
72
+ */
73
+ declare function buildModelOptions(namespaces: MlNamespace[], query: string): ModelTokenOption[];
74
+
75
+ type DiagnosticSeverity = "error" | "warning" | "info";
76
+ /** A diagnostic collapsed to a single field path (worst severity wins). */
77
+ interface TokenDiagnostic {
78
+ severity: DiagnosticSeverity;
79
+ message: string;
80
+ code: string;
81
+ }
82
+ /** The flat diagnostic shape surfaced to the host via `onResult`. */
83
+ interface TemplateDiagnostic {
84
+ code: string;
85
+ severity: DiagnosticSeverity;
86
+ message: string;
87
+ fieldPath?: string | null;
88
+ }
89
+ interface ModelValidationResult {
90
+ diagnostics: TemplateDiagnostic[];
91
+ maxTokenEstimate: number | null;
92
+ }
93
+ /**
94
+ * Every user-facing string the extension renders — override any of them for
95
+ * i18n. Some are templated (they receive the block name / operator / tag).
96
+ */
97
+ interface ModelLanguageLabels {
98
+ /** Shown when a value path in a condition isn't followed by an operator. */
99
+ expectedOperator: string;
100
+ /** A block opener (`if` / `for` / `#name`) that never closed in scope. */
101
+ unclosedBlock: (name: string) => string;
102
+ /** A close/branch tag with no matching opener. */
103
+ noOpenBlock: (name: string) => string;
104
+ /** Quick-fix: add a `| default: …` filter. */
105
+ addDefault: string;
106
+ /** Quick-fix: replace a mistyped operator. */
107
+ changeTo: (operator: string) => string;
108
+ /** Quick-fix: insert a close tag, e.g. `{{/if}}`. */
109
+ addCloseTag: (tag: string) => string;
110
+ /** Fallback quick-fix button text. */
111
+ quickFix: string;
112
+ /** Empty autocomplete menu. */
113
+ noMatches: string;
114
+ /** Severity chip text in the hover tooltip (rendered uppercase). */
115
+ severity: Record<DiagnosticSeverity, string>;
116
+ }
117
+ interface ModelSyntaxOptions {
118
+ /** Initial namespaces (autocomplete + highlighting). Static case only — for
119
+ * async / changing data push via the `setModelData` command instead. */
120
+ namespaces: MlNamespace[];
121
+ /** Initial org field schema (local validation). Static case only — for
122
+ * async / changing data push via the `setModelData` command instead. */
123
+ schema: FieldSchema;
124
+ /** Disable the local `validate()` pass entirely (structural squiggles stay). */
125
+ skipValidation: boolean;
126
+ /** Debounce for the validation pass, ms. */
127
+ debounceMs: number;
128
+ /** Which severities to render inline. Default: all three. */
129
+ severities: DiagnosticSeverity[];
130
+ /** Override any user-facing string (merged over the English defaults). */
131
+ labels: Partial<ModelLanguageLabels>;
132
+ /** Localize an engine diagnostic message (ML001/ML101/…) — return undefined
133
+ * to keep the engine's English. Branch on `code`, never on `message`. */
134
+ translateDiagnostic?: (d: TemplateDiagnostic) => string | undefined;
135
+ /** Surface hook for the full result (e.g. a diagnostics list + token meter). */
136
+ onResult?: (result: ModelValidationResult) => void;
137
+ }
138
+ interface ModelSyntaxStorage {
139
+ timer: ReturnType<typeof setTimeout> | null;
140
+ }
141
+ /** English defaults for every label. */
142
+ declare const DEFAULT_LABELS: ModelLanguageLabels;
143
+ /** Merge host overrides over the English defaults. */
144
+ declare function resolveLabels(overrides?: Partial<ModelLanguageLabels>): ModelLanguageLabels;
145
+
146
+ /** Friendly chip label for a token: resolves the path to a field label and
147
+ * appends a short filter summary ("First name · default"). Control tokens
148
+ * ({{if …}}, {{for …}}) render verbatim. */
149
+ declare function modelTokenDisplay(inner: string, namespaces: MlNamespace[]): string;
150
+ type ModelTokenTone = "value" | "directive" | "unknown";
151
+ /** Soft classification for chip colour. `validateTemplate` is authoritative for
152
+ * real errors — "unknown" is only a hint when the path can't be resolved. */
153
+ declare function modelTokenTone(inner: string, namespaces: MlNamespace[]): ModelTokenTone;
154
+
155
+ declare module "@tiptap/core" {
156
+ interface Commands<ReturnType> {
157
+ modelSyntax: {
158
+ /** Push the namespaces (autocomplete + highlighting) and the org field
159
+ * schema (local `validate()`). Re-runs validation against the schema. */
160
+ setModelData: (data: {
161
+ namespaces: MlNamespace[];
162
+ schema?: FieldSchema;
163
+ }) => ReturnType;
164
+ };
165
+ }
166
+ }
167
+ /**
168
+ * Self-contained model-language editing extension. Add it to any Tiptap editor
169
+ * and it owns the whole experience: raw-text `{{…}}` syntax highlighting, a `{{`
170
+ * autocomplete (variables / filters / control blocks), `flow.<cardId>` hover
171
+ * highlighting, hover diagnostics + quick-fixes, and live validation via the
172
+ * `model-language` package. The host pushes namespaces + schema via
173
+ * `setModelData`; everything else is configured through options.
174
+ */
175
+ declare const ModelSyntax: Extension<ModelSyntaxOptions, ModelSyntaxStorage>;
176
+
177
+ type HlKind = "keyword" | "operator" | "math" | "string" | "number" | "filter" | "func" | "path" | "punct";
178
+ interface HlToken {
179
+ start: number;
180
+ end: number;
181
+ kind: HlKind;
182
+ }
183
+ declare const HL_CLASS: Record<HlKind, string>;
184
+ /** Tokenize the raw inner text of a `{{ … }}` (no braces) into coloured spans. */
185
+ declare function tokenizeExpression(s: string): HlToken[];
186
+
187
+ /**
188
+ * Merge the autocomplete namespaces (so every offered field — incl. dynamic
189
+ * `flow.*` — is known to `validate()` and never flagged ML101) with the
190
+ * authoritative org schema (accurate type / nullable / enum values).
191
+ */
192
+ declare function buildValidateSchema(namespaces: MlNamespace[], orgSchema: FieldSchema): FieldSchema;
193
+
194
+ /** Collapse diagnostics to their field path, keeping the worst severity. */
195
+ declare function diagnosticsByPath(diags: TemplateDiagnostic[]): Map<string, TokenDiagnostic>;
196
+ /**
197
+ * Type-appropriate `| default: …` filter for the "Add default" quick-fix, so
198
+ * an enum/array defaults to a real option, a number to `0`, etc.
199
+ */
200
+ declare function defaultFilterFor(field?: MlField): string;
201
+ /**
202
+ * Closest operator whose name starts with the mistyped word (shortest wins),
203
+ * e.g. "contai" → "contains". Null when nothing plausibly matches.
204
+ */
205
+ declare function suggestOperator(word: string): string | null;
206
+
207
+ export { CONTACT_NAMESPACE_KEY, DATE_FORMATS, DEFAULT_LABELS, type DiagnosticSeverity, FLOW_NAMESPACE_KEY, HL_CLASS, type HlKind, ML_FILTERS, type MlField, type MlFieldType, type MlFilter, type MlNamespace, type ModelLanguageLabels, ModelSyntax, type ModelSyntaxOptions, type ModelTokenOption, type ModelTokenTone, type ModelValidationResult, type ParsedToken, STATIC_NAMESPACES, type TemplateDiagnostic, type TokenDiagnostic, buildModelOptions, buildValidateSchema, defaultFilterFor, diagnosticsByPath, modelTokenDisplay, modelTokenTone, parseModelToken, resolveLabels, suggestOperator, tokenizeExpression };