From 1c1ad9e5eb25921b738695ada997c7d62f8681f1 Mon Sep 17 00:00:00 2001 From: BoBoooooo <17746714@qq.com> Date: Mon, 15 Apr 2024 14:22:34 +0800 Subject: [PATCH] feat: support traverse native dom (#132) * Create sync.yml * Update sync.yml * feat: support nativeDom * ci: remove sync.yml * fix: update test case * fix: update isValidComponentName logic --- apps/playground/src/helpers/mock-files.ts | 89 ++++++++++++++++------- apps/playground/src/helpers/prototypes.ts | 74 +++++++++++++++++++ packages/core/src/helpers/string.ts | 9 ++- packages/core/tests/helpers.test.ts | 4 + 4 files changed, 148 insertions(+), 28 deletions(-) diff --git a/apps/playground/src/helpers/mock-files.ts b/apps/playground/src/helpers/mock-files.ts index 5ed38c05..24d5d445 100644 --- a/apps/playground/src/helpers/mock-files.ts +++ b/apps/playground/src/helpers/mock-files.ts @@ -173,6 +173,60 @@ class App extends React.Component { + + + + + Username: + + + + Password: + + + + Role: + + Admin + User + + + + + Submit + + + + + ); } @@ -227,38 +281,21 @@ const storeApp = ` import { defineStore } from '@music163/tango-boot'; export default defineStore({ - title: 'Page Title', array: [1, 2, 3], test: function() { console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); - console.log('test'); + }, + + submitForm: function(event) { + event.preventDefault(); + const formData = new FormData(event.target); + const username = formData.get("username"); + const password = formData.get("password"); + const role = formData.get("role"); + console.log("Submitted Data:", { username, password, role }); } }, 'app'); `; diff --git a/apps/playground/src/helpers/prototypes.ts b/apps/playground/src/helpers/prototypes.ts index da8e93f6..63e39515 100644 --- a/apps/playground/src/helpers/prototypes.ts +++ b/apps/playground/src/helpers/prototypes.ts @@ -85,8 +85,82 @@ basePrototypes['Section'].siblingNames = [ 'Section', ]; +export const nativeDomPrototypes = () => { + const doms = [ + 'div', + 'span', + 'h1', + 'h2', + 'p', + 'a', + 'img', + 'ul', + 'ol', + 'li', + 'input', + 'button', + 'form', + 'table', + 'tr', + 'td', + 'header', + 'footer', + 'nav', + 'section', + 'article', + 'aside', + 'main', + 'video', + 'audio', + 'label', + 'select', + 'option', + 'textarea', + 'iframe', + ]; + const componentPrototypes: Dict = doms.reduce( + (acc: Dict, tag) => { + acc[tag] = { + name: tag, + title: tag, + hasChildren: true, + package: '', + type: 'element', + props: [ + { + name: 'style', + title: '样式', + group: 'style', + setter: 'expressionSetter', + }, + { + name: 'className', + title: '类名', + setter: 'textSetter', + }, + { + name: 'id', + title: 'ID', + setter: 'textSetter', + }, + { + name: 'onClick', + title: '点击事件', + setter: 'actionSetter', + group: 'event', + }, + ], + }; + return acc; + }, + {}, + ); + return componentPrototypes; +}; + // iconfont: https://www.iconfont.cn/manage/index?manage_type=myprojects&projectId=2891794 const prototypes: Dict = { + ...nativeDomPrototypes(), ...(basePrototypes as any), SnippetSuccessResult, Snippet2ColumnLayout, diff --git a/packages/core/src/helpers/string.ts b/packages/core/src/helpers/string.ts index eedaa198..392e3d0e 100644 --- a/packages/core/src/helpers/string.ts +++ b/packages/core/src/helpers/string.ts @@ -75,15 +75,20 @@ export function inferFileType(filename: string): FileType { /** * 判断组件名是否合法 * @example Button -> valid - * @example div -> invalid + * @example isStrict ? div -> invalid : div -> valid * @param name + * @param isStrict 是否严格模式(严格模式不匹配原生标签) * @returns */ -export function isValidComponentName(name: string) { +export function isValidComponentName(name: string, isStrict = false) { if (!name) { return false; } + if (!isStrict) { + return true; + } + const firstChar = name.charAt(0); return firstChar === firstChar.toUpperCase(); } diff --git a/packages/core/tests/helpers.test.ts b/packages/core/tests/helpers.test.ts index f9c90fdf..9956188c 100644 --- a/packages/core/tests/helpers.test.ts +++ b/packages/core/tests/helpers.test.ts @@ -161,6 +161,10 @@ describe('string helpers', () => { it('isValidComponentName', () => { expect(isValidComponentName('Button')).toBeTruthy(); expect(isValidComponentName('Button.Group')).toBeTruthy(); + expect(isValidComponentName('div')).toBeTruthy(); + expect(isValidComponentName('')).toBeFalsy(); + expect(isValidComponentName(null)).toBeFalsy(); + expect(isValidComponentName(undefined)).toBeFalsy(); }); it('inferFileType', () => {