@mirage-engine/sandwich 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/sandwich.js +78 -0
- package/dist/sandwich.umd.js +1 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/vite.config.d.ts +2 -0
- package/package.json +22 -0
- package/src/index.ts +143 -0
- package/tsconfig.json +11 -0
- package/vite.config.ts +33 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 siwoo lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/sandwich.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var h = (r, e, t) => e in r ? d(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var n = (r, e, t) => (h(r, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
+
import { Tracker as l } from "@mirage-engine/dom-tracker";
|
|
5
|
+
class p {
|
|
6
|
+
constructor(e = {}) {
|
|
7
|
+
n(this, "frontSelector");
|
|
8
|
+
n(this, "midLayerElement");
|
|
9
|
+
n(this, "items", []);
|
|
10
|
+
n(this, "isInitialized", !1);
|
|
11
|
+
n(this, "midLayer", null);
|
|
12
|
+
n(this, "frontLayer", null);
|
|
13
|
+
n(this, "tracker", null);
|
|
14
|
+
this.frontSelector = e.frontSelector || ".front", this.midLayerElement = e.midLayerElement || null, this.items = [], this.isInitialized = !1, this.tracker = new l(document.body, e.trackerConfig || { resizeDebounce: !0 });
|
|
15
|
+
}
|
|
16
|
+
init() {
|
|
17
|
+
var e;
|
|
18
|
+
this.isInitialized || (this.createLayers(), this.setupMidLayer(), this.hijackFrontElements(), this.setupHooks(), (e = this.tracker) == null || e.start(), this.isInitialized = !0, console.log("🥪 sand-telepochi initialized!"));
|
|
19
|
+
}
|
|
20
|
+
// Allow passing an existing tracker (e.g. from mirage-engine/core)
|
|
21
|
+
useTracker(e) {
|
|
22
|
+
this.tracker = e, this.setupHooks();
|
|
23
|
+
}
|
|
24
|
+
createLayers() {
|
|
25
|
+
this.midLayer = document.createElement("div"), this.midLayer.id = "sand-layer-mid", Object.assign(this.midLayer.style, {
|
|
26
|
+
position: "fixed",
|
|
27
|
+
inset: "0",
|
|
28
|
+
zIndex: "9998",
|
|
29
|
+
pointerEvents: "none"
|
|
30
|
+
}), this.frontLayer = document.createElement("div"), this.frontLayer.id = "sand-layer-front", Object.assign(this.frontLayer.style, {
|
|
31
|
+
position: "fixed",
|
|
32
|
+
inset: "0",
|
|
33
|
+
zIndex: "9999",
|
|
34
|
+
pointerEvents: "none"
|
|
35
|
+
}), document.body.appendChild(this.midLayer), document.body.appendChild(this.frontLayer);
|
|
36
|
+
}
|
|
37
|
+
setupMidLayer() {
|
|
38
|
+
this.midLayerElement && this.midLayer && (this.midLayer.appendChild(this.midLayerElement), this.midLayerElement.style.pointerEvents = "auto");
|
|
39
|
+
}
|
|
40
|
+
hijackFrontElements() {
|
|
41
|
+
const e = document.querySelectorAll(this.frontSelector);
|
|
42
|
+
this.frontLayer && e.forEach((t) => {
|
|
43
|
+
var a;
|
|
44
|
+
const s = t.getBoundingClientRect(), i = window.getComputedStyle(t), o = document.createElement("div");
|
|
45
|
+
o.className = "sand-placeholder", Object.assign(o.style, {
|
|
46
|
+
display: i.display === "inline" ? "inline-block" : i.display,
|
|
47
|
+
width: `${s.width}px`,
|
|
48
|
+
height: `${s.height}px`,
|
|
49
|
+
boxSizing: "border-box",
|
|
50
|
+
marginTop: i.marginTop,
|
|
51
|
+
marginBottom: i.marginBottom,
|
|
52
|
+
marginLeft: i.marginLeft,
|
|
53
|
+
marginRight: i.marginRight,
|
|
54
|
+
visibility: "hidden",
|
|
55
|
+
pointerEvents: "none"
|
|
56
|
+
}), (a = t.parentNode) == null || a.insertBefore(o, t), this.frontLayer.appendChild(t), Object.assign(t.style, {
|
|
57
|
+
position: "fixed",
|
|
58
|
+
margin: "0",
|
|
59
|
+
boxSizing: "border-box",
|
|
60
|
+
width: `${s.width}px`,
|
|
61
|
+
height: `${s.height}px`,
|
|
62
|
+
pointerEvents: "auto"
|
|
63
|
+
}), this.items.push({ placeholder: o, original: t });
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
setupHooks() {
|
|
67
|
+
this.tracker && this.tracker.onBeforeRender.add(() => {
|
|
68
|
+
const e = this.items.map(({ placeholder: t }) => t.getBoundingClientRect());
|
|
69
|
+
this.items.forEach(({ original: t }, s) => {
|
|
70
|
+
const i = e[s];
|
|
71
|
+
t.style.top = `${i.top}px`, t.style.left = `${i.left}px`, t.style.width = `${i.width}px`, t.style.height = `${i.height}px`;
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
p as SandwichRenderer
|
|
78
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(t,i){typeof exports=="object"&&typeof module!="undefined"?i(exports,require("@mirage-engine/dom-tracker")):typeof define=="function"&&define.amd?define(["exports","@mirage-engine/dom-tracker"],i):(t=typeof globalThis!="undefined"?globalThis:t||self,i(t.SandwichRenderer={},t.DomTracker))})(this,function(t,i){"use strict";var c=Object.defineProperty;var l=(t,i,o)=>i in t?c(t,i,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[i]=o;var s=(t,i,o)=>(l(t,typeof i!="symbol"?i+"":i,o),o);class o{constructor(n={}){s(this,"frontSelector");s(this,"midLayerElement");s(this,"items",[]);s(this,"isInitialized",!1);s(this,"midLayer",null);s(this,"frontLayer",null);s(this,"tracker",null);this.frontSelector=n.frontSelector||".front",this.midLayerElement=n.midLayerElement||null,this.items=[],this.isInitialized=!1,this.tracker=new i.Tracker(document.body,n.trackerConfig||{resizeDebounce:!0})}init(){var n;this.isInitialized||(this.createLayers(),this.setupMidLayer(),this.hijackFrontElements(),this.setupHooks(),(n=this.tracker)==null||n.start(),this.isInitialized=!0,console.log("🥪 sand-telepochi initialized!"))}useTracker(n){this.tracker=n,this.setupHooks()}createLayers(){this.midLayer=document.createElement("div"),this.midLayer.id="sand-layer-mid",Object.assign(this.midLayer.style,{position:"fixed",inset:"0",zIndex:"9998",pointerEvents:"none"}),this.frontLayer=document.createElement("div"),this.frontLayer.id="sand-layer-front",Object.assign(this.frontLayer.style,{position:"fixed",inset:"0",zIndex:"9999",pointerEvents:"none"}),document.body.appendChild(this.midLayer),document.body.appendChild(this.frontLayer)}setupMidLayer(){this.midLayerElement&&this.midLayer&&(this.midLayer.appendChild(this.midLayerElement),this.midLayerElement.style.pointerEvents="auto")}hijackFrontElements(){const n=document.querySelectorAll(this.frontSelector);this.frontLayer&&n.forEach(e=>{var h;const d=e.getBoundingClientRect(),r=window.getComputedStyle(e),a=document.createElement("div");a.className="sand-placeholder",Object.assign(a.style,{display:r.display==="inline"?"inline-block":r.display,width:`${d.width}px`,height:`${d.height}px`,boxSizing:"border-box",marginTop:r.marginTop,marginBottom:r.marginBottom,marginLeft:r.marginLeft,marginRight:r.marginRight,visibility:"hidden",pointerEvents:"none"}),(h=e.parentNode)==null||h.insertBefore(a,e),this.frontLayer.appendChild(e),Object.assign(e.style,{position:"fixed",margin:"0",boxSizing:"border-box",width:`${d.width}px`,height:`${d.height}px`,pointerEvents:"auto"}),this.items.push({placeholder:a,original:e})})}setupHooks(){this.tracker&&this.tracker.onBeforeRender.add(()=>{const n=this.items.map(({placeholder:e})=>e.getBoundingClientRect());this.items.forEach(({original:e},d)=>{const r=n[d];e.style.top=`${r.top}px`,e.style.left=`${r.left}px`,e.style.width=`${r.width}px`,e.style.height=`${r.height}px`})})}}t.SandwichRenderer=o,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Tracker, TrackerConfig } from '@mirage-engine/dom-tracker';
|
|
2
|
+
|
|
3
|
+
interface SandwichOptions {
|
|
4
|
+
frontSelector?: string;
|
|
5
|
+
midLayerElement?: HTMLElement;
|
|
6
|
+
trackerConfig?: TrackerConfig;
|
|
7
|
+
}
|
|
8
|
+
export declare class SandwichRenderer {
|
|
9
|
+
private frontSelector;
|
|
10
|
+
private midLayerElement;
|
|
11
|
+
private items;
|
|
12
|
+
private isInitialized;
|
|
13
|
+
private midLayer;
|
|
14
|
+
private frontLayer;
|
|
15
|
+
private tracker;
|
|
16
|
+
constructor(options?: SandwichOptions);
|
|
17
|
+
init(): void;
|
|
18
|
+
useTracker(tracker: Tracker): void;
|
|
19
|
+
private createLayers;
|
|
20
|
+
private setupMidLayer;
|
|
21
|
+
private hijackFrontElements;
|
|
22
|
+
private setupHooks;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mirage-engine/sandwich",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"types": "src/index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "vite build"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/dltldn333/MirageEngine",
|
|
12
|
+
"directory": "packages/sandwich"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@mirage-engine/dom-tracker": "workspace:*"
|
|
16
|
+
},
|
|
17
|
+
"private": false,
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public",
|
|
20
|
+
"registry": "https://registry.npmjs.org/"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Tracker, TrackerConfig } from "@mirage-engine/dom-tracker";
|
|
2
|
+
|
|
3
|
+
interface SandwichOptions {
|
|
4
|
+
frontSelector?: string;
|
|
5
|
+
midLayerElement?: HTMLElement;
|
|
6
|
+
trackerConfig?: TrackerConfig;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface SandwichItem {
|
|
10
|
+
placeholder: HTMLDivElement;
|
|
11
|
+
original: HTMLElement;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class SandwichRenderer {
|
|
15
|
+
private frontSelector: string;
|
|
16
|
+
private midLayerElement: HTMLElement | null;
|
|
17
|
+
private items: SandwichItem[] = [];
|
|
18
|
+
private isInitialized: boolean = false;
|
|
19
|
+
private midLayer: HTMLDivElement | null = null;
|
|
20
|
+
private frontLayer: HTMLDivElement | null = null;
|
|
21
|
+
private tracker: Tracker | null = null;
|
|
22
|
+
|
|
23
|
+
constructor(options: SandwichOptions = {}) {
|
|
24
|
+
this.frontSelector = options.frontSelector || ".front";
|
|
25
|
+
this.midLayerElement = options.midLayerElement || null;
|
|
26
|
+
this.items = [];
|
|
27
|
+
this.isInitialized = false;
|
|
28
|
+
|
|
29
|
+
// We can either use a passed tracker, or create one if this is used standalone
|
|
30
|
+
this.tracker = new Tracker(document.body, options.trackerConfig || { resizeDebounce: true });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
init(): void {
|
|
34
|
+
if (this.isInitialized) return;
|
|
35
|
+
|
|
36
|
+
this.createLayers();
|
|
37
|
+
this.setupMidLayer();
|
|
38
|
+
this.hijackFrontElements();
|
|
39
|
+
|
|
40
|
+
this.setupHooks();
|
|
41
|
+
this.tracker?.start();
|
|
42
|
+
|
|
43
|
+
this.isInitialized = true;
|
|
44
|
+
console.log("🥪 sand-telepochi initialized!");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Allow passing an existing tracker (e.g. from mirage-engine/core)
|
|
48
|
+
useTracker(tracker: Tracker) {
|
|
49
|
+
this.tracker = tracker;
|
|
50
|
+
this.setupHooks();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private createLayers(): void {
|
|
54
|
+
this.midLayer = document.createElement("div");
|
|
55
|
+
this.midLayer.id = "sand-layer-mid";
|
|
56
|
+
Object.assign(this.midLayer.style, {
|
|
57
|
+
position: "fixed",
|
|
58
|
+
inset: "0",
|
|
59
|
+
zIndex: "9998",
|
|
60
|
+
pointerEvents: "none",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
this.frontLayer = document.createElement("div");
|
|
64
|
+
this.frontLayer.id = "sand-layer-front";
|
|
65
|
+
Object.assign(this.frontLayer.style, {
|
|
66
|
+
position: "fixed",
|
|
67
|
+
inset: "0",
|
|
68
|
+
zIndex: "9999",
|
|
69
|
+
pointerEvents: "none",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
document.body.appendChild(this.midLayer);
|
|
73
|
+
document.body.appendChild(this.frontLayer);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private setupMidLayer(): void {
|
|
77
|
+
if (this.midLayerElement && this.midLayer) {
|
|
78
|
+
this.midLayer.appendChild(this.midLayerElement);
|
|
79
|
+
this.midLayerElement.style.pointerEvents = "auto";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private hijackFrontElements(): void {
|
|
84
|
+
const frontElements = document.querySelectorAll<HTMLElement>(this.frontSelector);
|
|
85
|
+
|
|
86
|
+
if (!this.frontLayer) return;
|
|
87
|
+
|
|
88
|
+
frontElements.forEach((el) => {
|
|
89
|
+
const rect = el.getBoundingClientRect();
|
|
90
|
+
const computed = window.getComputedStyle(el);
|
|
91
|
+
const placeholder = document.createElement("div");
|
|
92
|
+
|
|
93
|
+
placeholder.className = "sand-placeholder";
|
|
94
|
+
|
|
95
|
+
Object.assign(placeholder.style, {
|
|
96
|
+
display: computed.display === "inline" ? "inline-block" : computed.display,
|
|
97
|
+
width: `${rect.width}px`,
|
|
98
|
+
height: `${rect.height}px`,
|
|
99
|
+
boxSizing: "border-box",
|
|
100
|
+
marginTop: computed.marginTop,
|
|
101
|
+
marginBottom: computed.marginBottom,
|
|
102
|
+
marginLeft: computed.marginLeft,
|
|
103
|
+
marginRight: computed.marginRight,
|
|
104
|
+
visibility: "hidden",
|
|
105
|
+
pointerEvents: "none",
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
el.parentNode?.insertBefore(placeholder, el);
|
|
109
|
+
|
|
110
|
+
this.frontLayer!.appendChild(el);
|
|
111
|
+
|
|
112
|
+
Object.assign(el.style, {
|
|
113
|
+
position: "fixed",
|
|
114
|
+
margin: "0",
|
|
115
|
+
boxSizing: "border-box",
|
|
116
|
+
width: `${rect.width}px`,
|
|
117
|
+
height: `${rect.height}px`,
|
|
118
|
+
pointerEvents: "auto",
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
this.items.push({ placeholder, original: el });
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private setupHooks(): void {
|
|
126
|
+
if (!this.tracker) return;
|
|
127
|
+
|
|
128
|
+
this.tracker.onBeforeRender.add(() => {
|
|
129
|
+
// Before render, sync positions (Read + Write)
|
|
130
|
+
// Since dom-tracker handles MutationObserver, we should sync rects.
|
|
131
|
+
// Wait, to avoid layout thrashing, we should read ALL rects first, then write ALL tops.
|
|
132
|
+
const rects = this.items.map(({ placeholder }) => placeholder.getBoundingClientRect());
|
|
133
|
+
|
|
134
|
+
this.items.forEach(({ original }, index) => {
|
|
135
|
+
const rect = rects[index];
|
|
136
|
+
original.style.top = `${rect.top}px`;
|
|
137
|
+
original.style.left = `${rect.left}px`;
|
|
138
|
+
original.style.width = `${rect.width}px`;
|
|
139
|
+
original.style.height = `${rect.height}px`;
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
package/tsconfig.json
ADDED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineConfig } from "vite";
|
|
2
|
+
import dts from "vite-plugin-dts";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
build: {
|
|
7
|
+
target: "es2015",
|
|
8
|
+
lib: {
|
|
9
|
+
entry: path.resolve(__dirname, "src/index.ts"),
|
|
10
|
+
name: "SandwichRenderer",
|
|
11
|
+
fileName: (format) =>
|
|
12
|
+
`sandwich.${format === "es" ? "js" : "umd.js"}`,
|
|
13
|
+
},
|
|
14
|
+
rollupOptions: {
|
|
15
|
+
external: ["@mirage-engine/dom-tracker"],
|
|
16
|
+
output: {
|
|
17
|
+
globals: {
|
|
18
|
+
"@mirage-engine/dom-tracker": "DomTracker",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
plugins: [
|
|
24
|
+
dts({
|
|
25
|
+
insertTypesEntry: true,
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
resolve: {
|
|
29
|
+
alias: {
|
|
30
|
+
"@": path.resolve(__dirname, "./src"),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|