mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 00:37:19 +00:00
Merge branch 'dev' into fix-permissions
This commit is contained in:
@@ -30,7 +30,7 @@ npm run build
|
|||||||
You can run the development server with:
|
You can run the development server with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm start
|
npm run dev
|
||||||
```
|
```
|
||||||
|
|
||||||
[squoosh]: https://squoosh.app
|
[squoosh]: https://squoosh.app
|
||||||
|
|||||||
@@ -183,31 +183,36 @@ function progressTracker(results) {
|
|||||||
return tracker;
|
return tracker;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkInputFilesValid(files) {
|
async function getInputFiles(paths) {
|
||||||
const validFiles = [];
|
const validFiles = [];
|
||||||
|
|
||||||
for (const file of files) {
|
for (const path of paths) {
|
||||||
try {
|
const files = (await fsp.lstat(path)).isDirectory()
|
||||||
await fsp.stat(file);
|
? (await fsp.readdir(path)).map(file => join(path, file))
|
||||||
} catch (err) {
|
: [path];
|
||||||
if (err.code === 'ENOENT') {
|
for (const file of files) {
|
||||||
console.warn(
|
try {
|
||||||
`Warning: Input file does not exist: ${resolvePath(file)}`,
|
await fsp.stat(file);
|
||||||
);
|
} catch (err) {
|
||||||
continue;
|
if (err.code === 'ENOENT') {
|
||||||
} else {
|
console.warn(
|
||||||
throw err;
|
`Warning: Input file does not exist: ${resolvePath(file)}`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
validFiles.push(file);
|
validFiles.push(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return validFiles;
|
return validFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFiles(files) {
|
async function processFiles(files) {
|
||||||
files = await checkInputFilesValid(files);
|
files = await getInputFiles(files);
|
||||||
|
|
||||||
const parallelism = cpus().length;
|
const parallelism = cpus().length;
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ function jobPromise(worker, msg) {
|
|||||||
|
|
||||||
export default class WorkerPool {
|
export default class WorkerPool {
|
||||||
constructor(numWorkers, workerFile) {
|
constructor(numWorkers, workerFile) {
|
||||||
this.closing = false;
|
this.numWorkers = numWorkers;
|
||||||
this.jobQueue = new TransformStream();
|
this.jobQueue = new TransformStream();
|
||||||
this.workerQueue = new TransformStream();
|
this.workerQueue = new TransformStream();
|
||||||
|
|
||||||
@@ -42,7 +42,6 @@ export default class WorkerPool {
|
|||||||
while (true) {
|
while (true) {
|
||||||
const { value, done } = await reader.read();
|
const { value, done } = await reader.read();
|
||||||
if (done) {
|
if (done) {
|
||||||
this.workerQueue.writable.close();
|
|
||||||
await this._terminateAll();
|
await this._terminateAll();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -50,12 +49,6 @@ export default class WorkerPool {
|
|||||||
const worker = await this._nextWorker();
|
const worker = await this._nextWorker();
|
||||||
jobPromise(worker, msg).then((result) => {
|
jobPromise(worker, msg).then((result) => {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
// If we are in the process of closing, `workerQueue` is
|
|
||||||
// already closed and we can’t requeue the worker.
|
|
||||||
if (this.closing) {
|
|
||||||
worker.terminate();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const writer = this.workerQueue.writable.getWriter();
|
const writer = this.workerQueue.writable.getWriter();
|
||||||
writer.write(worker);
|
writer.write(worker);
|
||||||
writer.releaseLock();
|
writer.releaseLock();
|
||||||
@@ -71,18 +64,15 @@ export default class WorkerPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _terminateAll() {
|
async _terminateAll() {
|
||||||
while (true) {
|
for (let n = 0; n < this.numWorkers; n++) {
|
||||||
const worker = await this._nextWorker();
|
const worker = await this._nextWorker();
|
||||||
if (!worker) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
}
|
}
|
||||||
|
this.workerQueue.writable.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
async join() {
|
async join() {
|
||||||
this.closing = true;
|
this.jobQueue.writable.getWriter().close();
|
||||||
this.jobQueue.writable.close();
|
|
||||||
await this.done;
|
await this.done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ export default class TwoUp extends HTMLElement {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
window.addEventListener('keydown', event => this._onKeyDown(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
@@ -94,6 +96,29 @@ export default class TwoUp extends HTMLElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeyDown event handler
|
||||||
|
private _onKeyDown(event: KeyboardEvent) {
|
||||||
|
if (event.code === 'Digit1' || event.code === 'Numpad1') {
|
||||||
|
this._position = 0;
|
||||||
|
this._relativePosition = 0;
|
||||||
|
this._setPosition();
|
||||||
|
} else if (event.code === 'Digit2' || event.code === 'Numpad2') {
|
||||||
|
const dimensionAxis = this.orientation === 'vertical' ? 'height' : 'width';
|
||||||
|
const bounds = this.getBoundingClientRect();
|
||||||
|
|
||||||
|
this._position = bounds[dimensionAxis] / 2;
|
||||||
|
this._relativePosition = (this._position / bounds[dimensionAxis]) / 2;
|
||||||
|
this._setPosition();
|
||||||
|
} else if (event.code === 'Digit3' || event.code === 'Numpad3') {
|
||||||
|
const dimensionAxis = this.orientation === 'vertical' ? 'height' : 'width';
|
||||||
|
const bounds = this.getBoundingClientRect();
|
||||||
|
|
||||||
|
this._position = bounds[dimensionAxis];
|
||||||
|
this._relativePosition = this._position / bounds[dimensionAxis];
|
||||||
|
this._setPosition();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private _resetPosition() {
|
private _resetPosition() {
|
||||||
// Set the initial position of the handle.
|
// Set the initial position of the handle.
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
|
|||||||
@@ -604,6 +604,7 @@ export default class Compress extends Component<Props, State> {
|
|||||||
const resizeState: Partial<ProcessorState['resize']> = {
|
const resizeState: Partial<ProcessorState['resize']> = {
|
||||||
width: decoded.width,
|
width: decoded.width,
|
||||||
height: decoded.height,
|
height: decoded.height,
|
||||||
|
method: vectorImage ? 'vector' : 'lanczos3',
|
||||||
// Disable resizing, to make it clearer to the user that something changed here
|
// Disable resizing, to make it clearer to the user that something changed here
|
||||||
enabled: false,
|
enabled: false,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user