- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
// Copyright 2026 Shota FUJI
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
import css from "./x-preview.css";
const SCALE_MIN = 0.1;
const SCALE_MAX = 5.0;
export class XPreview extends HTMLElement {
#slot = document.createElement("slot");
#gesturePlane = document.createElement("div");
#size = null;
#resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
this.#size = entry.contentRect;
return;
}
});
#dx = 0;
#dy = 0;
#scale = 1.0;
#isRenderScheduled = false;
constructor() {
super();
const shadow = this.attachShadow({
mode: "open",
});
const style = document.createElement("style");
style.textContent = css;
shadow.appendChild(style);
shadow.appendChild(this.#slot);
this.#gesturePlane.classList.add("gesture-plane");
shadow.appendChild(this.#gesturePlane);
const reset = document.createElement("button");
reset.textContent = "Reset Viewport";
reset.classList.add("reset");
reset.classList.add("hidden");
reset.addEventListener("click", (event) => {
this.#dx = 0;
this.#dy = 0;
this.#scale = 1.0;
this.#render();
});
shadow.appendChild(reset);
}
connectedCallback() {
this.#gesturePlane.addEventListener("wheel", this.#onWheel);
this.#render();
this.#resizeObserver.observe(this.#gesturePlane);
}
disconnectedCallback() {
this.#gesturePlane.removeEventListener("wheel", this.#onWheel);
this.#resizeObserver.disconnect();
}
#onWheel = (event) => {
event.preventDefault();
if (event.ctrlKey || event.metaKey) {
let { deltaY } = event;
// Workaround shitty design.
switch (event.deltaMode) {
// DOM_DELTA_LINE
case 1:
deltaY *= 15;
break;
// DOM_DELTA_PAGE
case 2:
deltaY *= 100;
break;
}
const previous = this.#scale;
this.#scale = Math.max(SCALE_MIN, Math.min(SCALE_MAX, this.#scale * (1 - deltaY / 100)));
const ox = event.offsetX - this.#size.width * 0.5;
const oy = event.offsetY - this.#size.height * 0.5;
this.#dx += ox / this.#scale - ox / previous;
this.#dy += oy / this.#scale - oy / previous;
} else {
this.#dx -= event.deltaX / this.#scale;
this.#dy -= event.deltaY / this.#scale;
}
this.#render();
};
#render() {
if (this.#isRenderScheduled) {
return;
}
this.#isRenderScheduled = true;
requestAnimationFrame(() => {
if (!this.isConnected) {
return;
}
for (const element of this.#slot.assignedElements()) {
element.style.transform = `scale(${this.#scale}) translate(${this.#dx}px, ${this.#dy}px)`;
}
this.#isRenderScheduled = false;
});
}
}