From 6736a87fec09acd10abb8bdc1c3eab9b0c1bffae Mon Sep 17 00:00:00 2001 From: Puru Vijay Date: Fri, 23 Jun 2023 23:29:03 +0530 Subject: [PATCH 1/3] Push --- packages/repl/package.json | 2 +- .../src/lib/Input/ComponentSelector.svelte | 213 +-------- .../src/lib/Input/OldComponentSelector.svelte | 415 ++++++++++++++++++ packages/repl/src/lib/Repl.svelte | 71 ++- packages/repl/src/lib/types.d.ts | 6 +- 5 files changed, 474 insertions(+), 233 deletions(-) create mode 100644 packages/repl/src/lib/Input/OldComponentSelector.svelte diff --git a/packages/repl/package.json b/packages/repl/package.json index ac36a29d..d75e2c08 100644 --- a/packages/repl/package.json +++ b/packages/repl/package.json @@ -39,7 +39,7 @@ "build": "vite build && npm run package", "preview": "vite preview", "package": "svelte-kit sync && svelte-package && publint", - "watch": "svelte-package --watch", + "package:watch": "svelte-package --watch", "prepublishOnly": "npm run package", "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch" diff --git a/packages/repl/src/lib/Input/ComponentSelector.svelte b/packages/repl/src/lib/Input/ComponentSelector.svelte index c8721966..75d78066 100644 --- a/packages/repl/src/lib/Input/ComponentSelector.svelte +++ b/packages/repl/src/lib/Input/ComponentSelector.svelte @@ -1,110 +1,29 @@
{#if $files.length}
- {#each $files as file, index (file)} -
select_file(index)} - on:keyup={(e) => e.key === ' ' && select_file(index)} - on:dblclick|stopPropagation={() => {}} - draggable={index !== editing_index} - on:dragstart={dragStart} - on:dragover|preventDefault={dragOver} - on:dragleave={dragLeave} - on:drop|preventDefault={dragEnd} + id={filename} + class:active={filename === $selected_name} + class:draggable={filename !== editing_name && index !== 0} > - {#if file.name === 'App' && index !== editing_index} + {#if file.name === 'App' && filename !== editing_name}
App.svelte{#if show_modified && file.modified}*{/if}
- {:else if index === editing_index} - {@const file = $files[editing_index]} - - - {file.name + (/\./.test(file.name) ? '' : `.${file.type}`)} - - - - - e.key === 'Enter' && !is_file_name_used(file) && e.currentTarget.blur()} - class:duplicate={is_file_name_used(file)} - /> - {:else} -
edit_tab(index)} - on:keyup={(e) => e.key === ' ' && edit_tab(index)} - > - {file.name}.{file.type}{#if show_modified && file.modified}*{/if} -
- - remove(index)} - on:keyup={(e) => e.key === ' ' && remove(index)} - > - - - - - - {/if} -
+ {:else if filename === editing_name}{/if} + {/each} +
+ {/if} +
+ + diff --git a/packages/repl/src/lib/Repl.svelte b/packages/repl/src/lib/Repl.svelte index f2243c4e..6acdcf3c 100644 --- a/packages/repl/src/lib/Repl.svelte +++ b/packages/repl/src/lib/Repl.svelte @@ -4,7 +4,7 @@ import { createEventDispatcher } from 'svelte'; import { derived, writable } from 'svelte/store'; import Bundler from './Bundler.js'; - import ComponentSelector from './Input/ComponentSelector.svelte'; + import ComponentSelector from './Input/OldComponentSelector.svelte'; import ModuleEditor from './Input/ModuleEditor.svelte'; import InputOutputToggle from './InputOutputToggle.svelte'; import Output from './Output/Output.svelte'; @@ -39,7 +39,7 @@ */ export async function set(data) { $files = data.files; - $selected_index = 0; + $selected_name = 'App.svelte'; rebundle(); @@ -58,37 +58,33 @@ export function markSaved() { $files = $files.map((val) => ({ ...val, modified: false })); - if (!$selected) return; + // if (!$selected) return; - $files[$selected_index].modified = false; + // const current = $files.find(val => get_full_filename(val) === $selected_name).modified = false; } /** @param {{ files: import('./types').File[], css?: string }} data */ export function update(data) { - if (!$selected) return; - - const { name, type } = $selected; - $files = data.files; - const matched_component_index = data.files.findIndex( - (file) => file.name === name && file.type === type - ); + const matched_file = data.files.find((file) => get_full_filename(file) === $selected_name); - $selected_index = matched_component_index === -1 ? 0 : matched_component_index; + $selected_name = matched_file ? get_full_filename(matched_file) : 'App.svelte'; injectedCSS = data.css ?? ''; - if (matched_component_index) { + if (matched_file) { $module_editor?.update({ - code: $files[matched_component_index].source, - lang: $files[matched_component_index].type + code: matched_file.source, + lang: matched_file.type }); - $output?.update?.($files[matched_component_index], $compile_options); + $output?.update?.(matched_file, $compile_options); $module_editor?.clearEditorState(); } + + dispatch('change', { files: $files }); } /** @type {ReturnType>} */ @@ -115,12 +111,19 @@ /** @type {ReplContext['files']} */ const files = writable([]); - /** @type {ReplContext['selected_index']} */ - const selected_index = writable(-1); + /** @type {ReplContext['selected_name']} */ + const selected_name = writable('App.svelte'); /** @type {ReplContext['selected']} */ - const selected = derived([files, selected_index], ([$files, $selected_index]) => { - return $selected_index !== -1 ? $files?.[$selected_index] ?? null : null; + const selected = derived([files, selected_name], ([$files, $selected_name]) => { + return ( + $files.find((val) => get_full_filename(val) === $selected_name) ?? { + name: '', + type: '', + source: '', + modified: false + } + ); }); /** @type {ReplContext['bundle']} */ @@ -146,7 +149,7 @@ set_repl_context({ files, - selected_index, + selected_name, selected, bundle, bundler, @@ -176,24 +179,24 @@ let is_select_changing = false; /** - * @param {number} index + * @param {string} filename */ - async function handle_select(index) { + async function handle_select(filename) { is_select_changing = true; - $selected_index = index; + $selected_name = filename; if (!$selected) return; await $module_editor?.set({ code: $selected.source, lang: $selected.type }); - if (EDITOR_STATE_MAP.has(get_full_filename($selected))) { - $module_editor?.setEditorState(EDITOR_STATE_MAP.get(get_full_filename($selected))); + if (EDITOR_STATE_MAP.has(filename)) { + $module_editor?.setEditorState(EDITOR_STATE_MAP.get(filename)); } else { $module_editor?.clearEditorState(); } - EDITOR_STATE_MAP.set(get_full_filename($selected), $module_editor?.getEditorState()); + EDITOR_STATE_MAP.set(filename, $module_editor?.getEditorState()); $output?.set($selected, $compile_options); @@ -212,8 +215,10 @@ file.source = event.detail.value; file.modified = true; + const idx = $files.findIndex((val) => get_full_filename(val) === $selected_name); + // @ts-ignore - $files[$selected_index] = file; + $files[idx] = file; return $files; }); @@ -233,15 +238,7 @@ async function go_to_warning_pos(item) { if (!item) return; - const match = /^(.+)\.(\w+)$/.exec(item.filename); - if (!match) return; // ??? - - const [, name, type] = match; - const file_index = $files.findIndex((file) => file.name === name && file.type === type); - - if (file_index === -1) return; - - await handle_select(file_index); + await handle_select(item.filename); $module_editor?.focus(); $module_editor?.setCursor(item.start.character); diff --git a/packages/repl/src/lib/types.d.ts b/packages/repl/src/lib/types.d.ts index 1ac2a320..456c5dbb 100644 --- a/packages/repl/src/lib/types.d.ts +++ b/packages/repl/src/lib/types.d.ts @@ -51,7 +51,7 @@ export type File = { export type ReplState = { files: File[]; - selected_index: number; + selected_name: string; selected: File | null; bundle: Bundle | null; bundler: import('./Bundler').default | null; @@ -64,7 +64,7 @@ export type ReplState = { export type ReplContext = { files: Writable; - selected_index: Writable; + selected_name: Writable; selected: Readable; bundle: Writable; bundler: Writable; @@ -78,7 +78,7 @@ export type ReplContext = { // Methods rebundle(): Promise; - handle_select(index: number): Promise; + handle_select(filename: string): Promise; handle_change( event: CustomEvent<{ value: string; From 097f212b80ac8f6eb3b3fa6c6729249a9b984004 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Tue, 27 Jun 2023 20:48:57 +0200 Subject: [PATCH 2/3] (fix) various issues about tab renaming (#497) --- .../src/lib/Input/ComponentSelector.svelte | 228 +++++++++- .../src/lib/Input/OldComponentSelector.svelte | 415 ------------------ packages/repl/src/lib/Repl.svelte | 2 +- .../repl/src/lib/workers/bundler/index.js | 5 +- 4 files changed, 222 insertions(+), 428 deletions(-) delete mode 100644 packages/repl/src/lib/Input/OldComponentSelector.svelte diff --git a/packages/repl/src/lib/Input/ComponentSelector.svelte b/packages/repl/src/lib/Input/ComponentSelector.svelte index 75d78066..9501cba1 100644 --- a/packages/repl/src/lib/Input/ComponentSelector.svelte +++ b/packages/repl/src/lib/Input/ComponentSelector.svelte @@ -1,9 +1,10 @@ @@ -49,20 +207,70 @@
{#each $files as file, index (file.name)} {@const filename = get_full_filename(file)} - - + {:else if filename === editing_name} + {@const editing_file = $files.find((file) => get_full_filename(file) === editing_name)} + + {#if editing_file} + + {input_value + (/\./.test(input_value) ? '' : `.${editing_file.type}`)} + + + + + e.key === 'Enter' && !is_file_name_used(editing_file) && e.currentTarget.blur()} + class:duplicate={is_file_name_used(editing_file)} + /> + {/if} + {:else} +
edit_tab(file)} + on:keyup={(e) => e.key === ' ' && edit_tab(file)} + > + {file.name}.{file.type}{#if show_modified && file.modified}*{/if} +
+ + remove(filename)} + on:keyup={(e) => e.key === ' ' && remove(filename)} + > + + + + + + {/if} +
{/each} - - {/if} - - - diff --git a/packages/repl/src/lib/Repl.svelte b/packages/repl/src/lib/Repl.svelte index 6acdcf3c..1b5a9b60 100644 --- a/packages/repl/src/lib/Repl.svelte +++ b/packages/repl/src/lib/Repl.svelte @@ -4,7 +4,7 @@ import { createEventDispatcher } from 'svelte'; import { derived, writable } from 'svelte/store'; import Bundler from './Bundler.js'; - import ComponentSelector from './Input/OldComponentSelector.svelte'; + import ComponentSelector from './Input/ComponentSelector.svelte'; import ModuleEditor from './Input/ModuleEditor.svelte'; import InputOutputToggle from './InputOutputToggle.svelte'; import Output from './Output/Output.svelte'; diff --git a/packages/repl/src/lib/workers/bundler/index.js b/packages/repl/src/lib/workers/bundler/index.js index 45447752..1c556091 100644 --- a/packages/repl/src/lib/workers/bundler/index.js +++ b/packages/repl/src/lib/workers/bundler/index.js @@ -275,7 +275,8 @@ async function get_bundle(uid, mode, cache, local_files_lookup) { if (importer && local_files_lookup.has(importer)) { // relative import in a REPL file // should've matched above otherwise importee doesn't exist - throw new Error(`Cannot find file "${importee}" imported by "${importer}" in the REPL`); + console.error(`Cannot find file "${importee}" imported by "${importer}" in the REPL`); + return; } else { // relative import in an external file const url = new URL(importee, importer).href; @@ -289,7 +290,7 @@ async function get_bundle(uid, mode, cache, local_files_lookup) { const match = /^((?:@[^/]+\/)?[^/]+)(\/.+)?$/.exec(importee); if (!match) { - throw new Error(`Invalid import "${importee}"`); + return console.error(`Invalid import "${importee}"`); } const pkg_name = match[1]; From 0b2970e8a2cab73213ef3b53182d561be100c31a Mon Sep 17 00:00:00 2001 From: Puru Vijay Date: Wed, 28 Jun 2023 00:23:53 +0530 Subject: [PATCH 3/3] Add changeset --- .changeset/bright-shoes-boil.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/bright-shoes-boil.md diff --git a/.changeset/bright-shoes-boil.md b/.changeset/bright-shoes-boil.md new file mode 100644 index 00000000..30412975 --- /dev/null +++ b/.changeset/bright-shoes-boil.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/repl': patch +--- + +Fix ComponentSelector