mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 13:04:30 -08:00
refactor
This commit is contained in:
@@ -33,6 +33,7 @@ export class Files {
|
|||||||
|
|
||||||
|
|
||||||
render(state) {
|
render(state) {
|
||||||
|
this.file_to_el = new Map()
|
||||||
const file_actions = state.has_file_system_access
|
const file_actions = state.has_file_system_access
|
||||||
? el('div', 'file_actions',
|
? el('div', 'file_actions',
|
||||||
el('a', {
|
el('a', {
|
||||||
@@ -100,6 +101,13 @@ export class Files {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
render_current_module(current_module) {
|
||||||
|
this.current_file = current_module
|
||||||
|
this.active_el.querySelector('.file_title').classList.remove('active')
|
||||||
|
this.active_el = this.file_to_el.get(current_module)
|
||||||
|
this.active_el.querySelector('.file_title').classList.add('active')
|
||||||
|
}
|
||||||
|
|
||||||
render_select_entrypoint(file, state) {
|
render_select_entrypoint(file, state) {
|
||||||
if(!state.has_file_system_access || file.kind == 'directory') {
|
if(!state.has_file_system_access || file.kind == 'directory') {
|
||||||
return null
|
return null
|
||||||
@@ -149,9 +157,11 @@ export class Files {
|
|||||||
: file.children.map(c => this.render_file(c, state))
|
: file.children.map(c => this.render_file(c, state))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
this.file_to_el.set(file.path, result)
|
||||||
|
|
||||||
if(file.path == state.current_module) {
|
if(file.path == state.current_module) {
|
||||||
this.active_el = result
|
this.active_el = result
|
||||||
this.active_file = file
|
this.current_file = file.path
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@@ -159,39 +169,34 @@ export class Files {
|
|||||||
|
|
||||||
async create_file(is_dir) {
|
async create_file(is_dir) {
|
||||||
|
|
||||||
if(this.active_file == null) {
|
|
||||||
throw new Error('no active file')
|
|
||||||
}
|
|
||||||
|
|
||||||
let name = prompt(`Enter ${is_dir ? 'directory' : 'file'} name`)
|
let name = prompt(`Enter ${is_dir ? 'directory' : 'file'} name`)
|
||||||
if(name == null) {
|
if(name == null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let dir
|
|
||||||
|
|
||||||
const root = get_state().project_dir
|
const root = get_state().project_dir
|
||||||
|
|
||||||
if(this.active_file.path == '' /* scratch */) {
|
let dir, file
|
||||||
|
|
||||||
|
if(this.current_file == '' /* scratch */) {
|
||||||
// Create in root directory
|
// Create in root directory
|
||||||
dir = root
|
dir = root
|
||||||
} else {
|
} else {
|
||||||
if(this.active_file.kind == 'directory') {
|
const find_file_with_parent = (dir, parent) => {
|
||||||
dir = this.active_file
|
if(dir.path == this.current_file) {
|
||||||
} else {
|
return [dir, parent]
|
||||||
|
|
||||||
const find_parent = (dir, parent) => {
|
|
||||||
if(dir.path == this.active_file.path) {
|
|
||||||
return parent
|
|
||||||
}
|
}
|
||||||
if(dir.children == null) {
|
if(dir.children == null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return map_find(dir.children, c => find_parent(c, dir))
|
return map_find(dir.children, c => find_file_with_parent(c, dir))
|
||||||
}
|
}
|
||||||
|
|
||||||
dir = find_parent(root)
|
([file, dir] = find_file_with_parent(root))
|
||||||
|
|
||||||
|
if(file.kind == 'directory') {
|
||||||
|
dir = file
|
||||||
|
} else {
|
||||||
if(dir == null) {
|
if(dir == null) {
|
||||||
throw new Error('illegal state')
|
throw new Error('illegal state')
|
||||||
}
|
}
|
||||||
@@ -214,10 +219,7 @@ export class Files {
|
|||||||
|
|
||||||
on_click(e, file) {
|
on_click(e, file) {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
this.active_el.querySelector('.file_title').classList.remove('active')
|
this.render_current_module(file.path)
|
||||||
this.active_el = e.currentTarget.parentElement
|
|
||||||
e.currentTarget.classList.add('active')
|
|
||||||
this.active_file = file
|
|
||||||
|
|
||||||
if(file.kind != 'directory') {
|
if(file.kind != 'directory') {
|
||||||
if(get_state().has_file_system_access) {
|
if(get_state().has_file_system_access) {
|
||||||
|
|||||||
10
src/effects.js
vendored
10
src/effects.js
vendored
@@ -133,14 +133,14 @@ export const render_initial_state = (ui, state) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const apply_side_effects = (prev, next, command, ui) => {
|
export const apply_side_effects = (prev, next, command, ui) => {
|
||||||
if(
|
if(prev.project_dir != next.project_dir) {
|
||||||
prev.project_dir != next.project_dir
|
|
||||||
||
|
|
||||||
prev.current_module != next.current_module
|
|
||||||
) {
|
|
||||||
ui.files.render(next)
|
ui.files.render(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(prev.current_module != next.current_module) {
|
||||||
|
ui.files.render_current_module(next.current_module)
|
||||||
|
}
|
||||||
|
|
||||||
if(prev.current_module != next.current_module) {
|
if(prev.current_module != next.current_module) {
|
||||||
localStorage.current_module = next.current_module
|
localStorage.current_module = next.current_module
|
||||||
ui.render_current_module(next.current_module)
|
ui.render_current_module(next.current_module)
|
||||||
|
|||||||
Reference in New Issue
Block a user