mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-15 10:09:45 +00:00
Compare commits
1 Commits
extract-pi
...
analytics
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce4010e52b |
18
_headers.ejs
18
_headers.ejs
@@ -1,18 +0,0 @@
|
|||||||
# Long-term cache by default.
|
|
||||||
/*
|
|
||||||
Cache-Control: max-age=31536000
|
|
||||||
|
|
||||||
# And here are the exceptions:
|
|
||||||
/
|
|
||||||
Cache-Control: no-cache
|
|
||||||
|
|
||||||
/serviceworker.js
|
|
||||||
Cache-Control: no-cache
|
|
||||||
|
|
||||||
/manifest.json
|
|
||||||
Cache-Control: must-revalidate, max-age=3600
|
|
||||||
|
|
||||||
# URLs in /assets do not include a hash and are mutable.
|
|
||||||
# But it isn't a big deal if the user gets an old version.
|
|
||||||
/assets/*
|
|
||||||
Cache-Control: must-revalidate, max-age=3600
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
const path = require('path');
|
|
||||||
const fs = require('fs');
|
|
||||||
const ejs = require('ejs');
|
|
||||||
const AssetsPlugin = require('assets-webpack-plugin');
|
|
||||||
|
|
||||||
module.exports = class AssetTemplatePlugin extends AssetsPlugin {
|
|
||||||
constructor(options) {
|
|
||||||
options = options || {};
|
|
||||||
if (!options.template) throw Error('AssetTemplatePlugin: template option is required.');
|
|
||||||
super({
|
|
||||||
useCompilerPath: true,
|
|
||||||
filename: options.filename,
|
|
||||||
processOutput: files => this._processOutput(files)
|
|
||||||
});
|
|
||||||
this._template = path.resolve(process.cwd(), options.template);
|
|
||||||
const ignore = options.ignore || /(manifest\.json|\.DS_Store)$/;
|
|
||||||
this._ignore = typeof ignore === 'function' ? ({ test: ignore }) : ignore;
|
|
||||||
}
|
|
||||||
|
|
||||||
_processOutput(files) {
|
|
||||||
const mapping = {
|
|
||||||
all: [],
|
|
||||||
byType: {},
|
|
||||||
entries: {}
|
|
||||||
};
|
|
||||||
for (const entryName in files) {
|
|
||||||
// non-entry-point-derived assets are collected under an empty string key
|
|
||||||
// since that's a bit awkward, we'll call them "assets"
|
|
||||||
const name = entryName === '' ? 'assets' : entryName;
|
|
||||||
const listing = files[entryName];
|
|
||||||
const entry = mapping.entries[name] = {
|
|
||||||
all: [],
|
|
||||||
byType: {}
|
|
||||||
};
|
|
||||||
for (let type in listing) {
|
|
||||||
const list = [].concat(listing[type]).filter(file => !this._ignore.test(file));
|
|
||||||
if (!list.length) continue;
|
|
||||||
mapping.all = mapping.all.concat(list);
|
|
||||||
mapping.byType[type] = (mapping.byType[type] || []).concat(list);
|
|
||||||
entry.all = entry.all.concat(list);
|
|
||||||
entry.byType[type] = (entry.byType[type] || []).concat(list);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mapping.files = mapping.all;
|
|
||||||
return ejs.render(fs.readFileSync(this._template, 'utf8'), mapping);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
33
package-lock.json
generated
33
package-lock.json
generated
@@ -578,27 +578,6 @@
|
|||||||
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
|
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"assets-webpack-plugin": {
|
|
||||||
"version": "3.9.7",
|
|
||||||
"resolved": "https://registry.npmjs.org/assets-webpack-plugin/-/assets-webpack-plugin-3.9.7.tgz",
|
|
||||||
"integrity": "sha512-yxo4MlSb++B88qQFE27Wf56ykGaDHZeKcSbrstSFOOwOxv33gWXtM49+yfYPSErlXPAMT5lVy3YPIhWlIFjYQw==",
|
|
||||||
"dev": true,
|
|
||||||
"requires": {
|
|
||||||
"camelcase": "^5.0.0",
|
|
||||||
"escape-string-regexp": "^1.0.3",
|
|
||||||
"lodash.assign": "^4.2.0",
|
|
||||||
"lodash.merge": "^4.6.1",
|
|
||||||
"mkdirp": "^0.5.1"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"camelcase": {
|
|
||||||
"version": "5.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.0.0.tgz",
|
|
||||||
"integrity": "sha512-faqwZqnWxbxn+F1d399ygeamQNy3lPp/H9H6rNrqYh4FSVCtcY+3cub1MxA8o9mDd55mM8Aghuu/kuyYA6VTsA==",
|
|
||||||
"dev": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"assign-symbols": {
|
"assign-symbols": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
|
||||||
@@ -7944,12 +7923,6 @@
|
|||||||
"integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=",
|
"integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"lodash.merge": {
|
|
||||||
"version": "4.6.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.1.tgz",
|
|
||||||
"integrity": "sha512-AOYza4+Hf5z1/0Hztxpm2/xiPZgi/cjMqdnKTUWTBSKchJlxXXuUSxCCl8rJlf4g6yww/j6mA8nC8Hw/EZWxKQ==",
|
|
||||||
"dev": true
|
|
||||||
},
|
|
||||||
"lodash.mergewith": {
|
"lodash.mergewith": {
|
||||||
"version": "4.6.1",
|
"version": "4.6.1",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.mergewith/-/lodash.mergewith-4.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.mergewith/-/lodash.mergewith-4.6.1.tgz",
|
||||||
@@ -9481,12 +9454,6 @@
|
|||||||
"integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=",
|
"integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"pinch-zoom-element": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/pinch-zoom-element/-/pinch-zoom-element-1.0.0.tgz",
|
|
||||||
"integrity": "sha512-n5yMOhV/zlOsHcTERBZIbGkhfwATAl9l5JLc5nPAhwAgY8f+PGDikh4TWgvPfGooFyQctLtD+3SNlIE3fEM/eQ==",
|
|
||||||
"dev": true
|
|
||||||
},
|
|
||||||
"pinkie": {
|
"pinkie": {
|
||||||
"version": "2.0.4",
|
"version": "2.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz",
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
"@types/pretty-bytes": "^5.1.0",
|
"@types/pretty-bytes": "^5.1.0",
|
||||||
"@types/webassembly-js-api": "0.0.1",
|
"@types/webassembly-js-api": "0.0.1",
|
||||||
"@webcomponents/custom-elements": "^1.2.1",
|
"@webcomponents/custom-elements": "^1.2.1",
|
||||||
"assets-webpack-plugin": "^3.9.7",
|
|
||||||
"babel-loader": "^7.1.5",
|
"babel-loader": "^7.1.5",
|
||||||
"babel-plugin-jsx-pragmatic": "^1.0.2",
|
"babel-plugin-jsx-pragmatic": "^1.0.2",
|
||||||
"babel-plugin-syntax-dynamic-import": "^6.18.0",
|
"babel-plugin-syntax-dynamic-import": "^6.18.0",
|
||||||
@@ -37,7 +36,6 @@
|
|||||||
"copy-webpack-plugin": "^4.5.3",
|
"copy-webpack-plugin": "^4.5.3",
|
||||||
"critters-webpack-plugin": "^2.0.1",
|
"critters-webpack-plugin": "^2.0.1",
|
||||||
"css-loader": "^0.28.11",
|
"css-loader": "^0.28.11",
|
||||||
"ejs": "^2.6.1",
|
|
||||||
"exports-loader": "^0.7.0",
|
"exports-loader": "^0.7.0",
|
||||||
"file-drop-element": "^0.0.9",
|
"file-drop-element": "^0.0.9",
|
||||||
"file-loader": "^1.1.11",
|
"file-loader": "^1.1.11",
|
||||||
@@ -47,12 +45,11 @@
|
|||||||
"if-env": "^1.0.4",
|
"if-env": "^1.0.4",
|
||||||
"linkstate": "^1.1.1",
|
"linkstate": "^1.1.1",
|
||||||
"loader-utils": "^1.1.0",
|
"loader-utils": "^1.1.0",
|
||||||
"mini-css-extract-plugin": "^0.4.4",
|
"pointer-tracker": "^2.0.3",
|
||||||
"minimatch": "^3.0.4",
|
"minimatch": "^3.0.4",
|
||||||
|
"mini-css-extract-plugin": "^0.4.4",
|
||||||
"node-sass": "^4.9.4",
|
"node-sass": "^4.9.4",
|
||||||
"optimize-css-assets-webpack-plugin": "^4.0.3",
|
"optimize-css-assets-webpack-plugin": "^4.0.3",
|
||||||
"pinch-zoom-element": "^1.0.0",
|
|
||||||
"pointer-tracker": "^2.0.3",
|
|
||||||
"preact": "^8.3.1",
|
"preact": "^8.3.1",
|
||||||
"prerender-loader": "^1.2.0",
|
"prerender-loader": "^1.2.0",
|
||||||
"pretty-bytes": "^5.1.0",
|
"pretty-bytes": "^5.1.0",
|
||||||
|
|||||||
374
src/components/Output/custom-els/PinchZoom/index.ts
Normal file
374
src/components/Output/custom-els/PinchZoom/index.ts
Normal file
@@ -0,0 +1,374 @@
|
|||||||
|
import PointerTracker, { Pointer } from 'pointer-tracker';
|
||||||
|
import './styles.css';
|
||||||
|
|
||||||
|
interface Point {
|
||||||
|
clientX: number;
|
||||||
|
clientY: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ChangeOptions {
|
||||||
|
/**
|
||||||
|
* Fire a 'change' event if values are different to current values
|
||||||
|
*/
|
||||||
|
allowChangeEvent?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ApplyChangeOpts extends ChangeOptions {
|
||||||
|
panX?: number;
|
||||||
|
panY?: number;
|
||||||
|
scaleDiff?: number;
|
||||||
|
originX?: number;
|
||||||
|
originY?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SetTransformOpts extends ChangeOptions {
|
||||||
|
scale?: number;
|
||||||
|
x?: number;
|
||||||
|
y?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScaleRelativeToValues = 'container' | 'content';
|
||||||
|
|
||||||
|
export interface ScaleToOpts extends ChangeOptions {
|
||||||
|
/** Transform origin. Can be a number, or string percent, eg "50%" */
|
||||||
|
originX?: number | string;
|
||||||
|
/** Transform origin. Can be a number, or string percent, eg "50%" */
|
||||||
|
originY?: number | string;
|
||||||
|
/** Should the transform origin be relative to the container, or content? */
|
||||||
|
relativeTo?: ScaleRelativeToValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDistance(a: Point, b?: Point): number {
|
||||||
|
if (!b) return 0;
|
||||||
|
return Math.sqrt((b.clientX - a.clientX) ** 2 + (b.clientY - a.clientY) ** 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMidpoint(a: Point, b?: Point): Point {
|
||||||
|
if (!b) return a;
|
||||||
|
|
||||||
|
return {
|
||||||
|
clientX: (a.clientX + b.clientX) / 2,
|
||||||
|
clientY: (a.clientY + b.clientY) / 2,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAbsoluteValue(value: string | number, max: number): number {
|
||||||
|
if (typeof value === 'number') return value;
|
||||||
|
|
||||||
|
if (value.trimRight().endsWith('%')) {
|
||||||
|
return max * parseFloat(value) / 100;
|
||||||
|
}
|
||||||
|
return parseFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// I'd rather use DOMMatrix/DOMPoint here, but the browser support isn't good enough.
|
||||||
|
// Given that, better to use something everything supports.
|
||||||
|
let cachedSvg: SVGSVGElement;
|
||||||
|
|
||||||
|
function getSVG(): SVGSVGElement {
|
||||||
|
return cachedSvg || (cachedSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function createMatrix(): SVGMatrix {
|
||||||
|
return getSVG().createSVGMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
function createPoint(): SVGPoint {
|
||||||
|
return getSVG().createSVGPoint();
|
||||||
|
}
|
||||||
|
|
||||||
|
const MIN_SCALE = 0.01;
|
||||||
|
|
||||||
|
export default class PinchZoom extends HTMLElement {
|
||||||
|
// The element that we'll transform.
|
||||||
|
// Ideally this would be shadow DOM, but we don't have the browser
|
||||||
|
// support yet.
|
||||||
|
private _positioningEl?: Element;
|
||||||
|
// Current transform.
|
||||||
|
private _transform: SVGMatrix = createMatrix();
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
// Watch for children changes.
|
||||||
|
// Note this won't fire for initial contents,
|
||||||
|
// so _stageElChange is also called in connectedCallback.
|
||||||
|
new MutationObserver(() => this._stageElChange())
|
||||||
|
.observe(this, { childList: true });
|
||||||
|
|
||||||
|
// Watch for pointers
|
||||||
|
const pointerTracker: PointerTracker = new PointerTracker(this, {
|
||||||
|
start: (pointer, event) => {
|
||||||
|
// We only want to track 2 pointers at most
|
||||||
|
if (pointerTracker.currentPointers.length === 2 || !this._positioningEl) return false;
|
||||||
|
event.preventDefault();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
move: (previousPointers) => {
|
||||||
|
this._onPointerMove(previousPointers, pointerTracker.currentPointers);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
this.addEventListener('wheel', event => this._onWheel(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
this._stageElChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
get x() {
|
||||||
|
return this._transform.e;
|
||||||
|
}
|
||||||
|
|
||||||
|
get y() {
|
||||||
|
return this._transform.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
get scale() {
|
||||||
|
return this._transform.a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the scale, adjusting x/y by a given transform origin.
|
||||||
|
*/
|
||||||
|
scaleTo(scale: number, opts: ScaleToOpts = {}) {
|
||||||
|
let {
|
||||||
|
originX = 0,
|
||||||
|
originY = 0,
|
||||||
|
} = opts;
|
||||||
|
|
||||||
|
const {
|
||||||
|
relativeTo = 'content',
|
||||||
|
allowChangeEvent = false,
|
||||||
|
} = opts;
|
||||||
|
|
||||||
|
const relativeToEl = (relativeTo === 'content' ? this._positioningEl : this);
|
||||||
|
|
||||||
|
// No content element? Fall back to just setting scale
|
||||||
|
if (!relativeToEl || !this._positioningEl) {
|
||||||
|
this.setTransform({ scale, allowChangeEvent });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rect = relativeToEl.getBoundingClientRect();
|
||||||
|
originX = getAbsoluteValue(originX, rect.width);
|
||||||
|
originY = getAbsoluteValue(originY, rect.height);
|
||||||
|
|
||||||
|
if (relativeTo === 'content') {
|
||||||
|
originX += this.x;
|
||||||
|
originY += this.y;
|
||||||
|
} else {
|
||||||
|
const currentRect = this._positioningEl.getBoundingClientRect();
|
||||||
|
originX -= currentRect.left;
|
||||||
|
originY -= currentRect.top;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._applyChange({
|
||||||
|
allowChangeEvent,
|
||||||
|
originX,
|
||||||
|
originY,
|
||||||
|
scaleDiff: scale / this.scale,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the stage with a given scale/x/y.
|
||||||
|
*/
|
||||||
|
setTransform(opts: SetTransformOpts = {}) {
|
||||||
|
const {
|
||||||
|
scale = this.scale,
|
||||||
|
allowChangeEvent = false,
|
||||||
|
} = opts;
|
||||||
|
|
||||||
|
let {
|
||||||
|
x = this.x,
|
||||||
|
y = this.y,
|
||||||
|
} = opts;
|
||||||
|
|
||||||
|
// If we don't have an element to position, just set the value as given.
|
||||||
|
// We'll check bounds later.
|
||||||
|
if (!this._positioningEl) {
|
||||||
|
this._updateTransform(scale, x, y, allowChangeEvent);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get current layout
|
||||||
|
const thisBounds = this.getBoundingClientRect();
|
||||||
|
const positioningElBounds = this._positioningEl.getBoundingClientRect();
|
||||||
|
|
||||||
|
// Not displayed. May be disconnected or display:none.
|
||||||
|
// Just take the values, and we'll check bounds later.
|
||||||
|
if (!thisBounds.width || !thisBounds.height) {
|
||||||
|
this._updateTransform(scale, x, y, allowChangeEvent);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create points for _positioningEl.
|
||||||
|
let topLeft = createPoint();
|
||||||
|
topLeft.x = positioningElBounds.left - thisBounds.left;
|
||||||
|
topLeft.y = positioningElBounds.top - thisBounds.top;
|
||||||
|
let bottomRight = createPoint();
|
||||||
|
bottomRight.x = positioningElBounds.width + topLeft.x;
|
||||||
|
bottomRight.y = positioningElBounds.height + topLeft.y;
|
||||||
|
|
||||||
|
// Calculate the intended position of _positioningEl.
|
||||||
|
const matrix = createMatrix()
|
||||||
|
.translate(x, y)
|
||||||
|
.scale(scale)
|
||||||
|
// Undo current transform
|
||||||
|
.multiply(this._transform.inverse());
|
||||||
|
|
||||||
|
topLeft = topLeft.matrixTransform(matrix);
|
||||||
|
bottomRight = bottomRight.matrixTransform(matrix);
|
||||||
|
|
||||||
|
// Ensure _positioningEl can't move beyond out-of-bounds.
|
||||||
|
// Correct for x
|
||||||
|
if (topLeft.x > thisBounds.width) {
|
||||||
|
x += thisBounds.width - topLeft.x;
|
||||||
|
} else if (bottomRight.x < 0) {
|
||||||
|
x += -bottomRight.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Correct for y
|
||||||
|
if (topLeft.y > thisBounds.height) {
|
||||||
|
y += thisBounds.height - topLeft.y;
|
||||||
|
} else if (bottomRight.y < 0) {
|
||||||
|
y += -bottomRight.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._updateTransform(scale, x, y, allowChangeEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update transform values without checking bounds. This is only called in setTransform.
|
||||||
|
*/
|
||||||
|
private _updateTransform(scale: number, x: number, y: number, allowChangeEvent: boolean) {
|
||||||
|
// Avoid scaling to zero
|
||||||
|
if (scale < MIN_SCALE) return;
|
||||||
|
|
||||||
|
// Return if there's no change
|
||||||
|
if (
|
||||||
|
scale === this.scale &&
|
||||||
|
x === this.x &&
|
||||||
|
y === this.y
|
||||||
|
) return;
|
||||||
|
|
||||||
|
this._transform.e = x;
|
||||||
|
this._transform.f = y;
|
||||||
|
this._transform.d = this._transform.a = scale;
|
||||||
|
|
||||||
|
this.style.setProperty('--x', this.x + 'px');
|
||||||
|
this.style.setProperty('--y', this.y + 'px');
|
||||||
|
this.style.setProperty('--scale', this.scale + '');
|
||||||
|
|
||||||
|
if (allowChangeEvent) {
|
||||||
|
const event = new Event('change', { bubbles: true });
|
||||||
|
this.dispatchEvent(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the direct children of this element change.
|
||||||
|
* Until we have have shadow dom support across the board, we
|
||||||
|
* require a single element to be the child of <pinch-zoom>, and
|
||||||
|
* that's the element we pan/scale.
|
||||||
|
*/
|
||||||
|
private _stageElChange() {
|
||||||
|
this._positioningEl = undefined;
|
||||||
|
|
||||||
|
if (this.children.length === 0) return;
|
||||||
|
|
||||||
|
this._positioningEl = this.children[0];
|
||||||
|
|
||||||
|
if (this.children.length > 1) {
|
||||||
|
console.warn('<pinch-zoom> must not have more than one child.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do a bounds check
|
||||||
|
this.setTransform({ allowChangeEvent: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onWheel(event: WheelEvent) {
|
||||||
|
if (!this._positioningEl) return;
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const currentRect = this._positioningEl.getBoundingClientRect();
|
||||||
|
let { deltaY } = event;
|
||||||
|
const { ctrlKey, deltaMode } = event;
|
||||||
|
|
||||||
|
if (deltaMode === 1) { // 1 is "lines", 0 is "pixels"
|
||||||
|
// Firefox uses "lines" for some types of mouse
|
||||||
|
deltaY *= 15;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ctrlKey is true when pinch-zooming on a trackpad.
|
||||||
|
const divisor = ctrlKey ? 100 : 300;
|
||||||
|
const scaleDiff = 1 - deltaY / divisor;
|
||||||
|
|
||||||
|
this._applyChange({
|
||||||
|
scaleDiff,
|
||||||
|
originX: event.clientX - currentRect.left,
|
||||||
|
originY: event.clientY - currentRect.top,
|
||||||
|
allowChangeEvent: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onPointerMove(previousPointers: Pointer[], currentPointers: Pointer[]) {
|
||||||
|
if (!this._positioningEl) return;
|
||||||
|
|
||||||
|
// Combine next points with previous points
|
||||||
|
const currentRect = this._positioningEl.getBoundingClientRect();
|
||||||
|
|
||||||
|
// For calculating panning movement
|
||||||
|
const prevMidpoint = getMidpoint(previousPointers[0], previousPointers[1]);
|
||||||
|
const newMidpoint = getMidpoint(currentPointers[0], currentPointers[1]);
|
||||||
|
|
||||||
|
// Midpoint within the element
|
||||||
|
const originX = prevMidpoint.clientX - currentRect.left;
|
||||||
|
const originY = prevMidpoint.clientY - currentRect.top;
|
||||||
|
|
||||||
|
// Calculate the desired change in scale
|
||||||
|
const prevDistance = getDistance(previousPointers[0], previousPointers[1]);
|
||||||
|
const newDistance = getDistance(currentPointers[0], currentPointers[1]);
|
||||||
|
const scaleDiff = prevDistance ? newDistance / prevDistance : 1;
|
||||||
|
|
||||||
|
this._applyChange({
|
||||||
|
originX, originY, scaleDiff,
|
||||||
|
panX: newMidpoint.clientX - prevMidpoint.clientX,
|
||||||
|
panY: newMidpoint.clientY - prevMidpoint.clientY,
|
||||||
|
allowChangeEvent: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Transform the view & fire a change event */
|
||||||
|
private _applyChange(opts: ApplyChangeOpts = {}) {
|
||||||
|
const {
|
||||||
|
panX = 0, panY = 0,
|
||||||
|
originX = 0, originY = 0,
|
||||||
|
scaleDiff = 1,
|
||||||
|
allowChangeEvent = false,
|
||||||
|
} = opts;
|
||||||
|
|
||||||
|
const matrix = createMatrix()
|
||||||
|
// Translate according to panning.
|
||||||
|
.translate(panX, panY)
|
||||||
|
// Scale about the origin.
|
||||||
|
.translate(originX, originY)
|
||||||
|
// Apply current translate
|
||||||
|
.translate(this.x, this.y)
|
||||||
|
.scale(scaleDiff)
|
||||||
|
.translate(-originX, -originY)
|
||||||
|
// Apply current scale.
|
||||||
|
.scale(this.scale);
|
||||||
|
|
||||||
|
// Convert the transform into basic translate & scale.
|
||||||
|
this.setTransform({
|
||||||
|
allowChangeEvent,
|
||||||
|
scale: matrix.a,
|
||||||
|
x: matrix.e,
|
||||||
|
y: matrix.f,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define('pinch-zoom', PinchZoom);
|
||||||
16
src/components/Output/custom-els/PinchZoom/missing-types.d.ts
vendored
Normal file
16
src/components/Output/custom-els/PinchZoom/missing-types.d.ts
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
declare interface CSSStyleDeclaration {
|
||||||
|
willChange: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TypeScript, you make me sad.
|
||||||
|
// https://github.com/Microsoft/TypeScript/issues/18756
|
||||||
|
interface Window {
|
||||||
|
PointerEvent: typeof PointerEvent;
|
||||||
|
Touch: typeof Touch;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare namespace JSX {
|
||||||
|
interface IntrinsicElements {
|
||||||
|
'pinch-zoom': HTMLAttributes;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/components/Output/custom-els/PinchZoom/styles.css
Normal file
14
src/components/Output/custom-els/PinchZoom/styles.css
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
pinch-zoom {
|
||||||
|
display: block;
|
||||||
|
overflow: hidden;
|
||||||
|
touch-action: none;
|
||||||
|
--scale: 1;
|
||||||
|
--x: 0;
|
||||||
|
--y: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pinch-zoom > * {
|
||||||
|
transform: translate(var(--x), var(--y)) scale(var(--scale));
|
||||||
|
transform-origin: 0 0;
|
||||||
|
will-change: transform;
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { h, Component } from 'preact';
|
import { h, Component } from 'preact';
|
||||||
import PinchZoom, { ScaleToOpts } from 'pinch-zoom-element/lib';
|
import PinchZoom, { ScaleToOpts } from './custom-els/PinchZoom';
|
||||||
import 'pinch-zoom-element/lib';
|
import './custom-els/PinchZoom';
|
||||||
import './custom-els/TwoUp';
|
import './custom-els/TwoUp';
|
||||||
import * as style from './style.scss';
|
import * as style from './style.scss';
|
||||||
import { bind, linkRef } from '../../lib/initial-util';
|
import { bind, linkRef } from '../../lib/initial-util';
|
||||||
|
|||||||
5
src/components/Output/missing-types.d.ts
vendored
5
src/components/Output/missing-types.d.ts
vendored
@@ -1,5 +0,0 @@
|
|||||||
declare namespace JSX {
|
|
||||||
interface IntrinsicElements {
|
|
||||||
'pinch-zoom': HTMLAttributes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -42,9 +42,6 @@ async function updateReady(reg: ServiceWorkerRegistration): Promise<void> {
|
|||||||
|
|
||||||
/** Set up the service worker and monitor changes */
|
/** Set up the service worker and monitor changes */
|
||||||
export async function offliner(showSnack: SnackBarElement['showSnackbar']) {
|
export async function offliner(showSnack: SnackBarElement['showSnackbar']) {
|
||||||
// This needs to be a typeof because Webpack.
|
|
||||||
if (typeof PRERENDER === 'boolean') return;
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
navigator.serviceWorker.register('../sw');
|
navigator.serviceWorker.register('../sw');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ export async function cacheBasics(cacheName: string, buildAssets: string[]) {
|
|||||||
const toCache = ['/', '/assets/favicon.ico'];
|
const toCache = ['/', '/assets/favicon.ico'];
|
||||||
|
|
||||||
const prefixesToCache = [
|
const prefixesToCache = [
|
||||||
|
// First interaction JS & CSS:
|
||||||
|
'first-interaction.',
|
||||||
// Main app JS & CSS:
|
// Main app JS & CSS:
|
||||||
'main-app.',
|
'main-app.',
|
||||||
// Service worker handler:
|
// Service worker handler:
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPl
|
|||||||
const WorkerPlugin = require('worker-plugin');
|
const WorkerPlugin = require('worker-plugin');
|
||||||
const AutoSWPlugin = require('./config/auto-sw-plugin');
|
const AutoSWPlugin = require('./config/auto-sw-plugin');
|
||||||
const CrittersPlugin = require('critters-webpack-plugin');
|
const CrittersPlugin = require('critters-webpack-plugin');
|
||||||
const AssetTemplatePlugin = require('./config/asset-template-plugin');
|
|
||||||
|
|
||||||
function readJson (filename) {
|
function readJson (filename) {
|
||||||
return JSON.parse(fs.readFileSync(filename));
|
return JSON.parse(fs.readFileSync(filename));
|
||||||
@@ -141,8 +140,8 @@ module.exports = function (_, env) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
test: /\.tsx?$/,
|
test: /\.tsx?$/,
|
||||||
loader: 'ts-loader',
|
exclude: nodeModules,
|
||||||
options: { allowTsInNodeModules: true }
|
loader: 'ts-loader'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// All the codec files define a global with the same name as their file name. `exports-loader` attaches those to `module.exports`.
|
// All the codec files define a global with the same name as their file name. `exports-loader` attaches those to `module.exports`.
|
||||||
@@ -239,11 +238,8 @@ module.exports = function (_, env) {
|
|||||||
compile: true
|
compile: true
|
||||||
}),
|
}),
|
||||||
|
|
||||||
new AutoSWPlugin({ version: VERSION }),
|
new AutoSWPlugin({
|
||||||
|
version: VERSION
|
||||||
isProd && new AssetTemplatePlugin({
|
|
||||||
template: path.join(__dirname, '_headers.ejs'),
|
|
||||||
filename: '_headers',
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
new ScriptExtHtmlPlugin({
|
new ScriptExtHtmlPlugin({
|
||||||
|
|||||||
Reference in New Issue
Block a user