Skip to content

Commit 1fb039c

Browse files
committed
hotfix(scripts): output script + minified script
1 parent 9d7c713 commit 1fb039c

4 files changed

Lines changed: 133 additions & 12 deletions

File tree

src/Blazor.IntersectionObserver/Blazor.IntersectionObserver.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
<ItemGroup>
6060
<!-- .js/.css files will be referenced via <script>/<link> tags; other content files will just be included in the app's 'dist' directory without any tags referencing them -->
61-
<EmbeddedResource Include="wwwroot\**\*.min.js" LogicalName="blazor:js:%(RecursiveDir)%(Filename)%(Extension)" />
61+
<EmbeddedResource Include="wwwroot\**\*.js" LogicalName="blazor:js:%(RecursiveDir)%(Filename)%(Extension)" />
6262
<EmbeddedResource Include="wwwroot\**\*.css" LogicalName="blazor:css:%(RecursiveDir)%(Filename)%(Extension)" />
6363
<EmbeddedResource Include="wwwroot\**" Exclude="**\*.js;**\*.css" LogicalName="blazor:file:%(RecursiveDir)%(Filename)%(Extension)" />
6464
</ItemGroup>
@@ -78,6 +78,9 @@
7878
<EmbeddedResource Update="wwwroot\blazor-intersection-observer.min.js">
7979
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
8080
</EmbeddedResource>
81+
<EmbeddedResource Update="wwwroot\blazor-intersection-observer.js">
82+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
83+
</EmbeddedResource>
8184
</ItemGroup>
8285

8386
<ItemGroup>
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import typescript from '@rollup/plugin-typescript';
22
import { terser } from "rollup-plugin-terser";
33

4-
export default {
5-
input: 'src/index.ts',
6-
output: {
7-
file: 'wwwroot/blazor-intersection-observer.min.js',
8-
format: 'es',
9-
sourcemap: false,
4+
export default [
5+
{
6+
input: 'src/index.ts',
7+
output: {
8+
file: 'wwwroot/blazor-intersection-observer.min.js',
9+
format: 'es',
10+
sourcemap: false,
11+
},
12+
plugins: [typescript({
13+
sourceMap: false,
14+
}), terser()],
1015
},
11-
plugins: [typescript({
12-
sourceMap: false,
13-
}), terser()],
14-
};
16+
{
17+
input: 'src/index.ts',
18+
output: {
19+
file: 'wwwroot/blazor-intersection-observer.js',
20+
format: 'es',
21+
sourcemap: false,
22+
},
23+
plugins: [typescript({
24+
sourceMap: false,
25+
})],
26+
}
27+
];
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const OBSERVER_ID_PREFIX = "blazor_plugin_observer__";
2+
let OBSERVER_ID = 1;
3+
const observerItems = new Map();
4+
function reset() {
5+
OBSERVER_ID = 0;
6+
observerItems.clear();
7+
}
8+
function getObserverItems() {
9+
return observerItems;
10+
}
11+
function getObserverElementId() {
12+
return `${OBSERVER_ID_PREFIX}${OBSERVER_ID++}`;
13+
}
14+
function createObserverItem(dotnetRef, callbackId, options) {
15+
const onEntry = onEntryChange(callbackId);
16+
const observer = new IntersectionObserver(onEntry, options);
17+
observerItems.set(callbackId, { dotnetRef, observer, elements: [] });
18+
return observerItems.get(callbackId);
19+
}
20+
function observeElement(callbackId, element) {
21+
const item = observerItems.get(callbackId);
22+
if (item == null) {
23+
throw new Error(`Failed to observe element for key: ${callbackId} as the observer does not exist`);
24+
}
25+
if (item.elements.some(record => record.element == element)) {
26+
console.warn(`BlazorIntersectionObserver: The element is already being observed by observer for key ${callbackId}`);
27+
return "";
28+
}
29+
const elementId = getObserverElementId();
30+
item.observer.observe(element);
31+
item.elements.push({ elementId, element });
32+
return elementId;
33+
}
34+
function create(dotnetRef, callbackId, options) {
35+
return createObserverItem(dotnetRef, callbackId, options);
36+
}
37+
function observe(dotnetRef, callbackId, node, options) {
38+
createObserverItem(dotnetRef, callbackId, options);
39+
return observeElement(callbackId, node);
40+
}
41+
function unobserve(callbackId, element) {
42+
var _a;
43+
const item = observerItems.get(callbackId);
44+
if (item == null) {
45+
throw new Error(`Failed to unobserve element for key: ${callbackId} as the observer does not exist`);
46+
}
47+
const unobserveElementId = (_a = item.elements.find((record) => record.element == element)) === null || _a === void 0 ? void 0 : _a.elementId;
48+
if (unobserveElementId == null) {
49+
console.warn(`BlazorIntersectionObserver: The record does not exist for observer: ${callbackId}`);
50+
}
51+
item.observer.unobserve(element);
52+
item.elements = item.elements.filter((record) => record.element != element);
53+
return unobserveElementId;
54+
}
55+
function disconnect(callbackId) {
56+
const item = observerItems.get(callbackId);
57+
if (item == null) {
58+
throw new Error(`Failed to disconnect for key: ${callbackId} as the observer does not exist`);
59+
}
60+
item.observer.disconnect();
61+
item.elements = [];
62+
return true;
63+
}
64+
function remove(callbackId) {
65+
if (disconnect(callbackId)) {
66+
return observerItems.delete(callbackId);
67+
}
68+
}
69+
function toEntryObject(entry) {
70+
function toRectReadOnlyObject(obj) {
71+
return {
72+
X: obj.x,
73+
Y: obj.y,
74+
Width: obj.width,
75+
Height: obj.height,
76+
Top: obj.top,
77+
Left: obj.left,
78+
Bottom: obj.bottom,
79+
Right: obj.right,
80+
};
81+
}
82+
return {
83+
IsIntersecting: entry.isIntersecting,
84+
IntersectionRatio: entry.intersectionRatio,
85+
Time: entry.time,
86+
BoundingClientRect: toRectReadOnlyObject(entry.boundingClientRect),
87+
IntersectionRect: toRectReadOnlyObject(entry.intersectionRect),
88+
RootBounds: toRectReadOnlyObject(entry.rootBounds),
89+
};
90+
}
91+
function onEntryChange(callbackId) {
92+
return (entries) => {
93+
if (!observerItems.has(callbackId)) {
94+
return;
95+
}
96+
const { dotnetRef } = observerItems.get(callbackId);
97+
const mapped = entries.map((entry) => {
98+
const mappedEntry = toEntryObject(entry);
99+
return mappedEntry;
100+
});
101+
dotnetRef.invokeMethodAsync("OnCallback", callbackId, mapped);
102+
};
103+
}
104+
105+
export { OBSERVER_ID_PREFIX, create, disconnect, getObserverItems, observe, observeElement, remove, reset, unobserve };

src/Blazor.IntersectionObserver/wwwroot/blazor-intersection-observer.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)