Skip to content

Commit

Permalink
perf: 校验属性及其类型
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Mar 3, 2024
1 parent 486bcfb commit cfbed0a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"dependencies": {
"@antv/g6": "^4.8.24",
"@gkd-kit/selector": "0.0.20",
"@gkd-kit/selector": "0.0.21",
"@rushstack/eslint-patch": "^1.7.2",
"@tsconfig/node20": "^20.1.2",
"@types/file-saver": "^2.0.7",
Expand Down
13 changes: 9 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/components/SearchCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ const searchBySelector = errorTry(() => {
if (!text) return;
if (enableSearchBySelector.value) {
const selector = errorWrap(() => parseSelector(text), `选择器非法`);
const selector = errorWrap(
() => parseSelector(text),
(e) => {
if (typeof e == 'string') {
return e;
}
return `非法选择器`;
},
);
if (
selectorResults.find(
(s) =>
Expand Down
82 changes: 66 additions & 16 deletions src/utils/selector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { CommonSelector, CommonTransform } from '@gkd-kit/selector';
import {
MultiplatformSelector,
MultiplatformTransform,
} from '@gkd-kit/selector';
import type { RawNode } from './types';

const transform = new CommonTransform<RawNode>(
const transform = new MultiplatformTransform<RawNode>(
(node, name) => {
const [key, subKey] = name.split('.');
if (subKey) {
Expand Down Expand Up @@ -32,32 +35,40 @@ export type Selector = {
};

export const parseSelector = (source: string): Selector => {
const cs = CommonSelector.Companion.parse(source);
const ms = MultiplatformSelector.Companion.parse(source);
for (const [name, type] of ms.nameToTypeList) {
if (!allowPropertyTypes[name]) {
throw `未知属性: ${name}`;
}
if (type != 'null' && allowPropertyTypes[name] != type) {
throw `非法类型: ${name}`;
}
}
const selector: Selector = {
tracks: cs.tracks,
trackIndex: cs.trackIndex,
connectKeys: cs.connectKeys,
canQf: cs.canQf,
qfIdValue: cs.qfIdValue,
qfVidValue: cs.qfVidValue,
qfTextValue: cs.qfTextValue,
canCopy: cs.propertyNames.every((name) => allowPropertyNames.has(name)),
toString: () => cs.toString(),
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 cs.match(node, transform) ?? void 0;
return ms.match(node, transform) ?? void 0;
},
querySelectorAll: (node) => {
return transform.querySelectorAll(node, cs);
return transform.querySelectorAll(node, ms);
},
querySelectorTrackAll: (node) => {
return transform.querySelectorTrackAll(node, cs);
return transform.querySelectorTrackAll(node, ms);
},
};
return selector;
};

export const checkSelector = (source: string) => {
return CommonSelector.Companion.parseOrNull(source) != null;
return MultiplatformSelector.Companion.parseOrNull(source) != null;
};

const allowPropertyNames = new Set([
Expand Down Expand Up @@ -89,3 +100,42 @@ const allowPropertyNames = new Set([
'depth',
'childCount',
]);

const PrimitiveValue = {
StringValue: { type: 'string' },
IntValue: { type: 'int' },
BooleanValue: { type: 'boolean' },
};

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,
};

0 comments on commit cfbed0a

Please sign in to comment.