Merge branch 'feat/image-to-css' into chore/all-my-stuffs

# Conflicts:
#	components.d.ts
#	package.json
#	pnpm-lock.yaml
#	src/components/TextareaCopyable.vue
#	src/tools/index.ts
#	vite.config.ts
This commit is contained in:
ShareVB committed 2024-11-03 20:45:33 +01:00
commit 8a4ebe2bfa
7 files changed
+373 -183

No files matched your search

+2
View File
@@ -159,11 +159,13 @@
"roboto-base64": "^0.1.2",
"sql-formatter": "^13.0.0",
"svg2png-wasm": "^1.4.1",
"svg-to-url": "^4.0.0",
"ua-parser-js": "^1.0.35",
"ulid": "^2.3.0",
"unicode-emoji-json": "^0.4.0",
"unplugin-auto-import": "^0.16.4",
"uuid": "^9.0.0",
"vite-plugin-node-polyfills": "^0.22.0",
"vue": "^3.3.4",
"vue-color-wheel": "^0.1.1",
"vue-i18n": "^9.9.1",
+228 -181
View File
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,47 @@
import { stringToUrl } from 'svg-to-url';
export type CSSType = 'Background' | 'Border' | 'ListItemBullet' | 'Url';
async function fileToDataUrl(file: File) {
if (file.type === 'image/svg+xml') {
const svgContent = (await (new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => resolve(reader.result?.toString() ?? '');
reader.onerror = error => reject(error);
})));
return svgToDataUrl(svgContent);
}
return new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result?.toString() ?? '');
reader.onerror = error => reject(error);
});
}
function svgToDataUrl(image: string) {
const getUrlFromSvgString = stringToUrl({});
return getUrlFromSvgString(image);
}
export async function imageToCSS(
image: File | string,
type: CSSType,
) {
if (image === '' || !image) {
return '';
}
const dataURI = image instanceof File ? await fileToDataUrl(image) : svgToDataUrl(image);
switch (type) {
case 'Background':
return `background-image: url(${dataURI});`;
case 'Border':
return `border-image-source: url(${dataURI});`;
case 'ListItemBullet':
return `li{\n list-style-image: ${dataURI};\n}\nli::marker{\n font-size: 1.5em;\n}'}`;
default:
return `url(${dataURI})`;
}
}
+80
View File
@@ -0,0 +1,80 @@
<script setup lang="ts">
import type { Ref } from 'vue';
import { type CSSType, imageToCSS } from './image-to-css.service';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
const typeOptions = [
{ label: 'Background', value: 'Background' },
{ label: 'Border', value: 'Border' },
{ label: 'ListItem Bullet', value: 'ListItemBullet' },
{ label: 'CSS Data Url', value: 'Url' },
];
const type = ref('Background');
const svgContent = ref('');
const fileInput = ref() as Ref<File | null>;
const cssCode = computedAsync(async () => {
try {
return (await imageToCSS(fileInput.value || svgContent.value, type.value as CSSType));
}
catch (e: any) {
return e.toString();
}
});
async function onUpload(file: File) {
if (file) {
fileInput.value = file;
}
}
watch(svgContent, (_, newValue) => {
if (newValue !== '') {
fileInput.value = null;
}
});
</script>
<template>
<div>
<c-file-upload
title="Drag and drop an image here, or click to select a file"
paste-image
@file-upload="onUpload"
/>
<n-p>OR</n-p>
<c-input-text
v-model:value="svgContent"
label="SVG Content"
placeholder="Paste your SVG content here"
mb-2
/>
<n-divider />
<c-select
v-model:value="type"
label-position="top"
label="CSS Type:"
:options="typeOptions"
placeholder="Select CSS Type"
/>
<div v-if="cssCode !== ''">
<n-divider />
<h3>CSS Code</h3>
<TextareaCopyable
:value="cssCode"
word-wrap
/>
</div>
</div>
</template>
<style lang="less" scoped>
::v-deep(.n-upload-trigger) {
width: 100%;
}
</style>
+12
View File
@@ -0,0 +1,12 @@
import { BrandCss3 } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Image to CSS',
path: '/image-to-css',
description: 'Convert image to CSS (url, background, ...)',
keywords: ['image', 'css'],
component: () => import('./image-to-css.vue'),
icon: BrandCss3,
createdAt: new Date('2024-05-11'),
});
+2
View File
@@ -43,6 +43,7 @@ import { tool as hddCalculator } from './hdd-calculator';
import { tool as heicConverter } from './heic-converter';
import { tool as icalParser } from './ical-parser';
import { tool as icalGenerator } from './ical-generator';
import { tool as imageToCss } from './image-to-css';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -195,6 +196,7 @@ export const toolsByCategory: ToolCategory[] = [
bounceParser,
colorWheel,
folderStructureDiagram,
imageToCss,
],
},
{
+2 -2
View File
@@ -122,10 +122,10 @@ export default defineConfig({
build: {
target: 'esnext',
rollupOptions: {
external: ['regex'],
external: ['node:fs/promises', 'fs', 'regex'],
},
},
optimizeDeps: {
include: ['re2-wasm-embedded'], // optionally specify dependency name
},
});
});