@lapikit/repl 0.0.0-insiders.e242cea → 0.0.0-insiders.ee3de8e

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Nycolaide <https://github.com/Nycolaide>.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1 +1,87 @@
1
1
  # @Lapikit/repl
2
+
3
+ @lapikit/repl is a Svelte component for Lapikit. It's a add-on package for Lapikit library.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -D @lapikit/repl
9
+ ```
10
+
11
+ ```javascript
12
+ // svelte.config.js
13
+
14
+ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
15
+
16
+ const config = {
17
+ preprocess: [vitePreprocess(), lapikitPreprocess({ plugins: ['repl'] })],
18
+
19
+ ...
20
+ };
21
+
22
+ export default config;
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```svelte
28
+ <kit:repl content="console.log('hello')" />
29
+ ```
30
+
31
+ ```svelte
32
+ <script lang="ts">
33
+ import sampleCounter from './samples/counter.svelte?raw';
34
+ import Counter from './samples/counter.svelte';
35
+ </script>
36
+
37
+ <kit:repl presentation content={sampleCounter}>
38
+ <Counter />
39
+ </kit:repl>
40
+ ```
41
+
42
+ ```svelte
43
+ <script lang="ts">
44
+ import sampleJson from './samples/json.json?raw';
45
+ import sampleCounter from './samples/counter.svelte?raw';
46
+ import sampleCss from './samples/css.css?raw';
47
+ import Counter from './samples/counter.svelte';
48
+ </script>
49
+
50
+ <kit:repl
51
+ presentation
52
+ content={{
53
+ 'Counter.svelte': { code: sampleCounter, lang: 'svelte' },
54
+ 'file.json': { code: sampleJson, lang: 'json' },
55
+ 'Styles.css': { code: sampleCss, lang: 'css' }
56
+ }}
57
+ >
58
+ <Counter />
59
+ </kit:repl>
60
+ ```
61
+
62
+ ```svelte
63
+ <script lang="ts">
64
+ import sampleJson from './samples/json.json?raw';
65
+ import sampleCounter from './samples/counter.svelte?raw';
66
+ import sampleCss from './samples/css.css?raw';
67
+ import Counter from './samples/counter.svelte';
68
+ </script>
69
+
70
+ <kit:repl
71
+ content={{
72
+ 'Counter.svelte': { code: sampleCounter, lang: 'svelte' },
73
+ 'file.json': { code: sampleJson, lang: 'json' },
74
+ 'Styles.css': { code: sampleCss, lang: 'css' }
75
+ }}
76
+ >
77
+ <Counter />
78
+ </kit:repl>
79
+ ```
80
+
81
+ ## Props
82
+
83
+ | Prop | Type | Default | Description |
84
+ | ------------ | --------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------- |
85
+ | content | string \| Record<string, { code: string; lang?: string }> | '' | The code content to be displayed in the REPL. It can be a single string or an object representing multiple files. |
86
+ | presentation | boolean | false | If true, the REPL will be in presentation mode, showing only the output without the code editor. |
87
+ | tiltle | string | '' | The title displayed on the REPL toolbar. |
@@ -0,0 +1,41 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+
4
+ let { children, type = 'button', disabled = false, ...rest }: PropsTypeButton = $props();
5
+
6
+ type PropsTypeButton = {
7
+ children?: Snippet;
8
+ type?: 'button' | 'submit' | 'reset';
9
+ disabled?: boolean;
10
+ [rest: string]: unknown;
11
+ };
12
+ </script>
13
+
14
+ <button {type} {disabled} {...rest}>
15
+ {@render children?.()}
16
+ </button>
17
+
18
+ <style>
19
+ button {
20
+ background: none;
21
+ border: none;
22
+ cursor: pointer;
23
+ display: flex;
24
+ align-self: center;
25
+ justify-content: center;
26
+ font-size: 0.875rem;
27
+ border-radius: 0.375rem;
28
+ transition: background-color 0.2s ease;
29
+ padding: 8px;
30
+ color: var(--kit-repl-secondary);
31
+ }
32
+
33
+ button:hover {
34
+ color: var(--kit-repl-primary);
35
+ }
36
+
37
+ button :global(svg) {
38
+ height: 20px;
39
+ width: 20px;
40
+ }
41
+ </style>
@@ -0,0 +1,9 @@
1
+ import type { Snippet } from 'svelte';
2
+ declare const Button: import("svelte").Component<{
3
+ [rest: string]: unknown;
4
+ children?: Snippet;
5
+ type?: "button" | "submit" | "reset";
6
+ disabled?: boolean;
7
+ }, {}, "">;
8
+ type Button = ReturnType<typeof Button>;
9
+ export default Button;
@@ -0,0 +1,64 @@
1
+ <script lang="ts">
2
+ import type { FilesProps } from './types.js';
3
+ import { dictionaryIcons } from './utils.js';
4
+
5
+ let { files, activeIndex = $bindable(), modeState, viewState }: FilesProps = $props();
6
+ </script>
7
+
8
+ {#if modeState !== 'playground' && viewState === 'code' && files && files.length > 1}
9
+ <div role="tablist" aria-label="Files">
10
+ {#each files as file, index (index)}
11
+ <button
12
+ type="button"
13
+ role="tab"
14
+ aria-selected={activeIndex === index}
15
+ aria-label="Select {file.name}"
16
+ class:active={activeIndex === index}
17
+ onclick={() => (activeIndex = index)}
18
+ >
19
+ {#if file.lang && dictionaryIcons[file.lang]}
20
+ <img src={dictionaryIcons[file.lang]} alt="{file.lang} icon" />
21
+ {/if}
22
+ <span>{file.name}</span>
23
+ </button>
24
+ {/each}
25
+ </div>
26
+ {/if}
27
+
28
+ <style>
29
+ div {
30
+ display: flex;
31
+ gap: calc(var(--kit-repl-spacing) * 2);
32
+ padding-left: calc(5 * var(--kit-repl-spacing));
33
+ padding-right: calc(5 * var(--kit-repl-spacing));
34
+ padding-block: calc(var(--kit-repl-spacing) * 2);
35
+ overflow-x: auto;
36
+ }
37
+
38
+ button {
39
+ display: flex;
40
+ align-items: center;
41
+ gap: calc(var(--kit-repl-spacing) * 2);
42
+ padding: calc(var(--kit-repl-spacing) * 2) calc(var(--kit-repl-spacing) * 3);
43
+ font-size: 0.875rem;
44
+ transition: all 0.2s ease;
45
+ border: 0;
46
+ white-space: nowrap;
47
+ background-color: transparent;
48
+ border-bottom: 2px solid transparent;
49
+ cursor: pointer;
50
+ }
51
+
52
+ button img {
53
+ width: 16px;
54
+ height: 16px;
55
+ }
56
+
57
+ button:hover {
58
+ border-color: #cfcfcf;
59
+ }
60
+
61
+ button.active {
62
+ border-color: #000000;
63
+ }
64
+ </style>
@@ -0,0 +1,4 @@
1
+ import type { FilesProps } from './types.js';
2
+ declare const Files: import("svelte").Component<FilesProps, {}, "activeIndex">;
3
+ type Files = ReturnType<typeof Files>;
4
+ export default Files;
package/dist/Repl.svelte CHANGED
@@ -1,39 +1,35 @@
1
1
  <script lang="ts">
2
2
  import { copyToClipboard } from './utils.js';
3
+ import { getHighlighterSingleton } from './shiki.js';
4
+ import type { FileItem, ReplProps } from './types.js';
3
5
 
4
- import { Copy, Check, Code, Codesandbox, Moon, Sun } from '@lucide/svelte';
6
+ // components
7
+ import Toolbar from './Toolbar.svelte';
8
+ import Files from './Files.svelte';
5
9
 
6
- import CodeIcon from './assets/languages/code.svg';
7
- import JavaScriptIcon from './assets/languages/javascript.svg';
8
- import TypeScriptIcon from './assets/languages/typescript.svg';
9
- import SvelteIcon from './assets/languages/svelte.svg';
10
- import CssIcon from './assets/languages/css.svg';
11
- import HtmlIcon from './assets/languages/html.svg';
12
- import { getHighlighterSingleton } from './shiki.js';
10
+ let { title, content, children, presentation }: ReplProps = $props();
13
11
 
14
- interface FileItem {
15
- name: string;
16
- content: string;
17
- lang?: string;
18
- }
12
+ // refs
13
+ let ref: null | HTMLElement = $state(null);
19
14
 
20
- let { title, content, children, ...rest } = $props();
21
- let codeHTML = $state('');
15
+ // states
16
+ let language = $state('sh');
17
+
18
+ let modeState: 'code' | 'playground' | 'mixed' = $state('code');
22
19
  let copyState = $state(false);
23
- let viewMode = $state('editor');
24
- let themeMode = $state('light');
20
+ let viewState: 'code' | 'preview' = $state('code');
21
+ let themeState: 'light' | 'dark' = $state('light');
25
22
 
26
- let ref: null | HTMLElement = $state(null);
27
- let typeContent = $state('code');
23
+ let codeHTML = $state('');
24
+ let activeFileIndex = $state(0);
28
25
 
29
- // multiple content types
30
26
  let files = $derived.by<FileItem[]>(() => {
31
27
  if (typeof content === 'object' && content !== null && 'code' in content) {
32
28
  return [
33
29
  {
34
30
  name: title || 'code',
35
31
  content: content.code,
36
- lang: content.lang || 'javascript'
32
+ lang: content.lang || 'sh'
37
33
  }
38
34
  ];
39
35
  }
@@ -41,8 +37,14 @@
41
37
  if (typeof content === 'object' && content !== null && !Array.isArray(content)) {
42
38
  return Object.entries(content).map(([name, fileContent]) => ({
43
39
  name,
44
- content: typeof fileContent === 'string' ? fileContent : (fileContent as any).code || '',
45
- lang: typeof fileContent === 'object' ? (fileContent as any).lang : 'javascript'
40
+ content:
41
+ typeof fileContent === 'string'
42
+ ? fileContent
43
+ : (fileContent as Record<string, unknown>).code || '',
44
+ lang:
45
+ typeof fileContent === 'object'
46
+ ? ((fileContent as Record<string, unknown>).lang as string)
47
+ : 'sh'
46
48
  }));
47
49
  }
48
50
 
@@ -50,32 +52,30 @@
50
52
  return content.map((item) => ({
51
53
  name: item.name,
52
54
  content: item.content || item.code || '',
53
- lang: item.lang || 'javascript'
55
+ lang: item.lang || 'sh'
54
56
  }));
55
57
  }
56
58
 
57
- return [{ name: 'code', content: content || '', lang: 'javascript' }];
59
+ return [{ name: 'code', content: content || '', lang: 'sh' }];
58
60
  });
59
-
60
- let activeFileIndex = $state(0);
61
61
  let activeFile = $derived(files[activeFileIndex]);
62
- let hasMultipleFiles = $derived(files.length > 1);
63
62
 
64
- $effect(() => {
65
- console.log('Props rest', rest);
63
+ $effect.pre(() => {
64
+ if (children && content && !presentation) {
65
+ modeState = 'mixed';
66
+ viewState = 'preview';
67
+ } else if (presentation) {
68
+ modeState = 'mixed';
69
+ viewState = 'code';
70
+ } else if (children && !content) {
71
+ modeState = 'playground';
72
+ viewState = 'preview';
73
+ } else {
74
+ modeState = 'code';
75
+ viewState = 'code';
76
+ }
66
77
  });
67
78
 
68
- let iconMap = {
69
- code: CodeIcon,
70
- javascript: JavaScriptIcon,
71
- typescript: TypeScriptIcon,
72
- svelte: SvelteIcon,
73
- css: CssIcon,
74
- html: HtmlIcon
75
- } as const;
76
-
77
- let currentIcon = $derived(iconMap[typeContent as keyof typeof iconMap] || CodeIcon);
78
-
79
79
  $effect(() => {
80
80
  if (copyState) {
81
81
  if (ref?.textContent) {
@@ -91,16 +91,16 @@
91
91
 
92
92
  $effect(() => {
93
93
  const file = activeFile;
94
- const theme = themeMode;
94
+ const theme = themeState;
95
95
 
96
96
  if (file?.content) {
97
97
  (async () => {
98
- console.log('Rendering file:', file);
99
98
  const highlighter = await getHighlighterSingleton();
100
99
 
100
+ language = file.lang || 'sh';
101
101
  const html = highlighter.codeToHtml(file.content, {
102
102
  theme: theme === 'light' ? 'github-light' : 'github-dark',
103
- lang: file.lang || 'javascript'
103
+ lang: file.lang || language
104
104
  });
105
105
 
106
106
  codeHTML = html;
@@ -109,235 +109,121 @@
109
109
  });
110
110
  </script>
111
111
 
112
- <div class="repl-container">
113
- <div class="repl-toolbar">
114
- <div class="repl-toolbar--title">
115
- <img src={currentIcon} alt="{typeContent} icon" class="toolbar-icon" />
116
- <span>{title || 'REPL Toolbar'}</span>
117
- </div>
118
-
119
- <div class="repl-toolbar--actions">
120
- {#if viewMode !== 'editor'}
121
- <button
122
- class="repl-btn--icon"
123
- onclick={() => (themeMode = themeMode === 'light' ? 'dark' : 'light')}
124
- title="Toggle Theme"
125
- >
126
- {#if themeMode === 'light'}
127
- <Moon class="toolbar-icon" />
128
- {:else}
129
- <Sun class="toolbar-icon" />
130
- {/if}
131
- </button>
132
- {/if}
133
- <button
134
- class="repl-btn--icon"
135
- title={viewMode === 'editor' ? 'Code' : 'Playground'}
136
- onclick={() => (viewMode = viewMode === 'editor' ? 'playground' : 'editor')}
137
- >
138
- {#if viewMode === 'editor'}
139
- <Code class="toolbar-icon" />
140
- {:else}
141
- <Codesandbox class="toolbar-icon" />
142
- {/if}
143
- </button>
144
- <button
145
- class="repl-btn--icon"
146
- onclick={() => (copyState = true)}
147
- title={copyState ? 'Copied!' : 'Copy'}
112
+ <div class="kit-repl">
113
+ {#if presentation}
114
+ <div class="kit-repl-content" class:kit-repl-content--playground={presentation}>
115
+ <div
116
+ class="wrapper-playground"
117
+ class:dark={themeState === 'dark'}
118
+ class:light={themeState === 'light'}
148
119
  >
149
- {#if copyState}
150
- <Check class="toolbar-icon" />
151
- {:else}
152
- <Copy class="toolbar-icon" />
153
- {/if}
154
- </button>
120
+ {@render children?.()}
121
+ </div>
155
122
  </div>
156
- </div>
123
+ {/if}
157
124
 
158
- <hr />
125
+ <div class="kit-repl-container">
126
+ <Toolbar
127
+ {title}
128
+ {language}
129
+ {presentation}
130
+ bind:copyState
131
+ bind:viewState
132
+ bind:themeState
133
+ bind:modeState
134
+ />
135
+
136
+ {#if modeState !== 'code'}
137
+ <hr />
138
+ {/if}
159
139
 
160
- {#if hasMultipleFiles && viewMode === 'editor'}
161
- <div class="sub-toolbar">
162
- {#each files as file, index (index)}
163
- <button
164
- class="file-tab"
165
- class:active={activeFileIndex === index}
166
- onclick={() => (activeFileIndex = index)}
167
- title={file.name}
140
+ <Files {files} bind:activeIndex={activeFileIndex} {modeState} {viewState} />
141
+
142
+ <div
143
+ class="kit-repl-content"
144
+ class:kit-repl-content--code={viewState === 'code' && !presentation}
145
+ >
146
+ {#if viewState === 'code'}
147
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
148
+ <div class="kit-repl-wrapper-highlight" bind:this={ref}>{@html codeHTML}</div>
149
+ {:else}
150
+ <div
151
+ class="kit-repl-wrapper-playground"
152
+ class:dark={themeState === 'dark'}
153
+ class:light={themeState === 'light'}
168
154
  >
169
- <img
170
- src={iconMap[file.lang as keyof typeof iconMap] || CodeIcon}
171
- alt="{file.lang} icon"
172
- class="file-icon"
173
- />
174
- <span>{file.name}</span>
175
- </button>
176
- {/each}
155
+ {@render children?.()}
156
+ </div>
157
+ {/if}
177
158
  </div>
178
- <hr />
179
- {/if}
180
-
181
- <div class="repl-content">
182
- {#if viewMode === 'editor'}
183
- <!-- eslint-disable-next-line svelte/no-at-html-tags -->
184
- <div class="wrapper-highlight" bind:this={ref}>{@html codeHTML}</div>
185
- {:else}
186
- <div>{@render children?.()}</div>
187
- {/if}
188
159
  </div>
189
160
  </div>
190
161
 
191
162
  <style>
192
- div.repl-container {
193
- --repl-spacing: 0.25rem;
194
- --repl-radius: 1rem;
195
- --repl-shiki-size: 1rem;
196
- --repl-shiki-tab-size: 2;
163
+ .kit-repl {
164
+ /* ui */
165
+ --kit-repl-spacing: 0.25rem;
166
+ --kit-repl-radius: 1rem;
197
167
 
198
- --repl-background: #f9f9f9;
199
- --repl-border-color: #ebebeb;
168
+ /* shiki override */
169
+ --kit-repl-shiki-size: 0.875rem;
170
+ --kit-repl-shiki-tab-size: 2;
200
171
 
201
- --repl-primary: #0d0d34;
202
- --repl-secondary: #8f8f8f;
203
-
204
- background-color: var(--repl-background);
205
- border-radius: var(--repl-radius);
206
- border: 2px solid var(--repl-border-color);
207
- }
208
-
209
- div.repl-container :global(pre) {
210
- background-color: var(--repl-background) !important;
211
- }
212
-
213
- div.repl-toolbar {
214
- display: flex;
215
- align-items: center;
216
- justify-content: space-between;
217
- gap: calc(var(--repl-spacing) * 3);
218
- padding-left: calc(5 * var(--repl-spacing));
219
- padding-right: calc(var(--repl-spacing) * 2);
220
- padding-block: calc(var(--repl-spacing) * 1.5);
221
- border-top-left-radius: var(--repl-radius);
222
- border-top-right-radius: var(--repl-radius);
223
- min-height: 36px;
224
- }
225
-
226
- div.repl-toolbar--actions {
227
- display: flex;
228
- align-items: center;
229
- gap: calc(var(--repl-spacing) * 2);
230
- }
231
-
232
- .sub-toolbar button {
233
- }
234
-
235
- div button.repl-btn--icon {
236
- background: none;
237
- border: none;
238
- cursor: pointer;
239
- display: flex;
240
- align-self: center;
241
- justify-content: center;
242
- font-size: 0.875rem;
243
- border-radius: 0.375rem;
244
- transition: background-color 0.2s ease;
245
- padding: 8px;
246
- color: var(--repl-secondary);
247
- }
248
-
249
- div button.repl-btn--icon:hover {
250
- color: var(--repl-primary);
172
+ /* colors */
173
+ --kit-repl-background: #f9f9f9;
174
+ --kit-repl-border-color: #ebebeb;
175
+ --kit-repl-primary: #0d0d34;
176
+ --kit-repl-secondary: #8f8f8f;
251
177
  }
252
-
253
- div button.repl-btn--icon :global(svg) {
254
- height: 20px;
255
- width: 20px;
178
+ .kit-repl-container {
179
+ background-color: var(--kit-repl-background);
180
+ border-radius: var(--kit-repl-radius);
181
+ border: 2px solid var(--kit-repl-border-color);
256
182
  }
257
183
 
258
- .repl-toolbar--title {
259
- display: flex;
260
- align-items: center;
261
- gap: calc(var(--repl-spacing) * 2);
262
- font-weight: 600;
263
- color: var(--repl-secondary);
264
- max-width: 80%;
265
- min-width: 0;
184
+ .kit-repl-container :global(pre) {
185
+ background-color: var(--kit-repl-background) !important;
266
186
  }
267
187
 
268
- .repl-toolbar--title span {
269
- overflow: hidden;
270
- text-overflow: ellipsis;
271
- white-space: nowrap;
188
+ .kit-repl-content {
189
+ display: flow-root;
190
+ margin-top: calc(var(--kit-repl-spacing) * 0);
191
+ padding-right: calc(10 * var(--kit-repl-spacing));
192
+ padding-left: calc(5 * var(--kit-repl-spacing));
193
+ padding-bottom: calc(4 * var(--kit-repl-spacing));
194
+ padding-top: calc(3 * var(--kit-repl-spacing));
195
+ position: relative;
272
196
  }
273
197
 
274
- .repl-toolbar--title .toolbar-icon {
275
- width: 20px;
276
- height: 20px;
277
- object-fit: contain;
278
- flex-shrink: 0;
198
+ .kit-repl-content--code {
199
+ padding-top: 0;
279
200
  }
280
201
 
281
- .repl-content {
282
- display: flow-root;
283
- margin-top: calc(var(--repl-spacing) * 0);
284
- padding-right: calc(10 * var(--repl-spacing));
285
- padding-left: calc(5 * var(--repl-spacing));
286
- padding-bottom: calc(4 * var(--repl-spacing));
287
- padding-top: calc(3 * var(--repl-spacing));
288
- position: relative;
202
+ .kit-repl-content--playground {
203
+ padding-top: calc(4 * var(--kit-repl-spacing));
204
+ padding-bottom: calc(10 * var(--kit-repl-spacing));
289
205
  }
290
206
 
291
207
  hr {
292
- max-width: calc(100% - 4.5rem);
293
- margin-inline-start: calc(4.5rem / 2);
208
+ max-width: calc(100% - 2.5rem);
209
+ margin-inline-start: calc(2.5rem / 2);
294
210
  display: block;
295
- border: thin solid var(--repl-border-color);
211
+ border: thin solid var(--kit-repl-border-color);
296
212
  margin-top: 0;
297
213
  margin-bottom: 0;
298
214
  }
299
215
 
300
- div.repl-container .wrapper-highlight :global(pre code) {
301
- font-size: var(--repl-shiki-size);
302
- -moz-tab-size: var(--repl-shiki-tab-size);
303
- tab-size: var(--repl-shiki-tab-size);
216
+ div.kit-repl-container .kit-repl-wrapper-highlight :global(pre code) {
217
+ font-size: var(--kit-repl-shiki-size);
218
+ -moz-tab-size: var(--kit-repl-shiki-tab-size);
219
+ tab-size: var(--kit-repl-shiki-tab-size);
304
220
  white-space: pre-wrap;
305
221
  word-break: break-word;
306
222
  }
307
223
 
308
- .sub-toolbar {
309
- display: flex;
310
- gap: calc(var(--repl-spacing) * 2);
311
- padding-left: calc(5 * var(--repl-spacing));
312
- padding-right: calc(5 * var(--repl-spacing));
313
- padding-block: calc(var(--repl-spacing) * 2);
314
- overflow-x: auto;
315
- }
316
-
317
- .file-tab {
318
- display: flex;
319
- align-items: center;
320
- gap: calc(var(--repl-spacing) * 2);
321
- padding: calc(var(--repl-spacing) * 2) calc(var(--repl-spacing) * 3);
322
- border-radius: 0.375rem;
323
- font-size: 0.875rem;
324
- transition: all 0.2s ease;
325
- border: 1px solid transparent;
326
- white-space: nowrap;
327
- }
328
-
329
- .file-tab:hover {
330
- background-color: #f5f5f5;
331
- }
332
-
333
- .file-tab.active {
334
- background-color: #f0f0f0;
335
- border-color: #d0d0d0;
336
- }
337
-
338
- .file-icon {
339
- width: 16px;
340
- height: 16px;
341
- object-fit: contain;
224
+ div.kit-repl-container .kit-repl-wrapper-playground {
225
+ background-color: var(--kit-repl-background);
226
+ border-radius: var(--kit-repl-radius);
227
+ padding: calc(4 * var(--kit-repl-spacing));
342
228
  }
343
229
  </style>
@@ -1,7 +1,4 @@
1
- declare const Repl: import("svelte").Component<{
2
- title: any;
3
- content: any;
4
- children: any;
5
- } & Record<string, any>, {}, "">;
1
+ import type { ReplProps } from './types.js';
2
+ declare const Repl: import("svelte").Component<ReplProps, {}, "">;
6
3
  type Repl = ReturnType<typeof Repl>;
7
4
  export default Repl;
@@ -0,0 +1,120 @@
1
+ <script lang="ts">
2
+ import type { ToolbarProps } from './types.js';
3
+ import Button from './Button.svelte';
4
+ import { dictionary } from './utils.js';
5
+ import { Copy, Check, Code, Codesandbox, Moon, Sun } from '@lucide/svelte';
6
+
7
+ let {
8
+ title,
9
+ language,
10
+ presentation,
11
+ copyState = $bindable(),
12
+ viewState = $bindable(),
13
+ themeState = $bindable(),
14
+ modeState = $bindable()
15
+ }: ToolbarProps = $props();
16
+
17
+ let languageKey = $derived(
18
+ Object.entries(dictionary).find(([, values]) => values.includes(language))?.[0] || language
19
+ );
20
+ </script>
21
+
22
+ <div class="kit-repl--toolbar">
23
+ <div
24
+ class="kit-repl--toolbar-title"
25
+ class:kit-repl--toolbar-title--language={!title && language}
26
+ class:kit-repl--toolbar-title--title={title}
27
+ >
28
+ {#if title}
29
+ <span>{title}</span>
30
+ {:else if language}
31
+ <span>{languageKey}</span>
32
+ {/if}
33
+ </div>
34
+
35
+ <div class="kit-repl--toolbar-actions">
36
+ {#if (modeState !== 'code' && viewState === 'preview') || presentation}
37
+ <Button
38
+ aria-label={themeState === 'light' ? 'Switch to dark theme' : 'Switch to light theme'}
39
+ aria-pressed={themeState === 'dark'}
40
+ onclick={() => (themeState = themeState === 'light' ? 'dark' : 'light')}
41
+ >
42
+ {#if themeState === 'light'}
43
+ <Moon />
44
+ {:else}
45
+ <Sun />
46
+ {/if}
47
+ </Button>
48
+ {/if}
49
+
50
+ {#if modeState === 'mixed' && !presentation}
51
+ <Button
52
+ aria-label={viewState === 'code' ? 'Show preview' : 'Show code'}
53
+ aria-pressed={viewState === 'preview'}
54
+ onclick={() => (viewState = viewState === 'code' ? 'preview' : 'code')}
55
+ >
56
+ {#if viewState === 'code'}
57
+ <Code />
58
+ {:else}
59
+ <Codesandbox />
60
+ {/if}
61
+ </Button>
62
+ {/if}
63
+
64
+ {#if modeState !== 'playground'}
65
+ <Button
66
+ aria-label={copyState ? 'Code copied' : 'Copy code'}
67
+ onclick={() => (copyState = true)}
68
+ >
69
+ {#if copyState}
70
+ <Check />
71
+ {:else}
72
+ <Copy />
73
+ {/if}
74
+ </Button>
75
+ {/if}
76
+ </div>
77
+ </div>
78
+
79
+ <style>
80
+ .kit-repl--toolbar {
81
+ display: flex;
82
+ align-items: center;
83
+ justify-content: space-between;
84
+ gap: calc(var(--kit-repl-spacing) * 3);
85
+ padding-left: calc(5 * var(--kit-repl-spacing));
86
+ padding-right: calc(var(--kit-repl-spacing) * 2);
87
+ padding-block: calc(var(--kit-repl-spacing) * 1.5);
88
+ border-top-left-radius: var(--kit-repl-radius);
89
+ border-top-right-radius: var(--kit-repl-radius);
90
+ min-height: 36px;
91
+ }
92
+
93
+ .kit-repl--toolbar .kit-repl--toolbar-title {
94
+ display: flex;
95
+ align-items: center;
96
+ gap: calc(var(--kit-repl-spacing) * 2);
97
+ max-width: 80%;
98
+ min-width: 0;
99
+ }
100
+
101
+ .kit-repl--toolbar-title--language {
102
+ font-size: 0.875rem;
103
+ line-height: 16px;
104
+ font-weight: 400;
105
+ color: #5d5d5d;
106
+ }
107
+
108
+ .kit-repl--toolbar-title--title {
109
+ font-weight: 500;
110
+ font-size: 1rem;
111
+ line-height: 20px;
112
+ color: #8f8f8f;
113
+ }
114
+
115
+ .kit-repl--toolbar .kit-repl--toolbar-actions {
116
+ display: flex;
117
+ align-items: center;
118
+ gap: calc(var(--kit-repl-spacing) * 2);
119
+ }
120
+ </style>
@@ -0,0 +1,4 @@
1
+ import type { ToolbarProps } from './types.js';
2
+ declare const Toolbar: import("svelte").Component<ToolbarProps, {}, "modeState" | "viewState" | "copyState" | "themeState">;
3
+ type Toolbar = ReturnType<typeof Toolbar>;
4
+ export default Toolbar;
package/dist/index.d.ts CHANGED
@@ -1,9 +1 @@
1
- declare function replPreprocess(): {
2
- markup({ content }: {
3
- content: string;
4
- filename?: string;
5
- }): {
6
- code: string;
7
- } | undefined;
8
- };
9
- export { replPreprocess };
1
+ export { default as KitRepl } from './Repl.svelte';
package/dist/index.js CHANGED
@@ -1,71 +1,2 @@
1
- const components = ['code'];
2
- function componentName(shortName) {
3
- return 'Kit' + shortName.charAt(0).toUpperCase() + shortName.slice(1);
4
- }
5
- function replPreprocess() {
6
- return {
7
- markup({ content }) {
8
- const hasComponent = components.some((comp) => content.includes(`<repl:${comp}`));
9
- if (!hasComponent)
10
- return;
11
- let processedContent = content;
12
- const importedComponents = new Set();
13
- let hasChanges = true;
14
- while (hasChanges) {
15
- hasChanges = false;
16
- for (const shortName of components) {
17
- const componentNameStr = componentName(shortName);
18
- const attrPattern = `(?:[^>"']|"[^"]*"|'[^']*')*?`;
19
- const selfClosingRegex = new RegExp(`<repl:${shortName}(${attrPattern})\\s*/>`, 'g');
20
- const pairRegex = new RegExp(`<repl:${shortName}(${attrPattern})>([\\s\\S]*?)<\\/repl:${shortName}\\s*>`, 'g');
21
- let newContent = processedContent.replace(selfClosingRegex, (fullMatch, attrs) => {
22
- hasChanges = true;
23
- importedComponents.add(componentNameStr);
24
- return `<${componentNameStr}${attrs} />`;
25
- });
26
- newContent = newContent.replace(pairRegex, (fullMatch, attrs, children) => {
27
- hasChanges = true;
28
- importedComponents.add(componentNameStr);
29
- return `<${componentNameStr}${attrs}>${children}</${componentNameStr}>`;
30
- });
31
- if (newContent !== processedContent) {
32
- processedContent = newContent;
33
- }
34
- }
35
- }
36
- if (processedContent === content)
37
- return;
38
- if (importedComponents.size > 0) {
39
- const imports = Array.from(importedComponents).join(', ');
40
- const scriptRegex = /<script(?![^>]*\bmodule\b)([^>]*)>/;
41
- const scriptMatch = processedContent.match(scriptRegex);
42
- if (scriptMatch && scriptMatch.index !== undefined) {
43
- const insertPos = scriptMatch.index + scriptMatch[0].length;
44
- processedContent =
45
- processedContent.slice(0, insertPos) +
46
- `\n\timport { ${imports} } from '@lapikit/repl/components';` +
47
- processedContent.slice(insertPos);
48
- }
49
- else {
50
- const moduleScriptMatch = processedContent.match(/<script[^>]*\bmodule\b[^>]*>/);
51
- if (moduleScriptMatch && moduleScriptMatch.index !== undefined) {
52
- const moduleScriptEnd = processedContent.indexOf('</script>', moduleScriptMatch.index) + '</script>'.length;
53
- processedContent =
54
- processedContent.slice(0, moduleScriptEnd) +
55
- `\n\n<script>\n\timport { ${imports} } from '@lapikit/repl/components';\n</script>` +
56
- processedContent.slice(moduleScriptEnd);
57
- }
58
- else {
59
- processedContent =
60
- `<script>\n\timport { ${imports} } from '@lapikit/repl/components';\n</script>\n\n` +
61
- processedContent;
62
- }
63
- }
64
- }
65
- return {
66
- code: processedContent
67
- };
68
- }
69
- };
70
- }
71
- export { replPreprocess };
1
+ // components
2
+ export { default as KitRepl } from './Repl.svelte';
@@ -0,0 +1,27 @@
1
+ import type { Snippet } from 'svelte';
2
+ export interface ReplProps {
3
+ title?: string;
4
+ content?: string | Record<string, string | Record<string, unknown>>;
5
+ children?: Snippet;
6
+ presentation?: boolean;
7
+ }
8
+ export interface ToolbarProps {
9
+ title?: string;
10
+ language: string;
11
+ copyState?: boolean;
12
+ presentation?: boolean;
13
+ viewState?: 'code' | 'preview';
14
+ themeState?: 'light' | 'dark';
15
+ modeState?: 'code' | 'playground' | 'mixed';
16
+ }
17
+ export interface FilesProps {
18
+ files?: FileItem[];
19
+ activeIndex: number;
20
+ viewState: 'code' | 'preview';
21
+ modeState: 'code' | 'playground' | 'mixed';
22
+ }
23
+ export interface FileItem {
24
+ name: string;
25
+ content: string;
26
+ lang?: string;
27
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/utils.d.ts CHANGED
@@ -1 +1,7 @@
1
1
  export declare const copyToClipboard: (value: string) => void;
2
+ export declare const dictionary: {
3
+ [key: string]: string[];
4
+ };
5
+ export declare const dictionaryIcons: {
6
+ [key: string]: string;
7
+ };
package/dist/utils.js CHANGED
@@ -1,3 +1,9 @@
1
+ import javascript from './languages/javascript.svg';
2
+ import typescript from './languages/typescript.svg';
3
+ import svelte from './languages/svelte.svg';
4
+ import css from './languages/css.svg';
5
+ import html from './languages/html.svg';
6
+ import shell from './languages/shell.svg';
1
7
  export const copyToClipboard = (value) => {
2
8
  if (navigator.clipboard && window.isSecureContext) {
3
9
  navigator.clipboard
@@ -28,3 +34,28 @@ export const copyToClipboard = (value) => {
28
34
  document.body.removeChild(zoneTexte);
29
35
  }
30
36
  };
37
+ export const dictionary = {
38
+ javascript: ['js', 'javascript'],
39
+ typescript: ['ts', 'typescript'],
40
+ svelte: ['svelte', 'svelte.ts', 'svelte.js'],
41
+ css: ['css', 'scss', 'less'],
42
+ html: ['html', 'htm'],
43
+ json: ['json'],
44
+ shell: ['bash', 'sh', 'shell']
45
+ };
46
+ export const dictionaryIcons = {
47
+ js: javascript,
48
+ ts: typescript,
49
+ 'svelte.ts': svelte,
50
+ 'svelte.js': svelte,
51
+ svelte: svelte,
52
+ shell: shell,
53
+ bash: shell,
54
+ sh: shell,
55
+ css: css,
56
+ scss: css,
57
+ less: css,
58
+ html: html,
59
+ htm: html,
60
+ json: shell
61
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lapikit/repl",
3
- "version": "0.0.0-insiders.e242cea",
3
+ "version": "0.0.0-insiders.ee3de8e",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
14
- "url": "https://github.com/Nycolaide/lapikit.git"
14
+ "url": "git+https://github.com/lapikit/lapikit-repl.git"
15
15
  },
16
16
  "scripts": {
17
17
  "dev": "vite dev",
@@ -39,11 +39,8 @@
39
39
  "type": "module",
40
40
  "exports": {
41
41
  ".": {
42
- "default": "./dist/index.js"
43
- },
44
- "./components": {
45
- "types": "./dist/components.d.ts",
46
- "svelte": "./dist/components.js"
42
+ "types": "./dist/index.d.ts",
43
+ "svelte": "./dist/index.js"
47
44
  }
48
45
  },
49
46
  "peerDependencies": {
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check-icon lucide-check"><path d="M20 6 9 17l-5-5"/></svg>
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-code-icon lucide-code"><path d="m16 18 6-6-6-6"/><path d="m8 6-6 6 6 6"/></svg>
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-codesandbox-icon lucide-codesandbox"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><polyline points="7.5 4.21 12 6.81 16.5 4.21"/><polyline points="7.5 19.79 7.5 14.6 3 12"/><polyline points="21 12 16.5 14.6 16.5 19.79"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" x2="12" y1="22.08" y2="12"/></svg>
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy-icon lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>
@@ -1 +0,0 @@
1
- export { default as KitCode } from './Repl.svelte';
@@ -1,2 +0,0 @@
1
- // components
2
- export { default as KitCode } from './Repl.svelte';
File without changes
File without changes
File without changes