@tooark/code 1.0.1 → 1.2.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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { EditorView } from '@codemirror/view';
2
- import { ArkThemeSelected } from '@tooark/tokens';
2
+ import { ArkIntent, ArkThemeSelected, ArkSize } from '@tooark/tokens';
3
3
  import { Completion, CompletionContext, CompletionResult } from '@codemirror/autocomplete';
4
4
 
5
5
  /** Tema do editor. "auto" segue o color-scheme do host, ou a preferência do sistema sem lado fixo. */
@@ -19,6 +19,20 @@ type ArkCodeCompletionSource = (context: CompletionContext) => CompletionResult
19
19
  * Sem ele, só `json` tem formatação (nativa, com o recuo configurado).
20
20
  */
21
21
  type ArkCodeFormatter = (value: string, language: ArkCodeLanguage) => string | Promise<string>;
22
+ /**
23
+ * Variável `{{chave}}` informada pelo app: oferecida depois de `{{` e pintada no texto. O escopo é livre (a lib não
24
+ * conhece nenhum); quando a mesma chave existe em mais de um escopo, o app manda só a que vale.
25
+ */
26
+ type ArkCodeVariable = {
27
+ /** Chave usada em `{{chave}}`: letras, dígitos, `_`, `.`, `-` e `$`. */
28
+ key: string;
29
+ /** Nome do escopo: vai para `data-scope` no token, para o detalhe da completion e para o tooltip. */
30
+ scope?: string;
31
+ /** Cor do token (fundo e texto suaves do intent). Padrão: "primary". */
32
+ intent?: ArkIntent;
33
+ /** Valor mostrado no tooltip e na completion; o app deixa de fora o que for segredo. */
34
+ value?: string;
35
+ };
22
36
  /** Opções de `createCodeEditor`; cada uma tem um `set*` correspondente na instância. */
