Skip to content

Commit

Permalink
Module Workers support in Firefox
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
tomayac committed Jan 26, 2022
1 parent 8d89914 commit 1caf928
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 50 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions docs/assets/index.54ec3c4a.js

Large diffs are not rendered by default.

26 changes: 0 additions & 26 deletions docs/assets/index.ecc37913.js

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docs/assets/module-workers-polyfill.min.dc7647fd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
}
</script>
<base target="_blank" />
<script type="module" crossorigin src="/assets/index.ecc37913.js"></script>
<script type="module" crossorigin src="/assets/index.54ec3c4a.js"></script>
<link rel="modulepreload" href="/assets/vendor.219e5f4d.js">
<link rel="stylesheet" href="/assets/index.7c07bd9d.css">
<link rel="manifest" href="/manifest.webmanifest"></head>
Expand Down
2 changes: 1 addition & 1 deletion docs/sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"canvas-size": "^1.2.5",
"esm-potrace-wasm": "^0.2.1",
"idb-keyval": "^6.1.0",
"module-workers-polyfill": "^0.3.2",
"pinch-zoom-element": "^1.1.1",
"svgo": "^2.8.0"
},
Expand Down
31 changes: 18 additions & 13 deletions src/js/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,25 @@ copyButton.addEventListener('click', async () => {
let svg = svgOutput.innerHTML;
showToast(i18n.t('optimizingSVG'), Infinity);
try {
// Safari treats user activation differently:
// https://bugs.webkit.org/show_bug.cgi?id=222262.
await navigator.clipboard.write([
new ClipboardItem({
'text/plain': new Promise(async (resolve) => {
svg = await optimizeSVG(svg);
resolve(new Blob([svg], { type: 'text/plain' }));
}),
'image/svg+xml': new Promise(async (resolve) => {
svg = await optimizeSVG(svg);
resolve(new Blob([svg], { type: 'image/svg+xml' }));
// Firefox only supports `navigator.clipboard.write()`.
if (!('ClipboardItem' in window)) {
await navigator.clipboard.writeText(await optimizeSVG(svg));
} else {
// Safari treats user activation differently:
// https://bugs.webkit.org/show_bug.cgi?id=222262.
await navigator.clipboard.write([
new ClipboardItem({
'text/plain': new Promise(async (resolve) => {
svg = await optimizeSVG(svg);
resolve(new Blob([svg], { type: 'text/plain' }));
}),
'image/svg+xml': new Promise(async (resolve) => {
svg = await optimizeSVG(svg);
resolve(new Blob([svg], { type: 'image/svg+xml' }));
}),
}),
}),
]);
]);
}
} catch (err) {
console.log(err.name, err.message);
svg = await optimizeSVG(svg);
Expand Down
5 changes: 4 additions & 1 deletion src/js/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const convertToColorSVG = async (imageData) => {
intervalID.current = setInterval(() => {
const svg = `${prefix}${paths}${suffix}`;
if (svg.length !== lastLength) {
svgOutput.setAttribute('transform', svgOutput.dataset.transform);
const transform = svgOutput.dataset.transform;
if (transform) {
svgOutput.setAttribute('transform', transform);
}
svgOutput.innerHTML = svg;
lastLength = svg.length;
}
Expand Down
20 changes: 20 additions & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,28 @@ if ('onbeforeinstallprompt' in window && 'onappinstalled' in window) {
installButton.style.display = 'none';
}

// From https://stackoverflow.com/a/62963963/6255000.
const supportsWorkerType = () => {
let supports = false;
const tester = {
get type() {
supports = true;
},
};
try {
new Worker('blob://', tester);
} finally {
return supports;
}
};

(async () => {
initUI();
if (!supportsWorkerType()) {
await import(
'../.././node_modules/module-workers-polyfill/module-workers-polyfill.min.js'
);
}

const updateSW = registerSW({
onOfflineReady() {
Expand Down
14 changes: 10 additions & 4 deletions src/js/orchestrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ const startProcessing = async () => {
}
const transform = svgOutput.getAttribute('transform');
svgOutput.innerHTML = spinnerSVG;
svgOutput.dataset.transform = transform;
svgOutput.setAttribute('transform', '');
if (transform) {
svgOutput.dataset.transform = transform;
svgOutput.setAttribute('transform', '');
}
const imageData = supportsOffscreenCanvas
? await preProcessInputImage()
: preProcessMainCanvas();
if (colorRadio.checked) {
const svg = await convertToColorSVG(imageData);
svgOutput.setAttribute('transform', transform);
if (transform) {
svgOutput.setAttribute('transform', transform);
}
displayResult(svg, COLOR);
} else {
const svg = await convertToMonochromeSVG(imageData);
svgOutput.setAttribute('transform', transform);
if (transform) {
svgOutput.setAttribute('transform', transform);
}
displayResult(svg, MONOCHROME);
}
};
Expand Down
6 changes: 5 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import webmanifest from './src/manifest.json';
export default {
plugins: [
dynamicImportVars({
include: ['./src/i18n/*', './src/js/filehandling.js'],
include: [
'./src/i18n/*',
'./src/js/filehandling.js',
'./node_modules/module-workers-polyfill/module-workers-polyfill.min.js',
],
}),
vitePWA({
registerType: 'autoUpdate',
Expand Down

0 comments on commit 1caf928

Please sign in to comment.