Skip to content

Commit 92e5732

Browse files
Merge pull request #93 from ArthurLobopro/use-svgo-in-browser-side
Use svgo in browser side
2 parents 4abd96a + 8c6af6f commit 92e5732

File tree

7 files changed

+45
-8
lines changed

7 files changed

+45
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.22.1
2+
3+
Feat: Run SVGO on Browser side to improve performance
4+
15
## 1.22.0
26

37
Feat: New Watermark position input

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "easy-codesnap",
33
"displayName": "Easy CodeSnap",
44
"description": "Take beautiful screenshots of your code 📷",
5-
"version": "1.22.0",
5+
"version": "1.22.1",
66
"l10n": "./l10n",
77
"repository": {
88
"type": "git",

src/commands/snap/SnapActions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from "vscode";
22
import { extensionSettingsNames } from "../../constants";
3-
import { reduceSVG } from "../../reduceSVG";
43
import type { ExtensionConfig } from "../../types";
54
import { hasOneSelection, t } from "../../util";
65
import { savePNG, saveSVG } from "./savers";
@@ -85,6 +84,6 @@ export class SnapActions {
8584
}
8685

8786
"copy-svg"({ data }: { data: string }) {
88-
vscode.env.clipboard.writeText(reduceSVG(data));
87+
vscode.env.clipboard.writeText(data);
8988
}
9089
}

src/commands/snap/savers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { homedir, platform } from "node:os";
22
import path from "node:path";
33
import * as vscode from "vscode";
4-
import { reduceSVG } from "../../reduceSVG";
54
import { t, writeFile } from "../../util";
65

76
const isWin = platform() === "win32";
@@ -49,9 +48,9 @@ export async function saveSVG(data: string) {
4948

5049
assignLastUsedImageUri(uri);
5150

52-
const reducedData = reduceSVG(data);
51+
//const reducedData = reduceSVG(data);
5352

5453
if (uri) {
55-
writeFile(uri.fsPath, reducedData, "utf-8");
54+
writeFile(uri.fsPath, data, "utf-8");
5655
}
5756
}

src/webview/exporters.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { elementToSVG } from "dom-to-svg";
55

66
import type { WebviewConfig } from "../types";
77
import { SessionConfig } from "./SessionConfig";
8+
//@ts-expect-error
9+
import { optimize } from "./lib/svgo.browser.js";
810
import { cameraFlashAnimation } from "./snap";
911
import { $$, vscode } from "./util";
1012

@@ -51,8 +53,10 @@ export async function exportSVG(
5153
"<style>.line-number,#window-title{user-select:none;}</style>",
5254
);
5355

56+
const minifiedSvg = optimize(svg, svgoConfig).data;
57+
5458
if (action === "copy") {
55-
vscode.postMessage({ type: "copy-svg", data: svg });
59+
vscode.postMessage({ type: "copy-svg", data: minifiedSvg });
5660
cameraFlashAnimation();
5761
vscode.postMessage({ type: "copied" });
5862
} else {
@@ -120,3 +124,31 @@ async function toPNGFallback(target: HTMLElement) {
120124

121125
return canvas.toDataURL();
122126
}
127+
128+
const svgoConfig = {
129+
floatPrecision: 8,
130+
multipass: true,
131+
plugins: [
132+
{
133+
name: "preset-default",
134+
params: {
135+
overrides: {
136+
removeViewBox: false,
137+
removeUnknownsAndDefaults: {
138+
defaultAttrs: true,
139+
uselessOverrides: true,
140+
unknownAttrs: true,
141+
keepAriaAttrs: false,
142+
keepDataAttrs: false,
143+
},
144+
},
145+
},
146+
},
147+
{
148+
name: "removeAttrs",
149+
params: {
150+
attrs: ["textLength", "text-decoration"],
151+
},
152+
},
153+
],
154+
};

src/webview/lib/svgo.browser.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"sourceMap": true,
1010
"rootDir": "src",
1111
"strict": true,
12-
"noImplicitReturns": true /* enable all strict type-checking options */
12+
"noImplicitReturns": true,
13+
"allowJs": true,
14+
/* enable all strict type-checking options */
1315
/* Additional Checks */
1416
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
1517
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

0 commit comments

Comments
 (0)