23
37
  type ArkCodeEditorOptions = {
24
38
  /** Texto inicial. Padrão: "". */
@@ -51,6 +65,21 @@ type ArkCodeEditorOptions = {
51
65
  autocomplete?: boolean;
52
66
  /** Chaves oferecidas como completions depois de `{{`; a escolha insere `{{chave}}`. Padrão: nenhuma. */
53
67
  variableKeys?: string[];
68
+ /**
69
+ * Variáveis com escopo e cor: oferecidas depois de `{{` como `variableKeys` e pintadas no texto, com tooltip do
70
+ * escopo e do valor. Padrão: nenhuma (nada é pintado).
71
+ */
72
+ variables?: ArkCodeVariable[];
73
+ /** Pinta em `danger`, sublinhado, o `{{chave}}` que não está em `variables` nem em `variableKeys`. Padrão: false. */
74
+ markUnknownVariables?: boolean;
75
+ /**
76
+ * Campo de uma linha na altura dos controles (`size`): sem calhas nem linha ativa, Enter chama `onSubmit` em vez
77
+ * de quebrar a linha, quebras coladas são removidas (como num `<input>`), Tab sai do editor e Ctrl+F fica com o
78
+ * navegador. Padrão: false.
79
+ */
80
+ singleLine?: boolean;
81
+ /** Escala da fonte e do recuo lateral; em `singleLine`, também a altura (`--ark-size-*`). Padrão: "md". */
82
+ size?: ArkSize;
54
83
  /** Palavras oferecidas como completion em qualquer linguagem (chaves de um schema, nomes conhecidos). */
55
84
  completions?: ArkCodeCompletion[];
56
85
  /** Fonte de completion própria, além das da linguagem. */
@@ -59,10 +88,15 @@ type ArkCodeEditorOptions = {
59
88
  formatter?: ArkCodeFormatter;
60
89
  /** Chamado a cada edição do usuário com o texto atual (não dispara em `setValue`). */
61
90
  onChange?: (value: string) => void;
91
+ /** Chamado com o texto atual quando Enter é pressionado em `singleLine` (fora da lista de completions). */
92
+ onSubmit?: (value: string) => void;
62
93
  /** Chamado quando `format()` falha (JSON inválido, formatador lançou). */
63
94
  onFormatError?: (error: unknown) => void;
64
95
  };
65
96
 
97
+ /** `HTMLElement` no navegador; fora dele, uma classe vazia para o módulo poder ser importado no servidor. */
98
+ declare const HTMLElementBase: typeof HTMLElement;
99
+
66
100
  /**
67
101
  * Custom Element que renderiza um editor de código CodeMirror 6.
68
102
  *
@@ -81,13 +115,20 @@ type ArkCodeEditorOptions = {
81
115
  * acompanha a troca em tempo de execução (`observeColorScheme` de
82
116
  * @tooark/tokens). O nó do CodeMirror leva `data-ark="code-editor"` e o
83
117
  * `testid` como `data-testid`.
118
+ *
119
+ * Opcionais, desligados até o app pedir: a propriedade `variables` pinta
120
+ * cada `{{chave}}` pelo intent da variável, com tooltip do escopo e do valor;
121
+ * `mark-unknown-variables` pinta em `danger` a chave desconhecida;
122
+ * `single-line` vira um campo de uma linha na altura dos controles (`size`),
123
+ * com Enter emitindo `ark-submit` (`detail: { value }`).
84
124
  */
85
- declare class ArkCodeEditor extends HTMLElement {
125
+ declare class ArkCodeEditor extends HTMLElementBase {
86
126
  static readonly tagName = "ark-code-editor";
87
127
  private instance;
88
128
  /** Texto antes de montar, ou o último conhecido depois de desmontar. */
89
129
  private pendingValue;
90
130
  private keys;
131
+ private vars;
91
132
  private items;
92
133
  private source;
93
134
  private formatFn;
@@ -100,6 +141,18 @@ declare class ArkCodeEditor extends HTMLElement {
100
141
  /** Chaves oferecidas como completions depois de `{{` (propriedade JS). */
101
142
  get variableKeys(): string[];
102
143
  set variableKeys(next: string[] | null | undefined);
144
+ /** Variáveis com escopo e cor (propriedade JS): completam depois de `{{` e pintam o `{{chave}}` no texto. */
145
+ get variables(): ArkCodeVariable[];
146
+ set variables(next: ArkCodeVariable[] | null | undefined);
147
+ /** Pinta em `danger` o `{{chave}}` fora de `variables` e `variableKeys` (atributo `mark-unknown-variables`). */
148
+ get markUnknownVariables(): boolean;
149
+ set markUnknownVariables(value: boolean | string | null | undefined);
150
+ /** Campo de uma linha na altura dos controles (atributo `single-line`); Enter emite `ark-submit`. */
151
+ get singleLine(): boolean;
152
+ set singleLine(value: boolean | string | null | undefined);
153
+ /** Escala da fonte e do recuo; em `single-line`, também a altura (atributo `size`). Padrão: "md". */
154
+ get size(): ArkSize;
155
+ set size(value: ArkSize);
103
156
  /** Palavras oferecidas como completion em qualquer linguagem (propriedade JS, formato do CodeMirror). */
104
157
  get completions(): ArkCodeCompletion[];
105
158
  set completions(next: ArkCodeCompletion[] | null | undefined);
@@ -166,6 +219,12 @@ declare class ArkCodeEditor extends HTMLElement {
166
219
  private teardown;
167
220
  }
168
221
 
222
+ declare global {
223
+ interface HTMLElementTagNameMap {
224
+ "ark-code-editor": ArkCodeEditor;
225
+ }
226
+ }
227
+
169
228
  type ArkCodeEditorInstance = {
170
229
  /** Instância nativa do CodeMirror (uso avançado: dispatch, state, extensões). */
171
230
  readonly view: EditorView;
@@ -201,6 +260,14 @@ type ArkCodeEditorInstance = {
201
260
  setAutocomplete(on: boolean): void;
202
261
  /** Troca as chaves oferecidas depois de `{{`. */
203
262
  setVariableKeys(keys: string[]): void;
263
+ /** Troca as variáveis com escopo e cor (completion, pintura e tooltip); lista vazia deixa de pintar. */
264
+ setVariables(variables: ArkCodeVariable[]): void;
265
+ /** Liga/desliga a marcação em `danger` dos `{{chave}}` desconhecidos. */
266
+ setMarkUnknownVariables(on: boolean): void;
267
+ /** Liga/desliga o campo de uma linha; ligar junta as linhas do documento sem chamar `onChange`. */
268
+ setSingleLine(on: boolean): void;
269
+ /** Troca a escala da fonte e do recuo lateral (e a altura em `singleLine`). */
270
+ setSize(size: ArkSize): void;
204
271
  /** Troca as palavras oferecidas como completion em qualquer linguagem. */
205
272
  setCompletions(items: ArkCodeCompletion[]): void;
206
273
  /** Troca a fonte de completion própria. */
@@ -225,7 +292,8 @@ declare function resolveCodeTheme(theme: ArkCodeTheme | undefined, element?: Ele
225
292
  * fechamento de pares, realce da linha ativa (só editável), busca por Ctrl+F, completions (da linguagem, de
226
293
  * `variableKeys`, de `completions` e de `completionSource`), Tab para recuar com saída por Esc+Tab, recuo por
227
294
  * espaços ou tabulação, fim de linha LF/CRLF na fronteira do valor, formatação por Shift+Alt+F (JSON nativo ou o
228
- * `formatter` do app) e tema pelos tokens.
295
+ * `formatter` do app) e tema pelos tokens. Opcionais, ligados só pelas opções: `variables` pintadas por intent com
296
+ * tooltip, `markUnknownVariables` e o campo `singleLine`.
229
297
  */
230
298
  declare function createCodeEditor(parent: HTMLElement, options?: ArkCodeEditorOptions): ArkCodeEditorInstance;
231
299
 
@@ -236,4 +304,4 @@ declare function createCodeEditor(parent: HTMLElement, options?: ArkCodeEditorOp
236
304
  declare function registerTooarkCode(): void;
237
305
 
238
306
  export { ArkCodeEditor, createCodeEditor, registerTooarkCode, resolveCodeTheme };
239
- export type { ArkCodeCompletion, ArkCodeCompletionSource, ArkCodeEditorInstance, ArkCodeEditorOptions, ArkCodeFormatter, ArkCodeIndentStyle, ArkCodeLanguage, ArkCodeLineEnding, ArkCodeTheme };
307
+ export type { ArkCodeCompletion, ArkCodeCompletionSource, ArkCodeEditorInstance, ArkCodeEditorOptions, ArkCodeFormatter, ArkCodeIndentStyle, ArkCodeLanguage, ArkCodeLineEnding, ArkCodeTheme, ArkCodeVariable };