@htmlplus/element 0.6.9 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
1
  import { PlusElement } from '../../types';
2
- export declare function Element(tag?: string): (constructor: PlusElement) => void;
2
+ export declare function Element(): (constructor: PlusElement) => void;
@@ -1,10 +1,11 @@
1
1
  import { camelCase, paramCase } from 'change-case';
2
2
  import * as CONSTANTS from '../../constants/index.js';
3
3
  import { call, fromAttribute, getConfig, getMembers, getTag, isServer, request } from '../utils/index.js';
4
- export function Element(tag) {
4
+ export function Element() {
5
5
  return function (constructor) {
6
6
  if (isServer())
7
7
  return;
8
+ const tag = getTag(constructor);
8
9
  if (customElements.get(tag))
9
10
  return;
10
11
  const members = getMembers(constructor);
@@ -13,8 +13,8 @@ export function Property(options) {
13
13
  if (next === previous)
14
14
  return;
15
15
  this[symbol] = next;
16
- request(this, name, previous, (skip) => {
17
- if (!(options === null || options === void 0 ? void 0 : options.reflect) || skip)
16
+ request(this, name, previous, (skipped) => {
17
+ if (!(options === null || options === void 0 ? void 0 : options.reflect) || skipped)
18
18
  return;
19
19
  target[CONSTANTS.API_LOCKED] = true;
20
20
  updateAttribute(host(this), name, next);
@@ -6,4 +6,4 @@ import { PlusElement } from '../../types';
6
6
  * @param previous The previous value of Property/State.
7
7
  * @param callback Invoked when the rendering phase is completed.
8
8
  */
9
- export declare const request: (target: PlusElement, name?: string, previous?: any, callback?: Function) => void;
9
+ export declare const request: (target: PlusElement, name?: string, previous?: any, callback?: ((skipped: boolean) => void) | undefined) => void;
@@ -1,6 +1,7 @@
1
1
  import { Plugin } from '../../types';
2
2
  export declare const CUSTOM_ELEMENT_OPTIONS: Partial<CustomElementOptions>;
3
3
  export interface CustomElementOptions {
4
+ prefix?: string;
4
5
  typings?: boolean;
5
6
  }
6
7
  export declare const customElement: (options?: CustomElementOptions) => Plugin;
@@ -3,7 +3,7 @@ import { pascalCase } from 'change-case';
3
3
  import * as CONSTANTS from '../../constants/index.js';
4
4
  import { addDependency, getType, print, visitor } from '../utils/index.js';
5
5
  export const CUSTOM_ELEMENT_OPTIONS = {
6
- // prefix: undefined,
6
+ prefix: undefined,
7
7
  typings: true
8
8
  };
9
9
  // TODO: support {variable && jsxElement}
@@ -12,13 +12,16 @@ export const customElement = (options) => {
12
12
  options = Object.assign({}, CUSTOM_ELEMENT_OPTIONS, options);
13
13
  const run = (context) => {
14
14
  const ast = t.cloneNode(context.fileAST, true);
15
+ // TODO
16
+ const tag = ((options === null || options === void 0 ? void 0 : options.prefix) || '') + context.componentKey;
17
+ const componentInterfaceName = `HTML${pascalCase(tag)}Element`;
15
18
  // attaches name
16
19
  visitor(ast, {
17
20
  ClassDeclaration(path) {
18
21
  const { body, id } = path.node;
19
22
  if (id.name != context.className)
20
23
  return;
21
- const node = t.classProperty(t.identifier(CONSTANTS.STATIC_TAG), t.stringLiteral(context.componentTag), undefined, undefined, undefined, true);
24
+ const node = t.classProperty(t.identifier(CONSTANTS.STATIC_TAG), t.stringLiteral(tag), undefined, undefined, undefined, true);
22
25
  t.addComment(node, 'leading', CONSTANTS.COMMENT_AUTO_ADDED_PROPERTY, true);
23
26
  body.body.unshift(node);
24
27
  }
@@ -51,6 +54,15 @@ export const customElement = (options) => {
51
54
  path.replaceWith(t.jsxAttribute(t.jsxIdentifier('class'), value));
52
55
  }
53
56
  });
57
+ // replaces 'tabIndex' attribute with 'tabindex'
58
+ visitor(ast, {
59
+ JSXAttribute(path) {
60
+ const { name, value } = path.node;
61
+ if (name.name != 'tabIndex')
62
+ return;
63
+ path.replaceWith(t.jsxAttribute(t.jsxIdentifier('tabindex'), value));
64
+ }
65
+ });
54
66
  // converts 'jsx' to 'uhtml' syntax
55
67
  visitor(ast, {
56
68
  JSXAttribute: {
@@ -220,7 +232,7 @@ export const customElement = (options) => {
220
232
  })
221
233
  ]))));
222
234
  body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
223
- t.tsInterfaceDeclaration(t.identifier(context.componentInterfaceName), null, [
235
+ t.tsInterfaceDeclaration(t.identifier(componentInterfaceName), null, [
224
236
  t.tSExpressionWithTypeArguments(t.identifier('HTMLElement')) // TODO
225
237
  ], t.tsInterfaceBody([
226
238
  ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
@@ -229,19 +241,19 @@ export const customElement = (options) => {
229
241
  }))
230
242
  ])),
231
243
  t.variableDeclaration('var', [
232
- t.variableDeclarator(Object.assign(t.identifier(context.componentInterfaceName), {
244
+ t.variableDeclarator(Object.assign(t.identifier(componentInterfaceName), {
233
245
  typeAnnotation: t.tSTypeAnnotation(t.tSTypeLiteral([
234
- t.tSPropertySignature(t.identifier('prototype'), t.tsTypeAnnotation(t.tSTypeReference(t.identifier(context.componentInterfaceName)))),
235
- t.tSConstructSignatureDeclaration(null, [], t.tSTypeAnnotation(t.tSTypeReference(t.identifier(context.componentInterfaceName))))
246
+ t.tSPropertySignature(t.identifier('prototype'), t.tsTypeAnnotation(t.tSTypeReference(t.identifier(componentInterfaceName)))),
247
+ t.tSConstructSignatureDeclaration(null, [], t.tSTypeAnnotation(t.tSTypeReference(t.identifier(componentInterfaceName))))
236
248
  ]))
237
249
  }))
238
250
  ]),
239
251
  t.tsInterfaceDeclaration(t.identifier('HTMLElementTagNameMap'), null, [], t.tsInterfaceBody([
240
- t.tSPropertySignature(t.stringLiteral(context.componentTag), t.tSTypeAnnotation(t.tSIntersectionType([t.tSTypeReference(t.identifier(context.componentInterfaceName))])))
252
+ t.tSPropertySignature(t.stringLiteral(tag), t.tSTypeAnnotation(t.tSIntersectionType([t.tSTypeReference(t.identifier(componentInterfaceName))])))
241
253
  ])),
242
254
  t.tSModuleDeclaration(t.identifier('JSX'), t.tSModuleBlock([
243
255
  t.tSInterfaceDeclaration(t.identifier('IntrinsicElements'), undefined, undefined, t.tSInterfaceBody([
244
- t.tSPropertySignature(t.stringLiteral(context.componentTag), t.tSTypeAnnotation(t.tSIntersectionType([
256
+ t.tSPropertySignature(t.stringLiteral(tag), t.tSTypeAnnotation(t.tSIntersectionType([
245
257
  t.tSTypeReference(t.identifier(context.className + 'JSX')),
246
258
  t.tSTypeLiteral([
247
259
  t.tSIndexSignature([
@@ -257,6 +269,7 @@ export const customElement = (options) => {
257
269
  declare: true,
258
270
  global: true
259
271
  }));
272
+ body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${context.className}Element`), undefined, t.tsTypeReference(t.tsQualifiedName(t.identifier('globalThis'), t.identifier(componentInterfaceName))))));
260
273
  }
261
274
  });
262
275
  }
@@ -1,8 +1,8 @@
1
1
  /**************************************************
2
2
  * THIS FILE IS AUTO-GENERATED, DO NOT EDIT MANUALY
3
3
  **************************************************/
4
- import '{{importerComponent.source}}';
5
- import type { {{importerComponentType.imported}} as {{importerComponentType.local}} } from '{{importerComponentType.source}}';
4
+ import { {{className}} as {{className}}Core } from '{{importerComponent.source}}';
5
+ import type { {{className}}Element, {{importerComponentType.imported}} as {{importerComponentType.local}} } from '{{importerComponentType.source}}';
6
6
 
7
7
  import { proxy } from '../proxy';
8
8
  import type { Rename } from '../proxy';
@@ -16,8 +16,8 @@ type Renamed = Rename<
16
16
  }
17
17
  >;
18
18
 
19
- export const {{className}} = proxy<{{componentInterfaceName}}, Renamed>(
20
- '{{componentTag}}',
19
+ export const {{className}} = proxy<{{className}}Element, Renamed>(
20
+ {{className}}Core.TAG,
21
21
  [{{#each classProperties}}'{{key.name}}', {{/each}}],
22
22
  [{{#each classEvents}}'{{key.name}}', {{/each}}],
23
23
  );
@@ -1,6 +1,2 @@
1
1
  import { Plugin } from '../../types';
2
- export declare const EXTRACT_OPTIONS: Partial<ExtractOptions>;
3
- export interface ExtractOptions {
4
- prefix?: string;
5
- }
6
- export declare const extract: (options?: ExtractOptions) => Plugin;
2
+ export declare const extract: () => Plugin;
@@ -1,12 +1,10 @@
1
1
  import * as t from '@babel/types';
2
- import { pascalCase, paramCase } from 'change-case';
2
+ import { paramCase } from 'change-case';
3
3
  import path from 'path';
4
4
  import * as CONSTANTS from '../../constants/index.js';
5
5
  import { hasDecorator, visitor } from '../utils/index.js';
6
- export const EXTRACT_OPTIONS = {};
7
- export const extract = (options) => {
6
+ export const extract = () => {
8
7
  const name = 'extract';
9
- options = Object.assign({}, EXTRACT_OPTIONS, options);
10
8
  const run = (context) => {
11
9
  var _a, _b;
12
10
  visitor(context.fileAST, {
@@ -22,26 +20,6 @@ export const extract = (options) => {
22
20
  path.skip();
23
21
  }
24
22
  },
25
- Decorator(path) {
26
- var _a;
27
- const name = (_a = path.node.expression.callee) === null || _a === void 0 ? void 0 : _a.name;
28
- // TODO
29
- if (CONSTANTS.DECORATOR_ELEMENT == name) {
30
- const [argument] = path.node.expression.arguments;
31
- if (argument) {
32
- context.componentTag = argument === null || argument === void 0 ? void 0 : argument.value;
33
- return;
34
- }
35
- context.componentTag = paramCase(path.parent.id.name);
36
- if (options === null || options === void 0 ? void 0 : options.prefix)
37
- context.componentTag = options.prefix + '-' + context.componentTag;
38
- path.replaceWith(t.decorator(t.callExpression(t.identifier(name), [
39
- t.stringLiteral(context.componentTag),
40
- ...path.node.expression.arguments.slice(1)
41
- ])));
42
- return;
43
- }
44
- },
45
23
  JSXElement(path) {
46
24
  var _a;
47
25
  const { openingElement } = path.node;
@@ -61,8 +39,6 @@ export const extract = (options) => {
61
39
  context.fileName = path.basename(context.filePath, context.fileExtension);
62
40
  context.className = (_b = (_a = context.class) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.name;
63
41
  context.componentKey = paramCase(context.className);
64
- // TODO
65
- context.componentInterfaceName = `HTML${pascalCase(context.componentTag)}Element`;
66
42
  context.classEvents = (context.classMembers || []).filter((member) => hasDecorator(member, CONSTANTS.DECORATOR_EVENT));
67
43
  context.classMethods = (context.classMembers || []).filter((member) => hasDecorator(member, CONSTANTS.DECORATOR_METHOD));
68
44
  context.classProperties = (context.classMembers || []).filter((member) => hasDecorator(member, CONSTANTS.DECORATOR_PROPERTY));
@@ -9,7 +9,7 @@ export const visualStudioCode = (options) => {
9
9
  const finish = (global) => {
10
10
  var _a, _b, _c, _d, _e;
11
11
  const contexts = global.contexts.sort((a, b) => {
12
- return a.componentTag.toUpperCase() > b.componentTag.toUpperCase() ? +1 : -1;
12
+ return a.componentKey.toUpperCase() > b.componentKey.toUpperCase() ? +1 : -1;
13
13
  });
14
14
  const json = {
15
15
  $schema: 'TODO',
@@ -19,7 +19,7 @@ export const visualStudioCode = (options) => {
19
19
  for (const context of contexts) {
20
20
  const description = (_a = getTags(context.class).find((tag) => !tag.key)) === null || _a === void 0 ? void 0 : _a.value;
21
21
  const tag = {
22
- name: context.componentTag,
22
+ name: context.componentKey,
23
23
  description: description,
24
24
  attributes: [],
25
25
  references: [
@@ -9,7 +9,7 @@ export const webTypes = (options) => {
9
9
  const finish = (global) => {
10
10
  var _a, _b, _c, _d, _e, _f, _g;
11
11
  const contexts = global.contexts.sort((a, b) => {
12
- return a.componentTag.toUpperCase() > b.componentTag.toUpperCase() ? +1 : -1;
12
+ return a.componentKey.toUpperCase() > b.componentKey.toUpperCase() ? +1 : -1;
13
13
  });
14
14
  const json = {
15
15
  '$schema': 'http://json.schemastore.org/web-types',
@@ -70,7 +70,7 @@ export const webTypes = (options) => {
70
70
  };
71
71
  });
72
72
  const component = {
73
- 'name': context.componentTag,
73
+ 'name': context.componentKey,
74
74
  'description': description,
75
75
  'doc-url': (_f = options.reference) === null || _f === void 0 ? void 0 : _f.call(options, context),
76
76
  'deprecated': hasTag(context.class, 'deprecated'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.6.9",
3
+ "version": "0.7.1",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
@@ -14,9 +14,7 @@ export interface Context {
14
14
  classProperties?: Array<ClassProperty>;
15
15
  classRender?: ClassMethod;
16
16
  classStates?: Array<ClassProperty>;
17
- componentInterfaceName?: string;
18
17
  componentKey?: string;
19
- componentTag?: string;
20
18
  directoryName?: string;
21
19
  directoryPath?: string;
22
20
  fileAST?: File;