@loccal/ui 0.3.0 → 0.4.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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Gerado por changesets a cada release. Entradas prefixadas pelo nome do componente.
|
|
4
4
|
|
|
5
|
+
## 0.4.0
|
|
6
|
+
|
|
7
|
+
### Minor
|
|
8
|
+
|
|
9
|
+
- code-editor: componente novo. Editor de codigo sem dependencias (textarea sobre a camada colorida de `code-view`), com numeracao, Tab, indentacao automatica, `onSave` (Ctrl/Cmd+S), `readOnly`, linha inicial e fonte configuravel
|
|
10
|
+
|
|
5
11
|
## 0.3.0
|
|
6
12
|
|
|
7
13
|
### Minor
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type LanguageId } from "../../lib/syntax.js";
|
|
3
|
+
type CodeEditorProps = {
|
|
4
|
+
/** Texto atual (controlado). Use `\n` como quebra de linha. */
|
|
5
|
+
value: string;
|
|
6
|
+
/** Chamado a cada alteracao com o texto completo. */
|
|
7
|
+
onValueChange: (value: string) => void;
|
|
8
|
+
/** Chamado em Ctrl+S / Cmd+S. O padrao do navegador (salvar pagina) e sempre impedido quando ha `onSave`. */
|
|
9
|
+
onSave?: (value: string) => void;
|
|
10
|
+
/** Linguagem (ex.: "go", "html", "ts"). Sem valor, e deduzida de `fileName`. */
|
|
11
|
+
language?: LanguageId | string;
|
|
12
|
+
/** Nome do arquivo, usado para deduzir a linguagem. */
|
|
13
|
+
fileName?: string;
|
|
14
|
+
/** Numeracao de linhas (padrao: ligada). */
|
|
15
|
+
lineNumbers?: boolean;
|
|
16
|
+
/** Tamanho da fonte em px (padrao 13). A familia vem de `style.fontFamily` ou da classe `font-mono`. */
|
|
17
|
+
fontSize?: number;
|
|
18
|
+
/** Altura de cada linha em px (padrao: 1,55 x fontSize). */
|
|
19
|
+
lineHeight?: number;
|
|
20
|
+
/** Largura de um Tab em colunas (padrao 4). */
|
|
21
|
+
tabSize?: number;
|
|
22
|
+
/** Foco automatico ao montar. */
|
|
23
|
+
autoFocus?: boolean;
|
|
24
|
+
/** Linha (1-based) onde colocar o cursor ao montar. */
|
|
25
|
+
line?: number;
|
|
26
|
+
readOnly?: boolean;
|
|
27
|
+
className?: string;
|
|
28
|
+
style?: React.CSSProperties;
|
|
29
|
+
"aria-label"?: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Editor de codigo sem dependencias: um textarea transparente sobre uma camada com o mesmo texto colorido
|
|
33
|
+
* (o realce de `CodeView`), com numeracao, indentacao automatica e Tab. O textarea e quem rola; a camada colorida e a
|
|
34
|
+
* numeracao acompanham o scroll. Nao quebra linhas (garante o alinhamento exato entre as camadas).
|
|
35
|
+
*/
|
|
36
|
+
declare function CodeEditor({ value, onValueChange, onSave, language, fileName, lineNumbers, fontSize, lineHeight, tabSize, autoFocus, line, readOnly, className, style, ...props }: CodeEditorProps): React.JSX.Element;
|
|
37
|
+
export { CodeEditor };
|
|
38
|
+
export type { CodeEditorProps };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { languageForFile, tokenizeLinesOf } from "../../lib/syntax.js";
|
|
5
|
+
import { cn } from "../../lib/utils.js";
|
|
6
|
+
/** Acima deste numero de linhas o realce e desligado (o texto continua editavel). */
|
|
7
|
+
const HIGHLIGHT_LIMIT = 20000;
|
|
8
|
+
/**
|
|
9
|
+
* Editor de codigo sem dependencias: um textarea transparente sobre uma camada com o mesmo texto colorido
|
|
10
|
+
* (o realce de `CodeView`), com numeracao, indentacao automatica e Tab. O textarea e quem rola; a camada colorida e a
|
|
11
|
+
* numeracao acompanham o scroll. Nao quebra linhas (garante o alinhamento exato entre as camadas).
|
|
12
|
+
*/
|
|
13
|
+
function CodeEditor({ value, onValueChange, onSave, language, fileName, lineNumbers = true, fontSize = 13, lineHeight, tabSize = 4, autoFocus = true, line, readOnly, className, style, ...props }) {
|
|
14
|
+
const lh = lineHeight ?? Math.round(fontSize * 1.55);
|
|
15
|
+
const lang = language ?? (fileName ? languageForFile(fileName) : "plaintext");
|
|
16
|
+
const deferred = React.useDeferredValue(value);
|
|
17
|
+
const lines = React.useMemo(() => {
|
|
18
|
+
const raw = deferred.split("\n");
|
|
19
|
+
return raw.length > HIGHLIGHT_LIMIT ? raw.map((l) => [{ type: "plain", text: l }]) : tokenizeLinesOf(deferred, lang);
|
|
20
|
+
}, [deferred, lang]);
|
|
21
|
+
const count = value.split("\n").length;
|
|
22
|
+
const ta = React.useRef(null);
|
|
23
|
+
const layer = React.useRef(null);
|
|
24
|
+
const nums = React.useRef(null);
|
|
25
|
+
const sync = React.useCallback(() => {
|
|
26
|
+
const el = ta.current;
|
|
27
|
+
if (!el)
|
|
28
|
+
return;
|
|
29
|
+
if (layer.current) {
|
|
30
|
+
layer.current.scrollTop = el.scrollTop;
|
|
31
|
+
layer.current.scrollLeft = el.scrollLeft;
|
|
32
|
+
}
|
|
33
|
+
if (nums.current)
|
|
34
|
+
nums.current.style.transform = `translateY(${-el.scrollTop}px)`;
|
|
35
|
+
}, []);
|
|
36
|
+
// Foco e posicao inicial do cursor.
|
|
37
|
+
React.useEffect(() => {
|
|
38
|
+
const el = ta.current;
|
|
39
|
+
if (!el)
|
|
40
|
+
return;
|
|
41
|
+
if (autoFocus)
|
|
42
|
+
el.focus({ preventScroll: true });
|
|
43
|
+
let pos = 0;
|
|
44
|
+
if (line && line > 1) {
|
|
45
|
+
const parts = el.value.split("\n");
|
|
46
|
+
pos = parts.slice(0, Math.min(line - 1, parts.length - 1)).reduce((n, l) => n + l.length + 1, 0);
|
|
47
|
+
el.scrollTop = Math.max(0, (line - 1) * lh - el.clientHeight / 3);
|
|
48
|
+
}
|
|
49
|
+
el.setSelectionRange(pos, pos);
|
|
50
|
+
sync();
|
|
51
|
+
// so na montagem
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
}, []);
|
|
54
|
+
const insert = (el, text) => {
|
|
55
|
+
// execCommand preserva o desfazer (Ctrl+Z); setRangeText e o plano B.
|
|
56
|
+
if (document.execCommand?.("insertText", false, text))
|
|
57
|
+
return;
|
|
58
|
+
el.setRangeText(text, el.selectionStart, el.selectionEnd, "end");
|
|
59
|
+
onValueChange(el.value);
|
|
60
|
+
};
|
|
61
|
+
const onKeyDown = (e) => {
|
|
62
|
+
const el = e.currentTarget;
|
|
63
|
+
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "s" && onSave) {
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
e.stopPropagation();
|
|
66
|
+
onSave(el.value);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (readOnly || e.ctrlKey || e.metaKey || e.altKey)
|
|
70
|
+
return;
|
|
71
|
+
if (e.key === "Tab" && !e.shiftKey) {
|
|
72
|
+
e.preventDefault();
|
|
73
|
+
insert(el, lang === "go" || lang === "makefile" ? "\t" : " ");
|
|
74
|
+
}
|
|
75
|
+
else if (e.key === "Enter" && !e.shiftKey) {
|
|
76
|
+
// Mantem a indentacao da linha atual.
|
|
77
|
+
const start = el.value.lastIndexOf("\n", el.selectionStart - 1) + 1;
|
|
78
|
+
const indent = /^[ \t]*/.exec(el.value.slice(start, el.selectionStart))?.[0] ?? "";
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
insert(el, "\n" + indent);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const font = { fontSize, lineHeight: `${lh}px`, fontVariantLigatures: "none", tabSize, ...style };
|
|
84
|
+
return (_jsxs("div", { "data-slot": "code-editor", "data-language": lang, style: font, className: cn("relative flex size-full min-h-0 overflow-hidden bg-card font-mono text-foreground", className), children: [lineNumbers ? (_jsx("div", { "aria-hidden": true, className: "shrink-0 overflow-hidden py-2 pr-[1.2ch] pl-3 text-right text-muted-foreground/70 tabular-nums select-none", style: { minWidth: `${String(count).length + 4}ch` }, children: _jsx("div", { ref: nums, children: Array.from({ length: count }, (_, i) => (_jsx("div", { style: { height: lh }, children: i + 1 }, i))) }) })) : (_jsx("div", { "aria-hidden": true, className: "w-3 shrink-0" })), _jsxs("div", { className: "relative min-w-0 flex-1", children: [_jsx("div", { ref: layer, "aria-hidden": true, "data-slot": "code-view", className: "pointer-events-none absolute inset-0 overflow-hidden py-2 pr-4 whitespace-pre", children: lines.map((l, i) => (_jsx("div", { style: { height: lh }, children: l.length === 0 ? "" : l.map((t, k) => (t.type === "plain" ? t.text : _jsx("span", { "data-t": t.type, children: t.text }, k))) }, i))) }), _jsx("textarea", { ref: ta, value: value, readOnly: readOnly, wrap: "off", spellCheck: false, autoCapitalize: "off", autoCorrect: "off", "aria-label": props["aria-label"] ?? fileName, "aria-multiline": true, onChange: (e) => onValueChange(e.target.value), onScroll: sync, onKeyDown: onKeyDown, className: "absolute inset-0 size-full resize-none overflow-auto [scrollbar-width:thin] border-0 bg-transparent py-2 pr-4 whitespace-pre text-transparent caret-foreground outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/60", style: { font: "inherit", tabSize } })] })] }));
|
|
85
|
+
}
|
|
86
|
+
export { CodeEditor };
|
|
@@ -3,7 +3,7 @@ import { type VariantProps } from "class-variance-authority";
|
|
|
3
3
|
import { Button } from "./button.js";
|
|
4
4
|
declare function InputGroup({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
|
5
5
|
declare const inputGroupAddonVariants: (props?: ({
|
|
6
|
-
align?: "inline-end" | "inline-start" | "block-
|
|
6
|
+
align?: "inline-end" | "inline-start" | "block-end" | "block-start" | null | undefined;
|
|
7
7
|
} & import("class-variance-authority/types").ClassProp) | undefined) => string;
|
|
8
8
|
declare function InputGroupAddon({ className, align, ...props }: React.ComponentProps<"div"> & VariantProps<typeof inputGroupAddonVariants>): React.JSX.Element;
|
|
9
9
|
declare const inputGroupButtonVariants: (props?: ({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loccal/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Loccal UI: biblioteca de componentes React com Tailwind 4, custom elements e CSS para web e desktop.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Marcelo Matzembacher",
|
|
@@ -179,6 +179,11 @@
|
|
|
179
179
|
"import": "./dist/components/ui/checkbox.js",
|
|
180
180
|
"default": "./dist/components/ui/checkbox.js"
|
|
181
181
|
},
|
|
182
|
+
"./code-editor": {
|
|
183
|
+
"types": "./dist/components/ui/code-editor.d.ts",
|
|
184
|
+
"import": "./dist/components/ui/code-editor.js",
|
|
185
|
+
"default": "./dist/components/ui/code-editor.js"
|
|
186
|
+
},
|
|
182
187
|
"./code-view": {
|
|
183
188
|
"types": "./dist/components/ui/code-view.d.ts",
|
|
184
189
|
"import": "./dist/components/ui/code-view.js",
|