-
Notifications
You must be signed in to change notification settings - Fork 56
/
selector.ts
174 lines (156 loc) · 4.49 KB
/
selector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
MultiplatformSelector,
MultiplatformTransform,
updateWasmToMatches,
} from '@gkd-kit/selector';
import type { RawNode } from './types';
import matchesInstantiate from '@gkd-kit/wasm_matches';
import matchesWasmUrl from '@gkd-kit/wasm_matches/dist/mod.wasm?url';
import store from './store';
import { settingsStorage } from './storage';
export const wasmLoadTask = matchesInstantiate(fetch(matchesWasmUrl))
.then((mod) => {
const toMatches = mod.exports.toMatches;
updateWasmToMatches(toMatches as any);
store.wasmSupported = true;
if (import.meta.env.PROD) {
console.log('use wasm matches');
}
})
.catch((e) => {
store.wasmSupported = false;
console.error(e);
if (import.meta.env.PROD) {
console.log('use js matches');
}
});
const transform = new MultiplatformTransform<RawNode>(
(node, name) => {
const [key, subKey] = name.split('.');
if (subKey) {
// @ts-ignore
return node.attr[key]?.[subKey];
}
// @ts-ignore
return node.attr[key];
},
(node) => node.attr.name,
(node) => node.children,
(node) => node.parent,
);
export type Selector = {
tracks: boolean[];
trackIndex: number;
connectKeys: string[];
canQf: boolean;
qfIdValue: string | null | undefined;
qfVidValue: string | null | undefined;
qfTextValue: string | null | undefined;
canCopy: boolean;
toString: () => string;
match: (node: RawNode) => RawNode | undefined;
querySelectorAll: (node: RawNode) => RawNode[];
querySelectorTrackAll: (node: RawNode) => RawNode[][];
};
export type ConnectKeyType = '+' | '-' | '>' | '<' | '<<';
export const parseSelector = (source: string): Selector => {
const ms = MultiplatformSelector.Companion.parse(source);
for (const [name, operator, type] of ms.binaryExpressions) {
if (operator == '~=' && !store.wasmSupported) {
if (!settingsStorage.ignoreWasmWarn) {
store.wasmErrorDlgVisible = true;
}
}
if (!allowPropertyTypes[name]) {
throw `未知属性: ${name}`;
}
if (
type != PrimitiveValue.NullValue.type &&
allowPropertyTypes[name] != type
) {
throw `非法类型: ${name}`;
}
}
const selector: Selector = {
tracks: ms.tracks,
trackIndex: ms.trackIndex,
connectKeys: ms.connectKeys,
canQf: ms.canQf,
qfIdValue: ms.qfIdValue,
qfVidValue: ms.qfVidValue,
qfTextValue: ms.qfTextValue,
canCopy: ms.propertyNames.every((name) => allowPropertyNames.has(name)),
toString: () => ms.toString(),
match: (node) => {
return ms.match(node, transform) ?? void 0;
},
querySelectorAll: (node) => {
return transform.querySelectorAll(node, ms);
},
querySelectorTrackAll: (node) => {
return transform.querySelectorTrackAll(node, ms);
},
};
return selector;
};
export const checkSelector = (source: string) => {
return MultiplatformSelector.Companion.parseOrNull(source) != null;
};
const allowPropertyNames = new Set([
'id',
'vid',
'name',
'text',
'text.length',
'desc',
'desc.length',
'clickable',
'focusable',
'checkable',
'checked',
'editable',
'longClickable',
'visibleToUser',
'left',
'top',
'right',
'bottom',
'width',
'height',
'index',
'depth',
'childCount',
]);
const PrimitiveValue = {
StringValue: { type: 'string' },
IntValue: { type: 'int' },
BooleanValue: { type: 'boolean' },
NullValue: { type: 'null' },
};
const allowPropertyTypes: Record<string, string> = {
id: PrimitiveValue.StringValue.type,
vid: PrimitiveValue.StringValue.type,
name: PrimitiveValue.StringValue.type,
text: PrimitiveValue.StringValue.type,
'text.length': PrimitiveValue.IntValue.type,
desc: PrimitiveValue.StringValue.type,
'desc.length': PrimitiveValue.IntValue.type,
clickable: PrimitiveValue.BooleanValue.type,
focusable: PrimitiveValue.BooleanValue.type,
checkable: PrimitiveValue.BooleanValue.type,
checked: PrimitiveValue.BooleanValue.type,
editable: PrimitiveValue.BooleanValue.type,
longClickable: PrimitiveValue.BooleanValue.type,
visibleToUser: PrimitiveValue.BooleanValue.type,
left: PrimitiveValue.IntValue.type,
top: PrimitiveValue.IntValue.type,
right: PrimitiveValue.IntValue.type,
bottom: PrimitiveValue.IntValue.type,
width: PrimitiveValue.IntValue.type,
height: PrimitiveValue.IntValue.type,
index: PrimitiveValue.IntValue.type,
depth: PrimitiveValue.IntValue.type,
childCount: PrimitiveValue.IntValue.type,
_id: PrimitiveValue.IntValue.type,
_pid: PrimitiveValue.IntValue.type,
};