Skip to content

Commit

Permalink
feat(multi selector): implement multi selector support
Browse files Browse the repository at this point in the history
  • Loading branch information
nemni8 committed Feb 24, 2021
1 parent 194a4e7 commit 897986c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src_types/pages/$w.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
// the first part of this file is being generated by => scripts/selector-declaration-builder.js
// Run `npm run generate-dts` to generate it
declare type IntersectionArrayAndBase<T, P> = {
type IntersectionArrayAndBase<T, P> = {
[K in keyof T]: K extends P ? T[K] : T[K] & T[K][];
};
type CommaChar = ","
type LegalChars = CommaChar | " " | "\n" | "\t"
type WixElements = PageElementsMap & IntersectionArrayAndBase<TypeNameToSdkType, 'Document'>;
type NicknameSelector = keyof PageElementsMap
type TypeSelector = keyof IntersectionArrayAndBase<TypeNameToSdkType, 'Document'>

declare type IsWixElementSelector<S> = S extends keyof WixElementSelector ? WixElementSelector[S] : never;
declare type WixElementSelector = PageElementsMap & IntersectionArrayAndBase<TypeNameToSdkType, 'Document'>;
type WixElementSelector = NicknameSelector | TypeSelector
type VaildSelectorsChars = WixElementSelector | LegalChars

type OnlyVaildSelectorsChars<S> =
S extends ""
? any
: S extends `${VaildSelectorsChars}${infer Tail}`
? OnlyVaildSelectorsChars<Tail>
: never

type HasComma<S> =
S extends `${CommaChar}${infer Tail}`
? unknown
: S extends `${VaildSelectorsChars}${infer Tail2}`
? HasCommaRec2<Tail2>
: never
type MultiSelector<S> = S & OnlyVaildSelectorsChars<S> & HasComma<S>
/**
* Selects and returns elements from a page.
*/
declare function $w<T extends keyof WixElementSelector, S extends string>(selector: T | S & IsWixElementSelector<S>):
S extends keyof WixElementSelector
? WixElementSelector[S]
declare function $w<T extends WixElementSelector, S extends string>(selector: T | MultiSelector<S>):
S extends keyof WixElements
? WixElements[S]
: any
/**
* The `$w` namespace contains everything you need in order to work
Expand All @@ -32,9 +51,9 @@ declare namespace $w {
/**
* Selects and returns elements from a page.
*/
type $w = <T extends keyof WixElementSelector, S extends string>(selector: T | S & IsWixElementSelector<S>) =>
S extends keyof WixElementSelector
? WixElementSelector[S]
type $w = <T extends keyof WixElements, S extends string>(selector: T | MultiSelector<S>) =>
S extends keyof WixElements
? WixElements[S]
: any
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ $w.onReady(function() {

$w("#text1").text = lastClicked
$w("#text2").text = previousPageURL
$w("Button").hide()
$w("Button").forEach(b => b.show())
$w("Button, Text").anyProperty()
$w("#button1, Text, Document").anyProperty2

});

0 comments on commit 897986c

Please sign in to comment.