Skip to content

Editing a file removes the selected class from the file in the filetree #169

Closed
@tomoam

Description

@tomoam

Describe the problem

When you edit a file in the editor, selected class is removed from the file in the filetree (only happens on first edit).

selected_class_is_removed.2.mov

This is caused by comparing file with $selected (i.e., these may refer to different objects with the same contents).

<div class="row" class:selected={file === $selected}>

When you open a tutorial page, deep copy of stub(file) is created...

/**
* @param {import('$lib/types').FileStub} stub
*/
function create_file(stub) {
// deep-copy stub so we can mutate it and not create a memory leak
stub = JSON.parse(JSON.stringify(stub));

...then, if you edit a file, change event will be dispatched, and file will be updated to deep copy of the one.

if (notify) {
stub.contents = contents;
dispatch('change', stub);
}

<Editor
stubs={$files}
selected={$selected}
read_only={$mobile}
on:change={update_stub}
/>

/**
* @param {CustomEvent<import('$lib/types').FileStub>} event
*/
function update_stub(event) {
const stub = event.detail;
const index = $files.findIndex((s) => s.name === stub.name);
$files[index] = stub;
adapter?.update([stub]).then((reload) => {
if (reload) {
schedule_iframe_reload();
}
});
update_complete_states([stub]);
}

Describe the proposed solution

Comparing with the respective name property instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions