Skip to content

Commit 55fda63

Browse files
committed
Merge branch 'feat/image-to-css' into chore/all-my-stuffs
2 parents 640a873 + 0f8463f commit 55fda63

File tree

5 files changed

+64
-146
lines changed

5 files changed

+64
-146
lines changed

components.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,13 @@ declare module '@vue/runtime-core' {
274274
NProgress: typeof import('naive-ui')['NProgress']
275275
NRadio: typeof import('naive-ui')['NRadio']
276276
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
277+
NRadio: typeof import('naive-ui')['NRadio']
278+
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
277279
NScrollbar: typeof import('naive-ui')['NScrollbar']
278280
NSelect: typeof import('naive-ui')['NSelect']
279281
NSlider: typeof import('naive-ui')['NSlider']
280282
NSpace: typeof import('naive-ui')['NSpace']
283+
NSpace: typeof import('naive-ui')['NSpace']
281284
NSpin: typeof import('naive-ui')['NSpin']
282285
NStatistic: typeof import('naive-ui')['NStatistic']
283286
NSwitch: typeof import('naive-ui')['NSwitch']

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
"spdx-satisfies": "^5.0.1",
213213
"sql-formatter": "^13.0.0",
214214
"sshpk": "^1.18.0",
215-
"svg-to-url": "^4.0.0",
215+
"svgo": "^3.3.2",
216216
"svg2png-wasm": "^1.4.1",
217217
"tesseract.js": "^5.1.1",
218218
"tidy-url": "^1.18.3",

pnpm-lock.yaml

Lines changed: 7 additions & 139 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/image-to-css/image-to-css.service.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1-
import { stringToUrl } from 'svg-to-url';
1+
import { type Config, type PluginConfig, optimize } from 'svgo';
2+
3+
function svgo(config: Config) {
4+
return (data: string) => {
5+
const { plugins = [], ...rest } = config || {};
6+
return optimize(data, {
7+
...rest,
8+
plugins: [
9+
...(plugins.length > 0 ? plugins : ['preset-default']),
10+
'removeXMLNS',
11+
] as PluginConfig[],
12+
}).data.replace(/^<svg/g, '<svg xmlns="http://www.w3.org/2000/svg"');
13+
};
14+
}
15+
16+
export function encodeStr(svgStr: string) {
17+
const encoded = encodeURIComponent(svgStr)
18+
.replace(/%20/g, ' ')
19+
.replace(/%3D/g, '=')
20+
.replace(/%3B/g, ';')
21+
.replace(/%3A/g, ':')
22+
.replace(/%2F/g, '/')
23+
.replace(/%2C/g, ',')
24+
.replace(/%22/g, '\'');
25+
26+
return `data:image/svg+xml,${encoded}`;
27+
}
228

329
export type CSSType = 'Background' | 'Border' | 'ListItemBullet' | 'Url';
430

@@ -21,9 +47,8 @@ async function fileToDataUrl(file: File) {
2147
});
2248
}
2349

24-
function svgToDataUrl(image: string) {
25-
const getUrlFromSvgString = stringToUrl({});
26-
return getUrlFromSvgString(image);
50+
function svgToDataUrl(svg: string) {
51+
return encodeStr(svgo({})(svg));
2752
}
2853

2954
export async function imageToCSS(

0 commit comments

Comments
 (0)