lapikit 0.0.0-insiders.f8dce9a → 0.0.0-insiders.fac243e

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.
@@ -15,7 +15,7 @@
15
15
  left: 0;
16
16
  height: 100%;
17
17
  width: 100%;
18
- background-color: color-mix(in oklab, black 70%, transparent);
18
+ background-color: color-mix(in oklab, black 75%, transparent);
19
19
  z-index: 9000;
20
20
  cursor: default;
21
21
  }
@@ -1,4 +1,4 @@
1
1
  import type { TextfieldProps } from './types.js';
2
- declare const Textfield: import("svelte").Component<TextfieldProps, {}, "value" | "ref">;
2
+ declare const Textfield: import("svelte").Component<TextfieldProps, {}, "ref" | "value">;
3
3
  type Textfield = ReturnType<typeof Textfield>;
4
4
  export default Textfield;
@@ -1,4 +1,7 @@
1
- declare function lapikitPreprocess(): {
1
+ type LapikitPreprocessOptions = {
2
+ plugins?: string[];
3
+ };
4
+ declare function lapikitPreprocess(options?: LapikitPreprocessOptions): {
2
5
  markup({ content }: {
3
6
  content: string;
4
7
  filename?: string;
@@ -2,23 +2,58 @@ const components = ['btn'];
2
2
  function componentName(shortName) {
3
3
  return 'Kit' + shortName.charAt(0).toUpperCase() + shortName.slice(1);
4
4
  }
5
- function lapikitPreprocess() {
5
+ // plugins lapikit
6
+ const lapikitPlugins = {
7
+ repl: {
8
+ components: ['repl'],
9
+ ref: '@lapikit/repl'
10
+ }
11
+ };
12
+ function lapikitPreprocess(options) {
6
13
  return {
7
14
  markup({ content }) {
8
- const hasComponent = components.some((comp) => content.includes(`<lpk:${comp}`));
15
+ const allComponents = [...components];
16
+ const componentToRef = new Map();
17
+ components.forEach((comp) => {
18
+ componentToRef.set(comp, 'lapikit/labs/components');
19
+ });
20
+ // plugins
21
+ if (options?.plugins) {
22
+ options.plugins.forEach((pluginKey) => {
23
+ const plugin = lapikitPlugins[pluginKey];
24
+ if (plugin) {
25
+ plugin.components.forEach((comp) => {
26
+ if (!allComponents.includes(comp)) {
27
+ allComponents.push(comp);
28
+ }
29
+ componentToRef.set(comp, plugin.ref);
30
+ });
31
+ }
32
+ });
33
+ }
34
+ const hasComponent = allComponents.some((comp) => content.includes(`<kit:${comp}`));
9
35
  if (!hasComponent)
10
36
  return;
11
37
  let processedContent = content;
12
- const importedComponents = new Set();
38
+ const importedComponents = new Map();
13
39
  let hasChanges = true;
14
40
  while (hasChanges) {
15
41
  hasChanges = false;
16
- for (const shortName of components) {
42
+ for (const shortName of allComponents) {
17
43
  const componentNameStr = componentName(shortName);
18
- const regex = new RegExp(`<lpk:${shortName}([\\s\\S]*?)>([\\s\\S]*?)<\\/lpk:${shortName}\\s*>`, 'g');
19
- const newContent = processedContent.replace(regex, (fullMatch, attrs, children) => {
44
+ const attrPattern = `(?:[^>"']|"[^"]*"|'[^']*')*?`;
45
+ const selfClosingRegex = new RegExp(`<kit:${shortName}(${attrPattern})\\s*/>`, 'g');
46
+ const pairRegex = new RegExp(`<kit:${shortName}(${attrPattern})>([\\s\\S]*?)<\\/kit:${shortName}\\s*>`, 'g');
47
+ let newContent = processedContent.replace(selfClosingRegex, (fullMatch, attrs) => {
48
+ hasChanges = true;
49
+ const ref = componentToRef.get(shortName) || 'lapikit/labs/components';
50
+ importedComponents.set(componentNameStr, ref);
51
+ return `<${componentNameStr}${attrs} />`;
52
+ });
53
+ newContent = newContent.replace(pairRegex, (fullMatch, attrs, children) => {
20
54
  hasChanges = true;
21
- importedComponents.add(componentNameStr);
55
+ const ref = componentToRef.get(shortName) || 'lapikit/labs/components';
56
+ importedComponents.set(componentNameStr, ref);
22
57
  return `<${componentNameStr}${attrs}>${children}</${componentNameStr}>`;
23
58
  });
24
59
  if (newContent !== processedContent) {
@@ -29,15 +64,25 @@ function lapikitPreprocess() {
29
64
  if (processedContent === content)
30
65
  return;
31
66
  if (importedComponents.size > 0) {
32
- const imports = Array.from(importedComponents).join(', ');
67
+ const importsByRef = new Map();
68
+ importedComponents.forEach((ref, component) => {
69
+ if (!importsByRef.has(ref)) {
70
+ importsByRef.set(ref, []);
71
+ }
72
+ importsByRef.get(ref).push(component);
73
+ });
74
+ const importLines = Array.from(importsByRef.entries())
75
+ .map(([ref, components]) => {
76
+ const imports = components.join(', ');
77
+ return `\n\timport { ${imports} } from '${ref}';`;
78
+ })
79
+ .join('');
33
80
  const scriptRegex = /<script(?![^>]*\bmodule\b)([^>]*)>/;
34
81
  const scriptMatch = processedContent.match(scriptRegex);
35
82
  if (scriptMatch && scriptMatch.index !== undefined) {
36
83
  const insertPos = scriptMatch.index + scriptMatch[0].length;
37
84
  processedContent =
38
- processedContent.slice(0, insertPos) +
39
- `\n\timport { ${imports} } from 'lapikit/labs/components';` +
40
- processedContent.slice(insertPos);
85
+ processedContent.slice(0, insertPos) + importLines + processedContent.slice(insertPos);
41
86
  }
42
87
  else {
43
88
  const moduleScriptMatch = processedContent.match(/<script[^>]*\bmodule\b[^>]*>/);
@@ -45,13 +90,11 @@ function lapikitPreprocess() {
45
90
  const moduleScriptEnd = processedContent.indexOf('</script>', moduleScriptMatch.index) + '</script>'.length;
46
91
  processedContent =
47
92
  processedContent.slice(0, moduleScriptEnd) +
48
- `\n\n<script>\n\timport { ${imports} } from 'lapikit/labs/components';\n</script>` +
93
+ `\n\n<script>${importLines}\n</script>` +
49
94
  processedContent.slice(moduleScriptEnd);
50
95
  }
51
96
  else {
52
- processedContent =
53
- `<script>\n\timport { ${imports} } from 'lapikit/labs/components';\n</script>\n\n` +
54
- processedContent;
97
+ processedContent = `<script>${importLines}\n</script>\n\n` + processedContent;
55
98
  }
56
99
  }
57
100
  }
@@ -0,0 +1,52 @@
1
+ <script lang="ts">
2
+ import { useClassName, useStyles } from './btn.svelte.js';
3
+ import { type SClassProp, type SStyleProp, splitSyntheticProps } from '../../utils/index.js';
4
+
5
+ interface Btn {
6
+ id?: string;
7
+ class?: string;
8
+ style?: string;
9
+ [rest: string]: any;
10
+ className?: string | undefined | null | Array<string | undefined | null>;
11
+ }
12
+
13
+ let {
14
+ class: className,
15
+ style: styleAttr,
16
+ children,
17
+ 's-class': sClass,
18
+ 's-style': sStyle,
19
+ ...rest
20
+ } = $props();
21
+
22
+ let { classDirectiveProps, styleDirectiveProps, regularProps } = $derived(
23
+ splitSyntheticProps(rest as Record<string, unknown>)
24
+ );
25
+
26
+ let finalClass = $derived(
27
+ useClassName({
28
+ baseClass: 'kit-button',
29
+ className,
30
+ sClass,
31
+ classDirectiveProps
32
+ }).value
33
+ );
34
+
35
+ let finalStyle = $derived(
36
+ useStyles({
37
+ styleAttr,
38
+ sStyle,
39
+ styleDirectiveProps
40
+ }).value
41
+ );
42
+ </script>
43
+
44
+ <button class={finalClass} style={finalStyle} {...regularProps}>
45
+ {@render children()}
46
+ </button>
47
+
48
+ <style>
49
+ .kit-btn {
50
+ border: 1px solid rgb(0, 0, 0);
51
+ }
52
+ </style>
@@ -0,0 +1,7 @@
1
+ declare const Btn: import("svelte").Component<{
2
+ class: any;
3
+ style: any;
4
+ children: any;
5
+ } & Record<string, any>, {}, "">;
6
+ type Btn = ReturnType<typeof Btn>;
7
+ export default Btn;
@@ -0,0 +1,71 @@
1
+ export function useClassName({ baseClass = '', className, sClass, classDirectiveProps } = {}) {
2
+ return {
3
+ get value() {
4
+ const classes = [];
5
+ if (baseClass) {
6
+ classes.push(baseClass);
7
+ }
8
+ if (typeof sClass === 'string' && sClass) {
9
+ classes.push(sClass);
10
+ }
11
+ if (Array.isArray(sClass)) {
12
+ for (const value of sClass) {
13
+ if (typeof value === 'string' && value) {
14
+ classes.push(value);
15
+ }
16
+ }
17
+ }
18
+ if (sClass && typeof sClass === 'object' && !Array.isArray(sClass)) {
19
+ Object.entries(sClass).forEach(([key, value]) => {
20
+ if (value === true) {
21
+ classes.push(key);
22
+ }
23
+ else if (typeof value === 'string' && value) {
24
+ classes.push(value);
25
+ }
26
+ });
27
+ }
28
+ if (classDirectiveProps) {
29
+ Object.entries(classDirectiveProps).forEach(([key, value]) => {
30
+ const base = key.replace('s-class_', '');
31
+ if (value === true) {
32
+ classes.push(base);
33
+ }
34
+ else if (typeof value === 'string' && value) {
35
+ classes.push(`${base}${value}`);
36
+ }
37
+ });
38
+ }
39
+ if (className) {
40
+ classes.push(className);
41
+ }
42
+ return classes.filter(Boolean).join(' ');
43
+ }
44
+ };
45
+ }
46
+ export function useStyles({ styleAttr, sStyle, styleDirectiveProps } = {}) {
47
+ return {
48
+ get value() {
49
+ const styles = [];
50
+ if (sStyle && typeof sStyle === 'object') {
51
+ Object.entries(sStyle).forEach(([key, value]) => {
52
+ if (value) {
53
+ styles.push(`${key}: ${value}`);
54
+ }
55
+ });
56
+ }
57
+ if (styleDirectiveProps) {
58
+ Object.entries(styleDirectiveProps).forEach(([key, value]) => {
59
+ const base = key.replace('s-style_', '');
60
+ if (value) {
61
+ styles.push(`${base}: ${value}`);
62
+ }
63
+ });
64
+ }
65
+ if (styleAttr) {
66
+ styles.push(styleAttr);
67
+ }
68
+ return styles.filter(Boolean).join('; ');
69
+ }
70
+ };
71
+ }
@@ -1 +1 @@
1
- export { default as KitBtn } from './button/button.svelte';
1
+ export { default as KitBtn } from './btn/btn.svelte';
@@ -1,2 +1,2 @@
1
1
  // components
2
- export { default as KitBtn } from './button/button.svelte';
2
+ export { default as KitBtn } from './btn/btn.svelte';
@@ -0,0 +1 @@
1
+ export * from "./props.js";
@@ -0,0 +1 @@
1
+ export * from "./props.js";
@@ -0,0 +1,15 @@
1
+ export type SClassProp = string | string[] | Record<string, boolean | string> | undefined;
2
+ export type SStyleProp = Record<string, boolean | string> | undefined;
3
+ export declare function splitSyntheticProps(allProps: Record<string, unknown>): {
4
+ classDirectiveProps: {
5
+ [k: string]: unknown;
6
+ };
7
+ styleDirectiveProps: {
8
+ [k: string]: unknown;
9
+ };
10
+ regularProps: {
11
+ [k: string]: unknown;
12
+ };
13
+ };
14
+ export declare function computeSClasses(sClass: SClassProp, classDirectiveProps: Record<string, unknown>): string;
15
+ export declare function computeSStyles(sStyle: SStyleProp, styleDirectiveProps: Record<string, unknown>): string;
@@ -0,0 +1,60 @@
1
+ export function splitSyntheticProps(allProps) {
2
+ const classDirectiveProps = Object.fromEntries(Object.entries(allProps).filter(([key]) => key.startsWith('s-class_')));
3
+ const styleDirectiveProps = Object.fromEntries(Object.entries(allProps).filter(([key]) => key.startsWith('s-style_')));
4
+ const regularProps = Object.fromEntries(Object.entries(allProps).filter(([key]) => !key.startsWith('s-class') && !key.startsWith('s-style')));
5
+ return { classDirectiveProps, styleDirectiveProps, regularProps };
6
+ }
7
+ export function computeSClasses(sClass, classDirectiveProps) {
8
+ const classes = [];
9
+ // s-class string
10
+ if (typeof sClass === 'string' && sClass) {
11
+ classes.push(sClass);
12
+ }
13
+ // s-class array
14
+ if (Array.isArray(sClass)) {
15
+ for (const value of sClass) {
16
+ if (typeof value === 'string' && value) {
17
+ classes.push(value);
18
+ }
19
+ }
20
+ }
21
+ // s-class object
22
+ if (sClass && typeof sClass === 'object' && !Array.isArray(sClass)) {
23
+ Object.entries(sClass).forEach(([key, value]) => {
24
+ if (value === true) {
25
+ classes.push(key);
26
+ }
27
+ else if (typeof value === 'string' && value) {
28
+ classes.push(value);
29
+ }
30
+ });
31
+ }
32
+ // s-class_xxx
33
+ Object.entries(classDirectiveProps).forEach(([key, value]) => {
34
+ const base = key.replace('s-class_', '');
35
+ if (value === true) {
36
+ classes.push(base);
37
+ }
38
+ else if (typeof value === 'string' && value) {
39
+ classes.push(`${base}${value}`);
40
+ }
41
+ });
42
+ return classes.join(' ');
43
+ }
44
+ export function computeSStyles(sStyle, styleDirectiveProps) {
45
+ const styles = [];
46
+ if (sStyle && typeof sStyle === 'object') {
47
+ Object.entries(sStyle).forEach(([key, value]) => {
48
+ if (value) {
49
+ styles.push(`${key}: ${value}`);
50
+ }
51
+ });
52
+ }
53
+ Object.entries(styleDirectiveProps).forEach(([key, value]) => {
54
+ const base = key.replace('s-style_', '');
55
+ if (value) {
56
+ styles.push(`${base}: ${value}`);
57
+ }
58
+ });
59
+ return styles.join('; ');
60
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lapikit",
3
- "version": "0.0.0-insiders.f8dce9a",
3
+ "version": "0.0.0-insiders.fac243e",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,24 +0,0 @@
1
- <script lang="ts">
2
- let { children, ...rest } = $props();
3
- </script>
4
-
5
- <button {...rest}>
6
- {@render children()}
7
- </button>
8
-
9
- <style>
10
- button {
11
- padding: 0.5em 1em;
12
- border: none;
13
- border-radius: 4px;
14
- background-color: #007bff;
15
- color: white;
16
- cursor: pointer;
17
- font-size: 1em;
18
- transition: background-color 0.3s ease;
19
- }
20
-
21
- button:hover {
22
- background-color: #0056b3;
23
- }
24
- </style>
@@ -1,5 +0,0 @@
1
- declare const Button: import("svelte").Component<{
2
- children: any;
3
- } & Record<string, any>, {}, "">;
4
- type Button = ReturnType<typeof Button>;
5
- export default Button;