@lisergia/core 14.0.1 → 15.0.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.
@@ -0,0 +1,20 @@
1
+
2
+ 
3
+ > @lisergia/core@14.0.0 build
4
+ > tsup src/index.ts --format cjs,esm --dts
5
+
6
+ CLI Building entry: src/index.ts
7
+ CLI Using tsconfig: tsconfig.json
8
+ CLI tsup v8.5.0
9
+ CLI Target: node16
10
+ CJS Build start
11
+ ESM Build start
12
+ CJS dist/index.cjs 16.00 KB
13
+ CJS ⚡️ Build success in 34ms
14
+ ESM dist/index.js 13.77 KB
15
+ ESM ⚡️ Build success in 34ms
16
+ DTS Build start
17
+ DTS ⚡️ Build success in 825ms
18
+ DTS dist/index.d.cts 5.76 KB
19
+ DTS dist/index.d.ts 5.76 KB
20
+ ⠙
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @lisergia/core
2
2
 
3
+ ## 15.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - Fix Link class URL redirect and build from Changesets.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @lisergia/config-tsconfig@15.0.0
13
+
3
14
  ## 14.0.0
4
15
 
5
16
  ### Major Changes
package/dist/index.cjs ADDED
@@ -0,0 +1,620 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Application: () => Application,
34
+ ApplicationManager: () => ApplicationManager,
35
+ Component: () => Component,
36
+ EventEmitter: () => EventEmitter,
37
+ Link: () => Link,
38
+ Links: () => Links,
39
+ Page: () => Page,
40
+ Tempus: () => import_tempus2.default
41
+ });
42
+ module.exports = __toCommonJS(index_exports);
43
+
44
+ // src/App.ts
45
+ var import_mobx2 = require("mobx");
46
+
47
+ // src/EventEmitter.ts
48
+ var import_auto_bind = __toESM(require("auto-bind"), 1);
49
+ var import_nanoevents = require("nanoevents");
50
+ var EventEmitter = class {
51
+ emitter;
52
+ entries = /* @__PURE__ */ new Map();
53
+ constructor() {
54
+ (0, import_auto_bind.default)(this);
55
+ this.emitter = (0, import_nanoevents.createNanoEvents)();
56
+ }
57
+ on(event, callback) {
58
+ if (!callback) {
59
+ return console.trace("No callback provided");
60
+ }
61
+ const emitter = this.emitter.on(event, callback);
62
+ this.entries.set(callback, emitter);
63
+ return emitter;
64
+ }
65
+ off(event, callback) {
66
+ const unsubscribe = this.entries.get(callback);
67
+ if (unsubscribe) {
68
+ unsubscribe();
69
+ }
70
+ }
71
+ fire(event, ...args) {
72
+ this.emitter.emit(event, ...args);
73
+ }
74
+ destroy() {
75
+ this.entries.forEach((unsubscribe) => unsubscribe());
76
+ }
77
+ };
78
+
79
+ // src/Component.ts
80
+ var Component = class extends EventEmitter {
81
+ application;
82
+ autoListeners;
83
+ autoMount;
84
+ classes;
85
+ selector;
86
+ selectors;
87
+ id;
88
+ element;
89
+ elements = {};
90
+ constructor({
91
+ application,
92
+ autoListeners = true,
93
+ autoMount = true,
94
+ classes,
95
+ element,
96
+ elements,
97
+ id
98
+ }) {
99
+ super();
100
+ this.application = application;
101
+ this.autoListeners = autoListeners;
102
+ this.autoMount = autoMount;
103
+ this.classes = classes;
104
+ this.selector = element;
105
+ this.selectors = elements;
106
+ this.id = id;
107
+ if (this.autoMount) {
108
+ this.create();
109
+ }
110
+ if (this.autoListeners) {
111
+ this.addEventListeners();
112
+ }
113
+ }
114
+ create() {
115
+ if (this.selector) {
116
+ this.initElement(this.selector);
117
+ }
118
+ if (this.selectors) {
119
+ this.initElements(this.selectors);
120
+ }
121
+ }
122
+ initElement(selector) {
123
+ if (selector instanceof HTMLElement) {
124
+ this.element = selector;
125
+ } else {
126
+ this.element = document.querySelector(selector);
127
+ }
128
+ }
129
+ initElements(selectors) {
130
+ for (const key in selectors) {
131
+ const selector = selectors[key];
132
+ if (selector === window) {
133
+ this.elements[key] = window;
134
+ } else if (selector instanceof HTMLElement) {
135
+ this.elements[key] = selector;
136
+ } else if (selector instanceof NodeList) {
137
+ this.elements[key] = selector;
138
+ } else if (Array.isArray(selector)) {
139
+ this.elements[key] = selector;
140
+ } else {
141
+ const elements = this.element.querySelectorAll(selector);
142
+ if (elements.length === 0) {
143
+ const elements2 = document.querySelectorAll(selector);
144
+ if (elements2.length === 0) {
145
+ this.elements[key] = null;
146
+ } else if (elements2.length === 1) {
147
+ this.elements[key] = elements2[0];
148
+ } else {
149
+ this.elements[key] = elements2;
150
+ }
151
+ } else if (elements.length === 1) {
152
+ this.elements[key] = elements[0];
153
+ } else {
154
+ this.elements[key] = elements;
155
+ }
156
+ }
157
+ }
158
+ }
159
+ addEventListeners() {
160
+ }
161
+ removeEventListeners() {
162
+ }
163
+ destroy() {
164
+ super.destroy();
165
+ this.removeEventListeners();
166
+ }
167
+ };
168
+
169
+ // src/Links.ts
170
+ var import_mobx = require("mobx");
171
+
172
+ // src/Link.ts
173
+ var Link = class extends Component {
174
+ constructor({ element }) {
175
+ super({ element });
176
+ }
177
+ onClick(event) {
178
+ event.preventDefault();
179
+ this.fire("click", this.element.href);
180
+ }
181
+ addEventListeners() {
182
+ const isLocal = this.element.href.includes(window.location.origin);
183
+ const isLocalPrevented = this.element.dataset.linkOverride === "";
184
+ const isNotEmail = !this.element.href.startsWith("mailto");
185
+ const isNotPhone = !this.element.href.startsWith("tel");
186
+ const isDownload = this.element.hasAttribute("download");
187
+ const isAnchor = this.element.href.includes("#");
188
+ if (isAnchor) {
189
+ const hash = this.element.href.split("#")[1];
190
+ if (hash) {
191
+ const element = document.querySelector(`#${hash}`);
192
+ if (element) {
193
+ return;
194
+ }
195
+ }
196
+ }
197
+ if (isLocalPrevented || isDownload) {
198
+ return;
199
+ }
200
+ if (isLocal) {
201
+ this.element.onclick = this.onClick;
202
+ } else if (isNotEmail && isNotPhone) {
203
+ this.element.rel = "noopener";
204
+ this.element.setAttribute("target", "_blank");
205
+ }
206
+ }
207
+ removeEventListeners() {
208
+ this.element.onclick = null;
209
+ }
210
+ };
211
+
212
+ // src/Links.ts
213
+ var Links = class extends EventEmitter {
214
+ constructor(application) {
215
+ super();
216
+ this.application = application;
217
+ (0, import_mobx.reaction)(
218
+ () => application.currentPage,
219
+ () => this.refresh(),
220
+ { fireImmediately: true }
221
+ );
222
+ }
223
+ addEventListeners() {
224
+ var _a;
225
+ (_a = this.links) == null ? void 0 : _a.forEach((link) => link.destroy());
226
+ const links = document.querySelectorAll("a");
227
+ this.links = Array.from(links).map((element) => {
228
+ const link = new Link({
229
+ element
230
+ });
231
+ link.on("click", this.onLinkClick);
232
+ return link;
233
+ });
234
+ }
235
+ onLinkClick(href) {
236
+ this.application.route = href.replace(window.location.origin, "");
237
+ }
238
+ refresh() {
239
+ this.addEventListeners();
240
+ }
241
+ };
242
+
243
+ // src/App.ts
244
+ var ApplicationManager = class extends Component {
245
+ template = document.documentElement.dataset.template ?? "404";
246
+ constructor() {
247
+ super({
248
+ autoListeners: false,
249
+ element: ".app"
250
+ });
251
+ (0, import_mobx2.makeObservable)(this, {
252
+ // Application DOM Element.
253
+ element: import_mobx2.observable,
254
+ // Components.
255
+ canvas: import_mobx2.observable,
256
+ components: import_mobx2.observable,
257
+ transition: import_mobx2.observable,
258
+ // Page Information.
259
+ currentPage: import_mobx2.observable,
260
+ nextPage: import_mobx2.observable,
261
+ // Route Information.
262
+ route: import_mobx2.observable,
263
+ // Template Information.
264
+ template: import_mobx2.observable,
265
+ // Page Scroll.
266
+ scroll: import_mobx2.computed
267
+ });
268
+ (0, import_mobx2.observe)(this.components, this.onComponentChange);
269
+ (0, import_mobx2.observe)(this, "route", this.onRouteChange);
270
+ (0, import_mobx2.autorun)(this.onTitleUpdate);
271
+ (0, import_mobx2.autorun)(this.onTemplateUpdate);
272
+ this.addEventListeners();
273
+ }
274
+ onTitleUpdate() {
275
+ var _a;
276
+ const title = (_a = this.nextPage) == null ? void 0 : _a.title;
277
+ if (title) {
278
+ document.title = title;
279
+ }
280
+ }
281
+ onTemplateUpdate() {
282
+ var _a;
283
+ const template = (_a = this.nextPage) == null ? void 0 : _a.template;
284
+ if (template) {
285
+ document.documentElement.dataset.template = template;
286
+ this.template = template;
287
+ }
288
+ }
289
+ //
290
+ // Components.
291
+ //
292
+ canvas;
293
+ components = [];
294
+ transition;
295
+ initComponents(components) {
296
+ const classes = components.map(
297
+ ({ component: Component2 }) => new Component2({
298
+ application: this
299
+ })
300
+ );
301
+ this.canvas = classes.find((component) => component.id === "canvas");
302
+ this.transition = classes.find((component) => component.id === "transition");
303
+ classes.forEach((component) => {
304
+ this.addComponent(component);
305
+ });
306
+ }
307
+ addComponent(component) {
308
+ this.components.push(component);
309
+ }
310
+ removeComponent(component) {
311
+ component.destroy();
312
+ const index = this.components.indexOf(component);
313
+ if (index !== -1) {
314
+ this.components.splice(index, 1);
315
+ }
316
+ }
317
+ onComponentChange(event) {
318
+ if (event.type === "splice") {
319
+ const { added, removed } = event;
320
+ }
321
+ }
322
+ //
323
+ // Datasets.
324
+ //
325
+ datasets = [];
326
+ initDatasets(datasets) {
327
+ this.datasets = datasets;
328
+ }
329
+ //
330
+ // Routes.
331
+ //
332
+ pages = /* @__PURE__ */ new Map();
333
+ initRoutes(routes) {
334
+ routes.forEach(({ component, template }) => {
335
+ this.pages.set(template, component);
336
+ });
337
+ }
338
+ //
339
+ // Sprites.
340
+ //
341
+ async initSprites(url = "/bundle.svg") {
342
+ const request = await window.fetch(url);
343
+ const response = await request.text();
344
+ const sprite = document.createElement("div");
345
+ sprite.innerHTML = response;
346
+ sprite.style.left = "-999999px";
347
+ sprite.style.opacity = "0";
348
+ sprite.style.position = "absolute";
349
+ sprite.style.top = "0";
350
+ document.body.appendChild(sprite);
351
+ }
352
+ initPage() {
353
+ this.createPage();
354
+ this.createLinks();
355
+ }
356
+ //
357
+ // Links.
358
+ //
359
+ createLinks() {
360
+ this.links = new Links(this);
361
+ }
362
+ //
363
+ // Page.
364
+ //
365
+ currentPage = void 0;
366
+ createPage(template = this.template) {
367
+ const PageClass = this.pages.get(template);
368
+ const page = new PageClass({
369
+ application: this,
370
+ datasets: this.datasets
371
+ });
372
+ this.currentPage = page;
373
+ this.currentPage.create();
374
+ }
375
+ destroyPage() {
376
+ if (this.currentPage) {
377
+ this.currentPage.destroy();
378
+ }
379
+ }
380
+ //
381
+ // Navigate.
382
+ //
383
+ route = window.location.pathname;
384
+ onRouteChange({ oldValue, newValue }) {
385
+ const href = newValue.replace(window.location.origin, "");
386
+ this.onRouteChangeRequest({
387
+ href,
388
+ pushState: true
389
+ });
390
+ }
391
+ async onRouteChangeRequest({ href, pushState = true }) {
392
+ const request = await window.fetch(href);
393
+ const response = await request.text();
394
+ this.onRequest({
395
+ href,
396
+ response,
397
+ pushState
398
+ });
399
+ }
400
+ //
401
+ // Request.
402
+ //
403
+ nextPage = {};
404
+ async onRequest({ href, response, pushState }) {
405
+ var _a, _b;
406
+ const domParser = new DOMParser();
407
+ const dom = domParser.parseFromString(response, "text/html");
408
+ const html = dom.querySelector("html");
409
+ const app = dom.querySelector(".app");
410
+ this.nextPage = {
411
+ element: app,
412
+ template: html.dataset.template ?? this.template,
413
+ title: dom.title ?? document.title
414
+ };
415
+ if (this.transition) {
416
+ await ((_b = (_a = this.transition).onTransition) == null ? void 0 : _b.call(_a, this));
417
+ } else {
418
+ this.currentPage.element.remove();
419
+ this.currentPage.destroy();
420
+ this.element.appendChild(this.nextPage.element.firstElementChild);
421
+ this.createPage(this.nextPage.template);
422
+ }
423
+ if (pushState) {
424
+ window.history.pushState({}, this.nextPage.title, href);
425
+ }
426
+ }
427
+ //
428
+ // Pop State.
429
+ //
430
+ onPopState(event) {
431
+ if (event.state === null) {
432
+ return event.preventDefault();
433
+ }
434
+ this.onRouteChangeRequest({
435
+ href: document.location.pathname,
436
+ pushState: false
437
+ });
438
+ }
439
+ //
440
+ // Scroll.
441
+ //
442
+ get scroll() {
443
+ var _a;
444
+ return ((_a = this.currentPage) == null ? void 0 : _a.scroll) ?? 0;
445
+ }
446
+ //
447
+ // Listeners.
448
+ //
449
+ addEventListeners() {
450
+ window.addEventListener("popstate", this.onPopState);
451
+ }
452
+ removeEventListeners() {
453
+ window.removeEventListener("popstate", this.onPopState);
454
+ }
455
+ };
456
+ var Application = new ApplicationManager();
457
+
458
+ // src/Page.ts
459
+ var import_lenis = __toESM(require("lenis"), 1);
460
+ var import_mobx3 = require("mobx");
461
+ var import_gsap = __toESM(require("gsap"), 1);
462
+ var import_ScrollTrigger = __toESM(require("gsap/src/ScrollTrigger"), 1);
463
+ var import_tempus = __toESM(require("tempus"), 1);
464
+ var Page = class extends Component {
465
+ datasets = [];
466
+ scroll = 0;
467
+ scrollEnabled = true;
468
+ scrollOptions = {};
469
+ constructor({
470
+ application,
471
+ classes,
472
+ datasets,
473
+ element,
474
+ elements,
475
+ scrollEnabled = true,
476
+ scrollOptions = {}
477
+ }) {
478
+ super({
479
+ autoMount: false,
480
+ autoListeners: false,
481
+ classes,
482
+ element,
483
+ elements
484
+ });
485
+ this.application = application;
486
+ this.datasets = datasets;
487
+ this.scrollEnabled = scrollEnabled;
488
+ this.scrollOptions = scrollOptions;
489
+ (0, import_mobx3.makeObservable)(this, {
490
+ element: import_mobx3.observable,
491
+ elements: import_mobx3.observable,
492
+ components: import_mobx3.observable,
493
+ scroll: import_mobx3.observable
494
+ });
495
+ }
496
+ create() {
497
+ super.create();
498
+ this.createResizeObserver();
499
+ this.createComponents();
500
+ this.createScroll();
501
+ this.addEventListeners();
502
+ }
503
+ createResizeObserver() {
504
+ this.resizeObserver = new ResizeObserver(() => {
505
+ this.onResize();
506
+ });
507
+ this.resizeObserver.observe(this.elements.wrapper);
508
+ }
509
+ destroyResizeObserver() {
510
+ this.resizeObserver.disconnect();
511
+ }
512
+ //
513
+ // Components.
514
+ //
515
+ components = /* @__PURE__ */ new Set();
516
+ createComponents() {
517
+ this.datasets.map(({ component: Component2, selector }) => {
518
+ const elements = this.element.querySelectorAll(selector);
519
+ const components = Array.from(elements).map((element) => {
520
+ const component = new Component2({
521
+ application: this.application,
522
+ element
523
+ });
524
+ return component;
525
+ });
526
+ components.forEach((component) => {
527
+ this.application.addComponent(component);
528
+ this.components.add(component);
529
+ });
530
+ });
531
+ }
532
+ destroyComponents() {
533
+ this.components.forEach((component) => {
534
+ this.application.removeComponent(component);
535
+ });
536
+ this.components.clear();
537
+ }
538
+ createScroll() {
539
+ if (!this.scrollEnabled) {
540
+ this.element.addEventListener("scroll", this.onScrollFallback);
541
+ return;
542
+ }
543
+ this.lenis = new import_lenis.default({
544
+ content: this.elements.wrapper,
545
+ wrapper: this.element,
546
+ ...this.scrollOptions
547
+ });
548
+ import_gsap.default.ticker.add((time) => {
549
+ this.lenis.raf(time * 1e3);
550
+ });
551
+ import_gsap.default.ticker.lagSmoothing(0);
552
+ this.lenis.on("scroll", (event) => {
553
+ import_ScrollTrigger.default.update();
554
+ this.onScroll(event.scroll);
555
+ this.emitter.emit("scroll", event);
556
+ });
557
+ }
558
+ destroyScroll() {
559
+ var _a;
560
+ if (!this.scrollEnabled) {
561
+ this.element.removeEventListener("scroll", this.onScrollFallback);
562
+ return;
563
+ }
564
+ (_a = this.lenis) == null ? void 0 : _a.destroy();
565
+ }
566
+ //
567
+ // Events.
568
+ //
569
+ onResize() {
570
+ var _a;
571
+ (_a = this.lenis) == null ? void 0 : _a.resize();
572
+ }
573
+ onRAF(time) {
574
+ var _a;
575
+ (_a = this.lenis) == null ? void 0 : _a.raf(time);
576
+ }
577
+ onScroll(scroll) {
578
+ this.scroll = scroll;
579
+ }
580
+ onScrollFallback() {
581
+ this.scroll = this.element.scrollTop;
582
+ this.emitter.emit("scroll", { scroll: this.scroll });
583
+ }
584
+ //
585
+ // Events.
586
+ //
587
+ addEventListeners() {
588
+ super.addEventListeners();
589
+ this.unsubscribeRaf = import_tempus.default.add(this.onRAF);
590
+ }
591
+ removeEventListeners() {
592
+ var _a;
593
+ super.removeEventListeners();
594
+ (_a = this.unsubscribeRaf) == null ? void 0 : _a.call(this);
595
+ this.unsubscribeRaf = void 0;
596
+ }
597
+ //
598
+ // Destroy.
599
+ //
600
+ destroy() {
601
+ super.destroy();
602
+ this.destroyResizeObserver();
603
+ this.destroyComponents();
604
+ this.destroyScroll();
605
+ }
606
+ };
607
+
608
+ // src/index.ts
609
+ var import_tempus2 = __toESM(require("tempus"), 1);
610
+ // Annotate the CommonJS export names for ESM import in node:
611
+ 0 && (module.exports = {
612
+ Application,
613
+ ApplicationManager,
614
+ Component,
615
+ EventEmitter,
616
+ Link,
617
+ Links,
618
+ Page,
619
+ Tempus
620
+ });