Skip to content

Commit

Permalink
feat(element): element events types
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed May 8, 2023
1 parent 299d0a1 commit 83774fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 16 additions & 1 deletion scripts/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export default async function buildTypes() {
elapsed.start('types');
let coreEventsReact = '';
let coreEventsVue = '';
let coreEventsElement = '';
let modulesEventsReact = '';
let modulesEventsVue = '';
let modulesEventsElement = '';

const replaceInstances = (content) => {
return content
Expand All @@ -29,6 +31,14 @@ export default async function buildTypes() {
coreEventsContent = coreEventsContent
.split('// CORE_EVENTS_START')[1]
.split('// CORE_EVENTS_END')[0];
coreEventsElement = replaceInstances(
coreEventsContent.replace(/ ([a-zA-Z_?]*): ([^;]*);/g, (string, name) => {
if (name.includes('_')) {
return '';
}
return ` ${name.toLowerCase()}: CustomEvent;`;
}),
);
coreEventsReact = replaceInstances(
coreEventsContent.replace(/ ([a-zA-Z]*): \(/g, (string, name) => {
return ` on${name[0].toUpperCase()}${name.substr(1)}?: (`;
Expand All @@ -50,6 +60,11 @@ export default async function buildTypes() {
let eventsContent = await fs.readFile(eventsFile, 'utf-8');
eventsContent = eventsContent.split('Events {')[1].split('}')[0].trim();
if (eventsContent.length) {
modulesEventsElement += replaceInstances(
eventsContent.replace(/ ([a-zA-Z]*): ([^;]*);/g, (string, name) => {
return ` ${name.toLowerCase()}: CustomEvent;`;
}),
);
modulesEventsReact += replaceInstances(
eventsContent.replace(/ ([a-zA-Z]*): \(/g, (string, name) => {
return ` on${name[0].toUpperCase()}${name.substr(1)}?: (`;
Expand Down Expand Up @@ -86,7 +101,7 @@ export default async function buildTypes() {
return fs.writeFile(destPath, content);
};
if (file.includes('swiper-element.d.ts')) {
return processTypingFile('', '');
return processTypingFile(coreEventsElement, modulesEventsElement);
}
if (file.includes('swiper-react.d.ts')) {
return processTypingFile(coreEventsReact, modulesEventsReact);
Expand Down
18 changes: 10 additions & 8 deletions src/element/swiper-element.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { SwiperOptions } from '../types/swiper-options';
import Swiper from '../types/swiper-class';
import { SwiperOptions, Swiper } from '../types/';

declare const register: (injectStyles?: boolean) => void;

// prettier-ignore
interface SwiperContainerEventMap extends HTMLElementEventMap {
slidechange: Event;
// CORE_EVENTS

// MODULES_EVENTS
}

interface SwiperContainer extends HTMLElement {}
interface SwiperContainer extends SwiperOptions {
swiper?: Swiper;
initialize?: () => void;
injectStyles?: string[];
injectStylesUrls?: string[];
eventsPrefix?: string;
swiper: Swiper;
initialize: () => void;
injectStyles: string[];
injectStylesUrls: string[];
eventsPrefix: string;
addEventListener<K extends keyof SwiperContainerEventMap>(
type: K,
listener: (this: SwiperContainer, ev: SwiperContainerEventMap[K]) => any,
Expand Down

0 comments on commit 83774fa

Please sign in to comment.