Tutorial/REPL tweaks (#2196)

* make dropdown more obviously interactive

* make component removal work in firefox

* dont recompile on component selection

* oops

* focus editor on selector blur, finesse styles

* fix input alignment on firefox
pull/2198/head
Rich Harris 7 years ago committed by GitHub
parent 00df135283
commit dd6a962f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -39,7 +39,9 @@
} }
code = new_code; code = new_code;
updating_externally = true;
if (editor) editor.setValue(code); if (editor) editor.setValue(code);
updating_externally = false;
} }
export function update(new_code) { export function update(new_code) {
@ -56,6 +58,10 @@
editor.refresh(); editor.refresh();
} }
export function focus() {
editor.focus();
}
const modes = { const modes = {
js: { js: {
name: 'javascript', name: 'javascript',
@ -73,7 +79,7 @@
const refs = {}; const refs = {};
let editor; let editor;
let updating = false; let updating_externally = false;
let marker; let marker;
let error_line; let error_line;
let destroyed = false; let destroyed = false;
@ -131,10 +137,6 @@
} }
}); });
beforeUpdate(() => {
updating = false;
});
function createEditor(mode) { function createEditor(mode) {
if (destroyed || !CodeMirror) return; if (destroyed || !CodeMirror) return;
@ -163,10 +165,9 @@
editor = CodeMirror.fromTextArea(refs.editor, opts); editor = CodeMirror.fromTextArea(refs.editor, opts);
editor.on('change', instance => { editor.on('change', instance => {
if (!updating) { if (!updating_externally) {
updating = true; const value = instance.getValue();
// code = instance.getValue(); dispatch('change', { value });
dispatch('change', { value: instance.getValue() });
} }
}); });

@ -5,7 +5,7 @@
export let handle_select; export let handle_select;
const { components, selected } = getContext('REPL'); const { components, selected, request_focus } = getContext('REPL');
let editing = null; let editing = null;
@ -28,7 +28,13 @@
if (match && match[2]) $selected.type = match[2]; if (match && match[2]) $selected.type = match[2];
editing = null; editing = null;
// re-select, in case the type changed
handle_select($selected);
components = components; // TODO necessary? components = components; // TODO necessary?
// focus the editor, but wait a beat (so key events aren't misdirected)
setTimeout(request_focus);
} }
function remove(component) { function remove(component) {
@ -43,7 +49,7 @@
console.error(`Could not find component! That's... odd`); console.error(`Could not find component! That's... odd`);
} }
selected.set($components[index] || $components[$components.length - 1]); handle_select($components[index] || $components[$components.length - 1]);
} }
} }
@ -70,7 +76,7 @@
}); });
components.update(components => components.concat(component)); components.update(components => components.concat(component));
selected.set(component); handle_select(component);
} }
</script> </script>
@ -83,7 +89,6 @@
.file-tabs { .file-tabs {
border: none; border: none;
padding: 0 0 0 5rem;
margin: 0; margin: 0;
white-space: nowrap; white-space: nowrap;
overflow-x: auto; overflow-x: auto;
@ -91,16 +96,21 @@
height: 10em; height: 10em;
} }
.file-tabs button { .file-tabs .button, .file-tabs button {
position: relative; position: relative;
display: inline-block;
font: 400 1.2rem/1.5 var(--font); font: 400 1.2rem/1.5 var(--font);
border-bottom: var(--border-w) solid transparent; border-bottom: var(--border-w) solid transparent;
padding: 1.2rem 1.2rem 0.8rem 0.5rem; padding: 1.2rem 1.4rem 0.8rem 0.8rem;
margin: 0 0.5rem 0 0; margin: 0;
color: #999; color: #999;
} }
.file-tabs button.active { .file-tabs .button:first-child {
padding-left: 1.2rem;
}
.file-tabs .button.active {
/* color: var(--second); */ /* color: var(--second); */
color: #333; color: #333;
border-bottom: var(--border-w) solid var(--prime); border-bottom: var(--border-w) solid var(--prime);
@ -119,26 +129,15 @@
input { input {
position: absolute; position: absolute;
width: 100%; width: 100%;
left: 0.5rem; left: 0.8rem;
top: 1.2rem; top: 1.2rem;
/* padding: 0 0.4rem; */
/* font-size: 1rem; */
font: 400 1.2rem/1.5 var(--font); font: 400 1.2rem/1.5 var(--font);
border: none; border: none;
color: var(--flash); color: var(--flash);
outline: none; outline: none;
line-height: 1;
background-color: transparent; background-color: transparent;
} }
.editable {
/* margin-right: 2.4rem; */
}
.uneditable {
/* padding-left: 1.2rem; */
}
.remove { .remove {
position: absolute; position: absolute;
display: none; display: none;
@ -155,36 +154,38 @@
color: var(--flash); color: var(--flash);
} }
.file-tabs button.active .editable { .file-tabs .button.active .editable {
cursor: text; cursor: text;
} }
.file-tabs button.active .remove { .file-tabs .button.active .remove {
display: inline-block; display: block;
} }
.add-new { .add-new {
position: absolute; position: absolute;
left: 0; left: 0;
top: 0; top: 0;
width: 5rem; padding: 1.2rem 1rem 0.8rem 0 !important;
height: 100%; height: 4.2rem;
text-align: center; text-align: center;
background-color: white; background-color: white;
} }
.add-new:hover { .add-new:hover {
color: var(--flash); color: var(--flash) !important;
} }
</style> </style>
<div class="component-selector"> <div class="component-selector">
{#if $components.length}
<div class="file-tabs" on:dblclick="{addNew}"> <div class="file-tabs" on:dblclick="{addNew}">
{#each $components as component} {#each $components as component}
<button <div
id={component.name} id={component.name}
class="button"
role="button"
class:active="{component === $selected}" class:active="{component === $selected}"
data-name={component.name}
on:click="{() => selectComponent(component)}" on:click="{() => selectComponent(component)}"
on:dblclick="{e => e.stopPropagation()}" on:dblclick="{e => e.stopPropagation()}"
> >
@ -198,9 +199,10 @@
<input <input
autofocus autofocus
spellcheck={false}
bind:value={editing.name} bind:value={editing.name}
on:focus={selectInput} on:focus={selectInput}
on:blur="{() => closeEdit()}" on:blur={closeEdit}
use:enter="{e => e.target.blur()}" use:enter="{e => e.target.blur()}"
> >
{:else} {:else}
@ -218,11 +220,12 @@
</span> </span>
{/if} {/if}
{/if} {/if}
</button>
{/each}
</div> </div>
{/each}
<button class="add-new" on:click={addNew} title="add new component"> <button class="add-new" on:click={addNew} title="add new component">
<Icon name="plus" /> <Icon name="plus" />
</button> </button>
</div> </div>
{/if}
</div>

@ -11,6 +11,10 @@
onMount(() => { onMount(() => {
register_module_editor(editor); register_module_editor(editor);
}); });
export function focus() {
editor.focus();
}
</script> </script>
<style> <style>

@ -100,7 +100,7 @@
position: relative; position: relative;
font: 400 1.2rem/1.5 var(--font); font: 400 1.2rem/1.5 var(--font);
border-bottom: var(--border-w) solid transparent; border-bottom: var(--border-w) solid transparent;
padding: 1.2rem 0.8rem 0.8rem 0.8rem; padding: 1.2rem 1.2rem 0.8rem 1.2rem;
color: #999; color: #999;
} }

@ -113,6 +113,10 @@
register_output(handlers) { register_output(handlers) {
output = handlers; output = handlers;
},
request_focus() {
module_editor.focus();
} }
}); });

@ -24,7 +24,6 @@
font-weight: 300; font-weight: 300;
font-size: var(--h6); font-size: var(--h6);
color: white; color: white;
cursor: pointer;
} }
a { a {
@ -54,6 +53,7 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
opacity: 0.0001; opacity: 0.0001;
cursor: pointer;
} }
</style> </style>

Loading…
Cancel
Save