mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
Updated list converter tool (#302)
* Add "Output as column" feature to list converter tool - Implemented functionality to convert list input into column output format. - Enhanced the FormatTransformer component to support line numbers and autosizing for multiline inputs. - Added tests to validate the new column output feature in both end-to-end and model tests. - Modified the list converter's conversion logic to handle item and list wrappers when outputting as a column. * fix: correct string formatting in list converter test * fix: correct string formatting in list converter test * fix: update test input and expected output for column conversion --------- Co-authored-by: Robert Molenkamp <robert@molenkamp.be> Co-authored-by: sharevb <sharevb@gmail.com>
This commit is contained in:
27 files changed
+510
-37
No files matched your search
Vendored
+19
@@ -239,10 +239,12 @@ declare module '@vue/runtime-core' {
|
||||
IcoConverter: typeof import('./src/tools/ico-converter/ico-converter.vue')['default']
|
||||
'IconMdi:brushVariant': typeof import('~icons/mdi/brush-variant')['default']
|
||||
'IconMdi:kettleSteamOutline': typeof import('~icons/mdi/kettle-steam-outline')['default']
|
||||
IconMdiCheck: typeof import('~icons/mdi/check')['default']
|
||||
IconMdiChevronDown: typeof import('~icons/mdi/chevron-down')['default']
|
||||
IconMdiChevronRight: typeof import('~icons/mdi/chevron-right')['default']
|
||||
IconMdiClose: typeof import('~icons/mdi/close')['default']
|
||||
IconMdiContentCopy: typeof import('~icons/mdi/content-copy')['default']
|
||||
IconMdiDownload: typeof import('~icons/mdi/download')['default']
|
||||
IconMdiEye: typeof import('~icons/mdi/eye')['default']
|
||||
IconMdiEyeOff: typeof import('~icons/mdi/eye-off')['default']
|
||||
IconMdiHeart: typeof import('~icons/mdi/heart')['default']
|
||||
@@ -374,18 +376,29 @@ declare module '@vue/runtime-core' {
|
||||
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
|
||||
NButton: typeof import('naive-ui')['NButton']
|
||||
NCard: typeof import('naive-ui')['NCard']
|
||||
NCheckbox: typeof import('naive-ui')['NCheckbox']
|
||||
NCheckboxGroup: typeof import('naive-ui')['NCheckboxGroup']
|
||||
NCode: typeof import('naive-ui')['NCode']
|
||||
NColorPicker: typeof import('naive-ui')['NColorPicker']
|
||||
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
|
||||
NDataTable: typeof import('naive-ui')['NDataTable']
|
||||
NDatePicker: typeof import('naive-ui')['NDatePicker']
|
||||
NDivider: typeof import('naive-ui')['NDivider']
|
||||
NDynamicInput: typeof import('naive-ui')['NDynamicInput']
|
||||
NEllipsis: typeof import('naive-ui')['NEllipsis']
|
||||
NForm: typeof import('naive-ui')['NForm']
|
||||
NFormItem: typeof import('naive-ui')['NFormItem']
|
||||
NGi: typeof import('naive-ui')['NGi']
|
||||
NginxFormatter: typeof import('./src/tools/nginx-formatter/nginx-formatter.vue')['default']
|
||||
NginxMemo: typeof import('./src/tools/nginx-memo/nginx-memo.vue')['default']
|
||||
'NginxMemo.content': typeof import('./src/tools/nginx-memo/nginx-memo.content.md')['default']
|
||||
NGrid: typeof import('naive-ui')['NGrid']
|
||||
NH1: typeof import('naive-ui')['NH1']
|
||||
NH3: typeof import('naive-ui')['NH3']
|
||||
NicewareBytesToPassphrase: typeof import('./src/tools/niceware-bytes-to-passphrase/niceware-bytes-to-passphrase.vue')['default']
|
||||
NIcon: typeof import('naive-ui')['NIcon']
|
||||
NImage: typeof import('naive-ui')['NImage']
|
||||
NInput: typeof import('naive-ui')['NInput']
|
||||
NInputNumber: typeof import('naive-ui')['NInputNumber']
|
||||
NInputNumberI18n: typeof import('./src/ui/n-input-number-i18n/n-input-number-i18n.vue')['default']
|
||||
'NInputNumberI18n.demo': typeof import('./src/ui/n-input-number-i18n/n-input-number-i18n.demo.vue')['default']
|
||||
@@ -393,12 +406,18 @@ declare module '@vue/runtime-core' {
|
||||
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
|
||||
NMenu: typeof import('naive-ui')['NMenu']
|
||||
NP: typeof import('naive-ui')['NP']
|
||||
NRadio: typeof import('naive-ui')['NRadio']
|
||||
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
|
||||
NScrollbar: typeof import('naive-ui')['NScrollbar']
|
||||
NSelect: typeof import('naive-ui')['NSelect']
|
||||
NSlider: typeof import('naive-ui')['NSlider']
|
||||
NSpace: typeof import('naive-ui')['NSpace']
|
||||
NSpin: typeof import('naive-ui')['NSpin']
|
||||
NStatistic: typeof import('naive-ui')['NStatistic']
|
||||
NSwitch: typeof import('naive-ui')['NSwitch']
|
||||
NTable: typeof import('naive-ui')['NTable']
|
||||
NTabPane: typeof import('naive-ui')['NTabPane']
|
||||
NTabs: typeof import('naive-ui')['NTabs']
|
||||
NText: typeof import('naive-ui')['NText']
|
||||
NTooltip: typeof import('naive-ui')['NTooltip']
|
||||
NumberInput: typeof import('./src/tools/pomodoro-timer/app/components/NumberInput.vue')['default']
|
||||
|
||||
@@ -730,6 +730,7 @@ tools:
|
||||
label-remove-duplicates: إزالة التكرارات
|
||||
label-convert-to-lowercase: تحويل إلى أحرف صغيرة
|
||||
label-keep-line-breaks: الحفاظ على فواصل الأسطر
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: قائمة الفرز
|
||||
placeholder-sort-alphabetically: فرز أبجديًا
|
||||
label-items-separator: فاصل العناصر
|
||||
|
||||
@@ -725,6 +725,7 @@ tools:
|
||||
label-remove-duplicates: Fjern dubletter
|
||||
label-convert-to-lowercase: Konvertér til små bogstaver
|
||||
label-keep-line-breaks: Behold linjeskift
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Sortér liste
|
||||
placeholder-sort-alphabetically: Sortér alfabetisk
|
||||
label-items-separator: Elementseparator
|
||||
|
||||
@@ -818,6 +818,7 @@ tools:
|
||||
label-remove-duplicates: Duplikate entfernen
|
||||
label-convert-to-lowercase: In Kleinbuchstaben umwandeln
|
||||
label-keep-line-breaks: Zeilenumbrüche beibehalten
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Liste sortieren
|
||||
placeholder-sort-alphabetically: Alphabetisch sortieren
|
||||
label-items-separator: Elementtrennzeichen
|
||||
|
||||
@@ -479,6 +479,7 @@ tools:
|
||||
label-remove-duplicates: Remove duplicates
|
||||
label-convert-to-lowercase: Convert to lowercase
|
||||
label-keep-line-breaks: Keep line breaks
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Sort list
|
||||
placeholder-sort-alphabetically: Sort alphabetically
|
||||
label-items-separator: Items Separator
|
||||
|
||||
@@ -501,6 +501,7 @@ tools:
|
||||
label-remove-duplicates: Eliminar duplicados
|
||||
label-convert-to-lowercase: Convertir a minúsculas
|
||||
label-keep-line-breaks: Mantener saltos de línea
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Ordenar lista
|
||||
placeholder-sort-alphabetically: Ordenar alfabéticamente
|
||||
label-items-separator: Separador de artículos
|
||||
|
||||
@@ -3856,6 +3856,7 @@ tools:
|
||||
label-convert-to-lowercase: Convertir en minuscules
|
||||
label-items-separator: Séparateur d'articles
|
||||
label-keep-line-breaks: Gardez les pauses de ligne
|
||||
label-output-as-column: Output as column
|
||||
label-no-sort: Pas de tri
|
||||
label-remove-duplicates: Supprimer les doublons
|
||||
label-sort-asc-binary: Trier ASC (binaire)
|
||||
|
||||
@@ -822,6 +822,7 @@ tools:
|
||||
label-remove-duplicates: Bain dúblaigh
|
||||
label-convert-to-lowercase: Tiontaigh go litreacha beaga
|
||||
label-keep-line-breaks: Coinnigh briseadh líne
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Liosta sórtála
|
||||
placeholder-sort-alphabetically: Sórtáil in ord aibítre
|
||||
label-items-separator: Deighilteoir Míreanna
|
||||
|
||||
@@ -746,6 +746,7 @@ tools:
|
||||
label-remove-duplicates: Rimuovi i duplicati
|
||||
label-convert-to-lowercase: Converti in minuscolo
|
||||
label-keep-line-breaks: Mantieni le interruzioni di riga
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Ordina elenco
|
||||
placeholder-sort-alphabetically: Ordina alfabeticamente
|
||||
label-items-separator: Separatore di elementi
|
||||
|
||||
@@ -615,6 +615,7 @@ tools:
|
||||
label-remove-duplicates: 중복 제거
|
||||
label-convert-to-lowercase: 소문자로 변환
|
||||
label-keep-line-breaks: 줄 바꿈을 유지하세요
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: 목록 정렬
|
||||
placeholder-sort-alphabetically: 알파벳순으로 정렬
|
||||
label-items-separator: 항목 구분 기호
|
||||
|
||||
@@ -717,6 +717,7 @@ tools:
|
||||
label-remove-duplicates: Duplicaten verwijderen
|
||||
label-convert-to-lowercase: Omzetten naar kleine letters
|
||||
label-keep-line-breaks: Regeleinden behouden
|
||||
label-output-as-column: Uitvoer als kolom
|
||||
label-sort-list: Lijst sorteren
|
||||
placeholder-sort-alphabetically: Alfabetisch sorteren
|
||||
label-items-separator: Items scheidingsteken
|
||||
|
||||
@@ -775,6 +775,7 @@ tools:
|
||||
label-remove-duplicates: Fjern duplikater
|
||||
label-convert-to-lowercase: Konverter til små bokstaver
|
||||
label-keep-line-breaks: Behold linjeskift
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Sorter liste
|
||||
placeholder-sort-alphabetically: Sorter alfabetisk
|
||||
label-items-separator: Elementseparator
|
||||
|
||||
@@ -494,6 +494,7 @@ tools:
|
||||
label-remove-duplicates: Usuń duplikaty
|
||||
label-convert-to-lowercase: Zamień na małe litery
|
||||
label-keep-line-breaks: Zachowaj podziały wierszy
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Sortuj listę
|
||||
placeholder-sort-alphabetically: Sortuj alfabetycznie
|
||||
label-items-separator: Separator elementów
|
||||
|
||||
@@ -500,6 +500,7 @@ tools:
|
||||
label-remove-duplicates: Remover duplicatas
|
||||
label-convert-to-lowercase: Converter para minúsculas
|
||||
label-keep-line-breaks: Manter quebras de linha
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Lista de classificação
|
||||
placeholder-sort-alphabetically: Classificar em ordem alfabética
|
||||
label-items-separator: Separador de Itens
|
||||
|
||||
@@ -765,6 +765,7 @@ tools:
|
||||
label-remove-duplicates: Удалить дубликаты
|
||||
label-convert-to-lowercase: Преобразовать в нижний регистр
|
||||
label-keep-line-breaks: Сохраняйте разрывы строк
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Сортировать список
|
||||
placeholder-sort-alphabetically: Сортировать по алфавиту
|
||||
label-items-separator: Разделитель элементов
|
||||
|
||||
@@ -696,6 +696,7 @@ tools:
|
||||
label-remove-duplicates: Yinelenenleri kaldır
|
||||
label-convert-to-lowercase: Küçük harfe dönüştür
|
||||
label-keep-line-breaks: Satır sonlarını koruyun
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Sıralama listesi
|
||||
placeholder-sort-alphabetically: Alfabetik olarak sırala
|
||||
label-items-separator: Öğe Ayırıcı
|
||||
|
||||
@@ -756,6 +756,7 @@ tools:
|
||||
label-remove-duplicates: Видалити дублікати
|
||||
label-convert-to-lowercase: Перетворити на малі регістри
|
||||
label-keep-line-breaks: Зберігати розриви рядків
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Сортувати список
|
||||
placeholder-sort-alphabetically: Сортувати за алфавітом
|
||||
label-items-separator: Роздільник елементів
|
||||
|
||||
@@ -765,6 +765,7 @@ tools:
|
||||
label-remove-duplicates: Xóa các bản sao
|
||||
label-convert-to-lowercase: Chuyển đổi thành chữ thường
|
||||
label-keep-line-breaks: Giữ ngắt dòng
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: Sắp xếp danh sách
|
||||
placeholder-sort-alphabetically: Sắp xếp theo thứ tự bảng chữ cái
|
||||
label-items-separator: Bộ phân tách mục
|
||||
|
||||
@@ -438,6 +438,7 @@ tools:
|
||||
label-remove-duplicates: 移除重复项
|
||||
label-convert-to-lowercase: 转换为小写
|
||||
label-keep-line-breaks: 保留换行符
|
||||
label-output-as-column: Output as column
|
||||
label-sort-list: 排序列表
|
||||
placeholder-sort-alphabetically: 按字母顺序排序
|
||||
label-items-separator: 项分隔符
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { UseValidationRule } from '@/composable/validation';
|
||||
import CInputText from '@/ui/c-input-text/c-input-text.vue';
|
||||
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
|
||||
import { translate as t } from '@/plugins/i18n.plugin';
|
||||
import { useAppTheme } from '@/ui/theme/themes';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
@@ -18,6 +19,9 @@ const props = withDefaults(
|
||||
downloadFileName?: string
|
||||
downloadButtonText?: string
|
||||
rows?: number
|
||||
inputLineNumbers?: boolean
|
||||
inputMaxRows?: number
|
||||
inputAutosize?: boolean
|
||||
}>(),
|
||||
{
|
||||
transformer: _.identity,
|
||||
@@ -30,19 +34,125 @@ const props = withDefaults(
|
||||
downloadFileName: '',
|
||||
downloadButtonText: t('formatTransformer.download'),
|
||||
rows: 15,
|
||||
inputLineNumbers: false,
|
||||
inputMaxRows: undefined,
|
||||
inputAutosize: true,
|
||||
},
|
||||
);
|
||||
|
||||
const {
|
||||
transformer, inputValidationRules, inputLabel, outputLabel, outputLanguage,
|
||||
inputPlaceholder, inputDefault, downloadFileName, downloadButtonText, rows,
|
||||
inputPlaceholder, inputDefault, downloadFileName, downloadButtonText, rows, inputLineNumbers, inputMaxRows, inputAutosize,
|
||||
} = toRefs(props);
|
||||
|
||||
const appTheme = useAppTheme();
|
||||
|
||||
const inputElement = ref<typeof CInputText>();
|
||||
const textareaElement = computed(() => {
|
||||
const textarea = inputElement.value?.textareaRef;
|
||||
if (!textarea) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof textarea === 'object' && 'addEventListener' in textarea && 'scrollTop' in textarea) {
|
||||
return textarea as HTMLTextAreaElement;
|
||||
}
|
||||
|
||||
if (typeof textarea === 'object' && 'value' in textarea) {
|
||||
const element = (textarea as { value?: unknown }).value;
|
||||
if (element && typeof element === 'object' && 'addEventListener' in element && 'scrollTop' in element) {
|
||||
return element as HTMLTextAreaElement;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
const input = ref(inputDefault.value);
|
||||
const output = computed(() => transformer.value(input.value));
|
||||
|
||||
const lineCount = computed(() => {
|
||||
if (input.value === '') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return input.value.split(/\r\n|\r|\n/).length;
|
||||
});
|
||||
|
||||
const lineNumbersText = computed(() => {
|
||||
if (!inputLineNumbers.value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Array.from({ length: lineCount.value }, (_, index) => index + 1).join('\n');
|
||||
});
|
||||
|
||||
const lineNumberGutterWidth = computed(() => Math.max(1, `${lineCount.value}`.length));
|
||||
const lineNumbersOffset = ref(0);
|
||||
const lineNumbersHeight = ref(0);
|
||||
const lineNumbersPaddingTop = ref('');
|
||||
const lineNumbersPaddingBottom = ref('');
|
||||
const lineNumbersLineHeight = ref('');
|
||||
const lineNumbersFontSize = ref('');
|
||||
|
||||
watch(
|
||||
() => ({
|
||||
textarea: textareaElement.value,
|
||||
enabled: inputLineNumbers.value,
|
||||
}),
|
||||
({ textarea, enabled }, _, onCleanup) => {
|
||||
if (!enabled || !textarea) {
|
||||
lineNumbersOffset.value = 0;
|
||||
lineNumbersHeight.value = 0;
|
||||
lineNumbersPaddingTop.value = '';
|
||||
lineNumbersPaddingBottom.value = '';
|
||||
lineNumbersLineHeight.value = '';
|
||||
lineNumbersFontSize.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const updateSize = () => {
|
||||
lineNumbersHeight.value = textarea.clientHeight;
|
||||
lineNumbersOffset.value = textarea.scrollTop;
|
||||
const computedStyle = window.getComputedStyle(textarea);
|
||||
lineNumbersPaddingTop.value = computedStyle.paddingTop;
|
||||
lineNumbersPaddingBottom.value = computedStyle.paddingBottom;
|
||||
lineNumbersLineHeight.value = computedStyle.lineHeight;
|
||||
lineNumbersFontSize.value = computedStyle.fontSize;
|
||||
};
|
||||
|
||||
const handleScroll = () => {
|
||||
lineNumbersOffset.value = textarea.scrollTop;
|
||||
};
|
||||
|
||||
updateSize();
|
||||
const resizeObserver = new ResizeObserver(updateSize);
|
||||
resizeObserver.observe(textarea);
|
||||
textarea.addEventListener('scroll', handleScroll);
|
||||
|
||||
onCleanup(() => {
|
||||
textarea.removeEventListener('scroll', handleScroll);
|
||||
resizeObserver.disconnect();
|
||||
});
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
watch(
|
||||
input,
|
||||
() => nextTick(() => {
|
||||
const textarea = textareaElement.value;
|
||||
if (textarea) {
|
||||
lineNumbersOffset.value = textarea.scrollTop;
|
||||
}
|
||||
}),
|
||||
{ flush: 'post' },
|
||||
);
|
||||
|
||||
function focusInput() {
|
||||
inputElement.value?.focus?.();
|
||||
}
|
||||
|
||||
const outputBase64 = computed(() => Base64.encode(output.value));
|
||||
const { download } = useDownloadFileFromBase64(
|
||||
{
|
||||
@@ -58,14 +168,33 @@ const { download } = useDownloadFileFromBase64(
|
||||
:placeholder="inputPlaceholder"
|
||||
:label="inputLabel"
|
||||
:rows="rows"
|
||||
autosize
|
||||
:max-rows="inputMaxRows"
|
||||
:autosize="inputAutosize"
|
||||
raw-text
|
||||
multiline
|
||||
test-id="input"
|
||||
:validation-rules="inputValidationRules"
|
||||
monospace
|
||||
mb-1
|
||||
/>
|
||||
>
|
||||
<template v-if="inputLineNumbers" #prefix>
|
||||
<div
|
||||
class="line-numbers font-mono"
|
||||
:style="{
|
||||
minWidth: `${lineNumberGutterWidth}ch`,
|
||||
height: lineNumbersHeight ? `${lineNumbersHeight}px` : '',
|
||||
paddingTop: lineNumbersPaddingTop,
|
||||
paddingBottom: lineNumbersPaddingBottom,
|
||||
lineHeight: lineNumbersLineHeight,
|
||||
fontSize: lineNumbersFontSize,
|
||||
}"
|
||||
aria-hidden="true"
|
||||
@click="focusInput"
|
||||
>
|
||||
<pre class="line-numbers__content" :style="{ transform: `translateY(-${lineNumbersOffset}px)` }">{{ lineNumbersText }}</pre>
|
||||
</div>
|
||||
</template>
|
||||
</CInputText>
|
||||
|
||||
<div overflow-auto>
|
||||
<div mb-5px>
|
||||
@@ -80,3 +209,26 @@ const { download } = useDownloadFileFromBase64(
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.line-numbers {
|
||||
align-self: stretch;
|
||||
flex: 0 0 auto;
|
||||
box-sizing: border-box;
|
||||
padding: 8px 8px 8px 0;
|
||||
margin-right: 8px;
|
||||
border-right: 1px solid v-bind('appTheme.default.color');
|
||||
color: v-bind('appTheme.text.mutedColor');
|
||||
overflow: hidden;
|
||||
user-select: none;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.line-numbers__content {
|
||||
margin: 0;
|
||||
white-space: pre;
|
||||
line-height: inherit;
|
||||
font-size: inherit;
|
||||
will-change: transform;
|
||||
}
|
||||
</style>
|
||||
@@ -36,4 +36,17 @@ test.describe('Tool - List converter', () => {
|
||||
const result = await page.getByTestId('area-content').innerText();
|
||||
expect(result.trim()).toEqual('\'1\', \'2\', \'4\', \'3\', \'5\'');
|
||||
});
|
||||
|
||||
test('List input should be converted to column output and strip wrappers', async ({ page }) => {
|
||||
await page.getByTestId('input').fill('(\'test\',\'test2\')');
|
||||
await page.getByTestId('outputAsColumn').check();
|
||||
await page.getByTestId('splitBySeparator').fill(',');
|
||||
await page.getByTestId('itemPrefix').fill('\'');
|
||||
await page.getByTestId('itemSuffix').fill('\'');
|
||||
await page.getByTestId('listPrefix').fill('(');
|
||||
await page.getByTestId('listSuffix').fill(')');
|
||||
|
||||
const result = await page.getByTestId('area-content').innerText();
|
||||
expect(result.trim()).toEqual('test\ntest2');
|
||||
});
|
||||
});
|
||||
@@ -85,6 +85,36 @@ describe('list-converter', () => {
|
||||
expect(convert(input, options)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should output as column', () => {
|
||||
const options: ConvertOptions = {
|
||||
trimItems: true,
|
||||
outputAsColumn: true,
|
||||
splitBySeparator: ',',
|
||||
};
|
||||
const input = '1,2,3';
|
||||
const expected = `1
|
||||
2
|
||||
3`;
|
||||
expect(convert(input, options)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should strip list and item wrappers when outputting as column', () => {
|
||||
const options: ConvertOptions = {
|
||||
trimItems: true,
|
||||
outputAsColumn: true,
|
||||
splitBySeparator: ',',
|
||||
itemPrefix: '\'',
|
||||
itemSuffix: '\'',
|
||||
listPrefix: '(',
|
||||
listSuffix: ')',
|
||||
};
|
||||
const input = '(\'1\',\'2\',\'3\')';
|
||||
const expected = `1
|
||||
2
|
||||
3`;
|
||||
expect(convert(input, options)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should sort by asc-num', () => {
|
||||
const options: ConvertOptions = {
|
||||
trimItems: true,
|
||||
|
||||
@@ -9,14 +9,51 @@ function whenever<T, R>(condition: boolean | undefined, fn: (value: T) => R) {
|
||||
condition ? fn(value) : value;
|
||||
}
|
||||
|
||||
function escapeRegExp(value: string) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
function buildPrefixRegExp(userPattern?: string, autoLiteral?: string) {
|
||||
const parts = [userPattern, autoLiteral ? escapeRegExp(autoLiteral) : undefined].filter(Boolean) as string[];
|
||||
if (parts.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return new RegExp(`^(?:${parts.join('|')})`, 'g');
|
||||
}
|
||||
|
||||
function buildSuffixRegExp(userPattern?: string, autoLiteral?: string) {
|
||||
const parts = [userPattern, autoLiteral ? escapeRegExp(autoLiteral) : undefined].filter(Boolean) as string[];
|
||||
if (parts.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return new RegExp(`(?:${parts.join('|')})$`, 'g');
|
||||
}
|
||||
|
||||
function convert(list: string, options: ConvertOptions): string {
|
||||
const lineBreak = options.keepLineBreaks ? '\n' : '';
|
||||
const outputAsColumn = options.outputAsColumn ?? false;
|
||||
const lineBreak = outputAsColumn || options.keepLineBreaks ? '\n' : '';
|
||||
const itemsSeparator = outputAsColumn ? '' : (options.itemsSeparator || '');
|
||||
const itemPrefix = outputAsColumn ? '' : (options.itemPrefix || '');
|
||||
const itemSuffix = outputAsColumn ? '' : (options.itemSuffix || '');
|
||||
const listPrefix = outputAsColumn ? '' : (options.listPrefix || '');
|
||||
const listSuffix = outputAsColumn ? '' : (options.listSuffix || '');
|
||||
|
||||
const splitSep = options.splitBySeparator ? `${options.splitBySeparator}|` : '';
|
||||
const splitRegExp = new RegExp(`(?:${splitSep}\\n)`, 'g');
|
||||
const filterRegExp = options.filterRegex ? new RegExp(options.filterRegex, 'v') : null;
|
||||
const notFilterRegExp = options.notFilterRegex ? new RegExp(options.notFilterRegex, 'v') : null;
|
||||
return _.chain(list)
|
||||
const removeItemPrefixRegExp = buildPrefixRegExp(options.removeItemPrefix, outputAsColumn ? options.itemPrefix : undefined);
|
||||
const removeItemSuffixRegExp = buildSuffixRegExp(options.removeItemSuffix, outputAsColumn ? options.itemSuffix : undefined);
|
||||
|
||||
let sourceList = list;
|
||||
if (outputAsColumn && options.listPrefix) {
|
||||
sourceList = sourceList.replace(new RegExp(`^${escapeRegExp(options.listPrefix)}`), '');
|
||||
}
|
||||
if (outputAsColumn && options.listSuffix) {
|
||||
sourceList = sourceList.replace(new RegExp(`${escapeRegExp(options.listSuffix)}$`), '');
|
||||
}
|
||||
|
||||
return _.chain(sourceList)
|
||||
.thru(whenever(options.lowerCase, text => text.toLowerCase()))
|
||||
.split(splitRegExp)
|
||||
.thru(whenever(options.removeDuplicates, _.uniq))
|
||||
@@ -26,10 +63,10 @@ function convert(list: string, options: ConvertOptions): string {
|
||||
.filter(text => notFilterRegExp === null || !notFilterRegExp?.test(text))
|
||||
.thru(whenever(!!options.sortList, parts => parts.sort(byOrder({ order: options.sortList }))))
|
||||
.without('')
|
||||
.map(p => options.removeItemPrefix ? p.replace(new RegExp(`^${options.removeItemPrefix}`, 'g'), '') : p)
|
||||
.map(p => options.removeItemSuffix ? p.replace(new RegExp(`${options.removeItemSuffix}$`, 'g'), '') : p)
|
||||
.map(p => (options.itemPrefix || '') + p + (options.itemSuffix || ''))
|
||||
.join((options.itemsSeparator || '') + lineBreak)
|
||||
.thru(text => [options.listPrefix, text, options.listSuffix].filter(l => l).join(lineBreak))
|
||||
.map(p => removeItemPrefixRegExp ? p.replace(removeItemPrefixRegExp, '') : p)
|
||||
.map(p => removeItemSuffixRegExp ? p.replace(removeItemSuffixRegExp, '') : p)
|
||||
.map(p => itemPrefix + p + itemSuffix)
|
||||
.join(itemsSeparator + lineBreak)
|
||||
.thru(text => [listPrefix, text, listSuffix].filter(l => l).join(lineBreak))
|
||||
.value();
|
||||
}
|
||||
@@ -17,4 +17,5 @@ export interface ConvertOptions {
|
||||
keepLineBreaks?: boolean
|
||||
filterRegex?: string
|
||||
notFilterRegex?: string
|
||||
outputAsColumn?: boolean
|
||||
}
|
||||
@@ -51,6 +51,7 @@ const conversionConfig = useStorage<ConvertOptions>('list-converter:conversionCo
|
||||
trimItems: true,
|
||||
removeDuplicates: true,
|
||||
keepLineBreaks: false,
|
||||
outputAsColumn: false,
|
||||
itemPrefix: '',
|
||||
itemSuffix: '',
|
||||
removeItemPrefix: '',
|
||||
@@ -92,7 +93,10 @@ function transformer(value: string) {
|
||||
<n-switch v-model:value="conversionConfig.lowerCase" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="t('tools.list-converter.texts.label-keep-line-breaks')" label-placement="left" label-width="150" :show-feedback="false" mb-2>
|
||||
<n-switch v-model:value="conversionConfig.keepLineBreaks" />
|
||||
<n-switch v-model:value="conversionConfig.keepLineBreaks" :disabled="conversionConfig.outputAsColumn" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="t('tools.list-converter.texts.label-output-as-column')" label-placement="left" label-width="150" :show-feedback="false" mb-2>
|
||||
<n-switch v-model:value="conversionConfig.outputAsColumn" data-test-id="outputAsColumn" />
|
||||
</n-form-item>
|
||||
</div>
|
||||
<div>
|
||||
@@ -117,6 +121,8 @@ function transformer(value: string) {
|
||||
label-width="120px"
|
||||
label-align="right"
|
||||
mb-2
|
||||
:disabled="conversionConfig.outputAsColumn"
|
||||
test-id="itemsSeparator"
|
||||
:placeholder="t('tools.list-converter.texts.placeholder-items-separator')"
|
||||
/>
|
||||
|
||||
@@ -127,6 +133,7 @@ function transformer(value: string) {
|
||||
label-width="120px"
|
||||
label-align="right"
|
||||
mb-2
|
||||
test-id="splitBySeparator"
|
||||
:placeholder="t('tools.list-converter.texts.placeholder-separator-for-splitting')"
|
||||
/>
|
||||
|
||||
@@ -191,5 +198,8 @@ function transformer(value: string) {
|
||||
:output-label="t('tools.list-converter.texts.output-label-your-transformed-data')"
|
||||
:transformer="transformer"
|
||||
download-file-name="output.txt"
|
||||
input-line-numbers
|
||||
:rows="25"
|
||||
:input-autosize="false"
|
||||
/>
|
||||
</template>
|
||||
@@ -1,10 +1,53 @@
|
||||
<script lang="ts" setup>
|
||||
import { useValidation } from '@/composable/validation';
|
||||
import CInputText from '@/ui/c-input-text/c-input-text.vue';
|
||||
import { useAppTheme } from '@/ui/theme/themes';
|
||||
|
||||
const value = ref('value');
|
||||
const valueLong = ref(
|
||||
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolorum, est modi iusto repellendus fuga accusantium atque at magnam aliquam eum explicabo vero quia, nobis quasi quis! Earum amet quam a?',
|
||||
);
|
||||
const lineNumbersValue = ref('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6');
|
||||
|
||||
const appTheme = useAppTheme();
|
||||
const lineNumberInput = ref<typeof CInputText>();
|
||||
const lineNumbersOffset = ref(0);
|
||||
const lineNumbersHeight = ref(0);
|
||||
const lineNumbersPaddingTop = ref('');
|
||||
const lineNumbersPaddingBottom = ref('');
|
||||
const lineNumbersLineHeight = ref('');
|
||||
const lineNumbersFontSize = ref('');
|
||||
|
||||
const lineCount = computed(() => {
|
||||
if (lineNumbersValue.value === '') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return lineNumbersValue.value.split(/\r\n|\r|\n/).length;
|
||||
});
|
||||
|
||||
const lineNumbersText = computed(() =>
|
||||
Array.from({ length: lineCount.value }, (_, index) => index + 1).join('\n'));
|
||||
const lineNumberGutterWidth = computed(() => Math.max(1, `${lineCount.value}`.length));
|
||||
const textareaElement = computed(() => {
|
||||
const textarea = lineNumberInput.value?.textareaRef;
|
||||
if (!textarea) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof textarea === 'object' && 'addEventListener' in textarea && 'scrollTop' in textarea) {
|
||||
return textarea as HTMLTextAreaElement;
|
||||
}
|
||||
|
||||
if (typeof textarea === 'object' && 'value' in textarea) {
|
||||
const element = (textarea as { value?: unknown }).value;
|
||||
if (element && typeof element === 'object' && 'addEventListener' in element && 'scrollTop' in element) {
|
||||
return element as HTMLTextAreaElement;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
const validationRules = [{ message: 'Length must be > 10', validator: (value: string) => value.length > 10 }];
|
||||
|
||||
@@ -12,61 +55,116 @@ const validation = useValidation({
|
||||
source: value,
|
||||
rules: validationRules,
|
||||
});
|
||||
|
||||
watch(
|
||||
textareaElement,
|
||||
(textarea, _, onCleanup) => {
|
||||
if (!textarea) {
|
||||
lineNumbersOffset.value = 0;
|
||||
lineNumbersHeight.value = 0;
|
||||
lineNumbersPaddingTop.value = '';
|
||||
lineNumbersPaddingBottom.value = '';
|
||||
lineNumbersLineHeight.value = '';
|
||||
lineNumbersFontSize.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const updateSize = () => {
|
||||
lineNumbersHeight.value = textarea.clientHeight;
|
||||
lineNumbersOffset.value = textarea.scrollTop;
|
||||
const computedStyle = window.getComputedStyle(textarea);
|
||||
lineNumbersPaddingTop.value = computedStyle.paddingTop;
|
||||
lineNumbersPaddingBottom.value = computedStyle.paddingBottom;
|
||||
lineNumbersLineHeight.value = computedStyle.lineHeight;
|
||||
lineNumbersFontSize.value = computedStyle.fontSize;
|
||||
};
|
||||
|
||||
const handleScroll = () => {
|
||||
lineNumbersOffset.value = textarea.scrollTop;
|
||||
};
|
||||
|
||||
updateSize();
|
||||
const resizeObserver = new ResizeObserver(updateSize);
|
||||
resizeObserver.observe(textarea);
|
||||
textarea.addEventListener('scroll', handleScroll);
|
||||
|
||||
onCleanup(() => {
|
||||
textarea.removeEventListener('scroll', handleScroll);
|
||||
resizeObserver.disconnect();
|
||||
});
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
watch(
|
||||
lineNumbersValue,
|
||||
() => nextTick(() => {
|
||||
const textarea = textareaElement.value;
|
||||
if (textarea) {
|
||||
lineNumbersOffset.value = textarea.scrollTop;
|
||||
}
|
||||
}),
|
||||
{ flush: 'post' },
|
||||
);
|
||||
|
||||
function focusLineNumbersInput() {
|
||||
lineNumberInput.value?.focus?.();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h2>Default</h2>
|
||||
|
||||
<c-input-text value="qsd" />
|
||||
<c-input-text
|
||||
<CInputText value="qsd" />
|
||||
<CInputText
|
||||
value="Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolorum, est modi iusto repellendus fuga accusantium atque at magnam aliquam eum explicabo vero quia, nobis quasi quis! Earum amet quam a?"
|
||||
/>
|
||||
|
||||
<h2>With placeholder</h2>
|
||||
|
||||
<c-input-text placeholder="Placeholder" />
|
||||
<CInputText placeholder="Placeholder" />
|
||||
|
||||
<h2>With label</h2>
|
||||
|
||||
<c-input-text label="Label" mb-2 />
|
||||
<c-input-text label="Label" mb-2 label-position="left" />
|
||||
<c-input-text label="Label" mb-2 label-position="left" label-width="100px" />
|
||||
<c-input-text label="Label" mb-2 label-position="left" label-width="100px" label-align="right" />
|
||||
<CInputText label="Label" mb-2 />
|
||||
<CInputText label="Label" mb-2 label-position="left" />
|
||||
<CInputText label="Label" mb-2 label-position="left" label-width="100px" />
|
||||
<CInputText label="Label" mb-2 label-position="left" label-width="100px" label-align="right" />
|
||||
|
||||
<h2>Readonly</h2>
|
||||
|
||||
<c-input-text value="value" readonly />
|
||||
<CInputText value="value" readonly />
|
||||
|
||||
<h2>Disabled</h2>
|
||||
|
||||
<c-input-text value="value" disabled />
|
||||
<CInputText value="value" disabled />
|
||||
|
||||
<h2>Validation</h2>
|
||||
|
||||
<c-input-text v-model:value="value" :validation-rules="validationRules" mb-2 />
|
||||
<c-input-text v-model:value="value" :validation-rules="validationRules" mb-2 label-position="left" label="Yo " />
|
||||
<c-input-text v-model:value="value" :validation="validation" />
|
||||
<c-input-text v-model:value="value" :validation="validation" multiline rows="3" />
|
||||
<CInputText v-model:value="value" :validation-rules="validationRules" mb-2 />
|
||||
<CInputText v-model:value="value" :validation-rules="validationRules" mb-2 label-position="left" label="Yo " />
|
||||
<CInputText v-model:value="value" :validation="validation" />
|
||||
<CInputText v-model:value="value" :validation="validation" multiline rows="3" />
|
||||
|
||||
<h2>Clearable</h2>
|
||||
|
||||
<c-input-text v-model:value="value" clearable />
|
||||
<CInputText v-model:value="value" clearable />
|
||||
|
||||
<h2>Type password</h2>
|
||||
|
||||
<c-input-text value="value" type="password" />
|
||||
<CInputText value="value" type="password" />
|
||||
|
||||
<h2>Multiline</h2>
|
||||
|
||||
<c-input-text value="value" multiline label="Label" mb-2 rows="1" />
|
||||
<c-input-text value="value" multiline label="Label" mb-2 />
|
||||
<c-input-text
|
||||
<CInputText value="value" multiline label="Label" mb-2 rows="1" />
|
||||
<CInputText value="value" multiline label="Label" mb-2 />
|
||||
<CInputText
|
||||
value="Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolorum, est modi iusto repellendus fuga accusantium atque at magnam aliquam eum explicabo vero quia, nobis quasi quis! Earum amet quam a?"
|
||||
multiline
|
||||
mb-2
|
||||
/>
|
||||
|
||||
<c-input-text
|
||||
<CInputText
|
||||
value="Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolorum, est modi iusto repellendus fuga accusantium atque at magnam aliquam eum explicabo vero quia, nobis quasi quis! Earum amet quam a?"
|
||||
multiline
|
||||
clearable
|
||||
@@ -74,6 +172,60 @@ const validation = useValidation({
|
||||
|
||||
<h2>Autosize</h2>
|
||||
|
||||
<c-input-text v-model:value="value" label="Autosize" rows="1" multiline autosize mb-2 />
|
||||
<c-input-text v-model:value="valueLong" label="Autosize monospace" rows="1" multiline autosize monospace mb-2 />
|
||||
<CInputText v-model:value="value" label="Autosize" rows="1" multiline autosize mb-2 />
|
||||
<CInputText v-model:value="valueLong" label="Autosize monospace" rows="1" multiline autosize monospace mb-2 />
|
||||
|
||||
<h2>Line numbers</h2>
|
||||
|
||||
<CInputText
|
||||
ref="lineNumberInput"
|
||||
v-model:value="lineNumbersValue"
|
||||
label="Line numbers"
|
||||
rows="6"
|
||||
multiline
|
||||
monospace
|
||||
raw-text
|
||||
mb-2
|
||||
>
|
||||
<template #prefix>
|
||||
<div
|
||||
class="line-numbers font-mono"
|
||||
:style="{
|
||||
minWidth: `${lineNumberGutterWidth}ch`,
|
||||
height: lineNumbersHeight ? `${lineNumbersHeight}px` : '',
|
||||
paddingTop: lineNumbersPaddingTop,
|
||||
paddingBottom: lineNumbersPaddingBottom,
|
||||
lineHeight: lineNumbersLineHeight,
|
||||
fontSize: lineNumbersFontSize,
|
||||
}"
|
||||
aria-hidden="true"
|
||||
@click="focusLineNumbersInput"
|
||||
>
|
||||
<pre class="line-numbers__content" :style="{ transform: `translateY(-${lineNumbersOffset}px)` }">{{ lineNumbersText }}</pre>
|
||||
</div>
|
||||
</template>
|
||||
</CInputText>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.line-numbers {
|
||||
align-self: stretch;
|
||||
flex: 0 0 auto;
|
||||
box-sizing: border-box;
|
||||
padding: 8px 8px 8px 0;
|
||||
margin-right: 8px;
|
||||
border-right: 1px solid v-bind('appTheme.default.color');
|
||||
color: v-bind('appTheme.text.mutedColor');
|
||||
overflow: hidden;
|
||||
user-select: none;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.line-numbers__content {
|
||||
margin: 0;
|
||||
white-space: pre;
|
||||
line-height: inherit;
|
||||
font-size: inherit;
|
||||
will-change: transform;
|
||||
}
|
||||
</style>
|
||||
@@ -28,6 +28,7 @@ const props = withDefaults(
|
||||
type?: 'text' | 'password'
|
||||
multiline?: boolean
|
||||
rows?: number | string
|
||||
maxRows?: number | string
|
||||
autosize?: boolean
|
||||
autofocus?: boolean
|
||||
monospace?: boolean
|
||||
@@ -56,6 +57,7 @@ const props = withDefaults(
|
||||
type: 'text',
|
||||
multiline: false,
|
||||
rows: 3,
|
||||
maxRows: undefined,
|
||||
autosize: false,
|
||||
autofocus: false,
|
||||
monospace: false,
|
||||
@@ -67,7 +69,7 @@ const emit = defineEmits(['update:value']);
|
||||
const value = useVModel(props, 'value', emit);
|
||||
const showPassword = ref(false);
|
||||
|
||||
const { id, placeholder, label, validationRules, labelPosition, labelWidth, labelAlign, autosize, readonly, disabled, clearable, type, multiline, rows, rawText, autofocus, monospace, pasteHtml } = toRefs(props);
|
||||
const { id, placeholder, label, validationRules, labelPosition, labelWidth, labelAlign, autosize, readonly, disabled, clearable, type, multiline, rows, maxRows, rawText, autofocus, monospace, pasteHtml } = toRefs(props);
|
||||
|
||||
const validation
|
||||
= props.validation
|
||||
@@ -107,7 +109,7 @@ function onPasteInputHtml(evt: ClipboardEvent) {
|
||||
}
|
||||
|
||||
watch(
|
||||
[value, autosize, multiline, inputWrapperRef, textareaRef],
|
||||
[value, autosize, multiline, maxRows, inputWrapperRef, textareaRef],
|
||||
() => nextTick(() => {
|
||||
if (props.multiline && autosize.value) {
|
||||
resizeTextarea();
|
||||
@@ -134,6 +136,26 @@ function checkOverflow(el: HTMLElement) {
|
||||
return isOverflowing;
|
||||
}
|
||||
|
||||
function getMaxHeight(textarea: HTMLTextAreaElement) {
|
||||
const maxRowsValue = Number(maxRows.value);
|
||||
if (!Number.isFinite(maxRowsValue) || maxRowsValue <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const computedStyle = window.getComputedStyle(textarea);
|
||||
const lineHeight = Number.parseFloat(computedStyle.lineHeight);
|
||||
const paddingTop = Number.parseFloat(computedStyle.paddingTop);
|
||||
const paddingBottom = Number.parseFloat(computedStyle.paddingBottom);
|
||||
const borderTop = Number.parseFloat(computedStyle.borderTopWidth);
|
||||
const borderBottom = Number.parseFloat(computedStyle.borderBottomWidth);
|
||||
|
||||
if (!Number.isFinite(lineHeight)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (lineHeight * maxRowsValue) + paddingTop + paddingBottom + borderTop + borderBottom;
|
||||
}
|
||||
|
||||
function resizeTextarea() {
|
||||
if (textareaRef.value === undefined) {
|
||||
return; // cannot chnge textarea if element is not available
|
||||
@@ -143,13 +165,32 @@ function resizeTextarea() {
|
||||
return; // textarea wont be displayed if multiline === false
|
||||
}
|
||||
|
||||
if (!checkOverflow(textareaRef.value)) {
|
||||
const textAreaElement = textareaRef.value!;
|
||||
const maxHeight = getMaxHeight(textAreaElement);
|
||||
|
||||
if (maxHeight !== null) {
|
||||
textAreaElement.style.maxHeight = `${maxHeight}px`;
|
||||
}
|
||||
else {
|
||||
textAreaElement.style.maxHeight = '';
|
||||
textAreaElement.style.overflowY = '';
|
||||
}
|
||||
|
||||
if (!checkOverflow(textAreaElement)) {
|
||||
if (maxHeight !== null) {
|
||||
textAreaElement.style.overflowY = 'hidden';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const textAreaElement = textareaRef.value!;
|
||||
|
||||
const scrollHeight = textAreaElement.scrollHeight + 2;
|
||||
if (maxHeight !== null) {
|
||||
const cappedHeight = Math.min(scrollHeight, maxHeight);
|
||||
textAreaElement.style.height = `${cappedHeight}px`;
|
||||
textAreaElement.style.overflowY = scrollHeight > maxHeight ? 'auto' : 'hidden';
|
||||
return;
|
||||
}
|
||||
|
||||
textAreaElement.style.height = `${scrollHeight}px`;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user