Skip to content

Commit d5dc1f9

Browse files
committed
feat(multi selector): implement multi selector support
1 parent 194a4e7 commit d5dc1f9

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"raw-loader": "^4.0.0",
6262
"shelljs": "^0.8.3",
6363
"tmp": "^0.1.0",
64-
"typescript": "^4.0.2",
64+
"typescript": "^4.1.3",
6565
"uglifyjs-webpack-plugin": "^2.2.0",
6666
"webpack": "^4.41.5",
6767
"webpack-cli": "^3.3.10"

src_types/pages/$w.d.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
// the first part of this file is being generated by => scripts/selector-declaration-builder.js
22
// Run `npm run generate-dts` to generate it
3-
declare type IntersectionArrayAndBase<T, P> = {
3+
type IntersectionArrayAndBase<T, P> = {
44
[K in keyof T]: K extends P ? T[K] : T[K] & T[K][];
55
};
6+
type CommaChar = ","
7+
type LegalChars = CommaChar | " " | "\n" | "\t"
8+
type WixElements = PageElementsMap & IntersectionArrayAndBase<TypeNameToSdkType, 'Document'>;
9+
type NicknameSelector = keyof PageElementsMap
10+
type TypeSelector = keyof IntersectionArrayAndBase<TypeNameToSdkType, 'Document'>
611

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

15+
type OnlyVaildSelectorsChars<S> =
16+
S extends ""
17+
? any
18+
: S extends `${VaildSelectorsChars}${infer Tail}`
19+
? OnlyVaildSelectorsChars<Tail>
20+
: never
21+
22+
type HasComma<S> =
23+
S extends `${CommaChar}${infer Tail}`
24+
? unknown
25+
: S extends `${VaildSelectorsChars}${infer Tail2}`
26+
? HasCommaRec2<Tail2>
27+
: never
28+
type MultiSelector<S> = S & OnlyVaildSelectorsChars<S> & HasComma<S>
1029
/**
1130
* Selects and returns elements from a page.
1231
*/
13-
declare function $w<T extends keyof WixElementSelector, S extends string>(selector: T | S & IsWixElementSelector<S>):
14-
S extends keyof WixElementSelector
15-
? WixElementSelector[S]
32+
declare function $w<T extends WixElementSelector, S extends string>(selector: T | MultiSelector<S>):
33+
S extends keyof WixElements
34+
? WixElements[S]
1635
: any
1736
/**
1837
* The `$w` namespace contains everything you need in order to work
@@ -32,9 +51,9 @@ declare namespace $w {
3251
/**
3352
* Selects and returns elements from a page.
3453
*/
35-
type $w = <T extends keyof WixElementSelector, S extends string>(selector: T | S & IsWixElementSelector<S>) =>
36-
S extends keyof WixElementSelector
37-
? WixElementSelector[S]
54+
type $w = <T extends WixElementSelector, S extends string>(selector: T | MultiSelector<S>) =>
55+
S extends keyof WixElements
56+
? WixElements[S]
3857
: any
3958
}
4059

test/it/code-samples/positive/pages-$w-dynamic-nicknames/dynamic.js

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ $w.onReady(function() {
2121

2222
$w("#text1").text = lastClicked
2323
$w("#text2").text = previousPageURL
24+
$w("Button").hide()
25+
$w("Button").forEach(b => b.show())
26+
$w("Button, Text").anyProperty()
27+
$w("#button1, Text, Document").anyProperty2
2428

2529
});
2630

0 commit comments

Comments
 (0)